PageRenderTime 47ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/storecommander/ead6f6fce09/SC/lib/js/ajaxfilemanager/ajax_image_save.php

https://gitlab.com/ptisky/API_prestashop
PHP | 215 lines | 164 code | 20 blank | 31 comment | 31 complexity | 4a58be6a89e46371d206e24c87e51729 MD5 | raw file
  1. <?php
  2. /**
  3. * Store Commander
  4. *
  5. * @category administration
  6. * @author Store Commander - support@storecommander.com
  7. * @version 2015-09-15
  8. * @uses Prestashop modules
  9. * @since 2009
  10. * @copyright Copyright &copy; 2009-2015, Store Commander
  11. * @license commercial
  12. * All rights reserved! Copying, duplication strictly prohibited
  13. *
  14. * *****************************************
  15. * * STORE COMMANDER *
  16. * * http://www.StoreCommander.com *
  17. * * V 2015-09-15 *
  18. * *****************************************
  19. *
  20. * Compatibility: PS version: 1.1 to 1.6.1
  21. *
  22. **/
  23. /**
  24. * image save function
  25. * @author Logan Cai (cailongqun [at] yahoo [dot] com [dot] cn)
  26. * @link www.phpletter.com
  27. * @since 22/May/2007
  28. *
  29. */
  30. require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . "inc" . DIRECTORY_SEPARATOR . "config.php");
  31. $error = "";
  32. $info = "";
  33. if (CONFIG_SYS_VIEW_ONLY || !CONFIG_OPTIONS_EDITABLE)
  34. {
  35. $error = SYS_DISABLED;
  36. }
  37. elseif (empty($_POST['path']))
  38. {
  39. $error = IMG_SAVE_EMPTY_PATH;
  40. }elseif (!file_exists($_POST['path']))
  41. {
  42. $error = IMG_SAVE_NOT_EXISTS;
  43. }elseif (!isUnderRoot($_POST['path']))
  44. {
  45. $error = IMG_SAVE_PATH_DISALLOWED;
  46. }elseif (($sessionDir = $session->getSessionDir()) == '')
  47. {
  48. $error = SESSION_PERSONAL_DIR_NOT_FOUND;
  49. }
  50. else
  51. {
  52. require_once(CLASS_HISTORY);
  53. $history = new History($_POST['path'], $session);
  54. if (!empty($_POST['mode']))
  55. {
  56. //get the original image which is the lastest session image if any when the system is in demo
  57. $lastestSessionImageInfo = $history->getLastestRestorable();
  58. if (sizeof($lastestSessionImageInfo) && CONFIG_SYS_DEMO_ENABLE)
  59. {
  60. $originalSessionImageInfo = $history->getOriginalImage();
  61. if (sizeof($originalSessionImageInfo))
  62. {
  63. $originalImage = $sessionDir . $originalSessionImageInfo['info']['name'];
  64. }
  65. }
  66. if (empty($originalImage))
  67. {
  68. $originalImage = $_POST['path'];
  69. }
  70. include_once(CLASS_IMAGE);
  71. $image = new Image();
  72. if ($image->loadImage($originalImage))
  73. {
  74. switch($_POST['mode'])
  75. {
  76. case "resize":
  77. if (!$image->resize($_POST['width'], $_POST['height'], (!empty($_POST['constraint'])?true:false)))
  78. {
  79. $error = IMG_SAVE_RESIZE_FAILED;
  80. }
  81. break;
  82. case "crop":
  83. if (!$image->crop($_POST['x'], $_POST['y'], $_POST['width'], $_POST['height']))
  84. {
  85. $error = IMG_SAVE_CROP_FAILED;
  86. }
  87. break;
  88. case "flip":
  89. if (!$image->flip($_POST['flip_angle']))
  90. {
  91. $error = IMG_SAVE_FLIP_FAILED;
  92. }
  93. break;
  94. case "rotate":
  95. if (!$image->rotate((int)($_POST['angle'])))
  96. {
  97. $error = IMG_SAVE_ROTATE_FAILED;
  98. }
  99. break;
  100. default:
  101. $error = IMG_SAVE_UNKNOWN_MODE;
  102. }
  103. if (empty($error))
  104. {
  105. $sessionNewPath = $sessionDir . uniqid(md5(time())) . "." . getFileExt($_POST['path']);
  106. if (!@copy($originalImage, $sessionNewPath))
  107. {//keep a copy under the session folder
  108. $error = IMG_SAVE_BACKUP_FAILED;
  109. }else
  110. {
  111. $isSaveAsRequest = (!empty($_POST['new_name']) && !empty($_POST['save_to'])?true:false);
  112. //save the modified image
  113. $sessionImageInfo = array('name'=>basename($sessionNewPath), 'restorable'=>1);
  114. $history->add($sessionImageInfo);
  115. if (CONFIG_SYS_DEMO_ENABLE)
  116. {//demo only
  117. if (isset($originalSessionImageInfo) && sizeof($originalSessionImageInfo))
  118. {
  119. $imagePath = $sessionDir . $originalSessionImageInfo['info']['name'];
  120. }else
  121. {
  122. $imagePath = $sessionDir . uniqid(md5(time())) . "." . getFileExt($_POST['path']);
  123. }
  124. }else
  125. {
  126. if ($isSaveAsRequest)
  127. {//save as request
  128. //check save to folder if exists
  129. $imagePath = addTrailingSlash(backslashToSlash($_POST['save_to'])) . $_POST['new_name'] . "." . getFileExt($_POST['path']);
  130. if (!file_exists($_POST['save_to']) || !is_dir($_POST['save_to']))
  131. {
  132. $error = IMG_SAVE_AS_FOLDER_NOT_FOUND;
  133. }elseif (file_exists($imagePath))
  134. {
  135. $error = IMG_SAVE_AS_NEW_IMAGE_EXISTS;
  136. }elseif (!preg_match("/^[a-zA-Z0-9_\- ]+$/", $_POST['new_name']))
  137. {
  138. $error = IMG_SAVE_AS_ERR_NAME_INVALID;
  139. }
  140. }else
  141. {//save request
  142. $imagePath = $originalImage;
  143. }
  144. }
  145. if ($image->saveImage($imagePath))
  146. {
  147. if (CONFIG_SYS_DEMO_ENABLE)
  148. {
  149. if (!isset($originalSessionImageInfo) || !sizeof($originalSessionImageInfo))
  150. {//keep this original image information on session for future reference if demo only
  151. $originalSessionImageInfo = array('name'=>basename($imagePath), 'restorable'=>0, 'is_original'=>1);
  152. $history->add($originalSessionImageInfo);
  153. }
  154. }
  155. $imageInfo = $image->getFinalImageInfo();
  156. }else
  157. {
  158. $error = IMG_SAVE_FAILED;
  159. }
  160. if (isset($imageInfo))
  161. {
  162. $info .= ",width:" . $imageInfo['width'] . "";
  163. $info .= ",height:" . $imageInfo['height'] . "";
  164. $info .= ",size:'" . transformFileSize($imageInfo['size']) . "'";
  165. if ($isSaveAsRequest)
  166. {
  167. $info .= ",save_as:'1'";
  168. }else
  169. {
  170. $info .= ",save_as:'0'";
  171. }
  172. $info .= ",folder_path:'" . dirname($imagePath) . "'";
  173. $info .= ",path:'" . backslashToSlash($imagePath) . "'";
  174. }
  175. }
  176. }
  177. }else
  178. {
  179. $error = IMG_SAVE_IMG_OPEN_FAILED;
  180. }
  181. }else
  182. {
  183. $error = IMG_SAVE_UNKNOWN_MODE;
  184. }
  185. }
  186. echo "{";
  187. echo "error:'" . $error . "'";
  188. if (isset($image) && is_object($image))
  189. {
  190. $image->DestroyImages();
  191. }
  192. echo $info;
  193. echo ",history:" . (isset($history) && is_object($history)?($history->getNumRestorable()):0) . "";
  194. echo "}";
  195. ?>