changed git call from https to git readonly
[atutor.git] / mods / wiki / tools / t_setupwiz.php
1 <?php
2 /*
3    Load [http://localhost/ewiki/tools/t_setupwiz.php] with your browser 
4    to generate an "ewiki.ini" file by using a simple configuration
5    wizard, which queries you about all the features (plugins) and
6    settings you wish to use.
7
8    Save the generated .ini file to disk and then load the "ini.php"
9    script instead of "config.php" or "ewiki.php".
10    
11    - not yet 100% compatible with the new *.meta data files
12    - beware of spaghetti code
13 */
14
15
16 #-- .meta info
17 include("../plugins/lib/pluginmetadata.php");
18 $_cached = !empty($_POST);
19 ewiki_pmd($_cached);
20
21 #-- defaults for the separately handled database settings in $db[]
22 if (!($db = $_REQUEST["db"])) {
23    $db = array(
24      "type" => NULL,
25      "server" => "localhost",
26      "dbname" => "test",
27      "table" => "ewiki",
28      "dir" => "/tmp",
29      "dba" => "/tmp/wiki.dbm",
30    );
31 }
32
33 #-- read in ewiki.ini, if one was uploaded (POST <input file>)
34 if ($li = $_FILES["load_ini"]["tmp_name"]) {
35    $ini = array();
36    $uu = preg_split('/^\[(\w+)\]/m', implode("",file($li)), -1, PREG_SPLIT_DELIM_CAPTURE);
37    for ($i=1; $i<=count($uu); $i+=2) {
38       $sect = $uu[$i];
39       preg_match_all('/^\s*(\w[^\s]+)\s*=[\t ]*(.+?)(\s;.+)?\s*$/m', $uu[$i+1], $rows);
40       foreach ($rows[1] as $r=>$name) {
41          $ini[$sect][$name][] = trim($rows[2][$r]);
42       }
43    }
44
45    #-- pre-set the separate $db[] hash
46    if ($ini["db"]) {
47       foreach ($ini["db"] as $i=>$val) {
48          $db[$i] = $val[0];
49    }  }
50 }
51
52 #-- data
53 $default_priority_level = array(
54    "core"=>"1", "required"=>"1",
55    "default"=>1, "standard"=>1,
56    "recommended"=>1, "important"=>1,
57 );
58
59
60 #-- heavily mixed list of features and options
61 #
62 # - an array of arrays
63 # - each entry gives a 'feature' or simply a text fragment
64 # - first level subarrays have following entries
65 #   [0] type setting, "!"=headline, "="=always_enabled_feature,
66 #       0=disabled, 1=enabled, "..."=text_fragment_only
67 #   [1] title
68 #   [2] text / description
69 #   [3] list of plugin file names (without .php)
70 #   [4] another subarray of option settings
71 # - an option setting subarray has following structure:
72 #   [0] <input> field type
73 #   [1] title
74 #   [2] EWIKI_ constant or $ewiki_ var name
75 #   [3] default setting (value)
76 #   [4] text / description
77 #   [5] options for <select> input, separated by "|" with "values=titles"
78 # - html text fragments can be inserted anywhere in titles or text and
79 #   description entries (used for "database selection" part)
80 #
81 #
82 $list = array();
83 foreach (ewiki_pmd_by_category() as $category=>$pmd) {
84    $list[] = array(
85       "!",
86       "$category",
87       "",
88    );
89    foreach ($pmd as $id=>$row) {
90       if (!ewiki_pmd_hidden($row)) {
91          $options = array();
92          foreach (explode("\n", $row["config"]) as $l) {
93             if ($l = trim($l)) {
94                $name = trim(strtok($l, "="));
95                $val = strtok("\n");
96                $comm = "";
97                if ($p = strpos($val, " //")) {
98                   $comm = substr($val, $p+3);
99                   $val = trim(trim(substr($val, 0, $p)), '"');
100                }
101                $options[] = array(
102                   preg_match("/^([01](\|[01])?)$/", $val) ? "checkbox" : (strpos($val, "|") ? "select" : "text"),
103                   $name,
104                   $name,
105                   strtok($val, "|="),
106                   $comm,
107                   $val,
108                );
109             }
110          }
111          $list[] = array(
112             ($uu = $default_priority_level[$row["priority"]]) ? $uu : 0,
113             htmlentities($row["title"]),
114             htmlentities($row["description"]),
115             $id,
116             $options,
117          );
118       }
119    }
120 }
121
122
123 array(
124       "...", "",
125       <<<EOT
126   <br>
127   <input type="radio" id="db-0" name="db[type]" value="none"><label for="db-0"> don't care</label>
128    <span class="feature-desc">If you don't need to open the database connection in the config.php - if a MySQL connection was already established somewhere else</span>
129    <br>
130   <br>
131   <input type="radio" id="db-1" name="db[type]" value="mysql"><label for="db-1"> built-in MySQL</label>   <br>
132   <input type="radio" id="db-2" name="db[type]" value="pgsql"><label for="db-2"> or PostgreSQL</label> <span class="feature-desc">(with anydb_ wrapper)</span>
133    <div class="option">
134       <label for="db-1-1">server </label><input type="text" id="db-1-1" name="db[server]" value="$db[server]"><br>
135       <label for="db-1-2">user name </label><input type="text" id="db-1-2" name="db[user]" value="$db[user]"><br>
136       <label for="db-1-3">password </label><input type="password" id="db-1-3" name="db[pw]" value="$db[pw]"><br>
137       <label for="db-1-4">database name </label><input type="text" id="db-1-4" name="db[dbname]" value="$db[dbname]"><br>
138       <label for="db-1-5">table name </label><input type="text" id="db-1-5" name="db[table]" value="$db[table]"> will be created automatically, when you activate ewiki for the first time<br>
139    </div>
140   <br>
141   <input type="radio" id="db-3" name="db[type]" value="flat"><label for="db-3"> flat file</label> <span class="feature-desc">database backend</span>   <br>
142   <input type="radio" id="db-4" name="db[type]" value="fast"><label for="db-4"> fast file</label> <span class="feature-desc">(compressed)</span>   <br>
143   <input type="radio" id="db-5" name="db[type]" value="dzf2"><label for="db-5"> new flat file backend 'dzf2'</label> <span class="feature-desc">(provides case-insensitive storage, plattform compatible, but more complicated structure)</span>
144    <div class="option">
145       <label for="db-3-1">storage directory </label><input type="text" id="db-3-1" name="db[dir]" value="$db[dir]">
146       <span class="option-desc">Note: the directory "/tmp" exists on most
147 Unix/Linux webservers, but will be purged on reboot; so you normally want to
148 use a different location for your pages. Choose a <i>relative path name</i>
149 (like for example "<kbd>./files</kbd>") and create that directory ("<kbd>mkdir
150 <i>files</i></kbd>" in FTP/shell) and give it <i>world-write permissions</i>
151 ("<kbd>chmod 777 <i>files</i></kbd>" in FTP/shell).</span>
152    </div>
153   <br>
154   <input type="radio" id="db-6" name="db[type]" value="dba"><label for="db-6"> .dbm</label> <span class="feature-desc"> Berkely database file</span>  <br>
155    <div class="option">
156       <label for="db-6-1">database file </label><input type="text" id="db-6-1" name="db[dba]" value="$db[dba]">
157       <span class="option-desc">The file name extension must be one of:
158       .dbm, .db2, .db3, .db4, .ndbm, .gdbm or .flatfile, and the file must
159       be world-writable of course</span>
160    </div>
161 EOT
162 );
163
164
165
166
167
168
169 #---------------------------------------------------------------------------
170
171
172
173 /*
174
175 #-- read in plugins/ dir
176 function find_plugin_files($realdir, $dirname) {
177    $r = array();
178    $dh = opendir($realdir);
179    while ($dh && ($fn = readdir($dh))) {
180       if ($fn[0] == ".") {
181         continue;
182       }
183       elseif (is_dir("$realdir/$fn")) {
184          $r = array_merge($r, find_plugin_files("$realdir/$fn", "$dirname/$fn"));
185       }
186       elseif ($len = strrpos($fn, ".")) {
187          $fn = substr($fn, 0, $len);
188          if (isset($r[$fn]) && $_SERVER["REQUEST_METHOD"]=="GET") {
189             echo "WARNING: two same named plugins '$fn' in two different locations!<br>\n";
190          }
191          $r[$fn] = "$dirname/$fn";
192       }
193    }
194    closedir($dh);
195    return($r);
196 }
197
198 $uu = dirname(__FILE__)."/../plugins";
199 $plugin_files = find_plugin_files($uu, "plugins");
200
201 #-- adjust incomplete filenames in feature $list
202 foreach ($list as $i1=>$d) {
203    if (is_array($d[3])) foreach($d[3] as $i2=>$fn) {
204       if (!strpos($fn, "/")) {
205          if ($fullfn = $plugin_files[$fn]) {
206             $list[$i1][3][$i2] = $fullfn;
207          }
208          else {
209             echo "WARNING: could not determine real file name for plugin '$fn'<br>\n";
210          }
211       }
212    }
213 }
214
215
216 */
217
218 #---------------------------------------------------------------------------
219
220
221
222
223 #-- inject values (into $list[]) imported from earlier loaded ewiki.ini
224 if ($ini) {
225    foreach ($list as $fid=>$row) {
226
227       #-- enable feature, if all requ/mentioned plugins were loaded in .ini
228       if (($row[0]===0) || ($row[0]===1)) { 
229          $is = all_in_array($row[3], $ini["plugins"]["load"]);
230          $list[$fid][0] = ($is ? 1 : 0);
231       }
232
233       #-- set feature options
234       if ($row[4]) {
235          foreach ($row[4] as $oid=>$opts) {
236             $name = $opts[2];
237             $val = $ini["settings"][$name][0];
238             if (strlen($val)) {
239                $list[$fid][4][$oid][3] = $val;
240       }  }  }
241 }  }
242
243
244 #-- compare two arrays, all elements of first must be in second
245 function all_in_array($a1, $a2) {
246    $a3 = array_intersect($a1, $a2);
247    return(count($a1) == count($a3));
248 }
249
250
251 #---------------------------------------------------------------------------
252
253
254
255 #-- prepare generation of config.php or ewiki.ini
256 #   (builds plugin and constant/var lists from _REQUEST settings)
257 #
258 if ($_REQUEST["feature"]) {
259
260   $set = &$_REQUEST["feature"];
261   $opt = &$_REQUEST["option"];
262
263   $c_plugins = array();
264   $c_settings = array(0=>array(), 1=>array());
265
266   #-- go through hardcoded feature $list
267   foreach ($list as $fid=>$row) {
268
269      #-- compare if feature array enabled in _REQUEST
270      $enabled = ($row[0] === "=") || ($set[$fid]);
271      if ($enabled) {
272 #echo "ENABLED=$set[$fid] feature[$fid], r0=$row[0], r1=$row[1],\n";
273
274         #-- list of plugins (always triggered)
275         $c_plugins[] = $row[3];
276
277         #-- settings, individual $_REQUEST entries
278         if ($options = $row[4]) {
279            foreach ($options as $oid=>$row) {
280               $i = $row[2];
281               $v = $opt[$fid][$oid];
282               if (strlen($v)) {
283                  $var = ($i[0] == "$") ? 1 : 0;
284                  $c_settings[$var][$i] = preg_match('/^\d+$/', $v) ? "$v" : "'$v'";
285               }
286            }
287         }
288
289      }
290   }#--if($enabled)
291   
292   #-- cleanup plugin list (in case one was injected twice)
293   $c_plugins = array_unique($c_plugins);
294
295 }#--if(<submit>)
296
297
298 #---------------------------------------------------------------------------
299
300
301 function config_php_db() {
302
303   global $db;
304
305   echo "#-- database connection/plugins\n";
306   switch ($db["type"]) {
307      case "mysql":
308         echo "// MySQL support is built-in, we only open the connection\n";
309         echo "define(\"EWIKI_DB_TABLE_NAME\", \"$db[table]\");\n";
310         echo "mysql_connect('$db[server]', '$db[user]', '$db[pw]');\n";
311         echo "mysql_query('USE $db[dbname]');\n\n";
312         break;
313
314      case "pgsql":
315         echo "define(\"EWIKI_DB_TABLE_NAME\", \"$db[table]\");\n";
316         echo "define(\"EWIKI_DB_UTF8\", 0);  //depends on your Postgres db\n";
317         echo "include_once(\"plugins/db/any.php\");\n";
318         echo "\$db = anydb_connect('', '$db[user]', '$db[pw]', '$db[dbname]', 'pgsql');\n\n";
319         break;
320
321      case "fast":
322         echo "define(\"EWIKI_DB_FAST_FILES\", 1);\n";
323      case "flat":
324         echo "define(\"EWIKI_DBFILES_DIRECTORY\", \"$db[dir]\");\n";
325         echo "include_once(\"plugins/db/flat_files.php\");\n";
326         echo "// the given directory must exist and be world-writable (chmod 777)\n\n";
327         break;        
328
329      case "dzf2":
330         echo "define(\"EWIKI_DBFILES_DIRECTORY\", \"$db[dir]\");\n";
331         echo "define(\"EWIKI_DBFF_ACCURATE\", 1);\n";
332         echo "define(\"DZF2_HIT_COUNTING\", 1);\n";
333         echo "include_once(\"plugins/db/dzf2.php\");\n";
334         echo "// the given directory must exist and be world-writable (chmod 777)\n\n";
335         break;
336
337      case "dba":
338         echo "define(\"EWIKI_DBA\", \"$db[dba]\");\n";
339         echo "include_once(\"plugins/db/dba.php\");\n";
340         break;
341
342      default:
343         echo "// you must open a connection (MySQL) outside of the config.php,\n";
344         echo "// it has not been configured with the setup wizard\n\n";
345         break;
346   }
347 }
348
349
350 function config_php_settings() {
351
352   global $c_settings;
353   
354   echo "#-- constants\n";
355   foreach ($c_settings[0] as $id=>$val) {
356      echo "define(\"$id\", $val);\n";
357   }
358
359   echo "\n#-- set a few configuration variables\n";
360   foreach ($c_settings[1] as $id=>$val) {
361      echo "$id = $val;\n";
362   }
363 }
364
365
366 #---------------------------------------------------------------------------
367
368
369
370 #-- write out "config.php" file
371 #
372 if ($_REQUEST["config_php"]) {
373   header("Content-Type: application/x-httpd-php");
374   header("Content-Disposition: attachment; filename=\"config.php\"");
375
376   #-- write out config.php
377   echo <<<EOT
378 <?php
379 # automatically generated config.php
380 # (see the ewiki configuration wizard)
381 #\n\n
382 EOT;
383
384   config_php_db();
385   config_php_settings();
386
387   echo "\n#-- load plugins\n";
388   foreach ($c_plugins as $id) if ($id != "core") {
389      
390      if ($fn = $ewiki_pmd[$id]["fn"]) {
391         echo "include_once(\"$fn\");\n";
392      }
393      else {
394         echo "//plugin not found: $id\n";
395      }
396   }
397
398   echo "\n#-- load ewiki 'lib'\ninclude_once(\"ewiki.php\");\n\n";
399   echo "?" . ">";
400   die();
401 }
402
403
404 #---------------------------------------------------------------------------
405
406
407
408 #-- write out as "ewiki.ini" file
409 #
410 if ($_REQUEST["ewiki_ini"]) {
411   header("Content-Type: text/x-ini-file");
412   header("Content-Disposition: attachment; filename=\"ewiki.ini\"");
413   out_ewiki_ini();
414   die();
415 }
416
417 #-- write out config.php
418 function out_ewiki_ini() {
419    global $db, $c_settings, $c_plugins, $ewiki_pmd;
420    echo "; automatically generated configuration summary\n; see ewiki config wizard\n";
421    echo "\n[db]\n";
422    echo ";init = \n";
423    foreach ($db as $id=>$val) {
424       echo "$id = $val\n";
425    }
426    echo "\n[settings]\n";
427    $c_settings = array_merge($c_settings[0], $c_settings[1]);
428    foreach ($c_settings as $id=>$val) {
429       $val = trim($val, "'");
430       echo "$id = $val\n";
431    }
432    echo "\n[plugins]\n";
433    foreach ($c_plugins as $id) {
434       if ($fn = $ewiki_pmd[$id]["fn"]) {
435          echo "load = $fn\n";
436       }
437       else {
438          echo ";not found = $id\n";
439       }
440    }
441    echo "\n\n";
442 }
443
444
445
446 #---------------------------------------------------------------------------
447
448
449
450 #-- generate a "monsterwiki.php" script (all-in-one)
451 #
452 if ($_REQUEST["monsterwiki_php"]) {
453   header("Content-Type: application/x-httpd-php");
454   header("Content-Disposition: attachment; filename=\"monsterwiki.php\"");
455   
456   #-- begin output
457   echo <<<EOT
458 <?php
459 #
460 # (this script was assembled with the ewiki configuration wizard)
461 # include() this instead of the bare 'ewiki.php' script as follows:
462 # <!php
463 #   include("monsterwiki.php");
464 #   ...
465 #   echo ewiki_page();
466 # !>
467 #\n\n
468 EOT;
469
470   config_php_db();
471   config_php_settings();
472   
473   echo "\n#-- end of settings,\n# plugins follow\n\n/*-- config summary in .ini format:\n";
474   out_ewiki_ini();  
475   echo "--*/\n\n" . "?".">";
476
477   #-- write plugins  
478   $PREFIX = "../";
479   foreach ($c_plugins as $fn) {
480      if (($fn != "core") && ($fn != "ewiki")) {
481         if ($fn = $ewiki_pmd[$fn]["fn"]) {
482            readfile($PREFIX . $fn);
483         }
484      }
485   }
486   #-- write core
487   readfile($PREFIX."ewiki.php");  
488   
489   die();
490 }
491
492
493
494 #---------------------------------------------------------------------------
495 # <html> page output otherwise
496
497 ?>
498 <html>
499 <head>
500  <title>ewiki configuration wizard</title>
501 <style type="text/css"><!--
502 html {
503   show-tags: as-you-like-dear-browser;
504 }
505 body {
506   margin: 0px; padding: 0px;
507   font: Verdana,sans-serif 16px;
508   color: #dddddd;
509 }
510 .left-bar {
511   margin: 0px;
512   float: left;
513   width: 140px;
514   height: 6000px;
515   padding: 0px 0px 0px 20px;
516 }
517 .left-bar .stripe {
518   width: 80px;
519   height: 20%;
520 }
521 .real-body {
522   padding-left: 40px;
523   width: 520px;
524 }
525 h1,h2,h3,h4,h5 {
526   background-color: #4c4c4e;
527   color: #ffffff;
528   margin-bottom: 3pt;
529 }
530 h2 {
531   background-color: #464646;
532   font-size: 20px;  
533 }
534 h1 {
535   background-color: #404040;
536   font-size: 24px;
537 }
538 input,checkbox,textarea,select {
539   background-color: #666666;
540   border: 1px solid #444444;
541   color: #dddddd;
542 }
543 input:focus {
544   border-color: #663333;
545 }
546 .feature-desc, .option-desc {
547   color: #aaaaaa;
548   font-size: 80%;
549 }
550 tt {
551   font-size: 120%;
552 }
553 .option {
554   color: #bbbbbb;
555 }
556 .option-desc {
557   font-size: 75%;
558 }
559 a {
560   color: #dddddd;
561   text-decoration: none;
562   border-bottom: dashed 1px #773333;
563 }
564 //--></style>
565 </head>
566 <body bgcolor="#555555"><div class="left-bar">
567   <div class="stripe" style="background:#662222">&nbsp;</div>
568   <div class="stripe" style="background:#642424">&nbsp;</div>
569   <div class="stripe" style="background:#622626">&nbsp;</div>
570   <div class="stripe" style="background:#602828">&nbsp;</div>
571   <div class="stripe" style="background:#5E2A2A">&nbsp;</div>
572 </div>
573 <br>
574 <div class="real-body">
575   <h1>ewiki configuration wizard</h1>
576   Generates an initial <tt>ewiki.ini</tt> or <tt>config.php</tt> for you.
577   The plugin list is unfiltered and therefore rather long. Skip all the
578   settings and options that don't look interesting or senseful at first.
579   <br><br>
580   
581   <b>Note</b>: This wizard has not yet been fully updated to handle the
582   new plugin *.meta files. Please use the 'tools/setup' console program
583   if you can.
584   <br><br>
585
586   You can reuse an earlier <tt>ewiki.ini</tt>, if you kept a copy of your
587   previously choosen settings:
588   <br>
589   <form action="t_setupwiz.php" method="POST" enctype="multipart/form-data" >
590   <input size="32" type="file" name="load_ini"> <input type="submit" value="load it">
591   <br><br>
592   
593 <?php
594
595   #-- go through list
596   foreach ($list as $fid=>$row) {
597
598      #-- print main feature field
599      switch ($row[0]) {
600
601         case "0":
602         case "1":
603            echo "  <input type=\"checkbox\" id=\"feature-$fid\" name=\"feature[$fid]\" value=\"1\" ".($row[0]?"checked":"").">\n";
604            echo "  <label for=\"feature-$fid\">$row[1]</label>\n";
605            if ($row[2]) {
606               echo "  <span class=\"feature-desc\">$row[2]</span>";
607            }
608            echo "<br>\n";
609            break;
610
611         case "=":
612            echo "  <input type=\"hidden\" name=\"feature[$fid]\" value=\"1\">\n";
613            echo "  $row[1]\n";
614            if ($row[2]) {
615               echo "  <span class=\"feature-desc\">$row[2]</span>";
616            }
617            echo "<br>\n";
618            break;
619
620         case "!":
621            echo "  <h3>$row[1]</h3>\n";
622         default:
623            echo "  $row[2]\n";
624            break;
625      }
626
627      #-- show up associated options
628      if ($options = $row[4])
629      foreach ($options as $oid=>$row) {
630         $id = "option-$fid-$oid";
631         echo '    <div class="option">';
632         echo " &nbsp; &nbsp; <label for=\"$id\">$row[1]</label> ";
633         switch($row[0]) {
634            case "checkbox":
635               $checked = ($row[3] ? " checked" : "");
636               echo "<input type=\"checkbox\" name=\"option[$fid][$oid]\" id=\"$id\" value=\"1\"$checked>";
637               break;
638            case "select":
639               echo "<select name=\"option[$fid][$oid]\" id=\"$id\">";
640               foreach (explode("|", $row[5]) as $val) {
641                  $title = $val;
642                  if (strpos($val, "=")) {
643                     list($val, $title) = explode("=", $val, 2);
644                  }
645                  $checked = (($row[3]==$value) ? " selected" : "");
646                  echo "<option value=\"$val\"$selected>$title</option>";
647               }
648               echo "</select>";
649               break;
650            default:
651               echo "<input type=\"$row[0]\" name=\"option[$fid][$oid]\" id=\"$id\" value=\"$row[3]\">";
652         }
653         echo "<span class=\"option-desc\"> $row[4]</span></div>\n";
654      }
655   }
656
657 ?>
658   <br>
659   <h2>fin</h2>
660   Now, that you've finished clicking around, you can save your configuration
661   settings. A save dialog will open, and you should store the files directly
662   into your ewiki/ directory.
663   <br><br>
664
665   <input style="color:#ffffff" type="submit" name="config_php" value="save config.php"> is
666   what you should do now; you can use it as replacement for the example
667   file distributed with ewiki.
668   <br><br>
669
670   <input type="submit" name="ewiki_ini" value="save ewiki.ini"> is
671   useful to later come back and reuse the settings you've made here.
672   <br><br>
673   
674   <input type="submit" name="monsterwiki_php" value="create monsterwiki script"> with
675   above options and extensions merged in; use this script instead of ewiki.php
676   then (it is believed to run a bit faster)
677   <br><br>
678
679   </form>
680 </div>
681 </body>
682 </html>