1<?php
2/*
3	FusionPBX
4	Version: MPL 1.1
5
6	The contents of this file are subject to the Mozilla Public License Version
7	1.1 (the "License"); you may not use this file except in compliance with
8	the License. You may obtain a copy of the License at
9	http://www.mozilla.org/MPL/
10
11	Software distributed under the License is distributed on an "AS IS" basis,
12	WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13	for the specific language governing rights and limitations under the
14	License.
15
16	The Original Code is FusionPBX
17
18	The Initial Developer of the Original Code is
19	Mark J Crane <markjcrane@fusionpbx.com>
20	Portions created by the Initial Developer are Copyright (C) 2008-2012
21	the Initial Developer. All Rights Reserved.
22
23	Contributor(s):
24	Mark J Crane <markjcrane@fusionpbx.com>
25*/
26
27// make sure the PATH_SEPARATOR is defined
28	umask(2);
29	if (!defined("PATH_SEPARATOR")) {
30		if (strpos($_ENV["OS"], "Win") !== false) {
31			define("PATH_SEPARATOR", ";");
32		} else {
33			define("PATH_SEPARATOR", ":");
34		}
35	}
36
37	if (!isset($output_format)) $output_format = (PHP_SAPI == 'cli') ? 'text' : 'html';
38
39	// make sure the document_root is set
40	$_SERVER["SCRIPT_FILENAME"] = str_replace("\\", '/', $_SERVER["SCRIPT_FILENAME"]);
41	if(PHP_SAPI == 'cli'){
42		chdir(pathinfo(realpath($_SERVER["PHP_SELF"]), PATHINFO_DIRNAME));
43		$script_full_path = str_replace("\\", '/', getcwd() . '/' . $_SERVER["SCRIPT_FILENAME"]);
44		$dirs = explode('/', pathinfo($script_full_path, PATHINFO_DIRNAME));
45		if (file_exists('/project_root.php')) {
46			$path = '/';
47		} else {
48			$i    = 1;
49			$path = '';
50			while ($i < count($dirs)) {
51				$path .= '/' . $dirs[$i];
52				if (file_exists($path. '/project_root.php')) {
53					break;
54				}
55				$i++;
56			}
57		}
58		$_SERVER["DOCUMENT_ROOT"] = $path;
59	}else{
60		$_SERVER["DOCUMENT_ROOT"]   = str_replace($_SERVER["PHP_SELF"], "", $_SERVER["SCRIPT_FILENAME"]);
61	}
62	$_SERVER["DOCUMENT_ROOT"]   = realpath($_SERVER["DOCUMENT_ROOT"]);
63// try to detect if a project path is being used
64	if (!defined('PROJECT_PATH')) {
65		if (is_dir($_SERVER["DOCUMENT_ROOT"]. '/fusionpbx')) {
66			define('PROJECT_PATH', '/fusionpbx');
67		} elseif (file_exists($_SERVER["DOCUMENT_ROOT"]. '/project_root.php')) {
68			define('PROJECT_PATH', '');
69		} else {
70			$dirs = explode('/', str_replace('\\', '/', pathinfo($_SERVER["PHP_SELF"], PATHINFO_DIRNAME)));
71			$i    = 1;
72			$path = $_SERVER["DOCUMENT_ROOT"];
73			while ($i < count($dirs)) {
74				$path .= '/' . $dirs[$i];
75				if (file_exists($path. '/project_root.php')) {
76					break;
77				}
78				$i++;
79			}
80			if(!file_exists($path. '/project_root.php')){
81				die("Failed to locate the Project Root by searching for project_root.php please contact support for assistance");
82			}
83			$project_path = str_replace($_SERVER["DOCUMENT_ROOT"], "", $path);
84			define('PROJECT_PATH', $project_path);
85		}
86		$_SERVER["PROJECT_ROOT"] = realpath($_SERVER["DOCUMENT_ROOT"] . PROJECT_PATH);
87		set_include_path(get_include_path() . PATH_SEPARATOR . $_SERVER["PROJECT_ROOT"]);
88	}
89
90?>