/products/photocrati_nextgen/modules/nextgen_addgallery_page/package.module.nextgen_addgallery_page.php

  1. <?php 
  2. /** 
  3. * Class A_Import_Folder_Form 
  4. * @mixin C_Form 
  5. * @adapts I_Form for import_folder context 
  6. */ 
  7. class A_Import_Folder_Form extends Mixin 
  8. function get_title() 
  9. return __("Import Folder", 'nggallery'); 
  10. function enqueue_static_resources() 
  11. wp_enqueue_style('jquery.filetree'); 
  12. wp_enqueue_style('ngg_progressbar'); 
  13. wp_enqueue_script('jquery.filetree'); 
  14. wp_enqueue_script('ngg_progressbar'); 
  15. function render() 
  16. return $this->object->render_partial('photocrati-nextgen_addgallery_page#import_folder', array('browse_sec_token' => C_WordPress_Security_Manager::get_instance()->get_request_token('nextgen_upload_image'), 'import_sec_token' => C_WordPress_Security_Manager::get_instance()->get_request_token('nextgen_upload_image')), TRUE); 
  17. /** 
  18. * Class A_Import_Media_Library_Form 
  19. * @mixin C_Form 
  20. * @adapts I_Form for import_media_library context 
  21. */ 
  22. class A_Import_Media_Library_Form extends Mixin 
  23. function get_title() 
  24. return __('Import from WordPress Media Library', 'nggallery'); 
  25. function enqueue_static_resources() 
  26. wp_enqueue_media(); 
  27. wp_enqueue_script('nextgen_media_library_import-js'); 
  28. wp_enqueue_style('nextgen_media_library_import-css'); 
  29. $url = admin_url() . 'admin.php?page=nggallery-manage-gallery&mode=edit&gid={gid}'; 
  30. $i18n_array = array('admin_url' => admin_url(), 'title' => __('Import Images into NextGen Gallery', 'nggallery'), 'import_multiple' => __('Import %s images', 'nggallery'), 'import_singular' => __('Import 1 image', 'nggallery'), 'imported_multiple' => sprintf(__('{count} images were uploaded successfully. <a href="%s" target="_blank">Manage gallery</a>', 'nggallery'), $url), 'imported_singular' => sprintf(__('1 image was uploaded successfully. <a href="%s" target="_blank">Manage gallery</a>', 'nggallery'), $url), 'imported_none' => __('0 images were uploaded', 'nggallery'), 'progress_title' => __('Importing gallery', 'nggallery'), 'in_progress' => __('In Progress...', 'nggallery'), 'gritter_title' => __('Upload complete', 'nggallery'), 'gritter_error' => __('An unexpected error occured. This is most likely due to a server misconfiguration. Check your PHP error log or ask your hosting provider for assistance.', 'nggallery')); 
  31. foreach (C_WordPress_Security_Manager::get_instance()->get_request_token('nextgen_upload_image')->get_request_list() as $name => $value) { 
  32. $i18n_array['sectoken'][$name] = $value; 
  33. wp_localize_script('nextgen_media_library_import-js', 'ngg_importml_i18n', $i18n_array); 
  34. function render() 
  35. $i18n = array('select-images-to-continue' => __('Please make a selection to continue', 'nggallery'), 'select-opener' => __('Select images', 'nggallery'), 'selected-image-import' => __('Import %d image(s)', 'nggallery')); 
  36. return $this->object->render_partial('photocrati-nextgen_addgallery_page#import_media_library', array('i18n' => $i18n, 'galleries' => $this->object->get_galleries()), TRUE); 
  37. function get_galleries() 
  38. $security = $this->get_registry()->get_utility('I_Security_Manager'); 
  39. $sec_actor = $security->get_current_actor(); 
  40. $galleries = array(); 
  41. if ($sec_actor->is_allowed('nextgen_edit_gallery')) { 
  42. $galleries = C_Gallery_Mapper::get_instance()->find_all(); 
  43. if (!$sec_actor->is_allowed('nextgen_edit_gallery_unowned')) { 
  44. $galleries_all = $galleries; 
  45. $galleries = array(); 
  46. foreach ($galleries_all as $gallery) { 
  47. if ($sec_actor->is_user() && $sec_actor->get_entity_id() == (int) $gallery->author) { 
  48. $galleries[] = $gallery; 
  49. return $galleries; 
  50. /** 
  51. * Class A_NextGen_AddGallery_Ajax 
  52. * @mixin C_Ajax_Controller 
  53. * @adapts I_Ajax_Controller 
  54. */ 
  55. class A_NextGen_AddGallery_Ajax extends Mixin 
  56. function cookie_dump_action() 
  57. foreach ($_COOKIE as $key => &$value) { 
  58. if (is_string($value)) { 
  59. $value = stripslashes($value); 
  60. return array('success' => 1, 'cookies' => $_COOKIE); 
  61. function upload_image_action() 
  62. $retval = array(); 
  63. $created_gallery = FALSE; 
  64. $gallery_id = intval($this->param('gallery_id')); 
  65. $gallery_name = urldecode($this->param('gallery_name')); 
  66. $gallery_mapper = C_Gallery_Mapper::get_instance(); 
  67. $error = FALSE; 
  68. if ($this->validate_ajax_request('nextgen_upload_image', TRUE)) { 
  69. // We need to create a gallery 
  70. if ($gallery_id == 0) { 
  71. if (strlen($gallery_name) > 0) { 
  72. $gallery = $gallery_mapper->create(array('title' => $gallery_name)); 
  73. if (!$gallery->save()) { 
  74. $retval['error'] = $gallery->get_errors(); 
  75. $error = TRUE; 
  76. } else { 
  77. $created_gallery = TRUE; 
  78. $gallery_id = $gallery->id(); 
  79. } else { 
  80. $error = TRUE; 
  81. $retval['error'] = __("No gallery name specified", 'nggallery'); 
  82. // Upload the image to the gallery 
  83. if (!$error) { 
  84. $retval['gallery_id'] = $gallery_id; 
  85. $settings = C_NextGen_Settings::get_instance(); 
  86. $storage = C_Gallery_Storage::get_instance(); 
  87. try { 
  88. if ($storage->is_zip()) { 
  89. if ($results = $storage->upload_zip($gallery_id)) { 
  90. $retval = $results; 
  91. } else { 
  92. $retval['error'] = __('Failed to extract images from ZIP', 'nggallery'); 
  93. } elseif ($image = $storage->upload_image($gallery_id)) { 
  94. $retval['image_ids'] = array($image->id()); 
  95. $retval['image_errors'] = array(); 
  96. // check if image was resized correctly 
  97. if ($settings->imgAutoResize) { 
  98. $image_path = $storage->get_full_abspath($image); 
  99. $image_thumb = new C_NggLegacy_Thumbnail($image_path, true); 
  100. if ($image_thumb->error) { 
  101. $retval['image_errors'][] = array('id' => $image->id(), 'error' => sprintf(__('Automatic image resizing failed [%1$s].', 'nggallery'), $image_thumb->errmsg)); 
  102. $image_thumb = null; 
  103. // check if thumb was generated correctly 
  104. $thumb_path = $storage->get_thumb_abspath($image); 
  105. if (!file_exists($thumb_path)) { 
  106. $retval['image_errors'][] = array('id' => $image->id(), 'error' => __('Thumbnail generation failed.', 'nggallery')); 
  107. } else { 
  108. $retval['error'] = __('Image generation failed', 'nggallery'); 
  109. $error = TRUE; 
  110. } catch (E_NggErrorException $ex) { 
  111. $retval['error'] = $ex->getMessage(); 
  112. $error = TRUE; 
  113. if ($created_gallery) { 
  114. $gallery_mapper->destroy($gallery_id); 
  115. } catch (Exception $ex) { 
  116. $retval['error'] = __("An unexpected error occured.", 'nggallery'); 
  117. $retval['error_details'] = $ex->getMessage(); 
  118. $error = TRUE; 
  119. } else { 
  120. $retval['error'] = __("No permissions to upload images. Try refreshing the page or ensuring that your user account has sufficient roles/privileges.", 'nggallery'); 
  121. $error = TRUE; 
  122. if ($error) { 
  123. return $retval; 
  124. } else { 
  125. $retval['gallery_name'] = esc_html($gallery_name); 
  126. return $retval; 
  127. function get_import_root_abspath() 
  128. if (is_multisite()) { 
  129. $root = C_Gallery_Storage::get_instance()->get_upload_abspath(); 
  130. } else { 
  131. $root = NGG_IMPORT_ROOT; 
  132. $root = str_replace('/', DIRECTORY_SEPARATOR, $root); 
  133. return untrailingslashit($root); 
  134. function browse_folder_action() 
  135. $retval = array(); 
  136. $html = array(); 
  137. if ($this->validate_ajax_request('nextgen_upload_image', TRUE)) { 
  138. if ($dir = urldecode($this->param('dir'))) { 
  139. $fs = C_Fs::get_instance(); 
  140. $root = $this->get_import_root_abspath(); 
  141. $browse_path = $fs->join_paths($root, $dir); 
  142. if (strpos(realpath($browse_path), realpath($root)) !== FALSE) { 
  143. if (@file_exists($browse_path)) { 
  144. $files = scandir($browse_path); 
  145. natcasesort($files); 
  146. if (count($files) > 2) { 
  147. /** The 2 accounts for . and .. */ 
  148. $html[] = "<ul class=\"jqueryFileTree\" style=\"display: none;\">"; 
  149. foreach ($files as $file) { 
  150. $file_path = $fs->join_paths($browse_path, $file); 
  151. $rel_file_path = str_replace($root, '', $file_path); 
  152. if (@file_exists($file_path) && $file != '.' && $file != '..' && is_dir($file_path)) { 
  153. $html[] = "<li class=\"directory collapsed\"><a href=\"#\" rel=\"" . htmlentities($rel_file_path) . "/\">" . htmlentities($file) . "</a></li>"; 
  154. $html[] = "</ul>"; 
  155. $retval['html'] = implode("\n", $html); 
  156. } else { 
  157. $retval['error'] = __("Directory does not exist.", 'nggallery'); 
  158. } else { 
  159. $retval['error'] = __("No permissions to browse folders. Try refreshing the page or ensuring that your user account has sufficient roles/privileges.", 'nggallery'); 
  160. } else { 
  161. $retval['error'] = __("No directory specified.", 'nggallery'); 
  162. } else { 
  163. $retval['error'] = __("No permissions to browse folders. Try refreshing the page or ensuring that your user account has sufficient roles/privileges.", 'nggallery'); 
  164. return $retval; 
  165. function import_folder_action() 
  166. $retval = array(); 
  167. if ($this->validate_ajax_request('nextgen_upload_image', TRUE)) { 
  168. if ($folder = $this->param('folder')) { 
  169. $storage = C_Gallery_Storage::get_instance(); 
  170. $fs = C_Fs::get_instance(); 
  171. try { 
  172. $keep_files = $this->param('keep_location') == 'on'; 
  173. $gallery_title = $this->param('gallery_title', NULL); 
  174. if (empty($gallery_title)) { 
  175. $gallery_title = NULL; 
  176. $root = $this->get_import_root_abspath(); 
  177. $import_path = $fs->join_paths($root, $folder); 
  178. if (strpos(realpath($import_path), realpath($root)) !== FALSE) { 
  179. $retval = $storage->import_gallery_from_fs($import_path, FALSE, !$keep_files, $gallery_title); 
  180. if (!$retval) { 
  181. $retval = array('error' => "Could not import folder. No images found."); 
  182. } else { 
  183. $retval['error'] = __("No permissions to import folders. Try refreshing the page or ensuring that your user account has sufficient roles/privileges.", 'nggallery'); 
  184. } catch (E_NggErrorException $ex) { 
  185. $retval['error'] = $ex->getMessage(); 
  186. } catch (Exception $ex) { 
  187. $retval['error'] = __("An unexpected error occured.", 'nggallery'); 
  188. $retval['error_details'] = $ex->getMessage(); 
  189. } else { 
  190. $retval['error'] = __("No folder specified", 'nggallery'); 
  191. } else { 
  192. $retval['error'] = __("No permissions to import folders. Try refreshing the page or ensuring that your user account has sufficient roles/privileges.", 'nggallery'); 
  193. return $retval; 
  194. function import_media_library_action() 
  195. $retval = array(); 
  196. $created_gallery = FALSE; 
  197. $gallery_id = intval($this->param('gallery_id')); 
  198. $gallery_name = urldecode($this->param('gallery_name')); 
  199. $gallery_mapper = C_Gallery_Mapper::get_instance(); 
  200. $image_mapper = C_Image_Mapper::get_instance(); 
  201. $attachment_ids = $this->param('attachment_ids'); 
  202. if ($this->validate_ajax_request('nextgen_upload_image', TRUE)) { 
  203. if (empty($attachment_ids) || !is_array($attachment_ids)) { 
  204. $retval['error'] = __('An unexpected error occured.', 'nggallery'); 
  205. if (empty($retval['error']) && $gallery_id == 0) { 
  206. if (strlen($gallery_name) > 0) { 
  207. $gallery = $gallery_mapper->create(array('title' => $gallery_name)); 
  208. if (!$gallery->save()) { 
  209. $retval['error'] = $gallery->get_errors(); 
  210. } else { 
  211. $created_gallery = TRUE; 
  212. $gallery_id = $gallery->id(); 
  213. } else { 
  214. $retval['error'] = __('No gallery name specified', 'nggallery'); 
  215. if (empty($retval['error'])) { 
  216. $retval['gallery_id'] = $gallery_id; 
  217. $storage = C_Gallery_Storage::get_instance(); 
  218. foreach ($attachment_ids as $id) { 
  219. try { 
  220. $abspath = get_attached_file($id); 
  221. $file_data = @file_get_contents($abspath); 
  222. $file_name = M_I18n::mb_basename($abspath); 
  223. $attachment = get_post($id); 
  224. if (empty($file_data)) { 
  225. $retval['error'] = __('Image generation failed', 'nggallery'); 
  226. break; 
  227. $image = $storage->upload_base64_image($gallery_id, $file_data, $file_name); 
  228. if ($image) { 
  229. // Potentially import metadata from WordPress 
  230. $image = $image_mapper->find($image->id()); 
  231. if (!empty($attachment->post_excerpt)) { 
  232. $image->alttext = $attachment->post_excerpt; 
  233. if (!empty($attachment->post_content)) { 
  234. $image->description = $attachment->post_content; 
  235. $image = apply_filters('ngg_medialibrary_imported_image', $image, $attachment); 
  236. $image_mapper->save($image); 
  237. } else { 
  238. $retval['error'] = __('Image generation failed', 'nggallery'); 
  239. break; 
  240. $retval['image_ids'][] = $image->{$image->id_field}; 
  241. } catch (E_NggErrorException $ex) { 
  242. $retval['error'] = $ex->getMessage(); 
  243. if ($created_gallery) { 
  244. $gallery_mapper->destroy($gallery_id); 
  245. } catch (Exception $ex) { 
  246. $retval['error'] = __('An unexpected error occured.', 'nggallery'); 
  247. $retval['error_details'] = $ex->getMessage(); 
  248. } else { 
  249. $retval['error'] = __('No permissions to upload images. Try refreshing the page or ensuring that your user account has sufficient roles/privileges.', 'nggallery'); 
  250. if (!empty($retval['error'])) { 
  251. return $retval; 
  252. } else { 
  253. $retval['gallery_name'] = esc_html($gallery_name); 
  254. return $retval; 
  255. /** 
  256. * Class A_NextGen_AddGallery_Controller 
  257. * @mixin C_NextGen_Admin_Page_Controller 
  258. * @adapts I_NextGen_Admin_Page 
  259. */ 
  260. class A_NextGen_AddGallery_Controller extends Mixin 
  261. function get_page_title() 
  262. return __('Add Gallery / Images', 'nggallery'); 
  263. function get_required_permission() 
  264. return 'NextGEN Upload images'; 
  265. function enqueue_backend_resources() 
  266. $this->call_parent('enqueue_backend_resources'); 
  267. wp_enqueue_style('nextgen_addgallery_page'); 
  268. wp_enqueue_script('nextgen_addgallery_page'); 
  269. wp_enqueue_script('frame_event_publisher'); 
  270. function show_save_button() 
  271. return FALSE; 
  272. /** 
  273. * Class A_NextGen_AddGallery_Pages 
  274. * @mixin C_Page_Manager 
  275. * @adapts I_Page_Manager 
  276. */ 
  277. class A_NextGen_AddGallery_Pages extends Mixin 
  278. function setup() 
  279. $this->object->add(NGG_ADD_GALLERY_SLUG, array('adapter' => 'A_NextGen_AddGallery_Controller', 'parent' => NGGFOLDER, 'add_menu' => TRUE, 'before' => 'nggallery-manage-gallery')); 
  280. return $this->call_parent('setup'); 
  281. /** 
  282. * Class A_Upload_Images_Form 
  283. * @mixin C_Form 
  284. * @adapts I_Form for "upload_images" context 
  285. */ 
  286. class A_Upload_Images_Form extends Mixin 
  287. function get_title() 
  288. return __("Upload Images", 'nggallery'); 
  289. function get_i18n_strings() 
  290. return array('no_image_uploaded' => __('No images were uploaded successfully.', 'nggallery'), 'one_image_uploaded' => __('1 image was uploaded successfully.', 'nggallery'), 'x_images_uploaded' => __('{count} images were uploaded successfully.', 'nggallery'), 'image_errors' => __('The following errors occured:', 'nggallery'), 'manage_gallery' => __('Manage gallery {name}', 'nggallery')); 
  291. /** 
  292. * Plupload stores its i18n JS *mostly* as "en.js" or "ar.js" - but some as zh_CN.js so we must check both if the 
  293. * first does not match. 
  294. * 
  295. * @return bool|string 
  296. */ 
  297. function _find_plupload_i18n() 
  298. $fs = C_Fs::get_instance(); 
  299. $router = C_Router::get_instance(); 
  300. $locale = get_locale(); 
  301. $dir = $fs->find_static_abspath('photocrati-nextgen_addgallery_page#plupload-2.1.1/i18n') . DIRECTORY_SEPARATOR; 
  302. $tmp = explode('_', $locale, 2); 
  303. $retval = FALSE; 
  304. if (file_exists($dir . $tmp[0] . '.js')) { 
  305. $retval = $tmp[0]; 
  306. } else { 
  307. if (file_exists($dir . $locale . '.js')) { 
  308. $retval = $locale; 
  309. if ($retval) { 
  310. $retval = $router->get_static_url('photocrati-nextgen_addgallery_page#plupload-2.1.1/i18n/' . $retval . '.js'); 
  311. return $retval; 
  312. function enqueue_static_resources() 
  313. wp_enqueue_style('ngg.plupload.queue'); 
  314. wp_enqueue_script('browserplus'); 
  315. wp_enqueue_script('ngg.plupload.queue'); 
  316. wp_localize_script('ngg.plupload.queue', 'NggUploadImages_i18n', $this->object->get_i18n_strings()); 
  317. $i18n = $this->_find_plupload_i18n(); 
  318. if (!empty($i18n)) { 
  319. wp_enqueue_script('ngg.plupload.i18n', $i18n, array('ngg.plupload.full'), NGG_SCRIPT_VERSION); 
  320. function render() 
  321. return $this->object->render_partial('photocrati-nextgen_addgallery_page#upload_images', array('plupload_options' => json_encode($this->object->get_plupload_options()), 'galleries' => $this->object->get_galleries(), 'sec_token' => C_WordPress_Security_Manager::get_instance()->get_request_token('nextgen_upload_image')), TRUE); 
  322. function get_plupload_options() 
  323. $retval = array(); 
  324. $retval['runtimes'] = 'gears, browserplus, html5, flash, silverlight, html4'; 
  325. $retval['max_file_size'] = strval(round((int) wp_max_upload_size() / 1024)) . 'kb'; 
  326. $retval['filters'] = $this->object->get_plupload_filters(); 
  327. $retval['flash_swf_url'] = includes_url('js/plupload/plupload.flash.swf'); 
  328. $retval['silverlight_xap_url'] = includes_url('js/plupload/plupload.silverlight.xap'); 
  329. $retval['debug'] = TRUE; 
  330. $retval['prevent_duplicates'] = TRUE; 
  331. return $retval; 
  332. function get_plupload_filters() 
  333. $retval = new stdClass(); 
  334. $retval->mime_types = array(); 
  335. $imgs = new stdClass(); 
  336. $imgs->title = "Image files"; 
  337. $imgs->extensions = "jpg, jpeg, gif, png, JPG, JPEG, GIF, PNG"; 
  338. $retval->mime_types[] = $imgs; 
  339. $settings = C_NextGen_Settings::get_instance(); 
  340. if (!is_multisite() || is_multisite() && $settings->get('wpmuZipUpload')) { 
  341. $zips = new stdClass(); 
  342. $zips->title = "Zip files"; 
  343. $zips->extensions = "zip, ZIP"; 
  344. $retval->mime_types[] = $zips; 
  345. $retval->xss_protection = TRUE; 
  346. return $retval; 
  347. function get_galleries() 
  348. $security = $this->get_registry()->get_utility('I_Security_Manager'); 
  349. $sec_actor = $security->get_current_actor(); 
  350. $galleries = array(); 
  351. if ($sec_actor->is_allowed('nextgen_edit_gallery')) { 
  352. $gallery_mapper = C_Gallery_Mapper::get_instance(); 
  353. $galleries = $gallery_mapper->find_all(); 
  354. if (!$sec_actor->is_allowed('nextgen_edit_gallery_unowned')) { 
  355. $galleries_all = $galleries; 
  356. $galleries = array(); 
  357. foreach ($galleries_all as $gallery) { 
  358. if ($sec_actor->is_user() && $sec_actor->get_entity_id() == (int) $gallery->author) { 
  359. $galleries[] = $gallery; 
  360. return $galleries; 
.