cosmetic
[diogenes.git] / include / diogenes.spool.inc.php
1 <?php
2 /*
3 * Copyright (C) 2003-2004 Polytechnique.org
4 * http://opensource.polytechnique.org/
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21
22 /** This class handles Diogenes spool operations.
23 */
24 class DiogenesSpool {
25 /** The barrel we are running on */
26 var $alias;
27 /** Absolute directory location for the barrel's spool. */
28 var $datadir;
29
30 /** The last command that was executed. */
31 var $cmd_call;
32
33 /** The output of the last command that was executed. */
34 var $cmd_output;
35
36 /** The return value of the last command. */
37 var $cmd_return;
38
39 /** The caller. It needs to define the 3 following methods :
40 *
41 * @see info, kill, log
42 */
43 var $caller;
44
45 /** The constructor.
46 *
47 * @param caller the caller
48 * @param alias the alias to work on
49 */
50 function DiogenesSpool(&$caller,$alias) {
51 global $globals;
52 $this->datadir = "{$globals->spoolroot}/$alias";
53 $this->caller =& $caller;
54 $this->alias = $alias;
55 }
56
57
58 /** Execute a shell command and store information about it.
59 *
60 * @param $cmd the command to execute
61 */
62 function cmdExec($cmd)
63 {
64 $this->cmd_call = $cmd;
65 unset($this->cmd_output);
66 unset($this->cmd_return);
67
68 exec($cmd, $this->cmd_output, $this->cmd_return);
69
70 return $this->cmd_return;
71 }
72
73
74 /** Return information about the last command that was executed.
75 */
76 function cmdStatus()
77 {
78 $out =
79 "-- [ command information ] -- \n".
80 "command : {$this->cmd_call}\n".
81 "return value : {$this->cmd_return}\n".
82 "-- [ command output ] --\n".
83 implode("\n", $this->cmd_output)."\n".
84 "--";
85 return $out;
86 }
87
88
89 /** Report an information.
90 *
91 * @param msg
92 */
93 function info($msg) {
94 $this->caller->info($msg);
95 }
96
97
98 /** Die with a given error message.
99 *
100 * @param msg
101 */
102 function kill($msg) {
103 $this->caller->kill($msg);
104 }
105
106
107 /** Record an action to the log.
108 *
109 * @param action
110 * @param data
111 */
112 function log($action,$data) {
113 $this->caller->log($action, $data);
114 }
115
116
117 /** Check that a path is valid.
118 *
119 * @param parent parent directory
120 * @param entry the item
121 * @param fatal should we consider a failure to be fatal?
122 */
123 function checkPath($parent,$entry,$fatal = true) {
124 $ret = preg_match("/^([a-zA-Z0-9\-_]+[\.,\/]?)*$/",$parent)
125 && preg_match("/^([a-zA-Z0-9\-_]+[\., ]?)*$/",$entry);
126
127 if (!$ret && $fatal)
128 $this->kill("malformed path ('$parent','$entry')");
129
130 return $ret;
131 }
132
133
134 /** Add missing tags to a Diogenes page to make it a proper HTML file
135 *
136 * @param html
137 * @see importHtmlString
138 */
139 function exportHtmlString($html)
140 {
141 // if we have the body open & close tags, return raw file
142 if (preg_match("/<body(\s[^>]*|)>(.*)<\/body>/si",$html))
143 return $html;
144
145 return "<html>\n<head><title>Diogenes page</title></head>\n<body>$html</body>\n</html>\n";
146 }
147
148
149 /** Makes a Diogenes page from a proper HTML file, that is return everything
150 * inside the "body" tags.
151 *
152 * @param html
153 * @see exportHtmlString
154 */
155 function importHtmlString($html)
156 {
157 // If available, run tidy to clean sources
158 if (function_exists('tidy_repair_string')) {
159 $tidy_config = array('drop-empty-paras' => true,
160 'drop-proprietary-attributes' => true,
161 'hide-comments' => true,
162 'logical-emphasis' => true,
163 'output-xhtml' => true,
164 'replace-color' => true,
165 'join-classes' => true,
166 'join-style' => true,
167 'clean' => true,
168 'show-body-only' => true,
169 'alt-text' => '[ inserted by TIDY ]',
170 'break-before-br' => true,
171 'indent' => true,
172 'vertical-space' => true,
173 'wrap' => 120);
174 if (function_exists('tidy_setopt')) { // Tidy 1.0
175 foreach ($tidy_config as $field=>$value) {
176 tidy_setopt($field, $value);
177 }
178 $html = tidy_repair_string($html);
179 } else { // Tidy 2.0
180 $html = tidy_repair_string($html, $tidy_config);
181 }
182 }
183
184 // if we cannot find the body open & close tags, return raw file
185 if (!preg_match("/<body(\s[^>]*|)>(.*)<\/body>/si",$html,$matches))
186 return $html;
187
188 return $matches[2];
189 }
190
191
192 /** Return the path of a spool "item" (file or directory).
193 *
194 * @param parent parent directory (optional)
195 * @param entry the item
196 */
197 function spoolPath($parent="",$entry="") {
198 $this->checkPath($parent,$entry);
199 return $this->datadir.($parent ? "/$parent": "") . ($entry ? "/$entry" : "");
200 }
201
202 }
203
204 ?>