Skip to content

Commit

Permalink
Fixing testswarm/ISSUE-51 and testswarm/ISSUE-61, moving getItem() to…
Browse files Browse the repository at this point in the history
… utilities, and fixing some E_NOTICE's

* Removing global $contextpath variable
* Creating utility function instead
* Replacing all usages
* Replacing var_dump(, true) with print_r(, true). var_dump doesn't have a "return" argument, only print_r does. Previously this would case the var to be dumped first (ie. echoed) and then the rest. print_r is what you want here.
  • Loading branch information
Krinkle authored and Krinkle committed May 13, 2011
1 parent ae11668 commit 003061c
Show file tree
Hide file tree
Showing 19 changed files with 67 additions and 52 deletions.
3 changes: 2 additions & 1 deletion config/config-sample.ini
Expand Up @@ -6,5 +6,6 @@
password = "root"
database = "testswarm"
[web]
; set to /testswarm to run a local copy in a non-root directory, also update .htaccess with RewriteBase /testswarm/
; set to /testswarm/ to run a local copy in a non-root directory, also update .htaccess with RewriteBase /testswarm/
; include trailing slash!
contextpath = ""
2 changes: 1 addition & 1 deletion content/addjob.php
@@ -1,4 +1,4 @@
<form action="/" method="POST">
<form action="<?php echo swarmpath( '/' ); ?>" method="POST">
<input type="hidden" name="client_id" value="<?php echo $client_id; ?>"/>
<input type="hidden" name="state" value="addjob"/>
<fieldset>
Expand Down
8 changes: 4 additions & 4 deletions content/home.php
Expand Up @@ -17,11 +17,11 @@
$user = $row[0];
$total = $row[1];

echo "<tr><td class='num'>$num</td><td><a href='" . $GLOBALS['contextpath'] . "/user/$user/'>$user</a></td><td class='num'>$total</td></tr>";
echo "<tr><td class='num'>$num</td><td><a href='" . swarmpath( "user/$user/" ) . "'>$user</a></td><td class='num'>$total</td></tr>";
$num++;
}

echo "</table><p class='right'><a href='" . $GLOBALS['contextpath'] . "/scores/'>All Scores...</a></p><h3>Rarest Browsers</h3><table class='scores'>";
echo "</table><p class='right'><a href='" . swarmpath( 'scores/' ) . "'>All Scores...</a></p><h3>Rarest Browsers</h3><table class='scores'>";

$result = mysql_queryf("SELECT name, SUM(runs) as allruns FROM run_useragent, useragents WHERE run_useragent.useragent_id=useragents.id GROUP BY name ORDER BY allruns LIMIT 10;");

