{'_table' . $name}) { $this->{'_table' . $name} = new Qs_Db_Table(array( 'name' => $this->_getTableName($name), 'db' => Qs_Db::getInstance() )); } return $this->{'_table'.$name}; } public function get($name) { $select = Qs_Db::getInstance()->select(); $select->from($this->_getTableName()); $select->where('`nameFs` = ' . Qs_Db::getInstance()->quote($name)); $select->limit(1); return Qs_Db::getInstance()->fetchRow($select); } protected function _getTableName($name = null) { if (null === $name) { $name = $this->_tableAlias; $propertyName = '_tableName'; } else { $propertyName = '_tableName' . $name; } if (!$this->{$propertyName}) { $this->{$propertyName} = Qs_Db::getTableName($name); } return $this->{$propertyName}; } public function insert($data) { $id = $this->table()->insert($data); return $id; } public function update($data, $id) { return $this->table()->updateByKey($data, array('id' => $id)); } public function delete($file, $name = null) { if (!empty($file)) { Qs_ImageFs::delete($this->getDestination($name) . '/' . $file); $this->table()->delete('nameFs = ' . $this->table()->getAdapter()->quote($file)); } return $this; } public function deleteFromDefaultDestination($file) { if (!empty($file)) { Qs_ImageFs::delete($this->getDefaultDestination() . '/' . $file); $this->table()->delete('nameFs = ' . $this->table()->getAdapter()->quote($file)); } return $this; } public function getDefaultDestination() { if (null === $this->_defaultDestination) { $this->_defaultDestination = WWW_PATH . '/' . Qs_ImageFs::WEB_PATH; } return $this->_defaultDestination; } public function getDestination($files = null) { $files = $this->_getFiles($files, false); $destinations = array(); foreach (array_keys($files) as $key) { if (isset($this->_files[$key]['destination'])) { $destinations[$key] = $this->_files[$key]['destination']; } else { $this->setDestination($this->getDefaultDestination(), $key); $destinations[$key] = $this->getDefaultDestination(); } } if (empty($destinations)) { return $this->getDefaultDestination(); } else if (count($destinations) == 1) { $destinations = current($destinations); } return $destinations; } public function duplicateFile($nameFs) { $fullName = Qs_FileFs::getFileFullName($nameFs); if (!($file = $this->get($nameFs))) { return false; } $tmpFile = constant('BASE_PATH') . '/tmp/tmpi-' . uniqid() . $nameFs; copy($fullName, $tmpFile); return $this->insertFile($tmpFile, $file['name']); } public function insertFile($file, $title = null) { if (!file_exists($file)) { return false; } $content = array( 'tmp_name' => $file, 'name' => (null == $title) ? basename($file) : $title, 'size' => filesize($file), ); $content['type'] = $this->getFileType($content); $id = $this->insert($content); $nameFs = $this->_getFileName($content['name'], $id); $this->update(array('nameFs' => $nameFs), $id); rename($file, WWW_PATH . '/' . Qs_ImageFs::WEB_PATH . '/' . $nameFs); return $nameFs; } 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; } // Should never return false when it's tested by the upload validator $nameFs = null; if ($content['error'] == UPLOAD_ERR_OK) { $content['type'] = $this->getFileType($content); $id = $this->insert($content); $nameFs = $this->prepareFileNameFS($file, $id); $this->update(array('nameFs' => $nameFs), $id); } if (!$nameFs || !move_uploaded_file($content['tmp_name'], $directory . $nameFs)) { if ($content['options']['ignoreNoFile']) { $this->_files[$file]['received'] = true; $this->_files[$file]['filtered'] = true; continue; } $this->_files[$file]['received'] = false; return false; } if (!empty($this->_resize) && isset($content['resize']) && $content['resize'] == 'y') { $file = $directory . $nameFs; $temp = BASE_PATH . '/tmp/' . $nameFs; rename($file, $temp); $options = $this->_resize; $options['destination'] = $file; $options['force'] = true; $options['symlink'] = false; Qs_ImageFs::resize($temp, $options); unlink($temp); } $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; } public function getFile() { throw new Zend_File_Transfer_Exception('Method not implemented'); } public function getFileEx($name) { if (isset($this->_files[$name])) { return $this->_files[$name]; } throw new Zend_File_Transfer_Exception('File not found: ' . $name); } public function _getFileName($name, $id) { $name = preg_replace('/[^(-_a-zA-Z0-9\.\!\@\#\$\%\^\&\*\(\)\+\=\~;\[\]\{\},)]+/', '_', $name); if (false !== ($dotPos = strrpos($name, '.'))) { return substr($name, 0, $dotPos) . '(' . intval($id) . ')' . substr($name, $dotPos); } else { return $name . '(' . intval($id) . ')'; } } public function prepareFileNameFS($file, $id) { if (null === ($content = $this->getFileEx($file))) { return null; } if (isset($content['nameFs'])) { return $content['nameFs']; } $this->_files[$file]['nameFs'] = $this->_getFileName($content['name'], $id); return $this->_files[$file]['nameFs']; } public function getFileName($file = null, $path = true) { $files = $this->_getFiles($file, true, true); $result = array(); $directory = ""; foreach($files as $file) { if (empty($this->_files[$file]['nameFs'])) { continue; } if ($path === true) { $directory = $this->getDestination($file) . DIRECTORY_SEPARATOR; } $result[$file] = $directory . $this->_files[$file]['nameFs']; } if (count($result) == 1) { return current($result); } return $result; } protected function _getFiles($files, $names = false, $noexception = true) { $files = parent::_getFiles($files, $names, $noexception); if (!empty($files)) { foreach ($this->_files as $name => $file) { if (empty($file['multifiles'])) { continue; } $firstName = reset($file['multifiles']); if (true === $this->_files[$firstName]['options']['ignoreNoFile']) { continue; } $count = count($file['multifiles']); foreach ($file['multifiles'] as $itemName) { if (UPLOAD_ERR_NO_FILE == $this->_files[$itemName]['error']) { if (--$count == 0) { continue; } $this->_files[$itemName]['options']['ignoreNoFile'] = true; } } } } return $files; } protected function getFileType($content) { $defaultType = 'application/octet-stream'; if (false === ($size = getimagesize($content['tmp_name']))) { if (false !== ($dotPos = strrpos($content['name'], '.'))) { $extension = substr($content['name'], $dotPos+1); if (empty($extension)) { return $defaultType; } $rows = $this->table('DFileType')->find($extension); if (!count($rows)) { return $defaultType; } return $rows->current()->type; } else { return $defaultType; } } return $size['mime']; } public function addError($message) { $this->_messages[] = $message; //$this->markAsError(); return $this; } }