Brian Waters | 13d9601 | 2017-12-08 16:53:31 -0600 | [diff] [blame] | 1 | <?php |
| 2 | |
| 3 | # The copyright of this file is the same as the freeDiameter project. Licence is BSD. |
| 4 | |
| 5 | # This file should no be called directly; |
| 6 | # instead it should be included from another script that sets its parameters as described below. |
| 7 | |
| 8 | ## $USERS |
| 9 | # An array of the user names to display; the empty array will display all users. |
| 10 | # This parameter MUST be set. |
| 11 | if (!isset($USERS)) |
| 12 | die('Do not call this file directly'); |
| 13 | |
| 14 | ## $START_TIME: |
| 15 | # If set, this restricts the displayed data to sessions starting after $START |
| 16 | |
| 17 | ## $END_TIME: |
| 18 | # If set, this restricts the displayed data to sessions starting before $END |
| 19 | |
| 20 | ## $LIMIT: |
| 21 | ## $LIMIT_OFFSET: |
| 22 | # If set, these limit the number of accounting records displayed (for pagination purpose) |
| 23 | |
| 24 | #------------------------------------------------------------------------------------------ |
| 25 | # DATABASE: |
| 26 | |
| 27 | /* The Connection String used to access that database: |
| 28 | Example: "host=localhost dbname=app_acct user=freediameter password=foo" */ |
| 29 | $CONNSTR=""; |
| 30 | |
| 31 | /* The name of the table containing the processed data (from process_records.php script) */ |
| 32 | $PROCESSED="processed"; |
| 33 | |
| 34 | #------------------------------------------------------------------------------------------ |
| 35 | |
| 36 | |
| 37 | /* Connect to the database */ |
| 38 | $dbconn = pg_connect($CONNSTR) |
| 39 | or die('Could not connect: ' . pg_last_error() . "\n"); |
| 40 | |
| 41 | /* Function to format download size (from php.net) */ |
| 42 | function human_readable( $size ) |
| 43 | { |
| 44 | $count = 0; |
| 45 | $format = array("B","KB","MB","GB","TB","PB","EB","ZB","YB"); |
| 46 | while(($size/1024)>1 && $count<8) |
| 47 | { |
| 48 | $size=$size/1024; |
| 49 | $count++; |
| 50 | } |
| 51 | if( $size >= 100 ) $decimals = 0; |
| 52 | elseif ($size >= 10 ) $decimals = 1; |
| 53 | else $decimals = 2; |
| 54 | $return = number_format($size,$decimals,'.',' ')." ".$format[$count]; |
| 55 | return $return; |
| 56 | } |
| 57 | |
| 58 | /* Build the SQL query */ |
| 59 | $sql = 'SELECT *, to_char(sess_start, \'YYYY-MM-DD HH24:MI:SS (TZ)\') as fmt_sess_start FROM "'.$PROCESSED.'"'; |
| 60 | $where=0; |
| 61 | if ($USERS) { |
| 62 | $USERS = array_map(pg_escape_bytea, $USERS); |
| 63 | $sql .= " WHERE user_name IN ('". join("', '", array_values($USERS))."') "; |
| 64 | $where = 1; |
| 65 | } |
| 66 | |
| 67 | if ($START_TIME) { |
| 68 | $START_TIME = pg_escape_string($START_TIME); |
| 69 | if ($where++) |
| 70 | $sql .= " AND "; |
| 71 | else |
| 72 | $sql .= " WHERE "; |
| 73 | $sql .= "sess_start >= '".$START_TIME."'"; |
| 74 | } |
| 75 | if ($END_TIME) { |
| 76 | $END_TIME = pg_escape_string($END_TIME); |
| 77 | if ($where++) |
| 78 | $sql .= " AND "; |
| 79 | else |
| 80 | $sql .= " WHERE "; |
| 81 | $sql .= "sess_start <= '".$END_TIME."'"; |
| 82 | } |
| 83 | |
| 84 | $sql .= " ORDER BY sess_start, sess_duration"; |
| 85 | |
| 86 | if ($LIMIT) |
| 87 | $sql .= " LIMIT $LIMIT"; |
| 88 | if ($LIMIT_OFFSET) |
| 89 | $sql .= " OFFSET $LIMIT_OFFSET"; |
| 90 | |
| 91 | /* Execute the query */ |
| 92 | $result = pg_query($dbconn, $sql) or die('Query failed: ' . pg_last_error() . "\n"); |
| 93 | $recs = pg_num_rows($result); |
| 94 | if ($recs == 0) { |
| 95 | echo "<p><em>Sorry, no data is available in this selection.</em></p>\n"; |
| 96 | } else { |
| 97 | echo "<p><strong>$recs</strong> records found.</p>\n"; |
| 98 | ?> |
| 99 | <table> |
| 100 | <tr> |
| 101 | <th>Device identifier</th> |
| 102 | <th>Access Device information</th> |
| 103 | <th>Session started on</th> |
| 104 | <th>Duration</th> |
| 105 | <th>Downloaded</th> |
| 106 | <th>Uploaded</th> |
| 107 | </tr> |
| 108 | <?php |
| 109 | while ($record = pg_fetch_array($result, null, PGSQL_ASSOC)) { |
| 110 | echo " <tr title='".htmlentities(pg_unescape_bytea($record["user_name"]))."'>\n"; |
| 111 | echo " <td>"; |
| 112 | echo htmlentities(pg_unescape_bytea($record["user_device"])); |
| 113 | echo "</td>\n"; |
| 114 | echo " <td>"; |
| 115 | echo htmlentities(pg_unescape_bytea($record["nas_info"])); |
| 116 | echo "</td>\n"; |
| 117 | echo " <td>"; |
| 118 | echo $record["fmt_sess_start"]; |
| 119 | echo "</td>\n"; |
| 120 | echo " <td>"; |
| 121 | echo htmlentities($record["sess_duration"]); |
| 122 | echo "</td>\n"; |
| 123 | echo " <td>"; |
| 124 | echo human_readable( $record["downl_bytes"] )." (".$record["downl_packets"]."pckts)"; |
| 125 | echo "</td>\n"; |
| 126 | echo " <td>"; |
| 127 | echo human_readable( $record["upl_bytes"] )." (".$record["upl_packets"]."pckts)"; |
| 128 | echo "</td>\n"; |
| 129 | echo " </tr>\n"; |
| 130 | |
| 131 | } |
| 132 | } |
| 133 | pg_free_result($result); |
| 134 | |
| 135 | |
| 136 | /* Closing connection */ |
| 137 | pg_close($dbconn); |
| 138 | |
| 139 | |
| 140 | |
| 141 | ?> |