XPressME Integration Kit

Trac

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

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

Database::getInstance()を使わずに、global $xoopsDB を使用する。(xoops3対策)

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