networking: adds a filter number for ICQ, adds a gpg icon
[platal.git] / modules / profile / page.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2008 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 interface ProfileSetting
23 {
24 /** Get a field and a value, check that the given value is
25 * valid, if not, return a corrected value. If no valid value can be
26 * computed from the input data, the success flag is set to false.
27 *
28 * If value is null, the default value should be returned.
29 * TODO: check this does not conflict with some possible values.
30 *
31 * Whatever happen, this function must always returns the function to
32 * show on the page to the user.
33 */
34 public function value(ProfilePage &$page, $field, $value, &$success);
35
36 /** Save the new value for the given field.
37 */
38 public function save(ProfilePage &$page, $field, $new_value);
39 }
40
41 abstract class ProfileNoSave implements ProfileSetting
42 {
43 public function save(ProfilePage &$page, $field, $new_value) { }
44 }
45
46 class ProfileWeb extends ProfileNoSave
47 {
48 public function value(ProfilePage &$page, $field, $value, &$success)
49 {
50 if (is_null($value)) {
51 return isset($page->values[$field]) ? $page->values[$field] : S::v($field);
52 }
53 $value = trim($value);
54 $success = empty($value) || preg_match("{^(https?|ftp)://[a-zA-Z0-9._%#+/?=&~-]+$}i", $value);
55 if (!$success) {
56 global $page;
57 $page->trigError('URL Incorrecte : une url doit commencer par http:// ou https:// ou ftp://'
58 . ' et ne pas contenir de caractères interdits');
59 }
60 return $value;
61 }
62 }
63
64 class ProfileEmail extends ProfileNoSave
65 {
66 public function value(ProfilePage &$page, $field, $value, &$success)
67 {
68 if (is_null($value)) {
69 return isset($page->values[$field]) ? $page->values[$field] : S::v($field);
70 }
71 $value = trim($value);
72 require_once 'xorg.misc.inc.php';
73 $success = empty($value) || isvalid_email($value);
74 if (!$success) {
75 global $page;
76 $page->trigError('Adresse Email invalide');
77 }
78 return $value;
79 }
80 }
81
82 class ProfileNumber extends ProfileNoSave
83 {
84 public function value(ProfilePage &$page, $field, $value, &$success)
85 {
86 if (is_null($value)) {
87 return isset($page->values[$field]) ? $page->values[$field] : S::v($field);
88 }
89 $value = trim($value);
90 $success = empty($value) || is_numeric($value);
91 if (!$success) {
92 global $page;
93 $page->trigError('Numéro invalide');
94 }
95 return $value;
96 }
97 }
98
99
100 class ProfileTel extends ProfileNoSave
101 {
102 public function value(ProfilePage &$page, $field, $value, &$success)
103 {
104 if (is_null($value)) {
105 return isset($page->values[$field]) ? $page->values[$field] : S::v($field);
106 }
107 $success = !preg_match('/[<>{}@&#~\/:;?,!§*_`\[\]|%$^=]/', $value, $matches);
108 if (!$success) {
109 global $page;
110 $page->trigError('Le numéro de téléphone contient un caractère interdit : ' . pl_entities($matches[0][0]));
111 }
112 return $value;
113 }
114 }
115
116 class ProfilePub extends ProfileNoSave
117 {
118 public function value(ProfilePage &$page, $field, $value, &$success)
119 {
120 $success = true;
121 if (is_null($value)) {
122 return isset($page->values[$field]) ? $page->values[$field] : S::v($field);
123 }
124 if (is_null($value) || !$value) {
125 $value = 'private';
126 } else if ($value == 'on') { // Checkbox
127 $value = 'public';
128 }
129 return $value;
130 }
131 }
132
133 class ProfileBool extends ProfileNoSave
134 {
135 public function value(ProfilePage &$page, $field, $value, &$success)
136 {
137 $success = true;
138 if (is_null($value)) {
139 $value = @$page->values[$field];
140 }
141 return $value ? "1" : "";
142 }
143 }
144
145 class ProfileDate extends ProfileNoSave
146 {
147 public function value(ProfilePage &$page, $field, $value, &$success)
148 {
149 $success = true;
150 if (is_null($value)) {
151 $value = preg_replace('/(\d{4})-(\d{2})-(\d{2})/', '\3/\2/\1', @$page->values[$field]);
152 } else {
153 $success = preg_match('@(\d{2})/(\d{2})/(\d{4})@', $value, $matches);
154 if (!$success) {
155 global $page;
156 $page->trigError("Les dates doivent être au format jj/mm/aaaa");
157 } else {
158 $day = (int)$matches[1];
159 $month = (int)$matches[2];
160 $year = (int)$matches[3];
161 $success = ($day > 0 && $day <= 31) && ($month > 0 && $month <= 12) && ($year > 1900 && $year <= 2020);
162 if (!$success) {
163 global $page;
164 $page->trigError("La date n'a pas une valeur valide");
165 }
166 }
167 }
168 return $value;
169 }
170 }
171
172 abstract class ProfileGeoloc implements ProfileSetting
173 {
174 protected function geolocAddress(array &$address, &$success)
175 {
176 require_once 'geoloc.inc.php';
177 $success = true;
178 unset($address['geoloc']);
179 unset($address['geoloc_cityid']);
180 if (@$address['parsevalid']
181 || (@$address['text'] && @$address['changed'])
182 || (@$address['text'] && !@$address['cityid'])) {
183 $address = array_merge($address, empty_address());
184 $new = get_address_infos(@$address['text']);
185 if (compare_addresses_text(@$address['text'], $geotxt = get_address_text($new))
186 || (@$address['parsevalid'] && @$address['cityid'])) {
187 $address = array_merge($address, $new);
188 $address['checked'] = true;
189 } else if (@$address['parsevalid']) {
190 $address = array_merge($address, cut_address(@$address['text']));
191 $address['checked'] = true;
192 $mailer = new PlMailer('geoloc/geoloc.mail.tpl');
193 $mailer->assign('text', get_address_text($address));
194 $mailer->assign('geoloc', $geotxt);
195 $mailer->send();
196 } else if (@$address['changed'] || !@$address['checked']) {
197 $success = false;
198 $address = array_merge($address, cut_address(@$address['text']));
199 $address['checked'] = false;
200 $address['geoloc'] = $geotxt;
201 $address['geoloc_cityid'] = $new['cityid'];
202 } else {
203 $address = array_merge($address, cut_address(@$address['text']));
204 $address['checked'] = true;
205 }
206 }
207 $address['precise_lat'] = rtrim($address['precise_lat'], '.0');
208 $address['precise_lon'] = rtrim($address['precise_lon'], '.0');
209 $address['text'] = get_address_text($address);
210 }
211 }
212
213
214 abstract class ProfilePage implements PlWizardPage
215 {
216 protected $wizard;
217 protected $pg_template;
218 protected $settings = array(); // A set ProfileSetting objects
219 protected $errors = array(); // A set of boolean with the value check errors
220 protected $changed = array(); // A set of boolean indicating wether the value has been changed
221 protected $watched = array(); // A set of boolean indicating the fields that are watched
222
223 public $orig = array();
224 public $values = array();
225
226 public function __construct(PlWizard &$wiz)
227 {
228 $this->wizard =& $wiz;
229 }
230
231 protected function _fetchData()
232 {
233 }
234
235 protected function fetchData()
236 {
237 if (count($this->orig) > 0) {
238 $this->values = $this->orig;
239 return;
240 }
241
242 $this->_fetchData();
243 foreach ($this->settings as $field=>&$setting) {
244 $success = false;
245 if (!is_null($setting)) {
246 $this->values[$field] = $setting->value($this, $field, null, $success);
247 } else if (!isset($this->values[$field])) {
248 $this->values[$field] = S::v($field);
249 }
250 $this->errors[$field] = false;
251 }
252 $this->orig = $this->values;
253 }
254
255 protected function _saveData()
256 {
257 }
258
259 protected function saveData()
260 {
261 require_once 'notifs.inc.php';
262 foreach ($this->settings as $field=>&$setting) {
263 if (!is_null($setting) && $this->changed[$field]) {
264 $setting->save($this, $field, $this->values[$field]);
265 }
266 if ($this->changed[$field] && @$this->watched[$field]) {
267 register_profile_update(S::i('uid'), $field);
268 }
269 }
270 $this->_saveData();
271
272 // Update the last modification date
273 XDB::execute('REPLACE INTO user_changes
274 SET user_id = {?}', S::v('uid'));
275 if (!S::has('suid')) {
276 register_watch_op(S::i('uid'), WATCH_FICHE);
277 }
278 global $platal;
279 $log =& $_SESSION['log'];
280 $log->log('profil', $platal->pl_self(1));
281 }
282
283 protected function checkChanges()
284 {
285 $newvalues = $this->values;
286 $this->values = array();
287 $this->fetchData();
288 $this->values = $newvalues;
289 $changes = false;
290 foreach ($this->settings as $field=>&$setting) {
291 if ($this->orig[$field] != $this->values[$field]) {
292 $this->changed[$field] = true;
293 $changes = true;
294 } else {
295 $this->changed[$field] = false;
296 }
297 }
298 return $changes;
299 }
300
301 protected function markChange()
302 {
303 }
304
305 public function template()
306 {
307 return 'profile/base.tpl';
308 }
309
310 protected function _prepare(PlatalPage &$page, $id)
311 {
312 }
313
314 public function prepare(PlatalPage &$page, $id)
315 {
316 if (count($this->values) == 0) {
317 $this->fetchData();
318 }
319 foreach ($this->values as $field=>&$value) {
320 $page->assign($field, $value);
321 }
322 $this->_prepare($page, $id);
323 $page->assign('profile_page', $this->pg_template);
324 $page->assign('errors', $this->errors);
325 }
326
327 public function process()
328 {
329 $global_success = true;
330 $this->fetchData();
331 foreach ($this->settings as $field=>&$setting) {
332 $success = false;
333 if (!is_null($setting)) {
334 $this->values[$field] = $setting->value($this, $field, Post::v($field, ''), $success);
335 } else {
336 $success = true;
337 $this->values[$field] = Post::v($field, '');
338 }
339 $this->errors[$field] = !$success;
340 $global_success = $global_success && $success;
341 }
342 if ($global_success) {
343 if ($this->checkChanges()) {
344 $this->saveData();
345 $this->markChange();
346 }
347 return Post::has('next_page') ? PlWizard::NEXT_PAGE : PlWizard::CURRENT_PAGE;
348 }
349 global $page;
350 $page->trigError("Certains champs n'ont pas pu être validés, merci de corriger les informations "
351 . "de ton profil et de revalider ta demande");
352 return PlWizard::CURRENT_PAGE;
353 }
354 }
355
356 require_once dirname(__FILE__) . '/general.inc.php';
357 require_once dirname(__FILE__) . '/addresses.inc.php';
358 require_once dirname(__FILE__) . '/groups.inc.php';
359 require_once dirname(__FILE__) . '/decos.inc.php';
360 require_once dirname(__FILE__) . '/jobs.inc.php';
361 require_once dirname(__FILE__) . '/skills.inc.php';
362 require_once dirname(__FILE__) . '/mentor.inc.php';
363
364 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
365 ?>