pdo.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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-2016
  17. the Initial Developer. All Rights Reserved.
  18. Contributor(s):
  19. Mark J Crane <[email protected]>
  20. Raymond Chandler <[email protected]>
  21. */
  22. //set the include path
  23. $conf = glob("{/usr/local/etc,/etc}/fusionpbx/config.conf", GLOB_BRACE);
  24. set_include_path(parse_ini_file($conf[0])['document.root']);
  25. //includes files
  26. require_once "resources/functions.php";
  27. //set defaults
  28. if (isset($dbtype)) {
  29. $db_type = $dbtype;
  30. }
  31. if (isset($dbhost)) {
  32. $db_host = $dbhost;
  33. }
  34. if (isset($dbport)) {
  35. $db_port = $dbport;
  36. }
  37. if (isset($dbname)) {
  38. $db_name = $dbname;
  39. }
  40. if (isset($dbusername)) {
  41. $db_username = $dbusername;
  42. }
  43. if (isset($dbpassword)) {
  44. $db_password = $dbpassword;
  45. }
  46. if (isset($db_file_path)) {
  47. $db_path = $db_file_path;
  48. }
  49. if (isset($dbfilename)) {
  50. $db_name = $dbfilename;
  51. }
  52. if (isset($dbsecure)) {
  53. $db_secure = $dbsecure;
  54. }
  55. if (isset($dbcertauthority)) {
  56. $db_cert_authority = $dbcertauthority;
  57. }
  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. //set the document_root
  98. if (empty($document_root)) {
  99. $document_root = $_SERVER["DOCUMENT_ROOT"];
  100. }
  101. //prepare the database connection
  102. if (empty($db_name)) {
  103. //if (empty($_SERVER["SERVER_NAME"])) { $_SERVER["SERVER_NAME"] = "http://localhost"; }
  104. $server_name = $_SERVER["SERVER_NAME"];
  105. $server_name = str_replace ("www.", "", $server_name);
  106. //$server_name = str_replace (".", "_", $server_name);
  107. $db_name_short = $server_name;
  108. $db_name = $server_name.'.db';
  109. }
  110. else {
  111. $db_name_short = $db_name;
  112. }
  113. $db_path = realpath($db_path);
  114. if (file_exists($db_path.'/'.$db_name)) {
  115. //echo "database file exists<br>";
  116. }
  117. else {
  118. if (is_writable($db_path.'/'.$db_name)) {
  119. //use database in current location
  120. }
  121. else {
  122. //not writable
  123. echo "The database ".$db_path."/".$db_name." does not exist or is not writable.";
  124. exit;
  125. }
  126. }
  127. if (!function_exists('php_md5')) {
  128. function php_md5($string) {
  129. return md5($string);
  130. }
  131. }
  132. if (!function_exists('php_unix_timestamp')) {
  133. function php_unix_timestamp($string) {
  134. return strtotime($string);
  135. }
  136. }
  137. if (!function_exists('php_now')) {
  138. function php_now() {
  139. return date("Y-m-d H:i:s");
  140. }
  141. }
  142. if (!function_exists('php_left')) {
  143. function php_left($string, $num) {
  144. return substr($string, 0, $num);
  145. }
  146. }
  147. if (!function_exists('php_right')) {
  148. function php_right($string, $num) {
  149. return substr($string, (strlen($string)-$num), strlen($string));
  150. }
  151. }
  152. if (!function_exists('php_sqlite_data_type')) {
  153. function php_sqlite_data_type($string, $field) {
  154. //get the string between the start and end characters
  155. $start = '(';
  156. $end = ')';
  157. $ini = stripos($string,$start);
  158. if ($ini == 0) return "";
  159. $ini += strlen($start);
  160. $len = stripos($string,$end,$ini) - $ini;
  161. $string = substr($string,$ini,$len);
  162. $str_data_type = '';
  163. $string_array = explode(',', $string);
  164. foreach($string_array as $lnvalue) {
  165. $fieldlistarray = explode (" ", $value);
  166. unset($fieldarray, $string, $field);
  167. }
  168. return $str_data_type;
  169. }
  170. }
  171. //database connection
  172. try {
  173. //create the database connection object
  174. //$db = new PDO('sqlite2:example.db'); //sqlite 2
  175. //$db = new PDO('sqlite::memory:'); //sqlite 3
  176. $db = new PDO('sqlite:'.$db_path.'/'.$db_name); //sqlite 3
  177. //enable foreign key constraints
  178. $db->query('PRAGMA foreign_keys = ON;');
  179. //add additional functions to SQLite so that they are accessible inside SQL
  180. //bool PDO::sqliteCreateFunction ( string function_name, callback callback [, int num_args] )
  181. $db->sqliteCreateFunction('md5', 'php_md5', 1);
  182. $db->sqliteCreateFunction('unix_timestamp', 'php_unix_timestamp', 1);
  183. $db->sqliteCreateFunction('now', 'php_now', 0);
  184. $db->sqliteCreateFunction('sqlitedatatype', 'php_sqlite_data_type', 2);
  185. $db->sqliteCreateFunction('strleft', 'php_left', 2);
  186. $db->sqliteCreateFunction('strright', 'php_right', 2);
  187. }
  188. catch (PDOException $error) {
  189. print "error: " . $error->getMessage() . "<br/>";
  190. die();
  191. }
  192. } //end if db_type sqlite
  193. if ($db_type == "mysql") {
  194. //database connection
  195. try {
  196. //required for mysql_real_escape_string
  197. if (function_exists('mysql_connect')) {
  198. $mysql_connection = @mysql_connect($db_host, $db_username, $db_password);
  199. //$mysql_connection = mysqli_connect($db_host, $db_username, $db_password,$db_name) or die("Error " . mysqli_error($link));
  200. }
  201. //mysql pdo connection
  202. if (strlen($db_host) == 0 && empty($db_port)) {
  203. //if both host and port are empty use the unix socket
  204. $db = new PDO("mysql:host=$db_host;unix_socket=/var/run/mysqld/mysqld.sock;dbname=$db_name;charset=utf8;", $db_username, $db_password);
  205. }
  206. else {
  207. if (empty($db_port)) {
  208. //leave out port if it is empty
  209. $db = new PDO("mysql:host=$db_host;dbname=$db_name;charset=utf8;", $db_username, $db_password, array(
  210. PDO::ATTR_ERRMODE,
  211. PDO::ERRMODE_EXCEPTION
  212. ));
  213. }
  214. else {
  215. $db = new PDO("mysql:host=$db_host;port=$db_port;dbname=$db_name;charset=utf8;", $db_username, $db_password, array(
  216. PDO::ATTR_ERRMODE,
  217. PDO::ERRMODE_EXCEPTION
  218. ));
  219. }
  220. }
  221. }
  222. catch (PDOException $error) {
  223. print "error: " . $error->getMessage() . "<br/>";
  224. die();
  225. }
  226. } //end if db_type mysql
  227. if ($db_type == "pgsql") {
  228. //database connection
  229. try {
  230. if (!isset($db_secure)) {
  231. $db_secure = false;
  232. }
  233. if (!empty($db_host)) {
  234. if (empty($db_port)) { $db_port = "5432"; }
  235. if ($db_secure == true) {
  236. $db = new PDO("pgsql:host=$db_host port=$db_port dbname=$db_name user=$db_username password=$db_password sslmode=verify-ca sslrootcert=$db_cert_authority");
  237. }
  238. else {
  239. $db = new PDO("pgsql:host=$db_host port=$db_port dbname=$db_name user=$db_username password=$db_password");
  240. }
  241. }
  242. else {
  243. $db = new PDO("pgsql:dbname=$db_name user=$db_username password=$db_password");
  244. }
  245. }
  246. catch (PDOException $error) {
  247. print "error: " . $error->getMessage() . "<br/>";
  248. die();
  249. }
  250. } //end if db_type pgsql
  251. if ($db_type == "odbc") {
  252. //database connection
  253. try {
  254. $db = new PDO("odbc:".$db_name);
  255. }
  256. catch (PDOException $error) {
  257. print "error: " . $error->getMessage() . "<br/>";
  258. die();
  259. }
  260. } //end if db_type pgsql
  261. //get the domain list
  262. if (!is_array($_SESSION['domains']) or !isset($_SESSION["domain_uuid"])) {
  263. //get the domain
  264. $domain_array = explode(":", $_SERVER["HTTP_HOST"]);
  265. //get the domains from the database
  266. $sql = "select * from v_domains";
  267. $prep_statement = $db->prepare($sql);
  268. $prep_statement->execute();
  269. $result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
  270. foreach($result as $row) {
  271. $domain_names[] = $row['domain_name'];
  272. }
  273. unset($prep_statement);
  274. //put the domains in natural order
  275. if (is_array($domain_names)) {
  276. natsort($domain_names);
  277. }
  278. //build the domains array in the correct order
  279. if (is_array($domain_names)) {
  280. foreach ($domain_names as $dn) {
  281. foreach ($result as $row) {
  282. if ($row['domain_name'] == $dn) {
  283. $domains[] = $row;
  284. }
  285. }
  286. }
  287. unset($result);
  288. }
  289. if (is_array($domains)) {
  290. foreach($domains as $row) {
  291. if (!isset($_SESSION['username'])) {
  292. if (count($domains) == 1) {
  293. $_SESSION["domain_uuid"] = $row["domain_uuid"];
  294. $_SESSION["domain_name"] = $row['domain_name'];
  295. }
  296. else {
  297. if ($row['domain_name'] == $domain_array[0] || $row['domain_name'] == 'www.'.$domain_array[0]) {
  298. $_SESSION["domain_uuid"] = $row["domain_uuid"];
  299. $_SESSION["domain_name"] = $row["domain_name"];
  300. }
  301. }
  302. }
  303. $_SESSION['domains'][$row['domain_uuid']] = $row;
  304. }
  305. unset($domains, $prep_statement);
  306. }
  307. }
  308. //get the software name
  309. if (!isset($_SESSION["software_name"])) {
  310. $sql = "select * from v_software ";
  311. $prep_statement = $db->prepare(check_sql($sql));
  312. if ($prep_statement) {
  313. $prep_statement->execute();
  314. $row = $prep_statement->fetch(PDO::FETCH_ASSOC);
  315. $_SESSION["software_name"] = $row['software_name'];
  316. }
  317. unset($prep_statement, $result);
  318. }
  319. //set the setting arrays
  320. if (!isset($_SESSION['domain']['menu'])) {
  321. $domain = new domains();
  322. $domain->set();
  323. }
  324. //set the domain_uuid variable from the session
  325. if (!empty($_SESSION["domain_uuid"])) {
  326. $domain_uuid = $_SESSION["domain_uuid"];
  327. }
  328. else {
  329. $domain_uuid = uuid();
  330. }
  331. ?>