<?php

class plusMod extends commonMod
{
    public function __construct()
    {
        parent::__construct();
    }
    
    // 增强模块

    //远程抓图
    public function index($content)
    {
        $content=html_out($content);
        $ext = 'gif|jpg|jpeg|bmp|png';
        preg_match_all("/(href|src)=([\"|']?)([^ \"'>]+\.($ext))\\2/i",$content, $matches); 
        $url='http://'.$_SERVER['HTTP_HOST'].__APP__.'/upload/index';

        foreach ($matches[3] as $key) {

            /*
            if( class_exists('SaeStorage') ) {
                $saetor = new SaeStorage();
                $imgurl=strstr($key,@$saetor->getUrl('public'));
                if($imgurl){
                    $arry[]=$key;
                }else{
                    $data['file']=$key;
                    $imgfile=Http::saePost($url,$data,10);
                    $imgdir=explode('|',$imgfile);
                    if($imgdir){
                    $arry[]=$imgdir[0];
                    }else{
                    $arry[]=$key;
                    }
                }


            }else{
            }

            */

              if(substr($key,0,4)=='http'){
                $data['file']=$key;
                $imgfile=Http::saePost($url,$data,10);
                $imgdir=explode('|',$imgfile);
                if($imgdir){
                   $arry[]=$imgdir[0]; 
                }else{
                   $arry[]=$key;
                }

              }

              if(substr($key,0,4)=='file'){
               $url='http://'.$_SERVER['HTTP_HOST'].__APP__.'/upload/index';
               $key=str_replace('file:///', '@', $key);

               $data['file']=$key;
                $dir=Http::curlPost($url,$data,10);
                $imgdir=explode('|',$dir);
                if($imgdir){

                   $arry[]=$imgdir[0]; 
                }else{
                   $arry[]=$key;
                }
                           
            }  //针对sae无效，哥放弃了 擦 那就只针对非sae了

            

        }
       return str_replace($matches[3],$arry,$content);     
        
    }

    //获取第一张图片
    public function onepic($content)
    {
        $ext = 'gif|jpg|jpeg|bmp|png';
        preg_match("/(href|src)=([\"|']?)([^ \"'>]+\.($ext))\\2/i",html_out($content), $matches); 
        return $matches[3];
    }

    //获取keyword
    public function getkeyword($title=null,$description=null)
    {
        error_reporting(0);
        header("content-type:text/html; charset=utf-8");
        if(empty($title)){
        $content=in($_POST['title']).in($_POST['description']);
        }else{
        $content=in($title).in($description);
        }
        $segment = new segment();
        $key=$segment->get_keyword(iconv('utf-8','gbk',$content));
        $tag=iconv('gbk','utf-8',str_replace(" ", ",", $key));
        if(empty($title)){
        echo $tag;
        }else{
        return $tag;
        }
    }

    //栏目转拼音过滤
    public function pinyin($content)
    {
        $py = new Pinyin();
        $content = in(deletehtml($content));
        $title_pinyin = preg_replace("/[^\x{4e00}-\x{9fa5}\w]/u",'',$content);
        $data = substr($py->output($title_pinyin, $utf8 = true),0,50);
        return $this->ifpinyin($data);
        
    }

    //栏目转拼音重复处理
    public function ifpinyin($data,$post=null)
    {
        $data = in(preg_replace("/[^\w]/u",'',$data));

        if(!empty($post)){

        $conditioninfo['pinyin'] = intval($_POST);
        $info = $this->model->table('channel')->where($conditioninfo)->find();

        $conditioncun['pinyin']=$data;
        $info2 = $this->model->table('channel')->where($conditioncun)->find(); 

        if ($info['pinyin']<>$data['pinyin'] && $info2['pinyin']==$data['pinyin']) {
           return $data.$this->getcode();//如果名称重复随机生成
        }else{
           return $data;
        }

        }else{

        $condition['pinyin']=$data;
        $clannel_info = $this->model->field('id,pinyin')->table('channel')->where($condition)->find();
        if ($clannel_info['pinyin'] == $data) {
        return $data.$this->getcode();//如果名称重复随机生成
        }else{
        return $data;
        }

        }

        
    }

    //随机生成英文+数字
    public function getcode ($length = 7, $mode = 0){
     switch ($mode) {
     default:
     $str = '1234567890abcdefghijklmnopgrstuvwxyz';
     break;
     }

     $result = '';
     $l = strlen($str)-1;
     $num=0;

     for($i = 0;$i < $length;$i ++){
        $num = rand(0, $l);
        $a=$str[$num];
        $result =$result.$a;
     }
     return $result;
     }

