140 lines
4.2 KiB
PHP
140 lines
4.2 KiB
PHP
<?php
|
|
// File name: sqllogin.inc.php
|
|
// Description: PHP meta subroutines to obtain the SQL log-in information
|
|
// Date: 2006-02-02
|
|
// Author: imacat <imacat@pristine.com.tw>
|
|
// Copyright: Copyright (C) 2006-2007 Pristine Communications
|
|
|
|
// Set the include path
|
|
if (!defined("INCPATH_SET")) {
|
|
require_once dirname(__FILE__) . "/incpath.inc.php";
|
|
}
|
|
// Referenced subroutines
|
|
require_once "monica/sqlconst.inc.php";
|
|
|
|
// get_sql_login_info: Obtain the SQL log-in information
|
|
function get_sql_login_info($type, $database = null, $host = null, $user = null)
|
|
{
|
|
// DBMS naming convension
|
|
switch ($type) {
|
|
case SQL_POSTGRESQL:
|
|
$names = array("PGHOST", "PGUSER", "PGPASSWORD", "PGDATABASE");
|
|
break;
|
|
case SQL_MYSQL:
|
|
$names = array("MYSQL_HOST", "MYSQL_USER", "MYSQL_PW", "MYSQL_DB");
|
|
break;
|
|
}
|
|
// Initialize the return values
|
|
$login = array(
|
|
$names[0] => null,
|
|
$names[1] => null,
|
|
$names[2] => null,
|
|
$names[3] => null,
|
|
);
|
|
|
|
// Obtain the SQL log-in information from the environment
|
|
$sqllogin = getenv("SQLLOGIN");
|
|
if ($sqllogin !== false) {
|
|
// Parse the SQL log-in information
|
|
// The first matched line is used, so put the default first
|
|
$sqllogin = base64_decode($sqllogin);
|
|
// Find all the matching records
|
|
$matches = array();
|
|
$lineno = 0;
|
|
$lines = explode("\n", $sqllogin);
|
|
foreach ($lines as $line) {
|
|
$cols = explode("\t", $line);
|
|
if (count($cols) == 4) {
|
|
$cols[] = "";
|
|
}
|
|
// Skip malformed lines
|
|
if (count($cols) != 5) {
|
|
continue;
|
|
}
|
|
// Not this DBMS type
|
|
if ($cols[0] != $type) {
|
|
continue;
|
|
}
|
|
for ($i = 0; $i < count($cols); $i++) {
|
|
if ($cols[$i] == "") {
|
|
$cols[$i] = null;
|
|
}
|
|
}
|
|
$count = 0;
|
|
$score = 0;
|
|
// Match this database?
|
|
if (is_null($database) || is_null($cols[4])) {
|
|
} elseif ($cols[4] == $database) {
|
|
$count++;
|
|
$score += 3;
|
|
} else {
|
|
continue;
|
|
}
|
|
// Match this user?
|
|
if (is_null($user) || is_null($cols[2])) {
|
|
} elseif ($cols[2] == $user) {
|
|
$count++;
|
|
$score += 2;
|
|
} else {
|
|
continue;
|
|
}
|
|
// Match this host?
|
|
if (is_null($host) || is_null($cols[1])) {
|
|
} elseif ($cols[1] == $host) {
|
|
$count++;
|
|
$score += 1;
|
|
} else {
|
|
continue;
|
|
}
|
|
$matches[] = array(
|
|
"host" => $cols[1],
|
|
"user" => $cols[2],
|
|
"password" => $cols[3],
|
|
"database" => $cols[4],
|
|
"matches" => $count,
|
|
"score" => $score,
|
|
"lineno" => ++$lineno,
|
|
);
|
|
}
|
|
if (count($matches) > 0) {
|
|
usort($matches, "_sqllogin_sort_matches");
|
|
$login[$names[0]] = $matches[0]["host"];
|
|
$login[$names[1]] = $matches[0]["user"];
|
|
$login[$names[2]] = $matches[0]["password"];
|
|
$login[$names[3]] = $matches[0]["database"];
|
|
}
|
|
}
|
|
|
|
// Apply the specified information
|
|
if (!is_null($database) && is_null($login[$names[3]])) {
|
|
$login[$names[3]] = $database;
|
|
}
|
|
if (!is_null($host) && is_null($login[$names[0]])) {
|
|
$login[$names[0]] = $host;
|
|
}
|
|
if (!is_null($user) && is_null($login[$names[1]])) {
|
|
$login[$names[1]] = $user;
|
|
}
|
|
|
|
// PostgreSQL database name cannot be empty
|
|
if ($type == SQL_POSTGRESQL && is_null($login[$names[3]])) {
|
|
$login[$names[3]] = "test";
|
|
}
|
|
|
|
return $login;
|
|
}
|
|
|
|
// _sqllogin_sort_matches: sort the matching lines
|
|
function _sqllogin_sort_matches($a, $b)
|
|
{
|
|
if ($a["matches"] != $b["matches"]) {
|
|
return $b["matches"] - $a["matches"];
|
|
}
|
|
if ($a["score"] != $b["score"]) {
|
|
return $b["score"] - $a["score"];
|
|
}
|
|
return $a["lineno"] - $b["lineno"];
|
|
}
|
|
|
|
?>
|