Expand Down Expand Up @@ -59,7 +59,7 @@ function loadBrowsers($name, $mobile) {
$num = preg_replace('/\w+ /', "", $row[1]);
?>
<div class="browser<?php echo $row[0] != $engine ? ' clear' : '';?><?php echo $row[3] ? ' you' : '';?>">
<img src="<?php echo $GLOBALS['contextpath']; ?>/images/<?php echo $row[0]; ?>.sm.png" class="browser-icon <?php echo $row[0]; ?>" alt="<?php echo $row[1]; ?>" title="<?php echo $row[1]; ?>"/>
<img src="<?php echo swarmpath( "images/{$row[0]}.sm.png" ); ?>" class="browser-icon <?php echo $row[0]; ?>" alt="<?php echo $row[1]; ?>" title="<?php echo $row[1]; ?>"/>
<span class="browser-name"><?php echo $num; ?></span>
<?php if ( intval($row[2]) > 0 ) {
echo "<span class='active'>", $row[2], "</span>";
Expand All @@ -82,7 +82,7 @@ function loadBrowsers($name, $mobile) {
<input type="submit" value="Join the Swarm"/>
</form>
<?php } else { ?>
<br/><p><strong>&raquo; <?php echo $_SESSION['username']; ?></strong> <a href="<?php echo $GLOBALS['contextpath']; ?>/run/<?php echo $_SESSION['username']; ?>/">Start Running Tests</a></p>
<br/><p><strong>&raquo; <?php echo $_SESSION['username']; ?></strong> <a href="<?php echo swarmpath("run/{$_SESSION['username']}/" ); ?>">Start Running Tests</a></p>
<?php } ?>
<?php } else { ?>
<div class="join">
Expand Down
4 changes: 2 additions & 2 deletions content/jobstatus.php
Expand Up @@ -66,7 +66,7 @@ function get_status2($num, $fail, $error, $total){
foreach ( $browsers as $browser ) {
if ( $last_browser["id"] != $browser["id"] ) {
$header .= '<th><div class="browser">' .
'<img src="' . $GLOBALS['contextpath'] . '/images/' . $browser["engine"] .
'<img src="' . swarmpath( 'images/' ) . $browser["engine"] .
'.sm.png" class="browser-icon ' . $browser["engine"] .
'" alt="' . $browser["name"] .
'" title="' . $browser["name"] .
Expand Down Expand Up @@ -115,7 +115,7 @@ function get_status2($num, $fail, $error, $total){
foreach ( $useragents[ $row["useragent_id"] ] as $ua ) {
$status = get_status2(intval($ua["status"]), intval($ua["fail"]), intval($ua["error"]), intval($ua["total"]));
if ( $last_browser != $ua["useragent_id"] ) {
$output .= "<td class='$status " . $row["browser"] . "'><a href='" . $GLOBALS['contextpath'] . "/?state=runresults&run_id=" . $row["run_id"] . "&client_id=" . $ua["client_id"] . "'>" .
$output .= "<td class='$status " . $row["browser"] . "'><a href='" . swarmpath ( '/' ) . "?state=runresults&run_id=" . $row["run_id"] . "&client_id=" . $ua["client_id"] . "'>" .
($ua["status"] == 2 ?
($ua["total"] < 0 ?
"Err" :
Expand Down
2 changes: 1 addition & 1 deletion content/login.php
Expand Up @@ -2,7 +2,7 @@
<fieldset>
<legend>Login</legend>
<?php echo $error; ?>
<p>Login using your TestSwarm username and password. If you don't have one you may <a href="<?php echo $GLOBALS['contextpath']; ?>/signup/">Signup Here</a>.</p>
<p>Login using your TestSwarm username and password. If you don't have one you may <a href="<?php echo swarmpath( 'signup/' ); ?>">Signup Here</a>.</p>
<label>Username: <input type="text" name="username"/></label><br/>
<label>Password: <input type="password" name="password"/></label><br/>
<input type="submit" value="Login"/>
Expand Down
2 changes: 1 addition & 1 deletion content/run.php
@@ -1,6 +1,6 @@
<div class="userinfo">
<div class="browser you">
<img src="<?php echo $GLOBALS['contextpath']; ?>/images/<?php echo $browser; ?>.sm.png" class="browser-icon <?php echo $browser; ?>" alt="<?php echo $useragent_name; ?>" title="<?php echo $useragent_name; ?>"/>
<img src="<?php echo swarmpath( "images/{$browser}/sm.png" ); ?>" class="browser-icon <?php echo $browser; ?>" alt="<?php echo $useragent_name; ?>" title="<?php echo $useragent_name; ?>"/>
<span class="browser-name"><?php echo preg_replace('/\w+ /', "", $useragent_name); ?></span>
</div>

Expand Down
2 changes: 1 addition & 1 deletion content/signup.php
Expand Up @@ -2,7 +2,7 @@
<fieldset>
<legend>Signup</legend>
<?php echo $error; ?>
<p>Signup to use TestSwarm. If you already have an account you may <a href="<?php echo $GLOBALS['contextpath']; ?>/login/">Login Here</a>.</p>
<p>Signup to use TestSwarm. If you already have an account you may <a href="<?php echo swarmpath( 'login/' ); ?>">Login Here</a>.</p>
<label>Username: <input type="text" name="username"/></label><br/>
<label>Password: <input type="password" name="password"/></label><br/>
<label>Email: <input type="text" name="email"/></label><br/>
Expand Down
8 changes: 4 additions & 4 deletions content/tinder.php
Expand Up @@ -58,14 +58,14 @@ function get_status2($num, $fail, $error, $total){
$name = "Linux";
}

echo "<li><img src='" . $GLOBALS['contextpath'] . "/images/$engine.sm.png' class='$engine'/> <strong class='name'>$browser_name $name</strong><br>Connected <span title='$since' class='pretty'>$since</span></li>";
echo "<li><img src='" . swarmpath( '/' ) . "images/$engine.sm.png' class='$engine'/> <strong class='name'>$browser_name $name</strong><br>Connected <span title='$since' class='pretty'>$since</span></li>";
}

echo "</ul>";

}

$job_search = preg_replace("/[^a-zA-Z ]/", "", $_REQUEST['job']);
$job_search = preg_replace("/[^a-zA-Z ]/", "", getItem( 'job', $_REQUEST, '' ) );
$job_search .= "%";

$search_result = mysql_queryf("SELECT jobs.name, jobs.status, jobs.id FROM jobs, users WHERE jobs.name LIKE %s AND users.name=%s AND jobs.user_id=users.id ORDER BY jobs.created DESC LIMIT 15;", $job_search, $search_user);
Expand All @@ -83,7 +83,7 @@ function get_status2($num, $fail, $error, $total){
$job_status = get_status(intval($row[1]));
$job_id = $row[2];

$output .= '<tr><th><a href="' . $GLOBALS['contextpath'] . '/job/' . $job_id . '/">' . strip_tags($job_name) . "</a></th>\n";
$output .= '<tr><th><a href="' . swarmpath( "job/{$job_id}/" ) . '">' . strip_tags($job_name) . "</a></th>\n";

$results = array();
$states = array();
Expand All @@ -101,7 +101,7 @@ function get_status2($num, $fail, $error, $total){
foreach ( $browsers as $browser ) {
if ( $last_browser["id"] != $browser["id"] ) {
$header .= '<th><div class="browser">' .
'<img src="' . $GLOBALS['contextpath'] . '/images/' . $browser["engine"] .
'<img src="' . swarmpath( 'images/' ) . $browser["engine"] .
'.sm.png" class="browser-icon ' . $browser["engine"] .
'" alt="' . $browser["name"] .
'" title="' . $browser["name"] .
Expand Down
15 changes: 0 additions & 15 deletions inc/init.php
@@ -1,19 +1,4 @@
<?php
/**
* Get item out of array, falling back on a default if need be.
* Complains loudly on failing.
*/
function getItem($key, $array, $default=null) {
if (array_key_exists($key, $array)) {
return $array[$key];
} else {
if (func_num_args() === 3) {
return $default;
} else {
throw Exception('Unable to find key '.$key.' in the array '.var_dump($array, true));
}
}
}
$username = getItem('username', $_SESSION, getItem('user', $_REQUEST, ''));
if ( !$username ) {
$username = $_REQUEST['user'];
Expand Down
30 changes: 30 additions & 0 deletions inc/utilities.php
Expand Up @@ -60,4 +60,34 @@ function mysql_queryf($string) {
}
return $result;
}

/**
* Get item out of array, falling back on a default if need be.
* Complains loudly on failing.
*/
function getItem($key, $array, $default=null) {
if (array_key_exists($key, $array)) {
return $array[$key];
} else {
if (func_num_args() === 3) {
return $default;
} else {
die('<b>getItem Error:</b> Unable to find key <b>'.$key.'</b> in the array '.print_r($array, true));
}
}
}

/*
* Central function to get paths to files and directories
* config "contextpath" should have trailing slash!
* @param $rel string Relative path from the root, without prefixed slash
* @return string Relative path from the domain root to the specified file or directory
*/
function swarmpath( $rel ) {
global $config;
if ( $rel[0] == '/' ) {
$rel = substr($rel, 1);
}
return $config['web']['contextpath'] . $rel;
}
?>
17 changes: 8 additions & 9 deletions index.php
Expand Up @@ -2,7 +2,6 @@
session_start();

$config = parse_ini_file("config.ini", true);
$contextpath = $config['web']['contextpath'];

// $DEBUG_ON = true;
require "inc/utilities.php";
Expand All @@ -12,7 +11,7 @@
// Increase the session timeout to two weeks
ini_set("session.gc_maxlifetime", "1209600");

$state = preg_replace("/[^a-z]/", "", $_REQUEST['state']);
$state = preg_replace("/[^a-z]/", "", getItem( 'state', $_REQUEST, '' ) );

if ( !$state ) {
$state = "home";
Expand All @@ -37,26 +36,26 @@
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Test Swarm: <?php echo $title; ?></title>
<link rel="stylesheet" href="<?php echo $contextpath; ?>/css/site.css"/>
<link rel="stylesheet" href="<?php echo swarmpath( 'css/site.css' ); ?>"/>
<?php echo isset($scripts) ? $scripts : ''; ?>
</head>
<body>
<ul class="nav">
<?php if ( $_SESSION['username'] && $_SESSION['auth'] == 'yes' ) { ?>
<li><strong><a href="/user/<?php echo $_SESSION['username'];?>/"><?php echo $_SESSION['username'];?></a></strong></li>
<li><a href="<?php echo $contextpath; ?>/run/<?php echo $_SESSION['username'];?>/">Join the Swarm</a></li>
<li><a href="<?php echo $contextpath; ?>/logout/">Logout</a></li>
<li><strong><a href="<?php echo swarmpath( "user/{$_SESSION['username']}/" ); ?>"><?php echo $_SESSION['username'];?></a></strong></li>
<li><a href="<?php echo swarmpath( "run/{$_SESSION['username']}" );?>">Join the Swarm</a></li>
<li><a href="<?php echo swarmpath( 'logout/' ); ?>">Logout</a></li>
<?php } else { ?>
<li><a href="<?php echo $contextpath; ?>/login/">Login</a></li>
<li><a href="<?php echo $contextpath; ?>/signup/">Signup</a></li>
<li><a href="<?php echo swarmpath( 'login/' ); ?>">Login</a></li>
<li><a href="<?php echo swarmpath( 'signup/' ); ?>">Signup</a></li>
<?php } ?>
<li><a href="http://github.com/jquery/testswarm">Source Code</a></li>
<li><a href="http://github.com/jquery/testswarm/issues">Bug Tracker</a></li>
<li><a href="http://groups.google.com/group/testswarm">Discuss</a></li>
<li><a href="http://twitter.com/testswarm">Updates</a></li>
<li><a href="http://wiki.github.com/jquery/testswarm">About</a></li>
</ul>
<h1><a href="<?php echo $contextpath; ?>/"><img src="<?php echo $contextpath; ?>/images/testswarm_logo_wordmark.png" alt="TestSwarm" title="TestSwarm"/></a></h1>
<h1><a href="<?php echo swarmpath( '/' ); ?>"><img src="<?php echo swarmpath( 'images/testswarm_logo_wordmark.png' ); ?>" alt="TestSwarm" title="TestSwarm"/></a></h1>
<h2><?php echo $title; ?></h2>
<div id="main">
<?php } if ( $state && file_exists($contentFile) ) {
Expand Down
2 changes: 1 addition & 1 deletion logic/home.php
@@ -1,3 +1,3 @@
<?php
$title = "Distributed Continuous Integration for JavaScript";
$scripts = '<script type="text/javascript" src="' . $GLOBALS['contextpath'] . '/js/jquery.js"></script>';
$scripts = '<script type="text/javascript" src="' . swarmpath( 'js/jquery.js' ) . '"></script>';
4 changes: 2 additions & 2 deletions logic/jobstatus.php
@@ -1,7 +1,7 @@
<?php

$title = "Job Status";
$scripts = '<script type="text/javascript" src="' . $GLOBALS['contextpath'] . '/js/jquery.js"></script>' .
'<script type="text/javascript" src="' . $GLOBALS['contextpath'] . '/js/status.js"></script>';
$scripts = '<script type="text/javascript" src="' . swarmpath( 'js/jquery.js' ) . '"></script>' .
'<script type="text/javascript" src="' . swarmpath( 'js/status.js' ) . '"></script>';

?>
2 changes: 1 addition & 1 deletion logic/login.php
Expand Up @@ -20,7 +20,7 @@
$_SESSION['auth'] = "yes";

session_write_close();
header("Location: " . $GLOBALS['contextpath'] . "/user/$username/");
header("Location: " . swarmpath( "user/$username/" ) );
exit();
} else {
$error = "<p>Error: Incorrect username or password.</p>";
Expand Down
4 changes: 2 additions & 2 deletions logic/run.php
Expand Up @@ -21,5 +21,5 @@
$scripts = "<script type='text/javascript'>var client_id = $client_id;</script>";
}

$scripts .= '<script type="text/javascript" src="' . $GLOBALS['contextpath'] . '/js/jquery.js"></script>' .
'<script type="text/javascript" src="' . $GLOBALS['contextpath'] . '/js/run.js?' . time() . '"></script>';
$scripts .= '<script type="text/javascript" src="' . swarmpath( 'js/jquery.js' ) .'"></script>' .
'<script type="text/javascript" src="' . swarmpath( 'js/run.js' ) . '?' . time() . '"></script>';
2 changes: 1 addition & 1 deletion logic/signup.php
Expand Up @@ -31,7 +31,7 @@
$_SESSION['auth'] = "yes";

session_write_close();
header("Location: " . $GLOBALS['contextpath'] . "/user/$username/");
header("Location: " . swarmpath( "user/$username/" ) );
exit();
}

Expand Down
6 changes: 3 additions & 3 deletions logic/tinder.php
Expand Up @@ -3,8 +3,8 @@
$search_user = preg_replace("/[^a-zA-Z0-9_ -]/", "", $_GET['user']);

$title = "$search_user";
$scripts = '<script type="text/javascript" src="' . $GLOBALS['contextpath'] . '/js/jquery.js"></script>' .
'<script type="text/javascript" src="' . $GLOBALS['contextpath'] . '/js/pretty.js"></script>' .
'<script type="text/javascript" src="' . $GLOBALS['contextpath'] . '/js/view.js"></script>';
$scripts = '<script type="text/javascript" src="' . swarmpath( 'js/jquery.js' ) . '"></script>' .
'<script type="text/javascript" src="' . swarmpath( 'js/pretty.js' ) . '"></script>' .
'<script type="text/javascript" src="' . swarmpath( 'js/view.js' ) . '"></script>';

?>
4 changes: 2 additions & 2 deletions logic/wipejob.php
Expand Up @@ -31,9 +31,9 @@
}

if ( $type == "delete" ) {
header("Location: /user/" . $_SESSION['username'] . "/");
header("Location: " . swarmpath( "user/{$_SESSION['username']}/" ) );
} else {
header("Location: /job/$job_id/");
header("Location: " . swarmpath( "job/{$job_id}/" ) );
}
}

Expand Down
2 changes: 1 addition & 1 deletion logic/wiperun.php
Expand Up @@ -20,7 +20,7 @@
}
}

header("Location: /job/$job_id/");
header("Location: " . swarmpath( "job/{$job_id}/" ) );
}

exit();
Expand Down

0 comments on commit 003061c

Please sign in to comment.