PageRenderTime 22ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/engine/core_modules/email.reservation.php

https://gitlab.com/Nightprince/Warcry-CMS
PHP | 235 lines | 148 code | 28 blank | 59 comment | 19 complexity | cc6c8abf2c399356467e4bb1104cac32 MD5 | raw file
  1. <?php
  2. if (!defined('init_engine'))
  3. {
  4. header('HTTP/1.0 404 not found');
  5. exit;
  6. }
  7. class EmailReservations
  8. {
  9. const EXPIRE_NEVER = 'never';
  10. const DEFAULT_APPLICATION = 'SYSTEM';
  11. /**
  12. ** Generates random key
  13. **/
  14. static public function generateKey()
  15. {
  16. //generate the key
  17. $key = uniqid(mt_rand(), true) . uniqid(mt_rand(), true);
  18. //strip dots
  19. return str_replace('.', '', $key);
  20. }
  21. /**
  22. ** Reserves email address uses array as arguments and returns array with key 'result' containing bool true/false
  23. ** bool true - will also return 'key' containing the key used for the reserve
  24. ** bool false - will also return 'error' containing error message
  25. **/
  26. static public function Reserve($args)
  27. {
  28. global $DB, $CORE;
  29. //define the default arguments
  30. $defaultArgs = array(
  31. 'application' => self::DEFAULT_APPLICATION,
  32. 'key' => self::generateKey(),
  33. 'time' => $CORE->getTime(),
  34. 'expire' => self::EXPIRE_NEVER,
  35. );
  36. //check if the arguments are array
  37. if (!is_array($args))
  38. {
  39. return array('result' => false, 'error' => 'The arguments must be passed as array.');
  40. }
  41. //check if email was passed
  42. if (!isset($args['email']))
  43. {
  44. return array('result' => false, 'error' => 'Please pass email to the arguments.');
  45. }
  46. //merge with defaults
  47. $args = array_merge($defaultArgs, $args);
  48. //insert new key
  49. $insert_res = $DB->prepare("INSERT INTO `reserved_emails` (`email`, `application`, `key`, `time`, `expire`) VALUES (:email, :app, :key, :time, :expire);");
  50. $insert_res->bindParam(':email', $args['email'], PDO::PARAM_STR);
  51. $insert_res->bindParam(':app', $args['application'], PDO::PARAM_STR);
  52. $insert_res->bindParam(':key', $args['key'], PDO::PARAM_STR);
  53. $insert_res->bindParam(':time', $args['time'], PDO::PARAM_STR);
  54. $insert_res->bindParam(':expire', $args['expire'], PDO::PARAM_STR);
  55. $insert_res->execute();
  56. if ($insert_res->rowCount() < 1)
  57. {
  58. return array('result' => false, 'error' => 'Unable to insert the record into the database.');
  59. }
  60. unset($insert_res);
  61. //return the key used for the reserve
  62. return array('result' => true, 'key' => $args['key']);
  63. }
  64. /**
  65. ** Removes E-mail Reservation
  66. ** Returns:
  67. ** true - upon success
  68. ** string - upon error
  69. **/
  70. static public function Unreserve($args)
  71. {
  72. global $DB;
  73. //define the default arguments
  74. $defaultArgs = array(
  75. 'application' => self::DEFAULT_APPLICATION,
  76. );
  77. //check if the arguments are array
  78. if (!is_array($args))
  79. {
  80. return 'The arguments must be passed as array.';
  81. }
  82. //check if email was passed
  83. if (!isset($args['email']))
  84. {
  85. return 'Please pass email to the arguments.';
  86. }
  87. //check if key was passed
  88. if (!isset($args['key']))
  89. {
  90. return 'Please pass key to the arguments.';
  91. }
  92. //merge with defaults
  93. $args = array_merge($defaultArgs, $args);
  94. //verify the key
  95. $res = $DB->prepare("SELECT * FROM `reserved_emails` WHERE `email` = :email AND `application` = :app LIMIT 1;");
  96. $res->bindParam(':email', $args['email'], PDO::PARAM_STR);
  97. $res->bindParam(':app', $args['application'], PDO::PARAM_STR);
  98. $res->execute();
  99. //check if we have a match
  100. if ($res->rowCount() == 0)
  101. {
  102. //the email is not reserved so just return success
  103. return true;
  104. }
  105. //fetch the found record
  106. $row = $res->fetch();
  107. //unset the res
  108. unset($res);
  109. //Now we must validate the key
  110. if ($args['key'] === $row['key'])
  111. {
  112. //delete the record we have the key
  113. $delete = $DB->prepare("DELETE FROM `reserved_emails` WHERE `id` = :id LIMIT 1;");
  114. $delete->bindParam(':id', $row['id'], PDO::PARAM_INT);
  115. $delete->execute();
  116. if ($delete->rowCount() == 0)
  117. {
  118. return 'Failed to delete the record.';
  119. }
  120. else
  121. {
  122. //the email is now available
  123. return true;
  124. }
  125. }
  126. else
  127. {
  128. return 'Wrong key was passed.';
  129. }
  130. }
  131. /**
  132. ** Checks if a Email Adress is reserved
  133. ** Returns:
  134. ** true - if reserved
  135. ** false - if available
  136. ** string - upon error
  137. **/
  138. static public function IsReserved($args)
  139. {
  140. global $DB, $CORE;
  141. //define the default arguments
  142. $defaultArgs = array(
  143. 'application' => self::DEFAULT_APPLICATION,
  144. );
  145. //check if the arguments are array
  146. if (!is_array($args))
  147. {
  148. return 'The arguments must be passed as array.';
  149. }
  150. //check if email was passed
  151. if (!isset($args['email']))
  152. {
  153. return 'Please pass email to the arguments.';
  154. }
  155. //merge with defaults
  156. $args = array_merge($defaultArgs, $args);
  157. //verify the key
  158. $res = $DB->prepare("SELECT * FROM `reserved_emails` WHERE `email` = :email AND `application` = :app LIMIT 1;");
  159. $res->bindParam(':email', $args['email'], PDO::PARAM_STR);
  160. $res->bindParam(':app', $args['application'], PDO::PARAM_STR);
  161. $res->execute();
  162. //check if we have a match
  163. if ($res->rowCount() == 0)
  164. {
  165. //the email address is not reserved
  166. return false;
  167. }
  168. else
  169. {
  170. //fetch the found record
  171. $row = $res->fetch();
  172. //free up some memory
  173. unset($res);
  174. //check expiration
  175. if ($row['expire'] == self::EXPIRE_NEVER)
  176. {
  177. //the email is reserved and it has no expiration time
  178. return true;
  179. }
  180. else
  181. {
  182. //check if it's expired
  183. //Convert to Time Object
  184. $timeObj = $CORE->getTime(true, $row['time']);
  185. $timeObj->add(date_interval_create_from_date_string($row['expire']));
  186. $expires = $timeObj->format('Y-m-d H:i:s');
  187. //now check if the time now is greater than the expiration
  188. if ($CORE->getTime() > $expires)
  189. {
  190. //The reservation has expired, delete it
  191. $delete = $DB->prepare("DELETE FROM `reserved_emails` WHERE `id` = :id LIMIT 1;");
  192. $delete->bindParam(':id', $row['id'], PDO::PARAM_INT);
  193. $delete->execute();
  194. unset($delete);
  195. //return email is not reserved
  196. return false;
  197. }
  198. else
  199. {
  200. //the reservation is still active
  201. return true;
  202. }
  203. }
  204. }
  205. unset($res);
  206. }
  207. }