Example #1
0
	function request() {
		//ajaxリクエスト以外
		if( !$this->RequestHandler->isAjax() ) {
			$this->set("result" , array("result" => "not ajax"));
			return;
		}

		//パラメータ解析
		$u_userid=$this->params['form']['u'];
		$u_id=$this->params['form']['id'];
		if ( $u_userid == "" or $u_id == "" ){
			$this->set("result" , array("result" => "parameter error"));
			return;
		}

		//ディレクトリ
		if ( CommonComponent::MakeDirectory($u_userid) != 0 ){
			$this->set("result" , array("result" => "create directory error"));
			return;
		}

		//SQL文取得
		$u_query="";
		$hql_file=DIR_REQUEST."/${u_userid}/${u_id}.hql";
		if ( !($fp=fopen($hql_file,"r")) ){
			$this->set("result" , array("result" => "file open error"));
			return;
		}
		while(!feof($fp)){
			$data = fgets($fp, 512);
			$u_query.=$data;
		}
		fclose($fp);

		//SQLファイルを書き換える(select文の前にselect文のコメントを挿入)
		if ( !($fp=fopen($hql_file,"w")) ){
			$this->set("result" , array("result" => "file open error", "id" => "$u_id"));
			return;
		}
		$arr=preg_split("/;/",$u_query);
		for ($i=0; $i<count($arr); $i++){
			$arr[$i]=str_replace(array("\r\n","\n","\r","\t"), ' ', $arr[$i]);
			$arr[$i]=ltrim($arr[$i]);
			if ( $arr[$i] == "" ){ continue; }
			if ( eregi('^--',$arr[$i]) ){
				$ret=fputs($fp,"$arr[$i]\n");
			}else{
				if ( eregi('^select',$arr[$i]) ){
					$ret=fputs($fp,"--$arr[$i]\n");
				}
				$ret=fputs($fp,"$arr[$i];\n");
			}
		}
		fclose($fp);

		//クエリの実行制限チェック
		list($res,$hive_database)=CommonComponent::HiveBefore($u_userid,$u_query);	
		if ( $res != 0 ){
			$this->set("result" , array("result" => "許可されていないクエリです"));
			return;
		}

		//同時実行数制限
		$run_cnt=CommonComponent::GetQueryExecuteNum();	
		//$this->log("CNT=$run_cnt",LOG_DEBUG);
		if ( $run_cnt >= WEBHIVE_MAX_REQUEST ){
			$this->set("result" , array("result" => "クエリ実行数が制限を超えました。しばらくたってから再実行してください"));
			return;
		}

		//クエリ実行履歴出力
		$runlog['Runhists']['username']=$u_userid;
		$runlog['Runhists']['hive_database']=$hive_database;
		$runlog['Runhists']['query']=$u_query;
		$runlog['Runhists']['rid']=$u_id;
		$runlog['Runhists']['rsts']=0;
		if ( !($this->Runhists->save($runlog, array('username','hive_database','query','rid','rsts') )) ){
			$this->set("result" , array("result" => "db access error"));
			return;
		}
		$runlog['Runhists']['id'] = $this->Runhists->getLastInsertID();

		//クエリ監査ログ出力
		CommonComponent::QueryAuditLogWrite($u_userid,$u_query);

		//HiveQLのバックグラウンド実行
		$cmd=CMD_PHP . " " . CMD_HIVE_SHELL . " $u_userid $u_id"; 
		$this->log("CMD=$cmd",LOG_DEBUG);
		exec("$cmd > /dev/null 2>&1 &",$result,$retval);
		$this->log("CMD=$cmd => $retval",LOG_DEBUG);
		$this->set("result" , array("result" => "ok", "id" => "$u_id"));
	}