Skip to content

Commit

Permalink
Revert legacy changes to PHP results posting code
Browse files Browse the repository at this point in the history
This partially reverts commit aef4f5c
  • Loading branch information
gregorbg committed Dec 11, 2022
1 parent e161afc commit 231fd6d
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 12 deletions.
2 changes: 1 addition & 1 deletion webroot/results/includes/WCAClasses/DBConn.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __construct($config, $charset = "utf8")
$this->conn = new \mysqli($config['host'], $config['user'], $config['pass'], $config['name'], $config['port']);
if($this->conn->connect_errno)
{
trigger_error("Failed to connect to MySQL: (" . $this->conn->connect_errno . ") " . $this->conn->connect_error, E_USER_ERROR);
trigger_error("Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error, E_USER_ERROR);
}

// Treat warnings as errors
Expand Down
38 changes: 38 additions & 0 deletions webroot/results/includes/_config.php.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

// data here is loaded into configuration class. Should be accessed by via configuration object (global $config in _framework file).

$config['database']['host'] = '...';
$config['database']['user'] = '...';
$config['database']['pass'] = '...';
$config['database']['name'] = '...';
$config['database']['port'] = '...';

$config['recaptcha']['publickey'] = '...';
$config['recaptcha']['privatekey'] = '...';

$config['maps']['api_key'] = '';

// check for PEAR mail (to send auth email)
if(class_exists('Mail')) {
$config['mail']['pear'] = true;
} else {
$config['mail']['pear'] = false;
}

if($config['mail']['pear']) {
$config['mail']['from'] = '';
$config['mail']['host'] = '';
$config['mail']['port'] = '';
$config['mail']['user'] = '';
$config['mail']['pass'] = '';
} else {
$config['mail']['from'] = '[email protected]';
}


// pathToRoot and filesPath are determined by config class - just a placeholder here. You can enter an explicit value if desired. Include trailing slash.
// pathToRoot is for web urls, etc. May be different than filesystem paths. Eg, "/results/".
$config['pathToRoot'] = "";
// filesPath is absolute path for system files. May be different than web urls. Eg, "/var/www/results/".
$config['filesPath'] = "";
109 changes: 98 additions & 11 deletions webroot/results/includes/_database.php
Original file line number Diff line number Diff line change
@@ -1,31 +1,92 @@
<?php

/**
icklerf:
I took liberty to replace each old mysql call with a direct replacement from the dbConn class
This is not the ideal way, but php should be replaced very soon(TM)
PLEASE NOTE: FUNCTIONALITY IN THIS FILE IS SET TO BE DEPRECATED. Eventually PHP will no longer support the MySQL API, so this code
needs to be phased out. Instead, please use or extend the mysqli connection class for any new code needing mysql functionality.
**/

establishDatabaseAccess();

#----------------------------------------------------------------------
function establishDatabaseAccess () {
#----------------------------------------------------------------------
global $config;
$db_config = $config->get('database');

#--- Connect to the database server.
# TODO: Upgrade
mysql_connect( $db_config['host'], $db_config['user'], $db_config['pass'] )
or showDatabaseError( "Unable to connect to the database." );

#--- Select the database.
mysql_select_db( $db_config['name'] )
or showDatabaseError( "Unable to access the database." );

dbCommand( "SET NAMES 'utf8'" );
}

#----------------------------------------------------------------------
function mysqlEscape ( $string ) {
#----------------------------------------------------------------------
global $wcadb_conn;
return $wcadb_conn->mysqlEscape($string);
return mysql_real_escape_string( $string );
}

#----------------------------------------------------------------------
function dbQuery ( $query ) {
#----------------------------------------------------------------------
global $wcadb_conn;
return $wcadb_conn->dbQuery($query);

startTimer();

if( wcaDebug() ){
startTimer();
global $dbQueryCtr;
$dbQueryCtr++;
echo "\n\n<!-- dbQuery(\n$query\n) -->\n\n";
echo "<br>";
stopTimer( 'printing the database query' );
}

startTimer();
$dbResult = mysql_query( $query )
or showDatabaseError( "Unable to perform database query." );
stopTimer( "pure database query" );

startTimer();
$rows = array();
while( $row = mysql_fetch_array( $dbResult ))
$rows[] = $row;
stopTimer( "fetching database query results" );

startTimer();
mysql_free_result( $dbResult );
stopTimer( "freeing the mysql result" );

global $dbQueryTotalTime;
$dbQueryTotalTime += stopTimer( "the whole dbQuery execution" );

return $rows;
}

#----------------------------------------------------------------------
function dbQueryHandle ( $query ) {
#----------------------------------------------------------------------
global $wcadb_conn;
return $wcadb_conn->dbQuery($query);

if( wcaDebug() ){
startTimer();
global $dbQueryCtr;
$dbQueryCtr++;
echo "\n\n<!-- dbQuery(\n$query\n) -->\n\n";
echo "<br>";
stopTimer( 'printing the database query' );
}

startTimer();
$dbResult = mysql_query( $query )
or showDatabaseError( "Unable to perform database query." );
global $dbQueryTotalTime;
$dbQueryTotalTime += stopTimer( "pure database query" );

return $dbResult;
}

#----------------------------------------------------------------------
Expand All @@ -38,8 +99,24 @@ function dbValue ( $query ) {
#----------------------------------------------------------------------
function dbCommand ( $command ) {
#----------------------------------------------------------------------
global $wcadb_conn;
return $wcadb_conn->dbCommand($command);

if( wcaDebug() ){
startTimer();
global $dbCommandCtr;
$dbCommandCtr++;
$commandForShow = strlen($command) < 1010
? $command
: substr($command,0,1000) . '[...' . (strlen($command)-1000) . '...]';
echo "\n\n<!-- dbCommand(\n$commandForShow\n) -->\n\n";
stopTimer( 'printing the database command' );
}

#--- Execute the command.
startTimer();
$dbResult = mysql_query( $command )
or showDatabaseError( "Unable to perform database command." );
global $dbCommandTotalTime;
$dbCommandTotalTime += stopTimer( "executing database command" );
}

#----------------------------------------------------------------------
Expand Down Expand Up @@ -196,6 +273,16 @@ function dbDebug ( $query ) {
echo "</table>";
}

#----------------------------------------------------------------------
function showDatabaseError ( $message ) {
#----------------------------------------------------------------------

#--- Normal users just get a "Sorry", developers/debuggers get more details
die( $_SERVER['SERVER_NAME'] == 'localhost' || wcaDebug()
? "<p>$message<br />\n(" . mysql_error() . ")</p>\n"
: "<p>Problem with the database, sorry. If this persists for several minutes, " .
"please tell us at <a href='mailto:[email protected]'>[email protected]</a></p>" );
}

#----------------------------------------------------------------------
function showDatabaseStatistics () {
Expand Down

0 comments on commit 231fd6d

Please sign in to comment.