v_sql_query_pdo.php 6.4 KB

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