linea21-core
[ class tree: linea21-core ] [ index: linea21-core ] [ all elements ]

Source for file class.upload.php

Documentation is available at class.upload.php

  1. <?php
  2. /**
  3.  * @package linea21.core
  4.  * @subpackage system
  5.  * @author linea21 <info@linea21.com>
  6.  * @version $id SVN
  7.  * @access public
  8.  * @license http://opensource.org/licenses/gpl-3.0.html
  9.  *  Upload management
  10.  */
  11.  
  12. class upload {
  13.   /* @parametres
  14.    * */
  15.   var $FINAL_NAME;
  16.   var $FILE;
  17.   var $DESTINATION;
  18.   var $FTP_SERVER = "localhost";
  19.   var $FTP_USER = "demo_linea";
  20.   var $FTP_PASSWD = "demo_linea";
  21.   var $FTP_MODE = "FTP_BINARY";
  22.   var $FTP_ADMINMAIL = MAIL_LINEA;
  23.   var $TEXT_LENGTH = 5;
  24.   var $TEXT = 5;
  25.   var $IMAGE_TYPE = array("jpeg""jpg""png""gif""svg");
  26.  
  27.   /**
  28.    * upload::CheckMaxFile()
  29.    * Vérifie la taille d'un fichier
  30.    *
  31.    * @access public
  32.    * @param string $file_size : taille du fichier a uploader
  33.    * @param string $max_file : taille de fichier maximale
  34.    * @return bool true si ok sinon message d'erreur (string)
  35.    */
  36.   function CheckMaxFile($file_size$max_file)
  37.   {
  38.     include('../languages/' U_L '/lang_system.'CHARSET .'.php');
  39.     if ($file_size $max_file{
  40.       return sprintf($lang['upload']['upload_size']formatBytes($max_file));
  41.     }
  42.     return true;
  43.   }
  44.  
  45.   /**
  46.    * upload::CheckMimeImageType()
  47.    * verifie le type Mime d'une image
  48.    *
  49.    * @access public
  50.    * @param string $dirname : chemin + nom du fichier
  51.    * @return boolean $result
  52.    */
  53.   function CheckMimeImageType($dirname)
  54.   {
  55.     if (function_exists('exif_imagetype')) {
  56.       $exif_type exif_imagetype($dirname);
  57.       if ($exif_type == || $exif_type == || $exif_type == 3return true;
  58.       else unlink($dirname);
  59.     }
  60.     return false;
  61.   }
  62.  
  63.   /**
  64.    * upload::CheckExtImage()
  65.    * Verifie l'extension d'une image
  66.    *
  67.    * @access public
  68.    * @param string $filename : nom du fichier
  69.    * @return string $result
  70.    */
  71.   function CheckExtImage($filename)
  72.   {
  73.     include('../languages/' U_L '/lang_system.'CHARSET .'.php');
  74.  
  75.     $result $lang['upload']['CheckExtImage'];
  76.  
  77.     $extension $this->GetExtension($filename);
  78.     $ext $this->IMAGE_TYPE;
  79.     for ($i 0;$i count($ext);$i++{
  80.       if ($extension == $ext[$i]return true;
  81.     }
  82.  
  83.     return $result;
  84.   }
  85.  
  86.   /**
  87.    * upload::GetExtension()
  88.    * détermine l'extension d'un fichier
  89.    *
  90.    * @access public
  91.    * @param string $filename : fichier source
  92.    * @return string $extension
  93.    */
  94.   function GetExtension($filename)
  95.   {
  96.     $ext explode('.'$filename);
  97.     $extension $ext[count($ext)-1];
  98.     return $extension;
  99.   }
  100.  
  101.   /**
  102.    * upload::GetName()
  103.    * obtenir le nom d'un fichier, sans son extension
  104.    *
  105.    * @access public
  106.    * @param string $filename : fichier source
  107.    * @return string $name
  108.    */
  109.   function GetName($filename)
  110.   {
  111.     $ext explode('.'$filename);
  112.     $i 0;
  113.     $name '';
  114.     while ($i count($ext)-1{
  115.       $name .= $ext[$i'.';
  116.       $i++;
  117.     }
  118.     return $name;
  119.   }
  120.  
  121.   /**
  122.    * getMediaAllowedExt()
  123.    * Return an array of
  124.    * allowed extensions
  125.    * @return array 
  126.    ***/
  127.   function getMediaAllowedExt({
  128.  
  129.     $a explode(','MEDIA_ALLOWED_EXT);
  130.  
  131.     return $a;
  132.   }
  133.  
  134.   /**
  135.    * upload::Archivefile()
  136.    * archive un fichier si nécessaire
  137.    *
  138.    * @access public
  139.    * @param  $path 
  140.    * @return void 
  141.    */
  142.   function Archivefile($path)
  143.   {
  144.     $ext $this->IMAGE_TYPE;
  145.     $short_path dirname($path);
  146.     $filename basename($path);
  147.     $temp_path $short_path "/temp_" $filename;
  148.     $shortname $this->GetName($filename);
  149.  
  150.     for($i 0$i count($ext)$i++{
  151.       $current_path $short_path "/" $shortname $ext[$i];
  152.       $current_old_path $short_path "/old_" $shortname $ext[$i];
  153.       $current_temp_path $short_path "/temp_" $shortname $ext[$i];
  154.       if (file_exists($current_temp_path)) {
  155.         if (file_exists($current_old_path)) unlink($current_old_path);
  156.         if (file_exists($current_path)) rename($current_path$current_old_path);
  157.         // gestion des fichiers de miniatures
  158.         if (file_exists(get_min_name($current_old_path))) unlink(get_min_name($current_old_path));
  159.         if (file_exists(get_min_name($current_path))) rename(get_min_name($current_path)get_min_name($current_old_path));
  160.       }
  161.     }
  162.  
  163.     if (file_exists($temp_path)) rename ($temp_path$path);
  164.     if (file_exists(get_min_name($temp_path))) rename (get_min_name($temp_path)get_min_name($path));
  165.     return true;
  166.   }
  167.  
  168.   /**
  169.    * upload::_HTTPUpload()
  170.    * upload un fichier - Protocole HTTP
  171.    *
  172.    * @access private
  173.    * @return boolean : Message d'erreur ou True;
  174.    */
  175.   function _HTTPUpload()
  176.   {
  177.     include('../languages/' U_L '/lang_system.'CHARSET .'.php');
  178.     $result false;
  179.     if ($this->FILE['name'!= 'none' && $this->FILE['size'!= 0{
  180.         
  181.         // if file already exists we delet it first
  182.       if (file_exists($this->DESTINATION . "/" $this->FINAL_NAME)) unlink ($this->DESTINATION . "/" $this->FINAL_NAME);
  183.       
  184.       if (!file_exists($this->DESTINATION . "/" $this->FINAL_NAME)) {
  185.           
  186.           // we create folder if needed
  187.         if (!file_exists($this->DESTINATION)) {
  188.           mkdir($this->DESTINATION0755true);
  189.         }
  190.         if (move_uploaded_file($this->FILE['tmp_name']$this->DESTINATION . $this->FINAL_NAME)) {
  191.           $result true;
  192.         else {
  193.           $result $lang['upload']['prohibited_transfert'];
  194.         }
  195.       else {
  196.         $result "'" $this->FINAL_NAME . "'" $lang['upload']['file_exist_yet'];
  197.       }
  198.     }
  199.     return $result;
  200.   }
  201.  
  202.   /**
  203.    * upload::_FTPUpload()
  204.    * !!! NON IMPLEMENTE/ NON TESTE !!!
  205.    * upload un fichier - Protocole FTP
  206.    *
  207.    * @access private
  208.    * @return boolean : Message d'erreur ou True;
  209.    */
  210.   function _FTPUpload()
  211.   {
  212.     include('../languages/' U_L '/lang_system.'CHARSET .'.php');
  213.     $this->DESTINATION = "./" $this->DESTINATION;
  214.     $result false;
  215.     $conn_id @ftp_connect($this->FTP_SERVER);
  216.     $login_result @ftp_login($conn_id$this->FTP_USER$this->FTP_PASSWD);
  217.     if ((!$conn_id|| (!$login_result)) {
  218.       $erreur $lang['upload']['ftp_conn_failed'END_LINE;
  219.       $erreur .= $lang['upload']['ftp_conn'' ' $this->FTP_SERVER . ' ' $lang['upload']['ftp_user'' : ' FTP_USER ;
  220.       if ($this->FTP_ADMINMAIL != -1@error_log ($erreur1$this->FTP_ADMINMAIL);
  221.     else {
  222.       if (!(@ftp_chdir($conn_id$this->DESTINATION))) {
  223.         ftp_mkdir($conn_id$this->DESTINATION);
  224.         ftp_chdir($conn_id$this->DESTINATION);
  225.       }
  226.       $upload_result ftp_put($conn_id$this->FINAL_NAME$this->FILE['tmp_name']$this->FTP_MODE);
  227.       if (!$upload_result{
  228.         $result $lang['upload']['ftp_transfert_error'];
  229.       else {
  230.         $result true;
  231.       }
  232.       ftp_close($conn_id);
  233.     }
  234.     return $result;
  235.   }
  236.   
  237.   
  238.   /**
  239.    * deleteImages()
  240.    * For a given image file (ie : /path/1.jpg), it will delete
  241.    * /path/1.gif, /path/1.jpeg, /path/1.png files
  242.    * @param string $file 
  243.    * @param boolean $exclude 
  244.    * @return boolean 
  245.    */
  246.   public function deleteImages($file$exclude true{
  247.        
  248.       $path_parts pathinfo($file);
  249.       $path $path_parts['dirname'].'/'.$path_parts['filename'];
  250.       
  251.       if($exclude === true{
  252.           $exts array_diff($this->IMAGE_TYPEarray($path_parts['extension']));
  253.       else {
  254.           $exts $this->IMAGE_TYPE;
  255.       }
  256.       
  257.       for($i=0$i count($exts)$i++{
  258.  
  259.           if(file_exists($path'.'$exts[$i])) {
  260.               unlink ($path'.'$exts[$i]);
  261.           }
  262.       }
  263.       return true;
  264.   
  265.   }
  266.  
  267.   /**
  268.    * upload::UploadFile()
  269.    * upload d'un fichier
  270.    *
  271.    * @access public
  272.    * @param string $file : tableau contenant les caractéristiques du fichier à télécharger
  273.    * @param string $final_name : nom de sortie du fichier
  274.    * @param string $destination : répertoire de destination du fichier
  275.    * @return boolean : Message d'erreur ou True;
  276.    */
  277.   function UploadFile($file$final_name$destination)
  278.   {
  279.     $this->FILE = $file;
  280.     $this->FINAL_NAME = $final_name;
  281.     $this->DESTINATION = str_replace('//','/',$destination);
  282.  
  283.     /**
  284.      * echo "poids maxi en ko : " . $_POST['MAX_FILE_SIZE'] . "<br>";
  285.      * echo "nom du fichier : " . $file['name'] . "<br>";
  286.      * echo "nom temp du fichier : " . $file['tmp_name'] . "<br>";
  287.      * echo "taille du fichier : " . $file['size'] . "<br>";
  288.      * echo "type du fichier : " . $file['type'] . "<br>";
  289.      * echo "ereur du fichier : " . $file['error'] . "<br>";
  290.      * echo "nom final du fichier : " . $final_name . "<br>";
  291.      * echo "destination du fichier : " . $destination . "<br>";
  292.      * echo "methode de transfert du fichier : " . $method . "<br>";
  293.      */
  294.  
  295.     switch (UPLOAD_METHOD{
  296.       case 'FTP':
  297.         $result $this->_FTPUpload();
  298.         break;
  299.       case 'HTTP':
  300.         $result $this->_HTTPUpload();
  301.         break;
  302.       default:
  303.         $result $this->_HTTPUpload();
  304.         break;
  305.     }
  306.  
  307.     return $result;
  308.   }
  309. }
  310.  
  311. ?>

Documentation generated on Thu, 20 Mar 2014 16:46:59 +0100 by phpDocumentor 1.4.1