    //添加tag
    public function addtag($keywords,$id)
    {
       $str = $keywords;
            $str = str_replace('，',',',$str);
            $str = str_replace(' ',',',$str);
            $strArray = explode(",", $str);
            foreach ($strArray as $list) {
                $condition5['tag'] = $list;
                $tags2 = $this->model->table('content_tag')->where($condition5)->find();
                if (!empty($tags2)) {
                    $data5 = array();
                    $data5['cite'] = $tags2['cite'] + 1; //引用数+1
                    if (!empty($tags2['aid'])) {
                        $data5['aid'] = $tags2['aid'] . ',' . $id;
                    } else {
                        $data5['aid'] = $id;
                    }
                    $this->model->table('content_tag')->data($data5)->where($condition5)->update();
                } else {
                    $data6 = array();
                    $data6['tag'] = $list;
                    $data6['aid'] = $id;
                    $this->model->table('content_tag')->data($data6)->insert();
            }
        }
        
    }

    //编辑tag
    public function edittag($keywords,$id)
    {
       $str = $keywords;
            $str = str_replace('，',',',$str);
            $str = str_replace(' ',',',$str);
            $strArray = explode(",", $str);
            foreach ($strArray as $list) {
                $condition5['tag'] = $list;
                $tags2 = $this->model->table('content_tag')->where($condition5)->find();
                if (!empty($tags2)) {
                    $data5 = array();
                    $data5['cite'] = $tags2['cite'] + 1; //引用数+1
                    if (!empty($tags2['aid'])) {
                        if (in_array($id, explode(',', $tags2['aid']))) //如果该文章已经存在
                            {
                        } else {
                            $data5['aid'] = $tags2['aid'] . ',' . $id;
                        }
                    } else {
                        $data5['aid'] = $id;
                    }
                    $this->model->table('content_tag')->data($data5)->where($condition5)->update();
                } else {
                    $data6 = array();
                    $data6['tag'] = $list;
                    $data6['aid'] = $id;
                    $this->model->table('content_tag')->data($data6)->insert();
            }

        }
        
    }

    //扩展模型字段录入
    public function addplus($post_info,$id)
    {

            $my_condition['id'] = intval($post_info['my_id']);
            $my_info = $this->model->table('plus_model')->where($my_condition)->find(); //查询所属模型

            $my_list_condition['pid'] = intval($my_info['id']);
            $my_list = $this->model->table('plus_model')->where($my_list_condition)->order('id desc')->
                select(); //列出模型字段
            foreach ($my_list as $my_vo) {
                if (!empty($my_vo['field'])) {

                    switch ($my_vo['type']) {
                        case 1:        
                        $my_data[$my_vo['field']] = in($post_info[$my_vo['field']]); //单行
                        break;
                        case 2:        
                        $my_data[$my_vo['field']] = in($post_info[$my_vo['field']]); //多行
                        break;
                        case 3:        
                        $my_data[$my_vo['field']] = html_in($post_info[$my_vo['field']]); //编辑器
                        break;
                        case 4:        
                        $my_data[$my_vo['field']] = in($post_info[$my_vo['field']]); //下拉菜单
                        break;
                        case 5:        
                        $my_data[$my_vo['field']] = in($post_info[$my_vo['field']]); //上传
                        break;
                        case 6:        
                        $my_data[$my_vo['field']] = intval($post_info[$my_vo['field']]); //数字
                        break;
                        case 7:        
                        $my_data[$my_vo['field']] = in($post_info[$my_vo['field']]); //货币
                        break;
                        case 8:        
                        $my_data[$my_vo['field']] = strtotime(in($post_info[$my_vo['field']])); //时间
                        break;
                    }
                     
                }
            }
            $my_data['my_aid'] = $id;
            $this->model->table($my_info['table'])->data($my_data)->insert(); //插入

    }


    //扩展模型字段修改
    public function editplus($post_info)
    {

            $my_condition['id'] = intval($post_info['my_id']);
            $my_info = $this->model->table('plus_model')->where($my_condition)->find(); //查询所属模型

            $my_list_condition['pid'] = intval($my_info['id']);
            $my_list = $this->model->table('plus_model')->where($my_list_condition)->order('id desc')->
                select(); //列出模型字段
            foreach ($my_list as $my_vo) {
                if (!empty($my_vo['field'])) {
                    switch ($my_vo['type']) {
                        case 1:        
                        $my_data[$my_vo['field']] = in($post_info[$my_vo['field']]); //单行
                        break;
                        case 2:        
                        $my_data[$my_vo['field']] = in($post_info[$my_vo['field']]); //多行
                        break;
                        case 3:        
                        $my_data[$my_vo['field']] = html_in($post_info[$my_vo['field']]); //编辑器
                        break;
                        case 4:        
                        $my_data[$my_vo['field']] = in($post_info[$my_vo['field']]); //下拉菜单
                        break;
                        case 5:        
                        $my_data[$my_vo['field']] = in($post_info[$my_vo['field']]); //上传
                        break;
                        case 6:        
                        $my_data[$my_vo['field']] = intval($post_info[$my_vo['field']]); //数字
                        break;
                        case 7:        
                        $my_data[$my_vo['field']] = in($post_info[$my_vo['field']]); //货币
                        break;
                        case 8:        
                        $my_data[$my_vo['field']] = strtotime(in($post_info[$my_vo['field']])); //时间
                        break;
                    }
                }
            }

            $my_data_condition['my_aid'] = $post_info['id'];
            if ($this->model->table($my_info['table'])->where($my_data_condition)->count()>0) {
                $this->model->table($my_info['table'])->data($my_data)->where($my_data_condition)->update(); //修改
            } else {
                $my_data['my_aid'] = $post_info['id'];
                $this->model->table($my_info['table'])->data($my_data)->insert(); //插入
            }

    }

