XPressME Integration Kit

Trac

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

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

ナビアイコン設定時、ソースが有効であるかチェックする Fixes#361

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