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.
 
 
 
 
 

300 lines
8.7 KiB

  1. <?php
  2. namespace dokuwiki\Subscriptions;
  3. use dokuwiki\Extension\AuthPlugin;
  4. use dokuwiki\Input\Input;
  5. use Exception;
  6. class SubscriberManager
  7. {
  8. /**
  9. * Check if subscription system is enabled
  10. *
  11. * @return bool
  12. */
  13. public function isenabled()
  14. {
  15. return actionOK('subscribe');
  16. }
  17. /**
  18. * Adds a new subscription for the given page or namespace
  19. *
  20. * This will automatically overwrite any existent subscription for the given user on this
  21. * *exact* page or namespace. It will *not* modify any subscription that may exist in higher namespaces.
  22. *
  23. * @throws Exception when user or style is empty
  24. *
  25. * @param string $id The target page or namespace, specified by id; Namespaces
  26. * are identified by appending a colon.
  27. * @param string $user
  28. * @param string $style
  29. * @param string $data
  30. *
  31. * @return bool
  32. */
  33. public function add($id, $user, $style, $data = '')
  34. {
  35. if (!$this->isenabled()) {
  36. return false;
  37. }
  38. // delete any existing subscription
  39. $this->remove($id, $user);
  40. $user = auth_nameencode(trim($user));
  41. $style = trim($style);
  42. $data = trim($data);
  43. if (!$user) {
  44. throw new Exception('no subscription user given');
  45. }
  46. if (!$style) {
  47. throw new Exception('no subscription style given');
  48. }
  49. if (!$data) {
  50. $data = time();
  51. } //always add current time for new subscriptions
  52. $line = "$user $style $data\n";
  53. $file = $this->file($id);
  54. return io_saveFile($file, $line, true);
  55. }
  56. /**
  57. * Removes a subscription for the given page or namespace
  58. *
  59. * This removes all subscriptions matching the given criteria on the given page or
  60. * namespace. It will *not* modify any subscriptions that may exist in higher
  61. * namespaces.
  62. *
  63. * @param string $id The target object’s (namespace or page) id
  64. * @param string|array $user
  65. * @param string|array $style
  66. * @param string|array $data
  67. *
  68. * @return bool
  69. * @throws Exception
  70. */
  71. public function remove($id, $user = null, $style = null, $data = null)
  72. {
  73. if (!$this->isenabled()) {
  74. return false;
  75. }
  76. $file = $this->file($id);
  77. if (!file_exists($file)) {
  78. return true;
  79. }
  80. $regexBuilder = new SubscriberRegexBuilder();
  81. $re = $regexBuilder->buildRegex($user, $style, $data);
  82. return io_deleteFromFile($file, $re, true);
  83. }
  84. /**
  85. * Get data for $INFO['subscribed']
  86. *
  87. * $INFO['subscribed'] is either false if no subscription for the current page
  88. * and user is in effect. Else it contains an array of arrays with the fields
  89. * “target”, “style”, and optionally “data”.
  90. *
  91. * @param string $id Page ID, defaults to global $ID
  92. * @param string $user User, defaults to $_SERVER['REMOTE_USER']
  93. *
  94. * @return array|false
  95. * @throws Exception
  96. *
  97. * @author Adrian Lang <lang@cosmocode.de>
  98. */
  99. public function userSubscription($id = '', $user = '')
  100. {
  101. if (!$this->isenabled()) {
  102. return false;
  103. }
  104. global $ID;
  105. /** @var Input $INPUT */
  106. global $INPUT;
  107. if (!$id) {
  108. $id = $ID;
  109. }
  110. if (!$user) {
  111. $user = $INPUT->server->str('REMOTE_USER');
  112. }
  113. if (empty($user)) {
  114. // not logged in
  115. return false;
  116. }
  117. $subs = $this->subscribers($id, $user);
  118. if ($subs === []) {
  119. return false;
  120. }
  121. $result = [];
  122. foreach ($subs as $target => $info) {
  123. $result[] = [
  124. 'target' => $target,
  125. 'style' => $info[$user][0],
  126. 'data' => $info[$user][1],
  127. ];
  128. }
  129. return $result;
  130. }
  131. /**
  132. * Recursively search for matching subscriptions
  133. *
  134. * This function searches all relevant subscription files for a page or
  135. * namespace.
  136. *
  137. * @param string $page The target object’s (namespace or page) id
  138. * @param string|array $user
  139. * @param string|array $style
  140. * @param string|array $data
  141. *
  142. * @return array
  143. * @throws Exception
  144. *
  145. * @author Adrian Lang <lang@cosmocode.de>
  146. *
  147. */
  148. public function subscribers($page, $user = null, $style = null, $data = null)
  149. {
  150. if (!$this->isenabled()) {
  151. return [];
  152. }
  153. // Construct list of files which may contain relevant subscriptions.
  154. $files = [':' => $this->file(':')];
  155. do {
  156. $files[$page] = $this->file($page);
  157. $page = getNS(rtrim($page, ':')) . ':';
  158. } while ($page !== ':');
  159. $regexBuilder = new SubscriberRegexBuilder();
  160. $re = $regexBuilder->buildRegex($user, $style, $data);
  161. // Handle files.
  162. $result = [];
  163. foreach ($files as $target => $file) {
  164. if (!file_exists($file)) {
  165. continue;
  166. }
  167. $lines = file($file);
  168. foreach ($lines as $line) {
  169. // fix old style subscription files
  170. if (strpos($line, ' ') === false) {
  171. $line = trim($line) . " every\n";
  172. }
  173. // check for matching entries
  174. if (!preg_match($re, $line, $m)) {
  175. continue;
  176. }
  177. // if no last sent is set, use 0
  178. if (!isset($m[3])) {
  179. $m[3] = 0;
  180. }
  181. $u = rawurldecode($m[1]); // decode the user name
  182. if (!isset($result[$target])) {
  183. $result[$target] = [];
  184. }
  185. $result[$target][$u] = [$m[2], $m[3]]; // add to result
  186. }
  187. }
  188. return array_reverse($result);
  189. }
  190. /**
  191. * Default callback for COMMON_NOTIFY_ADDRESSLIST
  192. *
  193. * Aggregates all email addresses of user who have subscribed the given page with 'every' style
  194. *
  195. * @param array &$data Containing the entries:
  196. * - $id (the page id),
  197. * - $self (whether the author should be notified,
  198. * - $addresslist (current email address list)
  199. * - $replacements (array of additional string substitutions, @KEY@ to be replaced by value)
  200. * @throws Exception
  201. *
  202. * @author Adrian Lang <lang@cosmocode.de>
  203. * @author Steven Danz <steven-danz@kc.rr.com>
  204. *
  205. * @todo move the whole functionality into this class, trigger SUBSCRIPTION_NOTIFY_ADDRESSLIST instead,
  206. * use an array for the addresses within it
  207. */
  208. public function notifyAddresses(&$data)
  209. {
  210. if (!$this->isenabled()) {
  211. return;
  212. }
  213. /** @var AuthPlugin $auth */
  214. global $auth;
  215. global $conf;
  216. /** @var \Input $INPUT */
  217. global $INPUT;
  218. $id = $data['id'];
  219. $self = $data['self'];
  220. $addresslist = $data['addresslist'];
  221. $subscriptions = $this->subscribers($id, null, 'every');
  222. $result = [];
  223. foreach ($subscriptions as $users) {
  224. foreach ($users as $user => $info) {
  225. $userinfo = $auth->getUserData($user);
  226. if ($userinfo === false) {
  227. continue;
  228. }
  229. if (!$userinfo['mail']) {
  230. continue;
  231. }
  232. if (!$self && $user == $INPUT->server->str('REMOTE_USER')) {
  233. continue;
  234. } //skip our own changes
  235. $level = auth_aclcheck($id, $user, $userinfo['grps']);
  236. if ($level >= AUTH_READ) {
  237. if (strcasecmp($userinfo['mail'], $conf['notify']) != 0) { //skip user who get notified elsewhere
  238. $result[$user] = $userinfo['mail'];
  239. }
  240. }
  241. }
  242. }
  243. $data['addresslist'] = trim($addresslist . ',' . implode(',', $result), ',');
  244. }
  245. /**
  246. * Return the subscription meta file for the given ID
  247. *
  248. * @author Adrian Lang <lang@cosmocode.de>
  249. *
  250. * @param string $id The target page or namespace, specified by id; Namespaces
  251. * are identified by appending a colon.
  252. *
  253. * @return string
  254. */
  255. protected function file($id)
  256. {
  257. $meta_fname = '.mlist';
  258. if (str_ends_with($id, ':')) {
  259. $meta_froot = getNS($id);
  260. $meta_fname = '/' . $meta_fname;
  261. } else {
  262. $meta_froot = $id;
  263. }
  264. return metaFN((string)$meta_froot, $meta_fname);
  265. }
  266. }