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

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