sql_query_pdo.php 6.7 KB

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