avoid the ugly include for varstream, use a dummy VarStream::init()
[platal.git] / include / validations / photos.inc.php
... / ...
CommitLineData
1<?php
2/***************************************************************************
3 * Copyright (C) 2003-2006 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
22// {{{ class PhotoReq
23
24class PhotoReq extends Validate
25{
26 // {{{ properties
27
28 var $mimetype;
29 var $data;
30 var $x;
31 var $y;
32
33 var $unique = true;
34
35 var $rules = "Refuser les photos copyrightées, de mineurs, ou ayant
36 un caractère pornographique, violent, etc... Si une photo est mal
37 cadrée (20% de photo et 80% de blanc par exemple), si c'est un
38 camarade antique, on lui arrange sinon on lui
39 refuse en lui expliquant gentiment le problème. Idem si les dimensions de
40 la photo sont archi trop grandes ou archi trop petites.";
41
42 // }}}
43 // {{{ constructor
44
45 function PhotoReq($_uid, $_data, $_stamp=0)
46 {
47 $this->Validate($_uid, true, 'photo', $_stamp);
48 }
49
50 // }}}
51 // {{{ function _get_image()
52
53 function _get_image($_data)
54 {
55 global $page;
56
57 VarStream::init();
58
59 // calcul de la taille de l'image
60 $GLOBALS['photoreq'] = $_data;
61 $image_infos = getimagesize('var://photoreq');
62 unset ($GLOBALS['photoreq']);
63
64 if (empty($image_infos)) {
65 $page->trig("Image invalide");
66 return false;
67 }
68 list($this->x, $this->y, $this->mimetype) = $image_infos;
69
70 switch ($this->mimetype) {
71 case 1: $this->mimetype = "gif"; break;
72 case 2: $this->mimetype = "jpeg"; break;
73 case 3: $this->mimetype = "png"; break;
74 default:
75 $page->trig("Type d'image invalide");
76 return false;
77 }
78
79 if (strlen($_data) > SIZE_MAX) {
80 $img = imagecreatefromstring($_data);
81 if (!$img) {
82 $page->trig("image trop grande et impossible à retailler automatiquement");
83 return false;
84 }
85
86 $nx = $x = imagesx($img);
87 $ny = $y = imagesy($img);
88
89 if ($nx > 240) { $ny = intval($ny*240/$nx); $nx = 240; }
90 if ($ny > 300) { $ny = intval($nx*300/$nx); $ny = 300; }
91 if ($nx < 160) { $ny = intval($ny*160/$nx); $nx = 160; }
92
93 $comp = '90';
94 $file = tempnam('/tmp', 'photo');
95
96 while (strlen($_data) > SIZE_MAX) {
97 $img2 = imagecreatetruecolor($nx, $ny);
98 imagecopyresampled($img2, $img, 0, 0, 0, 0, $nx, $ny, $x, $y);
99 imagejpeg($img2, $file, $comp);
100 $_data = file_get_contents($file);
101 $this->mimetype = 'jpeg';
102
103 $comp --;
104 }
105
106 unlink($file);
107 }
108 $this->data = $_data;
109 return true;
110 }
111
112 // }}}
113 // {{{ function get_request()
114
115 function get_request($uid)
116 {
117 return parent::get_request($uid,'photo');
118 }
119
120 // }}}
121 // {{{ function formu()
122
123 function formu()
124 { return 'include/form.valid.photos.tpl'; }
125
126 // }}}
127 // {{{ function editor()
128
129 function editor()
130 {
131 return 'include/form.valid.edit-photo.tpl';
132 }
133
134 // }}}
135 // {{{ function handle_editor()
136
137 function handle_editor()
138 {
139 if (isset($_FILES['userfile']['tmp_name'])) {
140 $file = $_FILES['userfile']['tmp_name'];
141 if ($data = file_get_contents($file)) {
142 if ($this->_get_image($data)) {
143 return true;
144 }
145 } else {
146 $page->trig('Fichier inexistant ou vide');
147 }
148 }
149 return false;
150 }
151
152 // }}}
153 // {{{ function _mail_subj
154
155 function _mail_subj()
156 {
157 return "[Polytechnique.org/PHOTO] Changement de photo";
158 }
159
160 // }}}
161 // {{{ function _mail_body
162
163 function _mail_body($isok)
164 {
165 if ($isok) {
166 return "Le changement de photo que tu as demandé vient d'être effectué.";
167 } else {
168 return "La demande de changement de photo que tu avais faite a été refusée.";
169 }
170 }
171
172 // }}}
173 // {{{ function commit()
174
175 function commit()
176 {
177 XDB::execute('REPLACE INTO photo (uid, attachmime, attach, x, y)
178 VALUES ({?},{?},{?},{?},{?})',
179 $this->uid, $this->mimetype, $this->data, $this->x, $this->y);
180 require_once('notifs.inc.php');
181 register_watch_op($this->uid,WATCH_FICHE);
182 return true;
183 }
184
185 // }}}
186}
187
188// }}}
189
190// vim:set et sw=4 sts=4 sws=4 foldmethod=marker:
191?>