Fork of FusionPBX but with LDAP kinda working
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

350 lines
9.8 KiB

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