| Conditions | 50 |
| Paths | > 20000 |
| Total Lines | 299 |
| Lines | 16 |
| Ratio | 5.35 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 28 | function ViewErrorLog() |
||
| 29 | { |
||
| 30 | global $scripturl, $txt, $context, $modSettings, $user_profile, $filter, $smcFunc; |
||
| 31 | |||
| 32 | // Viewing contents of a file? |
||
| 33 | if (isset($_GET['file'])) |
||
| 34 | return ViewFile(); |
||
| 35 | |||
| 36 | // Viewing contents of a backtrace? |
||
| 37 | if (isset($_GET['backtrace'])) |
||
| 38 | return ViewBacktrace(); |
||
| 39 | |||
| 40 | // Check for the administrative permission to do this. |
||
| 41 | isAllowedTo('admin_forum'); |
||
| 42 | |||
| 43 | // Templates, etc... |
||
| 44 | loadLanguage('ManageMaintenance'); |
||
| 45 | loadTemplate('Errors'); |
||
| 46 | |||
| 47 | // You can filter by any of the following columns: |
||
| 48 | $filters = array( |
||
| 49 | 'id_member' => array( |
||
| 50 | 'txt' => $txt['username'], |
||
| 51 | 'operator' => '=', |
||
| 52 | 'datatype' => 'int', |
||
| 53 | ), |
||
| 54 | 'ip' => array( |
||
| 55 | 'txt' => $txt['ip_address'], |
||
| 56 | 'operator' => '=', |
||
| 57 | 'datatype' => 'inet', |
||
| 58 | ), |
||
| 59 | 'session' => array( |
||
| 60 | 'txt' => $txt['session'], |
||
| 61 | 'operator' => 'LIKE', |
||
| 62 | 'datatype' => 'string', |
||
| 63 | ), |
||
| 64 | 'url' => array( |
||
| 65 | 'txt' => $txt['error_url'], |
||
| 66 | 'operator' => 'LIKE', |
||
| 67 | 'datatype' => 'string', |
||
| 68 | ), |
||
| 69 | 'message' => array( |
||
| 70 | 'txt' => $txt['error_message'], |
||
| 71 | 'operator' => 'LIKE', |
||
| 72 | 'datatype' => 'string', |
||
| 73 | ), |
||
| 74 | 'error_type' => array( |
||
| 75 | 'txt' => $txt['error_type'], |
||
| 76 | 'operator' => 'LIKE', |
||
| 77 | 'datatype' => 'string', |
||
| 78 | ), |
||
| 79 | 'file' => array( |
||
| 80 | 'txt' => $txt['file'], |
||
| 81 | 'operator' => 'LIKE', |
||
| 82 | 'datatype' => 'string', |
||
| 83 | ), |
||
| 84 | 'line' => array( |
||
| 85 | 'txt' => $txt['line'], |
||
| 86 | 'operator' => '=', |
||
| 87 | 'datatype' => 'int', |
||
| 88 | ), |
||
| 89 | ); |
||
| 90 | |||
| 91 | // Set up the filtering... |
||
| 92 | if (isset($_GET['value'], $_GET['filter']) && isset($filters[$_GET['filter']])) |
||
| 93 | $filter = array( |
||
| 94 | 'variable' => $_GET['filter'], |
||
| 95 | 'value' => array( |
||
| 96 | 'sql' => in_array($_GET['filter'], array('message', 'url', 'file')) ? base64_decode(strtr($_GET['value'], array(' ' => '+'))) : $smcFunc['db_escape_wildcard_string']($_GET['value']), |
||
| 97 | ), |
||
| 98 | 'href' => ';filter=' . $_GET['filter'] . ';value=' . $_GET['value'], |
||
| 99 | 'entity' => $filters[$_GET['filter']]['txt'] |
||
| 100 | ); |
||
| 101 | |||
| 102 | // Deleting, are we? |
||
| 103 | if (isset($_POST['delall']) || isset($_POST['delete'])) |
||
| 104 | deleteErrors(); |
||
| 105 | |||
| 106 | // Just how many errors are there? |
||
| 107 | $result = $smcFunc['db_query']('', ' |
||
| 108 | SELECT COUNT(*) |
||
| 109 | FROM {db_prefix}log_errors' . (isset($filter) ? ' |
||
| 110 | WHERE ' . $filter['variable'] . ' ' . $filters[$_GET['filter']]['operator'] . ' {' . $filters[$_GET['filter']]['datatype'] . ':filter}' : ''), |
||
| 111 | array( |
||
| 112 | 'filter' => isset($filter) ? $filter['value']['sql'] : '', |
||
| 113 | ) |
||
| 114 | ); |
||
| 115 | list ($num_errors) = $smcFunc['db_fetch_row']($result); |
||
| 116 | $smcFunc['db_free_result']($result); |
||
| 117 | |||
| 118 | // If this filter is empty... |
||
| 119 | if ($num_errors == 0 && isset($filter)) |
||
| 120 | redirectexit('action=admin;area=logs;sa=errorlog' . (isset($_REQUEST['desc']) ? ';desc' : '')); |
||
| 121 | |||
| 122 | // Clean up start. |
||
| 123 | if (!isset($_GET['start']) || $_GET['start'] < 0) |
||
| 124 | $_GET['start'] = 0; |
||
| 125 | |||
| 126 | // Do we want to reverse error listing? |
||
| 127 | $context['sort_direction'] = isset($_REQUEST['desc']) ? 'down' : 'up'; |
||
| 128 | |||
| 129 | // Set the page listing up. |
||
| 130 | $context['page_index'] = constructPageIndex($scripturl . '?action=admin;area=logs;sa=errorlog' . ($context['sort_direction'] == 'down' ? ';desc' : '') . (isset($filter) ? $filter['href'] : ''), $_GET['start'], $num_errors, $modSettings['defaultMaxListItems']); |
||
| 131 | $context['start'] = $_GET['start']; |
||
| 132 | |||
| 133 | // Update the error count |
||
| 134 | if (!isset($filter)) |
||
| 135 | $context['num_errors'] = $num_errors; |
||
| 136 | else |
||
| 137 | { |
||
| 138 | // We want all errors, not just the number of filtered messages... |
||
| 139 | $query = $smcFunc['db_query']('', ' |
||
| 140 | SELECT COUNT(id_error) |
||
| 141 | FROM {db_prefix}log_errors', |
||
| 142 | array() |
||
| 143 | ); |
||
| 144 | |||
| 145 | list($context['num_errors']) = $smcFunc['db_fetch_row']($query); |
||
| 146 | $smcFunc['db_free_result']($query); |
||
| 147 | } |
||
| 148 | |||
| 149 | // Find and sort out the errors. |
||
| 150 | $request = $smcFunc['db_query']('', ' |
||
| 151 | SELECT id_error, id_member, ip, url, log_time, message, session, error_type, file, line |
||
| 152 | FROM {db_prefix}log_errors' . (isset($filter) ? ' |
||
| 153 | WHERE ' . $filter['variable'] . ' ' . $filters[$_GET['filter']]['operator'] . ' {' . $filters[$_GET['filter']]['datatype'] . ':filter}' : '') . ' |
||
| 154 | ORDER BY id_error ' . ($context['sort_direction'] == 'down' ? 'DESC' : '') . ' |
||
| 155 | LIMIT {int:start}, {int:max}', |
||
| 156 | array( |
||
| 157 | 'filter' => isset($filter) ? $filter['value']['sql'] : '', |
||
| 158 | 'start' => $_GET['start'], |
||
| 159 | 'max' => $modSettings['defaultMaxListItems'], |
||
| 160 | ) |
||
| 161 | ); |
||
| 162 | $context['errors'] = array(); |
||
| 163 | $members = array(); |
||
| 164 | |||
| 165 | for ($i = 0; $row = $smcFunc['db_fetch_assoc']($request); $i++) |
||
| 166 | { |
||
| 167 | $search_message = preg_replace('~<span class="remove">(.+?)</span>~', '%', $smcFunc['db_escape_wildcard_string']($row['message'])); |
||
| 168 | if ($search_message == $filter['value']['sql']) |
||
| 169 | $search_message = $smcFunc['db_escape_wildcard_string']($row['message']); |
||
| 170 | $show_message = strtr(strtr(preg_replace('~<span class="remove">(.+?)</span>~', '$1', $row['message']), array("\r" => '', '<br>' => "\n", '<' => '<', '>' => '>', '"' => '"')), array("\n" => '<br>')); |
||
| 171 | |||
| 172 | $context['errors'][$row['id_error']] = array( |
||
| 173 | 'member' => array( |
||
| 174 | 'id' => $row['id_member'], |
||
| 175 | 'ip' => inet_dtop($row['ip']), |
||
| 176 | 'session' => $row['session'] |
||
| 177 | ), |
||
| 178 | 'time' => timeformat($row['log_time']), |
||
| 179 | 'timestamp' => $row['log_time'], |
||
| 180 | 'url' => array( |
||
| 181 | 'html' => $smcFunc['htmlspecialchars'](strpos($row['url'], 'cron.php') === false ? (substr($row['url'], 0, 1) == '?' ? $scripturl : '') . $row['url'] : $row['url']), |
||
| 182 | 'href' => base64_encode($smcFunc['db_escape_wildcard_string']($row['url'])) |
||
| 183 | ), |
||
| 184 | 'message' => array( |
||
| 185 | 'html' => $show_message, |
||
| 186 | 'href' => base64_encode($search_message) |
||
| 187 | ), |
||
| 188 | 'id' => $row['id_error'], |
||
| 189 | 'error_type' => array( |
||
| 190 | 'type' => $row['error_type'], |
||
| 191 | 'name' => isset($txt['errortype_' . $row['error_type']]) ? $txt['errortype_' . $row['error_type']] : $row['error_type'], |
||
| 192 | ), |
||
| 193 | 'file' => array(), |
||
| 194 | ); |
||
| 195 | if (!empty($row['file']) && !empty($row['line'])) |
||
| 196 | { |
||
| 197 | // Eval'd files rarely point to the right location and cause havoc for linking, so don't link them. |
||
| 198 | $linkfile = strpos($row['file'], 'eval') === false || strpos($row['file'], '?') === false; // De Morgan's Law. Want this true unless both are present. |
||
| 199 | |||
| 200 | $context['errors'][$row['id_error']]['file'] = array( |
||
| 201 | 'file' => $row['file'], |
||
| 202 | 'line' => $row['line'], |
||
| 203 | 'href' => $scripturl . '?action=admin;area=logs;sa=errorlog;file=' . base64_encode($row['file']) . ';line=' . $row['line'], |
||
| 204 | 'link' => $linkfile ? '<a href="' . $scripturl . '?action=admin;area=logs;sa=errorlog;file=' . base64_encode($row['file']) . ';line=' . $row['line'] . '" onclick="return reqWin(this.href, 600, 480, false);">' . $row['file'] . '</a>' : $row['file'], |
||
| 205 | 'search' => base64_encode($row['file']), |
||
| 206 | ); |
||
| 207 | } |
||
| 208 | |||
| 209 | // Make a list of members to load later. |
||
| 210 | $members[$row['id_member']] = $row['id_member']; |
||
| 211 | } |
||
| 212 | $smcFunc['db_free_result']($request); |
||
| 213 | |||
| 214 | // Load the member data. |
||
| 215 | if (!empty($members)) |
||
| 216 | { |
||
| 217 | // Get some additional member info... |
||
| 218 | $request = $smcFunc['db_query']('', ' |
||
| 219 | SELECT id_member, member_name, real_name |
||
| 220 | FROM {db_prefix}members |
||
| 221 | WHERE id_member IN ({array_int:member_list}) |
||
| 222 | LIMIT {int:members}', |
||
| 223 | array( |
||
| 224 | 'member_list' => $members, |
||
| 225 | 'members' => count($members), |
||
| 226 | ) |
||
| 227 | ); |
||
| 228 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
| 229 | $members[$row['id_member']] = $row; |
||
| 230 | $smcFunc['db_free_result']($request); |
||
| 231 | |||
| 232 | // This is a guest... |
||
| 233 | $members[0] = array( |
||
| 234 | 'id_member' => 0, |
||
| 235 | 'member_name' => '', |
||
| 236 | 'real_name' => $txt['guest_title'] |
||
| 237 | ); |
||
| 238 | |||
| 239 | // Go through each error and tack the data on. |
||
| 240 | foreach ($context['errors'] as $id => $dummy) |
||
| 241 | { |
||
| 242 | $memID = $context['errors'][$id]['member']['id']; |
||
| 243 | $context['errors'][$id]['member']['username'] = $members[$memID]['member_name']; |
||
| 244 | $context['errors'][$id]['member']['name'] = $members[$memID]['real_name']; |
||
| 245 | $context['errors'][$id]['member']['href'] = empty($memID) ? '' : $scripturl . '?action=profile;u=' . $memID; |
||
| 246 | $context['errors'][$id]['member']['link'] = empty($memID) ? $txt['guest_title'] : '<a href="' . $scripturl . '?action=profile;u=' . $memID . '">' . $context['errors'][$id]['member']['name'] . '</a>'; |
||
| 247 | } |
||
| 248 | } |
||
| 249 | |||
| 250 | // Filtering anything? |
||
| 251 | if (isset($filter)) |
||
| 252 | { |
||
| 253 | $context['filter'] = &$filter; |
||
| 254 | |||
| 255 | // Set the filtering context. |
||
| 256 | if ($filter['variable'] == 'id_member') |
||
| 257 | { |
||
| 258 | $id = $filter['value']['sql']; |
||
| 259 | loadMemberData($id, false, 'minimal'); |
||
| 260 | $context['filter']['value']['html'] = '<a href="' . $scripturl . '?action=profile;u=' . $id . '">' . $user_profile[$id]['real_name'] . '</a>'; |
||
| 261 | } |
||
| 262 | elseif ($filter['variable'] == 'url') |
||
| 263 | $context['filter']['value']['html'] = '\'' . strtr($smcFunc['htmlspecialchars']((substr($filter['value']['sql'], 0, 1) == '?' ? $scripturl : '') . $filter['value']['sql']), array('\_' => '_')) . '\''; |
||
| 264 | elseif ($filter['variable'] == 'message') |
||
| 265 | { |
||
| 266 | $context['filter']['value']['html'] = '\'' . strtr($smcFunc['htmlspecialchars']($filter['value']['sql']), array("\n" => '<br>', '<br />' => '<br>', "\t" => ' ', '\_' => '_', '\\%' => '%', '\\\\' => '\\')) . '\''; |
||
| 267 | $context['filter']['value']['html'] = preg_replace('~&lt;span class=&quot;remove&quot;&gt;(.+?)&lt;/span&gt;~', '$1', $context['filter']['value']['html']); |
||
| 268 | } |
||
| 269 | elseif ($filter['variable'] == 'error_type') |
||
| 270 | { |
||
| 271 | $context['filter']['value']['html'] = '\'' . strtr($smcFunc['htmlspecialchars']($filter['value']['sql']), array("\n" => '<br>', '<br />' => '<br>', "\t" => ' ', '\_' => '_', '\\%' => '%', '\\\\' => '\\')) . '\''; |
||
| 272 | } |
||
| 273 | else |
||
| 274 | $context['filter']['value']['html'] = &$filter['value']['sql']; |
||
| 275 | } |
||
| 276 | |||
| 277 | $context['error_types'] = array(); |
||
| 278 | |||
| 279 | $context['error_types']['all'] = array( |
||
| 280 | 'label' => $txt['errortype_all'], |
||
| 281 | 'description' => isset($txt['errortype_all_desc']) ? $txt['errortype_all_desc'] : '', |
||
| 282 | 'url' => $scripturl . '?action=admin;area=logs;sa=errorlog' . ($context['sort_direction'] == 'down' ? ';desc' : ''), |
||
| 283 | 'is_selected' => empty($filter), |
||
| 284 | ); |
||
| 285 | |||
| 286 | $sum = 0; |
||
| 287 | // What type of errors do we have and how many do we have? |
||
| 288 | $request = $smcFunc['db_query']('', ' |
||
| 289 | SELECT error_type, COUNT(*) AS num_errors |
||
| 290 | FROM {db_prefix}log_errors |
||
| 291 | GROUP BY error_type |
||
| 292 | ORDER BY error_type = {string:critical_type} DESC, error_type ASC', |
||
| 293 | array( |
||
| 294 | 'critical_type' => 'critical', |
||
| 295 | ) |
||
| 296 | ); |
||
| 297 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
| 298 | { |
||
| 299 | // Total errors so far? |
||
| 300 | $sum += $row['num_errors']; |
||
| 301 | |||
| 302 | $context['error_types'][$sum] = array( |
||
| 303 | 'label' => (isset($txt['errortype_' . $row['error_type']]) ? $txt['errortype_' . $row['error_type']] : $row['error_type']) . ' (' . $row['num_errors'] . ')', |
||
| 304 | 'description' => isset($txt['errortype_' . $row['error_type'] . '_desc']) ? $txt['errortype_' . $row['error_type'] . '_desc'] : '', |
||
| 305 | 'url' => $scripturl . '?action=admin;area=logs;sa=errorlog' . ($context['sort_direction'] == 'down' ? ';desc' : '') . ';filter=error_type;value=' . $row['error_type'], |
||
| 306 | 'is_selected' => isset($filter) && $filter['value']['sql'] == $smcFunc['db_escape_wildcard_string']($row['error_type']), |
||
| 307 | ); |
||
| 308 | } |
||
| 309 | $smcFunc['db_free_result']($request); |
||
| 310 | |||
| 311 | // Update the all errors tab with the total number of errors |
||
| 312 | $context['error_types']['all']['label'] .= ' (' . $sum . ')'; |
||
| 313 | |||
| 314 | // Finally, work out what is the last tab! |
||
| 315 | if (isset($context['error_types'][$sum])) |
||
| 316 | $context['error_types'][$sum]['is_last'] = true; |
||
| 317 | else |
||
| 318 | $context['error_types']['all']['is_last'] = true; |
||
| 319 | |||
| 320 | // And this is pretty basic ;). |
||
| 321 | $context['page_title'] = $txt['errlog']; |
||
| 322 | $context['has_filter'] = isset($filter); |
||
| 323 | $context['sub_template'] = 'error_log'; |
||
| 324 | |||
| 325 | createToken('admin-el'); |
||
| 326 | } |
||
| 327 | |||
| 469 | ?> |