typeStoring = $typeStoring; require_once 'class/DB/S_db2.php'; $this->db = S_db2::getInstance(); $this->fileTable = DB_PREFIX_SERVICE.'File'; $this->fileDataTable = DB_PREFIX_SERVICE.'FileData'; $this->fileOptTable = DB_PREFIX_SERVICE.'FileOpt'; } public static function exists($fileName) { return file_exists(FILE_DB_FS_PATH . '/' . $fileName); } function _getUniqueFileName ($title) { $fileInfoArr = pathinfo($title); $ext = $fileInfoArr['extension']; switch ($this->typeNameGener) { case FILE_DB_NAME_GENER_TIME_RAND: srand ((double) microtime() * 1000000); do { $name = time()."_".mt_rand(1, mt_getrandmax()); $fileName = $name.".".$ext; } while ( $this->nameExists($fileName) ); break; case FILE_DB_NAME_GENER_FILE_NAME_NUM: $fileName = substr($fileInfoArr['basename'], 0, -1 *(1 + strlen($fileInfoArr['extension']) ) ) ; $fileName = $fileName .'(' ; $table = 'tbl'.$this->fileTable; $table = $this->db->$table; $sql = "SELECT MAX(id) FROM $table"; $fileName .= ($this->db->queryOne($sql) + 1); $fileName .= ').'.$ext; break; } return $fileName; } function nameExists($name) { $table = 'tbl'.$this->fileTable; $table = $this->db->$table; $sql = "SELECT COUNT(*) FROM $table WHERE name = BINARY '$name'"; return $this->db->queryOne($sql); } function getIdByName($name) { $tableName = 'tbl' . $this->fileTable; $table = $this->db->$tableName; $sql = 'SELECT `id` ' . 'FROM `' . $table . '` ' . 'WHERE `name` = ' . $this->db->quote($name, 'text') . ' ' . 'LIMIT 1'; $res = $this->db->queryOne($sql); return null === $res ? false : $res; } function handleUpload($fileArr, $oldFile = null) { if (is_file($fileArr['tmp_name'])) { //if (is_uploaded_file($fileArr['tmp_name'])){ $newName = BASE_PATH.'/tmp/'.basename($fileArr['tmp_name']); rename($fileArr['tmp_name'], $newName); //move_uploaded_file($fileArr['tmp_name'], $newName); $fileArr['tmp_name'] = $newName; } if ( !(file_exists($fileArr['tmp_name']) && is_file( $fileArr['tmp_name'] )) ) { return $oldFile; } $file = $this->insert($fileArr['name'], $fileArr['tmp_name'], $fileArr['type'] ); unlink( $fileArr['tmp_name'] ); if ( !empty($oldFile) ){ $this->delete($oldFile); } return $file; } function insert($title, $file, $type = '', $name = '') { if ( !file_exists($file) || !is_file($file) ) { return false; } $type = $this->_validType($type); $title = $this->_validTitle($title); if (empty($name)) { $name = $this->_getUniqueFileName($title); } else { $this->delete($name); } $this->_initTables(); $fileArr = array( 'name' => $name, 'title' => $title, 'type' => $type, 'size' => filesize($file), ); $id_file = $this->save($fileArr); $this->_putData($file, $id_file); return $name; } function save($row) { if (false === ($id_file = $this->getIdByName($row['name']))) { return $this->tblFile->insert($row); } return $id_file; } function _initTables() { if (!is_object($this->tblFile)) { require_once('class/DB/DBTable.php'); $this->tblFile = new DBTable($this->fileTable); $this->tblFileData = new DBTable($this->fileDataTable); $this->tblFileOpt = new DBTable($this->fileOptTable); } } function _validType($type) { if ( empty($type) ) { $type = 'application/octet-stream'; } return $type; } function _validTitle($str) { return basename($str); } function _putDataDB($file, $id_file) { $id = intval($id); $this->_initTables(); $this->tblFileData->delete("id_file = '$id_file'"); $handle = fopen($file, 'rb'); $partNum = 0; while (!feof($handle)) { $partNum++; $contents = fread($handle, $this->blockSize ); $dataArr = array('id_file' => $id_file, 'part' => $partNum, 'data' => $contents); $this->tblFileData->insert($dataArr); } fclose($handle); } function _putDataFS($file, $id_file) { $arr = $this->getArr($id_file, 'id'); copy($file, WWW_PATH.FILE_DB_FS_DIR.'/'.$arr['name']); return true; } function _putData($file, $id_file) { // vdie($this->typeStoring); switch ($this->typeStoring) { case FILE_DB_TS_DB: $this->_putDataDB($file, $id_file); break; case FILE_DB_TS_FS: $this->_putDataFS($file, $id_file); break; } return true; } function getArr($id, $field = 'name') { $id = addslashes($id); $fileTable = 'tbl'.$this->fileTable; $fileOptTable = 'tbl'.$this->fileOptTable; $sql = "SELECT File.*, UNIX_TIMESTAMP(changed) AS changed_uts FROM {$this->db->$fileTable} AS File WHERE File.$field = '$id'"; $fileArr = $this->db->queryRow($sql); $fileArr['opt'] = array(); $sql = "SELECT * FROM {$this->db->$fileOptTable} WHERE id_file = '{$fileArr['id']}'"; foreach ($this->db->queryAll($sql) AS $opt){ $fileArr['opt'][$opt['name'] ] = $opt['value']; } return $fileArr; } function delete($id, $field = 'name') { $fileArr = $this->getArr($id, $field); $this->_initTables(); if (file_exists(WWW_PATH.FILE_DB_FS_DIR.'/'.$fileArr['name']) && is_file(WWW_PATH.FILE_DB_FS_DIR.'/'.$fileArr['name']) ){ unlink(WWW_PATH.FILE_DB_FS_DIR.'/'.$fileArr['name']); } $this->tblFileData->delete("id_file = '{$fileArr['id']}'"); $this->tblFile->delete("id = '{$fileArr['id']}'"); $this->tblFileOpt->delete("id_file = '{$fileArr['id']}'"); return true; } function get($id, $field = 'name') { $fileArr = $this->getArr($id, $field); if (empty($fileArr['id'])){ return false; } if (file_exists(WWW_PATH.FILE_DB_FS_DIR.'/'.$fileArr['name']) && is_file(WWW_PATH.FILE_DB_FS_DIR.'/'.$fileArr['name']) ) { require_once('class/HTTP.php'); skHTTP::redirect(Constant::get('BASE_URL').FILE_DB_FS_DIR.'/'.$fileArr['name']); } header('HTTP/1.0 404 Not Found'); die('Not Found'); require_once('class/DB/FileDB/Stream.php'); require_once ('HTTP/Download.php'); $dl = &new HTTP_Download(); $dl->setResource( FileDB_Stream::open($this, $fileArr['id']) ); $dl->setETag(md5($fileArr['name']) ); $dl->setContentDisposition(HTTP_DOWNLOAD_INLINE, $fileArr['title']); $dl->setContentType($fileArr['type']); $dl->setBufferSize($this->blockSize); $dl->setLastModified( $fileArr['changed_uts'] ); $dl->headers['Pragma'] = '1'; $gmdateExp = gmdate('D, d M Y H:i:s', time() + 3600 * 24 * 365 * 5 ); $dl->headers['Expires'] = $gmdateExp.' GMT'; $dl->setCacheControl('public', 3600 * 24 * 365 * 5); $dl->send(); return true; } function _validateDir($dir) { $dir = preg_replace('/\/+$/', '', $dir); if ( !preg_match('/^\//', $dir) ) { $dir = $this->basePath .'/'.$dir; } return $dir; } function saveFS($dir, $id, $field = 'name') { $fileArr = $this->getArr($id, $field); $dir = $this->_validateDir($dir); $tempnam = tempnam($dir, $this->saveFilePrefix); if (file_exists(WWW_PATH.FILE_DB_FS_DIR.'/'.$fileArr['name'])){ copy(WWW_PATH.FILE_DB_FS_DIR.'/'.$fileArr['name'], $tempnam); $this->typeStoring = FILE_DB_TS_FS; }else { $fileDataTable = 'tbl'.$this->fileDataTable; $handle = fopen($tempnam, 'wb'); $i = 0; do { $sql = "SELECT data FROM {$this->db->$fileDataTable} WHERE id_file = {$fileArr['id']} ORDER BY part ASC LIMIT $i, 1"; $data = $this->db->queryOne($sql); fwrite($handle, $data ); $i++; }while ($data); fclose($handle); } chmod($tempnam, 0755); return $tempnam; } } function file_db($name) { return FILE_DB_URL.'/'.urlencode($name); }