Merge branch 'master' of /home/git/platal into profile_edit
[platal.git] / include / validations / photos.inc.php
index 6d8fcc3..c62cff7 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2006 Polytechnique.org                              *
+ *  Copyright (C) 2003-2007 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -25,110 +25,72 @@ class PhotoReq extends Validate
 {
     // {{{ properties
 
-    var $mimetype;
-    var $data;
-    var $x;
-    var $y;
+    public $mimetype;
+    public $data;
+    public $x;
+    public $y;
 
-    var $unique = true;
+    public $unique = true;
+    public $valid  = false;
 
-    var $rules = "Refuser les photos copyrightées, de mineurs, ou ayant
-    un caractère pornographique, violent, etc... Si une photo est mal
-    cadrée (20% de photo et 80% de blanc par exemple), si c'est un
-    camarade antique, on lui arrange sinon on lui
-    refuse en lui expliquant gentiment le problème. Idem si les dimensions de
-    la photo sont archi trop grandes ou archi trop petites.";
+    public $rules = "Refuser les photos copyrightées, de mineurs, ou ayant
+        un caractère pornographique, violent, etc... Si une photo est mal
+        cadrée (20% de photo et 80% de blanc par exemple), si c'est un
+        camarade antique, on lui arrange sinon on lui
+        refuse en lui expliquant gentiment le problème. Idem si les dimensions de
+        la photo sont archi trop grandes ou archi trop petites.";
 
     // }}}
     // {{{ constructor
 
-    function PhotoReq($_uid, $_data, $_stamp=0)
+    public function __construct($_uid, PlUpload &$upload, $_stamp=0)
     {
-        $this->Validate($_uid, true, 'photo', $_stamp);
-        if (!$this->_get_image($_data)) {
-            return ($this = null);
-        }
+        parent::__construct($_uid, true, 'photo', $_stamp);
+        $this->read($upload);
     }
 
     // }}}
-    // {{{ function _get_image()
+    // {{{ function read
 
-    function _get_image($_data)
+    private function read(PlUpload &$upload)
     {
-        global $page;
-
-        // calcul de la taille de l'image
-        require_once dirname(__FILE__).'/../../classes/VarStream.php';
-        $GLOBALS['photoreq'] = $_data;
-        $image_infos = getimagesize('var://photoreq');
-        unset ($GLOBALS['photoreq']);
-
-        if (empty($image_infos)) {
-            $page->trig("Image invalide");
-            return false;
+        $this->valid = $upload->resizeImage(240, 300, 160, 0, SIZE_MAX);
+        if (!$this->valid) {
+            $this->trig('Le fichier que tu as transmis n\'est pas une image valide, ou est trop gros pour être traité');
         }
-        list($this->x, $this->y, $this->mimetype) = $image_infos;
-
-        switch ($this->mimetype) {
-            case 1: $this->mimetype = "gif";    break;
-            case 2: $this->mimetype = "jpeg";   break;
-            case 3: $this->mimetype = "png";    break;
-            default:
-                $page->trig("Type d'image invalide");
-                return false;
-        }
-
-        if (strlen($_data) > SIZE_MAX)  {
-            $img = imagecreatefromstring($_data);
-            if (!$img) {
-                $page->trig("image trop grande et impossible à retailler automatiquement");
-                return false;
-            }
-
-            $nx = $x = imagesx($img);
-            $ny = $y = imagesy($img);
-
-            if ($nx > 240) { $ny = intval($ny*240/$nx); $nx = 240; }
-            if ($ny > 300) { $ny = intval($nx*300/$nx); $ny = 300; }
-            if ($nx < 160) { $ny = intval($ny*160/$nx); $nx = 160; }
-
-            $comp = '90';
-            $file = tempnam('/tmp', 'photo');
-
-            while (strlen($_data) > SIZE_MAX) {
-                $img2  = imagecreatetruecolor($nx, $ny);
-                imagecopyresampled($img2, $img, 0, 0, 0, 0, $nx, $ny, $x, $y);
-                imagejpeg($img2, $file, $comp);
-                $_data = file_get_contents($file);
-                $this->mimetype = 'jpeg';
+        $this->data = $upload->getContents();
+        list($this->x, $this->y, $this->mimetype) = $upload->imageInfo();
+        $upload->rm();
+    }
 
-                $comp --;
-            }
+    // }}}
+    // {{{ function isValid()
 
-            unlink($file);
-        }
-        $this->data = $_data;
-        return true;
+    public function isValid()
+    {
+        return $this->valid;
     }
 
     // }}}
     // {{{ function get_request()
 
-    function get_request($uid)
+    static public function get_request($uid)
     {
-        return parent::get_request($uid,'photo');
+        return parent::get_typed_request($uid,'photo');
     }
 
     // }}}
     // {{{ function formu()
 
-    function formu()
-    { return 'include/form.valid.photos.tpl'; }
+    public function formu()
+    {
+        return 'include/form.valid.photos.tpl';
+    }
 
     // }}}
     // {{{ function editor()
 
-    function editor()
+    public function editor()
     {
         return 'include/form.valid.edit-photo.tpl';
     }
@@ -136,25 +98,24 @@ class PhotoReq extends Validate
     // }}}
     // {{{ function handle_editor()
 
-    function handle_editor()
+    protected function handle_editor()
     {
-        if (isset($_FILES['userfile']['tmp_name'])) {
-            $file = $_FILES['userfile']['tmp_name'];
-            if ($data = file_get_contents($file)) {
-                if ($this->_get_image($data)) {
-                    return true;
-                }
-            } else {
-                $page->trig('Fichier inexistant ou vide');
+        if (isset($_FILES['userfile'])) {
+            $upload =& PlUpload::get($_FILES['userfile'], S::v('forlife'), 'photo');
+            if (!$upload) {
+                $this->trig('Une erreur est survenue lors du téléchargement du fichier');
+                return false;
             }
+            $this->read($upload);
+            return $this->valid;
         }
-        return false; 
+        return false;
     }
 
     // }}}
     // {{{ function _mail_subj
 
-    function _mail_subj()
+    protected function _mail_subj()
     {
         return "[Polytechnique.org/PHOTO] Changement de photo";
     }
@@ -162,25 +123,25 @@ class PhotoReq extends Validate
     // }}}
     // {{{ function _mail_body
 
-    function _mail_body($isok)
+    protected function _mail_body($isok)
     {
         if ($isok) {
-            return "Le changement de photo que tu as demandé vient d'être effectué.";
+            return "Le changement de photo que tu as demandé vient d'être effectué.";
         } else {
-            return "La demande de changement de photo que tu avais faite a été refusée.";
+            return "La demande de changement de photo que tu avais faite a été refusée.";
         }
     }
 
     // }}}
     // {{{ function commit()
 
-    function commit()
+    public function commit()
     {
         XDB::execute('REPLACE INTO  photo (uid, attachmime, attach, x, y)
-                                      VALUES  ({?},{?},{?},{?},{?})',
-                                      $this->uid, $this->mimetype, $this->data, $this->x, $this->y);
-       require_once('notifs.inc.php');
-       register_watch_op($this->uid,WATCH_FICHE);
+                            VALUES  ({?},{?},{?},{?},{?})',
+                     $this->uid, $this->mimetype, $this->data, $this->x, $this->y);
+        require_once('notifs.inc.php');
+        register_watch_op($this->uid, WATCH_FICHE);
         return true;
     }
 
@@ -189,5 +150,5 @@ class PhotoReq extends Validate
 
 // }}}
 
-// vim:set et sw=4 sts=4 sws=4 foldmethod=marker:
+// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
 ?>