| 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 | |
| 83 | class 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 | } |
| 90 | $success = !preg_match('/[<>{}@&#~\/:;?,!§*_`\[\]|%$^=]/', $value, $matches); |
| 91 | if (!$success) { |
| 92 | global $page; |
| 93 | $page->trigError('Le numéro de téléphone contient un caractère interdit : ' . pl_entities($matches[0][0])); |
| 94 | } |
| 95 | return $value; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | class 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 | |
| 116 | class ProfileBool extends ProfileNoSave |
| 117 | { |
| 118 | public function value(ProfilePage &$page, $field, $value, &$success) |
| 119 | { |
| 120 | $success = true; |
| 121 | if (is_null($value)) { |
| 122 | $value = @$page->values[$field]; |
| 123 | } |
| 124 | return $value ? "1" : ""; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | class 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->trigError("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->trigError("La date n'a pas une valeur valide"); |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | return $value; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | abstract 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']); |
| 168 | if (compare_addresses_text(@$address['text'], $geotxt = get_address_text($new)) |
| 169 | || (@$address['parsevalid'] && @$address['cityid'])) { |
| 170 | $address = array_merge($address, $new); |
| 171 | $address['checked'] = true; |
| 172 | } else if (@$address['parsevalid']) { |
| 173 | $address = array_merge($address, cut_address(@$address['text'])); |
| 174 | $address['checked'] = true; |
| 175 | $mailer = new PlMailer('geoloc/geoloc.mail.tpl'); |
| 176 | $mailer->assign('text', get_address_text($address)); |
| 177 | $mailer->assign('geoloc', $geotxt); |
| 178 | $mailer->send(); |
| 179 | } else if (@$address['changed'] || !@$address['checked']) { |
| 180 | $success = false; |
| 181 | $address = array_merge($address, cut_address(@$address['text'])); |
| 182 | $address['checked'] = false; |
| 183 | $address['geoloc'] = $geotxt; |
| 184 | $address['geoloc_cityid'] = $new['cityid']; |
| 185 | } else { |
| 186 | $address = array_merge($address, cut_address(@$address['text'])); |
| 187 | $address['checked'] = true; |
| 188 | } |
| 189 | } |
| 190 | $address['precise_lat'] = rtrim($address['precise_lat'], '.0'); |
| 191 | $address['precise_lon'] = rtrim($address['precise_lon'], '.0'); |
| 192 | $address['text'] = get_address_text($address); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | |
| 197 | abstract class ProfilePage implements PlWizardPage |
| 198 | { |
| 199 | protected $wizard; |
| 200 | protected $pg_template; |
| 201 | protected $settings = array(); // A set ProfileSetting objects |
| 202 | protected $errors = array(); // A set of boolean with the value check errors |
| 203 | protected $changed = array(); // A set of boolean indicating wether the value has been changed |
| 204 | protected $watched = array(); // A set of boolean indicating the fields that are watched |
| 205 | |
| 206 | public $orig = array(); |
| 207 | public $values = array(); |
| 208 | |
| 209 | public function __construct(PlWizard &$wiz) |
| 210 | { |
| 211 | $this->wizard =& $wiz; |
| 212 | } |
| 213 | |
| 214 | protected function _fetchData() |
| 215 | { |
| 216 | } |
| 217 | |
| 218 | protected function fetchData() |
| 219 | { |
| 220 | if (count($this->orig) > 0) { |
| 221 | $this->values = $this->orig; |
| 222 | return; |
| 223 | } |
| 224 | |
| 225 | $this->_fetchData(); |
| 226 | foreach ($this->settings as $field=>&$setting) { |
| 227 | $success = false; |
| 228 | if (!is_null($setting)) { |
| 229 | $this->values[$field] = $setting->value($this, $field, null, $success); |
| 230 | } else if (!isset($this->values[$field])) { |
| 231 | $this->values[$field] = S::v($field); |
| 232 | } |
| 233 | $this->errors[$field] = false; |
| 234 | } |
| 235 | $this->orig = $this->values; |
| 236 | } |
| 237 | |
| 238 | protected function _saveData() |
| 239 | { |
| 240 | } |
| 241 | |
| 242 | protected function saveData() |
| 243 | { |
| 244 | require_once 'notifs.inc.php'; |
| 245 | foreach ($this->settings as $field=>&$setting) { |
| 246 | if (!is_null($setting) && $this->changed[$field]) { |
| 247 | $setting->save($this, $field, $this->values[$field]); |
| 248 | } |
| 249 | if ($this->changed[$field] && @$this->watched[$field]) { |
| 250 | register_profile_update(S::i('uid'), $field); |
| 251 | } |
| 252 | } |
| 253 | $this->_saveData(); |
| 254 | |
| 255 | // Update the last modification date |
| 256 | XDB::execute('REPLACE INTO user_changes |
| 257 | SET user_id = {?}', S::v('uid')); |
| 258 | if (!S::has('suid')) { |
| 259 | register_watch_op(S::i('uid'), WATCH_FICHE); |
| 260 | } |
| 261 | global $platal; |
| 262 | $log =& $_SESSION['log']; |
| 263 | $log->log('profil', $platal->pl_self(1)); |
| 264 | } |
| 265 | |
| 266 | protected function checkChanges() |
| 267 | { |
| 268 | $newvalues = $this->values; |
| 269 | $this->values = array(); |
| 270 | $this->fetchData(); |
| 271 | $this->values = $newvalues; |
| 272 | $changes = false; |
| 273 | foreach ($this->settings as $field=>&$setting) { |
| 274 | if ($this->orig[$field] != $this->values[$field]) { |
| 275 | $this->changed[$field] = true; |
| 276 | $changes = true; |
| 277 | } else { |
| 278 | $this->changed[$field] = false; |
| 279 | } |
| 280 | } |
| 281 | return $changes; |
| 282 | } |
| 283 | |
| 284 | protected function markChange() |
| 285 | { |
| 286 | } |
| 287 | |
| 288 | public function template() |
| 289 | { |
| 290 | return 'profile/base.tpl'; |
| 291 | } |
| 292 | |
| 293 | protected function _prepare(PlatalPage &$page, $id) |
| 294 | { |
| 295 | } |
| 296 | |
| 297 | public function prepare(PlatalPage &$page, $id) |
| 298 | { |
| 299 | if (count($this->values) == 0) { |
| 300 | $this->fetchData(); |
| 301 | } |
| 302 | foreach ($this->values as $field=>&$value) { |
| 303 | $page->assign($field, $value); |
| 304 | } |
| 305 | $this->_prepare($page, $id); |
| 306 | $page->assign('profile_page', $this->pg_template); |
| 307 | $page->assign('errors', $this->errors); |
| 308 | } |
| 309 | |
| 310 | public function process() |
| 311 | { |
| 312 | $global_success = true; |
| 313 | $this->fetchData(); |
| 314 | foreach ($this->settings as $field=>&$setting) { |
| 315 | $success = false; |
| 316 | if (!is_null($setting)) { |
| 317 | $this->values[$field] = $setting->value($this, $field, Post::v($field, ''), $success); |
| 318 | } else { |
| 319 | $success = true; |
| 320 | $this->values[$field] = Post::v($field, ''); |
| 321 | } |
| 322 | $this->errors[$field] = !$success; |
| 323 | $global_success = $global_success && $success; |
| 324 | } |
| 325 | if ($global_success) { |
| 326 | if ($this->checkChanges()) { |
| 327 | $this->saveData(); |
| 328 | $this->markChange(); |
| 329 | } |
| 330 | return Post::has('next_page') ? PlWizard::NEXT_PAGE : PlWizard::CURRENT_PAGE; |
| 331 | } |
| 332 | global $page; |
| 333 | $page->trigError("Certains champs n'ont pas pu être validés, merci de corriger les informations " |
| 334 | . "de ton profil et de revalider ta demande"); |
| 335 | return PlWizard::CURRENT_PAGE; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | require_once dirname(__FILE__) . '/general.inc.php'; |
| 340 | require_once dirname(__FILE__) . '/addresses.inc.php'; |
| 341 | require_once dirname(__FILE__) . '/groups.inc.php'; |
| 342 | require_once dirname(__FILE__) . '/decos.inc.php'; |
| 343 | require_once dirname(__FILE__) . '/jobs.inc.php'; |
| 344 | require_once dirname(__FILE__) . '/skills.inc.php'; |
| 345 | require_once dirname(__FILE__) . '/mentor.inc.php'; |
| 346 | |
| 347 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
| 348 | ?> |