//* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'file=' . basename($file) . ',line=' . $line . ',sql=' . $GLOBALS['last_sql']);
$result = mysql_query($GLOBALS['last_sql'], getSqlLink())
or sqlError($file, $line, 'file='. basename($file) . ',line=' . $line . ':mysql_error()=' . mysql_error() . ',last_query=' . $GLOBALS['last_sql']);
- //* DEBUG: */ logDebugMessage($file, $line, 'sql=' . $GLOBALS['last_sql'] . ',affected=' . sqlAffectedRows() . ',numRows='.(is_resource($result) ? sqlNumRows($result) : gettype($result)));
+ //* DEBUG: */ logDebugMessage($file, $line, 'sql=' . $GLOBALS['last_sql'] . ',affected=' . sqlAffectedRows() . ',numRows='.(isValidSqlLink($result) ? sqlNumRows($result) : gettype($result)));
// Calculate query time
$queryTime = microtime(TRUE) - $querytimeBefore;
} // END - if
// Append debug line
- appendLineToFile(getCachePath() . 'mysql.log', basename($file) . '|LINE=' . $line . '|NUM=' . (is_resource($result) ? sqlNumRows($result) : 'false') . '|AFFECTED=' . sqlAffectedRows() . '|QUERYTIME:' . ($queryTime * 1000) . 'ms): ' . str_replace(array(chr(13), PHP_EOL), array('', ' '), $GLOBALS['last_sql']));
+ appendLineToFile(getCachePath() . 'mysql.log', basename($file) . '|LINE=' . $line . '|NUM=' . (isValidSqlLink($result) ? sqlNumRows($result) : 'false') . '|AFFECTED=' . sqlAffectedRows() . '|QUERYTIME:' . ($queryTime * 1000) . 'ms): ' . str_replace(array(chr(13), PHP_EOL), array('', ' '), $GLOBALS['last_sql']));
} // END - if
// Increment stats entry
if (isset($GLOBALS['sql_numrows'][intval($resource)])) {
// Use cache
$lines = $GLOBALS['sql_numrows'][intval($resource)];
- } elseif (is_resource($resource)) {
+ } elseif (isValidSqlLink($resource)) {
// Get the count of rows from database
$lines = mysql_num_rows($resource);
// SQL fetch row
function sqlFetchRow ($resource) {
// Is $resource valid?
- if ((!is_resource($resource)) || (!isSqlLinkUp())) return FALSE;
+ if ((!isValidSqlLink($resource)) || (!isSqlLinkUp())) return FALSE;
// Fetch the data and return it
return mysql_fetch_row($resource);
// SQL fetch array
function sqlFetchArray ($resource) {
// Is $resource valid?
- if ((!is_resource($resource)) || (!isSqlLinkUp())) return FALSE;
+ if ((!isValidSqlLink($resource)) || (!isSqlLinkUp())) return FALSE;
// Load row as array from database
$row = mysql_fetch_assoc($resource);
// SQL result
function sqlResult ($resource, $row, $field = '0') {
// Is $resource valid?
- if ((!is_resource($resource)) || (!isSqlLinkUp())) return FALSE;
+ if ((!isValidSqlLink($resource)) || (!isSqlLinkUp())) return FALSE;
// Run the result command
$result = mysql_result($resource, $row, $field);
$linkResource = mysql_connect($host, $login, $password) or sqlError($file, $line, mysql_error());
// Set the link resource
- if (is_resource($linkResource)) {
+ if (isValidSqlLink($linkResource)) {
/*
* A non-resource (boolean) may happen on installation phase which
* shall not be set here. Only valid link resources shall be set so
// SQL free result
function sqlFreeResult ($resource) {
- if ((!is_resource($resource)) || (!isSqlLinkUp())) {
+ if ((!isValidSqlLink($resource)) || (!isSqlLinkUp())) {
// Abort here
return FALSE;
} // END - if
}
}
+// Checks whether given link is a valid SQL link
+function isValidSqlLink ($linkResource) {
+ // Is it a resource?
+ return is_resource($linkResource);
+}
+
// [EOF]
?>
--- /dev/null
+<?php
+/************************************************************************
+ * Mailer v0.2.1-FINAL Start: 06/12/2013 *
+ * =================== Last change: 06/12/2013 *
+ * *
+ * -------------------------------------------------------------------- *
+ * File : lib-mysqli.php *
+ * -------------------------------------------------------------------- *
+ * Short description : Database layer for MySQL 3/4/5 server (MySQLi) *
+ * -------------------------------------------------------------------- *
+ * Kurzbeschreibung : Datenbankschicht fuer MySQL 3/4/5 Server (MySQLi)*
+ * -------------------------------------------------------------------- *
+ * $Revision:: $ *
+ * $Date:: $ *
+ * $Tag:: 0.2.1-FINAL $ *
+ * $Author:: $ *
+ * -------------------------------------------------------------------- *
+ * Copyright (c) 2003 - 2009 by Roland Haeder *
+ * Copyright (c) 2009 - 2013 by Mailer Developer Team *
+ * For more information visit: http://mxchange.org *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ * *
+ * This program is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
+ * GNU General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU General Public License *
+ * along with this program; if not, write to the Free Software *
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
+ * MA 02110-1301 USA *
+ ************************************************************************/
+
+// Some security stuff...
+if (!defined('__SECURITY')) {
+ die();
+} // END - if
+
+// SQL queries
+function sqlQuery ($sqlString, $file, $line, $enableCodes = TRUE) {
+ // Is there cache?
+ if (!isset($GLOBALS[__FUNCTION__][$sqlString])) {
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Called: ' . $sqlString);
+
+ // Trim SQL string
+ $sqlStringModified = trim($sqlString);
+
+ // Empty query string or link is not up?
+ if (empty($sqlStringModified)) {
+ // Empty SQL string!
+ reportBug(__FUNCTION__, __LINE__, sprintf('SQL string is empty, please fix this: file=%s, line=%s',
+ basename($file),
+ $line
+ ));
+ } elseif (!isSqlLinkUp()) {
+ // We should not quietly ignore this
+ reportBug(__FUNCTION__, __LINE__, sprintf('Cannot query database: sqlString=%s,file=%s,line=%s',
+ $sqlStringModified,
+ basename($file),
+ $line
+ ));
+ }
+
+ // Remove \t, \n and \r from queries they may confuse some MySQL versions
+ $sqlStringModified = str_replace(array(chr(9), PHP_EOL, chr(13)), array(' ', ' ', ' '), $sqlStringModified);
+
+ // Compile config entries out
+ $sqlStringModified = sqlPrepareQueryString($sqlStringModified, $enableCodes);
+
+ // Cache it and remember as last SQL query
+ $GLOBALS[__FUNCTION__][$sqlString] = $sqlStringModified;
+ $GLOBALS['last_sql'] = $sqlStringModified;
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Stored cache: ' . $sqlStringModified);
+ } elseif (!isSqlLinkUp()) {
+ // Link went down while using cached SQL
+ reportBug(__FUNCTION__, __LINE__, 'Link went down while using cached SQL: sqlString=' . $sqlString . ',file=' . basename($file) . ',line=' . $line . ',enableCodes=' . intval($enableCodes));
+ } else {
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Cache used: ' . $sqlString);
+
+ // Use cache (to save a lot function calls
+ $GLOBALS['last_sql'] = $GLOBALS[__FUNCTION__][$sqlString];
+
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Cache is: ' . $sqlString);
+ }
+
+ // Starting time
+ $querytimeBefore = microtime(TRUE);
+
+ // Run SQL command
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'file=' . basename($file) . ',line=' . $line . ',sql=' . $GLOBALS['last_sql']);
+ $result = mysqli_query(getSqlLink(), $GLOBALS['last_sql'])
+ or sqlError($file, $line, 'file='. basename($file) . ',line=' . $line . ':mysqli_error()=' . mysqli_error() . ',last_query=' . $GLOBALS['last_sql']);
+ //* DEBUG: */ logDebugMessage($file, $line, 'sql=' . $GLOBALS['last_sql'] . ',affected=' . sqlAffectedRows() . ',numRows='.(isValidSqlLink($result) ? sqlNumRows($result) : gettype($result)));
+
+ // Calculate query time
+ $queryTime = microtime(TRUE) - $querytimeBefore;
+
+ // Add this query to array including timing
+ addSqlToDebug($result, $GLOBALS['last_sql'], $queryTime, $file, $line);
+
+ // Save last successfull query
+ setConfigEntry('db_last_query', $GLOBALS['last_sql']);
+
+ // Count all query times
+ incrementConfigEntry('sql_time', $queryTime);
+
+ // Count this query
+ incrementConfigEntry('sql_count');
+
+ // Debug output
+ if (isSqlDebugEnabled()) {
+ // Is this the first call?
+ if (!isset($GLOBALS['sql_first_entry'])) {
+ // Write first entry
+ appendLineToFile(getCachePath() . 'mysql.log', 'Module=' . getModule());
+ $GLOBALS['sql_first_entry'] = TRUE;
+ } // END - if
+
+ // Append debug line
+ appendLineToFile(getCachePath() . 'mysql.log', basename($file) . '|LINE=' . $line . '|NUM=' . (isValidSqlLink($result) ? sqlNumRows($result) : 'false') . '|AFFECTED=' . sqlAffectedRows() . '|QUERYTIME:' . ($queryTime * 1000) . 'ms): ' . str_replace(array(chr(13), PHP_EOL), array('', ' '), $GLOBALS['last_sql']));
+ } // END - if
+
+ // Increment stats entry
+ incrementStatsEntry('db_hits');
+
+ // Return the result
+ return $result;
+}
+
+// SQL num rows
+function sqlNumRows ($resource) {
+ // Valid link resource?
+ if (!isSqlLinkUp()) return FALSE;
+
+ // Get the count of rows from database
+ $lines = mysqli_num_rows($resource);
+
+ // Return lines
+ return $lines;
+}
+
+// SQL affected rows
+function sqlAffectedRows () {
+ // Valid link resource?
+ if (!isSqlLinkUp()) return FALSE;
+
+ // Get affected rows
+ $lines = mysqli_affected_rows(getSqlLink());
+
+ // Return it
+ return $lines;
+}
+
+// SQL fetch row
+function sqlFetchRow ($resource) {
+ // Is $resource valid?
+ if ((!isValidSqlLink($resource)) || (!isSqlLinkUp())) return FALSE;
+
+ // Fetch the data and return it
+ return mysqli_fetch_row($resource);
+}
+
+// SQL fetch array
+function sqlFetchArray ($resource) {
+ // Is $resource valid?
+ if ((!isValidSqlLink($resource)) || (!isSqlLinkUp())) return FALSE;
+
+ // Load row as array from database
+ $row = mysqli_fetch_assoc($resource);
+
+ // Return only arrays here
+ if (is_array($row)) {
+ // Return row
+ return $row;
+ } else {
+ // Return a false, else some loops would go endless...
+ return FALSE;
+ }
+}
+
+// SQL result
+function sqlResult ($resource, $row, $field = '0') {
+ // Is $resource valid?
+ if ((!isValidSqlLink($resource)) || (!isSqlLinkUp())) return FALSE;
+
+ // Run the result command
+ $result = mysqli_result($resource, $row, $field);
+
+ // ... and return the result
+ return $result;
+}
+
+// SQL connect
+function sqlConnectToDatabase ($host, $login, $password, $file, $line) {
+ // Try to connect
+ $linkResource = mysqli_connect($host, $login, $password) or sqlError($file, $line, mysqli_error());
+
+ // Set the link resource
+ if ($linkResource instanceof mysqli) {
+ /*
+ * A non-resource (boolean) may happen on installation phase which
+ * shall not be set here. Only valid link resources shall be set so
+ * isSqlLinkUp() will only return 'true' if there is really a
+ * working database link.
+ */
+ setSqlLink($file . ':' . __FUNCTION__, $line . ':' . __LINE__, $linkResource);
+
+ // Init charsets (UTF-8 is default now)
+ sqlQuery("SET
+ `character_set_results`='utf8',
+ `character_set_client`='utf8',
+ `character_set_connection`='utf8',
+ `character_set_database`='utf8',
+ `character_set_server`='utf8'", $file . ':' . __FUNCTION__, $line . ':' . __LINE__);
+ } // END - if
+
+ // Any errors encountered?
+ if (mysqli_connect_error()) {
+ // Something went horrible wrong
+ reportBug($file . ':' . __FUNCTION__, $line . ':' . __LINE__, 'Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error());
+ } // END - if
+
+ // Return the resource
+ //* DEBUG: */ logDebugMessage($file . ':' . __FUNCTION__, $line . ':' . __LINE__, 'linkResource[]=' . gettype($linkResource));
+ return $linkResource;
+}
+
+// SQL select database
+function sqlSelectDatabase ($dbName, $file, $line) {
+ // Is there still a valid link? If not, skip it.
+ if (!isSqlLinkUp()) return FALSE;
+
+ // Return the result
+ //* DEBUG: */ logDebugMessage($file . ':' . __FUNCTION__, $line . ':' . __LINE__, 'Selecting database ' . $dbName);
+ return mysqli_select_db(getSqlLink(), $dbName) or sqlError($file, $line, mysqli_error());
+}
+
+// SQL close link
+function sqlCloseLink ($file, $line) {
+ // Is the link up?
+ if (!isSqlLinkUp()) {
+ // Skip double close
+ //* DEBUG: */ logDebugMessage($file . ':' . __FUNCTION__, $line . ':' . __LINE__, 'Called but no link is open.');
+ return FALSE;
+ } // END - if
+
+ // Close database link and forget the link
+ $close = mysqli_close(getSqlLink()) or sqlError($file . ':' . __FUNCTION__, $line . ':' . __LINE__, mysqli_error());
+
+ // Close link in this layer
+ unsetSqlLinkUp(__FUNCTION__, __LINE__);
+
+ // Return the result
+ //* DEBUG: */ logDebugMessage($file . ':' . __FUNCTION__, $line . ':' . __LINE__, 'close[' . gettype($close) . ']=' . intval($close));
+ return $close;
+}
+
+// SQL free result
+function sqlFreeResult ($resource) {
+ if ((!isValidSqlLink($resource)) || (!isSqlLinkUp())) {
+ // Abort here
+ return FALSE;
+ } // END - if
+
+ // Free result
+ $res = mysqli_free_result($resource);
+
+ // And return that result of freeing it...
+ return $res;
+}
+
+// Get id from last INSERT command and secure id
+function getSqlInsertId () {
+ if (!isSqlLinkUp()) return FALSE;
+ return bigintval(mysqli_insert_id());
+}
+
+// Escape a string for the database
+function sqlEscapeString ($str, $secureString = TRUE, $strip = TRUE) {
+ // Is there cache?
+ if (!isset($GLOBALS['sql_escapes']['' . $str . ''])) {
+ // Debug message
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'str=' . $str . ' - BEFORE!');
+
+ // Prepare the string here
+ $str = sqlPrepareQueryString($str);
+
+ // Debug message
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'str=' . $str . ' - AFTER!');
+
+ // Secure string first? (which is the default behaviour!)
+ if ($secureString === TRUE) {
+ // Debug message
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'str=' . $str . ',strip=' . intval($strip) . ' - BEFORE!');
+
+ // Then do it here
+ $str = secureString($str, $strip);
+
+ // Debug message
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'str=' . $str . ',strip=' . intval($strip) . ' - AFTER!');
+ } // END - if
+
+ // Init (invalid) value
+ $ret = '!INVALID!';
+
+ if (!isSqlLinkUp()) {
+ // Fall-back to escapeQuotes() when there is no link
+ $ret = escapeQuotes($str);
+ } elseif (function_exists('mysqli_real_escape_string')) {
+ // Debug message
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'str=' . $str);
+
+ // The new and improved version
+ $ret = mysqli_real_escape_string(getSqlLink(), $str);
+
+ // Debug message
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'str=' . $str . ',ret=' . $ret);
+ } else {
+ // If nothing else works, fall back to escapeQuotes() again
+ $ret = escapeQuotes($str);
+ }
+
+ // Log message
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'str=' . $str . ',ret=' . $ret);
+
+ // Cache result
+ $GLOBALS['sql_escapes']['' . $str . ''] = $ret;
+ } // END - if
+
+ // Log message
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'str=' . $str . ',sql_escapes=' . $GLOBALS['sql_escapes']['' . $str . '']);
+
+ // Return it
+ return $GLOBALS['sql_escapes']['' . $str . ''];
+}
+
+// Log SQL errors to debug.log in installation phase or call reportBug()
+function sqlError ($file, $line, $message) {
+ // Remember plain error in last_sql_error
+ $GLOBALS['last_sql_error'] = mysqli_error();
+
+ // Is login set?
+ if (!empty($GLOBALS['mysql']['login'])) {
+ // Secure login name in message
+ $message = str_replace($GLOBALS['mysql']['login'], '***', $message);
+ } // END - if
+
+ // Is database password set?
+ if (!empty($GLOBALS['mysql']['password'])) {
+ // Secure password in message
+ $message = str_replace($GLOBALS['mysql']['password'], '***', $message);
+ } // END - if
+
+ // Is database name set?
+ if (!empty($GLOBALS['mysql']['dbase'])) {
+ // Secure database name in message
+ $message = str_replace($GLOBALS['mysql']['dbase'], '***', $message);
+ } // END - if
+
+ // Is there installation phase?
+ if (isInstaller()) {
+ /*
+ * In installation phase, we don't want SQL errors abort e.g. connection
+ * tests, so just log it away.
+ */
+ logDebugMessage($file, $line, $message);
+ } else {
+ // Regular mode, then call reportBug()
+ reportBug($file, $line, $message);
+ }
+}
+
+// Checks whether given link is a valid SQL link
+function isValidSqlLink ($linkInstance) {
+ // Is it a resource?
+ $isValid = (($linkInstance instanceof mysqli) || ($linkInstance instanceof mysqli_result));
+
+ // Debug message
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'linkInstance[]=' . gettype($linkInstance). ',isValid=' . intval($isValid));
+
+ // Return result
+ return $isValid;
+}
+
+// [EOF]
+?>
--- /dev/null
+<?xml version="1.0" encoding="UTF-8" ?>
+<!--
+This template provides meta data for listing API cache entries
+
+@author Roland Haeder <webmaster@mxchange.org>
+@version 0.2.1-FINAL
+@copyright (c) 2003 - 2009 by Roland Haeder
+@copyright (c) 2009 - 2013 by Mailer Developer Team
+@license GNU GPL 2.0 or any newer version
+@link http://mxchange.org
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
+MA 02110-1301 USA
+//-->
+<admin-list-data>
+ <!--
+ Call-back function, the extracted data from this XML will then be re-read
+ from that function. The most common function this XML uses is
+ doGenericListEntries() so mostly you can leave this alone unless you have JOINs.
+ //-->
+ <callback-function type="string" value="doGenericListEntries" />
+ <!--
+ Now we need information which template should be loaded and which functions
+ shall be called back. So lets start with the main list template. This mostly
+ requires no call-back function.
+ //-->
+ <list-template type="string" value="admin_list_network_cache" />
+ <!--
+ The template for all rows, mostly this name has a '_row' suffix and again,
+ no call-back function is usually required.
+ //-->
+ <list-row-template type="string" value="admin_list_network_cache_row" />
+ <!--
+ Message id to display if no entry could be found
+ //-->
+ <no-entry-found-message type="string" value="ADMIN_NETWORK_API_CACHE_404" />
+ <!--
+ The table(s) we shall grab the data from, all as list.
+ //-->
+ <data-tables>
+ <!--
+ A single table entry, with alias and without the configurable
+ _MYSQL_PREFIX. You can specify the table's name or alias later on.
+ //-->
+ <data-table type="string" alias="" value="network_cache" />
+ <!--
+ Columns to perform the SELECT statement on, with alias and name plus which table.
+ //-->
+ <select-data-from-list>
+ <!--
+ A single entry with table name (can be left empty), value (must
+ always be set), alias (again can be left empty) and SQL function
+ (can also be left empty) which shall be applied on the column.
+ //-->
+ <select-data-from-list-entry type="string" table="" value="network_cache_id" alias="" function="" />
+ <select-data-from-list-entry type="string" table="" value="network_type_id" alias="" function="" />
+ <select-data-from-list-entry type="string" table="" value="network_cache_data" alias="" function="" />
+ <select-data-from-list-entry type="string" table="" value="network_cache_body" alias="" function="" />
+ <select-data-from-list-entry type="string" table="" value="network_cache_type" alias="" function="" />
+ <select-data-from-list-entry type="string" table="" value="network_cache_added" alias="network_cache_added" function="UNIX_TIMESTAMP" />
+ <select-data-from-list-entry type="string" table="" value="network_cache_updated" alias="network_cache_updated" function="UNIX_TIMESTAMP" />
+ <select-data-from-list-entry type="string" table="" value="network_cache_admin_id" alias="" function="" />
+ </select-data-from-list>
+ <!--
+ And the column list to perform the WHERE statement on.
+ //-->
+ <where-select-from-list>
+ </where-select-from-list>
+ <!--
+ Columns to perform the ORDER BY statement (GROUP BY is not yet supported)
+ //-->
+ <order-by-list>
+ <!--
+ A single entry to perform the ORDER BY statement on, see above WHERE entry for details.
+ //-->
+ <order-by-list-entry type="string" table="" order="network_cache_updated" value="DESC" />
+ <order-by-list-entry type="string" table="" order="network_cache_added" value="DESC" />
+ </order-by-list>
+ </data-tables>
+ <!--
+ List all column names from the 'select-data-from-list' node here, but now
+ with call-back informations. The list must only contain those entries where
+ a call-back function shall be called for.
+ //-->
+ <column-callback-list>
+ </column-callback-list>
+ <!--
+ How the named column shall be logical linked to the above.
+ //-->
+ <where-condition type="string" name="" condition="" />
+</admin-list-data>