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

/titania/includes/tools/subscriptions.php

http://github.com/phpbb/customisation-db
PHP | 297 lines | 177 code | 43 blank | 77 comment | 26 complexity | dfccc4614d5856f2a2a81cfe39ce897f MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /**
  3. *
  4. * @package Titania
  5. * @copyright (c) 2008 phpBB Customisation Database Team
  6. * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License, version 2
  7. *
  8. */
  9. if (!defined('IN_TITANIA'))
  10. {
  11. exit;
  12. }
  13. // These lines can be removed/commented out when we get it integrated.
  14. define('SUBSCRIPTION_EMAIL', 1);
  15. define('SUBSCRIPTION_WATCH', 2);
  16. class titania_subscriptions
  17. {
  18. /**
  19. * Shorten the amount of code required for some places
  20. *
  21. * @param mixed $object_type
  22. * @param mixed $object_id
  23. * @param mixed $url
  24. */
  25. public static function handle_subscriptions($object_type, $object_id, $url)
  26. {
  27. if (!phpbb::$user->data['is_registered'])
  28. {
  29. // Cannot currently handle non-registered users
  30. return;
  31. }
  32. $subscribe = request_var('subscribe', '');
  33. if ($subscribe == 'subscribe' && check_link_hash(request_var('hash', ''), 'subscribe'))
  34. {
  35. titania_subscriptions::subscribe($object_type, $object_id);
  36. }
  37. else if ($subscribe == 'unsubscribe' && check_link_hash(request_var('hash', ''), 'unsubscribe'))
  38. {
  39. titania_subscriptions::unsubscribe($object_type, $object_id);
  40. }
  41. if (titania_subscriptions::is_subscribed($object_type, $object_id))
  42. {
  43. phpbb::$template->assign_vars(array(
  44. 'IS_SUBSCRIBED' => true,
  45. 'U_SUBSCRIBE' => titania_url::append_url($url, array('subscribe' => 'unsubscribe', 'hash' => generate_link_hash('unsubscribe'))),
  46. ));
  47. }
  48. else
  49. {
  50. phpbb::$template->assign_vars(array(
  51. 'U_SUBSCRIBE' => titania_url::append_url($url, array('subscribe' => 'subscribe', 'hash' => generate_link_hash('subscribe'))),
  52. ));
  53. }
  54. }
  55. /*
  56. * Is Subscribed
  57. */
  58. public static function is_subscribed($object_type, $object_id, $user_id = false)
  59. {
  60. $user_id = ($user_id === false) ? phpbb::$user->data['user_id'] : $user_id;
  61. $sql = 'SELECT watch_object_id FROM ' . TITANIA_WATCH_TABLE . ' WHERE ' . phpbb::$db->sql_build_array('SELECT', array(
  62. 'watch_object_type' => (int) $object_type,
  63. 'watch_object_id' => (int) $object_id,
  64. 'watch_user_id' => (int) $user_id,
  65. ));
  66. $result = phpbb::$db->sql_query($sql);
  67. $row = phpbb::$db->sql_fetchrow($result);
  68. phpbb::$db->sql_freeresult($result);
  69. return ($row) ? true : false;
  70. }
  71. /*
  72. * Subscribe
  73. */
  74. public static function subscribe($object_type, $object_id, $user_id = false, $subscription_type = SUBSCRIPTION_EMAIL)
  75. {
  76. $user_id = ($user_id === false) ? phpbb::$user->data['user_id'] : $user_id;
  77. if(self::is_subscribed($object_type, $object_id, $user_id))
  78. {
  79. return false;
  80. }
  81. // Build an insert
  82. $sql = 'INSERT INTO ' . TITANIA_WATCH_TABLE . ' ' . phpbb::$db->sql_build_array('INSERT', array(
  83. 'watch_object_type' => (int) $object_type,
  84. 'watch_type' => (int) $subscription_type,
  85. 'watch_object_id' => (int) $object_id,
  86. 'watch_user_id' => (int) $user_id,
  87. 'watch_mark_time' => time(),
  88. ));
  89. // Query and we're done
  90. phpbb::$db->sql_query($sql);
  91. return true;
  92. }
  93. /*
  94. * Unsubscribe
  95. */
  96. public static function unsubscribe($object_type, $object_id, $user_id = false)
  97. {
  98. $user_id = ($user_id === false) ? phpbb::$user->data['user_id'] : $user_id;
  99. // Get our delete query
  100. $sql = 'DELETE FROM ' . TITANIA_WATCH_TABLE . "
  101. WHERE watch_object_id = " . (int) $object_id . '
  102. AND watch_user_id = ' .(int) $user_id . '
  103. AND watch_object_type = ' . (int) $object_type;
  104. // Query and we're done
  105. phpbb::$db->sql_query($sql);
  106. return true;
  107. }
  108. /**
  109. * Send Notifications
  110. *
  111. * Using this function:
  112. * Call this function when you know the Object type, object id, and the email
  113. * template name.
  114. * Sample usage:
  115. *
  116. * <code>
  117. *
  118. * $object_type = SOME_OBJECT_CONSTANT_TYPE;
  119. * $obhect)id = 242974;
  120. *
  121. * titania_subscriptions::send_notifications($object_type, $object_id, 'mod_subscribe', array(
  122. * 'OBJECT_NAME' => 'Some MOD',
  123. * ));
  124. *
  125. * </code>
  126. *
  127. * The vars parameter will be used in the messanger assign vars, which will act
  128. * as the common vars when sending out the notifications. Data such as the MOD's
  129. * or Style's name should go here, what action was taken, etc. The usernaeme and
  130. * emails of the recepiants will be personalised by the function. Ensure the
  131. * email template has the {USERNAME} var present.
  132. *
  133. * @param $exclude_user User_id of the one who posted the item to exclude them from the sending
  134. *
  135. */
  136. public static function send_notifications($object_type, $object_id, $email_tpl, $vars, $exclude_user = false)
  137. {
  138. $sql = 'SELECT w.watch_user_id, w.watch_type, u.user_id, u.username, u.user_email, u.user_lang
  139. FROM ' . TITANIA_WATCH_TABLE . ' w, ' . USERS_TABLE . ' u
  140. WHERE w.watch_user_id = u.user_id ';
  141. if (is_array($object_type) || is_array($object_id))
  142. {
  143. // Both needs to be arrays if one is and they need to have the same number of elements.
  144. if (!is_array($object_type) || !is_array($object_id) || sizeof($object_type) != sizeof($object_id))
  145. {
  146. return;
  147. }
  148. $sql_objects = '';
  149. foreach ($object_type as $key => $value)
  150. {
  151. $sql_objects .= (($sql_objects == '') ? '' : ' OR ') . '(w.watch_object_type = ' . (int) $value . '
  152. AND w.watch_object_id = ' . (int) $object_id[$key] . ')';
  153. }
  154. $sql .= 'AND (' . $sql_objects . ')';
  155. unset($sql_objects);
  156. }
  157. else
  158. {
  159. $sql .= 'AND w.watch_object_type = ' . (int) $object_type . '
  160. AND w.watch_object_id = ' . (int) $object_id;
  161. }
  162. $sql .= ($exclude_user) ? ' AND w.watch_user_id <> ' . (int) $exclude_user : '';
  163. $result = phpbb::$db->sql_query($sql);
  164. // Throw everything here
  165. $user_data = array();
  166. while($row = phpbb::$db->sql_fetchrow($result))
  167. {
  168. // Use user_id for the keys to not send duplicates.
  169. $user_data[$row['user_id']] = array(
  170. 'username' => $row['username'],
  171. 'user_email' => $row['user_email'],
  172. 'user_lang' => $row['user_lang'],
  173. 'watch_type' => $row['watch_type'],
  174. );
  175. }
  176. // No one subscribed? We're done.
  177. if(!sizeof($user_data))
  178. {
  179. return;
  180. }
  181. // Send to each user
  182. // Add a new case statment for each subscription type
  183. foreach($user_data as $data)
  184. {
  185. // You wanted the email template parsed? Well here you go.
  186. if (file_exists(TITANIA_ROOT . 'language/' . $data['user_lang'] . '/email/' . $email_tpl))
  187. {
  188. $template = file_get_contents(TITANIA_ROOT . 'language/' . $data['user_lang'] . '/email/' . $email_tpl);
  189. }
  190. else if (file_exists(TITANIA_ROOT . 'language/' . phpbb::$config['default_lang'] . '/email/' . $email_tpl))
  191. {
  192. $template = file_get_contents(TITANIA_ROOT . 'language/' . phpbb::$config['default_lang'] . '/email/' . $email_tpl);
  193. }
  194. else
  195. {
  196. $template = file_get_contents(TITANIA_ROOT . 'language/en/email/' . $email_tpl);
  197. }
  198. foreach($vars as $var => $replace)
  199. {
  200. if(strtoupper($var) == 'SUBJECT')
  201. {
  202. continue;
  203. }
  204. $template = str_replace('{' . strtoupper($var) . '}', $replace, $template);
  205. }
  206. // Steal the subject if it exists
  207. $subject = '';
  208. if (($subject_start = strpos($template, 'Subject:')) !== false)
  209. {
  210. $subject_start += strlen('Subject:');
  211. $subject_length = strpos($template, "\n", $subject_start) - $subject_start;
  212. $subject = trim(substr($template, $subject_start, $subject_length));
  213. $template = trim(substr($template, ($subject_start + $subject_length)));
  214. }
  215. $subject = (isset($vars['SUBJECT']) && !$subject) ? $vars['SUBJECT'] : $subject;
  216. $subject = ($subject) ? $subject : phpbb::$user->lang['SUBSCRIPTION_NOTIFICATION'];
  217. // Generic messages that will be sent to each module individually
  218. $message = str_replace('{USERNAME}', $data['username'], $template);
  219. /*
  220. * Switch between the types.
  221. * ------------------------------------------
  222. * When adding a type, the final message will
  223. * be stored in $message, and the subject is
  224. * stored in $vars['SUBJECT'].
  225. */
  226. switch($data['watch_type'])
  227. {
  228. case SUBSCRIPTION_EMAIL:
  229. // Only make the object if we need it
  230. phpbb::_include('functions_messenger', false, 'messenger');
  231. $messenger = new messenger();
  232. $messenger->headers('X-AntiAbuse: Board servername - ' . phpbb::$config['server_name']);
  233. $messenger->headers('X-AntiAbuse: User_id - ' . phpbb::$user->data['user_id']);
  234. $messenger->headers('X-AntiAbuse: Username - ' . phpbb::$user->data['username']);
  235. // HAX
  236. $user_lang_path = phpbb::$user->lang_path;
  237. phpbb::$user->lang_path = TITANIA_ROOT . 'language/';
  238. $messenger->template('subscribe_generic');
  239. // Reverse HAX
  240. phpbb::$user->lang_path = $user_lang_path;
  241. $messenger->to($data['user_email'], $data['username']);
  242. $messenger->assign_vars(array_merge($vars, array(
  243. 'SUBJECT' => $subject,
  244. 'MESSAGE' => $message,
  245. // 'EMAIL_SIG' => '', // @TODO - Email Sig
  246. )));
  247. $messenger->send();
  248. $messenger->save_queue();
  249. break;
  250. }
  251. }
  252. return;
  253. }
  254. }