<?header('Content-Type: text/html; charset=utf-8'); 


include(
"shared_inc/language.inc.php");
include(
"shared_inc/wiki_functions.inc.php");
$inc_dir "flinfo_inc/lang";

/* 

Flinfo 1.0
Written by Florian Straub  (flominator@gmx,net)

Uses PHPFlickr by Dan Coulter (dancoulter@users.sourceforge.net)
Project Homepage: http://www.phpflickr.com/
Sourceforge Project Page: http://www.sourceforge.net/projects/phpflickr/
Released under GNU Lesser General Public License (http://www.gnu.org/copyleft/lgpl.html)
For more information about the class and upcoming tools and applications using it,
visit http://www.phpflickr.com/ or http://www.sourceforge.net/projects/phpflickr/
 */
 
//Contact Flickr 
/*
                        @TODO REMOVE FROM HERE
*/
require_once("flinfo_inc/phpFlickr.php");
$f = new phpFlickr("5189417f2343916bc186d4fbf81b16b4");

//Check and set language
$user_lang read_language();
get_language($user_lang$inc_dir);

$id $_REQUEST['id']; 

if(
$id=="src")
{
    
print_script();
    die();
}

$admin=false;
if(
$_REQUEST['admin']!="")
{
    
$admin=true;
}

if(
$id!="")
{

    
    if(!
$photo $f->photos_getInfo($id))
    {
        echo 
str_replace('_PHOTOID_'$id$messages['error']);
        unset(
$id);
    }
    
    
$nsid $photo['owner']['nsid'];

    if(
$_REQUEST['raw']=='on')
    {
        echo 
create_textarea($photo);
        die();
    }    
    else
    {
        echo 
"<table><tr><td>";
    }
}

