/wp-content/plugins/nextgen-gallery/products/photocrati_nextgen/modules/nextgen_addgallery_page/adapter.nextgen_addgallery_ajax.php

https://bitbucket.org/kenaku/karate · PHP · 175 lines · 153 code · 20 blank · 2 comment · 37 complexity · df1640eb2a6a9615124eff39ce283a7c MD5 · raw file

  1. <?php
  2. class A_NextGen_AddGallery_Ajax extends Mixin
  3. {
  4. function cookie_dump_action()
  5. {
  6. return array('success' => 1);
  7. }
  8. function upload_image_action()
  9. {
  10. $retval = array();
  11. $gallery_id = intval($this->param('gallery_id'));
  12. $gallery_name = urldecode($this->param('gallery_name'));
  13. $error = FALSE;
  14. if ($this->validate_ajax_request('nextgen_upload_image'))
  15. {
  16. // We need to create a gallery
  17. if ($gallery_id == 0) {
  18. if (strlen($gallery_name) > 0) {
  19. $gallery_mapper = $this->object->get_registry()->get_utility('I_Gallery_Mapper');
  20. $gallery = $gallery_mapper->create(array(
  21. 'title' => $gallery_name
  22. ));
  23. if (!$gallery->save()) {
  24. $retval['error'] = $gallery->get_errors();
  25. $error = TRUE;
  26. }
  27. else {
  28. $gallery_id = $gallery->id();
  29. }
  30. }
  31. else {
  32. $error = TRUE;
  33. $retval['error'] = "No gallery name specified";
  34. }
  35. }
  36. // Upload the image to the gallery
  37. if (!$error) {
  38. $retval['gallery_id'] = $gallery_id;
  39. $storage = $this->object->get_registry()->get_utility('I_Gallery_Storage');
  40. try{
  41. if ($storage->is_zip()) {
  42. if (($results = $storage->upload_zip($gallery_id))) {
  43. $retval = $results;
  44. }
  45. else $retval['error'] = 'Failed to extract images from ZIP';
  46. }
  47. elseif (($image = $storage->upload_image($gallery_id))) {
  48. $retval['image_ids'] = array($image->id());
  49. }
  50. else {
  51. $retval['error'] = 'Image generation failed';
  52. $error = TRUE;
  53. }
  54. }
  55. catch (E_NggErrorException $ex) {
  56. $retval['error'] = $ex->getMessage();
  57. $error = TRUE;
  58. }
  59. catch (Exception $ex) {
  60. $retval['error'] = "An unexpected error occured.";
  61. $retval['error_details'] = $ex->getMessage();
  62. $error = TRUE;
  63. }
  64. }
  65. }
  66. else {
  67. $retval['error'] = "No permissions to upload images. Try refreshing the page or ensuring that your user account has sufficient roles/privileges.";
  68. $error = TRUE;
  69. }
  70. if ($error) return $retval;
  71. else $retval['gallery_name'] = esc_html($gallery_name);
  72. return $retval;
  73. }
  74. function browse_folder_action()
  75. {
  76. $retval = array();
  77. $html = array();
  78. if ($this->validate_ajax_request('nextgen_upload_image'))
  79. {
  80. if (($dir = urldecode($this->param('dir')))) {
  81. $fs = $this->get_registry()->get_utility('I_Fs');
  82. $root = NEXTGEN_GALLERY_IMPORT_ROOT;
  83. $browse_path = $fs->join_paths($root, $dir);
  84. if (@file_exists($browse_path)) {
  85. $files = scandir($browse_path);
  86. natcasesort($files);
  87. if( count($files) > 2 ) { /* The 2 accounts for . and .. */
  88. $html[] = "<ul class=\"jqueryFileTree\" style=\"display: none;\">";
  89. foreach( $files as $file ) {
  90. $file_path = path_join($browse_path, $file);
  91. $rel_file_path = str_replace($root, '', $file_path);
  92. if(@file_exists($file_path) && $file != '.' && $file != '..' && is_dir($file_path) ) {
  93. $html[] = "<li class=\"directory collapsed\"><a href=\"#\" rel=\"" . htmlentities($rel_file_path) . "/\">" . htmlentities($file) . "</a></li>";
  94. }
  95. }
  96. $html[] = "</ul>";
  97. }
  98. $retval['html'] = implode("\n", $html);
  99. }
  100. else {
  101. $retval['error'] = "Directory does not exist.";
  102. }
  103. }
  104. else {
  105. $retval['error'] = "No directory specified.";
  106. }
  107. }
  108. else {
  109. $retval['error'] = "No permissions to browse folders. Try refreshing the page or ensuring that your user account has sufficient roles/privileges.";
  110. }
  111. return $retval;
  112. }
  113. function import_folder_action()
  114. {
  115. $retval = array();
  116. if ($this->validate_ajax_request('nextgen_upload_image'))
  117. {
  118. if (($folder = $this->param('folder'))) {
  119. $storage = C_Gallery_Storage::get_instance();
  120. $fs = C_Fs::get_instance();
  121. try {
  122. $keep_files = $this->param('keep_location') == 'on';
  123. $retval = $storage->import_gallery_from_fs($fs->join_paths(NEXTGEN_GALLERY_IMPORT_ROOT, $folder), false, !$keep_files);
  124. if (!$retval) $retval = array('error' => "Could not import folder. No images found.");
  125. }
  126. catch (E_NggErrorException $ex) {
  127. $retval['error'] = $ex->getMessage();
  128. }
  129. catch (Exception $ex) {
  130. $retval['error'] = "An unexpected error occured.";
  131. $retval['error_details'] = $ex->getMessage();
  132. }
  133. }
  134. else {
  135. $retval['error'] = "No folder specified";
  136. }
  137. }
  138. else {
  139. $retval['error'] = "No permissions to import folders. Try refreshing the page or ensuring that your user account has sufficient roles/privileges.";
  140. }
  141. return $retval;
  142. }
  143. function validate_ajax_request($action, $check_token = false)
  144. {
  145. $valid_request = false;
  146. $security = $this->get_registry()->get_utility('I_Security_Manager');
  147. $sec_actor = $security->get_current_actor();
  148. $sec_token = $security->get_request_token($action);
  149. if ($sec_actor->is_allowed($action) && (!$check_token || $sec_token->check_current_request()))
  150. {
  151. $valid_request = true;
  152. }
  153. return $valid_request;
  154. }
  155. }