查看: 10034|回复: 1

discuz缓存应用详解.

[复制链接]
发表于 2009-12-16 16:11:34 | 显示全部楼层 |阅读模式
台州网址导航
example.php 测试文件解释

<?php

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

require_once './include/cache.func.php';



//参数说明:  缓存标识名, 内置数据取得标识, 缓存数据(string), 缓存前缀.

//writetocache('文件名', $cachenames, $cachedata = '', $prefix = 'cache_')



// 第一种模式. 指针转成变量,写入到test.php当中, 目录在forundata/cache/

writetocache('test','',getcachevars(array('var'=>'变量值','phps'=>'discuz.net')), $prefix = 'caches_');



//第二种模式,这种比较好, 生成一个数组, 写在文件test2.php中.

writetocache('test2', '', '$_DCACHE[\'settings\'] = '.arrayeval(range(1,20)).";\n\n", $prefix = 'caches_');



//第三种模式,$cachedata内容是什么, 就写入是什么, 很强悍.

writetocache('test3', '',"array('var1'=>'mysql php','var2'=>'fenanr')", $prefix = 'caches_');



//第四种模式,当没有$prefix值时, 默认生成cache_xxxx.php的缓存命名.

writetocache('test4', '',"array('var1'=>'php 6','var2'=>'discuz')");

?>
复制代码总结:
   为了支撑高压力及访问量下的程序动作正常, discuz的缓存结构写得非常复杂. 当然, 不同的人思考方式不相同.
   这次用了几个常用例子讲解缓存系统的应用, 希望可以帮助到需要的朋友.
   由于限制了字节, 所以函数解释放二楼.
台州维博网络(www.tzweb.com)专门运用PHP+MYSQL/ASP.NET+MSSQL技术开发网站门户平台系统等。
 楼主| 发表于 2009-12-16 16:12:08 | 显示全部楼层
台州网址导航
cache.func.php文件详解


&lt;?php



/*

        [Discuz!] (C)2001-2009 Comsenz Inc.

        This is NOT a freeware, use is subject to license terms



        $Id: cache.func.php 21311 2009-11-26 01:35:43Z liulanbo $

*/



define('DISCUZ_KERNEL_VERSION', '7.2');

define('DISCUZ_KERNEL_RELEASE', '20091126');





