westdc-zf1/application/default/controllers/SubmitController.php

749 lines
20 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
class SubmitController extends Zend_Controller_Action
{
private $limit=10;
private $debug=1;
function preDispatch()
{
$this->view->config = Zend_Registry::get('config');
$this->db=Zend_Registry::get('db');
}
function indexAction()
{
}
//新建元数据
function newdataAction()
{
$ac = $this->_request->getParam('ac');
$id = $this->_request->getParam('id');
$this->wdb=Zend_Db::factory($this->view->config->geonetwork);
$auth = Zend_Auth::getInstance();
if($auth->hasIdentity())
{
$user = $auth->getIdentity();
$u_id = $user->id;
$this->view->isadmin=false;
if ($user->usertype=='administrator') $this->view->isadmin=true;
}
//根据已有元数据模板创建元数据
if(empty($ac))
{
$keywords = $this->_request->getParam('q');
$sql="select id,(regexp_matches(data,'<resTitle>(.*)</resTitle>'))[1] as title,(owner-$u_id) as isowner from metadata where istemplate='y' and schemaid='iso19115'";
if(!empty($keywords))
{
$this->view->q = $keywords;
$search=new Search($keywords);
$where=$search->sql_expr(array("data"));
$sql.=' and '.$where;
}
$sql.=" order by changedate desc";
$sth = $this->wdb->prepare($sql);
$sth->execute();
$rows = $sth->fetchAll();
$paginator = Zend_Paginator::factory($rows);
$paginator->setCurrentPageNumber($this->_getParam('page'));
$paginator->setItemCountPerPage(10);
$paginator->setView($this->view);
Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination_param.phtml');
$this->view->paginator=$paginator;
}
//根据已有数据创建元数据
else if($ac=="add")
{
$keywords = $this->_request->getParam('q');
$sql = "SELECT md.title,md.uuid,md.description,gn.id as gid FROM normalmetadata md
left join geonetworkmetadata gn on md.uuid=gn.uuid
WHERE gn.id is not null";
if(!empty($keywords))
{
$this->view->q = $keywords;
$search=new Search($keywords);
$where=$search->sql_expr(array("md.title","md.description"));
$sql.=' and '.$where;
}
$sql.=" order by md.ts_created desc";
$sth = $this->db->prepare($sql);
$sth->execute();
$rows = $sth->fetchAll();
$paginator = Zend_Paginator::factory($rows);
$paginator->setCurrentPageNumber($this->_getParam('page'));
$paginator->setItemCountPerPage(10);
$paginator->setView($this->view);
Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination_param.phtml');
$this->view->paginator=$paginator;
$this->_helper->viewRenderer('newdata-add');
}
}
//未提交数据列表
function unsubmitAction()
{
$ac = $this->_request->getParam('ac');
$id = $this->_request->getParam('id');
$this->wdb=Zend_Db::factory($this->view->config->geonetwork);
$auth = Zend_Auth::getInstance();
if($auth->hasIdentity())
{
$user = $auth->getIdentity();
$u_id = $user->id;
}
//提交数据
if($ac=="commit")
{
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender();
$data = "";
try{
$id = $this->_request->getParam('id');
if(empty($id) || !is_numeric($id))
{
$data = array("error"=>"参数错误");
$this->jsonexit($data);
return true;
}
$changelog = $this->_request->getParam('changelog');
if(empty($changelog))
{
$data = array("error"=>$this->alertbox('warning','请输入变更信息'));
$this->jsonexit($data);
return true;
}
// 1. 权限认定当前用户必须和其owner相同
// 数据应当没有评审状态,没有作者信息
$sql="select gn.id from geonetworkmetadata gn
left join mdstatus s on gn.uuid=s.uuid
left join mdauthor a on s.uuid=a.uuid
where (s.id is not null or a.id is not null) and gn.id=?";
$sth=$this->db->prepare($sql);
$sth->execute(array($id));
$row=$sth->fetch();
if (!empty($row))
{
$data = array("error"=>'错误的入口');
$this->jsonexit($data);
return true;
}
$sql="select uuid,data as xml from metadata where id=? and owner=?";
$sth=$this->wdb->prepare($sql);
$sth->execute(array($id,$u_id));
$row=$sth->fetch();
if (empty($row))
{
$data = array("error"=>'无权限修改数据');
$this->jsonexit($data);
return true;
}else{
$uuid = $row['uuid'];
}
$messages = array();
// 保存数据作者信息
$sql="insert into mdauthor (uuid,userid,ts_activated,status) values(?,?,now(),1)";
$sth=$this->db->query($sql,array($row['uuid'],$u_id));
// 2. 保存变化记录 save changelog & userid for the latest version
$sql = "UPDATE mdversion SET changelog=?,userid=? WHERE id in (select id from mdversion where uuid=? order by ts_created desc limit 1)";
$this->db->query($sql,array($changelog,$u_id,$row['uuid']));
// 处理文件权限和数据信息
$ftp_user = "qherc".$u_id."upload";
$sql = "SELECT * FROM pureftp WHERE userid=? AND homedir LIKE ?";
$sth = $this->db->prepare($sql);
$sth->execute(array($ftp_user,'%'.$uuid.'%'));
$row1 = $sth->fetch();
if(!empty($row1['passwd']))
{
$old=umask(0);
//$this->chmodr($row1['homedir'],0444);
umask($old);
}
//$path = $row1['homedir'];
//选择固定path地址以防止用户多次上传数据后homedir发生变更
$path='/home/wlx/qhhdata/upload/'.$uuid.'/';
//delete dataset & datafile records
$sql="delete from dataset where uuid=?";
$sth = $this->db->prepare($sql);
$sth->execute(array($uuid));
$sql = "INSERT INTO dataset (uuid,path) VALUES (?,?) RETURNING id";
$sth = $this->db->prepare($sql);
$rs = $sth->execute(array($uuid,$path));
if(!$rs)
{
$messages[] = "元数据信息写入失败";
/*
$data = array("error"=>'元数据信息写入失败');
$this->jsonexit($data);
return true;
*/
}
$temp = $sth->fetch(PDO::FETCH_ASSOC);
$dsid = $temp['id'];
$dir = new mydir();
$files=$dir->recursive($path);
foreach ($files as $k=>$v)
{
//$pathinfo = pathinfo($path.$v);
$filename = mb_substr($v,mb_strlen($path)+1);
$filesize = filesize($v);
$isdir=is_dir($v)?1:0;
$depth=substr_count($filename,"/")+1;
if (substr($filename,-1,1)=='/') $depth--;
//$this->chmodr($path.$v,0444);
$sql = "INSERT INTO datafile (dsid,filename,filesize,isdir,depth) VALUES (?,?,?,?,?)";
$sth = $this->db->prepare($sql);
$rs = $sth->execute(array($dsid,$filename,$filesize,$isdir,$depth));
if(!$rs)
{
$messages[] = "数据文件".$filename.'写入失败';
}
}
// 3. 保存数据评审状态
//导入元数据
$iso=new ISO19115();
@$iso->saveDB($this->db,$row['xml']);
//进入评审库
$sql="insert into mdstatus (uuid,status,userid) values(?,?,?)";
$this->db->query($sql,array($uuid,0,$u_id));
//email to admin
$mail=new WestdcMailer($this->view->config->smtp);
$mail->setFrom($this->view->config->service->email,'数据服务组');
$mailtp=new EmailText($this->db,"metadata-new-admin",array(
'user' => $user->username,
'uuid' => $iso->uuid,
'email'=> $user->email,
//元数据标题
'title'=> $iso->resTitle,
));
$mail->setBodyText($mailtp->getBody());
$mail->setSubject($mailtp->getSubject());
$mail->addTo($this->view->config->service->email);
$mail->send();
unset($mail);
unset($mailtp);
//email to author
$mail=new WestdcMailer($this->view->config->smtp);
$mail->setFrom($this->view->config->service->email,'数据服务组');
$mailtp=new EmailText($this->db,"metadata-new-author",array(
'user' => $user->username,
'uuid' => $iso->uuid,
'email'=> $user->email,
//元数据标题
'title'=> $iso->resTitle,
));
$mail->setBodyText($mailtp->getBody());
$mail->setSubject($mailtp->getSubject());
$mail->addTo($user->email);
$mail->addCc($this->view->config->service->email);
@$mail->send();
$data = array("commited"=>1,"error"=>$this->alertbox('ok','该版本已经成功提交,请等待数据中心进一步处理!'));
$this->jsonexit($data);
return true;
}catch(Exception $e) {
$msg = "提交失败,请确认权限后重试";
if($this->debug>0)
{$msg .= $e->getMessage();}
$data = array("error"=>$this->alertbox('error',$msg));
$this->jsonexit($data);
return true;
}
}
else
{
$sql = "SELECT (regexp_matches(gn.data,'<resTitle>(.*)</resTitle>'))[1] as title,gn.id,gn.uuid FROM geonetworkmetadata gn
WHERE gn.uuid not in (select uuid from metadata) and gn.owner=?
order by gn.id desc
";
$sth = $this->db->prepare($sql);
$sth->execute(array($u_id));
$rows = $sth->fetchAll();
$paginator = Zend_Paginator::factory($rows);
$paginator->setCurrentPageNumber($this->_getParam('page'));
$paginator->setItemCountPerPage(15);
$paginator->setView($this->view);
Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination_param.phtml');
$this->view->paginator=$paginator;
}
}
function uploadAction()
{
$this->_helper->layout()->disableLayout();
$uuid = $this->_request->getParam('uuid');
$this->view->uuid=$uuid;
$ac = $this->_request->getParam('ac');
$dataFilePath = "../data/datafiles/";
if($ac=='submit')
{
$this->_helper->viewRenderer->setNoRender();
$auth = Zend_Auth::getInstance();
if($auth->hasIdentity())
{
$user = $auth->getIdentity();
$userid = $user->id;
}
if(empty($userid))
{
$data = array("error"=>'请先登录后进行操作');
$this->jsonexit($data);
return true;
}
if(empty($uuid))
{
$data = array("error"=>'参数错误'.$uuid);
$this->jsonexit($data);
return true;
}
$files = $_REQUEST['files'];
if(empty($files) || !is_array($files))
{
$data = array("error"=>'请先上传文件');
$this->jsonexit($data);
return true;
}
//sql
$data = array("error"=>'数据文件保存成功');
$this->jsonexit($data);
return true;
}
if(empty($ac) && !empty($_FILES['Filedata']))
{
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender();
$data = "";
try{
$auth = Zend_Auth::getInstance();
if($auth->hasIdentity())
{
$user = $auth->getIdentity();
$userid = $user->id;
}
if(empty($userid)||!is_numeric($userid)){
$data = array("error"=>'请先登录');
$this->jsonexit($data);
return true;
}
include("files.php");
$msg = files::dataFilesUpload($dataFilePath,$_FILES['Filedata'],'datafiles',$uuid);
if(empty($msg['error']))
{
$filename = $msg['db_path'];
$filesize = $msg['file_size'];
$filedesc = $this->_request->getParam('filedesc');
$filetype = $this->_request->getParam('dir');
$realname = $msg['realname'];
$fileurl = $msg['file_url'];
$sql = "insert into attachments (filename,filetype,filedesc,userid,filesize,realname) values ('$filename','datafiles','$filedesc','$userid','$filesize','$realname') RETURNING id";
$sth = $this->db->prepare($sql);
$sth->execute();
$att = $sth->fetch(PDO::FETCH_ASSOC);
$attid = $att['id'];
$html = $realname.'[已完成]<input type="hidden" name="files[]" value="'.$attid.'" /><div class="cancel"><a href="javascript:;" id="deletebtn_'.$attid.'"><img border="0" src="/static/js/uploadify/cancel.png" /></a></div>';
$data = array(
'html'=>$html,
'attid'=>$attid,
'error'=>''
);
echo Zend_Json::encode($data);
exit();
}else{
@unlink($filename);
$data = array(
'error'=>'附件上传失败:'.$msg['error'],
);
echo Zend_Json::encode($data);
exit();
}
}catch(Exception $e){
if($this->debug>0)
{
$error="错误:".$e->getMessage();
}else{
$error="处理中发生错误";
}
$data = array(
'error'=>$error,
);
echo Zend_Json::encode($data);
exit();
}
}
if($ac == 'del')
{
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender();
$id = $this->_request->getParam('id');
$info = $this->getFileinfo($id);
$filepath = $dataFilePath.$info['filename'];
try{
$auth = Zend_Auth::getInstance();
if($auth->hasIdentity())
{
$user = $auth->getIdentity();
$userid = $user->id;
$sql = "delete from attachments where id='$id' and userid='$userid'";
if($this->db->exec($sql)>0)
{
@unlink($filepath);
echo "ok";
}
}
}catch(Exception $e){}
//不输出任何错误
}
}
function filesAction(){
$this->_helper->layout()->disableLayout();
$uuid = $this->_request->getParam('uuid');
$this->view->uuid=$uuid;
$ac = $this->_request->getParam('ac');
$dataFilePath = "../data/datafiles";
$auth = Zend_Auth::getInstance();
if($auth->hasIdentity())
{
$user = $auth->getIdentity();
$userid = $user->id;
}
if(empty($ac) || $ac=='list')
{
$sql = "SELECT * FROM attachments WHERE filetype='datafiles' AND userid=? ORDER BY id DESC";
$sth = $this->db->prepare($sql);
$sth->execute(array($userid));
$rows = $sth->fetchAll();
$paginator = Zend_Paginator::factory($rows);
$paginator->setCurrentPageNumber($this->_getParam('page'));
$paginator->setItemCountPerPage(8);
$paginator->setView($this->view);
Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination.phtml');
$this->view->paginator=$paginator;
}
if($ac=="editname")
{
$this->_helper->viewRenderer->setNoRender();
$name = $this->_request->getParam('name');
$id = $this->_request->getParam('id');
if(empty($name))
{
$data = array("error"=>'请输入文件名');
$this->jsonexit($data);
return true;
}
if(empty($id))
{
$data = array("error"=>'参数错误');
$this->jsonexit($data);
return true;
}
try{
$sql = "UPDATE attachments SET realname=? WHERE id=?";
$sth = $this->db->prepare($sql);
$ex = $sth->execute(array($name,$id));
if($ex)
{
$data = array("success"=>'1');
$this->jsonexit($data);
return true;
}
else
{
$data = array("error"=>'遇到错误请重试');
$this->jsonexit($data);
return true;
}
}catch(Exception $e){
$data = array("error"=>'遇到错误请重试'.$e->getMessage());
$this->jsonexit($data);
return true;
}
}
}//文件管理
//ftp上传
function ftpAction()
{
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender();
$ac = $this->_getParam('ac');
$uuid = $this->_getParam('uuid');
$this->view->uuid = $uuid;
$auth = Zend_Auth::getInstance();
if($auth->hasIdentity())
{
$user = $auth->getIdentity();
$userid = $user->id;
}
if(empty($ac))
{
$uname = 'qherc'.$userid.'upload';
$sql = "SELECT * FROM pureftp WHERE userid='$uname' ORDER BY pkid DESC";
$sth = $this->db->prepare($sql);
$sth->execute();
$row = $sth->fetch();
$homedir = "/home/wlx/qhhdata/upload/".$uuid."/";
$old=umask(0);
@mkdir($homedir,0777);
umask($old);
if(!empty($row['pkid']))
{
if(preg_match("/.*".$uuid.".*/",$row['homedir']))
{
$data = array(
'statu'=>1,
'user'=>$row['userid'],
'passwd'=>$row['passwd']
);
$this->jsonexit($data);
return true;
}else{
$uid = 1001;
$gid = 1001;
$passwd = $this->genRandomString(16);
$sql = "UPDATE pureftp SET passwd=?,uid=?,gid=?,homedir=? WHERE userid=?";
$sth = $this->db->prepare($sql);
$rs = $sth->execute(array($passwd,$uid,$gid,$homedir,$uname));
if($rs)
{
$data = array(
'statu'=>1,
'user'=>$uname,
'passwd'=>$passwd
);
$this->jsonexit($data);
return true;
}else{
$data = array(
'error'=>"FTP信息更新失败请重试"
);
$this->jsonexit($data);
return true;
}
}
}else{
$uid = 1001;
$gid = 1001;
$passwd = $this->genRandomString(16);
$sql = "INSERT INTO pureftp (userid,passwd,uid,gid,homedir) VALUES (?,?,?,?,?)";
$sth = $this->db->prepare($sql);
$rs = $sth->execute(array($uname,$passwd,$uid,$gid,$homedir));
if($rs)
{
$data = array(
'statu'=>1,
'user'=>$uname,
'passwd'=>$passwd
);
$this->jsonexit($data);
return true;
}else{
$data = array(
'error'=>"FTP信息更新失败请重试"
);
$this->jsonexit($data);
return true;
}
}
}
}//ftp上传
function genRandomString($len)
{
$chars = array(
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
"l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
"w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
"H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
"S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2",
"3", "4", "5", "6", "7", "8", "9"
);
$charsLen = count($chars) - 1;
shuffle($chars); // 将数组打乱
$output = "";
for ($i=0; $i<$len; $i++)
{
$output .= $chars[mt_rand(0, $charsLen)];
}
return $output;
}
public function getFileinfo($id){
$sql = "select * from attachments where id='$id'";
$re= $this->db->query($sql);
$row= $re->fetch();
return $row;
}
//成为作者后的后继处理工作
private function author_first($uuid,$author)
{
$sql="insert into mdversion (xml,ts_created,uuid,changelog,userid)
select x.data,m.ts_created,?,?,? from metadata m left join xml x on m.id=x.id
left join mdversion v on m.uuid=v.uuid
where m.uuid=? and v.changelog is null";
$sth=$this->db->prepare($sql);
try
{
$sth->execute(array($uuid,'初始版本 version 1.0',$author,$uuid));
} catch(Exception $e){
// do nothing here.
// 说明之前已经有对应数据
}
$this->wdb=Zend_Db::factory($this->view->config->geonetwork);
$sql="update metadata set owner=? where uuid=?";
$sth=$this->wdb->prepare($sql);
$sth->execute(array($author,$uuid));
}
/*
* jsonexit() 退出并返回json数据
*
* param array $data 要返回的JSON数据可以是任意数组
*
* return JSON-response
*/
public function jsonexit($data){
$this->getResponse()->setHeader('Content-Type', 'application/json')->appendBody(Zend_Json::encode($data));
return true;
}//jsonexit() 退出并返回json数据
//ajax 提示框
public function alertbox($type='',$body){
if($type == "error")
{
$img = '<img src="/images/alert_big_error.png" />';
$text = '<h4>'.$body.'</h4>';
return $img.$text;
}
if($type == "ok")
{
$img = '<img src="/images/alert_big_ok.png" />';
$text = '<h4>'.$body.'</h4>';
return $img.$text;
}
if($type == "warning")
{
$img = '<img src="/images/alert_big_warning.png" />';
$text = '<h4>'.$body.'</h4>';
return $img.$text;
}
if(empty($type))
{
$text = '<h4>'.$body.'</h4>';
return $text;
}
}
function chmodr($path, $filemode) {
if (!is_dir($path))
return chmod($path, $filemode);
$dh = opendir($path);
while (($file = readdir($dh)) !== false) {
if($file != '.' && $file != '..') {
$fullpath = $path.'/'.$file;
if(is_link($fullpath))
return FALSE;
elseif(!is_dir($fullpath) && !chmod($fullpath, $filemode))
return FALSE;
elseif(!$this->chmodr($fullpath, $filemode))
return FALSE;
}
}
closedir($dh);
if(chmod($path, $filemode))
return TRUE;
else
return FALSE;
}
}