speed up a.php

This commit is contained in:
2026-10-03 19:21:23 +02:00
parent fa2c4199c4
commit bf96993c29
2 changed files with 95 additions and 33 deletions
+94 -32
View File
@@ -230,12 +230,80 @@ function isKazArchive ($link) {
jirafeau_escape ($link ['mime_type']) == T_ARCHIVE_MIME; jirafeau_escape ($link ['mime_type']) == T_ARCHIVE_MIME;
} }
// ========================================
function getArchiveCacheLastUpdate () {
if (file_exists (FILE_LAST_CACHE))
return intval (trim (file (FILE_LAST_CACHE)[0]));
return 0;
}
function setArchiveCacheLastUpdate ($time) {
if (!file_exists (VAR_CACHE))
mkdir (VAR_CACHE, 0755);
file_put_contents (FILE_LAST_CACHE, $time.NL);
}
function getArchiveCacheFromSender ($sender) {
$archives = [];
if (!file_exists (VAR_CACHE.$sender))
return $archives;
$dir = scandir (VAR_CACHE.$sender);
foreach ($dir as $hash) {
$p = s2p ($hash);
$lines = file (VAR_FILES . $p . $hash);
if (!$lines)
continue;
$archives [] = readArchiveFromLines ($lines);
}
return $archives;
}
function updateArchiveCacheUpTo ($period) {
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")) {
continue;
}
if (is_dir ($d . $dirName)) {
// Push new found directory.
$stack [] = $d . $dirName . '/';
continue;
}
$time = filemtime ($d . $dirName);
if ($time < $minTime || $time >= $maxTime)
continue;
// Read link informations.
$l = jirafeau_get_link ($dirName);
if (!count ($l))
continue;
if (!isKazArchive ($l))
continue;
$archiveInfo = readArchiveFromLink ($l);
if (!$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']);
}
}
setArchiveCacheLastUpdate ($maxTime);
}
// ======================================== // ========================================
function readArchiveFromLink ($link) { function readArchiveFromLink ($link) {
$p = s2p ($link ['hash']); $p = s2p ($link ['hash']);
$lines = file (VAR_FILES . $p . $link ['hash']); $lines = file (VAR_FILES . $p . $link ['hash']);
$archive = readArchiveFromLines ($lines); if ($lines)
return $archive; return readArchiveFromLines ($lines);
global $message;
$message .= "can't read {$linkName}".NL;
return [];
} }
function readArchiveFromLines ($lines) { function readArchiveFromLines ($lines) {
$archive = []; $archive = [];
@@ -383,6 +451,8 @@ function deleteAction ($linkName) {
$fileName = $linkName; $fileName = $linkName;
$fileLink = $link; $fileLink = $link;
$fileTime = $fileLink ['upload_date']; $fileTime = $fileLink ['upload_date'];
// XXX
$stack = array (VAR_LINKS); $stack = array (VAR_LINKS);
while (($d = array_shift ($stack)) && $d != null) { while (($d = array_shift ($stack)) && $d != null) {
if (!file_exists ($d)) if (!file_exists ($d))
@@ -711,40 +781,17 @@ if (isset ($_REQUEST [A_ACTION])) {
setSenderLang ($sender, $matches [1]); setSenderLang ($sender, $matches [1]);
$message .= "Votre lang &agrave; &eacute;t&eacute; mise &agrave; jour."; $message .= "Votre lang &agrave; &eacute;t&eacute; mise &agrave; jour.";
break; break;
case preg_match ("/^".A_CACHE."(".implode ("|", array_keys ($periodText)).")$/i", $_REQUEST [A_ACTION], $matches):
updateArchiveCacheUpTo ($matches [1]);
$message .= "Cache mise &agrave; jour {$matches [1]}.";
break;
} }
} }
// list // list
$archives = []; $archives = getArchiveCacheFromSender ($sender);
$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")) {
continue;
}
if (is_dir ($d . $dirName)) {
/* Push new found directory. */
$stack [] = $d . $dirName . '/';
continue;
}
/* Read link informations. */
$l = jirafeau_get_link ($dirName);
if (!count ($l))
continue;
if (!isKazArchive ($l))
continue;
$archiveInfo = readArchiveFromLink ($l);
if ($sender != $archiveInfo [T_SENDER])
continue;
$archiveInfo ['link'] = $dirName;
$archiveInfo ['key'] = $l ['key'];
$archiveInfo ['maxtime'] = $l ['time'];
$archiveInfo ['hash'] = $l ['hash'];
$archives [] = $archiveInfo;
}
}
displayHeadPage ("Gestion des pièces jointes"); displayHeadPage ("Gestion des pièces jointes");
echo str_replace (["___SENDER___", "___ADMIN___", "___DATE___"], echo str_replace (["___SENDER___", "___ADMIN___", "___DATE___"],
[$sender, ($admin ? " (admin)" : ""), jirafeau_get_datetimefield (time ())], [$sender, ($admin ? " (admin)" : ""), jirafeau_get_datetimefield (time ())],
@@ -853,6 +900,21 @@ echo
'<button type="submit">'.M_REFRESH.'</button>'. '<button type="submit">'.M_REFRESH.'</button>'.
' la page.</form>'; ' la page.</form>';
if ($admin) {
echo
'<form method="post">'.
'Mettre &agrave; jour le cache jusqu\'&agrave; '.
'<input type="hidden" name="'.A_SENDER.'" value="'.$sender.'"/>'.
'<input type="hidden" name="'.A_TOKEN.'" value="'.$token.'"/>'.
'<select name="'.A_ACTION.'" style="width: auto !important;">';
foreach ($periodText as $item => $text)
echo ' <option value="'.A_CACHE.$item.'">'.$text.'</option>';
echo
'</select> '.
'<button type="submit">'."valider".'</button>'.
'.</form><br/>';
}
$userSise = 0; $userSise = 0;
$userTab = []; $userTab = [];
if ($archives) { if ($archives) {
+1 -1
View File
@@ -115,7 +115,7 @@
<div class="table"> <div class="table">
<form class="tr" action="<?php echo $_SERVER ['SCRIPT_NAME']; ?>" method="POST"> <form class="tr" action="<?php echo $_SERVER ['SCRIPT_NAME']; ?>" method="POST">
<span class="td"></span> <span class="td"></span>
<span class="td"><input name="<?php echo A_NAME;?>" type="text" value="<?php echo $name;?>" placeholder="Mon nuage"/></span> <span class="td"><input name="<?php echo A_NAME;?>" type="text" value="<?php echo $name;?>" placeholder="Nom du nuage"/></span>
<span class="td"><input name="<?php echo A_URL;?>" type="text" value="<?php echo $url;?>" placeholder="https://..."/></span> <span class="td"><input name="<?php echo A_URL;?>" type="text" value="<?php echo $url;?>" placeholder="https://..."/></span>
<?php echo getHiddenToken (); ?><?php echo getHiddenLink (); ?> <?php echo getHiddenToken (); ?><?php echo getHiddenLink (); ?>
<span class="td"><button class="btn btn-new" onclick="actionUrl (this, false, '<?php echo A_ACTION; ?>', '<?php echo T_ADD_URL; ?>')" ><?php echo L_ADD_URL; ?></button></span> <span class="td"><button class="btn btn-new" onclick="actionUrl (this, false, '<?php echo A_ACTION; ?>', '<?php echo T_ADD_URL; ?>')" ><?php echo L_ADD_URL; ?></button></span>