v_sql_db_conversion.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. <?php
  2. /*
  3. FusionPBX
  4. Version: MPL 1.1
  5. The contents of this file are subject to the Mozilla Public License Version
  6. 1.1 (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.mozilla.org/MPL/
  9. Software distributed under the License is distributed on an "AS IS" basis,
  10. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11. for the specific language governing rights and limitations under the
  12. License.
  13. The Original Code is FusionPBX
  14. The Initial Developer of the Original Code is
  15. Mark J Crane <[email protected]>
  16. Portions created by the Initial Developer are Copyright (C) 2008-2012
  17. the Initial Developer. All Rights Reserved.
  18. Contributor(s):
  19. Mark J Crane <[email protected]>
  20. */
  21. include "root.php";
  22. require_once "includes/require.php";
  23. require_once "includes/checkauth.php";
  24. require_once "includes/lib_schema.php";
  25. if (if_group("superadmin")) {
  26. //access granted
  27. }
  28. else {
  29. echo "access denied";
  30. exit;
  31. //show errors
  32. ini_set('display_errors', '1');
  33. //error_reporting (E_ALL); // Report everything
  34. error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING ); //hide notices and warnings
  35. //define the db file exists function
  36. function db_field_exists ($tmp_array, $column) {
  37. $result = false;
  38. foreach ($tmp_array as &$row) {
  39. if ($row[0] == $column) {
  40. $result = true;
  41. }
  42. return $result;
  43. }
  44. }
  45. //db_field_exists ($result_dest, $column)
  46. //destination info
  47. //set the domain_uuid
  48. $dest_domain_uuid = '1';
  49. //set the database type
  50. $db_dest_type = 'mysql'; //sqlite, mysql, pgsql, others with a manually created PDO connection
  51. //sqlite: the dbfilename and db_file_path are automatically assigned however the values can be overidden by setting the values here.
  52. //$dbfilename = 'fusionpbx.db'; //host name/ip address + '.db' is the default database filename
  53. //$db_file_path = $_SERVER["DOCUMENT_ROOT"].PROJECT_PATH.'/secure'; //the path is determined by a php variable
  54. //mysql: database connection information
  55. $db_host = '127.0.0.1'; //set the host only if the database is not local
  56. $db_port = '3306';
  57. $db_name = 'fusionpbx';
  58. $db_username = 'fusionpbx';
  59. $db_password = '';
  60. $db_create_username = 'root';
  61. $db_create_password = '';
  62. //pgsql: database connection information
  63. //$db_host = ''; //set the host only if the database is not local
  64. //$db_port = '';
  65. //$db_name = '';
  66. //$db_username = '';
  67. //$db_password = '';
  68. //$db_create_username = '';
  69. //$db_create_password = '';
  70. //load data into the database
  71. //create the sqlite database
  72. if ($db_dest_type == "sqlite") {
  73. //sqlite database will be created when the config.php is loaded and only if the database file does not exist
  74. $filename = $_SERVER["DOCUMENT_ROOT"].PROJECT_PATH.'/includes/install/sql/sqlite.sql';
  75. $file_contents = file_get_contents($filename);
  76. unset($filename);
  77. try {
  78. $db_dest = new PDO('sqlite:'.$db_filepath.'/'.$db_filename); //sqlite 3
  79. //$db_dest = new PDO('sqlite::memory:'); //sqlite 3
  80. $db_dest->beginTransaction();
  81. }
  82. catch (PDOException $error) {
  83. print "error: " . $error->getMessage() . "<br/>";
  84. die();
  85. }
  86. //replace \r\n with \n then explode on \n
  87. $file_contents = str_replace("\r\n", "\n", $file_contents);
  88. //loop line by line through all the lines of sql code
  89. $stringarray = explode("\n", $file_contents);
  90. $x = 0;
  91. foreach($stringarray as $sql) {
  92. try {
  93. if(stristr($sql, 'CREATE TABLE') === FALSE) {
  94. //not found do not execute
  95. }
  96. else {
  97. //execute create table sql strings
  98. $db_dest->query($sql);
  99. }
  100. }
  101. catch (PDOException $error) {
  102. echo "error: " . $error->getMessage() . " sql: $sql<br/>";
  103. }
  104. $x++;
  105. }
  106. unset ($file_contents, $sql);
  107. $db_dest->commit();
  108. }
  109. //create the postgres database
  110. if ($db_dest_type == "pgsql") {
  111. $filename = $_SERVER["DOCUMENT_ROOT"].PROJECT_PATH.'/includes/install/sql/pgsql.sql';
  112. $file_contents = file_get_contents($filename);
  113. //if $db_create_username provided, attempt to create new PG role and database
  114. if (strlen($db_create_username) > 0) {
  115. //create the database connection
  116. try {
  117. if (strlen($db_port) == 0) { $db_port = "5432"; }
  118. if (strlen($db_host) > 0) {
  119. $db_dest = new PDO("pgsql:host={$db_host} port={$db_port} user={$db_create_username} password={$db_create_password} dbname=template1");
  120. } else {
  121. $db_dest = new PDO("pgsql:host=localhost port={$db_port} user={$db_create_username} password={$db_create_password} dbname=template1");
  122. }
  123. } catch (PDOException $error) {
  124. print "error: " . $error->getMessage() . "<br/>";
  125. die();
  126. }
  127. //create the database, user, grant perms
  128. $db_dest->exec("CREATE DATABASE {$db_name}");
  129. $db_dest->exec("CREATE USER {$db_username} WITH PASSWORD '{$db_password}'");
  130. $db_dest->exec("GRANT ALL ON {$db_name} TO {$db_username}");
  131. //close database connection_aborted
  132. $db_dest = null;
  133. }
  134. //open database connection with $db_name
  135. try {
  136. if (strlen($db_port) == 0) { $db_port = "5432"; }
  137. if (strlen($db_host) > 0) {
  138. $db_dest = new PDO("pgsql:host={$db_host} port={$db_port} dbname={$db_name} user={$db_username} password={$db_password}");
  139. } else {
  140. $db_dest = new PDO("pgsql:host=localhost port={$db_port} user={$db_username} password={$db_password} dbname={$db_name}");
  141. }
  142. }
  143. catch (PDOException $error) {
  144. print "error: " . $error->getMessage() . "<br/>";
  145. die();
  146. }
  147. //replace \r\n with \n then explode on \n
  148. $file_contents = str_replace("\r\n", "\n", $file_contents);
  149. //loop line by line through all the lines of sql code
  150. $stringarray = explode("\n", $file_contents);
  151. $x = 0;
  152. foreach($stringarray as $sql) {
  153. if (strlen($sql) > 3) {
  154. try {
  155. if(stristr($sql, 'CREATE TABLE') === FALSE) {
  156. //not found do not execute
  157. }
  158. else {
  159. //execute create table sql strings
  160. $db_dest->query($sql);
  161. }
  162. }
  163. catch (PDOException $error) {
  164. echo "error: " . $error->getMessage() . " sql: $sql<br/>";
  165. die();
  166. }
  167. }
  168. $x++;
  169. }
  170. unset ($file_contents, $sql);
  171. }
  172. //create the mysql database
  173. if ($db_dest_type == "mysql") {
  174. $filename = $_SERVER["DOCUMENT_ROOT"].PROJECT_PATH.'/includes/install/sql/mysql.sql';
  175. $file_contents = file_get_contents($filename);
  176. //database connection
  177. try {
  178. if (strlen($db_host) == 0 && strlen($db_port) == 0) {
  179. //if both host and port are empty use the unix socket
  180. if (strlen($db_create_username) == 0) {
  181. $db_dest = new PDO("mysql:host=$db_host;unix_socket=/var/run/mysqld/mysqld.sock;", $db_username, $db_password);
  182. }
  183. else {
  184. $db_dest = new PDO("mysql:host=$db_host;unix_socket=/var/run/mysqld/mysqld.sock;", $db_create_username, $db_create_password);
  185. }
  186. }
  187. else {
  188. if (strlen($db_port) == 0) {
  189. //leave out port if it is empty
  190. if (strlen($db_create_username) == 0) {
  191. $db_dest = new PDO("mysql:host=$db_host;", $db_username, $db_password);
  192. }
  193. else {
  194. $db_dest = new PDO("mysql:host=$db_host;", $db_create_username, $db_create_password);
  195. }
  196. }
  197. else {
  198. if (strlen($db_create_username) == 0) {
  199. $db_dest = new PDO("mysql:host=$db_host;port=$db_port;", $db_username, $db_password);
  200. }
  201. else {
  202. $db_dest = new PDO("mysql:host=$db_host;port=$db_port;", $db_create_username, $db_create_password);
  203. }
  204. }
  205. }
  206. $db_dest->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  207. $db_dest->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
  208. }
  209. catch (PDOException $error) {
  210. if ($v_debug) {
  211. print "error: " . $error->getMessage() . "<br/>";
  212. }
  213. }
  214. //create the table, user and set the permissions only if the db_create_username was provided
  215. if (strlen($db_create_username) > 0) {
  216. //select the mysql database
  217. try {
  218. $db_dest->query("USE mysql;");
  219. }
  220. catch (PDOException $error) {
  221. if ($v_debug) {
  222. print "error: " . $error->getMessage() . "<br/>";
  223. }
  224. }
  225. //create user and set the permissions
  226. try {
  227. $tmp_sql = "CREATE USER '".$db_username."'@'%' IDENTIFIED BY '".$db_password."'; ";
  228. $db_dest->query($tmp_sql);
  229. }
  230. catch (PDOException $error) {
  231. if ($v_debug) {
  232. print "error: " . $error->getMessage() . "<br/>";
  233. }
  234. }
  235. //set account to unlimitted use
  236. try {
  237. $tmp_sql = "GRANT USAGE ON * . * TO '".$db_username."'@'localhost' ";
  238. $tmp_sql .= "IDENTIFIED BY '".$db_password."' ";
  239. $tmp_sql .= "WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0; ";
  240. $db_dest->query($tmp_sql);
  241. }
  242. catch (PDOException $error) {
  243. if ($v_debug) {
  244. print "error: " . $error->getMessage() . "<br/>";
  245. }
  246. }
  247. //create the database and set the create user with permissions
  248. try {
  249. $tmp_sql = "CREATE DATABASE IF NOT EXISTS ".$db_name."; ";
  250. $db_dest->query($tmp_sql);
  251. }
  252. catch (PDOException $error) {
  253. if ($v_debug) {
  254. print "error: " . $error->getMessage() . "<br/>";
  255. }
  256. }
  257. //set user permissions
  258. try {
  259. $db_dest->query("GRANT ALL PRIVILEGES ON ".$db_name.".* TO '".$db_username."'@'%'; ");
  260. }
  261. catch (PDOException $error) {
  262. if ($v_debug) {
  263. print "error: " . $error->getMessage() . "<br/>";
  264. }
  265. }
  266. //make the changes active
  267. try {
  268. $tmp_sql = "FLUSH PRIVILEGES; ";
  269. $db_dest->query($tmp_sql);
  270. }
  271. catch (PDOException $error) {
  272. if ($v_debug) {
  273. print "error: " . $error->getMessage() . "<br/>";
  274. }
  275. }
  276. } //if (strlen($db_create_username) > 0)
  277. //select the database
  278. try {
  279. $db_dest->query("USE ".$db_name.";");
  280. }
  281. catch (PDOException $error) {
  282. if ($v_debug) {
  283. print "error: " . $error->getMessage() . "<br/>";
  284. }
  285. }
  286. //add the defaults data into the database
  287. //replace \r\n with \n then explode on \n
  288. $file_contents = str_replace("\r\n", "\n", $file_contents);
  289. //loop line by line through all the lines of sql code
  290. $stringarray = explode("\n", $file_contents);
  291. $x = 0;
  292. foreach($stringarray as $sql) {
  293. if (strlen($sql) > 3) {
  294. try {
  295. if(stristr($sql, 'CREATE TABLE') === FALSE) {
  296. //not found do not execute
  297. }
  298. else {
  299. //execute create table sql strings
  300. $db_dest->query($sql);
  301. }
  302. }
  303. catch (PDOException $error) {
  304. //echo "error on line $x: " . $error->getMessage() . " sql: $sql<br/>";
  305. //die();
  306. }
  307. }
  308. $x++;
  309. }
  310. unset ($file_contents, $sql);
  311. }
  312. //get the list of tables
  313. if ($db_dest_type == "sqlite") {
  314. $sql = "SELECT name FROM sqlite_master ";
  315. $sql .= "WHERE type='table' ";
  316. $sql .= "order by name;";
  317. }
  318. if ($db_dest_type == "pgsql") {
  319. $sql = "select table_name as name ";
  320. $sql .= "from information_schema.tables ";
  321. $sql .= "where table_schema='public' ";
  322. $sql .= "and table_type='BASE TABLE' ";
  323. $sql .= "order by table_name ";
  324. }
  325. if ($db_dest_type == "mysql") {
  326. $sql = "show tables";
  327. }
  328. //get the default schema structure
  329. $prep_statement = $db_dest->prepare(check_sql($sql));
  330. $prep_statement->execute();
  331. $result_dest = $prep_statement->fetchAll(PDO::FETCH_NAMED);
  332. //clean the content from the table
  333. foreach ($result_dest as &$row) {
  334. $table_name = $row[0];
  335. $sql = 'delete from '.$table_name;
  336. //$db_dest->query($sql);
  337. }
  338. //add data into each table
  339. foreach ($result_dest as &$row) {
  340. //get the table name
  341. $table_name = $row[0];
  342. //$table_name = 'v_extensions';
  343. //$db_dest_type = "sqlite";
  344. //get the table source data
  345. $destination_column_array='';
  346. unset($destination_column_array);
  347. if ($db_dest_type == "sqlite") {
  348. $tmp_sql = "PRAGMA table_info($table_name);";
  349. }
  350. if ($db_dest_type == "pgsql") {
  351. }
  352. if ($db_dest_type == "mysql") {
  353. $tmp_sql = "show columns from $table_name;";
  354. }
  355. if (strlen($tmp_sql) > 0) {
  356. $prep_statement_2 = $db_dest->prepare(check_sql($tmp_sql));
  357. //$prep_statement_2 = $db->prepare(check_sql($tmp_sql));
  358. if ($prep_statement_2) {
  359. $prep_statement_2->execute();
  360. $result2 = $prep_statement_2->fetchAll(PDO::FETCH_ASSOC);
  361. }
  362. else {
  363. echo "<b>Error:</b>\n";
  364. echo "<pre>\n";
  365. print_r($db_dest->errorInfo());
  366. echo "</pre>\n";
  367. }
  368. $x = 0;
  369. foreach ($result2 as $row2) {
  370. if ($db_dest_type == "sqlite") {
  371. $destination_column_array[$x] = $row2['name'];
  372. }
  373. if ($db_dest_type == "mysql") {
  374. $destination_column_array[$x] = $row2['Field'];
  375. }
  376. if ($db_dest_type == "pgsql") {
  377. }
  378. $x++;
  379. }
  380. /*
  381. $x = 0;
  382. foreach ($result2[0] as $key => $value) {
  383. if ($db_dest_type == "sqlite" && $key == "name") {
  384. $destination_column_array[$x] = $key;
  385. }
  386. $x++;
  387. }
  388. */
  389. $destination_column_array_count = count($destination_column_array);
  390. }
  391. unset($prep_statement_2, $result2);
  392. //echo "<pre>\n";
  393. //print_r($destination_column_array);
  394. //echo "</pre>\n";
  395. //get the table source data
  396. $tmp_sql = "select * from $table_name";
  397. if (strlen($tmp_sql) > 0) {
  398. $prep_statement_2 = $db->prepare(check_sql($tmp_sql));
  399. if ($prep_statement_2) {
  400. $prep_statement_2->execute();
  401. $result2 = $prep_statement_2->fetchAll(PDO::FETCH_ASSOC);
  402. }
  403. else {
  404. echo "<b>Error:</b>\n";
  405. echo "<pre>\n";
  406. print_r($db->errorInfo());
  407. echo "</pre>\n";
  408. }
  409. $x = 0;
  410. foreach ($result2[0] as $key => $value) {
  411. $column_array[$x] = $key;
  412. $x++;
  413. }
  414. foreach ($result2 as &$row) {
  415. //build the sql query string
  416. if (substr($table_name, 0, 2) == 'v_') {
  417. $sql = "INSERT INTO $table_name (";
  418. $x = 1;
  419. foreach ($destination_column_array as $column) {
  420. if ($x < $destination_column_array_count) {
  421. $sql .= "".$column.", ";
  422. }
  423. else {
  424. $sql .= "".$column."";
  425. }
  426. $x++;
  427. }
  428. $sql .= ") ";
  429. $sql .= "VALUES( ";
  430. $x = 1;
  431. foreach ($destination_column_array as $column) {
  432. if ($x < $destination_column_array_count) {
  433. //if ($column == "domain_uuid") {
  434. // $sql .= "'".$dest_domain_uuid."',";
  435. //}
  436. //else {
  437. $sql .= "'".check_str($row[$column])."', ";
  438. //}
  439. }
  440. else {
  441. //if ($column == "domain_uuid") {
  442. // $sql .= "'".$dest_domain_uuid."'";
  443. //}
  444. //else {
  445. $sql .= "'".check_str($row[$column])."'";
  446. //}
  447. }
  448. $x++;
  449. }
  450. $sql .= ");\n";
  451. }
  452. //add the sql into the destination database
  453. echo $sql."<br />\n";
  454. $db_dest->query($sql);
  455. }
  456. }
  457. }
  458. ?>