Add missing 'check_redirect'
[platal.git] / modules / profile / page.inc.php
CommitLineData
f118f685
FB
1<?php
2/***************************************************************************
3 * Copyright (C) 2003-2007 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
fd38b30e
FB
22interface 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
41abstract class ProfileNoSave implements ProfileSetting
42{
43 public function save(ProfilePage &$page, $field, $new_value) { }
44}
45
fd38b30e
FB
46class 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 }
37d44b3b
FB
53 $value = trim($value);
54 $success = empty($value) || preg_match("{^(https?|ftp)://[a-zA-Z0-9._%#+/?=&~-]+$}i", $value);
fd38b30e
FB
55 if (!$success) {
56 global $page;
57 $page->trig('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
37d44b3b
FB
64class 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->trig('Adresse Email invalide');
77 }
78 return $value;
79 }
80}
81
82
fd38b30e
FB
83class ProfileTel extends ProfileNoSave
84{
85 public function value(ProfilePage &$page, $field, $value, &$success)
86 {
87 if (is_null($value)) {
88 return isset($page->values[$field]) ? $page->values[$field] : S::v($field);
89 }
a619333a 90 $success = !preg_match('/[<>{}@&#~\/:;?,!§*_`\[\]|%$^=]/', $value, $matches);
fd38b30e
FB
91 if (!$success) {
92 global $page;
a619333a 93 $page->trig('Le numéro de téléphone contient un caractère interdit : ' . pl_entities($matches[0][0]));
fd38b30e
FB
94 }
95 return $value;
96 }
97}
98
93553cea
FB
99class ProfilePub extends ProfileNoSave
100{
101 public function value(ProfilePage &$page, $field, $value, &$success)
102 {
103 $success = true;
104 if (is_null($value)) {
105 return isset($page->values[$field]) ? $page->values[$field] : S::v($field);
106 }
107 if (is_null($value) || !$value) {
108 $value = 'private';
109 } else if ($value == 'on') { // Checkbox
110 $value = 'public';
111 }
112 return $value;
113 }
114}
115
576777d7
FB
116class ProfileBool extends ProfileNoSave
117{
118 public function value(ProfilePage &$page, $field, $value, &$success)
119 {
120 $success = true;
121 if (is_null($value)) {
c24a06aa 122 $value = @$page->values[$field];
576777d7 123 }
ee12da4e 124 return $value ? "1" : "";
576777d7
FB
125 }
126}
127
7bff4cb0
FB
128class ProfileDate extends ProfileNoSave
129{
130 public function value(ProfilePage &$page, $field, $value, &$success)
131 {
132 $success = true;
133 if (is_null($value)) {
134 $value = preg_replace('/(\d{4})-(\d{2})-(\d{2})/', '\3/\2/\1', @$page->values[$field]);
135 } else {
136 $success = preg_match('@(\d{2})/(\d{2})/(\d{4})@', $value, $matches);
137 if (!$success) {
138 global $page;
139 $page->trig("Les dates doivent être au format jj/mm/aaaa");
140 } else {
141 $day = (int)$matches[1];
142 $month = (int)$matches[2];
143 $year = (int)$matches[3];
144 $success = ($day > 0 && $day <= 31) && ($month > 0 && $month <= 12) && ($year > 1900 && $year <= 2020);
145 if (!$success) {
146 global $page;
147 $page->trig("La date n'a pas une valeur valide");
148 }
149 }
150 }
151 return $value;
152 }
153}
154
37d44b3b
FB
155abstract class ProfileGeoloc implements ProfileSetting
156{
157 protected function geolocAddress(array &$address, &$success)
158 {
159 require_once 'geoloc.inc.php';
160 $success = true;
161 unset($address['geoloc']);
162 unset($address['geoloc_cityid']);
163 if (@$address['parsevalid']
164 || (@$address['text'] && @$address['changed'])
165 || (@$address['text'] && !@$address['cityid'])) {
166 $address = array_merge($address, empty_address());
167 $new = get_address_infos(@$address['text']);
1879c7b2
FB
168 if (compare_addresses_text(@$address['text'], $geotxt = get_address_text($new))
169 || (@$address['parsevalid'] && @$address['cityid'])) {
37d44b3b 170 $address = array_merge($address, $new);
f93fb300 171 $address['checked'] = true;
1879c7b2
FB
172 } else if (@$address['parsevalid']) {
173 $address = array_merge($address, cut_address(@$address['text']));
f93fb300
FB
174 $address['checked'] = true;
175 $mailer = new PlMailer('geoloc/mail_geoloc.tpl');
176 $mailer->assign('text', get_address_text($address));
177 $mailer->assign('geoloc', $geotxt);
178 $mailer->send();
179 } else if (@$address['changed'] || !@$address['checked']) {
37d44b3b
FB
180 $success = false;
181 $address = array_merge($address, cut_address(@$address['text']));
f93fb300 182 $address['checked'] = false;
37d44b3b
FB
183 $address['geoloc'] = $geotxt;
184 $address['geoloc_cityid'] = $new['cityid'];
f93fb300
FB
185 } else {
186 $address = array_merge($address, cut_address(@$address['text']));
187 $address['checked'] = true;
37d44b3b
FB
188 }
189 }
de0485fd
FB
190 $address['precise_lat'] = rtrim($address['precise_lat'], '.0');
191 $address['precise_lon'] = rtrim($address['precise_lon'], '.0');
37d44b3b
FB
192 $address['text'] = get_address_text($address);
193 }
194}
195
196
fd38b30e 197abstract class ProfilePage implements PlWizardPage
f118f685
FB
198{
199 protected $wizard;
fd38b30e
FB
200 protected $pg_template;
201 protected $settings = array(); // A set ProfileSetting objects
93553cea 202 protected $errors = array(); // A set of boolean with the value check errors
576777d7 203 protected $changed = array(); // A set of boolean indicating wether the value has been changed
fd38b30e 204
93553cea 205 public $orig = array();
fd38b30e 206 public $values = array();
f118f685
FB
207
208 public function __construct(PlWizard &$wiz)
209 {
210 $this->wizard =& $wiz;
211 }
212
7c2e0f0d
FB
213 protected function _fetchData()
214 {
215 }
216
fd38b30e
FB
217 protected function fetchData()
218 {
93553cea
FB
219 if (count($this->orig) > 0) {
220 $this->values = $this->orig;
221 return;
222 }
7c2e0f0d
FB
223
224 $this->_fetchData();
93553cea
FB
225 foreach ($this->settings as $field=>&$setting) {
226 $success = false;
227 if (!is_null($setting)) {
228 $this->values[$field] = $setting->value($this, $field, null, $success);
229 } else if (!isset($this->values[$field])) {
230 $this->values[$field] = S::v($field);
231 }
232 $this->errors[$field] = false;
233 }
234 $this->orig = $this->values;
fd38b30e
FB
235 }
236
7c2e0f0d
FB
237 protected function _saveData()
238 {
239 }
240
fd38b30e
FB
241 protected function saveData()
242 {
93553cea 243 foreach ($this->settings as $field=>&$setting) {
576777d7 244 if (!is_null($setting) && $this->changed[$field]) {
93553cea
FB
245 $setting->save($this, $field, $this->values[$field]);
246 }
247 }
7c2e0f0d 248 $this->_saveData();
576777d7
FB
249
250 // Update the last modification date
251 XDB::execute('REPLACE INTO user_changes
252 SET user_id = {?}', S::v('uid'));
3ebe4a0a
FB
253 if (!S::has('suid')) {
254 require_once 'notifs.inc.php';
255 register_watch_op(S::i('uid'), WATCH_FICHE);
256 }
576777d7
FB
257 global $platal;
258 $log =& $_SESSION['log'];
259 $log->log('profil', $platal->pl_self(1));
93553cea
FB
260 }
261
262 protected function checkChanges()
263 {
264 $newvalues = $this->values;
265 $this->values = array();
266 $this->fetchData();
267 $this->values = $newvalues;
576777d7 268 $changes = false;
93553cea
FB
269 foreach ($this->settings as $field=>&$setting) {
270 if ($this->orig[$field] != $this->values[$field]) {
576777d7
FB
271 $this->changed[$field] = true;
272 $changes = true;
273 } else {
274 $this->changed[$field] = false;
93553cea
FB
275 }
276 }
576777d7 277 return $changes;
93553cea
FB
278 }
279
280 protected function markChange()
281 {
fd38b30e
FB
282 }
283
f118f685
FB
284 public function template()
285 {
fd38b30e 286 return 'profile/base.tpl';
f118f685
FB
287 }
288
7c2e0f0d
FB
289 protected function _prepare(PlatalPage &$page, $id)
290 {
291 }
292
ddb64990 293 public function prepare(PlatalPage &$page, $id)
f118f685 294 {
fd38b30e
FB
295 if (count($this->values) == 0) {
296 $this->fetchData();
fd38b30e
FB
297 }
298 foreach ($this->values as $field=>&$value) {
299 $page->assign($field, $value);
300 }
7c2e0f0d 301 $this->_prepare($page, $id);
fd38b30e 302 $page->assign('profile_page', $this->pg_template);
93553cea 303 $page->assign('errors', $this->errors);
f118f685
FB
304 }
305
306 public function process()
307 {
fd38b30e
FB
308 $global_success = true;
309 $this->fetchData();
310 foreach ($this->settings as $field=>&$setting) {
311 $success = false;
93553cea 312 if (!is_null($setting)) {
85cc366b 313 $this->values[$field] = $setting->value($this, $field, Post::v($field, ''), $success);
93553cea
FB
314 } else {
315 $success = true;
85cc366b 316 $this->values[$field] = Post::v($field, '');
93553cea
FB
317 }
318 $this->errors[$field] = !$success;
fd38b30e
FB
319 $global_success = $global_success && $success;
320 }
321 if ($global_success) {
93553cea
FB
322 if ($this->checkChanges()) {
323 $this->saveData();
324 $this->markChange();
fd38b30e 325 }
93553cea 326 return Post::has('next_page') ? PlWizard::NEXT_PAGE : PlWizard::CURRENT_PAGE;
fd38b30e
FB
327 }
328 global $page;
93553cea 329 $page->trig("Certains champs n'ont pas pu être validés, merci de corriger les informations "
fd38b30e 330 . "de ton profil et de revalider ta demande");
f118f685
FB
331 return PlWizard::CURRENT_PAGE;
332 }
333}
334
fd38b30e 335require_once dirname(__FILE__) . '/general.inc.php';
0b14f91d 336require_once dirname(__FILE__) . '/addresses.inc.php';
92412b28 337require_once dirname(__FILE__) . '/groups.inc.php';
a7c28fff 338require_once dirname(__FILE__) . '/decos.inc.php';
3950bc21 339require_once dirname(__FILE__) . '/jobs.inc.php';
f25e1a56 340require_once dirname(__FILE__) . '/skills.inc.php';
6457b5e4 341require_once dirname(__FILE__) . '/mentor.inc.php';
f118f685
FB
342
343// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
344?>