function updatecache($cachename = '') {

        //分别引入 mysql操作库存,论坛名称,数据库前缀,最大论坛时间(估计是授权用户专用)

        global $db, $bbname, $tablepre, $maxbdays;

        //静态化一下数组,比如$cachename = setings     就读到这个数组  'settings'        =&gt; array('settings'),

        static $cachescript = array

                (



                'settings'        =&gt; array('settings'),

                'forums'        =&gt; array('forums'),

                'icons'                =&gt; array('icons'),

                'stamps'        =&gt; array('stamps'),

                'ranks'                =&gt; array('ranks'),

                'usergroups'        =&gt; array('usergroups'),

                'request'        =&gt; array('request'),

                'medals'        =&gt; array('medals'),

                'magics'        =&gt; array('magics'),

                'topicadmin'        =&gt; array('modreasons', 'stamptypeid'),

                'archiver'        =&gt; array('advs_archiver'),

                'register'        =&gt; array('advs_register', 'ipctrl'),

                'faqs'                =&gt; array('faqs'),

                'secqaa'        =&gt; array('secqaa'),

                'censor'        =&gt; array('censor'),

                'ipbanned'        =&gt; array('ipbanned'),

                'smilies'        =&gt; array('smilies_js'),

                'forumstick' =&gt; array('forumstick'),



                'index'                =&gt; array('announcements', 'onlinelist', 'forumlinks', 'advs_index', 'heats'),

                'forumdisplay'        =&gt; array('smilies', 'announcements_forum', 'globalstick', 'forums', 'icons', 'onlinelist', 'advs_forumdisplay', 'forumstick'),

                'viewthread'        =&gt; array('smilies', 'smileytypes', 'forums', 'usergroups', 'ranks', 'stamps', 'bbcodes', 'smilies', 'advs_viewthread', 'tags_viewthread', 'custominfo', 'groupicon', 'focus', 'stamps'),

                'post'                =&gt; array('bbcodes_display', 'bbcodes', 'smileycodes', 'smilies', 'smileytypes', 'icons', 'domainwhitelist'),

                'profilefields'        =&gt; array('fields_required', 'fields_optional'),

                'viewpro'        =&gt; array('fields_required', 'fields_optional', 'custominfo'),

                'bbcodes'        =&gt; array('bbcodes', 'smilies', 'smileytypes'),

                );

        //当最大时间有值时,就将在$cachescript 增加两段

        if($maxbdays) {

                $cachescript['birthdays'] = array('birthdays');

                $cachescript['index'][]   = 'birthdays_index';

        }

        // 组成更新数组.

        $updatelist = empty($cachename) ? array_values($cachescript) : (is_array($cachename) ? array('0' =&gt; $cachename) : array(array('0' =&gt; $cachename)));

        $updated = array();

        // 现在循环. 由于是二维数组, 所以循环两次.

        foreach($updatelist as $value) {

                foreach($value as $cname) {

                        //判断如果$updated数组为假,或者$updated中没有$cname的值,就进入, 目的是为了防止重复

                        if(empty($updated) || !in_array($cname, $updated)) {

                                $updated[] = $cname;  //进来一次, 就丢进数组, 以便循环中再次使用.

                                getcachearray($cname);   // 取得相应的值,并且生成缓存.

                        }

                }

        }

        

        // 假如是空参数进入, 就对所有的缓存标识作判断.

        foreach($cachescript as $script =&gt; $cachenames) {

                if(empty($cachename) || (!is_array($cachename) &amp;&amp; in_array($cachename, $cachenames)) || (is_array($cachename) &amp;&amp; array_intersect($cachename, $cachenames))) {

                        $cachedata = '';

                        $query = $db-&gt;query(&quot;SELECT data FROM {$tablepre}caches WHERE cachename in(&quot;.implodeids($cachenames).&quot;)&quot;);

                        while($data = $db-&gt;fetch_array($query)) {

                                $cachedata .= $data['data'];

                        }

                        writetocache($script, $cachenames, $cachedata);

                }

        }

        

        //假如参数为空,或者参数为styles 就处理模板风格等缓存

        if(!$cachename || $cachename == 'styles') {

                $stylevars = $styledata = $styleicons = array();

                $defaultstyleid = $db-&gt;result_first(&quot;SELECT value FROM {$tablepre}settings WHERE variable = 'styleid'&quot;);

                list(, $imagemaxwidth) = explode(&quot;\t&quot;, $db-&gt;result_first(&quot;SELECT value FROM {$tablepre}settings WHERE variable = 'zoomstatus'&quot;));

                $imagemaxwidth = $imagemaxwidth ? $imagemaxwidth : 600;

                $imagemaxwidthint = intval($imagemaxwidth);

                $query = $db-&gt;query(&quot;SELECT sv.* FROM {$tablepre}stylevars sv LEFT JOIN {$tablepre}styles s ON s.styleid = sv.styleid AND (s.available=1 OR s.styleid='$defaultstyleid')&quot;);

                while($var = $db-&gt;fetch_array($query)) {

                        $stylevars[$var['styleid']][$var['variable']] = $var['substitute'];

                }

                $query = $db-&gt;query(&quot;SELECT s.*, t.directory AS tpldir FROM {$tablepre}styles s LEFT JOIN {$tablepre}templates t ON s.templateid=t.templateid WHERE s.available=1 OR s.styleid='$defaultstyleid'&quot;);

                while($data = $db-&gt;fetch_array($query)) {

                        $data = array_merge($data, $stylevars[$data['styleid']]);

                        $datanew = array();

                        $data['imgdir'] = $data['imgdir'] ? $data['imgdir'] : 'images/default';

                        $data['styleimgdir'] = $data['styleimgdir'] ? $data['styleimgdir'] : $data['imgdir'];

                        foreach($data as $k =&gt; $v) {

                                if(substr($k, -7, 7) == 'bgcolor') {

                                        $newkey = substr($k, 0, -7).'bgcode';

                                        $datanew[$newkey] = setcssbackground($data, $k);

                                }

                        }

                        $data = array_merge($data, $datanew);

                        $styleicons[$data['styleid']] = $data['menuhover'];

                        if(strstr($data['boardimg'], ',')) {

                                $flash = explode(&quot;,&quot;, $data['boardimg']);

                                $flash[0] = trim($flash[0]);

                                $flash[0] = preg_match('/^http:\/\//i', $flash[0]) ? $flash[0] : $data['styleimgdir'].'/'.$flash[0];

                                $data['boardlogo'] = &quot;&lt;embed src=\&quot;&quot;.$flash[0].&quot;\&quot; width=\&quot;&quot;.trim($flash[1]).&quot;\&quot; height=\&quot;&quot;.trim($flash[2]).&quot;\&quot; type=\&quot;application/x-shockwave-flash\&quot; wmode=\&quot;transparent\&quot;&gt;&lt;/embed&gt;&quot;;

                        } else {

                                $data['boardimg'] = preg_match('/^http:\/\//i', $data['boardimg']) ? $data['boardimg'] : $data['styleimgdir'].'/'.$data['boardimg'];

                                $data['boardlogo'] = &quot;&lt;img src=\&quot;$data[boardimg]\&quot; alt=\&quot;$bbname\&quot; border=\&quot;0\&quot; /&gt;&quot;;

                        }

                        $data['bold'] = $data['nobold'] ? 'normal' : 'bold';

                        $contentwidthint = intval($data['contentwidth']);

                        $contentwidthint = $contentwidthint ? $contentwidthint : 600;

                        if(substr(trim($data['contentwidth']), -1, 1) != '%') {

                                if(substr(trim($imagemaxwidth), -1, 1) != '%') {

                                        $data['imagemaxwidth'] = $imagemaxwidthint &gt; $contentwidthint ? $contentwidthint : $imagemaxwidthint;

                                } else {

                                        $data['imagemaxwidth'] = intval($contentwidthint * $imagemaxwidthint / 100);

                                }

                        } else {

                                if(substr(trim($imagemaxwidth), -1, 1) != '%') {

                                        $data['imagemaxwidth'] = '%'.$imagemaxwidthint;

                                } else {

                                        $data['imagemaxwidth'] = ($imagemaxwidthint &gt; $contentwidthint ? $contentwidthint : $imagemaxwidthint).'%';

                                }

                        }

                        $data['verhash'] = random(3);

                        $styledata[] = $data;

                }

                foreach($styledata as $data) {

                        $data['styleicons'] = $styleicons;

                        writetocache($data['styleid'], '', getcachevars($data, 'CONST'), 'style_');

                        writetocsscache($data);

                }

        }

        //假如参数为空,或者参数为usergroups 就处理用户组等缓存

        if(!$cachename || $cachename == 'usergroups') {

                @include_once DISCUZ_ROOT.'forumdata/cache/cache_settings.php';

                $threadplugins = !isset($_DCACHE['settings']) ? $GLOBALS['threadplugins'] : $_DCACHE['settings'];

                $allowthreadplugin = $threadplugins ? unserialize($db-&gt;result_first(&quot;SELECT value FROM {$tablepre}settings WHERE variable='allowthreadplugin'&quot;)) : array();



                $query = $db-&gt;query(&quot;SELECT * FROM {$tablepre}usergroups u

                                        LEFT JOIN {$tablepre}admingroups a ON u.groupid=a.admingid&quot;);

                while($data = $db-&gt;fetch_array($query)) {

                        $ratearray = array();

                        if($data['raterange']) {

                                foreach(explode(&quot;\n&quot;, $data['raterange']) as $rating) {

                                        $rating = explode(&quot;\t&quot;, $rating);

                                        $ratearray[$rating[0]] = array('min' =&gt; $rating[1], 'max' =&gt; $rating[2], 'mrpd' =&gt; $rating[3]);

                                }

                        }

                        $data['raterange'] = $ratearray;

                        $data['grouptitle'] = $data['color'] ? '&lt;font color=&quot;'.$data['color'].'&quot;&gt;'.$data['grouptitle'].'&lt;/font&gt;' : $data['grouptitle'];

                        $data['grouptype'] = $data['type'];

                        $data['grouppublic'] = $data['system'] != 'private';

                        $data['groupcreditshigher'] = $data['creditshigher'];

                        $data['groupcreditslower'] = $data['creditslower'];

                        $data['allowthreadplugin'] = $threadplugins ? $allowthreadplugin[$data['groupid']] : array();

                        unset($data['type'], $data['system'], $data['creditshigher'], $data['creditslower'], $data['color'], $data['groupavatar'], $data['admingid']);

                        writetocache($data['groupid'], '', getcachevars($data), 'usergroup_');

                }

        }



        //假如参数为空,或者参数为admingroups 就处理管理用户组等缓存

        if(!$cachename || $cachename == 'admingroups') {

                $query = $db-&gt;query(&quot;SELECT * FROM {$tablepre}admingroups&quot;);

                while($data = $db-&gt;fetch_array($query)) {

                        writetocache($data['admingid'], '', getcachevars($data), 'admingroup_');

                }

        }



        if(!$cachename || $cachename == 'plugins') {

                $query = $db-&gt;query(&quot;SELECT pluginid, available, adminid, name, identifier, datatables, directory, copyright, modules FROM {$tablepre}plugins&quot;);

                while($plugin = $db-&gt;fetch_array($query)) {

                        $data = array_merge($plugin, array('modules' =&gt; array()), array('vars' =&gt; array()));

                        $plugin['modules'] = unserialize($plugin['modules']);

                        if(is_array($plugin['modules'])) {

                                foreach($plugin['modules'] as $module) {

                                        $data['modules'][$module['name']] = $module;

                                }

                        }

                        $queryvars = $db-&gt;query(&quot;SELECT variable, value FROM {$tablepre}pluginvars WHERE pluginid='$plugin[pluginid]'&quot;);

                        while($var = $db-&gt;fetch_array($queryvars)) {

                                $data['vars'][$var['variable']] = $var['value'];

                        }

                        writetocache($plugin['identifier'], '', &quot;\$_DPLUGIN['$plugin[identifier]'] = &quot;.arrayeval($data), 'plugin_');

                }

        }

        //假如参数为空,或者参数为threadsorts 就处理主题信息等缓存

        if(!$cachename || $cachename == 'threadsorts') {

                $sortlist = $templatedata = array();

                $query = $db-&gt;query(&quot;SELECT t.typeid AS sortid, tt.optionid, tt.title, tt.type, tt.unit, tt.rules, tt.identifier, tt.description, tv.required, tv.unchangeable, tv.search, tv.subjectshow

                        FROM {$tablepre}threadtypes t

                        LEFT JOIN {$tablepre}typevars tv ON t.typeid=tv.sortid

                        LEFT JOIN {$tablepre}typeoptions tt ON tv.optionid=tt.optionid

                        WHERE t.special='1' AND tv.available='1'

                        ORDER BY tv.displayorder&quot;);

                while($data = $db-&gt;fetch_array($query)) {

                        $data['rules'] = unserialize($data['rules']);

                        $sortid = $data['sortid'];

                        $optionid = $data['optionid'];

                        $sortlist[$sortid][$optionid] = array(

                                'title' =&gt; dhtmlspecialchars($data['title']),

                                'type' =&gt; dhtmlspecialchars($data['type']),

                                'unit' =&gt; dhtmlspecialchars($data['unit']),

                                'identifier' =&gt; dhtmlspecialchars($data['identifier']),

                                'description' =&gt; dhtmlspecialchars($data['description']),

                                'required' =&gt; intval($data['required']),

                                'unchangeable' =&gt; intval($data['unchangeable']),

                                'search' =&gt; intval($data['search']),

                                'subjectshow' =&gt; intval($data['subjectshow']),

                                );



                        if(in_array($data['type'], array('select', 'checkbox', 'radio'))) {

                                if($data['rules']['choices']) {

                                        $choices = array();

                                        foreach(explode(&quot;\n&quot;, $data['rules']['choices']) as $item) {

                                                list($index, $choice) = explode('=', $item);

                                                $choices[trim($index)] = trim($choice);

                                        }

                                        $sortlist[$sortid][$optionid]['choices'] = $choices;

                                } else {

                                        $typelist[$sortid][$optionid]['choices'] = array();

                                }

                        } elseif(in_array($data['type'], array('text', 'textarea'))) {

                                $sortlist[$sortid][$optionid]['maxlength'] = intval($data['rules']['maxlength']);

                        } elseif($data['type'] == 'image') {

                                $sortlist[$sortid][$optionid]['maxwidth'] = intval($data['rules']['maxwidth']);

                                $sortlist[$sortid][$optionid]['maxheight'] = intval($data['rules']['maxheight']);

                        } elseif($data['type'] == 'number') {

                                $sortlist[$sortid][$optionid]['maxnum'] = intval($data['rules']['maxnum']);

                                $sortlist[$sortid][$optionid]['minnum'] = intval($data['rules']['minnum']);

                        }

                }

                $query = $db-&gt;query(&quot;SELECT typeid, description, template, stemplate FROM {$tablepre}threadtypes WHERE special='1'&quot;);

                while($data = $db-&gt;fetch_array($query)) {

                        $templatedata[$data['typeid']] = $data['template'];

                        $stemplatedata[$data['typeid']] = $data['stemplate'];

                        $threaddesc[$data['typeid']] = dhtmlspecialchars($data['description']);

                }



                foreach($sortlist as $sortid =&gt; $option) {

                        writetocache($sortid, '', &quot;\$_DTYPE = &quot;.arrayeval($option).&quot;;\n\n\$_DTYPETEMPLATE = \&quot;&quot;.str_replace('&quot;', '\&quot;', $templatedata[$sortid]).&quot;\&quot;;\n\n\$_DSTYPETEMPLATE = \&quot;&quot;.str_replace('&quot;', '\&quot;', $stemplatedata[$sortid]).&quot;\&quot;;\n&quot;, 'threadsort_');

                }

        }



}



