![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/old/vendor/laminas/laminas-file/src/Transfer/Adapter/ |
<?php namespace Laminas\File\Transfer\Adapter; use Laminas\File\Transfer; use Laminas\File\Transfer\Exception; use Laminas\ProgressBar; use Laminas\ProgressBar\Adapter; use Laminas\Validator; /** * File transfer adapter class for the HTTP protocol * * @deprecated since 2.7.0, and scheduled for removal with 3.0.0 */ class Http extends AbstractAdapter { protected static $callbackApc = 'apc_fetch'; protected static $callbackUploadProgress = 'uploadprogress_get_info'; /** * Constructor for Http File Transfers * * @param array $options OPTIONAL Options to set * @throws Exception\PhpEnvironmentException if file uploads are not allowed */ public function __construct($options = []) { if (ini_get('file_uploads') == false) { throw new Exception\PhpEnvironmentException('File uploads are not allowed in your php config!'); } $this->setOptions($options); $this->prepareFiles(); $this->addValidator('Upload', false, $this->files); } /** * Sets a validator for the class, erasing all previous set * * @param array $validators Validator to set * @param string|array $files Files to limit this validator to * @return AbstractAdapter */ public function setValidators(array $validators, $files = null) { $this->clearValidators(); return $this->addValidators($validators, $files); } /** * Remove an individual validator * * @param string $name * @return AbstractAdapter */ public function removeValidator($name) { if ($name == 'Upload') { return $this; } return parent::removeValidator($name); } /** * Clear the validators * * @return AbstractAdapter */ public function clearValidators() { parent::clearValidators(); $this->addValidator('Upload', false, $this->files); return $this; } /** * Send the file to the client (Download) * * @param string|array $options Options for the file(s) to send * @return void * @throws Exception\BadMethodCallException Not implemented */ public function send($options = null) { throw new Exception\BadMethodCallException('Method not implemented'); } /** * Checks if the files are valid * * @param string|array $files (Optional) Files to check * @return bool True if all checks are valid */ public function isValid($files = null) { // Workaround for WebServer not conforming HTTP and omitting CONTENT_LENGTH $content = 0; if (isset($_SERVER['CONTENT_LENGTH'])) { $content = $_SERVER['CONTENT_LENGTH']; } elseif (! empty($_POST)) { $content = serialize($_POST); } // Workaround for a PHP error returning empty $_FILES when form data exceeds php settings if (empty($this->files) && ($content > 0)) { if (is_array($files)) { $files = current($files); } $temp = [$files => [ 'name' => $files, 'error' => 1]]; $validator = $this->validators[Validator\File\Upload::class]; $validator->setTranslator($this->getTranslator()) ->setFiles($temp) ->isValid($files, null); $this->messages += $validator->getMessages(); return false; } return parent::isValid($files); } /** * Receive the file from the client (Upload) * * @param string|array $files (Optional) Files to receive * @return bool */ public function receive($files = null) { if (! $this->isValid($files)) { return false; } $check = $this->getFiles($files); foreach ($check as $file => $content) { if (! $content['received']) { $directory = ''; $destination = $this->getDestination($file); if ($destination !== null) { $directory = $destination . DIRECTORY_SEPARATOR; } $filename = $directory . $content['name']; $rename = $this->getFilter('Rename'); if ($rename !== null) { $tmp = $rename->getNewName($content['tmp_name']); if ($tmp != $content['tmp_name']) { $filename = $tmp; } if (dirname($filename) == '.') { $filename = $directory . $filename; } $key = array_search(get_class($rename), $this->files[$file]['filters']); unset($this->files[$file]['filters'][$key]); } // Should never return false when it's tested by the upload validator if (! move_uploaded_file($content['tmp_name'], $filename)) { if ($content['options']['ignoreNoFile']) { $this->files[$file]['received'] = true; $this->files[$file]['filtered'] = true; continue; } $this->files[$file]['received'] = false; return false; } if ($rename !== null) { $this->files[$file]['destination'] = dirname($filename); $this->files[$file]['name'] = basename($filename); } $this->files[$file]['tmp_name'] = $filename; $this->files[$file]['received'] = true; } if (! $content['filtered']) { if (! $this->filter($file)) { $this->files[$file]['filtered'] = false; return false; } $this->files[$file]['filtered'] = true; } } return true; } /** * Checks if the file was already sent * * @param string|array $files Files to check * @return bool * @throws Exception\BadMethodCallException Not implemented */ public function isSent($files = null) { throw new Exception\BadMethodCallException('Method not implemented'); } /** * Checks if the file was already received * * @param string|array $files (Optional) Files to check * @return bool */ public function isReceived($files = null) { $files = $this->getFiles($files, false, true); if (empty($files)) { return false; } foreach ($files as $content) { if ($content['received'] !== true) { return false; } } return true; } /** * Checks if the file was already filtered * * @param string|array $files (Optional) Files to check * @return bool */ public function isFiltered($files = null) { $files = $this->getFiles($files, false, true); if (empty($files)) { return false; } foreach ($files as $content) { if ($content['filtered'] !== true) { return false; } } return true; } /** * Has a file been uploaded ? * * @param array|string|null $files * @return bool */ public function isUploaded($files = null) { $files = $this->getFiles($files, false, true); if (empty($files)) { return false; } foreach ($files as $file) { if (empty($file['name'])) { return false; } } return true; } /** * Returns the actual progress of file up-/downloads * * @param string|array $id The upload to get the progress for * @return array|null * @throws Exception\PhpEnvironmentException whether APC nor UploadProgress extension installed * @throws Exception\RuntimeException */ public static function getProgress($id = null) { if (! self::isApcAvailable() && ! self::isUploadProgressAvailable()) { throw new Exception\PhpEnvironmentException('Neither APC nor UploadProgress extension installed'); } $session = 'Laminas\File\Transfer\Adapter\Http\ProgressBar'; $status = [ 'total' => 0, 'current' => 0, 'rate' => 0, 'message' => '', 'done' => false ]; if (is_array($id)) { if (isset($id['progress'])) { $adapter = $id['progress']; } if (isset($id['session'])) { $session = $id['session']; } if (isset($id['id'])) { $id = $id['id']; } else { unset($id); } } if (! empty($id) && (($id instanceof Adapter\AbstractAdapter) || ($id instanceof ProgressBar\ProgressBar))) { $adapter = $id; unset($id); } if (empty($id)) { if (! isset($_GET['progress_key'])) { $status['message'] = 'No upload in progress'; $status['done'] = true; } else { $id = $_GET['progress_key']; } } if (! empty($id)) { if (self::isApcAvailable()) { $call = call_user_func(static::$callbackApc, ini_get('apc.rfc1867_prefix') . $id); if (is_array($call)) { $status = $call + $status; } } elseif (self::isUploadProgressAvailable()) { $call = call_user_func(static::$callbackUploadProgress, $id); if (is_array($call)) { $status = $call + $status; $status['total'] = isset($status['bytes_total']) ? $status['bytes_total'] : $status['total']; $status['current'] = isset($status['bytes_uploaded']) ? $status['bytes_uploaded'] : $status['current']; $status['rate'] = isset($status['speed_average']) ? $status['speed_average'] : $status['rate']; if ($status['total'] == $status['current']) { $status['done'] = true; } } } if (! is_array($call)) { $status['done'] = true; $status['message'] = 'Failure while retrieving the upload progress'; } elseif (! empty($status['cancel_upload'])) { $status['done'] = true; $status['message'] = 'The upload has been canceled'; } else { $status['message'] = static::toByteString($status['current']) . " - " . static::toByteString($status['total']); } $status['id'] = $id; } if (isset($adapter) && isset($status['id'])) { if ($adapter instanceof Adapter\AbstractAdapter) { $adapter = new ProgressBar\ProgressBar($adapter, 0, $status['total'], $session); } if (! ($adapter instanceof ProgressBar\ProgressBar)) { throw new Exception\RuntimeException('Unknown Adapter given'); } if ($status['done']) { $adapter->finish(); } else { $adapter->update($status['current'], $status['message']); } $status['progress'] = $adapter; } return $status; } /** * Checks the APC extension for progress information * * @return bool */ public static function isApcAvailable() { return (bool) ini_get('apc.enabled') && (bool) ini_get('apc.rfc1867') && is_callable(static::$callbackApc); } /** * Checks the UploadProgress extension for progress information * * @return bool */ public static function isUploadProgressAvailable() { return is_callable(static::$callbackUploadProgress); } /** * Prepare the $_FILES array to match the internal syntax of one file per entry * * @return Http */ protected function prepareFiles() { $this->files = []; foreach ($_FILES as $form => $content) { if (is_array($content['name'])) { foreach ($content as $param => $file) { foreach ($file as $number => $target) { $this->files[$form . '_' . $number . '_'][$param] = $target; $this->files[$form]['multifiles'][$number] = $form . '_' . $number . '_'; } } $this->files[$form]['name'] = $form; foreach ($this->files[$form]['multifiles'] as $key => $value) { if ($this->files[$value]['tmp_name'] !== '') { $this->files[$value]['name'] = basename($this->files[$value]['tmp_name']) . '_' . $this->files[$value]['name']; } $this->files[$value]['options'] = $this->options; $this->files[$value]['validated'] = false; $this->files[$value]['received'] = false; $this->files[$value]['filtered'] = false; $mimetype = $this->detectMimeType($this->files[$value]); $this->files[$value]['type'] = $mimetype; $filesize = $this->detectFileSize($this->files[$value]); $this->files[$value]['size'] = $filesize; if ($this->options['detectInfos']) { $_FILES[$form]['type'][$key] = $mimetype; $_FILES[$form]['size'][$key] = $filesize; } } } else { $this->files[$form] = $content; if ($this->files[$form]['tmp_name'] !== '') { $this->files[$form]['name'] = basename($this->files[$form]['tmp_name']) . '_' . $this->files[$form]['name']; } $this->files[$form]['options'] = $this->options; $this->files[$form]['validated'] = false; $this->files[$form]['received'] = false; $this->files[$form]['filtered'] = false; $mimetype = $this->detectMimeType($this->files[$form]); $this->files[$form]['type'] = $mimetype; $filesize = $this->detectFileSize($this->files[$form]); $this->files[$form]['size'] = $filesize; if ($this->options['detectInfos']) { $_FILES[$form]['type'] = $mimetype; $_FILES[$form]['size'] = $filesize; } } } return $this; } }