mirror of
https://github.com/fusionpbx/fusionpbx.git
synced 2026-01-06 03:33:49 +00:00
Add a missing file sip_profile_copy.php to the dev branch.
This commit is contained in:
26
app/sql_query/app_config.php
Normal file
26
app/sql_query/app_config.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
//application details
|
||||
$apps[$x]['name'] = "SQL Query";
|
||||
$apps[$x]['uuid'] = 'a8b8ca29-083d-fb9b-5552-cc272de18ea6';
|
||||
$apps[$x]['category'] = 'System';
|
||||
$apps[$x]['subcategory'] = '';
|
||||
$apps[$x]['version'] = '';
|
||||
$apps[$x]['license'] = 'Mozilla Public License 1.1';
|
||||
$apps[$x]['url'] = 'http://www.fusionpbx.com';
|
||||
$apps[$x]['description']['en'] = 'Run Structur Query Language commands.';
|
||||
|
||||
//menu details
|
||||
$apps[$x]['menu'][0]['title']['en'] = 'SQL Query';
|
||||
$apps[$x]['menu'][0]['uuid'] = 'a894fed7-5a17-f695-c3de-e32ce58b3794';
|
||||
$apps[$x]['menu'][0]['parent_uuid'] = '594d99c5-6128-9c88-ca35-4b33392cec0f';
|
||||
$apps[$x]['menu'][0]['category'] = 'internal';
|
||||
$apps[$x]['menu'][0]['path'] = '/app/sql_query/v_sql_query.php';
|
||||
$apps[$x]['menu'][0]['groups'][] = 'superadmin';
|
||||
|
||||
//permission details
|
||||
$apps[$x]['permissions'][0]['name'] = 'sql_query_execute';
|
||||
$apps[$x]['permissions'][0]['groups'][] = 'superadmin';
|
||||
|
||||
$apps[$x]['permissions'][1]['name'] = 'sql_query_backup';
|
||||
$apps[$x]['permissions'][1]['groups'][] = 'superadmin';
|
||||
?>
|
||||
50
app/sql_query/root.php
Normal file
50
app/sql_query/root.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?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>
|
||||
Portions created by the Initial Developer are Copyright (C) 2008-2012
|
||||
the Initial Developer. All Rights Reserved.
|
||||
|
||||
Contributor(s):
|
||||
Mark J Crane <markjcrane@fusionpbx.com>
|
||||
*/
|
||||
|
||||
// make sure the PATH_SEPARATOR is defined
|
||||
if (!defined("PATH_SEPARATOR")) {
|
||||
if ( strpos( $_ENV[ "OS" ], "Win" ) !== false ) { define("PATH_SEPARATOR", ";"); } else { define("PATH_SEPARATOR", ":"); }
|
||||
}
|
||||
|
||||
// make sure the document_root is set
|
||||
$_SERVER["SCRIPT_FILENAME"] = str_replace("\\", "/", $_SERVER["SCRIPT_FILENAME"]);
|
||||
$_SERVER["DOCUMENT_ROOT"] = str_replace($_SERVER["PHP_SELF"], "", $_SERVER["SCRIPT_FILENAME"]);
|
||||
$_SERVER["DOCUMENT_ROOT"] = realpath($_SERVER["DOCUMENT_ROOT"]);
|
||||
//echo "DOCUMENT_ROOT: ".$_SERVER["DOCUMENT_ROOT"]."<br />\n";
|
||||
//echo "PHP_SELF: ".$_SERVER["PHP_SELF"]."<br />\n";
|
||||
//echo "SCRIPT_FILENAME: ".$_SERVER["SCRIPT_FILENAME"]."<br />\n";
|
||||
|
||||
// if the project directory exists then add it to the include path otherwise add the document root to the include path
|
||||
if (is_dir($_SERVER["DOCUMENT_ROOT"].'/fusionpbx')){
|
||||
if(!defined('PROJECT_PATH')) { define('PROJECT_PATH', '/fusionpbx'); }
|
||||
set_include_path( get_include_path() . PATH_SEPARATOR . $_SERVER["DOCUMENT_ROOT"].'/fusionpbx' );
|
||||
}
|
||||
else {
|
||||
if(!defined('PROJECT_PATH')) { define('PROJECT_PATH', ''); }
|
||||
set_include_path( get_include_path() . PATH_SEPARATOR . $_SERVER['DOCUMENT_ROOT'] );
|
||||
}
|
||||
|
||||
?>
|
||||
133
app/sql_query/v_sql_backup.php
Normal file
133
app/sql_query/v_sql_backup.php
Normal file
@@ -0,0 +1,133 @@
|
||||
<?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>
|
||||
Portions created by the Initial Developer are Copyright (C) 2008-2012
|
||||
the Initial Developer. All Rights Reserved.
|
||||
|
||||
Contributor(s):
|
||||
Mark J Crane <markjcrane@fusionpbx.com>
|
||||
*/
|
||||
include "root.php";
|
||||
require_once "includes/require.php";
|
||||
require_once "includes/checkauth.php";
|
||||
if (permission_exists('sql_query_backup')) {
|
||||
//access granted
|
||||
}
|
||||
else {
|
||||
echo "access denied";
|
||||
exit;
|
||||
}
|
||||
|
||||
//pdo database connection
|
||||
if (strlen($_REQUEST['id']) > 0) {
|
||||
require_once "v_sql_query_pdo.php";
|
||||
}
|
||||
|
||||
//set the headers
|
||||
header('Content-type: application/octet-binary');
|
||||
header('Content-Disposition: attachment; filename=database_backup.sql');
|
||||
|
||||
//get the list of tables
|
||||
if ($db_type == "sqlite") {
|
||||
$sql = "SELECT name FROM sqlite_master ";
|
||||
$sql .= "WHERE type='table' ";
|
||||
$sql .= "order by name;";
|
||||
}
|
||||
if ($db_type == "pgsql") {
|
||||
$sql = "select table_name as name ";
|
||||
$sql .= "from information_schema.tables ";
|
||||
$sql .= "where table_schema='public' ";
|
||||
$sql .= "and table_type='BASE TABLE' ";
|
||||
$sql .= "order by table_name ";
|
||||
}
|
||||
if ($db_type == "mysql") {
|
||||
$sql = "show tables";
|
||||
}
|
||||
$prep_statement = $db->prepare(check_sql($sql));
|
||||
$prep_statement->execute();
|
||||
$result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
|
||||
foreach ($result as &$row) {
|
||||
$table_name = $row[0];
|
||||
|
||||
//get the table data
|
||||
$sql = "select * from $table_name";
|
||||
if (strlen($sql) > 0) {
|
||||
$prep_statement_2 = $db->prepare(check_sql($sql));
|
||||
if ($prep_statement_2) {
|
||||
$prep_statement_2->execute();
|
||||
$result2 = $prep_statement_2->fetchAll(PDO::FETCH_ASSOC);
|
||||
}
|
||||
else {
|
||||
echo "<b>Error:</b>\n";
|
||||
echo "<pre>\n";
|
||||
print_r($db->errorInfo());
|
||||
echo "</pre>\n";
|
||||
}
|
||||
|
||||
$x = 0;
|
||||
foreach ($result2[0] as $key => $value) {
|
||||
if ($row[$column] != "db") {
|
||||
$column_array[$x] = $key;
|
||||
$x++;
|
||||
}
|
||||
}
|
||||
|
||||
$column_array_count = count($column_array);
|
||||
|
||||
foreach ($result2 as &$row) {
|
||||
$sql = "INSERT INTO $table_name (";
|
||||
$x = 1;
|
||||
foreach ($column_array as $column) {
|
||||
if ($x < $column_array_count) {
|
||||
if (strlen($row[$column]) > 0) {
|
||||
$sql .= ''.$column.',';
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (strlen($row[$column]) > 0) {
|
||||
$sql .= ''.$column.'';
|
||||
}
|
||||
}
|
||||
$x++;
|
||||
}
|
||||
$sql .= ") ";
|
||||
$sql .= "VALUES( ";
|
||||
$x = 1;
|
||||
foreach ($column_array as $column) {
|
||||
if ($x < $column_array_count) {
|
||||
if (strlen($row[$column])> 0) {
|
||||
$sql .= "'".check_str($row[$column])."',";
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (strlen($row[$column])> 0) {
|
||||
$sql .= "'".check_str($row[$column])."'";
|
||||
}
|
||||
}
|
||||
$x++;
|
||||
}
|
||||
$sql .= ");\n";
|
||||
echo str_replace(",)", ")", $sql);
|
||||
}
|
||||
}
|
||||
|
||||
unset($column_array);
|
||||
}
|
||||
|
||||
?>
|
||||
495
app/sql_query/v_sql_db_conversion.php
Normal file
495
app/sql_query/v_sql_db_conversion.php
Normal file
@@ -0,0 +1,495 @@
|
||||
<?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>
|
||||
Portions created by the Initial Developer are Copyright (C) 2008-2012
|
||||
the Initial Developer. All Rights Reserved.
|
||||
|
||||
Contributor(s):
|
||||
Mark J Crane <markjcrane@fusionpbx.com>
|
||||
*/
|
||||
include "root.php";
|
||||
require_once "includes/require.php";
|
||||
require_once "includes/checkauth.php";
|
||||
require_once "includes/lib_schema.php";
|
||||
|
||||
if (if_group("superadmin")) {
|
||||
//access granted
|
||||
}
|
||||
else {
|
||||
echo "access denied";
|
||||
exit;
|
||||
|
||||
//show errors
|
||||
ini_set('display_errors', '1');
|
||||
//error_reporting (E_ALL); // Report everything
|
||||
error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING ); //hide notices and warnings
|
||||
|
||||
//define the db file exists function
|
||||
function db_field_exists ($tmp_array, $column) {
|
||||
$result = false;
|
||||
foreach ($tmp_array as &$row) {
|
||||
if ($row[0] == $column) {
|
||||
$result = true;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
//db_field_exists ($result_dest, $column)
|
||||
|
||||
//destination info
|
||||
//set the domain_uuid
|
||||
$dest_domain_uuid = '1';
|
||||
|
||||
//set the database type
|
||||
$db_dest_type = 'mysql'; //sqlite, mysql, pgsql, others with a manually created PDO connection
|
||||
|
||||
//sqlite: the dbfilename and db_file_path are automatically assigned however the values can be overidden by setting the values here.
|
||||
//$dbfilename = 'fusionpbx.db'; //host name/ip address + '.db' is the default database filename
|
||||
//$db_file_path = $_SERVER["DOCUMENT_ROOT"].PROJECT_PATH.'/secure'; //the path is determined by a php variable
|
||||
|
||||
//mysql: database connection information
|
||||
$db_host = '127.0.0.1'; //set the host only if the database is not local
|
||||
$db_port = '3306';
|
||||
$db_name = 'fusionpbx';
|
||||
$db_username = 'fusionpbx';
|
||||
$db_password = '';
|
||||
$db_create_username = 'root';
|
||||
$db_create_password = '';
|
||||
|
||||
//pgsql: database connection information
|
||||
//$db_host = ''; //set the host only if the database is not local
|
||||
//$db_port = '';
|
||||
//$db_name = '';
|
||||
//$db_username = '';
|
||||
//$db_password = '';
|
||||
//$db_create_username = '';
|
||||
//$db_create_password = '';
|
||||
|
||||
//load data into the database
|
||||
|
||||
//create the sqlite database
|
||||
if ($db_dest_type == "sqlite") {
|
||||
//sqlite database will be created when the config.php is loaded and only if the database file does not exist
|
||||
$filename = $_SERVER["DOCUMENT_ROOT"].PROJECT_PATH.'/includes/install/sql/sqlite.sql';
|
||||
$file_contents = file_get_contents($filename);
|
||||
unset($filename);
|
||||
try {
|
||||
$db_dest = new PDO('sqlite:'.$db_filepath.'/'.$db_filename); //sqlite 3
|
||||
//$db_dest = new PDO('sqlite::memory:'); //sqlite 3
|
||||
$db_dest->beginTransaction();
|
||||
}
|
||||
catch (PDOException $error) {
|
||||
print "error: " . $error->getMessage() . "<br/>";
|
||||
die();
|
||||
}
|
||||
|
||||
//replace \r\n with \n then explode on \n
|
||||
$file_contents = str_replace("\r\n", "\n", $file_contents);
|
||||
|
||||
//loop line by line through all the lines of sql code
|
||||
$stringarray = explode("\n", $file_contents);
|
||||
$x = 0;
|
||||
foreach($stringarray as $sql) {
|
||||
try {
|
||||
if(stristr($sql, 'CREATE TABLE') === FALSE) {
|
||||
//not found do not execute
|
||||
}
|
||||
else {
|
||||
//execute create table sql strings
|
||||
$db_dest->query($sql);
|
||||
}
|
||||
}
|
||||
catch (PDOException $error) {
|
||||
echo "error: " . $error->getMessage() . " sql: $sql<br/>";
|
||||
}
|
||||
$x++;
|
||||
}
|
||||
unset ($file_contents, $sql);
|
||||
$db_dest->commit();
|
||||
}
|
||||
|
||||
//create the postgres database
|
||||
if ($db_dest_type == "pgsql") {
|
||||
$filename = $_SERVER["DOCUMENT_ROOT"].PROJECT_PATH.'/includes/install/sql/pgsql.sql';
|
||||
$file_contents = file_get_contents($filename);
|
||||
|
||||
//if $db_create_username provided, attempt to create new PG role and database
|
||||
if (strlen($db_create_username) > 0) {
|
||||
//create the database connection
|
||||
try {
|
||||
if (strlen($db_port) == 0) { $db_port = "5432"; }
|
||||
if (strlen($db_host) > 0) {
|
||||
$db_dest = new PDO("pgsql:host={$db_host} port={$db_port} user={$db_create_username} password={$db_create_password} dbname=template1");
|
||||
} else {
|
||||
$db_dest = new PDO("pgsql:host=localhost port={$db_port} user={$db_create_username} password={$db_create_password} dbname=template1");
|
||||
}
|
||||
} catch (PDOException $error) {
|
||||
print "error: " . $error->getMessage() . "<br/>";
|
||||
die();
|
||||
}
|
||||
//create the database, user, grant perms
|
||||
$db_dest->exec("CREATE DATABASE {$db_name}");
|
||||
$db_dest->exec("CREATE USER {$db_username} WITH PASSWORD '{$db_password}'");
|
||||
$db_dest->exec("GRANT ALL ON {$db_name} TO {$db_username}");
|
||||
//close database connection_aborted
|
||||
$db_dest = null;
|
||||
}
|
||||
|
||||
//open database connection with $db_name
|
||||
try {
|
||||
if (strlen($db_port) == 0) { $db_port = "5432"; }
|
||||
if (strlen($db_host) > 0) {
|
||||
$db_dest = new PDO("pgsql:host={$db_host} port={$db_port} dbname={$db_name} user={$db_username} password={$db_password}");
|
||||
} else {
|
||||
$db_dest = new PDO("pgsql:host=localhost port={$db_port} user={$db_username} password={$db_password} dbname={$db_name}");
|
||||
}
|
||||
}
|
||||
catch (PDOException $error) {
|
||||
print "error: " . $error->getMessage() . "<br/>";
|
||||
die();
|
||||
}
|
||||
|
||||
//replace \r\n with \n then explode on \n
|
||||
$file_contents = str_replace("\r\n", "\n", $file_contents);
|
||||
|
||||
//loop line by line through all the lines of sql code
|
||||
$stringarray = explode("\n", $file_contents);
|
||||
$x = 0;
|
||||
foreach($stringarray as $sql) {
|
||||
if (strlen($sql) > 3) {
|
||||
try {
|
||||
if(stristr($sql, 'CREATE TABLE') === FALSE) {
|
||||
//not found do not execute
|
||||
}
|
||||
else {
|
||||
//execute create table sql strings
|
||||
$db_dest->query($sql);
|
||||
}
|
||||
}
|
||||
catch (PDOException $error) {
|
||||
echo "error: " . $error->getMessage() . " sql: $sql<br/>";
|
||||
die();
|
||||
}
|
||||
}
|
||||
$x++;
|
||||
}
|
||||
unset ($file_contents, $sql);
|
||||
}
|
||||
|
||||
//create the mysql database
|
||||
if ($db_dest_type == "mysql") {
|
||||
$filename = $_SERVER["DOCUMENT_ROOT"].PROJECT_PATH.'/includes/install/sql/mysql.sql';
|
||||
$file_contents = file_get_contents($filename);
|
||||
|
||||
//database connection
|
||||
try {
|
||||
if (strlen($db_host) == 0 && strlen($db_port) == 0) {
|
||||
//if both host and port are empty use the unix socket
|
||||
if (strlen($db_create_username) == 0) {
|
||||
$db_dest = new PDO("mysql:host=$db_host;unix_socket=/var/run/mysqld/mysqld.sock;", $db_username, $db_password);
|
||||
}
|
||||
else {
|
||||
$db_dest = new PDO("mysql:host=$db_host;unix_socket=/var/run/mysqld/mysqld.sock;", $db_create_username, $db_create_password);
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (strlen($db_port) == 0) {
|
||||
//leave out port if it is empty
|
||||
if (strlen($db_create_username) == 0) {
|
||||
$db_dest = new PDO("mysql:host=$db_host;", $db_username, $db_password);
|
||||
}
|
||||
else {
|
||||
$db_dest = new PDO("mysql:host=$db_host;", $db_create_username, $db_create_password);
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (strlen($db_create_username) == 0) {
|
||||
$db_dest = new PDO("mysql:host=$db_host;port=$db_port;", $db_username, $db_password);
|
||||
}
|
||||
else {
|
||||
$db_dest = new PDO("mysql:host=$db_host;port=$db_port;", $db_create_username, $db_create_password);
|
||||
}
|
||||
}
|
||||
}
|
||||
$db_dest->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
$db_dest->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
|
||||
}
|
||||
catch (PDOException $error) {
|
||||
if ($v_debug) {
|
||||
print "error: " . $error->getMessage() . "<br/>";
|
||||
}
|
||||
}
|
||||
|
||||
//create the table, user and set the permissions only if the db_create_username was provided
|
||||
if (strlen($db_create_username) > 0) {
|
||||
//select the mysql database
|
||||
try {
|
||||
$db_dest->query("USE mysql;");
|
||||
}
|
||||
catch (PDOException $error) {
|
||||
if ($v_debug) {
|
||||
print "error: " . $error->getMessage() . "<br/>";
|
||||
}
|
||||
}
|
||||
//create user and set the permissions
|
||||
try {
|
||||
$tmp_sql = "CREATE USER '".$db_username."'@'%' IDENTIFIED BY '".$db_password."'; ";
|
||||
$db_dest->query($tmp_sql);
|
||||
}
|
||||
catch (PDOException $error) {
|
||||
if ($v_debug) {
|
||||
print "error: " . $error->getMessage() . "<br/>";
|
||||
}
|
||||
}
|
||||
//set account to unlimitted use
|
||||
try {
|
||||
$tmp_sql = "GRANT USAGE ON * . * TO '".$db_username."'@'localhost' ";
|
||||
$tmp_sql .= "IDENTIFIED BY '".$db_password."' ";
|
||||
$tmp_sql .= "WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0; ";
|
||||
$db_dest->query($tmp_sql);
|
||||
}
|
||||
catch (PDOException $error) {
|
||||
if ($v_debug) {
|
||||
print "error: " . $error->getMessage() . "<br/>";
|
||||
}
|
||||
}
|
||||
//create the database and set the create user with permissions
|
||||
try {
|
||||
$tmp_sql = "CREATE DATABASE IF NOT EXISTS ".$db_name."; ";
|
||||
$db_dest->query($tmp_sql);
|
||||
}
|
||||
catch (PDOException $error) {
|
||||
if ($v_debug) {
|
||||
print "error: " . $error->getMessage() . "<br/>";
|
||||
}
|
||||
}
|
||||
//set user permissions
|
||||
try {
|
||||
$db_dest->query("GRANT ALL PRIVILEGES ON ".$db_name.".* TO '".$db_username."'@'%'; ");
|
||||
}
|
||||
catch (PDOException $error) {
|
||||
if ($v_debug) {
|
||||
print "error: " . $error->getMessage() . "<br/>";
|
||||
}
|
||||
}
|
||||
//make the changes active
|
||||
try {
|
||||
$tmp_sql = "FLUSH PRIVILEGES; ";
|
||||
$db_dest->query($tmp_sql);
|
||||
}
|
||||
catch (PDOException $error) {
|
||||
if ($v_debug) {
|
||||
print "error: " . $error->getMessage() . "<br/>";
|
||||
}
|
||||
}
|
||||
} //if (strlen($db_create_username) > 0)
|
||||
//select the database
|
||||
try {
|
||||
$db_dest->query("USE ".$db_name.";");
|
||||
}
|
||||
catch (PDOException $error) {
|
||||
if ($v_debug) {
|
||||
print "error: " . $error->getMessage() . "<br/>";
|
||||
}
|
||||
}
|
||||
|
||||
//add the defaults data into the database
|
||||
//replace \r\n with \n then explode on \n
|
||||
$file_contents = str_replace("\r\n", "\n", $file_contents);
|
||||
|
||||
//loop line by line through all the lines of sql code
|
||||
$stringarray = explode("\n", $file_contents);
|
||||
$x = 0;
|
||||
foreach($stringarray as $sql) {
|
||||
if (strlen($sql) > 3) {
|
||||
try {
|
||||
if(stristr($sql, 'CREATE TABLE') === FALSE) {
|
||||
//not found do not execute
|
||||
}
|
||||
else {
|
||||
//execute create table sql strings
|
||||
$db_dest->query($sql);
|
||||
}
|
||||
}
|
||||
catch (PDOException $error) {
|
||||
//echo "error on line $x: " . $error->getMessage() . " sql: $sql<br/>";
|
||||
//die();
|
||||
}
|
||||
}
|
||||
$x++;
|
||||
}
|
||||
unset ($file_contents, $sql);
|
||||
}
|
||||
|
||||
//get the list of tables
|
||||
if ($db_dest_type == "sqlite") {
|
||||
$sql = "SELECT name FROM sqlite_master ";
|
||||
$sql .= "WHERE type='table' ";
|
||||
$sql .= "order by name;";
|
||||
}
|
||||
if ($db_dest_type == "pgsql") {
|
||||
$sql = "select table_name as name ";
|
||||
$sql .= "from information_schema.tables ";
|
||||
$sql .= "where table_schema='public' ";
|
||||
$sql .= "and table_type='BASE TABLE' ";
|
||||
$sql .= "order by table_name ";
|
||||
}
|
||||
if ($db_dest_type == "mysql") {
|
||||
$sql = "show tables";
|
||||
}
|
||||
//get the default schema structure
|
||||
$prep_statement = $db_dest->prepare(check_sql($sql));
|
||||
$prep_statement->execute();
|
||||
$result_dest = $prep_statement->fetchAll(PDO::FETCH_NAMED);
|
||||
//clean the content from the table
|
||||
foreach ($result_dest as &$row) {
|
||||
$table_name = $row[0];
|
||||
$sql = 'delete from '.$table_name;
|
||||
//$db_dest->query($sql);
|
||||
}
|
||||
|
||||
//add data into each table
|
||||
foreach ($result_dest as &$row) {
|
||||
//get the table name
|
||||
$table_name = $row[0];
|
||||
|
||||
//$table_name = 'v_extensions';
|
||||
//$db_dest_type = "sqlite";
|
||||
|
||||
//get the table source data
|
||||
$destination_column_array='';
|
||||
unset($destination_column_array);
|
||||
if ($db_dest_type == "sqlite") {
|
||||
$tmp_sql = "PRAGMA table_info($table_name);";
|
||||
}
|
||||
if ($db_dest_type == "pgsql") {
|
||||
|
||||
}
|
||||
if ($db_dest_type == "mysql") {
|
||||
$tmp_sql = "show columns from $table_name;";
|
||||
}
|
||||
if (strlen($tmp_sql) > 0) {
|
||||
$prep_statement_2 = $db_dest->prepare(check_sql($tmp_sql));
|
||||
//$prep_statement_2 = $db->prepare(check_sql($tmp_sql));
|
||||
if ($prep_statement_2) {
|
||||
$prep_statement_2->execute();
|
||||
$result2 = $prep_statement_2->fetchAll(PDO::FETCH_ASSOC);
|
||||
}
|
||||
else {
|
||||
echo "<b>Error:</b>\n";
|
||||
echo "<pre>\n";
|
||||
print_r($db_dest->errorInfo());
|
||||
echo "</pre>\n";
|
||||
}
|
||||
$x = 0;
|
||||
foreach ($result2 as $row2) {
|
||||
if ($db_dest_type == "sqlite") {
|
||||
$destination_column_array[$x] = $row2['name'];
|
||||
}
|
||||
if ($db_dest_type == "mysql") {
|
||||
$destination_column_array[$x] = $row2['Field'];
|
||||
}
|
||||
if ($db_dest_type == "pgsql") {
|
||||
|
||||
}
|
||||
$x++;
|
||||
}
|
||||
/*
|
||||
$x = 0;
|
||||
foreach ($result2[0] as $key => $value) {
|
||||
if ($db_dest_type == "sqlite" && $key == "name") {
|
||||
$destination_column_array[$x] = $key;
|
||||
}
|
||||
$x++;
|
||||
}
|
||||
*/
|
||||
$destination_column_array_count = count($destination_column_array);
|
||||
}
|
||||
unset($prep_statement_2, $result2);
|
||||
//echo "<pre>\n";
|
||||
//print_r($destination_column_array);
|
||||
//echo "</pre>\n";
|
||||
|
||||
//get the table source data
|
||||
$tmp_sql = "select * from $table_name";
|
||||
if (strlen($tmp_sql) > 0) {
|
||||
$prep_statement_2 = $db->prepare(check_sql($tmp_sql));
|
||||
if ($prep_statement_2) {
|
||||
$prep_statement_2->execute();
|
||||
$result2 = $prep_statement_2->fetchAll(PDO::FETCH_ASSOC);
|
||||
}
|
||||
else {
|
||||
echo "<b>Error:</b>\n";
|
||||
echo "<pre>\n";
|
||||
print_r($db->errorInfo());
|
||||
echo "</pre>\n";
|
||||
}
|
||||
|
||||
$x = 0;
|
||||
foreach ($result2[0] as $key => $value) {
|
||||
$column_array[$x] = $key;
|
||||
$x++;
|
||||
}
|
||||
|
||||
foreach ($result2 as &$row) {
|
||||
//build the sql query string
|
||||
if (substr($table_name, 0, 2) == 'v_') {
|
||||
$sql = "INSERT INTO $table_name (";
|
||||
$x = 1;
|
||||
foreach ($destination_column_array as $column) {
|
||||
if ($x < $destination_column_array_count) {
|
||||
$sql .= "".$column.", ";
|
||||
}
|
||||
else {
|
||||
$sql .= "".$column."";
|
||||
}
|
||||
$x++;
|
||||
}
|
||||
$sql .= ") ";
|
||||
$sql .= "VALUES( ";
|
||||
$x = 1;
|
||||
foreach ($destination_column_array as $column) {
|
||||
if ($x < $destination_column_array_count) {
|
||||
//if ($column == "domain_uuid") {
|
||||
// $sql .= "'".$dest_domain_uuid."',";
|
||||
//}
|
||||
//else {
|
||||
$sql .= "'".check_str($row[$column])."', ";
|
||||
//}
|
||||
}
|
||||
else {
|
||||
//if ($column == "domain_uuid") {
|
||||
// $sql .= "'".$dest_domain_uuid."'";
|
||||
//}
|
||||
//else {
|
||||
$sql .= "'".check_str($row[$column])."'";
|
||||
//}
|
||||
}
|
||||
$x++;
|
||||
}
|
||||
$sql .= ");\n";
|
||||
}
|
||||
//add the sql into the destination database
|
||||
echo $sql."<br />\n";
|
||||
$db_dest->query($sql);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
171
app/sql_query/v_sql_query.php
Normal file
171
app/sql_query/v_sql_query.php
Normal file
@@ -0,0 +1,171 @@
|
||||
<?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>
|
||||
Portions created by the Initial Developer are Copyright (C) 2008-2012
|
||||
the Initial Developer. All Rights Reserved.
|
||||
|
||||
Contributor(s):
|
||||
Mark J Crane <markjcrane@fusionpbx.com>
|
||||
*/
|
||||
include "root.php";
|
||||
require_once "includes/require.php";
|
||||
require_once "includes/checkauth.php";
|
||||
if (permission_exists('sql_query_execute')) {
|
||||
//access granted
|
||||
}
|
||||
else {
|
||||
echo "access denied";
|
||||
exit;
|
||||
}
|
||||
|
||||
//show the header
|
||||
require_once "includes/header.php";
|
||||
|
||||
//pdo voicemail database connection
|
||||
require_once "v_sql_query_pdo.php";
|
||||
|
||||
//show the content
|
||||
//edit area
|
||||
echo " <script language=\"javascript\" type=\"text/javascript\" src=\"".PROJECT_PATH."/includes/edit_area/edit_area_full.js\"></script>\n";
|
||||
echo " <script language=\"Javascript\" type=\"text/javascript\">\n";
|
||||
echo "\n";
|
||||
echo " editAreaLoader.init({\n";
|
||||
echo " id: \"sql_cmd\" // id of the textarea to transform //, |, help\n";
|
||||
echo " ,start_highlight: true\n";
|
||||
//echo " ,display: \"later\"\n";
|
||||
echo " ,font_size: \"8\"\n";
|
||||
echo " ,allow_toggle: false\n";
|
||||
echo " ,language: \"en\"\n";
|
||||
echo " ,syntax: \"sql\"\n";
|
||||
echo " ,toolbar: \"search, go_to_line,|, fullscreen, |, undo, redo, |, select_font, |, syntax_selection, |, change_smooth_selection, highlight, reset_highlight, |, help\" //new_document,\n";
|
||||
echo " ,plugins: \"charmap\"\n";
|
||||
echo " ,charmap_default: \"arrows\"\n";
|
||||
echo "\n";
|
||||
echo " });\n";
|
||||
echo " </script>";
|
||||
|
||||
echo "<div align='center'>";
|
||||
echo "<table width='100%' border='0' cellpadding='0' cellspacing='2'>\n";
|
||||
|
||||
echo "<tr class='border'>\n";
|
||||
echo " <td align=\"left\">\n";
|
||||
echo " <br>";
|
||||
|
||||
echo "<form method='post' target='frame' action='v_sql_query_result.php' >";
|
||||
echo "<table width='100%' border='0' cellpadding='6' cellspacing='0'>\n";
|
||||
|
||||
echo "<tr>\n";
|
||||
echo "<td align='left' width='30%' nowrap><b>SQL Query</b></td>\n";
|
||||
echo "<td width='70%' align='right'>\n";
|
||||
if (strlen($_REQUEST['id']) > 0) {
|
||||
echo " <input type='button' class='btn' name='' alt='backup' onclick=\"window.location='v_sql_backup.php?id=".$_REQUEST['id']."'\" value='Backup'>\n";
|
||||
}
|
||||
else {
|
||||
echo " <input type='button' class='btn' name='' alt='backup' onclick=\"window.location='v_sql_backup.php'\" value='Backup'>\n";
|
||||
}
|
||||
echo " <input type='button' class='btn' name='' alt='backup' onclick=\"window.location='v_sql_query_db.php'\" value='Database'>\n";
|
||||
echo " <input type='button' class='btn' name='' alt='back' onClick=\"history.back()\" value='Back'>\n";
|
||||
echo "</td>\n";
|
||||
echo "</tr>\n";
|
||||
|
||||
echo "<tr>\n";
|
||||
echo "<td colspan='2' class='vtable' align='left'>\n";
|
||||
echo " <textarea name='sql_cmd' id='sql_cmd' rows='7' class='txt' wrap='off'>$sql_cmd</textarea\n";
|
||||
echo " <br />\n";
|
||||
echo "</td>\n";
|
||||
echo "</tr>\n";
|
||||
|
||||
echo " <tr>\n";
|
||||
echo " <td colspan='2' align='right'>\n";
|
||||
|
||||
/*
|
||||
echo " DB: <select name='sql_db'>\n";
|
||||
echo " <option value=''></option>\n";
|
||||
$sql = "";
|
||||
$sql .= "select * from v_databases ";
|
||||
$sql .= "where domain_uuid = '$domain_uuid' ";
|
||||
$prep_statement = $db->prepare(check_sql($sql));
|
||||
$prep_statement->execute();
|
||||
$result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
|
||||
foreach ($result as &$row) {
|
||||
//$database_type = $row["database_type"];
|
||||
//$database_host = $row["database_host"];
|
||||
//$database_port = $row["database_port"];
|
||||
//$database_name = $row["database_name"];
|
||||
//$database_username = $row["database_username"];
|
||||
//$database_password = $row["database_password"];
|
||||
//$database_path = $row["database_path"];
|
||||
//$database_description = $row["database_description"];
|
||||
echo " <option value='".$row["database_uuid"]."'>".$row["database_host"]." - ".$row["database_name"]."</option>\n";
|
||||
}
|
||||
unset ($prep_statement);
|
||||
echo " </select>\n";
|
||||
*/
|
||||
|
||||
echo " Type: <select name='sql_type'>\n";
|
||||
echo " <option value='default'>default</option>\n";
|
||||
echo " <option value='csv'>csv</option>\n";
|
||||
echo " <option value='sql insert into'>sql insert into</option>\n";
|
||||
echo " </select>\n";
|
||||
echo " \n";
|
||||
echo " \n";
|
||||
echo " Table: \n";
|
||||
echo " <select name='table_name'>\n";
|
||||
echo " <option value=''></option>\n";
|
||||
if ($db_type == "sqlite") {
|
||||
$sql = "SELECT name FROM sqlite_master ";
|
||||
$sql .= "WHERE type='table' ";
|
||||
$sql .= "order by name;";
|
||||
}
|
||||
if ($db_type == "pgsql") {
|
||||
$sql = "select table_name as name ";
|
||||
$sql .= "from information_schema.tables ";
|
||||
$sql .= "where table_schema='public' ";
|
||||
$sql .= "and table_type='BASE TABLE' ";
|
||||
$sql .= "order by table_name ";
|
||||
}
|
||||
if ($db_type == "mysql") {
|
||||
$sql = "show tables";
|
||||
}
|
||||
$prep_statement = $db->prepare(check_sql($sql));
|
||||
$prep_statement->execute();
|
||||
$result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
|
||||
foreach ($result as &$row) {
|
||||
echo " <option value='".$row['name']."'>".$row['name']."</option>\n";
|
||||
}
|
||||
echo " </select>\n";
|
||||
echo " <input type='hidden' name='id' value='".$_REQUEST['id']."'>\n";
|
||||
echo " <input type='submit' name='submit' class='btn' value='Execute'>\n";
|
||||
echo " </td>\n";
|
||||
echo " </tr>";
|
||||
|
||||
echo "</table>";
|
||||
echo "</form>";
|
||||
|
||||
echo " </td>";
|
||||
echo " </tr>";
|
||||
echo "</table>";
|
||||
echo "</div>";
|
||||
|
||||
echo "<iframe id='frame' width='100%' height='400' FRAMEBORDER='0' name='frame' style='background-color : #FFFFFF;'></iframe>\n";
|
||||
|
||||
//show the footer
|
||||
include "includes/require.php";
|
||||
require_once "includes/footer.php";
|
||||
?>
|
||||
149
app/sql_query/v_sql_query_db.php
Normal file
149
app/sql_query/v_sql_query_db.php
Normal file
@@ -0,0 +1,149 @@
|
||||
<?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>
|
||||
Portions created by the Initial Developer are Copyright (C) 2008-2012
|
||||
the Initial Developer. All Rights Reserved.
|
||||
|
||||
Contributor(s):
|
||||
Mark J Crane <markjcrane@fusionpbx.com>
|
||||
*/
|
||||
require_once "root.php";
|
||||
require_once "includes/require.php";
|
||||
require_once "includes/checkauth.php";
|
||||
if (if_group("admin") || if_group("superadmin")) {
|
||||
//access granted
|
||||
}
|
||||
else {
|
||||
echo "access denied";
|
||||
exit;
|
||||
}
|
||||
require_once "includes/header.php";
|
||||
require_once "includes/paging.php";
|
||||
|
||||
//get variables used to control the order
|
||||
$order_by = $_GET["order_by"];
|
||||
$order = $_GET["order"];
|
||||
|
||||
//show the content
|
||||
echo "<div align='center'>";
|
||||
echo "<table width='100%' border='0' cellpadding='0' cellspacing='2'>\n";
|
||||
echo "<tr class='border'>\n";
|
||||
echo " <td align=\"center\">\n";
|
||||
echo " <br>";
|
||||
|
||||
echo "<table width='100%' border='0'>\n";
|
||||
echo " <tr>\n";
|
||||
echo " <td width='50%' align=\"left\" nowrap=\"nowrap\"><b>Databases</b></td>\n";
|
||||
echo " <td width='50%' align=\"right\"> </td>\n";
|
||||
echo " </tr>\n";
|
||||
echo " <tr>\n";
|
||||
echo " <td align=\"left\" colspan='2'>\n";
|
||||
echo " Select the database to use.<br /><br />\n";
|
||||
echo " </td>\n";
|
||||
echo " </tr>\n";
|
||||
echo "</table>\n";
|
||||
|
||||
//prepare to page the results
|
||||
$sql = "";
|
||||
$sql .= " select count(*) as num_rows from v_databases ";
|
||||
if (strlen($order_by)> 0) { $sql .= "order by $order_by $order "; }
|
||||
$prep_statement = $db->prepare($sql);
|
||||
if ($prep_statement) {
|
||||
$prep_statement->execute();
|
||||
$row = $prep_statement->fetch(PDO::FETCH_ASSOC);
|
||||
if ($row['num_rows'] > 0) {
|
||||
$num_rows = $row['num_rows'];
|
||||
}
|
||||
else {
|
||||
$num_rows = '0';
|
||||
}
|
||||
}
|
||||
|
||||
//prepare to page the results
|
||||
$rows_per_page = 100;
|
||||
$param = "";
|
||||
$page = $_GET['page'];
|
||||
if (strlen($page) == 0) { $page = 0; $_GET['page'] = 0; }
|
||||
list($paging_controls, $rows_per_page, $var_3) = paging($num_rows, $param, $rows_per_page);
|
||||
$offset = $rows_per_page * $page;
|
||||
|
||||
//get the list
|
||||
$sql = "";
|
||||
$sql .= " select * from v_databases ";
|
||||
if (strlen($order_by)> 0) { $sql .= "order by $order_by $order "; }
|
||||
$sql .= " limit $rows_per_page offset $offset ";
|
||||
$prep_statement = $db->prepare(check_sql($sql));
|
||||
$prep_statement->execute();
|
||||
$result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
|
||||
$result_count = count($result);
|
||||
unset ($prep_statement, $sql);
|
||||
|
||||
$c = 0;
|
||||
$row_style["0"] = "row_style0";
|
||||
$row_style["1"] = "row_style1";
|
||||
|
||||
echo "<div align='center'>\n";
|
||||
echo "<table width='100%' border='0' cellpadding='0' cellspacing='0'>\n";
|
||||
|
||||
echo "<tr>\n";
|
||||
echo th_order_by('database_type', 'Type', $order_by, $order);
|
||||
echo th_order_by('database_host', 'Host', $order_by, $order);
|
||||
//echo th_order_by('database_port', 'Port', $order_by, $order);
|
||||
echo th_order_by('database_name', 'Name', $order_by, $order);
|
||||
//echo th_order_by('database_username', 'Username', $order_by, $order);
|
||||
//echo th_order_by('database_path', 'Path', $order_by, $order);
|
||||
echo th_order_by('database_description', 'Description', $order_by, $order);
|
||||
echo "<td align='right' width='21'>\n";
|
||||
//echo " <a href='db_edit.php' alt='add'>$v_link_label_add</a>\n";
|
||||
echo "</td>\n";
|
||||
echo "<tr>\n";
|
||||
|
||||
if ($result_count > 0) {
|
||||
foreach($result as $row) {
|
||||
echo "<tr >\n";
|
||||
echo " <td valign='top' class='".$row_style[$c]."'>".$row['database_type']." </td>\n";
|
||||
echo " <td valign='top' class='".$row_style[$c]."'>".$row['database_host']." </td>\n";
|
||||
//echo " <td valign='top' class='".$row_style[$c]."'>".$row['database_port']." </td>\n";
|
||||
echo " <td valign='top' class='".$row_style[$c]."'>".$row['database_name']." </td>\n";
|
||||
//echo " <td valign='top' class='".$row_style[$c]."'>".$row['database_username']." </td>\n";
|
||||
//echo " <td valign='top' class='".$row_style[$c]."'>".$row['database_path']." </td>\n";
|
||||
echo " <td valign='top' class='row_stylebg'>".$row['database_description']." </td>\n";
|
||||
echo " <td valign='top' align='right'>\n";
|
||||
echo " <a href='v_sql_query.php?id=".$row['db_uuid']."' alt='edit'>$v_link_label_edit</a>\n";
|
||||
echo " </td>\n";
|
||||
echo "</tr>\n";
|
||||
if ($c==0) { $c=1; } else { $c=0; }
|
||||
} //end foreach
|
||||
unset($sql, $result, $row_count);
|
||||
} //end if results
|
||||
|
||||
echo "</table>";
|
||||
echo "</div>";
|
||||
echo "<br><br>";
|
||||
echo "<br><br>";
|
||||
|
||||
echo "</td>";
|
||||
echo "</tr>";
|
||||
echo "</table>";
|
||||
echo "</div>";
|
||||
echo "<br><br>";
|
||||
|
||||
//include the footer
|
||||
require_once "includes/footer.php";
|
||||
?>
|
||||
225
app/sql_query/v_sql_query_pdo.php
Normal file
225
app/sql_query/v_sql_query_pdo.php
Normal file
@@ -0,0 +1,225 @@
|
||||
<?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>
|
||||
Portions created by the Initial Developer are Copyright (C) 2008-2012
|
||||
the Initial Developer. All Rights Reserved.
|
||||
|
||||
Contributor(s):
|
||||
Mark J Crane <markjcrane@fusionpbx.com>
|
||||
*/
|
||||
|
||||
//set the default values
|
||||
if (isset($db_file_path) > 0) {
|
||||
$db_path = $db_file_path;
|
||||
$db_name = $dbfilename;
|
||||
}
|
||||
|
||||
//get the db connection information
|
||||
/*
|
||||
if ($db) {
|
||||
$sql = "";
|
||||
$sql .= "select * from v_db ";
|
||||
$sql .= "where domain_uuid = '$domain_uuid' ";
|
||||
$sql .= "and db_uuid = '".$_REQUEST['id']."' ";
|
||||
$prep_statement = $db->prepare($sql);
|
||||
$prep_statement->execute();
|
||||
$result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
|
||||
foreach ($result as &$row) {
|
||||
$db_type = $row["db_type"];
|
||||
$db_host = $row["db_host"];
|
||||
$db_port = $row["db_port"];
|
||||
$db_name = $row["db_name"];
|
||||
$db_username = $row["db_username"];
|
||||
$db_password = $row["db_password"];
|
||||
$db_path = $row["db_path"];
|
||||
break;
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
//unset the database connection
|
||||
unset($db);
|
||||
|
||||
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") {
|
||||
if (!function_exists('phpmd5')) {
|
||||
function phpmd5($string) {
|
||||
return md5($string);
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('php_unix_timestamp')) {
|
||||
function php_unix_timestamp($string) {
|
||||
return strtotime($string);
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('phpnow')) {
|
||||
function phpnow() {
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
} //end function
|
||||
|
||||
//database connection
|
||||
try {
|
||||
//$db = new PDO('sqlite2:example.db'); //sqlite 2
|
||||
//$db = new PDO('sqlite::memory:'); //sqlite 3
|
||||
$db = new PDO('sqlite:'.realpath($db_path).'/'.$db_name); //sqlite 3
|
||||
|
||||
//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', 'phpmd5', 1);
|
||||
$db->sqliteCreateFunction('unix_timestamp', 'php_unix_timestamp', 1);
|
||||
$db->sqliteCreateFunction('now', 'phpnow', 0);
|
||||
$db->sqliteCreateFunction('sqlitedatatype', 'php_sqlite_data_type', 2);
|
||||
$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
|
||||
if (function_exists(mysql_connect)) {
|
||||
$mysql_connection = mysql_connect($db_host, $db_username, $db_password);
|
||||
}
|
||||
//mysql pdo connection
|
||||
if (strlen($db_host) == 0 && strlen($db_port) == 0) {
|
||||
//if both host and port are empty use the unix socket
|
||||
$db = new PDO("mysql:host=$db_host;unix_socket=/var/run/mysqld/mysqld.sock;dbname=$db_name", $db_username, $db_password);
|
||||
}
|
||||
else {
|
||||
if (strlen($db_port) == 0) {
|
||||
//leave out port if it is empty
|
||||
$db = new PDO("mysql:host=$db_host;dbname=$db_name;", $db_username, $db_password, array(
|
||||
PDO::ATTR_ERRMODE,
|
||||
PDO::ERRMODE_EXCEPTION
|
||||
));
|
||||
}
|
||||
else {
|
||||
$db = new PDO("mysql:host=$db_host;port=$db_port;dbname=$db_name;", $db_username, $db_password, array(
|
||||
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 {
|
||||
if (strlen($db_host) > 0) {
|
||||
if (strlen($db_port) == 0) { $db_port = "5432"; }
|
||||
$db = new PDO("pgsql:host=$db_host port=$db_port dbname=$db_name user=$db_username password=$db_password");
|
||||
}
|
||||
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
|
||||
|
||||
?>
|
||||
298
app/sql_query/v_sql_query_result.php
Normal file
298
app/sql_query/v_sql_query_result.php
Normal file
@@ -0,0 +1,298 @@
|
||||
<?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>
|
||||
Portions created by the Initial Developer are Copyright (C) 2008-2012
|
||||
the Initial Developer. All Rights Reserved.
|
||||
|
||||
Contributor(s):
|
||||
Mark J Crane <markjcrane@fusionpbx.com>
|
||||
*/
|
||||
include "root.php";
|
||||
require_once "includes/require.php";
|
||||
require_once "includes/checkauth.php";
|
||||
if (permission_exists('sql_query_execute')) {
|
||||
//access granted
|
||||
}
|
||||
else {
|
||||
echo "access denied";
|
||||
exit;
|
||||
}
|
||||
|
||||
//pdo database connection
|
||||
if (strlen($_REQUEST['id']) > 0) {
|
||||
require_once "v_sql_query_pdo.php";
|
||||
}
|
||||
|
||||
if (count($_POST)>0) {
|
||||
$sql_type = trim($_POST["sql_type"]);
|
||||
$sql_cmd = trim($_POST["sql_cmd"]);
|
||||
$table_name = trim($_POST["table_name"]);
|
||||
if (strlen($sql_cmd) == 0) { $sql_cmd = "select * from ".$table_name; }
|
||||
}
|
||||
|
||||
if (count($_POST)>0) {
|
||||
$tmp_header = "<html>\n";
|
||||
$tmp_header .= "<head>\n";
|
||||
$tmp_header .= "<style type='text/css'>\n";
|
||||
$tmp_header .= "\n";
|
||||
$tmp_header .= "body {\n";
|
||||
$tmp_header .= " font-size: 13px;\n";
|
||||
$tmp_header .= " color: #444444;\n";
|
||||
$tmp_header .= "}\n";
|
||||
$tmp_header .= "\n";
|
||||
$tmp_header .= "th {\n";
|
||||
$tmp_header .= " border-top: 1px solid #444444;\n";
|
||||
$tmp_header .= " border-bottom: 1px solid #444444;\n";
|
||||
$tmp_header .= " color: #FFFFFF;\n";
|
||||
$tmp_header .= " font-size: 12px;\n";
|
||||
$tmp_header .= " font-family: arial;\n";
|
||||
$tmp_header .= " font-weight: bold;\n";
|
||||
$tmp_header .= " background-color: #777777;\n";
|
||||
$tmp_header .= " background-image: url(".PROJECT_PATH."'/themes/horizontal/background_th.png');\n";
|
||||
$tmp_header .= " padding-top: 4px;\n";
|
||||
$tmp_header .= " padding-bottom: 4px;\n";
|
||||
$tmp_header .= " padding-right: 7px;\n";
|
||||
$tmp_header .= " padding-left: 7px;\n";
|
||||
$tmp_header .= "}\n";
|
||||
$tmp_header .= "\n";
|
||||
$tmp_header .= ".row_style0 {\n";
|
||||
$tmp_header .= " background-color: #EEEEEE;\n";
|
||||
$tmp_header .= " background-image: url(".PROJECT_PATH."'/themes/horizontal/background_cell.gif');\n";
|
||||
$tmp_header .= " border-bottom: 1px solid #999999;\n";
|
||||
$tmp_header .= " color: #444444;\n";
|
||||
$tmp_header .= " text-align: left;\n";
|
||||
$tmp_header .= " padding-top: 4px;\n";
|
||||
$tmp_header .= " padding-bottom: 4px;\n";
|
||||
$tmp_header .= " padding-right: 7px;\n";
|
||||
$tmp_header .= " padding-left: 7px;\n";
|
||||
$tmp_header .= "}\n";
|
||||
$tmp_header .= "\n";
|
||||
$tmp_header .= ".row_style0 a:link{ color:#444444; }\n";
|
||||
$tmp_header .= ".row_style0 a:visited{ color:#444444; }\n";
|
||||
$tmp_header .= ".row_style0 a:hover{ color:#444444; }\n";
|
||||
$tmp_header .= ".row_style0 a:active{ color:#444444; }\n";
|
||||
$tmp_header .= "\n";
|
||||
$tmp_header .= ".row_style1 {\n";
|
||||
$tmp_header .= " border-bottom: 1px solid #999999;\n";
|
||||
$tmp_header .= " background-color: #FFFFFF;\n";
|
||||
$tmp_header .= " color: #444444;\n";
|
||||
$tmp_header .= " text-align: left;\n";
|
||||
$tmp_header .= " padding-top: 4px;\n";
|
||||
$tmp_header .= " padding-bottom: 4px;\n";
|
||||
$tmp_header .= " padding-right: 7px;\n";
|
||||
$tmp_header .= " padding-left: 7px;\n";
|
||||
$tmp_header .= "}\n";
|
||||
$tmp_header .= "\n";
|
||||
$tmp_header .= "</style>";
|
||||
$tmp_header .= "</head>\n";
|
||||
$tmp_header .= "<body>\n";
|
||||
|
||||
$tmp_footer = "<body>\n";
|
||||
$tmp_footer .= "<html>\n";
|
||||
|
||||
if ($sql_type == "default") {
|
||||
|
||||
echo $tmp_header;
|
||||
|
||||
$c = 0;
|
||||
$row_style["0"] = "row_style0";
|
||||
$row_style["1"] = "row_style1";
|
||||
|
||||
$sql_array = explode(";", $sql_cmd);
|
||||
reset($sql_array);
|
||||
foreach($sql_array as $sql) {
|
||||
$sql = trim($sql);
|
||||
echo "<b>SQL Query:</b><br>\n";
|
||||
echo "".$sql."<br /><br />";
|
||||
|
||||
if (strlen($sql) > 0) {
|
||||
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
try {
|
||||
$prep_statement = $db->prepare(check_sql($sql));
|
||||
$prep_statement->execute();
|
||||
$result = $prep_statement->fetchAll(PDO::FETCH_ASSOC);
|
||||
echo "<b>Results: ".count($result)."</b><br />";
|
||||
}
|
||||
catch(PDOException $e) {
|
||||
echo "<b>Error:</b><br />\n";
|
||||
echo "<table>\n";
|
||||
echo "<tr>\n";
|
||||
echo "<td>\n";
|
||||
echo $e->getMessage();
|
||||
echo "</td>\n";
|
||||
echo "</tr>\n";
|
||||
echo "</table>\n";
|
||||
}
|
||||
|
||||
echo "<table width='100%' border='0' cellpadding='0' cellspacing='0'>\n";
|
||||
$x = 0;
|
||||
foreach ($result[0] as $key => $value) {
|
||||
echo "<th>".$key."</th>";
|
||||
$column_array[$x] = $key;
|
||||
$x++;
|
||||
}
|
||||
|
||||
$x = 1;
|
||||
foreach ($result as &$row) {
|
||||
if ($x > 1000) { break; }
|
||||
echo "<tr>\n";
|
||||
foreach ($column_array as $column) {
|
||||
echo "<td class='".$row_style[$c]."'> ".$row[$column]." </td>";
|
||||
}
|
||||
echo "</tr>\n";
|
||||
if ($c==0) { $c=1; } else { $c=0; }
|
||||
$x++;
|
||||
}
|
||||
echo "</table>\n";
|
||||
echo "<br>\n";
|
||||
}
|
||||
} //foreach($sql_array as $sql)
|
||||
echo $tmp_footer;
|
||||
}
|
||||
|
||||
if ($sql_type == "sql insert into") {
|
||||
echo $tmp_header;
|
||||
|
||||
$sql = trim($sql);
|
||||
echo "<b>SQL Query:</b><br>\n";
|
||||
echo "".$sql."<br /><br />";
|
||||
|
||||
//get the table data
|
||||
$sql = "select * from $table_name";
|
||||
if (strlen($sql) > 0) {
|
||||
$prep_statement = $db->prepare(check_sql($sql));
|
||||
if ($prep_statement) {
|
||||
$prep_statement->execute();
|
||||
$result = $prep_statement->fetchAll(PDO::FETCH_ASSOC);
|
||||
}
|
||||
else {
|
||||
echo "<b>Error:</b>\n";
|
||||
echo "<pre>\n";
|
||||
print_r($db->errorInfo());
|
||||
echo "</pre>\n";
|
||||
}
|
||||
|
||||
$x = 0;
|
||||
foreach ($result[0] as $key => $value) {
|
||||
$column_array[$x] = $key;
|
||||
$x++;
|
||||
}
|
||||
|
||||
$column_array_count = count($column_array);
|
||||
|
||||
foreach ($result as &$row) {
|
||||
echo "INSERT INTO $table_name (";
|
||||
$x = 1;
|
||||
foreach ($column_array as $column) {
|
||||
if ($x < $column_array_count) {
|
||||
if ($column != "menuid" && $column != "menuparentid") {
|
||||
echo "".$column.",";
|
||||
}
|
||||
}
|
||||
else {
|
||||
if ($column != "menuid" && $column != "menuparentid") {
|
||||
echo "".$column."";
|
||||
}
|
||||
}
|
||||
$x++;
|
||||
}
|
||||
echo ") ";
|
||||
|
||||
echo "VALUES ( ";
|
||||
$x = 1;
|
||||
foreach ($column_array as $column) {
|
||||
if ($x < $column_array_count) {
|
||||
if ($column != "menuid" && $column != "menuparentid") {
|
||||
echo "'".check_str($row[$column])."',";
|
||||
}
|
||||
}
|
||||
else {
|
||||
if ($column != "menuid" && $column != "menuparentid") {
|
||||
echo "'".check_str($row[$column])."'";
|
||||
}
|
||||
}
|
||||
$x++;
|
||||
}
|
||||
echo ");<br />\n";
|
||||
}
|
||||
}
|
||||
echo $tmp_footer;
|
||||
}
|
||||
|
||||
if ($sql_type == "csv") {
|
||||
//echo $tmp_header;
|
||||
|
||||
//set the headers
|
||||
header('Content-type: application/octet-binary');
|
||||
header('Content-Disposition: attachment; filename='.$table_name.'.sql');
|
||||
|
||||
//get the table data
|
||||
$sql = trim($sql);
|
||||
$sql = "select * from $table_name";
|
||||
if (strlen($sql) > 0) {
|
||||
$prep_statement = $db->prepare(check_sql($sql));
|
||||
if ($prep_statement) {
|
||||
$prep_statement->execute();
|
||||
$result = $prep_statement->fetchAll(PDO::FETCH_ASSOC);
|
||||
}
|
||||
else {
|
||||
echo "<b>Error:</b>\n";
|
||||
echo "<pre>\n";
|
||||
print_r($db->errorInfo());
|
||||
echo "</pre>\n";
|
||||
}
|
||||
|
||||
$x = 0;
|
||||
foreach ($result[0] as $key => $value) {
|
||||
$column_array[$x] = $key;
|
||||
$x++;
|
||||
}
|
||||
|
||||
$column_array_count = count($column_array);
|
||||
|
||||
$x = 1;
|
||||
foreach ($column_array as $column) {
|
||||
if ($x < $column_array_count) {
|
||||
echo "\"".$column."\",";
|
||||
}
|
||||
else {
|
||||
echo "\"".$column."\"";
|
||||
}
|
||||
$x++;
|
||||
}
|
||||
echo "\r\n";
|
||||
|
||||
foreach ($result as &$row) {
|
||||
$x = 1;
|
||||
foreach ($column_array as $column) {
|
||||
if ($x < $column_array_count) {
|
||||
echo "\"".check_str($row[$column])."\",";
|
||||
}
|
||||
else {
|
||||
echo "\"".check_str($row[$column])."\"";
|
||||
}
|
||||
$x++;
|
||||
}
|
||||
echo "\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user