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

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