XPressME Integration Kit

Trac

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

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

XOOPS側でのRewriteRule 適応時のブロック更新が行われないバグ修正 Fixes #250

File size: 10.9 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                        $ret = @xpress_XML_unserialize($xml_data);
30                        if (strstr($xml_data, '<?xml version="1.0" encoding="EUC-JP" ?>') !== false){
31                                $ans = mb_convert_variables('EUC-JP' , 'UTF-8', &$ret);
32                        }
33                        return $ret;
34        }
35       
36        function get_block_id($mydirname,$func_file,$options)
37        {
38                $options_string = '';
39                foreach ($options as $val){
40                        if (!empty($options_string)) $options_string .='|';
41                        $options_string .= $val;
42                }
43                        $xoopsDB =& Database::getInstance();
44                        $block_tbl = $xoopsDB->prefix('newblocks');     
45                        $module_dir = XOOPS_ROOT_PATH . '/modules/' . $mydirname;
46
47                        $sql = "SELECT bid FROM $block_tbl WHERE (func_file LIKE '$func_file') AND (options LIKE '$options_string')";
48                        $result =  $xoopsDB->query($sql, 0, 0);
49                        if ($xoopsDB->getRowsNum($result)  > 0){
50                                $row = $xoopsDB->fetchArray($result);
51                                $block_id = $row['bid'];
52                        }
53                        return $block_id;
54        }
55
56        function get_xpress_theme_name($mydirname)
57        {
58                global $wpdb;
59               
60                if (is_null($wpdb)){
61                        $xoopsDB =& Database::getInstance();
62                        $wp_prefix = $mydirname;
63                        if ($wp_prefix == 'wordpress') $wp_prefix = 'wp';
64
65                        $module_tbl = $xoopsDB->prefix($wp_prefix).'_options'; 
66                        $theme_name = '';       
67
68                        $sql = "SELECT option_value FROM $module_tbl WHERE option_name LIKE 'template'";
69                        $result =  $xoopsDB->query($sql, 0, 0);
70                        if ($xoopsDB->getRowsNum($result)  > 0){
71                                $row = $xoopsDB->fetchArray($result);
72                                $theme_name = $row['option_value'];
73                        }
74                } else {
75                        $theme_name = get_option('template');
76                }
77                return $theme_name;
78        }
79
80        function get_block_stylesheet_url($mydirname)
81        {
82                global $xoops_config;
83                $mydirpath = $xoops_config->xoops_root_path . '/modules/' . $mydirname;
84                $select_theme = get_xpress_theme_name($mydirname);
85                $style_file = $mydirpath . '/wp-content/themes/' . $select_theme . '/blocks/block_style.css';
86                if (file_exists($style_file))
87                        return $xoops_config->xoops_url . '/modules/' .$mydirname . '/wp-content/themes/' . $select_theme . '/blocks/block_style.css';
88                else   
89                        return $xoops_config->xoops_url . '/modules/' .$mydirname . '/wp-content/themes/xpress_default/blocks/block_style.css';
90        }
91       
92        function get_stylesheet_url($mydirname)
93        {
94                global $xoops_config;
95                $mydirpath = $xoops_config->xoops_root_path . '/modules/' . $mydirname;
96                $select_theme = get_xpress_theme_name($mydirname);
97                $style_file = $mydirpath . '/wp-content/themes/' . $select_theme . '/style.css';
98                if (file_exists($style_file))
99                        return $xoops_config->xoops_url . '/modules/' .$mydirname . '/wp-content/themes/' . $select_theme . '/style.css';
100                else   
101                        return $xoops_config->xoops_url . '/modules/' .$mydirname . '/wp-content/themes/xpress_default/style.css';
102        }
103       
104        function xpress_get_plugin_css($mydirname = '')
105        {
106                $this_url = '/modules/'. $mydirname;
107                $call_url = $_SERVER['REQUEST_URI'];
108                if (!strstr($call_url,$this_url)){                     
109                        global $xoops_config;
110                       
111                        // Module Main CSS
112                        $output = "\n".'<style type="text/css">@import url('.get_stylesheet_url($mydirname).');</style>';
113                       
114                        // hotDate
115                        $output .= "\n".'<style type="text/css">@import url('.$xoops_config->module_url.'/wp-content/plugins/hotDates/hotDates.css);</style>';
116                        return $output;
117                }
118                return '';
119        }
120
121        function xpress_block_css_set($mydirname = '')
122        {
123                $style_url =  get_block_stylesheet_url($mydirname);
124               
125                $tplVars =& $GLOBALS['xoopsTpl']->get_template_vars();
126                $csslink = "\n".'<link rel="stylesheet" type="text/css" media="screen" href="'. $style_url .'" />';
127                // Module Main CSS Plugin Css
128                $csslink .= xpress_get_plugin_css($mydirname);
129
130                        if(array_key_exists('xoops_block_header', $tplVars)) {
131                                if (!strstr($tplVars['xoops_block_header'],$csslink)) {
132                                        $GLOBALS['xoopsTpl']->assign('xoops_block_header',$tplVars['xoops_block_header'].$csslink);
133                                }
134                        } else {
135                                $GLOBALS['xoopsTpl']->assign('xoops_block_header',$csslink);
136                        }
137        }
138       
139       
140    function xpress_block_cache_found($mydirname,$block_name)
141    {
142        global $xoops_config;
143        if(defined('XOOPS_ROOT_PATH')){
144                $cache_dir = XOOPS_ROOT_PATH . '/cache/';
145        } else {
146                $cache_dir = $xoops_config->xoops_root_path . '/cache/';
147        }
148        $xml_name = $block_name . '.xml';
149
150        $filename = $cache_dir .$mydirname . '_' . $xml_name;
151                $cache_time = 0;
152//        if (file_exists($filename) && ((time() - filemtime($filename)) < $cache_time)) {
153        if (file_exists($filename)) {
154            return true;
155       } else {
156                        return false;
157                }
158    }
159       
160        function xpress_block_render($mydirname,$block_function_name,$options)
161        {
162                global $wpdb,$xoops_config;
163                $func_file = $block_function_name;
164                $call_theme_function_name = str_replace(".php", "", $block_function_name);
165                $inc_theme_file_name = $call_theme_function_name . '_theme.php';
166                $cache_title = str_replace(".php", "", $block_function_name);
167                $blockID =get_block_id($mydirname,$func_file,$options);         
168
169                $this_url = str_replace( $xoops_config->xoops_url , '' , get_option('home'));
170                $call_url = $_SERVER['REQUEST_URI'];
171                xpress_block_css_set($mydirname);
172                if (strstr($call_url,$this_url)){       
173                        $block_theme_file = get_block_file_path($mydirname,$inc_theme_file_name);
174                        require_once $block_theme_file['file_path'];
175                        $block = $call_theme_function_name($options);           //The block name and the called function name should be assumed to be the same name.
176                        if (!empty($block_theme_file['error']))
177                                $block['err_message'] = $block_theme_file['error'];
178                } else {
179                        if (xpress_block_cache_found($mydirname,$cache_title. $blockID)){
180                                $xml = xpress_block_cache_read($mydirname,$cache_title. $blockID);
181                                $block = $xml['block'];
182                        } else {
183                                $block['err_message'] = sprintf(_MB_XP2_BLOCK_CACHE_ERR, '<a href="' . XOOPS_URL . '/modules/' . $mydirname . '">' . $mydirname .'</a>');
184                        }
185                }
186                $option = explode('|' , $block['options']);
187               
188                if (isset($option[1]))
189                        $templates_file = $option[1];
190                else
191                        $templates_file = 'db:'.$mydirname. '_' . str_replace(".php", ".html", $block_function_name);
192                $tpl =& new XoopsTpl() ;
193                $tpl->assign( 'block' , $block ) ;
194                $ret['content'] = $tpl->fetch( $templates_file ) ;
195                return $ret ;
196        }
197       
198        function xpress_block_cache_refresh($mydirname)
199        {
200                global $xoops_db;
201                $mid = get_xpress_modid();
202               
203                // It is a block that needs cache arranged outside the module.
204                // Only the block arranged outside the module is detected here.
205                $newblocks = get_xoops_prefix() . "newblocks";
206                $block_module_link = get_xoops_prefix(). "block_module_link";
207                $sql  = "SELECT * FROM $newblocks LEFT JOIN $block_module_link ON {$newblocks}.bid = {$block_module_link}.block_id ";
208                $sql .= "WHERE {$newblocks}.mid = $mid AND {$newblocks}.visible = 1 AND {$block_module_link}.module_id != $mid ";
209                $sql .= "GROUP BY {$newblocks}.bid";
210
211                $blocks = $xoops_db->get_results($sql);
212                require_once get_xpress_dir_path() . '/include/xpress_block_render.php';
213
214                foreach($blocks as $block){
215                        $func_file = $block->func_file;
216                        $call_theme_function_name = str_replace(".php", "", $func_file);
217                        $inc_theme_file_name = str_replace(".php", "", $func_file) . '_theme.php';
218                        $cache_title = str_replace(".php", "", $func_file);
219                        $blockID = $block->bid;
220                        $options = explode("|", $block->options);
221
222                        $block_theme_file = get_block_file_path($mydirname,$inc_theme_file_name);
223                        require_once $block_theme_file['file_path'];
224                        $render = $call_theme_function_name($options);          //The block name and the called function name should be assumed to be the same name.                   
225                        $render_array['block'] = $render;
226                        $render_array['block']['options'] = $block->options;
227                        if (!empty($block_theme_file['error']))
228                                $render_array['block']['err_message'] = $block_theme_file['error'];
229                        if (xpress_block_cache_found($mydirname,$cache_title. $blockID)){       
230                                $render_serialize = xpress_XML_serialize($render_array);
231                                $render_md5 = md5($render_serialize);
232
233                                $cache_serialize = xpress_cache_read($mydirname,$cache_title. $blockID.'.xml');
234                                $cache_md5 = md5($cache_serialize);
235                               
236                                if ($render_md5 != $cache_md5){
237                                        xpress_block_cache_write($mydirname,$cache_title. $blockID, $render_array);
238                                }
239                        } else {
240                                xpress_block_cache_write($mydirname,$cache_title. $blockID, $render_array);
241                        }
242                }
243        }
244       
245        function xpress_unnecessary_block_cache_delete($mydirname)
246        {
247                global $xoops_db,$xoops_config;
248                $mid = get_xpress_modid();
249                $sql = "SELECT bid,options,func_file FROM " . get_xoops_prefix() . "newblocks WHERE mid = $mid AND visible = 1";
250                $blocks = $xoops_db->get_results($sql);
251                require_once get_xpress_dir_path() . '/include/xpress_block_render.php';
252
253                $pattern ='';
254                foreach($blocks as $block){
255                        $cache_file_name = $mydirname . '_'. str_replace(".php", "", $block->func_file) . $block->bid;
256                        if (!empty($pattern))  $pattern .= '|';
257                        $pattern .= $cache_file_name;
258                }
259                $pattern = '(' . $pattern . ')';
260               
261                $cache_dir = $xoops_config->xoops_root_path . '/cache/';
262                $cache_time = 0;
263        if ($dh = opendir($cache_dir)) {
264            while (($file = readdir($dh)) !== false) {
265                if (preg_match('/^' . preg_quote($mydirname) . '/', $file)) {
266                        if(! preg_match('/' . $pattern . '/', $file)) {
267                        unlink($cache_dir.$file);
268                    }
269                }
270            }
271            closedir($dh);
272        }
273    }
274   
275    function get_xpress_mod_config($mydirname,$conf_name=''){
276                $module_handler =& xoops_gethandler('module');
277                $xoopsModule =& $module_handler->getByDirname($mydirname);
278                $mid = $xoopsModule->getVar('mid');
279                $xoopsDB =& Database::getInstance();
280                $db_config = $xoopsDB->prefix('config');
281           
282                $wu_sql  =      "SELECT conf_value FROM  $db_config ";
283                $wu_sql .=      "WHERE (conf_modid = $mid ) AND (conf_name LIKE '$conf_name')";
284                $wu_res = $xoopsDB->queryF($wu_sql, 0, 0);
285                       
286                if ($wu_res === false){
287                        return 0;
288                } else {
289                        $xu_row = $xoopsDB->fetchArray($wu_res);
290                        return $xu_row['conf_value'];
291                }
292        }
293       
294}       
295?>
Note: See TracBrowser for help on using the repository browser.