<?php
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个
}
}
?>
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个
}
}
?>