XPressME Integration Kit

Trac

source: trunk/xpressme_integration_kit/include/xpress_block_render.php @ 668

Last change on this file since 668 was 657, checked in by toemon, 14 years ago

cube2.2よりcacheディレクトリがtrust_path側へ移ったことへの対応 Fixes#373

File size: 11.4 KB
RevLine 
[44]1<?php
[172]2if(!defined('XPRESS_BLOCK_RENDER_FUNCTION_READ')){
3        define('XPRESS_BLOCK_RENDER_FUNCTION_READ',1);
[44]4        require_once dirname( __FILE__ ) .'/xml.php' ;
5        require_once dirname( __FILE__ ) .'/xpress_cache.php' ;
[98]6        global $xoops_config;
[44]7       
[98]8        if (!is_object($xoops_config)){ // is call other modules
[134]9                require_once dirname(dirname( __FILE__ )) .'/class/config_from_xoops.class.php' ;
[98]10                $xoops_config = new ConfigFromXoops;
11        }
12       
[44]13        function xpress_block_cache_write($mydirname,$block_name,$block)
14        {
15                        $xml = xpress_XML_serialize($block);
16                        $xml_name = $block_name . '.xml';
[364]17                        if (WPLANG == 'ja_EUC'){
18                                $xml = str_replace('<?xml version="1.0" ?>', '<?xml version="1.0" encoding="EUC-JP" ?>' , $xml);
19                        }
[44]20                        xpress_cache_write($mydirname,$xml_name,$xml);
21        }
22        function xpress_block_cache_read($mydirname,$block_name)
23        {
[502]24                $xml_name = $block_name . '.xml';
25                $xml_data = xpress_cache_read($mydirname,$xml_name);
26               
27                $GLOBALS['DO_LIBXML_PATCH'] = get_xpress_mod_config($mydirname,'libxml_patch');
28               
29                // The character-code not treatable exists when 'XML_unserialize' of PHP5 processes EUC-JP.
30                // And, the result is returned by character-code UTF-8.
31                // Measures
32                // After the character-code is converted into UTF-8, XML_unserialize will be processed.
33                if ( strstr($xml_data, '<?xml version="1.0" encoding="EUC-JP" ?>') !== false
34                         && version_compare(PHP_VERSION, '5.0.0', '>') )
35                {
36                        $xml_data = str_replace('<?xml version="1.0" encoding="EUC-JP" ?>', '<?xml version="1.0" encoding="UTF-8" ?>', $xml_data);
37                        $ans = mb_convert_variables('UTF-8' , 'EUC-JP', &$xml_data); //EUC-JP to UTF-8
[364]38                        $ret = @xpress_XML_unserialize($xml_data);
[502]39                        $ans = mb_convert_variables('EUC-JP' , 'UTF-8', &$ret); //UTF-8 to EUC-JP
40                } else {
41                        $ret = xpress_XML_unserialize($xml_data);
42                }
43                return $ret;
[44]44        }
45       
46        function get_block_id($mydirname,$func_file,$options)
47        {
48                $options_string = '';
49                foreach ($options as $val){
50                        if (!empty($options_string)) $options_string .='|';
51                        $options_string .= $val;
52                }
53                        $xoopsDB =& Database::getInstance();
54                        $block_tbl = $xoopsDB->prefix('newblocks');     
55                        $module_dir = XOOPS_ROOT_PATH . '/modules/' . $mydirname;
56
57                        $sql = "SELECT bid FROM $block_tbl WHERE (func_file LIKE '$func_file') AND (options LIKE '$options_string')";
58                        $result =  $xoopsDB->query($sql, 0, 0);
59                        if ($xoopsDB->getRowsNum($result)  > 0){
60                                $row = $xoopsDB->fetchArray($result);
61                                $block_id = $row['bid'];
62                        }
63                        return $block_id;
64        }
65
[463]66        function get_block_mid($mydirname)
67        {
68                        $xoopsDB =& Database::getInstance();
69                        $modules_tbl = $xoopsDB->prefix('modules');     
70
71                        $sql = "SELECT mid FROM $modules_tbl WHERE dirname = '$mydirname'";
72                        $result =  $xoopsDB->query($sql, 0, 0);
73                        if ($xoopsDB->getRowsNum($result)  > 0){
74                                $row = $xoopsDB->fetchArray($result);
75                                $mid = $row['mid'];
76                        }
77                        return $mid;
78        }
79
[44]80        function get_xpress_theme_name($mydirname)
81        {
[46]82                global $wpdb;
83               
84                if (is_null($wpdb)){
85                        $xoopsDB =& Database::getInstance();
[583]86                        $wp_prefix = preg_replace('/wordpress/','wp',$mydirname);
[44]87
[46]88                        $module_tbl = $xoopsDB->prefix($wp_prefix).'_options'; 
89                        $theme_name = '';       
[44]90
[46]91                        $sql = "SELECT option_value FROM $module_tbl WHERE option_name LIKE 'template'";
92                        $result =  $xoopsDB->query($sql, 0, 0);
93                        if ($xoopsDB->getRowsNum($result)  > 0){
94                                $row = $xoopsDB->fetchArray($result);
95                                $theme_name = $row['option_value'];
96                        }
97                } else {
98                        $theme_name = get_option('template');
[44]99                }
100                return $theme_name;
101        }
102
[573]103        function xpress_block_header_set($mydirname = '')
[44]104        {
[573]105                require_once( dirname( __FILE__ ).'/xpress_block_header.php' );
106                $xml = xpress_block_header_cache_read($mydirname);
107                $block_header = $xml['block_header'];
108                if (!empty($block_header)){
109                        $tplVars =& $GLOBALS['xoopsTpl']->get_template_vars();
[44]110                        if(array_key_exists('xoops_block_header', $tplVars)) {
[573]111                                if (!strstr($tplVars['xoops_block_header'],$block_header)) {
112                                        $GLOBALS['xoopsTpl']->assign('xoops_block_header',$tplVars['xoops_block_header'].$block_header);
[44]113                                }
114                        } else {
[573]115                                $GLOBALS['xoopsTpl']->assign('xoops_block_header',$block_header);
[44]116                        }
[573]117                }
[44]118        }
119       
120       
[96]121    function xpress_block_cache_found($mydirname,$block_name)
122    {
123        global $xoops_config;
[657]124       
125                $cache_dir = $xoops_config->xoops_cache_path . '/';
[96]126        $xml_name = $block_name . '.xml';
127
128        $filename = $cache_dir .$mydirname . '_' . $xml_name;
129                $cache_time = 0;
130//        if (file_exists($filename) && ((time() - filemtime($filename)) < $cache_time)) {
131        if (file_exists($filename)) {
132            return true;
133       } else {
134                        return false;
135                }
136    }
137       
[44]138        function xpress_block_render($mydirname,$block_function_name,$options)
139        {
[455]140                global $wpdb,$xoops_config;
[44]141                $func_file = $block_function_name;
142                $call_theme_function_name = str_replace(".php", "", $block_function_name);
143                $inc_theme_file_name = $call_theme_function_name . '_theme.php';
144                $cache_title = str_replace(".php", "", $block_function_name);
[89]145                $blockID =get_block_id($mydirname,$func_file,$options);         
146
[483]147                $this_block_url = '/' . $mydirname . '/';
[96]148                $call_url = $_SERVER['REQUEST_URI'];
[483]149               
[506]150                if (strstr($call_url , $this_block_url) !== false && strstr($call_url , $this_block_url . 'admin/') === false){
[44]151                        $block_theme_file = get_block_file_path($mydirname,$inc_theme_file_name);
[448]152                        require_once $block_theme_file['file_path'];
[44]153                        $block = $call_theme_function_name($options);           //The block name and the called function name should be assumed to be the same name.
[448]154                        if (!empty($block_theme_file['error']))
155                                $block['err_message'] = $block_theme_file['error'];
[44]156                } else {
[96]157                        if (xpress_block_cache_found($mydirname,$cache_title. $blockID)){
158                                $xml = xpress_block_cache_read($mydirname,$cache_title. $blockID);
159                                $block = $xml['block'];
160                        } else {
[183]161                                $block['err_message'] = sprintf(_MB_XP2_BLOCK_CACHE_ERR, '<a href="' . XOOPS_URL . '/modules/' . $mydirname . '">' . $mydirname .'</a>');
[96]162                        }
[573]163                        xpress_block_header_set($mydirname);
[44]164                }
[656]165                $block['request_uri'] = $_SERVER['REQUEST_URI'];
[480]166                $temp_option = explode(':' , $options[1]);
[455]167               
[480]168                if (isset($temp_option[1])) {
[497]169                        $templates_file = $options[1];
[463]170                } else {
[455]171                        $templates_file = 'db:'.$mydirname. '_' . str_replace(".php", ".html", $block_function_name);
[463]172                }
173               
[89]174                $tpl =& new XoopsTpl() ;
[463]175                $tpl->template_dir = $xoops_config->module_path . '/templates';
176                if (!$tpl->template_exists($templates_file)){
177                        $src_file_path = $xoops_config->module_path . '/templates/' .$mydirname. '_' . str_replace(".php", ".html", $block_function_name);
[472]178                        $templates_file = add_xpress_tpl($mydirname,$templates_file,$src_file_path);
[463]179                }
[89]180                $tpl->assign( 'block' , $block ) ;
181                $ret['content'] = $tpl->fetch( $templates_file ) ;
[656]182                if(preg_match('/\S/',$ret['content'])){
183                        return $ret ;
184                }else {
185                        return null;
186                }
[44]187        }
188       
[463]189        function add_xpress_tpl($mydirname,$templates='',$src_file_path){
190                global $wpdb,$xoops_config , $xoops_db;
191               
192                $mid = get_block_mid($mydirname);
193
194                $temp_parm = explode(':' , $templates);
195                if (empty($temp_parm[1])) {
196                        $filename=$temp_parm[0];
197                        $type = 'db';
198                } else  {
199                        $filename=$temp_parm[1];
200                        $type = $temp_parm[0];
201                }
202                $temp_file_path = $xoops_config->module_path . '/templates/'. $filename;
203                $pattern = '^' . $mydirname . '_';
204                if (preg_match('/' . $pattern . '/' , $filename, $match)){ // file prefix check
205                        if (!file_exists($temp_file_path)){             // Repetition check
206                                if (file_exists($src_file_path)){       // source file check
207                                        $rcd = copy($src_file_path, $temp_file_path);
208                                }
209                        }
[472]210                        return  'file:' . $filename;
[463]211                }
[472]212                return $templates;
[463]213        }
214       
[172]215        function xpress_block_cache_refresh($mydirname)
[135]216        {
217                global $xoops_db;
218                $mid = get_xpress_modid();
[394]219               
220                // It is a block that needs cache arranged outside the module.
221                // Only the block arranged outside the module is detected here.
222                $newblocks = get_xoops_prefix() . "newblocks";
223                $block_module_link = get_xoops_prefix(). "block_module_link";
224                $sql  = "SELECT * FROM $newblocks LEFT JOIN $block_module_link ON {$newblocks}.bid = {$block_module_link}.block_id ";
225                $sql .= "WHERE {$newblocks}.mid = $mid AND {$newblocks}.visible = 1 AND {$block_module_link}.module_id != $mid ";
226                $sql .= "GROUP BY {$newblocks}.bid";
227
[135]228                $blocks = $xoops_db->get_results($sql);
229                require_once get_xpress_dir_path() . '/include/xpress_block_render.php';
230
231                foreach($blocks as $block){
232                        $func_file = $block->func_file;
233                        $call_theme_function_name = str_replace(".php", "", $func_file);
234                        $inc_theme_file_name = str_replace(".php", "", $func_file) . '_theme.php';
235                        $cache_title = str_replace(".php", "", $func_file);
236                        $blockID = $block->bid;
237                        $options = explode("|", $block->options);
238
239                        $block_theme_file = get_block_file_path($mydirname,$inc_theme_file_name);
[448]240                        require_once $block_theme_file['file_path'];
[135]241                        $render = $call_theme_function_name($options);          //The block name and the called function name should be assumed to be the same name.                   
242                        $render_array['block'] = $render;
243                        $render_array['block']['options'] = $block->options;
[448]244                        if (!empty($block_theme_file['error']))
245                                $render_array['block']['err_message'] = $block_theme_file['error'];
[135]246                        if (xpress_block_cache_found($mydirname,$cache_title. $blockID)){       
247                                $render_serialize = xpress_XML_serialize($render_array);
248                                $render_md5 = md5($render_serialize);
249
250                                $cache_serialize = xpress_cache_read($mydirname,$cache_title. $blockID.'.xml');
251                                $cache_md5 = md5($cache_serialize);
252                               
253                                if ($render_md5 != $cache_md5){
254                                        xpress_block_cache_write($mydirname,$cache_title. $blockID, $render_array);
255                                }
256                        } else {
257                                xpress_block_cache_write($mydirname,$cache_title. $blockID, $render_array);
258                        }
259                }
260        }
261       
[172]262        function xpress_unnecessary_block_cache_delete($mydirname)
[135]263        {
264                global $xoops_db,$xoops_config;
[463]265               
[135]266                $mid = get_xpress_modid();
267                $sql = "SELECT bid,options,func_file FROM " . get_xoops_prefix() . "newblocks WHERE mid = $mid AND visible = 1";
268                $blocks = $xoops_db->get_results($sql);
269                require_once get_xpress_dir_path() . '/include/xpress_block_render.php';
270
[588]271                $pattern =$mydirname . '_block_header';
[135]272                foreach($blocks as $block){
273                        $cache_file_name = $mydirname . '_'. str_replace(".php", "", $block->func_file) . $block->bid;
274                        if (!empty($pattern))  $pattern .= '|';
275                        $pattern .= $cache_file_name;
276                }
277                $pattern = '(' . $pattern . ')';
278               
[657]279                $cache_dir = $xoops_config->xoops_cache_path . '/';
[135]280                $cache_time = 0;
281        if ($dh = opendir($cache_dir)) {
282            while (($file = readdir($dh)) !== false) {
[482]283                if (preg_match('/^' . preg_quote($mydirname) . '_/', $file)) {
[135]284                        if(! preg_match('/' . $pattern . '/', $file)) {
285                        unlink($cache_dir.$file);
286                    }
287                }
288            }
289            closedir($dh);
290        }
291    }
[387]292   
293    function get_xpress_mod_config($mydirname,$conf_name=''){
294                $module_handler =& xoops_gethandler('module');
295                $xoopsModule =& $module_handler->getByDirname($mydirname);
296                $mid = $xoopsModule->getVar('mid');
297                $xoopsDB =& Database::getInstance();
298                $db_config = $xoopsDB->prefix('config');
299           
300                $wu_sql  =      "SELECT conf_value FROM  $db_config ";
301                $wu_sql .=      "WHERE (conf_modid = $mid ) AND (conf_name LIKE '$conf_name')";
302                $wu_res = $xoopsDB->queryF($wu_sql, 0, 0);
303                       
304                if ($wu_res === false){
305                        return 0;
306                } else {
307                        $xu_row = $xoopsDB->fetchArray($wu_res);
308                        return $xu_row['conf_value'];
309                }
[463]310        }       
[172]311}       
[44]312?>
Note: See TracBrowser for help on using the repository browser.