?>

   <h1>Flinfo</h1>
   <h2><? echo $messages['subtitle'?></h2> 
   <form>
    <? echo $messages['ID'?>: <input name="id" value="<? echo $id?>">
    <input type="submit" value="<? echo $messages['get_button'?>"><br>
    <input type="checkbox" name="raw"> <? echo $messages['raw'?>
    
    <? if($admin==true){
        echo 
"<input type='hidden' name='admin' value='$_REQUEST[admin]'>";
    }
        echo 
"<input type='hidden' name='user_lang' value='$_REQUEST[user_lang]'>";
    
?>
   </form>
   
<?
if($id!="")
{
    echo 
"</td>\n";
    
print_gui($photo);
}

//creates gui and textarea inside
function print_gui($photo)
{
    global 
$messages$f;
    
$imglink create_image_link($photo);
    
log_query($imglink);
    
    echo 
"<form id='upload' method=post enctype='multipart/form-data' action='http://commons.wikimedia.org/w/index.php?title=User%3AFlominator%2FFlinfo/Preview&action=submit' target='_blank' style='display:inline'>\n";
   
    echo 
"<input type='hidden' name='wpSummary' value='preview only, do not save changes'>\n";
    echo 
"<input type='hidden' value='"date "YmdHis" time() + (12 60 60) ) . "' name='wpStarttime'>\n";
    echo 
"<input type='hidden' value='' name='wpEdittime'>\n";

    echo 
"<td align='right' valign='top'>$imglink</td></tr><tr>\n";
    echo 
"  <td colspan=\"2\">\n";
    
$area create_textarea($photo);

    echo 
"   <textarea onchange='update_link()' name='wpTextbox1' cols='100' rows='".(count(explode("\n"$area))-1)."' onchange=\"update_link()\">\n";
    echo  
$area;
    echo 
"</textarea><br><br>";

    echo 
"
    <script>
    function update_link()
    {
            document.getElementById('form_link').href='"
.$messages['upload_form'].dest_file($photo).".jpg&wpUploadDescription='+ escape(document.getElementsByName('wpTextbox1')[0].value);
    }
    </script>
    "
;
    
    echo 
"<input type='submit' name='wpPreview' value='$messages[preview]'></form>";
    
    
//echo "<input type='button' onclick='update_link()' value='$messages[open_upload_form]'>";
    
echo "[<a id=\"form_link\" target=\"_blank\" href=\"".$messages['upload_form'].dest_file($photo).".jpg&wpUploadDescription=".urlencode($area)."\">$messages[open_upload_form]</a>]\n";
    
    echo 
"[<a target=\"_blank\" href=\"".$f->buildPhotoURL($photo"Original")."\">$messages[download_org]</a>]\n";
    
    
$size get_largest($photo['id']);
    echo 
"($size[width] x $size[height] $messages[pixels])";
}

function 
dest_file($photo)
{
    
$strDestFile "";
    if(
$_REQUEST['destfile']!="")
    {
        
$strDestFile $_REQUEST['destfile'];
    }
    else
    {
        
$strDestFile $photo['title'];
    }
    

    return 
urlencode(replace_brackets($strDestFile));
}

function 
replace_brackets($input)
{
    
$str_return str_replace("["""$input);
    
$str_return str_replace("]"""$str_return);
    return 
$str_return;
}

function 
create_image_link($photo)
{
    global 
$nsid$f;
    
$imglink "<a href=\"http://www.flickr.com/photos/$nsid/$photo[id]/\">";
    
$imglink.= " <img border='0' alt='$photo[title]' "."src=" $f->buildPhotoURL($photo"Square") . ">";
    
$imglink.= "</a>";
    return 
$imglink;
}

function 
create_textarea($photo)
{
    global 
$admin$id;
    
$lic_id $photo['license'];
    
//& causes problems in preview
    
$area str_replace("Book & Manuscript""Book and Manuscript"information($photo$lic_id));
    
$area.=get_geodata($id);
    
$area.= "\n=={{int:license}}==\n".commons_license(translate_license($lic_id));
    
$area.= "\n".flickerrreview($admin);
    
$area.= "\n".categories($photo['tags']['tag']);
    return 
$area;

}

function 
information ($photo$lic_id)
{
    global 
$nsid$messages;

    if(
$photo['owner']['realname']!="")
    {
        
$real_owner $photo['owner']['realname'];
    }
    else
    {
        
$real_owner $photo['owner']['username'];
    }
    
    
    if(
$photo['owner']['location']!="")
    {
        
$from ="from ".$photo['owner']['location'];
    }
    
    if((
real_stamp($photo['dates']['taken'])>0) && (real_stamp($photo['dates']['taken'])<=time()))
    {
        
$taken strftime($messages['created'], real_stamp($photo['dates']['taken']));
    }
    
$permission permission($lic_id$real_owner);
    
    
$desc description($photo);
    
    
$uploading_user $_REQUEST['uploading_user'];
    if(
$uploading_user!="")
    {
        return(
"{{Information
|Description=$desc
|Source=[http://www.flickr.com/photos/$nsid/$photo[id]/ "
replace_brackets($photo['title'])."]
* Uploaded by [[User:$uploading_user|$uploading_user]]
|Date=$taken
|Author=[http://www.flickr.com/people/$nsid $real_owner] $from
|Permission=$permission
|other_versions=
}}\n"
);
    }
    else
    {
    return(
"{{Information
|Description=$desc
|Source=[http://www.flickr.com/photos/$nsid/$photo[id]/ "
replace_brackets($photo['title'])."]
|Date=$taken
|Author=[http://www.flickr.com/people/$nsid $real_owner] $from
|Permission=$permission
|other_versions=
}}\n"
);
    }
}

function 
description($photo)
{
    if(
$_REQUEST['description']!="")
    {
        return 
$_REQUEST['description'];
    }

    
$desc convert_links(replace_brackets($photo['description']));
    
    if(
$desc=="")
    {
        
$desc $photo['title'];
    }

    return 
$desc;
}

function 
permission($lic_id$real_owner)
{
// links are to be retrieved from http://www.flickr.com/commons/usage/
    
global $id;
    
$permission_links['Australian War Memorial collection']='http://www.awm.gov.au/about/copyright/index.asp';
    
$permission_links['Bergen Public Library Norway']='http://nettbiblioteket.no/biblioteksinformasjon/flickr-commons.html';
    
$permission_links['Brooklyn Museum']='http://www.brooklynmuseum.org/flickr_commons.php';
    
$permission_links['Bibliothèque de Toulouse']='http://www.bibliothequedetoulouse.fr/fr/catalogues/02-mentions_legales.jsp';
    
$permission_links['Biblioteca de Arte / Art Library Fundação Calouste Gulbenkian']='http://www.biblarte.gulbenkian.pt/content.asp?cod=servico_leitura&menu=servicos&parent=servicos&lang=en';
    
$permission_links['DCPL Commons']='http://dclibrarylabs.org/projects/flickr-commons/';
    
$permission_links['Florida Memory']='http://www.floridamemory.com/disclaimer-flickr.cfm';
    
$permission_links['George Eastman House']='http://www.eastmanhouse.org/flickr/statement.php';
    
$permission_links['Getty Research Institute']='http://www.getty.edu/legal/copyright.html#flickr';
    
$permission_links['Imperial War Museum']='http://www.iwmcollections.org.uk/terms.php';
        
$permission_links['Library of the London School of Economics and Political Science']='http://www2.lse.ac.uk/library/archive/flickr_rights_statement.aspx';
    
$permission_links['Llyfrgell Genedlaethol Cymru  / The National Library of Wales']='http://www.llgc.org.uk/index.php?id=publicationrights#Flickr';
    
$permission_links['Musée McCord Museum']='http://www.mccord-museum.qc.ca/en/copyright.html#noknowncopyright';
    
$permission_links['Nationaal Archief']='http://beeldbank.nationaalarchief.nl/index.php?option=com_memorix&mrx_mod=content&mrx_item=auteursrechten';
    
$permission_links['Nantucket Historical Association']='http://www.nha.org/library/copyright-flickr.html';
    
$permission_links['National Galleries of Scotland']='http://www.nationalgalleries.org/aboutus/article/1:4946/295';
    
$permission_links['National Library NZ on The Commons']='http://www.natlib.govt.nz/collections/digital-collections/flickr';
    
$permission_links['National Maritime Museum']='http://www.nmm.ac.uk/copyright';
    
$permission_links['National Media Museum']='http://www.nationalmediamuseum.org.uk/Photography/copyright.asp';
    
$permission_links['New York Public Library']='http://www.nypl.org/legal/flickr.cfm';
    
$permission_links['Oregon State University Archives']='http://osulibrary.oregonstate.edu/archives/FlickrCommonsNoKnownCopyrightsRestrictions.html';
    
// might be obsolete:
    
$permission_links['Powerhouse Museum Collection']='http://www.powerhousemuseum.com/imageservices/?page_id=157';
    
$permission_links['Powerhouse Museum']='http://www.powerhousemuseum.com/imageservices/?page_id=157';
    
$permission_links['National Galleries of Scotland Commons']='http://www.nationalgalleries.org/aboutus/article/1:4946/295';
    
$permission_links['Smithsonian Institution']='http://www.si.edu/copyright/';
    
$permission_links['State Library of New South Wales']='http://www.sl.nsw.gov.au/about/policies/copyright.html';
    
$permission_links['State Library Queensland']='http://www.slq.qld.gov.au/home/copyright/#flickr';
    
$permission_links['Swedish National Heritage Board']='http://www.kms.raa.se/cocoon/bild/about.html';
    
$permission_links['The Field Museum Library']='http://www.fieldmuseum.org/research_collections/library/library_sites/photo_archives/flickr.htm';
    
$permission_links['Galt Museum & Archives on The Commons']='http://www.galtmuseum.com/copyright.htm';
    
$permission_links['The Library of Congress']='http://www.loc.gov/rr/print/195_copr.html#noknown';
    
$permission_links['The Library of Virginia']='http://www.lva.virginia.gov/copyright.htm';
    
$permission_links['The Swedish National Heritage Board']='http://www.kms.raa.se/cocoon/bild/about.html';
    
    
    if(
$lic_id==7//no known usage restrictions
    
{
        if(
$permission_links[$real_owner]=="")
        {
            if(
$_REQUEST['admin']!="Flominator")
            {
                
$mailaddy "flominator@gmx.net";
                
$text "Neue PD-Nutzung von $real_owner: http://wikipedia.ramselehof.de/flinfo.php?id=$id&admin=Flominator\n\n
http://www.flickr.com/commons/usage/ \n
                Gruß Flo"
;
                
mail($mailaddy"Wikipedia: Flinfo"$text);
            }
        }
        return 
$permission_links[$real_owner];
    }
    else
    {
        return 
"";
    }
}

function 
real_stamp ($date_string)
{
    
$date_string str_replace('-'' '$date_string);
    
$date_string str_replace(':'' '$date_string);
    
$parts explode(' '$date_string);
    
    
//Result is an array with 6 elements Y|M|D|h|m|s
    
return mktime($parts[3], $parts[4], $parts[5], $parts[1], $parts[2], $parts[0]);
}

function 
categories ($tags)
{
    
$cat_string "";
    
    if(
$_REQUEST['categories']!=""//tags from get parameters
    
{
        
$url_cats explode(';'$_REQUEST['categories']);
        foreach(
$url_cats AS $cat)
        {
             
$cat_string.=  "|Category:"strtoupper(substr($cat01)) . substr($cat1);
        }
    }
    else 
//flickr tags
    
{
        foreach(
$tags AS $tag)
        {
             
$cat_string.=  "|Category:"strtoupper(substr($tag['raw'], 01)) . substr($tag['raw'], 1);
        }
    }
    
    
//Remove the first pipe character
    
$valid_categories test_categoriessubstr$cat_string1) );
    
    if(
$valid_categories!="")
    {
        return 
$valid_categories;
    }
    else
    {
        return 
"{{subst:unc}}";
    }
}

function 
print_script()
{
    if(
$f fopen("flinfo.php""r"))
    {
        while(
$line fgets($f255))
        {
            
$file .= $line;
        }
        
fclose($f);
        echo 
"<pre>";
        echo 
highlight_string($file);
        echo 
"</pre>";
    }    
}

function 
log_query($photo_link)
{
    
$logfile "flinfo.htm";
    
    if(!
file_exists($logfile))
    {
        
$header '<table border="1" cellpadding="10" cellspaing="10">';
        
//$header = '<tr><td>Date</td><td>Time</td><td>IP</td><td>Picture</td></tr>';
        
    
}
    
    if(
$file fopen($logfile"a"))
    {
        
        
fputs($file"$header<tr>");
        
fputs($file"<td>".strftime("%Y-%m-%d")."</td>");
        
fputs($file"<td>".strftime("%H:%M")."</td>");
        
fputs($file"<td>".$_SERVER['REMOTE_ADDR']."</td>");
        
fputs($file"<td>$photo_link</td>");
        
fputs($file"</tr>");
        
fclose($file);
    }
    
}

function 
translate_license($lic_id)
{
    global 
$f$photo;
    
    if(
$lic_id!=7//no PD from LOC
    
{
        
$licenses $f->photos_licenses_getInfo();
        
        
        foreach(
$licenses as $license)
        {
            if(
$license['id']==$lic_id)
            {
                return 
$license['name'];
            }
        }
        return;
    }
    else
    {
        if((
stristr($photo['description'], "hdl.loc.gov/loc.pnp/fsac")) OR (stristr($photo['description'], "hdl.loc.gov/loc.pnp/pp.fsac")))
        {
            return 
"FSA";
        }
        
        if((
stristr($photo['description'], "hdl.loc.gov/loc.pnp/ggbain")) OR (stristr($photo['description'], "hdl.loc.gov/loc.pnp/pp.ggbain")))
        {
            return 
"Bain";
        }
        
        if(
stristr($photo['description'], "www.powerhousemuseum.com/collection/database/?irn"))
        {
            return 
"Powerhouse";
        }

        if(
stristr($photo['description'], "brooklynmuseum.org"))
        {
            return 
"Brooklyn Museum";
        }        
        if(
stristr($photo['description'], "Fonds Trutat"))
        {
            return 
"Fonds Trutat";
        }    
    
    
        return 
'FlickrCommons';
    }
    
}

function 
commons_license ($name)
{
    global 
$messages$photo$id$admin;
    
//echo $name;

    
switch($name)
    {
        case 
"Attribution-ShareAlike License"
        {
            return 
"{{cc-by-sa-2.0}}";
        }
        case 
"Attribution License":
        {
            return 
"{{cc-by-2.0}}";
        }
        case 
"United States Government Work":
        {
            return 
"{{PD-USGov-POTUS}}";
        }
        case 
"Bain":
        {    
            return 
"{{PD-Bain}}\n".tag_with_loc($photo"ggbain");
        }
        case 
"FSA":
        {
            return 
"{{PD-USGov-FSA}}\n".tag_with_loc($photo"fsac");
        }
        
        case 
"Powerhouse":
        {
            return 
"{{PD-Australia}}";
        }
        
        case 
"Fonds Trutat":
        {
            return 
"{{PD-old}}";
        }
        
        case 
"Brooklyn Museum":
        {
            return 
"{{Flickr-Brooklyn-Museum-image}}\n{{Flickr-no known copyright restrictions}}";
        }
    
        case 
"FlickrCommons":
        {
            
//return "{{PD-because|the uploader at Flickr said that there are 'No known copyright restrictions'}}";
            
return "{{Flickr-no known copyright restrictions}}";
        }
        
        default: 
        {
                if(
$admin==false)
                {
                    die(
$messages['copyrighted']);
                }
                else
                {
                    
$copyvio_reason str_replace('_LICENSE_NAME_'$name$messages['copyvio_reason']);
                    return 
'{{'.$messages['copyvio_template']."|$copyvio_reason --~~~~}}";
                }
        }
    }
}

function 
tag_with_loc ($photo$collection)
{
    
$loc_url_beginns_with 'hdl.loc.gov/loc.pnp/'.$collection.'.';
    
$beginning_of_loc_url strpos($photo['description'], $loc_url_beginns_with) + strlen($loc_url_beginns_with);
    
$end_of_loc_url strpos($photo['description'], "\""$beginning_of_loc_url);
    
$loc_url_len =     $end_of_loc_url $beginning_of_loc_url;
    
$loc_id substr($photo['description'], $beginning_of_loc_url$loc_url_len);
    return 
"{{LOC-image|id=" $collection '.' $loc_id "}}";
}

function 
flickerrreview($is_admin)
{
    if(
$is_admin)
    {
        return 
"{{flickrreview|$_REQUEST[admin]|{{subst:CURRENTMONTHNAME}} {{subst:CURRENTDAY}}, {{subst:CURRENTYEAR}}}}";
    }
    else
    {
        return 
'{{flickrreview}}';
    }

}

function 
analyse_array($arr)
{
    echo 
"<dir>";
    echo 
count($arr)." Element(e)<br>";
    
$keys array_keys($arr);
    
    foreach(
$keys as $key)
    {
        echo 
"Array[".$key."]=".$arr[$key]."<br>";
        if(
is_array($arr[$key]))
        {
            
analyse_array($arr[$key]);
        }
    }
    echo 
"</dir>";
}

function 
get_largest($id)
{
    global 
$f;
    
$sizes=$f->photos_getSizes($id);
    
$index count($sizes)-1;
    return 
$sizes[$index];
}

function 
test_categories($cats)
{
    
$existing_cats "";
    
$url =  "http://commons.wikimedia.org/w/api.php?action=query&format=php&prop=revisions&titles=";
    
$url.= urlencode($cats);
    
$arr unserialize file_get_contents $url) )  ;
    
    if(@
$keys array_keys($arr['query']['pages']))
    {
        
        foreach(
$keys as $key)
        {
            
//Category doesn't exist
            //if($arr['query']['pages'][$key]['missing']=="")  - no longer works
            
if($key>0)
            {
                
//checking if the category is a redirect
                
$cat $arr['query']['pages'][$key]['title'];
                
$url "http://commons.wikimedia.org/w/index.php?title=".urlencode($cat)."&action=raw";
                
                
$cat_content removeheaders(get_request("commons.wikimedia.org"$url));
                if(
stristr(strtolower($cat_content), "{{category redirect|"))
                {
                    
$cat="Category:" substr($cat_content20, (strpos($cat_content"}")-20));
                }
                
$existing_cats.= '[['$cat "]]\n";
            }    
        }
    }
    return 
$existing_cats;
}

function 
convert_links ($desc)
{
    
$desc str_replace('|''-'$desc);
    
$desc str_replace("\n\n"."This image was uploaded at high resolution and it helps us to know how our visitors work with our images, so if you use it, we'd love to know how!  Drop us a line by leaving a comment here or email our archivist: library@brooklynmuseum.org"""$desc);
    
//HTML link becomes external wiki link
    
$desc preg_replace('!<a\s[^>]*href\s*=\s*["\'](.*?)["\'][^>]*>(.*?)</a>!ims''[\1 \2]'$desc);
    
    
//wiki link with same alias as link becomes plain link -
    
$desc preg_replace('!\[([^\]\s]+)\s+\1\]!''[\1 \2]'$desc);
    
    
//str_replace("    [http://joeshlabotnik.livejournal.com/25237.html joeshlabotnik.livejournal.com/25237.html]
/*
    $desc = str_replace("<a href=\"", "[", $desc);
    $desc = str_replace("</a>", "]", $desc);
    $desc = str_replace(">", " ", $desc);
*/    
    
$desc str_replace('<strong>'"'''"$desc);
    
$desc str_replace('</strong>'"'''"$desc);
    
$desc str_replace('<b>'"'''"$desc);
    
$desc str_replace('</b>'"'''"$desc);
    
$desc str_replace('<i>'"''"$desc);
    
$desc str_replace('</i>'"''"$desc);
    return 
$desc;
}

function 
get_geodata($id)
{
    global 
$f;
    if(
$geo $f->photos_geo_getLocation($id))
    {
        return 
"{{Location dec|".$geo['location']['latitude']."|".$geo['location']['longitude']."|}}";
    }
}

?>
  </td>
 </tr>
</table>
<hr>
<a href='<? echo $messages['manual_link'?>'><? echo $messages['manual'?></a> - 
<a href='<? echo $messages['contact_link'?>'><? echo $messages['contact'?></a> - 
<a href="http://de.wikipedia.org/wiki/Benutzer:Flominator">by Flominator</a>
<!--[<a href="?id=src">View Source</a>]-->
</div>
1