root/wp-useronline/trunk/wp-useronline.php

Revision 52949, 38.4 kB (checked in by GamerZ, 4 days ago)

Remove nested dirname() call

Line 
1 <?php
2 /*
3 Plugin Name: WP-UserOnline
4 Plugin URI: http://lesterchan.net/portfolio/programming/php/
5 Description: Enable you to display how many users are online on your Wordpress blog with detailed statistics of where they are and who there are(Members/Guests/Search Bots).
6 Version: 2.31
7 Author: Lester 'GaMerZ' Chan
8 Author URI: http://lesterchan.net
9 */
10
11
12 /* 
13     Copyright 2008  Lester Chan  (email : lesterchan@gmail.com)
14
15     This program is free software; you can redistribute it and/or modify
16     it under the terms of the GNU General Public License as published by
17     the Free Software Foundation; either version 2 of the License, or
18     (at your option) any later version.
19
20     This program is distributed in the hope that it will be useful,
21     but WITHOUT ANY WARRANTY; without even the implied warranty of
22     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23     GNU General Public License for more details.
24
25     You should have received a copy of the GNU General Public License
26     along with this program; if not, write to the Free Software
27     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28 */
29
30
31 ### Load WP-Config File If This File Is Called Directly
32 if (!function_exists('add_action')) {
33     $wp_root = '../../..';
34     if (file_exists($wp_root.'/wp-load.php')) {
35         require_once($wp_root.'/wp-load.php');
36     } else {
37         require_once($wp_root.'/wp-config.php');
38     }
39 }
40
41
42 ### Use WordPress 2.6 Constants
43 if (!defined('WP_CONTENT_DIR')) {
44     define( 'WP_CONTENT_DIR', ABSPATH.'wp-content');
45 }
46 if (!defined('WP_CONTENT_URL')) {
47     define('WP_CONTENT_URL', get_option('siteurl').'/wp-content');
48 }
49 if (!defined('WP_PLUGIN_DIR')) {
50     define('WP_PLUGIN_DIR', WP_CONTENT_DIR.'/plugins');
51 }
52 if (!defined('WP_PLUGIN_URL')) {
53     define('WP_PLUGIN_URL', WP_CONTENT_URL.'/plugins');
54 }
55
56
57 ### Create Text Domain For Translations
58 add_action('init', 'useronline_textdomain');
59 function useronline_textdomain() {
60     if (!function_exists('wp_print_styles')) {
61         load_plugin_textdomain('wp-useronline', 'wp-content/plugins/wp-useronline');
62     } else {
63         load_plugin_textdomain('wp-useronline', false, 'wp-useronline');
64     }
65 }
66
67
68 ### UserOnline Table Name
69 global $wpdb;
70 $wpdb->useronline = $wpdb->prefix.'useronline';
71
72
73 ### Function: WP-UserOnline Menu
74 add_action('admin_menu', 'useronline_menu');
75 function useronline_menu() {
76     if (function_exists('add_submenu_page')) {
77         add_submenu_page('index.php'__('WP-UserOnline', 'wp-useronline'),  __('WP-UserOnline', 'wp-useronline'), 1, 'wp-useronline/wp-useronline.php', 'display_useronline');
78     }
79     if (function_exists('add_options_page')) {
80         add_options_page(__('Useronline', 'wp-useronline'), __('Useronline', 'wp-useronline'), 'manage_options', 'wp-useronline/useronline-options.php');
81     }
82 }
83
84
85 ### Function: Displays UserOnline Header
86 add_action('wp_head', 'useronline_header');
87 function useronline_header() {
88     wp_register_script('wp-useronline', WP_PLUGIN_URL.'/wp-useronline/useronline-js-packed.js', false, '2.31');
89     echo "\n".'<!-- Start Of Script Generated By WP-UserOnline 2.31 -->'."\n";
90     echo '<script type="text/javascript">'."\n";
91     echo '/* <![CDATA[ */'."\n";
92     echo "\t".'var useronline_ajax_url = \''.WP_PLUGIN_URL.'/wp-useronline/wp-useronline.php'."';\n";
93     echo "\t".'var useronline_timeout = '.(get_option('useronline_timeout')*1000).';'."\n";
94     echo '/* ]]> */'."\n";
95     echo '</script>'."\n";
96     wp_print_scripts(array('sack', 'wp-useronline'));
97     echo '<!-- End Of Script Generated By WP-UserOnline 2.31 -->'."\n";
98 }
99
100
101 ### Function: Process UserOnline
102 add_action('admin_head', 'useronline');
103 add_action('wp_head', 'useronline');
104 function useronline() {
105     global $wpdb, $useronline;
106     // Useronline Settings
107     $timeoutseconds = get_option('useronline_timeout');
108     $timestamp = current_time('timestamp');
109     $timeout = ($timestamp-$timeoutseconds);
110     $ip = get_ipaddress();
111     $url = addslashes(urlencode($_SERVER['REQUEST_URI']));
112     $referral = '';
113     $useragent = $_SERVER['HTTP_USER_AGENT'];
114     $current_user = wp_get_current_user();
115     if(!empty($_SERVER['HTTP_REFERER'])) {
116         $referral = addslashes(urlencode(strip_tags($_SERVER['HTTP_REFERER'])));
117     }
118     // Check For Bot
119     $bots = get_option('useronline_bots');
120     foreach ($bots as $name => $lookfor) {
121         if (stristr($useragent, $lookfor) !== false) {
122             $user_id = 0;
123             $display_name = addslashes($name);
124             $user_name = addslashes($lookfor);       
125             $type = 'bot';
126             $where = "WHERE ip = '$ip'";
127             $bot_found = true;
128             break;
129         }
130     }
131
132     // If No Bot Is Found, Then We Check Members And Guests
133     if(!$bot_found) {
134         // Check For Member
135         if($current_user->ID > 0) {
136             $user_id = $current_user->ID;
137             $display_name = addslashes($current_user->display_name);
138             $user_name = addslashes($current_user->user_login);       
139             $type = 'member';
140             $where = "WHERE userid = '$user_id'";
141         // Check For Comment Author (Guest)
142         } elseif(!empty($_COOKIE['comment_author_'.COOKIEHASH])) {
143             $user_id = 0;
144             $display_name = addslashes(trim($_COOKIE['comment_author_'.COOKIEHASH]));
145             $user_name = __('guest', 'wp-useronline').'_'.$display_name;       
146             $type = 'guest';
147             $where = "WHERE ip = '$ip'";
148         // Check For Guest
149         } else {
150             $user_id = 0;
151             $display_name = __('Guest', 'wp-useronline');
152             $user_name = "guest";       
153             $type = 'guest';
154             $where = "WHERE ip = '$ip'";
155         }
156     }
157
158     // Get User Agent
159     $useragent = addslashes($useragent);
160
161     // Check For Page Title
162     $make_page = wp_title('&raquo;', false);
163     if(empty($make_page)) {
164         $make_page = get_bloginfo('name');
165     } elseif(is_single()) {
166         $make_page = get_bloginfo('name').' &raquo; '.__('Blog Archive', 'wp-useronline').' '.$make_page;
167     } else {
168         $make_page = get_bloginfo('name').$make_page;
169     }
170     $make_page = addslashes($make_page);
171     
172     // Delete Users
173     $delete_users = $wpdb->query("DELETE FROM $wpdb->useronline $where OR (timestamp < $timeout)");
174     
175     // Insert Users
176     $insert_user = $wpdb->query("INSERT INTO $wpdb->useronline VALUES ('$timestamp', '$user_id', '$user_name', '$display_name', '$useragent', '$ip', '$make_page', '$url', '$type', '$referral')");
177
178     // Count Users Online
179     $useronline = intval($wpdb->get_var("SELECT COUNT(*) FROM $wpdb->useronline"));
180     
181     // Get Most User Online
182     $most_useronline = intval(get_option('useronline_most_users'));
183
184     // Check Whether Current Users Online Is More Than Most Users Online
185     if($useronline > $most_useronline) {
186         update_option('useronline_most_users', $useronline);
187         update_option('useronline_most_timestamp', current_time('timestamp'));
188     }
189 }
190
191
192 ### Function: Display UserOnline
193 if(!function_exists('get_useronline')) {
194     function get_useronline($display = true) {
195         // Template - Naming Conventions
196         $useronline_naming = get_option('useronline_naming');
197         // Template - User(s) Online
198         $template_useronline = stripslashes(get_option('useronline_template_useronline'));
199         $template_useronline = str_replace('%USERONLINE_PAGE_URL%', get_option('useronline_url'), $template_useronline);
200         $template_useronline = str_replace('%USERONLINE_MOSTONLINE_COUNT%', number_format_i18n(get_most_useronline()), $template_useronline);
201         $template_useronline = str_replace('%USERONLINE_MOSTONLINE_DATE%', get_most_useronline_date(), $template_useronline);
202         if(get_useronline_count() == 1) {
203             $template_useronline = str_replace('%USERONLINE_USERS%', stripslashes($useronline_naming['user']), $template_useronline);           
204         } else {
205             $useronline_naming_users = str_replace('%USERONLINE_COUNT%', number_format_i18n(get_useronline_count()), stripslashes($useronline_naming['users']));
206             $template_useronline = str_replace('%USERONLINE_USERS%', $useronline_naming_users, $template_useronline);
207         }
208         if($display) {
209             echo $template_useronline;
210         } else {
211             return $template_useronline;
212         }
213     }
214 }
215
216
217 ### Function: Display UserOnline Count
218 if(!function_exists('get_useronline_count')) {
219     function get_useronline_count($display = false) {
220         global $useronline;
221         if($display) {
222             echo number_format_i18n($useronline);
223         } else {
224             return $useronline;
225         }
226     }
227 }
228
229
230 ### Function: Display Max UserOnline
231 if(!function_exists('get_most_useronline')) {
232     function get_most_useronline($display = false) {
233         $most_useronline_users = intval(get_option('useronline_most_users'));
234         if($display) {
235             echo number_format_i18n($most_useronline_users);
236         } else {
237             return $most_useronline_users;
238         }
239     }
240 }
241
242
243 ### Function: Display Max UserOnline Date
244 if(!function_exists('get_most_useronline_date')) {
245     function get_most_useronline_date($display = false) {
246         $most_useronline_timestamp = get_option('useronline_most_timestamp');
247         $most_useronline_date = mysql2date(sprintf(__('%s @ %s', 'wp-useronline'), get_option('date_format'), get_option('time_format')), gmdate('Y-m-d H:i:s', $most_useronline_timestamp));
248         if($display) {
249             echo $most_useronline_date;
250         } else {
251             return $most_useronline_date;
252         }
253     }
254 }
255
256
257 ### Function Check If User Is Online
258 function is_online($user_login) {
259     global $wpdb;
260     $is_online = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->useronline WHERE username = '$user_login' LIMIT 1");
261     return intval($is_online);
262 }
263
264
265
266 ### Function: Update Member last Visit
267 //add_action('wp_head', 'update_memberlastvisit');
268 function update_memberlastvisit() {
269     global $current_user, $user_ID;
270     if(!empty($current_user) && intval($user_ID) > 0) {
271         update_user_option($user_ID, 'member_last_login', current_time('timestamp'));   
272     }
273 }
274
275
276 ### Function: Get Member last Visit
277 function get_memberlastvisit($user_id = 0) {
278     $date_format = sprintf(__('%s @ %s', 'wp-useronline'), get_option('date_format'), get_option('time_format'));
279     if($user_id == 0) {
280         return mysql2date($date_format, gmdate('Y-m-d H:i:s', get_user_option('member_last_login')));
281     } else {
282         return mysql2date($date_format, gmdate('Y-m-d H:i:s', get_user_option('member_last_login', $user_id)));
283     }
284 }
285
286
287 ### Function: Display Users Browsing The Site
288 function get_users_browsing_site($display = true) {
289     global $wpdb;
290
291     // Get Users Browsing Site
292     $page_url = addslashes(urlencode($_SERVER['REQUEST_URI']));
293     $users_browse = $wpdb->get_results("SELECT displayname, type FROM $wpdb->useronline ORDER BY type");
294
295     // Variables
296     $members = array();
297     $guests = array();
298     $bots = array();
299     $total_members = 0;
300     $total_guests = 0;
301     $total_bots = 0;
302     $nicetext_members = '';
303     $nicetext_guests = '';
304     $nicetext_bots = '';
305
306     // If There Is Users Browsing, Then We Execute
307     if($users_browse) {
308         // Get Users Information
309         foreach($users_browse as $user_browse) {
310             switch($user_browse->type) {
311                 case 'member':
312                     $members[] = stripslashes($user_browse->displayname);
313                     $total_members++;
314                     break;
315                 case 'guest':                       
316                     $guests[] = stripslashes($user_browse->displayname);
317                     $total_guests++;
318                     break;
319                 case 'bot':
320                     $bots[] = stripslashes($user_browse->displayname);
321                     $total_bots++;
322                     break;
323             }
324         }
325
326         // If We Do Not Display It, Return Respective Users Count
327         if(!$display) {
328             return array($total_members, $total_guests, $total_bots);
329         }
330         
331         // Template - Naming Conventions
332         $useronline_naming = get_option('useronline_naming');
333
334         // Template - User(s) Browsing Site
335         $options_browsingsite = get_option('useronline_template_browsingsite');
336         $separator_members_browsingsite = stripslashes($options_browsingsite[0]);
337         $separator_guests_browsingsite = stripslashes($options_browsingsite[1]);
338         $separator_bots_browsingsite = stripslashes($options_browsingsite[2]);
339         $template_browsingsite = stripslashes($options_browsingsite[3]);
340
341         // Nice Text For Users
342         if(get_useronline_count() == 1) {
343             $template_browsingsite = str_replace('%USERONLINE_USERS%', stripslashes($useronline_naming['user']), $template_browsingsite);       
344         } else {
345             $useronline_naming_users = str_replace('%USERONLINE_COUNT%', number_format_i18n(get_useronline_count()), stripslashes($useronline_naming['users']));
346             $template_browsingsite = str_replace('%USERONLINE_USERS%', $useronline_naming_users, $template_browsingsite);
347         }
348
349         // Print Member Name
350         if($members) {
351             $temp_member = '';
352             if(!function_exists('get_totalposts')) {
353                 foreach($members as $member) {
354                     $temp_member .= $member.$separator_members_browsingsite;
355                 }
356             } else {
357                 foreach($members as $member) {
358                     $temp_member .= useronline_stats_page_link($member).$separator_members_browsingsite;
359                 }
360             }
361             $template_browsingsite = str_replace('%USERONLINE_MEMBER_NAMES%', substr($temp_member, 0, -strlen($separator_members_browsingsite)), $template_browsingsite);
362         } else {
363             $template_browsingsite = str_replace('%USERONLINE_MEMBER_NAMES%', '', $template_browsingsite);
364         }
365
366         // Nice Text For Members
367         if($total_members > 1) {
368             $useronline_naming_members = str_replace('%USERONLINE_COUNT%', number_format_i18n($total_members), stripslashes($useronline_naming['members']));
369             $template_browsingsite = str_replace('%USERONLINE_MEMBERS%', $useronline_naming_members, $template_browsingsite);
370         } elseif($total_members == 1) {
371             $template_browsingsite = str_replace('%USERONLINE_MEMBERS%', stripslashes($useronline_naming['member']), $template_browsingsite);
372         } else {
373             $template_browsingsite = str_replace('%USERONLINE_MEMBERS%', '', $template_browsingsite);
374         }
375         
376         // Nice Text For Guests
377         if($total_guests > 1) {
378             $useronline_naming_guests = str_replace('%USERONLINE_COUNT%', number_format_i18n($total_guests), stripslashes($useronline_naming['guests']));
379             $template_browsingsite = str_replace('%USERONLINE_GUESTS%', $useronline_naming_guests, $template_browsingsite);
380         } elseif($total_guests == 1) {           
381             $template_browsingsite = str_replace('%USERONLINE_GUESTS%', stripslashes($useronline_naming['guest']), $template_browsingsite);
382         } else {
383             $template_browsingsite = str_replace('%USERONLINE_GUESTS%', '', $template_browsingsite);
384         }
385
386         // Nice Text For Bots
387         if($total_bots > 1) {
388             $useronline_naming_bots = str_replace('%USERONLINE_COUNT%', number_format_i18n($total_bots), stripslashes($useronline_naming['bots']));
389             $template_browsingsite = str_replace('%USERONLINE_BOTS%', $useronline_naming_bots, $template_browsingsite);
390         } elseif($total_bots == 1) {           
391             $template_browsingsite = str_replace('%USERONLINE_BOTS%', stripslashes($useronline_naming['bot']), $template_browsingsite);
392         } else {
393             $template_browsingsite = str_replace('%USERONLINE_BOTS%', '', $template_browsingsite);
394         }
395         // Seperators
396         if($total_members > 0 && $total_guests > 0) {
397             $template_browsingsite = str_replace('%USERONLINE_GUESTS_SEPERATOR%', $separator_guests_browsingsite, $template_browsingsite);
398         } else {
399             $template_browsingsite = str_replace('%USERONLINE_GUESTS_SEPERATOR%', '', $template_browsingsite);
400         }
401         if(($total_guests > 0 || $total_members > 0) && $total_bots > 0) {
402             $template_browsingsite = str_replace('%USERONLINE_BOTS_SEPERATOR%', $separator_bots_browsingsite, $template_browsingsite);
403         } else {
404             $template_browsingsite = str_replace('%USERONLINE_BOTS_SEPERATOR%', '', $template_browsingsite);
405         }
406
407         // Output The Template
408         echo $template_browsingsite;
409     } else {
410         // This Should Not Happen
411         _e('No User Is Browsing This Site', 'wp-useronline');
412     }
413 }
414
415
416 ### Function: Display Users Browsing The Page
417 function get_users_browsing_page($display = true) {
418     global $wpdb;
419
420     // Get Users Browsing Page
421     $page_url = addslashes(urlencode($_SERVER['REQUEST_URI']));
422     $users_browse = $wpdb->get_results("SELECT displayname, type FROM $wpdb->useronline WHERE url = '$page_url' ORDER BY type");
423
424     // Variables
425     $members = array();
426     $guests = array();
427     $bots = array();
428     $total_users = 0;
429     $total_members = 0;
430     $total_guests = 0;
431     $total_bots = 0;
432     $nicetext_members = '';
433     $nicetext_guests = '';
434     $nicetext_bots = '';
435
436     // If There Is Users Browsing, Then We Execute
437     if($users_browse) {
438         // Reassign Bots Name
439         $bots = get_option('useronline_bots');
440         $bots_name = array();
441         foreach($bots as $botname => $botlookfor) {
442             $bots_name[] = $botname;
443         }
444         // Get Users Information
445         foreach($users_browse as $user_browse) {
446             switch($user_browse->type) {
447                 case 'member':
448                     $members[] = stripslashes($user_browse->displayname);
449                     $total_members++;
450                     break;
451                 case 'guest':                       
452                     $guests[] = stripslashes($user_browse->displayname);
453                     $total_guests++;
454                     break;
455                 case 'bot':
456                     $bots[] = stripslashes($user_browse->displayname);
457                     $total_bots++;
458                     break;
459             }
460         }
461         $total_users = ($total_guests+$total_bots+$total_members);
462
463         // If We Do Not Display It, Return Respective Users Count
464         if(!$display) {
465             return array ($total_users, $total_members, $total_guests, $total_bots);
466         }
467
468         // Template - Naming Conventions
469         $useronline_naming = get_option('useronline_naming');
470
471         // Template - User(s) Browsing Site
472         $options_browsingpage = get_option('useronline_template_browsingpage');
473         $separator_members_browsingpage = stripslashes($options_browsingpage[0]);
474         $separator_guests_browsingpage = stripslashes($options_browsingpage[1]);
475         $separator_bots_browsingpage = stripslashes($options_browsingpage[2]);
476         $template_browsingpage = stripslashes($options_browsingpage[3]);
477
478         // Nice Text For Users
479         if($total_users == 1) {
480             $template_browsingpage = str_replace('%USERONLINE_USERS%', stripslashes($useronline_naming['user']), $template_browsingpage);       
481         } else {
482             $useronline_naming_users = str_replace('%USERONLINE_COUNT%', number_format_i18n($total_users), stripslashes($useronline_naming['users'<