| 18 | | // security check |
| 19 | | // this check is slightly different from most security checks since we want |
| 20 | | // to exclude stats being collected for site admins |
| 21 | | if (SecurityUtil::checkPermission('::', '::', ACCESS_ADMIN)) { |
| 22 | | return; |
| 23 | | } |
| 24 | | |
| 25 | | // check if stats collection is enabled |
| 26 | | if (!pnModGetVar('stats', 'collect')) { |
| 27 | | return; |
| 28 | | } |
| 29 | | |
| 30 | | // check for the sniffer module |
| 31 | | if (!pnModAvailable('Sniffer')) { |
| 32 | | LogUtil::registerError(_STATS_SNIFFERREQUIRED); |
| 33 | | return; |
| 34 | | } |
| 35 | | |
| 36 | | // check for the excluded ip address |
| 37 | | $ip = pnModGetVar('stats', 'excludeip'); |
| 38 | | $host = pnServerGetVar('REMOTE_ADDR'); |
| 39 | | if (substr($host, 0, strlen($ip)) == $ip) { |
| 40 | | return; |
| 41 | | } |
| 42 | | |
| 43 | | // call the sniffer module to collect the browser and os info |
| 44 | | $browserinfo = pnModAPIFunc('Sniffer', 'user', 'get'); |
| 45 | | |
| 46 | | // get the broswer data |
| 47 | | switch ($browserinfo->_browser_info['browser']) { |
| 48 | | // we treat mozilla firebird as firefox since these are the same product range |
| 49 | | case 'fx' : |
| 50 | | case 'fb' : |
| 51 | | $browser = 'Firefox'; |
| 52 | | break; |
| 53 | | case 'ie' : |
| 54 | | $browser = 'MSIE'; |
| 55 | | break; |
| 56 | | case 'ns' : |
| 57 | | $browser = 'Netscape'; |
| 58 | | break; |
| 59 | | case 'op' : |
| 60 | | $browser = 'Opera'; |
| 61 | | break; |
| 62 | | case 'sf' : |
| 63 | | $browser = 'Safari'; |
| 64 | | break; |
| 65 | | case 'mz' : |
| 66 | | $browser = 'Mozilla'; |
| 67 | | break; |
| 68 | | case 'lx' : |
| 69 | | $browser = 'Lynx'; |
| 70 | | break; |
| 71 | | case 'ch' : |
| 72 | | $browser = 'Chimera'; |
| 73 | | break; |
| 74 | | case 'ca' : |
| 75 | | $browser = 'Camino'; |
| 76 | | break; |
| 77 | | case 'ep' : |
| 78 | | $browser = 'Epiphany'; |
| 79 | | break; |
| 80 | | case 'ga' : |
| 81 | | $browser = 'Galeon'; |
| 82 | | break; |
| 83 | | case 'km' : |
| 84 | | $browser = 'K-Meleon'; |
| 85 | | break; |
| 86 | | case 'kq' : |
| 87 | | $browser = 'Konqueror'; |
| 88 | | break; |
| 89 | | default : |
| 90 | | $browser = 'Other'; |
| 91 | | break; |
| 92 | | } |
| 93 | | |
| 94 | | // get the platform and os data |
| 95 | | switch ($browserinfo->_browser_info['platform']) { |
| 96 | | case 'win': |
| 97 | | $os = 'Windows'; |
| 98 | | break; |
| 99 | | case 'mac': |
| 100 | | switch ($browserinfo->_browser_info['os']) { |
| 101 | | case 'osx': |
| 102 | | $os = 'MacOSX'; |
| 103 | | break; |
| 104 | | default: |
| 105 | | $os = 'Mac'; |
| 106 | | } |
| 107 | | break; |
| 108 | | case 'os2': |
| 109 | | $os = 'OS/2'; |
| 110 | | break; |
| 111 | | case '*nix': |
| 112 | | switch ($browserinfo->_browser_info['os']) { |
| 113 | | case 'linix': |
| 114 | | $os = 'Linux'; |
| 115 | | break; |
| 116 | | case 'bsd': |
| 117 | | case 'freebsd': |
| 118 | | $os = 'BSD'; |
| 119 | | break; |
| 120 | | case 'sun': |
| 121 | | case 'sun4': |
| 122 | | case 'sun5': |
| 123 | | case 'suni86': |
| 124 | | $os = 'SunOS'; |
| 125 | | break; |
| 126 | | case 'irix5': |
| 127 | | case 'irix6': |
| 128 | | $os = 'IRIX'; |
| 129 | | break; |
| 130 | | case 'aix': |
| 131 | | case 'aix1': |
| 132 | | case 'aix2': |
| 133 | | case 'aix3': |
| 134 | | $os = 'AIX'; |
| 135 | | break; |
| 136 | | default: |
| 137 | | $os = 'Other'; |
| 138 | | break; |
| 139 | | } |
| 140 | | break; |
| 141 | | default: |
| 142 | | $os = 'Other'; |
| 143 | | break; |
| 144 | | } |
| 145 | | |
| 146 | | // Save on the databases the obtained values |
| 147 | | $dbconn = pnDBGetConn(true); |
| 148 | | $pntable = pnDBGetTables(); |
| 149 | | |
| 150 | | $column = $pntable['counter_column']; |
| 151 | | $sql = "UPDATE $pntable[counter] |
| 152 | | SET $column[count]=$column[count]+1 |
| 153 | | WHERE ($column[type]='total' AND $column[var]='hits') |
| 154 | | OR ($column[var]='".DataUtil::formatForStore($browser)."' AND $column[type]='browser') |
| 155 | | OR ($column[var]='".DataUtil::formatForStore($os)."' AND $column[type]='os')"; |
| 156 | | $dbconn->Execute($sql); |
| 157 | | |
| 158 | | // Per-Day-Counter |
| 159 | | $xydate=date('dmY'); |
| 160 | | $column = $pntable['stats_date_column']; |
| 161 | | $xyval = $dbconn->Execute("SELECT $column[hits] as hits |
| 162 | | FROM $pntable[stats_date] |
| 163 | | WHERE $column[date]='".DataUtil::formatForStore($xydate)."'"); |
| 164 | | |
| 165 | | if ($dbconn->ErrorNo() != 0) { |
| 166 | | return LogUtil::registerError('Error accessing stats information'); |
| 167 | | } |
| 168 | | $ttemp=$xyval->GetRowAssoc(false); |
| 169 | | $xyval->MoveNext(); |
| 170 | | $happend=$ttemp['hits']; |
| 171 | | if ($happend==''||$happend==false||!$happend) { |
| 172 | | $column = $pntable['stats_date_column']; |
| 173 | | $dbconn->Execute("INSERT INTO $pntable[stats_date] |
| 174 | | ($column[date], $column[hits]) VALUES ('".DataUtil::formatForStore($xydate)."','1')"); |
| 175 | | } else { |
| 176 | | $column = $pntable['stats_date_column']; |
| 177 | | $dbconn->Execute("UPDATE $pntable[stats_date] |
| 178 | | SET $column[hits]=$column[hits]+1 |
| 179 | | WHERE $column[date]='".DataUtil::formatForStore($xydate)."'"); |
| 180 | | } |
| 181 | | |
| 182 | | // Per-Hour-Counter |
| 183 | | $xyhour=date('G'); |
| 184 | | $column = $pntable['stats_hour_column']; |
| 185 | | $dbconn->Execute("UPDATE $pntable[stats_hour] |
| 186 | | SET $column[hits]=$column[hits]+1 |
| 187 | | WHERE $column[hour]='".DataUtil::formatForStore($xyhour)."'"); |
| 188 | | |
| 189 | | // Weekday-Counter |
| 190 | | $xyweekday=date('w'); |
| 191 | | $column = $pntable['stats_week_column']; |
| 192 | | $dbconn->Execute("UPDATE $pntable[stats_week] |
| 193 | | SET $column[hits]=$column[hits]+1 |
| 194 | | WHERE $column[weekday]='".DataUtil::formatForStore($xyweekday)."'"); |
| 195 | | |
| 196 | | // Month-Counter |
| 197 | | $xymonth=date('m'); |
| 198 | | $column = $pntable['stats_month_column']; |
| 199 | | $dbconn->Execute("UPDATE $pntable[stats_month] |
| 200 | | SET $column[hits]=$column[hits]+1 |
| 201 | | WHERE $column[month]='".DataUtil::formatForStore($xymonth)."'"); |
| | 18 | // security check |
| | 19 | // this check is slightly different from most security checks since we want |
| | 20 | // to exclude stats being collected for site admins |
| | 21 | if (SecurityUtil::checkPermission('::', '::', ACCESS_ADMIN)) { |
| | 22 | return; |
| | 23 | } |
| | 24 | |
| | 25 | // check if stats collection is enabled |
| | 26 | if (!pnModGetVar('stats', 'collect')) { |
| | 27 | return; |
| | 28 | } |
| | 29 | |
| | 30 | // check for the sniffer module |
| | 31 | if (!pnModAvailable('Sniffer')) { |
| | 32 | LogUtil::registerError(_STATS_SNIFFERREQUIRED); |
| | 33 | return; |
| | 34 | } |
| | 35 | |
| | 36 | // check for the excluded ip address |
| | 37 | $ip = pnModGetVar('stats', 'excludeip'); |
| | 38 | $host = pnServerGetVar('REMOTE_ADDR'); |
| | 39 | if (substr($host, 0, strlen($ip)) == $ip) { |
| | 40 | return; |
| | 41 | } |
| | 42 | |
| | 43 | // call the sniffer module to collect the browser and os info |
| | 44 | $browserinfo = pnModAPIFunc('Sniffer', 'user', 'get'); |
| | 45 | |
| | 46 | // get the broswer data |
| | 47 | switch ($browserinfo->_browser_info['browser']) { |
| | 48 | // we treat mozilla firebird as firefox since these are the same product range |
| | 49 | case 'fx' : |
| | 50 | case 'fb' : |
| | 51 | $browser = 'Firefox'; |
| | 52 | break; |
| | 53 | case 'ie' : |
| | 54 | $browser = 'MSIE'; |
| | 55 | break; |
| | 56 | case 'ns' : |
| | 57 | $browser = 'Netscape'; |
| | 58 | break; |
| | 59 | case 'op' : |
| | 60 | $browser = 'Opera'; |
| | 61 | break; |
| | 62 | case 'sf' : |
| | 63 | $browser = 'Safari'; |
| | 64 | break; |
| | 65 | case 'mz' : |
| | 66 | $browser = 'Mozilla'; |
| | 67 | break; |
| | 68 | case 'lx' : |
| | 69 | $browser = 'Lynx'; |
| | 70 | break; |
| | 71 | case 'ch' : |
| | 72 | $browser = 'Chimera'; |
| | 73 | break; |
| | 74 | case 'ca' : |
| | 75 | $browser = 'Camino'; |
| | 76 | break; |
| | 77 | case 'ep' : |
| | 78 | $browser = 'Epiphany'; |
| | 79 | break; |
| | 80 | case 'ga' : |
| | 81 | $browser = 'Galeon'; |
| | 82 | break; |
| | 83 | case 'km' : |
| | 84 | $browser = 'K-Meleon'; |
| | 85 | break; |
| | 86 | case 'kq' : |
| | 87 | $browser = 'Konqueror'; |
| | 88 | break; |
| | 89 | default : |
| | 90 | $browser = 'Other'; |
| | 91 | break; |
| | 92 | } |
| | 93 | |
| | 94 | // get the platform and os data |
| | 95 | switch ($browserinfo->_browser_info['platform']) { |
| | 96 | case 'win': |
| | 97 | $os = 'Windows'; |
| | 98 | break; |
| | 99 | case 'mac': |
| | 100 | switch ($browserinfo->_browser_info['os']) { |
| | 101 | case 'osx': |
| | 102 | $os = 'MacOSX'; |
| | 103 | break; |
| | 104 | default: |
| | 105 | $os = 'Mac'; |
| | 106 | } |
| | 107 | break; |
| | 108 | case 'os2': |
| | 109 | $os = 'OS/2'; |
| | 110 | break; |
| | 111 | case '*nix': |
| | 112 | switch ($browserinfo->_browser_info['os']) { |
| | 113 | case 'linux': |
| | 114 | $os = 'Linux'; |
| | 115 | break; |
| | 116 | case 'bsd': |
| | 117 | case 'freebsd': |
| | 118 | $os = 'BSD'; |
| | 119 | break; |
| | 120 | case 'sun': |
| | 121 | case 'sun4': |
| | 122 | case 'sun5': |
| | 123 | case 'suni86': |
| | 124 | $os = 'SunOS'; |
| | 125 | break; |
| | 126 | case 'irix5': |
| | 127 | case 'irix6': |
| | 128 | $os = 'IRIX'; |
| | 129 | break; |
| | 130 | case 'aix': |
| | 131 | case 'aix1': |
| | 132 | case 'aix2': |
| | 133 | case 'aix3': |
| | 134 | $os = 'AIX'; |
| | 135 | break; |
| | 136 | default: |
| | 137 | $os = 'Other'; |
| | 138 | break; |
| | 139 | } |
| | 140 | break; |
| | 141 | default: |
| | 142 | $os = 'Other'; |
| | 143 | break; |
| | 144 | } |
| | 145 | |
| | 146 | // Save on the databases the obtained values |
| | 147 | $dbconn = pnDBGetConn(true); |
| | 148 | $pntable = pnDBGetTables(); |
| | 149 | |
| | 150 | $column = $pntable['counter_column']; |
| | 151 | $sql = "UPDATE $pntable[counter] |
| | 152 | SET $column[count]=$column[count]+1 |
| | 153 | WHERE ($column[type]='total' AND $column[var]='hits') |
| | 154 | OR ($column[var]='".DataUtil::formatForStore($browser)."' AND $column[type]='browser') |
| | 155 | OR ($column[var]='".DataUtil::formatForStore($os)."' AND $column[type]='os')"; |
| | 156 | $dbconn->Execute($sql); |
| | 157 | |
| | 158 | // Per-Day-Counter |
| | 159 | $xydate=date('dmY'); |
| | 160 | $column = $pntable['stats_date_column']; |
| | 161 | $xyval = $dbconn->Execute("SELECT $column[hits] as hits |
| | 162 | FROM $pntable[stats_date] |
| | 163 | WHERE $column[date]='".DataUtil::formatForStore($xydate)."'"); |
| | 164 | |
| | 165 | if ($dbconn->ErrorNo() != 0) { |
| | 166 | return LogUtil::registerError('Error accessing stats information'); |
| | 167 | } |
| | 168 | $ttemp=$xyval->GetRowAssoc(false); |
| | 169 | $xyval->MoveNext(); |
| | 170 | $happend=$ttemp['hits']; |
| | 171 | if ($happend==''||$happend==false||!$happend) { |
| | 172 | $column = $pntable['stats_date_column']; |
| | 173 | $dbconn->Execute("INSERT INTO $pntable[stats_date] |
| | 174 | ($column[date], $column[hits]) VALUES ('".DataUtil::formatForStore($xydate)."','1')"); |
| | 175 | } else { |
| | 176 | $column = $pntable['stats_date_column']; |
| | 177 | $dbconn->Execute("UPDATE $pntable[stats_date] |
| | 178 | SET $column[hits]=$column[hits]+1 |
| | 179 | WHERE $column[date]='".DataUtil::formatForStore($xydate)."'"); |
| | 180 | } |
| | 181 | |
| | 182 | // Per-Hour-Counter |
| | 183 | $xyhour=date('G'); |
| | 184 | $column = $pntable['stats_hour_column']; |
| | 185 | $dbconn->Execute("UPDATE $pntable[stats_hour] |
| | 186 | SET $column[hits]=$column[hits]+1 |
| | 187 | WHERE $column[hour]='".DataUtil::formatForStore($xyhour)."'"); |
| | 188 | |
| | 189 | // Weekday-Counter |
| | 190 | $xyweekday=date('w'); |
| | 191 | $column = $pntable['stats_week_column']; |
| | 192 | $dbconn->Execute("UPDATE $pntable[stats_week] |
| | 193 | SET $column[hits]=$column[hits]+1 |
| | 194 | WHERE $column[weekday]='".DataUtil::formatForStore($xyweekday)."'"); |
| | 195 | |
| | 196 | // Month-Counter |
| | 197 | $xymonth=date('m'); |
| | 198 | $column = $pntable['stats_month_column']; |
| | 199 | $dbconn->Execute("UPDATE $pntable[stats_month] |
| | 200 | SET $column[hits]=$column[hits]+1 |
| | 201 | WHERE $column[month]='".DataUtil::formatForStore($xymonth)."'"); |
| 206 | | // Security check |
| 207 | | if (!SecurityUtil::checkPermission('Stats::', '::', ACCESS_READ)) { |
| 208 | | return LogUtil::registerError (_MODULENOAUTH); |
| 209 | | } |
| 210 | | |
| 211 | | $dbconn = pnDBGetConn(true); |
| 212 | | $pntable = pnDBGetTables(); |
| 213 | | |
| 214 | | /************************ Begin Main stats collections *************************************/ |
| 215 | | // Stats Hour - Day Stats |
| 216 | | for ($i = 1; $i <= 12; $i++) { |
| 217 | | $monthnames[] = gmstrftime('%B', gmmktime(0,0,0,$i,1,'1970')); |
| 218 | | } |
| 219 | | // loop counter starts as 4 since 4th Jan 1970 is a sunday; the first day of the week |
| 220 | | for ($i = 4; $i <= 11; $i++) { |
| 221 | | $weekdaynames[] = gmstrftime('%A', gmmktime(0,0,0,1,$i,'1970')); |
| 222 | | } |
| 223 | | |
| 224 | | $toddate = date("dmY"); |
| 225 | | // 24hours ago = yesterday |
| 226 | | $yesdate = date("dmY",time()-(60*60*24)); |
| 227 | | |
| 228 | | $column = $pntable['stats_date_column']; |
| 229 | | $toddb = $dbconn->Execute("SELECT $column[hits] as hits FROM $pntable[stats_date] WHERE $column[date]='".DataUtil::formatForStore($toddate)."'"); |
| 230 | | if (!$toddb->EOF) { |
| 231 | | list($valtoday)=$toddb->fields; |
| 232 | | } else { |
| 233 | | $valtoday=0; |
| 234 | | } |
| 235 | | |
| 236 | | $column = $pntable['stats_date_column']; |
| 237 | | $yesdb = $dbconn->Execute("SELECT $column[hits] as hits FROM $pntable[stats_date] WHERE $column[date]='".DataUtil::formatForStore($yesdate)."'"); |
| 238 | | if (!$yesdb->EOF) { |
| 239 | | list($valyesday)=$yesdb->fields; |
| 240 | | } else { |
| 241 | | $valyesday=0; |
| 242 | | } |
| 243 | | |
| 244 | | // Fetch some more infos about best and worst day ever |
| 245 | | $column = $pntable['stats_date_column']; |
| 246 | | $query = "SELECT $column[date], $column[hits] |
| 247 | | FROM $pntable[stats_date] |
| 248 | | ORDER BY $column[hits] DESC"; |
| 249 | | $dbr = $dbconn->SelectLimit("$query,1"); |
| 250 | | list ($best_day_date, $best_day_hits) = $dbr->fields; |
| 251 | | |
| 252 | | $best_day = mktime(0, 0, 0, substr($best_day_date, 2, 2), substr($best_day_date, 0, 2), substr($best_day_date, 4, 4)); |
| 253 | | $query = "SELECT $column[date], $column[hits] |
| 254 | | FROM $pntable[stats_date] |
| 255 | | ORDER BY $column[hits] ASC"; |
| 256 | | $dbr = $dbconn->SelectLimit("$query,1"); |
| 257 | | list ($worst_day_date, $worst_day_hits) = $dbr->fields; |
| 258 | | $worst_day = mktime(0, 0, 0, substr($worst_day_date, 2, 2), substr($worst_day_date, 0, 2), substr($worst_day_date, 4, 4)); |
| 259 | | |
| 260 | | // get all rows from the db at once and go through the result |
| 261 | | $column = $pntable['stats_hour_column']; |
| 262 | | $result = $dbconn->Execute("SELECT $column[hits] FROM $pntable[stats_hour]"); |
| 263 | | $hour = 0; $sumhour = 0; |
| 264 | | |
| 265 | | while (!$result->EOF) { |
| 266 | | $hourhitamount[$hour] = $result->fields[0]; |
| 267 | | if ($hour == 0) { |
| 268 | | $hourbesthits = $hourhitamount[$hour]; |
| 269 | | $hourbest = 0; |
| 270 | | $hourbadhits = $hourhitamount[$hour]; |
| 271 | | $hourbad = 0; |
| 272 | | } |
| 273 | | if ($hourhitamount[$hour] > $hourbesthits) { |
| 274 | | $hourbesthits = $hourhitamount[$hour]; |
| 275 | | $hourbest = $hour; |
| 276 | | } |
| 277 | | if ($hourhitamount[$hour] < $hourbadhits) { |
| 278 | | $hourbadhits = $hourhitamount[$hour]; |
| 279 | | $hourbad = $hour; |
| 280 | | } |
| 281 | | $sumhour += $hourhitamount[$hour]; |
| 282 | | $hour++; |
| 283 | | $result->MoveNext(); |
| 284 | | } |
| 285 | | $result->Close(); |
| 286 | | |
| 287 | | // get all rows from the db at once and go through the result |
| 288 | | $column = $pntable['stats_week_column']; |
| 289 | | $result = $dbconn->Execute("SELECT $column[hits] FROM $pntable[stats_week]"); |
| 290 | | $weekday = 0; $sumweek = 0; |
| 291 | | while (!$result->EOF) { |
| 292 | | $weekhitamount[$weekday] = $result->fields[0]; |
| 293 | | if ($weekday == 0) { |
| 294 | | $weekdaybesthits = $weekhitamount[$weekday]; |
| 295 | | $weekdaybest = 0; |
| 296 | | $weekdaybadhits = $weekhitamount[$weekday]; |
| 297 | | $weekdaybad = 0; |
| 298 | | } |
| 299 | | if ($weekhitamount[$weekday] > $weekdaybesthits) { |
| 300 | | $weekdaybesthits = $weekhitamount[$weekday]; |
| 301 | | $weekdaybest = $weekday; |
| 302 | | } |
| 303 | | if ($weekhitamount[$weekday] < $weekdaybadhits) { |
| 304 | | $weekdaybadhits = $weekhitamount[$weekday]; |
| 305 | | $weekdaybad = $weekday; |
| 306 | | } |
| 307 | | $sumweek += $weekhitamount[$weekday]; |
| 308 | | $weekday++; |
| 309 | | $result->MoveNext(); |
| 310 | | } |
| 311 | | $result->Close(); |
| 312 | | |
| 313 | | // get all rows from the db at once and go through the result |
| 314 | | $column = $pntable['stats_month_column']; |
| 315 | | $result = $dbconn->Execute("SELECT $column[hits] FROM $pntable[stats_month]"); |
| 316 | | $month = 1; $summon = 0; |
| 317 | | $monthbesthits = 0; |
| 318 | | $monthbadhits = 0; |
| 319 | | while (!$result->EOF) { |
| 320 | | $monthhitamount[$month] = $result->fields[0]; |
| 321 | | if ($monthhitamount[$month] > $monthbesthits) { |
| 322 | | $monthbesthits = $monthhitamount[$month]; |
| 323 | | $monthbest = $month; |
| 324 | | } |
| 325 | | if ($monthhitamount[$month] < $monthbadhits) { |
| 326 | | $monthbadhits = $monthhitamount[$month]; |
| 327 | | $monthbad = $month; |
| 328 | | } |
| 329 | | $summon += $monthhitamount[$month]; |
| 330 | | $month++; |
| 331 | | $result->MoveNext(); |
| 332 | | } |
| 333 | | $result->Close(); |
| 334 | | /************************ End Main stats collections *************************************/ |
| 335 | | |
| 336 | | $byhour = array(); |
| 337 | | if (pnModGetVar('stats', 'twentyfourhour')) { |
| 338 | | for ($hour = 0; $hour < 24; $hour++) { |
| 339 | | $label = DataUtil::formatForDisplay($hour).':00 - '.DataUtil::formatForDisplay($hour).':59'; |
| 340 | | $byhour[] = array('label' => $label, 'percent' => round((100*$hourhitamount[$hour])/$hourbesthits,0), 'hits' => $hourhitamount[$hour]); |
| 341 | | } |
| 342 | | } else { |
| 343 | | for ($hour = 1; $hour < 24; $hour++) { |
| 344 | | if ($hourbesthits == 0) $hourbesthits = 1; |
| 345 | | if ($hour < 13) $label = DataUtil::formatForDisplay($hour).' am'; |
| 346 | | else $label = DataUtil::formatForDisplay($hour % 12).' pm'; |
| 347 | | $byhour[] = array('label' => $label, 'percent' => round((100*$hourhitamount[$hour])/$hourbesthits,0), 'hits' => $hourhitamount[$hour]); |
| 348 | | } |
| 349 | | if ($sumhour == 0) $sumhour = 1; |
| 350 | | $percent = round((100*$hourhitamount[0])/$sumhour,2); |
| 351 | | $byhour[] = array('label' => '12pm:', 'percent' => round((100*$hourhitamount[0])/$hourbesthits,0), 'hits' => $hourhitamount[0]); |
| 352 | | } |
| 353 | | |
| 354 | | $byweek = array(); |
| 355 | | if ($weekdaybesthits == 0) $weekdaybesthits = 1; |
| 356 | | $byweek[0] = array('percent' => round((100*$weekhitamount[0])/$weekdaybesthits,0), 'hits' => $weekhitamount[0]); |
| 357 | | for ($weekday=1; $weekday<=6; $weekday++) { |
| 358 | | if ($weekdaybesthits == 0) $weekdaybesthits = 1; |
| 359 | | $byweek[] = array('percent' => round((100*$weekhitamount[$weekday])/$weekdaybesthits,0), 'hits' => $weekhitamount[$weekday]); |
| 360 | | } |
| 361 | | |
| 362 | | $bymonth = array(); |
| 363 | | for ($month=1; $month<=12; $month++){ |
| 364 | | if ($monthbesthits == 0) $monthbesthits = 1; |
| 365 | | $bymonth[] = array('percent' => round((100*$monthhitamount[$month])/$monthbesthits,0), 'hits' => $monthhitamount[$month]); |
| 366 | | } |
| 367 | | |
| 368 | | //form the results set |
| 369 | | $mainstats = array( 'valtoday' => $valtoday, 'valyesday' => $valyesday, |
| 370 | | 'best_day' => $best_day, 'best_day_hits' => $best_day_hits, |
| 371 | | 'worst_day' => $worst_day, 'worst_day_hits' => $worst_day_hits, |
| 372 | | 'weekdaynames' => $weekdaynames, 'weekdaybest' => $weekdaybest, 'weekdaybesthits' => $weekdaybesthits, |
| 373 | | 'weekdaybad' => $weekdaybad, 'weekdaybadhits' => $weekdaybadhits, |
| 374 | | 'hourbest' => $hourbest, 'hourbesthits' => $hourbesthits, |
| 375 | | 'hourbad' => $hourbad, 'hourbadhits' => $hourbadhits, |
| 376 | | 'byweek' => $byweek, 'byhour' => $byhour, 'bymonth' => $bymonth); |
| 377 | | return $mainstats; |
| | 206 | // Security check |
| | 207 | if (!SecurityUtil::checkPermission('Stats::', '::', ACCESS_READ)) { |
| | 208 | return LogUtil::registerError (_MODULENOAUTH); |
| | 209 | } |
| | 210 | |
| | 211 | $dbconn = pnDBGetConn(true); |
| | 212 | $pntable = pnDBGetTables(); |
| | 213 | |
| | 214 | /************************ Begin Main stats collections *************************************/ |
| | 215 | // Stats Hour - Day Stats |
| | 216 | for ($i = 1; $i <= 12; $i++) { |
| | 217 | $monthnames[] = gmstrftime('%B', gmmktime(0,0,0,$i,1,'1970')); |
| | 218 | } |
| | 219 | // loop counter starts as 4 since 4th Jan 1970 is a sunday; the first day of the week |
| | 220 | for ($i = 4; $i <= 11; $i++) { |
| | 221 | $weekdaynames[] = gmstrftime('%A', gmmktime(0,0,0,1,$i,'1970')); |
| | 222 | } |
| | 223 | |
| | 224 | $toddate = date("dmY"); |
| | 225 | // 24hours ago = yesterday |
| | 226 | $yesdate = date("dmY",time()-(60*60*24)); |
| | 227 | |
| | 228 | $column = $pntable['stats_date_column']; |
| | 229 | $toddb = $dbconn->Execute("SELECT $column[hits] as hits FROM $pntable[stats_date] WHERE $column[date]='".DataUtil::formatForStore($toddate)."'"); |
| | 230 | if (!$toddb->EOF) { |
| | 231 | list($valtoday)=$toddb->fields; |
| | 232 | } else { |
| | 233 | $valtoday=0; |
| | 234 | } |
| | 235 | |
| | 236 | $column = $pntable['stats_date_column']; |
| | 237 | $yesdb = $dbconn->Execute("SELECT $column[hits] as hits FROM $pntable[stats_date] WHERE $column[date]='".DataUtil::formatForStore($yesdate)."'"); |
| | 238 | if (!$yesdb->EOF) { |
| | 239 | list($valyesday)=$yesdb->fields; |
| | 240 | } else { |
| | 241 | $valyesday=0; |
| | 242 | } |
| | 243 | |
| | 244 | // Fetch some more infos about best and worst day ever |
| | 245 | $column = $pntable['stats_date_column']; |
| | 246 | $query = "SELECT $column[date], $column[hits] |
| | 247 | FROM $pntable[stats_date] |
| | 248 | ORDER BY $column[hits] DESC"; |
| | 249 | $dbr = $dbconn->SelectLimit("$query,1"); |
| | 250 | list ($best_day_date, $best_day_hits) = $dbr->fields; |
| | 251 | |
| | 252 | $best_day = mktime(0, 0, 0, substr($best_day_date, 2, 2), substr($best_day_date, 0, 2), substr($best_day_date, 4, 4)); |
| | 253 | $query = "SELECT $column[date], $column[hits] |
| | 254 | FROM $pntable[stats_date] |
| | 255 | ORDER BY $column[hits] ASC"; |
| | 256 | $dbr = $dbconn->SelectLimit("$query,1"); |
| | 257 | list ($worst_day_date, $worst_day_hits) = $dbr->fields; |
| | 258 | $worst_day = mktime(0, 0, 0, substr($worst_day_date, 2, 2), substr($worst_day_date, 0, 2), substr($worst_day_date, 4, 4)); |
| | 259 | |
| | 260 | // get all rows from the db at once and go through the result |
| | 261 | $column = $pntable['stats_hour_column']; |
| | 262 | $result = $dbconn->Execute("SELECT $column[hits] FROM $pntable[stats_hour]"); |
| | 263 | $hour = 0; $sumhour = 0; |
| | 264 | |
| | 265 | while (!$result->EOF) { |
| | 266 | $hourhitamount[$hour] = $result->fields[0]; |
| | 267 | if ($hour == 0) { |
| | 268 | $hourbesthits = $hourhitamount[$hour]; |
| | 269 | $hourbest = 0; |
| | 270 | $hourbadhits = $hourhitamount[$hour]; |
| | 271 | $hourbad = 0; |
| | 272 | } |
| | 273 | if ($hourhitamount[$hour] > $hourbesthits) { |
| | 274 | $hourbesthits = $hourhitamount[$hour]; |
| | 275 | $hourbest = $hour; |
| | 276 | } |
| | 277 | if ($hourhitamount[$hour] < $hourbadhits) { |
| | 278 | $hourbadhits = $hourhitamount[$hour]; |
| | 279 | $hourbad = $hour; |
| | 280 | } |
| | 281 | $sumhour += $hourhitamount[$hour]; |
| | 282 | $hour++; |
| | 283 | $result->MoveNext(); |
| | 284 | } |
| | 285 | |