Example #1
0
 /**
  * 生成栏目html
  */
 protected function createCat($cat, $page = 1)
 {
     if ($cat['typeid'] == 3) {
         return false;
     }
     if ($cat['setting']['url']['use'] == 0 || $cat['setting']['url']['tohtml'] == 0 || $cat['setting']['url']['list'] == '') {
         return false;
     }
     $url = substr($this->getCaturl($cat, $page), strlen(self::get_base_url()));
     //去掉域名部分
     if (substr($url, -5) != '.html') {
         $file = 'index.html';
         //文件名
         $dir = $url;
         //目录
     } else {
         $file = basename($url);
         $dir = str_replace($file, '', $url);
     }
     $this->mkdirs($dir);
     $dir = substr($dir, -1) == '/' ? substr($dir, 0, -1) : $dir;
     $htmlfile = $dir ? $dir . '/' . $file : $file;
     ob_start();
     $catid = $cat['catid'];
     $cat = $this->cats[$catid];
     if (empty($cat)) {
         return;
     }
     $this->view->setTheme(true);
     if ($cat['typeid'] == 1) {
         //内部栏目
         $this->view->assign($cat);
         $this->view->assign(listSeo($cat, $page));
         $this->view->assign(array('page' => $page, 'catid' => $catid, 'pageurl' => urlencode($this->getCaturl($cat, '{page}'))));
         $this->view->display(substr($cat['child'] == 1 ? $cat['categorytpl'] : $cat['listtpl'], 0, -5));
     } elseif ($cat['typeid'] == 2) {
         //单网页
         $cat = $this->get_content_page($cat, 0, $page);
         $cat['content'] = relatedlink($cat['content']);
         $this->view->assign($cat);
         $this->view->assign(listSeo($cat, $page));
         $this->view->display(substr($cat['showtpl'], 0, -5));
     }
     $this->view->setTheme(false);
     if (!file_put_contents($htmlfile, ob_get_clean(), LOCK_EX)) {
         $this->adminMsg(lang('a-com-11', array('1' => $htmlfile)));
     }
     $htmlfiles = $this->cache->get('html_files');
     $htmlfiles[] = $htmlfile;
     if (empty($page) || $page == 1) {
         $onefile = str_replace('{page}', 1, substr($this->getCaturl($cat, '{page}'), strlen(self::get_base_url())));
         @copy($htmlfile, $onefile);
         $htmlfiles[] = $onefile;
     }
     $this->cache->set('html_files', $htmlfiles);
     if (strpos($cat['content'], '{-page-}') !== false) {
         $content = explode('{-page-}', $cat['content']);
         $pageid = count($content) >= $page ? $page - 1 : count($content) - 1;
         $page_id = 1;
         $pagelist = array();
         $cat['content'] = $content[$pageid];
         foreach ($content as $t) {
             $pagelist[$page_id] = getCaturl($cat, $page_id);
             $page_id++;
         }
         if (isset($pagelist[$page + 1])) {
             $this->createCat($cat, $page + 1);
         }
     }
     return true;
 }
Example #2
0
 /**
  * 内容分页
  */
 protected function get_content_page($data, $type, $page)
 {
     if (strpos($data['content'], '{-page-}') !== false) {
         //内容分页判断
         $content = explode('{-page-}', $data['content']);
         $pageid = count($content) >= $page ? $page - 1 : count($content) - 1;
         $page_id = 1;
         $pagelist = $pagename = array();
         foreach ($content as $k => $t) {
             if (preg_match('/\\[stitle\\](.*)\\[\\/stitle\\]/', $t, $stitle)) {
                 //子标题判断
                 $content[$k] = str_replace($stitle[0], '', $t);
                 $pagename[$page_id] = $stitle[1];
                 if ($k == $pageid) {
                     $data['stitle'] = $stitle[1];
                 }
             } else {
                 $pagename[$page_id] = $page_id;
             }
             $pagelist[$page_id] = $type == 0 ? getCaturl($data, $page_id) : getUrl($data, $page_id);
             $page_id++;
         }
         $data['content'] = $content[$pageid];
         $this->view->assign(array('page' => $page, 'contentname' => $pagename, 'contentpage' => $pagelist));
     }
     return $data;
 }
