public function createFile($name, $data = null) { global $charset, $base_path, $id_rep; if ($this->check_write_permission()) { if ($charset !== 'utf-8') { $name = utf8_decode($name); } $filename = realpath($base_path . "/temp/") . "/webdav_" . md5($name . time()) . "." . extension_fichier($name); $fp = fopen($filename, "w"); if (!$fp) { //on a pas le droit d'écriture throw new DAV\Exception\Forbidden('Permission denied to create file (filename ' . $filename . ')'); } while ($buf = fread($data, 1024)) { fwrite($fp, $buf); } fclose($fp); $notice_id = $this->notice_id; $bulletin_id = 0; $query = "SELECT CONCAT(niveau_biblio, niveau_hierar) AS niveau FROM notices WHERE notice_id = " . $this->notice_id; $result = pmb_mysql_query($query); if (pmb_mysql_num_rows($result)) { $row = pmb_mysql_fetch_object($result); if ($row->niveau == "b2") { $query = "SELECT bulletin_id FROM bulletins WHERE num_notice = " . $this->notice_id; $result = pmb_mysql_query($query); if (pmb_mysql_num_rows($result)) { $row = pmb_mysql_fetch_object($result); $notice_id = 0; $bulletin_id = $row->bulletin_id; } } } $explnum = new \explnum(0, $notice_id, $bulletin_id); $id_rep = $this->parentNode->config['upload_rep']; $explnum->get_file_from_temp($filename, $name, $this->parentNode->config['up_place']); $explnum->params['explnum_statut'] = $this->config['default_docnum_statut']; $explnum->update(); $this->update_notice($this->notice_id); if (file_exists($filename)) { unlink($filename); } } else { //on a pas le droit d'écriture throw new DAV\Exception\Forbidden('Permission denied to create file (filename ' . $name . ')'); return false; } }
public function httpPOSTHandler($method, $uri) { if ($method != 'POST') { return true; } if (isset($_POST['sabreAction'])) { switch ($_POST['sabreAction']) { case 'mkcol': if (isset($_POST['name']) && trim($_POST['name'])) { // Using basename() because we won't allow slashes list(, $folderName) = DAV\URLUtil::splitPath(trim($_POST['name'])); $this->server->createDirectory($uri . '/' . $folderName); } break; case 'put': if ($_FILES) { $file = $_FILES['file']; } else { break; } $newName = trim($file['name']); list(, $newName) = DAV\URLUtil::splitPath(trim($file['name'])); if (isset($_POST['name']) && trim($_POST['name'])) { $newName2 = trim($_POST['name']); list(, $newName2) = DAV\URLUtil::splitPath($newName2); $newName = $newName2 . "." . extension_fichier($newName); } // Making sure we only have a 'basename' component if (is_uploaded_file($file['tmp_name'])) { $this->server->createFile($uri . '/' . $newName, fopen($file['tmp_name'], 'r')); } } } $this->server->httpResponse->setHeader('Location', $this->server->httpRequest->getUri()); return false; }
function extract_metas($filename, $mimetype, $tmp = false) { global $base_path, $class_path; global $charset; //$metas = array(); switch ($mimetype) { //EPub case "application/epub+zip": //Exiftool ne donnerait rien, mais un Epub contient toutes les infos qui nous faut ! require_once $class_path . "/epubData.class.php"; $epub = new epubData($filename); $metas = $epub->metas; break; case "application/pdf": // exec("exiftool -struct -J -q ".$filename,$metas); // $metas = json_decode(implode("\n",$metas),true); exec("exiftool " . $filename, $tab); $metas = array(); foreach ($tab as $row) { $elem = explode(":", $row); $key = trim(str_replace(" ", "", array_shift($elem))); $value = trim(implode(":", $elem)); if ($charset != "utf-8") { $key = utf8_decode($key); $value = utf8_decode($value); } $metas[$key] = $value; } break; default: $type = substr($mimetype, 0, strpos($mimetype, "/")); switch ($type) { case "image": case "video": case "audio": exec("exiftool " . $filename, $tab); $metas = array(); foreach ($tab as $row) { $elem = explode(":", $row); $key = trim(str_replace(" ", "", array_shift($elem))); $value = trim(implode(":", $elem)); if ($charset != "utf-8" && mb_detect_encoding($value) == 'UTF-8') { $key = utf8_decode($key); $value = utf8_decode($value); } $metas[$key] = $value; } break; case "text": //pas de métas pertinantes sur une fichier texte... break; default: if (!extension_fichier(basename($filename))) { $new_name = basename($filename) . "temp"; //Pour éviter que si pas d'extension on perde le fichier } else { $new_name = str_replace(extension_fichier(basename($filename)), "pdf", basename($filename)); } $new_path = dirname($filename) . "/" . $new_name; exec("curl http://localhost:8080/converter/converted/" . $new_name . " -F \"inputDocument=@{$filename}\" > " . $new_path); //Ne doit marcher que dans un cas très précis, pas vrai Arnaud $metas = extract_metas($new_path, "application/pdf", true); break; } break; } if ($tmp) { unlink($filename); } return $metas; }
function analyse_file() { if ($this->tache_docnum_file) { create_tableau_mimetype(); $userfile_name = $this->tache_docnum_file['name']; $userfile_temp = $this->tache_docnum_file['tmp_name']; $userfile_moved = basename($userfile_temp); $userfile_name = preg_replace("/ |'|\\|\"|\\//m", "_", $userfile_name); $userfile_ext = ''; if ($userfile_name) { $userfile_ext = extension_fichier($userfile_name); } move_uploaded_file($userfile_temp, "./temp/" . $userfile_moved); $file_name = "./temp/" . $userfile_moved; $fp = fopen($file_name, "r"); $contenu = fread($fp, filesize($file_name)); fclose($fp); $mime = trouve_mimetype($userfile_moved, $userfile_ext); if (!$mime) { $mime = "application/data"; } $this->tache_docnum_mimetype = $mime; $this->tache_docnum_nomfichier = $userfile_name; $this->tache_docnum_extfichier = $userfile_ext; $this->tache_docnum_data = $contenu; unlink($file_name); } }
function create_docnum($doc) { global $bull_id; global $notice_id; $id_rep = 1; $rep = new upload_folder($id_rep); $name = $doc['nom_fic'] ? $doc['nom_fic'] : $doc['titre']; $filename = strtolower(implode("_", explode(" ", $name))); $filename = checkIfExist($rep->repertoire_path, $filename, $filename); file_put_contents($rep->repertoire_path . $filename, file_get_contents($doc['url'])); $ext_fichier = extension_fichier($filename); if ($doc['mimetype'] == "") { create_tableau_mimetype(); $mimetype = trouve_mimetype($filename, $ext_fichier); } else { $mimetype = $doc['mimetype']; } $insert = "insert into explnum set "; if ($bull_id != 0) { $insert .= "explnum_bulletin = {$bull_id}, "; } else { $insert .= "explnum_notice = {$notice_id}, "; } $insert .= "explnum_nom = '" . $doc['titre'] . "', "; $insert .= "explnum_mimetype = '{$mimetype}', "; $insert .= "explnum_nomfichier = '" . $filename . "', "; $insert .= "explnum_extfichier = '{$ext_fichier}', "; $insert .= "explnum_repertoire = {$id_rep}, "; $insert .= "explnum_path = '/'"; $result = mysql_query($insert); $explnum_id = mysql_insert_id(); return $explnum_id; }
/** * Exécution du processus d'indexation */ function run_index() { if ($this->mimetype == 'URL') { //récupération par cURL $this->get_file_from_curl($this->file_url); create_tableau_mimetype(); $this->mimetype = trouve_mimetype($this->fichier); if (!$this->mimetype) { //Test sur l'extension du fichier $this->ext = extension_fichier($this->file_url); $this->mimetype = trouve_mimetype($this->file_url, $this->ext); } if (!$this->mimetype && $this->explnum_nomfichier) { //Test sur l'extension du fichier $this->ext = extension_fichier($this->explnum_nomfichier); $this->mimetype = trouve_mimetype($this->file_url, $this->ext); } if ($this->mimetype && !$this->ext) { $this->ext = extension_fichier($this->file_url); } if ($this->mimetype && $this->explnum_nomfichier && !$this->ext) { $this->ext = extension_fichier($this->explnum_nomfichier); } if (!$this->mimetype) { $this->mimetype = "URL"; } global $prefix_url_image; if ($prefix_url_image) { $tmpprefix_url_image = $prefix_url_image; } else { $tmpprefix_url_image = "./"; } if ($tmp = construire_vignette('', "", $this->file_url)) { $this->vignette = $tmp; } else { $this->vignette = construire_vignette('', $tmpprefix_url_image . "images/mimetype/" . icone_mimetype($this->mimetype, $this->ext)); } } else { //récupération dans la base $this->get_file($this->file_content); create_tableau_mimetype(); if (!$this->mimetype) { $this->mimetype = trouve_mimetype($this->fichier); } if (!$this->mimetype && $this->explnum_nomfichier) { //Test sur l'extension du fichier $this->ext = extension_fichier($this->explnum_nomfichier); $this->mimetype = trouve_mimetype($this->fichier, $this->ext); } } if (file_exists($this->fichier)) { //On parse le XML pour recupérer le nom de la classe $this->parse_xml(); //On choisit la classe correspondant au traitement du type MIME $this->choose_class($this->class_associee); } }
function get_file_from_temp($filename, $name, $upload_place) { global $base_path; global $ck_index; global $id_rep, $up_place; $up_place = $upload_place; create_tableau_mimetype(); $ck_index = true; //Initialisation des tableaux d'infos $this->infos_docnum = $this->params = array(); $this->infos_docnum["mime"] = trouve_mimetype($filename, extension_fichier($name)); $this->infos_docnum["nom"] = substr($name, 0, strrpos($name, ".")); if (!$this->infos_docnum["nom"]) { $this->infos_docnum["nom"] = $name; } $this->infos_docnum["notice"] = $this->explnum_notice; $this->infos_docnum["bull"] = $this->explnum_bulletin; $this->infos_docnum["url"] = ""; $this->infos_docnum["fic"] = false; $this->infos_docnum["contenu_vignette"] = construire_vignette('', substr($filename, strrpos($filename, "/"))); $this->infos_docnum["userfile_name"] = $name; $this->infos_docnum["userfile_ext"] = extension_fichier($name); if ($up_place && $id_rep != 0) { $upfolder = new upload_folder($id_rep); $chemin_hasher = "/"; if ($upfolder->isHashing()) { $rep = $upfolder->hachage($this->infos_docnum["userfile_name"]); @mkdir($rep); $chemin_hasher = $upfolder->formate_path_to_nom($rep); $file_name = $rep . $this->infos_docnum["userfile_name"]; $chemin = $upfolder->formate_path_to_save($chemin_hasher); } else { $file_name = $upfolder->get_path($this->infos_docnum["userfile_name"]) . $this->infos_docnum["userfile_name"]; $chemin = $upfolder->formate_path_to_save("/"); } $this->infos_docnum["path"] = $chemin; $file_name = $upfolder->encoder_chaine($file_name); if (!$this->explnum_nomfichier) { //Si je suis en création de fichier numérique $nom_tmp = $this->infos_docnum["userfile_name"]; $continue = true; $compte = 1; do { $query = "select explnum_notice,explnum_id from explnum where explnum_nomfichier = '" . addslashes($nom_tmp) . "' AND explnum_repertoire='" . $id_rep . "' AND explnum_path='" . addslashes($this->infos_docnum["path"]) . "'"; $result = mysql_query($query); if (mysql_num_rows($result) && mysql_result($result, 0, 0) != $this->infos_docnum["notice"]) { //Si j'ai déjà un document numérique avec ce fichier pour une autre notice je dois le renommer pour ne pas perdre l'ancien if (preg_match("/^(.+)(\\..+)\$/i", $this->infos_docnum["userfile_name"], $matches)) { $nom_tmp = $matches[1] . "_" . $compte . $matches[2]; } else { $nom_tmp = $this->infos_docnum["userfile_name"] . "_" . $compte; } $compte++; } else { if (mysql_num_rows($result)) { //J'ai déjà ce fichier pour cette notice //Je dois enlever l'ancien document numérique pour ne pas l'avoir en double $old_docnum = new explnum(mysql_result($result, 0, 1)); $old_docnum->delete(); } else { } $continue = false; } } while ($continue); if ($compte != 1) { $this->infos_docnum["userfile_name"] = $nom_tmp; if ($upfolder->isHashing()) { $file_name = $rep . $this->infos_docnum["userfile_name"]; } else { $file_name = $upfolder->get_path($this->infos_docnum["userfile_name"]) . $this->infos_docnum["userfile_name"]; } $file_name = $upfolder->encoder_chaine($file_name); } else { } } else { } rename($filename, $file_name); } else { //enregistrement en base $this->infos_docnum["contenu"] = file_get_contents($filename); } $this->params["maj_mimetype"] = true; $this->params["maj_data"] = true; $this->params["maj_vignette"] = true; }
/** * @param $name : file name * @param $filename : file name with path * @return Array : le tableau des métadonnées du fichier */ static function getMetadata($filename, $name) { \create_tableau_mimetype(); $mimetype = \trouve_mimetype($filename, extension_fichier($name)); if ($mimetype == "application/epub+zip") { //récupération de l'image $epub = new \epubData(realpath($filename)); //TODO : Vérifier la récupération des métadonnées d'un epub avec \extract_metas(), sinon rétablir les commentaires ici et le else plus bas // $tmp=array(); // $tmp=$epub->metas; // foreach($tmp as $key=>$val){ // $metas[strtoupper(substr($key,0,1)).substr($key,1)]=$val; // } $img = imagecreatefromstring($epub->getCoverContent()); $file = tempnam(sys_get_temp_dir(), "vign"); imagepng($img, $file); $entry['thumbnail_url'] = 'data:image/png;base64,' . base64_encode(file_get_contents($file)); unlink($file); } // else{ return \extract_metas(realpath($filename), $mimetype); // } }
function createFile($name, $data = null) { if ($this->check_write_permission()) { global $base_path; global $id_rep; global $charset; if ($charset !== 'utf-8') { $name = utf8_decode($name); } $filename = realpath($base_path . "/temp/") . "/webdav_" . md5($name . time()) . "." . extension_fichier($name); $fp = fopen($filename, "w"); if (!$fp) { //on a pas le droit d'écriture throw new DAV\Exception\Forbidden('Permission denied to create file (filename ' . $filename . ')'); } while ($buf = fread($data, 1024)) { fwrite($fp, $buf); } fclose($fp); if (!filesize($filename)) { //Erreur de copie du fichier //unlink($filename); //throw new Sabre_DAV_Exception_FileNotFound('Empty file (filename ' . $filename . ')'); } $notice_id = $this->get_notice_by_meta($name, $filename); $this->update_notice($notice_id); $explnum = new \explnum(0, $notice_id); $id_rep = $this->config['upload_rep']; $explnum->get_file_from_temp($filename, $name, $this->config['up_place']); $explnum->update(); if (file_exists($filename)) { unlink($filename); } } else { //on a pas le droit d'écriture throw new DAV\Exception\Forbidden('Permission denied to create file (filename ' . $name . ')'); } }