    //获取扩展字段
    public function ext_field()
    {
        $info = $this->model->table('channel')->field('id,ext_mid')->where('id=' . $_POST['cid'])->find();
        $ext_mid = $info['ext_mid']; //取得字段
        if ($ext_mid == 0) //判断是否为字段
            {
            echo json_encode('');
            exit;
        }
        $condition['pid'] = $ext_mid;
        $model_list = $this->model->table('plus_model')->where($condition)->select();
        echo json_encode($model_list); //输出

    }

    //删除扩展字段
    public function del_ext_field($mid,$id)
    {
        $my_condition['id'] = $mid;
        $my_info = $this->model->table('plus_model')->where($my_condition)->find(); //查询所属模型
        $ext_condition['my_aid'] = $id;
        $this->model->table($my_info['table'])->where($ext_condition)->delete();

    }

    //获取html文件列表
    public function dirhtml($dir)
    {
    $dir_data = array();
    if (! @is_dir($dir) )
    {
        die("目录不存在 ($dir)");
    }
    if (! $dir_handle = @opendir($dir) )
    {
        die("无法打开目录 ($dir)");
    }
    while ( $file = @readdir($dir_handle) )
    {
        if ( @filetype($dir . $file) !== '' && $file !== '.' )
        {
            if( @filetype($dir . $file) == 'dir' && $file !== '..' )
            {
                $dir_data['folders'][$file] = $file;
            }
            else
            {
             $file_sub=end(explode('.', $file));
             if ($file !== '..' && $file_sub=='html' )
             {
                $dir_data['files'][$file] = $file;
             }
            }
        }
    }
    return $dir_data;
    }


    //获取有权限的栏目
    public function auth_class()
    {
        $condition['id']=$_SESSION[$this->config['spot'] . 'admin_gid'];
        $group=$this->model->table('admin_group')->where($condition)->find();
        $auth= explode(',', $group['power_value']);
        foreach ( $auth as $auth_value) {
            $auth_condition['id'] = $auth_value;
            $auth_info = $this->model->table('resource')->field('id,cid')->where($auth_condition)->find(); 
            if(!empty($auth_info['cid'])){
            $auth_cid.=$auth_info['cid'].',';
            }
        }
        return substr($auth_cid,0,-1) ;
    }


    //获取目录大小
    public function dirsize($dir)
    {

      return @$this->getRealSize($this->getDirSize($dir));

    }

    //获取目录大小
    public function getDirSize($dir)  
        {  
            if(!is_dir($dir)){
                return '0';
                exit;
            }

            $handle = opendir($dir);
            while (false!==($FolderOrFile = readdir($handle)))  
            {  
                if($FolderOrFile != "." && $FolderOrFile != "..")  
                {  
                    if(is_dir("$dir/$FolderOrFile"))  
                    {  
                        $sizeResult += $this->getDirSize("$dir/$FolderOrFile");  
                    }  
                    else  
                    {  
                        $sizeResult += filesize("$dir/$FolderOrFile");  
                    }  
                }     
            }  
  
            closedir($handle);  
            return $sizeResult;  
     } 

     //单位自动转换函数  
    public function getRealSize($size)  
    {  
        $kb = 1024;          //Kilobyte  
        $mb = 1024 * $kb;    //Megabyte  
        $gb = 1024 * $mb;    //Gigabyte  
        $tb = 1024 * $gb;    //Terabyte  
             
        if($size < $kb)  
        {  
            return $size." B";  
        }  
        else if($size < $mb)  
        {  
            return round($size/$kb,2)." KB";  
        }  
        else if($size < $gb)  
        {  
            return round($size/$mb,2)." MB";  
        }  
        else if($size < $tb)  
        {  
            return round($size/$gb,2)." GB";  
        }  
        else  
        {  
            return round($size/$tb,2)." TB";  
        }  
    }      


	
}

?>