Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

734 Zeilen
23 KiB

  1. <?php
  2. /**
  3. * @file src/Core/NotificationsManager.php
  4. * @brief Methods for read and write notifications from/to database
  5. * or for formatting notifications
  6. */
  7. namespace Friendica\Core;
  8. use Friendica\BaseObject;
  9. use Friendica\Content\Text\BBCode;
  10. use Friendica\Content\Text\HTML;
  11. use Friendica\Core\Logger;
  12. use Friendica\Core\Protocol;
  13. use Friendica\Database\DBA;
  14. use Friendica\Model\Contact;
  15. use Friendica\Model\Item;
  16. use Friendica\Util\DateTimeFormat;
  17. use Friendica\Util\Proxy as ProxyUtils;
  18. use Friendica\Util\Temporal;
  19. use Friendica\Util\XML;
  20. /**
  21. * @brief Methods for read and write notifications from/to database
  22. * or for formatting notifications
  23. */
  24. class NotificationsManager extends BaseObject
  25. {
  26. /**
  27. * @brief set some extra note properties
  28. *
  29. * @param array $notes array of note arrays from db
  30. * @return array Copy of input array with added properties
  31. *
  32. * Set some extra properties to note array from db:
  33. * - timestamp as int in default TZ
  34. * - date_rel : relative date string
  35. * - msg_html: message as html string
  36. * - msg_plain: message as plain text string
  37. */
  38. private function _set_extra($notes)
  39. {
  40. $rets = [];
  41. foreach ($notes as $n) {
  42. $local_time = DateTimeFormat::local($n['date']);
  43. $n['timestamp'] = strtotime($local_time);
  44. $n['date_rel'] = Temporal::getRelativeDate($n['date']);
  45. $n['msg_html'] = BBCode::convert($n['msg'], false);
  46. $n['msg_plain'] = explode("\n", trim(HTML::toPlaintext($n['msg_html'], 0)))[0];
  47. $rets[] = $n;
  48. }
  49. return $rets;
  50. }
  51. /**
  52. * @brief Get all notifications for local_user()
  53. *
  54. * @param array $filter optional Array "column name"=>value: filter query by columns values
  55. * @param string $order optional Space separated list of column to sort by.
  56. * Prepend name with "+" to sort ASC, "-" to sort DESC. Default to "-date"
  57. * @param string $limit optional Query limits
  58. *
  59. * @return array of results or false on errors
  60. */
  61. public function getAll($filter = [], $order = "-date", $limit = "")
  62. {
  63. $filter_str = [];
  64. $filter_sql = "";
  65. foreach ($filter as $column => $value) {
  66. $filter_str[] = sprintf("`%s` = '%s'", $column, DBA::escape($value));
  67. }
  68. if (count($filter_str) > 0) {
  69. $filter_sql = "AND " . implode(" AND ", $filter_str);
  70. }
  71. $aOrder = explode(" ", $order);
  72. $asOrder = [];
  73. foreach ($aOrder as $o) {
  74. $dir = "asc";
  75. if ($o[0] === "-") {
  76. $dir = "desc";
  77. $o = substr($o, 1);
  78. }
  79. if ($o[0] === "+") {
  80. $dir = "asc";
  81. $o = substr($o, 1);
  82. }
  83. $asOrder[] = "$o $dir";
  84. }
  85. $order_sql = implode(", ", $asOrder);
  86. if ($limit != "") {
  87. $limit = " LIMIT " . $limit;
  88. }
  89. $r = q(
  90. "SELECT * FROM `notify` WHERE `uid` = %d $filter_sql ORDER BY $order_sql $limit",
  91. intval(local_user())
  92. );
  93. if (DBA::isResult($r)) {
  94. return $this->_set_extra($r);
  95. }
  96. return false;
  97. }
  98. /**
  99. * @brief Get one note for local_user() by $id value
  100. *
  101. * @param int $id identity
  102. * @return array note values or null if not found
  103. */
  104. public function getByID($id)
  105. {
  106. $r = q(
  107. "SELECT * FROM `notify` WHERE `id` = %d AND `uid` = %d LIMIT 1",
  108. intval($id),
  109. intval(local_user())
  110. );
  111. if (DBA::isResult($r)) {
  112. return $this->_set_extra($r)[0];
  113. }
  114. return null;
  115. }
  116. /**
  117. * @brief set seen state of $note of local_user()
  118. *
  119. * @param array $note note array
  120. * @param bool $seen optional true or false, default true
  121. * @return bool true on success, false on errors
  122. */
  123. public function setSeen($note, $seen = true)
  124. {
  125. return q(
  126. "UPDATE `notify` SET `seen` = %d WHERE (`link` = '%s' OR (`parent` != 0 AND `parent` = %d AND `otype` = '%s')) AND `uid` = %d",
  127. intval($seen),
  128. DBA::escape($note['link']),
  129. intval($note['parent']),
  130. DBA::escape($note['otype']),
  131. intval(local_user())
  132. );
  133. }
  134. /**
  135. * @brief set seen state of all notifications of local_user()
  136. *
  137. * @param bool $seen optional true or false. default true
  138. * @return bool true on success, false on error
  139. */
  140. public function setAllSeen($seen = true)
  141. {
  142. return q(
  143. "UPDATE `notify` SET `seen` = %d WHERE `uid` = %d",
  144. intval($seen),
  145. intval(local_user())
  146. );
  147. }
  148. /**
  149. * @brief List of pages for the Notifications TabBar
  150. *
  151. * @return array with with notifications TabBar data
  152. */
  153. public function getTabs()
  154. {
  155. $selected = defaults(self::getApp()->argv, 1, '');
  156. $tabs = [
  157. [
  158. 'label' => L10n::t('System'),
  159. 'url' => 'notifications/system',
  160. 'sel' => (($selected == 'system') ? 'active' : ''),
  161. 'id' => 'system-tab',
  162. 'accesskey' => 'y',
  163. ],
  164. [
  165. 'label' => L10n::t('Network'),
  166. 'url' => 'notifications/network',
  167. 'sel' => (($selected == 'network') ? 'active' : ''),
  168. 'id' => 'network-tab',
  169. 'accesskey' => 'w',
  170. ],
  171. [
  172. 'label' => L10n::t('Personal'),
  173. 'url' => 'notifications/personal',
  174. 'sel' => (($selected == 'personal') ? 'active' : ''),
  175. 'id' => 'personal-tab',
  176. 'accesskey' => 'r',
  177. ],
  178. [
  179. 'label' => L10n::t('Home'),
  180. 'url' => 'notifications/home',
  181. 'sel' => (($selected == 'home') ? 'active' : ''),
  182. 'id' => 'home-tab',
  183. 'accesskey' => 'h',
  184. ],
  185. [
  186. 'label' => L10n::t('Introductions'),
  187. 'url' => 'notifications/intros',
  188. 'sel' => (($selected == 'intros') ? 'active' : ''),
  189. 'id' => 'intro-tab',
  190. 'accesskey' => 'i',
  191. ],
  192. ];
  193. return $tabs;
  194. }
  195. /**
  196. * @brief Format the notification query in an usable array
  197. *
  198. * @param array $notifs The array from the db query
  199. * @param string $ident The notifications identifier (e.g. network)
  200. * @return array
  201. * string 'label' => The type of the notification
  202. * string 'link' => URL to the source
  203. * string 'image' => The avatar image
  204. * string 'url' => The profile url of the contact
  205. * string 'text' => The notification text
  206. * string 'when' => The date of the notification
  207. * string 'ago' => T relative date of the notification
  208. * bool 'seen' => Is the notification marked as "seen"
  209. */
  210. private function formatNotifs(array $notifs, $ident = "")
  211. {
  212. $notif = [];
  213. $arr = [];
  214. if (DBA::isResult($notifs)) {
  215. foreach ($notifs as $it) {
  216. // Because we use different db tables for the notification query
  217. // we have sometimes $it['unseen'] and sometimes $it['seen].
  218. // So we will have to transform $it['unseen']
  219. if (array_key_exists('unseen', $it)) {
  220. $it['seen'] = ($it['unseen'] > 0 ? false : true);
  221. }
  222. // For feed items we use the user's contact, since the avatar is mostly self choosen.
  223. if (!empty($it['network']) && $it['network'] == Protocol::FEED) {
  224. $it['author-avatar'] = $it['contact-avatar'];
  225. }
  226. // Depending on the identifier of the notification we need to use different defaults
  227. switch ($ident) {
  228. case 'system':
  229. $default_item_label = 'notify';
  230. $default_item_link = System::baseUrl(true) . '/notify/view/' . $it['id'];
  231. $default_item_image = ProxyUtils::proxifyUrl($it['photo'], false, ProxyUtils::SIZE_MICRO);
  232. $default_item_url = $it['url'];
  233. $default_item_text = strip_tags(BBCode::convert($it['msg']));
  234. $default_item_when = DateTimeFormat::local($it['date'], 'r');
  235. $default_item_ago = Temporal::getRelativeDate($it['date']);
  236. break;
  237. case 'home':
  238. $default_item_label = 'comment';
  239. $default_item_link = System::baseUrl(true) . '/display/' . $it['parent-guid'];
  240. $default_item_image = ProxyUtils::proxifyUrl($it['author-avatar'], false, ProxyUtils::SIZE_MICRO);
  241. $default_item_url = $it['author-link'];
  242. $default_item_text = L10n::t("%s commented on %s's post", $it['author-name'], $it['parent-author-name']);
  243. $default_item_when = DateTimeFormat::local($it['created'], 'r');
  244. $default_item_ago = Temporal::getRelativeDate($it['created']);
  245. break;
  246. default:
  247. $default_item_label = (($it['id'] == $it['parent']) ? 'post' : 'comment');
  248. $default_item_link = System::baseUrl(true) . '/display/' . $it['parent-guid'];
  249. $default_item_image = ProxyUtils::proxifyUrl($it['author-avatar'], false, ProxyUtils::SIZE_MICRO);
  250. $default_item_url = $it['author-link'];
  251. $default_item_text = (($it['id'] == $it['parent'])
  252. ? L10n::t("%s created a new post", $it['author-name'])
  253. : L10n::t("%s commented on %s's post", $it['author-name'], $it['parent-author-name']));
  254. $default_item_when = DateTimeFormat::local($it['created'], 'r');
  255. $default_item_ago = Temporal::getRelativeDate($it['created']);
  256. }
  257. // Transform the different types of notification in an usable array
  258. switch ($it['verb']) {
  259. case ACTIVITY_LIKE:
  260. $notif = [
  261. 'label' => 'like',
  262. 'link' => System::baseUrl(true) . '/display/' . $it['parent-guid'],
  263. 'image' => ProxyUtils::proxifyUrl($it['author-avatar'], false, ProxyUtils::SIZE_MICRO),
  264. 'url' => $it['author-link'],
  265. 'text' => L10n::t("%s liked %s's post", $it['author-name'], $it['parent-author-name']),
  266. 'when' => $default_item_when,
  267. 'ago' => $default_item_ago,
  268. 'seen' => $it['seen']
  269. ];
  270. break;
  271. case ACTIVITY_DISLIKE:
  272. $notif = [
  273. 'label' => 'dislike',
  274. 'link' => System::baseUrl(true) . '/display/' . $it['parent-guid'],
  275. 'image' => ProxyUtils::proxifyUrl($it['author-avatar'], false, ProxyUtils::SIZE_MICRO),
  276. 'url' => $it['author-link'],
  277. 'text' => L10n::t("%s disliked %s's post", $it['author-name'], $it['parent-author-name']),
  278. 'when' => $default_item_when,
  279. 'ago' => $default_item_ago,
  280. 'seen' => $it['seen']
  281. ];
  282. break;
  283. case ACTIVITY_ATTEND:
  284. $notif = [
  285. 'label' => 'attend',
  286. 'link' => System::baseUrl(true) . '/display/' . $it['parent-guid'],
  287. 'image' => ProxyUtils::proxifyUrl($it['author-avatar'], false, ProxyUtils::SIZE_MICRO),
  288. 'url' => $it['author-link'],
  289. 'text' => L10n::t("%s is attending %s's event", $it['author-name'], $it['parent-author-name']),
  290. 'when' => $default_item_when,
  291. 'ago' => $default_item_ago,
  292. 'seen' => $it['seen']
  293. ];
  294. break;
  295. case ACTIVITY_ATTENDNO:
  296. $notif = [
  297. 'label' => 'attendno',
  298. 'link' => System::baseUrl(true) . '/display/' . $it['parent-guid'],
  299. 'image' => ProxyUtils::proxifyUrl($it['author-avatar'], false, ProxyUtils::SIZE_MICRO),
  300. 'url' => $it['author-link'],
  301. 'text' => L10n::t("%s is not attending %s's event", $it['author-name'], $it['parent-author-name']),
  302. 'when' => $default_item_when,
  303. 'ago' => $default_item_ago,
  304. 'seen' => $it['seen']
  305. ];
  306. break;
  307. case ACTIVITY_ATTENDMAYBE:
  308. $notif = [
  309. 'label' => 'attendmaybe',
  310. 'link' => System::baseUrl(true) . '/display/' . $it['parent-guid'],
  311. 'image' => ProxyUtils::proxifyUrl($it['author-avatar'], false, ProxyUtils::SIZE_MICRO),
  312. 'url' => $it['author-link'],
  313. 'text' => L10n::t("%s may attend %s's event", $it['author-name'], $it['parent-author-name']),
  314. 'when' => $default_item_when,
  315. 'ago' => $default_item_ago,
  316. 'seen' => $it['seen']
  317. ];
  318. break;
  319. case ACTIVITY_FRIEND:
  320. if (!isset($it['object'])) {
  321. $notif = [
  322. 'label' => 'friend',
  323. 'link' => $default_item_link,
  324. 'image' => $default_item_image,
  325. 'url' => $default_item_url,
  326. 'text' => $default_item_text,
  327. 'when' => $default_item_when,
  328. 'ago' => $default_item_ago,
  329. 'seen' => $it['seen']
  330. ];
  331. break;
  332. }
  333. /// @todo Check if this part here is used at all
  334. Logger::log('Complete data: ' . json_encode($it) . ' - ' . System::callstack(20), Logger::DEBUG);
  335. $xmlhead = "<" . "?xml version='1.0' encoding='UTF-8' ?" . ">";
  336. $obj = XML::parseString($xmlhead . $it['object']);
  337. $it['fname'] = $obj->title;
  338. $notif = [
  339. 'label' => 'friend',
  340. 'link' => System::baseUrl(true) . '/display/' . $it['parent-guid'],
  341. 'image' => ProxyUtils::proxifyUrl($it['author-avatar'], false, ProxyUtils::SIZE_MICRO),
  342. 'url' => $it['author-link'],
  343. 'text' => L10n::t("%s is now friends with %s", $it['author-name'], $it['fname']),
  344. 'when' => $default_item_when,
  345. 'ago' => $default_item_ago,
  346. 'seen' => $it['seen']
  347. ];
  348. break;
  349. default:
  350. $notif = [
  351. 'label' => $default_item_label,
  352. 'link' => $default_item_link,
  353. 'image' => $default_item_image,
  354. 'url' => $default_item_url,
  355. 'text' => $default_item_text,
  356. 'when' => $default_item_when,
  357. 'ago' => $default_item_ago,
  358. 'seen' => $it['seen']
  359. ];
  360. }
  361. $arr[] = $notif;
  362. }
  363. }
  364. return $arr;
  365. }
  366. /**
  367. * @brief Get network notifications
  368. *
  369. * @param int|string $seen If 0 only include notifications into the query
  370. * which aren't marked as "seen"
  371. * @param int $start Start the query at this point
  372. * @param int $limit Maximum number of query results
  373. *
  374. * @return array with
  375. * string 'ident' => Notification identifier
  376. * array 'notifications' => Network notifications
  377. */
  378. public function networkNotifs($seen = 0, $start = 0, $limit = 80)
  379. {
  380. $ident = 'network';
  381. $notifs = [];
  382. $condition = ['wall' => false, 'uid' => local_user()];
  383. if ($seen === 0) {
  384. $condition['unseen'] = true;
  385. }
  386. $fields = ['id', 'parent', 'verb', 'author-name', 'unseen', 'author-link', 'author-avatar', 'contact-avatar',
  387. 'network', 'created', 'object', 'parent-author-name', 'parent-author-link', 'parent-guid'];
  388. $params = ['order' => ['created' => true], 'limit' => [$start, $limit]];
  389. $items = Item::selectForUser(local_user(), $fields, $condition, $params);
  390. if (DBA::isResult($items)) {
  391. $notifs = $this->formatNotifs(Item::inArray($items), $ident);
  392. }
  393. $arr = [
  394. 'notifications' => $notifs,
  395. 'ident' => $ident,
  396. ];
  397. return $arr;
  398. }
  399. /**
  400. * @brief Get system notifications
  401. *
  402. * @param int|string $seen If 0 only include notifications into the query
  403. * which aren't marked as "seen"
  404. * @param int $start Start the query at this point
  405. * @param int $limit Maximum number of query results
  406. *
  407. * @return array with
  408. * string 'ident' => Notification identifier
  409. * array 'notifications' => System notifications
  410. */
  411. public function systemNotifs($seen = 0, $start = 0, $limit = 80)
  412. {
  413. $ident = 'system';
  414. $notifs = [];
  415. $sql_seen = "";
  416. if ($seen === 0) {
  417. $sql_seen = " AND NOT `seen` ";
  418. }
  419. $r = q(
  420. "SELECT `id`, `url`, `photo`, `msg`, `date`, `seen`, `verb` FROM `notify`
  421. WHERE `uid` = %d $sql_seen ORDER BY `date` DESC LIMIT %d, %d ",
  422. intval(local_user()),
  423. intval($start),
  424. intval($limit)
  425. );
  426. if (DBA::isResult($r)) {
  427. $notifs = $this->formatNotifs($r, $ident);
  428. }
  429. $arr = [
  430. 'notifications' => $notifs,
  431. 'ident' => $ident,
  432. ];
  433. return $arr;
  434. }
  435. /**
  436. * @brief Get personal notifications
  437. *
  438. * @param int|string $seen If 0 only include notifications into the query
  439. * which aren't marked as "seen"
  440. * @param int $start Start the query at this point
  441. * @param int $limit Maximum number of query results
  442. *
  443. * @return array with
  444. * string 'ident' => Notification identifier
  445. * array 'notifications' => Personal notifications
  446. */
  447. public function personalNotifs($seen = 0, $start = 0, $limit = 80)
  448. {
  449. $ident = 'personal';
  450. $notifs = [];
  451. $myurl = str_replace('http://', '', self::getApp()->contact['nurl']);
  452. $diasp_url = str_replace('/profile/', '/u/', $myurl);
  453. $condition = ["NOT `wall` AND `uid` = ? AND (`item`.`author-id` = ? OR `item`.`tag` REGEXP ? OR `item`.`tag` REGEXP ?)",
  454. local_user(), public_contact(), $myurl . '\\]', $diasp_url . '\\]'];
  455. if ($seen === 0) {
  456. $condition[0] .= " AND `unseen`";
  457. }
  458. $fields = ['id', 'parent', 'verb', 'author-name', 'unseen', 'author-link', 'author-avatar', 'contact-avatar',
  459. 'network', 'created', 'object', 'parent-author-name', 'parent-author-link', 'parent-guid'];
  460. $params = ['order' => ['created' => true], 'limit' => [$start, $limit]];
  461. $items = Item::selectForUser(local_user(), $fields, $condition, $params);
  462. if (DBA::isResult($items)) {
  463. $notifs = $this->formatNotifs(Item::inArray($items), $ident);
  464. }
  465. $arr = [
  466. 'notifications' => $notifs,
  467. 'ident' => $ident,
  468. ];
  469. return $arr;
  470. }
  471. /**
  472. * @brief Get home notifications
  473. *
  474. * @param int|string $seen If 0 only include notifications into the query
  475. * which aren't marked as "seen"
  476. * @param int $start Start the query at this point
  477. * @param int $limit Maximum number of query results
  478. *
  479. * @return array with
  480. * string 'ident' => Notification identifier
  481. * array 'notifications' => Home notifications
  482. */
  483. public function homeNotifs($seen = 0, $start = 0, $limit = 80)
  484. {
  485. $ident = 'home';
  486. $notifs = [];
  487. $condition = ['wall' => true, 'uid' => local_user()];
  488. if ($seen === 0) {
  489. $condition['unseen'] = true;
  490. }
  491. $fields = ['id', 'parent', 'verb', 'author-name', 'unseen', 'author-link', 'author-avatar', 'contact-avatar',
  492. 'network', 'created', 'object', 'parent-author-name', 'parent-author-link', 'parent-guid'];
  493. $params = ['order' => ['created' => true], 'limit' => [$start, $limit]];
  494. $items = Item::selectForUser(local_user(), $fields, $condition, $params);
  495. if (DBA::isResult($items)) {
  496. $notifs = $this->formatNotifs(Item::inArray($items), $ident);
  497. }
  498. $arr = [
  499. 'notifications' => $notifs,
  500. 'ident' => $ident,
  501. ];
  502. return $arr;
  503. }
  504. /**
  505. * @brief Get introductions
  506. *
  507. * @param bool $all If false only include introductions into the query
  508. * which aren't marked as ignored
  509. * @param int $start Start the query at this point
  510. * @param int $limit Maximum number of query results
  511. *
  512. * @return array with
  513. * string 'ident' => Notification identifier
  514. * array 'notifications' => Introductions
  515. */
  516. public function introNotifs($all = false, $start = 0, $limit = 80)
  517. {
  518. $ident = 'introductions';
  519. $notifs = [];
  520. $sql_extra = "";
  521. if (!$all) {
  522. $sql_extra = " AND `ignore` = 0 ";
  523. }
  524. /// @todo Fetch contact details by "Contact::getDetailsByUrl" instead of queries to contact, fcontact and gcontact
  525. $r = q(
  526. "SELECT `intro`.`id` AS `intro_id`, `intro`.*, `contact`.*,
  527. `fcontact`.`name` AS `fname`, `fcontact`.`url` AS `furl`, `fcontact`.`addr` AS `faddr`,
  528. `fcontact`.`photo` AS `fphoto`, `fcontact`.`request` AS `frequest`,
  529. `gcontact`.`location` AS `glocation`, `gcontact`.`about` AS `gabout`,
  530. `gcontact`.`keywords` AS `gkeywords`, `gcontact`.`gender` AS `ggender`,
  531. `gcontact`.`network` AS `gnetwork`, `gcontact`.`addr` AS `gaddr`
  532. FROM `intro`
  533. LEFT JOIN `contact` ON `contact`.`id` = `intro`.`contact-id`
  534. LEFT JOIN `gcontact` ON `gcontact`.`nurl` = `contact`.`nurl`
  535. LEFT JOIN `fcontact` ON `intro`.`fid` = `fcontact`.`id`
  536. WHERE `intro`.`uid` = %d $sql_extra AND `intro`.`blocked` = 0
  537. LIMIT %d, %d",
  538. intval($_SESSION['uid']),
  539. intval($start),
  540. intval($limit)
  541. );
  542. if (DBA::isResult($r)) {
  543. $notifs = $this->formatIntros($r);
  544. }
  545. $arr = [
  546. 'ident' => $ident,
  547. 'notifications' => $notifs,
  548. ];
  549. return $arr;
  550. }
  551. /**
  552. * @brief Format the notification query in an usable array
  553. *
  554. * @param array $intros The array from the db query
  555. * @return array with the introductions
  556. */
  557. private function formatIntros($intros)
  558. {
  559. $knowyou = '';
  560. foreach ($intros as $it) {
  561. // There are two kind of introduction. Contacts suggested by other contacts and normal connection requests.
  562. // We have to distinguish between these two because they use different data.
  563. // Contact suggestions
  564. if ($it['fid']) {
  565. $return_addr = bin2hex(self::getApp()->user['nickname'] . '@' . self::getApp()->getHostName() . ((self::getApp()->getURLPath()) ? '/' . self::getApp()->getURLPath() : ''));
  566. $intro = [
  567. 'label' => 'friend_suggestion',
  568. 'notify_type' => L10n::t('Friend Suggestion'),
  569. 'intro_id' => $it['intro_id'],
  570. 'madeby' => $it['name'],
  571. 'madeby_url' => $it['url'],
  572. 'madeby_zrl' => Contact::magicLink($it['url']),
  573. 'madeby_addr' => $it['addr'],
  574. 'contact_id' => $it['contact-id'],
  575. 'photo' => (!empty($it['fphoto']) ? ProxyUtils::proxifyUrl($it['fphoto'], false, ProxyUtils::SIZE_SMALL) : "images/person-300.jpg"),
  576. 'name' => $it['fname'],
  577. 'url' => $it['furl'],
  578. 'zrl' => Contact::magicLink($it['furl']),
  579. 'hidden' => $it['hidden'] == 1,
  580. 'post_newfriend' => (intval(PConfig::get(local_user(), 'system', 'post_newfriend')) ? '1' : 0),
  581. 'knowyou' => $knowyou,
  582. 'note' => $it['note'],
  583. 'request' => $it['frequest'] . '?addr=' . $return_addr,
  584. ];
  585. // Normal connection requests
  586. } else {
  587. $it = $this->getMissingIntroData($it);
  588. if (empty($it['url'])) {
  589. continue;
  590. }
  591. // Don't show these data until you are connected. Diaspora is doing the same.
  592. if ($it['gnetwork'] === Protocol::DIASPORA) {
  593. $it['glocation'] = "";
  594. $it['gabout'] = "";
  595. $it['ggender'] = "";
  596. }
  597. $intro = [
  598. 'label' => (($it['network'] !== Protocol::OSTATUS) ? 'friend_request' : 'follower'),
  599. 'notify_type' => (($it['network'] !== Protocol::OSTATUS) ? L10n::t('Friend/Connect Request') : L10n::t('New Follower')),
  600. 'dfrn_id' => $it['issued-id'],
  601. 'uid' => $_SESSION['uid'],
  602. 'intro_id' => $it['intro_id'],
  603. 'contact_id' => $it['contact-id'],
  604. 'photo' => (!empty($it['photo']) ? ProxyUtils::proxifyUrl($it['photo'], false, ProxyUtils::SIZE_SMALL) : "images/person-300.jpg"),
  605. 'name' => $it['name'],
  606. 'location' => BBCode::convert($it['glocation'], false),
  607. 'about' => BBCode::convert($it['gabout'], false),
  608. 'keywords' => $it['gkeywords'],
  609. 'gender' => $it['ggender'],
  610. 'hidden' => $it['hidden'] == 1,
  611. 'post_newfriend' => (intval(PConfig::get(local_user(), 'system', 'post_newfriend')) ? '1' : 0),
  612. 'url' => $it['url'],
  613. 'zrl' => Contact::magicLink($it['url']),
  614. 'addr' => $it['gaddr'],
  615. 'network' => $it['gnetwork'],
  616. 'knowyou' => $it['knowyou'],
  617. 'note' => $it['note'],
  618. ];
  619. }
  620. $arr[] = $intro;
  621. }
  622. return $arr;
  623. }
  624. /**
  625. * @brief Check for missing contact data and try to fetch the data from
  626. * from other sources
  627. *
  628. * @param array $arr The input array with the intro data
  629. *
  630. * @return array The array with the intro data
  631. */
  632. private function getMissingIntroData($arr)
  633. {
  634. // If the network and the addr isn't available from the gcontact
  635. // table entry, take the one of the contact table entry
  636. if (empty($arr['gnetwork']) && !empty($arr['network'])) {
  637. $arr['gnetwork'] = $arr['network'];
  638. }
  639. if (empty($arr['gaddr']) && !empty($arr['addr'])) {
  640. $arr['gaddr'] = $arr['addr'];
  641. }
  642. // If the network and addr is still not available
  643. // get the missing data data from other sources
  644. if (empty($arr['gnetwork']) || empty($arr['gaddr'])) {
  645. $ret = Contact::getDetailsByURL($arr['url']);
  646. if (empty($arr['gnetwork']) && !empty($ret['network'])) {
  647. $arr['gnetwork'] = $ret['network'];
  648. }
  649. if (empty($arr['gaddr']) && !empty($ret['addr'])) {
  650. $arr['gaddr'] = $ret['addr'];
  651. }
  652. }
  653. return $arr;
  654. }
  655. }