XPressME Integration Kit

Trac

Ticket #266: xpress_block_render.php

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