fileupload.class.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. <?php
  2. /* Copyright (C) 2011-2022 Regis Houssin <regis.houssin@inodbox.com>
  3. * Copyright (C) 2011-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/core/class/fileupload.class.php
  20. * \brief File to return Ajax response on file upload
  21. *
  22. * Option MAIN_USE_JQUERY_FILEUPLOAD must be enabled to have feature working. Use is NOT secured !
  23. */
  24. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  25. require_once DOL_DOCUMENT_ROOT.'/core/lib/images.lib.php';
  26. /**
  27. * This class is used to manage file upload using ajax
  28. */
  29. class FileUpload
  30. {
  31. protected $options;
  32. protected $fk_element;
  33. protected $element;
  34. /**
  35. * Constructor
  36. *
  37. * @param array $options Options array
  38. * @param int $fk_element fk_element
  39. * @param string $element element
  40. */
  41. public function __construct($options = null, $fk_element = null, $element = null)
  42. {
  43. global $db, $conf;
  44. global $object;
  45. global $hookmanager;
  46. // Feature not enabled. Warning feature not used and not secured so disabled.
  47. if (!getDolGlobalInt('MAIN_USE_JQUERY_FILEUPLOAD')) {
  48. return;
  49. }
  50. $hookmanager->initHooks(array('fileupload'));
  51. $this->fk_element = $fk_element;
  52. $this->element = $element;
  53. $pathname = $filename = $element;
  54. if (preg_match('/^([^_]+)_([^_]+)/i', $element, $regs)) {
  55. $pathname = $regs[1];
  56. $filename = $regs[2];
  57. }
  58. $parentForeignKey = '';
  59. // For compatibility
  60. if ($element == 'propal') {
  61. $pathname = 'comm/propal';
  62. $dir_output = $conf->$element->dir_output;
  63. } elseif ($element == 'facture') {
  64. $pathname = 'compta/facture';
  65. $dir_output = $conf->$element->dir_output;
  66. } elseif ($element == 'project') {
  67. $element = $pathname = 'projet';
  68. $dir_output = $conf->$element->dir_output;
  69. } elseif ($element == 'project_task') {
  70. $pathname = 'projet';
  71. $filename = 'task';
  72. $dir_output = $conf->project->dir_output;
  73. $parentForeignKey = 'fk_project';
  74. $parentClass = 'Project';
  75. $parentElement = 'projet';
  76. $parentObject = 'project';
  77. } elseif ($element == 'fichinter') {
  78. $element = 'ficheinter';
  79. $dir_output = $conf->$element->dir_output;
  80. } elseif ($element == 'order_supplier') {
  81. $pathname = 'fourn';
  82. $filename = 'fournisseur.commande';
  83. $dir_output = $conf->fournisseur->commande->dir_output;
  84. } elseif ($element == 'invoice_supplier') {
  85. $pathname = 'fourn';
  86. $filename = 'fournisseur.facture';
  87. $dir_output = $conf->fournisseur->facture->dir_output;
  88. } elseif ($element == 'product') {
  89. $dir_output = $conf->product->multidir_output[$conf->entity];
  90. } elseif ($element == 'productbatch') {
  91. $dir_output = $conf->productbatch->multidir_output[$conf->entity];
  92. } elseif ($element == 'action') {
  93. $pathname = 'comm/action';
  94. $filename = 'actioncomm';
  95. $dir_output = $conf->agenda->dir_output;
  96. } elseif ($element == 'chargesociales') {
  97. $pathname = 'compta/sociales';
  98. $filename = 'chargesociales';
  99. $dir_output = $conf->tax->dir_output;
  100. } else {
  101. $dir_output = $conf->$element->dir_output;
  102. }
  103. // If pathname and filename are null then we can still upload files
  104. // IF we have specified upload_dir on $this->options
  105. if ($pathname !== null && $filename !== null) {
  106. dol_include_once('/'.$pathname.'/class/'.$filename.'.class.php');
  107. $classname = ucfirst($filename);
  108. if ($element == 'order_supplier') {
  109. $classname = 'CommandeFournisseur';
  110. } elseif ($element == 'invoice_supplier') {
  111. $classname = 'FactureFournisseur';
  112. }
  113. $object = new $classname($db);
  114. $object->fetch($fk_element);
  115. if (!empty($parentForeignKey)) {
  116. dol_include_once('/'.$parentElement.'/class/'.$parentObject.'.class.php');
  117. $parent = new $parentClass($db);
  118. $parent->fetch($object->$parentForeignKey);
  119. if (!empty($parent->socid)) {
  120. $parent->fetch_thirdparty();
  121. }
  122. $object->$parentObject = clone $parent;
  123. } else {
  124. $object->fetch_thirdparty();
  125. }
  126. $object_ref = dol_sanitizeFileName($object->ref);
  127. if ($element == 'invoice_supplier') {
  128. $object_ref = get_exdir($object->id, 2, 0, 0, $object, 'invoice_supplier').$object_ref;
  129. } elseif ($element == 'project_task') {
  130. $object_ref = $object->project->ref.'/'.$object_ref;
  131. }
  132. }
  133. $this->options = array(
  134. 'script_url' => $_SERVER['PHP_SELF'],
  135. 'upload_dir' => $dir_output.'/'.$object_ref.'/',
  136. 'upload_url' => DOL_URL_ROOT.'/document.php?modulepart='.$element.'&attachment=1&file=/'.$object_ref.'/',
  137. 'param_name' => 'files',
  138. // Set the following option to 'POST', if your server does not support
  139. // DELETE requests. This is a parameter sent to the client:
  140. 'delete_type' => 'DELETE',
  141. // The php.ini settings upload_max_filesize and post_max_size
  142. // take precedence over the following max_file_size setting:
  143. 'max_file_size' => null,
  144. 'min_file_size' => 1,
  145. 'accept_file_types' => '/.+$/i',
  146. // The maximum number of files for the upload directory:
  147. 'max_number_of_files' => null,
  148. // Image resolution restrictions:
  149. 'max_width' => null,
  150. 'max_height' => null,
  151. 'min_width' => 1,
  152. 'min_height' => 1,
  153. // Set the following option to false to enable resumable uploads:
  154. 'discard_aborted_uploads' => true,
  155. 'image_versions' => array(
  156. // Uncomment the following version to restrict the size of
  157. // uploaded images. You can also add additional versions with
  158. // their own upload directories:
  159. /*
  160. 'large' => array(
  161. 'upload_dir' => dirname($_SERVER['SCRIPT_FILENAME']).'/files/',
  162. 'upload_url' => $this->getFullUrl().'/files/',
  163. 'max_width' => 1920,
  164. 'max_height' => 1200,
  165. 'jpeg_quality' => 95
  166. ),
  167. */
  168. 'thumbnail' => array(
  169. 'upload_dir' => $dir_output.'/'.$object_ref.'/thumbs/',
  170. 'upload_url' => DOL_URL_ROOT.'/document.php?modulepart='.$element.'&attachment=1&file=/'.$object_ref.'/thumbs/',
  171. 'max_width' => 80,
  172. 'max_height' => 80
  173. )
  174. )
  175. );
  176. global $action;
  177. $hookmanager->executeHooks(
  178. 'overrideUploadOptions',
  179. array(
  180. 'options' => &$options,
  181. 'element' => $element
  182. ),
  183. $object,
  184. $action
  185. );
  186. if ($options) {
  187. $this->options = array_replace_recursive($this->options, $options);
  188. }
  189. // At this point we should have a valid upload_dir in options
  190. //if ($pathname === null && $filename === null) { // OR or AND???
  191. if ($pathname === null || $filename === null) {
  192. if (!key_exists("upload_dir", $this->options)) {
  193. setEventMessage('If $fk_element = null or $element = null you must specify upload_dir on $options', 'errors');
  194. throw new Exception('If $fk_element = null or $element = null you must specify upload_dir on $options');
  195. } elseif (!is_dir($this->options['upload_dir'])) {
  196. setEventMessage('The directory '.$this->options['upload_dir'].' doesn\'t exists', 'errors');
  197. throw new Exception('The directory '.$this->options['upload_dir'].' doesn\'t exists');
  198. } elseif (!is_writable($this->options['upload_dir'])) {
  199. setEventMessage('The directory '.$this->options['upload_dir'].' is not writable', 'errors');
  200. throw new Exception('The directory '.$this->options['upload_dir'].' is not writable');
  201. }
  202. }
  203. }
  204. /**
  205. * Return full URL
  206. *
  207. * @return string URL
  208. */
  209. protected function getFullUrl()
  210. {
  211. $https = !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off';
  212. return
  213. ($https ? 'https://' : 'http://').
  214. (!empty($_SERVER['REMOTE_USER']) ? $_SERVER['REMOTE_USER'].'@' : '').
  215. (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : ($_SERVER['SERVER_NAME'].
  216. ($https && $_SERVER['SERVER_PORT'] === 443 ||
  217. $_SERVER['SERVER_PORT'] === 80 ? '' : ':'.$_SERVER['SERVER_PORT']))).
  218. substr($_SERVER['SCRIPT_NAME'], 0, strrpos($_SERVER['SCRIPT_NAME'], '/'));
  219. }
  220. /**
  221. * Set delete url
  222. *
  223. * @param object $file Filename
  224. * @return void
  225. */
  226. protected function setFileDeleteUrl($file)
  227. {
  228. $file->delete_url = $this->options['script_url']
  229. .'?file='.urlencode($file->name).'&fk_element='.urlencode($this->fk_element).'&element='.urlencode($this->element);
  230. $file->delete_type = $this->options['delete_type'];
  231. if ($file->delete_type !== 'DELETE') {
  232. $file->delete_url .= '&_method=DELETE';
  233. }
  234. }
  235. /**
  236. * getFileObject
  237. *
  238. * @param string $file_name Filename
  239. * @return stdClass|null
  240. */
  241. protected function getFileObject($file_name)
  242. {
  243. if (!getDolGlobalInt('MAIN_USE_JQUERY_FILEUPLOAD')) {
  244. return null;
  245. }
  246. $file_path = $this->options['upload_dir'].$file_name;
  247. if (is_file($file_path) && $file_name[0] !== '.') {
  248. $file = new stdClass();
  249. $file->name = $file_name;
  250. $file->mime = dol_mimetype($file_name, '', 2);
  251. $file->size = filesize($file_path);
  252. $file->url = $this->options['upload_url'].rawurlencode($file->name);
  253. foreach ($this->options['image_versions'] as $version => $options) {
  254. if (is_file($options['upload_dir'].$file_name)) {
  255. $tmp = explode('.', $file->name);
  256. $file->{$version.'_url'} = $options['upload_url'].rawurlencode($tmp[0].'_mini.'.$tmp[1]);
  257. }
  258. }
  259. $this->setFileDeleteUrl($file);
  260. return $file;
  261. }
  262. return null;
  263. }
  264. /**
  265. * getFileObjects
  266. *
  267. * @return array Array of objects
  268. */
  269. protected function getFileObjects()
  270. {
  271. return array_values(array_filter(array_map(array($this, 'getFileObject'), scandir($this->options['upload_dir']))));
  272. }
  273. /**
  274. * Create thumbs of a file uploaded. Only the "mini" thumb is generated.
  275. *
  276. * @param string $file_name Filename
  277. * @param string $options is array('max_width', 'max_height')
  278. * @return boolean
  279. */
  280. protected function createScaledImage($file_name, $options)
  281. {
  282. global $maxwidthmini, $maxheightmini;
  283. if (!getDolGlobalInt('MAIN_USE_JQUERY_FILEUPLOAD')) {
  284. return false;
  285. }
  286. $file_path = $this->options['upload_dir'].$file_name;
  287. $new_file_path = $options['upload_dir'].$file_name;
  288. if (dol_mkdir($options['upload_dir']) >= 0) {
  289. list($img_width, $img_height) = @getimagesize($file_path);
  290. if (!$img_width || !$img_height) {
  291. return false;
  292. }
  293. $res = vignette($file_path, $maxwidthmini, $maxheightmini, '_mini'); // We don't use ->addThumbs here because there is no object and we don't need all thumbs, only the "mini".
  294. if (preg_match('/error/i', $res)) {
  295. return false;
  296. }
  297. return true;
  298. } else {
  299. return false;
  300. }
  301. }
  302. /**
  303. * Enter description here ...
  304. *
  305. * @param string $uploaded_file Uploade file
  306. * @param object $file File
  307. * @param string $error Error
  308. * @param string $index Index
  309. * @return boolean True if OK, False if KO
  310. */
  311. protected function validate($uploaded_file, $file, $error, $index)
  312. {
  313. if (!getDolGlobalInt('MAIN_USE_JQUERY_FILEUPLOAD')) {
  314. return false;
  315. }
  316. if ($error) {
  317. $file->error = $error;
  318. return false;
  319. }
  320. if (!$file->name) {
  321. $file->error = 'missingFileName';
  322. return false;
  323. }
  324. if (!preg_match($this->options['accept_file_types'], $file->name)) {
  325. $file->error = 'acceptFileTypes';
  326. return false;
  327. }
  328. if ($uploaded_file && is_uploaded_file($uploaded_file)) {
  329. $file_size = filesize($uploaded_file);
  330. } else {
  331. $file_size = $_SERVER['CONTENT_LENGTH'];
  332. }
  333. if ($this->options['max_file_size'] && (
  334. $file_size > $this->options['max_file_size'] ||
  335. $file->size > $this->options['max_file_size'])
  336. ) {
  337. $file->error = 'maxFileSize';
  338. return false;
  339. }
  340. if ($this->options['min_file_size'] &&
  341. $file_size < $this->options['min_file_size']) {
  342. $file->error = 'minFileSize';
  343. return false;
  344. }
  345. if (is_numeric($this->options['max_number_of_files']) && (
  346. count($this->getFileObjects()) >= $this->options['max_number_of_files'])
  347. ) {
  348. $file->error = 'maxNumberOfFiles';
  349. return false;
  350. }
  351. list($img_width, $img_height) = @getimagesize($uploaded_file);
  352. if (is_numeric($img_width)) {
  353. if ($this->options['max_width'] && $img_width > $this->options['max_width'] ||
  354. $this->options['max_height'] && $img_height > $this->options['max_height']) {
  355. $file->error = 'maxResolution';
  356. return false;
  357. }
  358. if ($this->options['min_width'] && $img_width < $this->options['min_width'] ||
  359. $this->options['min_height'] && $img_height < $this->options['min_height']) {
  360. $file->error = 'minResolution';
  361. return false;
  362. }
  363. }
  364. return true;
  365. }
  366. /**
  367. * Enter description here ...
  368. *
  369. * @param int $matches ???
  370. * @return string ???
  371. */
  372. protected function upcountNameCallback($matches)
  373. {
  374. $index = isset($matches[1]) ? intval($matches[1]) + 1 : 1;
  375. $ext = isset($matches[2]) ? $matches[2] : '';
  376. return ' ('.$index.')'.$ext;
  377. }
  378. /**
  379. * Enter description here ...
  380. *
  381. * @param string $name ???
  382. * @return string ???
  383. */
  384. protected function upcountName($name)
  385. {
  386. return preg_replace_callback('/(?:(?: \(([\d]+)\))?(\.[^.]+))?$/', array($this, 'upcountNameCallback'), $name, 1);
  387. }
  388. /**
  389. * trimFileName
  390. *
  391. * @param string $name Filename
  392. * @param string $type ???
  393. * @param string $index ???
  394. * @return string
  395. */
  396. protected function trimFileName($name, $type, $index)
  397. {
  398. // Remove path information and dots around the filename, to prevent uploading
  399. // into different directories or replacing hidden system files.
  400. // Also remove control characters and spaces (\x00..\x20) around the filename:
  401. $file_name = trim(basename(stripslashes($name)), ".\x00..\x20");
  402. // Add missing file extension for known image types:
  403. $matches = array();
  404. if (strpos($file_name, '.') === false && preg_match('/^image\/(gif|jpe?g|png)/', $type, $matches)) {
  405. $file_name .= '.'.$matches[1];
  406. }
  407. if ($this->options['discard_aborted_uploads']) {
  408. while (is_file($this->options['upload_dir'].$file_name)) {
  409. $file_name = $this->upcountName($file_name);
  410. }
  411. }
  412. return $file_name;
  413. }
  414. /**
  415. * handleFileUpload
  416. *
  417. * @param string $uploaded_file Uploade file
  418. * @param string $name Name
  419. * @param int $size Size
  420. * @param string $type Type
  421. * @param string $error Error
  422. * @param string $index Index
  423. * @return stdClass|null
  424. */
  425. protected function handleFileUpload($uploaded_file, $name, $size, $type, $error, $index)
  426. {
  427. if (!getDolGlobalInt('MAIN_USE_JQUERY_FILEUPLOAD')) {
  428. return null;
  429. }
  430. $file = new stdClass();
  431. $file->name = $this->trimFileName($name, $type, $index);
  432. $file->mime = dol_mimetype($file->name, '', 2);
  433. $file->size = intval($size);
  434. $file->type = $type;
  435. if ($this->validate($uploaded_file, $file, $error, $index) && dol_mkdir($this->options['upload_dir']) >= 0) {
  436. $file_path = $this->options['upload_dir'].$file->name;
  437. $append_file = !$this->options['discard_aborted_uploads'] && is_file($file_path) && $file->size > filesize($file_path);
  438. clearstatcache();
  439. if ($uploaded_file && is_uploaded_file($uploaded_file)) {
  440. // multipart/formdata uploads (POST method uploads)
  441. if ($append_file) {
  442. file_put_contents($file_path, fopen($uploaded_file, 'r'), FILE_APPEND);
  443. } else {
  444. dol_move_uploaded_file($uploaded_file, $file_path, 1, 0, 0, 0, 'userfile');
  445. }
  446. } else {
  447. // Non-multipart uploads (PUT method support)
  448. file_put_contents($file_path, fopen('php://input', 'r'), $append_file ? FILE_APPEND : 0);
  449. }
  450. $file_size = filesize($file_path);
  451. if ($file_size === $file->size) {
  452. $file->url = $this->options['upload_url'].rawurlencode($file->name);
  453. foreach ($this->options['image_versions'] as $version => $options) {
  454. if ($this->createScaledImage($file->name, $options)) {
  455. $tmp = explode('.', $file->name);
  456. $file->{$version.'_url'} = $options['upload_url'].rawurlencode($tmp[0].'_mini.'.$tmp[1]);
  457. }
  458. }
  459. } elseif ($this->options['discard_aborted_uploads']) {
  460. unlink($file_path);
  461. $file->error = 'abort';
  462. }
  463. $file->size = $file_size;
  464. $this->setFileDeleteUrl($file);
  465. }
  466. return $file;
  467. }
  468. /**
  469. * Output data
  470. *
  471. * @return void
  472. */
  473. public function get()
  474. {
  475. if (!getDolGlobalInt('MAIN_USE_JQUERY_FILEUPLOAD')) {
  476. return;
  477. }
  478. $file_name = isset($_REQUEST['file']) ?
  479. basename(stripslashes($_REQUEST['file'])) : null;
  480. if ($file_name) {
  481. $info = $this->getFileObject($file_name);
  482. } else {
  483. $info = $this->getFileObjects();
  484. }
  485. header('Content-type: application/json');
  486. echo json_encode($info);
  487. }
  488. /**
  489. * Output data
  490. *
  491. * @return string|void
  492. */
  493. public function post()
  494. {
  495. if (!getDolGlobalInt('MAIN_USE_JQUERY_FILEUPLOAD')) {
  496. return;
  497. }
  498. if (isset($_REQUEST['_method']) && $_REQUEST['_method'] === 'DELETE') {
  499. return $this->delete();
  500. }
  501. $upload = isset($_FILES[$this->options['param_name']]) ?
  502. $_FILES[$this->options['param_name']] : null;
  503. $info = array();
  504. if ($upload && is_array($upload['tmp_name'])) {
  505. // param_name is an array identifier like "files[]",
  506. // $_FILES is a multi-dimensional array:
  507. foreach ($upload['tmp_name'] as $index => $value) {
  508. $info[] = $this->handleFileUpload(
  509. $upload['tmp_name'][$index],
  510. isset($_SERVER['HTTP_X_FILE_NAME']) ? $_SERVER['HTTP_X_FILE_NAME'] : $upload['name'][$index],
  511. isset($_SERVER['HTTP_X_FILE_SIZE']) ? $_SERVER['HTTP_X_FILE_SIZE'] : $upload['size'][$index],
  512. isset($_SERVER['HTTP_X_FILE_TYPE']) ? $_SERVER['HTTP_X_FILE_TYPE'] : $upload['type'][$index],
  513. $upload['error'][$index],
  514. $index
  515. );
  516. }
  517. } elseif ($upload || isset($_SERVER['HTTP_X_FILE_NAME'])) {
  518. // param_name is a single object identifier like "file",
  519. // $_FILES is a one-dimensional array:
  520. $info[] = $this->handleFileUpload(
  521. isset($upload['tmp_name']) ? $upload['tmp_name'] : null,
  522. isset($_SERVER['HTTP_X_FILE_NAME']) ? $_SERVER['HTTP_X_FILE_NAME'] : (isset($upload['name']) ? $upload['name'] : null),
  523. isset($_SERVER['HTTP_X_FILE_SIZE']) ? $_SERVER['HTTP_X_FILE_SIZE'] : (isset($upload['size']) ? $upload['size'] : null),
  524. isset($_SERVER['HTTP_X_FILE_TYPE']) ? $_SERVER['HTTP_X_FILE_TYPE'] : (isset($upload['type']) ? $upload['type'] : null),
  525. isset($upload['error']) ? $upload['error'] : null,
  526. 0
  527. );
  528. }
  529. header('Vary: Accept');
  530. $json = json_encode($info);
  531. $redirect = isset($_REQUEST['redirect']) ?
  532. stripslashes($_REQUEST['redirect']) : null;
  533. if ($redirect) {
  534. header('Location: '.sprintf($redirect, rawurlencode($json)));
  535. return;
  536. }
  537. if (isset($_SERVER['HTTP_ACCEPT']) &&
  538. (strpos($_SERVER['HTTP_ACCEPT'], 'application/json') !== false)) {
  539. header('Content-type: application/json');
  540. } else {
  541. header('Content-type: text/plain');
  542. }
  543. echo $json;
  544. }
  545. /**
  546. * Delete uploaded file
  547. *
  548. * @return string|null
  549. */
  550. public function delete()
  551. {
  552. if (!getDolGlobalInt('MAIN_USE_JQUERY_FILEUPLOAD')) {
  553. return null;
  554. }
  555. $file_name = isset($_REQUEST['file']) ?
  556. basename(stripslashes($_REQUEST['file'])) : null;
  557. $file_path = $this->options['upload_dir'].$file_name;
  558. $success = is_file($file_path) && $file_name[0] !== '.' && unlink($file_path);
  559. if ($success) {
  560. foreach ($this->options['image_versions'] as $version => $options) {
  561. $file = $options['upload_dir'].$file_name;
  562. if (is_file($file)) {
  563. unlink($file);
  564. }
  565. }
  566. }
  567. header('Content-type: application/json');
  568. echo json_encode($success);
  569. }
  570. }