function rebuilt_file_db() { $idir = rm_dots_dir(scandir($GLOBALS['BT_ROOT_PATH'] . $GLOBALS['dossier_images'])); // scans also subdir of img/* (in one single array of paths) foreach ($idir as $i => $e) { $subelem = $GLOBALS['BT_ROOT_PATH'] . $GLOBALS['dossier_images'] . '/' . $e; if (is_dir($subelem)) { unset($idir[$i]); // rm folder entry itself $subidir = rm_dots_dir(scandir($subelem)); foreach ($subidir as $j => $im) { $idir[] = $e . '/' . $im; } } } foreach ($idir as $i => $e) { $idir[$i] = '/' . $e; } $fdir = rm_dots_dir(scandir($GLOBALS['BT_ROOT_PATH'] . $GLOBALS['dossier_fichiers'])); // supprime les miniatures de la liste... $idir = array_filter($idir, function ($file) { return !(preg_match('#-thb\\.jpg$#', $file) or strpos($file, 'index.html') == 4); }); $files_disk = array_merge($idir, $fdir); $files_db = $files_db_id = array(); // supprime les fichiers dans la DB qui ne sont plus sur le disque foreach ($GLOBALS['liste_fichiers'] as $id => $file) { if (!in_array($file['bt_path'] . '/' . $file['bt_filename'], $files_disk)) { unset($GLOBALS['liste_fichiers'][$id]); } $files_db[] = $file['bt_path'] . '/' . $file['bt_filename']; $files_db_id[] = $file['bt_id']; } // ajoute les images/* du disque qui ne sont pas encore dans la DB. foreach ($idir as $file) { $filepath = $GLOBALS['BT_ROOT_PATH'] . $GLOBALS['dossier_images'] . '/' . $file; if (!in_array($file, $files_db)) { $time = filemtime($filepath); $id = date('YmdHis', $time); // vérifie que l’ID ne se trouve pas déjà dans le tableau. Sinon, modifie la date (en allant dans le passé) while (array_key_exists($id, $files_db_id)) { $time--; $id = date('YmdHis', $time); } $files_db_id[] = $id; $new_img = array('bt_id' => $id, 'bt_type' => 'image', 'bt_fileext' => strtolower(pathinfo($filepath, PATHINFO_EXTENSION)), 'bt_filesize' => filesize($filepath), 'bt_filename' => $file, 'bt_content' => '', 'bt_wiki_content' => '', 'bt_dossier' => 'default', 'bt_checksum' => sha1_file($filepath), 'bt_statut' => 0, 'bt_path' => preg_match('#^/[0-9a-f]{2}/#', $file) ? substr($file, 0, 3) : ''); list($new_img['bt_dim_w'], $new_img['bt_dim_h']) = getimagesize($filepath); // l’ajoute au tableau $GLOBALS['liste_fichiers'][] = $new_img; } // crée une miniature de l’image create_thumbnail($filepath); } // fait pareil pour les files/* foreach ($fdir as $file) { if (!in_array($file, $files_db)) { $filepath = $GLOBALS['BT_ROOT_PATH'] . $GLOBALS['dossier_fichiers'] . '/' . $file; $time = filemtime($filepath); $id = date('YmdHis', $time); // vérifie que l’ID ne se trouve pas déjà dans le tableau. Sinon, modifie la date (en allant dans le passé) while (array_key_exists($id, $files_db_id)) { $time--; $id = date('YmdHis', $time); } $files_db_id[] = $id; $ext = strtolower(pathinfo($filepath, PATHINFO_EXTENSION)); $new_file = array('bt_id' => $id, 'bt_type' => detection_type_fichier($ext), 'bt_fileext' => $ext, 'bt_filesize' => filesize($filepath), 'bt_filename' => $file, 'bt_content' => '', 'bt_wiki_content' => '', 'bt_dossier' => 'default', 'bt_checksum' => sha1_file($filepath), 'bt_statut' => 0, 'bt_path' => ''); // l’ajoute au tableau $GLOBALS['liste_fichiers'][] = $new_file; } } // tri le tableau fusionné selon les bt_id (selon une des clés d'un sous tableau). $GLOBALS['liste_fichiers'] = tri_selon_sous_cle($GLOBALS['liste_fichiers'], 'bt_id'); // finalement enregistre la liste des fichiers. file_put_contents($GLOBALS['fichier_liste_fichiers'], '<?php /* ' . chunk_split(base64_encode(serialize($GLOBALS['liste_fichiers']))) . ' */'); }
function init_post_fichier() { //no $mode : it's always admin. // on edit : get file info from form if (isset($_POST['is_it_edit']) and $_POST['is_it_edit'] == 'yes') { $file_id = htmlspecialchars($_POST['file_id']); $filename = pathinfo(htmlspecialchars($_POST['filename']), PATHINFO_FILENAME); $ext = strtolower(pathinfo(htmlspecialchars($_POST['filename']), PATHINFO_EXTENSION)); $checksum = htmlspecialchars($_POST['sha1_file']); $size = htmlspecialchars($_POST['filesize']); $type = detection_type_fichier($ext); $dossier = htmlspecialchars($_POST['dossier']); $path = htmlspecialchars($_POST['path']); // on new post, get info from the file itself } else { $file_id = date('YmdHis'); $dossier = htmlspecialchars($_POST['dossier']); // ajout de fichier par upload if (!empty($_FILES['fichier']) and $_FILES['fichier']['error'] == 0) { $filename = pathinfo($_FILES['fichier']['name'], PATHINFO_FILENAME); $ext = strtolower(pathinfo($_FILES['fichier']['name'], PATHINFO_EXTENSION)); $checksum = sha1_file($_FILES['fichier']['tmp_name']); $size = $_FILES['fichier']['size']; $type = detection_type_fichier($ext); $path = ''; // ajout par une URL d’un fichier distant } elseif (!empty($_POST['fichier'])) { $filename = pathinfo(parse_url($_POST['fichier'], PHP_URL_PATH), PATHINFO_FILENAME); $ext = strtolower(pathinfo(parse_url($_POST['fichier'], PHP_URL_PATH), PATHINFO_EXTENSION)); $checksum = sha1_file($_POST['fichier']); // works with URL files $size = ''; // same (even if we could use "filesize" with the URL, it would over-use data-transfer) $path = ''; $type = detection_type_fichier($ext); } else { // ERROR redirection(basename($_SERVER['PHP_SELF']) . '?errmsg=error_image_add'); return FALSE; } } // nom du fichier : si nom donné, sinon nom du fichier inchangé $filename = diacritique(htmlspecialchars(!empty($_POST['nom_entree']) ? $_POST['nom_entree'] : $filename), '', '0') . '.' . $ext; $statut = (isset($_POST['statut']) and $_POST['statut'] == 'on') ? '0' : '1'; $fichier = array('bt_id' => $file_id, 'bt_type' => $type, 'bt_fileext' => $ext, 'bt_filesize' => $size, 'bt_filename' => $filename, 'bt_content' => stripslashes(protect_markup(clean_txt($_POST['description']))), 'bt_wiki_content' => stripslashes(protect_markup(clean_txt($_POST['description']))), 'bt_checksum' => $checksum, 'bt_statut' => $statut, 'bt_dossier' => empty($dossier) ? 'default' : $dossier, 'bt_path' => empty($path) ? '/' . substr($checksum, 0, 2) : $path); return $fichier; }