Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Update uses of deprecated globals with TestSwarmContext

- All instances of $swarmBrowser replaced, global removed.
- All instances of $swarmConfig replaced, global removed.
- All instances of $swarmDB replaced, global removed.
- All instances of $swarmRequest replaced, global removed.
  • Loading branch information...
commit 1aaa420351294389a71bc25df4423ea74fc4d54a 1 parent a9e49fa
@Krinkle authored
View
19 content/home.php
@@ -61,11 +61,13 @@
/** @return bool: Whether the current user was found in the swarm */
function loadBrowsers($headingTitle, $mobile) {
- global $swarmBrowser, $swarmDB;
+ global $swarmContext;
+ $bi = $swarmContext->getBrowserInfo();
+ $db = $swarmContext->getDB();
$foundSelf = false;
- $rows = $swarmDB->getRows(str_queryf(
+ $rows = $db->getRows(str_queryf(
"SELECT
useragents.engine as engine,
useragents.name as name,
@@ -82,8 +84,8 @@ function loadBrowsers($headingTitle, $mobile) {
AND mobile = %s
ORDER BY engine, name;",
swarmdb_dateformat( strtotime( '1 minute ago' ) ),
- $swarmBrowser->getBrowserCodename(),
- $swarmBrowser->getBrowserVersion(),
+ $bi->getBrowserCodename(),
+ $bi->getBrowserVersion(),
$mobile
));
@@ -116,10 +118,13 @@ function loadBrowsers($headingTitle, $mobile) {
return $foundSelf;
}
+$request = $swarmContext->getRequest();
+$bi = $swarmContext->getBrowserInfo();
+
if ( $found ) { ?>
<div class="join">
<p><strong>TestSwarm Needs Your Help!</strong> You have a browser that we need to test against, you should join the swarm to help us out.</p>
- <?php if ( !$swarmRequest->getSessionData( "username" ) ) { ?>
+ <?php if ( !$request->getSessionData( "username" ) ) { ?>
<form action="" method="get">
<input type="hidden" name="state" value="run"/>
<br/><strong>Username:</strong><br/>
@@ -127,12 +132,12 @@ function loadBrowsers($headingTitle, $mobile) {
<input type="submit" value="Join the Swarm"/>
</form>
<?php } else { ?>
- <br/><p><strong>&raquo; <?php echo $swarmRequest->getSessionData( "username" ); ?></strong> <a href="<?php echo swarmpath("run/{$swarmRequest->getSessionData( "username" )}/" ); ?>">Start Running Tests</a></p>
+ <br/><p><strong>&raquo; <?php echo $request->getSessionData( "username" ); ?></strong> <a href="<?php echo swarmpath("run/{$request->getSessionData( "username" )}/" ); ?>">Start Running Tests</a></p>
<?php } ?>
</div>
<?php } else { ?>
<div class="join">
<p>TestSwarm doesn't need your help at this time. If you wish to help run tests you should load up one of the below browsers.</p>
- <p>If you feel that this may be a mistake, copy the following information (<?php echo $swarmBrowser->getBrowserCodename(); ?> <?php echo $swarmBrowser->getBrowserVersion(); ?> <?php echo $swarmBrowser->getOsCodename(); ?>) and your <a href="http://useragentstring.com/">useragent string</a>, and post it to the <a href="//groups.google.com/group/testswarm">discussion group</a>.</a>
+ <p>If you feel that this may be a mistake, copy the following information (<?php echo $bi->getBrowserCodename(); ?> <?php echo $bi->getBrowserVersion(); ?> <?php echo $bi->getOsCodename(); ?>) and your <a href="http://useragentstring.com/">useragent string</a>, and post it to the <a href="//groups.google.com/group/testswarm">discussion group</a>.</a>
</div>
<?php }
View
7 content/run.php
@@ -1,7 +1,10 @@
+<?php
+$bi = $swarmContext->getBrowserInfo();
+?>
<div class="userinfo">
<div class="browser you">
- <img src="<?php echo swarmpath( "images/{$swarmBrowser->getBrowserCodename()}.sm.png" ); ?>" class="browser-icon <?php echo $swarmBrowser->getBrowserCodename(); ?>" alt="<?php echo $swarmBrowser->getSwarmUserAgentName(); ?>" title="<?php echo $swarmBrowser->getSwarmUserAgentName(); ?>"/>
- <span class="browser-name"><?php echo preg_replace('/\w+ /', "", $swarmBrowser->getSwarmUserAgentName()); ?></span>
+ <img src="<?php echo swarmpath( "images/{$bi->getBrowserCodename()}.sm.png" ); ?>" class="browser-icon <?php echo $bi->getBrowserCodename(); ?>" alt="<?php echo $bi->getSwarmUserAgentName(); ?>" title="<?php echo $bi->getSwarmUserAgentName(); ?>"/>
+ <span class="browser-name"><?php echo preg_replace('/\w+ /', "", $bi->getSwarmUserAgentName()); ?></span>
</div>
<h3><?php echo $username; ?></h3>
View
2  content/tinder.php
@@ -98,7 +98,7 @@ function get_status2($num, $fail, $error, $total){
}
- $job_search = preg_replace( "/[^a-zA-Z ]/", "", $swarmRequest->getVal( "job", "" ) );
+ $job_search = preg_replace( "/[^a-zA-Z ]/", "", $swarmContext->getRequest()->getVal( "job", "" ) );
$job_search .= "%";
$search_result = mysql_queryf(
View
1  inc/Database.php
@@ -110,6 +110,7 @@ public function getNumRows( $res ) {
/**
* Queries other than SELECT, such as DELETE, UPDATE and INSERT.
+ * @return resource|false
*/
public function query( $sql ) {
return $this->doQuery( $sql );
View
6 inc/TestSwarm.php
@@ -16,11 +16,11 @@ class TestSwarmContext {
* needs to be passed is an array with all setting keys from testswarm.ini
* (including ones commented out in the sample file, it has to contain them all)
* Population of default values of optional settings happens in init.php
- * @param $swarmConfig array
+ * @param $config array
*/
- public function __construct( Array $swarmConfig ) {
+ public function __construct( Array $config ) {
$conf = new stdClass;
- foreach ( $swarmConfig as $key => $val ) {
+ foreach ( $config as $key => $val ) {
$conf->$key = is_array( $val ) ? (object)$val : $val;
}
$this->conf = $conf;
View
39 inc/init-usersession.php
@@ -2,7 +2,8 @@
/**
* Initialize global variables related to a user,
* for requests that require a username.
- * Sets the following globals:
+ *
+ * @todo Deprecate these globals:
* - $username
* - $user_id
* - $client_id
@@ -10,9 +11,13 @@
* @since 0.1.0
* @package TestSwarm
*/
- global $swarmBrowser, $swarmDB, $swarmRequest;
+ global $swarmContext;
+
+ $bi = $swarmContext->getBrowserInfo();
+ $db = $swarmContext->getDB();
+ $request = $swarmContext->getRequest();
- $username = $swarmRequest->getSessionData( "username", $swarmRequest->getVal( "user" ) );
+ $username = $request->getSessionData( "username", $request->getVal( "user" ) );
if ( !$username ) {
$username = $_REQUEST["user"];
}
@@ -27,13 +32,13 @@
exit( "Username required. ?user=USERNAME." );
}
- $client_id = preg_replace( "/[^0-9]/", "", $swarmRequest->getVal( "client_id" ) );
+ $client_id = preg_replace( "/[^0-9]/", "", $request->getVal( "client_id" ) );
// Client passed
if ( $client_id ) {
// Verify that the client exists,
// And get the user ID.
- $user_id = $swarmDB->getOne(str_queryf(
+ $user_id = $db->getOne(str_queryf(
"SELECT
user_id
FROM
@@ -46,7 +51,7 @@
if ( $user_id ) {
// If the client ID is already provided, update its record so
// that we know that it's still alive
- $swarmDB->query(str_queryf(
+ $db->query(str_queryf(
"UPDATE clients SET updated=%s WHERE id=%u LIMIT 1;",
swarmdb_dateformat( SWARM_NOW ),
$client_id
@@ -62,37 +67,37 @@
} else {
// If the useragent isn't known, abort with an error message
- if ( !$swarmBrowser->isKnownInTestSwarm() ) {
+ if ( !$bi->isKnownInTestSwarm() ) {
echo "Your browser is not supported for testing right now.\n"
- . "Browser: {$swarmBrowser->getBrowserCodename()} Version: {$swarmBrowser->getBrowserVersion()}";
+ . "Browser: {$bi->getBrowserCodename()} Version: {$bi->getBrowserVersion()}";
exit;
}
// Figure out what the user's ID number is
- $user_id = $swarmDB->getOne(str_queryf( "SELECT id FROM users WHERE name=%s;", $username ));
+ $user_id = $db->getOne(str_queryf( "SELECT id FROM users WHERE name=%s;", $username ));
// If the user doesn't have one, create a new user account
if ( !$user_id ) {
- $swarmDB->query(str_queryf(
+ $db->query(str_queryf(
"INSERT INTO users (name, created, updated, seed) VALUES(%s, %s, %s, RAND());",
$username,
swarmdb_dateformat( SWARM_NOW ),
swarmdb_dateformat( SWARM_NOW )
));
- $user_id = $swarmDB->getInsertId();
+ $user_id = $db->getInsertId();
}
// Insert in a new record for the client and get its ID
- $swarmDB->query(str_queryf(
+ $db->query(str_queryf(
"INSERT INTO clients (user_id, useragent_id, useragent, os, ip, created)
VALUES(%u, %u, %s, %s, %s, %s);",
$user_id,
- $swarmBrowser->getSwarmUserAgentID(),
- $swarmBrowser->getRawUA(),
- $swarmBrowser->getOsCodename(),
- $swarmRequest->getIP(),
+ $bi->getSwarmUserAgentID(),
+ $bi->getRawUA(),
+ $bi->getOsCodename(),
+ $request->getIP(),
swarmdb_dateformat( SWARM_NOW )
));
- $client_id = $swarmDB->getInsertId();
+ $client_id = $db->getInsertId();
}
View
29 inc/init.php
@@ -89,15 +89,24 @@
/**
+ * Context
+ * @{
+ */
+$swarmContext = new TestSwarmContext( $swarmConfig );
+
+/**@}*/
+
+
+/**
* Debugging
* @{
*/
function swarmExceptionHandler( Exception $e ) {
- global $swarmConfig;
+ global $swarmContext;
$msg = "<h2>TestSwarm internal error</h2>\n\n";
- if ( $swarmConfig["debug"]["show_exception_details"] ) {
+ if ( $swarmContext->getConf()->debug->show_exception_details ) {
$msg .=
'<p>' . nl2br( htmlspecialchars( $e->getMessage() ) ) .
'</p><p>Backtrace:</p><p>' . nl2br( htmlspecialchars( $e->getTraceAsString() ) ) .
@@ -118,7 +127,7 @@ function swarmExceptionHandler( Exception $e ) {
set_exception_handler( "swarmExceptionHandler" );
-if ( $swarmConfig["debug"]["php_error_reporting"] ) {
+if ( $swarmContext->getConf()->debug->php_error_reporting ) {
error_reporting( E_ALL );
ini_set( "display_errors", 1 );
}
@@ -127,20 +136,6 @@ function swarmExceptionHandler( Exception $e ) {
/**
- * Context
- * @{
- */
-$swarmContext = new TestSwarmContext( $swarmConfig );
-
-// Set globals for backwards compatibility
-$swarmDB = $swarmContext->getDB();
-$swarmRequest = $swarmContext->getRequest();
-$swarmBrowser = $swarmContext->getBrowserInfo();
-
-/**@}*/
-
-
-/**
* Session
* @{
*/
View
11 inc/utilities.php
@@ -40,10 +40,12 @@ function array_extend( $arr1, $arr2 ) {
* @source php.net/mysql-query#86447
*/
function mysql_queryf(/* $string, $arg, .. */) {
+ global $swarmContext;
+
$args = func_get_args();
$sql_query = call_user_func_array( 'str_queryf', $args );
- $result = mysql_query( $sql_query );
+ $result = $swarmContext->getDB()->query( $sql_query );
if (!$result) {
echo "Invalid query: " . mysql_error();
exit;
@@ -137,14 +139,14 @@ function swarmdb_dateformat( $timestamp = 0 ) {
* @return string Relative path from the domain root to the specified file or directory
*/
function swarmpath( $rel ) {
- global $swarmConfig;
+ global $swarmContext;
// Only normalize the contextpath once
static $contextpath;
if ( is_null( $contextpath ) ) {
// Add trailing slash if it's missing
- $path = $swarmConfig["web"]["contextpath"];
+ $path = $swarmContext->getConf()->web->contextpath;
if ( substr( $path, -1 ) !== '/' ) {
$path = "$path/";
}
@@ -156,7 +158,8 @@ function swarmpath( $rel ) {
$path = "/$path";
}
- $swarmConfig["web"]["contextpath"] = $path;
+ // Update it (just in case it's used elsewhere)
+ $swarmContext->getConf()->web->contextpath = $path;
}
// Just in case, strip the leading slash
View
10 index.php
@@ -12,7 +12,7 @@
require_once "inc/init.php";
-$state = preg_replace("/[^a-z]/", "", $swarmRequest->getVal( "state", "" ) );
+$state = preg_replace("/[^a-z]/", "", $swarmContext->getRequest()->getVal( "state", "" ) );
if ( !$state ) {
$state = "home";
@@ -38,15 +38,15 @@
<head>
<meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
- <title><?php echo htmlentities( $swarmConfig['web']['title'] . ': ' . $title ); ?></title>
+ <title><?php echo htmlentities( $swarmContext->getConf()->web->title . ': ' . $title ); ?></title>
<link rel="stylesheet" href="<?php echo swarmpath( "css/site.css" ); ?>"/>
<script>window.SWARM = <?php echo json_encode( array(
- // Derived version of $swarmConfig for the browser
- // (not the entire array since it also contains DB passwords!)
+ // Export a simplified version of the TestSwarm configuration object to the browser
+ // (not the entire object since it also contains DB password and such..).
"web" => array(
"contextpath" => swarmpath( "" ),
),
- "client" => $swarmConfig["client"],
+ "client" => $swarmContext->getConf()->client,
) ); ?>;</script>
<?php
echo isset( $scripts ) ? "\t" . $scripts . "\n" : "";
View
8 logic/addjob.php
@@ -2,12 +2,14 @@
$title = "Add New Job";
- if ( $swarmRequest->getVal( "state" ) !== "addjob" || !$swarmRequest->hasKeys( "user", "auth", "job_name" ) ) {
+ $request = $swarmContext->getRequest();
+
+ if ( $request->getVal( "state" ) !== "addjob" || !$request->hasKeys( "user", "auth", "job_name" ) ) {
return;
}
- $username = $swarmRequest->getVal( "user", "" );
- $auth = $swarmRequest->getVal( "auth", "" );
+ $username = $request->getVal( "user", "" );
+ $auth = $request->getVal( "auth", "" );
$user_id = null;
$result = mysql_queryf("SELECT id FROM users WHERE name=%s AND auth=%s;", $username, $auth);
View
10 logic/getrun.php
@@ -1,7 +1,9 @@
<?php
require "inc/init-usersession.php";
- global $swarmConfig;
+ global $swarmContext;
+
+ $bi = $swarmContext->getBrowserInfo();
$result = mysql_queryf(
"SELECT
@@ -13,7 +15,7 @@
AND NOT EXISTS (SELECT 1 FROM run_client WHERE run_useragent.run_id = run_id AND client_id = %u)
ORDER BY run_id DESC
LIMIT 1;",
- $swarmBrowser->getSwarmUserAgentID(),
+ $bi->getSwarmUserAgentID(),
$client_id
);
@@ -45,7 +47,7 @@
mysql_queryf(
"UPDATE run_useragent SET runs = runs + 1, status = 1 WHERE run_id=%u AND useragent_id=%u LIMIT 1;",
$run_id,
- $swarmBrowser->getSwarmUserAgentID()
+ $bi->getSwarmUserAgentID()
);
# Initialize the client run
@@ -66,7 +68,7 @@
}
echo json_encode( array(
- "swarmUpdate" => array( "client" => $swarmConfig["client"] ),
+ "swarmUpdate" => array( "client" => $swarmContext->getConf()->client ),
"runInfo" => $runInfo,
) );
View
8 logic/login.php
@@ -1,17 +1,19 @@
<?php
$title = "Login";
+ $request = $swarmContext->getRequest();
+
if ( isset( $_SESSION["username"] ) && isset( $_SESSION["auth"] ) && $_SESSION["auth"] == "yes" ) {
$username = $_SESSION["username"];
header("Location: " . swarmpath( "user/$username/" ) );
exit;
}
- $username = preg_replace("/[^a-zA-Z0-9_ -]/", "", $swarmRequest->getVal( "username" ) );
- $password = $swarmRequest->getVal( "password" );
+ $username = preg_replace("/[^a-zA-Z0-9_ -]/", "", $request->getVal( "username" ) );
+ $password = $request->getVal( "password" );
$error = "";
- if ( $swarmRequest->wasPosted() && $username && $password ) {
+ if ( $request->wasPosted() && $username && $password ) {
$result = mysql_queryf(
"SELECT id
View
6 logic/run.php
@@ -1,9 +1,11 @@
<?php
require "inc/init-usersession.php";
- $user = $swarmRequest->getVal( "username", $swarmRequest->getSessionData( "username" ) );
+ $request = $swarmContext->getRequest();
- if ( !$swarmRequest->getVal( "user" ) && $user ) {
+ $user = $request->getVal( "username", $request->getSessionData( "username" ) );
+
+ if ( !$request->getVal( "user" ) && $user ) {
header("Location: " . swarmpath( "run/$user/" ) );
exit;
}
View
23 logic/saverun.php
@@ -1,13 +1,16 @@
<?php
require "inc/init-usersession.php";
- if ( $swarmRequest->wasPosted() ) {
- $run_id = preg_replace( "/[^0-9]/", "", $swarmRequest->getVal( "run_id", "" ) );
- $fail = preg_replace( "/[^0-9-]/", "", $swarmRequest->getVal( "fail", "" ) );
- $error = preg_replace( "/[^0-9-]/", "", $swarmRequest->getVal( "error", "" ) );
- $total = preg_replace( "/[^0-9-]/", "", $swarmRequest->getVal( "total", "" ) );
+ $bi = $swarmContext->getBrowserInfo();
+ $request = $swarmContext->getRequest();
- $results = gzencode( $swarmRequest->getVal( "results", "" ) );
+ if ( $request->wasPosted() ) {
+ $run_id = preg_replace( "/[^0-9]/", "", $request->getVal( "run_id", "" ) );
+ $fail = preg_replace( "/[^0-9-]/", "", $request->getVal( "fail", "" ) );
+ $error = preg_replace( "/[^0-9-]/", "", $request->getVal( "error", "" ) );
+ $total = preg_replace( "/[^0-9-]/", "", $request->getVal( "total", "" ) );
+
+ $results = gzencode( $request->getVal( "results", "" ) );
} else {
$results = false;
}
@@ -49,7 +52,7 @@
AND clients.useragent_id = %u;",
$run_id,
$client_id,
- $swarmBrowser->getSwarmUserAgentID()
+ $bi->getSwarmUserAgentID()
);
while ( $row = mysql_fetch_array($result) ) {
@@ -66,7 +69,7 @@
WHERE useragent_id = %u
AND run_id = %u
LIMIT 1;",
- $swarmBrowser->getSwarmUserAgentID(),
+ $bi->getSwarmUserAgentID(),
$run_id
);
} else {
@@ -84,7 +87,7 @@
AND clients.useragent_id = %u;",
$run_id,
$client_id,
- $swarmBrowser->getSwarmUserAgentID()
+ $bi->getSwarmUserAgentID()
);
while ( $row = mysql_fetch_array($result) ) {
@@ -105,7 +108,7 @@
WHERE useragent_id = %u
AND run_id = %u
LIMIT 1;",
- $swarmBrowser->getSwarmUserAgentID(),
+ $bi->getSwarmUserAgentID(),
$run_id
);
}
View
12 logic/signup.php
@@ -1,15 +1,17 @@
<?php
$title = "Signup";
- if ( !$swarmRequest->wasPosted() ) {
+ $request = $swarmContext->getRequest();
+
+ if ( !$request->wasPosted() ) {
return;
)
- $username = preg_replace("/[^a-zA-Z0-9_ -]/", "", $swarmRequest->getVal( "username", false ) );
- $password = $swarmRequest->getVal( "password", false );
+ $username = preg_replace("/[^a-zA-Z0-9_ -]/", "", $request->getVal( "username", false ) );
+ $password = $request->getVal( "password", false );
- $email = $swarmRequest->getVal( "email" );
- $request = $swarmRequest->getVal( "request" );
+ $email = $request->getVal( "email" );
+ $request = $request->getVal( "request" );
$error = "";
if ( !$username || !$password ) {
View
2  logic/tinder.php
@@ -1,6 +1,6 @@
<?php
- $search_user = preg_replace( "/[^a-zA-Z0-9_ -]/", "", $swarmRequest->getVal( "user" ) );
+ $search_user = preg_replace( "/[^a-zA-Z0-9_ -]/", "", $swarmContext->getRequest()->getVal( "user" ) );
$title = "$search_user";
$scripts =
Please sign in to comment.
Something went wrong with that request. Please try again.