diff --git a/src/Jirafeau/a.php b/src/Jirafeau/a.php index cf9af98..0ecfe19 100644 --- a/src/Jirafeau/a.php +++ b/src/Jirafeau/a.php @@ -1,16 +1,27 @@ track - a.php?p=email => period - a.php?u=month&h=HHHHHHHH => update deadline - a.php?g=l~k => zip - a.php?time=month&key=password + POST file => upload - a.php?s=mel@domain.org => form - a.php?s=mel@domain.org&t=password + [action] => manage account - action: a=login a=logout a=r[on|off] a=p[minute|hour|day|week|month|quarter|semester] + a.php?time=[month|...]&key={password} + POST file => upload + a.php?g={linkHash}~{key} => zip + a.php?c=[minute|hour|day|week|month|quarter|semester] => update track cache => cron + a.php?u=[month|...]&h={linkHash} => update deadline + a.php?r={email} => track status + a.php?p={email} => period status + a.php?l={email} => lang status + a.php?s={mel@domain} => form + a.php?s={mel@domain}&t={token}&{action} => manage account + + action: + | a=login => login + | a=logout => logout + | a=m[none|footer|attachment|both] => set mode + | a=c[minute|hour|day|week|month|quarter|semester] => update track cache + | a=r[on|off] => set followed email + | a=p[minute|hour|day|week|month|quarter|semester] => set perriod + | a=l[fr|en|br] => set lang + | d={linkHash} => delete link */ require_once (__DIR__ . "/lib/attach-setup.php"); @@ -18,12 +29,12 @@ require_once (__DIR__ . "/lib/attach-setup.php"); $modeText = ['none' => "sans", 'footer' => "pied de page", 'attachment' => "pièce jointe", 'both' => "les deux"]; $trackText = ['on' => "oui", 'off' => "non"]; $periodText = ['minute' => "minute", 'hour' => "heure", 'day' => "jour", 'week' => "semaine", 'month' => "mois", 'quarter' => "trimestre", "semester" => "semestre"]; -$periodButton = ['hour' => ["🕐", ">1 heure"], // 1F550 - 'day' => ["🕑", ">1 jour"], // 1F551 - 'week' => ["🕒", "> 1 semaine"], // 1F552 - 'month' => ["🕓", "> 1 mois"], // 1F553 - 'quarter' => ["🕔", "> 1 trimestre"], // 1F554 - 'semester' => ["🕕", "> 1 semestre"] // 1F555 +$periodButton = ['hour' => ["🕐", "garantir 1 heure"], // 1F550 + 'day' => ["🕑", "garantir 1 jour"], // 1F551 + 'week' => ["🕒", "garantir 1 semaine"], // 1F552 + 'month' => ["🕓", "garantir 1 mois"], // 1F553 + 'quarter' => ["🕔", "garantir 1 trimestre"], // 1F554 + 'semester' => ["🕕", "garantir 1 semestre"] // 1F555 ]; $langText = ['fr' => "Francais", 'br' => "Breton", 'en' => "english"]; $doLogout = ''; @@ -91,6 +102,11 @@ if (isset ($_REQUEST [A_GET]) && !empty ($_REQUEST [A_GET])) { $doDownload = true; } +$doCache = false; +if (isset ($_REQUEST [A_CACHE]) && !empty ($_REQUEST [A_CACHE])) { + $doCache = true; +} + // ======================================== function returnError ($msg) { displayHeadPage ("Gestion des pièces jointes"); @@ -243,58 +259,83 @@ function getArchiveCacheFromSender ($sender) { $l = jirafeau_get_link ($hash); if (empty ($l)) continue; - $archives [] = readArchiveFromLink ($l); - - // $p = s2p ($hash); - // $lines = file (VAR_FILES . $p . $hash); - // if (!$lines) - // continue; - // $archives [] = readArchiveFromLines ($lines); + $archives [] = readArchiveFromLink ($l, $hash); } return $archives; } +function cleanArchiveCache () { + global $message, $admin; + if (!file_exists (VAR_CACHE)) + return; + $listSender = scandir (VAR_CACHE); + foreach ($listSender as $cacheSender) { + if (strcmp ($cacheSender, '.') === 0 || strcmp ($cacheSender, '..') === 0 || + preg_match ('/\.tmp/i', "$cacheSender")) { + continue; + } + if (! is_dir (VAR_CACHE.$cacheSender)) + continue; + $listLink = scandir (VAR_CACHE.$cacheSender.'/'); + foreach ($listLink as $cacheLink) { + if (strcmp ($cacheLink, '.') === 0 || strcmp ($cacheLink, '..') === 0 || + preg_match ('/\.tmp/i', "$cacheLink")) { + continue; + } + $link = VAR_LINKS . s2p("$cacheLink") . $cacheLink; + if (!file_exists($link)) { + if ($admin) + $message .= "Remove ".$cacheSender."/".$cacheLink."
"; + unlink (VAR_CACHE.$cacheSender."/".$cacheLink); + } + } + } +} + function updateArchiveCacheUpTo ($period) { + global $message; + cleanArchiveCache (); if (!file_exists (VAR_CACHE)) mkdir (VAR_CACHE, 0755); $minTime = getArchiveCacheLastUpdate (); $maxTime = time ()-period2seconds ($period); $stack = array (VAR_LINKS); - while ( ($d = array_shift ($stack)) && $d != null) { - $dir = scandir ($d); - foreach ($dir as $dirName) { - if (strcmp ($dirName, '.') === 0 || strcmp ($dirName, '..') === 0 || - preg_match ('/\.tmp/i', "$dirName")) { + while ( ($dirName = array_shift ($stack)) && $dirName != null) { + $list = scandir ($dirName); + foreach ($list as $fileName) { + if (strcmp ($fileName, '.') === 0 || strcmp ($fileName, '..') === 0 || + preg_match ('/\.tmp/i', "$fileName")) { continue; } - if (is_dir ($d . $dirName)) { + if (is_dir ($dirName . $fileName)) { // Push new found directory. - $stack [] = $d . $dirName . '/'; + $stack [] = $dirName . $fileName . '/'; continue; } - $time = filemtime ($d . $dirName); + $time = filemtime ($dirName . $fileName); if ($time < $minTime || $time >= $maxTime) continue; // Read link informations. - $l = jirafeau_get_link ($dirName); + $l = jirafeau_get_link ($fileName); if (!count ($l)) continue; if (!isKazArchive ($l)) continue; - $archiveInfo = readArchiveFromLink ($l); + $archiveInfo = readArchiveFromLink ($l, $fileName); if (!isset ($archiveInfo [T_SENDER]) || empty ($archiveInfo [T_SENDER])) continue; if (!file_exists (VAR_CACHE.$archiveInfo [T_SENDER])) mkdir (VAR_CACHE.$archiveInfo [T_SENDER], 0755); - touch (VAR_CACHE.$archiveInfo [T_SENDER]."/".$l ['hash']); + touch (VAR_CACHE.$archiveInfo [T_SENDER]."/".$fileName); } } setArchiveCacheLastUpdate ($maxTime); + $message .= "Cache mise à jour ({$period})."; } // ======================================== -function readArchiveFromLink ($link) { +function readArchiveFromLink ($link, $linkName) { $p = s2p ($link ['hash']); $lines = file (VAR_FILES . $p . $link ['hash']); if (empty ($lines)) { @@ -302,9 +343,6 @@ function readArchiveFromLink ($link) { $message .= "can't read {$linkName}".NL; return []; } -// return readArchiveFromLines ($lines); -// } -// function readArchiveFromLines ($lines) { $archive = []; $error = false; foreach ($lines as $line) { @@ -315,9 +353,7 @@ function readArchiveFromLink ($link) { case preg_match ("/^\s*time:\s*(\d{4}([:-]\d{2}){5})\s*$/i", $line, $matches): $archive [T_TIME] = $matches [1]; break; - // XXX - //case preg_match ("/^\s*sender:\s*(([a-z0-9_+-]+)(\.[a-z0-9_+-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6})\s*$/i", $line, $matches): - case preg_match ("/^\s*sender:\s*(([a-z0-9_=+-]+)(\.[a-z0-9_=+-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6})\s*$/i", $line, $matches): + case preg_match ("/^\s*sender:\s*(([a-z0-9_=+-]+)(\.[a-z0-9_=+-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6})\s*$/i", $line, $matches): $archive [T_SENDER] = $matches [1]; break; // XXX issue ")" @@ -335,11 +371,13 @@ function readArchiveFromLink ($link) { break; } } - global $message, $admin; - if ($error && $admin) - $message .= "readArchiveFromLines
".print_r ($lines, true)."
"; - $archiveInfo ['maxtime'] = $link ['time']; - return $error ? [] : $archive; + if ($error) + return []; + $archive ['maxtime'] = $link ['time']; + $archive ['hash'] = $link ['hash']; + $archive ['key'] = $link ['key']; + $archive ['link'] = $linkName; + return $archive; } // ======================================== @@ -398,27 +436,41 @@ function isAdmin ($sender) { } // ======================================== +function deleteLinkAndCache ($linkName, $fileName) { + global $sender, $message; + jirafeau_delete_link ($linkName); + $cacheToDelete = VAR_CACHE.$sender."/".$linkName; + if (file_exists ($cacheToDelete)) { + if (!empty ($fileName)) + $message .= "
Remove " . jirafeau_escape ($fileName) . " (".$linkName.")
"; + unlink ($cacheToDelete); + } +} + function deleteAction ($linkName) { - global $sender, $token, $message, $doLogout; + global $sender, $message; $link = jirafeau_get_link ($linkName); //$message .= "ln: ".$linkName." l: "."
".print_r ($link, 1)."
mt: ".getTimeFile ($link ['hash'])."
"; if (!count ($link)) return; if (isKazArchive ($link)) { + // Acrhive $dirName = $linkName; $dirLink = $link; $dirTime = $dirLink ['upload_date']; - $archiveInfo = readArchiveFromLink ($dirLink); - if (! count ($archiveInfo)) + $archiveInfo = readArchiveFromLink ($dirLink, $linkName); + if (! count ($archiveInfo)) { + $message .= "Archive introuvable ({$dirName})
"; return; + } if ($sender != $archiveInfo [T_SENDER]) { setSenderFake ("rmdir: not owner", $sender, $archiveInfo [T_SENDER], $dirLink, null); $message .= "Tentative de supprimer un envoi dont vous n'êtes pas le propriétaire"; return; } $fileToDelete = false; - if ($archiveInfo [T_NEWKAZ]) + if (isset ($archiveInfo [T_NEWKAZ]) && count ($archiveInfo [T_NEWKAZ])) foreach ($archiveInfo [T_NEWKAZ] as [$fileName, $cryptKey]) { $fileLink = jirafeau_get_link ($fileName); if (! count ($fileLink)) @@ -427,8 +479,8 @@ function deleteAction ($linkName) { if (! valideTime ($dirTime, $fileTime)) { setSenderFake ("rmdir: newfile not same time", $sender, null, $dirLink, $fileLink); $message .= "Cet envoi a été forgée". - str_replace (["___FILENAME___", "___DIRTIME___", "___FILETIME___"], - [$fileLink ['file_name'], $dirTime , $fileTime], M_INCONSISTENT_DATES); + str_replace (["___FILENAME___", "___DIRTIME___", "___FILETIME___"], + [$fileLink ['file_name'], $dirTime , $fileTime], M_INCONSISTENT_DATES); return; } $fileToDelete = true; @@ -436,90 +488,21 @@ function deleteAction ($linkName) { $message .= "l'envoi ".$archiveInfo [T_TIME]." est supprimé"; if ($fileToDelete) $message .= " avec" : "."; + deleteLinkAndCache ($dirName, ''); return; } - $fileName = $linkName; - $fileLink = $link; - $fileTime = $fileLink ['upload_date']; - // XXX - $stack = array (VAR_LINKS); - while (($d = array_shift ($stack)) && $d != null) { - if (!file_exists ($d)) - continue; - $dir = scandir ($d); - foreach ($dir as $dirName) { - if (strcmp ($dirName, '.') === 0 || strcmp ($dirName, '..') === 0 || - preg_match ('/\.tmp/i', "$dirName")) { - continue; - } - if (is_dir ($d . $dirName)) { - $stack [] = $d . $dirName . '/'; - continue; - } - $dirLink = jirafeau_get_link ($dirName); - //$dirTime = getTimeFile ($dirLink ['hash']); - $dirTime = $dirLink ['upload_date']; - if (!count ($dirLink)) - continue; - if (!isKazArchive ($dirLink)) - continue; - $archiveInfo = readArchiveFromLink ($dirLink); - if (! count ($archiveInfo)) - return; - if ($archiveInfo [T_NEWKAZ]) - foreach ($archiveInfo [T_NEWKAZ] as [$newName, $cryptKey]) { - if ($fileName != $newName) - continue; - if ($sender == $archiveInfo [T_SENDER]) { - if (valideTime ($dirTime, $fileTime)) { - jirafeau_delete_link ($fileName); - $message .= jirafeau_escape ($fileLink ['file_name'])." est supprimé"; - // check empty dir - $empty = true; - foreach ([T_OLD, T_NEWKAZ] as $cat) - if ($empty && isset ($archiveInfo [$cat])) - foreach ($archiveInfo [$cat] as [$l, $c]) - if (count (jirafeau_get_link ($l))) { - $empty = false; - break; - } - if ($empty) { - $message .= " ainsi que l'envoie ".$archiveInfo [T_TIME]." qui est vide."; - jirafeau_delete_link ($dirName); - } else - $message .= "."; - break; - } - setSenderFake ("rm: dir not same time", $sender, null, $dirLink, $fileLink); - $message .= "Cet envoi a été forgée. ". - str_replace (["___FILENAME___", "___DIRTIME___", "___FILETIME___"], - [$fileLink ['file_name'], $dirTime , $fileTime], M_INCONSISTENT_DATES); - break; - } - if (valideTime ($dirTime, $fileTime)) { - setSenderFake ("rm: not owner", $sender, $archiveInfo [T_SENDER], $dirLink, $fileLink); - $message .= "Tentative de supprimer un envoi dont vous n'êtes pas le propriétaire.". - str_replace (["___FILENAME___", "___DIRTIME___", "___FILETIME___"], - [$fileLink ['file_name'], $dirTime , $fileTime], M_INCONSISTENT_DATES); - break; - } - setSenderFake ("rm: find not owner", $archiveInfo [T_SENDER], $sender, $dirLink, $fileLink); - $message .= "Quelqu'un avétait revandiqué cet envoi. (".$sender." != ".$archiveInfo [T_SENDER].")"; - break; - } - } - } + // only file + deleteLinkAndCache ($linkName, $link ['file_name']); } // ======================================== @@ -538,7 +521,7 @@ if ($doUpdate) { $content = '' . $time . NL; if (isKazArchive ($link)) { - $archiveInfo = readArchiveFromLink ($l); + $archiveInfo = readArchiveFromLink ($l, $linkName); if (count ($archiveInfo)) { foreach ([T_OLD, T_NEWKAZ] as $cat) if (isset ($archiveInfo [$cat])) @@ -553,6 +536,16 @@ if ($doUpdate) { exit; } +if ($doCache) { + global $message; + updateArchiveCacheUpTo ($_REQUEST [A_CACHE]); + + displayHeadPage ("Gestion des pièces jointes"); + echo '
' . $message . '
'; + displayFootPage (); + exit; +} + // ======================================== if ($doDownload) { // check archive exist @@ -570,7 +563,9 @@ if ($doDownload) { if ($key && (empty ($cryptKey) || $key != $cryptKey)) returnError (t ('BAD_PSW')); - $archiveInfo = readArchiveFromLink ($link); + $archiveInfo = readArchiveFromLink ($link, $linkName); + if (! count ($archiveInfo)) + returnError (t ('FILE_404')); // check entries $archiveContent = []; @@ -624,7 +619,7 @@ if ($doDownload) { // build zip $dirname = M_NO_SENDER.(isset ($archiveInfo [T_TIME]) && !empty ($archiveInfo [T_TIME])) ? - $archiveInfo [T_TIME] : date ("Y-m-d-H:i:s"); + $archiveInfo [T_TIME] : date ("Y-m-d-H:i:s"); $dirname = str_replace (":", "_", $dirname); $tmpFileName = tempnam (sys_get_temp_dir (), $dirname."-"); $zip = new ZipArchive; @@ -672,19 +667,6 @@ if ($doDownload) { } $zip->close (); - - if (false) { - // log - $message .= print_r ($archiveInfo, 1); - $message .= print_r ($archiveContent, 1); - - header ('HTTP/1.0 200 OK'); - header ('Content-Length: ' . strlen ($message)); - header ('Content-Type: text/plain'); - echo $message; - exit; - } - if (!is_file ($tmpFileName.T_ZIP_EXT,)) returnError (E_OPEN_ZIP); @@ -720,7 +702,7 @@ if (! ($sender && $token && $token == $refToken && $message .= '
'.M_TOO_LONG_LOGGED.'
'.NL; if ($message) - echo $message; + echo $message; displayLogin (M_LOGIN_CONFIG); displayFootPage (); @@ -781,10 +763,8 @@ if (isset ($_REQUEST [A_ACTION])) { setSenderLang ($sender, $matches [1]); $message .= "Votre lang à été mise à jour."; break; - case preg_match ("/^".A_CACHE."(".implode ("|", array_keys ($periodText)).")$/i", $_REQUEST [A_ACTION], $matches): updateArchiveCacheUpTo ($matches [1]); - $message .= "Cache mise à jour {$matches [1]}."; break; } } @@ -796,6 +776,7 @@ displayHeadPage ("Gestion des pièces jointes"); echo str_replace (["___SENDER___", "___ADMIN___", "___DATE___"], [$sender, ($admin ? " (admin)" : ""), jirafeau_get_datetimefield (time ())], M_WELCOME); + if ($message) echo '
Info : '.$message.'
'; echo '