// 这是css缓存文件生成需要的处理css背景颜色函数

function setcssbackground(&amp;$data, $code) {

        $codes = explode(' ', $data[$code]);

        $css = $codevalue = '';

        for($i = 0; $i &lt; count($codes); $i++) {

                if($i &lt; 2) {

                        if($codes[$i] != '') {

                                if($codes[$i]{0} == '#') {

                                        $css .= strtoupper($codes[$i]).' ';

                                        $codevalue = strtoupper($codes[$i]);

                                } elseif(preg_match('/^http:\/\//i', $codes[$i])) {

                                        $css .= 'url(\&quot;'.$codes[$i].'\&quot;) ';

                                } else {

                                        $css .= 'url(&quot;'.$data['styleimgdir'].'/'.$codes[$i].'&quot;) ';

                                }

                        }

                } else {

                        $css .= $codes[$i].' ';

                }

        }

        $data[$code] = $codevalue;

        $css = trim($css);

        return $css ? 'background: '.$css : '';

}

// 更新系统配置缓存

function updatesettings() {

        global $_DCACHE;

        if(isset($_DCACHE['settings']) &amp;&amp; is_array($_DCACHE['settings'])) {

                writetocache('settings', '', '$_DCACHE[\'settings\'] = '.arrayeval($_DCACHE['settings']).&quot;;\n\n&quot;);

        }

}



