PageRenderTime 54ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://bitbucket.org/babanesma/mysimpleadmin
PHP | 483 lines | 297 code | 63 blank | 123 comment | 65 complexity | 91ca12c343f7a595d7184400deb84e37 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-2011 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 23775 2011-03-01 17:25:24Z ralph $
  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-2011 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. $files = current($files);
  120. }
  121. $temp = array($files => array(
  122. 'name' => $files,
  123. 'error' => 1));
  124. $validator = $this->_validators['Zend_Validate_File_Upload'];
  125. $validator->setFiles($temp)
  126. ->isValid($files, null);
  127. $this->_messages += $validator->getMessages();
  128. return false;
  129. }
  130. return parent::isValid($files);
  131. }
  132. /**
  133. * Receive the file from the client (Upload)
  134. *
  135. * @param string|array $files (Optional) Files to receive
  136. * @return bool
  137. */
  138. public function receive($files = null)
  139. {
  140. if (!$this->isValid($files)) {
  141. return false;
  142. }
  143. $check = $this->_getFiles($files);
  144. foreach ($check as $file => $content) {
  145. if (!$content['received']) {
  146. $directory = '';
  147. $destination = $this->getDestination($file);
  148. if ($destination !== null) {
  149. $directory = $destination . DIRECTORY_SEPARATOR;
  150. }
  151. $filename = $directory . $content['name'];
  152. $rename = $this->getFilter('Rename');
  153. if ($rename !== null) {
  154. $tmp = $rename->getNewName($content['tmp_name']);
  155. if ($tmp != $content['tmp_name']) {
  156. $filename = $tmp;
  157. }
  158. if (dirname($filename) == '.') {
  159. $filename = $directory . $filename;
  160. }
  161. $key = array_search(get_class($rename), $this->_files[$file]['filters']);
  162. unset($this->_files[$file]['filters'][$key]);
  163. }
  164. // Should never return false when it's tested by the upload validator
  165. if (!move_uploaded_file($content['tmp_name'], $filename)) {
  166. if ($content['options']['ignoreNoFile']) {
  167. $this->_files[$file]['received'] = true;
  168. $this->_files[$file]['filtered'] = true;
  169. continue;
  170. }
  171. $this->_files[$file]['received'] = false;
  172. return false;
  173. }
  174. if ($rename !== null) {
  175. $this->_files[$file]['destination'] = dirname($filename);
  176. $this->_files[$file]['name'] = basename($filename);
  177. }
  178. $this->_files[$file]['tmp_name'] = $filename;
  179. $this->_files[$file]['received'] = true;
  180. }
  181. if (!$content['filtered']) {
  182. if (!$this->_filter($file)) {
  183. $this->_files[$file]['filtered'] = false;
  184. return false;
  185. }
  186. $this->_files[$file]['filtered'] = true;
  187. }
  188. }
  189. return true;
  190. }
  191. /**
  192. * Checks if the file was already sent
  193. *
  194. * @param string|array $file Files to check
  195. * @return bool
  196. * @throws Zend_File_Transfer_Exception Not implemented
  197. */
  198. public function isSent($files = null)
  199. {
  200. require_once 'Zend/File/Transfer/Exception.php';
  201. throw new Zend_File_Transfer_Exception('Method not implemented');
  202. }
  203. /**
  204. * Checks if the file was already received
  205. *
  206. * @param string|array $files (Optional) Files to check
  207. * @return bool
  208. */
  209. public function isReceived($files = null)
  210. {
  211. $files = $this->_getFiles($files, false, true);
  212. if (empty($files)) {
  213. return false;
  214. }
  215. foreach ($files as $content) {
  216. if ($content['received'] !== true) {
  217. return false;
  218. }
  219. }
  220. return true;
  221. }
  222. /**
  223. * Checks if the file was already filtered
  224. *
  225. * @param string|array $files (Optional) Files to check
  226. * @return bool
  227. */
  228. public function isFiltered($files = null)
  229. {
  230. $files = $this->_getFiles($files, false, true);
  231. if (empty($files)) {
  232. return false;
  233. }
  234. foreach ($files as $content) {
  235. if ($content['filtered'] !== true) {
  236. return false;
  237. }
  238. }
  239. return true;
  240. }
  241. /**
  242. * Has a file been uploaded ?
  243. *
  244. * @param array|string|null $file
  245. * @return bool
  246. */
  247. public function isUploaded($files = null)
  248. {
  249. $files = $this->_getFiles($files, false, true);
  250. if (empty($files)) {
  251. return false;
  252. }
  253. foreach ($files as $file) {
  254. if (empty($file['name'])) {
  255. return false;
  256. }
  257. }
  258. return true;
  259. }
  260. /**
  261. * Returns the actual progress of file up-/downloads
  262. *
  263. * @param string $id The upload to get the progress for
  264. * @return array|null
  265. */
  266. public static function getProgress($id = null)
  267. {
  268. if (!function_exists('apc_fetch') and !function_exists('uploadprogress_get_info')) {
  269. require_once 'Zend/File/Transfer/Exception.php';
  270. throw new Zend_File_Transfer_Exception('Neither APC nor uploadprogress extension installed');
  271. }
  272. $session = 'Zend_File_Transfer_Adapter_Http_ProgressBar';
  273. $status = array(
  274. 'total' => 0,
  275. 'current' => 0,
  276. 'rate' => 0,
  277. 'message' => '',
  278. 'done' => false
  279. );
  280. if (is_array($id)) {
  281. if (isset($id['progress'])) {
  282. $adapter = $id['progress'];
  283. }
  284. if (isset($id['session'])) {
  285. $session = $id['session'];
  286. }
  287. if (isset($id['id'])) {
  288. $id = $id['id'];
  289. } else {
  290. unset($id);
  291. }
  292. }
  293. if (!empty($id) && (($id instanceof Zend_ProgressBar_Adapter) || ($id instanceof Zend_ProgressBar))) {
  294. $adapter = $id;
  295. unset($id);
  296. }
  297. if (empty($id)) {
  298. if (!isset($_GET['progress_key'])) {
  299. $status['message'] = 'No upload in progress';
  300. $status['done'] = true;
  301. } else {
  302. $id = $_GET['progress_key'];
  303. }
  304. }
  305. if (!empty($id)) {
  306. if (self::isApcAvailable()) {
  307. $call = call_user_func(self::$_callbackApc, ini_get('apc.rfc1867_prefix') . $id);
  308. if (is_array($call)) {
  309. $status = $call + $status;
  310. }
  311. } else if (self::isUploadProgressAvailable()) {
  312. $call = call_user_func(self::$_callbackUploadProgress, $id);
  313. if (is_array($call)) {
  314. $status = $call + $status;
  315. $status['total'] = $status['bytes_total'];
  316. $status['current'] = $status['bytes_uploaded'];
  317. $status['rate'] = $status['speed_average'];
  318. if ($status['total'] == $status['current']) {
  319. $status['done'] = true;
  320. }
  321. }
  322. }
  323. if (!is_array($call)) {
  324. $status['done'] = true;
  325. $status['message'] = 'Failure while retrieving the upload progress';
  326. } else if (!empty($status['cancel_upload'])) {
  327. $status['done'] = true;
  328. $status['message'] = 'The upload has been canceled';
  329. } else {
  330. $status['message'] = self::_toByteString($status['current']) . " - " . self::_toByteString($status['total']);
  331. }
  332. $status['id'] = $id;
  333. }
  334. if (isset($adapter) && isset($status['id'])) {
  335. if ($adapter instanceof Zend_ProgressBar_Adapter) {
  336. require_once 'Zend/ProgressBar.php';
  337. $adapter = new Zend_ProgressBar($adapter, 0, $status['total'], $session);
  338. }
  339. if (!($adapter instanceof Zend_ProgressBar)) {
  340. require_once 'Zend/File/Transfer/Exception.php';
  341. throw new Zend_File_Transfer_Exception('Unknown Adapter given');
  342. }
  343. if ($status['done']) {
  344. $adapter->finish();
  345. } else {
  346. $adapter->update($status['current'], $status['message']);
  347. }
  348. $status['progress'] = $adapter;
  349. }
  350. return $status;
  351. }
  352. /**
  353. * Checks the APC extension for progress information
  354. *
  355. * @return boolean
  356. */
  357. public static function isApcAvailable()
  358. {
  359. return (bool) ini_get('apc.enabled') && (bool) ini_get('apc.rfc1867') && is_callable(self::$_callbackApc);
  360. }
  361. /**
  362. * Checks the UploadProgress extension for progress information
  363. *
  364. * @return boolean
  365. */
  366. public static function isUploadProgressAvailable()
  367. {
  368. return is_callable(self::$_callbackUploadProgress);
  369. }
  370. /**
  371. * Prepare the $_FILES array to match the internal syntax of one file per entry
  372. *
  373. * @param array $files
  374. * @return array
  375. */
  376. protected function _prepareFiles()
  377. {
  378. $this->_files = array();
  379. foreach ($_FILES as $form => $content) {
  380. if (is_array($content['name'])) {
  381. foreach ($content as $param => $file) {
  382. foreach ($file as $number => $target) {
  383. $this->_files[$form . '_' . $number . '_'][$param] = $target;
  384. $this->_files[$form]['multifiles'][$number] = $form . '_' . $number . '_';
  385. }
  386. }
  387. $this->_files[$form]['name'] = $form;
  388. foreach($this->_files[$form]['multifiles'] as $key => $value) {
  389. $this->_files[$value]['options'] = $this->_options;
  390. $this->_files[$value]['validated'] = false;
  391. $this->_files[$value]['received'] = false;
  392. $this->_files[$value]['filtered'] = false;
  393. $mimetype = $this->_detectMimeType($this->_files[$value]);
  394. $this->_files[$value]['type'] = $mimetype;
  395. $filesize = $this->_detectFileSize($this->_files[$value]);
  396. $this->_files[$value]['size'] = $filesize;
  397. if ($this->_options['detectInfos']) {
  398. $_FILES[$form]['type'][$key] = $mimetype;
  399. $_FILES[$form]['size'][$key] = $filesize;
  400. }
  401. }
  402. } else {
  403. $this->_files[$form] = $content;
  404. $this->_files[$form]['options'] = $this->_options;
  405. $this->_files[$form]['validated'] = false;
  406. $this->_files[$form]['received'] = false;
  407. $this->_files[$form]['filtered'] = false;
  408. $mimetype = $this->_detectMimeType($this->_files[$form]);
  409. $this->_files[$form]['type'] = $mimetype;
  410. $filesize = $this->_detectFileSize($this->_files[$form]);
  411. $this->_files[$form]['size'] = $filesize;
  412. if ($this->_options['detectInfos']) {
  413. $_FILES[$form]['type'] = $mimetype;
  414. $_FILES[$form]['size'] = $filesize;
  415. }
  416. }
  417. }
  418. return $this;
  419. }
  420. }