2012-06-04 16:58:40 +02:00
|
|
|
<?php
|
|
|
|
|
/*
|
|
|
|
|
FusionPBX
|
|
|
|
|
Version: MPL 1.1
|
|
|
|
|
|
|
|
|
|
The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
|
1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
|
the License. You may obtain a copy of the License at
|
|
|
|
|
http://www.mozilla.org/MPL/
|
|
|
|
|
|
|
|
|
|
Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
|
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
|
for the specific language governing rights and limitations under the
|
|
|
|
|
License.
|
|
|
|
|
|
|
|
|
|
The Original Code is FusionPBX
|
|
|
|
|
|
|
|
|
|
The Initial Developer of the Original Code is
|
|
|
|
|
Mark J Crane <markjcrane@fusionpbx.com>
|
2017-01-05 04:40:36 +01:00
|
|
|
Portions created by the Initial Developer are Copyright (C) 2008-2016
|
2012-06-04 16:58:40 +02:00
|
|
|
the Initial Developer. All Rights Reserved.
|
|
|
|
|
|
|
|
|
|
Contributor(s):
|
|
|
|
|
Mark J Crane <markjcrane@fusionpbx.com>
|
|
|
|
|
Raymond Chandler <intralanman@gmail.com>
|
|
|
|
|
*/
|
2017-01-05 04:40:36 +01:00
|
|
|
|
2022-10-11 00:35:14 +02:00
|
|
|
//includes files
|
2023-06-15 19:28:23 +02:00
|
|
|
require_once __DIR__ . "/require.php";
|
2017-01-05 04:40:36 +01:00
|
|
|
require_once "resources/functions.php";
|
2012-06-04 16:58:40 +02:00
|
|
|
|
|
|
|
|
//set defaults
|
2014-08-17 09:29:27 +02:00
|
|
|
if (isset($dbtype)) {
|
2014-08-17 09:09:57 +02:00
|
|
|
$db_type = $dbtype;
|
2012-06-04 16:58:40 +02:00
|
|
|
}
|
2014-08-17 09:29:27 +02:00
|
|
|
if (isset($dbhost)) {
|
2014-08-17 09:09:57 +02:00
|
|
|
$db_host = $dbhost;
|
2012-06-04 16:58:40 +02:00
|
|
|
}
|
2014-08-17 09:29:27 +02:00
|
|
|
if (isset($dbport)) {
|
2014-08-17 09:09:57 +02:00
|
|
|
$db_port = $dbport;
|
2012-06-04 16:58:40 +02:00
|
|
|
}
|
2014-08-17 09:29:27 +02:00
|
|
|
if (isset($dbname)) {
|
2014-08-17 09:09:57 +02:00
|
|
|
$db_name = $dbname;
|
2012-06-04 16:58:40 +02:00
|
|
|
}
|
2014-08-17 09:29:27 +02:00
|
|
|
if (isset($dbusername)) {
|
2014-08-17 09:09:57 +02:00
|
|
|
$db_username = $dbusername;
|
2012-06-04 16:58:40 +02:00
|
|
|
}
|
2014-08-17 09:29:27 +02:00
|
|
|
if (isset($dbpassword)) {
|
2014-08-17 09:09:57 +02:00
|
|
|
$db_password = $dbpassword;
|
2012-06-04 16:58:40 +02:00
|
|
|
}
|
2014-08-17 09:29:27 +02:00
|
|
|
if (isset($db_file_path)) {
|
2014-08-17 09:09:57 +02:00
|
|
|
$db_path = $db_file_path;
|
2012-06-04 16:58:40 +02:00
|
|
|
}
|
2014-08-17 09:29:27 +02:00
|
|
|
if (isset($dbfilename)) {
|
2014-08-17 09:09:57 +02:00
|
|
|
$db_name = $dbfilename;
|
2012-06-04 16:58:40 +02:00
|
|
|
}
|
2019-06-06 05:10:58 +02:00
|
|
|
if (isset($dbsecure)) {
|
|
|
|
|
$db_secure = $dbsecure;
|
|
|
|
|
}
|
|
|
|
|
if (isset($dbcertauthority)) {
|
|
|
|
|
$db_cert_authority = $dbcertauthority;
|
|
|
|
|
}
|
2012-06-04 16:58:40 +02:00
|
|
|
|
|
|
|
|
if (!function_exists('get_db_field_names')) {
|
|
|
|
|
function get_db_field_names($db, $table, $db_name='fusionpbx') {
|
|
|
|
|
$query = sprintf('SELECT * FROM %s LIMIT 1', $table);
|
|
|
|
|
foreach ($db->query($query, PDO::FETCH_ASSOC) as $row) {
|
|
|
|
|
return array_keys($row);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// if we're still here, we need to try something else
|
|
|
|
|
$fields = array();
|
|
|
|
|
$driver = $db->getAttribute(PDO::ATTR_DRIVER_NAME);
|
|
|
|
|
if ($driver == 'sqlite') {
|
|
|
|
|
$query = sprintf("Pragma table_info(%s);", $table);
|
|
|
|
|
$stmt = $db->prepare($query);
|
|
|
|
|
$result = $stmt->execute();
|
|
|
|
|
$rows = $stmt->fetchAll(PDO::FETCH_NAMED);
|
|
|
|
|
//printf('<pre>%s</pre>', print_r($rows, true));
|
|
|
|
|
$row_count = count($rows);
|
|
|
|
|
//printf('<pre>%s</pre>', print_r($rows, true));
|
|
|
|
|
for ($i = 0; $i < $row_count; $i++) {
|
|
|
|
|
array_push($fields, $rows[$i]['name']);
|
|
|
|
|
}
|
|
|
|
|
return $fields;
|
|
|
|
|
} else {
|
|
|
|
|
$query = sprintf("SELECT * FROM information_schema.columns
|
|
|
|
|
WHERE table_schema='%s' AND table_name='%s';"
|
|
|
|
|
, $db_name, $table
|
|
|
|
|
);
|
|
|
|
|
$stmt = $db->prepare($query);
|
|
|
|
|
$result = $stmt->execute();
|
|
|
|
|
$rows = $stmt->fetchAll(PDO::FETCH_NAMED);
|
|
|
|
|
$row_count = count($rows);
|
|
|
|
|
//printf('<pre>%s</pre>', print_r($rows, true));
|
|
|
|
|
for ($i = 0; $i < $row_count; $i++) {
|
|
|
|
|
array_push($fields, $rows[$i]['COLUMN_NAME']);
|
|
|
|
|
}
|
|
|
|
|
return $fields;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($db_type == "sqlite") {
|
2012-06-24 08:45:12 +02:00
|
|
|
|
|
|
|
|
//set the document_root
|
2023-05-05 18:46:37 +02:00
|
|
|
if (empty($document_root)) {
|
2012-06-24 08:45:12 +02:00
|
|
|
$document_root = $_SERVER["DOCUMENT_ROOT"];
|
|
|
|
|
}
|
|
|
|
|
|
2012-06-04 16:58:40 +02:00
|
|
|
//prepare the database connection
|
2023-05-05 18:46:37 +02:00
|
|
|
if (empty($db_name)) {
|
|
|
|
|
//if (empty($_SERVER["SERVER_NAME"])) { $_SERVER["SERVER_NAME"] = "http://localhost"; }
|
2012-06-04 16:58:40 +02:00
|
|
|
$server_name = $_SERVER["SERVER_NAME"];
|
|
|
|
|
$server_name = str_replace ("www.", "", $server_name);
|
|
|
|
|
//$server_name = str_replace (".", "_", $server_name);
|
|
|
|
|
$db_name_short = $server_name;
|
|
|
|
|
$db_name = $server_name.'.db';
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$db_name_short = $db_name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$db_path = realpath($db_path);
|
|
|
|
|
if (file_exists($db_path.'/'.$db_name)) {
|
|
|
|
|
//echo "database file exists<br>";
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
if (is_writable($db_path.'/'.$db_name)) {
|
|
|
|
|
//use database in current location
|
|
|
|
|
}
|
2013-03-26 19:35:19 +01:00
|
|
|
else {
|
|
|
|
|
//not writable
|
2012-06-04 16:58:40 +02:00
|
|
|
echo "The database ".$db_path."/".$db_name." does not exist or is not writable.";
|
|
|
|
|
exit;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!function_exists('php_md5')) {
|
|
|
|
|
function php_md5($string) {
|
|
|
|
|
return md5($string);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!function_exists('php_unix_timestamp')) {
|
|
|
|
|
function php_unix_timestamp($string) {
|
|
|
|
|
return strtotime($string);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!function_exists('php_now')) {
|
|
|
|
|
function php_now() {
|
|
|
|
|
return date("Y-m-d H:i:s");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!function_exists('php_left')) {
|
|
|
|
|
function php_left($string, $num) {
|
|
|
|
|
return substr($string, 0, $num);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!function_exists('php_right')) {
|
|
|
|
|
function php_right($string, $num) {
|
|
|
|
|
return substr($string, (strlen($string)-$num), strlen($string));
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-04-29 19:21:26 +02:00
|
|
|
if (!function_exists('php_sqlite_data_type')) {
|
|
|
|
|
function php_sqlite_data_type($string, $field) {
|
|
|
|
|
|
|
|
|
|
//get the string between the start and end characters
|
|
|
|
|
$start = '(';
|
|
|
|
|
$end = ')';
|
|
|
|
|
$ini = stripos($string,$start);
|
|
|
|
|
if ($ini == 0) return "";
|
|
|
|
|
$ini += strlen($start);
|
|
|
|
|
$len = stripos($string,$end,$ini) - $ini;
|
|
|
|
|
$string = substr($string,$ini,$len);
|
|
|
|
|
|
|
|
|
|
$str_data_type = '';
|
|
|
|
|
$string_array = explode(',', $string);
|
|
|
|
|
foreach($string_array as $lnvalue) {
|
|
|
|
|
$fieldlistarray = explode (" ", $value);
|
|
|
|
|
unset($fieldarray, $string, $field);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $str_data_type;
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-06-04 16:58:40 +02:00
|
|
|
|
|
|
|
|
//database connection
|
|
|
|
|
try {
|
|
|
|
|
//create the database connection object
|
|
|
|
|
//$db = new PDO('sqlite2:example.db'); //sqlite 2
|
|
|
|
|
//$db = new PDO('sqlite::memory:'); //sqlite 3
|
|
|
|
|
$db = new PDO('sqlite:'.$db_path.'/'.$db_name); //sqlite 3
|
|
|
|
|
//enable foreign key constraints
|
|
|
|
|
$db->query('PRAGMA foreign_keys = ON;');
|
|
|
|
|
//add additional functions to SQLite so that they are accessible inside SQL
|
|
|
|
|
//bool PDO::sqliteCreateFunction ( string function_name, callback callback [, int num_args] )
|
|
|
|
|
$db->sqliteCreateFunction('md5', 'php_md5', 1);
|
|
|
|
|
$db->sqliteCreateFunction('unix_timestamp', 'php_unix_timestamp', 1);
|
|
|
|
|
$db->sqliteCreateFunction('now', 'php_now', 0);
|
2016-04-29 19:15:02 +02:00
|
|
|
$db->sqliteCreateFunction('sqlitedatatype', 'php_sqlite_data_type', 2);
|
2012-06-04 16:58:40 +02:00
|
|
|
$db->sqliteCreateFunction('strleft', 'php_left', 2);
|
|
|
|
|
$db->sqliteCreateFunction('strright', 'php_right', 2);
|
|
|
|
|
}
|
|
|
|
|
catch (PDOException $error) {
|
|
|
|
|
print "error: " . $error->getMessage() . "<br/>";
|
|
|
|
|
die();
|
|
|
|
|
}
|
|
|
|
|
} //end if db_type sqlite
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ($db_type == "mysql") {
|
|
|
|
|
//database connection
|
|
|
|
|
try {
|
|
|
|
|
//required for mysql_real_escape_string
|
2016-04-26 05:20:31 +02:00
|
|
|
if (function_exists('mysql_connect')) {
|
2014-08-24 11:46:32 +02:00
|
|
|
$mysql_connection = @mysql_connect($db_host, $db_username, $db_password);
|
2016-03-11 14:57:01 +01:00
|
|
|
//$mysql_connection = mysqli_connect($db_host, $db_username, $db_password,$db_name) or die("Error " . mysqli_error($link));
|
2012-06-04 16:58:40 +02:00
|
|
|
}
|
|
|
|
|
//mysql pdo connection
|
2023-05-05 18:46:37 +02:00
|
|
|
if (strlen($db_host) == 0 && empty($db_port)) {
|
2012-06-04 16:58:40 +02:00
|
|
|
//if both host and port are empty use the unix socket
|
2016-04-26 05:20:31 +02:00
|
|
|
$db = new PDO("mysql:host=$db_host;unix_socket=/var/run/mysqld/mysqld.sock;dbname=$db_name;charset=utf8;", $db_username, $db_password);
|
2012-06-04 16:58:40 +02:00
|
|
|
}
|
|
|
|
|
else {
|
2023-05-05 18:46:37 +02:00
|
|
|
if (empty($db_port)) {
|
2012-06-04 16:58:40 +02:00
|
|
|
//leave out port if it is empty
|
2016-04-26 05:20:31 +02:00
|
|
|
$db = new PDO("mysql:host=$db_host;dbname=$db_name;charset=utf8;", $db_username, $db_password, array(
|
2012-06-04 16:58:40 +02:00
|
|
|
PDO::ATTR_ERRMODE,
|
|
|
|
|
PDO::ERRMODE_EXCEPTION
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
else {
|
2016-04-26 05:20:31 +02:00
|
|
|
$db = new PDO("mysql:host=$db_host;port=$db_port;dbname=$db_name;charset=utf8;", $db_username, $db_password, array(
|
2012-06-04 16:58:40 +02:00
|
|
|
PDO::ATTR_ERRMODE,
|
|
|
|
|
PDO::ERRMODE_EXCEPTION
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (PDOException $error) {
|
|
|
|
|
print "error: " . $error->getMessage() . "<br/>";
|
|
|
|
|
die();
|
|
|
|
|
}
|
|
|
|
|
} //end if db_type mysql
|
|
|
|
|
|
|
|
|
|
if ($db_type == "pgsql") {
|
|
|
|
|
//database connection
|
|
|
|
|
try {
|
2020-10-31 18:44:20 +01:00
|
|
|
if (!isset($db_secure)) {
|
|
|
|
|
$db_secure = false;
|
2019-06-06 05:10:58 +02:00
|
|
|
}
|
2023-05-05 18:46:37 +02:00
|
|
|
if (!empty($db_host)) {
|
|
|
|
|
if (empty($db_port)) { $db_port = "5432"; }
|
2020-10-31 18:44:20 +01:00
|
|
|
if ($db_secure == true) {
|
2019-06-06 05:10:58 +02:00
|
|
|
$db = new PDO("pgsql:host=$db_host port=$db_port dbname=$db_name user=$db_username password=$db_password sslmode=verify-ca sslrootcert=$db_cert_authority");
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$db = new PDO("pgsql:host=$db_host port=$db_port dbname=$db_name user=$db_username password=$db_password");
|
|
|
|
|
}
|
2012-06-04 16:58:40 +02:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$db = new PDO("pgsql:dbname=$db_name user=$db_username password=$db_password");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (PDOException $error) {
|
|
|
|
|
print "error: " . $error->getMessage() . "<br/>";
|
|
|
|
|
die();
|
|
|
|
|
}
|
|
|
|
|
} //end if db_type pgsql
|
|
|
|
|
|
2022-11-08 17:45:04 +01:00
|
|
|
if ($db_type == "odbc") {
|
|
|
|
|
//database connection
|
|
|
|
|
try {
|
|
|
|
|
$db = new PDO("odbc:".$db_name);
|
|
|
|
|
}
|
|
|
|
|
catch (PDOException $error) {
|
|
|
|
|
print "error: " . $error->getMessage() . "<br/>";
|
|
|
|
|
die();
|
|
|
|
|
}
|
|
|
|
|
} //end if db_type pgsql
|
|
|
|
|
|
2015-07-26 01:44:56 +02:00
|
|
|
?>
|