150 lines
5.6 KiB
PHP
150 lines
5.6 KiB
PHP
<?php
|
|
/**
|
|
* The model file of file module of ZenTaoMS.
|
|
*
|
|
* ZenTaoMS is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* ZenTaoMS is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU Lesser General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
* along with ZenTaoMS. If not, see <http://www.gnu.org/licenses/>.
|
|
*
|
|
* @copyright Copyright 2009-2010 青岛易软天创网络科技有限公司(www.cnezsoft.com)
|
|
* @author Chunsheng Wang <chunsheng@cnezsoft.com>
|
|
* @package file
|
|
* @version $Id$
|
|
* @link http://www.zentaoms.com
|
|
*/
|
|
?>
|
|
<?php
|
|
class fileModel extends model
|
|
{
|
|
public $savePath = '';
|
|
public $webPath = '';
|
|
public $now = 0;
|
|
|
|
/* 构造函数。*/
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->now = time();
|
|
$this->setSavePath();
|
|
$this->setWebPath();
|
|
}
|
|
|
|
/* 通过对象类型获取文件列表。*/
|
|
public function getByObject($objectType, $objectID)
|
|
{
|
|
return $this->dao->select('*')->from(TABLE_FILE)->where('objectType')->eq($objectType)->andWhere('objectID')->eq((int)$objectID)->orderBy('id')->fetchAll();
|
|
}
|
|
|
|
/* 获得某一个文件的信息。*/
|
|
public function getById($fileID)
|
|
{
|
|
$file = $this->dao->findById($fileID)->from(TABLE_FILE)->fetch();
|
|
$file->webPath = $this->webPath . $file->pathname;
|
|
$file->realPath = $this->app->getAppRoot() . "www/data/upload/{$this->app->company->id}/" . $file->pathname;
|
|
return $file;
|
|
}
|
|
|
|
/* 保存上传的文件。*/
|
|
public function saveUpload($objectType = '', $objectID = '', $extra = '')
|
|
{
|
|
$fileTitles = array();
|
|
$now = helper::today();
|
|
$files = $this->getUpload();
|
|
|
|
foreach($files as $id => $file)
|
|
{
|
|
move_uploaded_file($file['tmpname'], $this->savePath . $file['pathname']);
|
|
$file['objectType'] = $objectType;
|
|
$file['objectID'] = $objectID;
|
|
$file['addedBy'] = $this->app->user->account;
|
|
$file['addedDate'] = $now;
|
|
$file['extra'] = $extra;
|
|
unset($file['tmpname']);
|
|
$this->dao->insert(TABLE_FILE)->data($file)->exec();
|
|
$fileTitles[$this->dao->lastInsertId()] = $file['title'];
|
|
}
|
|
return $fileTitles;
|
|
}
|
|
|
|
/* 获得文件数量。*/
|
|
public function getCount()
|
|
{
|
|
return count($this->getUpload());
|
|
}
|
|
|
|
/* 获取上传的文件信息。*/
|
|
private function getUpload($htmlTagName = 'files')
|
|
{
|
|
$files = array();
|
|
if(!isset($_FILES[$htmlTagName])) return $files;
|
|
|
|
/* 表单定义中的变量名是数组。*/
|
|
if(is_array($_FILES[$htmlTagName]['name']))
|
|
{
|
|
extract($_FILES[$htmlTagName]);
|
|
foreach($name as $id => $filename)
|
|
{
|
|
if(empty($filename)) continue;
|
|
$file['extension'] = $this->getExtension($filename);
|
|
$file['pathname'] = $this->setPathName($id, $file['extension']);
|
|
$file['title'] = !empty($_POST['labels'][$id]) ? htmlspecialchars($_POST['labels'][$id]) : str_replace('.' . $file['extension'], '', $filename);
|
|
$file['size'] = $size[$id];
|
|
$file['tmpname'] = $tmp_name[$id];
|
|
$files[] = $file;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if(empty($_FILES[$htmlTagName]['name'])) return $files;
|
|
extract($_FILES[$htmlTagName]);
|
|
$file['extension'] = $this->getExtension($name);
|
|
$file['pathname'] = $this->setPathName(0, $file['extension']);
|
|
$file['title'] = !empty($_POST['labels'][0]) ? htmlspecialchars($_POST['labels'][0]) : pathinfo($filename, PATHINFO_FILENAME);
|
|
$file['size'] = $size;
|
|
$file['tmpname'] = $tmp_name;
|
|
return array($file);
|
|
}
|
|
return $files;
|
|
}
|
|
|
|
/* 获取文件扩展名。*/
|
|
private function getExtension($filename)
|
|
{
|
|
$extension = pathinfo($filename, PATHINFO_EXTENSION);
|
|
if(empty($extension)) return 'txt';
|
|
if(strpos($this->config->file->dangers, $extension) !== false) return 'txt';
|
|
return $extension;
|
|
}
|
|
|
|
/* 设置要存储的文件名。*/
|
|
private function setPathName($fileID, $extension)
|
|
{
|
|
$sessionID = session_id();
|
|
$randString = substr($sessionID, mt_rand(0, strlen($sessionID) - 5), 3);
|
|
return date('Ym/dHis', $this->now) . $fileID . mt_rand(0, 10000) . $randString . '.' . $extension;
|
|
}
|
|
|
|
/* 设置存储路径。*/
|
|
private function setSavePath()
|
|
{
|
|
$savePath = $this->app->getAppRoot() . "www/data/upload/{$this->app->company->id}/" . date('Ym/', $this->now);
|
|
if(!file_exists($savePath)) mkdir($savePath, 0777, true);
|
|
$this->savePath = dirname($savePath) . '/';
|
|
}
|
|
|
|
/* 设置web访问路径。*/
|
|
private function setWebPath()
|
|
{
|
|
$this->webPath = $this->app->getWebRoot() . "data/upload/{$this->app->company->id}/";
|
|
}
|
|
}
|