* @license PHP License * @package WB * @subpackage content */ /** * Load classes */ WBClass::load('WBUser'); /** * User Registration and password renewal * * * * @version 0.4.3 * @package WB * @subpackage content */ class WBUser_Register extends WBUser { /** * logger * @var WBLog */ protected $log; /** * form processor * @var WBFormProcessor */ protected $formProc; /** * Private constructor * * Load authenticated user from session */ public function __construct() { WBClass::load('WBLog'); } /** * register user * * create new user data * * @param array $data * @return string $id user's id */ public function register($data) { if (empty($data)) { return null; } $new = $this->getEditableData($data); $new['approved'] = 0; $new['enabled'] = 0; // check required fields $this->loadConfig(); foreach ($this->config['fields']['required'] as $r) { if (!isset($data[$r]) || empty($data[$r])) { return null; } // some required fields are not editable if (!isset($new[$r])) { $new[$r] = $data[$r]; } } // guess language from current locale settings if (empty($new['lang']) && class_exists('patI18n', false)) { $new['lang'] = patI18n::getLocale(patI18n::LOCALE_TYPE_COMPLETE); } $storage = $this->getStorageModule(); /** @var $storage WBUser_Storage */ $id = $storage->set($new, true); $oc = WBClass::create('WBDatasource_ObscureCode'); /** @var $oc WBDatasource_ObscureCode */ $obscure = $oc->setNamespace(__CLASS__ . ':register')->setId($id)->create()->get(); // pass id and obscure code to event $new['id'] = $id; $new['obscurecode'] = $id . $obscure; $new = array_merge($data, $new); WBEvent::trigger('user:registered', 'Created new user account {NICKNAME} for {EMAIL}', $new); return $id; } /** * appove newley generated account * * Obscure-string is concatenated of user's id an her activation code. * * @param string $obscure * @param bool $clear whether to clear the code or not * @return bool true on success */ public function approve($obscure, $clear = true) { $id = null; $code = null; $oc = $this->extractObscureCode($obscure, __CLASS__ . ':register', $id, $code); if (!$oc || !$oc->check($code)) { return false; } $storage = $this->getStorageModule(); /** @var $storage WBUser_Storage */ if (!$storage->load($id)) { return false; } $data = array( 'approved' => 1 ); $storage->set($data); $data = $storage->get(); WBEvent::trigger('user:approved', 'Approved user account {NICKNAME}', $data); if ($clear) { $oc->clear(); } $this->id = $id; return true; } /** * init password renewal * * @param array $data */ public function renewPassword($data) { // be more tolarant and find user by email address or nickname if (isset($data['nicknameoremail'])) { if (strstr($data['nicknameoremail'], '@')) { $data['email'] = $data['nicknameoremail']; } else { $data['nickname'] = $data['nicknameoremail']; } unset($data['nicknameoremail']); } // cannot find user without nickname or email address if (!isset($data['nickname']) && !isset($data['email'])) { return; } // find user id $storage = $this->getStorageModule(); /** @var $storage WBUser_Storage */ $id = $storage->find($data); if (empty($id)) { return; } $oc = WBClass::create('WBDatasource_ObscureCode'); /** @var $oc WBDatasource_ObscureCode */ $obscure = $oc->setNamespace(__CLASS__ . ':passwordRenewal')->setId($id)->create()->get(); // get rest of user's data and add id as well as obscure code $data = $storage->get(); $data['id'] = $id; $data['obscurecode'] = $id . $obscure; WBEvent::trigger('user:password:renew', 'Init user {ID} password renewal', $data); } /** * approve password renewal * * @param string $obscure * @param bool $clear whether to clear the code or not * @return bool true if code match */ public function approvePasswordRenewal($obscure, $clear = true) { $id = null; $code = null; $oc = $this->extractObscureCode($obscure, __CLASS__ . ':passwordRenewal', $id, $code); if (!$oc || !$oc->check($code)) { return false; } if ($clear) { $oc->clear(); } $this->id = $id; return true; } /** * set password * * set new password for user * * @todo throw exeptions * @param string $password * @param string $id */ public function setPassword($password, $id = null) { if (!$id) { $id = $this->id; } if (empty($id)) { return; } if (empty($password)) { return; } $storage = $this->getStorageModule(); if (!$storage->load($id)) { return; } $save['password'] = $password; /** @var $storage WBUser_Storage */ $storage->set($save); } /** * extract code and id from obscure code * * Demount obscure string and init obscureCode object * * @param string $obscure * @param string $ns * @param string $id * @param string $code * @return WBDatasource_ObscureCode */ protected function extractObscureCode($obscure, $ns, &$id, &$code) { if (!preg_match('/^(\d+)([a-zA-Z]\w+[a-zA-Z])$/', $obscure, $match)) { return false; } $id = $match[1]; $code = $match[2]; $oc = WBClass::create('WBDatasource_ObscureCode'); /** @var $oc WBDatasource_ObscureCode */ $oc->setNamespace($ns)->setId($id); return $oc; } }