PageRenderTime 68ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/admin.reguser.class.php

http://litepublisher.googlecode.com/
PHP | 165 lines | 131 code | 27 blank | 7 comment | 24 complexity | aba9681107d726f8044faa5f9b2c3d6a MD5 | raw file
Possible License(s): AGPL-1.0, GPL-3.0
  1. <?php
  2. /**
  3. * Lite Publisher
  4. * Copyright (C) 2010 - 2013 Vladimir Yushko http://litepublisher.ru/ http://litepublisher.com/
  5. * Dual licensed under the MIT (mit.txt)
  6. * and GPL (gpl.txt) licenses.
  7. **/
  8. class tadminreguser extends tadminform {
  9. private $regstatus;
  10. private $backurl;
  11. public static function i() {
  12. return getinstance(__class__);
  13. }
  14. protected function create() {
  15. parent::create();
  16. $this->basename = 'admin.reguser';
  17. $this->addevents('oncontent');
  18. $this->data['widget'] = '';
  19. $this->section = 'users';
  20. $this->regstatus = false;
  21. }
  22. public function gettitle() {
  23. return tlocal::get('users', 'adduser');
  24. }
  25. public function getlogged() {
  26. if (litepublisher::$options->cookieenabled) {
  27. return litepublisher::$options->authcookie();
  28. } else {
  29. $auth = tauthdigest::i();
  30. return $auth->auth();
  31. }
  32. }
  33. public function request($arg) {
  34. if (!litepublisher::$options->usersenabled || !litepublisher::$options->reguser) return 403;
  35. parent::request($arg);
  36. if (!empty($_GET['confirm'])) {
  37. $confirm = $_GET['confirm'];
  38. $email = $_GET['email'];
  39. tsession::start('reguser-' . md5($email));
  40. if (!isset($_SESSION['email']) || ($email != $_SESSION['email']) || ($confirm != $_SESSION['confirm'])) {
  41. if (!isset($_SESSION['email'])) session_destroy();
  42. $this->regstatus = 'error';
  43. return;
  44. }
  45. $this->backurl = $_SESSION['backurl'];
  46. $users = tusers::i();
  47. $id = $users->add(array(
  48. 'password' => $_SESSION['password'],
  49. 'name' => $_SESSION['name'],
  50. 'email' => $_SESSION['email']
  51. ));
  52. session_destroy();
  53. if ($id) {
  54. $this->regstatus = 'ok';
  55. $expired = time() + 1210000;
  56. $cookie = md5uniq();
  57. litepublisher::$options->user = $id;
  58. litepublisher::$options->updategroup();
  59. litepublisher::$options->setcookies($cookie, $expired);
  60. } else {
  61. $this->regstatus = 'error';
  62. }
  63. }
  64. }
  65. public function getcontent() {
  66. $result = '';
  67. $html = $this->html;
  68. $lang = tlocal::admin('users');
  69. if ($this->logged) return $html->logged();
  70. $args = new targs();
  71. if ($this->regstatus) {
  72. switch ($this->regstatus) {
  73. case 'ok':
  74. if (!$this->backurl) $this->backurl = tusergroups::i()->gethome(litepublisher::$options->group);
  75. if (!strbegin($this->backurl, 'http://')) $this->backurl = litepublisher::$site->url . $this->backurl;
  76. $args->backurl = $this->backurl;
  77. return $html->successreg($args);
  78. case 'mail':
  79. return $html->h4->waitconfirm;
  80. case 'error':
  81. $result .= $html->h4->invalidregdata;
  82. }
  83. }
  84. $form = '';
  85. foreach (array('email', 'name') as $name) {
  86. $args->$name = isset($_POST[$name]) ? $_POST[$name] : '';
  87. $form .= "[text=$name]";
  88. }
  89. $lang = tlocal::i('users');
  90. $args->formtitle = $lang->regform;
  91. $args->data['$lang.email'] = 'email';
  92. $result .= $this->widget;
  93. if (isset($_GET['backurl'])) {
  94. //normalize
  95. $result = str_replace('&amp;backurl=', '&backurl=', $result);
  96. $result = str_replace('backurl=', 'backurl=' . urlencode($_GET['backurl']), $result);
  97. $result = str_replace('backurl%3D', 'backurl%3D' . urlencode(urlencode($_GET['backurl'])), $result);
  98. }
  99. $result .= $html->adminform($form, $args);
  100. $result = str_replace(' action=""',' action="' . litepublisher::$site->url . '/admin/reguser/"', $result);
  101. $this->callevent('oncontent', array(&$result));
  102. return $result;
  103. }
  104. public function processform() {
  105. $this->regstatus = 'error';
  106. extract($_POST, EXTR_SKIP);
  107. $email = strtolower(trim($email));
  108. if (!tcontentfilter::ValidateEmail($email)) return sprintf('<p><strong>%s</strong></p>', tlocal::get('comment', 'invalidemail'));
  109. if (substr_count($email, '.', 0, strpos($email, '@')) > 2) return sprintf('<p><strong>%s</strong></p>', tlocal::get('comment', 'invalidemail'));
  110. $users = tusers::i();
  111. if ($id = $users->emailexists($email)) {
  112. if ('comuser' != $users->getvalue($id, 'status')) return $this->html->h4->invalidregdata;
  113. }
  114. tsession::start('reguser-' . md5($email));
  115. $_SESSION['email'] = $email;
  116. $_SESSION['name'] = $name;
  117. $confirm = md5(mt_rand() . litepublisher::$secret. microtime());
  118. $_SESSION['confirm'] = $confirm;
  119. $password = md5uniq();
  120. $_SESSION['password'] = $password;
  121. $_SESSION['backurl'] = isset($_GET['backurl']) ? $_GET['backurl'] : '';
  122. session_write_close();
  123. $args = new targs();
  124. $args->name = $name;
  125. $args->email = $email;
  126. $args->confirm = $confirm;
  127. $args->password = $password;
  128. $args->confirmurl = litepublisher::$site->url . '/admin/reguser/' . litepublisher::$site->q . 'email=' . urlencode($email);
  129. tlocal::usefile('mail');
  130. $lang = tlocal::i('mailusers');
  131. $theme = ttheme::i();
  132. $subject = $theme->parsearg($lang->subject, $args);
  133. $body = $theme->parsearg($lang->body, $args);
  134. tmailer::sendmail(litepublisher::$site->name, litepublisher::$options->fromemail,
  135. $name, $email, $subject, $body);
  136. $this->regstatus = 'mail';
  137. }
  138. }//class