X-Git-Url: http://git.polytechnique.org/?a=blobdiff_plain;ds=sidebyside;f=include%2Fdiogenes.spool.inc.php;fp=include%2Fdiogenes.spool.inc.php;h=69fc18441f0f7373d17753f30d34a6c0e9d29aa7;hb=6855525e48fad5de270500a5445c4f4ff85d8bda;hp=0000000000000000000000000000000000000000;hpb=e69709aa8ee6108a1197e46b45367ba8dab55a52;p=diogenes.git diff --git a/include/diogenes.spool.inc.php b/include/diogenes.spool.inc.php new file mode 100644 index 0000000..69fc184 --- /dev/null +++ b/include/diogenes.spool.inc.php @@ -0,0 +1,177 @@ +datadir = "{$globals->spoolroot}/$alias"; + $this->caller =& $caller; + $this->alias = $alias; + } + + + /** Execute a shell command and store information about it. + * + * @param $cmd the command to execute + */ + function cmdExec($cmd) + { + $this->cmd_call = $cmd; + unset($this->cmd_output); + unset($this->cmd_return); + + exec($cmd, $this->cmd_output, $this->cmd_return); + + return $this->cmd_return; + } + + + /** Return information about the last command that was executed. + */ + function cmdStatus() + { + $out = + "-- [ command information ] -- \n". + "command : {$this->cmd_call}\n". + "return value : {$this->cmd_return}\n". + "-- [ command output ] --\n". + implode("\n", $this->cmd_output)."\n". + "--"; + return $out; + } + + + /** Report an information. + * + * @param msg + */ + function info($msg) { + $this->caller->info($msg); + } + + + /** Die with a given error message. + * + * @param msg + */ + function kill($msg) { + $this->caller->kill($msg); + } + + + /** Record an action to the log. + * + * @param action + * @param data + */ + function log($action,$data) { + $this->caller->log($action, $data); + } + + + /** Check that a path is valid. + * + * @param parent parent directory + * @param entry the item + * @param fatal should we consider a failure to be fatal? + */ + function checkPath($parent,$entry,$fatal = true) { + $ret = preg_match("/^([a-zA-Z0-9\-_]+[\.,\/]?)*$/",$parent) + && preg_match("/^([a-zA-Z0-9\-_]+[\., ]?)*$/",$entry); + + if (!$ret && $fatal) + $this->kill("malformed path ('$parent','$entry')"); + + return $ret; + } + + + /** Add missing tags to a Diogenes page to make it a proper HTML file + * + * @param html + * @see importHtmlString + */ + function exportHtmlString($html) + { + // if we have the body open & close tags, return raw file + if (preg_match("/]*|)>(.*)<\/body>/si",$html)) + return $html; + + return "\nDiogenes page\n$html\n\n"; + } + + + /** Makes a Diogenes page from a proper HTML file, that is return everything + * inside the "body" tags. + * + * @param html + * @see exportHtmlString + */ + function importHtmlString($html) + { + // if we cannot find the body open & close tags, return raw file + if (!preg_match("/]*|)>(.*)<\/body>/si",$html,$matches)) + return $html; + + return $matches[2]; + } + + + /** Return the path of a spool "item" (file or directory). + * + * @param parent parent directory (optional) + * @param entry the item + */ + function spoolPath($parent="",$entry="") { + $this->checkPath($parent,$entry); + return $this->datadir.($parent ? "/$parent": "") . ($entry ? "/$entry" : ""); + } + +} + +?>