Changeset 16071 for extensions/flickr2piwigo/main.inc.php
- Timestamp:
- Jun 27, 2012, 1:53:21 AM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
extensions/flickr2piwigo/main.inc.php
r16063 r16071 17 17 define('FLICKR_FS_CACHE', $conf['data_location'].'flickr_cache/'); 18 18 19 add_event_handler('get_admin_plugin_menu_links', 'flickr2_admin_menu');20 19 21 function flickr2_admin_menu($menu) 20 add_event_handler('get_admin_plugin_menu_links', 'flickr_admin_menu'); 21 22 function flickr_admin_menu($menu) 22 23 { 23 24 array_push($menu, array( … … 28 29 } 29 30 30 add_event_handler('ws_add_methods', 'flickr2_add_ws_method');31 31 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 } 32 include_once(FLICKR_PATH . 'include/ws_functions.inc.php'); 47 33 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 } 34 add_event_handler('ws_add_methods', 'flickr_add_ws_method'); 157 35 158 36 ?>
Note: See TracChangeset
for help on using the changeset viewer.