Warning: chmod(): Operation not permitted in /var/www/hopeinstoughton_www/wp-content/plugins/simple-universal-google-analytics/main.php on line 8 [0] in function chmod in /var/www/hopeinstoughton_www/wp-content/plugins/simple-universal-google-analytics/main.php on line 8 [1] in function include_once in /var/www/hopeinstoughton_www/wp-settings.php on line 560 [2] in function require_once in /var/www/hopeinstoughton_www/wp-config.php on line 85 [3] in function require_once in /var/www/hopeinstoughton_www/wp-load.php on line 50 [4] in function require_once in /var/www/hopeinstoughton_www/wp-blog-header.php on line 13 [5] in function require in /var/www/hopeinstoughton_www/index.php on line 17
| Server IP : 94.177.8.99 / Your IP : 216.73.217.165 Web Server : Apache/2.4.58 (Ubuntu) System : Linux aries 6.8.0-134-generic #134-Ubuntu SMP PREEMPT_DYNAMIC Fri Jun 26 18:43:11 UTC 2026 x86_64 User : www-data ( 33) PHP Version : 8.1.34 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /var/www/hopeinstoughton_books/core/config/ |
Upload File : |
<?php
define('MYSQL_BUFFER', 1);
/*
Unexpected Duplicate record errors: table may be marked as Crashed, and need repair.
*/
class database_my {
var $dbobj;
var $user = '';
var $password = '';
var $host;
var $error;
var $db;
var $lastsql;
var $options;
var $failedonly;
// connection string: mysql://user:password@hostname/databasename
// password is encoded
function __construct($connectionstring) {
$this->error = '';
self::setConnection($connectionstring);
$this->options = 0;
$this->dbobj = NULL;
}
public function open($databasename = '') {
if ($databasename !='') $this->db = $databasename;
$this->close(); // close any currently open connection
// TRUE => Don't recycle current active connections - they may be for a different database
if (!($this->dbobj = @mysql_connect($this->host, $this->user, base64_decode($this->password), TRUE))) {
$this->error = 'Could not connect: ' . $this->host;
return 0;
}
if (!mysql_select_db($this->db, $this->dbobj)) {
$this->error = 'select error: ' . mysql_error($this->dbobj);
return 0;
}
return 1;
}
public function close() {
$this->error = '';
if ($this->dbobj != NULL) mysql_close($this->dbobj);
$this->dbobj = NULL;
}
function __destruct() {
$this->close();
}
public function setConnection($connection_string) {
if (preg_match('~mysql://(.+)/(.*?)$~', $connection_string, $elements)) {
// if ($_SERVER['REMOTE_ADDR'] == '75.68.105.117')
$this->db = $elements[2];
$host = $elements[1];
if (preg_match('~^(.*):(.*)@(.*)$~', $host, $elements)) {
$this->user = $elements[1];
$this->password = $elements[2];
$this->host = $elements[3];
} else {
$this->host = $host; // leave existing username unchanged
}
} else {
$this->error = 'not a mysql connection string';
}
}
private function _updateField(&$values, $name, $value) {
if ($values != '') $values .= ',';
$values .= "`$name`=$value";
}
// http://dev.mysql.com/doc/refman/5.1/en/string-syntax.html
public function formatString($value, $sz = 0) {
// format the value suitable for storing in the database
// $value - string needs to be processed, $sz - max size of string (field size)
if ($value === NULL) return 'NULL';
if ($sz > 0) $value = substr($value, 0, $sz);
$value = str_replace(array("\\", "'", "\n", "\r", "\t", "\0"),
array("\\\\", "\\'", "\\n", "\\r", "\\t", "\\0"), $value);
return "'$value'";
}
public function saveRecord(&$data, $primarykey = '') { // primarykey => for autoinc fields
global $VARIABLE;
$this->error = ''; // clear out any previous error
$this->errnum = 0;
$cmd = 'replace';
$existing = isset($data['__existing']) ? $data['__existing'] : 0;
$tablename = isset($data['__table']) ? $data['__table'] : '';
if ($tablename == '') {
$this->error = 'cannot save - no table specified';
return 0;
}
$values = $skipkey = $whereclause = '';
if (!$existing && $primarykey != '') $skipkey = $primarykey;
if ($existing) { // do an update command
$dtable = strtolower($tablename);
$ddatabase = strtolower($this->db);
if (($p = strpos($tablename, '.')) !== FALSE) {
$ddatabase = strtolower(substr($tablename, 0, $p));
$dtable = strtolower(substr($tablename, $p + 1));
}
$cmd = 'update';
$whereclause = self::GenerateWhereClause($data, $ddatabase, $dtable);
} else {
$cmd = 'insert';
}
foreach($data as $k => $v) {
if ((substr($k, 0, 2) != '__') && ($k != $skipkey)) { // all field names beginning with '__' are ignored
if (!isset($VARIABLE["$k.STT"]) || $cmd != 'update') { // skip fields that have not changed, if that feature is being used.
self::_updateField($values, $k, self::formatString($v, 0));
}
}
}
if ($whereclause != '') $whereclause = " where $whereclause";
$this->lastsql = "$cmd $tablename set $values$whereclause";
if (!($result = mysql_query($this->lastsql, $this->dbobj))) {
$this->errnum = mysql_errno($this->dbobj);
$this->error = "$cmd error: " . mysql_error($this->dbobj);
return 0;
}
if ($skipkey !='') $data[$primarykey] = mysql_insert_id($this->dbobj);
$data['__existing'] = 1;
return 1;
}
private function loadRecord($row, $tablename) {
$res = array();
$res['__table'] = $tablename;
$res['__existing'] = 0;
if ($row !== NULL) {
$res['__existing'] = 1;
foreach($row as $k => $v) {
if (!is_numeric($k)) {
$k = strtolower($k);
$res[$k] = $v;
}
}
}
return $res;
}
// Perform a sql query.
// $sql query to be executed. '%s' and '%d' should be used as placeholders. Cannot contain the keyword "limit"
// $sqlparam array of values (or NULL) for the placeholders in $sql
// $itemproc callback routine, executed for each record returned by the query
// $procparam a general purpose parameter, can be used by $itemproc
// $tablename name of the table this data belongs to. Required if the data in the table is going to be changed
// $limit max number of records to be returned. Used instead of the keyword "limit" - it is specific to mysql. M$SQL uses "top"
public function query($sql, $sqlparam, $itemproc, $procparam, $tablename = '', $limit = 0) {
$this->error = ''; $count = 0; $this->errnum = 0;
if (is_string($limit) || $limit > 0) $sql .= " limit $limit";
if ($sqlparam !== NULL) $sql = self::_perform_substitution($sql, $sqlparam);
// Check to see if the callback function exists
if ( is_array($itemproc) && is_object($itemproc[0]) ) {
$ok = method_exists($itemproc[0], $itemproc[1]) ? 2 : 0;
} else {
$ok = ($itemproc != '' && function_exists($itemproc)) ? 1 : 0;
}
if ($this->options & MYSQL_BUFFER) {
mysql_query('set SQL_BUFFER_RESULT=1', $this->dbobj);
}
$this->lastsql = $sql;
$result = mysql_query($sql, $this->dbobj);
if ($result == NULL) {
$this->errnum = mysql_errno($this->dbobj);
$this->error = 'query error: ' . mysql_error($this->dbobj);
return NULL;
}
$temp = $sql;
if (substr($temp, 0, 2) == '/*') { // remove any embedded comments
$p = strpos($temp, '*/');
$temp = trim(substr($temp, $p + 2));
}
$tok = strtoupper(strtok($temp, " \n\t"));
if ($tok == 'SELECT' || $tok == 'SHOW') {
while($row = mysql_fetch_array($result)) {
$ITEM = self::loadRecord($row, $tablename);
$count++;
if ($ok == 2) {
if ($itemproc[0]->$itemproc[1]($ITEM, $procparam)) break;
} elseif ($ok) {
if ($itemproc($ITEM, $procparam)) break;
} else {
$procparam = $ITEM;
}
}
mysql_free_result($result);
if (!$ok && !$count) $procparam = self::loadRecord(NULL, $tablename);
}
return $procparam;
}
// Lookup only returns the first record matching the criteria
public function lookup($sql, $sqlparam = NULL, $tablename = '') {
return self::query($sql, $sqlparam, '', NULL, $tablename, 1);
}
private function GenerateWhereClause(&$data, $ddatabase, $dtable) {
if (!isset($_SESSION["$ddatabase:$dtable"])) {
$_SESSION["$ddatabase:$dtable"] = self::GetTableDefinition($ddatabase,$dtable);
}
// session data missing? create it
$whereclause = '';
foreach($_SESSION["$ddatabase:$dtable"] as $field) {
if ($whereclause != '') $whereclause .= ' and ';
$value = self::formatString($data[$field], 0);
$whereclause .= "`$field`=$value";
}
return $whereclause;
}
// Prevent sql injection vulnerabilities
private function _perform_substitution($sql, $sqlparam) {
if (!is_array($sqlparam)) $sqlparam = array($sqlparam); // single item - does not have to be in an array
$p = 0;
while (($q = strpos($sql, '%', $p)) !== FALSE) {
$macro = strtolower(substr($sql, $q, 2));
$value = (count($sqlparam)) ? array_shift($sqlparam) : '';
switch($macro) {
case '%s':
$value = self::FormatString($value);
break;
case '%d':
$value = intval($value);
break;
case '%f':
$value = floatval($value);
break;
default:
$value = '';
}
$sql = substr($sql, 0, $q) . $value . substr($sql, $q + 2);
$p = $q + strlen($value);
}
return $sql;
}
private function GetTableDefinition($ddatabase, $dtable) {
// to do: ddatabase should not be ignored
$keyorder = self::Query("show keys from `$dtable`", NULL, array($this, '_idyp__keys'), array());
if (!count($keyorder)) {
echo "No primary key defined. \n";
exit;
}
return $keyorder;
}
function _idyp__keys(&$ITEM, &$param) {
if ($ITEM['key_name'] == 'PRIMARY') {
$n = intval($ITEM['seq_in_index']);
$param[$n] = strtolower($ITEM['column_name']);
}
return 0;
}
}
function dberr($DB) {
if (!empty($DB->error)) {
echo "Database Error: {$DB->error}<br>\n";
echo "Database Query: {$DB->lastsql}<br>\n";
exit;
}
}