PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Zend/File/Transfer/Adapter/Http.php

https://gitlab.com/LisovyiEvhenii/ismextensions
PHP | 487 lines | 293 code | 64 blank | 130 comment | 66 complexity | 98db3d564539a149becd025db2996089 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_File_Transfer
  17. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * @see Zend_File_Transfer_Adapter_Abstract
  23. */
  24. #require_once 'Zend/File/Transfer/Adapter/Abstract.php';
  25. /**
  26. * File transfer adapter class for the HTTP protocol
  27. *
  28. * @category Zend
  29. * @package Zend_File_Transfer
  30. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Zend_File_Transfer_Adapter_Http extends Zend_File_Transfer_Adapter_Abstract
  34. {
  35. protected static $_callbackApc = 'apc_fetch';
  36. protected static $_callbackUploadProgress = 'uploadprogress_get_info';
  37. /**
  38. * Constructor for Http File Transfers
  39. *
  40. * @param array $options OPTIONAL Options to set
  41. */
  42. public function __construct($options = array())
  43. {
  44. if (ini_get('file_uploads') == false) {
  45. #require_once 'Zend/File/Transfer/Exception.php';
  46. throw new Zend_File_Transfer_Exception('File uploads are not allowed in your php config!');
  47. }
  48. $this->setOptions($options);
  49. $this->_prepareFiles();
  50. $this->addValidator('Upload', false, $this->_files);
  51. }
  52. /**
  53. * Sets a validator for the class, erasing all previous set
  54. *
  55. * @param string|array $validator Validator to set
  56. * @param string|array $files Files to limit this validator to
  57. * @return Zend_File_Transfer_Adapter
  58. */
  59. public function setValidators(array $validators, $files = null)
  60. {
  61. $this->clearValidators();
  62. return $this->addValidators($validators, $files);
  63. }
  64. /**
  65. * Remove an individual validator
  66. *
  67. * @param string $name
  68. * @return Zend_File_Transfer_Adapter_Abstract
  69. */
  70. public function removeValidator($name)
  71. {
  72. if ($name == 'Upload') {
  73. return $this;
  74. }
  75. return parent::removeValidator($name);
  76. }
  77. /**
  78. * Remove an individual validator
  79. *
  80. * @param string $name
  81. * @return Zend_File_Transfer_Adapter_Abstract
  82. */
  83. public function clearValidators()
  84. {
  85. parent::clearValidators();
  86. $this->addValidator('Upload', false, $this->_files);
  87. return $this;
  88. }
  89. /**
  90. * Send the file to the client (Download)
  91. *
  92. * @param string|array $options Options for the file(s) to send
  93. * @return void
  94. * @throws Zend_File_Transfer_Exception Not implemented
  95. */
  96. public function send($options = null)
  97. {
  98. #require_once 'Zend/File/Transfer/Exception.php';
  99. throw new Zend_File_Transfer_Exception('Method not implemented');
  100. }
  101. /**
  102. * Checks if the files are valid
  103. *
  104. * @param string|array $files (Optional) Files to check
  105. * @return boolean True if all checks are valid
  106. */
  107. public function isValid($files = null)
  108. {
  109. // Workaround for WebServer not conforming HTTP and omitting CONTENT_LENGTH
  110. $content = 0;
  111. if (isset($_SERVER['CONTENT_LENGTH'])) {
  112. $content = $_SERVER['CONTENT_LENGTH'];
  113. } else if (!empty($_POST)) {
  114. $content = serialize($_POST);
  115. }
  116. // Workaround for a PHP error returning empty $_FILES when form data exceeds php settings
  117. if (empty($this->_files) && ($content > 0)) {
  118. if (is_array($files)) {
  119. if (0 === count($files)) {
  120. return false;
  121. }
  122. $files = current($files);
  123. }
  124. $temp = array($files => array(
  125. 'name' => $files,
  126. 'error' => 1));
  127. $validator = $this->_validators['Zend_Validate_File_Upload'];
  128. $validator->setFiles($temp)
  129. ->isValid($files, null);
  130. $this->_messages += $validator->getMessages();
  131. return false;
  132. }
  133. return parent::isValid($files);
  134. }
  135. /**
  136. * Receive the file from the client (Upload)
  137. *
  138. * @param string|array $files (Optional) Files to receive
  139. * @return bool
  140. */
  141. public function receive($files = null)
  142. {
  143. if (!$this->isValid($files)) {
  144. return false;
  145. }
  146. $check = $this->_getFiles($files);
  147. foreach ($check as $file => $content) {
  148. if (!$content['received']) {
  149. $directory = '';
  150. $destination = $this->getDestination($file);
  151. if ($destination !== null) {
  152. $directory = $destination . DIRECTORY_SEPARATOR;
  153. }
  154. $filename = $directory . $content['name'];
  155. $rename = $this->getFilter('Rename');
  156. if ($rename !== null) {
  157. $tmp = $rename->getNewName($content['tmp_name']);
  158. if ($tmp != $content['tmp_name']) {
  159. $filename = $tmp;
  160. }
  161. if (dirname($filename) == '.') {
  162. $filename = $directory . $filename;
  163. }
  164. $key = array_search(get_class($rename), $this->_files[$file]['filters']);
  165. unset($this->_files[$file]['filters'][$key]);
  166. }
  167. // Should never return false when it's tested by the upload validator
  168. if (!move_uploaded_file($content['tmp_name'], $filename)) {
  169. if ($content['options']['ignoreNoFile']) {
  170. $this->_files[$file]['received'] = true;
  171. $this->_files[$file]['filtered'] = true;
  172. continue;
  173. }
  174. $this->_files[$file]['received'] = false;
  175. return false;
  176. }
  177. if ($rename !== null) {
  178. $this->_files[$file]['destination'] = dirname($filename);
  179. $this->_files[$file]['name'] = basename($filename);
  180. }
  181. $this->_files[$file]['tmp_name'] = $filename;
  182. $this->_files[$file]['received'] = true;
  183. }
  184. if (!$content['filtered']) {
  185. if (!$this->_filter($file)) {
  186. $this->_files[$file]['filtered'] = false;
  187. return false;
  188. }
  189. $this->_files[$file]['filtered'] = true;
  190. }
  191. }
  192. return true;
  193. }
  194. /**
  195. * Checks if the file was already sent
  196. *
  197. * @param string|array $file Files to check
  198. * @return bool
  199. * @throws Zend_File_Transfer_Exception Not implemented
  200. */
  201. public function isSent($files = null)
  202. {
  203. #require_once 'Zend/File/Transfer/Exception.php';
  204. throw new Zend_File_Transfer_Exception('Method not implemented');
  205. }
  206. /**
  207. * Checks if the file was already received
  208. *
  209. * @param string|array $files (Optional) Files to check
  210. * @return bool
  211. */
  212. public function isReceived($files = null)
  213. {
  214. $files = $this->_getFiles($files, false, true);
  215. if (empty($files)) {
  216. return false;
  217. }
  218. foreach ($files as $content) {
  219. if ($content['received'] !== true) {
  220. return false;
  221. }
  222. }
  223. return true;
  224. }
  225. /**
  226. * Checks if the file was already filtered
  227. *
  228. * @param string|array $files (Optional) Files to check
  229. * @return bool
  230. */
  231. public function isFiltered($files = null)
  232. {
  233. $files = $this->_getFiles($files, false, true);
  234. if (empty($files)) {
  235. return false;
  236. }
  237. foreach ($files as $content) {
  238. if ($content['filtered'] !== true) {
  239. return false;
  240. }
  241. }
  242. return true;
  243. }
  244. /**
  245. * Has a file been uploaded ?
  246. *
  247. * @param array|string|null $file
  248. * @return bool
  249. */
  250. public function isUploaded($files = null)
  251. {
  252. $files = $this->_getFiles($files, false, true);
  253. if (empty($files)) {
  254. return false;
  255. }
  256. foreach ($files as $file) {
  257. if (empty($file['name'])) {
  258. return false;
  259. }
  260. }
  261. return true;
  262. }
  263. /**
  264. * Returns the actual progress of file up-/downloads
  265. *
  266. * @param string $id The upload to get the progress for
  267. * @return array|null
  268. */
  269. public static function getProgress($id = null)
  270. {
  271. if (!function_exists('apc_fetch') and !function_exists('uploadprogress_get_info')) {
  272. #require_once 'Zend/File/Transfer/Exception.php';
  273. throw new Zend_File_Transfer_Exception('Neither APC nor uploadprogress extension installed');
  274. }
  275. $session = 'Zend_File_Transfer_Adapter_Http_ProgressBar';
  276. $status = array(
  277. 'total' => 0,
  278. 'current' => 0,
  279. 'rate' => 0,
  280. 'message' => '',
  281. 'done' => false
  282. );
  283. if (is_array($id)) {
  284. if (isset($id['progress'])) {
  285. $adapter = $id['progress'];
  286. }
  287. if (isset($id['session'])) {
  288. $session = $id['session'];
  289. }
  290. if (isset($id['id'])) {
  291. $id = $id['id'];
  292. } else {
  293. unset($id);
  294. }
  295. }
  296. if (!empty($id) && (($id instanceof Zend_ProgressBar_Adapter) || ($id instanceof Zend_ProgressBar))) {
  297. $adapter = $id;
  298. unset($id);
  299. }
  300. if (empty($id)) {
  301. if (!isset($_GET['progress_key'])) {
  302. $status['message'] = 'No upload in progress';
  303. $status['done'] = true;
  304. } else {
  305. $id = $_GET['progress_key'];
  306. }
  307. }
  308. if (!empty($id)) {
  309. if (self::isApcAvailable()) {
  310. $call = call_user_func(self::$_callbackApc, ini_get('apc.rfc1867_prefix') . $id);
  311. if (is_array($call)) {
  312. $status = $call + $status;
  313. }
  314. } else if (self::isUploadProgressAvailable()) {
  315. $call = call_user_func(self::$_callbackUploadProgress, $id);
  316. if (is_array($call)) {
  317. $status = $call + $status;
  318. $status['total'] = $status['bytes_total'];
  319. $status['current'] = $status['bytes_uploaded'];
  320. $status['rate'] = $status['speed_average'];
  321. if ($status['total'] == $status['current']) {
  322. $status['done'] = true;
  323. }
  324. }
  325. }
  326. if (!is_array($call)) {
  327. $status['done'] = true;
  328. $status['message'] = 'Failure while retrieving the upload progress';
  329. } else if (!empty($status['cancel_upload'])) {
  330. $status['done'] = true;
  331. $status['message'] = 'The upload has been canceled';
  332. } else {
  333. $status['message'] = self::_toByteString($status['current']) . " - " . self::_toByteString($status['total']);
  334. }
  335. $status['id'] = $id;
  336. }
  337. if (isset($adapter) && isset($status['id'])) {
  338. if ($adapter instanceof Zend_ProgressBar_Adapter) {
  339. #require_once 'Zend/ProgressBar.php';
  340. $adapter = new Zend_ProgressBar($adapter, 0, $status['total'], $session);
  341. }
  342. if (!($adapter instanceof Zend_ProgressBar)) {
  343. #require_once 'Zend/File/Transfer/Exception.php';
  344. throw new Zend_File_Transfer_Exception('Unknown Adapter given');
  345. }
  346. if ($status['done']) {
  347. $adapter->finish();
  348. } else {
  349. $adapter->update($status['current'], $status['message']);
  350. }
  351. $status['progress'] = $adapter;
  352. }
  353. return $status;
  354. }
  355. /**
  356. * Checks the APC extension for progress information
  357. *
  358. * @return boolean
  359. */
  360. public static function isApcAvailable()
  361. {
  362. return (bool) ini_get('apc.enabled') && (bool) ini_get('apc.rfc1867') && is_callable(self::$_callbackApc);
  363. }
  364. /**
  365. * Checks the UploadProgress extension for progress information
  366. *
  367. * @return boolean
  368. */
  369. public static function isUploadProgressAvailable()
  370. {
  371. return is_callable(self::$_callbackUploadProgress);
  372. }
  373. /**
  374. * Prepare the $_FILES array to match the internal syntax of one file per entry
  375. *
  376. * @param array $files
  377. * @return array
  378. */
  379. protected function _prepareFiles()
  380. {
  381. $this->_files = array();
  382. foreach ($_FILES as $form => $content) {
  383. if (is_array($content['name'])) {
  384. foreach ($content as $param => $file) {
  385. foreach ($file as $number => $target) {
  386. $this->_files[$form . '_' . $number . '_'][$param] = $target;
  387. $this->_files[$form]['multifiles'][$number] = $form . '_' . $number . '_';
  388. }
  389. }
  390. $this->_files[$form]['name'] = $form;
  391. foreach($this->_files[$form]['multifiles'] as $key => $value) {
  392. $this->_files[$value]['options'] = $this->_options;
  393. $this->_files[$value]['validated'] = false;
  394. $this->_files[$value]['received'] = false;
  395. $this->_files[$value]['filtered'] = false;
  396. $mimetype = $this->_detectMimeType($this->_files[$value]);
  397. $this->_files[$value]['type'] = $mimetype;
  398. $filesize = $this->_detectFileSize($this->_files[$value]);
  399. $this->_files[$value]['size'] = $filesize;
  400. if ($this->_options['detectInfos']) {
  401. $_FILES[$form]['type'][$key] = $mimetype;
  402. $_FILES[$form]['size'][$key] = $filesize;
  403. }
  404. }
  405. } else {
  406. $this->_files[$form] = $content;
  407. $this->_files[$form]['options'] = $this->_options;
  408. $this->_files[$form]['validated'] = false;
  409. $this->_files[$form]['received'] = false;
  410. $this->_files[$form]['filtered'] = false;
  411. $mimetype = $this->_detectMimeType($this->_files[$form]);
  412. $this->_files[$form]['type'] = $mimetype;
  413. $filesize = $this->_detectFileSize($this->_files[$form]);
  414. $this->_files[$form]['size'] = $filesize;
  415. if ($this->_options['detectInfos']) {
  416. $_FILES[$form]['type'] = $mimetype;
  417. $_FILES[$form]['size'] = $filesize;
  418. }
  419. }
  420. }
  421. return $this;
  422. }
  423. }