📄 db.php
字号:
<?## db.php - Database functions, data manipulation functions# and generic post/get handling## Dan Huby (dan@montala.net) for Oxfam, April 2006# Set larger time limitset_time_limit(120);# ensure no caching (dynamic site)header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the pastheader("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modifiedheader("Cache-Control: max-age=0"); // HTTP/1.1#header("Cache-Control: post-check=0, pre-check=0", false);#header("Pragma: no-cache"); # Error handlingfunction errorhandler($errno, $errstr, $errfile, $errline) { if (error_reporting()) { ?> </select></table></table></table> <div style="border:1px solid black;font-family:verdana,arial,helvetica;position:absolute;top:100px;left:100px; background-color:white;width:400px;padding:20px;border-bottom-width:4px;border-right-width:4px;font-size:15px;color:black;"> <table cellpadding=5 cellspacing=0><tr><td valign=middle><img src="admin/gfx/cherrybomb.gif"></td><td valign=middle align=left>Sorry, an error has occured.</td></tr></table> <p style="font-size:11px;color:black;">Please <a href="#" onClick="history.go(-1)">go back</a> and try something else.</p> <p style="font-size:11px;color:black;">You can <a href="check.php">check</a> your installation configuration.</p> <hr style="margin-top:20px;"><p style="font-size:11px;color:black;"><? echo "$errfile line $errline: $errstr"; ?></p> </div> <? # Uncomment next line to send e-mail with error details. Useful for debug. # mail ("errors@montala.net","Error", $_SERVER["REQUEST_URI"] . "\n$errfile line $errline: $errstr\n\n\nDumping SERVER:\n" . print_r($_SERVER,true) . "\n\nDumping ENVIRONMENT:\n" . print_r($_ENV,true) . "\n\nDumping GET:\n" . print_r($_GET,true) . "\n\nDumping POST:\n" . print_r($_POST,true)); exit(); } }error_reporting(E_ALL);set_error_handler("errorhandler");# Set some defaults$infobox=true;# *** LOAD CONFIG ***# Load the default config first, if it exists, so any new settings are present even if missing from config.phpif (file_exists(dirname(__FILE__)."/config.default.php")) {include "config.default.php";}# Load the real configif (!file_exists(dirname(__FILE__)."/config.php")) {exit("You must copy 'config.default.php' to 'config.php' in the include directory, and edit the file to alter the settings as appropriate.");}include "config.php";# *** CONNECT TO DATABASE ***mysql_connect($mysql_server,$mysql_username,$mysql_password);mysql_select_db($mysql_db);set_magic_quotes_runtime(0);# statistics$querycount=0;$querytime=0;$queryhist="";# -----------LANGUAGES AND PLUGINS-------------------------------# Include the appropriate language file$pagename=str_replace(".php","",pagename());if (isset($defaultlanguage)) {$language=$defaultlanguage;} else {$language="en";}if (isset($_COOKIE["language"])) {$language=$_COOKIE["language"];}# Always include the english pack (in case items have not yet been translated)include dirname(__FILE__)."/../languages/en.php";if ($language!="en") { include dirname(__FILE__)."/../languages/" . $language . ".php"; }/*if ($config_pluginmanager_enabled){ unset($plugins); include dirname(__FILE__)."/.././dynamic/plugins.php";}*/# Include language files for for each of the plugins too (if provided)for ($n=0;$n<count($plugins);$n++) { $langpath=dirname(__FILE__)."/../plugins/" . $plugins[$n] . "/languages/"; if (file_exists($langpath . "en.php")) {include $langpath . "en.php";} if ($language!="en") { if (file_exists($langpath . $language . ".php")) {include $langpath . $language . ".php";} } # Also include plugin configuration. $configpath=dirname(__FILE__)."/../plugins/" . $plugins[$n] . "/config/config.php"; if (file_exists($configpath)) {include $configpath;} # Also include plugin hook file for this page. $hookpath=dirname(__FILE__)."/../plugins/" . $plugins[$n] . "/hooks/" . $pagename . ".php"; if (file_exists($hookpath)) {include $hookpath;} # Support an 'all' hook $hookpath=dirname(__FILE__)."/../plugins/" . $plugins[$n] . "/hooks/all.php"; if (file_exists($hookpath)) {include $hookpath;} }# Set character set.if (($pagename!="download") && ($pagename!="graph")) {header("Content-Type: text/html; charset=UTF-8");} // Make sure we're using UTF-8.#------------------------------------------------------# Pre-load all text for this page.$site_text=array();$results=sql_query("select language,name,text from site_text where (page='$pagename' or page='all') and (specific_to_group is null or specific_to_group=0)");for ($n=0;$n<count($results);$n++) {$site_text[$results[$n]["language"] . "-" . $results[$n]["name"]]=$results[$n]["text"];}# Blank the header insert$headerinsert="";# Initialise hook for pluginshook("initialise");function hook($name,$pagename="") { # Plugin architecture. Look for a hook with this name and execute. if ($pagename=="") {global $pagename;} # If page name not provided, use global page name. global $plugins; $found=false; for ($n=0;$n<count($plugins);$n++) { # "All" hooks $function="Hook" . ucfirst($plugins[$n]) . "All" . ucfirst($name); if (function_exists($function)) { # Function must return 'true' if successful (so existing functionality is replaced) $found=eval ("return " . $function . "();"); } # Specific hook $function="Hook" . ucfirst($plugins[$n]) . ucfirst($pagename) . ucfirst($name); if (function_exists($function)) { # Function must return 'true' if successful (so existing functionality is replaced) $found=eval ("return " . $function . "();"); } } return $found; }function sql_query($sql,$cache=false,$fetchrows=-1,$dbstruct=true) { # sql_query(sql) - execute a query and return the results as an array. # Database functions are wrapped in this way so supporting a database server other than MySQL is # easier. # $cache is not used at this time - it was intended for disk based results caching which may be added in the future. # If $fetchrows is set we don't have to loop through all the returned rows. We # just fetch $fetchrows row but pad the array to the full result set size with empty values. # This has been added retroactively to support large result sets, yet a pager can work as if a full # result set has been returned as an array (as it was working previously). global $db,$querycount,$querytime,$queryhist; $querycount++; #$queryhist.=$sql . "\n\n\n";# stats $counter=0; $time_start = microtime(true); $result=mysql_query($sql); $error=mysql_error(); if ($error!="") { if ($error=="Server shutdown in progress") { echo "<span class=error>Sorry, but this query would return too many results. Please try refining your query by adding addition keywords or search parameters.<!--$sql--></span>"; } else { # Check that all database tables and columns exist using the files in the 'dbstruct' folder. if ($dbstruct) # should we do this? { CheckDBStruct("dbstruct"); global $plugins; for ($n=0;$n<count($plugins);$n++) { CheckDBStruct("plugins/" . $plugins[$n] . "/dbstruct"); } # Try again (no dbstruct this time to prevent an endless loop) return sql_query($sql,$cache,$fetchrows,false); exit(); } errorhandler("N/A", $error . "<br/><br/>" . $sql, "(database)", "N/A"); } exit; } elseif ($result===true) { # no result set, (query was insert, update etc.) $time_end = microtime(true); $querytime += ($time_end - $time_start); } else { $row=array(); while (($rs=mysql_fetch_array($result)) && (($counter<$fetchrows) || ($fetchrows==-1))) { while (list($name,$value)=each($rs)) { if (!is_integer($name)) # do not run for integer values (MSSQL returns two keys for each returned column, a numeric and a text) { $row[$counter][$name]=str_replace("\\","",stripslashes($value)); } } $counter++; } $time_end = microtime(true); $querytime += ($time_end - $time_start); # If we haven't returned all the rows ($fetchrows isn't -1) then we need to fill the array so the count # is still correct (even though these rows won't be shown). $rows=count($row); $totalrows=mysql_num_rows($result);#echo "-- $rows out of $totalrows --"; if (($fetchrows!=-1) && ($rows<$totalrows)) {$row=array_pad($row,$totalrows,0);} return $row; } } function sql_value($query,$default) { # return a single value from a database query, or the default if no rows # The value returned must have the column name aliased to 'value' $result=sql_query($query); if (count($result)==0) {return $default;} else {return $result[0]["value"];} }function sql_array($query) { # Like sql_value() but returns an array of all values found. # The value returned must have the column name aliased to 'value' $return=array(); $result=sql_query($query); for ($n=0;$n<count($result);$n++) { $return[]=$result[$n]["value"]; } return $return; }function sql_insert_id() { # Return last inserted ID (abstraction) return mysql_insert_id(); }function CheckDBStruct($path) { # Check the database structure against the text files stored in $path. # Add tables / columns / data / indices as necessary. global $mysql_db; # Check for path $path=dirname(__FILE__) . "/../" . $path; # Make sure this works when called from non-root files.. if (!file_exists($path)) {return false;} # Tables first. # Load existing tables list $ts=sql_query("show tables"); $tables=array(); for ($n=0;$n<count($ts);$n++) { $tables[]=$ts[$n]["Tables_in_" . $mysql_db]; } $dh=opendir($path); while (($file = readdir($dh)) !== false) { if (substr($file,0,6)=="table_") { $table=str_replace(".txt","",substr($file,6)); # Check table exists if (!in_array($table,$tables)) { # Create Table $sql=""; $f=fopen($path . "/" . $file,"r"); while (($col = fgetcsv($f,5000)) !== false) { if ($sql.="") {$sql.=", ";} $sql.=$col[0] . " " . $col[1]; if ($col[4]!="") {$sql.=" default " . $col[4];} if ($col[3]=="PRI") {$sql.=" primary key";}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -