PageRenderTime 57ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

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

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