Permalink
Browse files

Need for Speed;

Online Users are now cached + options to decide how long.
Removed redundant queries.

Additions;
Debug may now be enabled from admin options.
If you can't get to admin options, you may delete the debug.lock file in
the cache directory to exit debug mode.
  • Loading branch information...
1 parent 10129e3 commit 4bd5bcd7f6841ce40d17c971a124f9c016842d3e @WisdomPanda committed Nov 9, 2011
Showing with 252 additions and 57 deletions.
  1. +32 −1 admin_options.php
  2. +4 −0 footer.php
  3. +47 −0 include/cache.php
  4. +18 −3 include/common.php
  5. +62 −12 include/functions.php
  6. +49 −9 include/request.php
  7. +22 −27 index.php
  8. +10 −3 lang/English/admin_options.php
  9. +3 −0 lang/English/common.php
  10. +5 −2 viewtopic.php
View
@@ -89,6 +89,8 @@
'maintenance_message' => pun_trim($_POST['form']['maintenance_message']),
'use_topic_stamp' => intval($_POST['form']['use_topic_stamp']),
'regen_token' => intval($_POST['form']['regen_token']),
+ 'enable_debug' => intval($_POST['form']['enable_debug']),
+ 'users_online_refresh' => intval($_POST['form']['users_online_refresh']),
);
if ($form['board_title'] == '')
message($lang_admin_options['Must enter title message']);
@@ -110,7 +112,22 @@
if ($form['date_format'] == '')
$form['date_format'] = 'Y-m-d';
-
+
+ //Try and sort out the debug file situation.
+ if (!file_exists(FORUM_CACHE_DIR.'debug.lock') && $form['enable_debug'] == '1') {
+ if (!$file = fopen(FORUM_CACHE_DIR.'debug.lock', 'x')) {
+ message($lang_admin_options['enable_debug_make_error']);
+ } //End if.
+
+ fwrite($file, '1', 1);
+ fflush($file);
+ fclose($file);
+
+ } else if (file_exists(FORUM_CACHE_DIR.'debug.lock') && $form['enable_debug'] != '1') {
+ if (!@unlink(FORUM_CACHE_DIR.'debug.lock')) {
+ message($lang_admin_options['enable_debug_delete_error']);
+ } //End if.
+ } //End if.
require PUN_ROOT.'include/email.php';
@@ -426,6 +443,13 @@
<span><?php echo $lang_admin_options['o_regen_token_info'] ?></span>
</td>
</tr>
+ <tr>
+ <th scope="row"><?php echo $lang_admin_options['users_online_refresh'] ?></th>
+ <td>
+ <input type="text" name="form[users_online_refresh]" size="3" maxlength="4" value="<?php echo $pun_config['o_users_online_refresh'] ?>" />
+ <span><?php echo $lang_admin_options['users_online_refresh_info'] ?></span>
+ </td>
+ </tr>
</table>
</div>
</fieldset>
@@ -862,6 +886,13 @@
<span><?php echo $lang_admin_options['Maintenance message help'] ?></span>
</td>
</tr>
+ <tr>
+ <th scope="row"><?php echo $lang_admin_options['enable_debug'] ?></th>
+ <td>
+ <input type="radio" name="form[enable_debug]" value="1"<?php if ($pun_config['o_enable_debug'] == '1') echo ' checked="checked"' ?> />&#160;<strong><?php echo $lang_admin_common['Yes'] ?></strong>&#160;&#160;&#160;<input type="radio" name="form[enable_debug]" value="0"<?php if ($pun_config['o_enable_debug'] != '1') echo ' checked="checked"' ?> />&#160;<strong><?php echo $lang_admin_common['No'] ?></strong>
+ <span><?php echo sprintf($lang_admin_options['enable_debug_info'], FORUM_CACHE_DIR.'debug.lock') ?></span>
+ </td>
+ </tr>
</table>
</div>
</fieldset>
View
@@ -162,6 +162,10 @@
// Display executed queries (if enabled)
if (defined('PUN_SHOW_QUERIES'))
display_saved_queries();
+
+if (defined('PUN_SHOW_REQUESTS')) {
+ display_saved_requests();
+} //End if.
$tpl_temp = trim(ob_get_contents());
$tpl_main = str_replace('<pun_footer>', $tpl_temp, $tpl_main);
View
@@ -36,6 +36,53 @@ function generate_config_cache()
@apc_delete_file(FORUM_CACHE_DIR.'cache_config.php');
}
+function generate_online_cache() {
+ global $db, $lang_index;
+
+ // Update online list
+ update_users_online();
+
+ $fh = @fopen(FORUM_CACHE_DIR.'cache_online.php', 'wb');
+ if (!$fh) {
+ error('Unable to online cache file to cache directory. Please make sure PHP has write access to the directory \''.pun_htmlspecialchars(FORUM_CACHE_DIR).'\'', __FILE__, __LINE__);
+ } //End if.
+
+ if (!file_put_contents(FORUM_CACHE_DIR.'online.stamp', time())) {
+ error('Unable to online cache file to cache directory. Please make sure PHP has write access to the directory \''.pun_htmlspecialchars(FORUM_CACHE_DIR).'\'', __FILE__, __LINE__);
+ } //End if.
+
+ fwrite($fh, '<?php'."\n\n".'define(\'PUN_ONLINE_LOADED\', 1);'."\n\n".'?>'."\n\n");
+ // Fetch users online info and generate strings for output
+ $num_guests = 0;
+ $users = array();
+ $result = $db->query('SELECT o.user_id, o.ident, o.idle,sc.user_id,sc.character_id,c.character_id,c.character_name,o.logged FROM '.$db->prefix.'online AS o,'.$db->prefix.'api_selected_char AS sc, '.$db->prefix.'api_characters AS c WHERE o.idle=0 AND sc.user_id=o.user_id AND sc.character_id=c.character_id ORDER BY o.ident', true) or error('Unable to fetch online list', __FILE__, __LINE__, $db->error());
+
+ while ($pun_user_online = $db->fetch_assoc($result))
+ {
+ if ($pun_user_online['user_id'] > 1)
+ {
+ if ($pun_user['g_view_users'] == '1')
+ $users[] = "\n\t\t\t\t".'<dd><a href="profile.php?id='.$pun_user_online['user_id'].'">'.pun_htmlspecialchars($pun_user_online['character_name']).'</a>';
+ else
+ $users[] = "\n\t\t\t\t".'<dd>'.pun_htmlspecialchars($pun_user_online['character_name']);
+ }
+ else
+ ++$num_guests;
+ }
+
+ $num_users = count($users);
+ fwrite($fh, "\t\t\t\t".'<dd><span>'.sprintf($lang_index['Users online'], '<strong>'.forum_number_format($num_users).'</strong>').'</span></dd>'."\n\t\t\t\t".'<dd><span>'.sprintf($lang_index['Guests online'], '<strong>'.forum_number_format($num_guests).'</strong>').'</span></dd>'."\n\t\t\t".'</dl>'."\n");
+
+
+ if ($num_users > 0)
+ fwrite($fh, "\t\t\t".'<dl id="onlinelist" class="clearb">'."\n\t\t\t\t".'<dt><strong>'.$lang_index['Online'].' </strong></dt>'."\t\t\t\t".implode(',</dd> ', $users).'</dd>'."\n\t\t\t".'</dl>'."\n");
+ else
+ fwrite($fh, "\t\t\t".'<div class="clearer"></div>'."\n");
+
+ fclose($fh);
+
+} //End generate_online_cache().
+
//
// Generate the bans cache PHP script
View
@@ -124,6 +124,24 @@ function stripslashes_array($array)
require FORUM_CACHE_DIR.'cache_config.php';
}
+//Debugging is now based out of the admin options.
+if ($pun_config['o_enable_debug'] == 1) {
+ //The lock file is made by the options, if it doesn't exist, someone has deleted it.
+ if (file_exists(FORUM_CACHE_DIR.'debug.lock')) {
+ define('PUN_DEBUG', 1);
+ } else {
+ //Turn it off fully, just to be sure.
+ $pun_config['o_enable_debug'] = 0;
+ $db->query('UPDATE '.$db->prefix.'config SET conf_value=0 WHERE conf_name=\'o_enable_debug\'');
+ if (!defined('FORUM_CACHE_FUNCTIONS_LOADED'))
+ require PUN_ROOT.'include/cache.php';
+ generate_config_cache();
+ } //End if - else.
+} //End if.
+
+//define('PUN_SHOW_QUERIES', 1);
+//define('PUN_SHOW_REQUESTS', 1);
+
//Set the session length.
//This is not something I'd like anyone tampering with unless they are aware of the kind of issues it could bring.
//Anyone comfortable with editing the session length should be OK with editing this file.
@@ -187,9 +205,6 @@ function stripslashes_array($array)
// Check if current user is banned
check_bans();
-// Update online list
-update_users_online();
-
// Check to see if we logged in without a cookie being set
if ($pun_user['is_guest'] && isset($_GET['login']))
message($lang_common['No cookie']);
View
@@ -6,9 +6,6 @@
* License: http://www.gnu.org/licenses/gpl.html GPL version 2 or higher
*/
-//Comment this out after.
-//define('PUN_DEBUG', 1);
-
//
// Return current timestamp (with microseconds) as a float
//
@@ -62,21 +59,14 @@ function check_cookie(&$pun_user)
} else {
$_SESSION['igb'] = false;
} //End if - else.
-
- //There is an easier way to do this, but PostgreSQL complains about it, so here is a messy way.
- //The general idea is to remove old sessions.
- $db->query("DELETE FROM ".$db->prefix."session WHERE (stamp<".$earlier." AND length=".$pun_config['session_length'].") OR stamp<".$much_earlier);
//Lets pull our session data out.
if (isset($_COOKIE[$cookie_name]) && preg_match('/^(\d+)\:([0-9a-zA-Z]{32}):([0-9a-fA-F]{32})$/', $_COOKIE[$cookie_name], $matches)) {
$cookie['user_id'] = intval($matches[1]);
$cookie['token'] = $matches[2];
$cookie['hash'] = $matches[3];
$cookie['remember'] = 1;
- } //End if.
-
- //The cookie is more long term, the session is for when they don't select "Remember Me"
- if (isset($_SESSION[$cookie_name]) && preg_match('/^(\d+)\:([0-9a-zA-Z]{32}):([0-9a-fA-F]{32})$/', $_SESSION[$cookie_name], $matches)) {
+ } else if (isset($_SESSION[$cookie_name]) && preg_match('/^(\d+)\:([0-9a-zA-Z]{32}):([0-9a-fA-F]{32})$/', $_SESSION[$cookie_name], $matches)) {
$cookie['user_id'] = intval($matches[1]);
$cookie['token'] = $matches[2];
$cookie['hash'] = $matches[3];
@@ -88,6 +78,7 @@ function check_cookie(&$pun_user)
if (defined('PUN_DEBUG')) {
error('Hash mismatch.', __FILE__, __LINE__, $db->error());
} //End if.
+ unset($_SESSION[$cookie_name]);
sleep(3);
set_default_user();
return; //The session has been messed with, abort!
@@ -132,6 +123,7 @@ function check_cookie(&$pun_user)
if (defined('PUN_DEBUG')) {
error('No user found.', __FILE__, __LINE__, $db->error());
} //End if.
+ unset($_SESSION[$cookie_name]);
set_default_user();
sleep(3);
return; //Chances are they have no active session.
@@ -148,6 +140,7 @@ function check_cookie(&$pun_user)
$expire = $now + 31536000; // The cookie expires after a year
forum_setcookie($cookie_name, pun_hash(uniqid(rand(), true)), $expire);
} //End if.
+ unset($_SESSION[$cookie_name]);
set_default_user();
sleep(3);
return; //Trying to access from an old session, or they are evil!
@@ -163,6 +156,7 @@ function check_cookie(&$pun_user)
$expire = $now + 31536000; // The cookie expires after a year
forum_setcookie($cookie_name, pun_hash(uniqid(rand(), true)), $expire);
} //End if.
+ unset($_SESSION[$cookie_name]);
set_default_user();
return;
} //End if.
@@ -1334,7 +1328,6 @@ function array_insert(&$input, $offset, $element, $key = null)
$input = array_merge(array_slice($input, 0, $offset), array($key => $element), array_slice($input, $offset));
}
-
//
// Display a message when board is in maintenance mode
//
@@ -1576,6 +1569,10 @@ function redirect($destination_url, $message, $link_back = true)
// Display executed queries (if enabled)
if (defined('PUN_SHOW_QUERIES'))
display_saved_queries();
+
+ if (defined('PUN_SHOW_REQUESTS')) {
+ display_saved_requests($pun_request);
+ } //End if.
$tpl_temp = trim(ob_get_contents());
$tpl_redir = str_replace('<pun_footer>', $tpl_temp, $tpl_redir);
@@ -2136,6 +2133,59 @@ function display_saved_queries()
}
+/**
+ * Display the requests made, if any.
+ */
+function display_saved_requests()
+{
+ global $pun_request, $lang_common;
+
+ // Get the queries so that we can print them out
+ $saved_queries = $pun_request->saved_requests;
+
+?>
+
+<div id="debug" class="blocktable">
+ <h2><span><?php echo $lang_common['Debug table'] ?></span></h2>
+ <div class="box">
+ <div class="inbox">
+ <table cellspacing="0">
+ <thead>
+ <tr>
+ <th class="tcl" scope="col"><?php echo $lang_common['Request times'] ?></th>
+ <th class="tcr" scope="col"><?php echo $lang_common['Request'] ?></th>
+ </tr>
+ </thead>
+ <tbody>
+<?php
+
+ $req_time_total = 0.0;
+ foreach ($saved_queries as $cur_req)
+ {
+ $req_time_total += $cur_req[1];
+
+?>
+ <tr>
+ <td class="tcl"><?php echo ($cur_req[1] != 0) ? $cur_req[1] : '&#160;' ?></td>
+ <td class="tcr"><?php echo pun_htmlspecialchars($cur_req[0]) ?></td>
+ </tr>
+<?php
+
+ }
+
+?>
+ <tr>
+ <td class="tcl" colspan="2"><?php printf($lang_common['Total request time'], $req_time_total.' s') ?></td>
+ </tr>
+ </tbody>
+ </table>
+ </div>
+ </div>
+</div>
+<?php
+
+} //End display_saved_requests().
+
//
// Dump contents of variable(s)
Oops, something went wrong.

0 comments on commit 4bd5bcd

Please sign in to comment.