XPressME Integration Kit

Trac

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

Last change on this file since 364 was 364, checked in by toemon, 15 years ago

EUC-JP時のxml_parseエラー修正 fixes #202 (thx goldmagic)

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