BioTorrents.de’s version of Gazelle
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

notificationsmanager.class.php 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. <?php
  2. class NotificationsManager
  3. {
  4. // Option types
  5. const OPT_DISABLED = 0;
  6. const OPT_POPUP = 1;
  7. const OPT_TRADITIONAL = 2;
  8. // Importances
  9. const IMPORTANT = 'information';
  10. const CRITICAL = 'error';
  11. const WARNING = 'warning';
  12. const INFO = 'confirmation';
  13. public static $Importances = array(
  14. 'important' => self::IMPORTANT,
  15. 'critical' => self::CRITICAL,
  16. 'warning' => self::WARNING,
  17. 'info' => self::INFO
  18. );
  19. // Types. These names must correspond to column names in users_notifications_settings
  20. const NEWS = 'News';
  21. const BLOG = 'Blog';
  22. const STAFFBLOG = 'StaffBlog';
  23. const STAFFPM = 'StaffPM';
  24. const INBOX = 'Inbox';
  25. const QUOTES = 'Quotes';
  26. const SUBSCRIPTIONS = 'Subscriptions';
  27. const TORRENTS = 'Torrents';
  28. const COLLAGES = 'Collages';
  29. const SITEALERTS = 'SiteAlerts';
  30. const FORUMALERTS = 'ForumAlerts';
  31. const REQUESTALERTS = 'RequestAlerts';
  32. const COLLAGEALERTS = 'CollageAlerts';
  33. const TORRENTALERTS = 'TorrentAlerts';
  34. const GLOBALNOTICE = 'Global';
  35. public static $Types = array(
  36. 'News',
  37. 'Blog',
  38. 'StaffPM',
  39. 'Inbox',
  40. 'Quotes',
  41. 'Subscriptions',
  42. 'Torrents',
  43. 'Collages',
  44. 'SiteAlerts',
  45. 'ForumAlerts',
  46. 'RequestAlerts',
  47. 'CollageAlerts',
  48. 'TorrentAlerts'
  49. );
  50. private $UserID;
  51. private $Notifications;
  52. private $Settings;
  53. private $Skipped;
  54. public function __construct($UserID, $Skip = [], $Load = true, $AutoSkip = true)
  55. {
  56. $this->UserID = $UserID;
  57. $this->Notifications = [];
  58. $this->Settings = self::get_settings($UserID);
  59. $this->Skipped = $Skip;
  60. if ($AutoSkip) {
  61. foreach ($this->Settings as $Key => $Value) {
  62. // Skip disabled and traditional settings
  63. if ($Value == self::OPT_DISABLED || $this->is_traditional($Key)) {
  64. $this->Skipped[$Key] = true;
  65. }
  66. }
  67. }
  68. if ($Load) {
  69. $this->load_global_notification();
  70. if (!isset($this->Skipped[self::NEWS])) {
  71. $this->load_news();
  72. }
  73. if (!isset($this->Skipped[self::BLOG])) {
  74. $this->load_blog();
  75. }
  76. // if (!isset($this->Skipped[self::STAFFBLOG])) {
  77. // $this->load_staff_blog();
  78. // }
  79. if (!isset($this->Skipped[self::STAFFPM])) {
  80. $this->load_staff_pms();
  81. }
  82. if (!isset($this->Skipped[self::INBOX])) {
  83. $this->load_inbox();
  84. }
  85. if (!isset($this->Skipped[self::TORRENTS])) {
  86. $this->load_torrent_notifications();
  87. }
  88. if (!isset($this->Skipped[self::COLLAGES])) {
  89. $this->load_collage_subscriptions();
  90. }
  91. if (!isset($this->Skipped[self::QUOTES])) {
  92. $this->load_quote_notifications();
  93. }
  94. if (!isset($this->Skipped[self::SUBSCRIPTIONS])) {
  95. $this->load_subscriptions();
  96. }
  97. // $this->load_one_reads(); // The code that sets these notices is commented out.
  98. }
  99. }
  100. public function get_notifications()
  101. {
  102. return $this->Notifications;
  103. }
  104. public function clear_notifications_array()
  105. {
  106. unset($this->Notifications);
  107. $this->Notifications = [];
  108. }
  109. private function create_notification($Type, $ID, $Message, $URL, $Importance)
  110. {
  111. $this->Notifications[$Type] = array(
  112. 'id' => (int)$ID,
  113. 'message' => $Message,
  114. 'url' => $URL,
  115. 'importance' => $Importance
  116. );
  117. }
  118. public static function notify_user($UserID, $Type, $Message, $URL, $Importance)
  119. {
  120. self::notify_users(array($UserID), $Type, $Message, $URL, $Importance);
  121. }
  122. public static function notify_users($UserIDs, $Type, $Message, $URL, $Importance)
  123. {
  124. /*
  125. if (!isset($Importance)) {
  126. $Importance = self::INFO;
  127. }
  128. $Type = db_string($Type);
  129. if (!empty($UserIDs)) {
  130. $UserIDs = implode(',', $UserIDs);
  131. $QueryID = G::$DB->get_query_id();
  132. G::$DB->query("
  133. SELECT UserID
  134. FROM users_notifications_settings
  135. WHERE $Type != 0
  136. AND UserID IN ($UserIDs)");
  137. $UserIDs = [];
  138. while (list($ID) = G::$DB->next_record()) {
  139. $UserIDs[] = $ID;
  140. }
  141. G::$DB->set_query_id($QueryID);
  142. foreach ($UserIDs as $UserID) {
  143. $OneReads = G::$Cache->get_value("notifications_one_reads_$UserID");
  144. if (!$OneReads) {
  145. $OneReads = [];
  146. }
  147. array_unshift($OneReads, $this->create_notification($OneReads, "oneread_" . uniqid(), null, $Message, $URL, $Importance));
  148. $OneReads = array_filter($OneReads);
  149. G::$Cache->cache_value("notifications_one_reads_$UserID", $OneReads, 0);
  150. }
  151. }
  152. */
  153. }
  154. public static function get_notification_enabled_users($Type, $UserID)
  155. {
  156. $Type = db_string($Type);
  157. $UserWhere = '';
  158. if (isset($UserID)) {
  159. $UserID = (int)$UserID;
  160. $UserWhere = " AND UserID = '$UserID'";
  161. }
  162. $QueryID = G::$DB->get_query_id();
  163. G::$DB->query("
  164. SELECT UserID
  165. FROM users_notifications_settings
  166. WHERE $Type != 0
  167. $UserWhere");
  168. $IDs = [];
  169. while (list($ID) = G::$DB->next_record()) {
  170. $IDs[] = $ID;
  171. }
  172. G::$DB->set_query_id($QueryID);
  173. return $IDs;
  174. }
  175. public function load_one_reads()
  176. {
  177. $OneReads = G::$Cache->get_value('notifications_one_reads_' . G::$LoggedUser['ID']);
  178. if (is_array($OneReads)) {
  179. $this->Notifications = $this->Notifications + $OneReads;
  180. }
  181. }
  182. public static function clear_one_read($ID)
  183. {
  184. $OneReads = G::$Cache->get_value('notifications_one_reads_' . G::$LoggedUser['ID']);
  185. if ($OneReads) {
  186. unset($OneReads[$ID]);
  187. if (count($OneReads) > 0) {
  188. G::$Cache->cache_value('notifications_one_reads_' . G::$LoggedUser['ID'], $OneReads, 0);
  189. } else {
  190. G::$Cache->delete_value('notifications_one_reads_' . G::$LoggedUser['ID']);
  191. }
  192. }
  193. }
  194. public function load_global_notification()
  195. {
  196. $GlobalNotification = G::$Cache->get_value('global_notification');
  197. if ($GlobalNotification) {
  198. $Read = G::$Cache->get_value('user_read_global_' . G::$LoggedUser['ID']);
  199. if (!$Read) {
  200. $this->create_notification(self::GLOBALNOTICE, 0, $GlobalNotification['Message'], $GlobalNotification['URL'], $GlobalNotification['Importance']);
  201. }
  202. }
  203. }
  204. public static function get_global_notification()
  205. {
  206. return G::$Cache->get_value('global_notification');
  207. }
  208. public static function set_global_notification($Message, $URL, $Importance, $Expiration)
  209. {
  210. if (empty($Message) || empty($Expiration)) {
  211. error('Error setting notification');
  212. }
  213. G::$Cache->cache_value('global_notification', array("Message" => $Message, "URL" => $URL, "Importance" => $Importance, "Expiration" => $Expiration), $Expiration);
  214. }
  215. public static function delete_global_notification()
  216. {
  217. G::$Cache->delete_value('global_notification');
  218. }
  219. public static function clear_global_notification()
  220. {
  221. $GlobalNotification = G::$Cache->get_value('global_notification');
  222. if ($GlobalNotification) {
  223. // This is some trickery
  224. // since we can't know which users have the read cache key set
  225. // we set the expiration time of their cache key to that of the length of the notification
  226. // this gaurantees that their cache key will expire after the notification expires
  227. G::$Cache->cache_value('user_read_global_' . G::$LoggedUser['ID'], true, $GlobalNotification['Expiration']);
  228. }
  229. }
  230. public function load_news()
  231. {
  232. $MyNews = G::$LoggedUser['LastReadNews'];
  233. $CurrentNews = G::$Cache->get_value('news_latest_id');
  234. $Title = G::$Cache->get_value('news_latest_title');
  235. if ($CurrentNews === false || $Title === false) {
  236. $QueryID = G::$DB->get_query_id();
  237. G::$DB->query('
  238. SELECT ID, Title
  239. FROM news
  240. ORDER BY Time DESC
  241. LIMIT 1');
  242. if (G::$DB->has_results()) {
  243. list($CurrentNews, $Title) = G::$DB->next_record();
  244. } else {
  245. $CurrentNews = -1;
  246. }
  247. G::$DB->set_query_id($QueryID);
  248. G::$Cache->cache_value('news_latest_id', $CurrentNews, 0);
  249. G::$Cache->cache_value('news_latest_title', $Title, 0);
  250. }
  251. if ($MyNews < $CurrentNews) {
  252. $this->create_notification(self::NEWS, $CurrentNews, "Announcement: $Title", "index.php#news$CurrentNews", self::IMPORTANT);
  253. }
  254. }
  255. public function load_blog()
  256. {
  257. $MyBlog = G::$LoggedUser['LastReadBlog'];
  258. $CurrentBlog = G::$Cache->get_value('blog_latest_id');
  259. $Title = G::$Cache->get_value('blog_latest_title');
  260. if ($CurrentBlog === false) {
  261. $QueryID = G::$DB->get_query_id();
  262. G::$DB->query('
  263. SELECT ID, Title
  264. FROM blog
  265. WHERE Important = 1
  266. ORDER BY Time DESC
  267. LIMIT 1');
  268. if (G::$DB->has_results()) {
  269. list($CurrentBlog, $Title) = G::$DB->next_record();
  270. } else {
  271. $CurrentBlog = -1;
  272. }
  273. G::$DB->set_query_id($QueryID);
  274. G::$Cache->cache_value('blog_latest_id', $CurrentBlog, 0);
  275. G::$Cache->cache_value('blog_latest_title', $Title, 0);
  276. }
  277. if ($MyBlog < $CurrentBlog) {
  278. $this->create_notification(self::BLOG, $CurrentBlog, "Blog: $Title", "blog.php#blog$CurrentBlog", self::IMPORTANT);
  279. }
  280. }
  281. public function load_staff_blog()
  282. {
  283. if (check_perms('users_mod')) {
  284. global $SBlogReadTime, $LatestSBlogTime;
  285. if (!$SBlogReadTime && ($SBlogReadTime = G::$Cache->get_value('staff_blog_read_' . G::$LoggedUser['ID'])) === false) {
  286. $QueryID = G::$DB->get_query_id();
  287. G::$DB->query("
  288. SELECT Time
  289. FROM staff_blog_visits
  290. WHERE UserID = " . G::$LoggedUser['ID']);
  291. if (list($SBlogReadTime) = G::$DB->next_record()) {
  292. $SBlogReadTime = strtotime($SBlogReadTime);
  293. } else {
  294. $SBlogReadTime = 0;
  295. }
  296. G::$DB->set_query_id($QueryID);
  297. G::$Cache->cache_value('staff_blog_read_' . G::$LoggedUser['ID'], $SBlogReadTime, 1209600);
  298. }
  299. if (!$LatestSBlogTime && ($LatestSBlogTime = G::$Cache->get_value('staff_blog_latest_time')) === false) {
  300. $QueryID = G::$DB->get_query_id();
  301. G::$DB->query('
  302. SELECT MAX(Time)
  303. FROM staff_blog');
  304. if (list($LatestSBlogTime) = G::$DB->next_record()) {
  305. $LatestSBlogTime = strtotime($LatestSBlogTime);
  306. } else {
  307. $LatestSBlogTime = 0;
  308. }
  309. G::$DB->set_query_id($QueryID);
  310. G::$Cache->cache_value('staff_blog_latest_time', $LatestSBlogTime, 1209600);
  311. }
  312. if ($SBlogReadTime < $LatestSBlogTime) {
  313. $this->create_notification(self::STAFFBLOG, 0, 'New Staff Blog Post!', 'staffblog.php', self::IMPORTANT);
  314. }
  315. }
  316. }
  317. public function load_staff_pms()
  318. {
  319. $NewStaffPMs = G::$Cache->get_value('staff_pm_new_' . G::$LoggedUser['ID']);
  320. if ($NewStaffPMs === false) {
  321. $QueryID = G::$DB->get_query_id();
  322. G::$DB->query("
  323. SELECT COUNT(ID)
  324. FROM staff_pm_conversations
  325. WHERE UserID = '" . G::$LoggedUser['ID'] . "'
  326. AND Unread = '1'");
  327. list($NewStaffPMs) = G::$DB->next_record();
  328. G::$DB->set_query_id($QueryID);
  329. G::$Cache->cache_value('staff_pm_new_' . G::$LoggedUser['ID'], $NewStaffPMs, 0);
  330. }
  331. if ($NewStaffPMs > 0) {
  332. $Title = 'You have ' . ($NewStaffPMs == 1 ? 'a' : $NewStaffPMs) . ' new Staff PM' . ($NewStaffPMs > 1 ? 's' : '');
  333. $this->create_notification(self::STAFFPM, 0, $Title, 'staffpm.php', self::INFO);
  334. }
  335. }
  336. public function load_inbox()
  337. {
  338. $NewMessages = G::$Cache->get_value('inbox_new_' . G::$LoggedUser['ID']);
  339. if ($NewMessages === false) {
  340. $QueryID = G::$DB->get_query_id();
  341. G::$DB->query("
  342. SELECT COUNT(UnRead)
  343. FROM pm_conversations_users
  344. WHERE UserID = '" . G::$LoggedUser['ID'] . "'
  345. AND UnRead = '1'
  346. AND InInbox = '1'");
  347. list($NewMessages) = G::$DB->next_record();
  348. G::$DB->set_query_id($QueryID);
  349. G::$Cache->cache_value('inbox_new_' . G::$LoggedUser['ID'], $NewMessages, 0);
  350. }
  351. if ($NewMessages > 0) {
  352. $Title = 'You have ' . ($NewMessages == 1 ? 'a' : $NewMessages) . ' new message' . ($NewMessages > 1 ? 's' : '');
  353. $this->create_notification(self::INBOX, 0, $Title, Inbox::get_inbox_link(), self::INFO);
  354. }
  355. }
  356. public function load_torrent_notifications()
  357. {
  358. if (check_perms('site_torrents_notify')) {
  359. $NewNotifications = G::$Cache->get_value('notifications_new_' . G::$LoggedUser['ID']);
  360. if ($NewNotifications === false) {
  361. $QueryID = G::$DB->get_query_id();
  362. G::$DB->query("
  363. SELECT COUNT(UserID)
  364. FROM users_notify_torrents
  365. WHERE UserID = ' " . G::$LoggedUser['ID'] . "'
  366. AND UnRead = '1'");
  367. list($NewNotifications) = G::$DB->next_record();
  368. G::$DB->set_query_id($QueryID);
  369. G::$Cache->cache_value('notifications_new_' . G::$LoggedUser['ID'], $NewNotifications, 0);
  370. }
  371. }
  372. if (isset($NewNotifications) && $NewNotifications > 0) {
  373. $Title = 'You have ' . ($NewNotifications == 1 ? 'a' : $NewNotifications) . ' new torrent notification' . ($NewNotifications > 1 ? 's' : '');
  374. $this->create_notification(self::TORRENTS, 0, $Title, 'torrents.php?action=notify', self::INFO);
  375. }
  376. }
  377. public function load_collage_subscriptions()
  378. {
  379. if (check_perms('site_collages_subscribe')) {
  380. $NewCollages = G::$Cache->get_value('collage_subs_user_new_' . G::$LoggedUser['ID']);
  381. if ($NewCollages === false) {
  382. $QueryID = G::$DB->get_query_id();
  383. G::$DB->query("
  384. SELECT COUNT(DISTINCT s.CollageID)
  385. FROM users_collage_subs AS s
  386. JOIN collages AS c ON s.CollageID = c.ID
  387. JOIN collages_torrents AS ct ON ct.CollageID = c.ID
  388. WHERE s.UserID = " . G::$LoggedUser['ID'] . "
  389. AND ct.AddedOn > s.LastVisit
  390. AND c.Deleted = '0'");
  391. list($NewCollages) = G::$DB->next_record();
  392. G::$DB->set_query_id($QueryID);
  393. G::$Cache->cache_value('collage_subs_user_new_' . G::$LoggedUser['ID'], $NewCollages, 0);
  394. }
  395. if ($NewCollages > 0) {
  396. $Title = 'You have ' . ($NewCollages == 1 ? 'a' : $NewCollages) . ' new collage update' . ($NewCollages > 1 ? 's' : '');
  397. $this->create_notification(self::COLLAGES, 0, $Title, 'userhistory.php?action=subscribed_collages', self::INFO);
  398. }
  399. }
  400. }
  401. public function load_quote_notifications()
  402. {
  403. if (isset(G::$LoggedUser['NotifyOnQuote']) && G::$LoggedUser['NotifyOnQuote']) {
  404. $QuoteNotificationsCount = Subscriptions::has_new_quote_notifications();
  405. if ($QuoteNotificationsCount > 0) {
  406. $Title = 'New quote' . ($QuoteNotificationsCount > 1 ? 's' : '');
  407. $this->create_notification(self::QUOTES, 0, $Title, 'userhistory.php?action=quote_notifications', self::INFO);
  408. }
  409. }
  410. }
  411. public function load_subscriptions()
  412. {
  413. $SubscriptionsCount = Subscriptions::has_new_subscriptions();
  414. if ($SubscriptionsCount > 0) {
  415. $Title = 'New subscription' . ($SubscriptionsCount > 1 ? 's' : '');
  416. $this->create_notification(self::SUBSCRIPTIONS, 0, $Title, 'userhistory.php?action=subscriptions', self::INFO);
  417. }
  418. }
  419. public static function clear_news($News)
  420. {
  421. $QueryID = G::$DB->get_query_id();
  422. if (!$News) {
  423. if (!$News = G::$Cache->get_value('news')) {
  424. G::$DB->query('
  425. SELECT
  426. ID,
  427. Title,
  428. Body,
  429. Time
  430. FROM news
  431. ORDER BY Time DESC
  432. LIMIT 1');
  433. $News = G::$DB->to_array(false, MYSQLI_NUM, false);
  434. G::$Cache->cache_value('news_latest_id', $News[0][0], 0);
  435. }
  436. }
  437. if (G::$LoggedUser['LastReadNews'] != $News[0][0]) {
  438. G::$Cache->begin_transaction('user_info_heavy_' . G::$LoggedUser['ID']);
  439. G::$Cache->update_row(false, array('LastReadNews' => $News[0][0]));
  440. G::$Cache->commit_transaction(0);
  441. G::$DB->query("
  442. UPDATE users_info
  443. SET LastReadNews = '".$News[0][0]."'
  444. WHERE UserID = " . G::$LoggedUser['ID']);
  445. G::$LoggedUser['LastReadNews'] = $News[0][0];
  446. }
  447. G::$DB->set_query_id($QueryID);
  448. }
  449. public static function clear_blog($Blog)
  450. {
  451. $QueryID = G::$DB->get_query_id();
  452. if (!isset($Blog) || !$Blog) {
  453. if (!$Blog = G::$Cache->get_value('blog')) {
  454. G::$DB->query("
  455. SELECT
  456. b.ID,
  457. um.Username,
  458. b.UserID,
  459. b.Title,
  460. b.Body,
  461. b.Time,
  462. b.ThreadID
  463. FROM blog AS b
  464. LEFT JOIN users_main AS um ON b.UserID = um.ID
  465. ORDER BY Time DESC
  466. LIMIT 1");
  467. $Blog = G::$DB->to_array();
  468. }
  469. }
  470. if (G::$LoggedUser['LastReadBlog'] < $Blog[0][0]) {
  471. G::$Cache->begin_transaction('user_info_heavy_' . G::$LoggedUser['ID']);
  472. G::$Cache->update_row(false, array('LastReadBlog' => $Blog[0][0]));
  473. G::$Cache->commit_transaction(0);
  474. G::$DB->query("
  475. UPDATE users_info
  476. SET LastReadBlog = '". $Blog[0][0]."'
  477. WHERE UserID = " . G::$LoggedUser['ID']);
  478. G::$LoggedUser['LastReadBlog'] = $Blog[0][0];
  479. }
  480. G::$DB->set_query_id($QueryID);
  481. }
  482. public static function clear_staff_pms()
  483. {
  484. $QueryID = G::$DB->get_query_id();
  485. G::$DB->query("
  486. SELECT ID
  487. FROM staff_pm_conversations
  488. WHERE Unread = true
  489. AND UserID = " . G::$LoggedUser['ID']);
  490. $IDs = [];
  491. while (list($ID) = G::$DB->next_record()) {
  492. $IDs[] = $ID;
  493. }
  494. $IDs = implode(',', $IDs);
  495. if (!empty($IDs)) {
  496. G::$DB->query("
  497. UPDATE staff_pm_conversations
  498. SET Unread = false
  499. WHERE ID IN ($IDs)");
  500. }
  501. G::$Cache->delete_value('staff_pm_new_' . G::$LoggedUser['ID']);
  502. G::$DB->set_query_id($QueryID);
  503. }
  504. public static function clear_inbox()
  505. {
  506. $QueryID = G::$DB->get_query_id();
  507. G::$DB->query("
  508. SELECT ConvID
  509. FROM pm_conversations_users
  510. WHERE Unread = '1'
  511. AND UserID = " . G::$LoggedUser['ID']);
  512. $IDs = [];
  513. while (list($ID) = G::$DB->next_record()) {
  514. $IDs[] = $ID;
  515. }
  516. $IDs = implode(',', $IDs);
  517. if (!empty($IDs)) {
  518. G::$DB->query("
  519. UPDATE pm_conversations_users
  520. SET Unread = '0'
  521. WHERE ConvID IN ($IDs)
  522. AND UserID = " . G::$LoggedUser['ID']);
  523. }
  524. G::$Cache->delete_value('inbox_new_' . G::$LoggedUser['ID']);
  525. G::$DB->set_query_id($QueryID);
  526. }
  527. public static function clear_torrents()
  528. {
  529. $QueryID = G::$DB->get_query_id();
  530. G::$DB->query("
  531. SELECT TorrentID
  532. FROM users_notify_torrents
  533. WHERE UserID = ' " . G::$LoggedUser['ID'] . "'
  534. AND UnRead = '1'");
  535. $IDs = [];
  536. while (list($ID) = G::$DB->next_record()) {
  537. $IDs[] = $ID;
  538. }
  539. $IDs = implode(',', $IDs);
  540. if (!empty($IDs)) {
  541. G::$DB->query("
  542. UPDATE users_notify_torrents
  543. SET Unread = '0'
  544. WHERE TorrentID IN ($IDs)
  545. AND UserID = " . G::$LoggedUser['ID']);
  546. }
  547. G::$Cache->delete_value('notifications_new_' . G::$LoggedUser['ID']);
  548. G::$DB->set_query_id($QueryID);
  549. }
  550. public static function clear_collages()
  551. {
  552. $QueryID = G::$DB->get_query_id();
  553. G::$DB->query("
  554. UPDATE users_collage_subs
  555. SET LastVisit = NOW()
  556. WHERE UserID = " . G::$LoggedUser['ID']);
  557. G::$Cache->delete_value('collage_subs_user_new_' . G::$LoggedUser['ID']);
  558. G::$DB->set_query_id($QueryID);
  559. }
  560. public static function clear_quotes()
  561. {
  562. $QueryID = G::$DB->get_query_id();
  563. G::$DB->query("
  564. UPDATE users_notify_quoted
  565. SET UnRead = '0'
  566. WHERE UserID = " . G::$LoggedUser['ID']);
  567. G::$Cache->delete_value('notify_quoted_' . G::$LoggedUser['ID']);
  568. G::$DB->set_query_id($QueryID);
  569. }
  570. public static function clear_subscriptions()
  571. {
  572. $QueryID = G::$DB->get_query_id();
  573. if (($UserSubscriptions = G::$Cache->get_value('subscriptions_user_' . G::$LoggedUser['ID'])) === false) {
  574. G::$DB->query("
  575. SELECT TopicID
  576. FROM users_subscriptions
  577. WHERE UserID = " . G::$LoggedUser['ID']);
  578. if ($UserSubscriptions = G::$DB->collect(0)) {
  579. G::$Cache->cache_value('subscriptions_user_' . G::$LoggedUser['ID'], $UserSubscriptions, 0);
  580. }
  581. }
  582. if (!empty($UserSubscriptions)) {
  583. G::$DB->query("
  584. INSERT INTO forums_last_read_topics (UserID, TopicID, PostID)
  585. SELECT '" . G::$LoggedUser['ID'] . "', ID, LastPostID
  586. FROM forums_topics
  587. WHERE ID IN (".implode(',', $UserSubscriptions).')
  588. ON DUPLICATE KEY UPDATE
  589. PostID = LastPostID');
  590. }
  591. G::$Cache->delete_value('subscriptions_user_new_' . G::$LoggedUser['ID']);
  592. G::$DB->set_query_id($QueryID);
  593. }
  594. /*
  595. // todo: Figure out what these functions are supposed to do and fix them
  596. public static function send_notification($UserID, $ID, $Type, $Message, $URL, $Importance = 'alert', $AutoExpire = false) {
  597. $Notifications = G::$Cache->get_value("user_cache_notifications_$UserID");
  598. if (empty($Notifications)) {
  599. $Notifications = [];
  600. }
  601. array_unshift($Notifications, $this->create_notification($Type, $ID, $Message, $URL, $Importance, $AutoExpire));
  602. G::$Cache->cache_value("user_cache_notifications_$UserID", $Notifications, 0);
  603. }
  604. public static function clear_notification($UserID, $Index) {
  605. $Notifications = G::$Cache->get_value("user_cache_notifications_$UserID");
  606. if (count($Notifications)) {
  607. unset($Notifications[$Index]);
  608. $Notifications = array_values($Notifications);
  609. G::$Cache->cache_value("user_cache_notifications_$UserID", $Notifications, 0);
  610. }
  611. }
  612. */
  613. public static function get_settings($UserID)
  614. {
  615. $Results = G::$Cache->get_value("users_notifications_settings_$UserID");
  616. if (!$Results) {
  617. $QueryID = G::$DB->get_query_id();
  618. G::$DB->query("
  619. SELECT *
  620. FROM users_notifications_settings
  621. WHERE UserID = ?", $UserID);
  622. $Results = G::$DB->next_record(MYSQLI_ASSOC, false);
  623. G::$DB->set_query_id($QueryID);
  624. G::$Cache->cache_value("users_notifications_settings_$UserID", $Results, 0);
  625. }
  626. return $Results;
  627. }
  628. public static function save_settings($UserID, $Settings = false)
  629. {
  630. if (!is_array($Settings)) {
  631. // A little cheat technique, gets all keys in the $_POST array starting with 'notifications_'
  632. $Settings = array_intersect_key($_POST, array_flip(preg_grep('/^notifications_/', array_keys($_POST))));
  633. }
  634. $Update = [];
  635. foreach (self::$Types as $Type) {
  636. $Popup = array_key_exists("notifications_{$Type}_popup", $Settings);
  637. $Traditional = array_key_exists("notifications_{$Type}_traditional", $Settings);
  638. $Result = self::OPT_DISABLED;
  639. if ($Popup) {
  640. $Result = self::OPT_POPUP;
  641. } elseif ($Traditional) {
  642. $Result = self::OPT_TRADITIONAL;
  643. }
  644. $Update[] = "$Type = $Result";
  645. }
  646. $Update = implode(',', $Update);
  647. $QueryID = G::$DB->get_query_id();
  648. G::$DB->query("
  649. UPDATE users_notifications_settings
  650. SET $Update
  651. WHERE UserID = ?", $UserID);
  652. G::$DB->set_query_id($QueryID);
  653. G::$Cache->delete_value("users_notifications_settings_$UserID");
  654. }
  655. public function is_traditional($Type)
  656. {
  657. return $this->Settings[$Type] == self::OPT_TRADITIONAL;
  658. }
  659. public function is_skipped($Type)
  660. {
  661. return isset($this->Skipped[$Type]);
  662. }
  663. public function use_noty()
  664. {
  665. return in_array(self::OPT_POPUP, $this->Settings);
  666. }
  667. }