* @package file
* @version $Id: control.php 4129 2013-01-18 01:58:14Z wwccss $
* @link http://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($fileCount = 1, $percent = 0.9, $filesName = "files", $labelsName = "labels")
{
if(!file_exists($this->file->savePath))
{
printf($this->lang->file->errorNotExists, $this->file->savePath);
return false;
}
elseif(!is_writable($this->file->savePath))
{
printf($this->lang->file->errorCanNotWrite, $this->file->savePath, $this->file->savePath);
return false;
}
$this->view->filesName = $filesName;
$this->view->labelsName = $labelsName;
$this->display();
}
/**
* AJAX: get upload request from the web editor.
*
* @param string $uid
* @access public
* @return void
*/
public function ajaxUpload($uid = '')
{
$file = $this->file->getUpload('imgFile');
if(!isset($file[0]) or !in_array($file[0]['extension'], $this->config->file->imageExtensions))
{
return print(json_encode(array('result' => 'fail', 'message' => $this->lang->file->errorFileFormate)));
}
$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)));
}
else
{
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();
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(defined('RUN_MODE') && RUN_MODE == 'api')
{
if($uid) $_SESSION['album']['used'][$uid][$fileID] = $fileID;
$_SERVER['SCRIPT_NAME'] = 'index.php';
return $this->send(array('status' => 'success', 'id' => $fileID, 'url' => $url));
}
else
{
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));
}
else
{
return print(json_encode(array('error' => 1, 'message' => $error)));
}
}
}
return $this->send(array('status' => 'error', 'message' => $this->lang->file->uploadImagesExplain));
}
/**
* Down a file.
*
* @param int $fileID
* @param string $mouse
* @access public
* @return void
*/
public function download($fileID, $mouse = '')
{
if(session_id() != $this->app->sessionID) helper::restartSession($this->app->sessionID);
$file = $this->file->getById($fileID);
if(empty($file))
{
if(defined('RUN_MODE') && RUN_MODE == 'api') return $this->send(array('status' => 'fail', 'code' => 404, 'message' => $this->lang->file->fileNotFound));
if(isInModal())
{
$this->view->error = $this->lang->file->fileNotFound;
return $this->display();
}
else
{
return print($this->lang->file->fileNotFound);
}
}
if(!$this->file->checkPriv($file))
{
echo js::alert($this->lang->file->accessDenied);
if(isInModal()) return print(js::reload('parent.parent'));
return print(js::locate(helper::createLink('my', 'index'), 'parent.parent'));
}
/* Judge the mode, down or open. */
$mode = 'down';
$fileTypes = 'txt|jpg|jpeg|gif|png|bmp|xml|html';
if(stripos($fileTypes, $file->extension) !== false && $mouse == 'left') $mode = 'open';
if($file->extension == 'txt')
{
$extension = 'txt';
if(($position = strrpos($file->title, '.')) !== false) $extension = substr($file->title, $position + 1);
if($extension != 'txt') $mode = 'down';
$file->extension = $extension;
}
if($this->file->fileExists($file))
{
/* If the mode is open, locate directly. */
if($mode == 'open')
{
if(stripos('txt|jpg|jpeg|gif|png|bmp', $file->extension) !== false)
{
$this->view->file = $file;
$this->view->charset = $this->get->charset ? $this->get->charset : $this->config->charset;
$this->view->fileType = ($file->extension == 'txt') ? 'txt' : 'image';
$this->display();
}
else
{
$this->locate($file->webPath);
}
}
else
{
/* Down the file. */
$fileName = $file->title;
if(!preg_match("/\.{$file->extension}$/", $fileName)) $fileName .= '.' . $file->extension;
$this->sendDownHeader($fileName, $file->extension, $file->realPath, 'file');
}
}
else
{
if(defined('RUN_MODE') && RUN_MODE == 'api') return $this->send(array('status' => 'fail', 'code' => 404, 'message' => $this->lang->file->fileNotFound));
if(isInModal())
{
$this->view->error = $this->lang->file->fileNotFound;
return $this->display();
}
else
{
return print($this->lang->file->fileNotFound);
}
}
}
/**
* Export as csv format.
*
* @access public
* @return void
*/
public function export2CSV()
{
$fields = $this->post->fields;
$rows = $this->post->rows;
/* Build csv content. */
$output = '';
if($rows)
{
$output .= '"'. implode('","', $fields) . '"' . "\n";
foreach($rows as $row)
{
$output .= '"';
foreach($fields as $fieldName => $fieldLabel)
{
$output .= isset($row->$fieldName) ? str_replace(array('"', ' '), array('“', ' '), htmlspecialchars_decode(strip_tags($row->$fieldName, ''))) : '';
$output .= '","';
}
$output .= '"' . "\n";
}
if($this->post->kind == 'task' && $this->config->vision != 'lite') $output .= $this->lang->file->childTaskTips;
}
if($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 = "