1<?php
2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
3//
4// All Rights Reserved. See copyright.txt for details and a complete list of authors.
5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
6// $Id$
7
8require_once(dirname(__DIR__) . '/init/initlib.php');
9
10class TikiSetup extends TikiInit
11{
12	/*!
13		Check that everything is set up properly
14
15		\static
16	*/
17	static function check($tikidomain = '')
18	{
19		static $checked;
20
21		if ($checked) {
22			return;
23		}
24
25		$checked = true;
26
27
28		if (isset($_SERVER['SERVER_SOFTWARE']) && strpos($_SERVER['SERVER_SOFTWARE'], 'IIS') == true) {
29			if (array_key_exists('SCRIPT_FILENAME', $_SERVER)) {
30				$docroot = dirname($_SERVER['SCRIPT_FILENAME']);
31			} elseif (array_key_exists('PATH_TRANSLATED', $_SERVER)) {
32				$docroot = dirname($_SERVER['PATH_TRANSLATED']);
33			} else {
34				$docroot = getcwd();
35			}
36		} else {
37			$docroot = getcwd();
38		}
39
40		$errors = self::checkSession();
41
42		$wwwuser = '';
43		$wwwgroup = '';
44
45		if (TikiSetup::isWindows()) {
46			$wwwuser = 'SYSTEM';
47			$wwwgroup = 'SYSTEM';
48		}
49
50		if (function_exists('posix_getuid')) {
51			$user = @posix_getpwuid(@posix_getuid());
52
53			$group = @posix_getpwuid(@posix_getgid());
54			$wwwuser = $user ? $user['name'] : false;
55			$wwwgroup = $group ? $group['name'] : false;
56		}
57
58		if (! $wwwuser) {
59			$wwwuser = 'nobody (or the user account the web server is running under)';
60		}
61
62		if (! $wwwgroup) {
63			$wwwgroup = 'nobody (or the group account the web server is running under)';
64		}
65
66		static $dirs = [
67				'dump',
68				'img/wiki',
69				'img/wiki_up',
70				'modules/cache',
71				'temp',
72				'temp/templates_c',
73				# 'var',
74				# 'var/log',
75				# 'var/log/irc',
76		];
77
78		foreach ($dirs as $dir) {
79			if (! is_dir("$docroot/$dir/$tikidomain")) {
80				$errors .= "The directory '$docroot/$dir/$tikidomain' does not exist.\n";
81			} else {
82				if (! TikiSetup::is_writeable("$docroot/$dir/$tikidomain")) {
83					$errors .= "The directory '$docroot/$dir/$tikidomain' is not writeable by $wwwuser.\n";
84				}
85			}
86		}
87
88		if ($errors) {
89			$PHP_CONFIG_FILE_PATH = PHP_CONFIG_FILE_PATH;
90
91			ob_start();
92			phpinfo(INFO_MODULES);
93			$httpd_conf = 'httpd.conf';
94
95			if (preg_match('/Server Root<\/b><\/td><td\s+align="left">([^<]*)</', ob_get_contents(), $m)) {
96				$httpd_conf = $m[1] . '/' . $httpd_conf;
97			}
98
99			ob_end_clean();
100
101			print "
102<html><body>
103<h2><font color='red'>Tiki is not properly set up:</font></h2>
104<pre>
105$errors
106";
107			if ($tikidomain) {
108				$install_link = '?multi=' . urlencode($tikidomain);
109			}
110
111			if (! TikiSetup::isWindows()) {
112				print "Your options:
113
114
115	1- With FTP access:
116		a) Change the permissions (chmod) of the directories to 777.
117		b) Create any missing directories
118		c) <a href='tiki-install.php$install_link'>Execute the Tiki installer again</a> (Once you have executed these commands, this message will disappear!)
119
120	or
121
122	2- With shell (SSH) access, you can run the command below.
123
124		a) Run setup.sh and follow the instructions:
125			\$ bash
126			\$ cd $docroot
127			\$ sh setup.sh
128
129		The script will offer you options depending on your server configuration.
130
131		b) <a href='tiki-install.php$install_link'>Execute the Tiki installer again</a> (Once you have executed these commands, this message will disappear!)
132
133
134<hr>
135If you have problems accessing a directory, check the open_basedir entry in
136$PHP_CONFIG_FILE_PATH/php.ini or $httpd_conf.
137
138<hr>
139
140<a href='http://doc.tiki.org/Installation' target='_blank'>Consult the tiki.org installation guide</a> if you need more help or <a href='http://tiki.org/tiki-forums.php' target='_blank'>visit the forums</a>
141
142</pre></body></html>";
143			}
144			exit;
145		}
146	}
147
148	/**
149	 * Checks if we're using files for the sessions and checks the dir is accessible
150	 * But only if the session has not been started yet.
151	 *
152	 * @return string errors if present
153	 */
154	public static function checkSession()
155	{
156		$errors = '';
157
158		if (ini_get('session.save_handler') == 'files') {
159			$save_path = ini_get('session.save_path');
160			if (empty($save_path)) {
161				$save_path = session_save_path();
162				if (empty($save_path)) {
163					$save_path = sys_get_temp_dir();
164				}
165			}
166			// check if we can check it. The session.save_path can be outside
167			// the open_basedir paths.
168			$open_basedir = ini_get('open_basedir');
169			if (empty($open_basedir)) {
170				if (! is_dir($save_path)) {
171					$errors .= "The directory '$save_path' does not exist or PHP is not allowed to access it (check open_basedir entry in php.ini).\n";
172				} elseif (! TikiSetup::is_writeable($save_path)) {
173					$errors .= "The directory '$save_path' is not writeable.\n";
174				}
175			}
176
177			if ($errors) {
178				$save_path = sys_get_temp_dir();
179
180				if (is_dir($save_path) && TikiSetup::is_writeable($save_path) && session_status() !== PHP_SESSION_ACTIVE) {
181					session_save_path($save_path);
182					$errors = '';
183				}
184			}
185		}
186		return $errors;
187	}
188}
189