PageRenderTime 135ms CodeModel.GetById 40ms app.highlight 16ms RepoModel.GetById 75ms app.codeStats 0ms

/htdocs/install/inc.php

https://bitbucket.org/speedealing/speedealing
PHP | 344 lines | 215 code | 65 blank | 64 comment | 33 complexity | db07bc3fe18a3e7ecec61d2dbb40c817 MD5 | raw file
  1<?php
  2
  3/* Copyright (C) 2004		Rodolphe Quiedeville	<rodolphe@quiedeville.org>
  4 * Copyright (C) 2004		Benoit Mortier			<benoit.mortier@opensides.be>
  5 * Copyright (C) 2004		Sebastien DiCintio		<sdicintio@ressource-toi.org>
  6 * Copyright (C) 2007-2012	Laurent Destailleur		<eldy@users.sourceforge.net>
  7 * Copyright (C) 2007-2013	Regis Houssin			<regis.houssin@capnetworks.com>
  8 * Copyright (C) 2012-2013	Herve Prot				<herve.prot@symeos.com>
  9 * Copyright (C) 2012		Marcos GarcĂ­a
 10 *
 11 * This program is free software; you can redistribute it and/or modify
 12 * it under the terms of the GNU General Public License as published by
 13 * the Free Software Foundation; either version 3 of the License, or
 14 * (at your option) any later version.
 15 *
 16 * This program is distributed in the hope that it will be useful,
 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 19 * GNU General Public License for more details.
 20 *
 21 * You should have received a copy of the GNU General Public License
 22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 23 */
 24
 25// Just to define version DOL_VERSION
 26if (!defined('DOL_INC_FOR_VERSION_ERROR'))
 27    define('DOL_INC_FOR_VERSION_ERROR', '1');
 28require_once realpath(dirname(__FILE__)) . '/../filefunc.inc.php';
 29
 30require_once DOL_DOCUMENT_ROOT . '/core/class/translatestandalone.class.php';
 31require_once DOL_DOCUMENT_ROOT . '/core/lib/functions.lib.php';
 32require_once DOL_DOCUMENT_ROOT . '/core/lib/admin.lib.php';
 33require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php';
 34
 35// For couchdb
 36if (!class_exists('couch'))
 37	require DOL_DOCUMENT_ROOT . '/core/db/couchdb/lib/couch.php';
 38if (!class_exists('couchAdmin'))
 39	require DOL_DOCUMENT_ROOT . '/core/db/couchdb/lib/couchAdmin.php';
 40if (!class_exists('couchClient'))
 41	require DOL_DOCUMENT_ROOT . '/core/db/couchdb/lib/couchClient.php';
 42if (!class_exists('User'))
 43	require DOL_DOCUMENT_ROOT . '/user/class/user.class.php';
 44
 45
 46// Avoid warnings with strict mode E_STRICT
 47$conf = new stdClass(); // instantiate $conf explicitely
 48$conf->global = new stdClass();
 49$conf->file = new stdClass();
 50$conf->db = new stdClass();
 51
 52
 53// Correction PHP_SELF (ex pour apache via caudium) car PHP_SELF doit valoir URL relative
 54// et non path absolu.
 55if (isset($_SERVER["DOCUMENT_URI"]) && $_SERVER["DOCUMENT_URI"]) {
 56    $_SERVER["PHP_SELF"] = $_SERVER["DOCUMENT_URI"];
 57}
 58
 59// Define vars
 60$conffiletoshowshort = "conf.php";
 61// Define localization of conf file
 62$conffile = DOL_DOCUMENT_ROOT . '/conf/conf.php';
 63$conffiletoshow = "conf/conf.php";
 64
 65if (! defined('DONOTLOADCONF') && file_exists($conffile))
 66	include_once $conffile;	// Load conf file
 67
 68// Define DOL_URL_ROOT
 69// If defined (Ie: Apache with Linux)
 70if (isset($_SERVER["SCRIPT_URI"])) {
 71	$main_url_root = $_SERVER["SCRIPT_URI"];
 72}
 73// If defined (Ie: Apache with Caudium)
 74elseif (isset($_SERVER["SERVER_URL"]) && isset($_SERVER["DOCUMENT_URI"])) {
 75	$main_url_root = $_SERVER["SERVER_URL"] . $_SERVER["DOCUMENT_URI"];
 76}
 77// If SCRIPT_URI, SERVER_URL, DOCUMENT_URI not defined (Ie: Apache 2.0.44 for Windows)
 78else {
 79	$scheme = 'http';
 80	if (!empty($_SERVER["HTTP_HOST"]))
 81		$serverport = $_SERVER["HTTP_HOST"];
 82	else
 83		$serverport = $_SERVER["SERVER_NAME"];
 84	$main_url_root = $scheme . "://" . $serverport . $_SERVER["SCRIPT_NAME"];
 85}
 86// Clean proposed URL
 87$main_url_root = preg_replace('/\/$/', '', $main_url_root);     // Remove the /
 88$main_url_root = preg_replace('/\/index\.php$/', '', $main_url_root);  // Remove the /index.php
 89$main_url_root = preg_replace('/\/phpinfo\.php$/', '', $main_url_root);   // Remove the /phpinfo.php
 90$main_url_root = preg_replace('/\/prerequisite\.php$/', '', $main_url_root);   // Remove the /prerequisite.php
 91$main_url_root = preg_replace('/\/ajax$/', '', $main_url_root);   // Remove the /ajax
 92$main_url_root = preg_replace('/\/install$/', '', $main_url_root);   // Remove the /install
 93$uri = preg_replace('/^http(s?):\/\//i', '', $main_url_root);	// $uri contains url without http*
 94define('MAIN_SERVER_NAME', $uri);
 95$suburi = strstr($uri, '/');       // $suburi contains url without domain
 96if ($suburi == '/')
 97    $suburi = '';   // If $suburi is /, it is now ''
 98define('DOL_URL_ROOT', $suburi);    // URL relative root ('', '/dolibarr', ...)
 99
