2
0

Http.php 14 KB

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