XPressME Integration Kit

Trac

source: branches/Ver3.0/xpressme_integration_kit/include/xpress_block_render.php @ 757

Last change on this file since 757 was 757, checked in by toemon, 13 years ago

ConfigFromXoopsクラスを廃止し、modInfoクラスを使用するように変更
WP2.2以下で使用するテンプレートold_templateの廃止

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