MDL-11082 Improved groups upgrade performance 1.8x -> 1.9; thanks Eloy for telling...
[moodle-pu.git] / mod / wiki / ewiki / plugins / spages.php
blob301ed4be0fea2058eea1c1f3d60946980dccd046
1 <?php
3 /*
4 The StaticPages plugin allows you to put some .html or .php files
5 into dedicated directories, which then will get available with their
6 basename as ewiki pages. The files can be in wiki format (.txt or no
7 extension), they can also be in .html format and they may even contain
8 php code (.php).
10 Of course it is not possible to provide anything else, than viewing
11 those pages (editing is not possible), but it is of course up to you
12 to add php code to achieve some interactivity.
13 The idea for this plugin was 'borought' from http://geeklog.org/.
15 In your static page .php files you cannot do everything you could
16 normally do, there are some restrictions because of the way these static
17 pages are processed. You need to use $GLOBALS to access variables other
18 than the $ewiki_ ones. To return headers() you must append them to the
19 $headers[] or $ewiki_headers[] array.
21 If you define("EWIKI_SPAGES_DIR") then this directory will be read
22 initially, but you could also just edit the following list/array of
23 directories, or call ewiki_init_spages() yourself.
27 #-- specify which dirs to search for page files
28 ewiki_init_spages(
29 array(
30 "spages",
31 # "/usr/local/share/wikipages",
32 # "C:/Documents/StaticPages/",
35 if (defined("EWIKI_SPAGES_DIR")) {
36 ewiki_init_spages(EWIKI_SPAGES_DIR);
40 #-- plugin glue
41 # - will be added automatically by _init_spages()
44 #-- return page
45 function ewiki_spage($id, $data, $action) {
47 global $ewiki_spages, $ewiki_plugins;
49 $r = "";
51 #-- filename from $id
52 $fn = $ewiki_spages[strtolower($id)];
54 #-- php file
55 if (strpos($fn, ".php") || strpos($fn, ".htm")) {
57 #-- start new ob level
58 ob_start();
59 ob_implicit_flush(0);
61 #-- prepare environment
62 global $ewiki_id, $ewiki_title, $ewiki_author, $ewiki_ring,
63 $ewiki_t, $ewiki_config, $ewiki_action, $_EWIKI,
64 $ewiki_auth_user, $ewiki_headers, $headers;
65 $ewiki_headers = array();
66 $headers = &$ewiki_headers;
68 #-- execute script
69 include($fn);
71 #-- close ob
72 $r = ob_get_contents();
73 ob_end_clean();
75 #-- add headers
76 if ($ewiki_headers) {
77 headers(implode("\n", $ewiki_headers));
81 #-- wiki file
82 else {
84 $f = fopen($fn, "rb");
85 $r = fread($f, 256<<10);
86 fclose($f);
88 $r = $ewiki_plugins["render"][0]($r);
91 #-- strip <html> and <head> parts (if any)
92 if (($l = strpos(strtolower($r), "<body")) &&
93 ($w = strpos(strtolower($r), "</body")) )
95 $l = strpos($r, ">", $l+1) + 1;
96 $w = $w - $l;
97 $r = substr($r, $l, $w);
100 #-- return body (means successful handled)
101 return($r);
107 #-- return page
108 #<old># $ewiki_plugins["handler"][] = "ewiki_handler_spages";
109 function ewiki_handler_spages($id, $data, $action) {
111 global $ewiki_spages;
113 #-- compare requested page $id with spages` $id values
114 $i0 = strtolower($id);
115 foreach ($ewiki_spages as $i1 => $fn) {
116 if (strtolower($i1)==$i0) {
118 return(ewiki_spage($id));
126 #-- init
127 function ewiki_init_spages($dirs, $idprep="") {
129 global $ewiki_spages, $ewiki_plugins;
131 if (!is_array($dirs)) {
132 $dirs = array($dirs);
135 #-- go through list of directories
136 foreach ($dirs as $dir) {
138 if (empty($dir)) {
139 continue;
142 #-- read in one directory
143 $dh = opendir($dir);
144 while ($fn = readdir($dh)) {
146 #-- skip over . and ..
147 if ($fn[0] == ".") { continue; }
149 #-- be recursive
150 if ($fn && is_dir("$dir/$fn")) {
151 if ($fn != trim($fn, ".")) {
152 $fnadd = trim($fn, ".") . ".";
154 else {
155 $fnadd = "$fn/";
158 ewiki_init_spages(array("$dir/$fn"), "$idprep$fnadd");
160 continue;
163 #-- strip filename extensions
164 $id = str_replace(
165 array(".html", ".htm", ".php", ".txt", ".wiki", ".src"),
167 basename($fn)
170 #-- register spage file and as page plugin (one for every spage)
171 $ewiki_spages[strtolower("$idprep$id")] = "$dir/$fn";
172 $ewiki_plugins["page"]["$idprep$id"] = "ewiki_spage";
175 closedir($dh);