MIOLO20
Carregando...
Procurando...
Nenhuma entrada encontrada
class_support.php
Ir para a documentação deste ficheiro.
1<?php
2
3/************************************************
4* *
5* TScounter *
6* *
7* by Thomas Schuster *
8* http://www.TSinter.net *
9* *
10* file: class_support.php *
11* version: 3.2 *
12* license: GNU General Public License *
13* created: 20.04.2002 *
14* email: admin@TSinter.net *
15* *
16* *
17* This is an advanced counter in OO-design *
18* which uses both, session management AND *
19* ip comparison with a reload restriction, *
20* to recognize different visitors of a *
21* website. It recognizes over 100 spiders and *
22* robots and does not count them as visitors. *
23* It counts the impressions for every webpage *
24* of the website and track the visitors way. *
25* *
26* Copyright (c) 2001-2002 TSinter.net *
27* All rights reserved. *
28* *
29************************************************/
30
32{
33
34 //--------------------------------//
35 // This class provides some basic //
36 // functionality, which is often //
37 // used by other scripts. //
38 //--------------------------------//
39
40 /*
41 ** Function: _checkMinimumVersion
42 ** Input: STRING version
43 ** Output: BOOLEAN
44 ** Description: Check if the current php version is equal
45 ** or newer than the minimum required version.
46 */
47 function _checkMinimumVersion($version)
48 {
49
50 # Get rid of the points.
51 $minimum_version = preg_replace("%\.%", "", $version);
52 $current_version = preg_replace("%\.%", "", phpversion());
53
54 if ($current_version < $minimum_version)
55 return FALSE;
56 else
57 return TRUE;
58 }
59
60 /*
61 ** Function: _cookieCheck
62 ** Input: VOID
63 ** Output: BOOLEAN
64 ** Description: Checks the existence of a session variable. If the session
65 ** variable exists, the client browser supports cookies.
66 */
67 function _cookieCheck()
68 {
69 global $cookie;
70
71 if (!isset($cookie))
72 {
73 $cookie = 1;
74 return FALSE;
75 }
76 else
77 return TRUE;
78 }
79
80 /*
81 ** Function: _writeLine
82 ** Input: STRING file, STRING line, CHAR mode
83 ** Output: INTEGER message_id
84 ** Description: Write a string to a file. The mode is
85 ** specified like in the fopen function.
86 */
87 function _writeLine($file, $line, $mode)
88 {
89 $fp = fopen($file, $mode) or die("Error while _writeLine to $file");
90
91 if (flock($fp, LOCK_EX))
92 {
93 if ($mode == "a")
94 fseek($fp, 0, SEEK_END);
95
96 fputs($fp, $line);
97
98 # Successful execution
99 $message_id = 1;
100 }
101 else
102 {
103 # LOCK_EX error
104 $message_id = 2;
105 }
106
107 if (flock($fp, LOCK_UN))
108 fclose($fp);
109 else
110 {
111 # LOCK_UN error
112 $message_id = 3;
113 }
114
115 return $message_id;
116 }
117
118 /*
119 ** Function: _writeArray
120 ** Input: STRING file, STRING line, CHAR mode
121 ** Output: INTEGER message_id
122 ** Description: Write an array to a file. The mode is
123 ** specified like in the fopen function.
124 */
125 function _writeArray($file, $array, $mode)
126 {
127 $fp = fopen($file, $mode) or die("Error while _writeArray to $file");
128
129 if (flock($fp, LOCK_EX))
130 {
131 for ($i = 0; $i < sizeof($array); $i++)
132 fputs($fp, $array[$i]);
133
134 # Successful execution
135 $message_id = 1;
136 }
137 else
138 {
139 # LOCK_EX error
140 $message_id = 2;
141 }
142
143 if (flock($fp, LOCK_UN))
144 fclose($fp);
145 else
146 {
147 # LOCK_UN error
148 $message_id = 3;
149 }
150
151 return $message_id;
152 }
153
154 /*
155 ** Function: _incrementIfEqual
156 ** Input: STRING string, &STRING array[], INTEGER index_equal, INTEGER index_increment
157 ** Output: INTEGER changed
158 ** Description: Increment a specified array variable, if two strings are equal.
159 */
160 function _incrementIfEqual($string, &$array, $index_equal, $index_increment)
161 {
162 $changed = 0;
163
164 for ($i = 0; $i < sizeof($array); $i++)
165 {
166 $line = explode("|", $array[$i]);
167
168 if (!strcmp($string, $line[$index_equal]))
169 {
170 $changed = 1;
171 $line[$index_increment] = $line[$index_increment] + 1;
172 }
173
174 $array[$i] = implode("|", $line);
175 }
176
177 return $changed;
178 }
179
180 /*
181 ** Function: _replace_nl
182 ** Input: STRING string
183 ** Output: STRING return_result
184 ** Description: Replace \n with html <br />.
185 */
186 function _replace_nl($string)
187 {
188 $lines = explode("\n", $string);
189 $return_result = "";
190
191 # while there are strings with \n at the end
192 for ($i = 0; $i < count($lines); $i++)
193 {
194 # delete unneccessary characters
195 $lines[$i] = trim($lines[$i]);
196
197 # add <br /> if there is no <br />, some people with html-skills already
198 # add a <br> or a <br /> and press the enter key
199
200 if ((substr($lines[$i], -4) != '<br>') && (substr($lines[$i], -4) != '<br />'))
201 $lines[$i] .= '<br />';
202
203 $return_result .= $lines[$i];
204 }
205
206 return $return_result;
207 }
208
209 /*
210 ** Function: _getDirArray
211 ** Input: STRING path
212 ** Output: STRING dir_array[]
213 ** Description: Get an array with all filenames of a directory.
214 */
215 function _getDirArray($path)
216 {
217 $dir_array = array(
218 );
219
220 # Load directory into an array.
221 $handle = opendir($path);
222
223 # Fill the array with the filenames.
224 while (($file = readdir($handle)) !== FALSE)
225 {
226 if ($file != "." && $file != "..")
227 $dir_array[sizeof($dir_array)] = $file;
228 }
229
230 # Clean up and sort.
231 closedir($handle);
232
233 return $dir_array;
234 }
235
236 /*
237 ** Function: _getListOfFiles
238 ** Input: STRING path_absolute, STRING path_relative, STRING match_string
239 ** Output: STRING files[]
240 ** Description: Get a list of all files of a directory which name matches
241 ** the match_string. Also get the filesizes.
242 */
243 function _getListOfFiles($path_absolute, $path_relative, $match_string)
244 {
245
246 # Load directory into an array.
247 $handle = opendir($path_absolute);
248
249 # Fill the array with the filenames and filesizes.
250 $i = 0;
251
252 while (($file = readdir($handle)) !== FALSE)
253 {
254 if (strstr($file, $match_string))
255 {
256 $files[$i]["name"] = $file;
257 $files[$i]["path"] = $path_relative . $file;
258 $files[$i]["size"] = round(filesize($path_absolute . $file) / 1024, 2);
259 $i++;
260 }
261 }
262
263 # Clean up and sort.
264 closedir($handle);
265
266 return $files;
267 }
268
269 /*
270 ** Function: _getSortedArray
271 ** Input: STRING unsorted_array[], INTEGER sort_index
272 ** Output: STRING sorted_array[]
273 ** Description: Sort a multi-dimensional array. Each dimension is treated
274 ** like a column of a table. The $column specifies the
275 ** primary column to sort by.
276 */
277 function _getSortedArray($unsorted_array, $column)
278 {
279 for ($i = 0; $i < sizeof($unsorted_array); $i++)
280 {
281 $sort_line = explode("|", $unsorted_array[$i]);
282
283 # Find out how many columns exist. Only four columns are allowed.
284 # Important: The last column contains always the \r\n !
285
286 for ($j = 0; $j < sizeof($sort_line); $j++)
287 {
288 # Fill the columns of the table.
289 if (strcmp("n/a", $sort_line[0]))
290 $sorted_array[$j][$i] = $sort_line[$j];
291 else
292 $undefined_line = explode("|", $unsorted_array[$i]);
293 }
294
295 if ($column == 0)
296 {
297 $index_1 = 1;
298 $index_2 = 2;
299 }
300 else if ($column == 1)
301 {
302 $index_1 = 0;
303 $index_2 = 2;
304 }
305 else if ($column == 2)
306 {
307 $index_1 = 0;
308 $index_2 = 1;
309 }
310
311 switch (sizeof($sort_line))
312 {
313 case 2:
314 array_multisort($sorted_array[$column]);
315
316 break;
317
318 case 3:
319 array_multisort($sorted_array[$column], SORT_NUMERIC, SORT_DESC, $sorted_array[$index_1]);
320
321 break;
322
323 case 4:
324 array_multisort($sorted_array[$column], SORT_NUMERIC, SORT_DESC, $sorted_array[$index_1], $sorted_array[$index_2]);
325
326 break;
327 }
328 }
329
330 if (isset($undefined_line))
331 {
332 for ($i = 0; $i < sizeof($undefined_line); $i++)
333 $sorted_array[$i][sizeof($sorted_array[$i])] = $undefined_line[$i];
334 }
335
336 return $sorted_array;
337 }
338
339 /*
340 ** Function: _getSumOfColumn
341 ** Input: STRING table[], INTEGER column
342 ** Output: INTEGER sum
343 ** Description: Sum up every entry of a specified column of a table.
344 */
345 function _getSumOfColumn($table, $column)
346 {
347 $sum = 0;
348
349 for ($i = 0; $i < sizeof($table); $i++)
350 {
351 $table_line = explode("|", $table[$i]);
352 $sum += $table_line[$column];
353 }
354
355 return $sum;
356 }
357
358 /*
359 ** Function: _listArray
360 ** Input: STRING array[]
361 ** Output: STRING str
362 ** Description: List the key-value-pairs of an array.
363 */
364 function _listArray($array)
365 {
366 while (list($key, $value) = each($array))
367 {
368 $str .= "<b>$key:</b> $value<br />\n";
369 }
370
371 return $str;
372 }
373
374 /*
375 ** Function: _backup
376 ** Input: STRING log_data[], STRING date
377 ** Output: INTEGER message_id;
378 ** Description: Backup specified files and delete old backups.
379 */
380 function _backup($log_data, $backup_dir, $backup_log)
381 {
382 $message_id = 0;
383
384 # Get the date of the last backups
385 $backup_file = file($backup_log);
386 $backup_line = explode("|", $backup_file[0]);
387 $timeOfLastBackup = $backup_line[0];
388
389 if (!file_exists($backup_dir))
390 {
391
392 # Create backup directory.
393 mkdir($backup_dir, 0755);
394 }
395
396 while (list($log_name, $log_path) = each($log_data))
397 {
398 $path_original = pathinfo($log_path);
399
400 # Specify path and name of backup files.
401 if ($this->_checkMinimumVersion("4.1.0"))
402 $bak_path = $backup_dir . basename($path_original["basename"], ".log") . ".bak";
403 else
404 {
405 $basename = basename($path_original["basename"]);
406 $basename = substr($basename, 0, -4);
407 $bak_path = $backup_dir . $basename . ".bak";
408 }
409
410 if (copy($log_path, $bak_path))
411 {
412
413 # Files successfully backed up.
414
415 # Note time of execution.
416 $backup_file[0] = "time of last backup|" . time() . "|\r\n";
417
418 if (!isset($backup_file[1]))
419 $backup_file[1] = "atomatic backup made?|0|\r\n";
420
421 $this->_writeArray($backup_log, $backup_file, "w");
422 $message_id = 1;
423 }
424 }
425
426 return $message_id;
427 }
428
429 /*
430 ** Function: _delete
431 ** Input: STRING file
432 ** Output: VOID
433 ** Description: Delete a file in the server.
434 */
435 function _delete($file)
436 {
437
438 # Try the standard unix command.
439 $delete = unlink($file);
440
441 if (file_exists($file))
442 {
443 # Server might run under Windows.
444
445 $filesys = preg_replace("/\//", "\\", $file);
446 $delete = system("del $filesys");
447
448 clearstatcache();
449
450 if (file_exists($file))
451 {
452
453 # Set the file access permission explicitly.
454 $delete = chmod($file, 0775);
455 $delete = unlink($file);
456 $delete = system("del $filesys");
457 }
458 }
459 }
460
461 /*
462 ** Function: _processLogin
463 ** Input: STRING password_data, STRING name, STRING pass
464 ** Output: INTEGER message_id
465 ** Description: Login function. Sets a session variable if user is valid.
466 */
467 function _processLogin($password_data, $name, $pass)
468 {
469
470 # Search name in data file. If name exists,
471 # check password.
472 $temp_file = file($password_data);
473
474 for ($i = 0; $i < sizeof($temp_file); $i++)
475 {
476 $temp_line = explode("|", $temp_file[$i]);
477
478 if (!strcmp($temp_line[0], $name))
479 {
480 # Name does exist.
481 $crypt_pass = crypt($pass, CRYPT_BLOWFISH);
482
483 if (!strcmp($temp_line[1], $crypt_pass))
484 {
485 # Password is correct.
486
487 session_register("logged_in");
488
489 # Check the php version.
490 if ($this->_checkMinimumVersion("4.1.0"))
491 {
492
493 # php version is at least 4.1.0
494 $_SERVER["logged_in"] = TRUE;
495 }
496 else
497 {
498
499 # php version is older than 4.1.0
500 global $HTTP_SERVER_VARS;
501 $HTTP_SERVER_VARS["logged_in"] = TRUE;
502 }
503
504 # Welcome message
505 $message_id = 1;
506 }
507 else
508 {
509
510 # Wrong password
511 $message_id = 2;
512 }
513 }
514 else
515 {
516
517 # Wrong user name
518 $message_id = 3;
519 }
520 }
521
522 return $message_id;
523 }
524
525 /*
526 ** Function: _setPassword
527 ** Input: STRING data_dir, STRING password_data, STRING user, STRING pass
528 ** Output: VOID
529 ** Description: Adds a user to a user database.
530 */
531 function _setPassword($data_dir, $password_data, $user, $pass)
532 {
533 if (!file_exists($data_dir))
534 mkdir($data_dir, 0755);
535 else
536 chmod($data_dir, 0755);
537
538 $crypted_password = crypt($pass, CRYPT_BLOWFISH);
539 $password_line = $user . "|" . $crypted_password . "|\r\n";
540 return $this->_writeLine($password_data, $password_line, "a");
541 }
542
543 /*
544 ** Function: _getFormatedTime
545 ** Input: INTEGER timestamp, STRING mode
546 ** Output: STRING
547 ** Description: Returns a formated date string.
548 */
549 function _getFormatedTime($timestamp, $mode)
550 {
551 switch ($mode)
552 {
553 case "minute":
554 return date("j.n.Y, H:i", $timestamp);
555
556 break;
557
558 case "day":
559 return date("j.n.Y", $timestamp);
560
561 break;
562 }
563 }
564
565 /*
566 ** Function: _defineJumpMenu
567 ** Input: VOID
568 ** Output: VOID
569 ** Description: Defines a java script jump menu.
570 */
572 {
573
574 # This function displays JavaScript code
575 # and has to be called between the <head>
576 # tags of the webpage.
577
578 echo("<script language=\"javascript\" type=\"text/javascript\">\n");
579 echo("<!--\n");
580 echo("function JumpMenu(targ,selObj,restore){\n");
581 echo(" eval(targ+\".location='\"+selObj.options[selObj.selectedIndex].value+\"'\")\n");
582 echo(" if (restore) selObj.selectedIndex=0\n");
583 echo("}\n");
584 echo("//-->\n");
585 echo("</script>\n");
586 }
587
588 /*
589 ** Function: _definePopUpWindow
590 ** Input: VOID
591 ** Output: VOID
592 ** Description: Defines a java script popup.
593 */
595 {
596 echo("<script Language=\"JavaScript\" type=\"text/javascript\">\n");
597 echo("<!--\n");
598 echo("function popup(url, name, width, height)\n");
599 echo("{\n");
600 echo("settings=\n");
601 echo("\"toolbar=no,location=no,directories=no,\"+\n");
602 echo("\"status=no,menubar=no,scrollbars=yes,\"+\n");
603 echo("\"resizable=yes,width=\"+width+\",height=\"+height;\n");
604 echo("MyNewWindow=window.open(url,name,settings);\n");
605 echo("}\n");
606 echo("//-->\n");
607 echo("</script>\n");
608
609 return 0;
610 }
611
612 /*
613 ** Function: _displayJumpMenu
614 ** Input: STRING option_selected, STRING array, STRING variable
615 ** Output: VOID
616 ** Description: Displays a java script menu.
617 */
618 function _displayJumpMenu($option_selected, $array, $variable)
619 {
620 echo("<select class=\"jumpMenu\" name=\"jumpMenu\" onchange=\"JumpMenu('parent',this,0)\">");
621 echo(" <option selected=\"selected\">" . $option_selected . "</option>");
622
623 # Now display all entries from the array.
624 # The newest entry is on top.
625
626 for ($i = sizeof($array) - 1; $i >= 0; $i--)
627 {
628 echo("<option value=\"$this->webpage?" . $variable . "=" . $array[$i]["link"] . "\">");
629 echo($array[$i]["name"]);
630 echo("</option>");
631 }
632
633 echo("</select>");
634 }
635}
636?>
_backup($log_data, $backup_dir, $backup_log)
_displayJumpMenu($option_selected, $array, $variable)
_incrementIfEqual($string, &$array, $index_equal, $index_increment)
_setPassword($data_dir, $password_data, $user, $pass)
_writeLine($file, $line, $mode)
_getFormatedTime($timestamp, $mode)
_getListOfFiles($path_absolute, $path_relative, $match_string)
_checkMinimumVersion($version)
_processLogin($password_data, $name, $pass)
_writeArray($file, $array, $mode)
_getSortedArray($unsorted_array, $column)
_getSumOfColumn($table, $column)