/products/photocrati_nextgen/modules/ngglegacy/admin/functions.php

  1. <?php 
  2.  
  3. if(preg_match('#' . basename(__FILE__) . '#', $_SERVER['PHP_SELF'])) { die('You are not allowed to call this page directly.'); } 
  4.  
  5. /** 
  6. * nggAdmin - Class for admin operation 
  7. * 
  8. * @package NextGEN Gallery 
  9. * @author Alex Rabe 
  10. * 
  11. * @access public 
  12. */ 
  13. class nggAdmin{ 
  14.  
  15. /** 
  16. * create a new gallery & folder 
  17. * 
  18. * @class nggAdmin 
  19. * @param string $name of the gallery 
  20. * @param string $defaultpath 
  21. * @param bool $output if the function should show an error messsage or not 
  22. * @return 
  23. */ 
  24. static function create_gallery($title, $defaultpath, $output = true) { 
  25.  
  26. global $user_ID; 
  27. $fs = C_Fs::get_instance(); 
  28. $storage = C_Gallery_Storage::get_instance(); 
  29.  
  30. // get the current user ID 
  31. wp_get_current_user(); 
  32.  
  33. //cleanup pathname 
  34. $name = sanitize_file_name( sanitize_title($title) ); 
  35. $name = apply_filters('ngg_gallery_name', $name); 
  36. $txt = ''; 
  37.  
  38. $galleryObj = new stdClass; 
  39. $galleryObj->path = ''; 
  40. $nggRoot = $storage->get_gallery_abspath($galleryObj); 
  41.  
  42. // No gallery name ? 
  43. if ( empty($name) ) { 
  44. if ($output) nggGallery::show_error( __('No valid gallery name!', 'nggallery') ); 
  45. return false; 
  46.  
  47. $galleryObj = new stdClass; 
  48. $galleryObj->path = $fs->join_paths($defaultpath, $name); 
  49. $gallery_path = $storage->get_gallery_abspath($galleryObj); 
  50.  
  51. // check for main folder 
  52. if ( !is_dir($nggRoot) ) { 
  53. if ( !wp_mkdir_p( $nggRoot ) ) { 
  54. $txt = __('Directory', 'nggallery').' <strong>' . esc_html( $nggRoot ) . '</strong> '.__('didn\'t exist. Please create first the main gallery folder ', 'nggallery').'!<br />'; 
  55. $txt .= __('Check this link, if you didn\'t know how to set the permission :', 'nggallery').' <a href="http://codex.wordpress.org/Changing_File_Permissions">http://codex.wordpress.org/Changing_File_Permissions</a> '; 
  56. if ($output) nggGallery::show_error($txt); 
  57. return false; 
  58.  
  59. // 1. Check for existing folder 
  60. if ( is_dir($gallery_path) && !(SAFE_MODE) ) { 
  61. $suffix = 1; 
  62. do { 
  63. $alt_name = substr ($name, 0, 200 - ( strlen( $suffix ) + 1 ) ) . "_$suffix"; 
  64. $galleryObj->path = $fs->join_paths($defaultpath, $alt_name); 
  65. $gallery_path = $storage->get_gallery_abspath($galleryObj); 
  66. $dir_check = is_dir($gallery_path); 
  67. $suffix++; 
  68. } while ( $dir_check ); 
  69. $name = $alt_name; 
  70.  
  71. $thumb_path = $fs->join_paths($gallery_path, 'thumbs'); 
  72.  
  73. // 2. Create new gallery folder 
  74. if ( !wp_mkdir_p ($gallery_path) ) 
  75. $txt = __('Unable to create directory ', 'nggallery') . esc_html($gallery_path) . '!<br />'; 
  76.  
  77. // 3. Check folder permission 
  78. if ( !is_writeable($gallery_path) ) 
  79. $txt .= __('Directory', 'nggallery').' <strong>' . esc_html($gallery_path) . '</strong> '.__('is not writeable !', 'nggallery').'<br />'; 
  80.  
  81. // 4. Now create thumbnail folder inside 
  82. if ( !is_dir($thumb_path) ) { 
  83. if ( !wp_mkdir_p ($thumb_path) ) 
  84. $txt .= __('Unable to create directory ', 'nggallery').' <strong>' . esc_html($thumb_path) . '/thumbs !</strong>'; 
  85.  
  86. if (SAFE_MODE) { 
  87. $help = __('The server setting Safe-Mode is on !', 'nggallery'); 
  88. $help .= '<br />'.__('If you have problems, please create directory', 'nggallery').' <strong>' . esc_html($gallery_path) . '</strong> '; 
  89. $help .= __('and the thumbnails directory', 'nggallery').' <strong>' . esc_html($thumb_path) . '</strong> '.__('with permission 777 manually !', 'nggallery'); 
  90. if ($output) nggGallery::show_message($help); 
  91.  
  92. // show a error message 
  93. if ( !empty($txt) ) { 
  94. if (SAFE_MODE) { 
  95. // for safe_mode , better delete folder, both folder must be created manually 
  96. @rmdir($thumb_path); 
  97. @rmdir($gallery_path); 
  98. if ($output) nggGallery::show_error($txt); 
  99. return false; 
  100.  
  101. // now add the gallery to the database 
  102. $galleryID = nggdb::add_gallery($title, $defaultpath . $name, '', 0, 0, $user_ID ); 
  103. // here you can inject a custom function 
  104. do_action('ngg_created_new_gallery', $galleryID); 
  105.  
  106. // return only the id if defined 
  107. if ($output == false) 
  108. return $galleryID; 
  109.  
  110. if ($galleryID != false && !isset($_REQUEST['attach_to_post'])) 
  111. $url = admin_url() . 'admin.php?page=nggallery-manage-gallery&mode=edit&gid=' . $galleryID; 
  112. $message = sprintf(__('Gallery successfully created. <a href="%s" target="_blank">Manage gallery</a>', 'nggallery'), $url); 
  113. if ($output) 
  114. nggGallery::show_message($message, 'gallery_created_msg'); 
  115. return true; 
  116.  
  117. /** 
  118. * Scan folder for new images 
  119. * 
  120. * @class nggAdmin 
  121. * @param string $dirname 
  122. * @return array $files list of image filenames 
  123. */ 
  124. static function scandir( $dirname = '.' ) { 
  125. $ext = apply_filters('ngg_allowed_file_types', array('jpeg', 'jpg', 'png', 'gif') ); 
  126.  
  127. $files = array(); 
  128. if( $handle = opendir( $dirname ) ) { 
  129. while( false !== ( $file = readdir( $handle ) ) ) { 
  130. $info = M_I18n::mb_pathinfo( $file ); 
  131. // just look for images with the correct extension 
  132. if ( isset($info['extension']) ) { 
  133. if ( in_array( strtolower($info['extension']), $ext) ) { 
  134. if (!seems_utf8($file)) { 
  135. $file = utf8_encode( $file ); 
  136.  
  137. $files[] = $file; 
  138.  
  139. closedir( $handle ); 
  140. sort( $files ); 
  141. return ( $files ); 
  142.  
  143. /** 
  144. * nggAdmin::createThumbnail() - function to create or recreate a thumbnail 
  145. * 
  146. * @class nggAdmin 
  147. * @param object | int $image contain all information about the image or the id 
  148. * @return string result code 
  149. * @since v1.0.0 
  150. */ 
  151. static function create_thumbnail($image) { 
  152.  
  153. if (is_object($image)) { 
  154. if (isset($image->id)) { 
  155. $image = $image->id; 
  156. elseif (isset($image->pid)) { 
  157. $image = $image->pid; 
  158. $storage = C_Gallery_Storage::get_instance(); 
  159.  
  160. // XXX NextGEN Legacy wasn't handling watermarks or reflections at this stage, so we're forcefully disabling them to maintain compatibility 
  161. $params = array('watermark' => false, 'reflection' => false); 
  162. $result = $storage->generate_thumbnail($image, $params); 
  163.  
  164. if (!$result) 
  165. // XXX there isn't any error handling unfortunately at the moment in the generate_thumbnail functions, need a way to return proper error status 
  166. return __('Error while creating thumbnail.', 'nggallery'); 
  167.  
  168. // success 
  169. return '1'; 
  170.  
  171. /** 
  172. * nggAdmin::resize_image() - create a new image, based on the height /width 
  173. * 
  174. * @class nggAdmin 
  175. * @param object | int $image contain all information about the image or the id 
  176. * @param integer $width optional 
  177. * @param integer $height optional 
  178. * @return string result code 
  179. */ 
  180. static function resize_image($image, $width = 0, $height = 0) 
  181. if (is_object($image)) { 
  182. if (isset($image->id)) { 
  183. $image = $image->id; 
  184. elseif (isset($image->pid)) { 
  185. $image = $image->pid; 
  186.  
  187. $storage = C_Gallery_Storage::get_instance(); 
  188. // XXX maybe get rid of this...it's needed to get width/height defaults, placing these directly in generate_image_size could have unwanted consequences 
  189. $settings = C_NextGen_Settings::get_instance(); 
  190.  
  191. // XXX NextGEN Legacy wasn't handling watermarks or reflections at this stage, so we're forcefully disabling them to maintain compatibility 
  192. $params = array('watermark' => false, 'reflection' => false); 
  193.  
  194. if ($width > 0) { 
  195. $params['width'] = $width; 
  196. else { 
  197. $params['width'] = $settings->imgWidth; 
  198.  
  199. if ($height > 0) { 
  200. $params['height'] = $height; 
  201. else { 
  202. $params['height'] = $settings->imgHeight; 
  203.  
  204. $result = $storage->generate_image_size($image, 'full', $params); 
  205.  
  206. if (!$result) 
  207. // XXX there isn't any error handling unfortunately at the moment in the generate_thumbnail functions, need a way to return proper error status 
  208. return __('Error while resizing image.', 'nggallery'); 
  209.  
  210. // success 
  211. return '1'; 
  212.  
  213. /** 
  214. * Rotated/Flip an image based on the orientation flag or a definded angle 
  215. * 
  216. * @param int|object $image 
  217. * @param string (optional) $dir, CW (clockwise)or CCW (counter clockwise), if set to false, the exif flag will be used 
  218. * @param string (optional) $flip, could be either false | V (flip vertical) | H (flip horizontal) 
  219. * @return string result code 
  220. */ 
  221. static function rotate_image($image, $dir = false, $flip = false) 
  222. if (is_object($image)) { 
  223. if (isset($image->id)) $image = $image->id; 
  224. elseif (isset($image->pid)) $image = $image->pid; 
  225. $storage = C_Gallery_Storage::get_instance(); 
  226.  
  227. // XXX NextGEN Legacy wasn't handling watermarks or reflections at this stage, so we're forcefully disabling them to maintain compatibility 
  228. $params = array('watermark' => false, 'reflection' => false); 
  229. $rotation = null; 
  230.  
  231. if ($dir === 'CW') { 
  232. $rotation = 90; 
  233. else if ($dir === 'CCW') { 
  234. $rotation = -90; 
  235. // if you didn't define a rotation, we look for the orientation flag in EXIF 
  236. else if ($dir === false) { 
  237. $meta = new nggMeta( $image); 
  238. $exif = $meta->get_EXIF(); 
  239.  
  240. if (isset($exif['Orientation'])) { 
  241.  
  242. switch ($exif['Orientation']) { 
  243. case 5 : // vertical flip + 90 rotate right 
  244. $flip = 'V'; 
  245. case 6 : // 90 rotate right 
  246. $rotation = 90; 
  247. break; 
  248. case 7 : // horizontal flip + 90 rotate right 
  249. $flip = 'H'; 
  250. case 8 : // 90 rotate left 
  251. $rotation = -90; 
  252. break; 
  253. case 4 : // vertical flip 
  254. $flip = 'V'; 
  255. break; 
  256. case 3 : // 180 rotate left 
  257. $rotation = -180; 
  258. break; 
  259. case 2 : // horizontal flip 
  260. $flip = 'H'; 
  261. break; 
  262. case 1 : // no action in the case it doesn't need a rotation 
  263. default: 
  264. return '0'; 
  265. break; 
  266. } else 
  267. return '0'; 
  268.  
  269. if ($rotation != null) { 
  270. $params['rotation'] = $rotation; 
  271.  
  272. if ($flip != null) { 
  273. $params['flip'] = $flip; 
  274.  
  275. $result = $storage->generate_image_size($image, 'full', $params); 
  276.  
  277. if (!$result) 
  278. // XXX there isn't any error handling unfortunately at the moment in the generate_thumbnail functions, need a way to return proper error status 
  279. return __('Error while rotating image.', 'nggallery'); 
  280.  
  281. // success 
  282. return '1'; 
  283.  
  284. /** 
  285. * nggAdmin::set_watermark() - set the watermark for the image 
  286. * 
  287. * @class nggAdmin 
  288. * @param object | int $image contain all information about the image or the id 
  289. * @return string result code 
  290. */ 
  291. static function set_watermark($image) { 
  292.  
  293. if (is_object($image)) { 
  294. if (isset($image->id)) { 
  295. $image = $image->id; 
  296. elseif (isset($image->pid)) { 
  297. $image = $image->pid; 
  298.  
  299. $storage = C_Gallery_Storage::get_instance(); 
  300.  
  301. // XXX NextGEN Legacy was only handling watermarks at this stage, so we're forcefully disabling all else 
  302. $params = array('watermark' => true, 'reflection' => false, 'crop' => false); 
  303. $result = $storage->generate_image_size($image, 'full', $params); 
  304.  
  305. if (!$result) 
  306. // XXX there isn't any error handling unfortunately at the moment in the generate_thumbnail functions, need a way to return proper error status 
  307. return __('Error while applying watermark to image.', 'nggallery'); 
  308.  
  309. // success 
  310. return '1'; 
  311.  
  312. /** 
  313. * Recover image from backup copy and reprocess it 
  314. * 
  315. * @class nggAdmin 
  316. * @since 1.5.0 
  317. * @param object | int $image contain all information about the image or the id 
  318. * @return string result code 
  319. */ 
  320.  
  321. static function recover_image($image) { 
  322.  
  323. return C_Gallery_Storage::get_instance()->recover_image($image); 
  324.  
  325. /** 
  326. * Add images to database 
  327. * 
  328. * @class nggAdmin 
  329. * @param int $galleryID 
  330. * @param array $imageslist 
  331. * @return array $image_ids Id's which are sucessful added 
  332. */ 
  333. static function add_Images($galleryID, $imageslist) { 
  334.  
  335. global $wpdb, $ngg; 
  336.  
  337. $image_ids = array(); 
  338.  
  339. if ( is_array($imageslist) ) { 
  340. foreach($imageslist as $picture) { 
  341.  
  342. // filter function to rename/change/modify image before 
  343. $picture = apply_filters('ngg_pre_add_new_image', $picture, $galleryID); 
  344.  
  345. // strip off the extension of the filename 
  346. $path_parts = M_I18n::mb_pathinfo( $picture ); 
  347. $alttext = ( !isset($path_parts['filename']) ) ? substr($path_parts['basename'], 0, strpos($path_parts['basename'], '.')) : $path_parts['filename']; 
  348. // save it to the database 
  349. $pic_id = nggdb::add_image( $galleryID, $picture, '', $alttext ); 
  350.  
  351. if (C_NextGen_Settings::get_instance()->imgBackup && !empty($pic_id)) 
  352. $storage = C_Gallery_Storage::get_instance(); 
  353. $storage->backup_image($pic_id); 
  354.  
  355. if ( !empty($pic_id) ) 
  356. $image_ids[] = $pic_id; 
  357.  
  358. // add the metadata 
  359. nggAdmin::import_MetaData( $pic_id ); 
  360.  
  361. // auto rotate 
  362. nggAdmin::rotate_image( $pic_id ); 
  363.  
  364. // Autoresize image if required 
  365. if ($ngg->options['imgAutoResize']) { 
  366. $imagetmp = nggdb::find_image( $pic_id ); 
  367. $sizetmp = @getimagesize ( $imagetmp->imagePath ); 
  368. $widthtmp = $ngg->options['imgWidth']; 
  369. $heighttmp = $ngg->options['imgHeight']; 
  370. if (($sizetmp[0] > $widthtmp && $widthtmp) || ($sizetmp[1] > $heighttmp && $heighttmp)) { 
  371. nggAdmin::resize_image( $pic_id ); 
  372.  
  373. // action hook for post process after the image is added to the database 
  374. $image = array( 'id' => $pic_id, 'filename' => $picture, 'galleryID' => $galleryID); 
  375. do_action('ngg_added_new_image', $image); 
  376.  
  377. } // is_array 
  378.  
  379. // delete dirsize after adding new images 
  380. delete_transient( 'dirsize_cache' ); 
  381.  
  382. do_action('ngg_after_new_images_added', $galleryID, $image_ids ); 
  383.  
  384. return $image_ids; 
  385.  
  386.  
  387. /** 
  388. * Import some meta data into the database (if avialable) 
  389. * 
  390. * @class nggAdmin 
  391. * @param array|int $imagesIds 
  392. * @return string result code 
  393. */ 
  394. static function import_MetaData($imagesIds) { 
  395.  
  396. global $wpdb; 
  397.  
  398. require_once(NGGALLERY_ABSPATH . '/lib/image.php'); 
  399.  
  400. if (!is_array($imagesIds)) 
  401. $imagesIds = array($imagesIds); 
  402.  
  403. foreach($imagesIds as $imageID) { 
  404.  
  405. // Get the image 
  406. $image = NULL; 
  407. if (is_int($imageID)) { 
  408. $image = C_Image_Mapper::get_instance()->find($imageID); 
  409. else $image = $imageID; 
  410.  
  411. if ($image) { 
  412.  
  413. $meta = nggAdmin::get_MetaData( $image); 
  414.  
  415. // get the title 
  416. $alttext = empty( $meta['title'] ) ? $image->alttext : $meta['title']; 
  417.  
  418. // get the caption / description field 
  419. $description = empty( $meta['caption'] ) ? $image->description : $meta['caption']; 
  420.  
  421. // get the file date/time from exif 
  422. $timestamp = $meta['timestamp']; 
  423.  
  424. // first update database 
  425. $result = $wpdb->query( 
  426. $wpdb->prepare("UPDATE $wpdb->nggpictures SET 
  427. alttext = %s,  
  428. description = %s,  
  429. imagedate = %s 
  430. WHERE pid = %d", $alttext, $description, $timestamp, $image->pid) ); 
  431.  
  432. if ($result === false) 
  433. return ' <strong>' . esc_html( $image->filename ) . ' ' . __('(Error : Couldn\'t not update data base)', 'nggallery') . '</strong>'; 
  434.  
  435. //this flag will inform us that the import is already one time performed 
  436. $meta['common']['saved'] = true; 
  437. $result = nggdb::update_image_meta($image->pid, $meta['common']); 
  438.  
  439. if ($result === false) 
  440. return ' <strong>' . esc_html( $image->filename ) . ' ' . __('(Error : Couldn\'t not update meta data)', 'nggallery') . '</strong>'; 
  441.  
  442. // add the tags if we found some 
  443. if ($meta['keywords']) { 
  444. $taglist = explode(', ', $meta['keywords']); 
  445. wp_set_object_terms($image->pid, $taglist, 'ngg_tag'); 
  446.  
  447. } else 
  448. return ' <strong>' . esc_html( $image->filename ) . ' ' . __('(Error : Couldn\'t not find image)', 'nggallery') . '</strong>';// error check 
  449.  
  450. return '1'; 
  451.  
  452. /** 
  453. * nggAdmin::get_MetaData() 
  454. * 
  455. * @class nggAdmin 
  456. * @require NextGEN Meta class 
  457. * @param int $id image ID 
  458. * @return array metadata 
  459. */ 
  460. static function get_MetaData($image_or_id) { 
  461.  
  462. require_once(NGGALLERY_ABSPATH . '/lib/meta.php'); 
  463.  
  464. $meta = array(); 
  465.  
  466. $pdata = new nggMeta($image_or_id); 
  467.  
  468. $meta['title'] = trim ( $pdata->get_META('title') ); 
  469. $meta['caption'] = trim ( $pdata->get_META('caption') ); 
  470. $meta['keywords'] = trim ( $pdata->get_META('keywords') ); 
  471. $meta['timestamp'] = $pdata->get_date_time(); 
  472. // this contain other useful meta information 
  473. $meta['common'] = $pdata->get_common_meta(); 
  474. // hook for addon plugin to add more meta fields 
  475. $meta = apply_filters('ngg_get_image_metadata', $meta, $pdata); 
  476.  
  477. return $meta; 
  478.  
  479.  
  480. /** 
  481. * Maybe import some meta data to the database. The functions checks the flag 'saved' 
  482. * and if based on compat reason (pre V1.4.0) we save then some meta datas to the database 
  483. * 
  484. * @since V1.4.0 
  485. * @param int $id 
  486. * @return result 
  487. */ 
  488. function maybe_import_meta( $image_or_id ) { 
  489.  
  490. require_once(NGGALLERY_ABSPATH . '/lib/meta.php'); 
  491. $id = is_int($image_or_id) ? $image_or_id : $image_or_id->{$image_or_id->id_field}; 
  492. $meta_obj = new nggMeta( $image_or_id ); 
  493.  
  494. if ( $meta_obj->image->meta_data['saved'] != true ) { 
  495. $common = $meta_obj->get_common_meta(); 
  496. //this flag will inform us that the import is already one time performed 
  497. $common['saved'] = true; 
  498. $result = nggdb::update_image_meta($id, $common); 
  499. } else 
  500. return false; 
  501.  
  502. return $result; 
  503.  
  504.  
  505. /** 
  506. * nggAdmin::import_gallery() 
  507. * TODO: Check permission of existing thumb folder & images 
  508. * 
  509. * @class nggAdmin 
  510. * @param string $galleryfolder contains relative path to the gallery itself 
  511. * @return void 
  512. */ 
  513. static function import_gallery($galleryfolder, $gallery_id=NULL) { 
  514.  
  515. global $wpdb, $user_ID; 
  516.  
  517. // get the current user ID 
  518. wp_get_current_user(); 
  519.  
  520. $created_msg = ''; 
  521.  
  522. // remove trailing slash at the end, if somebody use it 
  523. $galleryfolder = untrailingslashit($galleryfolder); 
  524.  
  525. $fs = C_Fs::get_instance(); 
  526. if (is_null($gallery_id)) { 
  527. $gallerypath = $fs->join_paths($fs->get_document_root('content'), $galleryfolder); 
  528. } else { 
  529. $storage = C_Gallery_Storage::get_instance(); 
  530. $gallerypath = $storage->get_gallery_abspath($gallery_id); 
  531.  
  532. if (!is_dir($gallerypath)) { 
  533. nggGallery::show_error(sprintf(__("Directory <strong>%s</strong> doesn`t exist!", 'nggallery'), esc_html($gallerypath))); 
  534. return ; 
  535.  
  536. // read list of images 
  537. $new_imageslist = nggAdmin::scandir($gallerypath); 
  538.  
  539. if (empty($new_imageslist)) { 
  540. nggGallery::show_message(sprintf(__("Directory <strong>%s</strong> contains no pictures", 'nggallery'), esc_html($gallerypath))); 
  541. return; 
  542.  
  543. // take folder name as gallery name 
  544. $galleryname = basename($galleryfolder); 
  545. $galleryname = apply_filters('ngg_gallery_name', $galleryname); 
  546.  
  547. // check for existing gallery folder 
  548. if (is_null($gallery_id)) 
  549. $gallery_id = $wpdb->get_var("SELECT gid FROM $wpdb->nggallery WHERE path = '$galleryfolder' "); 
  550.  
  551. if (!$gallery_id) { 
  552. // now add the gallery to the database 
  553. $gallery_id = nggdb::add_gallery( $galleryname, $galleryfolder, '', 0, 0, $user_ID ); 
  554. if (!$gallery_id) { 
  555. nggGallery::show_error(__('Database error. Could not add gallery!', 'nggallery')); 
  556. return; 
  557. else { 
  558. do_action('ngg_created_new_gallery', $gallery_id); 
  559. $created_msg = sprintf( 
  560. _n("Gallery <strong>%s</strong> successfully created!", 'Galleries <strong>%s</strong> successfully created!', 1, 'nggallery'),  
  561. esc_html($galleryname) 
  562. ); 
  563.  
  564. // Look for existing image list 
  565. $old_imageslist = $wpdb->get_col("SELECT filename FROM $wpdb->nggpictures WHERE galleryid = '$gallery_id' "); 
  566.  
  567. // if no images are there, create empty array 
  568. if ($old_imageslist == NULL) 
  569. $old_imageslist = array(); 
  570.  
  571. // check difference 
  572. $new_images = array_diff($new_imageslist, $old_imageslist); 
  573.  
  574. // all images must be valid files 
  575. foreach($new_images as $key => $picture) { 
  576.  
  577. // filter function to rename/change/modify image before 
  578. $picture = apply_filters('ngg_pre_add_new_image', $picture, $gallery_id); 
  579. $new_images[$key] = $picture; 
  580.  
  581. if (!@getimagesize($gallerypath . '/' . $picture) ) { 
  582. unset($new_images[$key]); 
  583. @unlink($gallerypath . '/' . $picture); 
  584.  
  585. // add images to database 
  586. $image_ids = nggAdmin::add_Images($gallery_id, $new_images); 
  587. do_action('ngg_after_new_images_added', $gallery_id, $image_ids); 
  588.  
  589. //add the preview image if needed 
  590. nggAdmin::set_gallery_preview ( $gallery_id ); 
  591.  
  592. // now create thumbnails 
  593. nggAdmin::do_ajax_operation( 'create_thumbnail' , $image_ids, __('Create new thumbnails', 'nggallery') ); 
  594.  
  595. //TODO:Message will not shown, because AJAX routine require more time, message should be passed to AJAX 
  596. $message = $created_msg . sprintf(_n('%s picture successfully added', '%s pictures successfully added', count($image_ids), 'nggallery'), count($image_ids)); 
  597. $message .= ' [<a href="' . admin_url() . 'admin.php?page=nggallery-manage-gallery&mode=edit&gid=' . $gallery_id . '" >'; 
  598. $message .= __('Edit gallery', 'nggallery'); 
  599. $message .= '</a>]'; 
  600.  
  601. nggGallery::show_message($message); 
  602.  
  603. return; 
  604.  
  605. /** 
  606. * Capability check. Check is the ID fit's to the user_ID 
  607. * 
  608. * @class nggAdmin 
  609. * @param int $check_ID is the user_id 
  610. * @return bool $result 
  611. */ 
  612. static function can_manage_this_gallery($check_ID) { 
  613.  
  614. global $user_ID, $wp_roles; 
  615.  
  616. if ( !current_user_can('NextGEN Manage others gallery') ) { 
  617. // get the current user ID 
  618. wp_get_current_user(); 
  619.  
  620. if ( $user_ID != $check_ID) 
  621. return false; 
  622.  
  623. return true; 
  624.  
  625.  
  626. /** 
  627. * Initate the Ajax operation 
  628. * 
  629. * @class nggAdmin 
  630. * @param string $operation name of the function which should be executed 
  631. * @param array $image_array 
  632. * @param string $title name of the operation 
  633. * @return string the javascript output 
  634. */ 
  635. static function do_ajax_operation( $operation, $image_array, $title = '' ) { 
  636.  
  637. if ( !is_array($image_array) || empty($image_array) ) 
  638. return; 
  639.  
  640. $js_array = implode('", "', $image_array); 
  641.  
  642. // send out some JavaScript, which initate the ajax operation 
  643. ?> 
  644. <script type="text/javascript"> 
  645.  
  646. Images = new Array("<?php echo $js_array; ?>"); 
  647.  
  648. nggAjaxOptions = { 
  649. operation: "<?php echo $operation; ?>",  
  650. ids: Images,  
  651. header: "<?php echo $title; ?>",  
  652. maxStep: Images.length 
  653. }; 
  654.  
  655. jQuery(document).ready( function() { 
  656. nggProgressBar.init( nggAjaxOptions ); 
  657. nggAjax.init( nggAjaxOptions ); 
  658. } ); 
  659. </script> 
  660.  
  661. <?php 
  662.  
  663. /** 
  664. * nggAdmin::set_gallery_preview() - define a preview pic after the first upload, can be changed in the gallery settings 
  665. * 
  666. * @class nggAdmin 
  667. * @param int $galleryID 
  668. * @return void 
  669. */ 
  670. static function set_gallery_preview( $galleryID ) 
  671. $gallery_mapper = C_Gallery_Mapper::get_instance(); 
  672. if (($gallery = $gallery_mapper->find($galleryID))) { 
  673. if (!$gallery->previewpic) { 
  674. $image_mapper = C_Image_Mapper::get_instance(); 
  675. if (($image = $image_mapper->select()->where(array('galleryid = %d', $galleryID))->where(array('exclude != 1'))-> 
  676. order_by($image_mapper->get_primary_key_column())->limit(1)->run_query())) { 
  677. $gallery->previewpic = $image->{$image->id_field}; 
  678. $gallery_mapper->save($gallery); 
  679.  
  680. /** 
  681. * Return a JSON coded array of Image ids for a requested gallery 
  682. * 
  683. * @class nggAdmin 
  684. * @param int $galleryID 
  685. * @return arry (JSON) 
  686. */ 
  687. static function get_image_ids( $galleryID ) { 
  688.  
  689. if ( !function_exists('json_encode') ) 
  690. return(-2); 
  691.  
  692. $gallery = nggdb::get_ids_from_gallery($galleryID, 'pid', 'ASC', false); 
  693.  
  694. header('Content-Type: text/plain; charset=' . get_option('blog_charset'), true); 
  695. $output = json_encode($gallery); 
  696.  
  697. return $output; 
  698.  
  699. /** 
  700. * Deprecated function, restored to fix compatibility with "NextGen Public Uploader" 
  701. * 
  702. * @deprecated 
  703. * @class nggAdmin 
  704. * @param string $filename 
  705. * @return bool $result 
  706. */ 
  707. function chmod($filename = '') 
  708. $stat = @stat(dirname($filename)); 
  709. $perms = $stat['mode'] & 0000666; 
  710. if (@chmod($filename, $perms)) 
  711. return true; 
  712. return false; 
  713.  
  714. } // END class nggAdmin 
  715.  
  716. // XXX temporary...used as a quick fix to refresh I_Settings_Manager when the nextgen option is updated manually in order to run Hooks etc. 
  717. function ngg_refreshSavedSettings() 
  718. if (class_exists('C_NextGen_Settings')) { 
  719. $settings = C_NextGen_Settings::get_instance(); 
  720.  
  721. if ($settings != null) 
  722. $width = $settings->thumbwidth; 
  723. $height = $settings->thumbheight; 
  724. $new_dimension = "{$width}x{$height}"; 
  725. $dimensions = (array) $settings->thumbnail_dimensions; 
  726.  
  727. if (!in_array($new_dimension, $dimensions)) { 
  728. $dimensions[] = $new_dimension; 
  729. $settings->thumbnail_dimensions = $dimensions; 
  730. $settings->save(); 
  731.  
  732. return true; 
  733.  
  734. return false; 
.