debug = 1;
$this->view->pageID = "heihe-".$this->_request->getActionName();
$this->view->nav[] = array('link'=>"/heihe",'title'=>'黑河计划数据管理中心');
}
function indexAction()
{
$sql="select m.uuid,m.title,m.id,m.description from normalmetadata m left join thumbnail t on m.id=t.id where m.uuid in (select d.uuid from datasource d left join source s on d.sourceid=s.id where s.code='heihe') and length(t.data)>2 order by random() limit 3";
$this->view->meatdata=$this->db->fetchAll($sql);
$archive = new Archive($this->db);
$this->view->info = $archive->getOneArchive('黑河流域数据简介','about');
}
/*
* 数据浏览
*/
function browseAction()
{
$page=(int)$this->_request->getParam('page');
if (empty($page)) $page=1;
$offset=$this->limit*($page-1);
$state=$this->db->query("select count(*) from normalmetadata where uuid in (select d.uuid from datasource d left join source s on d.sourceid=s.id where s.code='heihe') ");
$row=$state->fetchAll();
$sum=$row[0]['count'];
$select=$this->db->select();
$select->from('normalmetadata','*')->where(" uuid in (select d.uuid from datasource d left join source s on d.sourceid=s.id where s.code='heihe') ")->order('title desc')->limitPage($page,$this->limit);
$this->view->metadata = $this->db->fetchAll($select);
$this->view->page=new Pagination($sum,$page,$this->limit);
$this->view->offset=$offset+1;
}
function thumbAction()
{
$page=(int)$this->_request->getParam('page');
if (empty($page)) $page=1;
$offset=$this->limit*($page-1);
$state=$this->db->query("select count(*) from normalmetadata where uuid in (select d.uuid from datasource d left join source s on d.sourceid=s.id where s.code='heihe') ");
$row=$state->fetchAll();
$sum=$row[0]['count'];
$select=$this->db->select();
$select->from('normalmetadata as m','m.*')
->where(" uuid in (select d.uuid from datasource d left join source s on d.sourceid=s.id where s.code='heihe') ")
->order('m.title desc')->limitPage($page,$this->limit);
$this->view->metadata = $this->db->fetchAll($select);
$this->view->page=new Pagination($sum,$page,$this->limit);
}
function documentAction()
{
$page=(int)$this->_request->getParam('page');
if (empty($page)) $page=1;
$offset=$this->limit*($page-1);
$row=$this->db->fetchAll("select count(*) from reference where id in (select refid from mdref where uuid in (select uuid from normalmetadata where uuid in (select d.uuid from datasource d left join source s on d.sourceid=s.id where s.code='heihe') ))");
$sum=$row[0]['count'];
$sql="select * from reference where id in (select refid from mdref where uuid in (select uuid from normalmetadata where uuid in (select d.uuid from datasource d left join source s on d.sourceid=s.id where s.code='heihe') )) order by reference limit ? offset ?";
$this->view->refs=$this->db->fetchAll($sql,array($this->limit,$offset));
$this->view->page=new Pagination($sum,$page,$this->limit,"文献");
$this->view->offset=$offset+1;
}
function baseAction()
{
$this->getmd(array('冰川','沙漠','地貌','植被','草场','沙漠化','基础地理','遥感影像','水文地质','社会经济','人口','SWAT','NPP','WRF','辐射','NEP'));
$archive = new Archive($this->db);
$this->view->info = $archive->getOneArchive('黑河计划-基础数据','about');
$this->view->nav[] = array('link'=>"/heihe/base",'title'=>'基础数据');
$this->_helper->viewRenderer('base');
}
function geobaseAction()
{
$this->getmd('基础地理');
$archive = new Archive($this->db);
$this->view->info = $archive->getOneArchive('黑河计划-基础地理数据','about');
$this->view->nav[] = array('link'=>"/heihe/geobase",'title'=>'基础地理数据');
$this->_helper->viewRenderer('base');
}
function coreAction()
{
$this->getmd(array('DEM','土地利用','土壤','气象','水文观测','灌溉','试验'));
$archive = new Archive($this->db);
$this->view->info = $archive->getOneArchive('黑河计划-核心数据','about');
$this->view->nav[] = array('link'=>"/heihe/core",'title'=>'核心数据');
$this->_helper->viewRenderer('base');
}
function demAction()
{
$this->getmd('DEM');
$archive = new Archive($this->db);
$this->view->info = $archive->getOneArchive('黑河计划-DEM','about');
$this->view->nav[] = array('link'=>"/heihe/dem",'title'=>'DEM');
$this->_helper->viewRenderer('base');
}
function rsAction()
{
$this->getmd('遥感影像');
$archive = new Archive($this->db);
$this->view->info = $archive->getOneArchive('黑河计划-遥感影像','about');
$this->view->nav[] = array('link'=>"/heihe/rs",'title'=>'遥感影像');
$this->_helper->viewRenderer('base');
}
function waterAction()
{
$this->getmd('水文地质');
$archive = new Archive($this->db);
$this->view->info = $archive->getOneArchive('黑河计划-水文水资源数据','about');
$this->view->nav[] = array('link'=>"/heihe/water",'title'=>'水文水资源数据');
$this->_helper->viewRenderer('base');
}
function modelAction()
{
$this->getmd(array('WRF','NPP','SWAT','NEP','辐射'));
$archive = new Archive($this->db);
$this->view->info = $archive->getOneArchive('黑河计划-模型数据','about');
$this->view->nav[] = array('link'=>"/heihe/model",'title'=>'模型数据');
$this->_helper->viewRenderer('base');
}
function economicAction()
{
$this->getmd(array('人口','社会经济'));
$archive = new Archive($this->db);
$this->view->info = $archive->getOneArchive('黑河计划-社会经济数据','about');
$this->view->nav[] = array('link'=>"/heihe/economic",'title'=>'社会经济数据');
$this->_helper->viewRenderer('base');
}
function landsurfaceAction()
{
$this->getmd(array('冰川','沙漠','地貌','植被','草场','沙漠化'));
$archive = new Archive($this->db);
$this->view->info = $archive->getOneArchive('黑河计划-陆地表层数据','about');
$this->view->nav[] = array('link'=>"/heihe/landsurface",'title'=>'陆地表层数据');
$this->_helper->viewRenderer('base');
}
function landuseAction()
{
$this->getmd('土地利用');
$archive = new Archive($this->db);
$this->view->info = $archive->getOneArchive('黑河计划-土地利用数据','about');
$this->view->nav[] = array('link'=>"/heihe/landuse",'title'=>'土地利用数据');
$this->_helper->viewRenderer('base');
}
function soilAction()
{
$this->getmd('土壤');
$archive = new Archive($this->db);
$this->view->info = $archive->getOneArchive('黑河计划-土壤数据','about');
$this->view->nav[] = array('link'=>"/heihe/soil",'title'=>'土壤数据');
$this->_helper->viewRenderer('base');
}
function meteoAction()
{
$this->getmd('气象');
$archive = new Archive($this->db);
$this->view->info = $archive->getOneArchive('黑河计划-气象观测数据','about');
$this->view->nav[] = array('link'=>"/heihe/meteo",'title'=>'气象观测数据');
$this->_helper->viewRenderer('base');
}
function hydroAction()
{
$this->getmd('水文观测');
$archive = new Archive($this->db);
$this->view->info = $archive->getOneArchive('黑河计划-水文观测数据','about');
$this->view->nav[] = array('link'=>"/heihe/hydro",'title'=>'水文观测数据');
$this->_helper->viewRenderer('base');
}
function irragationAction()
{
$this->getmd('灌溉');
$archive = new Archive($this->db);
$this->view->info = $archive->getOneArchive('黑河计划-灌溉数据','about');
$this->view->nav[] = array('link'=>"/heihe/irragation",'title'=>'灌溉数据');
$this->_helper->viewRenderer('base');
}
function obsAction()
{
$this->getmd('试验');
$archive = new Archive($this->db);
$this->view->info = $archive->getOneArchive('黑河计划-观测试验数据','about');
$this->view->nav[] = array('link'=>"/heihe/obs",'title'=>'观测试验数据');
$this->_helper->viewRenderer('base');
}
function listAction()
{
$sql="select uuid,title from normalmetadata where uuid in (select d.uuid from datasource d left join source s on d.sourceid=s.id where s.code='heihe') order by timebegin,title";
$this->view->metadata=$this->db->fetchAll($sql);
}
function tagAction()
{
$key = $this->_request->getParam('key');
$keytype = $this->_request->getParam('keytype');
$page=(int)$this->_request->getParam('page');
if (empty($page)) $page=1;
$offset=$this->limit*($page-1);
if (!empty($key)) {
$this->view->codename=$key;
$sql=$this->db->quoteInto("select count(k.id) from keyword k left join normalmetadata m on k.id=m.id where m.uuid in (select d.uuid from datasource d left join source s on d.sourceid=s.id where s.code='heihe') and k.keyword=?",$key);
$state=$this->db->query($sql);
$row=$state->fetchAll();
$sum=$row[0]['count'];
$sql="select uuid,title,id,description from normalmetadata where uuid in (select d.uuid from datasource d left join source s on d.sourceid=s.id where s.code='heihe') and id in (select id from keyword where keyword='".$key."') order by timebegin,title limit ? offset ?";
$this->view->metadata=$this->db->fetchAll($sql,array($this->limit,$offset));
$this->view->page=new Pagination($sum,$page,$this->limit);
} else {
$sql="select k.keyword,count(*),k.keytype from keyword k left join normalmetadata m on k.id=m.id where length(k.keyword)>0 and m.uuid in (select d.uuid from datasource d left join source s on d.sourceid=s.id where s.code='heihe') ";
if (!empty($keytype) && ($keytype=='place' || $keytype=='theme' || $keytype=='discipline'||$keytype=='temporal')) $sql.=" and k.keytype='".$keytype."'";
$sql.=' group by k.keyword,k.keytype order by k.keytype,k.keyword,count desc';
$state=$this->db->query($sql);
$this->view->keytype=$keytype;
$this->view->keywords=$state->fetchAll();
}
}
function searchAction()
{
$this->view->addHelperPath('helper','Zend_View_Helper_');
$form=new SearchForm();
$this->view->form=$form;
//$form->submit->setLabel('快速搜索');
$key=$this->_request->getParam('q');
if (!empty($key)) {
$search=new Search($key);
$where=$search->sql_expr(array("title","description"));
$page=@(int)$this->_request->getParam('page');
if (empty($page)) $page=1;
$offset=$this->limit*($page-1);
$row=$this->db->fetchAll("select count(*) from normalmetadata where uuid in (select d.uuid from datasource d left join source s on d.sourceid=s.id where s.code='heihe') and ".$where);
$sum=$row[0]['count'];
$sql="select uuid,title,id,description from normalmetadata where uuid in (select d.uuid from datasource d left join source s on d.sourceid=s.id where s.code='heihe') and ".$where." order by timebegin,title limit ? offset ?";
$this->view->metadata=$this->db->fetchAll($sql,array($this->limit,$offset));
$this->view->page=new Pagination($sum,$page,$this->limit);
$this->view->key=$key;
$this->view->offset=$offset+1;
}
}
function timemapAction()
{
$sql="select id,uuid,west,south,north,east,title,timebegin,timeend from normalmetadata where uuid in (select d.uuid from datasource d left join source s on d.sourceid=s.id where s.code='heihe') and timebegin is not null";
$this->view->rows=$this->db->fetchAll($sql);
}
function timelineAction()
{
$fn="heihetime.xml";
$rows=$this->db->fetchAll("select ts_created from normalmetadata where uuid in (select d.uuid from datasource d left join source s on d.sourceid=s.id where s.code='heihe') order by ts_created desc limit 1");
$last_update=strtotime($rows[0]['ts_created']);
if (!file_exists($fn) || (filemtime($fn)<$last_update))
{
$dateformat="M j Y";
$rows=$this->db->fetchAll("select id,uuid,description,title,timebegin,timeend from normalmetadata where uuid in (select d.uuid from datasource d left join source s on d.sourceid=s.id where s.code='heihe') and timebegin is not null order by timebegin,title");
$timexml='';
foreach($rows as $row) {
$timexml.='';
$desc_length=mb_strlen($row['description'],"UTF-8");
$desc=mb_substr($row['description'],0,($desc_length>300)?300:$desc_length,"UTF-8");
if ($desc_length>300) $desc.=" ...";
$timexml.=htmlspecialchars($desc);
$timexml.="\n";
}
$timexml.='';
$fp=fopen($fn,'w');
fwrite($fp,$timexml);
fclose($fp);
}
}
function categoryAction()
{
$code = (int)$this->_request->getParam('code');
$this->view->code = $code;
$page=(int)$this->_request->getParam('page');
if (empty($page)) $page=1;
$offset=$this->limit*($page-1);
$this->view->category=$this->db->fetchAll("select c.code,name,name_zh,count(*) from category c left join normalmetadata m on c.id=m.id,categorycode cc where c.code=cc.code and m.uuid in (select d.uuid from datasource d left join source s on d.sourceid=s.id where s.code='heihe') group by c.code,cc.name,cc.name_zh");
if ($code>0 && $code<20) {
$row=$this->db->fetchAll("select count(*) from normalmetadata where uuid in (select d.uuid from datasource d left join source s on d.sourceid=s.id where s.code='heihe') and id in (select id from category where code=".$code.")");
$sum=$row[0]['count'];
$sql="select uuid,title,description,id from normalmetadata where uuid in (select d.uuid from datasource d left join source s on d.sourceid=s.id where s.code='heihe') and id in (select distinct id from category where code=".$code.") order by timebegin,title limit ? offset ?";
$this->view->metadata=$this->db->fetchAll($sql,array($this->limit,$offset));
$this->view->page=new Pagination($sum,$page,$this->limit);
$this->view->offset=$offset+1;
$row=$this->db->fetchRow("select name,name_zh from categorycode where code=?",$code);
$this->view->codename=(empty($row['name_zh'])?$row['name']:$row['name_zh']);
} else {
//提供全部分类列表
}
}
private function getmd($keyword,$type='theme')
{
$page=(int)$this->_request->getParam('page');
if (empty($page)) $page=1;
$offset=$this->limit*($page-1);
if (!is_array($keyword))
{
$keyword=array($keyword);
}
$key="(";
foreach($keyword as $k)
{
$key.="'".$k."',";
}
$key=substr($key,0,-1);
$key.=")";
$keyword=$key;
$state=$this->db->query("select count(m.*) from normalmetadata m left join datasource d on m.uuid=d.uuid left join source s on d.sourceid=s.id where s.code='heihe' and m.id in (select id from keyword where keytype='".$type."' and keyword in ".$keyword.")");
$row=$state->fetchAll();
$sum=$row[0]['count'];
//@todo: add order with title
$sql="select uuid,title from normalmetadata where uuid in (select d.uuid from datasource d left join source s on d.sourceid=s.id where s.code='heihe') and id in (select id from keyword where keytype='".$type."' and keyword in ".$keyword.") order by timebegin,title limit ? offset ?";
$this->view->metadata=$this->db->fetchAll($sql,array($this->limit,$offset));
$this->view->page=new Pagination($sum,$page,$this->limit);
$this->view->offset=$offset+1;
}
//获取多项条件并列关系的数据并必须满足limited条件
//$keyword:字符串或字符串数组
//$limited:限制字符串
private function getmdlimited($keyword,$limited)
{
$page=(int)$this->_request->getParam('page');
if (empty($page)) $page=1;
$offset=$this->limit*($page-1);
if (!is_array($keyword))
{
$keyword=array($keyword);
}
$key="(";
foreach($keyword as $k)
{
$key.="'".$k."',";
}
$key=substr($key,0,-1);
$key.=")";
$keyword=$key;
$state=$this->db->query("select count(m.*) from normalmetadata m left join datasource d on m.uuid=d.uuid left join source s on d.sourceid=s.id where s.code='heihe' and m.id in (select id from keyword where keyword='".$limited."') and m.id in (select id from keyword where keyword in ".$keyword.")");
$row=$state->fetchAll();
$sum=$row[0]['count'];
//@todo: add order with title
$sql="select uuid,title from normalmetadata where uuid in (select d.uuid from datasource d left join source s on d.sourceid=s.id where s.code='heihe') and id in (select id from keyword where keyword='".$limited."') and id in (select id from keyword where keyword in ".$keyword.") order by timebegin,title limit ? offset ?";
$this->view->metadata=$this->db->fetchAll($sql,array($this->limit,$offset));
$this->view->page=new Pagination($sum,$page,$this->limit);
$this->view->offset=$offset+1;
}
/*
* submitAction() //数据汇交
*
* param string $ac //动作
* param int $id //数据模版ID
* param int $group //geonetwork页面跳转参数
*
* return view
*/
function submitAction(){
$ac = $this->_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($ac == "newdata")
{
$do = $this->_getParam('do');
if(empty($do))
{
$this->_helper->viewRenderer('submit-newdata');
$keywords = $this->_request->getParam('q');
$sql="select id,(regexp_matches(data,'(.*)'))[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;
return true;
}
if($do == "add")
{
$this->_helper->viewRenderer('submit-newdata-add');
$this->view->pageNav = "newdata-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;
return true;
}
return true;
}//newdata
//未提交数据
if($ac == "unsubmit" || $ac=="")
{
$do = $this->_getParam('do');
//未提交的数据列表
if(empty($do) || $do=="index")
{
$this->_helper->viewRenderer('submit-unsubmit');
$sql = "SELECT (regexp_matches(gn.data,'(.*)'))[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;
return true;
}//index
//提交
if($do=="commit")
{
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender();
$data = "";
$id = $this->_request->getParam('id');
try{
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->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'];
}
//检查元数据错误
$iso=new ISO19115();
@$iso->loadXML($row['xml']);
if ($iso->validate())
{
$data=array("error"=>"元数据导入过程中发现错误。
".implode("
",$iso->error));
$this->jsonexit($data);
return true;
}
$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']));
file_get_contents("http://ftp2.westgis.ac.cn/proftp_upload.php?uuid=".$row['uuid']."&filelist=1");
//自动添加数据分类为黑河
$sql="insert into datasource (uuid,sourceid) values(?,1)";
$sth=$this->db->prepare($sql);
try{$sth->execute(array($uuid));}catch(Exception $e) {}
// 3. 保存数据评审状态
//导入元数据
$iso->saveDB($this->db);
//进入评审库
$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,"heihe-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->jsonexit($data);
return true;
}catch(Exception $e) {
$sql="delete from mdstatus where uuid in (select uuid from geonetworkmetadata where id=?)";
$this->db->query($sql,array($id));
$sql="delete from mdauthor where userid=? and uuid in (select uuid from geonetworkmetadata where id=?)";
$this->db->query($sql,array($u_id,$id));
$msg = "提交失败,请确认权限后重试";
if($this->debug>0)
{$msg .= $e->getMessage();}
$data = array("error"=>$msg);
$this->jsonexit($data);
return true;
}
}//commit
return true;
}//unsubmit
//FTP
else if($ac == "ftp")
{
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender();
$uuid = $this->_getParam('uuid');
$this->view->uuid = $uuid;
if(empty($uuid) || !preg_match("/^[0-9A-Za-z]{8}-[0-9A-Za-z]{4}-[0-9A-Za-z]{4}-[0-9A-Za-z]{4}-[0-9A-Za-z]{12}$/",$uuid))
{
$data = array(
'error'=>"参数错误"
);
$this->jsonexit($data);
return true;
}
//安全检查: uuid必须是当前用户且为新建数据
$sql="select * from geonetworkmetadata where uuid=? and uuid not in (select uuid from metadata) and owner=?";
$sth=$this->db->prepare($sql);
$sth->execute(array($uuid,$u_id));
$row=$sth->fetch();
if (empty($row))
{
$data = array(
'error'=>"参数错误"
);
$this->jsonexit($data);
return true;
}
//ftp 用户名
$uname = 'westdc'.$u_id.'upload';
//ftp路径
$homedir = "/disk1/WestDC/upload/".$uuid."/";
//ftp用户表
$ftptable=' pureftp ';//ftp2.westgis.ac.cn
//$sql = "SELECT * FROM proftpusers WHERE userid='$uname' ORDER BY pkid DESC";
$sql = "SELECT * FROM $ftptable WHERE userid='$uname' ORDER BY pkid DESC";
$sth = $this->db->prepare($sql);
$sth->execute();
$row = $sth->fetch();
//create directory for upload
//server is not localhost, so we need a trick
//$old=umask(0);
//@mkdir($homedir,0777);
//umask($old);
$page=file_get_contents('http://ftp2.westgis.ac.cn/proftp_upload.php?uuid='.$uuid);
if (!empty($page)) die($page);//there are errors in visit ftp page
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 = 1002;
$gid = 1002;
$passwd = $this->genRandomString(16);
//$sql = "UPDATE proftpusers SET passwd=?,uid=?,gid=?,homedir=? WHERE userid=?";
//$sth = $this->db->prepare($sql);
//$rs = $sth->execute(array($passwd,$uid,$gid,$homedir,$uname));
//$sql="update proftpusers SET passwd='".$passwd."',uid=".$uid.",gid=".$gid.",homedir='".$homedir."' WHERE userid='".$uname."'";
$sql="update ".$ftptable." SET passwd='".$passwd."',uid=".$uid.",gid=".$gid.",homedir='".$homedir."' WHERE userid='".$uname."'";
$rs=$this->db->query($sql);
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 = 1002;
$gid = 1002;
$passwd = $this->genRandomString(16);
//$sql = "INSERT INTO proftpusers (userid,passwd,uid,gid,homedir) VALUES (?,?,?,?,?)";
//$sth = $this->db->prepare($sql);
//$rs = $sth->execute(array($uname,$passwd,$uid,$gid,$homedir));
//$sql="insert into proftpusers (userid,passwd,uid,gid,homedir) values('".$uname."','".$passwd."',109,1002,'".$homedir."')";
$sql="insert into ".$ftptable." (userid,passwd,uid,gid,homedir) values('".$uname."','".$passwd."',1002,1002,'".$homedir."')";
$rs=$this->db->query($sql);
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;
}
}//end if
}//ftp
}//function submitAction()
/*
* projectsAction() 数据汇交计划
*
*
*
*
*/
function projectsAction()
{
$ac = $this->_getParam('ac');
$invite = $this->_getParam('invite');
$heihe = new Heihe($this->db);
$auth = Zend_Auth::getInstance();
if($auth->hasIdentity())
{
$user = $auth->getIdentity();
$uid = $user->id;
$this->view->user_email = $user_email = $user->email;
}
$this->view->page = $this->_getParam('page');
$this->view->pageLimit = 15;
include_once("helper/view.php");
if(empty($ac) || $ac == "index" || $ac == 'my')
{
$this->_helper->viewRenderer('project-index');
$this->view->tabID='projects-index';
//Search Link
$this->view->searchLink = "/heihe/projects/ac/index/";
$q = $this->_getParam('q');
$wheresql = array();
if(!empty($q))
{
if(!preg_match("/^[\x{4e00}-\x{9fa5}A-Za-z0-9_]+$/u",$q))
{
$this->view->error = "输入的搜索条件有误,只允许输入汉字、数字、字母";
return true;
}
$wheresql[] = " (p.title LIKE '%$q%' OR
p.code LIKE '%$q%' OR
p.name LIKE '%$q%')";
$this->view->searchKeyword = $q;
}
if($ac == "my")
{
//防止通过其它方式访问,先判断是否是登录用户,如果不是,抛出消息后强制用户退出
$auth = Zend_Auth::getInstance();
if(!$auth->hasIdentity())
$this->_redirect('/account/login?href=/heihe/projects/ac/my');
$wheresql[] = " p.email='$user_email' ";
$this->view->tabID='projects-my';
}
if(count($wheresql)>0)
{
$wheresql = join(" AND ",$wheresql);
}else{
$wheresql = "";
}
if(!empty($wheresql))
{
$wheresql = " WHERE ".$wheresql;
}
$sql = "SELECT p.*,f.id as fid FROM heiheproject p left join fund f on p.code=f.fund_id $wheresql
ORDER BY p.id asc";
$sth = $this->db->query($sql);
$rows = $sth->fetchAll();
foreach ($rows as $k=>$v)
{
$rows[$k]['status'] = $heihe->getStatus($v['status']);
}
view::addPaginator($rows,$this->view,$this->_request);
}//ac == index
if($ac == "submitting")
{
$this->_helper->viewRenderer('project-index');
$this->view->tabID='projects-submitting';
$sql = "SELECT p.*,f.id as fid FROM heiheproject p left join fund f on p.code=f.fund_id WHERE p.status=1";
$sth = $this->db->query($sql);
$rows = $sth->fetchAll();
foreach ($rows as $k=>$v)
{
$rows[$k]['status'] = $heihe->getStatus($v['status']);
}
view::addPaginator($rows,$this->view,$this->_request);
}//ac == submitting 正在提交
if($ac == "unsubmit")
{
$this->_helper->viewRenderer('project-index');
$this->view->tabID='projects-unsubmit';
$sql = "SELECT p.*,f.id as fid FROM heiheproject p left join fund f on p.code=f.fund_id WHERE p.status=0";
$sth = $this->db->query($sql);
$rows = $sth->fetchAll();
foreach ($rows as $k=>$v)
{
$rows[$k]['status'] = $heihe->getStatus($v['status']);
}
view::addPaginator($rows,$this->view,$this->_request);
}//ac == unsubmit 未提交
if($ac == "submit")
{
$this->_helper->viewRenderer('project-index');
$this->view->tabID='projects-submit';
$sql = "SELECT p.*,f.id as fid FROM heiheproject p left join fund f on p.code=f.fund_id WHERE p.status>0";
$sth = $this->db->query($sql);
$rows = $sth->fetchAll();
foreach ($rows as $k=>$v)
{
$rows[$k]['status'] = $heihe->getStatus($v['status']);
}
view::addPaginator($rows,$this->view,$this->_request);
}//ac == submit 已提交
if($ac == "upload")
{
$this->_helper->layout->setLayout('layout-iframe');
$this->_helper->viewRenderer('project-upload');
$submit = $this->_getParam('submit');
$pid = $this->_getParam('pid');
if(empty($pid) || !is_numeric($pid))
{
$this->view->error = "参数错误";
return true;
}
$this->view->pid = $pid;
if(empty($uid))
{
$this->view->error = "请先登录";
return true;
}
$sql = "SELECT * FROM heiheproject WHERE id=$pid";
$sth = $this->db->query($sql);
$row = $sth->fetch();
if($row['email']!= $user_email)
{
$this->view->error = "您没有权限进行此操作";
return true;
}
if(!empty($submit) && empty($this->view->error))
{
$files=new files();
$msg = $files -> upload($this->view->config->upload,$_FILES['Filedata'],'heihe');
if(empty($msg['error']))
{
$msg['error']="";
$filename = $msg['db_path'];
$filesize = $msg['file_size'];
$filedesc = $this->_request->getParam('filedesc');
$filetype = $msg['file_type'];
$realname = $msg['realname'];
if(!empty($row['attachid']))
{
$sql = "SELECT * FROM attachments WHERE id={$row['attachid']}";
$sth = $this->db->query($sql);
$attach = $sth->fetch();
$this->db->exec("DELETE FROM attachments WHERE id={$row['attachid']} AND filetype='heihe'");
@unlink($this->view->config->upload.$attach['filename']);
}
$sql = " INSERT INTO attachments (filename,filetype,filedesc,userid,filesize,realname) values
('$filename','heihe','$filedesc','$uid','$filesize','$realname') RETURNING id";
$sth = $this->db->prepare($sql);
$sth->execute();
$att = $sth->fetch(PDO::FETCH_ASSOC);
$msg['attid'] = $attid = $att['id'];
$sql = "UPDATE heiheproject SET attachid=$attid WHERE id=$pid";
$sth = $this->db->exec($sql);
if($sth)
{
$this->view->message = "上传成功";
}else{
@unlink($filename);
$this->view->error = '附件上传失败:写入附件表出错';
return true;
}
}else{
@unlink($filename);
$this->view->error = "附件上传失败".$msg['error'];
return true;
}
}
$sql = "SELECT * FROM heiheproject WHERE id=$pid";
$sth = $this->db->query($sql);
$row = $sth->fetch();
$sql = "SELECT * FROM attachments WHERE id = {$row['attachid']}";
$sth = $this->db->query($sql);
$this->view->att = $sth->fetch();
return true;
}
if(!empty($invite))
{
$this->_helper->viewRenderer('project-invite');
$pid = $this->_getParam('pid');
$level = $heihe->expertActive($pid,$invite,$user_email);
if($level>100)
{
$this->view->error = "数据中心对您的邀请已经确认!感谢您的支持";
return true;
}else{
$this->view->error = $heihe->expertActiveError($level);
return true;
}
}//专家邀请链接
}//projectsAction()
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;
}
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;
}
//成为作者后的后继处理工作
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));
}
//基于数据作者的浏览(包括认证后的数据作者以及未认证的数据作者)
function authorAction()
{
$ac = $this->_request->getParam('ac');
$id = (int)$this->_request->getParam('id');
if ($ac=='verified') {
//已经认证过的数据作者
$this->view->tabID='author-verified';
$this->view->ac='verified';
if ($id) {
//列出作者的数据
$sql="select username,realname from users where id=?";
$this->view->author=$this->db->fetchRow($sql,array($id));
$sql="select m.* from normalmetadata m left join mdauthor a on a.uuid=m.uuid where m.uuid in (select d.uuid from datasource d left join source s on d.sourceid=s.id where s.code='heihe') and a.userid=?";
$sth = $this->db->prepare($sql);
$sth->execute(array($id));
$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 {
//已经认证过的数据作者
$sql="select u.username,u.realname,u.id,count(u.id) as count from mdauthor a left join users u on a.userid=u.id where a.uuid in (select d.uuid from datasource d left join source s on d.sourceid=s.id where s.code='heihe') and a.status=1 and a.uuid in (select uuid from normalmetadata) group by u.id,u.username,u.realname";
$sth = $this->db->prepare($sql);
$sth->execute();
$rows = $sth->fetchAll();
$paginator = Zend_Paginator::factory($rows);
$paginator->setCurrentPageNumber($this->_getParam('page'));
$paginator->setItemCountPerPage(50);
$paginator->setView($this->view);
Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination_param.phtml');
$this->view->paginator=$paginator;
}
} else if ($ac=='unverified' || empty($ac)) {
//未认证的数据作者
$this->view->tabID='author-unverified';
$this->view->ac='unverified';
if ($id) {
//列出数据
$sql="select individual as username from responsible where id=?";
$this->view->author=$this->db->fetchRow($sql,array($id));
$sql="select distinct m.* from normalmetadata m left join role r on m.uuid=r.uuid left join responsible s on r.resid=s.id where r.uuid in (select d.uuid from datasource d left join source s on d.sourceid=s.id where s.code='heihe') and r.role in ('pointOfContact','resourceProvider','owner') and s.id=?";
$sth = $this->db->prepare($sql);
$sth->execute(array($id));
$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 {
//列出所有作者
$sql="select distinct responsible.individual as username,responsible.id from responsible left join role on role.resid=responsible.id where role.uuid in (select d.uuid from datasource d left join source s on d.sourceid=s.id where s.code='heihe') and role.role in ('pointOfContact','resourceProvider','owner')";
$sth = $this->db->prepare($sql);
$sth->execute();
$rows = $sth->fetchAll();
$paginator = Zend_Paginator::factory($rows);
$paginator->setCurrentPageNumber($this->_getParam('page'));
$paginator->setItemCountPerPage(50);
$paginator->setView($this->view);
Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination_param.phtml');
$this->view->paginator=$paginator;
}
}
}
function fundAction()
{
$id = (int)$this->_request->getParam('id');
if (!empty($id)) {
$sql="select * from fund where id=?";
$this->view->fund=$this->db->fetchRow($sql,array($id));
if ($this->view->fund) {
$sql="select distinct m.* from normalmetadata m left join mdfund mf on m.uuid=mf.uuid where m.uuid in (select d.uuid from datasource d left join source s on d.sourceid=s.id where s.code='heihe') and mf.fid=?";
$sth = $this->db->prepare($sql);
$sth->execute(array($id));
$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 {
//提供全部分类列表
$sql="select f.id,f.title,f.fund_id,f.fund_type,f.ts_created,count(m.id) as datacount,sum(md.filesize) as filesize from fund f left join mdfund m on f.id=m.fid left join metadata md on m.uuid=md.uuid left join heiheproject hp on f.fund_id=hp.code where hp.id is not null and m.uuid in (select d.uuid from datasource d left join source s on d.sourceid=s.id where s.code='heihe') group by f.id,f.title,f.fund_id,f.fund_type,f.ts_created order by f.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('fund-list');
}
}
function orgAction()
{
$page = $this->_request->getParam('page');
$name = $this->_request->getParam('name');
$state=$this->db->query("select distinct responsible.organisation from responsible left join role on role.resid=responsible.id where role.uuid in (select d.uuid from datasource d left join source s on d.sourceid=s.id where s.code='heihe') and role.role in ('pointOfContact','resourceProvider','owner')");
$this->view->organisation=$state->fetchAll();
if (!empty($name)) {
$this->view->codename=$name;
$sql="select distinct m.* from normalmetadata m left join role r on m.uuid=r.uuid left join responsible s on r.resid=s.id where m.uuid in (select d.uuid from datasource d left join source s on d.sourceid=s.id where s.code='heihe') and r.role in ('pointOfContact','resourceProvider','owner') and s.organisation=?";
$sth = $this->db->prepare($sql);
$sth->execute(array($name));
$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 {
//提供全部分类列表
}
}
//实现基于DOI信息的浏览
function doiAction()
{
$ac = $this->_request->getParam('ac');
if ($ac=='unready') {
//列出没有DOI的数据
$sql="select m.* from normalmetadata m where length(m.doi)<3 and m.uuid not in (select uuid from datadoi) and m.uuid in (select ds.uuid from datasource ds left join source s on ds.sourceid=s.id where s.code='heihe') order by m.ts_created desc";
$this->view->tabID='doi-unready';
} else if ($ac=='prepare') {
//列出有DOI但还未进行提交申请的数据
$sql="select m.* from normalmetadata m where length(m.doi)>3 and m.uuid not in (select uuid from datadoi) and m.uuid in (select ds.uuid from datasource ds left join source s on ds.sourceid=s.id where s.code='heihe') order by m.ts_created desc";
$this->view->tabID='doi-prepare';
} else if ($ac=='unsubmit') {
//列出有DOI并计划提交申请的数据
$sql="select m.*,d.doi as datadoi,date(d.ts_created) as ts_created from normalmetadata m left join datadoi d on m.uuid=d.uuid where length(m.doi)>3 and d.ts_created is not null and d.ts_submitted is null and m.uuid in (select ds.uuid from datasource ds left join source s on ds.sourceid=s.id where s.code='heihe') order by d.ts_created desc";
$this->view->tabID='doi-unsubmit';
} else if ($ac=='submit') {
//列出有DOI并计划提交申请的数据
$sql="select m.*,d.doi as datadoi,date(d.ts_submitted) as ts_submitted from normalmetadata m left join datadoi d on m.uuid=d.uuid where length(m.doi)>3 and d.ts_submitted is not null and d.ts_published is null and m.uuid in (select ds.uuid from datasource ds left join source s on ds.sourceid=s.id where s.code='heihe') order by d.ts_submitted desc,d.ts_created desc";
$this->view->tabID='doi-submit';
} else if ($ac=='publish' || empty($ac)) {
//默认:列出最新发布的DOI数据
$sql="select m.*,d.doi as datadoi,date(d.ts_submitted) as ts_submitted,date(d.ts_published) as ts_published from normalmetadata m left join datadoi d on m.uuid=d.uuid where length(m.doi)>3 and d.ts_published is not null and m.uuid in (select ds.uuid from datasource ds left join source s on ds.sourceid=s.id where s.code='heihe') order by d.ts_published desc,d.ts_submitted desc,d.ts_created desc";
$this->view->tabID='doi-publish';
}
$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;
}
}