PageRenderTime 42ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/tine20/library/Zend/File/Transfer/Adapter/Http.php

https://gitlab.com/rsilveira1987/Expresso
PHP | 453 lines | 275 code | 56 blank | 122 comment | 61 complexity | 2579dc60211fdbbb4b77ef7ddc35c0f6 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-2009 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Http.php 10020 2009-08-18 14:34:09Z j.fischer@metaways.de $
  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-2008 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->_files = $this->_prepareFiles($_FILES);
  49. $this->addValidator('Upload', false, $this->_files);
  50. if (is_array($options)) {
  51. $this->setOptions($options);
  52. }
  53. }
  54. /**
  55. * Sets a validator for the class, erasing all previous set
  56. *
  57. * @param string|array $validator Validator to set
  58. * @param string|array $files Files to limit this validator to
  59. * @return Zend_File_Transfer_Adapter
  60. */
  61. public function setValidators(array $validators, $files = null)
  62. {
  63. $this->clearValidators();
  64. return $this->addValidators($validators, $files);
  65. }
  66. /**
  67. * Remove an individual validator
  68. *
  69. * @param string $name
  70. * @return Zend_File_Transfer_Adapter_Abstract
  71. */
  72. public function removeValidator($name)
  73. {
  74. if ($name == 'Upload') {
  75. return $this;
  76. }
  77. return parent::removeValidator($name);
  78. }
  79. /**
  80. * Remove an individual validator
  81. *
  82. * @param string $name
  83. * @return Zend_File_Transfer_Adapter_Abstract
  84. */
  85. public function clearValidators()
  86. {
  87. parent::clearValidators();
  88. $this->addValidator('Upload', false, $this->_files);
  89. return $this;
  90. }
  91. /**
  92. * Send the file to the client (Download)
  93. *
  94. * @param string|array $options Options for the file(s) to send
  95. * @return void
  96. * @throws Zend_File_Transfer_Exception Not implemented
  97. */
  98. public function send($options = null)
  99. {
  100. require_once 'Zend/File/Transfer/Exception.php';
  101. throw new Zend_File_Transfer_Exception('Method not implemented');
  102. }
  103. /**
  104. * Checks if the files are valid
  105. *
  106. * @param string|array $files (Optional) Files to check
  107. * @return boolean True if all checks are valid
  108. */
  109. public function isValid($files = null)
  110. {
  111. // Workaround for a PHP error returning empty $_FILES when form data exceeds php settings
  112. if (empty($this->_files) && ($_SERVER['CONTENT_LENGTH'] > 0)) {
  113. if (is_array($files)) {
  114. $files = current($files);
  115. }
  116. $temp = array($files => array(
  117. 'name' => $files,
  118. 'error' => 1));
  119. $validator = $this->_validators['Zend_Validate_File_Upload'];
  120. $validator->setFiles($temp)
  121. ->isValid($files, null);
  122. $this->_messages += $validator->getMessages();
  123. return false;
  124. }
  125. return parent::isValid($files);
  126. }
  127. /**
  128. * Receive the file from the client (Upload)
  129. *
  130. * @param string|array $files (Optional) Files to receive
  131. * @return bool
  132. */
  133. public function receive($files = null)
  134. {
  135. if (!$this->isValid($files)) {
  136. return false;
  137. }
  138. $check = $this->_getFiles($files);
  139. foreach ($check as $file => $content) {
  140. if (!$content['received']) {
  141. $directory = '';
  142. $destination = $this->getDestination($file);
  143. if ($destination !== null) {
  144. $directory = $destination . DIRECTORY_SEPARATOR;
  145. }
  146. $filename = $directory . $content['name'];
  147. $rename = $this->getFilter('Rename');
  148. if ($rename !== null) {
  149. $tmp = $rename->getNewName($content['tmp_name']);
  150. if ($tmp != $content['tmp_name']) {
  151. $filename = $tmp;
  152. }
  153. if (dirname($filename) == '.') {
  154. $filename = $directory . $filename;
  155. }
  156. $key = array_search(get_class($rename), $this->_files[$file]['filters']);
  157. unset($this->_files[$file]['filters'][$key]);
  158. }
  159. // Should never return false when it's tested by the upload validator
  160. if (!move_uploaded_file($content['tmp_name'], $filename)) {
  161. if ($content['options']['ignoreNoFile']) {
  162. $this->_files[$file]['received'] = true;
  163. $this->_files[$file]['filtered'] = true;
  164. continue;
  165. }
  166. $this->_files[$file]['received'] = false;
  167. return false;
  168. }
  169. if ($rename !== null) {
  170. $this->_files[$file]['destination'] = dirname($filename);
  171. $this->_files[$file]['name'] = basename($filename);
  172. }
  173. $this->_files[$file]['tmp_name'] = $filename;
  174. $this->_files[$file]['received'] = true;
  175. }
  176. if (!$content['filtered']) {
  177. if (!$this->_filter($file)) {
  178. $this->_files[$file]['filtered'] = false;
  179. return false;
  180. }
  181. $this->_files[$file]['filtered'] = true;
  182. }
  183. }
  184. return true;
  185. }
  186. /**
  187. * Checks if the file was already sent
  188. *
  189. * @param string|array $file Files to check
  190. * @return bool
  191. * @throws Zend_File_Transfer_Exception Not implemented
  192. */
  193. public function isSent($files = null)
  194. {
  195. require_once 'Zend/File/Transfer/Exception.php';
  196. throw new Zend_File_Transfer_Exception('Method not implemented');
  197. }
  198. /**
  199. * Checks if the file was already received
  200. *
  201. * @param string|array $files (Optional) Files to check
  202. * @return bool
  203. */
  204. public function isReceived($files = null)
  205. {
  206. $files = $this->_getFiles($files, false, true);
  207. if (empty($files)) {
  208. return false;
  209. }
  210. foreach ($files as $content) {
  211. if ($content['received'] !== true) {
  212. return false;
  213. }
  214. }
  215. return true;
  216. }
  217. /**
  218. * Checks if the file was already filtered
  219. *
  220. * @param string|array $files (Optional) Files to check
  221. * @return bool
  222. */
  223. public function isFiltered($files = null)
  224. {
  225. $files = $this->_getFiles($files, false, true);
  226. if (empty($files)) {
  227. return false;
  228. }
  229. foreach ($files as $content) {
  230. if ($content['filtered'] !== true) {
  231. return false;
  232. }
  233. }
  234. return true;
  235. }
  236. /**
  237. * Has a file been uploaded ?
  238. *
  239. * @param array|string|null $file
  240. * @return bool
  241. */
  242. public function isUploaded($files = null)
  243. {
  244. $files = $this->_getFiles($files, false, true);
  245. if (empty($files)) {
  246. return false;
  247. }
  248. foreach ($files as $file) {
  249. if (empty($file['name'])) {
  250. return false;
  251. }
  252. }
  253. return true;
  254. }
  255. /**
  256. * Returns the actual progress of file up-/downloads
  257. *
  258. * @param string $id The upload to get the progress for
  259. * @return array|null
  260. */
  261. public static function getProgress($id = null)
  262. {
  263. if (!function_exists('apc_fetch') and !function_exists('uploadprogress_get_info')) {
  264. require_once 'Zend/File/Transfer/Exception.php';
  265. throw new Zend_File_Transfer_Exception('Wether APC nor uploadprogress extension installed');
  266. }
  267. $session = 'Zend_File_Transfer_Adapter_Http_ProgressBar';
  268. $status = array(
  269. 'total' => 0,
  270. 'current' => 0,
  271. 'rate' => 0,
  272. 'message' => '',
  273. 'done' => false
  274. );
  275. if (is_array($id)) {
  276. if (isset($id['progress'])) {
  277. $adapter = $id['progress'];
  278. }
  279. if (isset($id['session'])) {
  280. $session = $id['session'];
  281. }
  282. if (isset($id['id'])) {
  283. $id = $id['id'];
  284. } else {
  285. unset($id);
  286. }
  287. }
  288. if (!empty($id) && (($id instanceof Zend_ProgressBar_Adapter) || ($id instanceof Zend_ProgressBar))) {
  289. $adapter = $id;
  290. unset($id);
  291. }
  292. if (empty($id)) {
  293. if (!isset($_GET['progress_key'])) {
  294. $status['message'] = 'No upload in progress';
  295. $status['done'] = true;
  296. } else {
  297. $id = $_GET['progress_key'];
  298. }
  299. }
  300. if (!empty($id)) {
  301. if (self::isApcAvailable()) {
  302. $call = call_user_func(self::$_callbackApc, 'upload_' . $id);
  303. if (is_array($call)) {
  304. $status = $call + $status;
  305. }
  306. } else if (self::isUploadProgressAvailable()) {
  307. $call = call_user_func(self::$_callbackUploadProgress, $id);
  308. if (is_array($call)) {
  309. $status = $call + $status;
  310. $status['total'] = $status['bytes_total'];
  311. $status['current'] = $status['bytes_uploaded'];
  312. $status['rate'] = $status['speed_average'];
  313. if ($status['total'] == $status['current']) {
  314. $status['done'] = true;
  315. }
  316. }
  317. }
  318. if (!is_array($call)) {
  319. $status['done'] = true;
  320. $status['message'] = 'Failure while retrieving the upload progress';
  321. } else if (!empty($status['cancel_upload'])) {
  322. $status['done'] = true;
  323. $status['message'] = 'The upload has been canceled';
  324. } else {
  325. $status['message'] = self::_toByteString($status['current']) . " - " . self::_toByteString($status['total']);
  326. }
  327. $status['id'] = $id;
  328. }
  329. if (isset($adapter) && isset($status['id'])) {
  330. if ($adapter instanceof Zend_ProgressBar_Adapter) {
  331. require_once 'Zend/ProgressBar.php';
  332. $adapter = new Zend_ProgressBar($adapter, 0, $status['total'], $session);
  333. }
  334. if (!($adapter instanceof Zend_ProgressBar)) {
  335. require_once 'Zend/File/Transfer/Exception.php';
  336. throw new Zend_File_Transfer_Exception('Unknown Adapter given');
  337. }
  338. if ($status['done']) {
  339. $adapter->finish();
  340. } else {
  341. $adapter->update($status['current'], $status['message']);
  342. }
  343. $status['progress'] = $adapter;
  344. }
  345. return $status;
  346. }
  347. /**
  348. * Checks the APC extension for progress information
  349. *
  350. * @return boolean
  351. */
  352. public static function isApcAvailable()
  353. {
  354. return (bool) ini_get('apc.enabled') && (bool) ini_get('apc.rfc1867') && is_callable(self::$_callbackApc);
  355. }
  356. /**
  357. * Checks the UploadProgress extension for progress information
  358. *
  359. * @return boolean
  360. */
  361. public static function isUploadProgressAvailable()
  362. {
  363. return is_callable(self::$_callbackUploadProgress);
  364. }
  365. /**
  366. * Prepare the $_FILES array to match the internal syntax of one file per entry
  367. *
  368. * @param array $files
  369. * @return array
  370. */
  371. protected function _prepareFiles(array $files = array())
  372. {
  373. $result = array();
  374. foreach ($files as $form => $content) {
  375. if (is_array($content['name'])) {
  376. foreach ($content as $param => $file) {
  377. foreach ($file as $number => $target) {
  378. $result[$form . '_' . $number . '_'][$param] = $target;
  379. $result[$form . '_' . $number . '_']['options'] = $this->_options;
  380. $result[$form . '_' . $number . '_']['validated'] = false;
  381. $result[$form . '_' . $number . '_']['received'] = false;
  382. $result[$form . '_' . $number . '_']['filtered'] = false;
  383. $result[$form]['multifiles'][$number] = $form . '_' . $number . '_';
  384. $result[$form]['name'] = $form;
  385. }
  386. }
  387. } else {
  388. $result[$form] = $content;
  389. $result[$form]['options'] = $this->_options;
  390. $result[$form]['validated'] = false;
  391. $result[$form]['received'] = false;
  392. $result[$form]['filtered'] = false;
  393. }
  394. }
  395. return $result;
  396. }
  397. }