root/wp-slimstat-ex/tags/1.3/wp-slimstat-ex.php

Revision 70, 17.9 KB (checked in by 082net, 2 years ago)

## wp-slimstat-ex ##
small support for WP 2.1 (wp_enqueue_script)

Line 
1<?php
2/*
3Plugin Name: WP-SlimStat-Ex
4Plugin URI: http://082net.com/tag/wp-slimstat-ex/
5Description: Track your blog stats. Based on <a href="http://www.duechiacchiere.it/">Mr. Coolmann</a>'s <a href="http://www.duechiacchiere.it/wp-slimstat/">Wp-SlimStat</a>.
6Version: 1.3
7Author: Cheon, Young-Min
8Author URI: http://082net.com/
9*/
10
11/*
12Change Log::
13        v 1.3::
14                Tried to include files only when needed.
15                Support more browsers.
16                Changed way to detect language(language-region).
17                Support browser's back button with ajax
18                        - powered by ajax.history.js by Siegfried Puchbauer <rails-spinoffs@lists.rubyonrails.org>
19                You can display some modules on page by inserting {SLIMSTAT} to content or wpSSBlogStats to page templates.
20                Now use prototype.js again.
21                Added "display available modules" on SlimStat-Admin.
22                Fixed small bugs.
23
24        v 1.2::
25                Now use mootools.js(http://mootools.net)
26                Added utf-16le converting(for Korean)
27        v 1.1::
28                Localized SlimStat-Admin
29                Fixed Download Manager Pin referer error
30                Changed external tracking method
31                Fixed importing Wp-ShortStat
32                Importing Wp-ShortStat now support search string
33        v 1.0c::
34                Fixed mis-typed vist_type on option page
35        v1.0a::
36                Fixed SSDL_Mgr pin bug
37                Fixed guess post title error with default permalink structure
38        v1.0::
39                Added Performance tool to SlimStat-Ex-Admin
40                Added Disable AJAX option
41                Added time offset option :fix offet time between server and blog
42                Added manual remote ip filter option
43                Ability to track external PHP web tool on your server
44                Improved guess post title ability
45                Added wp_slimstat_ex::base (plugin file name related to plugins folder).
46                Open 'whois?' link to new sized window
47                Removed moo.fx.pack. (SmoothScroll(module anchor) function doesn't exists any more)
48                Modified codes for faster loading.(Wp-SlimStat-Ex is now more faster!)
49                Fixed time interval filter bug.
50                Pins::
51                        Remove Pins from table which dose not exists
52                        Added get_module_custom function for Pins
53                        Added whois link to SpamStats, PathStats Pin
54                        Added option to not show unblocked behavior(key:00000000) from SpamStats
55                        Added page link(more results) to PathStats and SpamStats(currently does not work with filter)
56
57ABOUT MODIFICATION ::
58This plugin is just ALPHA version and it may cause any trouble with your wordpress blog.
59If so, please let me know about that. (http://082net.com/about/#contact-me)
60
61Almost php and sql codes has written by Mr. Coolmann(http://www.duechiacchiere.it)
62What I've done is intergrating Ajax, constructing Pins(plugable panel) condition and some little patches.
63Thanks, Coolmann.
64
65License ::
66/*
67This program is free software; you can redistribute it and/or
68modify it under the terms of the GNU General Public License
69as published by the Free Software Foundation; either version 2
70of the License, or (at your option) any later version.
71
72This program is distributed in the hope that it will be useful,
73but WITHOUT ANY WARRANTY; without even the implied warranty of
74MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
75GNU General Public License for more details.
76
77http://www.gnu.org/licenses/gpl.txt
78
79You should have received a copy of the GNU General Public License
80along with this program; if not, write to the Free Software
81Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
82______________________________________________________________*/
83
84/*
85Original Authors(others) ::
86MooTools :: http://mootools.net by Mad4Milk (http://mad4milk.net/).
87icons :: Mark James(http://www.famfamfam.com/)
88Ajax.History :: Siegfried Puchbauer <rails-spinoffs@lists.rubyonrails.org>
89*/
90
91// Protect the script from direct access
92if ( !function_exists('is_admin') ) {
93        header("Location: /");
94        exit();
95}
96
97define('SLIMSTATPATH',  realpath( dirname( __FILE__ ) ) . '/');
98define('wp_slimstat_ex',  'wp_slimstat_ex');
99
100include_once(SLIMSTATPATH . 'wp-slimstat-ex-config.php');
101
102if ( !defined(WPLANG) || '' == constant('WPLANG') ) {
103        include_once(ABSPATH . WPINC . '/streams.php');
104        include_once(ABSPATH . WPINC . '/gettext.php');
105}
106
107load_plugin_textdomain('wp-slimstat-ex', 'wp-content/plugins/'.dirname($SlimCfg->base).'/lang');       
108
109class wp_slimstat_ex {
110
111        function add_js($admin = true) {
112                echo '
113                <!-- Added by Wp-SlimStat -->
114                '.(($admin)?$baseJS:'').'<script src="'.$SlimCfg->pluginURL.'/js/ajax-slimstat-proto.js" type="text/javascript"></script>';
115        }
116
117        function admin_head() {
118                global $SlimCfg, $wp_version;
119                $load_js = "";
120                if($wp_version < 2.1 && $SlimCfg->option['use_ajax']) {
121                        $js_before = '<script type="text/javascript" src="'.$SlimCfg->pluginURL.'/js/';
122                        $js_after = '"></script>'."\n";
123                $load_js .= $js_before.'prototype.js'.$js_after;
124                $load_js .= $js_before.'moo.fx.js'.$js_after;
125                $load_js .= $js_before.'ajax.history.js'.$js_after;
126//              $load_js .= $js_before.'dhtmlHistory.js'.$js_after;
127                $load_js .= $js_before.'ajax-slimstat-proto.js'.$js_after;
128                }
129                echo '
130<!-- Added by Wp-SlimStat-Ex '.$SlimCfg->version.' -->'."\n".$load_js.'<link rel="stylesheet" type="text/css" media="screen" href="'.$SlimCfg->pluginURL.'/css/slimstat.css" />';       
131        }
132
133        function enqueue_script() {
134                global $SlimCfg;
135                if($SlimCfg->option['use_ajax']) {
136                        $src_path = $SlimCfg->pluginURL.'/js/';
137                        wp_enqueue_script('prototype');
138                        wp_enqueue_script('moo.fx.js', $src_path.'moo.fx.js', array('prototype'), $SlimCfg->version);
139                        wp_enqueue_script('ajax.history', $src_path.'ajax.history.js', array('prototype'), $SlimCfg->version);
140                        wp_enqueue_script('ajax-slimstat-proto', $src_path.'ajax-slimstat-proto.js', array('prototype', 'moo.fx.js','ajax.history'), $SlimCfg->version);
141                }
142        }
143
144
145        function admin_footer() {
146                global $SlimCfg;
147                if(!$SlimCfg->option['use_ajax'])
148                        return;
149                $script = '<script type="text/javascript"><!--'."\n";
150                $script .= 'var historyHandler = Ajax.History.initialize({'."\n\t";
151                $script .= 'callback: function(hash){SlimStat.ajax(hash);},'."\n\t";
152                $script .= 'iframeSrc: "'.$SlimCfg->pluginURL.'/js/_blank.html"});'."\n";
153                $script .= '//--!></script>'."\n";
154                echo $script;
155        }
156
157        function wp_head() {
158                global $SlimCfg;
159                $load_js = '';
160                if($SlimCfg->option['blog_stats'] && $SlimCfg->option['blog_js'] !== 'none') {
161                        $js_before = '<script type="text/javascript" src="'.$SlimCfg->pluginURL.'/js/';
162                        $js_after = '"></script>'."\n\t";
163                        $load_js = $js_before.'ajax-slimstat-'.(($SlimCfg->option['blog_js'] == 'mootools')?'moo':'proto').'.js'.$js_after;
164                        $load_js .= ($SlimCfg->option['blog_js'] == 'prototype')?$js_before.'moo.fx.js'.$js_after:'';
165                }
166                $load_css = '<link rel="stylesheet" type="text/css" media="screen" href="'.$SlimCfg->pluginURL.'/css/slimstat-blog.css.php'.((!$SlimCfg->option['blog_stats'])?'?load=simple':'').'" />';
167                echo '
168                <!-- Added by Wp-SlimStat-Ex '.$SlimCfg->version.' -->
169                '.$load_js.$load_css.
170                ';
171        }
172
173        function option_page() {
174                global $SlimCfg;
175                if (function_exists('add_options_page')) {
176                        add_options_page("SlimStat", "SlimStat", 9, dirname(__FILE__).'/wp-slimstat-ex-options.php');
177                }
178        }
179
180        function is_slimstat_page () {
181                global $SlimCfg;
182                return (strpos($_GET['page'], $SlimCfg->base) !== false);
183        }
184
185        function _createSlimTable($table = 'stats', $check_only = false) {
186            global $wpdb, $SlimCfg;
187               
188                switch($table) {
189                        case 'country':
190                                $createTable = $SlimCfg->table_countries;
191                                $myTableStatsQuery = "CREATE TABLE `$createTable` (
192                                        `ip_from` int(10) unsigned default 0,
193                                        `ip_to` int(10) unsigned default 0,
194                                        `country_code` char(2) default '',
195                                        KEY `ip_from_to_idx` (`ip_from`,`ip_to`)
196                                        )";
197                                break;
198                        case 'feed':
199                                $createTable = $SlimCfg->table_feed;
200                                $myTableStatsQuery = "CREATE TABLE `$createTable` (
201                                        `id` int(11) unsigned NOT NULL auto_increment,
202                                        `remote_ip` int unsigned default 0,
203                                        `language` varchar(5) default '',
204                                        `country` varchar(2) default '',
205                                        `domain` varchar(255) default '',
206                                        `referer` varchar(255) default '',
207                                        `searchterms` varchar(255) default '',
208                                        `resource` varchar(255) default '',
209                                        `platform` TINYINT default -1,
210                                        `browser` SMALLINT default -1,
211                                        `version` varchar(15) default '',
212                                        `visit` int(10) unsigned NOT NULL default '0',
213                                        `dt` int(10) unsigned default 0,
214                                        UNIQUE KEY `id` (`id`),
215                                        KEY `searchterms` (`searchterms`),
216                                        KEY `visit` (`visit`),
217                                        KEY `resource_total` (`resource`, `dt`),
218                                        KEY `dt_total` (`dt`,`remote_ip`)
219                                        ) PACK_KEYS=1 ";
220                                break;
221                        case 'date':
222                                $createTable = $SlimCfg->table_dt;
223                                $myTableStatsQuery = "CREATE TABLE `$createTable` (
224                                        `dt_start` int(10) unsigned NOT NULL default 0,
225                                        `dt_end` int(10) unsigned NOT NULL default 0,
226                                        `hits` int(10) unsigned NOT NULL default 0,
227                                        `visits` int(10) unsigned NOT NULL default 0,
228                                        `uniques` int(10) unsigned NOT NULL default 0,
229                                        `type` smallint(2) unsigned NOT NULL default 0,
230                                        KEY `dt_total` (`dt_start`, `dt_end`, `type`)
231                                        )";
232                                break;
233                        case 'pins':
234                                $createTable = $SlimCfg->table_pins;
235                                $myTableStatsQuery = "CREATE TABLE `$createTable` (
236                                        `id` int(11) unsigned NOT NULL auto_increment,
237                                        `name` tinytext NOT NULL,
238                                        `title` tinytext default '',
239                                        `author` tinytext NOT NULL,
240                                        `url` varchar(200) NOT NULL default '',
241                                        `text` varchar(255) default '',
242                                        `modules` longtext NOT NULL,
243                                        `version` varchar(15) default '',
244                                        `active` int(1) NOT NULL default 0,
245                                        UNIQUE KEY `id` (`id`)
246                                        )";
247                                break;
248                        case 'stats': default:
249                                $createTable = $SlimCfg->table_stats;
250                                $myTableStatsQuery = "CREATE TABLE `$createTable` (
251                                        `id` int(11) unsigned NOT NULL auto_increment,
252                                        `remote_ip` int unsigned default 0,
253                                        `language` varchar(5) default '',
254                                        `country` varchar(2) default '',
255                                        `domain` varchar(255) default '',
256                                        `referer` varchar(255) default '',
257                                        `searchterms` varchar(255) default '',
258                                        `resource` varchar(255) default '',
259                                        `platform` TINYINT default -1,
260                                        `browser` SMALLINT default -1,
261                                        `version` varchar(15) default '',
262                                        `visit` int(10) unsigned NOT NULL default '0',
263                                        `dt` int(10) unsigned NOT NULL default '0',
264                                        UNIQUE KEY `id` (`id`),
265                                        KEY `referer` (`referer`),
266                                        KEY `country` (`country`),
267                                        KEY `searchterms` (`searchterms`),
268                                        KEY `visit` (`visit`),
269                                        KEY `resource_total` (`resource`, `dt`),
270                                        KEY `dt_total` (`dt`,`remote_ip`)
271                                        ) PACK_KEYS=1" ;
272                                break;
273                }
274                // This is the schema for data about visits.
275
276                if(!$check_only) {
277                        // Check if table is already there
278                        foreach ($wpdb->get_col("SHOW TABLES", 0) as $table ) {
279                                if ($table == $createTable) {
280                                        return false;
281                                }
282                        }
283                       
284                        // Ok, let's proceed
285                        if ( $wpdb->query( $myTableStatsQuery ) === false ) {
286                                return false;
287                        }
288                }
289                // Just to be sure, we check that tables were actually created
290                foreach ( $wpdb->get_col("SHOW TABLES", 0) as $table ) {
291                        if ( $table == $createTable ) {
292                                return true;
293                        }
294                }       
295                return false;
296        }
297        // end createSlimTable
298       
299        function _importCountriesData() {
300                global $wpdb, $SlimCfg;
301               
302                $myCountryFile = "wp-slimstat.csv";
303               
304                // To avoid problems with SAFE_MODE, we will not use is_file
305                // of file_exists, but a loop through current directory
306                $isCountryFile = false;
307                $myFileHandle = opendir(dirname(__FILE__));
308                while ( false !== ($myFileName = readdir($myFileHandle)) && !$isCountryFile ) {
309                        if ( $myCountryFile == $myFileName ) {
310                                $isCountryFile = true;
311                        }
312                }
313                closedir($myFileHandle);
314                               
315                // If there is already a (not empty) country table, skip import
316                $myCountryRows = $wpdb->get_var("SELECT COUNT(*) FROM `$SlimCfg->table_countries`", 0);
317                if ( $myCountryRows > 0 && $myCountryRows !== false ) {
318                        return false;
319                }
320                               
321                // Import file, if exists
322                if ( $isCountryFile ) {
323                        // Allow plenty of time for this to happen
324                        @set_time_limit( 180 ); 
325
326                        // Remove all the rows
327                        $myQuery = "TRUNCATE `$SlimCfg->table_countries`";
328                       
329                        if ( $wpdb->query( $myQuery ) === false ) {
330                                return false;
331                        }
332                       
333                        $myCountryDataStr = "";
334                        $fd = fopen( dirname(__FILE__)."/".$myCountryFile, "r" );
335
336                        while ( !feof( $fd ) ) {
337                                $myCountryDataStr .= fread( $fd, 4096 );
338                                $myStringToSplit = strrev( strstr( strrev( $myCountryDataStr ), "\n" ) );
339                                $myCountryDataStr = substr( $myCountryDataStr, strlen( $myStringToSplit ) );
340                                $myCountryDataArray = explode( "\n", $myStringToSplit );
341                                foreach ( $myCountryDataArray as $aCountryDatum ) {
342                                        $fields = explode( ",", $aCountryDatum, 3 );
343                                        if ( sizeof( $fields ) == 3 ) {
344                                                $myQuery = 
345                                                        "INSERT INTO `$SlimCfg->table_countries` (
346                                                                `ip_from`, `ip_to`, `country_code` )
347                                                                VALUES ( \"".implode( "\", \"", $fields )."\" 
348                                                        )";
349                                               
350                                                        $wpdb->query( $myQuery );
351                                        }
352                                }
353                        }
354                        fclose( $fd ); 
355                        return true;
356                }
357                else return false;
358                               
359        } 
360        // end importCountriesData
361
362        function setup() { 
363                // set options if not exists
364                if(!get_settings('wp_slimstat_ex')) {
365                        $options['tracking'] = 1;
366                        $options['usepins'] = 1;
367                        $options['cachelimit'] = 0;
368                        $options['guesstitle'] = 1;
369                        $options['dbmaxage'] = 0;
370                        $options['limitrows'] = 30;
371                        $options['iptohost'] = 1;
372                        $options['whois'] = 1;
373                        $options['meta'] = 0;
374                        $options['visit_type'] = 'uniques';
375                        $options['count_type'] = 'hits';
376                        $options['stats_type'] = 'all';
377                        $options['time_offset'] = 0;
378                        $options['ignore'] = "";
379                        $options['use_ajax'] = 1;
380                        $options['blog_stats'] = 0;
381                        $options['blog_js'] = 'none';
382                        add_option('wp_slimstat_ex', $options);
383                }
384
385                $isStatsCreated = wp_slimstat_ex::_createSlimTable('stats');
386                $isFeedCreated = wp_slimstat_ex::_createSlimTable('feed');
387                $isDtCreated = wp_slimstat_ex::_createSlimTable('date');
388                $isPinCreated = wp_slimstat_ex::_createSlimTable('pins');
389
390                if ($SlimCfg->external_iptc !== 'external')             
391                        $isCountriesCreated = wp_slimstat_ex::_createSlimTable('country');
392               
393                // Now import data for "country to ip" conversion
394                if ( $isCountriesCreated && $SlimCfg->external_iptc !== 'external' ) {
395                        wp_slimstat_ex::_importCountriesData();
396                }
397        }// end setup
398
399}
400// end of class wp_slimstat_ex
401
402/* Setup, Option Page, Load CSS & Javascripts
403-------------------------------------------*/
404if($wp_version < 2) {
405        if (isset($_GET['activate']) && $_GET['activate'] == 'true') {
406                add_action( 'init', array( 'wp_slimstat_ex', 'setup' ) );
407        }
408} else {
409        add_action('activate_'.$SlimCfg->_basename(__FILE__), array('wp_slimstat_ex', 'setup'));
410}
411
412if($wp_version >= 2.1 && wp_slimstat_ex::is_slimstat_page())
413        add_action('init', array('wp_slimstat_ex', 'enqueue_script'));
414
415if (wp_slimstat_ex::is_slimstat_page()) {
416        add_action('admin_head', array('wp_slimstat_ex', 'admin_head'), 20);
417        add_action('admin_footer', array('wp_slimstat_ex', 'admin_footer'));
418}
419add_action('admin_menu', array('wp_slimstat_ex', 'option_page'));
420
421if($SlimCfg->option['blog_stats'] || $SlimCfg->option['meta'])
422        add_action('wp_head', array('wp_slimstat_ex', 'wp_head'), 20);
423
424/* Tracking
425-------------------------------------------*/
426include_once(SLIMSTATPATH . 'lib/track.php');
427add_action( 'shutdown', array( &$SSTrack, 'slimtrack' ) );
428
429/* Include libs
430-------------------------------------------*/
431require_once(SLIMSTATPATH . 'lib/functions.php');
432
433if(is_admin()) {
434        require_once(SLIMSTATPATH . 'lib/display.php');
435
436        /* Stats Display
437        -------------------------------------------*/
438        $wpSSDisplay =& SSDisplay::get_instance();
439        add_action('admin_menu', array(&$wpSSDisplay,'add_tab'));
440        unset($wpSSDisplay);
441}
442
443if($SlimCfg->option['meta']) {
444        add_action('wp_meta', array('SSFunction', 'simple_hvu'));
445}
446
447/* Print Stats on Blog
448------------------------------------------------------------------------*/
449
450function wpSSBlogStats($IDs = array(1,2,3), $echo = true) {
451        global $SlimCfg, $wpdb;
452        if(is_array($IDs) && !empty($IDs)) {
453                if($SlimCfg->option['blog_js'] == 'none' || !$SlimCfg->option['blog_js'])
454                        $SlimCfg->option['use_ajax'] = false; // instance option
455                require_once(SLIMSTATPATH . 'lib/modules.php');
456                require_once(SLIMSTATPATH . 'lib/pins.php');
457                $wrapper = '<a id="ajax_request" href="'.$SlimCfg->ajaxReq.'"></a>';
458                $wrapper .= '<a id="slm2" name="slm2"></a>'."\n";
459                $wrapper .= '<div id="wp_slimstat">'."\n";
460                $nav = '<p class="module_nav">'.__('Modules', 'wp-slimstat-ex').' : ';
461                $output = '';
462                foreach($IDs as $id) {
463                        $module = SSFunction::id2module($id);
464                        if($module) {
465                                $output .= call_user_func(array('SSModule', $module), '(1 = 1)');
466                                $nav .= '[ <a href="#module_'.$id.'">'.SSFunction::get_title($id).'</a> ] ';
467                        } elseif($id > 10000) {
468                                $pinid = floor($id/100) - 100;
469                                $mo = $id - (($pinid+100)*100) - 1;
470                                $pin = SSFunction::pin_mod_info($pinid);
471                                if($pin && isset($pin['modules'][$mo])) {
472                                        $file = SLIMSTATPATH . 'pins/'. $pin['name'] . '/pin.php';
473                                        require_once($file);
474                                        eval('$'.$pin["name"].' =& new $pin["name"]();'."\n");
475                                        eval('$output .= $'.$pin["name"].'->'.$pin["modules"][$mo]["name"].'(\'(1 = 1)\');'."\n");
476                                        $nav .= '[ <a href="#module_'.$id.'">'.$pin["modules"][$mo]["title"].'</a> ] ';
477                                } else 
478                                        $output .= '<p>Wrong module ID '.$id.'</p>';
479                        }
480                } 
481                $nav .= '<span id="slimloading"> ( Loading... ) </span><span id="debug"></span>';
482                $nav .= '</p>';
483        $wrapper .= $nav.$output.'</div>';
484        if($echo) 
485                echo $wrapper;
486        else 
487                return $wrapper;
488        }
489}
490
491if($SlimCfg->option['blog_stats']) :
492
493function wpSSBlogStats_Replace($m) {
494        $m = trim($m[1]);
495        if(empty($m))
496                $IDs = array(1,2,3);
497        else 
498                $IDs = explode(',', str_replace(array('|', ' '), '', $m));
499        return wpSSBlogStats($IDs, false);
500}
501
502function wpSSBlogStats_Filter($content) {
503        return preg_replace_callback('/[<p>]*{SLIMSTAT(.*?)}\n*[<\/p>]*/', 'wpSSBlogStats_Replace', $content);
504}
505
506add_filter('the_content', 'wpSSBlogStats_Filter', 10);
507
508endif; // if opiton['blog_stats']
509
510function print_simple_hvu($wrap="") {
511        SSFunction::simple_hvu($wrap);
512}
513
514?>
Note: See TracBrowser for help on using the browser.