2 /***************************************************************************
3 * Copyright (C) 2003-2008 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 interface ProfileSetting
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.
28 * If value is null, the default value should be returned.
29 * TODO: check this does not conflict with some possible values.
31 * Whatever happen, this function must always returns the function to
32 * show on the page to the user.
34 public function value(ProfilePage
&$page, $field, $value, &$success);
36 /** Save the new value for the given field.
38 public function save(ProfilePage
&$page, $field, $new_value);
41 abstract class ProfileNoSave
implements ProfileSetting
43 public function save(ProfilePage
&$page, $field, $new_value) { }
46 class ProfileWeb
extends ProfileNoSave
48 public function value(ProfilePage
&$page, $field, $value, &$success)
50 if (is_null($value)) {
51 return isset($page->values
[$field]) ?
$page->values
[$field] : S
::v($field);
53 $value = trim($value);
54 $success = empty($value) ||
preg_match("{^(https?|ftp)://[a-zA-Z0-9._%#+/?=&~-]+$}i", $value);
57 $page->trigError('URL Incorrecte : une url doit commencer par http:// ou https:// ou ftp://'
58 . ' et ne pas contenir de caractères interdits');
64 class ProfileEmail
extends ProfileNoSave
66 public function value(ProfilePage
&$page, $field, $value, &$success)
68 if (is_null($value)) {
69 return isset($page->values
[$field]) ?
$page->values
[$field] : S
::v($field);
71 $value = trim($value);
72 require_once 'xorg.misc.inc.php';
73 $success = empty($value) ||
isvalid_email($value);
76 $page->trigError('Adresse Email invalide');
82 class ProfileNumber
extends ProfileNoSave
84 public function value(ProfilePage
&$page, $field, $value, &$success)
86 if (is_null($value)) {
87 return isset($page->values
[$field]) ?
$page->values
[$field] : S
::v($field);
89 $value = trim($value);
90 $success = empty($value) ||
is_numeric($value);
93 $page->trigError('Numéro invalide');
100 class ProfileTel
extends ProfileNoSave
102 public function value(ProfilePage
&$page, $field, $value, &$success)
104 if (is_null($value)) {
105 return isset($page->values
[$field]) ?
$page->values
[$field] : S
::v($field);
107 $success = !preg_match('/[<>{}@&#~\/:;?,!§*_`\[\]|%$^=]/', $value, $matches);
110 $page->trigError('Le numéro de téléphone contient un caractère interdit : ' . pl_entities($matches[0][0]));
116 class ProfilePub
extends ProfileNoSave
118 public function value(ProfilePage
&$page, $field, $value, &$success)
121 if (is_null($value)) {
122 return isset($page->values
[$field]) ?
$page->values
[$field] : S
::v($field);
124 if (is_null($value) ||
!$value) {
126 } else if ($value == 'on') { // Checkbox
133 class ProfileBool
extends ProfileNoSave
135 public function value(ProfilePage
&$page, $field, $value, &$success)
138 if (is_null($value)) {
139 $value = @$page->values
[$field];
141 return $value ?
"1" : "";
145 class ProfileDate
extends ProfileNoSave
147 public function value(ProfilePage
&$page, $field, $value, &$success)
150 if (is_null($value)) {
151 $value = preg_replace('/(\d{4})-(\d{2})-(\d{2})/', '\3/\2/\1', @$page->values
[$field]);
153 $success = preg_match('@(\d{2})/(\d{2})/(\d{4})@', $value, $matches);
156 $page->trigError("Les dates doivent être au format jj/mm/aaaa");
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);
164 $page->trigError("La date n'a pas une valeur valide");
172 abstract class ProfileGeoloc
implements ProfileSetting
174 protected function geolocAddress(array &$address, &$success)
176 require_once 'geoloc.inc.php';
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);
196 } else if (@$address['changed'] ||
!@$address['checked']) {
198 $address = array_merge($address, cut_address(@$address['text']));
199 $address['checked'] = false
;
200 $address['geoloc'] = $geotxt;
201 $address['geoloc_cityid'] = $new['cityid'];
203 $address = array_merge($address, cut_address(@$address['text']));
204 $address['checked'] = true
;
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);
214 abstract class ProfilePage
implements PlWizardPage
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
223 public $orig = array();
224 public $values = array();
226 public function __construct(PlWizard
&$wiz)
228 $this->wizard
=& $wiz;
231 protected function _fetchData()
235 protected function fetchData()
237 if (count($this->orig
) > 0) {
238 $this->values
= $this->orig
;
243 foreach ($this->settings
as $field=>&$setting) {
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);
250 $this->errors
[$field] = false
;
252 $this->orig
= $this->values
;
255 protected function _saveData()
259 protected function saveData()
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]);
266 if ($this->changed
[$field] && @$this->watched
[$field]) {
267 register_profile_update(S
::i('uid'), $field);
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
);
279 $log =& $_SESSION['log'];
280 $log->log('profil', $platal->pl_self(1));
283 protected function checkChanges()
285 $newvalues = $this->values
;
286 $this->values
= array();
288 $this->values
= $newvalues;
290 foreach ($this->settings
as $field=>&$setting) {
291 if ($this->orig
[$field] != $this->values
[$field]) {
292 $this->changed
[$field] = true
;
295 $this->changed
[$field] = false
;
301 protected function markChange()
305 public function template()
307 return 'profile/base.tpl';
310 protected function _prepare(PlatalPage
&$page, $id)
314 public function prepare(PlatalPage
&$page, $id)
316 if (count($this->values
) == 0) {
319 foreach ($this->values
as $field=>&$value) {
320 $page->assign($field, $value);
322 $this->_prepare($page, $id);
323 $page->assign('profile_page', $this->pg_template
);
324 $page->assign('errors', $this->errors
);
327 public function process()
329 $global_success = true
;
331 foreach ($this->settings
as $field=>&$setting) {
333 if (!is_null($setting)) {
334 $this->values
[$field] = $setting->value($this, $field, Post
::v($field, ''), $success);
337 $this->values
[$field] = Post
::v($field, '');
339 $this->errors
[$field] = !$success;
340 $global_success = $global_success && $success;
342 if ($global_success) {
343 if ($this->checkChanges()) {
347 return Post
::has('next_page') ? PlWizard
::NEXT_PAGE
: PlWizard
::CURRENT_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
;
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';
364 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: