PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/engine/core_modules/accounts.activation.php

https://gitlab.com/Nightprince/Warcry-CMS
PHP | 263 lines | 166 code | 30 blank | 67 comment | 14 complexity | 07e5d0d2c2417a9408d6a66d35063490 MD5 | raw file
  1. <?php
  2. if (!defined('init_engine'))
  3. {
  4. header('HTTP/1.0 404 not found');
  5. exit;
  6. }
  7. class AccountsActivation
  8. {
  9. protected $salt = 'tdcActivation'; //Secret Word
  10. private $key = false;
  11. private $account;
  12. public function __construct($accid = false)
  13. {
  14. if ($accid)
  15. $this->account = $accid;
  16. }
  17. /**
  18. ** Generates random key by the account ID and salt
  19. **/
  20. public function generateKey()
  21. {
  22. $this->key = uniqid(mt_rand(), true) . sha1($this->account . $this->salt) . uniqid(mt_rand(), true);
  23. $this->key = str_replace('.', '', $this->key);
  24. }
  25. /**
  26. ** Registers the key generated by ($this->generateKey()) into the database
  27. **
  28. ** Returns:
  29. ** --------------------------------------------------------------------------------------------
  30. ** true - Returned when the `activation` record is inserted
  31. ** false - Returned when the `activation` query failed to insert
  32. ** - Returned when there is no key
  33. ** --------------------------------------------------------------------------------------------
  34. **/
  35. public function registerKey()
  36. {
  37. global $DB, $CORE;
  38. //check if we have key
  39. if ($this->key)
  40. {
  41. //erase old keys
  42. $delete_res = $DB->prepare("DELETE FROM `activations` WHERE `account` = :account");
  43. $delete_res->bindParam(':account', $this->account, PDO::PARAM_INT);
  44. $delete_res->execute();
  45. //insert new key
  46. $insert_res = $DB->prepare("INSERT INTO `activations` (`account` ,`key` ,`time`) VALUES (:account, :key, :time)");
  47. $insert_res->bindParam(':account', $this->account, PDO::PARAM_INT);
  48. $insert_res->bindParam(':key', $this->key, PDO::PARAM_STR);
  49. $insert_res->bindParam(':time', $CORE->getTime(), PDO::PARAM_STR);
  50. $insert_res->execute();
  51. if ($insert_res->rowCount() < 1)
  52. {
  53. return false;
  54. }
  55. }
  56. else
  57. {
  58. return false;
  59. }
  60. return true;
  61. }
  62. /**
  63. ** Encodes the key on Base64 and returns it
  64. **/
  65. public function get_encodedKey()
  66. {
  67. if ($this->key)
  68. {
  69. return base64_encode($this->key);
  70. }
  71. else
  72. {
  73. //no key was generated, so we do it now
  74. $this->generateKey();
  75. //return the key using the same function
  76. return $this->get_encodedKey();
  77. }
  78. }
  79. /**
  80. ** Get account id, if stored
  81. **/
  82. public function get_storedAccountID()
  83. {
  84. if ($this->account)
  85. {
  86. return $this->account;
  87. }
  88. else
  89. {
  90. return false;
  91. }
  92. }
  93. /**
  94. ** Decodes the key on Base64 and stores it in the class
  95. **/
  96. public function set_decodedKey($key = false)
  97. {
  98. if ($key)
  99. {
  100. $this->key = base64_decode($key);
  101. }
  102. else
  103. {
  104. return false;
  105. }
  106. }
  107. /**
  108. ** Sends mail for account activation
  109. **
  110. ** Selects account record by ID stored in the class from __construct()
  111. **
  112. ** Returns:
  113. ** --------------------------------------------------------------------------------------------
  114. ** true - Returned when the mail is successfully sent
  115. ** false - Returned when the function fails to load mail HTML
  116. ** - Returned when the accounts query failed
  117. ** - Returned when the PHPMailer class failed to send mail
  118. ** --------------------------------------------------------------------------------------------
  119. **/
  120. public function sendMail()
  121. {
  122. global $config, $DB;
  123. //setup the PHPMailer class
  124. $mail = new PHPMailerLite();
  125. $mail->IsSendmail();
  126. $mail->SetFrom($config['Email'], 'DuloStore Support');
  127. //select the account record
  128. $res = $DB->prepare("SELECT id, email, firstName, lastName FROM `accounts` WHERE `id` = :account LIMIT 1");
  129. $res->bindParam(':account', $this->account, PDO::PARAM_INT);
  130. $res->execute();
  131. if ($res->rowCount() > 0)
  132. {
  133. $row = $res->fetch(PDO::FETCH_ASSOC);
  134. //get the message html
  135. $message = file_get_contents($config['RootPath'] . '/activation_mail.html');
  136. //break if the function failed to laod HTML
  137. if (!$message)
  138. {
  139. return false;
  140. }
  141. //replace the tags with info
  142. $search = array('{FIRST_NAME}', '{LAST_NAME}', '{URL}');
  143. $replace = array($row['firstName'], $row['lastName'], $config['BaseURL'] . '/index.php?page=activation&key=' . $this->get_encodedKey());
  144. $message = str_replace($search, $replace, $message);
  145. $mail->AddAddress($row['email'], $row['firstName']. ' ' .$row['lastName']);
  146. $mail->Subject = 'DuloStore Account Activation';
  147. $mail->MsgHTML($message);
  148. if (!$mail->Send())
  149. {
  150. return false;
  151. }
  152. }
  153. else
  154. {
  155. return false;
  156. }
  157. return true;
  158. }
  159. /**
  160. ** Activates account by rawDecodedUrl key sent to email
  161. **
  162. ** Return Codes:
  163. ** --------------------------------------------------------------------------------------------
  164. ** 'noKey' - Returned when there is no key
  165. ** 'missingRecord' - Returned when there is no activation record by the given key
  166. ** 'expiredKey' - Returned when the activation record is old (expired)
  167. ** 'updateFailed' - Returned when the accounts query has failed to update the status to active
  168. ** 'success' - Returned when the function succeeds
  169. ** --------------------------------------------------------------------------------------------
  170. **/
  171. public function activateByKey()
  172. {
  173. global $DB, $CORE;
  174. if ($this->key)
  175. {
  176. //make the query
  177. $res = $DB->prepare("SELECT * FROM `activations` WHERE `key` = :key LIMIT 1");
  178. //bind some parameters
  179. $res->bindParam(':key', $this->key, PDO::PARAM_STR);
  180. //run the query
  181. $res->execute();
  182. if ($res->rowCount() > 0)
  183. {
  184. //fetch associetive array
  185. $row = $res->fetch(PDO::FETCH_ASSOC);
  186. $this->account = $row['account'];
  187. //get the record time in timestamp
  188. $recordDate = new DateTime($row['time']);
  189. $recordTimestamp = $recordDate->getTimestamp();
  190. //create new time now -24 hours
  191. $newDate = $CORE->getTime(true);
  192. $newDate->modify("-24 hours");
  193. $agoTimestamp = $newDate->getTimestamp();
  194. //check if the key has expired
  195. if ($recordTimestamp < $agoTimestamp)
  196. {
  197. return 'expiredKey';
  198. }
  199. else
  200. {
  201. //delete the activation records
  202. $del = $DB->prepare("DELETE FROM `activations` WHERE `account` = :acc");
  203. $del->bindParam(':acc', $row['account'], PDO::PARAM_INT);
  204. $del->execute();
  205. //activate the account
  206. $update = $DB->prepare("UPDATE `accounts` SET `status` = 'active' WHERE `id` = :acc");
  207. $update->bindParam(':acc', $row['account'], PDO::PARAM_INT);
  208. $update->execute();
  209. if ($res->rowCount() > 0)
  210. {
  211. return 'success';
  212. }
  213. else
  214. {
  215. return 'updateFailed';
  216. }
  217. }
  218. }
  219. else
  220. {
  221. //no record with this key
  222. return 'missingRecord';
  223. }
  224. }
  225. else
  226. {
  227. return 'noKey';
  228. }
  229. }
  230. public function __destrruct()
  231. {
  232. }
  233. }