Happy New Year !
[platal.git] / include / validations / photos.inc.php
index f158f28..87d6c86 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2007 Polytechnique.org                              *
+ *  Copyright (C) 2003-2013 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -21,7 +21,7 @@
 
 // {{{ class PhotoReq
 
-class PhotoReq extends Validate
+class PhotoReq extends ProfileValidate
 {
     // {{{ properties
 
@@ -34,7 +34,7 @@ class PhotoReq extends Validate
     public $valid  = false;
 
     public $rules = "Refuser les photos copyrightées, de mineurs, ou ayant
-        un caractère pornographique, violent, etc... Si une photo est mal
+        un caractère pornographique, violent&hellip; 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
@@ -43,76 +43,28 @@ class PhotoReq extends Validate
     // }}}
     // {{{ constructor
 
-    public function __construct($_uid, $_data, $_stamp=0)
+    public function __construct(User $_user, Profile $_profile, PlUpload $upload, $_stamp = 0)
     {
-        $this->Validate($_uid, true, 'photo', $_stamp);
-        $this->valid = $this->_get_image($_data);
+        parent::__construct($_user, $_profile, true, 'photo', $_stamp);
+        $this->read($upload);
     }
 
     // }}}
-    // {{{ function _get_image()
+    // {{{ function read
 
-    private function _get_image($_data)
+    private function read(PlUpload $upload)
     {
-        global $page;
-
-        VarStream::init();
-
-        // calcul de la taille de l'image
-        $GLOBALS['photoreq'] = $_data;
-        $image_infos = getimagesize('var://photoreq');
-        unset ($GLOBALS['photoreq']);
-
-        if (empty($image_infos)) {
-            $page->trig("Image invalide");
-            return false;
-        }
-        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';
-
-                $comp --;
-            }
-
-            unlink($file);
+        $this->valid = $upload->resizeImage(240, 300, 160, 0, SIZE_MAX);
+        if (!$this->valid) {
+            $this->trigError('Le fichier que tu as transmis n\'est pas une image valide, ou est trop gros pour être traité.');
         }
-        $this->data = $_data;
-        return true;
+        $this->data = $upload->getContents();
+        list($this->x, $this->y, $this->mimetype) = $upload->imageInfo();
+        $upload->rm();
     }
 
     // }}}
-    // {{{ function isValid()
+    // {{{ function isValid()
 
     public function isValid()
     {
@@ -122,9 +74,9 @@ class PhotoReq extends Validate
     // }}}
     // {{{ function get_request()
 
-    static public function get_request($uid)
+    static public function get_request($pid)
     {
-        return parent::get_typed_request($uid,'photo');
+        return parent::get_typed_request($pid, 'photo');
     }
 
     // }}}
@@ -148,17 +100,16 @@ class PhotoReq extends Validate
 
     protected function handle_editor()
     {
-        if (isset($_FILES['userfile']['tmp_name'])) {
-            $file = $_FILES['userfile']['tmp_name'];
-            if ($data = file_get_contents($file)) {
-                if ($this->valid = $this->_get_image($data)) {
-                    return true;
-                }
-            } else {
-                $page->trig('Fichier inexistant ou vide');
+        if (isset($_FILES['userfile'])) {
+            $upload =& PlUpload::get($_FILES['userfile'], S::user()->login(), 'photo');
+            if (!$upload) {
+                $this->trigError('Une erreur est survenue lors du téléchargement du fichier.');
+                return false;
             }
+            $this->read($upload);
+            return $this->valid;
         }
-        return false; 
+        return false;
     }
 
     // }}}
@@ -186,11 +137,12 @@ class PhotoReq extends Validate
 
     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);
+        XDB::execute('INSERT INTO  profile_photos (pid, attachmime, attach, x, y)
+                           VALUES  ({?}, {?}, {?}, {?}, {?})
+          ON DUPLICATE KEY UPDATE  attachmime = VALUES(attachmime), attach = VALUES(attach),
+                                   x = VALUES(x), y = VALUES(y)',
+                     $this->profile->id(), $this->mimetype, $this->data, $this->x, $this->y);
+
         return true;
     }