root/trunk/lib/wassup.class.php

Revision 153, 11.4 kB (checked in by root, 5 months ago)

1.6.2b added type selection in spy view

Line 
1<?php
2/* #######
3 * ##  wassupOptions - A PHP Class for Wassup plugin option settings.
4 * ##    Contains variables and functions used to set or change wassup
5 * ##    settings in Wordpress' wp_options table and to output those
6 * ##    values for use in forms.
7 * ##  Author: Helene D. 2/24/08
8 */
9class wassupOptions {
10        /* general/detail settings */
11        var $wassup_refresh = "3";
12        var $wassup_userlevel = "8";
13        var $wassup_screen_res = "800";
14        var $wassup_default_type = "";
15        var $wassup_default_spy_type = "";
16        var $wassup_default_limit = "10";
17        var $wassup_top10 ;
18        var $wassup_dashboard_chart;
19        var $wassup_geoip_map;
20        var $wassup_googlemaps_key;
21        var $wassup_time_format;        //new
22
23        /* recording settings */
24        var $wassup_active = "1";
25        var $wassup_loggedin = "1";
26        var $wassup_spider = "1";
27        var $wassup_attack = "1";
28        var $wassup_hack = "1"; //new - to identify/record break-in attempts
29        var $wassup_exclude;
30        var $wassup_exclude_url;
31
32        /* spam settings */
33        var $wassup_spamcheck;
34        var $wassup_spam;
35        var $wassup_refspam;
36
37        /* table/file management settings */
38        var $wassup_savepath;
39        var $delete_auto;
40        var $delete_auto_size;
41        var $wassup_remind_mb;
42        var $wassup_remind_flag;
43        var $wassup_uninstall;  //for complete uninstall of wassup
44        var $wassup_optimize;   //for optimize table once a day
45
46        /* chart display settings */
47        var $wassup_chart;
48        var $wassup_chart_type;
49
50        /* widget settings */
51        var $wassup_widget_title;
52        var $wassup_widget_ulclass;
53        var $wassup_widget_loggedin;
54        var $wassup_widget_comauth;
55        var $wassup_widget_search;
56        var $wassup_widget_searchlimit;
57        var $wassup_widget_ref;
58        var $wassup_widget_reflimit;
59        var $wassup_widget_topbr;
60        var $wassup_widget_topbrlimit;
61        var $wassup_widget_topos;
62        var $wassup_widget_toposlimit;
63        var $wassup_widget_chars;
64
65        /* temporary action settings */
66        var $wassup_alert_message;      //used to display alerts
67        var $wmark;
68        var $wip;
69        var $whash = "";        //wp_hash value used by action.php
70
71        /* Constructor */
72        function wassupoptions() {
73                //# initialize class variables with current options
74                //# or with defaults if none
75                $this->loadSettings();
76        }
77
78        /* Methods */
79        function loadDefaults() {
80                $this->wassup_active = "1";
81                $this->wassup_loggedin = "1";
82                $this->wassup_spider = "1";
83                $this->wassup_attack = "1";
84                $this->wassup_hack = "1";
85                $this->wassup_spamcheck = "1";
86                $this->wassup_spam = "1";
87                $this->wassup_refspam = "1";
88                $this->wassup_exclude = "";
89                $this->wassup_exclude_url = "";
90                $this->wassup_savepath = null;
91                $this->wassup_chart = "1";
92                $this->wassup_chart_type = "2";
93                $this->delete_auto = "never";
94                $this->delete_auto_size = "0";
95                $this->wassup_remind_mb = "0";
96                $this->wassup_remind_flag = "0";
97                $this->wassup_refresh = "3";
98                $this->wassup_userlevel = "8";
99                $this->wassup_screen_res = "800";
100                $this->wassup_default_type = "everything";
101                $this->wassup_default_spy_type = "everything";
102                $this->wassup_default_limit = "10";
103                $this->wassup_dashboard_chart = "0";
104                $this->wassup_geoip_map = "0";
105                $this->wassup_googlemaps_key = "";
106                $this->wassup_time_format = "24";
107                $this->wassup_widget_title = "Visitors Online";
108                $this->wassup_widget_ulclass = "links";
109                $this->wassup_widget_loggedin = "1";
110                $this->wassup_widget_comauth = "1";
111                $this->wassup_widget_search = "1";
112                $this->wassup_widget_searchlimit = "5";
113                $this->wassup_widget_ref = "1";
114                $this->wassup_widget_reflimit = "5";
115                $this->wassup_widget_topbr = "1";
116                $this->wassup_widget_topbrlimit = "5";
117                $this->wassup_widget_topos = "1";
118                $this->wassup_widget_toposlimit = "5";
119                $this->wassup_widget_chars = "18";
120                $this->wassup_alert_message = "";
121                $this->wassup_uninstall = "0";
122                $this->wassup_optimize = wassup_get_time();
123                $this->wassup_top10 = serialize(array("topsearch"=>"1",
124                                        "topreferrer"=>"1",
125                                        "toprequest"=>"1",
126                                        "topbrowser"=>"1",
127                                        "topos"=>"1",
128                                        "toplocale"=>"0",
129                                        "topfeed"=>"0",
130                                        "topcrawler"=>"0",
131                                        "topvisitor"=>"0",
132                                        "topreferrer_exclude"=>""));
133                $this->whash = $this->get_wp_hash();
134        }
135
136        //#Load class variables with current options or with defaults
137        function loadSettings() {
138                //# load class variables with current options or load
139                //#   default settings if no options set.
140                $options_array = get_option('wassup_settings');
141                if (empty($options_array)) {
142                        $this->loadDefaults();
143                } else {
144                        foreach ($options_array as $optionkey => $optionvalue) {
145                                //if (isset($this->$optionkey)) { //returns false for null values
146                                if (array_key_exists($optionkey,$this)) {
147                                        $this->$optionkey = $optionvalue;
148                                }
149                        }
150                }
151                return true;
152        }
153
154        //#Save class variables to the Wordpress options table
155        function saveSettings() {
156                //#  convert class variables into an array and save using
157                //#  Wordpress functions, "update_option" or "add_option"
158                //#convert class into array...
159                $settings_array = array();
160                foreach (array_keys(get_class_vars(get_class($this))) as $k) {
161                        $settings_array[$k] = $this->$k;
162                }
163                //#save array to options table...
164                $options_check = get_option('wassup_settings');
165                if (empty($options_check)) {
166                        add_option('wassup_settings', $settings_array, 'Options for WassUp');
167                } else {
168                        update_option('wassup_settings', $settings_array);
169                }
170                return true;
171        }
172
173        function deleteSettings() {
174                //#delete the contents of the options table...
175                delete_option('wassup_settings');
176        }
177
178        //#Return an array containing all possible values of the given
179        //#  class variable, $key. For use in form validation, etc.
180        function getItemOptions($key="",$meta="") {
181                $item_options = array();
182                $item_options_meta = array();
183                if ($key == "wassup_screen_res") {
184                        $item_options = array("640","800","1024","1200");
185                        $item_options_meta = array("&nbsp;640",
186                                "&nbsp;800",
187                                "1024",
188                                "1200");
189                } elseif ($key == "wassup_userlevel") {
190                        $item_options = array("","8","6","2");
191                        $item_options_meta = array("--",
192                                __("Administrators","wassup"),
193                                __("Contributors","wassup"),
194                                __("Authors","wassup"));
195                } elseif ($key == "wassup_chart_type") {
196                        $item_options = array("1","2");
197                        $item_options_meta = array(
198                                __("One - two lines chart one axis","wassup"),
199                                __("Two - two lines chart two axes","wassup"));
200                } elseif ($key == "wassup_default_type") {
201                        $item_options = array("everything","spider","nospider","spam","nospam","nospamspider","loggedin","comauthor","searchengine","referrer");
202                        $item_options_meta = array(
203                                __("Everything","wassup"),
204                                __("Spider","wassup"),
205                                __("No spider","wassup"),
206                                __("Spam","wassup"),
207                                __("No Spam","wassup"),
208                                __("No Spam, No Spider","wassup"),
209                                __("Users logged in","wassup"),
210                                __("Comment authors","wassup"),
211                                __("Referer from search engine","wassup"),
212                                __("Referer from ext link","wassup"));
213                } elseif ($key == "wassup_default_spy_type") {
214                        $item_options = array("everything","spider","nospider","spam","nospam","nospamspider","loggedin","comauthor","searchengine","referrer");
215                        $item_options_meta = array(
216                                __("Everything","wassup"),
217                                __("Spider","wassup"),
218                                __("No spider","wassup"),
219                                __("Spam","wassup"),
220                                __("No Spam","wassup"),
221                                __("No Spam, No Spider","wassup"),
222                                __("Users logged in","wassup"),
223                                __("Comment authors","wassup"),
224                                __("Referer from search engine","wassup"),
225                                __("Referer from ext link","wassup"));
226                } elseif ($key == "wassup_default_limit") {
227                        $item_options = array("10","20","50","100");
228                        $item_options_meta = array("&nbsp;10",
229                                "&nbsp;20",
230                                "&nbsp;50",
231                                "100");
232                } elseif ($key == "delete_auto") {
233                        $item_options = array("never","-1 day","-1 week","-1 month","-3 months","-6 months","-1 year");
234                        $item_options_meta = array(
235                                __("Don't delete anything","wassup"),
236                                __("24 hours","wassup"),
237                                __("1 week","wassup"),
238                                __("1 month","wassup"),
239                                __("3 months","wassup"),
240                                __("6 months","wassup"),
241                                __("1 year","wassup"));
242                } elseif (!empty($key)) {       //enable/disable is default
243                        $item_options =  array("1","0");
244                        $item_options_meta =  array("Enable","Disable");
245                }
246                if ($meta == "meta") {
247                        return $item_options_meta;
248                } else {
249                        return $item_options;
250                }
251        } //end getItemValues
252
253        //#generates <options> tags for the given class variable, $itemkey
254        //#  for use in a <select> form.
255        function showFormOptions ($itemkey="",$selected="",$optionargs="") {
256                $form_items =$this->getItemOptions($itemkey);
257                if (count($form_items) > 0) {
258                        $form_items_meta = $this->getItemOptions($itemkey,"meta");
259                        if (empty($selected)) { 
260                                if (!empty($this->$itemkey)) {
261                                        $selected = $this->$itemkey;
262                                } else { 
263                                        $selected = $form_items[0];
264                                }
265                        }
266                        foreach ($form_items as $k => $option_item) {
267                                echo "\n\t\t".'<option value="'.$optionargs.$option_item.'"';
268                                if ($selected == $option_item) { echo ' SELECTED>'; }
269                                else { echo '>'; }
270                                echo $form_items_meta[$k].'&nbsp;&nbsp;</option>';
271                        }
272                }
273        } //end showFormOptions
274
275
276        //#Sets the class variable, wassup_savepath, with the given
277        //#  value $savepath
278        function setSavepath($savepath="") {
279                $savepath = rtrim($savepath,"/");
280                $siteurl = rtrim(get_bloginfo('siteurl'),"/");
281                if (!empty($savepath)) {
282                        //remove site URL from path in case user entered it
283                        if (strpos($savepath, $siteurl) === 0) {
284                                $tmppath=substr($savepath,strlen($siteurl)+1);
285                        } elseif (strpos($savepath,'/') === 0 && !$this->isWritableFolder($savepath)) {
286                                $tmppath=substr($savepath,1);
287                        } elseif (strpos($savepath,'./') === 0 ) {
288                                $tmppath=substr($savepath,2);
289                        } else { 
290                                $tmppath = $savepath;
291                        }
292                        //append website root or home directory to relative paths...
293                        if (preg_match('/^[a-zA-Z]/',$tmppath) > 0 || strpos($tmppath,'../') === 0) {
294                                if (!empty($_ENV['DOCUMENT_ROOT'])) {
295                                        $tmppath = rtrim($_ENV['DOCUMENT_ROOT'],'/').'/'.$tmppath;
296                                } elseif (!empty($_ENV['HOME'])) {
297                                        $tmppath = rtrim($_ENV['HOME'],'/').'/'.$tmppath;
298                                }
299                                if ($this->isWritableFolder($tmppath)) {
300                                        $savepath = $tmppath;
301                                }
302                        } 
303                }
304                $this->wassup_savepath = $savepath;
305        }
306
307        //#Return true if the given directory path exists and is writable
308        function isWritableFolder($folderpath="") {
309                $folderpath=trim($folderpath);  //remove white spaces
310                if (!empty($folderpath) && strpos($folderpath,'http://') !== 0 ) {
311                        if (file_exists($folderpath)) { 
312                                $testfile = rtrim($folderpath,"/")."/temp".time().'.txt';
313                                //#check that the directory is writable...
314                                if (@touch($testfile)) { unlink($testfile); }
315                                else { return false; }
316                        } else {
317                                return false;
318                        }
319                } else {
320                        return false;
321                }
322                return true;
323        }
324
325        //#Set a wp_hash value and return it
326        function get_wp_hash($hashkey="") {
327                $wassuphash = "";
328                if (function_exists('wp_hash')) { 
329                        if (empty($hashkey)) {
330                                if (defined('SECRET_KEY')) { 
331                                        $hashkey = SECRET_KEY;
332                                } else { 
333                                        $hashkey = "wassup";
334                                }
335                        }
336                        $wassuphash = wp_hash($hashkey);
337                }
338                return $wassuphash;
339        } //end function get_wp_hash
340
341        //#show a system message in Wassup Admin menus
342        function showMessage($message="") {
343                if (empty($message) && !empty($this->wassup_alert_message)) {
344                        $message = $this->wassup_alert_message;
345                }
346                //#check for error message/notice message
347                if (stristr($message,"error") !== FALSE || stristr($message,"problem") !== FALSE) {
348                        echo '<div class="fade error" id="wassup-error"><p style="color:#d00;padding:10px;">'.$message;
349                        //print_r($this); // #debug
350                        echo '</p></div>'."\n";
351                } else {
352                        echo '<div class="fade updated" id="wassup-message"><p style="color:#040;padding:10px;">'.$message;
353                        //print_r($this); // #debug
354                        echo '</p></div>'."\n";
355                }
356        } //end showMessage
357
358        function showError($message="") {
359                $this->showMessage($message);
360        }
361} //end class wassupOptions
362?>
Note: See TracBrowser for help on using the browser.