| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | class SlimCfg { |
|---|
| 4 | var $version = '1.604'; // Current SlimStat-Ex version |
|---|
| 5 | var $external_iptc = 'internal'; // set this 'external' to use external ip-to-country database |
|---|
| 6 | var $uaOption = false; // Personal use... please forget about this ;) |
|---|
| 7 | var $last_db_update_version = '1.6'; |
|---|
| 8 | var $tbPrefix, $table_stats, $table_countries, $table_feed, $table_resource, $table_dt, $table_pins, $current_table; |
|---|
| 9 | var $option, $exclude, $pluginURL, $ajaxReq, $get, $basedir, $web_path, $dt, $indexkey; |
|---|
| 10 | var $is_wpmu; |
|---|
| 11 | var $version_check_url = 'http://082net.com/update-check.php?check_plugin=wp-slimstat-ex'; |
|---|
| 12 | var $plugin_home = 'http://082net.com/tag/wp-slimstat-ex/'; |
|---|
| 13 | var $bot_array = array(); |
|---|
| 14 | |
|---|
| 15 | function SlimCfg() { |
|---|
| 16 | $this->_init(); |
|---|
| 17 | } |
|---|
| 18 | |
|---|
| 19 | function _init() { |
|---|
| 20 | global $table_prefix; |
|---|
| 21 | $this->is_wpmu = $this->is_wpmu(); |
|---|
| 22 | if (defined('SLIMSTAT_EXTRACK')) { // if external tracking |
|---|
| 23 | global $slimtrack_ext; |
|---|
| 24 | $table_prefix = $slimtrack_ext['table_prefix']; |
|---|
| 25 | } |
|---|
| 26 | // We use a bunch of tables to store data |
|---|
| 27 | global $wpmuBaseTablePrefix; |
|---|
| 28 | // Table names |
|---|
| 29 | if($this->is_wpmu && isset($wpmuBaseTablePrefix)) |
|---|
| 30 | $this->table_countries = $wpmuBaseTablePrefix . "slim_countries"; |
|---|
| 31 | else |
|---|
| 32 | $this->table_countries = $table_prefix . "slim_countries"; // ip-to-country |
|---|
| 33 | $this->tbPrefix = $table_prefix . "slimex_"; |
|---|
| 34 | $this->table_stats = $table_prefix . "slimex_stats"; // common stats |
|---|
| 35 | $this->table_feed = $table_prefix . "slimex_feed"; // feed stats |
|---|
| 36 | $this->table_dt = $table_prefix . "slimex_dt"; // compressed hit, vists, uniques |
|---|
| 37 | $this->table_pins = $table_prefix . "slimex_pins"; // Pin information |
|---|
| 38 | $this->table_resource = $table_prefix . "slimex_resource"; // resource table |
|---|
| 39 | |
|---|
| 40 | $this->basedir = dirname($this->_basename(__FILE__)); // plugin folder name |
|---|
| 41 | $this->pluginURL = get_option('siteurl')."/wp-content/plugins/".$this->basedir; // Plugin URL(path) |
|---|
| 42 | $this->ajaxReq = $this->pluginURL."/lib/r"; // Ajax request URL |
|---|
| 43 | $this->web_path = $this->_getWebPath(); // wordpress intall folder related to site root |
|---|
| 44 | $this->get = $this->parse_GET(); // panel, filters, encoded filters |
|---|
| 45 | $this->current_table = $this->select_table(); // Current table to select |
|---|
| 46 | $this->dt = array($this->_mktime(time()), $this->_mktime(time(), true)); // Blog midnight and server midnight |
|---|
| 47 | $this->option = get_option('wp_slimstat_ex'); // SlimStat options |
|---|
| 48 | $this->exclude = get_option('wp_slimstat_ex_exclude'); // Exclusion options |
|---|
| 49 | $this->indexkey = array('common'=>$this->_getIndexKeys('common', true), 'feed'=>$this->_getIndexKeys('feed', true)); |
|---|
| 50 | // if( $this->exclude['ignore_bots'] ) { |
|---|
| 51 | $this->bot_array['bots'] = array(12,43,45,46,47,48,49,50,51,52,53,107,108,109,110,111,121,135,136,159); |
|---|
| 52 | $this->bot_array['feeds'] = array(13,14,15,20,44,54,55,56,57,61,91,95,96,97,98,99,100,130,131,132,133,134,137,138,139,140,141,142,143,144,146,147,148,149,150,151,152,154,156,157,1998); |
|---|
| 53 | $this->bot_array['validators'] = array(16,17,18,93,123,124,125,126,127,128,129,1999); |
|---|
| 54 | $this->bot_array['tools'] = array(19,22,40,41,72,73,77,80,85,58,59,60,145,155); |
|---|
| 55 | // } |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | function _getIndexKeys($table = 'common', $deep = false) { |
|---|
| 59 | global $wpdb; |
|---|
| 60 | $_table = $this->string2table($table); |
|---|
| 61 | $current_keys = array(); |
|---|
| 62 | $pre_len = array(); |
|---|
| 63 | $cur_len = array(); |
|---|
| 64 | $key_array = array('dt_total', 'resource_total', 'resource', 'searchterms', 'domain', 'referer', 'platform', 'browser', 'language', 'visit', 'country', 'remote_ip', 'dt', 'ip_to', 'ip_from_to_idx', 'ip_from_idx'); |
|---|
| 65 | if($myIndexStructure = $wpdb->get_results("SHOW INDEX FROM $_table", ARRAY_A)){ |
|---|
| 66 | foreach ( $myIndexStructure as $index_details ) { |
|---|
| 67 | $key = $index_details['Key_name']; |
|---|
| 68 | $col = $index_details['Column_name']; |
|---|
| 69 | $len = $index_details['Cardinality']; |
|---|
| 70 | $len = isset($pre_len[$key]) ? max($pre_len[$key], $len) : $len; |
|---|
| 71 | $pre_len[$key] = $len; |
|---|
| 72 | if(in_array($key, $key_array)) { |
|---|
| 73 | if($deep) { |
|---|
| 74 | $current_keys[$key]['column'][] = $col; |
|---|
| 75 | $current_keys[$key]['length'] = $len; |
|---|
| 76 | } else |
|---|
| 77 | $current_keys[] = $key; |
|---|
| 78 | } |
|---|
| 79 | } |
|---|
| 80 | return $current_keys; |
|---|
| 81 | } |
|---|
| 82 | return array(); |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | function use_indexkey($mokey, $table='') { |
|---|
| 86 | $table = ($table == '') ? $this->current_table : $table; |
|---|
| 87 | $keys = ($table == $this->table_stats) ? $this->indexkey['common'] : $this->indexkey['feed']; |
|---|
| 88 | $pkeys = array(); |
|---|
| 89 | if(isset($keys['remote_ip'])) $pkeys[] = 'remote_ip'; |
|---|
| 90 | if(isset($keys['resource'])) $pkeys[] = 'resource'; |
|---|
| 91 | if(isset($keys['referer'])) $pkeys[] = 'referer'; |
|---|
| 92 | if(isset($keys['domain'])) $pkeys[] = 'domain'; |
|---|
| 93 | if(isset($this->get['fd']) && isset($keys['dt'])) {// dt index will automatically applied |
|---|
| 94 | if($mokey != 'dt' && in_array($mokey, $pkeys)) |
|---|
| 95 | return " USE INDEX (dt,{$mokey}) "; |
|---|
| 96 | return " USE INDEX (dt)"; |
|---|
| 97 | } |
|---|
| 98 | if(!isset($this->get['ff']))// use default |
|---|
| 99 | return ""; |
|---|
| 100 | $use_key = ""; |
|---|
| 101 | if(!isset($keys[$mokey])) |
|---|
| 102 | $mokey = ""; |
|---|
| 103 | switch($this->get['ff']) { |
|---|
| 104 | case 0: |
|---|
| 105 | if(isset($keys['domain']))// varchar(255) index keys... |
|---|
| 106 | return " USE INDEX (domain) "; |
|---|
| 107 | break; |
|---|
| 108 | case 1: |
|---|
| 109 | if(isset($keys['searchterms']))// varchar(255) index keys... |
|---|
| 110 | return " USE INDEX (searchterms) "; |
|---|
| 111 | break; |
|---|
| 112 | case 2: |
|---|
| 113 | if(isset($keys['resource']))// This is primary key |
|---|
| 114 | return " USE INDEX (resource) "; |
|---|
| 115 | break; |
|---|
| 116 | case 3: |
|---|
| 117 | if(isset($keys['remote_ip']))// This is primary key |
|---|
| 118 | return " USE INDEX (remote_ip) "; |
|---|
| 119 | $use_key = 'remote_ip'; |
|---|
| 120 | break; |
|---|
| 121 | case 4: |
|---|
| 122 | $use_key = 'browser'; |
|---|
| 123 | break; |
|---|
| 124 | case 5: |
|---|
| 125 | $use_key = 'platform'; |
|---|
| 126 | break; |
|---|
| 127 | case 6: |
|---|
| 128 | $use_key = 'country'; |
|---|
| 129 | break; |
|---|
| 130 | case 6: |
|---|
| 131 | $use_key = 'language'; |
|---|
| 132 | break; |
|---|
| 133 | default: |
|---|
| 134 | break; |
|---|
| 135 | } |
|---|
| 136 | if(!isset($keys[$use_key])) |
|---|
| 137 | $use_key = $mokey; |
|---|
| 138 | if('' == $use_key) |
|---|
| 139 | return ""; |
|---|
| 140 | return " USE INDEX (".(($keys[$mokey]['length'] > $keys[$use_key]['length']) ? $mokey : $use_key).") "; |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | // key length desc |
|---|
| 144 | function indexkey_sort($a, $b) { |
|---|
| 145 | if ($a['length'] == $b['length']) |
|---|
| 146 | return 0; |
|---|
| 147 | return ($a['length'] > $b['length']) ? -1 : 1; |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | function _basename($file) { |
|---|
| 151 | $file = preg_replace('|\\\\+|', '\\\\', $file); |
|---|
| 152 | $file = preg_replace('/^.*wp-content[\\\\\/]plugins[\\\\\/]/', '', $file); |
|---|
| 153 | $file = preg_replace('/[\\\\\/]/', '/', $file); |
|---|
| 154 | return $file; |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | function _getWebPath($url="home") { |
|---|
| 158 | $home_path = parse_url(get_option($url)); |
|---|
| 159 | if ( isset($home_path['path']) ) |
|---|
| 160 | $home_path = $home_path['path']; |
|---|
| 161 | else |
|---|
| 162 | $home_path = ''; |
|---|
| 163 | $home_path = trim($home_path, '/'); |
|---|
| 164 | return $home_path; |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | function my_esc( $str = '' ) { |
|---|
| 168 | return addslashes( $str );// Disable rest for now, causing problems |
|---|
| 169 | if( version_compare( phpversion(), '4.3.0' ) == '-1' ) |
|---|
| 170 | return mysql_escape_string( $str ); |
|---|
| 171 | else |
|---|
| 172 | return mysql_real_escape_string( $str ); |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | function select_table() { |
|---|
| 176 | $neededTable = ( $this->get['pn'] == 2 ) ? $this->table_feed : $this->table_stats; |
|---|
| 177 | return $neededTable; |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | function string2table($table) { |
|---|
| 181 | switch($table) { |
|---|
| 182 | case 'feed': |
|---|
| 183 | $_table = $this->table_feed; |
|---|
| 184 | break; |
|---|
| 185 | case 'country': |
|---|
| 186 | $_table = $this->table_countries; |
|---|
| 187 | break; |
|---|
| 188 | case 'dt': |
|---|
| 189 | $_table = $this->table_dt; |
|---|
| 190 | break; |
|---|
| 191 | case 'pins': |
|---|
| 192 | $_table = $this->table_pins; |
|---|
| 193 | break; |
|---|
| 194 | case 'resource': |
|---|
| 195 | $_table = $this->table_resource; |
|---|
| 196 | break; |
|---|
| 197 | case 'common': |
|---|
| 198 | $_table = $this->table_stats; |
|---|
| 199 | break; |
|---|
| 200 | default: |
|---|
| 201 | $_table = $table; |
|---|
| 202 | break; |
|---|
| 203 | } |
|---|
| 204 | return $_table; |
|---|
| 205 | } |
|---|
| 206 | |
|---|
| 207 | function sstime($time, $back_to_server_time = false) { |
|---|
| 208 | $offset = (int)$this->option['time_offset'] * 60 * 60; |
|---|
| 209 | if ($offset !== 0) { |
|---|
| 210 | $time = $back_to_server_time ? $time - $offset : $time + $offset; |
|---|
| 211 | } |
|---|
| 212 | return $time; |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | function _mktime($time, $back_to_server_time = false) { |
|---|
| 216 | $dt = $this->sstime($time); |
|---|
| 217 | $new_dt = mktime( 0, 0, 0, date( "n", $dt ), date( "d", $dt ), date( "Y", $dt ) ); |
|---|
| 218 | if($back_to_server_time) |
|---|
| 219 | $new_dt = $this->sstime( $new_dt, true ); |
|---|
| 220 | return $new_dt; |
|---|
| 221 | } |
|---|
| 222 | |
|---|
| 223 | function _date($date_format, $time) { |
|---|
| 224 | return mysql2date($date_format, date('Y-m-d H:i:s', $time)); |
|---|
| 225 | } |
|---|
| 226 | |
|---|
| 227 | function parse_GET($filter_encode = true, $interval = true, $normal = true) { |
|---|
| 228 | $get = array(); |
|---|
| 229 | $get['pn'] = (isset($_GET['panel']))?(int)$_GET['panel']:1; |
|---|
| 230 | $f_interval = isset($_GET['fd']) ? trim(urldecode($_GET['fd'])) : null; |
|---|
| 231 | if( $normal && isset($_GET['fi']) && isset($_GET['ff']) && isset($_GET['ft']) ) { |
|---|
| 232 | $get['ff'] = (int)$_GET['ff']; |
|---|
| 233 | $get['ft'] = (int)$_GET['ft']; |
|---|
| 234 | $get['fi'] = urldecode($_GET['fi']); |
|---|
| 235 | } |
|---|
| 236 | if ( $interval && isset( $f_interval ) && strpos( $f_interval, "|" ) ) { |
|---|
| 237 | $get['fd'] = array(0, 0); |
|---|
| 238 | $intervals = explode( "|", $f_interval, 2 ); |
|---|
| 239 | $intervals[0] = (int)$intervals[0]; |
|---|
| 240 | $intervals[1] = (int)$intervals[1]; |
|---|
| 241 | if( max( $intervals ) > 0 && ($intervals[1] > $intervals[0]) ) { |
|---|
| 242 | $get['fd'] = array( $intervals[0], $intervals[1] ); |
|---|
| 243 | } |
|---|
| 244 | } |
|---|
| 245 | $get['slim_table'] = isset($_GET['slim_table']) ? trim($_GET['slim_table']) : null; |
|---|
| 246 | //re-encode filter values |
|---|
| 247 | if($filter_encode) { |
|---|
| 248 | $get['fi_encode'] = ""; |
|---|
| 249 | $get['fd_encode'] = ""; |
|---|
| 250 | if($normal && isset($get['fi'])) |
|---|
| 251 | $get['fi_encode'] = '&ff='.$get['ff'].'&ft='.$get['ft'].'&fi='.urlencode($get['fi']); |
|---|
| 252 | if ( $interval && isset($get['fd']) ) |
|---|
| 253 | $get['fd_encode'] = '&fd='.$f_interval; |
|---|
| 254 | if($get['slim_table']) |
|---|
| 255 | $get['fi_encode'] .= '&slim_table='.$get['slim_table']; |
|---|
| 256 | } |
|---|
| 257 | $get['offset'] = isset($_GET['offset']) ? (int)$_GET['offset'] : 0; |
|---|
| 258 | $get['offset'] = ($get['offset'] < 0) ? 0 : $get['offset']; |
|---|
| 259 | return $get; |
|---|
| 260 | } |
|---|
| 261 | |
|---|
| 262 | function truncate($text, $len = 120) { |
|---|
| 263 | if (function_exists('mb_strcut')) { |
|---|
| 264 | $output = (strlen($text) >$len) ? mb_strcut($text, 0, $len, get_option('blog_charset')) . '...' : $text; |
|---|
| 265 | } else { |
|---|
| 266 | $output = (strlen($text) >$len) ? substr($text, 0, $len) . "..." : $text; |
|---|
| 267 | } |
|---|
| 268 | return $output; |
|---|
| 269 | } |
|---|
| 270 | |
|---|
| 271 | function trimString($r, $length = 26) { |
|---|
| 272 | $r = eregi_replace( "http://", "", $r ); |
|---|
| 273 | $r = eregi_replace( "^www.", "", $r ); |
|---|
| 274 | $r = $this->truncate($r, $length); |
|---|
| 275 | return $r; |
|---|
| 276 | } |
|---|
| 277 | |
|---|
| 278 | function is_wpmu() { |
|---|
| 279 | global $wp_version; |
|---|
| 280 | return strpos($wp_version, 'wordpress-mu') !== false; |
|---|
| 281 | } |
|---|
| 282 | |
|---|
| 283 | // borrowed from K2 theme (http://getk2.com) |
|---|
| 284 | function wp_version() { |
|---|
| 285 | global $wp_version, $wporg_version; |
|---|
| 286 | if(!$this->is_wpmu) |
|---|
| 287 | return $wp_version; |
|---|
| 288 | |
|---|
| 289 | if(isset($wporg_version)) { |
|---|
| 290 | preg_match("/\d\.\d/i", $wporg_version, $match); |
|---|
| 291 | return $match[0]; |
|---|
| 292 | } |
|---|
| 293 | // wpmu - increment version by 1.0 to match wp |
|---|
| 294 | preg_match("/\d\.\d/i", $wp_version, $match); |
|---|
| 295 | $match[0] = $match[0] + 1.0; |
|---|
| 296 | } |
|---|
| 297 | |
|---|
| 298 | //borrowed from Extended Live Archives(http://www.sonsofskadi.net/extended-live-archive/) |
|---|
| 299 | function version_check() { |
|---|
| 300 | $remote == false; |
|---|
| 301 | /* if( function_exists('wp_remote_fopen') && (ini_get('allow_url_fopen') || function_exists('curl_init')) ) { |
|---|
| 302 | $remote = wp_remote_fopen($this->version_check_url); |
|---|
| 303 | } else {*/ |
|---|
| 304 | if(!class_exists('Snoopy')) |
|---|
| 305 | require(ABSPATH . 'wp-includes/class-snoopy.php'); |
|---|
| 306 | $client = new Snoopy(); |
|---|
| 307 | $client->_fp_timeout = 4; |
|---|
| 308 | if (@$client->fetch($this->version_check_url) !== false) { |
|---|
| 309 | $remote = $client->results; |
|---|
| 310 | } |
|---|
| 311 | // } |
|---|
| 312 | if(!$remote || strlen($remote) > 8 || 'error' == $remote) return -1; |
|---|
| 313 | if ($remote > $this->version) return 1; else return 0; |
|---|
| 314 | } |
|---|
| 315 | |
|---|
| 316 | function check_user($redirect = true) { |
|---|
| 317 | global $user_level; |
|---|
| 318 | auth_redirect(); |
|---|
| 319 | $location = get_option('siteurl'). '/wp-admin/options-general.php?page=wp-slimstat-ex-options'; |
|---|
| 320 | get_currentuserinfo(); |
|---|
| 321 | if ( $user_level < 8 ) { |
|---|
| 322 | if($redirect) { |
|---|
| 323 | if(function_exists('wp_redirect')) |
|---|
| 324 | wp_redirect($location); |
|---|
| 325 | else |
|---|
| 326 | header('Location: '.$location.''); |
|---|
| 327 | exit(); |
|---|
| 328 | } else |
|---|
| 329 | return false; |
|---|
| 330 | } |
|---|
| 331 | return true; |
|---|
| 332 | } |
|---|
| 333 | |
|---|
| 334 | function &get_instance() { |
|---|
| 335 | static $instance = array(); |
|---|
| 336 | if ( empty( $instance ) ) { |
|---|
| 337 | $instance[] =& new SlimCfg(); |
|---|
| 338 | } |
|---|
| 339 | return $instance[0]; |
|---|
| 340 | } |
|---|
| 341 | } |
|---|
| 342 | |
|---|
| 343 | if(!isset($SlimCfg)) |
|---|
| 344 | $SlimCfg =& SlimCfg::get_instance(); |
|---|
| 345 | ?> |
|---|