depollueur/src/Jirafeau/a.php

461 lines
16 KiB
PHP
Raw Normal View History

2022-02-10 01:37:43 +01:00
<?php
/*
* Kaz addon (see https://git.kaz.bzh/KAZ/depollueur for information)
* create un archive for a set of file or update file deadline
a.php?u=month&h=HHHHHHHH => deadline
a.php?g=l~k => zip
a.php?s=mel@domain.org => send status e-mail
*/
2022-02-11 01:01:24 +01:00
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
2022-02-10 01:37:43 +01:00
2022-02-11 01:01:24 +01:00
define ('JIRAFEAU_ROOT', dirname (__FILE__) . '/');
2022-02-10 01:37:43 +01:00
2022-02-11 01:01:24 +01:00
require (JIRAFEAU_ROOT . 'lib/settings.php');
require (JIRAFEAU_ROOT . 'lib/functions.php');
require (JIRAFEAU_ROOT . 'lib/lang.php');
define ('A_GET', 'g');
define ('A_HASH', 'h');
define ('A_SENDER', 's');
define ('A_UPDATE', 'u');
define ('A_OPEN_TOKEN', 'o');
define ('E_BAD_SENDER_NAME', 'm&egrave;l incorrect : ');
define ('E_BAD_ARCHIVE_NAME', 'bad archive name format : ');
define ('E_CREATE_ZIP', "Impossible de cr&eacute;er l'archive.");
define ('E_OPEN_ZIP', "Impossible d'ouvrir l'archive.");
define ('M_BAD_KEY', "Mauvaise clef pour ");
define ('M_FILE', " fichier.");
define ('M_FILES', " fichiers.");
define ('M_FILES_NOT_FOUND', " fichiers sont expir&eacute;s.");
define ('M_FILES_RENAMED', " fichiers renomm&eacute;s.");
define ('M_FILE_NOT_FOUND', " fichier est expir&eacute;.");
define ('M_FILE_RENAMED', " fichier renomm&eacute;.");
define ('M_NO_FILENAME', 'SansNom');
define ('M_NO_SENDER', 'kaz');
define ('M_OLD_ATTACHEMENT_DIRNAME', "RappelHistorique");
define ('M_INTRO_FORM', "O&ugrave; sont les derni&egrave;res pi&egrave;ces jointe que j'ai envoy&eacute; ?");
define ('M_SEND_TOKEN', "Vous allez recevoir un lien d'acc&egrave;s temporaire &agrave; vos donn&eacute;es.");
define ('T_BAD_PASW', 'bad_psw');
define ('T_CRYPTED', 'crypted');
define ('T_CRYPT_KEY', 'crypt_key');
define ('T_ENTRIES', 'entries');
define ('T_FILENAME', 'file_name');
define ('T_HASH', 'hash');
define ('T_NEW', 'new');
define ('T_NOT_FOUND', 'not_found');
define ('T_OLD', 'old');
define ('T_RENAME', 'rename');
define ('T_SENDER', 'sender');
define ('T_TIMESTAMP', 'timestamp');
define ('T_WARNING_FILENAME', "-Avertissement.txt");
define ('T_ZIP_EXT', ".zip");
2022-02-10 01:37:43 +01:00
/* Operations may take a long time.
* Be sure PHP's safe mode is off.
*/
@set_time_limit (0);
/* Remove errors. */
@error_reporting (0);
$do_update = false;
2022-02-11 01:01:24 +01:00
if (isset ($_REQUEST[A_UPDATE]) && !empty ($_REQUEST[A_UPDATE])) {
2022-02-10 01:37:43 +01:00
$do_update = true;
}
$do_download = false;
2022-02-11 01:01:24 +01:00
if (isset ($_REQUEST[A_GET]) && !empty ($_REQUEST[A_GET])) {
2022-02-10 01:37:43 +01:00
$do_download = true;
}
// ========================================
function return_error ($msg) {
require (JIRAFEAU_ROOT.'lib/template/header.php');
echo '<div class="error"><p>' . $msg . '</p></div>';
require (JIRAFEAU_ROOT.'lib/template/footer.php');
exit;
}
// ========================================
/** Update link
* @param $link the link's name (hash)
* @param $update_period the periode (i.e in : "month")
*/
function jirafeau_update_link ($link_name, $link, $update_period) {
$time_max = $link ['time'];
$time_up = time () + $update_period;
$time_more = $time_up + JIRAFEAU_HOUR;
if ($time_max < 0 || $time_up < $time_max)
return $time_max;
$link ['time'] = $time_more;
2022-02-11 01:01:24 +01:00
$link_tmp_name = VAR_LINKS . $link ['hash'] . rand (0, 10000) . '.tmp';
2022-02-10 01:37:43 +01:00
$handle = fopen ($link_tmp_name, 'w');
fwrite ($handle,
2022-02-11 01:01:24 +01:00
$link ['file_name'] .NL. $link ['mime_type'] .NL. $link ['file_size'] .NL. $link ['key'] .NL. $link ['time'] .NL.
$link ['hash'] .NL. $link ['onetime'] .' '.JIRAFEAU_MONTH . ' '. JIRAFEAU_DAY .NL. $link ['upload_date'] .NL.
$link ['ip'] .NL. $link ['link_code'] .NL. $link ['crypted']);
2022-02-10 01:37:43 +01:00
fclose ($handle);
2022-02-11 01:01:24 +01:00
$link_file = VAR_LINKS . s2p ("$link_name") . $link_name;
2022-02-10 01:37:43 +01:00
rename ($link_tmp_name, $link_file);
return $time_more;
}
// ========================================
function read_archive_info ($link) {
$p = s2p ($link ['hash']);
// read archive info
$result=[];
2022-02-11 01:01:24 +01:00
foreach (file (VAR_FILES . $p . $link ['hash']) as $line) {
2022-02-10 01:37:43 +01:00
switch (true) {
case preg_match ("/^\s*src:\s*(([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6})\s*$/i", $line, $matches):
2022-02-11 01:01:24 +01:00
$result[T_SENDER] = $matches[1];
2022-02-10 01:37:43 +01:00
break;
case preg_match ("/^\s*time:\s*(\d{4}([:-]\d{2}){5})\s*$/i", $line, $matches):
2022-02-11 01:01:24 +01:00
$result[T_TIMESTAMP] = $matches[1];
2022-02-10 01:37:43 +01:00
break;
case preg_match ("/^\s*old:\s*([0-9a-zA-Z_-]+)\s+([0-9a-zA-Z_-]+)\s*$/", $line, $matches):
2022-02-11 01:01:24 +01:00
$result[T_OLD][] = [$matches[1], $matches[2]];
2022-02-10 01:37:43 +01:00
break;
case preg_match ("/^\s*new:\s*([0-9a-zA-Z_-]+)\s+([0-9a-zA-Z_-]+)\s*$/", $line, $matches):
2022-02-11 01:01:24 +01:00
$result[T_NEW][] = [$matches[1], $matches[2]];
2022-02-10 01:37:43 +01:00
break;
default:
break;
}
}
return $result;
}
2022-02-11 01:01:24 +01:00
// ========================================
function send_email($receiver, $receiver_name, $subject, $body_string){
// SERVER SETTINGS
$mail = new PHPMailer (true);
$mail->isSMTP ();
$mail->Host = 'smtp';
$mail->SMTPAuth = false;
$mail->SMTPAutoTLS = false;
$mail->SMTPSecure = "none";
$mail->Port = 25;
//Recipients (change this for every project)
$mail->setFrom ('no-reply@kaz.local', '');
$mail->addAddress ($receiver, $receiver_name);
//Content
$mail->isHTML (false);
$mail->Subject = $subject;
$mail->Body = $body_string;
//send the message, check for errors
if (!$mail->send ()) {
//echo 'Mailer Error: ' . $mail->ErrorInfo;
return 0;
} else {
//echo 'Message sent!';
return 1;
}
}
2022-02-10 01:37:43 +01:00
// ========================================
if ($do_update) {
$update_period = JIRAFEAU_MONTH;
2022-02-11 01:01:24 +01:00
switch ($_REQUEST[A_UPDATE]) {
2022-02-10 01:37:43 +01:00
case 'minute':
$update_period = JIRAFEAU_MINUTE;
break;
case 'hour':
$update_period = JIRAFEAU_HOUR;
break;
case 'day':
$update_period = JIRAFEAU_DAY;
break;
case 'week':
$update_period = JIRAFEAU_WEEK;
break;
case 'month':
$update_period = JIRAFEAU_MONTH;
break;
case 'quarter':
$update_period = JIRAFEAU_QUARTER;
break;
case 'year':
$update_period = JIRAFEAU_YEAR;
break;
default:
2022-02-11 01:01:24 +01:00
return_error (t ('ERR_OCC') . ' (update_period)');
2022-02-10 01:37:43 +01:00
}
2022-02-11 01:01:24 +01:00
$link_name = $_REQUEST[A_HASH];
if (!preg_match ('/[0-9a-zA-Z_-]+$/', $link_name))
return_error (t ('FILE_404'));
$link = jirafeau_get_link ($link_name);
if (count ($link) == 0)
return_error (t ('FILE_404'));
$time = jirafeau_update_link ($link_name, $link, $update_period);
2022-02-10 01:37:43 +01:00
$content = '' . $time . NL;
2022-02-11 01:01:24 +01:00
header ('HTTP/1.0 200 OK');
header ('Content-Length: ' . strlen ($content));
header ('Content-Type: text/plain');
2022-02-10 01:37:43 +01:00
echo $content;
exit;
}
// ========================================
if ($do_download) {
// check archive exist
2022-02-11 01:01:24 +01:00
$couple = explode ("~", $_REQUEST[A_GET], 2);
2022-02-10 01:37:43 +01:00
if (count ($couple) == 0)
2022-02-11 01:01:24 +01:00
return_error (E_BAD_ARCHIVE_NAME.$_REQUEST [A_GET]);
2022-02-10 01:37:43 +01:00
$link_name = $couple [0];
if (!$link_name || !preg_match ('/[0-9a-zA-Z_-]+$/', $link_name))
2022-02-11 01:01:24 +01:00
return_error (E_BAD_ARCHIVE_NAME.$_REQUEST [A_GET]);
2022-02-10 01:37:43 +01:00
$crypt_key = count ($couple) == 2 ? $couple [1] : "";
$link = jirafeau_get_link ($link_name);
if (count ($link) == 0)
return_error (t ('FILE_404'));
2022-02-11 01:01:24 +01:00
$key = $link ['key'];
2022-02-10 01:37:43 +01:00
if ($key && (empty ($crypt_key) || $key != $crypt_key))
return_error (t ('BAD_PSW'));
$archive_info = read_archive_info ($link);
// check entries
$archive_content = [];
$modif = false;
2022-02-11 01:01:24 +01:00
$single_name = [];
foreach ([T_OLD, T_NEW] as $cat)
2022-02-10 01:37:43 +01:00
if (isset ($archive_info[$cat]))
foreach ($archive_info[$cat] as [$link_name, $crypt_key]) {
$link = jirafeau_get_link ($link_name);
if (count ($link) == 0) {
2022-02-11 01:01:24 +01:00
if (isset ($archive_content[T_NOT_FOUND]))
++$archive_content[T_NOT_FOUND];
else
$archive_content[T_NOT_FOUND] = 1;
2022-02-10 01:37:43 +01:00
$modif = true;
continue;
}
2022-02-11 01:01:24 +01:00
$key = $link ['key'];
2022-02-10 01:37:43 +01:00
if ($key && (empty ($crypt_key) || $key != $crypt_key)) {
2022-02-11 01:01:24 +01:00
if (isset ($archive_content[T_BAD_PASW]))
++$archive_content[T_BAD_PASW];
else
$archive_content[T_BAD_PASW] = 1;
2022-02-10 01:37:43 +01:00
$modif = true;
continue;
}
2022-02-11 01:01:24 +01:00
$src_name = $dst_name = ($link ['file_name'] ? $link ['file_name'] : M_NO_FILENAME);
2022-02-10 01:37:43 +01:00
if (in_array ($src_name, $single_name))
for ($i = 0; $i < 10000; ++$i) {
$dst_name = sprintf ("%s-%2d", $src_name, $i);
if (!in_array ($dst_name, $single_name)) {
2022-02-11 01:01:24 +01:00
if (isset ($archive_content[T_RENAME]))
++$archive_content[T_RENAME];
else
$archive_content[T_RENAME] = 1;
2022-02-10 01:37:43 +01:00
$modif = true;
break;
}
}
$single_name [] = $dst_name;
2022-02-11 01:01:24 +01:00
$archive_content[$cat][T_ENTRIES][] = [T_HASH => $link ['hash'], T_FILENAME => $dst_name, T_CRYPT_KEY => $crypt_key, T_CRYPTED => $link['crypted']];
2022-02-10 01:37:43 +01:00
}
// build zip
2022-02-11 01:01:24 +01:00
$dirname = (isset ($archive_info[T_SENDER]) && !empty ($archive_info[T_SENDER]))
? $archive_info[T_SENDER] : M_NO_SENDER;
$dirname .= "-" . (isset ($archive_info[T_TIMESTAMP]) && !empty ($archive_info[T_TIMESTAMP]))
? $archive_info[T_TIMESTAMP] : date ("Ymd-His");
2022-02-10 01:37:43 +01:00
$tmpFileName = tempnam (sys_get_temp_dir (), $dirname."-");
$zip = new ZipArchive;
if (!$zip)
2022-02-11 01:01:24 +01:00
return_error (E_CREATE_ZIP);
if ($zip->open ($tmpFileName.T_ZIP_EXT, ZipArchive::CREATE) !== TRUE)
return_error (E_OPEN_ZIP);
2022-02-10 01:37:43 +01:00
// create info XXX
if ($modif) {
$info = '';
2022-02-11 01:01:24 +01:00
if (isset ($archive_info[T_NOT_FOUND]))
$info .= $archive_info[T_NOT_FOUND]. ($archive_info[T_NOT_FOUND] ? M_FILE_NOT_FOUND : M_FILES_NOT_FOUND).NL;
if (isset ($archive_info[T_BAD_PASW]))
$info .= M_BAD_KEY. $archive_info[T_BAD_PASW]. ($archive_info[T_BAD_PASW] ? M_FILE : M_FILES).NL;
if (isset ($archive_info[$cat][T_RENAME]))
$info .= $archive_info[$cat][T_RENAME]. ($archive_info[$cat][T_RENAME] ? M_FILE_RENAMED : M_FILES_RENAMED).NL;
$zip->addFromString ($dirname.T_WARNING_FILENAME, $info);
2022-02-10 01:37:43 +01:00
}
2022-02-11 01:01:24 +01:00
foreach ([T_OLD, T_NEW] as $cat)
2022-02-10 01:37:43 +01:00
if (isset ($archive_info [$cat])) {
2022-02-11 01:01:24 +01:00
$subdir = $dirname . ($cat == T_NEW ? "" : "/".M_OLD_ATTACHEMENT_DIRNAME);
foreach ($archive_content [$cat][T_ENTRIES] as $entry) {
$p = s2p ($entry [T_HASH]);
if ($entry [T_CRYPTED]) {
2022-02-10 01:37:43 +01:00
$m = mcrypt_module_open ('rijndael-256', '', 'ofb', '');
2022-02-11 01:01:24 +01:00
$md5_key = md5 ($entry [T_CRYPT_KEY]);
2022-02-10 01:37:43 +01:00
$iv = jirafeau_crypt_create_iv ($md5_key, mcrypt_enc_get_iv_size ($m));
mcrypt_generic_init ($m, $md5_key, $iv);
2022-02-11 01:01:24 +01:00
$r = fopen (VAR_FILES . $p . $entry [T_HASH], 'r');
2022-02-10 01:37:43 +01:00
$content = "";
while (!feof ($r)) {
$dec = mdecrypt_generic ($m, fread ($r, 1024));
$content .= $dec;
ob_flush ();
}
fclose ($r);
2022-02-11 01:01:24 +01:00
$zip->addFromString ($subdir."/".$entry [T_FILENAME], $content);
2022-02-10 01:37:43 +01:00
mcrypt_generic_deinit ($m);
mcrypt_module_close ($m);
continue;
}
2022-02-11 01:01:24 +01:00
$zip->addFile (VAR_FILES.$p.$entry [T_HASH], $subdir."/".$entry [T_FILENAME]);
2022-02-10 01:37:43 +01:00
}
}
$zip->close ();
2022-02-11 01:01:24 +01:00
if (!is_file ($tmpFileName.T_ZIP_EXT,))
return_error (E_OPEN_ZIP);
2022-02-10 01:37:43 +01:00
if (false) {
// log
$content = print_r ($archive_info, 1);
$content .= print_r ($archive_content, 1);
2022-02-11 01:01:24 +01:00
header ('HTTP/1.0 200 OK');
header ('Content-Length: ' . strlen ($content));
header ('Content-Type: text/plain');
2022-02-10 01:37:43 +01:00
echo $content;
exit;
}
header ("Content-Type: application/zip");
header ('Content-Disposition: filename="'.$dirname.'.zip"');
2022-02-11 01:01:24 +01:00
$r = fopen ($tmpFileName.".zip", 'r');
2022-02-10 01:37:43 +01:00
while (!feof ($r)) {
print fread ($r, 1024);
ob_flush ();
}
fclose ($r);
unlink ($tmpFileName.".zip");
unlink ($tmpFileName);
exit;
}
// ========================================
// XXX form send
$content = "TODO send form".NL;
2022-02-11 01:01:24 +01:00
$sender = '';
if (isset ($_REQUEST [A_SENDER]) && !empty ($_REQUEST [A_SENDER])) {
$sender=$_REQUEST [A_SENDER];
if (!preg_match ("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $sender))
return_error (E_BAD_SENDER_NAME.$sender);
2022-02-10 01:37:43 +01:00
$content = "${sender}".NL;
}
2022-02-11 01:01:24 +01:00
if (!$sender) {
require (JIRAFEAU_ROOT . 'lib/template/header.php');
echo M_INTRO_FORM;
?>
<Form method="post" class="form login">
<fieldset>
<table>
<tr>
<td class = "label"><label for = "enter_password"><?php echo "votre m&egrave;l" . ':'; ?></label>
</td>
</tr>
<tr>
<td class = "field"><input type = "text" name = "s" id = "s" size = "40" />
</td>
</tr>
<tr class = "nav">
<td class = "nav next">
<input type = "submit" name = "key" value ="<?php echo t ('LOGIN'); ?>" />
</td>
</tr>
</table>
</fieldset>
</form>
<?php
require (JIRAFEAU_ROOT.'lib/template/footer.php');
exit;
}
$open_service = '';
if (!isset ($_REQUEST [A_OPEN_TOKEN]) || empty ($_REQUEST [A_OPEN_TOKEN]))
$open_service=$_REQUEST [A_OPEN_TOKEN];
if (!isset ($open_service)) {
// send e-mail
//preg_match ("%^http.*//depot.([^/]+)/?$%i", $cfg ['web_root'], $matches);
// $domain = $matches[1];
// $to = $sender;
// $subject = 'Lien de consultation des envoies de pi&egrave;ces jointes';
// $message = 'Bonjour !';
// $headers =
// 'From: no-reply@'.$domain.'' . "\r\n" .
// 'X-Mailer: PHP/' . phpversion();
// mail ($to, $subject, $message, $headers);
$result = send_email ($sender, "", "Lien de consultation des envoies de pi&egrave;ces jointes", "Bonjour!");
require (JIRAFEAU_ROOT . 'lib/template/header.php');
echo M_SEND_TOKEN;
require (JIRAFEAU_ROOT.'lib/template/footer.php');
exit;
}
$found = [];
$stack = array (VAR_LINKS);
while ( ($d = array_shift ($stack)) && $d != null) {
$dir = scandir ($d);
foreach ($dir as $node) {
if (strcmp ($node, '.') == 0 || strcmp ($node, '..') == 0 ||
preg_match ('/\.tmp/i', "$node")) {
continue;
}
if (is_dir ($d . $node)) {
/* Push new found directory. */
$stack[] = $d . $node . '/';
} elseif (is_file ($d . $node)) {
/* Read link informations. */
$l = jirafeau_get_link ($node);
if (!count ($l)) {
continue;
}
if (!@preg_match ("/archive_content/", jirafeau_escape ($l ['file_name'])) || jirafeau_escape ($l ['mime_type']) != "text/plain") {
continue;
}
$archive_info = read_archive_info ($l);
if ($sender != $archive_info [T_SENDER])
continue;
if (isset ($archive_info [T_NEW]))
foreach ($archive_info [T_NEW] as [$link_name, $crypt_key])
$found [$link_name] = $crypt_key;
}
}
}
foreach ($found as $link_name => $crypt_key)
$content .= $link_name . " - " . $crypt_key . NL;
2022-02-10 01:37:43 +01:00
// XXX find
// add link : name / time / download / delete
// sort by name ?
// $tmpFileName = tempnam (sys_get_temp_dir (), $dirname."-");
// $fd = fopen ($tmpFileName, "w");
// if (!$fd)
// return_error ("Unable to open tmp file!");
// fwrite ($fd, $content);
// fclose ($fd);
2022-02-11 01:01:24 +01:00
header ('HTTP/1.0 200 OK');
header ('Content-Length: ' . strlen ($content));
header ('Content-Type: text/plain');
2022-02-10 01:37:43 +01:00
echo $content;
?>