diff --git a/classes/Search.php b/classes/Search.php index f53a224..e77aef5 100644 --- a/classes/Search.php +++ b/classes/Search.php @@ -2,9 +2,9 @@ /** * @package AutoIndex * - * @copyright Copyright (C) 2002-2005 Justin Hagstrom + * @copyright Copyright (C) 2002-2004 Justin Hagstrom * @license http://www.gnu.org/licenses/gpl.html GNU General Public License (GPL) - * @version $Id: Search.php,v 2.2.6 2023/11/25 23:30:08 orynider Exp $ + * @version $Id: Stats.php,v 2.2.6 2023/11/15 08:08:08 orynider Exp $ * @link http://autoindex.sourceforge.net */ @@ -30,167 +30,496 @@ if (!defined('IN_AUTOINDEX') || !IN_AUTOINDEX) } /** - * Similar to DirectoryListDetailed, except this filters out certain - * entries based on filename. + * Creates and displays detailed statistics from the log file. * * @author Justin Hagstrom - * @version 1.0.3 (July 06, 2005) + * @version 1.0.1 (July 12, 2004) * @package AutoIndex */ -class Search extends DirectoryListDetailed +class Stats { /** - * @var array List of matched filenames + * @var array Stores number of downloads per file extension */ - private $matches; + private $extensions; /** - * @return string The HTML text that makes up the search box + * @var array Hits per day */ - public static function search_box() - { - global $words, $subdir, $request; - - $search = ($request->is_set_get('search') ? Url::html_output($request->get('search')) : ''); - $mode = ($request->is_set_get('search_mode') ? self::clean_mode($request->is_set_get('search_mode')) : 'f'); - $modes = array('files' => 'f', 'folders' => 'd', 'both' => 'fd'); - - $out = ' -
' . ' -

- ' . ' -
- - -

-
'; - - return $out; - } + private $dates; /** - * @param string $filename - * @param string $string - * @return bool True if string matches filename + * @var array Unique hits per day */ - private static function match(&$filename, &$string) - { - if (preg_match_all('/(?<=")[^"]+(?=")|[^ "]+/', $string, $matches)) - { - foreach ($matches[0] as $w) - { - if (stripos($filename, $w) !== false) - { - return true; - } - } - } - return false; - } + private $unique_hits; /** - * Merges $obj into $this. + * @var array Keys are the country codes and values are the number of visits + */ + private $countries; + + /** + * @var int Total views of the base_dir + */ + private $total_hits; + + /** + * @var int The number of days that there is a log entry for + */ + private $num_days; + + /** + * @var int Average hits per day ($total_hits / $num_days) + */ + private $avg; + + /** + * Returns $num formatted with a color (green for positive numbers, red + * for negative numbers, and black for 0). * - * @param Search $obj - */ - private function merge(Search $obj) - { - $this->total_folders += $obj->__get('total_folders'); - $this->total_files += $obj->__get('total_files'); - $this->total_downloads += $obj->__get('total_downloads'); - $this->total_size->add_size($obj->__get('total_size')); - $this->matches = array_merge($this->matches, $obj->__get('contents')); - } - - /** - * Returns a string with all characters except 'd' and 'f' stripped. - * Either 'd' 'f' 'df' will be returned, defaults to 'f' - * - * @param string $mode + * @param int $num * @return string */ - private static function clean_mode($mode) + private static function get_change_color($num) { - $str = ''; - if (stripos($mode, 'f') !== false) + if ($num > 0) { - $str .= 'f'; + return '+'; } - if (stripos($mode, 'd') !== false) + if ($num < 0) { - $str .= 'd'; + return ''; } - else if ($str == '') - { - $str = 'f'; - } - return $str; + return ''; } /** - * @param string $query String to search for - * @param string $dir The folder to search (recursive) - * @param string $mode Should be f (files), d (directories), or fd (both) + * If $array[$num] is set, it will be incremented by 1, otherwise it will + * be set to 1. + * + * @param int $num + * @param array $array */ - public function __construct($query, $dir, $mode) + private static function add_num_to_array($num, &$array) { - if (strlen($query) < 2 || strlen($query) > 20) + isset($array[$num]) ? $array[$num]++ : $array[$num] = 1; + } + + /** + * Reads the log file, and sets the member variables after doing + * calculations. + */ + public function __construct() + { + $extensions = $dates = $unique_hits = $countries = array(); + $total_hits = 0; + global $config, $request; + $log_file = $config->__get('log_file'); + $base_dir = $config->__get('base_dir'); + $h = @fopen($log_file, 'rb'); + if ($h === false) { - throw new ExceptionDisplay('Search query is either too long or too short.'); + throw new ExceptionDisplay("Cannot open log file: $log_file"); } - $mode = self::clean_mode($mode); - $dir = Item::make_sure_slash($dir); - DirectoryList::__construct($dir); - $this->matches = array(); - $this->total_size = new Size(0); - $this->total_downloads = $this->total_folders = $this->total_files = 0; - foreach ($this as $item) + while (!feof($h)) { - if ($item == '..') + $entries = explode("\t", rtrim(fgets($h, 1024), "\r\n")); + if (count($entries) === 7) { - continue; - } - if (@is_dir($dir . $item)) - { - if (stripos($mode, 'd') !== false && self::match($item, $query)) + //find the number of unique visits + if ($entries[5] == $base_dir) { - $temp = new DirItem($dir, $item); - $this->matches[] = $temp; - if ($temp->__get('size')->__get('bytes') !== false) + $total_hits++; + if (!in_array($entries[3], $unique_hits)) { - $this->total_size->add_size($temp->__get('size')); + $unique_hits[] = Url::html_output($entries[3]); } - $this->total_folders++; + + //find country codes by hostnames + $cc = FileItem::ext($entries[3]); + if (preg_match('/^[a-z]+$/i', $cc)) + { + self::add_num_to_array($cc, $countries); + } + + //find the dates of the visits + self::add_num_to_array($entries[0], $dates); + } + + //find file extensions + $ext = FileItem::ext($entries[6]); + if (preg_match('/^[\w-]+$/', $ext)) + { + self::add_num_to_array($ext, $extensions); } - $sub_search = new Search($query, $dir . $item, $mode); - $this->merge($sub_search); - } - else if (stripos($mode, 'f') !== false && self::match($item, $query)) - { - $temp = new FileItem($dir, $item); - $this->matches[] = $temp; - $this->total_size->add_size($temp->__get('size')); - $this->total_downloads += $temp->__get('downloads'); - $this->total_files++; } } - global $words, $config, $subdir, $request; - $link = ' ' - . Url::html_output($dir) . ' '; - $this->path_nav = $words->__get('search results for') - . $link . $words->__get('and its subdirectories'); - $this->contents = $this->matches; - unset($this->matches); + fclose($h); + $this->num_days = count($dates); + $this->avg = round($total_hits / $this->num_days); + $this->extensions = $extensions; + $this->dates = $dates; + $this->unique_hits = $unique_hits; + $this->countries = $countries; + $this->total_hits = $total_hits; + } + + /** + * Uses the display class to output results. + */ + public function display() + { + static $country_codes = array( + 'af' => 'Afghanistan', + 'al' => 'Albania', + 'dz' => 'Algeria', + 'as' => 'American Samoa', + 'ad' => 'Andorra', + 'ao' => 'Angola', + 'ai' => 'Anguilla', + 'aq' => 'Antarctica', + 'ag' => 'Antigua and Barbuda', + 'ar' => 'Argentina', + 'am' => 'Armenia', + 'aw' => 'Aruba', + 'au' => 'Australia', + 'at' => 'Austria', + 'ax' => 'Ålang Islands', + 'az' => 'Azerbaidjan', + 'bs' => 'Bahamas', + 'bh' => 'Bahrain', + 'bd' => 'Banglades', + 'bb' => 'Barbados', + 'by' => 'Belarus', + 'be' => 'Belgium', + 'bz' => 'Belize', + 'bj' => 'Benin', + 'bm' => 'Bermuda', + 'bo' => 'Bolivia', + 'ba' => 'Bosnia-Herzegovina', + 'bw' => 'Botswana', + 'bv' => 'Bouvet Island', + 'br' => 'Brazil', + 'io' => 'British Indian O. Terr.', + 'bn' => 'Brunei Darussalam', + 'bg' => 'Bulgaria', + 'bf' => 'Burkina Faso', + 'bi' => 'Burundi', + 'bt' => 'Buthan', + 'kh' => 'Cambodia', + 'cm' => 'Cameroon', + 'ca' => 'Canada', + 'cv' => 'Cape Verde', + 'ky' => 'Cayman Islands', + 'cf' => 'Central African Rep.', + 'td' => 'Chad', + 'cl' => 'Chile', + 'cn' => 'China', + 'cx' => 'Christmas Island', + 'cc' => 'Cocos (Keeling) Isl.', + 'co' => 'Colombia', + 'km' => 'Comoros', + 'cg' => 'Congo', + 'ck' => 'Cook Islands', + 'cr' => 'Costa Rica', + 'hr' => 'Croatia', + 'cu' => 'Cuba', + 'cy' => 'Cyprus', + 'cz' => 'Czech Republic', + 'cs' => 'Czechoslovakia', + 'dk' => 'Denmark', + 'dj' => 'Djibouti', + 'dm' => 'Dominica', + 'do' => 'Dominican Republic', + 'tp' => 'East Timor', + 'ec' => 'Ecuador', + 'eg' => 'Egypt', + 'sv' => 'El Salvador', + 'gq' => 'Equatorial Guinea', + 'ee' => 'Estonia', + 'et' => 'Ethiopia', + 'fk' => 'Falkland Isl. (UK)', + 'fo' => 'Faroe Islands', + 'fj' => 'Fiji', + 'fi' => 'Finland', + 'fr' => 'France', + 'fx' => 'France (European Terr.)', + 'tf' => 'French Southern Terr.', + 'ga' => 'Gabon', + 'gm' => 'Gambia', + 'ge' => 'Georgia', + 'de' => 'Germany', + 'gh' => 'Ghana', + 'gi' => 'Gibraltar', + 'gb' => 'Great Britain (UK)', + 'gr' => 'Greece', + 'gl' => 'Greenland', + 'gd' => 'Grenada', + 'gp' => 'Guadeloupe (Fr)', + 'gu' => 'Guam (US)', + 'gt' => 'Guatemala', + 'gn' => 'Guinea', + 'gw' => 'Guinea Bissau', + 'gy' => 'Guyana', + 'gf' => 'Guyana (Fr)', + 'ht' => 'Haiti', + 'hm' => 'Heard & McDonald Isl.', + 'hn' => 'Honduras', + 'hk' => 'Hong Kong', + 'hu' => 'Hungary', + 'is' => 'Iceland', + 'in' => 'India', + 'id' => 'Indonesia', + 'ir' => 'Iran', + 'iq' => 'Iraq', + 'ie' => 'Ireland', + 'il' => 'Israel', + 'it' => 'Italy', + 'ci' => 'Ivory Coast', + 'jm' => 'Jamaica', + 'jp' => 'Japan', + 'jo' => 'Jordan', + 'kz' => 'Kazachstan', + 'ke' => 'Kenya', + 'kg' => 'Kirgistan', + 'ki' => 'Kiribati', + 'kp' => 'North Korea', + 'kr' => 'South Korea', + 'kw' => 'Kuwait', + 'la' => 'Laos', + 'lv' => 'Latvia', + 'lb' => 'Lebanon', + 'ls' => 'Lesotho', + 'lr' => 'Liberia', + 'ly' => 'Libya', + 'li' => 'Liechtenstein', + 'lt' => 'Lithuania', + 'lu' => 'Luxembourg', + 'mo' => 'Macau', + 'mg' => 'Madagascar', + 'mw' => 'Malawi', + 'my' => 'Malaysia', + 'mv' => 'Maldives', + 'ml' => 'Mali', + 'mt' => 'Malta', + 'mh' => 'Marshall Islands', + 'mk' => 'Macedonia', + 'mq' => 'Martinique (Fr.)', + 'mr' => 'Mauritania', + 'mu' => 'Mauritius', + 'mx' => 'Mexico', + 'fm' => 'Micronesia', + 'md' => 'Moldavia', + 'mc' => 'Monaco', + 'mn' => 'Mongolia', + 'ms' => 'Montserrat', + 'ma' => 'Morocco', + 'mz' => 'Mozambique', + 'mm' => 'Myanmar', + 'na' => 'Namibia', + 'nr' => 'Nauru', + 'np' => 'Nepal', + 'an' => 'Netherland Antilles', + 'nl' => 'Netherlands', + 'nt' => 'Neutral Zone', + 'nc' => 'New Caledonia (Fr.)', + 'nz' => 'New Zealand', + 'ni' => 'Nicaragua', + 'ne' => 'Niger', + 'ng' => 'Nigeria', + 'nu' => 'Niue', + 'nf' => 'Norfolk Island', + 'mp' => 'Northern Mariana Isl.', + 'no' => 'Norway', + 'om' => 'Oman', + 'pk' => 'Pakistan', + 'pw' => 'Palau', + 'pa' => 'Panama', + 'pg' => 'Papua New Guinea', + 'py' => 'Paraguay', + 'pe' => 'Peru', + 'ph' => 'Philippines', + 'pn' => 'Pitcairn', + 'pl' => 'Poland', + 'pf' => 'Polynesia (Fr.)', + 'pt' => 'Portugal', + 'pr' => 'Puerto Rico (US)', + 'qa' => 'Qatar', + 're' => 'Réunion (Fr.)', + 'ro' => 'Romania', + 'ru' => 'Russian Federation', + 'rw' => 'Rwanda', + 'lc' => 'Saint Lucia', + 'ws' => 'Samoa', + 'sm' => 'San Marino', + 'sa' => 'Saudi Arabia', + 'sn' => 'Senegal', + 'sc' => 'Seychelles', + 'sl' => 'Sierra Leone', + 'sg' => 'Singapore', + 'sk' => 'Slovak Republic', + 'si' => 'Slovenia', + 'sb' => 'Solomon Islands', + 'so' => 'Somalia', + 'za' => 'South Africa', + 'su' => 'Soviet Union', + 'es' => 'Spain', + 'lk' => 'Sri Lanka', + 'sh' => 'St. Helena', + 'pm' => 'St. Pierre & Miquelon', + 'st' => 'St. Tome and Principe', + 'kn' => 'St. Kitts Nevis Anguilla', + 'vc' => 'St. Vincent & Grenadines', + 'sd' => 'Sudan', + 'sr' => 'Suriname', + 'sj' => 'Svalbard & Jan Mayen Isl.', + 'sz' => 'Swaziland', + 'se' => 'Sweden', + 'ch' => 'Switzerland', + 'sy' => 'Syria', + 'tj' => 'Tadjikistan', + 'tw' => 'Taiwan', + 'tz' => 'Tanzania', + 'th' => 'Thailand', + 'tg' => 'Togo', + 'tk' => 'Tokelau', + 'to' => 'Tonga', + 'tt' => 'Trinidad & Tobago', + 'tn' => 'Tunisia', + 'tr' => 'Turkey', + 'tm' => 'Turkmenistan', + 'tc' => 'Turks & Caicos Islands', + 'tv' => 'Tuvalu', + 'ug' => 'Uganda', + 'ua' => 'Ukraine', + 'ae' => 'United Arab Emirates', + 'uk' => 'United Kingdom', + 'us' => 'United States', + 'uy' => 'Uruguay', + 'um' => 'US Minor outlying Isl.', + 'uz' => 'Uzbekistan', + 'vu' => 'Vanuatu', + 'va' => 'Vatican City State', + 've' => 'Venezuela', + 'vn' => 'Vietnam', + 'vg' => 'Virgin Islands (British)', + 'vi' => 'Virgin Islands (US)', + 'wf' => 'Wallis & Futuna Islands', + 'wlk' => 'Wales', + 'eh' => 'Western Sahara', + 'ye' => 'Yemen', + 'yu' => 'Yugoslavia', + 'zr' => 'Zaire', + 'zm' => 'Zambia', + 'zw' => 'Zimbabwe', + 'mil' => 'United States Military', + 'gov' => 'United States Government', + 'com' => 'Commercial', + 'net' => 'Network', + 'org' => 'Non-Profit Organization', + 'edu' => 'Educational', + 'int' => 'International', + 'aero' => 'Air Transport Industry', + 'biz' => 'Businesses', + 'coop' => 'Non-profit cooperatives', + 'arpa' => 'Arpanet', + 'info' => 'Info', + 'name' => 'Name', + 'nato' => 'Nato', + 'museum' => 'Museum', + 'pro' => 'Pro' + ); + + $str = ' + + + + '. " + + + + + + + + + + +
 TotalDaily
Hits{$this->total_hits}{$this->avg}" . '
Unique Hits' . count($this->unique_hits) . '' . round(count($this->unique_hits) / $this->num_days) . '
+

Percent Unique: ' . number_format(count($this->unique_hits) / $this->total_hits * 100, 1) . '

'; + + arsort($this->extensions); + arsort($this->countries); + + $date_nums = array_values($this->dates); + $str .= ' + + + + + + + '; + $i = 0; + foreach ($this->dates as $day => $num) + { + $diff = $num - $this->avg; + $change = (($i > 0) ? ($num - $date_nums[$i-1]) : 0); + $change_color = self::get_change_color($change); + $diff_color = self::get_change_color($diff); + $class = (($i++ % 2) ? 'dark_row' : 'light_row'); + $str .= " + + + + + + "; + } + $str .= ' +
DateHits That DayChange From Previous DayDifference From Average (' . $this->avg . ')
$day$num$change_color$change$diff_color$diff
+

+ + + + + + '; + $i = 0; + foreach ($this->extensions as $ext => $num) + { + $class = (($i++ % 2) ? 'dark_row' : 'light_row'); + $str .= " + + + "; + } + $str .= ' +
Downloads based on file extensionsTotalDaily
$ext$num" . number_format($num / $this->num_days, 1) . "
+

+ + + + + + '; + $i = 0; + foreach ($this->countries as $c => $num) + { + $c_code = (isset($country_codes[strtolower($c)]) ? ' (' . $country_codes[strtolower($c)] . ')' : ''); + $class = (($i++ % 2) ? 'dark_row' : 'light_row'); + $str .= " + + + + + \n"; + } + $str .= ' +
Hostname ISP extensionTotalDaily
$c{$c_code}$num" . number_format($num / $this->num_days, 1) . "
+

Continue.

'; + echo new Display($str); + die(); } }