PageRenderTime 53ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/htdocs/wp-content/plugins/nextgen-gallery/admin/functions.php

https://bitbucket.org/dkrzos/phc
PHP | 1501 lines | 816 code | 319 blank | 366 comment | 204 complexity | 294ac6193c11534a3a2770ee254811d5 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. if(preg_match('#' . basename(__FILE__) . '#', $_SERVER['PHP_SELF'])) { die('You are not allowed to call this page directly.'); }
  3. /**
  4. * nggAdmin - Class for admin operation
  5. *
  6. * @package NextGEN Gallery
  7. * @author Alex Rabe
  8. *
  9. * @access public
  10. */
  11. class nggAdmin{
  12. /**
  13. * create a new gallery & folder
  14. *
  15. * @class nggAdmin
  16. * @param string $name of the gallery
  17. * @param string $defaultpath
  18. * @param bool $output if the function should show an error messsage or not
  19. * @return
  20. */
  21. function create_gallery($title, $defaultpath, $output = true) {
  22. global $user_ID;
  23. // get the current user ID
  24. get_currentuserinfo();
  25. //cleanup pathname
  26. $name = sanitize_file_name( sanitize_title($title) );
  27. $name = apply_filters('ngg_gallery_name', $name);
  28. $nggRoot = WINABSPATH . $defaultpath;
  29. $txt = '';
  30. // No gallery name ?
  31. if ( empty($name) ) {
  32. if ($output) nggGallery::show_error( __('No valid gallery name!', 'nggallery') );
  33. return false;
  34. }
  35. // check for main folder
  36. if ( !is_dir($nggRoot) ) {
  37. if ( !wp_mkdir_p( $nggRoot ) ) {
  38. $txt = __('Directory', 'nggallery').' <strong>' . esc_html( $defaultpath ) . '</strong> '.__('didn\'t exist. Please create first the main gallery folder ', '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. }
  44. // check for permission settings, Safe mode limitations are not taken into account.
  45. if ( !is_writeable( $nggRoot ) ) {
  46. $txt = __('Directory', 'nggallery').' <strong>' . esc_html( $defaultpath ) . '</strong> '.__('is not writeable !', 'nggallery').'<br />';
  47. $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> ';
  48. if ($output) nggGallery::show_error($txt);
  49. return false;
  50. }
  51. // 1. Check for existing folder
  52. if ( is_dir(WINABSPATH . $defaultpath . $name ) && !(SAFE_MODE) ) {
  53. $suffix = 1;
  54. do {
  55. $alt_name = substr ($name, 0, 200 - ( strlen( $suffix ) + 1 ) ) . "_$suffix";
  56. $dir_check = is_dir(WINABSPATH . $defaultpath . $alt_name );
  57. $suffix++;
  58. } while ( $dir_check );
  59. $name = $alt_name;
  60. }
  61. // define relative path to gallery inside wp root folder
  62. $nggpath = $defaultpath . $name;
  63. // 2. Create new gallery folder
  64. if ( !wp_mkdir_p (WINABSPATH . $nggpath) )
  65. $txt = __('Unable to create directory ', 'nggallery') . esc_html( $nggpath ) . '!<br />';
  66. // 3. Check folder permission
  67. if ( !is_writeable(WINABSPATH . $nggpath ) )
  68. $txt .= __('Directory', 'nggallery').' <strong>' . esc_html( $nggpath ) . '</strong> '.__('is not writeable !', 'nggallery').'<br />';
  69. // 4. Now create thumbnail folder inside
  70. if ( !is_dir(WINABSPATH . $nggpath . '/thumbs') ) {
  71. if ( !wp_mkdir_p ( WINABSPATH . $nggpath . '/thumbs') )
  72. $txt .= __('Unable to create directory ', 'nggallery').' <strong>' . esc_html( $nggpath ) . '/thumbs !</strong>';
  73. }
  74. if (SAFE_MODE) {
  75. $help = __('The server setting Safe-Mode is on !', 'nggallery');
  76. $help .= '<br />'.__('If you have problems, please create directory', 'nggallery').' <strong>' . esc_html( $nggpath ) . '</strong> ';
  77. $help .= __('and the thumbnails directory', 'nggallery').' <strong>' . esc_html( $nggpath ) . '/thumbs</strong> '.__('with permission 777 manually !', 'nggallery');
  78. if ($output) nggGallery::show_message($help);
  79. }
  80. // show a error message
  81. if ( !empty($txt) ) {
  82. if (SAFE_MODE) {
  83. // for safe_mode , better delete folder, both folder must be created manually
  84. @rmdir(WINABSPATH . $nggpath . '/thumbs');
  85. @rmdir(WINABSPATH . $nggpath);
  86. }
  87. if ($output) nggGallery::show_error($txt);
  88. return false;
  89. }
  90. // now add the gallery to the database
  91. $galleryID = nggdb::add_gallery($title, $nggpath, '', 0, 0, $user_ID );
  92. // here you can inject a custom function
  93. do_action('ngg_created_new_gallery', $galleryID);
  94. // return only the id if defined
  95. if ($output == false)
  96. return $galleryID;
  97. if ($galleryID != false) {
  98. $message = __('Gallery ID %1$s successfully created. You can show this gallery in your post or page with the shortcode %2$s.<br/>','nggallery');
  99. $message = sprintf($message, $galleryID, '<strong>[nggallery id=' . $galleryID . ']</strong>');
  100. $message .= '<a href="' . admin_url() . 'admin.php?page=nggallery-manage-gallery&mode=edit&gid=' . $galleryID . '" >';
  101. $message .= __('Edit gallery','nggallery');
  102. $message .= '</a>';
  103. if ($output) nggGallery::show_message($message);
  104. }
  105. return true;
  106. }
  107. /**
  108. * nggAdmin::import_gallery()
  109. * TODO: Check permission of existing thumb folder & images
  110. *
  111. * @class nggAdmin
  112. * @param string $galleryfolder contains relative path to the gallery itself
  113. * @return void
  114. */
  115. function import_gallery($galleryfolder) {
  116. global $wpdb, $user_ID;
  117. // get the current user ID
  118. get_currentuserinfo();
  119. $created_msg = '';
  120. // remove trailing slash at the end, if somebody use it
  121. $galleryfolder = untrailingslashit($galleryfolder);
  122. $gallerypath = WINABSPATH . $galleryfolder;
  123. if (!is_dir($gallerypath)) {
  124. nggGallery::show_error(__('Directory', 'nggallery').' <strong>' . esc_html( $gallerypath ) .'</strong> '.__('doesn&#96;t exist!', 'nggallery'));
  125. return ;
  126. }
  127. // read list of images
  128. $new_imageslist = nggAdmin::scandir($gallerypath);
  129. if (empty($new_imageslist)) {
  130. nggGallery::show_message(__('Directory', 'nggallery').' <strong>' . esc_html( $gallerypath ) . '</strong> '.__('contains no pictures', 'nggallery'));
  131. return;
  132. }
  133. // check & create thumbnail folder
  134. if ( !nggGallery::get_thumbnail_folder($gallerypath) )
  135. return;
  136. // take folder name as gallery name
  137. $galleryname = basename($galleryfolder);
  138. $galleryname = apply_filters('ngg_gallery_name', $galleryname);
  139. // check for existing gallery folder
  140. $gallery_id = $wpdb->get_var("SELECT gid FROM $wpdb->nggallery WHERE path = '$galleryfolder' ");
  141. if (!$gallery_id) {
  142. // now add the gallery to the database
  143. $gallery_id = nggdb::add_gallery( $galleryname, $galleryfolder, '', 0, 0, $user_ID );
  144. if (!$gallery_id) {
  145. nggGallery::show_error(__('Database error. Could not add gallery!','nggallery'));
  146. return;
  147. }
  148. $created_msg = _n( 'Gallery', 'Galleries', 1, 'nggallery' ) . ' <strong>' . esc_html( $galleryname ) . '</strong> ' . __('successfully created!','nggallery') . '<br />';
  149. }
  150. // Look for existing image list
  151. $old_imageslist = $wpdb->get_col("SELECT filename FROM $wpdb->nggpictures WHERE galleryid = '$gallery_id' ");
  152. // if no images are there, create empty array
  153. if ($old_imageslist == NULL)
  154. $old_imageslist = array();
  155. // check difference
  156. $new_images = array_diff($new_imageslist, $old_imageslist);
  157. // all images must be valid files
  158. foreach($new_images as $key => $picture) {
  159. // filter function to rename/change/modify image before
  160. $picture = apply_filters('ngg_pre_add_new_image', $picture, $gallery_id);
  161. $new_images[$key] = $picture;
  162. if (!@getimagesize($gallerypath . '/' . $picture) ) {
  163. unset($new_images[$key]);
  164. @unlink($gallerypath . '/' . $picture);
  165. }
  166. }
  167. // add images to database
  168. $image_ids = nggAdmin::add_Images($gallery_id, $new_images);
  169. //add the preview image if needed
  170. nggAdmin::set_gallery_preview ( $gallery_id );
  171. // now create thumbnails
  172. nggAdmin::do_ajax_operation( 'create_thumbnail' , $image_ids, __('Create new thumbnails','nggallery') );
  173. //TODO:Message will not shown, because AJAX routine require more time, message should be passed to AJAX
  174. $message = $created_msg . count($image_ids) .__(' picture(s) successfully added','nggallery');
  175. $message .= ' [<a href="' . admin_url() . 'admin.php?page=nggallery-manage-gallery&mode=edit&gid=' . $gallery_id . '" >';
  176. $message .= __('Edit gallery','nggallery');
  177. $message .= '</a>]';
  178. nggGallery::show_message($message);
  179. return;
  180. }
  181. /**
  182. * Scan folder for new images
  183. *
  184. * @class nggAdmin
  185. * @param string $dirname
  186. * @return array $files list of image filenames
  187. */
  188. function scandir( $dirname = '.' ) {
  189. $ext = apply_filters('ngg_allowed_file_types', array('jpeg', 'jpg', 'png', 'gif') );
  190. $files = array();
  191. if( $handle = opendir( $dirname ) ) {
  192. while( false !== ( $file = readdir( $handle ) ) ) {
  193. $info = pathinfo( $file );
  194. // just look for images with the correct extension
  195. if ( isset($info['extension']) )
  196. if ( in_array( strtolower($info['extension']), $ext) )
  197. $files[] = utf8_encode( $file );
  198. }
  199. closedir( $handle );
  200. }
  201. sort( $files );
  202. return ( $files );
  203. }
  204. /**
  205. * nggAdmin::createThumbnail() - function to create or recreate a thumbnail
  206. *
  207. * @class nggAdmin
  208. * @param object | int $image contain all information about the image or the id
  209. * @return string result code
  210. * @since v1.0.0
  211. */
  212. function create_thumbnail($image) {
  213. global $ngg;
  214. if(! class_exists('ngg_Thumbnail'))
  215. require_once( nggGallery::graphic_library() );
  216. if ( is_numeric($image) )
  217. $image = nggdb::find_image( $image );
  218. if ( !is_object($image) )
  219. return __('Object didn\'t contain correct data','nggallery');
  220. // before we start we import the meta data to database (required for uploads before V1.4.0)
  221. nggAdmin::maybe_import_meta( $image->pid );
  222. // check for existing thumbnail
  223. if (file_exists($image->thumbPath))
  224. if (!is_writable($image->thumbPath))
  225. return esc_html( $image->filename ) . __(' is not writeable ','nggallery');
  226. $thumb = new ngg_Thumbnail($image->imagePath, TRUE);
  227. // skip if file is not there
  228. if (!$thumb->error) {
  229. if ($ngg->options['thumbfix']) {
  230. // calculate correct ratio
  231. $wratio = $ngg->options['thumbwidth'] / $thumb->currentDimensions['width'];
  232. $hratio = $ngg->options['thumbheight'] / $thumb->currentDimensions['height'];
  233. if ($wratio > $hratio) {
  234. // first resize to the wanted width
  235. $thumb->resize($ngg->options['thumbwidth'], 0);
  236. // get optimal y startpos
  237. $ypos = ($thumb->currentDimensions['height'] - $ngg->options['thumbheight']) / 2;
  238. $thumb->crop(0, $ypos, $ngg->options['thumbwidth'],$ngg->options['thumbheight']);
  239. } else {
  240. // first resize to the wanted height
  241. $thumb->resize(0, $ngg->options['thumbheight']);
  242. // get optimal x startpos
  243. $xpos = ($thumb->currentDimensions['width'] - $ngg->options['thumbwidth']) / 2;
  244. $thumb->crop($xpos, 0, $ngg->options['thumbwidth'],$ngg->options['thumbheight']);
  245. }
  246. //this create a thumbnail but keep ratio settings
  247. } else {
  248. $thumb->resize($ngg->options['thumbwidth'],$ngg->options['thumbheight']);
  249. }
  250. // save the new thumbnail
  251. $thumb->save($image->thumbPath, $ngg->options['thumbquality']);
  252. nggAdmin::chmod ($image->thumbPath);
  253. //read the new sizes
  254. $new_size = @getimagesize ( $image->thumbPath );
  255. $size['width'] = $new_size[0];
  256. $size['height'] = $new_size[1];
  257. // add them to the database
  258. nggdb::update_image_meta($image->pid, array( 'thumbnail' => $size) );
  259. }
  260. $thumb->destruct();
  261. if ( !empty($thumb->errmsg) )
  262. return ' <strong>' . esc_html( $image->filename ) . ' (Error : '.$thumb->errmsg .')</strong>';
  263. // success
  264. return '1';
  265. }
  266. /**
  267. * nggAdmin::resize_image() - create a new image, based on the height /width
  268. *
  269. * @class nggAdmin
  270. * @param object | int $image contain all information about the image or the id
  271. * @param integer $width optional
  272. * @param integer $height optional
  273. * @return string result code
  274. */
  275. function resize_image($image, $width = 0, $height = 0) {
  276. global $ngg;
  277. if(! class_exists('ngg_Thumbnail'))
  278. require_once( nggGallery::graphic_library() );
  279. if ( is_numeric($image) )
  280. $image = nggdb::find_image( $image );
  281. if ( !is_object($image) )
  282. return __('Object didn\'t contain correct data','nggallery');
  283. // before we start we import the meta data to database (required for uploads before V1.4.0)
  284. nggAdmin::maybe_import_meta( $image->pid );
  285. // if no parameter is set, take global settings
  286. $width = ($width == 0) ? $ngg->options['imgWidth'] : $width;
  287. $height = ($height == 0) ? $ngg->options['imgHeight'] : $height;
  288. if (!is_writable($image->imagePath))
  289. return ' <strong>' . esc_html( $image->filename ) . __(' is not writeable','nggallery') . '</strong>';
  290. $file = new ngg_Thumbnail($image->imagePath, TRUE);
  291. // skip if file is not there
  292. if (!$file->error) {
  293. // If required save a backup copy of the file
  294. if ( ($ngg->options['imgBackup'] == 1) && (!file_exists($image->imagePath . '_backup')) )
  295. @copy ($image->imagePath, $image->imagePath . '_backup');
  296. $file->resize($width, $height);
  297. $file->save($image->imagePath, $ngg->options['imgQuality']);
  298. // read the new sizes
  299. $size = @getimagesize ( $image->imagePath );
  300. // add them to the database
  301. nggdb::update_image_meta($image->pid, array( 'width' => $size[0], 'height' => $size[1] ) );
  302. $file->destruct();
  303. } else {
  304. $file->destruct();
  305. return ' <strong>' . esc_html( $image->filename ) . ' (Error : ' . $file->errmsg . ')</strong>';
  306. }
  307. return '1';
  308. }
  309. /**
  310. * Rotated/Flip an image based on the orientation flag or a definded angle
  311. *
  312. * @param int|object $image
  313. * @param string (optional) $dir, CW (clockwise)or CCW (counter clockwise), if set to false, the exif flag will be used
  314. * @param string (optional) $flip, could be either false | V (flip vertical) | H (flip horizontal)
  315. * @return string result code
  316. */
  317. function rotate_image($image, $dir = false, $flip = false) {
  318. global $ngg;
  319. if(! class_exists('ngg_Thumbnail'))
  320. require_once( nggGallery::graphic_library() );
  321. if ( is_numeric($image) )
  322. $image = nggdb::find_image( $image );
  323. if ( !is_object($image) )
  324. return __('Object didn\'t contain correct data','nggallery');
  325. if (!is_writable($image->imagePath))
  326. return ' <strong>' . esc_html( $image->filename ) . __(' is not writeable','nggallery') . '</strong>';
  327. // if you didn't define a rotation, we look for the orientation flag in EXIF
  328. if ( $dir === false ) {
  329. $meta = new nggMeta( $image->pid );
  330. $exif = $meta->get_EXIF();
  331. if (isset($exif['Orientation'])) {
  332. switch ($exif['Orientation']) {
  333. case 5 : // vertical flip + 90 rotate right
  334. $flip = 'V';
  335. case 6 : // 90 rotate right
  336. $dir = 'CW';
  337. break;
  338. case 7 : // horizontal flip + 90 rotate right
  339. $flip = 'H';
  340. case 8 : // 90 rotate left
  341. $dir = 'CCW';
  342. break;
  343. case 4 : // vertical flip
  344. $flip = 'V';
  345. break;
  346. case 3 : // 180 rotate left
  347. $dir = 180;
  348. break;
  349. case 2 : // horizontal flip
  350. $flip = 'H';
  351. break;
  352. case 1 : // no action in the case it doesn't need a rotation
  353. default:
  354. return '0';
  355. break;
  356. }
  357. } else
  358. return '0';
  359. }
  360. $file = new ngg_Thumbnail( $image->imagePath, TRUE );
  361. // skip if file is not there
  362. if (!$file->error) {
  363. // If required save a backup copy of the file
  364. if ( ($ngg->options['imgBackup'] == 1) && (!file_exists($image->imagePath . '_backup')) )
  365. @copy ($image->imagePath, $image->imagePath . '_backup');
  366. // before we start we import the meta data to database (required for uploads before V1.4.X)
  367. nggAdmin::maybe_import_meta( $image->pid );
  368. if ( $dir !== 0 )
  369. $file->rotateImage( $dir );
  370. if ( $dir === 180)
  371. $file->rotateImage( 'CCW' ); // very special case, we rotate the image two times
  372. if ( $flip == 'H')
  373. $file->flipImage(true, false);
  374. if ( $flip == 'V')
  375. $file->flipImage(false, true);
  376. $file->save($image->imagePath, $ngg->options['imgQuality']);
  377. // read the new sizes
  378. $size = @getimagesize ( $image->imagePath );
  379. // add them to the database
  380. nggdb::update_image_meta($image->pid, array( 'width' => $size[0], 'height' => $size[1] ) );
  381. }
  382. $file->destruct();
  383. if ( !empty($file->errmsg) )
  384. return ' <strong>' . esc_html( $image->filename ) . ' (Error : '.$file->errmsg .')</strong>';
  385. return '1';
  386. }
  387. /**
  388. * nggAdmin::set_watermark() - set the watermark for the image
  389. *
  390. * @class nggAdmin
  391. * @param object | int $image contain all information about the image or the id
  392. * @return string result code
  393. */
  394. function set_watermark($image) {
  395. global $ngg;
  396. if(! class_exists('ngg_Thumbnail'))
  397. require_once( nggGallery::graphic_library() );
  398. if ( is_numeric($image) )
  399. $image = nggdb::find_image( $image );
  400. if ( !is_object($image) )
  401. return __('Object didn\'t contain correct data','nggallery');
  402. // before we start we import the meta data to database (required for uploads before V1.4.0)
  403. nggAdmin::maybe_import_meta( $image->pid );
  404. if (!is_writable($image->imagePath))
  405. return ' <strong>' . esc_html( $image->filename ) . __(' is not writeable','nggallery') . '</strong>';
  406. $file = new ngg_Thumbnail( $image->imagePath, TRUE );
  407. // skip if file is not there
  408. if (!$file->error) {
  409. // If required save a backup copy of the file
  410. if ( ($ngg->options['imgBackup'] == 1) && (!file_exists($image->imagePath . '_backup')) )
  411. @copy ($image->imagePath, $image->imagePath . '_backup');
  412. if ($ngg->options['wmType'] == 'image') {
  413. $file->watermarkImgPath = $ngg->options['wmPath'];
  414. $file->watermarkImage($ngg->options['wmPos'], $ngg->options['wmXpos'], $ngg->options['wmYpos']);
  415. }
  416. if ($ngg->options['wmType'] == 'text') {
  417. $file->watermarkText = $ngg->options['wmText'];
  418. $file->watermarkCreateText($ngg->options['wmColor'], $ngg->options['wmFont'], $ngg->options['wmSize'], $ngg->options['wmOpaque']);
  419. $file->watermarkImage($ngg->options['wmPos'], $ngg->options['wmXpos'], $ngg->options['wmYpos']);
  420. }
  421. $file->save($image->imagePath, $ngg->options['imgQuality']);
  422. }
  423. $file->destruct();
  424. if ( !empty($file->errmsg) )
  425. return ' <strong>' . esc_html( $image->filename ) . ' (Error : '.$file->errmsg .')</strong>';
  426. return '1';
  427. }
  428. /**
  429. * Recover image from backup copy and reprocess it
  430. *
  431. * @class nggAdmin
  432. * @since 1.5.0
  433. * @param object | int $image contain all information about the image or the id
  434. * @return string result code
  435. */
  436. function recover_image($image) {
  437. global $ngg;
  438. if ( is_numeric($image) )
  439. $image = nggdb::find_image( $image );
  440. if ( !is_object( $image ) )
  441. return __('Object didn\'t contain correct data','nggallery');
  442. if (!is_writable( $image->imagePath ))
  443. return ' <strong>' . esc_html( $image->filename ) . __(' is not writeable','nggallery') . '</strong>';
  444. if (!file_exists( $image->imagePath . '_backup' )) {
  445. return ' <strong>'.__('File do not exists','nggallery').'</strong>';
  446. }
  447. if (!@copy( $image->imagePath . '_backup' , $image->imagePath) )
  448. return ' <strong>'.__('Couldn\'t restore original image','nggallery').'</strong>';
  449. require_once(NGGALLERY_ABSPATH . '/lib/meta.php');
  450. $meta_obj = new nggMeta( $image->pid );
  451. $common = $meta_obj->get_common_meta();
  452. $common['saved'] = true;
  453. $result = nggdb::update_image_meta($image->pid, $common);
  454. return '1';
  455. }
  456. /**
  457. * Add images to database
  458. *
  459. * @class nggAdmin
  460. * @param int $galleryID
  461. * @param array $imageslist
  462. * @return array $image_ids Id's which are sucessful added
  463. */
  464. function add_Images($galleryID, $imageslist) {
  465. global $wpdb, $ngg;
  466. $image_ids = array();
  467. if ( is_array($imageslist) ) {
  468. foreach($imageslist as $picture) {
  469. // filter function to rename/change/modify image before
  470. $picture = apply_filters('ngg_pre_add_new_image', $picture, $galleryID);
  471. // strip off the extension of the filename
  472. $path_parts = pathinfo( $picture );
  473. $alttext = ( !isset($path_parts['filename']) ) ? substr($path_parts['basename'], 0,strpos($path_parts['basename'], '.')) : $path_parts['filename'];
  474. // save it to the database
  475. $pic_id = nggdb::add_image( $galleryID, $picture, '', $alttext );
  476. if ( !empty($pic_id) )
  477. $image_ids[] = $pic_id;
  478. // add the metadata
  479. nggAdmin::import_MetaData( $pic_id );
  480. // auto rotate
  481. nggAdmin::rotate_image( $pic_id );
  482. // Autoresize image if required
  483. if ($ngg->options['imgAutoResize']) {
  484. $imagetmp = nggdb::find_image( $pic_id );
  485. $sizetmp = @getimagesize ( $imagetmp->imagePath );
  486. $widthtmp = $ngg->options['imgWidth'];
  487. $heighttmp = $ngg->options['imgHeight'];
  488. if (($sizetmp[0] > $widthtmp && $widthtmp) || ($sizetmp[1] > $heighttmp && $heighttmp)) {
  489. nggAdmin::resize_image( $pic_id );
  490. }
  491. }
  492. // action hook for post process after the image is added to the database
  493. $image = array( 'id' => $pic_id, 'filename' => $picture, 'galleryID' => $galleryID);
  494. do_action('ngg_added_new_image', $image);
  495. }
  496. } // is_array
  497. // delete dirsize after adding new images
  498. delete_transient( 'dirsize_cache' );
  499. do_action('ngg_after_new_images_added', $galleryID, $image_ids );
  500. return $image_ids;
  501. }
  502. /**
  503. * Import some meta data into the database (if avialable)
  504. *
  505. * @class nggAdmin
  506. * @param array|int $imagesIds
  507. * @return string result code
  508. */
  509. function import_MetaData($imagesIds) {
  510. global $wpdb;
  511. require_once(NGGALLERY_ABSPATH . '/lib/image.php');
  512. if (!is_array($imagesIds))
  513. $imagesIds = array($imagesIds);
  514. foreach($imagesIds as $imageID) {
  515. $image = nggdb::find_image( $imageID );
  516. if (!$image->error) {
  517. $meta = nggAdmin::get_MetaData( $image->pid );
  518. // get the title
  519. $alttext = empty( $meta['title'] ) ? $image->alttext : $meta['title'];
  520. // get the caption / description field
  521. $description = empty( $meta['caption'] ) ? $image->description : $meta['caption'];
  522. // get the file date/time from exif
  523. $timestamp = $meta['timestamp'];
  524. // first update database
  525. $result = $wpdb->query(
  526. $wpdb->prepare("UPDATE $wpdb->nggpictures SET
  527. alttext = %s,
  528. description = %s,
  529. imagedate = %s
  530. WHERE pid = %d", $alttext, $description, $timestamp, $image->pid) );
  531. if ($result === false)
  532. return ' <strong>' . esc_html( $image->filename ) . ' ' . __('(Error : Couldn\'t not update data base)', 'nggallery') . '</strong>';
  533. //this flag will inform us that the import is already one time performed
  534. $meta['common']['saved'] = true;
  535. $result = nggdb::update_image_meta($image->pid, $meta['common']);
  536. if ($result === false)
  537. return ' <strong>' . esc_html( $image->filename ) . ' ' . __('(Error : Couldn\'t not update meta data)', 'nggallery') . '</strong>';
  538. // add the tags if we found some
  539. if ($meta['keywords']) {
  540. $taglist = explode(',', $meta['keywords']);
  541. wp_set_object_terms($image->pid, $taglist, 'ngg_tag');
  542. }
  543. } else
  544. return ' <strong>' . esc_html( $image->filename ) . ' ' . __('(Error : Couldn\'t not find image)', 'nggallery') . '</strong>';// error check
  545. }
  546. return '1';
  547. }
  548. /**
  549. * nggAdmin::get_MetaData()
  550. *
  551. * @class nggAdmin
  552. * @require NextGEN Meta class
  553. * @param int $id image ID
  554. * @return array metadata
  555. */
  556. function get_MetaData($id) {
  557. require_once(NGGALLERY_ABSPATH . '/lib/meta.php');
  558. $meta = array();
  559. $pdata = new nggMeta( $id );
  560. $meta['title'] = trim ( $pdata->get_META('title') );
  561. $meta['caption'] = trim ( $pdata->get_META('caption') );
  562. $meta['keywords'] = trim ( $pdata->get_META('keywords') );
  563. $meta['timestamp'] = $pdata->get_date_time();
  564. // this contain other useful meta information
  565. $meta['common'] = $pdata->get_common_meta();
  566. // hook for addon plugin to add more meta fields
  567. $meta = apply_filters('ngg_get_image_metadata', $meta, $pdata);
  568. return $meta;
  569. }
  570. /**
  571. * Maybe import some meta data to the database. The functions checks the flag 'saved'
  572. * and if based on compat reason (pre V1.4.0) we save then some meta datas to the database
  573. *
  574. * @since V1.4.0
  575. * @param int $id
  576. * @return result
  577. */
  578. function maybe_import_meta( $id ) {
  579. require_once(NGGALLERY_ABSPATH . '/lib/meta.php');
  580. $meta_obj = new nggMeta( $id );
  581. if ( $meta_obj->image->meta_data['saved'] != true ) {
  582. $common = $meta_obj->get_common_meta();
  583. //this flag will inform us that the import is already one time performed
  584. $common['saved'] = true;
  585. $result = nggdb::update_image_meta($id, $common);
  586. } else
  587. return false;
  588. return $result;
  589. }
  590. /**
  591. * Unzip a file via the PclZip class
  592. *
  593. * @class nggAdmin
  594. * @require PclZip class
  595. * @param string $dir
  596. * @param string $file
  597. * @return bool
  598. */
  599. function unzip($dir, $file) {
  600. if(! class_exists('PclZip'))
  601. require_once(ABSPATH . 'wp-admin/includes/class-pclzip.php');
  602. $archive = new PclZip($file);
  603. // extract all files in one folder
  604. if ($archive->extract(PCLZIP_OPT_PATH, $dir, PCLZIP_OPT_REMOVE_ALL_PATH,
  605. PCLZIP_CB_PRE_EXTRACT, 'ngg_getOnlyImages',
  606. PCLZIP_CB_POST_EXTRACT, 'ngg_checkExtract') == 0) {
  607. nggGallery::show_error( 'Error : ' . $archive->errorInfo(true) );
  608. return false;
  609. }
  610. return true;
  611. }
  612. /**
  613. * nggAdmin::getOnlyImages()
  614. *
  615. * @class nggAdmin
  616. * @param mixed $p_event
  617. * @param mixed $p_header
  618. * @return bool
  619. */
  620. function getOnlyImages($p_event, &$p_header) {
  621. // avoid null byte hack (THX to Dominic Szablewski)
  622. if ( strpos($p_header['filename'], chr(0) ) !== false )
  623. $p_header['filename'] = substr ( $p_header['filename'], 0, strpos($p_header['filename'], chr(0) ));
  624. // check for extension
  625. $info = pathinfo($p_header['filename']);
  626. // check for extension
  627. $ext = apply_filters('ngg_allowed_file_types', array('jpeg', 'jpg', 'png', 'gif') );
  628. if ( in_array( strtolower($info['extension']), $ext) ) {
  629. // For MAC skip the ".image" files
  630. if ($info['basename']{0} == '.' )
  631. return 0;
  632. else {
  633. // sanitize the file name before we do further processing
  634. $info['basename'] = sanitize_file_name( $info['basename'] );
  635. $p_header['filename'] = $info['dirname'] . '/' . $info['basename'];
  636. return 1;
  637. }
  638. }
  639. // ----- all other files are skipped
  640. else {
  641. return 0;
  642. }
  643. }
  644. /**
  645. * Import a ZIP file via a upload form or a URL
  646. *
  647. * @class nggAdmin
  648. * @param int (optional) $galleryID
  649. * @return bool $result
  650. */
  651. function import_zipfile($galleryID) {
  652. global $ngg, $wpdb;
  653. if (nggWPMU::check_quota())
  654. return false;
  655. $defaultpath = $ngg->options['gallerypath'];
  656. $zipurl = $_POST['zipurl'];
  657. // if someone entered a URL try to upload it
  658. if (!empty($zipurl) && (function_exists('curl_init')) ) {
  659. if (!(preg_match('/^http(s)?:\/\//i', $zipurl) )) {
  660. nggGallery::show_error( __('No valid URL path ','nggallery') );
  661. return false;
  662. }
  663. $temp_zipfile = tempnam('/tmp', 'zipimport_');
  664. $filename = basename($zipurl);
  665. //Grab the zip via cURL
  666. $save = fopen ( $temp_zipfile, "w" );
  667. $ch = curl_init ();
  668. curl_setopt ( $ch, CURLOPT_FILE, $save );
  669. curl_setopt ( $ch, CURLOPT_HEADER, 0 );
  670. curl_setopt ( $ch, CURLOPT_BINARYTRANSFER, 1 );
  671. curl_setopt ( $ch, CURLOPT_URL, $zipurl );
  672. $success = curl_exec ( $ch );
  673. if (!$success)
  674. nggGallery::show_error( __('Import via cURL failed.','nggallery') . ' Error code ' . curl_errno( $ch ) . ' : ' . curl_error( $ch ) );
  675. curl_close ( $ch );
  676. fclose($save);
  677. if (!$success)
  678. return false;
  679. } else {
  680. $temp_zipfile = $_FILES['zipfile']['tmp_name'];
  681. $filename = $_FILES['zipfile']['name'];
  682. // Chrome return a empty content-type : http://code.google.com/p/chromium/issues/detail?id=6800
  683. if ( !preg_match('/chrome/i', $_SERVER['HTTP_USER_AGENT']) ) {
  684. // check if file is a zip file
  685. if ( !preg_match('/(zip|download|octet-stream)/i', $_FILES['zipfile']['type']) ) {
  686. @unlink($temp_zipfile); // del temp file
  687. nggGallery::show_error(__('Uploaded file was no or a faulty zip file ! The server recognized : ','nggallery') . $_FILES['zipfile']['type']);
  688. return false;
  689. }
  690. }
  691. }
  692. // should this unpacked into a new folder ?
  693. if ( $galleryID == '0' ) {
  694. //cleanup and take the zipfile name as folder name
  695. $foldername = sanitize_title(strtok ($filename, '.'));
  696. $foldername = $defaultpath . $foldername;
  697. } else {
  698. // get foldername if selected
  699. $foldername = $wpdb->get_var("SELECT path FROM $wpdb->nggallery WHERE gid = '$galleryID' ");
  700. }
  701. if ( empty($foldername) ) {
  702. nggGallery::show_error( __('Could not get a valid foldername', 'nggallery') );
  703. return false;
  704. }
  705. // set complete folder path
  706. $newfolder = WINABSPATH . $foldername;
  707. // check first if the traget folder exist
  708. if (!is_dir($newfolder)) {
  709. // create new directories
  710. if (!wp_mkdir_p ($newfolder)) {
  711. $message = sprintf(__('Unable to create directory %s. Is its parent directory writable by the server?', 'nggallery'), esc_html( $newfolder ) );
  712. nggGallery::show_error($message);
  713. return false;
  714. }
  715. if (!wp_mkdir_p ($newfolder . '/thumbs')) {
  716. nggGallery::show_error(__('Unable to create directory ', 'nggallery') . esc_html( $newfolder ). '/thumbs !');
  717. return false;
  718. }
  719. }
  720. // unzip and del temp file
  721. $result = nggAdmin::unzip($newfolder, $temp_zipfile);
  722. @unlink($temp_zipfile);
  723. if ($result) {
  724. $message = __('Zip-File successfully unpacked','nggallery') . '<br />';
  725. // parse now the folder and add to database
  726. $message .= nggAdmin::import_gallery( $foldername );
  727. nggGallery::show_message($message);
  728. }
  729. return true;
  730. }
  731. /**
  732. * Function for uploading of images via the upload form
  733. *
  734. * @class nggAdmin
  735. * @return void
  736. */
  737. function upload_images() {
  738. global $nggdb;
  739. // WPMU action
  740. if (nggWPMU::check_quota())
  741. return;
  742. // Images must be an array
  743. $imageslist = array();
  744. // get selected gallery
  745. $galleryID = (int) $_POST['galleryselect'];
  746. if ($galleryID == 0) {
  747. nggGallery::show_error(__('No gallery selected !','nggallery'));
  748. return;
  749. }
  750. // get the path to the gallery
  751. $gallery = $nggdb->find_gallery($galleryID);
  752. if ( empty($gallery->path) ){
  753. nggGallery::show_error(__('Failure in database, no gallery path set !','nggallery'));
  754. return;
  755. }
  756. // read list of images
  757. $dirlist = nggAdmin::scandir($gallery->abspath);
  758. $imagefiles = $_FILES['imagefiles'];
  759. if (is_array($imagefiles)) {
  760. foreach ($imagefiles['name'] as $key => $value) {
  761. // look only for uploded files
  762. if ($imagefiles['error'][$key] == 0) {
  763. $temp_file = $imagefiles['tmp_name'][$key];
  764. //clean filename and extract extension
  765. $filepart = nggGallery::fileinfo( $imagefiles['name'][$key] );
  766. $filename = $filepart['basename'];
  767. // check for allowed extension and if it's an image file
  768. $ext = array('jpg', 'png', 'gif');
  769. if ( !in_array($filepart['extension'], $ext) || !@getimagesize($temp_file) ){
  770. nggGallery::show_error('<strong>' . esc_html( $imagefiles['name'][$key] ) . ' </strong>' . __('is no valid image file!','nggallery'));
  771. continue;
  772. }
  773. // check if this filename already exist in the folder
  774. $i = 0;
  775. while ( in_array( $filename, $dirlist ) ) {
  776. $filename = $filepart['filename'] . '_' . $i++ . '.' .$filepart['extension'];
  777. }
  778. $dest_file = $gallery->abspath . '/' . $filename;
  779. //check for folder permission
  780. if ( !is_writeable($gallery->abspath) ) {
  781. $message = sprintf(__('Unable to write to directory %s. Is this directory writable by the server?', 'nggallery'), esc_html($gallery->abspath) );
  782. nggGallery::show_error($message);
  783. return;
  784. }
  785. // save temp file to gallery
  786. if ( !@move_uploaded_file($temp_file, $dest_file) ){
  787. nggGallery::show_error(__('Error, the file could not be moved to : ','nggallery') . esc_html( $dest_file ) );
  788. nggAdmin::check_safemode( $gallery->abspath );
  789. continue;
  790. }
  791. if ( !nggAdmin::chmod($dest_file) ) {
  792. nggGallery::show_error(__('Error, the file permissions could not be set','nggallery'));
  793. continue;
  794. }
  795. // add to imagelist & dirlist
  796. $imageslist[] = $filename;
  797. $dirlist[] = $filename;
  798. }
  799. }
  800. }
  801. if (count($imageslist) > 0) {
  802. // add images to database
  803. $image_ids = nggAdmin::add_Images($galleryID, $imageslist);
  804. //create thumbnails
  805. nggAdmin::do_ajax_operation( 'create_thumbnail' , $image_ids, __('Create new thumbnails','nggallery') );
  806. //add the preview image if needed
  807. nggAdmin::set_gallery_preview ( $galleryID );
  808. nggGallery::show_message( count($image_ids) . __(' Image(s) successfully added','nggallery'));
  809. }
  810. return;
  811. }
  812. /**
  813. * Upload function will be called via the Flash uploader
  814. *
  815. * @class nggAdmin
  816. * @param integer $galleryID
  817. * @return string $result
  818. */
  819. static function swfupload_image($galleryID = 0) {
  820. global $nggdb;
  821. if ($galleryID == 0)
  822. return __('No gallery selected !', 'nggallery');
  823. // WPMU action
  824. if (nggWPMU::check_quota())
  825. return '0';
  826. // Check the upload
  827. if (!isset($_FILES['Filedata']) || !is_uploaded_file($_FILES['Filedata']['tmp_name']) || $_FILES['Filedata']['error'] != 0)
  828. return __('Invalid upload. Error Code : ', 'nggallery') . $_FILES['Filedata']['error'];
  829. // get the filename and extension
  830. $temp_file = $_FILES['Filedata']['tmp_name'];
  831. $filepart = nggGallery::fileinfo( $_FILES['Filedata']['name'] );
  832. $filename = $filepart['basename'];
  833. // check for allowed extension
  834. $ext = apply_filters('ngg_allowed_file_types', array('jpeg', 'jpg', 'png', 'gif') );
  835. if (!in_array( strtolower( $filepart['extension'] ), $ext))
  836. return esc_html( $_FILES[$key]['name'] ) . __('is no valid image file!', 'nggallery');
  837. // get the path to the gallery
  838. $gallery = $nggdb->find_gallery( (int) $galleryID );
  839. if ( empty($gallery->path) ){
  840. @unlink($temp_file);
  841. return __('Failure in database, no gallery path set !', 'nggallery');
  842. }
  843. // read list of images
  844. $imageslist = nggAdmin::scandir( WINABSPATH . $gallery->path );
  845. // check if this filename already exist
  846. $i = 0;
  847. while (in_array($filename, $imageslist)) {
  848. $filename = $filepart['filename'] . '_' . $i++ . '.' . $filepart['extension'];
  849. }
  850. $dest_file = WINABSPATH . $gallery->path . '/' . $filename;
  851. // save temp file to gallery
  852. if ( !@move_uploaded_file($_FILES["Filedata"]['tmp_name'], $dest_file) ){
  853. nggAdmin::check_safemode(WINABSPATH . $gallery->path);
  854. return __('Error, the file could not be moved to : ','nggallery'). esc_html( $dest_file );
  855. }
  856. if ( !nggAdmin::chmod($dest_file) )
  857. return __('Error, the file permissions could not be set','nggallery');
  858. return '0';
  859. }
  860. /**
  861. * Set correct file permissions (taken from wp core)
  862. *
  863. * @class nggAdmin
  864. * @param string $filename
  865. * @return bool $result
  866. */
  867. function chmod($filename = '') {
  868. $stat = @ stat( dirname($filename) );
  869. $perms = $stat['mode'] & 0000666; // Remove execute bits for files
  870. if ( @chmod($filename, $perms) )
  871. return true;
  872. return false;
  873. }
  874. /**
  875. * Check UID in folder and Script
  876. * Read http://www.php.net/manual/en/features.safe-mode.php to understand safe_mode
  877. *
  878. * @class nggAdmin
  879. * @param string $foldername
  880. * @return bool $result
  881. */
  882. function check_safemode($foldername) {
  883. if ( SAFE_MODE ) {
  884. $script_uid = ( ini_get('safe_mode_gid') ) ? getmygid() : getmyuid();
  885. $folder_uid = fileowner($foldername);
  886. if ($script_uid != $folder_uid) {
  887. $message = sprintf(__('SAFE MODE Restriction in effect! You need to create the folder <strong>%s</strong> manually','nggallery'), esc_html( $foldername ) );
  888. $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 );
  889. nggGallery::show_error($message);
  890. return false;
  891. }
  892. }
  893. return true;
  894. }
  895. /**
  896. * Capability check. Check is the ID fit's to the user_ID
  897. *
  898. * @class nggAdmin
  899. * @param int $check_ID is the user_id
  900. * @return bool $result
  901. */
  902. function can_manage_this_gallery($check_ID) {
  903. global $user_ID, $wp_roles;
  904. if ( !current_user_can('NextGEN Manage others gallery') ) {
  905. // get the current user ID
  906. get_currentuserinfo();
  907. if ( $user_ID != $check_ID)
  908. return false;
  909. }
  910. return true;
  911. }
  912. /**
  913. * Move images from one folder to another
  914. *
  915. * @class nggAdmin
  916. * @param array|int $pic_ids ID's of the images
  917. * @param int $dest_gid destination gallery
  918. * @return void
  919. */
  920. function move_images($pic_ids, $dest_gid) {
  921. $errors = '';
  922. $count = 0;
  923. if ( !is_array($pic_ids) )
  924. $pic_ids = array($pic_ids);
  925. // Get destination gallery
  926. $destination = nggdb::find_gallery( $dest_gid );
  927. $dest_abspath = WINABSPATH . $destination->path;
  928. if ( $destination == null ) {
  929. nggGallery::show_error(__('The destination gallery does not exist','nggallery'));
  930. return;
  931. }
  932. // Check for folder permission
  933. if ( !is_writeable( $dest_abspath ) ) {
  934. $message = sprintf(__('Unable to write to directory %s. Is this directory writable by the server?', 'nggallery'), esc_html( $dest_abspath ) );
  935. nggGallery::show_error($message);
  936. return;
  937. }
  938. // Get pictures
  939. $images = nggdb::find_images_in_list($pic_ids);
  940. foreach ($images as $image) {
  941. $i = 0;
  942. $tmp_prefix = '';
  943. $destination_file_name = $image->filename;
  944. // check if the filename already exist, then we add a copy_ prefix
  945. while (file_exists( $dest_abspath . '/' . $destination_file_name)) {
  946. $tmp_prefix = 'copy_' . ($i++) . '_';
  947. $destination_file_name = $tmp_prefix . $image->filename;
  948. }
  949. $destination_path = $dest_abspath . '/' . $destination_file_name;
  950. $destination_thumbnail = $dest_abspath . '/thumbs/thumbs_' . $destination_file_name;
  951. // Move files
  952. if ( !@rename($image->imagePath, $destination_path) ) {
  953. $errors .= sprintf(__('Failed to move image %1$s to %2$s','nggallery'),
  954. '<strong>' . esc_html( $image->filename ) . '</strong>', esc_html( $destination_path ) ) . '<br />';
  955. continue;
  956. }
  957. // Move backup file, if possible
  958. @rename($image->imagePath . '_backup', $destination_path . '_backup');
  959. // Move the thumbnail, if possible
  960. @rename($image->thumbPath, $destination_thumbnail);
  961. // Change the gallery id in the database , maybe the filename
  962. if ( nggdb::update_image($image->pid, $dest_gid, $destination_file_name) )
  963. $count++;
  964. }
  965. if ( $errors != '' )
  966. nggGallery::show_error($errors);
  967. $link = '<a href="' . admin_url() . 'admin.php?page=nggallery-manage-gallery&mode=edit&gid=' . $destination->gid . '" >' . esc_html( $destination->title ) . '</a>';
  968. $messages = sprintf(__('Moved %1$s picture(s) to gallery : %2$s .','nggallery'), $count, $link);
  969. nggGallery::show_message($messages);
  970. return;
  971. }
  972. /**
  973. * Copy images to another gallery
  974. *
  975. * @class nggAdmin
  976. * @param array|int $pic_ids ID's of the images
  977. * @param int $dest_gid destination gallery
  978. * @return void
  979. */
  980. function copy_images($pic_ids, $dest_gid) {
  981. require_once(NGGALLERY_ABSPATH . '/lib/meta.php');
  982. $errors = $messages = '';
  983. if (!is_array($pic_ids))
  984. $pic_ids = array($pic_ids);
  985. // Get destination gallery
  986. $destination = nggdb::find_gallery( $dest_gid );
  987. if ( $destination == null ) {
  988. nggGallery::show_error(__('The destination gallery does not exist','nggallery'));
  989. return;
  990. }
  991. // Check for folder permission
  992. if (!is_writeable(WINABSPATH.$destination->path)) {
  993. $message = sprintf(__('Unable to write to directory %s. Is this directory writable by the server?', 'nggallery'), esc_html( WINABSPATH.$destination->path) );
  994. nggGallery::show_error($message);
  995. return;
  996. }
  997. // Get pictures
  998. $images = nggdb::find_images_in_list($pic_ids);
  999. $destination_path = WINABSPATH . $destination->path;
  1000. foreach ($images as $image) {
  1001. // WPMU action
  1002. if ( nggWPMU::check_quota() )
  1003. return;
  1004. $i = 0;
  1005. $tmp_prefix = '';
  1006. $destination_file_name = $image->filename;
  1007. while (file_exists($destination_path . '/' . $destination_file_name)) {
  1008. $tmp_prefix = 'copy_' . ($i++) . '_';
  1009. $destination_file_name = $tmp_prefix . $image->filename;
  1010. }
  1011. $destination_file_path = $destination_path . '/' . $destination_file_name;
  1012. $destination_thumb_file_path = $destination_path . '/' . $image->thumbFolder . $image->thumbPrefix . $destination_file_name;
  1013. // Copy files
  1014. if ( !@copy($image->imagePath, $destination_file_path) ) {
  1015. $errors .= sprintf(__('Failed to copy image %1$s to %2$s','nggallery'),
  1016. esc_html( $image->filename ), esc_html( $destination_file_path) ) . '<br />';
  1017. continue;
  1018. }
  1019. // Copy backup file, if possible
  1020. @copy($image->imagePath . '_backup', $destination_file_path . '_backup');
  1021. // Copy the thumbnail if possible
  1022. @copy($image->thumbPath, $destination_thumb_file_path);
  1023. // Create new database entry for the image
  1024. $new_pid = nggdb::insert_image( $destination->gid, $destination_file_name, $image->alttext, $image->description, $image->exclude);
  1025. if (!isset($new_pid)) {
  1026. $errors .= sprintf(__('Failed to copy database row for picture %s','nggallery'), $image->pid) . '<br />';
  1027. continue;
  1028. }
  1029. // Copy tags
  1030. nggTags::copy_tags($image->pid, $new_pid);
  1031. // Copy meta information
  1032. $meta = new nggMeta($image->pid);
  1033. nggdb::update_image_meta( $new_pid, $meta->image->meta_data);
  1034. if ( $tmp_prefix != '' ) {
  1035. $messages .= sprintf(__('Image %1$s (%2$s) copied as image %3$s (%4$s) &raquo; The file already existed in the destination gallery.','nggallery'),
  1036. $image->pid, esc_html($image->filename), $new_pid, esc_html($destination_file_name) ) . '<br />';
  1037. } else {
  1038. $messages .= sprintf(__('Image %1$s (%2$s) copied as image %3$s (%4$s)','nggallery'),
  1039. $image->pid, esc_html($image->filename), $new_pid, esc_html($destination_file_name) ) . '<br />';
  1040. }
  1041. }
  1042. // Finish by showing errors or success
  1043. if ( $errors == '' ) {
  1044. $link = '<a href="' . admin_url() . 'admin.php?page=nggallery-manage-gallery&mode=edit&gid=' . $destination->gid . '" >' . esc_html($destination->title) . '</a>';
  1045. $messages .= '<hr />' . sprintf(__('Copied %1$s picture(s) to gallery: %2$s .','nggallery'), count($images), $link);
  1046. }
  1047. if ( $messages != '' )
  1048. nggGallery::show_message($messages);
  1049. if ( $errors != '' )
  1050. nggGallery::show_error($errors);
  1051. return;
  1052. }
  1053. /**
  1054. * Initate the Ajax operation
  1055. *
  1056. * @class nggAdmin
  1057. * @param string $operation name of the function which should be executed
  1058. * @param array $image_array
  1059. * @param string $title name of the operation
  1060. * @return string the javascript output
  1061. */
  1062. function do_ajax_operation( $operation, $image_array, $title = '' ) {
  1063. if ( !is_array($image_array) || empty($image_array) )
  1064. return;
  1065. $js_array = implode('","', $image_array);
  1066. // send out some JavaScript, which initate the ajax operation
  1067. ?>
  1068. <script type="text/javascript">
  1069. Images = new Array("<?php echo $js_array; ?>");
  1070. nggAjaxOptions = {
  1071. operation: "<?php echo $operation; ?>",
  1072. ids: Images,
  1073. header: "<?php echo $title; ?>",
  1074. maxStep: Images.length
  1075. };
  1076. jQuery(document).ready( function(){
  1077. nggProgressBar.init( nggAjaxOptions );
  1078. nggAjax.init( nggAjaxOptions );
  1079. } );
  1080. </script>
  1081. <?php
  1082. }
  1083. /**
  1084. * nggAdmin::set_gallery_preview() - define a preview pic after the first upload, can be changed in the gallery settings
  1085. *
  1086. * @class nggAdmin
  1087. * @param int $galleryID
  1088. * @return void
  1089. */
  1090. function set_gallery_preview( $galleryID ) {
  1091. global $wpdb;
  1092. $gallery = nggdb::find_gallery( $galleryID );
  1093. // in the case no preview image is setup, we do this now
  1094. if ($gallery->previewpic == 0) {
  1095. $firstImage = $wpdb->get_var("SELECT pid FROM $wpdb->nggpictures WHERE exclude != 1 AND galleryid = '$galleryID' ORDER by pid DESC limit 0,1");
  1096. if ($firstImage) {
  1097. $wpdb->query("UPDATE $wpdb->nggallery SET previewpic = '$firstImage' WHERE gid = '$galleryID'");
  1098. wp_cache_delete($galleryID, 'ngg_gallery');
  1099. }
  1100. }
  1101. return;
  1102. }
  1103. /**
  1104. * Return a JSON coded array of Image ids for a requested gallery
  1105. *
  1106. * @class nggAdmin
  1107. * @param int $galleryID
  1108. * @return arry (JSON)
  1109. */
  1110. function get_image_ids( $galleryID ) {
  1111. if ( !function_exists('json_encode') )
  1112. return(-2);
  1113. $gallery = nggdb::get_ids_from_gallery($galleryID, 'pid', 'ASC', false);
  1114. header('Content-Type: text/plain; charset=' . get_option('blog_charset'), true);
  1115. $output = json_encode($gallery);
  1116. return $output;
  1117. }
  1118. /**
  1119. * Decode upload error to normal message
  1120. *
  1121. * @class nggAdmin
  1122. * @access internal
  1123. * @param int $code php upload error code
  1124. * @return string message
  1125. */
  1126. function decode_upload_error( $code ) {
  1127. switch ($code) {
  1128. case UPLOAD_ERR_INI_SIZE:
  1129. $message = __ ( 'The uploaded file exceeds the upload_max_filesize directive in php.ini', 'nggallery' );
  1130. break;
  1131. case UPLOAD_ERR_FORM_SIZE:
  1132. $message = __ ( 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form', 'nggallery' );
  1133. break;
  1134. case UPLOAD_ERR_PARTIAL:
  1135. $message = __ ( 'The uploaded file was only partially uploaded', 'nggallery' );
  1136. break;
  1137. case UPLOAD_ERR_NO_FILE:
  1138. $message = __ ( 'No file was uploaded', 'nggallery' );
  1139. break;
  1140. case UPLOAD_ERR_NO_TMP_DIR:
  1141. $message = __ ( 'Missing a temporary folder', 'nggallery' );
  1142. break;
  1143. case UPLOAD_ERR_CANT_WRITE:
  1144. $message = __ ( 'Failed to write file to disk', 'nggallery' );
  1145. break;
  1146. case UPLOAD_ERR_EXTENSION:
  1147. $message = __ ( 'File upload stopped by extension', 'nggallery' );
  1148. break;
  1149. default:
  1150. $message = __ ( 'Unknown upload error', 'nggallery' );
  1151. break;
  1152. }
  1153. return $message;
  1154. }
  1155. } // END class nggAdmin
  1156. /**
  1157. * TODO: Cannot be member of a class ? Check PCLZIP later...
  1158. *
  1159. * @param mixed $p_event
  1160. * @param mixed $p_header
  1161. * @return
  1162. */
  1163. function ngg_getOnlyImages($p_event, &$p_header) {
  1164. return nggAdmin::getOnlyImages($p_event, $p_header);
  1165. }
  1166. /**
  1167. * Ensure after zip extraction that it could be only a image file
  1168. *
  1169. * @param mixed $p_event
  1170. * @param mixed $p_header
  1171. * @return 1
  1172. */
  1173. function ngg_checkExtract($p_event, &$p_header) {
  1174. // look for valid extraction
  1175. if ($p_header['status'] == 'ok') {
  1176. // check if it's any image file, delete all other files
  1177. if ( !@getimagesize ( $p_header['filename'] ))
  1178. unlink($p_header['filename']);
  1179. }
  1180. return 1;
  1181. }
  1182. ?>