sql_query_pdo.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. //set the include path
  22. $conf = glob("{/usr/local/etc,/etc}/fusionpbx/config.conf", GLOB_BRACE);
  23. set_include_path(parse_ini_file($conf[0])['document.root']);
  24. //includes files
  25. require_once "resources/require.php";
  26. require_once "resources/check_auth.php";
  27. //check permissions
  28. if (permission_exists('sql_query')) {
  29. //access granted
  30. }
  31. else {
  32. echo "access denied";
  33. exit;
  34. }
  35. //set the default values
  36. if (isset($db_file_path) > 0) {
  37. $db_path = $db_file_path;
  38. $db_name = $dbfilename;
  39. }
  40. //get the db connection information
  41. if (is_uuid($_REQUEST['id'])) {
  42. $sql = "select * from v_databases ";
  43. $sql .= "where database_uuid = :database_uuid ";
  44. $parameters['database_uuid'] = $_REQUEST['id'];
  45. $database = new database;
  46. $row = $database->select($sql, $parameters, 'row');
  47. if (is_array($row) && @sizeof($row) != 0) {
  48. $db_type = $row["database_type"];
  49. $db_host = $row["database_host"];
  50. $db_port = $row["database_port"];
  51. $db_name = $row["database_name"];
  52. $db_username = $row["database_username"];
  53. $db_password = $row["database_password"];
  54. $db_path = $row["database_path"];
  55. }
  56. unset($sql, $parameters, $row);
  57. }
  58. //unset the database connection
  59. unset($db);
  60. if (!function_exists('get_db_field_names')) {
  61. function get_db_field_names($db, $table, $db_name='fusionpbx') {
  62. $query = sprintf('SELECT * FROM %s LIMIT 1', $table);
  63. foreach ($db->query($query, PDO::FETCH_ASSOC) as $row) {
  64. return array_keys($row);
  65. }
  66. // if we're still here, we need to try something else
  67. $fields = array();
  68. $driver = $db->getAttribute(PDO::ATTR_DRIVER_NAME);
  69. if ($driver == 'sqlite') {
  70. $query = sprintf("Pragma table_info(%s);", $table);
  71. $stmt = $db->prepare($query);
  72. $result = $stmt->execute();
  73. $rows = $stmt->fetchAll(PDO::FETCH_NAMED);
  74. //printf('<pre>%s</pre>', print_r($rows, true));
  75. $row_count = count($rows);
  76. //printf('<pre>%s</pre>', print_r($rows, true));
  77. for ($i = 0; $i < $row_count; $i++) {
  78. array_push($fields, $rows[$i]['name']);
  79. }
  80. return $fields;
  81. } else {
  82. $query = sprintf("SELECT * FROM information_schema.columns
  83. WHERE table_schema='%s' AND table_name='%s';"
  84. , $db_name, $table
  85. );
  86. $stmt = $db->prepare($query);
  87. $result = $stmt->execute();
  88. $rows = $stmt->fetchAll(PDO::FETCH_NAMED);
  89. $row_count = count($rows);
  90. //printf('<pre>%s</pre>', print_r($rows, true));
  91. for ($i = 0; $i < $row_count; $i++) {
  92. array_push($fields, $rows[$i]['COLUMN_NAME']);
  93. }
  94. return $fields;
  95. }
  96. }
  97. }
  98. if ($db_type == "sqlite") {
  99. if (!function_exists('phpmd5')) {
  100. function phpmd5($string) {
  101. return md5($string);
  102. }
  103. }
  104. if (!function_exists('php_unix_timestamp')) {
  105. function php_unix_timestamp($string) {
  106. return strtotime($string);
  107. }
  108. }
  109. if (!function_exists('phpnow')) {
  110. function phpnow() {
  111. return date("Y-m-d H:i:s");
  112. }
  113. }
  114. if (!function_exists('php_left')) {
  115. function php_left($string, $num) {
  116. return substr($string, 0, $num);
  117. }
  118. }
  119. if (!function_exists('php_right')) {
  120. function php_right($string, $num) {
  121. return substr($string, (strlen($string)-$num), strlen($string));
  122. }
  123. }
  124. if (!function_exists('php_sqlite_data_type')) {
  125. function php_sqlite_data_type($string, $field) {
  126. //get the string between the start and end characters
  127. $start = '(';
  128. $end = ')';
  129. $ini = stripos($string,$start);
  130. if ($ini == 0) return "";
  131. $ini += strlen($start);
  132. $len = stripos($string,$end,$ini) - $ini;
  133. $string = substr($string,$ini,$len);
  134. $str_data_type = '';
  135. $string_array = explode(',', $string);
  136. foreach($string_array as $lnvalue) {
  137. $fieldlistarray = explode (" ", $value);
  138. unset($fieldarray, $string, $field);
  139. }
  140. return $str_data_type;
  141. }
  142. } //end function
  143. //database connection
  144. try {
  145. //$db = new PDO('sqlite2:example.db'); //sqlite 2
  146. //$db = new PDO('sqlite::memory:'); //sqlite 3
  147. $db = new PDO('sqlite:'.realpath($db_path).'/'.$db_name); //sqlite 3
  148. //add additional functions to SQLite so that they are accessible inside SQL
  149. //bool PDO::sqliteCreateFunction ( string function_name, callback callback [, int num_args] )
  150. $db->sqliteCreateFunction('md5', 'phpmd5', 1);
  151. $db->sqliteCreateFunction('unix_timestamp', 'php_unix_timestamp', 1);
  152. $db->sqliteCreateFunction('now', 'phpnow', 0);
  153. $db->sqliteCreateFunction('sqlitedatatype', 'php_sqlite_data_type', 2);
  154. $db->sqliteCreateFunction('strleft', 'php_left', 2);
  155. $db->sqliteCreateFunction('strright', 'php_right', 2);
  156. }
  157. catch (PDOException $error) {
  158. print "error: " . $error->getMessage() . "<br/>";
  159. die();
  160. }
  161. } //end if db_type sqlite
  162. if ($db_type == "mysql") {
  163. //database connection
  164. try {
  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. ?>