<?php
/**
* JS调用
* @author moufer<moufer@163.com>
* @copyright www.mudder.com.cn
* @version $Id js.php 2 2008-2-16 00:18 $
*/

define('SCRIPTNAV', 'JSTRANSFER');

require_once './include/common.inc.php';

$_config['jstransfer'] && is_array($_config['jstransfer']) && extract($_config['jstransfer'], EXTR_SKIP);

if(!$_config['jstransfer'] || !is_array($_config['jstransfer']) || !$enable) {
    exit("document.write(\"系统JS调用功能尚未启用\");");
}

if ($access && !in_array($_SERVER['HTTP_HOST'], explode("\r\n",$access))) {
	exit("document.write(\"{$_SERVER['HTTP_HOST']}已限制调用。\");");
}

if(isset($callid)) {

    $query = $_datacall[$callid];
    if(!$query) return;
    $file = 'datacall_'.$callid;

    $stylefile = MUDDER_ROOT.'data/templates/'.$file.'.htm';
    if(!file_exists($stylefile)) {
        if(!@$fp = fopen($stylefile, 'w')) {
            die("document.write(\"Directory './data/templates' not found or have no access!\")");
        }
        $template = datacall($callid);
        flock($fp, 2);
        fwrite($fp, $template);
        fclose($fp);
    }
    $objfile = MUDDER_ROOT.'./data/templates/1_'.$file.'.tpl.php';
    if(!file_exists($objfile) || (@filemtime($stylefile) > @filemtime($objfile))) {
        require_once MUDDER_ROOT.'./include/template.func.php';
        $ext = empty($_config['tplext']) ? '.htm' : $_config['tplext'];
        parse_template($file, 1, './data/templates', './data/templates', $ext);
    }
    include $objfile;

    $output = ob_get_contents();
    ob_end_clean();
    require_once MUDDER_ROOT.'./include/rewrite.inc.php';
    $output = str_replace('"', '\"', $output);
    $output = str_replace("\r\n", '', $output);
    $output = str_replace("\n", '', $output);
    $output = "document.write(\"$output\")";
    $_config['gzipcompress'] ? @ob_start('ob_gzhandler') : ob_start();
    echo $output;

} else {

    if($jssort != 'shop' && $jssort != 'review') {
        exit("document.write(\"未定义的调用类型。\");");
    }

    $sort = $jssort=='shop' ? (isset($sort) && $sort > 0 && $sort <= 3 ? $sort : 0) : (isset($sort) && $sort > 0 && $sort <= 3 ? $sort : 0);
    $num = isset($num) && intval(trim($num)) >= 1 ? intval(trim($num)) : 10;
    $intercept = isset($intercept) && $intercept > 0 ? intval($intercept) : 0;
    $openwindow = isset($openwindow) && $openwindow > 0 ? 1 : 0;
    $pcdname = isset($pcdname) && $pcdname > 0 ? 1 : 0;

    if(!$sort) {
        exit("document.write(\"未选择显示类型。\");");
    }

    $cachename = 'js_'.md5($jssort.$panels.$sort.$num);
    $cachefile = MUDDER_CACHEDIR.'cache_'.$cachename.'.php';

    if((@!include($cachefile)) || $timestamp - $_createtime_js > $cachelife) {

        $panels = $panels ? explode('_', $panels) : '';
        if($panels && is_array($panels)) foreach($panels as $panel) {
            $where .= ($where ? " OR " : " ")."classcode like '{$panel}__'";
        }
        if($jssort == 'shop') {
            $select = "sid AS shopid,shopname,subname,classcode,reviews";
            $from = " {$dbpre}shops ";
            $where = " status='1' ".($where ? 'AND '.$where : '');
        } else {
            $select = "a.rid as reviewid,a.content,b.shopname,b.subname,b.classcode,b.reviews";
            $from = " {$dbpre}reviews a LEFT JOIN {$dbpre}shops b ON(a.shopid=b.sid)";
            $where = " a.status='1' AND b.status='1' ".($where ? ' AND '.$where : '');
        }
        if($sort == 1) {
            $orderby = $jssort=='shop' ? " reviews DESC" : " a.posttime DESC ";
        } elseif($sort == 2) {
            $orderby = $jssort=='shop' ? " sumreview DESC" : " a.flower DESC ";
        } elseif($sort == 3) {
            $orderby = $jssort=='shop' ? " addtime DESC" : " a.respond DESC ";
        }

        $datalist = array();
        $query = $db->query("SELECT $select FROM $from WHERE $where ORDER BY $orderby LIMIT $num");
        while($result = $db->fetch_array($query)) {
            $datalist[] = $result;
        }

        if($cachelife > 0 ) {
            $writedata = "\$datalist = unserialize('".add_cs_lashes(serialize($datalist))."');\r\n\r\n";
            $writedata .= "\$_createtime_js = $timestamp;";
            file_exists($cachefile) && @unlink($cachefile);
            write_cache($cachename,$writedata);
            unset($writedata);
        }
    }

    $target = $openwindow ? ' target=\\"_blank\\"' : '';

    if($datalist) foreach($datalist as $data) {
        $data['shopname'] .= $data['subname'] ? "({$data['subname']})" : '';
        if($jssort == 'shop') {

            $title = ($pcdname ? '['.$_panels[substr($data['classcode'],0,2)].']' : '').$data['shopname'];
            $title = $intercept ? trimmed_title($title,$intercept) : $title;
            $url = $_config['siteurl'].'shop.php?shopid='.$data['shopid'];
        } elseif($jssort == 'review') {
            $title = ($pcdname ? '['.$_panels[substr($data['classcode'],0,2)].']' : '').'['.$data['shopname'].']'.html_special_chars(str_replace("\r\n",'.',$data['content']));
            $title = $intercept ? trimmed_title($title,$intercept) : $title;
            $url = $_config['siteurl'].'viewreview.php?reviewid='.$data['reviewid'];
        }
        echo "document.write(\"<a href=\\\"$url\\\"$target>$title</a><br />\");\r\n";
    }
    output();
}
?>
