sql_query_pdo.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. require_once "root.php";
  22. require_once "includes/require.php";
  23. require_once "resources/check_auth.php";
  24. if (if_group("admin") || if_group("superadmin")) {
  25. //access granted
  26. }
  27. else {
  28. echo "access denied";
  29. exit;
  30. }
  31. //set the default values
  32. if (isset($db_file_path) > 0) {
  33. $db_path = $db_file_path;
  34. $db_name = $dbfilename;
  35. }
  36. //get the db connection information
  37. if (strlen($_REQUEST['id']) > 0) {
  38. $sql = "select * from v_databases ";
  39. $sql .= "where database_uuid = '".$_REQUEST['id']."' ";
  40. $prep_statement = $db->prepare($sql);
  41. $prep_statement->execute();
  42. $result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
  43. foreach ($result as &$row) {
  44. $db_type = $row["database_type"];
  45. $db_host = $row["database_host"];
  46. $db_port = $row["database_port"];
  47. $db_name = $row["database_name"];
  48. $db_username = $row["database_username"];
  49. $db_password = $row["database_password"];
  50. $db_path = $row["database_path"];
  51. break;
  52. }
  53. }
  54. //unset the database connection
  55. unset($db);
  56. if (!function_exists('get_db_field_names')) {
  57. function get_db_field_names($db, $table, $db_name='fusionpbx') {
  58. $query = sprintf('SELECT * FROM %s LIMIT 1', $table);
  59. foreach ($db->query($query, PDO::FETCH_ASSOC) as $row) {
  60. return array_keys($row);
  61. }
  62. // if we're still here, we need to try something else
  63. $fields = array();
  64. $driver = $db->getAttribute(PDO::ATTR_DRIVER_NAME);
  65. if ($driver == 'sqlite') {
  66. $query = sprintf("Pragma table_info(%s);", $table);
  67. $stmt = $db->prepare($query);
  68. $result = $stmt->execute();
  69. $rows = $stmt->fetchAll(PDO::FETCH_NAMED);
  70. //printf('<pre>%s</pre>', print_r($rows, true));
  71. $row_count = count($rows);
  72. //printf('<pre>%s</pre>', print_r($rows, true));
  73. for ($i = 0; $i < $row_count; $i++) {
  74. array_push($fields, $rows[$i]['name']);
  75. }
  76. return $fields;
  77. } else {
  78. $query = sprintf("SELECT * FROM information_schema.columns
  79. WHERE table_schema='%s' AND table_name='%s';"
  80. , $db_name, $table
  81. );
  82. $stmt = $db->prepare($query);
  83. $result = $stmt->execute();
  84. $rows = $stmt->fetchAll(PDO::FETCH_NAMED);
  85. $row_count = count($rows);
  86. //printf('<pre>%s</pre>', print_r($rows, true));
  87. for ($i = 0; $i < $row_count; $i++) {
  88. array_push($fields, $rows[$i]['COLUMN_NAME']);
  89. }
  90. return $fields;
  91. }
  92. }
  93. }
  94. if ($db_type == "sqlite") {
  95. if (!function_exists('phpmd5')) {
  96. function phpmd5($string) {
  97. return md5($string);
  98. }
  99. }
  100. if (!function_exists('php_unix_timestamp')) {
  101. function php_unix_timestamp($string) {
  102. return strtotime($string);
  103. }
  104. }
  105. if (!function_exists('phpnow')) {
  106. function phpnow() {
  107. return date("Y-m-d H:i:s");
  108. }
  109. }
  110. if (!function_exists('php_left')) {
  111. function php_left($string, $num) {
  112. return substr($string, 0, $num);
  113. }
  114. }
  115. if (!function_exists('php_right')) {
  116. function php_right($string, $num) {
  117. return substr($string, (strlen($string)-$num), strlen($string));
  118. }
  119. }
  120. if (!function_exists('php_sqlite_data_type')) {
  121. function php_sqlite_data_type($string, $field) {
  122. //get the string between the start and end characters
  123. $start = '(';
  124. $end = ')';
  125. $ini = stripos($string,$start);
  126. if ($ini == 0) return "";
  127. $ini += strlen($start);
  128. $len = stripos($string,$end,$ini) - $ini;
  129. $string = substr($string,$ini,$len);
  130. $str_data_type = '';
  131. $string_array = explode(',', $string);
  132. foreach($string_array as $lnvalue) {
  133. $fieldlistarray = explode (" ", $value);
  134. unset($fieldarray, $string, $field);
  135. }
  136. return $str_data_type;
  137. }
  138. } //end function
  139. //database connection
  140. try {
  141. //$db = new PDO('sqlite2:example.db'); //sqlite 2
  142. //$db = new PDO('sqlite::memory:'); //sqlite 3
  143. $db = new PDO('sqlite:'.realpath($db_path).'/'.$db_name); //sqlite 3
  144. //add additional functions to SQLite so that they are accessible inside SQL
  145. //bool PDO::sqliteCreateFunction ( string function_name, callback callback [, int num_args] )
  146. $db->sqliteCreateFunction('md5', 'phpmd5', 1);
  147. $db->sqliteCreateFunction('unix_timestamp', 'php_unix_timestamp', 1);
  148. $db->sqliteCreateFunction('now', 'phpnow', 0);
  149. $db->sqliteCreateFunction('sqlitedatatype', 'php_sqlite_data_type', 2);
  150. $db->sqliteCreateFunction('strleft', 'php_left', 2);
  151. $db->sqliteCreateFunction('strright', 'php_right', 2);
  152. }
  153. catch (PDOException $error) {
  154. print "error: " . $error->getMessage() . "<br/>";
  155. die();
  156. }
  157. } //end if db_type sqlite
  158. if ($db_type == "mysql") {
  159. //database connection
  160. try {
  161. //required for mysql_real_escape_string
  162. if (function_exists(mysql_connect)) {
  163. $mysql_connection = mysql_connect($db_host, $db_username, $db_password);
  164. }
  165. //mysql pdo connection
  166. if (strlen($db_host) == 0 && strlen($db_port) == 0) {
  167. //if both host and port are empty use the unix socket
  168. $db = new PDO("mysql:host=$db_host;unix_socket=/var/run/mysqld/mysqld.sock;dbname=$db_name", $db_username, $db_password);
  169. }
  170. else {
  171. if (strlen($db_port) == 0) {
  172. //leave out port if it is empty
  173. $db = new PDO("mysql:host=$db_host;dbname=$db_name;", $db_username, $db_password, array(
  174. PDO::ATTR_ERRMODE,
  175. PDO::ERRMODE_EXCEPTION
  176. ));
  177. }
  178. else {
  179. $db = new PDO("mysql:host=$db_host;port=$db_port;dbname=$db_name;", $db_username, $db_password, array(
  180. PDO::ATTR_ERRMODE,
  181. PDO::ERRMODE_EXCEPTION
  182. ));
  183. }
  184. }
  185. }
  186. catch (PDOException $error) {
  187. print "error: " . $error->getMessage() . "<br/>";
  188. die();
  189. }
  190. } //end if db_type mysql
  191. if ($db_type == "pgsql") {
  192. //database connection
  193. try {
  194. if (strlen($db_host) > 0) {
  195. if (strlen($db_port) == 0) { $db_port = "5432"; }
  196. $db = new PDO("pgsql:host=$db_host port=$db_port dbname=$db_name user=$db_username password=$db_password");
  197. }
  198. else {
  199. $db = new PDO("pgsql:dbname=$db_name user=$db_username password=$db_password");
  200. }
  201. }
  202. catch (PDOException $error) {
  203. print "error: " . $error->getMessage() . "<br/>";
  204. die();
  205. }
  206. } //end if db_type pgsql
  207. if ($db_type == "odbc") {
  208. //database connection
  209. try {
  210. unset($db);
  211. $db = new PDO("odbc:$db_name", "$db_username", "$db_password");
  212. }
  213. catch (PDOException $e) {
  214. echo 'Connection failed: ' . $e->getMessage();
  215. }
  216. } //end if db_type odbc
  217. ?>