Example #3
0
 /**
  * 解析标签list
  */
 protected function _listdata($param)
 {
     $_param = explode(' ', $param);
     $param = array();
     foreach ($_param as $p) {
         $mark = strpos($p, '=');
         if ($p && $mark !== false) {
             $var = substr($p, 0, $mark);
             $val = substr($p, $mark + 1);
             if (isset($var) && $var) {
                 $param[$var] = $val;
             }
         }
     }
     $system = $fields = $_fields = $not = $in = $or = $between = $like = array();
     $dbcache = isset($param['cache']) ? (int) $param['cache'] : 0;
     unset($param['cache']);
     if (is_array($param)) {
         foreach ($param as $key => $val) {
             //参数归类
             if (in_array($key, array('return', 'more', 'page', 'urlrule', 'num', 'join', 'on', 'order', 'table', 'pagesize', 'pagerule', 'action', 'tag', 'extend', 'site', 'form', 'fields'))) {
                 $system[$key] = $val;
             } else {
                 if (substr($key, 0, 3) == 'NOT') {
                     $key = substr($key, 3);
                     $not[] = $key;
                 } elseif (substr($key, 0, 2) == 'OR') {
                     $key = substr($key, 2);
                     $or[] = $key;
                 } elseif (substr($key, 0, 2) == 'IN') {
                     $key = substr($key, 2);
                     $in[] = $key;
                 } elseif (substr($key, 0, 2) == 'BW') {
                     $key = substr($key, 2);
                     $between[] = $key;
                 } elseif (substr($key, 0, 4) == 'LIKE') {
                     $key = substr($key, 4);
                     $like[] = $key;
                 }
                 $fields[$key] = $val;
                 $_fields[] = $key;
             }
         }
     }
     $where = '';
     //设置站点id
     $system['site'] = !isset($system['site']) || empty($system['site']) ? App::get_site_id() : $system['site'];
     //Action判断
     if (isset($system['action']) && $system['action'] == 'position') {
         //推荐位
         $data = position($system['site'], $fields['id'], isset($fields['catid']) ? $fields['catid'] : 0, isset($system['num']) ? $system['num'] : 0);
         if ($data) {
             $db = Controller::model('content');
             foreach ($data as $i => $t) {
                 if ($t['contentid']) {
                     $row = $db->db->where('id', $t['contentid'])->get('content_' . $system['site'])->row_array();
                     $data[$i] = $t + $row;
                 }
             }
         }
         if (isset($system['return']) && $system['return'] && $system['return'] != 't') {
             return array('return_' . $system['return'] => $data, 'total_' . $system['return'] => count($data));
         }
         return array('return' => $data, 'total' => count($data));
     } elseif (isset($system['action']) && $system['action'] == 'keywords') {
         //搜索关键字
         $search = Controller::model('search');
         $num = $system['num'] ? (int) $system['num'] : 5;
         if (isset($system['order']) && $system['order']) {
             $order = null;
             $orders = explode(',', $system['order']);
             foreach ($orders as $t) {
                 list($_field, $_order) = explode('_', $t);
                 if (in_array($_field, array('id', 'keywords', 'addtime', 'total'))) {
                     $_orderby = isset($_order) && strtoupper($_order) == 'ASC' ? 'ASC' : 'DESC';
                     $order .= '`' . $_field . '` ' . $_orderby . ',';
                 }
             }
             if (substr($order, -1) == ',') {
                 $order = substr($order, 0, -1);
             }
         } else {
             $order = '`total` DESC';
         }
         $data = $search->execute('select distinct keywords as title from ' . $search->prefix . 'search order by ' . $order . ' limit ' . $num, true, $dbcache);
         if (isset($system['return']) && $system['return'] && $system['return'] != 't') {
             return array('return_' . $system['return'] => $data, 'total_' . $system['return'] => count($data));
         }
         return array('return' => $data, 'total' => count($data));
     } elseif (isset($system['action']) && $system['action'] == 'sql') {
         //
     } elseif (isset($system['action']) && $system['action'] == 'relation') {
         if (isset($system['tag']) && $system['tag']) {
             //按关键字搜索
             if (isset($fields['id']) && $fields['id']) {
                 $where .= '`id`<>' . (int) $fields['id'];
             }
             $tags = @explode(',', $system['tag']);
             $kwhere = $k = NULL;
             foreach ($tags as $tag) {
                 if ($tag) {
                     if (empty($k)) {
                         $kwhere .= '`title` like "%' . $tag . '%"';
                     } else {
                         $kwhere .= ' OR `title` like "%' . $tag . '%"';
                     }
                     $k = 1;
                 }
             }
             if ($kwhere) {
                 $where .= ' AND (' . $kwhere . ')';
             }
             unset($k, $tags, $tag, $kwhere, $system['table'], $fields['id']);
         } else {
             //手动设置的相关文章
             $data = $this->relation($fields['id'], $system['num']);
             if (isset($system['return']) && $system['return'] && $system['return'] != 't') {
                 return array('return_' . $system['return'] => $data, 'total_' . $system['return'] => count($data));
             }
             return array('return' => $data, 'total' => count($data));
         }
     } elseif (isset($system['action']) && $system['action'] == 'field') {
         //字段信息
         $mods = get_model_data();
         $mod = $mods[$fields['modelid']];
         $data = array();
         if ($mod['fields']) {
             foreach ($mod['fields']['data'] as $t) {
                 if ($fields['name'] == $t['field']) {
                     //加载字段配置文件
                     App::auto_load('fields');
                     $data_fields = '';
                     $data_fields .= '<tr id="fine_' . $t['field'] . '">';
                     $data_fields .= '<th>' . (isset($t['not_null']) && $t['not_null'] ? '<font color="red">*</font> ' : '') . $t['name'] . ':</th>';
                     $data_fields .= '<td>';
                     $func = 'content_' . $t['formtype'];
                     //防止出错,把字段内容转换成数组格式
                     $content = array($fields['value']);
                     $content = var_export($content, true);
                     $field_config = var_export($t, true);
                     if (function_exists($func)) {
                         eval("\$data_fields .= " . $func . "(" . $t['field'] . ", " . $content . ", " . $field_config . ");");
                     }
                     $data_fields .= $t['tips'] ? '<div class="onShow">' . $t['tips'] . '</div>' : '';
                     $data_fields .= '<span id="ck_' . $t['field'] . '"></span>';
                     $data_fields .= '</td>';
                     $data_fields .= '</tr>';
                     $data[0]['form'] = $data_fields;
                     if ($t['setting']) {
                         $c = string2array($t['setting']);
                         if ($c['content']) {
                             $select = explode(PHP_EOL, $c['content']);
                             $vdata = array();
                             foreach ($select as $i => $c) {
                                 list($n, $v) = explode('|', $c);
                                 $vdata[trim($n)] = $v === null ? trim($n) : trim($v);
                             }
                             $data[0]['data'] = $vdata;
                         }
                     }
                 }
             }
         }
         if (isset($system['return']) && $system['return'] && $system['return'] != 't') {
             return array('return_' . $system['return'] => $data);
         }
         return array('return' => $data);
     }
     //主表判断
     if (isset($system['table']) && $system['table']) {
         $table = $system['table'];
     } elseif (isset($system['form']) && $system['form']) {
         $table = 'form_' . $system['site'] . '_' . $system['form'];
     } else {
         $table = 'content';
     }
     //加载Model实例
     if (strpos($table, '.') !== false) {
         list($plugin, $table) = explode('.', $table);
         $db = App::plugin_model($plugin, $table);
     } else {
         $db = Controller::model('content');
         if (strpos($table, 'content') === 0) {
             $table .= '_' . $system['site'];
         }
     }
     $table = $db->prefix . $table;
     $db->table_name = $table;
     $table_join = $table_data = $table_fields = $table_join_fields = $table_data_fields = $arrchilds = null;
     $_table_fields = $db->get_table_fields();
     $table_fields = array_intersect($_fields, $_table_fields);
     //status判断
     if (in_array('status', $_table_fields)) {
         $where .= ($where ? ' AND ' : ' ') . ' `' . $table . '`.`status`=1';
     }
     if (isset($fields['catid']) && $fields['catid']) {
         //栏目信息
         $cats = get_category_data($system['site']);
         $cat = $cats[$fields['catid']];
     }
     if (isset($system['join']) && $system['join'] && $system['on']) {
         //JOIN联合查询
         $table_join = $system['join'];
         //加载Model实例
         if (strpos($table_join, '.') !== false) {
             list($plugin, $table_join) = explode('.', $table_join);
             $db_join = App::plugin_model($plugin, $table_join);
         } else {
             $db_join = Controller::model($table_join);
         }
         $_table_join_fields = $db_join->get_table_fields();
         $table_join_fields = array_intersect($_fields, $_table_join_fields);
         foreach ($table_join_fields as $k => $c) {
             if (in_array($c, $table_fields)) {
                 unset($table_join_fields[$k]);
             }
         }
         $table_join = $db->prefix . $table_join;
     }
     if (isset($system['more']) && $system['more']) {
         //附表
         $model = null;
         if ($table == $db->prefix . 'content_' . $system['site']) {
             $models = get_model_data('content', $system['site']);
             if (isset($fields['catid']) && $fields['catid'] && isset($cat) && $cat) {
                 $model = $models[$cat['modelid']];
             } elseif (isset($fields['modelid']) && $fields['modelid']) {
                 $model = $models[$fields['modelid']];
             }
         } elseif ($table == $db->prefix . 'member' && isset($fields['modelid']) && $fields['modelid']) {
             $cache = new cache_file();
             $models = $cache->get('model_member');
             $model = $models[$fields['modelid']];
         }
         if ($model) {
             $table_data = $model['tablename'];
             $db_data = Controller::model($table_data);
             $_table_data_fields = $db_data->get_table_fields();
             $table_data_fields = array_intersect($_fields, $_table_data_fields);
             foreach ($table_data_fields as $k => $c) {
                 if (in_array($c, $table_fields)) {
                     unset($table_data_fields[$k]);
                 }
             }
             $table_data = $db->prefix . $table_data;
         }
     }
     //WHERE整合
     $fieldsAll = array($table => $table_fields, $table_join => $table_join_fields, $table_data => $table_data_fields);
     foreach ($fieldsAll as $_table => $t) {
         if (is_array($t)) {
             foreach ($t as $f) {
                 if ($fields[$f] == '') {
                     continue;
                 }
                 $and_or = in_array($f, $or) ? 'OR' : 'AND';
                 //栏目条件根据子栏目来做为条件
                 if ($f == 'catid' && isset($fields['catid']) && $fields['catid']) {
                     if (isset($cat) && $cat && $cat['child']) {
                         $arrchilds = $cat['arrchilds'];
                         $not_in = in_array($f, $not) ? 'NOT IN' : 'IN';
                         $where .= ' ' . $and_or . ' `' . $_table . '`.`catid` ' . $not_in . ' (' . $arrchilds . ')';
                     } elseif (strpos($fields['catid'], ',') !== false) {
                         $not_in = in_array($f, $not) ? 'NOT IN' : 'IN';
                         $where .= ' ' . $and_or . ' `' . $_table . '`.`catid` ' . $not_in . ' (' . $fields['catid'] . ')';
                     } else {
                         $not_in = in_array($f, $not) ? '<>' : '=';
                         $where .= ' ' . $and_or . ' `' . $_table . '`.`catid`' . $not_in . $fields['catid'];
                     }
                 } elseif ($f == 'thumb' && isset($fields['thumb']) && is_numeric($fields['thumb'])) {
                     $where .= $fields['thumb'] ? ' ' . $and_or . ' `' . $_table . '`.`thumb`<>""' : ' ' . $and_or . ' `' . $_table . '`.`thumb`=""';
                 } else {
                     $not_in = in_array($f, $in) ? 'IN' : '';
                     $not_in = in_array($f, $not) ? 'NOT IN' : $not_in;
                     if (in_array($f, $between)) {
                         if (strpos($fields[$f], '_') !== false) {
                             list($v1, $v2) = explode('_', $fields[$f]);
                             $v1 = is_numeric($v1) ? $v1 : '"' . addslashes($v1) . '"';
                             $v2 = is_numeric($v2) ? $v2 : '"' . addslashes($v2) . '"';
                             $where .= ' ' . $and_or . ' `' . $_table . '`.`' . $f . '` BETWEEN ' . $v1 . ' AND ' . $v2;
                         } elseif (strpos($fields[$f], ',') !== false) {
                             list($v1, $v2) = explode(',', $fields[$f]);
                             $v1 = is_numeric($v1) ? $v1 : '"' . addslashes($v1) . '"';
                             $v2 = is_numeric($v2) ? $v2 : '"' . addslashes($v2) . '"';
                             $where .= ' ' . $and_or . ' `' . $_table . '`.`' . $f . '` BETWEEN ' . $v1 . ' AND ' . $v2;
                         } else {
                             continue;
                         }
                     } elseif ($not_in) {
                         $where .= ' ' . $and_or . ' `' . $_table . '`.`' . $f . '` ' . $not_in . ' (' . $fields[$f] . ')';
                     } elseif (in_array($f, $like)) {
                         $value = addslashes($fields[$f]);
                         $where .= ' ' . $and_or . ' `' . $_table . '`.`' . $f . '` LIKE "' . $value . '"';
                     } else {
                         $value = is_numeric($fields[$f]) ? $fields[$f] : '"' . addslashes($fields[$f]) . '"';
                         $where .= ' ' . $and_or . ' `' . $_table . '`.`' . $f . '`=' . $value . '';
                     }
                 }
             }
         }
     }
     if ($where) {
         if (substr($where, 0, 4) == ' AND') {
             $where = ' WHERE' . substr($where, 4);
         } elseif (substr($where, 0, 3) == ' OR') {
             $where = ' WHERE' . substr($where, 3);
         } else {
             $where = ' WHERE' . $where;
         }
         //对WHERE延展
         if (isset($system['extend']) && substr($system['extend'], 0, 6) == 'WHERE.') {
             $where .= ' ' . str_replace('-', ' ', substr($system['extend'], 6));
             unset($system['extend']);
         }
     }
     //延展list
     $extend = '';
     if (isset($system['extend']) && $system['extend']) {
         $extend = ' ' . str_replace('-', ' ', $system['extend']);
     }
     //FROM整合
     $from = 'FROM ' . $table;
     if ($table_data) {
         $from .= ' LEFT JOIN ' . $table_data . ' ON `' . $table . '`.`' . $db->get_primary_key() . '`=`' . $table_data . '`.`' . $db_data->get_primary_key() . '`';
     }
     if ($table_join && $system['on']) {
         $_join_name = null;
         if (in_array($system['on'], $_table_fields)) {
             $_join_name = $table;
         } elseif (isset($_table_data_fields) && in_array($system['on'], $_table_data_fields)) {
             $_join_name = $table_data;
         }
         if ($_join_name) {
             $from .= ' LEFT JOIN ' . $table_join . ' ON `' . $table_join . '`.`' . $db_join->get_primary_key() . '`=`' . $_join_name . '`.`' . $system['on'] . '`';
         }
     }
     //ORDER排序
     $order = '';
     if (isset($system['order']) && $system['order']) {
         if (strtoupper($system['order']) == 'RAND()') {
             $order .= ' ORDER BY RAND()';
         } else {
             $orders = explode(',', $system['order']);
             foreach ($orders as $t) {
                 list($_field, $_order) = explode('_', $t);
                 $_name = null;
                 if (in_array($_field, $_table_fields)) {
                     $_name = $table;
                 } elseif (isset($_table_data_fields) && in_array($_field, $_table_data_fields)) {
                     $_name = $table_data;
                 } elseif (isset($_table_join_fields) && in_array($_field, $_table_join_fields)) {
                     $_name = $table_join;
                 }
                 $_orderby = isset($_order) && strtoupper($_order) == 'ASC' ? 'ASC' : 'DESC';
                 if ($_name) {
                     $order .= ' `' . $_name . '`.`' . $_field . '` ' . $_orderby . ',';
                 }
             }
             if (substr($order, -1) == ',') {
                 $order = ' ORDER BY' . substr($order, 0, -1);
             }
         }
     }
     //limit与分页
     $limit = '';
     if (isset($system['num']) && $system['num']) {
         $limit = ' LIMIT ' . $system['num'];
     } elseif (isset($system['page'])) {
         $pageurl = '';
         $system['page'] = (int) $system['page'] ? (int) $system['page'] : 1;
         if ($system['urlrule']) {
             $pageurl = str_replace(array('_page_', '[page]'), '{page}', $system['urlrule']);
             $pagesize = $system['pagesize'] ? $system['pagesize'] : (isset($cat['pagesize']) ? $cat['pagesize'] : 10);
         } elseif ($cat) {
             $pageurl = getCaturl($cat, '{page}');
             $pagesize = $system['pagesize'] ? $system['pagesize'] : $cat['pagesize'];
         } else {
             $pagesize = $system['pagesize'] ? $system['pagesize'] : 10;
             $pageurl = '{page}';
         }
         $sql = 'SELECT count(*) AS total ' . $from . ' ' . $where;
         $count = $db->execute($sql, false, $dbcache);
         $total = $count['total'];
         $pagelist = Controller::instance('pagelist');
         $pagelist->loadconfig($system['pagerule'] ? $system['pagerule'] : 'pagerule');
         $start_id = $pagesize * ($system['page'] - 1);
         $limit = ' LIMIT ' . $start_id . ',' . $pagesize;
         $pagelist = $pagelist->total($total)->url($pageurl)->num($pagesize)->page($system['page'])->output();
     }
     //查询字段筛选
     if (isset($system['fields']) && $system['fields']) {
         $fields_array = explode(',', $system['fields']);
         //字段参数转为数组
         $select_array = array();
         foreach ($fields_array as $t) {
             //遍历字段参数数组,筛选无用字段、字段附加表前缀
             if (in_array($t, $_table_fields)) {
                 //主表字段
                 $select_array[] = '`' . $table . '`.`' . $t . '`';
             } elseif (isset($_table_data_fields) && in_array($t, $_table_data_fields)) {
                 //附表字段
                 $select_array[] = '`' . $table_data . '`.`' . $t . '`';
             } elseif (isset($_table_join_fields) && in_array($t, $_table_join_fields)) {
                 //联合表字段
                 $select_array[] = '`' . $table_join . '`.`' . $t . '`';
             }
         }
         $select_field = empty($select_array) ? ' * ' : ' ' . implode(',', $select_array) . ' ';
         //组合成sql查询格式
         unset($select_array, $fields_array);
     } else {
         $select_field = ' * ';
     }
     //查询结果
     $sql = 'SELECT' . $select_field . $from . $where . $order . $limit . $extend;
     $data = $db->execute($sql, true, $dbcache);
     //释放变量
     unset($_param, $param, $par, $p, $fields, $_fields, $not, $in, $or, $between, $dbcache, $like);
     unset($table, $db, $table_join, $table_data, $table_fields, $table_join_fields, $table_data_fields, $arrchilds, $_table_fields);
     unset($fieldsAll, $_table_data_fields, $cache, $db_join, $cats, $cat, $models, $model, $db_data, $where, $order, $from);
     if (isset($system['return']) && $system['return'] && $system['return'] != 't') {
         return array('sql_' . $system['return'] => $sql, 'total_' . $system['return'] => isset($total) ? $total : count($data), 'return_' . $system['return'] => $data, 'pagelist_' . $system['return'] => $pagelist);
     }
     return array('pagelist' => $pagelist, 'return' => $data, 'sql' => $sql, 'total' => isset($total) ? $total : count($data));
 }