getValidator('Upload')->setMessages($this->_getFileUploadMessages()); } public function setMessages(array $messages) { $this->_messages = $messages; return $this; } function getResize() { return $this->_resize; } // $spec values: // 1) 800x600 // 2) 800x600o function setResize($spec) { if (is_string($spec)) { $this->_resize = Qs_ImageFs::parse($spec); } else { throw new Qs_Form_Element_Exception('Invalid image size specified: ' . $spec); } return $this; } protected function _prepareFiles() { parent::_prepareFiles(); foreach ($this->_files as $name => &$file) { if (version_compare(phpversion(), '5.3.0', '<') && 1 == get_magic_quotes_gpc()) { $file['name'] = stripslashes($file['name']); } $options = (array) Qs_Request::getPostValue($name, array()); $file = array_merge($file, $options); } return $this; } public function getUploadedCount($files = null) { $count = 0; $files = $this->_getFiles($files, false, true); if (empty($files)) { return $count; } foreach ($files as $file) { if (!empty($file['name'])) { $count++; } } return $count; } protected function _getFileUploadMessages() { $messages = array( Zend_Validate_File_Upload::INI_SIZE => "The file exceeds the defined ini size", Zend_Validate_File_Upload::FORM_SIZE => "The file exceeds the defined form size", Zend_Validate_File_Upload::PARTIAL => "The file was only partially uploaded", Zend_Validate_File_Upload::NO_FILE => "The file was not uploaded", Zend_Validate_File_Upload::NO_TMP_DIR => "No temporary directory was found for the file", Zend_Validate_File_Upload::CANT_WRITE => "The file can't be written", Zend_Validate_File_Upload::EXTENSION => "The extension returned an error while uploading the file", Zend_Validate_File_Upload::ATTACK => "The file was illegal uploaded, possible attack", Zend_Validate_File_Upload::FILE_NOT_FOUND => "The file was not found", Zend_Validate_File_Upload::UNKNOWN => "Unknown error while uploading the file" ); return $messages; } public function getPluginLoader($type) { $type = strtoupper($type); switch ($type) { case self::FILTER: case self::VALIDATE: $prefixSegment = ucfirst(strtolower($type)); $pathSegment = $prefixSegment; if (!isset($this->_loaders[$type])) { $paths = array( 'Zend_' . $prefixSegment . '_' => 'Zend/' . $pathSegment . '/', 'Zend_' . $prefixSegment . '_File' => 'Zend/' . $pathSegment . '/File', 'Qs_' . $prefixSegment . '_' => 'Qs/' . $pathSegment . '/', 'Qs_' . $prefixSegment . '_File' => 'Qs/' . $pathSegment . '/File', ); $this->_loaders[$type] = new Zend_Loader_PluginLoader($paths); } else { $loader = $this->_loaders[$type]; $prefix = 'Zend_' . $prefixSegment . '_File_'; if (!$loader->getPaths($prefix)) { $loader->addPrefixPath($prefix, str_replace('_', '/', $prefix)); } $prefix = 'Qs_' . $prefixSegment . '_File_'; if (!$loader->getPaths($prefix)) { $loader->addPrefixPath($prefix, str_replace('_', '/', $prefix)); } } return $this->_loaders[$type]; default: // require_once 'Zend/File/Transfer/Exception.php'; throw new Zend_File_Transfer_Exception(sprintf('Invalid type "%s" provided to getPluginLoader()', $type)); } } // перевизначення Zend_File_Transfer_Adapter_Http::isValid() // поправив тільки виклик Zend_File_Transfer_Adapter_Abstract::isValid(), зараз викликається наш метод (_isValidAbstract) 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']; } else if (!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 = array($files => array( 'name' => $files, 'error' => 1)); $validator = array_key_exists('Qs_Validate_File_Upload', $this->_validators) ? $this->_validators['Qs_Validate_File_Upload'] : $this->_validators['Zend_Validate_File_Upload']; $validator->setFiles($temp) ->isValid($files, null); $this->_messages += $validator->getMessages(); return false; } return $this->_isValidAbstract($files); } // перевизначення Zend_File_Transfer_Adapter_Abstract::isValid() protected function _isValidAbstract($files = null) { $check = $this->_getFiles($files, false, true); if (empty($check)) { return false; } $translator = $this->getTranslator(); $this->_messages = array(); $break = false; foreach($check as $key => $content) { if (array_key_exists('validators', $content) && in_array('Zend_Validate_File_Count', $content['validators'])) { $validator = $this->_validators['Zend_Validate_File_Count']; if (array_key_exists('destination', $content)) { $checkit = $content['destination']; } else { $checkit = dirname($content['tmp_name']); } $checkit .= DIRECTORY_SEPARATOR . $content['name']; $validator->addFile($checkit); $count = $content; } } if (isset($count)) { if (!$validator->isValid($count['tmp_name'], $count)) { $this->_messages += $validator->getMessages(); } } foreach ($check as $key => $content) { $fileerrors = array(); if (array_key_exists('validators', $content) && $content['validated']) { continue; } if (array_key_exists('validators', $content)) { foreach ($content['validators'] as $class) { $validator = $this->_validators[$class]; if (method_exists($validator, 'setTranslator')) { $validator->setTranslator($translator); } if ((substr($class, -12) === '_File_Upload') and (empty($content['tmp_name']))) { $tocheck = $key; } else { $tocheck = $content['tmp_name']; } if (!$validator->isValid($tocheck, $content)) { $fileerrors += $validator->getMessages(); } if (!empty($content['options']['ignoreNoFile']) and (isset($fileerrors['fileUploadErrorNoFile']))) { unset($fileerrors['fileUploadErrorNoFile']); break; } if ((substr($class, -12) === '_File_Upload') and (count($fileerrors) > 0)) { break; } if (($this->_break[$class]) and (count($fileerrors) > 0)) { $break = true; break; } } } if (count($fileerrors) > 0) { $this->_files[$key]['validated'] = false; } else { $this->_files[$key]['validated'] = true; } $this->_messages += $fileerrors; if ($break) { break; } } if (count($this->_messages) > 0) { return false; } return true; } /** * Send the file to the client (Download) * * @param string|array $options Options for the file(s) to send * @return void * @throws Zend_File_Transfer_Exception Not implemented */ public function send($options = null) { if (!is_string($options)) { throw new Zend_File_Transfer_Exception('Method not implemented'); } $file = $options; header('Content-Disposition: attachment; filename="' . basename($file) . '"'); header('Content-Type: application/force-download'); header('Content-Transfer-Encoding: binary'); header('Content-Length: ' . filesize($file)); if (($fh = fopen($file, 'rb'))) { while(!feof($fh)) { print(fread($fh, 1024 * 8)); flush(); ob_flush(); if (connection_status() != 0) { break; } } fclose($fh); } } }