Add by-account monitoring
[platal.git] / include / validations / photos.inc.php
CommitLineData
0337d704 1<?php
2/***************************************************************************
5ddeb07c 3 * Copyright (C) 2003-2007 Polytechnique.org *
0337d704 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
63528107 27
0337d704 28 var $mimetype;
29 var $data;
30 var $x;
31 var $y;
32
33 var $unique = true;
eef2d586 34 var $valid = false;
0337d704 35
a7de4ef7 36 var $rules = "Refuser les photos copyrightées, de mineurs, ou ayant
37 un caractère pornographique, violent, etc... Si une photo est mal
38 cadrée (20% de photo et 80% de blanc par exemple), si c'est un
63528107 39 camarade antique, on lui arrange sinon on lui
a7de4ef7 40 refuse en lui expliquant gentiment le problème. Idem si les dimensions de
0337d704 41 la photo sont archi trop grandes ou archi trop petites.";
42
43 // }}}
44 // {{{ constructor
63528107 45
0337d704 46 function PhotoReq($_uid, $_data, $_stamp=0)
47 {
0337d704 48 $this->Validate($_uid, true, 'photo', $_stamp);
eef2d586 49 $this->valid = $this->_get_image($_data);
5af85e81 50 }
51
52 // }}}
53 // {{{ function _get_image()
54
55 function _get_image($_data)
56 {
57 global $page;
63528107 58
c83168b1 59 VarStream::init();
60
0337d704 61 // calcul de la taille de l'image
0337d704 62 $GLOBALS['photoreq'] = $_data;
63 $image_infos = getimagesize('var://photoreq');
64 unset ($GLOBALS['photoreq']);
65
66 if (empty($image_infos)) {
67 $page->trig("Image invalide");
5af85e81 68 return false;
0337d704 69 }
70 list($this->x, $this->y, $this->mimetype) = $image_infos;
71
72 switch ($this->mimetype) {
73 case 1: $this->mimetype = "gif"; break;
74 case 2: $this->mimetype = "jpeg"; break;
75 case 3: $this->mimetype = "png"; break;
76 default:
77 $page->trig("Type d'image invalide");
5af85e81 78 return false;
0337d704 79 }
80
81 if (strlen($_data) > SIZE_MAX) {
82 $img = imagecreatefromstring($_data);
83 if (!$img) {
a7de4ef7 84 $page->trig("image trop grande et impossible à retailler automatiquement");
5af85e81 85 return false;
0337d704 86 }
87
88 $nx = $x = imagesx($img);
89 $ny = $y = imagesy($img);
90
91 if ($nx > 240) { $ny = intval($ny*240/$nx); $nx = 240; }
92 if ($ny > 300) { $ny = intval($nx*300/$nx); $ny = 300; }
93 if ($nx < 160) { $ny = intval($ny*160/$nx); $nx = 160; }
94
95 $comp = '90';
96 $file = tempnam('/tmp', 'photo');
97
98 while (strlen($_data) > SIZE_MAX) {
99 $img2 = imagecreatetruecolor($nx, $ny);
100 imagecopyresampled($img2, $img, 0, 0, 0, 0, $nx, $ny, $x, $y);
101 imagejpeg($img2, $file, $comp);
102 $_data = file_get_contents($file);
103 $this->mimetype = 'jpeg';
104
105 $comp --;
106 }
107
108 unlink($file);
109 }
110 $this->data = $_data;
5af85e81 111 return true;
0337d704 112 }
63528107 113
0337d704 114 // }}}
eef2d586 115 // {{{ function isValid()
116
117 function isValid()
118 {
119 return $this->valid;
120 }
121
122 // }}}
0337d704 123 // {{{ function get_request()
124
125 function get_request($uid)
126 {
20d7932b 127 return parent::get_typed_request($uid,'photo');
0337d704 128 }
129
130 // }}}
131 // {{{ function formu()
132
133 function formu()
134 { return 'include/form.valid.photos.tpl'; }
135
136 // }}}
5af85e81 137 // {{{ function editor()
138
139 function editor()
140 {
141 return 'include/form.valid.edit-photo.tpl';
142 }
143
144 // }}}
145 // {{{ function handle_editor()
146
147 function handle_editor()
148 {
149 if (isset($_FILES['userfile']['tmp_name'])) {
150 $file = $_FILES['userfile']['tmp_name'];
151 if ($data = file_get_contents($file)) {
eef2d586 152 if ($this->valid = $this->_get_image($data)) {
5af85e81 153 return true;
154 }
155 } else {
156 $page->trig('Fichier inexistant ou vide');
157 }
158 }
159 return false;
160 }
161
162 // }}}
0337d704 163 // {{{ function _mail_subj
164
165 function _mail_subj()
166 {
167 return "[Polytechnique.org/PHOTO] Changement de photo";
168 }
169
170 // }}}
171 // {{{ function _mail_body
63528107 172
0337d704 173 function _mail_body($isok)
174 {
175 if ($isok) {
a7de4ef7 176 return "Le changement de photo que tu as demandé vient d'être effectué.";
0337d704 177 } else {
a7de4ef7 178 return "La demande de changement de photo que tu avais faite a été refusée.";
0337d704 179 }
180 }
181
182 // }}}
183 // {{{ function commit()
63528107 184
0337d704 185 function commit()
186 {
08cce2ff 187 XDB::execute('REPLACE INTO photo (uid, attachmime, attach, x, y)
0337d704 188 VALUES ({?},{?},{?},{?},{?})',
189 $this->uid, $this->mimetype, $this->data, $this->x, $this->y);
190 require_once('notifs.inc.php');
191 register_watch_op($this->uid,WATCH_FICHE);
192 return true;
193 }
194
195 // }}}
196}
197
198// }}}
199
a7de4ef7 200// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
0337d704 201?>