xref: /dokuwiki/install.php (revision 5cfb88156f790ba8677badc88863a85569149942)
1<?php
2/**
3 *  Dokuwiki installation assistance
4 *
5 *  @author      Chris Smith <chris@jalakai.co.uk>
6 */
7
8if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__)).'/');
9if(!defined('DOKU_CONF')) define('DOKU_CONF',DOKU_INC.'conf/');
10if(!defined('DOKU_LOCAL')) define('DOKU_LOCAL',DOKU_INC.'conf/');
11
12if(!defined('DEBUG')) define('DEBUG', false);
13
14// ------------------------------------------------------------------------------------
15// important settings ...
16// installation dependent local config file list
17$config_files = array(
18  'local' => DOKU_LOCAL.'local.php',
19  'users' => DOKU_LOCAL.'users.auth.php',
20  'auth'  => DOKU_LOCAL.'acl.auth.php'
21);
22
23// other installation dir/file permission requirements
24$install_permissions = array(
25  'data'      => 'data',
26  'pages'     => 'data/pages',
27  'attic'     => 'data/attic',
28  'media'     => 'data/media',
29  'meta'      => 'data/meta',
30  'cache'     => 'data/cache',
31  'locks'     => 'data/locks',
32  'changelog' => 'data/changes.log'
33);
34
35// array use to verify unchanged dokuwiki.php files, 'version' => 'md5 hash'
36$dokuwiki_php = DOKU_CONF.'dokuwiki.php';
37$dokuwiki_hash = array(
38  '2005-09-22' => 'e33223e957b0b0a130d0520db08f8fb7',
39  '2006-03-05' => '51295727f79ab9af309a2fd9e0b61acc',
40  '2006-03-09' => '51295727f79ab9af309a2fd9e0b61acc',
41);
42
43// language strings
44
45// ------------------------------------------------------------------------------------
46// initialise variables ...
47
48$msg = array();
49$error = array();
50$debug = array();
51$process_form = false;
52
53// form variables with default values
54$title       = "";
55$location    = true;
56$data        = "./data";
57$changeslog  = true;
58$acl         = true;
59$superuser   = "";
60$fullname    = "";
61$email       = "";
62
63// check for dokuwiki
64// (for now assume included with Dokuwiki install & resident in dokuwiki root folder)
65
66// ------------------------------------------------------------------------------------
67// check for virgin dokuwiki installation
68$virgin_install = true;
69
70  // $config_files mustn't exist
71  foreach ($config_files as $file) {
72    if (@file_exists($file)) {
73      $virgin_install = false;
74      $file = str_replace($_SERVER['DOCUMENT_ROOT'],'{DOCUMENT_ROOT}', $file);
75      $msg[] = "<span class=\"file\">$file</span> exists"; }
76  }
77
78  // main dokuwiki config file (conf/dokuwiki.php) must not have been modified
79  $installation_hash = md5(@file_get_contents($dokuwiki_php));
80  if (!in_array($installation_hash, $dokuwiki_hash)) {
81    $virgin_install = false;
82    $msg[] = "unrecognised or modified dokuwiki.php -- hash=$installation_hash";
83  }
84// ------------------------------------------------------------------------------------
85// check for other basic installation & configuration details (to be nice)
86
87$changeslog_exists = @file_exists(DOKU_INC.'data/changes.log');
88
89if (!is_writable(DOKU_CONF)) {
90  $file = str_replace($_SERVER['DOCUMENT_ROOT'],'{DOCUMENT_ROOT}', DOKU_CONF);
91  $error[] = "<span class=\"file\">$file</span> must be writable by the web server.";
92}
93
94//-------------------------------------------------------------------------------------
95// utility functions
96
97/**
98 * remove magic quotes recursivly
99 *
100 * @author Andreas Gohr <andi@splitbrain.org>
101 */
102function remove_magic_quotes(&$array) {
103  foreach (array_keys($array) as $key) {
104    if (is_array($array[$key])) {
105      remove_magic_quotes($array[$key]);
106    }else {
107      $array[$key] = stripslashes($array[$key]);
108    }
109  }
110}
111
112function cleanText($var, $default, $regex, $msg) {
113  global $error;
114
115  $value = isset($_REQUEST[$var]) ? $_REQUEST[$var] : $default;
116
117  if ($regex) {
118    if (!preg_match($regex, $value)) {
119      $error[] = "$var - illegal/unrecognised value";
120    }
121  }
122  return $value;
123}
124
125function fileWrite($name, $filename, $data) {
126  global $error;
127
128  if (($fp = @fopen($filename, 'wb')) === false) {
129    $filename = str_replace($_SERVER['DOCUMENT_ROOT'],'{DOCUMENT_ROOT}', $filename);
130    $error[] = "Unable to create $name (<span class=\"file\">$filename</span>).  You will need to check directory/file permissions and create the file manually.";
131    return false;
132  }
133
134  if (!empty($data)) { fwrite($fp, $data);  }
135  fclose($fp);
136  return true;
137}
138
139// ------------------------------------------------------------------------------------
140// form processing ...
141if (isset($_REQUEST['submit'])) {
142  if (!$virgin_install) {
143    $msg[] = "unable to apply updates, installation already modified";
144
145  } else {
146    // apply updates per form instructions
147    $process_form = true;
148
149    if (get_magic_quotes_gpc()) {
150      if (!empty($_REQUEST)) remove_magic_quotes($_REQUEST);
151    }
152
153    $title = cleanText('title', '', '');
154    $location = isset($_REQUEST['location']);
155    $data = cleanText('data', '', '');
156    $changeslog = isset($_REQUEST['changeslog']);
157    $acl = isset($_REQUEST['acl']);
158    $superuser = cleanText('superuser','','/\S+/', );
159    $password = cleanText('password','','/\S+/');
160    $confirm = cleanText('confirm','','/^'.preg_quote($password,'/').'$/');
161    $fullname = cleanText('fullname','','');
162    $email = cleanText('email','','');
163
164    $debug = compact('title','location','data','changeslog','acl','superuser','password','confirm');
165
166    if (empty($error)) {
167      // all incoming data is ok ... lets do ...
168      // create changes.log
169      if (!$changeslog_exists) {
170        $filename = realpath((empty($data) || ($data{0} != "/")) ? DOKU_INC.$data : $data).'/changes.log';
171        fileWrite('changeslog',$filename, '');
172      }
173
174      // create local.php
175      $output = "";
176      if (!empty($title)) $output .= '$conf[\'title\'] = \''.addslashes($title)."';\n";
177      if (!empty($data)) $output .= '$conf[\'data\'] = \''.$data."';\n";
178      if ($acl) $output .= '$conf[\'useacl\'] = 1'.";\n";
179      if (!empty($superuser)) $output .= '$conf[\'superuser\'] = \''.$superuser."';\n";
180
181      if (!empty($output)) {
182        $output = '<'.'?php
183/*
184 * Dokuwiki\'s Main Configuration File - Local Settings
185 * Auto-generated by install script
186 * Date: '.date('r').'
187 */'."\n".$output;
188           fileWrite('local configuration settings file',DOKU_LOCAL.'local.php',$output);
189       }
190
191      if ($acl) {
192        // create users.auth.php
193        // --- user:MD5password:Real Name:email:groups,comma,seperated
194        $output = (!empty($superuser)) ? join(":",array($superuser, md5($password), $fullname, $email, 'users')) : "";
195        $output = @file_get_contents(DOKU_CONF.'users.auth.php.dist')."\n$output\n";
196
197        fileWrite('acl user file', DOKU_LOCAL.'users.auth.php', $output);
198
199        // create acl.auth.php
200        $output = @file_get_contents(DOKU_CONF.'acl.auth.php.dist');
201        fileWrite('acl authorisations file', DOKU_LOCAL.'acl.auth.php', $output);
202      }
203    }
204  }
205}
206//-------------------------------------------------------------------------------------
207
208$show_form = !$process_form && $virgin_install && empty($error);
209
210?>
211<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
212<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
213<head>
214<meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8" />
215<title>Dokuwiki Installer</title>
216<style type="text/css">
217<!--/*--><![CDATA[/*><!--*/
218
219html {margin: 0; padding: 0;}
220body {
221  width: 90%;
222  margin: 0 auto;
223  font: 84% Verdana, Helvetica, Arial, sans-serif;
224}
225
226a {
227  white-space: nowrap;
228}
229
230img {
231  border: none;
232}
233
234.abbr {
235  border-bottom: 2px dotted #444;
236}
237
238.alert .file {
239  color: #a03333;
240}
241
242.error .file {
243  color: #c33;
244}
245
246h1 img {
247  vertical-align: middle;
248}
249
250form, fieldset {
251  margin: 1em 0;
252  padding: 0;
253  border: none;
254  width: 100%;
255}
256
257ul {
258  font-size: 80%;
259}
260
261form .field {
262  margin: 0.5em 0;
263}
264
265label {
266  display: block;
267}
268
269label span {
270  display: block;
271}
272
273label input.text {
274  width: 95%;
275}
276
277#instructions {
278  float: right;
279  width: 34%;
280}
281
282#details {
283  float: left;
284  width: 58%;
285}
286
287#process {
288  margin: 1.5em 0;
289}
290
291#debug, #footer {
292  clear: both;
293}
294
295#acl, #files {
296  border: 1px solid #ccc;
297  padding: 0.5em 0 1em 0;
298}
299
300fieldset.dependent {
301  margin-left: 2em;
302}
303
304
305/*]]>*/-->
306</style>
307<script type="text/javascript">
308<!--//--><![CDATA[//><!--
309
310
311//--><!]]>
312</script>
313</head>
314<body>
315<h1><img src="http://wiki.splitbrain.org/_media/wiki:dokuwiki-64.png" alt="" />Dokuwiki Installer</h1>
316<div id="instructions">
317  <p>This page assists in the installation and configuration of <a href="http://wiki.splitbrain.org">Dokuwiki</a>.</p>
318  <p>Dokuwiki uses ordinary files for the storage of wiki pages and other information associated with those pages
319  (e.g. images, search indexes, old revisions, etc).  In order to operate successfully Dokuwiki <strong>must</strong>
320  have write access to the directories that hold those files.  This installer is not capable of setting up directory
321  permissions, that normally needs to be done directly or if you are using hosting, through your hosting
322  control panel (e.g. cPanel).</p>
323  <p>This installer will setup your Dokuwiki configuration for <span class="abbr" title="access control list">ACL</span>,
324  which in turn allows administrator login and access to Dokuwiki's admin menu for installing plugins, managing
325  users, managing access to wiki pages and alteration of configuration settings.  It isn't required for Dokuwiki to
326  operate, however it will make Dokuwiki easier to administer.</p>
327  <p>Use these links for details concerning <a href="http://wiki.splitbrain.org/wiki:installation">installation instructions</a>
328  and <a href="http://wiki.splitbrain.org/wiki:configuration">configuration settings</a>.</p>
329</div>
330<div id="details">
331<?php if (!$virgin_install) { ?>
332  <p>Modified installation detected.</p>
333  <ul class="alert">
334<?php   foreach ($msg as $text) { ?>
335    <li><?php echo $text?></li>
336<?php   } ?>
337  </ul>
338  <p>For security reasons this script will only work with a new &amp; unmodified Dokuwiki installation.
339  You should either re-extract the files from the downloaded package or consult the complete
340  <a href="http://wiki.splitbrain.org/wiki:install">Dokuwiki installation instructions</a></p>
341<?php } /* end if (!virgin_install) */ ?>
342<?php if (!$process_form && !empty($error)) { ?>
343  <p>One or more incorrect directory/file permissions were found.</p>
344  <ul class="error">
345<?php   foreach ($error as $text) { ?>
346    <li><?php echo $text ?></li>
347<?php   } ?>
348  </ul>
349  <p>In order to complete this installation the above directories and files need to have their
350    permissions altered as indicated. Please correct the above problems before trying again.</p>
351<?php } /* end if (!$process_form && !empty($error)) */ ?>
352<?php if ($process_form) { ?>
353<?php   if (empty($error)) { ?>
354  <p>Configuration updated successfully.</p>
355  <p>Now that your initial dokuwiki configuration has been set you should delete this file to prevent its further use
356  which may damage your dokuwiki installation and/or configuration.</p>
357  <p>Use this link to visit your new <a href="doku.php" title="my new dokuWiki">wiki</a></p>
358<?php   } else { ?>
359  <p>The following errors were encountered ... </p>
360  <ul class="error">
361<?php     foreach ($error as $text) { ?>
362    <li><?php echo $text?></li>
363<?php     } ?>
364  </ul>
365  <p>return to <a href="install.php">installation form</a></p>
366<?php   } ?>
367<?php } ?>
368<?php if ($show_form) { ?>
369  <form action="" method="post">
370    <fieldset id="wiki">
371      <div class="field"><label><span> Wiki Name </span><input class="text" type="text" name="title" value="<?php echo $title ?>" /></label></div>
372      <fieldset id="acl">
373      <div class="field"><label><input class="checkbox" type="checkbox" name="acl" <?php echo(($acl ? 'checked="checked"' : ''));?> /> Enable ACL </label></div>
374        <fieldset class="dependent">
375          <div class="field"><label><span> Superuser </span><input class="text" type="text" name="superuser" value="<?php echo $superuser ?>" /></label></div>
376          <div class="field"><label><span> Full name </span><input class="text" type="text" name="fullname" value="<?php echo $fullname ?>" /></label></div>
377          <div class="field"><label><span> Email Address </span><input class="text" type="text" name="email" value="<?php echo $email ?>" /></label></div>
378          <div class="field"><label><span> Superuser password </span><input class="text" type="password" name="password" /></label></div>
379          <div class="field"><label><span> Confirm password </span><input class="text" type="password" name="confirm" /></label></div>
380        </fieldset>
381      </fieldset>
382      <fieldset id="files">
383        <div class="field"><label><input class="checkbox" type="checkbox" name="location" <?php echo(($location ? 'checked="checked"' : ''));?> />Use default wiki location</label></div>
384        <fieldset class="dependent">
385          <div class="field"><label><span> Wiki Location </span><input class="text" type="text" name="data" value="<?php echo $data ?>" /></label></div>
386        </fieldset>
387      </fieldset>
388<?php if (!$changeslog_exists) { ?>
389      <div class="field"><label><input class="checkbox" type="checkbox" name="changeslog" <?php echo(($changeslog ? 'checked="checked"' : ''));?> />Create changes.log file</label></div>
390<?php } ?>
391    </fieldset>
392    <fieldset id="process">
393      <input class="button" type="submit" name="submit" value="Process Configuration Changes" />
394    </fieldset>
395  </form>
396<?php } ?>
397</div><!-- #details -->
398<?php if (DEBUG) { ?>
399<div id="debug">
400  <pre>
401  <?php print_r($_REQUEST); print_r($debug); print_r($error); ?>
402  </pre>
403</div>
404<?php } ?>
405<div id="footer">
406  <a href="http://wiki.splitbrain.org"><img src="lib/tpl/default/images/button-dw.png" alt="powered by dokuwiki" /></a>
407  <a href="http://www.php.net"><img src="lib/tpl/default/images/button-php.gif" alt="powered by php" /></a>
408</div>
409</body>
410</html>