XPressME Integration Kit

Trac

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

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

ブロックテンプレートがファイルテンプレートになってしまうバグ修正 Fixes #276

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