100// Security check
101$lockfile = DOL_DOCUMENT_ROOT . '/install/install.lock';
102if (file_exists($lockfile)) {
103	header("Location: " . DOL_URL_ROOT . "/");
104	exit;
105}
106
107// $langs object
108$langs = new TranslateStandalone(DOL_DOCUMENT_ROOT);
109if (GETPOST('lang'))
110    $langs->setDefaultLang(GETPOST('lang', 'alpha'));
111else
112    $langs->setDefaultLang('auto');
113
114if (!empty($_SESSION['db_json_files'])) {
115	$jsonfiles = $_SESSION['db_json_files'];
116} else {
117	// Get json files list
118	$jsonfiles = array();
119	// Get dict files
120	$fileslist = dol_dir_list(DOL_DOCUMENT_ROOT . '/install/couchdb/json', 'files');
121	foreach($fileslist as $file) {
122			$jsonfiles[$file['name']] = $file['fullname'];
123	}
124	// Stock in session for best performance
125	$_SESSION['db_json_files'] = $jsonfiles;
126}
127
128// Now we load forced value from install.forced.php file.
129$useforcedwizard=false;
130$forcedfile = DOL_DOCUMENT_ROOT . '/install/install.forced.php';
131if (@file_exists($forcedfile)) {
132	$useforcedwizard=true;
133	include $forcedfile;
134}
135
136
137/**
138 * Show HTML header of install pages
139 *
140 * @return	void
141 */
142function pHeader() {
143    global $conf, $langs;
144    global $jsonfiles;
145
146    $langs->load("main");
147    $langs->load("admin");
148
149    include 'tpl/header.tpl.php';
150}
151
152/**
153 * Print HTML footer of install pages
154 *
155 * @return	void
156 */
157function pFooter() {
158	global $langs;
159
160	include 'tpl/footer.tpl.php';
161}
162
163/**
164 *  Save configuration file. No particular permissions are set by installer.
165 *
166 *  @return	void
167 */
168function write_conf_file() {
169	global $conf, $langs;
170	global $couchdb_host, $couchdb_port, $force_https;
171	global $memcached_host, $memcached_port, $useforcedwizard, $force_install_urlrewrite;
172	global $conffile, $conffiletoshowshort;
173
174	$key = md5(uniqid(mt_rand(), TRUE)); // Generate random hash
175
176	$fp = fopen("$conffile", "w");
177	if ($fp) {
178		clearstatcache();
179
180		fputs($fp, '<?php' . "\n");
181		fputs($fp, '//' . "\n");
182		fputs($fp, '// File generated by Speedealing installer ' . "\n");
183		fputs($fp, '//' . "\n");
184		fputs($fp, '// Take a look at conf.php.example file for an example of ' . $conffiletoshowshort . ' file' . "\n");
185		fputs($fp, '// and explanations for all possibles parameters.' . "\n");
186		fputs($fp, '//' . "\n\n");
187
188		/* Authentication */
189
190		fputs($fp, '// Authentication (obsolete ?)');
191		fputs($fp, "\n");
192
193		fputs($fp, '$main_authentication=\'dolibarr\';');
194		fputs($fp, "\n");
195
196		fputs($fp, '// Authentication for backward compatibility');
197		fputs($fp, "\n");
198
199		fputs($fp, '$dolibarr_main_authentication=\'dolibarr\';');
200		fputs($fp, "\n\n");
201
202		/* CouchDB */
203
204		fputs($fp, '// Couchdb settings');
205		fputs($fp, "\n");
206
207		fputs($fp, '$main_couchdb_host=\'' . str_replace("'", "\'", $couchdb_host) . '\';');
208		fputs($fp, "\n");
209
210		fputs($fp, '$main_couchdb_port=\'' . str_replace("'", "\'", $couchdb_port) . '\';');
211		fputs($fp, "\n");
212
213		fputs($fp, '// Couchdb settings for backward compatibility');
214		fputs($fp, "\n");
215
216		fputs($fp, '$dolibarr_main_couchdb_host=\'' . str_replace("'", "\'", $couchdb_host) . '\';');
217		fputs($fp, "\n");
218
219		fputs($fp, '$dolibarr_main_couchdb_port=\'' . str_replace("'", "\'", $couchdb_port) . '\';');
220		fputs($fp, "\n\n");
221
222		/* Memcached */
223
224		if (!empty($memcached_host) && !empty($memcached_port)) {
225			fputs($fp, '// Memcached settings');
226			fputs($fp, "\n");
227
228			fputs($fp, '$main_memcached_host=\'' . str_replace("'", "\'", $memcached_host) . '\';');
229			fputs($fp, "\n");
230
231			fputs($fp, '$main_memcached_port=\'' . str_replace("'", "\'", $memcached_port) . '\';');
232			fputs($fp, "\n");
233
234			fputs($fp, '// Memcached settings for backward compatibility');
235			fputs($fp, "\n");
236
237			fputs($fp, '$dolibarr_main_memcached_host=\'' . str_replace("'", "\'", $memcached_host) . '\';');
238			fputs($fp, "\n");
239
240			fputs($fp, '$dolibarr_main_memcached_port=\'' . str_replace("'", "\'", $memcached_port) . '\';');
241			fputs($fp, "\n\n");
242		}
243
244		/* URL rewriting (multicompany) */
245
246		if ($useforcedwizard && !empty($force_install_urlrewrite)) {
247			fputs($fp, '// URL rewriting');
248			fputs($fp, "\n");
249
250			fputs($fp, '$main_urlrewrite=\'1\';');
251			fputs($fp, "\n");
252
253			fputs($fp, '// URL rewriting for backward compatibility');
254			fputs($fp, "\n");
255
256			fputs($fp, '$dolibarr_urlrewrite=\'1\';');
257			fputs($fp, "\n\n");
258		} else {
259			fputs($fp, '// URL rewriting');
260			fputs($fp, "\n");
261
262			fputs($fp, '$main_urlrewrite=\'0\';');
263			fputs($fp, "\n");
264
265			fputs($fp, '// URL rewriting for backward compatibility');
266			fputs($fp, "\n");
267
268			fputs($fp, '$dolibarr_urlrewrite=\'0\';');
269			fputs($fp, "\n\n");
270		}
271
272		/* Specific setting */
273
274		fputs($fp, '// Specific settings');
275		fputs($fp, "\n");
276
277		fputs($fp, '$main_prod=\'0\';');
278		fputs($fp, "\n");
279
280		fputs($fp, '$main_nocsrfcheck=\'0\';');
281		fputs($fp, "\n");
282
283		fputs($fp, '$main_force_https=\'' . (!empty($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == 'on'?1:0) . '\';');
284		fputs($fp, "\n");
285
286		fputs($fp, '$main_cookie_cryptkey=\'' . $key . '\';');
287		fputs($fp, "\n");
288
289		fputs($fp, '$mailing_limit_sendbyweb=\'0\';');
290		fputs($fp, "\n");
291
292		fputs($fp, '// Specific settings for backward compatibility');
293		fputs($fp, "\n");
294
295		fputs($fp, '$dolibarr_main_prod=\'0\';');
296		fputs($fp, "\n");
297
298		fputs($fp, '$dolibarr_nocsrfcheck=\'0\';');
299		fputs($fp, "\n");
300
301		fputs($fp, '$dolibarr_main_force_https=\'' . (!empty($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == 'on'?1:0) . '\';');
302		fputs($fp, "\n");
303
304		fputs($fp, '$dolibarr_main_cookie_cryptkey=\'' . $key . '\';');
305		fputs($fp, "\n");
306
307		fputs($fp, '$dolibarr_mailing_limit_sendbyweb=\'0\';');
308		fputs($fp, "\n");
309
310		fputs($fp, '?>');
311		fclose($fp);
312
313		if (file_exists("$conffile"))
314			return 1;
315		else
316			return -1;
317	}
318	else
319		return -2;
320}
321
322/**
323 *	Save lock file.
324 *
325 *  @return	void
326 */
327function write_lock_file() {
328	global $lockfile;
329
330	$force_install_lockinstall = 444;
331	$fp = fopen($lockfile, "w");
332	if ($fp) {
333		fwrite($fp, "This is a lock file to prevent use of install pages (set with permission " . $force_install_lockinstall . ")");
334		fclose($fp);
335		@chmod($lockfile, octdec($force_install_lockinstall));
336		if (file_exists("$lockfile"))
337			return 1;
338		else
339			return -1;
340	}
341	else
342		return -2;
343}
344?>