nggAdmin

NggAdmin - Class for admin operation.

Defined (1)

The class is defined in the following location(s).

/admin/functions.php  
  1. class nggAdmin{ 
  2.  
  3. /** 
  4. * Create a new gallery in a new folder 
  5. * @param string $title The gallery name. 
  6. * @param string $defaultpath The path where the gallery should be added. 
  7. * @param bool $output (optional) If the function should show an error messsage or not. 
  8. * @param string $description (optional) The gallery description. 
  9. * @since 1.9.24 Added the description parameter. 
  10. * @return bool|int True if successful, otherwise false.$galleryID if is successful and output = false) 
  11. */ 
  12.  
  13. static function create_gallery($title, $defaultpath, $output = true, $description = '') { 
  14.  
  15. global $user_ID; 
  16.  
  17. get_currentuserinfo(); //get current user ID & sets global object $current_user 
  18. $name = sanitize_file_name( sanitize_title($title) ); //cleanup pathname 
  19. $name = apply_filters('ngg_gallery_name', $name); 
  20.  
  21. if ( empty($name) ) { // No gallery name ? 
  22. if ($output) nggGallery::show_error( __('No valid gallery name!', 'nggallery') ); 
  23. return false; 
  24.  
  25. $txt = ''; 
  26. $nggRoot = WINABSPATH . $defaultpath; 
  27.  
  28. // check for main folder 
  29. if ( !is_dir($nggRoot) ) { 
  30. if ( !wp_mkdir_p( $nggRoot ) ) { 
  31. $txt = __('Directory', 'nggallery').' <strong>' . esc_html( $defaultpath ) . '</strong> '.__('didn\'t exist. Please create first the main gallery folder ', 'nggallery').'!<br />'; 
  32. $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> '; 
  33. if ($output) nggGallery::show_error($txt); 
  34. return false; 
  35.  
  36. // check for permission settings, Safe mode limitations are not taken into account.  
  37. if ( !is_writeable( $nggRoot ) ) { 
  38. $txt = __('Directory', 'nggallery').' <strong>' . esc_html( $defaultpath ) . '</strong> '.__('is not writeable !', 'nggallery').'<br />'; 
  39. $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> '; 
  40. if ($output) nggGallery::show_error($txt); 
  41. return false; 
  42.  
  43. // 1. Check for existing folder, if it already exists, create new one with suffix 
  44. if ( is_dir($nggRoot . $name ) && !(SAFE_MODE) ) { 
  45. $suffix = 1; 
  46. do { 
  47. $alt_name = substr ($name, 0, 200 - ( strlen( $suffix ) + 1 ) ) . "_$suffix"; 
  48. $dir_check = is_dir($nggRoot . $alt_name ); 
  49. $suffix++; 
  50. } while ( $dir_check ); 
  51. $name = $alt_name; 
  52. }  
  53. // define relative path to gallery inside wp root folder 
  54. $nggpath = $defaultpath . $name; 
  55. $win_nggpath = WINABSPATH . $nggpath; 
  56. // 2. Create new gallery folder 
  57.  
  58. if ( !wp_mkdir_p ($win_nggpath) ) 
  59. $txt = __('Unable to create directory ', 'nggallery') . esc_html( $nggpath ) . '!<br />'; 
  60.  
  61. // 3. Check folder permission 
  62. if ( !is_writeable($win_nggpath) ) 
  63. $txt .= __('Directory', 'nggallery').' <strong>' . esc_html( $nggpath ) . '</strong> '.__('is not writeable !', 'nggallery').'<br />'; 
  64.  
  65. // 4. Now create thumbnail folder inside 
  66. if ( !is_dir($win_nggpath . '/thumbs') ) { 
  67. if ( !wp_mkdir_p ( $win_nggpath . '/thumbs') ) 
  68. $txt .= __('Unable to create directory ', 'nggallery').' <strong>' . esc_html( $nggpath ) . '/thumbs !</strong>'; 
  69.  
  70. if (SAFE_MODE) { 
  71. $help = __('The server setting Safe-Mode is on !', 'nggallery');  
  72. $help .= '<br />'.__('If you have problems, please create directory', 'nggallery').' <strong>' . esc_html( $nggpath ) . '</strong> ';  
  73. $help .= __('and the thumbnails directory', 'nggallery').' <strong>' . esc_html( $nggpath ) . '/thumbs</strong> '.__('with permission 777 manually !', 'nggallery'); 
  74. if ($output) nggGallery::show_message($help); 
  75.  
  76. // show a error message  
  77. if ( !empty($txt) ) { 
  78. if (SAFE_MODE) { 
  79. // for safe_mode , better delete folder, both folder must be created manually 
  80. @rmdir($win_nggpath . '/thumbs'); 
  81. @rmdir($win_nggpath); 
  82. if ($output) nggGallery::show_error($txt); 
  83. return false; 
  84.  
  85. //clean the description and add the gallery 
  86. $description = nggGallery::suppress_injection($description); 
  87. $galleryID = nggdb::add_gallery($title, $nggpath, $description, 0, 0, $user_ID ); 
  88. // here you can inject a custom function 
  89. do_action('ngg_created_new_gallery', $galleryID); 
  90.  
  91. // return only the id if defined 
  92. if ($output == false) 
  93. return $galleryID; 
  94.  
  95. if ($galleryID != false) { 
  96. $message = __('Gallery ID %1$s successfully created. You can show this gallery in your post or page with the shortcode %2$s.<br/>', 'nggallery'); 
  97. $message = sprintf($message, $galleryID, '<strong>[nggallery id=' . $galleryID . ']</strong>'); 
  98. $message .= '<a href="' . admin_url() . 'admin.php?page=nggallery-manage&mode=image&gid=' . $galleryID . '" >'; 
  99. $message .= __('Edit gallery', 'nggallery'); 
  100. $message .= '</a>'; 
  101.  
  102. nggGallery::show_message($message); 
  103. return true; 
  104.  
  105. /** 
  106. * nggAdmin::import_gallery() 
  107. * TODO: Check permission of existing thumb folder & images 
  108. * since 1.9.19: sanitize existing folders and images - see old_import_gallery() to use the old system 
  109. *  
  110. * @class nggAdmin 
  111. * @param string $galleryfolder contains relative path to the gallery itself 
  112. * @return void 
  113. */ 
  114.  
  115. static function import_gallery($galleryfolder) { 
  116. /** 
  117. * @global nggdb $nggdb 
  118. */ 
  119. global $wpdb, $user_ID, $nggdb; 
  120.  
  121. // get the current user ID 
  122. get_currentuserinfo(); 
  123.  
  124. $created_msg = NULL; 
  125.  
  126. // remove trailing slash at the end, if somebody use it 
  127. $galleryfolder = untrailingslashit($galleryfolder); 
  128. $gallerypath = WINABSPATH . $galleryfolder; 
  129.  
  130. if (!is_dir($gallerypath)) { 
  131. nggGallery::show_error(__('Directory', 'nggallery').' <strong>' . esc_html( $gallerypath ) .'</strong> '.__('doesn`t exist!', 'nggallery')); 
  132. return ; 
  133.  
  134. $test_images = nggAdmin::scandir($gallerypath); 
  135.  
  136. if (empty($test_images)) { 
  137. nggGallery::show_message(__('Directory', 'nggallery').' <strong>' . esc_html( $gallerypath ) . '</strong> '.__('contains no pictures', 'nggallery')); 
  138. return; 
  139.  
  140. //save orginals 
  141. $old_galleryfolder = $galleryfolder; 
  142. $old_gallerypath = $gallerypath; 
  143.  
  144. //sanitize name 
  145. $new_name = sanitize_file_name( sanitize_title( basename( $galleryfolder ) ) ); 
  146.  
  147. //set new names 
  148. $galleryfolder = dirname( $galleryfolder ) . "/" . sanitize_file_name( sanitize_title( basename( $galleryfolder ) ) ); 
  149. $gallerypath = WINABSPATH . $galleryfolder; 
  150.  
  151. //rename existing folder 
  152. if ( !($old_galleryfolder == $galleryfolder)) { 
  153. //check if it exists 
  154. $increment = ''; //start with no suffix 
  155.  
  156. while(file_exists($gallerypath . $increment)) { 
  157. $increment++; 
  158. $galleryfolder = dirname( $galleryfolder ) . "/" . basename( $galleryfolder ) . $increment; 
  159. $gallerypath = WINABSPATH . $galleryfolder; 
  160.  
  161. if ( !rename( $old_gallerypath , $gallerypath ) ) 
  162. nggGallery::show_message(__('Something went wrong when renaming', 'nggallery').' <strong>' . esc_html( $old_galleryfolder ) .'</strong>! ' .__('Importing was aborted.', 'nggallery')); 
  163.  
  164. // check & create thumbnail folder 
  165. if ( !nggGallery::get_thumbnail_folder($gallerypath) ) 
  166. return; 
  167.  
  168. // take folder name as gallery name  
  169. $galleryname = basename($galleryfolder); 
  170. $galleryname = apply_filters('ngg_gallery_name', $galleryname); 
  171.  
  172. // check for existing gallery folder 
  173. $gallery_id = $wpdb->get_var("SELECT gid FROM $wpdb->nggallery WHERE path = '$old_galleryfolder' "); 
  174.  
  175. if (!$gallery_id) { 
  176. // now add the gallery to the database 
  177. $gallery_id = nggdb::add_gallery( $galleryname, $galleryfolder, '', 0, 0, $user_ID ); 
  178. if (!$gallery_id) { 
  179. nggGallery::show_error(__('Database error. Could not add gallery!', 'nggallery')); 
  180. return; 
  181. $created_msg = __( 'Gallery', 'nggallery' ) . ' <strong>' . esc_html( $galleryname ) . '</strong> ' . __('successfully created!', 'nggallery') . '<br />'; 
  182. } else { 
  183. //if the gallery path has changed, update the database 
  184. if ( !($old_galleryfolder == $galleryfolder )) { 
  185. $wpdb->query( $wpdb->prepare ("UPDATE $wpdb->nggallery SET path= '%s' WHERE gid = %d", $galleryfolder, $gallery_id ) ); 
  186.  
  187. // Look for existing image list and sanitize file names before scanning for new images 
  188. $db_images = $nggdb->get_gallery($gallery_id); 
  189. $updated = array(); 
  190.  
  191. foreach( $db_images as $image ) { 
  192.  
  193. //save old values 
  194. $old_name = $image->filename; 
  195. $old_path = $gallerypath . '/' . $old_name; 
  196.  
  197. //get new name 
  198. $filepart = nggGallery::fileinfo( $old_name ); 
  199.  
  200. //only rename if necessary 
  201. if ( !($old_name == $filepart['basename']) ) { 
  202.  
  203. //check if the sanitized name already exists 
  204. $increment = ''; //start with no suffix 
  205.  
  206. while(file_exists($gallerypath . '/' . $filepart['filename'] . $increment . '.' . $filepart['extension'])) { 
  207. $increment++; 
  208.  
  209. //define new values 
  210. $name = $filepart['filename'] . $increment . '.' . $filepart['extension']; 
  211. $new_path = $gallerypath . "/" . $name; 
  212.  
  213. //rename the file and update alttext 
  214. rename($old_path, $new_path); 
  215. $alttext = sanitize_file_name( $image->alttext ); 
  216.  
  217. // update the database 
  218. nggdb::update_image($image->pid, false, $name, false, $alttext); 
  219.  
  220. $updated[] = $image->pid; 
  221.  
  222. // read list of images 
  223. $new_imageslist = nggAdmin::scandir($gallerypath); 
  224. $old_imageslist = $wpdb->get_col("SELECT filename FROM $wpdb->nggpictures WHERE galleryid = '$gallery_id' "); 
  225.  
  226. // if no images are there, create empty array 
  227. if ($old_imageslist == NULL)  
  228. $old_imageslist = array(); 
  229.  
  230. // check difference 
  231. $new_images = array_diff($new_imageslist, $old_imageslist); 
  232.  
  233. // all images must be valid files 
  234. foreach($new_images as $key => $picture) { 
  235.  
  236. //rename images with the cleaned filename 
  237. $old_path = $gallerypath . '/' . $picture; 
  238. $filepart = nggGallery::fileinfo( $picture ); 
  239.  
  240. //define new values 
  241. $picture = $filepart['basename']; 
  242. $new_path = $gallerypath . "/" . $picture; 
  243.  
  244. rename($old_path, $new_path);  
  245.  
  246. // filter function to rename/change/modify image before 
  247. $picture = apply_filters('ngg_pre_add_new_image', $picture, $gallery_id); 
  248. $new_images[$key] = $picture; 
  249.  
  250. if (!@getimagesize($gallerypath . '/' . $picture) ) { 
  251. unset($new_images[$key]); 
  252. @unlink($gallerypath . '/' . $picture);  
  253.  
  254. // add images to database  
  255. $image_ids = nggAdmin::add_Images($gallery_id, $new_images); 
  256.  
  257. //add the preview image if needed 
  258. nggAdmin::set_gallery_preview ( $gallery_id ); 
  259.  
  260. // now create thumbnails 
  261. nggAdmin::do_ajax_operation( 'create_thumbnail' , array_merge($image_ids, $updated), __('Create new thumbnails', 'nggallery') ); 
  262.  
  263. //TODO:Message will not shown, because AJAX routine require more time, message should be passed to AJAX 
  264. $message = $created_msg; 
  265. if ( count($updated) > 0) 
  266. $message .= $c . __(' picture(s) successfully renamed', 'nggallery') . '<br />'; 
  267. if ( count($image_ids) > 0 ) 
  268. $message .= count($image_ids) .__(' picture(s) successfully added', 'nggallery') . '<br />'; 
  269. if ($created_msg) { 
  270. $message .= ' [<a href="' . admin_url() . 'admin.php?page=nggallery-manage&mode=image&gid=' . $gallery_id . '" >'; 
  271. $message .= __('Edit gallery', 'nggallery'); 
  272. $message .= '</a>]'; 
  273. if (!$message) 
  274. $message = __('No images were added.', 'nggallery'); 
  275.  
  276. nggGallery::show_message($message);  
  277.  
  278. return; 
  279.  
  280.  
  281. /** 
  282. * nggAdmin::old_import_gallery() 
  283. * TODO: Check permission of existing thumb folder & images 
  284. * Use is not recommended! 
  285. *  
  286. * @class nggAdmin 
  287. * @param string $galleryfolder contains relative path to the gallery itself 
  288. * @return void 
  289. */ 
  290. static function old_import_gallery($galleryfolder) { 
  291.  
  292. global $wpdb, $user_ID; 
  293.  
  294. // get the current user ID 
  295. get_currentuserinfo(); 
  296.  
  297. $created_msg = ''; 
  298.  
  299. // remove trailing slash at the end, if somebody use it 
  300. $galleryfolder = untrailingslashit($galleryfolder); 
  301. $gallerypath = WINABSPATH . $galleryfolder; 
  302.  
  303. if (!is_dir($gallerypath)) { 
  304. nggGallery::show_error(__('Directory', 'nggallery').' <strong>' . esc_html( $gallerypath ) .'</strong> '.__('doesn`t exist!', 'nggallery')); 
  305. return ; 
  306.  
  307. // read list of images 
  308. $new_imageslist = nggAdmin::scandir($gallerypath); 
  309.  
  310. if (empty($new_imageslist)) { 
  311. nggGallery::show_message(__('Directory', 'nggallery').' <strong>' . esc_html( $gallerypath ) . '</strong> '.__('contains no pictures', 'nggallery')); 
  312. return; 
  313.  
  314. // check & create thumbnail folder 
  315. if ( !nggGallery::get_thumbnail_folder($gallerypath) ) 
  316. return; 
  317.  
  318. // take folder name as gallery name  
  319. $galleryname = basename($galleryfolder); 
  320. $galleryname = apply_filters('ngg_gallery_name', $galleryname); 
  321.  
  322. // check for existing gallery folder 
  323. $gallery_id = $wpdb->get_var("SELECT gid FROM $wpdb->nggallery WHERE path = '$galleryfolder' "); 
  324.  
  325. if (!$gallery_id) { 
  326. // now add the gallery to the database 
  327. $gallery_id = nggdb::add_gallery( $galleryname, $galleryfolder, '', 0, 0, $user_ID ); 
  328. if (!$gallery_id) { 
  329. nggGallery::show_error(__('Database error. Could not add gallery!', 'nggallery')); 
  330. return; 
  331. $created_msg = __( 'Gallery', 'nggallery' ) . ' <strong>' . esc_html( $galleryname ) . '</strong> ' . __('successfully created!', 'nggallery') . '<br />'; 
  332.  
  333. // Look for existing image list 
  334. $old_imageslist = $wpdb->get_col("SELECT filename FROM $wpdb->nggpictures WHERE galleryid = '$gallery_id' "); 
  335.  
  336. // if no images are there, create empty array 
  337. if ($old_imageslist == NULL)  
  338. $old_imageslist = array(); 
  339.  
  340. // check difference 
  341. $new_images = array_diff($new_imageslist, $old_imageslist); 
  342.  
  343. // all images must be valid files 
  344. foreach($new_images as $key => $picture) { 
  345.  
  346. // filter function to rename/change/modify image before 
  347. $picture = apply_filters('ngg_pre_add_new_image', $picture, $gallery_id); 
  348. $new_images[$key] = $picture; 
  349.  
  350. if (!@getimagesize($gallerypath . '/' . $picture) ) { 
  351. unset($new_images[$key]); 
  352. @unlink($gallerypath . '/' . $picture);  
  353.  
  354. // add images to database  
  355. $image_ids = nggAdmin::add_Images($gallery_id, $new_images); 
  356.  
  357. //add the preview image if needed 
  358. nggAdmin::set_gallery_preview ( $gallery_id ); 
  359.  
  360. // now create thumbnails 
  361. nggAdmin::do_ajax_operation( 'create_thumbnail' , $image_ids, __('Create new thumbnails', 'nggallery') ); 
  362.  
  363. //TODO:Message will not shown, because AJAX routine require more time, message should be passed to AJAX 
  364. $message = $created_msg . count($image_ids) .__(' picture(s) successfully added', 'nggallery'); 
  365. $message .= ' [<a href="' . admin_url() . 'admin.php?page=nggallery-manage&mode=image&gid=' . $gallery_id . '" >'; 
  366. $message .= __('Edit gallery', 'nggallery'); 
  367. $message .= '</a>]'; 
  368.  
  369. nggGallery::show_message($message);  
  370.  
  371. return; 
  372.  
  373.  
  374. /** 
  375. * Scan folder for new images 
  376. *  
  377. * @class nggAdmin 
  378. * @param string $dirname 
  379. * @return array $files list of image filenames  
  380. */ 
  381. static function scandir( $dirname = '.' ) { 
  382. $ext = apply_filters('ngg_allowed_file_types', array('jpeg', 'jpg', 'png', 'gif') ); 
  383.  
  384. $files = array();  
  385. if( $handle = opendir( $dirname ) ) {  
  386. while( false !== ( $file = readdir( $handle ) ) ) { 
  387. $info = pathinfo( $file ); 
  388. // just look for images with the correct extension 
  389. if ( isset($info['extension']) ) 
  390. if ( in_array( strtolower($info['extension']), $ext) ) 
  391. $files[] = utf8_encode( $file ); 
  392. }  
  393. closedir( $handle );  
  394. }  
  395. sort( $files ); 
  396. return ( $files );  
  397.  
  398. /** 
  399. * nggAdmin::createThumbnail() - function to create or recreate a thumbnail 
  400. * @param nggImage|int $image The image or its id. 
  401. * @param int $width The width of the new thumbnail. 
  402. * @param int $height The height of the new thumbnail. 
  403. * @param null|bool $fix If the ratio should be respected or not. 
  404. * @return string result code 
  405. * @since v1.0.0 
  406. * @since 1.9.27 The options for the dimensions are new, but backwards compatible. 
  407. */ 
  408. static function create_thumbnail( $image, $width = 0, $height = 0, $fix = null ) { 
  409.  
  410. $options = get_option( 'ngg_options' ); 
  411.  
  412. /** 
  413. * Set the default variables. 
  414. */ 
  415. if ( $width == 0 ) { 
  416. $width = $options['thumbwidth']; 
  417. if ( $height == 0 ) { 
  418. $height = $options['thumbheight']; 
  419. if ( $fix === null ) { 
  420. $fix = $options['thumbfix']; 
  421.  
  422.  
  423. if ( ! class_exists( 'ngg_Thumbnail' ) ) { 
  424. require_once( nggGallery::graphic_library() ); 
  425.  
  426. if ( is_numeric( $image ) ) { 
  427. $image = nggdb::find_image( $image ); 
  428.  
  429. if ( ! is_object( $image ) ) { 
  430. return __( 'Object didn\'t contain correct data', 'nggallery' ); 
  431.  
  432. // before we start we import the meta data to database (required for uploads before V1.4.0) 
  433. nggAdmin::maybe_import_meta( $image->pid ); 
  434.  
  435. // check for existing thumbnail 
  436. if ( file_exists( $image->thumbPath ) ) { 
  437. if ( ! is_writable( $image->thumbPath ) ) { 
  438. return esc_html( $image->filename ) . __( ' is not writeable ', 'nggallery' ); 
  439.  
  440. $thumb = new ngg_Thumbnail( $image->imagePath, true ); 
  441.  
  442. // skip if file is not there 
  443. if ( ! $thumb->error ) { 
  444. if ( $fix ) { 
  445.  
  446. // calculate correct ratio 
  447. $wratio = $width / $thumb->currentDimensions['width']; 
  448. $hratio = $height / $thumb->currentDimensions['height']; 
  449.  
  450. if ( $wratio > $hratio ) { 
  451. // first resize to the wanted width 
  452. $thumb->resize( $width, 0 ); 
  453. // get optimal y startpos 
  454. $ypos = ( $thumb->currentDimensions['height'] - $height ) / 2; 
  455. $thumb->crop( 0, $ypos, $width, $height ); 
  456. } else { 
  457. // first resize to the wanted height 
  458. $thumb->resize( 0, $height ); 
  459. // get optimal x startpos 
  460. $xpos = ( $thumb->currentDimensions['width'] - $width ) / 2; 
  461. $thumb->crop( $xpos, 0, $width, $height ); 
  462. //this create a thumbnail but keep ratio settings 
  463. } else { 
  464. $thumb->resize( $width, $height ); 
  465.  
  466. // save the new thumbnail 
  467. $thumb->save( $image->thumbPath, $options['thumbquality'] ); 
  468. nggAdmin::chmod( $image->thumbPath ); 
  469.  
  470. //read the new sizes 
  471. $new_size = @getimagesize( $image->thumbPath ); 
  472. $size['width'] = $new_size[0]; 
  473. $size['height'] = $new_size[1]; 
  474.  
  475. // add them to the database 
  476. nggdb::update_image_meta( $image->pid, array( 'thumbnail' => $size ) ); 
  477.  
  478. $thumb->destruct(); 
  479.  
  480. if ( ! empty( $thumb->errmsg ) ) { 
  481. return ' <strong>' . esc_html( $image->filename ) . ' (Error : ' . $thumb->errmsg . ')</strong>'; 
  482.  
  483. // success 
  484. return '1'; 
  485.  
  486. /** 
  487. * nggAdmin::resize_image() - create a new image, based on the height /width 
  488. *  
  489. * @class nggAdmin 
  490. * @param object | int $image contain all information about the image or the id 
  491. * @param integer $width optional  
  492. * @param integer $height optional 
  493. * @return string result code 
  494. */ 
  495. static function resize_image($image, $width = 0, $height = 0) { 
  496.  
  497. global $ngg; 
  498.  
  499. if(! class_exists('ngg_Thumbnail')) 
  500. require_once( nggGallery::graphic_library() ); 
  501.  
  502. if ( is_numeric($image) ) 
  503. $image = nggdb::find_image( $image ); 
  504.  
  505. if ( !is_object($image) )  
  506. return __('Object didn\'t contain correct data', 'nggallery');  
  507.  
  508. // before we start we import the meta data to database (required for uploads before V1.4.0) 
  509. nggAdmin::maybe_import_meta( $image->pid ); 
  510.  
  511. // if no parameter is set, take global settings 
  512. $width = ($width == 0) ? $ngg->options['imgWidth'] : $width; 
  513. $height = ($height == 0) ? $ngg->options['imgHeight'] : $height; 
  514.  
  515. if (!is_writable($image->imagePath)) 
  516. return ' <strong>' . esc_html( $image->filename ) . __(' is not writeable', 'nggallery') . '</strong>'; 
  517.  
  518. $file = new ngg_Thumbnail($image->imagePath, TRUE); 
  519.  
  520. // skip if file is not there 
  521. if (!$file->error) { 
  522.  
  523. // If required save a backup copy of the file 
  524. if ( ($ngg->options['imgBackup'] == 1) && (!file_exists($image->imagePath . '_backup')) ) 
  525. @copy ($image->imagePath, $image->imagePath . '_backup'); 
  526.  
  527. $file->resize($width, $height); 
  528. $file->save($image->imagePath, $ngg->options['imgQuality']); 
  529. // read the new sizes 
  530. $size = @getimagesize ( $image->imagePath ); 
  531. // add them to the database 
  532. nggdb::update_image_meta($image->pid, array( 'width' => $size[0], 'height' => $size[1] ) ); 
  533. $file->destruct(); 
  534. } else { 
  535. $file->destruct(); 
  536. return ' <strong>' . esc_html( $image->filename ) . ' (Error : ' . $file->errmsg . ')</strong>'; 
  537.  
  538. return '1'; 
  539.  
  540. /** 
  541. * Rotated/Flip an image based on the orientation flag or a definded angle 
  542. *  
  543. * @param int|object $image 
  544. * @param string (optional) $dir, CW (clockwise)or CCW (counter clockwise), if set to false, the exif flag will be used 
  545. * @param string (optional) $flip, could be either false | V (flip vertical) | H (flip horizontal) 
  546. * @return string result code 
  547. */ 
  548. static function rotate_image($image, $dir = false, $flip = false) { 
  549.  
  550. global $ngg; 
  551.  
  552. if(! class_exists('ngg_Thumbnail')) 
  553. require_once( nggGallery::graphic_library() ); 
  554.  
  555. if ( is_numeric($image) ) 
  556. $image = nggdb::find_image( $image ); 
  557.  
  558. if ( !is_object($image) )  
  559. return __('Object didn\'t contain correct data', 'nggallery');  
  560.  
  561. if (!is_writable($image->imagePath)) 
  562. return ' <strong>' . esc_html( $image->filename ) . __(' is not writeable', 'nggallery') . '</strong>'; 
  563.  
  564. // if you didn't define a rotation, we look for the orientation flag in EXIF 
  565. if ( $dir === false ) { 
  566. $meta = new nggMeta( $image->pid ); 
  567. $exif = $meta->get_EXIF(); 
  568.  
  569. if (isset($exif['Orientation'])) { 
  570.  
  571. switch ($exif['Orientation']) { 
  572. case 5 : // vertical flip + 90 rotate right 
  573. $flip = 'V'; 
  574. case 6 : // 90 rotate right 
  575. $dir = 'CW'; 
  576. break; 
  577. case 7 : // horizontal flip + 90 rotate right 
  578. $flip = 'H'; 
  579. case 8 : // 90 rotate left 
  580. $dir = 'CCW'; 
  581. break; 
  582. case 4 : // vertical flip 
  583. $flip = 'V'; 
  584. break; 
  585. case 3 : // 180 rotate left 
  586. $dir = 180; 
  587. break; 
  588. case 2 : // horizontal flip 
  589. $flip = 'H'; 
  590. break;  
  591. case 1 : // no action in the case it doesn't need a rotation 
  592. default: 
  593. return '0'; 
  594. break;  
  595. } else 
  596. return '0'; 
  597. $file = new ngg_Thumbnail( $image->imagePath, TRUE ); 
  598.  
  599. // skip if file is not there 
  600. if (!$file->error) { 
  601.  
  602. // If required save a backup copy of the file 
  603. if ( ($ngg->options['imgBackup'] == 1) && (!file_exists($image->imagePath . '_backup')) ) 
  604. @copy ($image->imagePath, $image->imagePath . '_backup'); 
  605.  
  606. // before we start we import the meta data to database (required for uploads before V1.4.X) 
  607. nggAdmin::maybe_import_meta( $image->pid ); 
  608.  
  609. if ( $dir !== 0 ) 
  610. $file->rotateImage( $dir ); 
  611. if ( $dir === 180) 
  612. $file->rotateImage( 'CCW' ); // very special case, we rotate the image two times 
  613. if ( $flip == 'H') 
  614. $file->flipImage(true, false); 
  615. if ( $flip == 'V') 
  616. $file->flipImage(false, true); 
  617.  
  618. $file->save($image->imagePath, $ngg->options['imgQuality']); 
  619.  
  620. // read the new sizes 
  621. $size = @getimagesize ( $image->imagePath ); 
  622. // add them to the database 
  623. nggdb::update_image_meta($image->pid, array( 'width' => $size[0], 'height' => $size[1] ) ); 
  624.  
  625.  
  626. $file->destruct(); 
  627.  
  628. if ( !empty($file->errmsg) ) 
  629. return ' <strong>' . esc_html( $image->filename ) . ' (Error : '.$file->errmsg .')</strong>';  
  630.  
  631. return '1'; 
  632.  
  633.  
  634. /** 
  635. * nggAdmin::set_watermark() - set the watermark for the image 
  636. *  
  637. * @class nggAdmin 
  638. * @param object | int $image contain all information about the image or the id 
  639. * @return string result code 
  640. */ 
  641. static function set_watermark($image) { 
  642.  
  643. global $ngg; 
  644.  
  645. if(! class_exists('ngg_Thumbnail')) 
  646. require_once( nggGallery::graphic_library() ); 
  647.  
  648. if ( is_numeric($image) ) 
  649. $image = nggdb::find_image( $image ); 
  650.  
  651. if ( !is_object($image) )  
  652. return __('Object didn\'t contain correct data', 'nggallery');  
  653.  
  654. // before we start we import the meta data to database (required for uploads before V1.4.0) 
  655. nggAdmin::maybe_import_meta( $image->pid );  
  656.  
  657. if (!is_writable($image->imagePath)) 
  658. return ' <strong>' . esc_html( $image->filename ) . __(' is not writeable', 'nggallery') . '</strong>'; 
  659.  
  660. $file = new ngg_Thumbnail( $image->imagePath, TRUE ); 
  661.  
  662. // skip if file is not there 
  663. if (!$file->error) { 
  664.  
  665. // If required save a backup copy of the file 
  666. if ( ($ngg->options['imgBackup'] == 1) && (!file_exists($image->imagePath . '_backup')) ) 
  667. @copy ($image->imagePath, $image->imagePath . '_backup'); 
  668.  
  669. if ($ngg->options['wmType'] == 'image') { 
  670. $file->watermarkImgPath = $ngg->options['wmPath']; 
  671. $file->watermarkImage($ngg->options['wmPos'], $ngg->options['wmXpos'], $ngg->options['wmYpos']);  
  672. if ($ngg->options['wmType'] == 'text') { 
  673. $file->watermarkText = $ngg->options['wmText']; 
  674. $file->watermarkCreateText($ngg->options['wmColor'], $ngg->options['wmFont'], $ngg->options['wmSize'], $ngg->options['wmOpaque']); 
  675. $file->watermarkImage($ngg->options['wmPos'], $ngg->options['wmXpos'], $ngg->options['wmYpos']);  
  676. $file->save($image->imagePath, $ngg->options['imgQuality']); 
  677.  
  678. $file->destruct(); 
  679.  
  680. if ( !empty($file->errmsg) ) 
  681. return ' <strong>' . esc_html( $image->filename ) . ' (Error : '.$file->errmsg .')</strong>';  
  682.  
  683. return '1'; 
  684.  
  685. /** 
  686. * Recover image from backup copy and reprocess it 
  687. *  
  688. * @class nggAdmin 
  689. * @since 1.5.0 
  690. * @param object | int $image contain all information about the image or the id 
  691. * @return string result code 
  692. */ 
  693.  
  694. static function recover_image($image) { 
  695.  
  696. global $ngg; 
  697.  
  698. if ( is_numeric($image) ) 
  699. $image = nggdb::find_image( $image ); 
  700.  
  701. if ( !is_object( $image ) )  
  702. return __('Object didn\'t contain correct data', 'nggallery');  
  703.  
  704. if (!is_writable( $image->imagePath )) 
  705. return ' <strong>' . esc_html( $image->filename ) . __(' is not writeable', 'nggallery') . '</strong>'; 
  706.  
  707. if (!file_exists( $image->imagePath . '_backup' )) { 
  708. return ' <strong>'.__('File do not exists', 'nggallery').'</strong>'; 
  709.  
  710. if (!@copy( $image->imagePath . '_backup' , $image->imagePath) ) 
  711. return ' <strong>'.__('Couldn\'t restore original image', 'nggallery').'</strong>'; 
  712.  
  713. require_once(NGGALLERY_ABSPATH . '/lib/meta.php'); 
  714.  
  715. $meta_obj = new nggMeta( $image->pid ); 
  716.  
  717. $common = $meta_obj->get_common_meta(); 
  718. $common['saved'] = true;  
  719. $result = nggdb::update_image_meta($image->pid, $common);  
  720.  
  721. return '1'; 
  722.  
  723.  
  724. /** 
  725. * Add images to database 
  726. *  
  727. * @class nggAdmin 
  728. * @param int $galleryID 
  729. * @param array $imageslist 
  730. * @return array $image_ids Id's which are sucessful added 
  731. */ 
  732. static function add_Images($galleryID, $imageslist) { 
  733.  
  734. global $wpdb, $ngg; 
  735.  
  736. $image_ids = array(); 
  737.  
  738. if ( is_array($imageslist) ) { 
  739. foreach($imageslist as $picture) { 
  740.  
  741. // filter function to rename/change/modify image before 
  742. $picture = apply_filters('ngg_pre_add_new_image', $picture, $galleryID); 
  743.  
  744. // strip off the extension of the filename 
  745. $path_parts = pathinfo( $picture ); 
  746. $alttext = ( !isset($path_parts['filename']) ) ? substr($path_parts['basename'], 0, strpos($path_parts['basename'], '.')) : $path_parts['filename']; 
  747. // save it to the database 
  748. $pic_id = nggdb::add_image( $galleryID, $picture, '', $alttext );  
  749.  
  750. if ( !empty($pic_id) )  
  751. $image_ids[] = $pic_id; 
  752.  
  753. // add the metadata 
  754. nggAdmin::import_MetaData( $pic_id ); 
  755.  
  756. // auto rotate 
  757. nggAdmin::rotate_image( $pic_id );  
  758.  
  759. // Autoresize image if required 
  760. if ($ngg->options['imgAutoResize']) { 
  761. $imagetmp = nggdb::find_image( $pic_id ); 
  762. $sizetmp = @getimagesize ( $imagetmp->imagePath ); 
  763. $widthtmp = $ngg->options['imgWidth']; 
  764. $heighttmp = $ngg->options['imgHeight']; 
  765. if (($sizetmp[0] > $widthtmp && $widthtmp) || ($sizetmp[1] > $heighttmp && $heighttmp)) { 
  766. nggAdmin::resize_image( $pic_id ); 
  767.  
  768. // action hook for post process after the image is added to the database 
  769. $image = array( 'id' => $pic_id, 'filename' => $picture, 'galleryID' => $galleryID); 
  770. do_action('ngg_added_new_image', $image); 
  771.  
  772. }  
  773. } // is_array 
  774.  
  775. // delete dirsize after adding new images 
  776. delete_transient( 'dirsize_cache' ); 
  777.  
  778. do_action('ngg_after_new_images_added', $galleryID, $image_ids ); 
  779.  
  780. return $image_ids; 
  781.  
  782.  
  783. /** 
  784. * Import some meta data into the database (if avialable) 
  785. *  
  786. * @class nggAdmin 
  787. * @param array|int $imagesIds 
  788. * @return string result code 
  789. */ 
  790. static function import_MetaData($imagesIds) { 
  791.  
  792. global $wpdb; 
  793.  
  794. require_once(NGGALLERY_ABSPATH . '/lib/image.php'); 
  795.  
  796. if (!is_array($imagesIds)) 
  797. $imagesIds = array($imagesIds); 
  798.  
  799. foreach($imagesIds as $imageID) { 
  800.  
  801. $image = nggdb::find_image( $imageID ); 
  802. if (!$image->error) { 
  803.  
  804. $meta = nggAdmin::get_MetaData( $image->pid ); 
  805.  
  806. // get the title 
  807. $alttext = empty( $meta['title'] ) ? $image->alttext : $meta['title']; 
  808.  
  809. // get the caption / description field 
  810. $description = empty( $meta['caption'] ) ? $image->description : $meta['caption']; 
  811.  
  812. // get the file date/time from exif 
  813. $timestamp = $meta['timestamp']; 
  814. // first update database 
  815. $result = $wpdb->query(  
  816. $wpdb->prepare("UPDATE $wpdb->nggpictures SET  
  817. alttext = %s,  
  818. description = %s,  
  819. imagedate = %s 
  820. WHERE pid = %d", $alttext, $description, $timestamp, $image->pid) ); 
  821.  
  822. if ($result === false) 
  823. return ' <strong>' . esc_html( $image->filename ) . ' ' . __('(Error : Couldn\'t not update data base)', 'nggallery') . '</strong>';  
  824.  
  825. //this flag will inform us that the import is already one time performed 
  826. $meta['common']['saved'] = true;  
  827. $result = nggdb::update_image_meta($image->pid, $meta['common']); 
  828.  
  829. if ($result === false) 
  830. return ' <strong>' . esc_html( $image->filename ) . ' ' . __('(Error : Couldn\'t not update meta data)', 'nggallery') . '</strong>';  
  831.  
  832. // add the tags if we found some 
  833. if ($meta['keywords']) { 
  834. $taglist = explode(', ', $meta['keywords']); 
  835. wp_set_object_terms($image->pid, $taglist, 'ngg_tag'); 
  836.  
  837. } else 
  838. return ' <strong>' . esc_html( $image->filename ) . ' ' . __('(Error : Couldn\'t not find image)', 'nggallery') . '</strong>';// error check 
  839.  
  840. return '1';  
  841.  
  842. /** 
  843. * nggAdmin::get_MetaData() 
  844. *  
  845. * @class nggAdmin 
  846. * @require NextGEN Meta class 
  847. * @param int $id image ID 
  848. * @return array metadata 
  849. */ 
  850. static function get_MetaData($id) { 
  851.  
  852. require_once(NGGALLERY_ABSPATH . '/lib/meta.php'); 
  853.  
  854. $meta = array(); 
  855.  
  856. $pdata = new nggMeta( $id ); 
  857.  
  858. $meta['title'] = trim ( $pdata->get_META('title') );  
  859. $meta['caption'] = trim ( $pdata->get_META('caption') );  
  860. $meta['keywords'] = trim ( $pdata->get_META('keywords') ); 
  861. $meta['timestamp'] = $pdata->get_date_time(); 
  862. // this contain other useful meta information 
  863. $meta['common'] = $pdata->get_common_meta(); 
  864. // hook for addon plugin to add more meta fields 
  865. $meta = apply_filters('ngg_get_image_metadata', $meta, $pdata); 
  866.  
  867. return $meta; 
  868.  
  869.  
  870. /** 
  871. * Maybe import some meta data to the database. The functions checks the flag 'saved' 
  872. * and if based on compat reason (pre V1.4.0) we save then some meta datas to the database 
  873. *  
  874. * @since V1.4.0 
  875. * @param int $id 
  876. * @return result 
  877. */ 
  878. static function maybe_import_meta( $id ) { 
  879.  
  880. require_once(NGGALLERY_ABSPATH . '/lib/meta.php'); 
  881.  
  882. $meta_obj = new nggMeta( $id ); 
  883.  
  884. if ( $meta_obj->image->meta_data['saved'] != true ) { 
  885. $common = $meta_obj->get_common_meta(); 
  886. //this flag will inform us that the import is already one time performed 
  887. $common['saved'] = true;  
  888. $result = nggdb::update_image_meta($id, $common); 
  889. } else 
  890. return false; 
  891.  
  892. return $result;  
  893.  
  894.  
  895. /** 
  896. * Unzip a file via the PclZip class 
  897. *  
  898. * @class nggAdmin 
  899. * @require PclZip class 
  900. * @param string $dir 
  901. * @param string $file 
  902. * @return bool 
  903. */ 
  904. static function unzip($dir, $file) { 
  905.  
  906. if(! class_exists('PclZip')) 
  907. require_once(ABSPATH . 'wp-admin/includes/class-pclzip.php'); 
  908.  
  909. $archive = new PclZip($file); 
  910.  
  911. // extract all files in one folder 
  912. if ($archive->extract(PCLZIP_OPT_PATH, $dir, PCLZIP_OPT_REMOVE_ALL_PATH,  
  913. PCLZIP_CB_PRE_EXTRACT, 'ngg_getOnlyImages',  
  914. PCLZIP_CB_POST_EXTRACT, 'ngg_checkExtract') == 0) { 
  915. nggGallery::show_error( 'Error : ' . $archive->errorInfo(true) ); 
  916. return false; 
  917.  
  918. return true; 
  919.  
  920. /** 
  921. * nggAdmin::getOnlyImages() 
  922. *  
  923. * @class nggAdmin 
  924. * @param mixed $p_event 
  925. * @param mixed $p_header 
  926. * @return bool 
  927. */ 
  928. static function getOnlyImages($p_event, &$p_header) { 
  929. // avoid null byte hack (THX to Dominic Szablewski) 
  930. if ( strpos($p_header['filename'], chr(0) ) !== false )  
  931. $p_header['filename'] = substr ( $p_header['filename'], 0, strpos($p_header['filename'], chr(0) ));  
  932. // check for extension 
  933. $info = pathinfo($p_header['filename']); 
  934. // check for extension 
  935. $ext = apply_filters('ngg_allowed_file_types', array('jpeg', 'jpg', 'png', 'gif') );  
  936. if ( in_array( strtolower($info['extension']), $ext) ) { 
  937. // For MAC skip the ".image" files 
  938. if ($info['basename']{0} == '.' )  
  939. return 0; 
  940. else { 
  941. // sanitize the file name before we do further processing 
  942. $info['basename'] = sanitize_file_name( $info['basename'] ); 
  943. $p_header['filename'] = $info['dirname'] . '/' . $info['basename']; 
  944. return 1; 
  945.  
  946. // ----- all other files are skipped 
  947. else { 
  948. return 0; 
  949.  
  950. /** 
  951. * Import a ZIP file via a upload form or a URL 
  952. *  
  953. * @class nggAdmin 
  954. * @param int (optional) $galleryID 
  955. * @return bool $result 
  956. */ 
  957. static function import_zipfile($galleryID) { 
  958.  
  959. global $ngg, $wpdb; 
  960.  
  961. if (nggWPMU::check_quota()) 
  962. return false; 
  963.  
  964. $defaultpath = $ngg->options['gallerypath'];  
  965. $zipurl = isset($_POST['zipurl'])?$_POST['zipurl']:""; 
  966.  
  967. // if someone entered a URL try to upload it 
  968. if (!empty($zipurl) && (function_exists('curl_init')) ) { 
  969.  
  970. if (!(preg_match('/^http(s)?:\/\//i', $zipurl) )) { 
  971. nggGallery::show_error( __('No valid URL path ', 'nggallery') ); 
  972. return false;  
  973.  
  974. $temp_zipfile = tempnam('/tmp', 'zipimport_'); 
  975. $filename = basename($zipurl); 
  976.  
  977. //Grab the zip via cURL 
  978. $save = fopen ( $temp_zipfile, "w" ); 
  979. $ch = curl_init (); 
  980. curl_setopt ( $ch, CURLOPT_FILE, $save ); 
  981. curl_setopt ( $ch, CURLOPT_HEADER, 0 ); 
  982. curl_setopt ( $ch, CURLOPT_BINARYTRANSFER, 1 ); 
  983. curl_setopt ( $ch, CURLOPT_URL, $zipurl ); 
  984. $success = curl_exec ( $ch ); 
  985. if (!$success) 
  986. nggGallery::show_error( __('Import via cURL failed.', 'nggallery') . ' Error code ' . curl_errno( $ch ) . ' : ' . curl_error( $ch ) ); 
  987. curl_close ( $ch ); 
  988. fclose($save); 
  989.  
  990. if (!$success) 
  991. return false;  
  992.  
  993. } else { 
  994.  
  995. $temp_zipfile = $_FILES['zipfile']['tmp_name']; 
  996. $filename = $_FILES['zipfile']['name'];  
  997.  
  998. // Chrome return a empty content-type : http://code.google.com/p/chromium/issues/detail?id=6800 
  999. if ( !preg_match('/chrome/i', $_SERVER['HTTP_USER_AGENT']) ) { 
  1000. // check if file is a zip file 
  1001. if ( !preg_match('/(zip|download|octet-stream)/i', $_FILES['zipfile']['type']) ) { 
  1002. @unlink($temp_zipfile); // del temp file 
  1003. nggGallery::show_error(__('Uploaded file was no or a faulty zip file ! The server recognized : ', 'nggallery') . $_FILES['zipfile']['type']); 
  1004. return false;  
  1005.  
  1006. // should this unpacked into a new folder ?  
  1007. if ( $galleryID == '0' ) {  
  1008. //cleanup and take the zipfile name as folder name 
  1009. $foldername = sanitize_title(strtok ($filename, '.')); 
  1010. $foldername = $defaultpath . $foldername; 
  1011. } else { 
  1012. // get foldername if selected 
  1013. $foldername = $wpdb->get_var("SELECT path FROM $wpdb->nggallery WHERE gid = '$galleryID' "); 
  1014.  
  1015. if ( empty($foldername) ) { 
  1016. nggGallery::show_error( __('Could not get a valid foldername', 'nggallery') ); 
  1017. return false; 
  1018.  
  1019. // set complete folder path 
  1020. $newfolder = WINABSPATH . $foldername; 
  1021.  
  1022. // check first if the traget folder exist 
  1023. if (!is_dir($newfolder)) { 
  1024. // create new directories 
  1025. if (!wp_mkdir_p ($newfolder)) { 
  1026. $message = sprintf(__('Unable to create directory %s. Is its parent directory writable by the server?', 'nggallery'), esc_html( $newfolder ) ); 
  1027. nggGallery::show_error($message); 
  1028. return false; 
  1029. if (!wp_mkdir_p ($newfolder . '/thumbs')) { 
  1030. nggGallery::show_error(__('Unable to create directory ', 'nggallery') . esc_html( $newfolder ). '/thumbs !'); 
  1031. return false; 
  1032. }  
  1033.  
  1034. // unzip and del temp file  
  1035. $result = nggAdmin::unzip($newfolder, $temp_zipfile); 
  1036. @unlink($temp_zipfile);  
  1037.  
  1038. if ($result) { 
  1039. $message = __('Zip-File successfully unpacked', 'nggallery') . '<br />';  
  1040.  
  1041. // parse now the folder and add to database 
  1042. nggAdmin::import_gallery( $foldername ); 
  1043. nggGallery::show_message($message); 
  1044.  
  1045. return true; 
  1046.  
  1047. /** 
  1048. * Function for uploading of images via the upload form 
  1049. *  
  1050. * @class nggAdmin 
  1051. * @return void 
  1052. */ 
  1053. static function upload_images() { 
  1054.  
  1055. global $nggdb; 
  1056.  
  1057. // WPMU action 
  1058. if (nggWPMU::check_quota()) 
  1059. return; 
  1060.  
  1061. // Images must be an array 
  1062. $imageslist = array(); 
  1063.  
  1064. // get selected gallery 
  1065. $galleryID = (int) $_POST['galleryselect']; 
  1066.  
  1067. if ($galleryID == 0) { 
  1068. nggGallery::show_error(__('No gallery selected !', 'nggallery')); 
  1069. return;  
  1070.  
  1071. // get the path to the gallery  
  1072. $gallery = $nggdb->find_gallery($galleryID); 
  1073.  
  1074. if ( empty($gallery->path) ) { 
  1075. nggGallery::show_error(__('Failure in database, no gallery path set !', 'nggallery')); 
  1076. return; 
  1077. }  
  1078.  
  1079. // read list of images 
  1080. $dirlist = nggAdmin::scandir($gallery->abspath); 
  1081.  
  1082. $imagefiles = $_FILES['imagefiles']; 
  1083.  
  1084. if (is_array($imagefiles)) { 
  1085. foreach ($imagefiles['name'] as $key => $value) { 
  1086.  
  1087. // look only for uploded files 
  1088. if ($imagefiles['error'][$key] == 0) { 
  1089.  
  1090. $temp_file = $imagefiles['tmp_name'][$key]; 
  1091.  
  1092. //clean filename and extract extension 
  1093. $filepart = nggGallery::fileinfo( $imagefiles['name'][$key] ); 
  1094. $filename = $filepart['basename']; 
  1095.  
  1096. // check for allowed extension and if it's an image file 
  1097. $ext = array('jpg', 'png', 'gif');  
  1098. if ( !in_array($filepart['extension'], $ext) || !@getimagesize($temp_file) ) {  
  1099. nggGallery::show_error('<strong>' . esc_html( $imagefiles['name'][$key] ) . ' </strong>' . __('is no valid image file!', 'nggallery')); 
  1100. continue; 
  1101.  
  1102. // check if this filename already exist in the folder 
  1103. $i = 0; 
  1104. while ( in_array( $filename, $dirlist ) ) { 
  1105. $filename = $filepart['filename'] . '_' . $i++ . '.' .$filepart['extension']; 
  1106.  
  1107. $dest_file = $gallery->abspath . '/' . $filename; 
  1108.  
  1109. //check for folder permission 
  1110. if ( !is_writeable($gallery->abspath) ) { 
  1111. $message = sprintf(__('Unable to write to directory %s. Is this directory writable by the server?', 'nggallery'), esc_html($gallery->abspath) ); 
  1112. nggGallery::show_error($message); 
  1113. return;  
  1114.  
  1115. // save temp file to gallery 
  1116. if ( !@move_uploaded_file($temp_file, $dest_file) ) { 
  1117. nggGallery::show_error(__('Error, the file could not be moved to : ', 'nggallery') . esc_html( $dest_file ) ); 
  1118. nggAdmin::check_safemode( $gallery->abspath );  
  1119. continue; 
  1120. }  
  1121. if ( !nggAdmin::chmod($dest_file) ) { 
  1122. nggGallery::show_error(__('Error, the file permissions could not be set', 'nggallery')); 
  1123. continue; 
  1124.  
  1125. // add to imagelist & dirlist 
  1126. $imageslist[] = $filename; 
  1127. $dirlist[] = $filename; 
  1128.  
  1129.  
  1130. if (count($imageslist) > 0) { 
  1131.  
  1132. // add images to database  
  1133. $image_ids = nggAdmin::add_Images($galleryID, $imageslist); 
  1134.  
  1135. //create thumbnails 
  1136. nggAdmin::do_ajax_operation( 'create_thumbnail' , $image_ids, __('Create new thumbnails', 'nggallery') ); 
  1137.  
  1138. //add the preview image if needed 
  1139. nggAdmin::set_gallery_preview ( $galleryID ); 
  1140.  
  1141. nggGallery::show_message( count($image_ids) . __(' Image(s) successfully added', 'nggallery')); 
  1142.  
  1143. return; 
  1144.  
  1145.  
  1146. /** 
  1147. * Upload function will be called via the Flash uploader 
  1148. *  
  1149. * @class nggAdmin 
  1150. * @param integer $galleryID 
  1151. * @return string $result 
  1152. */ 
  1153. static function swfupload_image($galleryID = 0) { 
  1154.  
  1155. global $nggdb; 
  1156.  
  1157. if ($galleryID == 0) 
  1158. return __('No gallery selected !', 'nggallery'); 
  1159.  
  1160. // WPMU action 
  1161. if (nggWPMU::check_quota()) 
  1162. return '0'; 
  1163.  
  1164. // Check the upload 
  1165. if (!isset($_FILES['Filedata']) || !is_uploaded_file($_FILES['Filedata']['tmp_name']) || $_FILES['Filedata']['error'] != 0)  
  1166. return __('Invalid upload. Error Code : ', 'nggallery') . $_FILES['Filedata']['error']; 
  1167.  
  1168. // get the filename and extension 
  1169. $temp_file = $_FILES['Filedata']['tmp_name']; 
  1170.  
  1171. $filepart = nggGallery::fileinfo( $_FILES['Filedata']['name'] ); 
  1172. $filename = $filepart['basename']; 
  1173.  
  1174. // check for allowed extension 
  1175. $ext = apply_filters('ngg_allowed_file_types', array('jpeg', 'jpg', 'png', 'gif') );  
  1176. if (!in_array( strtolower( $filepart['extension'] ), $ext)) 
  1177. return esc_html( $_FILES[$key]['name'] ) . __('is no valid image file!', 'nggallery'); 
  1178.  
  1179. // get the path to the gallery 
  1180. $gallery = $nggdb->find_gallery( (int) $galleryID );  
  1181. if ( empty($gallery->path) ) { 
  1182. @unlink($temp_file);  
  1183. return __('Failure in database, no gallery path set !', 'nggallery'); 
  1184. }  
  1185.  
  1186. // read list of images 
  1187. $imageslist = nggAdmin::scandir( WINABSPATH . $gallery->path ); 
  1188.  
  1189. // check if this filename already exist 
  1190. $i = 0; 
  1191. while (in_array($filename, $imageslist)) { 
  1192. $filename = $filepart['filename'] . '_' . $i++ . '.' . $filepart['extension']; 
  1193.  
  1194. $dest_file = WINABSPATH . $gallery->path . '/' . $filename; 
  1195.  
  1196. // save temp file to gallery 
  1197. if ( !@move_uploaded_file($_FILES["Filedata"]['tmp_name'], $dest_file) ) { 
  1198. nggAdmin::check_safemode(WINABSPATH . $gallery->path);  
  1199. return __('Error, the file could not be moved to : ', 'nggallery'). esc_html( $dest_file ); 
  1200. }  
  1201.  
  1202. if ( !nggAdmin::chmod($dest_file) ) 
  1203. return __('Error, the file permissions could not be set', 'nggallery'); 
  1204.  
  1205. return '0'; 
  1206. }  
  1207.  
  1208. /** 
  1209. * Set correct file permissions (taken from wp core) 
  1210. *  
  1211. * @class nggAdmin 
  1212. * @param string $filename 
  1213. * @return bool $result 
  1214. */ 
  1215. static function chmod($filename = '') { 
  1216.  
  1217. $stat = @ stat( dirname($filename) ); 
  1218. $perms = $stat['mode'] & 0000666; // Remove execute bits for files 
  1219. if ( @chmod($filename, $perms) ) 
  1220. return true; 
  1221.  
  1222. return false; 
  1223.  
  1224. /** 
  1225. * Check UID in folder and Script 
  1226. * Read http://www.php.net/manual/en/features.safe-mode.php to understand safe_mode 
  1227. *  
  1228. * @class nggAdmin 
  1229. * @param string $foldername 
  1230. * @return bool $result 
  1231. */ 
  1232. static function check_safemode($foldername) { 
  1233.  
  1234. if ( SAFE_MODE ) { 
  1235.  
  1236. $script_uid = ( ini_get('safe_mode_gid') ) ? getmygid() : getmyuid(); 
  1237. $folder_uid = fileowner($foldername); 
  1238.  
  1239. if ($script_uid != $folder_uid) { 
  1240. $message = sprintf(__('SAFE MODE Restriction in effect! You need to create the folder <strong>%s</strong> manually', 'nggallery'), esc_html( $foldername ) ); 
  1241. $message .= '<br />' . sprintf(__('When safe_mode is on, PHP checks to see if the owner (%s) of the current script matches the owner (%s) of the file to be operated on by a file function or its directory', 'nggallery'), $script_uid, $folder_uid ); 
  1242. nggGallery::show_error($message); 
  1243. return false; 
  1244.  
  1245. return true; 
  1246.  
  1247. /** 
  1248. * Capability check. Check is the ID fit's to the user_ID 
  1249. *  
  1250. * @class nggAdmin 
  1251. * @param int $check_ID is the user_id 
  1252. * @return bool $result 
  1253. */ 
  1254. static function can_manage_this_gallery($check_ID) { 
  1255.  
  1256. global $user_ID, $wp_roles; 
  1257.  
  1258. if ( !current_user_can('NextGEN Manage others gallery') ) { 
  1259. // get the current user ID 
  1260. get_currentuserinfo(); 
  1261.  
  1262. if ( $user_ID != $check_ID) 
  1263. return false; 
  1264.  
  1265. return true; 
  1266.  
  1267.  
  1268. /** 
  1269. * Move images from one folder to another 
  1270. *  
  1271. * @class nggAdmin 
  1272. * @param array|int $pic_ids ID's of the images 
  1273. * @param int $dest_gid destination gallery 
  1274. * @return void 
  1275. */ 
  1276. static function move_images($pic_ids, $dest_gid) { 
  1277.  
  1278. $errors = ''; 
  1279. $count = 0; 
  1280.  
  1281. if ( !is_array($pic_ids) ) 
  1282. $pic_ids = array($pic_ids); 
  1283.  
  1284. // Get destination gallery 
  1285. $destination = nggdb::find_gallery( $dest_gid ); 
  1286. $dest_abspath = WINABSPATH . $destination->path; 
  1287.  
  1288. if ( $destination == null ) { 
  1289. nggGallery::show_error(__('The destination gallery does not exist', 'nggallery')); 
  1290. return; 
  1291.  
  1292. // Check for folder permission 
  1293. if ( !is_writeable( $dest_abspath ) ) { 
  1294. $message = sprintf(__('Unable to write to directory %s. Is this directory writable by the server?', 'nggallery'), esc_html( $dest_abspath ) ); 
  1295. nggGallery::show_error($message); 
  1296. return;  
  1297.  
  1298. // Get pictures 
  1299. $images = nggdb::find_images_in_list($pic_ids); 
  1300.  
  1301. foreach ($images as $image) {  
  1302.  
  1303. $i = 0; 
  1304. $tmp_prefix = ''; 
  1305.  
  1306. $destination_file_name = $image->filename; 
  1307. // check if the filename already exist, then we add a copy_ prefix 
  1308. while (file_exists( $dest_abspath . '/' . $destination_file_name)) { 
  1309. $tmp_prefix = 'copy_' . ($i++) . '_'; 
  1310. $destination_file_name = $tmp_prefix . $image->filename; 
  1311.  
  1312. $destination_path = $dest_abspath . '/' . $destination_file_name; 
  1313. $destination_thumbnail = $dest_abspath . '/thumbs/thumbs_' . $destination_file_name; 
  1314.  
  1315. // Move files 
  1316. if ( !@rename($image->imagePath, $destination_path) ) { 
  1317. $errors .= sprintf(__('Failed to move image %1$s to %2$s', 'nggallery'),  
  1318. '<strong>' . esc_html( $image->filename ) . '</strong>', esc_html( $destination_path ) ) . '<br />'; 
  1319. continue;  
  1320.  
  1321. // Move backup file, if possible 
  1322. @rename($image->imagePath . '_backup', $destination_path . '_backup'); 
  1323. // Move the thumbnail, if possible 
  1324. @rename($image->thumbPath, $destination_thumbnail); 
  1325.  
  1326. // Change the gallery id in the database , maybe the filename 
  1327. if ( nggdb::update_image($image->pid, $dest_gid, $destination_file_name) ) 
  1328. $count++; 
  1329.  
  1330.  
  1331. if ( $errors != '' ) 
  1332. nggGallery::show_error($errors); 
  1333.  
  1334. $link = '<a href="' . admin_url() . 'admin.php?page=nggallery-manage&mode=image&gid=' . $destination->gid . '" >' . esc_html( $destination->title ) . '</a>'; 
  1335. $messages = sprintf(__('Moved %1$s picture(s) to gallery : %2$s .', 'nggallery'), $count, $link); 
  1336. nggGallery::show_message($messages); 
  1337.  
  1338. return; 
  1339.  
  1340. /** 
  1341. * Copy images to another gallery 
  1342. *  
  1343. * @class nggAdmin 
  1344. * @param array|int $pic_ids ID's of the images 
  1345. * @param int $dest_gid destination gallery 
  1346. * @return void 
  1347. */ 
  1348. static function copy_images($pic_ids, $dest_gid) { 
  1349.  
  1350. require_once(NGGALLERY_ABSPATH . '/lib/meta.php'); 
  1351.  
  1352. $errors = $messages = ''; 
  1353.  
  1354. if (!is_array($pic_ids)) 
  1355. $pic_ids = array($pic_ids); 
  1356.  
  1357. // Get destination gallery 
  1358. $destination = nggdb::find_gallery( $dest_gid ); 
  1359. if ( $destination == null ) { 
  1360. nggGallery::show_error(__('The destination gallery does not exist', 'nggallery')); 
  1361. return; 
  1362.  
  1363. // Check for folder permission 
  1364. if (!is_writeable(WINABSPATH.$destination->path)) { 
  1365. $message = sprintf(__('Unable to write to directory %s. Is this directory writable by the server?', 'nggallery'), esc_html( WINABSPATH.$destination->path) ); 
  1366. nggGallery::show_error($message); 
  1367. return;  
  1368.  
  1369. // Get pictures 
  1370. $images = nggdb::find_images_in_list($pic_ids); 
  1371. $destination_path = WINABSPATH . $destination->path; 
  1372.  
  1373. foreach ($images as $image) {  
  1374. // WPMU action 
  1375. if ( nggWPMU::check_quota() ) 
  1376. return; 
  1377.  
  1378. $i = 0; 
  1379. $tmp_prefix = '';  
  1380. $destination_file_name = $image->filename; 
  1381. while (file_exists($destination_path . '/' . $destination_file_name)) { 
  1382. $tmp_prefix = 'copy_' . ($i++) . '_'; 
  1383. $destination_file_name = $tmp_prefix . $image->filename; 
  1384.  
  1385. $destination_file_path = $destination_path . '/' . $destination_file_name; 
  1386. $destination_thumb_file_path = $destination_path . '/' . $image->thumbFolder . $image->thumbPrefix . $destination_file_name; 
  1387.  
  1388. // Copy files 
  1389. if ( !@copy($image->imagePath, $destination_file_path) ) { 
  1390. $errors .= sprintf(__('Failed to copy image %1$s to %2$s', 'nggallery'),  
  1391. esc_html( $image->filename ), esc_html( $destination_file_path) ) . '<br />'; 
  1392. continue;  
  1393.  
  1394. // Copy backup file, if possible 
  1395. @copy($image->imagePath . '_backup', $destination_file_path . '_backup'); 
  1396. // Copy the thumbnail if possible 
  1397. @copy($image->thumbPath, $destination_thumb_file_path); 
  1398.  
  1399. // Create new database entry for the image 
  1400. $new_pid = nggdb::insert_image( $destination->gid, $destination_file_name, $image->alttext, $image->description, $image->exclude); 
  1401.  
  1402. if (!isset($new_pid)) {  
  1403. $errors .= sprintf(__('Failed to copy database row for picture %s', 'nggallery'), $image->pid) . '<br />'; 
  1404. continue;  
  1405.  
  1406. // Copy tags 
  1407. nggTags::copy_tags($image->pid, $new_pid); 
  1408.  
  1409. // Copy meta information 
  1410. $meta = new nggMeta($image->pid); 
  1411. nggdb::update_image_meta( $new_pid, $meta->image->meta_data); 
  1412.  
  1413. if ( $tmp_prefix != '' ) { 
  1414. $messages .= sprintf(__('Image %1$s (%2$s) copied as image %3$s (%4$s) » The file already existed in the destination gallery.', 'nggallery'),  
  1415. $image->pid, esc_html($image->filename), $new_pid, esc_html($destination_file_name) ) . '<br />'; 
  1416. } else { 
  1417. $messages .= sprintf(__('Image %1$s (%2$s) copied as image %3$s (%4$s)', 'nggallery'),  
  1418. $image->pid, esc_html($image->filename), $new_pid, esc_html($destination_file_name) ) . '<br />'; 
  1419.  
  1420.  
  1421. // Finish by showing errors or success 
  1422. if ( $errors == '' ) { 
  1423. $link = '<a href="' . admin_url() . 'admin.php?page=nggallery-manage&mode=image&gid=' . $destination->gid . '" >' . esc_html($destination->title) . '</a>'; 
  1424. $messages .= '<hr />' . sprintf(__('Copied %1$s picture(s) to gallery: %2$s .', 'nggallery'), count($images), $link); 
  1425. }  
  1426.  
  1427. if ( $messages != '' ) 
  1428. nggGallery::show_message($messages); 
  1429.  
  1430. if ( $errors != '' ) 
  1431. nggGallery::show_error($errors); 
  1432.  
  1433. return; 
  1434.  
  1435. /** 
  1436. * Initiate an AJAX operation. 
  1437. * Note: when operating on galleries instead of images, the ID's of the gallery should be used instead of the 
  1438. * images. The mode should be 'gallery' then. 
  1439. * @param string $operation Name of the function that should be executed. 
  1440. * @param array $image_array ID's of the images or the gallery. 
  1441. * @param string $title The title that should be displayed on the modal window. 
  1442. * @param string $mode Which mode to run in. 
  1443. * @param array $data Data to be passed as GET parameters. 
  1444. */ 
  1445. static function do_ajax_operation( $operation, $image_array, $title = '', $mode = 'image', $data = array() ) { 
  1446.  
  1447. if ( ! is_array( $image_array ) || empty( $image_array ) ) { 
  1448. return; 
  1449.  
  1450. $js_array = implode( ', ', $image_array ); 
  1451. $data = json_encode($data); 
  1452.  
  1453. //This initiates the AJAX operations. 
  1454. ?> 
  1455. <script type="text/javascript"> 
  1456.  
  1457. images = [<?php echo esc_js($js_array); ?>]; 
  1458.  
  1459. nggAjaxOptions = { 
  1460. operation: "<?php echo esc_js($operation) ?>",  
  1461. ids: images,  
  1462. header: "<?php echo esc_js($title) ?>",  
  1463. maxStep: images.length,  
  1464. mode: "<?php echo esc_js($mode) ?>",  
  1465. data: <?php echo $data; ?> 
  1466. }; 
  1467.  
  1468. jQuery(document).ready(function() { 
  1469. nggProgressBar.init(nggAjaxOptions); 
  1470. nggAjax.init(nggAjaxOptions); 
  1471. }); 
  1472. </script> 
  1473.  
  1474. <?php 
  1475.  
  1476. /** 
  1477. * nggAdmin::set_gallery_preview() - define a preview pic after the first upload, can be changed in the gallery settings 
  1478. *  
  1479. * @class nggAdmin 
  1480. * @param int $galleryID 
  1481. * @return void 
  1482. */ 
  1483. static function set_gallery_preview( $galleryID ) { 
  1484.  
  1485. global $wpdb; 
  1486.  
  1487. $gallery = nggdb::find_gallery( $galleryID ); 
  1488.  
  1489. // in the case no preview image is setup, we do this now 
  1490. if ($gallery->previewpic == 0) { 
  1491. $firstImage = $wpdb->get_var("SELECT pid FROM $wpdb->nggpictures WHERE exclude != 1 AND galleryid = '$galleryID' ORDER by pid DESC limit 0, 1"); 
  1492. if ($firstImage) { 
  1493. $wpdb->query("UPDATE $wpdb->nggallery SET previewpic = '$firstImage' WHERE gid = '$galleryID'"); 
  1494. wp_cache_delete($galleryID, 'ngg_gallery'); 
  1495.  
  1496. return; 
  1497.  
  1498. /** 
  1499. * Return a JSON coded array of Image ids for a requested gallery 
  1500. *  
  1501. * @class nggAdmin 
  1502. * @param int $galleryID 
  1503. * @return array (JSON) 
  1504. */ 
  1505. static function get_image_ids( $galleryID ) { 
  1506.  
  1507. if ( !function_exists('json_encode') ) 
  1508. return(-2); 
  1509.  
  1510. $gallery = nggdb::get_ids_from_gallery($galleryID, 'pid', 'ASC', false); 
  1511.  
  1512. header('Content-Type: text/plain; charset=' . get_option('blog_charset'), true); 
  1513. $output = json_encode(array_map('intval', $gallery)); 
  1514.  
  1515. return $output; 
  1516.  
  1517. /** 
  1518. * Decode upload error to normal message 
  1519. * @class nggAdmin 
  1520. * @access internal 
  1521. * @param int $code php upload error code 
  1522. * @return string message 
  1523. */ 
  1524.  
  1525. static function decode_upload_error( $code ) { 
  1526.  
  1527. switch ($code) { 
  1528. case UPLOAD_ERR_INI_SIZE: 
  1529. $message = __ ( 'The uploaded file exceeds the upload_max_filesize directive in php.ini', 'nggallery' ); 
  1530. break; 
  1531. case UPLOAD_ERR_FORM_SIZE: 
  1532. $message = __ ( 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form', 'nggallery' ); 
  1533. break; 
  1534. case UPLOAD_ERR_PARTIAL: 
  1535. $message = __ ( 'The uploaded file was only partially uploaded', 'nggallery' ); 
  1536. break; 
  1537. case UPLOAD_ERR_NO_FILE: 
  1538. $message = __ ( 'No file was uploaded', 'nggallery' ); 
  1539. break; 
  1540. case UPLOAD_ERR_NO_TMP_DIR: 
  1541. $message = __ ( 'Missing a temporary folder', 'nggallery' ); 
  1542. break; 
  1543. case UPLOAD_ERR_CANT_WRITE: 
  1544. $message = __ ( 'Failed to write file to disk', 'nggallery' ); 
  1545. break; 
  1546. case UPLOAD_ERR_EXTENSION: 
  1547. $message = __ ( 'File upload stopped by extension', 'nggallery' ); 
  1548. break; 
  1549. default: 
  1550. $message = __ ( 'Unknown upload error', 'nggallery' ); 
  1551. break; 
  1552.  
  1553. return $message;  
  1554.  
  1555. } // END class nggAdmin