sql_db_conversion.php 15 KB

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