Qbulider

class Qbuilder{
private  $_wapurl;    //wap網頁所在的頁面
private  $_db;        //數據庫鏈接


public function Qbuilder(){
$this->_db = Yii::app()->db;
$this->_wapurl = "";
}


/**
* 生成wap頁面
*
* @param string $telnumber  手機號
* @param string $action     動作
* @return string url返回wap頁面的訪問地址
*/
public function Gwapage($telnumber,$action){
   $wapPath = Yii::app()->params['wapPath'];
$dir_html = $_SERVER['DOCUMENT_ROOT'].$wapPath.$telnumber;
if(!file_exists($dir_html))
  mkdir($dir_html);
$fname = $telnumber."_".date('Ymd').".html";  //文件名稱
$filename = $dir_html."/".$fname;
$i = 1;
$content = '<!DOCTYPE html>';
$content .= '<html lang="en">';
$content .= "<head>";
$content .= '<meta charset="utf-8" />';
$content .= '<style type="text/css">';
$content .= 'body,dt,dd,form{margin:0;padding:0;line-height:1.2;}';
$content .= '#boxCQ dl{overflow:hidden;}';
        $content .= '#boxCQ dt{font-weight:bold;padding:5px;}';
$content .= '#boxCQ dd{float:left;margin:3px 5px;min-width:120px;padding:5px;border:1px solid gray;cursor:default;}';
$content .= '#boxCQ dd.hot{background:#f9aca4;}';
$content .= '#boxCQ{padding:10px;}';
        $content .= '#boxCQ input[type="submit"]{display:block;margin:10px auto;height:80px;padding:0 50px;font-size:30px;font-weight:bold;float:left;}';
$content .= '</style>';
$content .= "</head>";
$content .= "<body>";
$content .= "<form  id='boxCQ' method='POST' action='".$action."'>";


        $key = 'question';
        $value = Yii::app()->memcache->get($key);
        if(empty($value)){
        return  false;
        }
        else{
$arr_qinfo = json_decode($value);
//print_r($arr_qinfo);exit();
$len = count($arr_qinfo)-1;
$arr_num = $this->NoRand(0,$len,10);
$j = 0;


foreach ($arr_num as $val){
   $qinfo = $arr_qinfo[$val]->qinfo;


   $arr = explode("|",$qinfo);


$q =  $i."、".$arr[0];  //問題
$a1 = trim($arr[2]); //選項1
$a2 = trim($arr[4]);
$a3 = trim($arr[6]);
   if(!isset($arr[8])){   //拋出異常所在
    echo iconv('utf-8','gbk',$q);exit;
   }
$a4 = trim($arr[8]);
$c = base64_encode(trim($arr[9]));//正確答案




           $content .= "<dl>";
$content .= "<dt>".$q."</dt>";
$content .= "<dd>".$a1."</dd>";
$content .= "<dd>".$a2."</dd>";
$content .= "<dd>".$a3."</dd>";
$content .= "<dd>".$a4."</dd>";
$content .= "<input type='hidden' name='a".$i."' />";
$content .= "<input type='hidden' name='c".$i."' value='".$c."'>";
$content .= "</dl>";
$i++;
}






$content .= "<input type='hidden' name ='telnumber' value='".$telnumber."'>";
$content .= "<input type='submit' value='闖 關' style='width:200px;height:45px;'>";
$content .= "</form>";
$content .= <<<EOT
<script type="text/javascript" defer="defer">
void function(selector) {
var styleName = "hot", tagName = "DD";
var dls = document.querySelectorAll("#boxCQ > dl");
var dlclick = function(e) {
var tag = e.target;
var lis = this.getElementsByTagName(tagName);
var ipt = this.querySelector("input");
if (tag.nodeName == tagName) {
if (tag.getAttribute("class")) {
tag.removeAttribute("class");
ipt.value = null;
return;
}
for (var i = 0, j; j = lis[i++]; ) {
if (j == tag) {
tag.className = styleName
ipt.value = selector[i-1]
} else {
j.removeAttribute("class");
}
}
}
}
for (var i = 0, j; j = dls[i++]; ) {
j.addEventListener("click", dlclick);
}
}(["A","B","C","D"]);
</script>
EOT;
$content .= "</body>";
$content .= "</html>";


$fp = fopen($filename,'w+');
fwrite($fp,$content);
fclose($fp);
return "http://".$_SERVER['HTTP_HOST'].$wapPath.$telnumber."/".$fname;
        }


}


/**
* 刪除生成的wap文件
*
* @param unknown_type $telnumber
*/
public function delFile($telnumber){
$wapPath = Yii::app()->params['wapPath'];
$wapPath =  '/waphtml/';
$this->delDirAndFile($_SERVER['DOCUMENT_ROOT'].$wapPath.$telnumber."/");


}




//循環刪除目錄和文件函數
public function delDirAndFile( $dirName )
{
if ( $handle = opendir( "$dirName" ) ) {
  while ( false !== ( $item = readdir( $handle ) ) ) {
  if ( $item != "." && $item != ".." ) {
  if ( is_dir( "$dirName/$item" ) ) {
  delDirAndFile( "$dirName/$item" );
  } else {


if(@unlink( "$dirName/$item" ) )return "1";
  }
  }
 }
 closedir( $handle );
if( rmdir( $dirName ) ) return "1";
  }
}




/**
* 隨機產生隨機數
*
* @param int $begin 開始數目
* @param int $end   結束數目
* @param int $limit  要取的數目
* @return array
*/
function NoRand($begin=0,$end=99,$limit=10){
$rand_array=range($begin,$end);
shuffle($rand_array);//調用現成的數組隨機排列函數
return array_slice($rand_array,0,$limit);//截取前$limit個
}
}
?>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章