* */ /** * Class docserver : manage the docservers and the filename in the classment on the docservers * * @author Claire Figueras * @license GPL * @package Maarch LetterBox 2.0 * @version 1.1 */ class docserver { /** * Draw the filename * * @param string $path_template path * @return array filename array */ private $error = null; private $sleep_time = 500; //ms private $timeout = 5; //sec private function filename($path_template) { // Draw the filename //if ($_SESSION['config']['system'] == "true") //{ // Draw the filename $file_tab = scandir($path_template); array_shift($file_tab); array_shift($file_tab); $nb_files = count($file_tab); if ($nb_files == 0 ) { //LB20060801 rights change : wrx if (!mkdir($path_template."1",0000755)) { return false; } else { //$destination_rept = $path_template."1/"; $destination_rept = $path_template."1".DIRECTORY_SEPARATOR; $file_destination_name = "1"; return array("destination_rept" => $destination_rept, "file_destination_name" => $file_destination_name); } } else { //Get next usable tree for the docserver // $destination_rept = $path_template.count($file_tab)."/"; $destination_rept = $path_template.count($file_tab).DIRECTORY_SEPARATOR; $file_tab2 = scandir($path_template.strval(count($file_tab))); // delete the . and .. line array_shift($file_tab2); array_shift($file_tab2); $nb_files2 = count($file_tab2); //if number of files => 2000 then we create a new directory if ($nb_files2 >= 2000 ) { $new_rept = ($nb_files) + 1; //LB20060801 rights change : wrx if (!mkdir($path_template.$new_rept,0000755)) { return false; } else { //$destination_rept = $path_template.$new_rept."/"; $destination_rept = $path_template.$new_rept.DIRECTORY_SEPARATOR; $file_destination_name = "1"; return array("destination_rept" => $destination_rept, "file_destination_name" => $file_destination_name); } } else { $new_file_name = ($nb_files2) + 1; //echo $destination_rept.$new_file_name."
"; //print_r($file_tab2); //modifs cfi 30 07 2008: correction annomalie docserver $greater = $new_file_name; for($n=0;$n"; if((int)$greater <= (int)$current_file_name[0]) { //echo 'new file name : '.(int)$greater.'
current : '.(int)$current_file_name[0].'
'; //echo "
warning greater file already exists".$file_tab2[$n]."
"; //$new_file_name ++; //$nb_inc ++; if((int)$greater == (int)$current_file_name[0]) { $greater ++; } else // $greater < current { $greater = (int)$current_file_name[0] +1; } } } $file_destination_name = $greater ; //echo 'destinatation : '.$file_destination_name."
"; // echo $greater; // exit(); ///////////// Fin correctif return array("destination_rept" => $destination_rept, "file_destination_name" => $file_destination_name); } } /*} else { $file_tab = scandir($path_template); array_shift($file_tab); array_shift($file_tab); $nb_files = count($file_tab); if ($nb_files == 0 ) { if (!mkdir($path_template."1",0000200)) { $_SESSION['error'] = _FILE_SEND_ERROR.". "._TRY_AGAIN.". "._MORE_INFOS." : ".$_SESSION['config']['adminname'].""; header("location: file_index.php"); exit; } else { $destination_rept = $path_template."1\\"; $file_destination_name = "1"; return array("destination_rept" => $destination_rept, "file_destination_name" => $file_destination_name); } } else { //Get next usable tree for the docserver $destination_rept = $path_template.count($file_tab)."\\"; $file_tab2 = scandir($path_template.strval(count($file_tab))); // delete the . and .. line array_shift($file_tab2); array_shift($file_tab2); $nb_files2 = count($file_tab2); //if number of files => 2000 then we create a new directory if ($nb_files2 >= 2000 ) { $new_rept = ($nb_files) + 1; if (!mkdir($path_template.$new_rept,0000755)) { $_SESSION['error'] = _FILE_SEND_ERROR.". "._TRY_AGAIN.". "._MORE_INFOS." : ".$_SESSION['config']['adminname'].""; header("location: file_index.php"); exit; } else { $destination_rept = $path_template.$new_rept."\\"; $file_destination_name = "1"; return array("destination_rept" => $destination_rept, "file_destination_name" => $file_destination_name); } } else { $new_file_name = ($nb_files2) + 1; //echo $destination_rept.$new_file_name."
"; //print_r($file_tab2); //modifs cfi 30 07 2008: correction annomalie docserver $greater = $new_file_name; for($n=0;$n"; if((int)$greater <= (int)$current_file_name[0]) { //echo 'new file name : '.(int)$greater.'
current : '.(int)$current_file_name[0].'
'; //echo "
warning greater file already exists".$file_tab2[$n]."
"; //$new_file_name ++; //$nb_inc ++; if((int)$greater == (int)$current_file_name[0]) { $greater ++; } else // $greater < current { $greater = (int)$current_file_name[0] +1; } } } $file_destination_name = $greater ; //echo 'destinatation : '.$file_destination_name."
"; // echo $greater; // exit(); ///////////// Fin correctif return array("destination_rept" => $destination_rept, "file_destination_name" => $file_destination_name); } } }*/ } public function get_last_error() { return $this->error; } public function store($docserver_id,$path_template,$filepath,$extension,$filesize) { require_once("class_db.php"); $db = new dbquery(); $end_at = time() + $this->timeout; try { while( true ) { //Lock the docserver : nobody can save file into the docserver $db->query("UPDATE docservers SET locked = 1 WHERE docserver_id = '".$docserver_id."' AND (locked = 0 OR locked IS NULL)"); if( $db->affected_rows() == 1 ) { break; } else { if( time() >= $end_at ) { throw new Exception(_DOCSERVER_LOCKED); } usleep( $this->sleep_time * 1000 ); } } } catch(Exception $e) { $this->error = $e->getMessage(); //Unlock the docserver $db->query("UPDATE docservers SET locked = 0 WHERE docserver_id = '".$docserver_id."'"); return array(); } //Retrieve the name of the new file in the docserver $new_file = $this->filename($path_template); if( !isset($new_file) || !is_array($new_file) ) { //Unlock the docserver $db->query("UPDATE docservers SET locked = 0 WHERE docserver_id = '".$docserver_id."'"); return array(); } try { //The file come from HTTP POST REQUEST if( is_uploaded_file($filepath) ) { move_uploaded_file($filepath,$new_file["destination_rept"].$new_file["file_destination_name"].".".$extension); } //Else else { copy($filepath,$new_file["destination_rept"].$new_file["file_destination_name"].".".$extension); } } catch(Exception $e) { $this->error = _FILE_SEND_ERROR.". "._TRY_AGAIN.". "._MORE_INFOS." : ".$e->getMessage(); //Unlock the docserver $db->query("UPDATE docservers SET locked = 0 WHERE docserver_id = '".$docserver_id."'"); return array(); } $db->query("UPDATE docservers SET actual_size = actual_size + ".$filesize." WHERE docserver_id = '".$docserver_id."'" ); //Unlock the docserver $db->query("UPDATE docservers SET locked = 0 WHERE docserver_id = '".$docserver_id."'"); return $new_file; } } ?>