PageRenderTime 94ms CodeModel.GetById 37ms RepoModel.GetById 0ms app.codeStats 1ms

/modules/profile/register.php

https://gitlab.com/VoyaTrax/vtCMS3
PHP | 324 lines | 243 code | 38 blank | 43 comment | 71 complexity | b1591cb90c4b1ef3273d34db90c50ff5 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, GPL-3.0, LGPL-3.0
  1. <?php
  2. /*
  3. You may not change or alter any portion of this comment or credits
  4. of supporting developers from this source code or any supporting source code
  5. which is considered copyrighted (c) material of the original comment or credit authors.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. */
  10. /**
  11. * Extended User Profile
  12. *
  13. * @copyright The XOOPS Project http://sourceforge.net/projects/xoops/
  14. * @license http://www.fsf.org/copyleft/gpl.html GNU public license
  15. * @package profile
  16. * @since 2.3.0
  17. * @author Taiwen Jiang <phppp@users.sourceforge.net>
  18. * @author Jan Pedersen
  19. * @author trabis <lusopoemas@gmail.com>
  20. * @version $Id: register.php 10408 2012-12-16 18:43:15Z trabis $
  21. */
  22. include dirname(__FILE__) . DIRECTORY_SEPARATOR . 'header.php';
  23. $xoops = Xoops::getInstance();
  24. if ($xoops->isUser()) {
  25. header('location: userinfo.php?uid= ' . $xoops->user->getVar('uid'));
  26. exit();
  27. }
  28. if (!empty($_GET['op']) && in_array($_GET['op'], array('actv', 'activate'))) {
  29. header("location: ./activate.php" . (empty($_SERVER['QUERY_STRING']) ? "" : "?" . $_SERVER['QUERY_STRING']));
  30. exit();
  31. }
  32. $myts = MyTextSanitizer::getInstance();
  33. $xoops->getConfigs();
  34. if (!$xoops->getConfig('allow_register')) {
  35. $xoops->redirect('index.php', 6, _US_NOREGISTER);
  36. }
  37. $op = !isset($_POST['op']) ? 'register' : $_POST['op'];
  38. $current_step = isset($_POST['step']) ? intval($_POST['step']) : 0;
  39. // The newly introduced variable $_SESSION['profile_post'] is contaminated by $_POST, thus we use an old vaiable to hold uid parameter
  40. $uid = !empty($_SESSION['profile_register_uid']) ? intval($_SESSION['profile_register_uid']) : 0;
  41. // First step is already secured by with the captcha Token so lets check the others
  42. if ($current_step > 0 && !$xoops->security()->check()) {
  43. $xoops->redirect('user.php', 5, _PROFILE_MA_EXPIRED);
  44. }
  45. $criteria = new CriteriaCompo();
  46. $criteria->setSort("step_order");
  47. $regstep_handler = $xoops->getModuleHandler('regstep');
  48. if (!$steps = $regstep_handler->getAll($criteria, null, false, false)) {
  49. $xoops->redirect(XOOPS_URL . '/', 6, _PROFILE_MA_NOSTEPSAVAILABLE);
  50. }
  51. foreach (array_keys($steps) as $key) {
  52. $steps[$key]['step_no'] = $key + 1;
  53. }
  54. $xoops->header('profile_register.html');
  55. $xoops->tpl()->assign('steps', $steps);
  56. $xoops->tpl()->assign('lang_register_steps', _PROFILE_MA_REGISTER_STEPS);
  57. $xoops->appendConfig('profile_breadcrumbs', array(
  58. 'link' => $xoops->url('modules/profile/register.php'),
  59. 'title' => _PROFILE_MA_REGISTER
  60. ));
  61. if (isset($steps[$current_step])) {
  62. $xoops->appendConfig('profile_breadcrumbs', array('title' => $steps[$current_step]['step_name']));
  63. }
  64. $member_handler = $xoops->getHandlerMember();
  65. /* @var $profile_handler ProfileProfileHandler */
  66. $profile_handler = $xoops->getModuleHandler('profile');
  67. $fields = $profile_handler->loadFields();
  68. $userfields = $profile_handler->getUserVars();
  69. if ($uid == 0) {
  70. // No user yet? Create one and set default values.
  71. $newuser = $member_handler->createUser();
  72. $profile = $profile_handler->create();
  73. if (count($fields) > 0) {
  74. /* @var ProfileField $field */
  75. foreach ($fields as $field) {
  76. $fieldname = $field->getVar('field_name');
  77. if (in_array($fieldname, $userfields)) {
  78. $default = $field->getVar('field_default');
  79. if ($default === '' || $default === null) {
  80. continue;
  81. }
  82. $newuser->setVar($fieldname, $default);
  83. }
  84. }
  85. }
  86. } else {
  87. // We already have a user? Just load it! Security is handled by token so there is no fake uid here.
  88. $newuser = $member_handler->getUser($uid);
  89. $profile = $profile_handler->getProfile($uid);
  90. }
  91. // Lets merge current $_POST with $_SESSION['profile_post'] so we can have access to info submited in previous steps
  92. // Get all fields that we can expect from a $_POST including our private '_message_'
  93. $fieldnames = array();
  94. /* @var ProfileField $field */
  95. foreach ($fields as $field) {
  96. $fieldnames[] = $field->getVar('field_name');
  97. }
  98. $fieldnames = array_merge($fieldnames, $userfields);
  99. $fieldnames[] = '_message_';
  100. // Get $_POST that matches above criteria, we do not need to store step, tokens, etc
  101. $postfields = array();
  102. foreach ($fieldnames as $fieldname) {
  103. if (isset($_POST[$fieldname])) {
  104. $postfields[$fieldname] = $_POST[$fieldname];
  105. }
  106. }
  107. if ($current_step == 0) {
  108. // Reset any previous session for first step
  109. $_SESSION['profile_post'] = array();
  110. $_SESSION['profile_register_uid'] = null;
  111. } else {
  112. // Merge current $_POST with $_SESSION['profile_post']
  113. $_SESSION['profile_post'] = array_merge($_SESSION['profile_post'], $postfields);
  114. $_POST = array_merge($_SESSION['profile_post'], $_POST);
  115. }
  116. // Set vars from $_POST/$_SESSION['profile_post']
  117. foreach ($fields as $fieldname => $field) {
  118. if (!isset($_POST[$fieldname])) {
  119. continue;
  120. }
  121. $value = $field->getValueForSave($_POST[$fieldname]);
  122. if (in_array($field, $userfields)) {
  123. $newuser->setVar($fieldname, $value);
  124. } else {
  125. $profile->setVar($fieldname, $value);
  126. }
  127. }
  128. $stop = '';
  129. //Client side validation
  130. if (isset($_POST['step']) && isset($_SESSION['profile_required'])) {
  131. foreach ($_SESSION['profile_required'] as $name => $title) {
  132. if (!isset($_POST[$name]) || empty($_POST[$name])) {
  133. $stop .= sprintf(_FORM_ENTER, $title) . '<br />';
  134. }
  135. }
  136. }
  137. // Check user data at first step
  138. if ($current_step == 1) {
  139. $uname = isset($_POST['uname']) ? $myts->stripSlashesGPC(trim($_POST['uname'])) : '';
  140. $email = isset($_POST['email']) ? $myts->stripSlashesGPC(trim($_POST['email'])) : '';
  141. $url = isset($_POST['url']) ? $myts->stripSlashesGPC(trim($_POST['url'])) : '';
  142. $pass = isset($_POST['pass']) ? $myts->stripSlashesGPC(trim($_POST['pass'])) : '';
  143. $vpass = isset($_POST['vpass']) ? $myts->stripSlashesGPC(trim($_POST['vpass'])) : '';
  144. $agree_disc = (isset($_POST['agree_disc']) && intval($_POST['agree_disc'])) ? 1 : 0;
  145. if ($xoops->getConfig('reg_dispdsclmr') != 0 && $xoops->getConfig('reg_disclaimer') != '') {
  146. if (empty($agree_disc)) {
  147. $stop .= _US_UNEEDAGREE . '<br />';
  148. }
  149. }
  150. $newuser->setVar('uname', $uname);
  151. $newuser->setVar('email', $email);
  152. $newuser->setVar('pass', $pass ? md5($pass) : '');
  153. $stop .= XoopsUserUtility::validate($newuser, $pass, $vpass);
  154. $xoopsCaptcha = XoopsCaptcha::getInstance();
  155. if (!$xoopsCaptcha->verify()) {
  156. $stop .= $xoopsCaptcha->getMessage();
  157. }
  158. }
  159. // If the last step required SAVE or if we're on the last step then we will insert/update user on database
  160. if ($current_step > 0 && empty($stop) && (!empty($steps[$current_step - 1]['step_save']) || !isset($steps[$current_step]))) {
  161. $isNew = $newuser->isNew();
  162. //Did created an user already? If not then let us set some extra info
  163. if ($isNew) {
  164. $uname = isset($_POST['uname']) ? $myts->stripSlashesGPC(trim($_POST['uname'])) : '';
  165. $email = isset($_POST['email']) ? $myts->stripSlashesGPC(trim($_POST['email'])) : '';
  166. $url = isset($_POST['url']) ? $myts->stripSlashesGPC(trim($_POST['url'])) : '';
  167. $pass = isset($_POST['pass']) ? $myts->stripSlashesGPC(trim($_POST['pass'])) : '';
  168. $newuser->setVar('uname', $uname);
  169. $newuser->setVar('email', $email);
  170. $newuser->setVar('pass', $pass ? md5($pass) : '');
  171. $actkey = substr(md5(uniqid(mt_rand(), 1)), 0, 8);
  172. $newuser->setVar('actkey', $actkey, true);
  173. $newuser->setVar('user_regdate', time(), true);
  174. $newuser->setVar('uorder', $xoops->getConfig('com_order'), true);
  175. $newuser->setVar('umode', $xoops->getConfig('com_mode'), true);
  176. $newuser->setVar('theme', $xoops->getConfig('theme_set'), true);
  177. $newuser->setVar('user_avatar', 'avatars/blank.gif', true);
  178. if ($xoops->getConfig('activation_type') == 1) {
  179. $newuser->setVar('level', 1, true);
  180. } else {
  181. $newuser->setVar('level', 0, true);
  182. }
  183. }
  184. // Insert/update user and check if we have succeded
  185. if (!$member_handler->insertUser($newuser)) {
  186. $stop .= _US_REGISTERNG . "<br />";
  187. $stop .= implode('<br />', $newuser->getErrors());
  188. } else {
  189. // User inserted! Now insert custom profile fields
  190. $profile->setVar('profile_id', $newuser->getVar('uid'));
  191. $profile_handler->insert($profile);
  192. // We are good! If this is 'was' a new user then we handle notification
  193. if ($isNew) {
  194. if ($xoops->getConfig('new_user_notify') == 1 && $xoops->getConfig('new_user_notify_group')) {
  195. $xoopsMailer = $xoops->getMailer();
  196. $xoopsMailer->reset();
  197. $xoopsMailer->useMail();
  198. $xoopsMailer->setToGroups($member_handler->getGroup($xoops->getConfig('new_user_notify_group')));
  199. $xoopsMailer->setFromEmail($xoops->getConfig('adminmail'));
  200. $xoopsMailer->setFromName($xoops->getConfig('sitename'));
  201. $xoopsMailer->setSubject(sprintf(_US_NEWUSERREGAT, $xoops->getConfig('sitename')));
  202. $xoopsMailer->setBody(sprintf(_US_HASJUSTREG, $newuser->getVar('uname')));
  203. $xoopsMailer->send(true);
  204. }
  205. $message = "";
  206. if (!$member_handler->addUserToGroup(XOOPS_GROUP_USERS, $newuser->getVar('uid'))) {
  207. $message = _PROFILE_MA_REGISTER_NOTGROUP . "<br />";
  208. } else {
  209. if ($xoops->getConfig('activation_type') == 1) {
  210. XoopsUserUtility::sendWelcome($newuser);
  211. } else {
  212. if ($xoops->getConfig('activation_type') == 0) {
  213. $xoopsMailer = $xoops->getMailer();
  214. $xoopsMailer->reset();
  215. $xoopsMailer->useMail();
  216. $xoopsMailer->setTemplate('register.tpl');
  217. $xoopsMailer->assign('SITENAME', $xoops->getConfig('sitename'));
  218. $xoopsMailer->assign('ADMINMAIL', $xoops->getConfig('adminmail'));
  219. $xoopsMailer->assign('SITEURL', XOOPS_URL . "/");
  220. $xoopsMailer->assign('X_UPASS', $_POST['vpass']);
  221. $xoopsMailer->setToUsers($newuser);
  222. $xoopsMailer->setFromEmail($xoops->getConfig('adminmail'));
  223. $xoopsMailer->setFromName($xoops->getConfig('sitename'));
  224. $xoopsMailer->setSubject(sprintf(_US_USERKEYFOR, $newuser->getVar('uname')));
  225. if (!$xoopsMailer->send(true)) {
  226. $_SESSION['profile_post']['_message_'] = 0;
  227. } else {
  228. $_SESSION['profile_post']['_message_'] = 1;
  229. }
  230. } else {
  231. if ($xoops->getConfig('activation_type') == 2) {
  232. $xoopsMailer = $xoops->getMailer();
  233. $xoopsMailer->reset();
  234. $xoopsMailer->useMail();
  235. $xoopsMailer->setTemplate('adminactivate.tpl');
  236. $xoopsMailer->assign('USERNAME', $newuser->getVar('uname'));
  237. $xoopsMailer->assign('USEREMAIL', $newuser->getVar('email'));
  238. $xoopsMailer->assign('USERACTLINK', XOOPS_URL . "/modules/" . $xoops->module->getVar('dirname', 'n') . '/activate.php?id=' . $newuser->getVar('uid') . '&actkey=' . $newuser->getVar('actkey', 'n'));
  239. $xoopsMailer->assign('SITENAME', $xoops->getConfig('sitename'));
  240. $xoopsMailer->assign('ADMINMAIL', $xoops->getConfig('adminmail'));
  241. $xoopsMailer->assign('SITEURL', XOOPS_URL . "/");
  242. $xoopsMailer->setToGroups($member_handler->getGroup($xoops->getConfig('activation_group')));
  243. $xoopsMailer->setFromEmail($xoops->getConfig('adminmail'));
  244. $xoopsMailer->setFromName($xoops->getConfig('sitename'));
  245. $xoopsMailer->setSubject(sprintf(_US_USERKEYFOR, $newuser->getVar('uname')));
  246. if (!$xoopsMailer->send()) {
  247. $_SESSION['profile_post']['_message_'] = 2;
  248. } else {
  249. $_SESSION['profile_post']['_message_'] = 3;
  250. }
  251. }
  252. }
  253. }
  254. }
  255. if ($message) {
  256. $xoops->tpl()->append('confirm', $message);
  257. }
  258. $_SESSION['profile_register_uid'] = $newuser->getVar('uid');
  259. }
  260. }
  261. }
  262. if (!empty($stop) || isset($steps[$current_step])) {
  263. include_once dirname(__FILE__) . '/include/forms.php';
  264. $current_step = empty($stop) ? $current_step : $current_step - 1;
  265. $reg_form = profile_getRegisterForm($newuser, $profile, $steps[$current_step]);
  266. $reg_form->assign($xoops->tpl());
  267. $xoops->tpl()->assign('current_step', $current_step);
  268. $xoops->tpl()->assign('stop', $stop);
  269. } else {
  270. // No errors and no more steps, finish
  271. $xoops->tpl()->assign('finish', _PROFILE_MA_REGISTER_FINISH);
  272. $xoops->tpl()->assign('current_step', -1);
  273. if ($xoops->getConfig('activation_type') == 1 && !empty($_SESSION['profile_post']['pass'])) {
  274. $xoops->tpl()->assign('finish_login', _PROFILE_MA_FINISH_LOGIN);
  275. $xoops->tpl()->assign('finish_uname', $newuser->getVar('uname'));
  276. $xoops->tpl()->assign('finish_pass', htmlspecialchars($_SESSION['profile_post']['pass']));
  277. }
  278. if (isset($_SESSION['profile_post']['_message_'])) {
  279. //todo, if user is activated by admin, then we should inform it along with error messages. _US_YOURREGMAILNG is not enough
  280. $messages = array(_US_YOURREGMAILNG, _US_YOURREGISTERED, _US_YOURREGMAILNG, _US_YOURREGISTERED2);
  281. $xoops->tpl()->assign('finish_message', $messages[$_SESSION['profile_post']['_message_']]);
  282. }
  283. $_SESSION['profile_post'] = null;
  284. }
  285. include dirname(__FILE__) . DIRECTORY_SEPARATOR . 'footer.php';