3 * Copyright (C) 2003-2004 Polytechnique.org
4 * http://opensource.polytechnique.org/
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.
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.
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
22 /** This class handles Diogenes spool operations.
25 /** The barrel we are running on */
27 /** Absolute directory location for the barrel's spool. */
30 /** The last command that was executed. */
33 /** The output of the last command that was executed. */
36 /** The return value of the last command. */
39 /** The caller. It needs to define the 3 following methods :
41 * @see info, kill, log
47 * @param caller the caller
48 * @param alias the alias to work on
50 function DiogenesSpool(&$caller,$alias) {
52 $this->datadir
= "{$globals->spoolroot}/$alias";
53 $this->caller
=& $caller;
54 $this->alias
= $alias;
58 /** Execute a shell command and store information about it.
60 * @param $cmd the command to execute
62 function cmdExec($cmd)
64 $this->cmd_call
= $cmd;
65 unset($this->cmd_output
);
66 unset($this->cmd_return
);
68 exec($cmd, $this->cmd_output
, $this->cmd_return
);
70 return $this->cmd_return
;
74 /** Return information about the last command that was executed.
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".
89 /** Report an information.
94 $this->caller
->info($msg);
98 /** Die with a given error message.
102 function kill($msg) {
103 $this->caller
->kill($msg);
107 /** Record an action to the log.
112 function log($action,$data) {
113 $this->caller
->log($action, $data);
117 /** Check that a path is valid.
119 * @param parent parent directory
120 * @param entry the item
121 * @param fatal should we consider a failure to be fatal?
123 function checkPath($parent,$entry,$fatal = true
) {
124 $ret = preg_match("/^([a-zA-Z0-9\-_]+[\.,\/]?)*$/",$parent)
125 && preg_match("/^([a-zA-Z0-9\-_]+[\., ]?)*$/",$entry);
128 $this->kill("malformed path ('$parent','$entry')");
134 /** Add missing tags to a Diogenes page to make it a proper HTML file
137 * @see importHtmlString
139 function exportHtmlString($html)
141 // if we have the body open & close tags, return raw file
142 if (preg_match("/<body(\s[^>]*|)>(.*)<\/body>/si",$html))
145 return "<html>\n<head><title>Diogenes page</title></head>\n<body>$html</body>\n</html>\n";
149 /** Makes a Diogenes page from a proper HTML file, that is return everything
150 * inside the "body" tags.
153 * @see exportHtmlString
155 function importHtmlString($html)
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
,
168 'show-body-only' => true
,
169 'alt-text' => '[ inserted by TIDY ]',
170 'break-before-br' => true
,
172 'vertical-space' => true
,
174 if (function_exists('tidy_setopt')) { // Tidy 1.0
175 foreach ($tidy_config as $field=>$value) {
176 tidy_setopt($field, $value);
178 $html = tidy_repair_string($html);
180 $html = tidy_repair_string($html, $tidy_config);
184 // if we cannot find the body open & close tags, return raw file
185 if (!preg_match("/<body(\s[^>]*|)>(.*)<\/body>/si",$html,$matches))
192 /** Return the path of a spool "item" (file or directory).
194 * @param parent parent directory (optional)
195 * @param entry the item
197 function spoolPath($parent="",$entry="") {
198 $this->checkPath($parent,$entry);
199 return $this->datadir
.($parent ?
"/$parent": "") . ($entry ?
"/$entry" : "");