PageRenderTime 62ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/public/_lib/class.Subscriber.php

https://gitlab.com/oytunistrator/jobberbase
PHP | 253 lines | 207 code | 29 blank | 17 comment | 16 complexity | f67428b483d7efd4110f8fc914994091 MD5 | raw file
  1. <?php
  2. /**
  3. * jobber job board platform
  4. *
  5. * @author Rimas Kudelis <http://rimas.kudelis.lt>
  6. * @license You are free to edit and use this work, but it would be nice if you always referenced the original author ;)
  7. * (see license.txt).
  8. *
  9. * Subscriber class – represents a subscriber to job announcements and handles their subscriptions
  10. */
  11. class Subscriber {
  12. protected $_id;
  13. protected $_email;
  14. protected $_auth;
  15. protected $_keywords;
  16. public function __construct($email)
  17. {
  18. global $db;
  19. $email = (string)$email;
  20. $sql = 'SELECT id, auth, keywords FROM '.DB_PREFIX.'subscribers WHERE email = "' . $email . '"';
  21. $result = $db->QueryRow($sql);
  22. if ($result)
  23. {
  24. $this->_email = $email;
  25. $this->_id = $result['id'];
  26. $this->_auth = $result['auth'];
  27. $this->_keywords = $result['keywords'];
  28. }
  29. else
  30. {
  31. $auth = self::generateAuthCode();
  32. $sql = 'INSERT INTO '.DB_PREFIX.'subscribers (email, auth)
  33. VALUES ("' . $email . '", "' . $auth . '")';
  34. if($db->Execute($sql))
  35. {
  36. $this->_email = $email;
  37. $this->_id = $db->insert_id;
  38. $this->_auth = $auth;
  39. $this->_keywords = '';
  40. }
  41. }
  42. }
  43. public function getAuthCode()
  44. {
  45. return $this->_auth;
  46. }
  47. public function getKeywords()
  48. {
  49. return $this->_keywords;
  50. }
  51. public function setKeywords($keywords)
  52. {
  53. global $db;
  54. $keywords = explode(',', $_POST['keywords']);
  55. foreach ($keywords as &$keyword)
  56. {
  57. $keyword = trim($keyword);
  58. }
  59. $keywords = implode(',', $keywords);
  60. $sql = 'UPDATE '.DB_PREFIX.'subscribers SET keywords = "' . $db->real_escape_string($keywords) . '" WHERE id = ' . $this->_id;
  61. if ($db->Execute($sql))
  62. {
  63. $this->_keywords = $keywords;
  64. return true;
  65. }
  66. return false;
  67. }
  68. // Update/confirm the list of subscriptions for the user.
  69. public function updateSubscriptions($categories=array())
  70. {
  71. global $db;
  72. if (!is_array($categories))
  73. {
  74. $categories = array($categories);
  75. }
  76. // Delete existing subscriptions
  77. $sql = 'DELETE FROM '.DB_PREFIX.'subscriptions WHERE subscriber_id = ' . $this->_id;
  78. if ($db->Execute($sql))
  79. {
  80. if (empty($categories))
  81. {
  82. // Do nothing – updating to "no subscriptions"
  83. return true;
  84. }
  85. else
  86. {
  87. // Add the supplied subscriptions
  88. $addCombos = array();
  89. foreach ($categories as $category)
  90. {
  91. $addCombos[] = '(' . $this->_id . ',' . (int)$category . ',1)';
  92. }
  93. $addCombos = implode(',', $addCombos);
  94. $sql = 'INSERT INTO '.DB_PREFIX.'subscriptions (subscriber_id, category_id, confirmed) VALUES ' . $addCombos;
  95. return $db->Execute($sql);
  96. }
  97. }
  98. }
  99. // Subscribe an email to a given category
  100. public function addSubscription($category, $confirmed=false)
  101. {
  102. global $db;
  103. if ($this->isSubscribed($category))
  104. {
  105. return true; // do nothing if already subscribed and confirmed
  106. }
  107. $sql = 'REPLACE INTO '.DB_PREFIX.'subscriptions (subscriber_id, category_id, confirmed) VALUES (' . $this->_id . ',' . (int)$category . ',' . (int)(bool)$confirmed . ')';
  108. return $db->Execute($sql);
  109. }
  110. public function getConfirmedSubscriptions()
  111. {
  112. return $this->getSubscriptions(true);
  113. }
  114. public function getUnconfirmedSubscriptions()
  115. {
  116. return $this->getSubscriptions(false);
  117. }
  118. public function getSubscriptions($confirmed=null)
  119. {
  120. global $db;
  121. if (is_null($confirmed))
  122. {
  123. $sql = 'SELECT category_id FROM '.DB_PREFIX.'subscriptions WHERE subscriber_id = ' . $this->_id;
  124. }
  125. else
  126. {
  127. $sql = 'SELECT category_id FROM '.DB_PREFIX.'subscriptions WHERE subscriber_id = ' . $this->_id . ' AND confirmed = ' . (int)(bool)$confirmed;
  128. }
  129. if ($tmpResult = $db->QueryArray($sql))
  130. {
  131. // The following line works only with PHP 5.3 and above
  132. // $result = array_map(function($item){ return $item['category_id']; }, $tmpResult);
  133. $result = array();
  134. foreach ($tmpResult as $key=>$row)
  135. {
  136. $result[$key] = $row['category_id'];
  137. }
  138. return $result;
  139. }
  140. return false;
  141. }
  142. // Returns true if a given user is subscribed to a given category and the subscription is confirmed
  143. public function isSubscribed($category)
  144. {
  145. global $db;
  146. $sql = 'SELECT confirmed FROM '.DB_PREFIX.'subscriptions WHERE subscriber_id = ' . $this->_id . ' AND category_id = ' . (int)$category;
  147. return (bool)$db->QueryItem($sql);
  148. }
  149. public function delete()
  150. {
  151. global $db;
  152. $sql = 'DELETE FROM '.DB_PREFIX.'subscriptions WHERE subscriber_id = ' . $this->_id;
  153. if ($db->Execute($sql))
  154. {
  155. $sql = 'DELETE FROM '.DB_PREFIX.'subscribers WHERE id = ' . $this->_id;
  156. return $db->Execute($sql);
  157. }
  158. else
  159. {
  160. return false;
  161. }
  162. }
  163. public static function isValid($email, $auth)
  164. {
  165. global $db;
  166. $sql = 'SELECT id FROM '.DB_PREFIX.'subscribers WHERE email = "' . $email . '" AND auth = "' . $auth . '"';
  167. return (bool)$db->QueryItem($sql);
  168. }
  169. public static function sendJob($jobId)
  170. {
  171. global $db;
  172. $job = new Job($jobId);
  173. $subscribers = self::getCategorySubscribers($job->mCategoryId);
  174. $postman = new Postman();
  175. foreach ($subscribers as $subscriber)
  176. {
  177. if (!empty($subscriber['keywords']))
  178. {
  179. $keywords = '/\b(' . str_replace(',', '|', preg_quote($subscriber['keywords'])) . ')\b/i';
  180. if (!(preg_match($keywords, $job->mTitle) || preg_match($keywords, $job->mSummary) || preg_match($keywords, strip_tags($job->mDescription))))
  181. {
  182. continue;
  183. }
  184. }
  185. $postman->MailSubscriptionJobPosted($subscriber['email'], $subscriber['auth'], $job);
  186. $sql = 'INSERT INTO '.DB_PREFIX.'subscriber_mail_log (email, job_id, job_title, job_summary, date)
  187. VALUES ("'.$subscriber['email'].'", '.$jobId.', "'.$job->mTitle.'", "'.$job->mSummary.'", NOW())';
  188. $db->Execute($sql);
  189. }
  190. }
  191. public static function getCategorySubscribers($categoryId, $includeImplicit=true)
  192. {
  193. global $db;
  194. if ($includeImplicit)
  195. {
  196. $categoryFilter = 'b.category_id IN (0,' . $categoryId . ')';
  197. }
  198. else
  199. {
  200. $categoryFilter = 'b.category_id = ' . $categoryId;
  201. }
  202. $sql = 'SELECT DISTINCT a.id as id, a.email as email, a.auth as auth, a.keywords as keywords
  203. FROM '.DB_PREFIX.'subscribers a, '.DB_PREFIX.'subscriptions b
  204. WHERE a.id = b.subscriber_id
  205. AND b.confirmed = 1
  206. AND ' . $categoryFilter;
  207. if ($tmpResult = $db->QueryArray($sql))
  208. {
  209. $result = array();
  210. foreach ($tmpResult as $subscriber)
  211. {
  212. $result[$subscriber['id']] = $subscriber;
  213. }
  214. return $result;
  215. }
  216. return false;
  217. }
  218. protected static function generateAuthCode()
  219. {
  220. $auth = md5(uniqid() . time());
  221. return $auth;
  222. }
  223. }
  224. ?>