changed git call from https to git readonly
[atutor.git] / mods / wiki / plugins / spages.php
1 <?php
2
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). Some binary files may also be thrown into there, but
9    you should use PathInfo or a ModRewrite setup then.
10
11    Of course it is not possible to provide anything else, than viewing
12    those pages (editing is not possible), but it is of course up to you
13    to add php code to achieve some interactivity.
14    The idea for this plugin was 'borought' from http://geeklog.org/.
15
16    In your static page .php files you cannot do everything you could
17    normally do, there are some restrictions because of the way these static
18    pages are processed. You need to use $GLOBALS to access variables other
19    than the $ewiki_ ones. To return headers() you must append them to the
20    $headers[] or $ewiki_headers[] array.
21
22    If you define("EWIKI_SPAGES_DIR") then this directory will be read
23    initially, but you could also just edit the following list/array of 
24    directories, or call ewiki_init_spages() yourself.
25 */
26
27
28 #-- specify which dirs to search for page files
29 ewiki_init_spages(
30    array(
31       "spages",
32       # "/usr/local/share/wikipages",
33       # "C:/Documents/StaticPages/",
34    )
35 );
36 if (defined("EWIKI_SPAGES_DIR")) {
37    ewiki_init_spages(EWIKI_SPAGES_DIR);
38 }
39 define("EWIKI_SPAGES_BIN", 1);
40
41
42 #-- plugin glue
43 # - will be added automatically by _init_spages()
44
45
46 #-- return page
47 function ewiki_spage($id, &$data, $action) {
48
49    global $ewiki_spages, $ewiki_plugins, $ewiki_t;
50
51    $r = "";
52
53    #-- filename from $id
54    $fn = $ewiki_spages[strtolower($id)];
55
56    #-- php file
57    if (strpos($fn, ".php") || strpos($fn, ".htm")) {
58
59       #-- start new ob level
60       ob_start();
61       ob_implicit_flush(0);
62
63       #-- prepare environment
64       global $ewiki_id, $ewiki_title, $ewiki_author, $ewiki_ring,
65              $ewiki_t, $ewiki_config, $ewiki_action, $_EWIKI,
66              $ewiki_auth_user, $ewiki_headers, $headers;
67       $ewiki_headers = array();
68       $headers = &$ewiki_headers;
69
70       #-- execute script
71       include($fn);
72
73       #-- close ob
74       $r = ob_get_contents();
75       ob_end_clean();
76
77       #-- add headers
78       if ($ewiki_headers) {
79          headers(implode("\n", $ewiki_headers));
80       }
81       $clean_html = true;
82    }
83
84    #-- plain binary file
85    elseif (EWIKI_SPAGES_BIN && !headers_sent() && preg_match('#\.(png|gif|jpe?g|zip|tar)#', $fn)) {
86       $ct = "application/octet-stream";
87       if (function_exists("mime_content_type")) {
88          $ct = mime_content_type($fn);
89       }
90       header("Content-Type: $ct");
91       header("ETag: ewiki:spages:".md5($r).":0");
92       header("Last-Modified: " . gmstrftime($ewiki_t["C"]["DATE"], filemtime($fn)));
93       passthru($r);
94    }
95
96    #-- wiki file
97    else {
98       $f = gzopen($fn, "rb");
99       $r = gzread($f, 256<<10);
100       gzclose($f);
101
102       #-- render as text/plain, text/x-wiki
103       if ($r) {
104          $r = $ewiki_plugins["render"][0]($r);
105       }
106    }
107
108    #-- strip <html> and <head> parts (if any)
109    if ($clean_html) {
110       $r = preg_replace('#^.+<body[^>]*>(.+)</body>.+$#is', '$1', $r);
111    }
112
113    #-- return body (means successfully handled)
114    return($r);
115 }
116
117
118
119 #-- init
120 function ewiki_init_spages($dirs, $idprep="") {
121
122    global $ewiki_spages, $ewiki_plugins;
123
124    if (!is_array($dirs)) {
125       $dirs = array($dirs);
126    }
127
128    #-- go through list of directories
129    foreach ($dirs as $dir) {
130
131       if (empty($dir)) {
132          continue;
133       }
134
135       #-- read in one directory
136       $dh = opendir($dir);
137       while ($fn = readdir($dh)) {
138
139          #-- skip over . and ..
140          if ($fn[0] == ".") { continue; }
141
142          #-- be recursive
143          if ($fn && is_dir("$dir/$fn")) {
144             if ($fn != trim($fn, ".")) {
145                $fnadd = trim($fn, ".") . ".";
146             }
147             else {
148                $fnadd = "$fn/";
149             }
150
151             ewiki_init_spages(array("$dir/$fn"), "$idprep$fnadd");
152
153             continue;
154          }
155
156          #-- strip filename extensions
157          $id = str_replace(
158                   array(".html", ".htm", ".php", ".txt", ".wiki", ".src"),
159                   "",
160                   basename($fn)
161          );
162
163          #-- register spage file and as page plugin (one for every spage)
164          $ewiki_spages[strtolower("$idprep$id")] = "$dir/$fn";
165          $ewiki_plugins["page"]["$idprep$id"] = "ewiki_spage";
166
167       }
168       closedir($dh);
169    }
170
171 }
172
173
174
175 ?>