sql_query_pdo.php 6.7 KB

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