Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
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.

419 lines
11 KiB

10 years ago
10 years ago
6 years ago
10 years ago
10 years ago
6 years ago
10 years ago
10 years ago
  1. <?php
  2. require_once("include/datetime.php");
  3. require_once('include/bbcode.php');
  4. require_once('include/ForumManager.php');
  5. require_once('include/group.php');
  6. require_once("mod/proxy.php");
  7. function ping_init(&$a) {
  8. header("Content-type: text/xml");
  9. echo "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
  10. <result>";
  11. $xmlhead="<"."?xml version='1.0' encoding='UTF-8' ?".">";
  12. if(local_user()){
  13. // Different login session than the page that is calling us.
  14. if(intval($_GET['uid']) && intval($_GET['uid']) != local_user()) {
  15. echo '<invalid>1</invalid></result>';
  16. killme();
  17. }
  18. $notifs = ping_get_notifications(local_user());
  19. $sysnotify = 0; // we will update this in a moment
  20. $tags = array();
  21. $comments = array();
  22. $likes = array();
  23. $dislikes = array();
  24. $friends = array();
  25. $posts = array();
  26. $home = 0;
  27. $network = 0;
  28. $groups_unseen = array();
  29. $forums_unseen = array();
  30. $r = q("SELECT `item`.`id`,`item`.`parent`, `item`.`verb`, `item`.`wall`, `item`.`author-name`,
  31. `item`.`contact-id`, `item`.`author-link`, `item`.`author-avatar`, `item`.`created`, `item`.`object`,
  32. `pitem`.`author-name` as `pname`, `pitem`.`author-link` as `plink`
  33. FROM `item` INNER JOIN `item` as `pitem` ON `pitem`.`id`=`item`.`parent`
  34. WHERE `item`.`unseen` = 1 AND `item`.`visible` = 1 AND
  35. `item`.`deleted` = 0 AND `item`.`uid` = %d AND `pitem`.`parent` != 0
  36. AND `item`.`contact-id` != %d
  37. ORDER BY `item`.`created` DESC",
  38. intval(local_user()), intval(local_user())
  39. );
  40. if(count($r)) {
  41. $arr = array('items' => $r);
  42. call_hooks('network_ping', $arr);
  43. foreach ($r as $it) {
  44. if($it['wall'])
  45. $home ++;
  46. else
  47. $network ++;
  48. switch($it['verb']){
  49. case ACTIVITY_TAG:
  50. $obj = parse_xml_string($xmlhead.$it['object']);
  51. $it['tname'] = $obj->content;
  52. $tags[] = $it;
  53. break;
  54. case ACTIVITY_LIKE:
  55. $likes[] = $it;
  56. break;
  57. case ACTIVITY_DISLIKE:
  58. $dislikes[] = $it;
  59. break;
  60. case ACTIVITY_FRIEND:
  61. $obj = parse_xml_string($xmlhead.$it['object']);
  62. $it['fname'] = $obj->title;
  63. $friends[] = $it;
  64. break;
  65. default:
  66. if ($it['parent']!=$it['id']) {
  67. $comments[] = $it;
  68. } else {
  69. if(! $it['wall'])
  70. $posts[] = $it;
  71. }
  72. }
  73. }
  74. }
  75. if($network) {
  76. if(intval(feature_enabled(local_user(),'groups'))) {
  77. // Find out how unseen network posts are spread across groups
  78. $groups_unseen = groups_count_unseen();
  79. }
  80. if(intval(feature_enabled(local_user(),'forumlist_widget'))) {
  81. $forums_unseen = ForumManager::count_unseen_items();
  82. }
  83. }
  84. $intros1 = q("SELECT `intro`.`id`, `intro`.`datetime`,
  85. `fcontact`.`name`, `fcontact`.`url`, `fcontact`.`photo`
  86. FROM `intro` LEFT JOIN `fcontact` ON `intro`.`fid` = `fcontact`.`id`
  87. WHERE `intro`.`uid` = %d AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`fid`!=0",
  88. intval(local_user())
  89. );
  90. $intros2 = q("SELECT `intro`.`id`, `intro`.`datetime`,
  91. `contact`.`name`, `contact`.`url`, `contact`.`photo`
  92. FROM `intro` LEFT JOIN `contact` ON `intro`.`contact-id` = `contact`.`id`
  93. WHERE `intro`.`uid` = %d AND `intro`.`blocked` = 0 AND `intro`.`ignore` = 0 AND `intro`.`contact-id`!=0",
  94. intval(local_user())
  95. );
  96. $intro = count($intros1) + count($intros2);
  97. $intros = $intros1+$intros2;
  98. $myurl = $a->get_baseurl() . '/profile/' . $a->user['nickname'] ;
  99. $mails = q("SELECT * FROM `mail`
  100. WHERE `uid` = %d AND `seen` = 0 AND `from-url` != '%s' ",
  101. intval(local_user()),
  102. dbesc($myurl)
  103. );
  104. $mail = count($mails);
  105. if ($a->config['register_policy'] == REGISTER_APPROVE && is_site_admin()){
  106. $regs = q("SELECT `contact`.`name`, `contact`.`url`, `contact`.`micro`, `register`.`created`, COUNT(*) as `total` FROM `contact` RIGHT JOIN `register` ON `register`.`uid`=`contact`.`uid` WHERE `contact`.`self`=1");
  107. if($regs)
  108. $register = $regs[0]['total'];
  109. } else {
  110. $register = "0";
  111. }
  112. $all_events = 0;
  113. $all_events_today = 0;
  114. $events = 0;
  115. $events_today = 0;
  116. $birthdays = 0;
  117. $birthdays_today = 0;
  118. $ev = q("SELECT count(`event`.`id`) as total, type, start, adjust FROM `event`
  119. WHERE `event`.`uid` = %d AND `start` < '%s' AND `finish` > '%s' and `ignore` = 0
  120. ORDER BY `start` ASC ",
  121. intval(local_user()),
  122. dbesc(datetime_convert('UTC','UTC','now + 7 days')),
  123. dbesc(datetime_convert('UTC','UTC','now'))
  124. );
  125. if($ev && count($ev)) {
  126. $all_events = intval($ev[0]['total']);
  127. if($all_events) {
  128. $str_now = datetime_convert('UTC',$a->timezone,'now','Y-m-d');
  129. foreach($ev as $x) {
  130. $bd = false;
  131. if($x['type'] === 'birthday') {
  132. $birthdays ++;
  133. $bd = true;
  134. }
  135. else {
  136. $events ++;
  137. }
  138. if(datetime_convert('UTC',((intval($x['adjust'])) ? $a->timezone : 'UTC'), $x['start'],'Y-m-d') === $str_now) {
  139. $all_events_today ++;
  140. if($bd)
  141. $birthdays_today ++;
  142. else
  143. $events_today ++;
  144. }
  145. }
  146. }
  147. }
  148. /**
  149. * return xml from notification array
  150. *
  151. * @param array $n Notification array:
  152. * 'href' => notification link
  153. * 'name' => subject name
  154. * 'url' => subject url
  155. * 'photo' => subject photo
  156. * 'date' => notification date
  157. * 'seen' => bool true/false
  158. * 'message' => notification message. "{0}" will be replaced by subject name
  159. **/
  160. function xmlize($n){
  161. $contact = get_contact_details_by_url($n['url']);
  162. if (isset($contact["micro"]))
  163. $n['photo'] = proxy_url($contact["micro"], false, PROXY_SIZE_MICRO);
  164. else
  165. $n['photo'] = proxy_url($n['photo'], false, PROXY_SIZE_MICRO);
  166. $n['message'] = html_entity_decode($n['message'], ENT_COMPAT | ENT_HTML401, "UTF-8");
  167. $n['name'] = html_entity_decode($n['name'], ENT_COMPAT | ENT_HTML401, "UTF-8");
  168. // Are the nofications calles from the regular process or via the friendica app?
  169. $regularnotifications = (intval($_GET['uid']) AND intval($_GET['_']));
  170. $a = get_app();
  171. if ($a->is_friendica_app() OR !$regularnotifications)
  172. $n['message'] = str_replace("{0}", $n['name'], $n['message']);
  173. $local_time = datetime_convert('UTC',date_default_timezone_get(),$n['date']);
  174. call_hooks('ping_xmlize', $n);
  175. $notsxml = '<note id="%d" href="%s" name="%s" url="%s" photo="%s" date="%s" seen="%s" timestamp="%s" >%s</note>'."\n";
  176. return sprintf ( $notsxml, intval($n['id']),
  177. xmlify($n['href']), xmlify(xmlify($n['name'])), xmlify($n['url']), xmlify($n['photo']),
  178. xmlify(relative_date($n['date'])), xmlify($n['seen']), xmlify(strtotime($local_time)),
  179. xmlify($n['message'])
  180. );
  181. }
  182. echo "<intro>$intro</intro>
  183. <mail>$mail</mail>
  184. <net>$network</net>
  185. <home>$home</home>\r\n";
  186. if ($register!=0) echo "<register>$register</register>";
  187. if (count($groups_unseen)) {
  188. echo '<groups>';
  189. foreach ($groups_unseen as $it)
  190. if ($it['count'] > 0)
  191. echo '<group id="'.$it['id'].'">'.$it['count']."</group>";
  192. echo "</groups>";
  193. }
  194. if (count($forums_unseen)) {
  195. echo '<forums>';
  196. foreach ($forums_unseen as $it)
  197. if ($it['count'] > 0)
  198. echo '<forum id="'.$it['id'].'">'.$it['count']."</forum>";
  199. echo "</forums>";
  200. }
  201. echo "<all-events>$all_events</all-events>
  202. <all-events-today>$all_events_today</all-events-today>
  203. <events>$events</events>
  204. <events-today>$events_today</events-today>
  205. <birthdays>$birthdays</birthdays>
  206. <birthdays-today>$birthdays_today</birthdays-today>\r\n";
  207. if(count($notifs) && (! $sysnotify)) {
  208. foreach($notifs as $zz) {
  209. if($zz['seen'] == 0)
  210. $sysnotify ++;
  211. }
  212. }
  213. echo ' <notif count="'. ($sysnotify + $intro + $mail + $register) .'">';
  214. // merge all notification types in one array
  215. if ($intro>0){
  216. foreach ($intros as $i) {
  217. $n = array(
  218. 'href' => $a->get_baseurl().'/notifications/intros/'.$i['id'],
  219. 'name' => $i['name'],
  220. 'url' => $i['url'],
  221. 'photo' => $i['photo'],
  222. 'date' => $i['datetime'],
  223. 'seen' => false,
  224. 'message' => t("{0} wants to be your friend"),
  225. );
  226. $notifs[] = $n;
  227. }
  228. }
  229. if ($mail>0){
  230. foreach ($mails as $i) {
  231. $n = array(
  232. 'href' => $a->get_baseurl().'/message/'.$i['id'],
  233. 'name' => $i['from-name'],
  234. 'url' => $i['from-url'],
  235. 'photo' => $i['from-photo'],
  236. 'date' => $i['created'],
  237. 'seen' => false,
  238. 'message' => t("{0} sent you a message"),
  239. );
  240. $notifs[] = $n;
  241. }
  242. }
  243. if ($register>0){
  244. foreach ($regs as $i) {
  245. $n = array(
  246. 'href' => $a->get_baseurl().'/admin/users/',
  247. 'name' => $i['name'],
  248. 'url' => $i['url'],
  249. 'photo' => $i['micro'],
  250. 'date' => $i['created'],
  251. 'seen' => false,
  252. 'message' => t("{0} requested registration"),
  253. );
  254. $notifs[] = $n;
  255. }
  256. }
  257. // sort notifications by $[]['date']
  258. $sort_function = function($a, $b) {
  259. $adate = date($a['date']);
  260. $bdate = date($b['date']);
  261. if ($adate == $bdate) {
  262. return 0;
  263. }
  264. return ($adate < $bdate) ? 1 : -1;
  265. };
  266. usort($notifs, $sort_function);
  267. if(count($notifs)) {
  268. foreach($notifs as $n) {
  269. echo xmlize($n);
  270. }
  271. }
  272. echo " </notif>";
  273. }
  274. echo " <sysmsgs>";
  275. if(x($_SESSION,'sysmsg')){
  276. foreach ($_SESSION['sysmsg'] as $m){
  277. echo "<notice>".xmlify($m)."</notice>";
  278. }
  279. unset($_SESSION['sysmsg']);
  280. }
  281. if(x($_SESSION,'sysmsg_info')){
  282. foreach ($_SESSION['sysmsg_info'] as $m){
  283. echo "<info>".xmlify($m)."</info>";
  284. }
  285. unset($_SESSION['sysmsg_info']);
  286. }
  287. echo " </sysmsgs>";
  288. echo"</result>
  289. ";
  290. killme();
  291. }
  292. function ping_get_notifications($uid) {
  293. $result = array();
  294. $offset = 0;
  295. $seen = false;
  296. $seensql = "NOT";
  297. $order = "DESC";
  298. $quit = false;
  299. $a = get_app();
  300. do {
  301. $r = q("SELECT `notify`.*, `item`.`visible`, `item`.`spam`, `item`.`deleted`
  302. FROM `notify` LEFT JOIN `item` ON `item`.`id` = `notify`.`iid`
  303. WHERE `notify`.`uid` = %d AND `notify`.`msg` != ''
  304. AND NOT (`notify`.`type` IN (%d, %d))
  305. AND $seensql `notify`.`seen` ORDER BY `notify`.`date` $order LIMIT %d, 50",
  306. intval($uid),
  307. intval(NOTIFY_INTRO),
  308. intval(NOTIFY_MAIL),
  309. intval($offset)
  310. );
  311. if (!$r AND !$seen) {
  312. $seen = true;
  313. $seensql = "";
  314. $order = "DESC";
  315. $offset = 0;
  316. } elseif (!$r)
  317. $quit = true;
  318. else
  319. $offset += 50;
  320. foreach ($r AS $notification) {
  321. if (is_null($notification["visible"]))
  322. $notification["visible"] = true;
  323. if (is_null($notification["spam"]))
  324. $notification["spam"] = 0;
  325. if (is_null($notification["deleted"]))
  326. $notification["deleted"] = 0;
  327. $notification["message"] = strip_tags(bbcode($notification["msg"]));
  328. $notification["name"] = strip_tags(bbcode($notification["name"]));
  329. // Replace the name with {0} but ensure to make that only once
  330. // The {0} is used later and prints the name in bold.
  331. if ($notification['name'] != "")
  332. $pos = strpos($notification["message"],$notification['name']);
  333. else
  334. $pos = false;
  335. if ($pos !== false)
  336. $notification["message"] = substr_replace($notification["message"],"{0}",$pos,strlen($notification["name"]));
  337. $notification['href'] = $a->get_baseurl() . '/notify/view/' . $notification['id'];
  338. if ($notification["visible"] AND !$notification["spam"] AND
  339. !$notification["deleted"] AND !is_array($result[$notification["parent"]])) {
  340. $result[$notification["parent"]] = $notification;
  341. }
  342. }
  343. } while ((count($result) < 50) AND !$quit);
  344. return($result);
  345. }