* @package file
* @version $Id: control.php 4129 2013-01-18 01:58:14Z wwccss $
* @link https://www.zentao.net
*/
class file extends control
{
/**
* Build the upload form.
*
* @param int $fileCount
* @param float $percent
* @param string $filesName
* @param string $labelsName
* @access public
* @return void
*/
public function buildForm(int $fileCount = 1, float $percent = 0.9, string $filesName = "files", string $labelsName = "labels")
{
if(!file_exists($this->file->savePath)) return printf($this->lang->file->errorNotExists, $this->file->savePath);
if(!is_writable($this->file->savePath)) return printf($this->lang->file->errorCanNotWrite, $this->file->savePath, $this->file->savePath);
$this->view->filesName = $filesName;
$this->view->labelsName = $labelsName;
$this->display();
}
/**
* Build the old upload form.
*
* @param int $fileCount
* @param float $percent
* @param string $filesName
* @param string $labelsName
* @access public
* @return void
*/
public function buildOldForm(int $fileCount = 1, float $percent = 0.9, string $filesName = "files", string $labelsName = "labels")
{
if(!file_exists($this->file->savePath)) return printf($this->lang->file->errorNotExists, $this->file->savePath);
if(!is_writable($this->file->savePath)) return printf($this->lang->file->errorCanNotWrite, $this->file->savePath, $this->file->savePath);
$this->view->filesName = $filesName;
$this->view->labelsName = $labelsName;
$this->display();
}
/**
* AJAX: get upload request from the web editor.
*
* @param string $uid
* @param string $objectType
* @param int $objectID
* @param string $extra
* @param string $field
* @param bool $api
* @access public
* @return void
*/
public function ajaxUpload(string $uid = '', string $objectType = '', int $objectID = 0, string $extra = '', string $field = 'imgFile', bool $api = false, string $gid = '')
{
$file = $this->file->getUpload($field);
if(!isset($file[0]) or strpos(",{$this->config->file->allowed},", ",{$file[0]['extension']},") === false) return print(json_encode(array('result' => 'fail', 'message' => $this->lang->file->errorFileFormat)));
$file = $file[0];
if($file)
{
if($file['size'] == 0)
{
if(defined('RUN_MODE') && RUN_MODE == 'api') return print(json_encode(array('status' => 'error', 'message' => $this->lang->file->errorFileUpload)));
return print(json_encode(array('error' => 1, 'message' => $this->lang->file->errorFileUpload)));
}
if(@move_uploaded_file($file['tmpname'], $this->file->savePath . $this->file->getSaveName($file['pathname'])))
{
/* Compress image for jpg and bmp. */
$file = $this->file->compressImage($file);
$file['addedBy'] = $this->app->user->account;
$file['addedDate'] = helper::today();
$file['objectType'] = $objectType;
$file['objectID'] = $objectID;
$file['extra'] = $extra;
$file['gid'] = empty($gid) ? '' : base64_decode($gid);
unset($file['tmpname']);
$this->dao->insert(TABLE_FILE)->data($file)->exec();
$fileID = $this->dao->lastInsertID();
$url = $this->createLink('file', 'read', "fileID=$fileID", $file['extension']);
if($uid) $_SESSION['album'][$uid][] = $fileID;
if($api || (defined('RUN_MODE') && RUN_MODE == 'api'))
{
if($uid) $_SESSION['album']['used'][$uid][$fileID] = $fileID;
$_SERVER['SCRIPT_NAME'] = 'index.php';
return $this->send(array('result' => 'success', 'status' => 'success', 'id' => $fileID, 'url' => $url, 'data' => array('id' => $fileID, 'url' => $url))); // 兼容老的 API 形式。 Compatible with the old API.
}
return print(json_encode(array('error' => 0, 'url' => $url)));
}
else
{
$error = strip_tags(sprintf($this->lang->file->errorCanNotWrite, $this->file->savePath, $this->file->savePath));
if(defined('RUN_MODE') && RUN_MODE == 'api') return $this->send(array('status' => 'error', 'message' => $error));
return print(json_encode(array('error' => 1, 'message' => $error)));
}
}
return $this->send(array('status' => 'error', 'message' => $this->lang->file->uploadImagesExplain));
}
/**
* Preview a file.
*
* @param int $fileID
* @param string $mouse
* @access public
* @return void
*/
public function preview(int $fileID, string $mouse = '')
{
return print($this->fetch('file', 'download', "fileID=$fileID&mouse=$mouse"));
}
/**
* Down a file.
*
* @param int $fileID
* @param string $mouse
* @access public
* @return void
*/
public function download(int $fileID, string $mouse = '')
{
$file = $this->file->getById($fileID);
if(empty($file) || !$this->file->fileExists($file))
{
$this->view->error = $this->lang->file->fileNotFound;
if(defined('RUN_MODE') && RUN_MODE == 'api') return $this->send(array('status' => 'fail', 'code' => 404, 'message' => $this->view->error));
if(isInModal()) return $this->display();
return print($this->view->error);
}
/* Unset the menu. */
if(!empty($this->lang->{$this->app->tab}->menu)) $this->lang->{$this->app->tab}->menu = array();
if(!$this->file->checkPriv($file))
{
if(isInModal()) return $this->send(array('result' => 'success', 'message' => $this->lang->file->accessDenied, 'closeModal' => true, 'load' => true));
return $this->send(array('result' => 'success', 'message' => $this->lang->file->accessDenied, 'load' => helper::createLink('my', 'index')));
}
/* Judge the mode, down or open. */
$mode = $this->fileZen->getDownloadMode($file, $mouse);
/* Down the file. */
if($mode != 'open')
{
$fileName = $file->title;
if(!preg_match("/\.{$file->extension}$/", $fileName)) $fileName .= '.' . $file->extension;
return $this->sendDownHeader($fileName, $file->extension, $file->realPath, 'file');
}
/* If the mode is open, locate directly. */
$this->view->title = $this->lang->file->previewFile;
$this->view->file = $file;
$this->view->charset = $this->get->charset ? $this->get->charset : $this->config->charset;
$this->view->fileType = ($file->extension == 'txt') ? 'txt' : ($file->extension == 'mp4' ? 'video' : 'image');
$this->display();
}
/**
* Export as csv format.
*
* @access public
* @return void
*/
public function export2CSV()
{
$fields = $this->post->fields;
$rows = $this->post->rows;
$domain = common::getSysURL();
/* Build csv content. */
$output = '';
if($rows)
{
$output .= '"'. implode('","', $fields) . '"' . "\n";
foreach($rows as $row)
{
$output .= '"';
foreach($fields as $fieldName => $fieldLabel)
{
/* 标签替换成链接。 */
if(in_array($fieldName, $this->config->file->img2href))
{
$pattern = '/
/';
$replacement = '$1 (' . $domain . '/$2)';
$row->$fieldName = preg_replace($pattern, $replacement, $row->$fieldName);
}
if(isset($row->$fieldName) && is_numeric($row->$fieldName)) $row->$fieldName = $row->$fieldName . "\t";
if(isset($row->$fieldName)) $row->$fieldName = str_replace('"', '“', $row->$fieldName);
$output .= isset($row->$fieldName) ? str_replace(array('"', ' ', '>'), array('“', ' ', '>'), htmlSpecialString(strip_tags((string)$row->$fieldName, '
'))) : '';
$output .= '","';
}
$output .= '"' . "\n";
}
if($this->post->kind == 'task' && $this->config->vision != 'lite') $output .= $this->lang->file->childTaskTips;
}
$output = helper::replaceEmoji($output);
$output = htmlspecialchars_decode(htmlspecialchars_decode($output, ENT_NOQUOTES), ENT_NOQUOTES);
if(isset($_POST['encode']) && $this->post->encode != "utf-8") $output = helper::convertEncoding($output, 'utf-8', $this->post->encode . '//TRANSLIT');
$this->sendDownHeader($this->post->fileName, 'csv', $output);
}
/**
* export as xml format
*
* @access public
* @return void
*/
public function export2XML()
{
$fields = $this->post->fields;
$rows = $this->post->rows;
$output = "
')) : '';
$output .= " <$fieldName>$fieldValue$fieldName>\n";
}
$output .= "