// 写入缓存文件, 详解一下.

function writetocache($script, $cachenames, $cachedata = '', $prefix = 'cache_') {

        global $authkey;

        //假如$cachenames是数组,并且$cachedata为假

        if(is_array($cachenames) &amp;&amp; !$cachedata) {

                foreach($cachenames as $name) {

                        //赋予内置函数的指定标识, 即可有数据返回

                        $cachedata .= getcachearray($name, $script);

                }

        }

        $dir = DISCUZ_ROOT.'./forumdata/cache/';

        //如果缓存目录不存在, 就生成一个.

        if(!is_dir($dir)) {

                @mkdir($dir, 0777);

        }

        if($fp = @fopen(&quot;$dir$prefix$script.php&quot;, 'wb')) {

                fwrite($fp, &quot;&lt;?php\n//Discuz! cache file, DO NOT modify me!&quot;.

                        &quot;\n//Created: &quot;.date(&quot;M j, Y, G:i&quot;).

                        &quot;\n//Identify: &quot;.md5($prefix.$script.'.php'.$cachedata.$authkey).&quot;\n\n$cachedata?&gt;&quot;);

                fclose($fp);

        } else {

                exit('Can not write to cache files, please check directory ./forumdata/ and ./forumdata/cache/ .');

        }

}

// 更新css缓存生成过程中, 里面的路径问题, 参数为css.htm的内容.

function writetocsscache($data) {

        $cssdata = '';

        foreach(array('_common' =&gt; array('css_common', 'css_append'),

                        '_special' =&gt; array('css_special', 'css_special_append'),

                        '_wysiwyg' =&gt; array('css_wysiwyg', '_wysiwyg_append'),

                        '_seditor' =&gt; array('css_seditor', 'css_seditor_append'),

                        '_calendar' =&gt; array('css_calendar', 'css_calendar_append'),

                        '_moderator' =&gt; array('css_moderator', 'css_moderator_append'),

                        '_script' =&gt; array('css_script', 'css_script_append'),

                        '_task_newbie' =&gt; array('css_task_newbie', 'css_task_newbie_append')

                ) as $extra =&gt; $cssfiles) {

                $cssdata = '';

                foreach($cssfiles as $css) {

                        $cssfile = DISCUZ_ROOT.'./'.$data['tpldir'].'/'.$css.'.htm';

                        !file_exists($cssfile) &amp;&amp; $cssfile = DISCUZ_ROOT.'./templates/default/'.$css.'.htm';

                        if(file_exists($cssfile)) {

                                $fp = fopen($cssfile, 'r');

                                $cssdata .= @fread($fp, filesize($cssfile)).&quot;\n\n&quot;;

                                fclose($fp);

                        }

                }

                $cssdata = preg_replace(&quot;/\{([A-Z0-9]+)\}/e&quot;, '\$data[strtolower(\'\1\')]', $cssdata);

                $cssdata = preg_replace(&quot;/&lt;\?.+?\?&gt;\s*/&quot;, '', $cssdata);

                $cssdata = !preg_match('/^http:\/\//i', $data['styleimgdir']) ? str_replace(&quot;url(\&quot;$data[styleimgdir]&quot;, &quot;url(\&quot;../../$data[styleimgdir]&quot;, $cssdata) : $cssdata;

                $cssdata = !preg_match('/^http:\/\//i', $data['styleimgdir']) ? str_replace(&quot;url($data[styleimgdir]&quot;, &quot;url(../../$data[styleimgdir]&quot;, $cssdata) : $cssdata;

                $cssdata = !preg_match('/^http:\/\//i', $data['imgdir']) ? str_replace(&quot;url(\&quot;$data[imgdir]&quot;, &quot;url(\&quot;../../$data[imgdir]&quot;, $cssdata) : $cssdata;

                $cssdata = !preg_match('/^http:\/\//i', $data['imgdir']) ? str_replace(&quot;url($data[imgdir]&quot;, &quot;url(../../$data[imgdir]&quot;, $cssdata) : $cssdata;

                if($extra != '_script') {

                        $cssdata = preg_replace(array('/\s*([,;:\{\}])\s*/', '/[\t\n\r]/', '/\/\*.+?\*\//'), array('\\1', '',''), $cssdata);

                }

                if(@$fp = fopen(DISCUZ_ROOT.'./forumdata/cache/style_'.$data['styleid'].$extra.'.css', 'w')) {

                        fwrite($fp, $cssdata);

                        fclose($fp);

                } else {

                        exit('Can not write to cache files, please check directory ./forumdata/ and ./forumdata/cache/ .');

                }

        }



}



// 将js文件复制到缓存目录.

function writetojscache() {

        $dir = DISCUZ_ROOT.'include/js/';

        $dh = opendir($dir);

        $remove = array(

                '/(^|\r|\n)\/\*.+?(\r|\n)\*\/(\r|\n)/is',

                '/\/\/note.+?(\r|\n)/i',

                '/\/\/debug.+?(\r|\n)/i',

                '/(^|\r|\n)(\s|\t)+/',

                '/(\r|\n)/',

        );

        while(($entry = readdir($dh)) !== false) {

                if(fileext($entry) == 'js') {

                        $jsfile = $dir.$entry;

                        $fp = fopen($jsfile, 'r');

                        $jsdata = @fread($fp, filesize($jsfile));

                        fclose($fp);

                        $jsdata = preg_replace($remove, '', $jsdata);

                        if(@$fp = fopen(DISCUZ_ROOT.'./forumdata/cache/'.$entry, 'w')) {

                                fwrite($fp, $jsdata);

                                fclose($fp);

                        } else {

                                exit('Can not write to cache files, please check directory ./forumdata/ and ./forumdata/cache/ .');

                        }

                }

        }

}



//取得缓存数组, 内置.  标识名,及文件名.

function getcachearray($cachename, $script = '') {

        global $db, $timestamp, $tablepre, $timeoffset, $maxbdays, $smcols, $smrows, $charset, $scriptlang;

        //省略上千代码.

}



//组成缓存文件需要的数据数组.

function getcachevars($data, $type = 'VAR') {

        $evaluate = '';

        foreach($data as $key =&gt; $val) {

                if(!preg_match(&quot;/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/&quot;, $key)) {

                        continue;

                }

                if(is_array($val)) {

                        $evaluate .= &quot;\$$key = &quot;.arrayeval($val).&quot;;\n&quot;;

                } else {

                        $val = addcslashes($val, '\'\\');

                        $evaluate .= $type == 'VAR' ? &quot;\$$key = '$val';\n&quot; : &quot;define('&quot;.strtoupper($key).&quot;', '$val');\n&quot;;

                }

        }

        return $evaluate;

}



//取得广告设置的内容数组.

function advertisement($range) {

        global $db, $tablepre, $timestamp;



        $advs = array();

        $query = $db-&gt;query(&quot;SELECT * FROM {$tablepre}advertisements WHERE available&gt;'0' AND starttime&lt;='$timestamp' ORDER BY displayorder&quot;);

        if($db-&gt;num_rows($query)) {

                while($adv = $db-&gt;fetch_array($query)) {

                        if(in_array($adv['type'], array('footerbanner', 'thread'))) {

                                $parameters = unserialize($adv['parameters']);

                                $position = isset($parameters['position']) &amp;&amp; in_array($parameters['position'], array(2, 3)) ? $parameters['position'] : 1;

                                $type = $adv['type'].$position;

                        } else {

                                $type = $adv['type'];

                        }

                        $adv['targets'] = in_array($adv['targets'], array('', 'all')) ? ($type == 'text' ? 'forum' : (substr($type, 0, 6) == 'thread' ? 'forum' : 'all')) : $adv['targets'];

                        foreach(explode(&quot;\t&quot;, $adv['targets']) as $target) {

                                if($range == 'index' &amp;&amp; substr($target, 0, 3) == 'gid') {

                                        $advs['cat'][$type][substr($target, 3)][] = $adv['advid'];

                                        $advs['items'][$adv['advid']] = $adv['code'];

                                }

                                $target = $target == '0' || $type == 'intercat' ? 'index' : (in_array($target, array('all', 'index', 'forumdisplay', 'viewthread', 'register', 'redirect', 'archiver')) ? $target : ($target == 'forum' ? 'forum_all' : 'forum_'.$target));

                                if((($range == 'forumdisplay' &amp;&amp; !in_array($adv['type'], array('thread', 'interthread'))) || $range == 'viewthread') &amp;&amp;  substr($target, 0, 6) == 'forum_') {

                                        if($adv['type'] == 'thread') {

                                                foreach(isset($parameters['displayorder']) ? explode(&quot;\t&quot;, $parameters['displayorder']) : array('0') as $postcount) {

                                                        $advs['type'][$type.'_'.$postcount][$target][] = $adv['advid'];

                                                }

                                        } else {

                                                $advs['type'][$type][$target][] = $adv['advid'];

                                        }

                                        $advs['items'][$adv['advid']] = $adv['code'];

                                } elseif($range == 'all' &amp;&amp; in_array($target, array('all', 'redirect'))) {

                                        $advs[$target]['type'][$type][] = $adv['advid'];

                                        $advs[$target]['items'][$adv['advid']] = $adv['code'];

                                } elseif($range == 'index' &amp;&amp; $type == 'intercat') {

                                        $parameters = unserialize($adv['parameters']);

                                        foreach(is_array($parameters['position']) ? $parameters['position'] : array('0') as $position) {

                                                $advs['type'][$type][$position][] = $adv['advid'];

                                                $advs['items'][$adv['advid']] = $adv['code'];

                                        }

                                } elseif($target == $range || ($range == 'index' &amp;&amp; $target == 'forum_all' &amp;&amp; $type == 'text')) {

                                        $advs['type'][$type][] = $adv['advid'];

                                        $advs['items'][$adv['advid']] = $adv['code'];

                                }

                        }

                }

        }



        return $advs;

}



// 简单判断.

function pluginmodulecmp($a, $b) {

        return $a['displayorder'] &gt; $b['displayorder'] ? 1 : -1;

}

// 计算长宽的函数

function smthumb($size, $smthumb = 50) {

        if($size[0] &lt;= $smthumb &amp;&amp; $size[1] &lt;= $smthumb) {

                return array('w' =&gt; $size[0], 'h' =&gt; $size[1]);

        }

        $sm = array();

        $x_ratio = $smthumb / $size[0];

        $y_ratio = $smthumb / $size[1];

        if(($x_ratio * $size[1]) &lt; $smthumb) {

                $sm['h'] = ceil($x_ratio * $size[1]);

                $sm['w'] = $smthumb;

        } else {

                $sm['w'] = ceil($y_ratio * $size[0]);

                $sm['h'] = $smthumb;

        }

        return $sm;

}



// 处理缓存生成时部分内容样式的解析

function parsehighlight($highlight) {

        if($highlight) {

                $colorarray = array('', 'red', 'orange', 'yellow', 'green', 'cyan', 'blue', 'purple', 'gray');

                $string = sprintf('%02d', $highlight);

                $stylestr = sprintf('%03b', $string[0]);



                $style = ' style=&quot;';

                $style .= $stylestr[0] ? 'font-weight: bold;' : '';

                $style .= $stylestr[1] ? 'font-style: italic;' : '';

                $style .= $stylestr[2] ? 'text-decoration: underline;' : '';

                $style .= $string[1] ? 'color: '.$colorarray[$string[1]] : '';

                $style .= '&quot;';

        } else {

                $style = '';

        }

        return $style;

}

//  这就是传说的array的立体型输出.

function arrayeval($array, $level = 0) {

        if(!is_array($array)) {

                return &quot;'&quot;.$array.&quot;'&quot;;

        }

        if(is_array($array) &amp;&amp; function_exists('var_export')) {

                return var_export($array, true);

        }



        $space = '';

        for($i = 0; $i &lt;= $level; $i++) {

                $space .= &quot;\t&quot;;

        }

        $evaluate = &quot;Array\n$space(\n&quot;;

        $comma = $space;

        if(is_array($array)) {

                foreach($array as $key =&gt; $val) {

                        $key = is_string($key) ? '\''.addcslashes($key, '\'\\').'\'' : $key;

                        $val = !is_array($val) &amp;&amp; (!preg_match(&quot;/^\-?[1-9]\d*$/&quot;, $val) || strlen($val) &gt; 12) ? '\''.addcslashes($val, '\'\\').'\'' : $val;

                        if(is_array($val)) {

                                $evaluate .= &quot;$comma$key =&gt; &quot;.arrayeval($val, $level + 1);

                        } else {

                                $evaluate .= &quot;$comma$key =&gt; $val&quot;;

                        }

                        $comma = &quot;,\n$space&quot;;

                }

        }

        $evaluate .= &quot;\n$space)&quot;;

        return $evaluate;

}



?&gt;
台州维博网络(www.tzweb.com)专门运用PHP+MYSQL/ASP.NET+MSSQL技术开发网站门户平台系统等。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

网站推广
关于我们
  • 台州朗动科技(Tzweb.com)拥有多年开发网站平台系统门户手机客户端等业务的成功经验。主要从事:政企网站,系统平台,微信公众号,各类小程序,手机APP客户端,浙里办微应用,浙政钉微应用、主机域名、虚拟空间、后期维护等服务,满足不同企业公司的需求,是台州地区领先的网络技术服务商!

Hi,扫描关注我

Copyright © 2005-2026 站长论坛 All rights reserved

Powered by 站长论坛 with TZWEB Update Techonolgy Support

快速回复 返回顶部 返回列表