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