From bcb727f7acf6683b9037eb8664f7b59a3f9aa541 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois?= Date: Thu, 12 Oct 2023 19:41:28 +0200 Subject: [PATCH] first --- README.md | 3 + adecWattBD.class.php | 369 +++++++++++++++++++++++++++++++++++++++++++ adecWattBD.php | 55 +++++++ conf/default.php | 10 ++ conf/metadata.php | 11 ++ lang/en/lang.php | 9 ++ lang/en/settings.php | 12 ++ lang/fr/lang.php | 9 ++ lang/fr/settings.php | 12 ++ plugin.info.txt | 7 + syntax.php | 46 ++++++ 11 files changed, 543 insertions(+) create mode 100644 adecWattBD.class.php create mode 100644 adecWattBD.php create mode 100644 conf/default.php create mode 100644 conf/metadata.php create mode 100644 lang/en/lang.php create mode 100644 lang/en/settings.php create mode 100644 lang/fr/lang.php create mode 100644 lang/fr/settings.php create mode 100644 plugin.info.txt create mode 100644 syntax.php diff --git a/README.md b/README.md index a70b12a..6684845 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,5 @@ # adecwatt +Greffon pour DokuWiki permettant de gérer les données partagé du logiciel AdecWatt. + +https://adecwatt.parlenet.org/ diff --git a/adecWattBD.class.php b/adecWattBD.class.php new file mode 100644 index 0000000..f323afd --- /dev/null +++ b/adecWattBD.class.php @@ -0,0 +1,369 @@ + + * + * Plugin AdecWatt: file transfert + */ + +class adecWattDB { + // ======================================== + var $dbDirName; + var $excludeFileName; + var $userDbName; + var $aclDbName; + var $nameDb; + var $idDb; + var $aclDb; + var $login; + var $id; + var $groups; + var $fpLog; + + function __construct ($plugin) { + global $conf; + // date_default_timezone_set ("UTC"); + $this->dbDirName = + ((!$conf['savedir'] || strpos ($conf['savedir'], '.') === 0) ? DOKU_INC : ""). + $conf['savedir'].'/'.trim ($plugin->getConf ('dataDir').'/'); + $this->excludeFileName = explode ("|", ".|..|timestamp"); + $this->userDbName = $this->dbDirName.'users.txt'; + $this->aclDbName = $this->dbDirName.'acl.txt'; + $this->fpLog = fopen ('/home/parlenet/adec.log', 'a+'); + } + + function tmpLog ($msg) { + fwrite ($this->fpLog, date ("YmdHis ").$msg.NL); + fflush ($this->fpLog); + } + + // ======================================== + function getInfos () { + global $INPUT; + $this->login = $INPUT->server->str ('REMOTE_USER'); + if (!$this->login) + return; + $this->readUser (); + $this->id = $this->nameDb [$this->login]; + if ($this->id === null) + return; + $this->groups = explode (',', $this->idDb[$this->id]); + } + + function getRoles ($name) { + $this->getInfos (); + if (!$this->login) { + echo "Not connected!".NL; + return; + } + global $INFO; + if (isset ($INFO['userinfo'] ['grps']) && in_array ('admin', $INFO ['userinfo'] ['grps'])) { + // si admin + } + if (!$this->id) { + "1|Visitor"; + } + echo $this->id."|".$this->idDb[$this->id].NL; + } + + function readUser () { + $this->nameDb = array (); + $this->idDb = array (); + $user = file ($this->userDbName); + foreach ($user as $line) { + $line = trim ($line); + if (empty ($line) || ($line[0] == '#')) + continue; // skip blank lines & comments + list ($id, $name, $roles) = explode ('|', $line, 3); + // XXX test nom double + $this->nameDb [$name] = $id; + $this->idDb [$id] = $roles; + } + } + + // ======================================== + function readAcl () { + $this->aclDb = array (); + $acl = file ($this->aclDbName); + foreach ($acl as $line) { + $line = trim ($line); + if (empty ($line) || ($line[0] == '#')) + continue; // skip blank lines & comments + list ($starts, $rest) = preg_split ('/[ \t]+/', $line, 2); + $user = array (); + $group = array (); + foreach (explode (',', $rest) as $word) { + $word = trim ($word); + if ($word[0] == '@') + $group[] = substr ($word, 1); + else + $user[] = $word; + } + $this->aclDb [$starts] = array ("users" => $user, "groups" => $group); + } + } + + function checkAcl ($path) { + foreach (array_keys ($this->aclDb) as $starts) + if (substr ($path, 0, strlen ($starts)) === $starts && + (in_array ($this->login, $this->aclDb[$starts]["users"]) || array_intersect ($this->groups, $this->aclDb[$starts]["groups"]))) + return true; + return false; + } + + // ======================================== + function cleanTmp () { + $tmpDir = sys_get_temp_dir (); + if (!$dh = opendir ($tmpDir)) + return; + $today = time (); + $aMoment = 60; + while (($child = readdir ($dh)) !== false) { + $fileName = $tmpDir."/".$child; + if (!is_file ($fileName) || filesize ($fileName)) + continue; + if ($today - filemtime ($fileName) < $aMoment) + continue; + @unlink ($fileName); + } + } + + function getTreeInfo ($fileName, $name) { + if (is_file ($fileName)) + return date ("YmdHis ", filemtime ($fileName)).filesize ($fileName)." ".$name.NL; + if (!is_dir ($fileName)) + return ""; + $result = ""; + if ($dh = opendir ($fileName)) { + while (($child = readdir ($dh)) !== false) { + if (in_array (strtolower ($child), $this->excludeFileName)) + continue; + if (is_file ($child) && eregi ('.*\.back', $child, $b)) + continue; + $result .= $this->getTreeInfo ($fileName."/".$child, $name."/".$child); + } + closedir($dh); + } + return $result; + } + function getTmpFile () { + return tempnam (sys_get_temp_dir (), "download"); + } + function getDataFileName ($version, $fileName) { + return $this->dbDirName."/".$this->checkPath ("/".$version."/".$fileName); + } + function checkPath ($path) { + return trim (str_replace (array ("//", "/./", "/../"), "/", "/".$path), "/"); + } + function getPostZipFile ($cmd) { + if (!$_FILES) { + echo "No zip file sended!".NL; + return null; + } + $file = $_FILES [$cmd]; + if (!$file) + $file = $_FILES [array_keys ($_FILES)[0]]; + if (!$file['tmp_name']) { + echo "Empty zip file!".NL; + return null; + } + $zip = new ZipArchive; + if ($zip->open ($file['tmp_name']) !== TRUE) { + echo "Can't open zip ".$file['tmp_name']."!".NL; + return null; + } + return $zip; + } + function getPostZipFileListContent ($cmd, $name) { + $zip = $this->getPostZipFile ($cmd); + if (!$zip) + return ""; + $listFilesName = $zip->getFromName ($name); + $listFilesName = str_replace ("\r", "", $listFilesName); + $zip->close (); + return $listFilesName; + } + + // ======================================== + // directory + // from name return zip [file:info] + function zipList ($version, $name) { + $this->cleanTmp (); + $fileName = $this->dbDirName."/".$this->checkPath ("/".$version."/".$name); + $this->sendZipText ($name, $this->getTreeInfo ($fileName, $name)); + } + + // get files + // from zip file contains list + function zipGets ($version, $name) { + $listFilesName = $this->getPostZipFileListContent ('zipGets', $name); + if (!$listFilesName) { + echo "No request!".NL; + return; + } + $tmpFileName = $this->getTmpFile (); + $zip = $this->sendZipOpen ($tmpFileName); + if (!$zip) + return; + foreach (explode ("\n", $listFilesName) as $fileName) { + if (!$fileName) + continue; + $zip->addFile ($this->getDataFileName ($version, $fileName), $fileName); + } + $this->sendZipClose ($zip, $tmpFileName); + } + + // put files + // from zip contains all files + function zipPuts ($version, $name) { + $this->cleanTmp (); + $this->getInfos (); + if (!$this->login) { + echo "Not connected!".NL; + return; + } + $this->readAcl (); + $tmpFileName = $this->getTmpFile (); + $zip = new ZipArchive; + if ($zip->open ($tmpFileName, ZipArchive::CREATE) !== TRUE) { + echo "Can't open $tmpFileName".NL; + return; + } + $zip = $this->getPostZipFile ($cmd); + if (!$zip) { + echo "No zip!".NL; + return; + } + $validFiles = array (); + for ($i = 0; $i < $zip->numFiles; $i++) { + $entry = $this->checkPath ("/".$zip->getNameIndex ($i)); + if (!$this->checkAcl ($entry)) + continue; + $validFiles [] = $entry; + } + if (!$validFiles) { + echo "No file sended!".NL; + return; + } + $pathExtract = $this->dbDirName."/".$version."/"; + foreach ($validFiles as $oldFile) { + $filename = realpath ($pathExtract.$oldFile); + if (is_file ($filename)) + rename ($filename, dirname ($filename)."/".basename ($filename).".back"); + } + $zip->extractTo ($pathExtract, $validFiles); + // change time + for ($i = 0; $i < $zip->numFiles; $i++) { + $entry = $this->checkPath ("/".$zip->getNameIndex ($i)); + if (! in_array ($entry, $validFiles)) + continue; + $filename = realpath ($pathExtract.$entry); + $this->tmpLog ("coucou touch :".$filename." ".$zip->statIndex ($i)["mtime"]); + touch ($filename, $zip->statIndex ($i)["mtime"]); + } + $this->sendZipText ($name, implode (NL, $validFiles)); + $zip->close (); + } + + // remove all files + // from zip contains filesname + function zipRemove ($version, $name) { + $this->cleanTmp (); + $listFilesName = $this->getPostZipFileListContent ('zipRemove', $name); + if (!$listFilesName) { + echo "No request!".NL; + return; + } + $this->getInfos (); + if (!$this->login) { + echo "Not connected!".NL; + return; + } + $this->readAcl (); + $succes = array (); + foreach (explode ("\n", $listFilesName) as $fileName) { + if (!$fileName) + continue; + $fileName = $this->checkPath ("/".$fileName); + if (!$this->checkAcl ($fileName)) + continue; + unlink ($this->getDataFileName ($version, $fileName)); + $succes[] = $fileName; + } + $this->sendZipText ($name, implode (NL, $succes)); + } + + // get one file + // from querry-string (not used) + function getZip ($version, $name) { + $name = $this->checkPath ($name); + $tmpFileName = $this->getTmpFile (); + $zip = $this->sendZipOpen ($tmpFileName); + if (!$zip) + return; + $zip->addFile ($this->getDataFileName ($version, $name), $name); + $this->sendZipClose ($zip, $tmpFileName); + } + + // ======================================== + function sendZipOpen ($tmpFileName) { + $zip = new ZipArchive; + if ($zip->open ($tmpFileName, ZipArchive::CREATE) !== TRUE) { + echo "Can't open $tmpFileName".NL; + return null; + } + return $zip; + } + function sendZipClose ($zip, $tmpFileName) { + $zip->close (); + $this->sendAbsFile ($tmpFileName, "application/zip"); + unlink ($tmpFileName); + } + function sendZipText ($name, $content) { + $tmpFileName = $this->getTmpFile (); + $zip = $this->sendZipOpen ($tmpFileName); + if (!$zip) + return; + $zip->addFromString ($name, $content); + $this->sendZipClose ($zip, $tmpFileName); + } + function sendAbsFile ($fileName, $mimeType) { + if (!is_file ($fileName)) { + echo "File not found <$fileName> <$mimeType>!".NL; + return; + } + header ("Content-Type: $mimeType"); + echo file_get_contents ($fileName); + } + + // ======================================== + // old API + // ======================================== + function sendDataFile ($version, $fileName, $mimeType) { + $this->sendAbsFile ($this->getDataFileName ($version, $fileName), $mimeType); + } + function getDir ($version, $name) { + $this->sendDataFile ($version, $name."/timestamp", "text/plain"); + } + function getDataFile ($version, $name) { + $mimeType = ""; + if (eregi ('.*\.lpt$', $name, $b)) + $mimeType = "application/lpt"; + elseif (eregi ('.*\.lpi$', $name, $b)) + $mimeType = "text/plain"; + elseif (eregi ('.*\.png$', $name, $b)) + $mimeType = "image/png"; + elseif (eregi ('.*\.jpg$', $name, $b)) + $mimeType = "image/jpg"; + elseif (eregi ('.*\.jar$', $name, $b)) + $mimeType = "application/java"; + elseif (eregi ('.*\.xml', $name, $b)) + $mimeType = "application/xml"; + else { + echo "Bad type $name!".NL; + return; + } + $this->sendDataFile ($version, $name, $mimeType); + } + // ======================================== +} diff --git a/adecWattBD.php b/adecWattBD.php new file mode 100644 index 0000000..f040fc4 --- /dev/null +++ b/adecWattBD.php @@ -0,0 +1,55 @@ + + * + * Plugin Schedule: manage events per wiki @groups + */ + +// faire un XML + +if (!defined ('DOKU_INC')) + define ('DOKU_INC', realpath (dirname (__FILE__).'/../../../').'/'); +require_once (DOKU_INC.'inc/init.php'); +require_once (DOKU_INC.'inc/common.php'); +require_once (DOKU_INC.'inc/auth.php'); +require_once (realpath (dirname (__FILE__)).'/adecWattBD.class.php'); +{ + //phpinfo (); + if (isset ($_SERVER['REMOTE_USER'])) + $INFO['userinfo'] = $auth->getUserData ($auth->cleanUser ($_SERVER['REMOTE_USER'])); + $adecplug =& plugin_load ('syntax', 'adecwatt'); + /* $isAdmin = false; */ + /* isset ($INFO ['userinfo']) && */ + /* isset ($INFO ['userinfo']['grps']) && */ + /* in_array (trim ($adecplug->getConf ('adminGroup')), $INFO ['userinfo']['grps']); */ + + $adecWattDB = new adecWattDB ($adecplug); + $version = ''; + if ($_REQUEST ['version']) + $version = $_REQUEST ['version']."/"; // old invocation + $path=$_REQUEST ['name']; + + if ('getRoles' == $_REQUEST ['action']) + $adecWattDB->getRoles ($path); + elseif ('zipList' == $_REQUEST ['action']) + $adecWattDB->zipList ($version, $path); + elseif ('zipGets' == $_REQUEST ['action']) + $adecWattDB->zipGets ($version, $path); + elseif ('zipPuts' == $_REQUEST ['action']) + $adecWattDB->zipPuts ($version, $path); + elseif ('zipRemove' == $_REQUEST ['action']) + $adecWattDB->zipRemove ($version, $path); + + // not used + elseif ('getZip' == $_REQUEST ['action']) + $adecWattDB->getZip ($version, $path); + + // old + elseif ('list' == $_REQUEST ['action']) + $adecWattDB->getDir ($version, $path); + elseif ('get' == $_REQUEST ['action']) + $adecWattDB->getDataFile ($version, $path); + else + die ("No Ajax Function !"); +} diff --git a/conf/default.php b/conf/default.php new file mode 100644 index 0000000..d3eead0 --- /dev/null +++ b/conf/default.php @@ -0,0 +1,10 @@ + + * + * Options for the adecwatt Plugin + */ +$conf['dataDir'] = 'adecwatt'; +$conf['adecWattGroup'] = 'adecwatt'; +?> diff --git a/conf/metadata.php b/conf/metadata.php new file mode 100644 index 0000000..af0185b --- /dev/null +++ b/conf/metadata.php @@ -0,0 +1,11 @@ + + * + * Metadata for configuration manager plugin + * Additions for the AdecWatt plugin + */ +$meta['dataDir'] = array('string'); +$meta['adecWattGroup'] = array('string'); +?> diff --git a/lang/en/lang.php b/lang/en/lang.php new file mode 100644 index 0000000..ff76af4 --- /dev/null +++ b/lang/en/lang.php @@ -0,0 +1,9 @@ + + * + * English language file + */ + +?> diff --git a/lang/en/settings.php b/lang/en/settings.php new file mode 100644 index 0000000..1518f05 --- /dev/null +++ b/lang/en/settings.php @@ -0,0 +1,12 @@ + + * + * English language file + */ + +// for the configuration manager +$lang['dataDir'] = 'Directory for data'; +$lang['adecWattGroup'] = 'performed validation group'; +?> diff --git a/lang/fr/lang.php b/lang/fr/lang.php new file mode 100644 index 0000000..cca59f0 --- /dev/null +++ b/lang/fr/lang.php @@ -0,0 +1,9 @@ + + * + * French language file + */ + +?> diff --git a/lang/fr/settings.php b/lang/fr/settings.php new file mode 100644 index 0000000..843b8db --- /dev/null +++ b/lang/fr/settings.php @@ -0,0 +1,12 @@ + + * + * French language file + */ + +// for the configuration manager +$lang['dataDir'] = 'répertoire où sont placer les données'; +$lang['adecWattGroup'] = 'groupe de validation des propositions'; +?> diff --git a/plugin.info.txt b/plugin.info.txt new file mode 100644 index 0000000..9226527 --- /dev/null +++ b/plugin.info.txt @@ -0,0 +1,7 @@ +base adecwatt +author Francois Merciol +email dokuplugin@merciol.fr +date 2017-09-17 +name adecWatt Plugin +desc Data base management for remoteUpdate +url https://adecWatt.parlenet.org/files/plugins/adecwatt.tgz diff --git a/syntax.php b/syntax.php new file mode 100644 index 0000000..905fb3d --- /dev/null +++ b/syntax.php @@ -0,0 +1,46 @@ + + * + * Plugin AdecWatt: manage forms for adecWatt DB + */ + +if (!defined ('DOKU_INC')) + define ('DOKU_INC', realpath (dirname (__FILE__).'/../../').'/'); +if (!defined ('DOKU_PLUGIN')) + define ('DOKU_PLUGIN', DOKU_INC.'lib/plugins/'); +require_once(DOKU_PLUGIN.'syntax.php'); + +// ============================================================ +class syntax_plugin_adecwatt extends DokuWiki_Syntax_Plugin { + + // ============================================================ + function getType () { return 'substition'; } + function getPType () { return 'block'; } + function getSort () { return 299; } + function connectTo ($mode) { + $this->Lexer->addSpecialPattern ('\{\{adecwat56[^}]*\}\}', $mode, 'plugin_adecwatt'); + } + + // ============================================================ + function handle ($match, $state, $pos, Doku_Handler $handler) { + switch ($state) { + case DOKU_LEXER_SPECIAL : + return trim (substr ($match, 12, -2)); // "{{adecwatt" => 12 "}}" => 2 + } + return false; + } + + // ============================================================ + function render ($mode, Doku_Renderer $renderer, $indata) { + if ($mode != 'xhtml') + return false; + $data = " ".$indata." "; + return true; + } + + + // ============================================================ +} // syntax_plugin_LODGING +?>