XPressME Integration Kit

Trac

source: trunk/xpressme_integration_kit/wp-content/plugins/xpressme/include/xpress_common_functions.php @ 540

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

投稿後のブロックのキャッシュリフレッシュでエラーが発生するバグ修正 Fixes #304

File size: 12.7 KB
Line 
1<?php
2global $xoops_config;
3if (!is_object($xoops_config)){ // is call other modules
4        require_once dirname(dirname(dirname(dirname(dirname( __FILE__ ))))) .'/class/config_from_xoops.class.php' ;
5        $xoops_config = new ConfigFromXoops;
6}
7
8function get_xoops_config($config_name,$module_dir){
9        global $xoops_db;
10       
11        $modules_db = get_xoops_prefix() . 'modules';
12        $config_db = get_xoops_prefix() . 'config';
13
14        $moduleID = $xoops_db->get_var("SELECT mid FROM $modules_db WHERE dirname = '$module_dir'");
15        if (empty($moduleID)) return null;
16        $conf_value = $xoops_db->get_var("SELECT conf_value FROM $config_db WHERE (conf_modid = $moduleID) AND (conf_name = '$config_name')");
17        if (empty($conf_value)) return null;
18        return  $conf_value;
19}
20
21// xoops db
22function get_xpress_dir_path()
23{
24        return ABSPATH;
25}
26
27function get_xpress_dir_name()
28{
29        return basename(ABSPATH);
30}
31
32function get_wp_prefix_only()
33{
34        $dir_name = get_xpress_dir_name();
35        $prefix = $dir_name;
36        if ($prefix == 'wordpress') $prefix = 'wp';
37       
38        $prefix = $prefix . '_';
39        return $prefix;
40}
41
42function get_xoops_prefix()
43{
44        global $xoops_config;
45        $ret =$xoops_config->xoops_db_prefix . '_';
46        return $ret;
47}
48
49function get_xoops_trust_path()
50{
51        global $xoops_config;
52        $ret =$xoops_config->xoops_trust_path;
53        return $ret;
54}
55
56function get_xoops_root_path()
57{
58        global $xoops_config;
59        $ret =$xoops_config->xoops_root_path;
60        return $ret;
61}
62
63function get_wp_prefix()
64{
65        $prefix = get_xoops_prefix() . get_wp_prefix_only();
66        return $prefix;
67}
68function get_xoops_url()
69{
70        global $xoops_config;
71        $ret =$xoops_config->xoops_url ;
72        return $ret;
73}
74
75function get_xpress_url()
76{
77        global $xoops_config;
78        $ret =$xoops_config->module_url ;
79        return $ret;
80}
81
82function get_xpress_modid()
83{
84        global $xoops_db;
85       
86        $modulename = get_xpress_dir_name();   
87        $sql = "SELECT mid FROM " . get_xoops_prefix() . "modules WHERE dirname = '$modulename'";
88        $mid = $xoops_db->get_var($sql);
89        return $mid;   
90}
91
92function get_xpress_db_version()
93{
94        include get_xpress_dir_path() . '/wp-includes/version.php';
95        return $wp_db_version;
96}
97
98function is_xpress_mobile()
99{
100        //ktai_style
101        if (function_exists('is_ktai')){
102                if (is_ktai()) {
103 //                     $file_path = $GLOBALS['xoopsModuleConfig']["ktai_style_tmpdir"] . '/comments.php';
104                        return true;
105                }
106        }
107       
108        //mobg
109        if (function_exists('is_mobile')) {
110                if (is_mobile()){
111                        return true;
112                }
113        }
114        if (
115          preg_match("/DoCoMo/", $_SERVER['HTTP_USER_AGENT']) ||
116          preg_match("/softbank/", $_SERVER['HTTP_USER_AGENT']) ||
117          preg_match("/vodafone/", $_SERVER['HTTP_USER_AGENT']) ||
118          preg_match("/J-PHONE/", $_SERVER['HTTP_USER_AGENT']) ||
119          preg_match("/UP\.Browser/", $_SERVER['HTTP_USER_AGENT']) ||
120          preg_match("/ASTEL/", $_SERVER['HTTP_USER_AGENT']) ||
121          preg_match("/PDXGW/", $_SERVER['HTTP_USER_AGENT'])
122        )
123        {
124                return true;
125        } else {
126                return false;
127        }
128}
129
130function block_cache_refresh()
131{
132        global $xoops_db;
133        $mid = get_xpress_modid();
134        $sql = "SELECT bid,options,func_file FROM " . get_xoops_prefix() . "newblocks WHERE mid = $mid";
135        $blocks = $xoops_db->get_results($sql);
136        $mydirname = get_xpress_dir_name();
137        require_once get_xpress_dir_path() . '/include/xpress_block_render.php';
138
139        foreach($blocks as $block){
140                $func_file = $block->func_file;
141               
142                // Avoid the failure of the operation when switch_to_blog() and other plugin code is called on the admin page.
143                $excludes = '|global_recent_posts_list_block.php|my_.*_block.php|';
144                if (preg_match('/' . $excludes . '/' , $func_file)) continue;
145               
146                $call_theme_function_name = str_replace(".php", "", $func_file);
147                $inc_theme_file_name = str_replace(".php", "", $func_file) . '_theme.php';
148                $cache_title = str_replace(".php", "", $func_file);
149                $blockID = $block->bid;
150                $options = explode("|", $block->options);
151
152                $block_theme_file = get_block_file_path($mydirname,$inc_theme_file_name);
153                require_once $block_theme_file['file_path'];
154                $block_render = $call_theme_function_name($options);            //The block name and the called function name should be assumed to be the same name.                     
155                $xml['block'] = $block_render;
156                $xml['block']['options'] = $block->options;
157                xpress_block_cache_write($mydirname,$cache_title. $blockID, $xml);
158        }
159}
160function is_wordpress_style()
161{
162        global $xpress_config;
163       
164        if ($xpress_config->viewer_type == 'wordpress') return true;
165        if ($xpress_config->viewer_type == 'xoops') return false;
166       
167        // user select
168        $get_style = isset($_GET["style"]) ? $_GET["style"] : '';
169        $cookie_style = isset($_COOKIE["xpress_style"]) ? $_COOKIE["xpress_style"] : '';
170       
171        // set style
172        if (!empty($get_style)){
173                $style = $get_style;
174        } else {
175                if (!empty($cookie_style)){
176                        $style = $cookie_style;
177                } else {
178                        $style = 'x';
179                }
180        }
181       
182        // set cookie
183        if (empty($cookie_style)){
184                setcookie("xpress_style", $style);
185                $_COOKIE["xpress_style"] = $style;
186        } else {
187                if ($style != $cookie_style) {
188                        setcookie("xpress_style", $style);
189                        $_COOKIE["xpress_style"] = $style;
190                }
191        }
192        if ($style == 'w') {
193                return true;
194        } else {
195                return false;
196        }
197}
198
199function wp_meta_add_xpress_menu()
200{
201        global $xpress_config;
202        if ($xpress_config->viewer_type == 'user_select'){
203                echo disp_mode_set();
204        }
205        if (function_exists('wp_theme_switcher') ) {   
206                echo '<li>' . __('Themes') . ':';
207                wp_theme_switcher('dropdown');
208                echo '</li>';
209        }
210}
211
212function disp_mode_set(){
213        global $xpress_config;
214       
215        $select ="";
216        if ($xpress_config->viewer_type == 'user_select'){
217                $style = isset($_GET["style"]) ? $_GET["style"] : (isset($_COOKIE["xpress_style"]) ? $_COOKIE["xpress_style"] : "");
218
219                switch($style) {
220                case 'w':
221                        $select ='<li><a href="'.get_settings('siteurl').'/?style=x" title="'. __('Switch to XOOPS mode','xpressme').'">'.__('Switch to XOOPS mode','xpressme').'</a></li>';
222//                      $select.='<img src="'. get_settings('siteurl').'/images/external.png" alt="'.__('Switch to XOOPS mode','xpressme') . '"></a></li>';
223                        break;
224                case 'x':
225                        $select='<li><a href="'.get_settings('siteurl').'/?style=w" title="'.__('Switch to WordPress mode','xpressme').'">'.__('Switch to WordPress mode','xpressme').'</a></li>';
226                        break;
227                default:
228                        $select='<li><a href="'.get_settings('siteurl').'/?style=w" title="'.__('Switch to WordPress mode','xpressme').'">'.__('Switch to WordPress mode','xpressme').'</a></li>';
229                        break;
230                }
231        }
232        return $select;
233}
234
235function xpress_comment_count( $count ) {
236        global $id;
237        $post_comments =get_comments('status=approve&post_id=' . $id);
238        $comments_by_type = &separate_comments($post_comments);
239        return count($comments_by_type['comment']);
240}
241
242function xpress_set_author_cookie($query_vars)
243{
244        global $wp , $wpdb;
245       
246        if (is_admin()) return $query_vars;
247       
248        $author_cookie = 'select_' . get_xpress_dir_name() . "_author" ;
249        if(xpress_is_multi_user()){
250                if (!empty($_GET)){
251                        $auth = intval( @$_GET["author"] );
252                        if ($auth > 0){
253                                setcookie($author_cookie, $auth, time()+3600, COOKIEPATH);
254                                $_COOKIE[$author_cookie] = $auth;
255                        }
256                } else {
257                        if(xpress_is_wp_version('<','2.1')){  // Maybe, I think that it is ver2.1 or less.
258                                if (!empty($wp->matched_query) ){
259                                        if (strpos($wp->matched_query,'author_name') !== false ){
260                                                $pattern = "author_name\s*=\s*(.*)\s*";
261                                                if ( preg_match ( "/".$pattern."/i", $wp->matched_query, $match ) ){
262                                                        $author_name = "$match[1]";
263                                                        $auth = $wpdb->get_var("SELECT ID FROM $wpdb->users WHERE user_login = '$author_name'");
264
265                                                        setcookie($author_cookie, $auth, time()+3600, COOKIEPATH);
266                                                        $_COOKIE[$author_cookie] = $auth;
267                                                }
268                                        }
269                                } else {
270                                        setcookie($author_cookie, 0, time()+3600, COOKIEPATH);
271                                        $_COOKIE[$author_cookie] = 0;
272                                }
273                        } else {
274                                if (!empty($wp->query_vars) ){
275                                        if (!empty($wp->query_vars['author_name']) ){
276                                                $author_name = $wp->query_vars['author_name'];
277                                                $auth = $wpdb->get_var("SELECT ID FROM $wpdb->users WHERE user_login = '$author_name'");
278
279                                                setcookie($author_cookie, $auth, time()+3600, COOKIEPATH);
280                                                $_COOKIE[$author_cookie] = $auth;
281                                        }
282                                } else {
283                                        setcookie($author_cookie, 0, time()+3600, COOKIEPATH);
284                                        $_COOKIE[$author_cookie] = 0;
285                                }
286                        }
287                }
288        }else{
289        //      $GLOBALS["wp_xoops_author"] = null;
290                setcookie($author_cookie, 0, time()+3600, COOKIEPATH);
291                $_COOKIE[$author_cookie] = 0;
292        }
293        return $query_vars;
294}
295
296function xpress_query_filter($query)
297{
298        if (is_admin()) return $query;
299
300        $author_cookie = 'select_' . get_xpress_dir_name() . "_author" ;
301       
302        if (strpos($query,'SELECT') === false)  return $query;
303
304        $select_pattern = "SELECT(.*)post_author(.*)FROM";
305        if (preg_match ( "/".$select_pattern."/i", $query, $select_match ))
306                return $query;
307
308        $query = preg_replace('/\s\s+/', ' ', $query);
309        if (!empty($_COOKIE[$author_cookie])){
310                if(xpress_is_wp_version('<','2.1')){
311                        $pattern = "WHERE.*AND\s?\(*post_author\s*=";
312                        if ( preg_match ( "/".$pattern."/i", $query, $match ) ){
313                                return $query;
314                        }
315                        $pattern = "WHERE\s?post_author\s*="; // get_usernumposts()
316                        if ( preg_match ( "/".$pattern."/i", $query, $match ) ){
317                                return $query;
318                        }
319                        $pattern = "WHERE.*post_status\s*=\s*'publish'\s*\)?";
320                        if ( preg_match ( "/".$pattern."/i", $query, $match ) ){
321                               
322                                $where_str = "$match[0]";
323                                $where_arry = split(' ',$where_str);
324                                $post_prefix = '';
325                                foreach ($where_arry as $p){
326                                        if ( preg_match ( "/post_status/", $p, $match3 ) ){
327                                                $post_prefix = preg_replace("/post_status/", "", $p);
328                                                $post_prefix = preg_replace("/\(/", "", $post_prefix);
329                                                break;
330                                        }
331                                }
332                                $patern = 'WHERE';                             
333                                $replace = "WHERE {$post_prefix}post_author = " . intval($_COOKIE[$author_cookie]) . " AND ";
334                                $query = preg_replace("/$patern/", $replace, $query);
335                        }
336                } else {
337                        $pattern = "WHERE.*post_type\s*=\s*'post'\s*\)?";                       
338                        if ( preg_match ( "/".$pattern."/i", $query, $match ) ){
339                                $where_str = "$match[0]";
340                                $where_arry = split(' ',$where_str);
341                                $post_prefix = '';
342                                foreach ($where_arry as $p){
343                                        if ( preg_match ( "/post_type/", $p, $match3 ) ){
344                                                $post_prefix = preg_replace("/post_type/", "", $p);
345                                                $post_prefix = preg_replace("/\(/", "", $post_prefix);
346                                                break;
347                                        }
348                                }
349                                preg_match ( "/post_type(.*)/", $where_str, $p_match );
350                                $patern_s = $p_match[0];
351                                $patern = preg_replace('/\)/', '\)', $patern_s);
352                               
353                                $replace = $patern_s . " AND {$post_prefix}post_author = " . intval($_COOKIE[$author_cookie]) . " ";
354
355                                $query = preg_replace("/$patern/", $replace, $query);
356                        }
357                }
358        }
359//      xpress_show_sql_quary($query);
360        return $query;
361}
362
363function get_block_file_path($mydirname,$file_name)
364{
365        global $xoops_config, $xpress_config;
366        $mydirpath = $xoops_config->xoops_root_path . '/modules/' . $mydirname;
367        $select_theme = xpress_ThemeTemplate(get_xpress_theme_name($mydirname));
368        $xpress_default_theme = 'xpress_default';
369        $select_block = '/wp-content/themes/' . $select_theme . '/blocks/' . $file_name;
370        $default_block = '/wp-content/themes/xpress_default/blocks/' . $file_name;
371        $select_block_path = $mydirpath . $select_block;
372        $default_block_path =  $mydirpath . $default_block;
373
374        $block_file_data = array();
375        $block_file_data['file_path'] = $default_block_path;
376        $block_file_data['error'] = '';
377
378        if($select_theme != $xpress_default_theme){
379                if (file_exists($select_block_path)){
380                        $select_block_version = get_block_version($select_block_path);
381                        $default_block_version = get_block_version($default_block_path);
382                        if (version_compare($select_block_version,$default_block_version, "<")){
383                                $block_file_data['file_path'] = $default_block_path;
384                                if ($xpress_config->is_block_error_display){
385                                        $error_str = '<div style="color:red">';
386                                        $error_str .= sprintf(__('Block file %1$s is an old version %2$s.<br />used block file %3$s of new version %4$s.','xpressme'),$select_block,$select_block_version,$default_block,$default_block_version);
387                                        $error_str .= '</div>';
388                                        $block_file_data['error'] = $error_str;
389                                }
390                        } else {
391                                $block_file_data['file_path'] = $select_block_path;
392                                $block_file_data['error'] = '';
393                        }
394                }
395        }
396        return $block_file_data;
397}
398
399function get_block_version($file_path = ''){
400        $array_file = file($file_path);
401        $pattern = '^[\s|\/]*[B|b]lock\s+[V|v]ersion\s*[:|;]\s*([0-9|.]*)';
402        $version = '0.1';
403        if (empty($file_path)) return $version;
404        if (!file_exists($file_path)) return $version;
405        if (count($array_file) > 5) $file_count = 5; else $file_count = count($array_file);
406        for ($i = 0 ; $i < $file_count ; $i++){
407                if (preg_match('/' . $pattern . '/' ,$array_file[$i],$matchs)){
408                        $version = $matchs[1];
409                        break;
410                }
411        }
412        return $version;
413}
414
415?>
Note: See TracBrowser for help on using the repository browser.