XPressME Integration Kit

Trac

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

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

xoops_mod_wordpress060_alphaの複製モジュールからのアップデート対応 fixes#335

File size: 11.3 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 = preg_replace('/wordpress/','wp',$mydirname);
87
88                        $module_tbl = $xoopsDB->prefix($wp_prefix).'_options'; 
89                        $theme_name = '';       
90
91                        $sql = "SELECT option_value FROM $module_tbl WHERE option_name LIKE 'template'";
92                        $result =  $xoopsDB->query($sql, 0, 0);
93                        if ($xoopsDB->getRowsNum($result)  > 0){
94                                $row = $xoopsDB->fetchArray($result);
95                                $theme_name = $row['option_value'];
96                        }
97                } else {
98                        $theme_name = get_option('template');
99                }
100                return $theme_name;
101        }
102
103        function xpress_block_header_set($mydirname = '')
104        {
105                require_once( dirname( __FILE__ ).'/xpress_block_header.php' );
106                $xml = xpress_block_header_cache_read($mydirname);
107                $block_header = $xml['block_header'];
108                if (!empty($block_header)){
109                        $tplVars =& $GLOBALS['xoopsTpl']->get_template_vars();
110                        if(array_key_exists('xoops_block_header', $tplVars)) {
111                                if (!strstr($tplVars['xoops_block_header'],$block_header)) {
112                                        $GLOBALS['xoopsTpl']->assign('xoops_block_header',$tplVars['xoops_block_header'].$block_header);
113                                }
114                        } else {
115                                $GLOBALS['xoopsTpl']->assign('xoops_block_header',$block_header);
116                        }
117                }
118        }
119       
120       
121    function xpress_block_cache_found($mydirname,$block_name)
122    {
123        global $xoops_config;
124        if(defined('XOOPS_ROOT_PATH')){
125                $cache_dir = XOOPS_ROOT_PATH . '/cache/';
126        } else {
127                $cache_dir = $xoops_config->xoops_root_path . '/cache/';
128        }
129        $xml_name = $block_name . '.xml';
130
131        $filename = $cache_dir .$mydirname . '_' . $xml_name;
132                $cache_time = 0;
133//        if (file_exists($filename) && ((time() - filemtime($filename)) < $cache_time)) {
134        if (file_exists($filename)) {
135            return true;
136       } else {
137                        return false;
138                }
139    }
140       
141        function xpress_block_render($mydirname,$block_function_name,$options)
142        {
143                global $wpdb,$xoops_config;
144                $func_file = $block_function_name;
145                $call_theme_function_name = str_replace(".php", "", $block_function_name);
146                $inc_theme_file_name = $call_theme_function_name . '_theme.php';
147                $cache_title = str_replace(".php", "", $block_function_name);
148                $blockID =get_block_id($mydirname,$func_file,$options);         
149
150                $this_block_url = '/' . $mydirname . '/';
151                $call_url = $_SERVER['REQUEST_URI'];
152               
153                if (strstr($call_url , $this_block_url) !== false && strstr($call_url , $this_block_url . 'admin/') === false){
154                        $block_theme_file = get_block_file_path($mydirname,$inc_theme_file_name);
155                        require_once $block_theme_file['file_path'];
156                        $block = $call_theme_function_name($options);           //The block name and the called function name should be assumed to be the same name.
157                        if (!empty($block_theme_file['error']))
158                                $block['err_message'] = $block_theme_file['error'];
159                } else {
160                        if (xpress_block_cache_found($mydirname,$cache_title. $blockID)){
161                                $xml = xpress_block_cache_read($mydirname,$cache_title. $blockID);
162                                $block = $xml['block'];
163                        } else {
164                                $block['err_message'] = sprintf(_MB_XP2_BLOCK_CACHE_ERR, '<a href="' . XOOPS_URL . '/modules/' . $mydirname . '">' . $mydirname .'</a>');
165                        }
166                        xpress_block_header_set($mydirname);
167                }
168                $temp_option = explode(':' , $options[1]);
169               
170                if (isset($temp_option[1])) {
171                        $templates_file = $options[1];
172                } else {
173                        $templates_file = 'db:'.$mydirname. '_' . str_replace(".php", ".html", $block_function_name);
174                }
175               
176                $tpl =& new XoopsTpl() ;
177                $tpl->template_dir = $xoops_config->module_path . '/templates';
178                if (!$tpl->template_exists($templates_file)){
179                        $src_file_path = $xoops_config->module_path . '/templates/' .$mydirname. '_' . str_replace(".php", ".html", $block_function_name);
180                        $templates_file = add_xpress_tpl($mydirname,$templates_file,$src_file_path);
181                }
182                $tpl->assign( 'block' , $block ) ;
183                $ret['content'] = $tpl->fetch( $templates_file ) ;
184                return $ret ;
185        }
186       
187        function add_xpress_tpl($mydirname,$templates='',$src_file_path){
188                global $wpdb,$xoops_config , $xoops_db;
189               
190                $mid = get_block_mid($mydirname);
191
192                $temp_parm = explode(':' , $templates);
193                if (empty($temp_parm[1])) {
194                        $filename=$temp_parm[0];
195                        $type = 'db';
196                } else  {
197                        $filename=$temp_parm[1];
198                        $type = $temp_parm[0];
199                }
200                $temp_file_path = $xoops_config->module_path . '/templates/'. $filename;
201                $pattern = '^' . $mydirname . '_';
202                if (preg_match('/' . $pattern . '/' , $filename, $match)){ // file prefix check
203                        if (!file_exists($temp_file_path)){             // Repetition check
204                                if (file_exists($src_file_path)){       // source file check
205                                        $rcd = copy($src_file_path, $temp_file_path);
206                                }
207                        }
208                        return  'file:' . $filename;
209                }
210                return $templates;
211        }
212       
213        function xpress_block_cache_refresh($mydirname)
214        {
215                global $xoops_db;
216                $mid = get_xpress_modid();
217               
218                // It is a block that needs cache arranged outside the module.
219                // Only the block arranged outside the module is detected here.
220                $newblocks = get_xoops_prefix() . "newblocks";
221                $block_module_link = get_xoops_prefix(). "block_module_link";
222                $sql  = "SELECT * FROM $newblocks LEFT JOIN $block_module_link ON {$newblocks}.bid = {$block_module_link}.block_id ";
223                $sql .= "WHERE {$newblocks}.mid = $mid AND {$newblocks}.visible = 1 AND {$block_module_link}.module_id != $mid ";
224                $sql .= "GROUP BY {$newblocks}.bid";
225
226                $blocks = $xoops_db->get_results($sql);
227                require_once get_xpress_dir_path() . '/include/xpress_block_render.php';
228
229                foreach($blocks as $block){
230                        $func_file = $block->func_file;
231                        $call_theme_function_name = str_replace(".php", "", $func_file);
232                        $inc_theme_file_name = str_replace(".php", "", $func_file) . '_theme.php';
233                        $cache_title = str_replace(".php", "", $func_file);
234                        $blockID = $block->bid;
235                        $options = explode("|", $block->options);
236
237                        $block_theme_file = get_block_file_path($mydirname,$inc_theme_file_name);
238                        require_once $block_theme_file['file_path'];
239                        $render = $call_theme_function_name($options);          //The block name and the called function name should be assumed to be the same name.                   
240                        $render_array['block'] = $render;
241                        $render_array['block']['options'] = $block->options;
242                        if (!empty($block_theme_file['error']))
243                                $render_array['block']['err_message'] = $block_theme_file['error'];
244                        if (xpress_block_cache_found($mydirname,$cache_title. $blockID)){       
245                                $render_serialize = xpress_XML_serialize($render_array);
246                                $render_md5 = md5($render_serialize);
247
248                                $cache_serialize = xpress_cache_read($mydirname,$cache_title. $blockID.'.xml');
249                                $cache_md5 = md5($cache_serialize);
250                               
251                                if ($render_md5 != $cache_md5){
252                                        xpress_block_cache_write($mydirname,$cache_title. $blockID, $render_array);
253                                }
254                        } else {
255                                xpress_block_cache_write($mydirname,$cache_title. $blockID, $render_array);
256                        }
257                }
258        }
259       
260        function xpress_unnecessary_block_cache_delete($mydirname)
261        {
262                global $xoops_db,$xoops_config;
263               
264                $mid = get_xpress_modid();
265                $sql = "SELECT bid,options,func_file FROM " . get_xoops_prefix() . "newblocks WHERE mid = $mid AND visible = 1";
266                $blocks = $xoops_db->get_results($sql);
267                require_once get_xpress_dir_path() . '/include/xpress_block_render.php';
268
269                $pattern ='';
270                foreach($blocks as $block){
271                        $cache_file_name = $mydirname . '_'. str_replace(".php", "", $block->func_file) . $block->bid;
272                        if (!empty($pattern))  $pattern .= '|';
273                        $pattern .= $cache_file_name;
274                }
275                $pattern = '(' . $pattern . ')';
276               
277                $cache_dir = $xoops_config->xoops_root_path . '/cache/';
278                $cache_time = 0;
279        if ($dh = opendir($cache_dir)) {
280            while (($file = readdir($dh)) !== false) {
281                if (preg_match('/^' . preg_quote($mydirname) . '_/', $file)) {
282                        if(! preg_match('/' . $pattern . '/', $file)) {
283                        unlink($cache_dir.$file);
284                    }
285                }
286            }
287            closedir($dh);
288        }
289    }
290   
291    function get_xpress_mod_config($mydirname,$conf_name=''){
292                $module_handler =& xoops_gethandler('module');
293                $xoopsModule =& $module_handler->getByDirname($mydirname);
294                $mid = $xoopsModule->getVar('mid');
295                $xoopsDB =& Database::getInstance();
296                $db_config = $xoopsDB->prefix('config');
297           
298                $wu_sql  =      "SELECT conf_value FROM  $db_config ";
299                $wu_sql .=      "WHERE (conf_modid = $mid ) AND (conf_name LIKE '$conf_name')";
300                $wu_res = $xoopsDB->queryF($wu_sql, 0, 0);
301                       
302                if ($wu_res === false){
303                        return 0;
304                } else {
305                        $xu_row = $xoopsDB->fetchArray($wu_res);
306                        return $xu_row['conf_value'];
307                }
308        }       
309}       
310?>
Note: See TracBrowser for help on using the repository browser.