C_NextGen_API

Class C_NextGen_API.

Defined (1)

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

/products/photocrati_nextgen/modules/nextgen_xmlrpc/package.module.nextgen_xmlrpc.php  
  1. class C_NextGen_API extends C_Component 
  2. const CRON_KEY = 'nextgen.api.task_list'; 
  3. /** NOTE: these constants' numeric values are used elsewhere, don't change the values */ 
  4. const ERR_NO_TASK_LIST = 1001; 
  5. const ERR_NOT_AUTHENTICATED = 1002; 
  6. const ERR_NOT_AUTHORIZED = 1003; 
  7. const ERR_JOB_NOT_ADDED = 1004; 
  8. const ERR_FTP_NOT_AUTHENTICATED = 1101; 
  9. const ERR_FTP_NOT_CONNECTED = 1102; 
  10. const ERR_FTP_NO_PATH = 1103; 
  11. const INFO_NO_JOB_LIST = 6001; 
  12. const INFO_JOB_LIST_FINISHED = 6002; 
  13. const INFO_JOB_LIST_UNFINISHED = 6003; 
  14. const INFO_EXECUTION_LOCKED = 6004; 
  15. public static $_instances = array(); 
  16. var $_start_time; 
  17. public static function get_instance($context = false) 
  18. if (!isset(self::$_instances[$context])) { 
  19. self::$_instances[$context] = new self($context); 
  20. return self::$_instances[$context]; 
  21. function define($context = false) 
  22. parent::define($context); 
  23. $this->implement('I_NextGen_API'); 
  24. $this->_start_time = time(); 
  25. function should_stop_execution() 
  26. $timeout = defined('NGG_API_JOB_HANDLER_TIMEOUT') ? intval(NGG_API_JOB_HANDLER_TIMEOUT) : intval(ini_get('max_execution_time')) - 3; 
  27. $timeout = $timeout > 0 ? $timeout : 27; 
  28. /** most hosts have a limit of 30 seconds execution time, so 27 should be a safe default */ 
  29. return time() - $this->_start_time >= $timeout; 
  30. function is_execution_locked() 
  31. $lock_time = get_option('ngg_api_execution_lock', 0); 
  32. if ($lock_time == 0) { 
  33. return false; 
  34. $lock_max = defined('NGG_API_EXECUTION_LOCK_MAX') ? intval(NGG_API_EXECUTION_LOCK_MAX) : 0; 
  35. $lock_max = $lock_max > 0 ? $lock_max : 60 * 5; 
  36. /** if the lock is 5 minutes old assume something went wrong and the lock couldn't be unset */ 
  37. $time_diff = time() - $lock_time; 
  38. if ($time_diff > $lock_max) { 
  39. return false; 
  40. return true; 
  41. function set_execution_locked($locked) 
  42. if ($locked) { 
  43. update_option('ngg_api_execution_lock', time()); 
  44. } else { 
  45. update_option('ngg_api_execution_lock', 0); 
  46. function get_job_list() 
  47. return get_option('ngg_api_job_list'); 
  48. function add_job($job_data, $app_config, $task_list) 
  49. $job_list = $this->get_job_list(); 
  50. $job_id = uniqid(); 
  51. while (isset($job_list[$job_id])) { 
  52. $job_id = uniqid(); 
  53. $job = array('id' => $job_id, 'data' => $job_data, 'app_config' => $app_config, 'task_list' => $task_list); 
  54. $job_list[$job_id] = $job; 
  55. update_option('ngg_api_job_list', $job_list); 
  56. return $job_id; 
  57. function _update_job($job_id, $job) 
  58. $job_list = $this->get_job_list(); 
  59. if (isset($job_list[$job_id])) { 
  60. $job_list[$job_id] = $job; 
  61. update_option('ngg_api_job_list', $job_list); 
  62. function remove_job($job_id) 
  63. $job_list = $this->get_job_list(); 
  64. if (isset($job_list[$job_id])) { 
  65. unset($job_list[$job_id]); 
  66. update_option('ngg_api_job_list', $job_list); 
  67. function get_job($job_id) 
  68. $job_list = $this->get_job_list(); 
  69. if (isset($job_list[$job_id])) { 
  70. return $job_list[$job_id]; 
  71. return null; 
  72. function get_job_data($job_id) 
  73. $job = $this->get_job($job_id); 
  74. if ($job != null) { 
  75. return $job['data']; 
  76. return null; 
  77. function get_job_task_list($job_id) 
  78. $job = $this->get_job($job_id); 
  79. if ($job != null) { 
  80. return $job['task_list']; 
  81. return null; 
  82. function set_job_task_list($job_id, $task_list) 
  83. $job = $this->get_job($job_id); 
  84. if ($job != null) { 
  85. $job['task_list'] = $task_list; 
  86. $this->_update_job($job_id, $job); 
  87. return true; 
  88. return false; 
  89. function get_job_status_file($job_id) 
  90. $job = $this->get_job($job_id); 
  91. if ($job != null) { 
  92. return $job['task_list']; 
  93. return null; 
  94. function create_filesystem_access($args, $method = null) 
  95. // taken from wp-admin/includes/file.php but with modifications 
  96. if (!$method && isset($args['connection_type']) && 'ssh' == $args['connection_type'] && extension_loaded('ssh2') && function_exists('stream_get_contents')) { 
  97. $method = 'ssh2'; 
  98. if (!$method && extension_loaded('ftp')) { 
  99. $method = 'ftpext'; 
  100. if (!$method && (extension_loaded('sockets') || function_exists('fsockopen'))) { 
  101. $method = 'ftpsockets'; 
  102. //Sockets: Socket extension; PHP Mode: FSockopen / fwrite / fread 
  103. if (!$method) { 
  104. return false; 
  105. require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php'; 
  106. if (!class_exists("WP_Filesystem_{$method}")) { 
  107. /** 
  108. * Filter the path for a specific filesystem method class file. 
  109. * @since 2.6.0 
  110. * @see get_filesystem_method() 
  111. * @param string $path Path to the specific filesystem method class file. 
  112. * @param string $method The filesystem method to use. 
  113. */ 
  114. $abstraction_file = apply_filters('filesystem_method_file', ABSPATH . 'wp-admin/includes/class-wp-filesystem-' . $method . '.php', $method); 
  115. if (!file_exists($abstraction_file)) { 
  116. return false; 
  117. require_once $abstraction_file; 
  118. $method_class = "WP_Filesystem_{$method}"; 
  119. $wp_filesystem = new $method_class($args); 
  120. //Define the timeouts for the connections. Only available after the construct is called to allow for per-transport overriding of the default. 
  121. if (!defined('FS_CONNECT_TIMEOUT')) { 
  122. define('FS_CONNECT_TIMEOUT', 30); 
  123. if (!defined('FS_TIMEOUT')) { 
  124. define('FS_TIMEOUT', 30); 
  125. if (is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code()) { 
  126. return false; 
  127. if (!$wp_filesystem->connect()) { 
  128. if ($method == 'ftpext') { 
  129. // attempt connecting with alternative method 
  130. return $this->create_filesystem_access($args, 'ftpsockets'); 
  131. return false; 
  132. //There was an error connecting to the server. 
  133. // Set the permission constants if not already set. 
  134. if (!defined('FS_CHMOD_DIR')) { 
  135. define('FS_CHMOD_DIR', fileperms(ABSPATH) & 0777 | 0755); 
  136. if (!defined('FS_CHMOD_FILE')) { 
  137. define('FS_CHMOD_FILE', fileperms(ABSPATH . 'index.php') & 0777 | 0644); 
  138. return $wp_filesystem; 
  139. // returns an actual scalar ID based on parametric ID (e.g. a parametric ID could represent the query ID from another task) 
  140. function get_query_id($id, &$task_list) 
  141. $task_id = $id; 
  142. if (is_object($task_id) || is_array($task_id)) { 
  143. $id = null; 
  144. // it was specified that the query ID is referencing the result from another task 
  145. if (isset($task_id['target']) && $task_id['target'] == 'task') { 
  146. if (isset($task_id['id']) && isset($task_list[$task_id['id']])) { 
  147. $target_task = $task_list[$task_id['id']]; 
  148. if (isset($target_task['query']['id'])) { 
  149. $id = $target_task['query']['id']; 
  150. return $id; 
  151. // returns an actual scalar ID based on parametric ID (e.g. a parametric ID could represent the resulting object ID from another task) 
  152. function get_object_id($id, &$result_list) 
  153. $task_id = $id; 
  154. if (is_object($task_id) || is_array($task_id)) { 
  155. $id = null; 
  156. // it was specified that the query ID is referencing the result from another task 
  157. if (isset($task_id['target']) && $task_id['target'] == 'task') { 
  158. if (isset($task_id['id']) && isset($result_list[$task_id['id']])) { 
  159. $target_result = $result_list[$task_id['id']]; 
  160. if (isset($target_result['object_id'])) { 
  161. $id = $target_result['object_id']; 
  162. return $id; 
  163. function handle_job($job_id, $job_data, $app_config, $task_list) 
  164. $job_user = $job_data['user']; 
  165. $task_count = count($task_list); 
  166. $done_count = 0; 
  167. $skip_count = 0; 
  168. $task_list_result = array(); 
  169. wp_set_current_user($job_user); 
  170. /** This block does all of the filesystem magic: 
  171. * - determines web paths based on FTP paths 
  172. * - initializes the WP_Filesystem mechanism in case this host doesn't support direct file access 
  173. * (this might not be 100% reliable right now due to NG core not making use of WP_Filesystem) 
  174. */ 
  175. // $ftp_path is assumed to be WP_CONTENT_DIR as accessed through the FTP mount point 
  176. $ftp_path = rtrim($app_config['ftp_path'], '/\\'); 
  177. $full_path = rtrim($app_config['full_path'], '/\\'); 
  178. $root_path = rtrim(WP_CONTENT_DIR, '/\\'); 
  179. $creds = true; 
  180. // WP_Filesystem(true) requests direct filesystem access 
  181. $fs_sep = DIRECTORY_SEPARATOR; 
  182. $wp_fs = null; 
  183. require_once ABSPATH . 'wp-admin/includes/file.php'; 
  184. if (get_filesystem_method() !== 'direct') { 
  185. $fs_sep = '/'; 
  186. $ftp_method = isset($app_config['ftp_method']) ? $app_config['ftp_method'] : 'ftp'; 
  187. $creds = array('connection_type' => $ftp_method == 'sftp' ? 'ssh' : 'ftp', 'hostname' => $app_config['ftp_host'], 'port' => $app_config['ftp_port'], 'username' => $app_config['ftp_user'], 'password' => $app_config['ftp_pass']); 
  188. if (WP_Filesystem($creds)) { 
  189. $wp_fs = $GLOBALS['wp_filesystem']; 
  190. $path_prefix = $full_path; 
  191. if ($wp_fs->method === 'direct') { 
  192. $path_prefix = str_replace($ftp_path, $root_path, $full_path); 
  193. foreach ($task_list as &$task_item) { 
  194. $task_id = isset($task_item['id']) ? $task_item['id'] : null; 
  195. $task_name = isset($task_item['name']) ? $task_item['name'] : null; 
  196. $task_type = isset($task_item['type']) ? $task_item['type'] : null; 
  197. $task_auth = isset($task_item['auth']) ? $task_item['auth'] : null; 
  198. $task_query = isset($task_item['query']) ? $task_item['query'] : null; 
  199. $task_object = isset($task_item['object']) ? $task_item['object'] : null; 
  200. $task_status = isset($task_item['status']) ? $task_item['status'] : null; 
  201. $task_result = isset($task_item['result']) ? $task_item['result'] : null; 
  202. // make sure we don't repeat execution of already finished tasks 
  203. if ($task_status == 'done') { 
  204. $done_count++; 
  205. // for previously finished tasks, store the result as it may be needed by future tasks 
  206. if ($task_id != null && $task_result != null) { 
  207. $task_list_result[$task_id] = $task_result; 
  208. continue; 
  209. // make sure only valid and authorized tasks are executed 
  210. if ($task_status == 'error' || $task_auth != 'allow') { 
  211. $skip_count++; 
  212. continue; 
  213. // the task query ID can be a simple (integer) ID or more complex ID that gets converted to a simple ID, for instance to point to an object that is the result of a previously finished task 
  214. if (isset($task_query['id'])) { 
  215. $task_query['id'] = $this->get_object_id($task_query['id'], $task_list_result); 
  216. $task_error = null; 
  217. switch ($task_type) { 
  218. case 'gallery_add': 
  219. $mapper = C_Gallery_Mapper::get_instance(); 
  220. $gallery = null; 
  221. $gal_errors = ''; 
  222. if (isset($task_query['id'])) { 
  223. $gallery = $mapper->find($task_query['id'], true); 
  224. if ($gallery == null) { 
  225. $title = isset($task_object['title']) ? $task_object['title'] : ''; 
  226. $gallery = $mapper->create(array('title' => $title)); 
  227. if (!$gallery || !$gallery->save()) { 
  228. if ($gallery != null) { 
  229. $gal_errors = $gallery->get_errors(); 
  230. if ($gal_errors != null) { 
  231. $gal_errors = ' [' . json_encode($gal_errors) . ']'; 
  232. $gallery = null; 
  233. if ($gallery != null) { 
  234. $task_status = 'done'; 
  235. $task_result['object_id'] = $gallery->id(); 
  236. } else { 
  237. $task_status = 'error'; 
  238. $task_error = array('level' => 'fatal', 'message' => sprintf(__('Gallery creation failed for "%1$s"%2$s.', 'nggallery'), $title, $gal_errors)); 
  239. break; 
  240. case 'gallery_remove': 
  241. case 'gallery_edit': 
  242. if (isset($task_query['id'])) { 
  243. $mapper = C_Gallery_Mapper::get_instance(); 
  244. $gallery = $mapper->find($task_query['id'], true); 
  245. $error = null; 
  246. if ($gallery != null) { 
  247. if ($task_type == 'gallery_remove') { 
  248. if (!$mapper->destroy($gallery, true)) { 
  249. $error = __('Failed to remove gallery (%1$s).', 'nggallery'); 
  250. } else { 
  251. if ($task_type == 'gallery_edit') { 
  252. if (isset($task_object['name'])) { 
  253. $gallery->name = $task_object['name']; 
  254. if (isset($task_object['title'])) { 
  255. $gallery->title = $task_object['title']; 
  256. if (isset($task_object['description'])) { 
  257. $gallery->galdesc = $task_object['description']; 
  258. if (isset($task_object['preview_image'])) { 
  259. $gallery->previewpic = $task_object['preview_image']; 
  260. if (isset($task_object['property_list'])) { 
  261. $properties = $task_object['property_list']; 
  262. foreach ($properties as $key => $value) { 
  263. $gallery->{$key} = $value; 
  264. // this is used to determine whether the task is complete 
  265. $image_list_unfinished = false; 
  266. if (isset($task_object['image_list']) && $wp_fs != null) { 
  267. $storage_path = isset($task_object['storage_path']) ? $task_object['storage_path'] : null; 
  268. $storage_path = trim($storage_path, '/\\'); 
  269. $storage = C_Gallery_Storage::get_instance(); 
  270. $image_mapper = C_Image_Mapper::get_instance(); 
  271. $creds = true; 
  272. $images_folder = $path_prefix . $fs_sep . $storage_path . $fs_sep; 
  273. $images_folder = str_replace(array('\\', '/'), $fs_sep, $images_folder); 
  274. $images = $task_object['image_list']; 
  275. $result_images = isset($task_result['image_list']) ? $task_result['image_list'] : array(); 
  276. $image_count = count($images); 
  277. $result_image_count = count($result_images); 
  278. for ($image_index = $result_image_count; $image_index < $image_count; $image_index++) { 
  279. $image = $images[$image_index]; 
  280. $image_id = isset($image['id']) ? $image['id'] : null; 
  281. $image_filename = isset($image['filename']) ? $image['filename'] : null; 
  282. $image_path = isset($image['path']) ? $image['path'] : null; 
  283. $image_action = isset($image['action']) ? $image['action'] : null; 
  284. $image_status = 'skip'; 
  285. if ($image_filename == null) { 
  286. $image_filename = basename($image_path); 
  287. $ngg_image = $image_mapper->find($image_id, TRUE); 
  288. // ensure that we don't transpose the image from one gallery to another in case a remoteId is passed in for the image but the gallery associated to the collection cannot be found 
  289. if ($ngg_image && $ngg_image->galleryid != $gallery->id()) { 
  290. $ngg_image = null; 
  291. $image_id = null; 
  292. $image_error = null; 
  293. if ($image_action == "delete") { 
  294. // image was deleted 
  295. if ($ngg_image != null) { 
  296. $settings = C_NextGen_Settings::get_instance(); 
  297. $delete_fine = true; 
  298. if ($settings->deleteImg) { 
  299. if (!$storage->delete_image($ngg_image)) { 
  300. $image_error = __('Could not delete image file(s) from disk (%1$s).', 'nggallery'); 
  301. } else { 
  302. if (!$image_mapper->destroy($ngg_image)) { 
  303. $image_error = __('Could not remove image from gallery (%1$s).', 'nggallery'); 
  304. if ($image_error == null) { 
  305. do_action('ngg_delete_picture', $ngg_image->{$ngg_image->id_field}); 
  306. $image_status = 'done'; 
  307. } else { 
  308. $image_error = __('Could not remove image because image was not found (%1$s).', 'nggallery'); 
  309. } else { 
  310. /** image was added or edited and needs updating */ 
  311. $image_path = $images_folder . $image_path; 
  312. if ($image_path != null && $wp_fs->exists($image_path)) { 
  313. try { 
  314. $ngg_image = $storage->upload_base64_image($gallery, $wp_fs->get_contents($image_path), $image_filename, $image_id, true); 
  315. if ($ngg_image != null) { 
  316. $image_status = 'done'; 
  317. $image_id = $ngg_image->{$ngg_image->id_field}; 
  318. } catch (E_NoSpaceAvailableException $e) { 
  319. $image_error = __('No space available for image (%1$s).', 'nggallery'); 
  320. } catch (E_UploadException $e) { 
  321. $image_error = $e->getMessage . __(' (%1$s).', 'nggallery'); 
  322. } catch (E_No_Image_Library_Exception $e) { 
  323. $error = __('No image library present, image uploads will fail (%1$s).', 'nggallery'); 
  324. // no point in continuing if the image library is not present but we don't break here to ensure that all images are processed (otherwise they'd be processed in further fruitless handle_job calls) 
  325. } catch (E_InsufficientWriteAccessException $e) { 
  326. $image_error = __('Inadequate system permissions to write image (%1$s).', 'nggallery'); 
  327. } catch (E_InvalidEntityException $e) { 
  328. $image_error = __('Requested image with id (%2$s) doesn\'t exist (%1$s).', 'nggallery'); 
  329. } catch (E_EntityNotFoundException $e) { 
  330. // gallery doesn't exist - already checked above so this should never happen 
  331. // delete temporary image 
  332. $wp_fs->delete($image_path); 
  333. } else { 
  334. $image_error = __('Could not find image file for image (%1$s).', 'nggallery'); 
  335. if ($image_error != null) { 
  336. $image_status = 'error'; 
  337. $image['error'] = array('level' => 'fatal', 'message' => sprintf($image_error, $image_filename, $image_id)); 
  338. if ($image_id) { 
  339. $image['id'] = $image_id; 
  340. if ($image_status) { 
  341. $image['status'] = $image_status; 
  342. // append processed image to result image_list array 
  343. $result_images[] = $image; 
  344. if ($this->should_stop_execution()) { 
  345. break; 
  346. $task_result['image_list'] = $result_images; 
  347. $image_list_unfinished = count($result_images) < $image_count; 
  348. // if images have finished processing, remove the folder used to store the temporary images (the folder should be empty due to delete() calls above) 
  349. if (!$image_list_unfinished) { 
  350. $wp_fs->rmdir($images_folder); 
  351. } else { 
  352. if ($wp_fs == null) { 
  353. $error = __('Could not access file system for gallery (%1$s).', 'nggallery'); 
  354. if (!$gallery->save()) { 
  355. if ($error == null) { 
  356. $gal_errors = '[' . json_encode($gallery->get_errors()) . ']'; 
  357. $error = __('Failed to save modified gallery (%1$s). ' . $gal_errors, 'nggallery'); 
  358. } else { 
  359. $error = __('Could not find gallery (%1$s).', 'nggallery'); 
  360. // XXX workaround for $gallery->save() returning false even if successful 
  361. if (isset($task_result['image_list'])) { 
  362. $task_result['object_id'] = $gallery->id(); 
  363. if ($error == null) { 
  364. $task_status = 'done'; 
  365. $task_result['object_id'] = $gallery->id(); 
  366. } else { 
  367. $task_status = 'error'; 
  368. $task_error = array('level' => 'fatal', 'message' => sprintf($error, (string) $task_query['id'])); 
  369. if ($image_list_unfinished) { 
  370. // we override the status of the task when the image list has not finished processing 
  371. $task_status = 'unfinished'; 
  372. } else { 
  373. $task_status = 'error'; 
  374. $task_error = array('level' => 'fatal', 'message' => __('No gallery was specified to edit.', 'nggallery')); 
  375. break; 
  376. case 'album_add': 
  377. $mapper = C_Album_Mapper::get_instance(); 
  378. $name = isset($task_object['name']) ? $task_object['name'] : ''; 
  379. $desc = isset($task_object['description']) ? $task_object['description'] : ''; 
  380. $previewpic = isset($task_object['preview_image']) ? $task_object['preview_image'] : 0; 
  381. $sortorder = isset($task_object['sort_order']) ? $task_object['sort_order'] : ''; 
  382. $page_id = isset($task_object['page_id']) ? $task_object['page_id'] : 0; 
  383. $album = null; 
  384. if (isset($task_query['id'])) { 
  385. $album = $mapper->find($task_query['id'], true); 
  386. if ($album == null) { 
  387. $album = $mapper->create(array('name' => $name, 'previewpic' => $previewpic, 'albumdesc' => $desc, 'sortorder' => $sortorder, 'pageid' => $page_id)); 
  388. if (!$album || !$album->save()) { 
  389. $album = null; 
  390. if ($album != null) { 
  391. $task_status = 'done'; 
  392. $task_result['object_id'] = $album->id(); 
  393. } else { 
  394. $task_status = 'error'; 
  395. $task_error = array('level' => 'fatal', 'message' => __('Album creation failed.', 'nggallery')); 
  396. break; 
  397. case 'album_remove': 
  398. case 'album_edit': 
  399. if (isset($task_query['id'])) { 
  400. $mapper = C_Album_Mapper::get_instance(); 
  401. $album = $mapper->find($task_query['id'], true); 
  402. $error = null; 
  403. if ($album) { 
  404. if ($task_type == 'album_remove') { 
  405. if (!$album->destroy()) { 
  406. $error = __('Failed to remove album (%1$s).', 'nggallery'); 
  407. } else { 
  408. if ($task_type == 'album_edit') { 
  409. if (isset($task_object['name'])) { 
  410. $album->name = $task_object['name']; 
  411. if (isset($task_object['description'])) { 
  412. $album->albumdesc = $task_object['description']; 
  413. if (isset($task_object['preview_image'])) { 
  414. $album->previewpic = $task_object['preview_image']; 
  415. if (isset($task_object['property_list'])) { 
  416. $properties = $task_object['property_list']; 
  417. foreach ($properties as $key => $value) { 
  418. $album->{$key} = $value; 
  419. if (isset($task_object['item_list'])) { 
  420. $item_list = $task_object['item_list']; 
  421. $sortorder = $album->sortorder; 
  422. $count = count($sortorder); 
  423. $album_items = array(); 
  424. for ($index = 0; $index < $count; $index++) { 
  425. $album_items[$sortorder[$index]] = $index; 
  426. foreach ($item_list as $item_info) { 
  427. $item_id = isset($item_info['id']) ? $item_info['id'] : null; 
  428. $item_type = isset($item_info['type']) ? $item_info['type'] : null; 
  429. $item_index = isset($item_info['index']) ? $item_info['index'] : null; 
  430. // translate ID in case this gallery has been created as part of this job 
  431. $item_id = $this->get_object_id($item_id, $task_list_result); 
  432. if ($item_id != null) { 
  433. if ($item_type == 'album') { 
  434. $item_id = 'a' . $item_id; 
  435. $album_items[$item_id] = $count + $item_index; 
  436. asort($album_items); 
  437. $album->sortorder = array_keys($album_items); 
  438. if (!$mapper->save($album)) { 
  439. $error = __('Failed to save modified album (%1$s).', 'nggallery'); 
  440. } else { 
  441. $error = __('Could not find album (%1$s).', 'nggallery'); 
  442. if ($error == null) { 
  443. $task_status = 'done'; 
  444. $task_result['object_id'] = $album->id(); 
  445. } else { 
  446. $task_status = 'error'; 
  447. $task_error = array('level' => 'fatal', 'message' => sprintf($error, (string) $task_query['id'])); 
  448. } else { 
  449. $task_status = 'error'; 
  450. $task_error = array('level' => 'fatal', 'message' => __('No album was specified to edit.', 'nggallery')); 
  451. break; 
  452. case 'gallery_list_get': 
  453. $mapper = C_Gallery_Mapper::get_instance(); 
  454. $gallery_list = $mapper->find_all(); 
  455. $result_list = array(); 
  456. foreach ($gallery_list as $gallery) { 
  457. $gallery_result = array('id' => $gallery->id(), 'name' => $gallery->name, 'title' => $gallery->title, 'description' => $gallery->galdesc, 'preview_image' => $gallery->previewpic); 
  458. $result_list[] = $gallery_result; 
  459. $task_status = 'done'; 
  460. $task_result['gallery_list'] = $result_list; 
  461. break; 
  462. case 'image_list_move': 
  463. break; 
  464. $task_item['result'] = $task_result; 
  465. $task_item['status'] = $task_status; 
  466. $task_item['error'] = $task_error; 
  467. // for previously finished tasks, store the result as it may be needed by future tasks 
  468. if ($task_id != null && $task_result != null) { 
  469. $task_list_result[$task_id] = $task_result; 
  470. // if the task has finished, either successfully or unsuccessfully, increase count for done tasks 
  471. if ($task_status != 'unfinished') { 
  472. $done_count++; 
  473. if ($this->should_stop_execution()) { 
  474. break; 
  475. $this->set_job_task_list($job_id, $task_list); 
  476. if ($task_count > $done_count + $skip_count) { 
  477. // unfinished tasks, return false 
  478. return false; 
  479. } else { 
  480. // everything was finished, remove job and write status file 
  481. $this->remove_job($job_id); 
  482. $status_file = '_ngg_job_status_' . strval($job_id) . '.txt'; 
  483. $status_content = json_encode($task_list); 
  484. if ($wp_fs != null) { 
  485. $status_path = $path_prefix . $fs_sep . $status_file; 
  486. $status_path = str_replace(array('\\', '/'), $fs_sep, $status_path); 
  487. $wp_fs->put_contents($status_path, $status_content); 
  488. } else { 
  489. // if WP_Filesystem failed try one last desperate attempt at direct file writing 
  490. $status_path = str_replace($ftp_path, $root_path, $full_path) . DIRECTORY_SEPARATOR . $status_file; 
  491. $status_path = str_replace(array('\\', '/'), DIRECTORY_SEPARATOR, $status_path); 
  492. file_put_contents($status_path, $status_content); 
  493. return true;