XPressME Integration Kit

Trac

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

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

ブロックキャッシュXMLのXML_Unserializerでxml_parseエラー(PHP5 & EUC-JP)のバグ修正 Fixes #279

File size: 13.0 KB
Line 
1<?php
2if(!defined('XPRESS_BLOCK_RENDER_FUNCTION_READ')){
3        define('XPRESS_BLOCK_RENDER_FUNCTION_READ',1);
4        require_once dirname( __FILE__ ) .'/xml.php' ;
5        require_once dirname( __FILE__ ) .'/xpress_cache.php' ;
6        global $xoops_config;
7       
8        if (!is_object($xoops_config)){ // is call other modules
9                require_once dirname(dirname( __FILE__ )) .'/class/config_from_xoops.class.php' ;
10                $xoops_config = new ConfigFromXoops;
11        }
12       
13        function xpress_block_cache_write($mydirname,$block_name,$block)
14        {
15                        $xml = xpress_XML_serialize($block);
16                        $xml_name = $block_name . '.xml';
17                        if (WPLANG == 'ja_EUC'){
18                                $xml = str_replace('<?xml version="1.0" ?>', '<?xml version="1.0" encoding="EUC-JP" ?>' , $xml);
19                        }
20                        xpress_cache_write($mydirname,$xml_name,$xml);
21        }
22        function xpress_block_cache_read($mydirname,$block_name)
23        {
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
38                        $ret = @xpress_XML_unserialize($xml_data);
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        }
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
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
80        function get_xpress_theme_name($mydirname)
81        {
82                global $wpdb;
83               
84                if (is_null($wpdb)){
85                        $xoopsDB =& Database::getInstance();
86                        $wp_prefix = $mydirname;
87                        if ($wp_prefix == 'wordpress') $wp_prefix = 'wp';
88
89                        $module_tbl = $xoopsDB->prefix($wp_prefix).'_options'; 
90                        $theme_name = '';       
91
92                        $sql = "SELECT option_value FROM $module_tbl WHERE option_name LIKE 'template'";
93                        $result =  $xoopsDB->query($sql, 0, 0);
94                        if ($xoopsDB->getRowsNum($result)  > 0){
95                                $row = $xoopsDB->fetchArray($result);
96                                $theme_name = $row['option_value'];
97                        }
98                } else {
99                        $theme_name = get_option('template');
100                }
101                return $theme_name;
102        }
103
104        function get_block_stylesheet_url($mydirname)
105        {
106                global $xoops_config;
107                $mydirpath = $xoops_config->xoops_root_path . '/modules/' . $mydirname;
108                $select_theme = get_xpress_theme_name($mydirname);
109                $style_file = $mydirpath . '/wp-content/themes/' . $select_theme . '/blocks/block_style.css';
110                if (file_exists($style_file))
111                        return $xoops_config->xoops_url . '/modules/' .$mydirname . '/wp-content/themes/' . $select_theme . '/blocks/block_style.css';
112                else   
113                        return $xoops_config->xoops_url . '/modules/' .$mydirname . '/wp-content/themes/xpress_default/blocks/block_style.css';
114        }
115       
116        function get_stylesheet_url($mydirname)
117        {
118                global $xoops_config;
119                $mydirpath = $xoops_config->xoops_root_path . '/modules/' . $mydirname;
120                $select_theme = get_xpress_theme_name($mydirname);
121                $style_file = $mydirpath . '/wp-content/themes/' . $select_theme . '/style.css';
122                if (file_exists($style_file))
123                        return $xoops_config->xoops_url . '/modules/' .$mydirname . '/wp-content/themes/' . $select_theme . '/style.css';
124                else   
125                        return $xoops_config->xoops_url . '/modules/' .$mydirname . '/wp-content/themes/xpress_default/style.css';
126        }
127       
128        function xpress_get_plugin_css($mydirname = '')
129        {
130                $this_url = '/modules/'. $mydirname;
131                $call_url = $_SERVER['REQUEST_URI'];
132                if (!strstr($call_url,$this_url)){                     
133                        global $xoops_config;
134                       
135                        // Module Main CSS
136                        $output = "\n".'<style type="text/css">@import url('.get_stylesheet_url($mydirname).');</style>';
137                       
138                        // hotDate
139                        $output .= "\n".'<style type="text/css">@import url('.$xoops_config->module_url.'/wp-content/plugins/hotDates/hotDates.css);</style>';
140                        return $output;
141                }
142                return '';
143        }
144
145        function xpress_block_css_set($mydirname = '')
146        {
147                $style_url =  get_block_stylesheet_url($mydirname);
148               
149                $tplVars =& $GLOBALS['xoopsTpl']->get_template_vars();
150                $csslink = "\n".'<link rel="stylesheet" type="text/css" media="screen" href="'. $style_url .'" />';
151                // Module Main CSS Plugin Css
152                $csslink .= xpress_get_plugin_css($mydirname);
153
154                        if(array_key_exists('xoops_block_header', $tplVars)) {
155                                if (!strstr($tplVars['xoops_block_header'],$csslink)) {
156                                        $GLOBALS['xoopsTpl']->assign('xoops_block_header',$tplVars['xoops_block_header'].$csslink);
157                                }
158                        } else {
159                                $GLOBALS['xoopsTpl']->assign('xoops_block_header',$csslink);
160                        }
161        }
162       
163       
164    function xpress_block_cache_found($mydirname,$block_name)
165    {
166        global $xoops_config;
167        if(defined('XOOPS_ROOT_PATH')){
168                $cache_dir = XOOPS_ROOT_PATH . '/cache/';
169        } else {
170                $cache_dir = $xoops_config->xoops_root_path . '/cache/';
171        }
172        $xml_name = $block_name . '.xml';
173
174        $filename = $cache_dir .$mydirname . '_' . $xml_name;
175                $cache_time = 0;
176//        if (file_exists($filename) && ((time() - filemtime($filename)) < $cache_time)) {
177        if (file_exists($filename)) {
178            return true;
179       } else {
180                        return false;
181                }
182    }
183       
184        function xpress_block_render($mydirname,$block_function_name,$options)
185        {
186                global $wpdb,$xoops_config;
187                $func_file = $block_function_name;
188                $call_theme_function_name = str_replace(".php", "", $block_function_name);
189                $inc_theme_file_name = $call_theme_function_name . '_theme.php';
190                $cache_title = str_replace(".php", "", $block_function_name);
191                $blockID =get_block_id($mydirname,$func_file,$options);         
192
193                $this_block_url = '/' . $mydirname . '/';
194                $call_url = $_SERVER['REQUEST_URI'];
195               
196                xpress_block_css_set($mydirname);
197                if (strstr($call_url , $this_block_url) !== false){
198                        $block_theme_file = get_block_file_path($mydirname,$inc_theme_file_name);
199                        require_once $block_theme_file['file_path'];
200                        $block = $call_theme_function_name($options);           //The block name and the called function name should be assumed to be the same name.
201                        if (!empty($block_theme_file['error']))
202                                $block['err_message'] = $block_theme_file['error'];
203                } else {
204                        if (xpress_block_cache_found($mydirname,$cache_title. $blockID)){
205                                $xml = xpress_block_cache_read($mydirname,$cache_title. $blockID);
206                                $block = $xml['block'];
207                        } else {
208                                $block['err_message'] = sprintf(_MB_XP2_BLOCK_CACHE_ERR, '<a href="' . XOOPS_URL . '/modules/' . $mydirname . '">' . $mydirname .'</a>');
209                        }
210                }
211                $temp_option = explode(':' , $options[1]);
212               
213                if (isset($temp_option[1])) {
214                        $templates_file = $options[1];
215                } else {
216                        $templates_file = 'db:'.$mydirname. '_' . str_replace(".php", ".html", $block_function_name);
217                }
218               
219                $tpl =& new XoopsTpl() ;
220                $tpl->template_dir = $xoops_config->module_path . '/templates';
221                if (!$tpl->template_exists($templates_file)){
222                        $src_file_path = $xoops_config->module_path . '/templates/' .$mydirname. '_' . str_replace(".php", ".html", $block_function_name);
223                        $templates_file = add_xpress_tpl($mydirname,$templates_file,$src_file_path);
224                }
225                $tpl->assign( 'block' , $block ) ;
226                $ret['content'] = $tpl->fetch( $templates_file ) ;
227                return $ret ;
228        }
229       
230        function add_xpress_tpl($mydirname,$templates='',$src_file_path){
231                global $wpdb,$xoops_config , $xoops_db;
232               
233                $mid = get_block_mid($mydirname);
234
235                $temp_parm = explode(':' , $templates);
236                if (empty($temp_parm[1])) {
237                        $filename=$temp_parm[0];
238                        $type = 'db';
239                } else  {
240                        $filename=$temp_parm[1];
241                        $type = $temp_parm[0];
242                }
243                $temp_file_path = $xoops_config->module_path . '/templates/'. $filename;
244                $pattern = '^' . $mydirname . '_';
245                if (preg_match('/' . $pattern . '/' , $filename, $match)){ // file prefix check
246                        if (!file_exists($temp_file_path)){             // Repetition check
247                                if (file_exists($src_file_path)){       // source file check
248                                        $rcd = copy($src_file_path, $temp_file_path);
249                                }
250                        }
251                        return  'file:' . $filename;
252                }
253                return $templates;
254        }
255       
256        function xpress_block_cache_refresh($mydirname)
257        {
258                global $xoops_db;
259                $mid = get_xpress_modid();
260               
261                // It is a block that needs cache arranged outside the module.
262                // Only the block arranged outside the module is detected here.
263                $newblocks = get_xoops_prefix() . "newblocks";
264                $block_module_link = get_xoops_prefix(). "block_module_link";
265                $sql  = "SELECT * FROM $newblocks LEFT JOIN $block_module_link ON {$newblocks}.bid = {$block_module_link}.block_id ";
266                $sql .= "WHERE {$newblocks}.mid = $mid AND {$newblocks}.visible = 1 AND {$block_module_link}.module_id != $mid ";
267                $sql .= "GROUP BY {$newblocks}.bid";
268
269                $blocks = $xoops_db->get_results($sql);
270                require_once get_xpress_dir_path() . '/include/xpress_block_render.php';
271
272                foreach($blocks as $block){
273                        $func_file = $block->func_file;
274                        $call_theme_function_name = str_replace(".php", "", $func_file);
275                        $inc_theme_file_name = str_replace(".php", "", $func_file) . '_theme.php';
276                        $cache_title = str_replace(".php", "", $func_file);
277                        $blockID = $block->bid;
278                        $options = explode("|", $block->options);
279
280                        $block_theme_file = get_block_file_path($mydirname,$inc_theme_file_name);
281                        require_once $block_theme_file['file_path'];
282                        $render = $call_theme_function_name($options);          //The block name and the called function name should be assumed to be the same name.                   
283                        $render_array['block'] = $render;
284                        $render_array['block']['options'] = $block->options;
285                        if (!empty($block_theme_file['error']))
286                                $render_array['block']['err_message'] = $block_theme_file['error'];
287                        if (xpress_block_cache_found($mydirname,$cache_title. $blockID)){       
288                                $render_serialize = xpress_XML_serialize($render_array);
289                                $render_md5 = md5($render_serialize);
290
291                                $cache_serialize = xpress_cache_read($mydirname,$cache_title. $blockID.'.xml');
292                                $cache_md5 = md5($cache_serialize);
293                               
294                                if ($render_md5 != $cache_md5){
295                                        xpress_block_cache_write($mydirname,$cache_title. $blockID, $render_array);
296                                }
297                        } else {
298                                xpress_block_cache_write($mydirname,$cache_title. $blockID, $render_array);
299                        }
300                }
301        }
302       
303        function xpress_unnecessary_block_cache_delete($mydirname)
304        {
305                global $xoops_db,$xoops_config;
306               
307                $mid = get_xpress_modid();
308                $sql = "SELECT bid,options,func_file FROM " . get_xoops_prefix() . "newblocks WHERE mid = $mid AND visible = 1";
309                $blocks = $xoops_db->get_results($sql);
310                require_once get_xpress_dir_path() . '/include/xpress_block_render.php';
311
312                $pattern ='';
313                foreach($blocks as $block){
314                        $cache_file_name = $mydirname . '_'. str_replace(".php", "", $block->func_file) . $block->bid;
315                        if (!empty($pattern))  $pattern .= '|';
316                        $pattern .= $cache_file_name;
317                }
318                $pattern = '(' . $pattern . ')';
319               
320                $cache_dir = $xoops_config->xoops_root_path . '/cache/';
321                $cache_time = 0;
322        if ($dh = opendir($cache_dir)) {
323            while (($file = readdir($dh)) !== false) {
324                if (preg_match('/^' . preg_quote($mydirname) . '_/', $file)) {
325                        if(! preg_match('/' . $pattern . '/', $file)) {
326                        unlink($cache_dir.$file);
327                    }
328                }
329            }
330            closedir($dh);
331        }
332    }
333   
334    function get_xpress_mod_config($mydirname,$conf_name=''){
335                $module_handler =& xoops_gethandler('module');
336                $xoopsModule =& $module_handler->getByDirname($mydirname);
337                $mid = $xoopsModule->getVar('mid');
338                $xoopsDB =& Database::getInstance();
339                $db_config = $xoopsDB->prefix('config');
340           
341                $wu_sql  =      "SELECT conf_value FROM  $db_config ";
342                $wu_sql .=      "WHERE (conf_modid = $mid ) AND (conf_name LIKE '$conf_name')";
343                $wu_res = $xoopsDB->queryF($wu_sql, 0, 0);
344                       
345                if ($wu_res === false){
346                        return 0;
347                } else {
348                        $xu_row = $xoopsDB->fetchArray($wu_res);
349                        return $xu_row['conf_value'];
350                }
351        }       
352}       
353?>
Note: See TracBrowser for help on using the repository browser.