Changeset 16071


Ignore:
Timestamp:
06/27/12 01:53:21 (7 years ago)
Author:
mistic100
Message:

code cleanup, don't ask username anymore (prohibit importing photos from another account)

Location:
extensions/flickr2piwigo
Files:
1 added
11 edited

Legend:

Unmodified
Added
Removed
  • extensions/flickr2piwigo/admin/config.php

    r16063 r16071  
    77    'api_key' => trim($_POST['api_key']), 
    88    'secret_key' => trim($_POST['secret_key']), 
    9     'username' => trim($_POST['username']), 
    109    ); 
    1110  unset($_SESSION['phpFlickr_auth_token']); 
     
    1413 
    1514 
    16 $template->assign('flickr2piwigo', $conf['flickr2piwigo']); 
    17  
    1815$template->assign(array( 
     16  'flickr2piwigo' => $conf['flickr2piwigo'], 
    1917  'FLICKR_HELP_CONTENT' => load_language('help_api_key.html', FLICKR_PATH, array('return'=>true)), 
    20   'FLICKR_CALLBACK' => get_absolute_root_url().FLICKR_ADMIN.'-import', 
     18  'FLICKR_CALLBACK' => get_absolute_root_url() . FLICKR_ADMIN . '-import', 
    2119  )); 
    2220 
    2321 
    24 $template->set_filename('flickr2piwigo', dirname(__FILE__).'/template/config.tpl'); 
    25  
     22$template->set_filename('flickr2piwigo', dirname(__FILE__) . '/template/config.tpl'); 
    2623 
    2724?> 
  • extensions/flickr2piwigo/admin/import.php

    r16070 r16071  
    33 
    44// check API parameters and connect to flickr 
    5 if (empty($conf['flickr2piwigo']['api_key']) or empty($conf['flickr2piwigo']['secret_key']) or empty($conf['flickr2piwigo']['username'])) 
     5if ( empty($conf['flickr2piwigo']['api_key']) or empty($conf['flickr2piwigo']['secret_key']) ) 
    66{ 
    77  array_push($page['warnings'], l10n('Please fill your API keys on the configuration tab')); 
     
    1616   
    1717  // must authenticate 
    18   if (empty($_SESSION['phpFlickr_auth_token']) and @$_GET['action']!='login') 
     18  $u = $flickr->test_login(); 
     19  if ( ($u === false or empty($_SESSION['phpFlickr_auth_token'])) and @$_GET['action']!='login' ) 
    1920  { 
    2021    $_GET['action'] = 'init_login'; 
    2122  } 
    22   else 
    23   { 
    24     // get user id 
    25     $u = $flickr->people_findByUsername($conf['flickr2piwigo']['username']); 
    26     if ($u === false) 
    27     { 
    28       array_push($page['errors'], l10n('Unknown username, please verify your configuration')); 
    29       $_GET['action'] = 'error'; 
    30     } 
    31   } 
    32    
    3323   
    3424  // generate token after authentication 
     
    4838  case 'init_login': 
    4939  { 
    50     $template->assign('flickr_login', FLICKR_ADMIN.'-import&action=login'); 
     40    $template->assign('flickr_login', FLICKR_ADMIN . '-import&action=login'); 
    5141    break; 
    5242  } 
     
    6353  { 
    6454    $_SESSION['page_infos'][] = l10n('Successfully logued to you Flickr account'); 
    65     redirect(FLICKR_ADMIN.'-import'); 
     55    redirect(FLICKR_ADMIN . '-import'); 
    6656    break; 
    6757  } 
     
    7262    unset($_SESSION['phpFlickr_auth_token']); 
    7363    $_SESSION['page_infos'][] = l10n('Logued out'); 
    74     redirect(FLICKR_ADMIN.'-import'); 
     64    redirect(FLICKR_ADMIN . '-import'); 
    7565    break; 
    7666  } 
     
    8171    $u = $flickr->people_getInfo($u['id']); 
    8272    $template->assign(array( 
    83       'username' => $conf['flickr2piwigo']['username'], 
    84       'profile_url' => $u['photosurl'], 
    85       'logout_url' => FLICKR_ADMIN.'-import&action=logout', 
    86       'list_albums_url' => FLICKR_ADMIN.'-import&action=list_albums', 
    87       'import_all_url' => FLICKR_ADMIN.'-import&action=list_all', 
     73      'username' => $u['username'], 
     74      'profile_url' => $u['profileurl'], 
     75      'logout_url' => FLICKR_ADMIN . '-import&action=logout', 
     76      'list_albums_url' => FLICKR_ADMIN . '-import&action=list_albums', 
     77      'import_all_url' => FLICKR_ADMIN . '-import&action=list_all', 
    8878      )); 
    8979    break; 
     
    10090    foreach ($albums as &$album) 
    10191    { 
    102       //$album['U_IMPORT_ALL'] = FLICKR_ADMIN.'-import&action=import_album&album='.$album['id']; 
    103       $album['U_LIST'] = FLICKR_ADMIN.'-import&action=list_photos&album='.$album['id']; 
     92      $album['U_LIST'] = FLICKR_ADMIN . '-import&action=list_photos&album='.$album['id']; 
    10493    } 
    10594    unset($album); 
     
    114103        'description' => null, 
    115104        'photos' => $wo_albums['photos']['total'], 
    116         //'U_IMPORT_ALL' => FLICKR_ADMIN.'-import&action=import_album&album='.$album['id'], 
    117         'U_LIST' => FLICKR_ADMIN.'-import&action=list_photos&album=not_in_set', 
     105        'U_LIST' => FLICKR_ADMIN . '-import&action=list_photos&album=not_in_set', 
    118106        )); 
    119107    } 
     
    129117  case 'list_photos': 
    130118  { 
     119    $self_url = FLICKR_ADMIN . '-import&action=list_photos&album='.$_GET['album']; 
     120    $flickr_prefix = 'flickr-'.$u['username'].'-'; 
     121    $flickr_root_url = $flickr->urls_getUserPhotos($u['id']); 
     122     
     123    // pagination 
    131124    if (isset($_GET['start']))   $page['start'] = intval($_GET['start']); 
    132125    else                         $page['start'] = 0; 
     
    134127    else                         $page['display'] = 20; 
    135128     
    136     $self_url = FLICKR_ADMIN.'-import&action=list_photos&album='.$_GET['album']; 
    137     $flickr_prefix = 'flickr-'.$conf['flickr2piwigo']['username'].'-'; 
    138     $flickr_root_url = $flickr->urls_getUserPhotos($u['id']); 
     129    // get photos 
     130    if ($_GET['album'] == 'not_in_set') 
     131    { 
     132      $all_photos = $flickr->photos_getNotInSet(NULL, NULL, NULL, NULL, 'photos', NULL, NULL, 500); 
     133      $all_photos = $all_photos['photos']['photo']; 
     134    } 
     135    else 
     136    { 
     137      $all_photos = $flickr->photosets_getPhotos($_GET['album'], NULL, NULL, 500, NULL, 'photos'); 
     138      $all_photos = $all_photos['photoset']['photo']; 
     139    } 
    139140     
    140141    // get existing photos 
     
    147148    $existing_photos = array_map(create_function('$p', '$p=preg_replace("#^'.$flickr_prefix.'([0-9]+)\.([a-z]{3,4})$#i", "$1", $p); return $p;'), $existing_photos); 
    148149     
    149     // get photos 
    150     if ($_GET['album'] == 'not_in_set') 
    151     { 
    152       $all_photos = $flickr->photos_getNotInSet(NULL, NULL, NULL, NULL, 'photos', NULL, NULL, 500); 
    153       $all_photos = $all_photos['photos']['photo']; 
    154     } 
    155     else 
    156     { 
    157       $all_photos = $flickr->photosets_getPhotos($_GET['album'], NULL, NULL, 500, NULL, 'photos'); 
    158       $all_photos = $all_photos['photoset']['photo']; 
    159     } 
    160      
    161     // remove existing 
     150    // remove existing photos 
    162151    $duplicates = 0; 
    163152    foreach ($all_photos as $i => $photo) 
     
    169158      } 
    170159    } 
     160     
    171161    if ($duplicates>0) 
    172162    { 
     
    214204    break; 
    215205  } 
    216      
     206   
     207  // list all photos of the user 
    217208  case 'list_all': 
    218209  { 
    219     $flickr_prefix = 'flickr-'.$conf['flickr2piwigo']['username'].'-'; 
     210    $flickr_prefix = 'flickr-'.$u['username'].'-'; 
    220211     
    221212    // get all photos in all albums 
     
    246237    $existing_photos = array_map(create_function('$p', '$p=preg_replace("#^'.$flickr_prefix.'([0-9]+)\.([a-z]{3,4})$#i", "$1", $p); return $p;'), $existing_photos); 
    247238     
    248     // remove duplicates 
     239    // remove existing photos 
    249240    $duplicates = 0; 
    250241    foreach ($all_photos as $id => &$photo) 
     
    264255    } 
    265256    unset($photo); 
     257    $all_photos = array_values($all_photos); 
     258     
    266259    if ($duplicates>0) 
    267260    { 
    268261      array_push($page['infos'], l10n_dec('%d picture is not displayed because already existing in the database.', '%d pictures are not displayed because already existing in the database.', $duplicates)); 
    269262    } 
    270     $all_photos = array_values($all_photos); 
    271263     
    272264    $template->assign(array( 
    273265      'nb_elements' => count($all_photos), 
    274266      'all_elements' => json_encode($all_photos), 
    275       'F_ACTION' => FLICKR_ADMIN.'-import&action=import_set', 
     267      'F_ACTION' => FLICKR_ADMIN . '-import&action=import_set', 
    276268      )); 
    277269       
     
    293285      $_SESSION['page_infos'][] = sprintf(l10n('%d pictures imported'), $_POST['done']); 
    294286    } 
    295     redirect(FLICKR_ADMIN.'-import'); 
     287    redirect(FLICKR_ADMIN . '-import'); 
    296288  } 
    297289} 
    298290 
     291 
    299292$template->assign('ACTION', $_GET['action']); 
    300293 
    301 $template->set_filename('flickr2piwigo', dirname(__FILE__).'/template/import.tpl'); 
     294$template->set_filename('flickr2piwigo', dirname(__FILE__) . '/template/import.tpl'); 
    302295 
    303296?> 
  • extensions/flickr2piwigo/admin/template/config.tpl

    r16063 r16071  
    2323      </label> 
    2424    </li> 
    25  
    26     <li> 
    27       <label> 
    28         <span class="property">{'Username'|@translate}</span> 
    29         <input type="text" name="username" value="{$flickr2piwigo.username}" size="20"></label> 
    30       </label> 
    31     </li> 
    3225  </ul> 
    3326</fieldset> 
  • extensions/flickr2piwigo/admin/template/import.list_photos.tpl

    r16063 r16071  
    330330  /* pagination loader */ 
    331331  jQuery('#navigation a').click(function() { 
    332     $("#loader_display").fadeIn() 
     332    $("#loader_display").fadeIn(); 
    333333  }); 
    334334}); 
  • extensions/flickr2piwigo/admin/template/import.tpl

    r16064 r16071  
    55</div> 
    66 
     7{* <!-- LOGIN --> *} 
    78{if $ACTION == 'init_login'} 
    89<p><input type="submit" onClick="javascript:window.location.href ='{$flickr_login}';" value="{'Login'|@translate}"></p> 
    910 
     11{* <!-- MAIN MENU --> *} 
    1012{elseif $ACTION == 'main'} 
     13{footer_script}{literal} 
     14jQuery('input[type="submit"]').click(function() { 
     15  window.location.href = $(this).attr("data"); 
     16}); 
     17jQuery('.load').click(function() { 
     18  $("#loader_import").fadeIn(); 
     19}); 
     20{/literal}{/footer_script} 
     21 
    1122<p> 
    1223  <b>{'Logued as'|@translate}</b> : <a href="{$profile_url}" target="_blank">{$username}</a><br><br> 
    13   <input type="submit" onClick="javascript:window.location.href ='{$logout_url}';" value="{'Logout'|@translate}"> 
     24  <input type="submit" data="{$logout_url}" value="{'Logout'|@translate}"> 
    1425</p> 
    1526<br> 
    1627<p> 
    17   <input type="submit" onClick="javascript:window.location.href ='{$list_albums_url}';" value="{'List my albums'|@translate}"> 
    18   <input type="submit" onClick="javascript:window.location.href ='{$import_all_url}';" value="{'Import all my pictures'|@translate}"> 
     28  <input type="submit" data="{$list_albums_url}" class="load" value="{'List my albums'|@translate}"> 
     29  <input type="submit" data="{$import_all_url}" class="load" value="{'Import all my pictures'|@translate}"> 
     30  <br> 
     31  <span id="loader_import" style="display:none;"><img src="admin/themes/default/images/ajax-loader.gif"> <i>{'Processing...'|@translate}</i></span> 
    1932</p> 
    2033 
     34{* <!-- ALBUMS LIST --> *} 
    2135{elseif $ACTION == 'list_albums'} 
     36{footer_script}{literal} 
     37jQuery('.load').click(function() { 
     38  $("#loader_import").fadeIn(); 
     39}); 
     40{/literal}{/footer_script} 
     41 
    2242<h3>{'%d albums'|@translate|@sprintf:$total_albums}</h3> 
    2343<ul id="albumsList"> 
    2444{foreach from=$albums item=album} 
    2545  <li {if $album.id == "not_in_set"}class="not_in_set"{/if}> 
    26     <b><a href="{$album.U_LIST}">{$album.title}</a></b> <i>{'(%d photos)'|@translate|@sprintf:$album.photos}</i> {if $album.description}- {$album.description|@truncate:100}{/if} 
    27     {*- <a href="{$album.U_IMPORT_ALL}">{'Import all pictures of this album'|@translate}</a>*} 
     46    <b><a href="{$album.U_LIST}" class="load">{$album.title}</a></b> <i>{'(%d photos)'|@translate|@sprintf:$album.photos}</i>  
     47    {if $album.description}- {$album.description|@truncate:100}{/if} 
    2848  </li> 
    2949{/foreach} 
    3050</ul> 
     51<span id="loader_import" style="display:none;"><img src="admin/themes/default/images/ajax-loader.gif"> <i>{'Processing...'|@translate}</i></span> 
    3152 
     53{* <!-- PHOTOS LIST --> *} 
    3254{elseif $ACTION == 'list_photos'} 
    3355{include file=$FLICKR_ABS_PATH|@cat:'admin/template/import.list_photos.tpl'} 
    3456 
     57{* <!-- IMPORT ALL --> *} 
    3558{elseif $ACTION == 'list_all'} 
    3659{include file=$FLICKR_ABS_PATH|@cat:'admin/template/import.list_all.tpl'} 
  • extensions/flickr2piwigo/language/en_UK/help_api_key.html

    r16063 r16071  
    88  <li>Click <b>Edit auth flow for this app</b></li> 
    99  <li>Enter a desciption again, past in <b>Callback URL</b> the above URL and click <b>Save changes</b></li> 
    10   <li>Don't forget to write your exact username in Flickr2Piwigo configuration</li> 
    1110  <li>You're done!</li> 
    1211</ol> 
  • extensions/flickr2piwigo/language/en_UK/plugin.lang.php

    r16063 r16071  
    2525$lang['(%d photos)'] = '(%d photos)'; 
    2626$lang['%d albums'] = '%d albums'; 
    27 //$lang['Import all pictures of this album'] = 'Import all pictures of this album'; 
    2827$lang['List pictures of this album'] = 'List pictures of this album'; 
    2928$lang['Begin transfert'] = 'Begin transfert'; 
  • extensions/flickr2piwigo/language/fr_FR/help_api_key.html

    r16063 r16071  
    88  <li>Cliquez sur <b>Modifier le flux d'authentification</b></li> 
    99  <li>Entrer à nouveau une description, collez dans <b>URL de rappel</b> l'URL ci-dessus et cliquez sur <b>Save changes</b></li> 
    10   <li>N'oubliez pas de renseigner votre nom d'utilisateur exact dans la configuration de Flickr2Piwigo</li> 
    1110  <li>Terminé!</li> 
    1211</ol> 
  • extensions/flickr2piwigo/language/fr_FR/plugin.lang.php

    r16063 r16071  
    2525$lang['(%d photos)'] = '(%d photos)'; 
    2626$lang['%d albums'] = '%d albums'; 
    27 //$lang['Import all pictures of this album'] = 'Importer toutes les photos de cet album'; 
    2827$lang['List pictures of this album'] = 'Lister les photos de cet album'; 
    2928$lang['Begin transfert'] = 'Commencer les transfert'; 
  • extensions/flickr2piwigo/main.inc.php

    r16063 r16071  
    1717define('FLICKR_FS_CACHE', $conf['data_location'].'flickr_cache/'); 
    1818 
    19 add_event_handler('get_admin_plugin_menu_links', 'flickr2_admin_menu'); 
    2019 
    21 function flickr2_admin_menu($menu)  
     20add_event_handler('get_admin_plugin_menu_links', 'flickr_admin_menu'); 
     21 
     22function flickr_admin_menu($menu)  
    2223{ 
    2324  array_push($menu, array( 
     
    2829} 
    2930 
    30 add_event_handler('ws_add_methods', 'flickr2_add_ws_method'); 
    3131 
    32 function flickr2_add_ws_method($arr) 
    33 { 
    34   $service = &$arr[0]; 
    35    
    36   $service->addMethod( 
    37     'pwg.images.addFlickr', 
    38     'ws_images_addFlickr', 
    39     array( 
    40       'category' => array('default' => null),    
    41       'id' => array('default' => null), 
    42       'fills' => array('default' =>null), 
    43       ), 
    44     'Used by Flickr2Piwigo, fills € (fill_name,fill_posted,fill_taken,fill_author,fill_tags)' 
    45     ); 
    46 } 
     32include_once(FLICKR_PATH . 'include/ws_functions.inc.php'); 
    4733 
    48 function ws_images_addFlickr($photo, &$service) 
    49 { 
    50   global $conf; 
    51    
    52   if (!is_admin()) 
    53   { 
    54     return new PwgError(403, 'Forbidden'); 
    55   } 
    56    
    57   $conf['flickr2piwigo'] = unserialize($conf['flickr2piwigo']); 
    58    
    59   if (empty($conf['flickr2piwigo']['api_key']) or empty($conf['flickr2piwigo']['secret_key']) or empty($conf['flickr2piwigo']['username'])) 
    60   { 
    61     return new PwgError(500, l10n('Please fill your API keys on the configuration tab')); 
    62   } 
    63    
    64   if (empty($_SESSION['phpFlickr_auth_token'])) 
    65   { 
    66     return new PwgError(403, l10n('API not authenticated')); 
    67   } 
    68    
    69   // category 
    70   if (!preg_match('#^[0-9]+$#', $photo['category'])) 
    71   { 
    72     $categories_names = explode(',', $photo['category']); 
    73      
    74     $photo['category'] = array(); 
    75     foreach ($categories_names as $category_name) 
    76     { 
    77       $query = ' 
    78 SELECT id FROM '.CATEGORIES_TABLE.' 
    79   WHERE LOWER(name) = "'.strtolower($category_name).'" 
    80 ;'; 
    81       $result = pwg_query($query); 
    82        
    83       if (pwg_db_num_rows($result)) 
    84       { 
    85         list($cat_id) = pwg_db_fetch_row($result); 
    86         array_push($photo['category'], $cat_id); 
    87       } 
    88       else 
    89       { 
    90         include_once(PHPWG_ROOT_PATH . 'admin/include/functions.php'); 
    91         $cat = create_virtual_category($category_name); 
    92         array_push($photo['category'], $cat_id['id']); 
    93       } 
    94     } 
    95   } 
    96   else 
    97   { 
    98     $photo['category'] = array($photo['category']); 
    99   } 
    100    
    101   // init flickr API 
    102   include_once(FLICKR_PATH . 'include/phpFlickr/phpFlickr.php'); 
    103   $flickr = new phpFlickr($conf['flickr2piwigo']['api_key'], $conf['flickr2piwigo']['secret_key']); 
    104   $flickr->enableCache('fs', FLICKR_FS_CACHE); 
    105    
    106   // photos infos 
    107   $photo_f = $flickr->photos_getInfo($photo['id']); 
    108   $photo = array_merge($photo, $photo_f['photo']); 
    109   $photo['url'] = $flickr->get_biggest_size($photo['id'], 'original'); 
    110   $photo['path'] = FLICKR_FS_CACHE . 'flickr-'.$conf['flickr2piwigo']['username'].'-'.$photo['id'].'.'.get_extension($photo['url']); 
    111    
    112   // copy file 
    113   $file = fopen($photo['url'], "rb"); 
    114   $newf = fopen($photo['path'], "wb"); 
    115   while (!feof($file)) 
    116   { 
    117     fwrite($newf, fread($file, 1024 * 8 ), 1024 * 8 ); 
    118   } 
    119   fclose($file); 
    120   fclose($newf); 
    121    
    122   // add to database 
    123   include_once(PHPWG_ROOT_PATH . 'admin/include/functions_upload.inc.php'); 
    124   $photo['image_id'] = add_uploaded_file($photo['path'], basename($photo['path']), $photo['category']); 
    125    
    126   // do some updates 
    127   if (!empty($photo['fills'])) 
    128   { 
    129     $photo['fills'] = rtrim($photo['fills'], ','); 
    130     $photo['fills'] = explode(',', $photo['fills']); 
    131    
    132     $updates = array(); 
    133     if (in_array('fill_name', $photo['fills']))   $updates['name'] = $photo['title'];  
    134     if (in_array('fill_posted', $photo['fills'])) $updates['date_available'] = date('Y-d-m H:i:s', $photo['dates']['posted']); 
    135     if (in_array('fill_taken', $photo['fills']))  $updates['date_creation'] = $photo['dates']['taken']; 
    136     if (in_array('fill_author', $photo['fills'])) $updates['author'] = $conf['flickr2piwigo']['username']; 
    137      
    138     if (count($updates)) 
    139     { 
    140       single_update( 
    141         IMAGES_TABLE, 
    142         $updates, 
    143         array('id' => $photo['image_id']) 
    144         ); 
    145     } 
    146      
    147     if ( !empty($photo['tags']['tag']) and in_array('fill_tags', $photo['fills']) ) 
    148     { 
    149       $raw_tags = array_map(create_function('$t', 'return $t["_content"];'), $photo['tags']['tag']); 
    150       $raw_tags = implode(',', $raw_tags); 
    151       set_tags(get_tag_ids($raw_tags), $photo['image_id']); 
    152     } 
    153   } 
    154    
    155   return sprintf(l10n('%s imported'), $photo['title']); 
    156 } 
     34add_event_handler('ws_add_methods', 'flickr_add_ws_method'); 
    15735 
    15836?> 
  • extensions/flickr2piwigo/maintain.inc.php

    r16070 r16071  
    77    'api_key' => null, 
    88    'secret_key' => null, 
    9     'username' => null, 
    109    )) 
    1110  ); 
    12    
     11 
    1312 
    1413function plugin_install()  
    1514{ 
    1615  global $conf; 
     16   
    1717  conf_update_param('flickr2piwigo', flickr2piwigo_default_config); 
     18   
    1819  mkdir($conf['data_location'].'flickr_cache/', 0755); 
    1920} 
     
    3031  if (!file_exists($conf['data_location'].'flickr_cache/')) 
    3132  { 
    32     mdir($conf['data_location'].'flickr_cache/', 0755); 
     33    mkdir($conf['data_location'].'flickr_cache/', 0755); 
    3334  } 
    3435} 
     
    3738{ 
    3839  pwg_query('DELETE FROM `'. CONFIG_TABLE .'` WHERE param = "flickr2piwigo" LIMIT 1;'); 
     40   
     41  rrmdir($conf['data_location'].'flickr_cache/'); 
    3942} 
    4043 
     44function rrmdir($dir) 
     45{ 
     46  if (!is_dir($dir)) 
     47  { 
     48    return false; 
     49  } 
     50  $dir = rtrim($dir, '/'); 
     51  $objects = scandir($dir); 
     52  $return = true; 
     53   
     54  foreach ($objects as $object) 
     55  { 
     56    if ($object !== '.' && $object !== '..') 
     57    { 
     58      $path = $dir.'/'.$object; 
     59      if (filetype($path) == 'dir')  
     60      { 
     61        $return = $return && rrmdir($path);  
     62      } 
     63      else  
     64      { 
     65        $return = $return && @unlink($path); 
     66      } 
     67    } 
     68  } 
     69   
     70  return $return && @rmdir($dir); 
     71}  
     72 
    4173?> 
Note: See TracChangeset for help on using the changeset viewer.