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.
 
 
 
 
 
 

889 lines
27 KiB

  1. <?php
  2. function network_init(App $a) {
  3. if (! local_user()) {
  4. notice( t('Permission denied.') . EOL);
  5. return;
  6. }
  7. $is_a_date_query = false;
  8. if (x($_GET, 'cid') && intval($_GET['cid']) != 0) {
  9. $cid = $_GET['cid'];
  10. }
  11. if ($a->argc > 1) {
  12. for ($x = 1; $x < $a->argc; $x ++) {
  13. if (is_a_date_arg($a->argv[$x])) {
  14. $is_a_date_query = true;
  15. break;
  16. }
  17. }
  18. }
  19. // convert query string to array. remove friendica args
  20. $query_array = array();
  21. $query_string = str_replace($a->cmd."?", "", $a->query_string);
  22. parse_str($query_string, $query_array);
  23. array_shift($query_array);
  24. // fetch last used network view and redirect if needed
  25. if (! $is_a_date_query) {
  26. $sel_tabs = network_query_get_sel_tab($a);
  27. $sel_nets = network_query_get_sel_net();
  28. $sel_groups = network_query_get_sel_group($a);
  29. $last_sel_tabs = get_pconfig(local_user(), 'network.view','tab.selected');
  30. $last_sel_nets = get_pconfig(local_user(), 'network.view', 'net.selected');
  31. $last_sel_groups = get_pconfig(local_user(), 'network.view', 'group.selected');
  32. $remember_tab = ($sel_tabs[0] === 'active' && is_array($last_sel_tabs) && $last_sel_tabs[0] !== 'active');
  33. $remember_net = ($sel_nets === false && $last_sel_nets && $last_sel_nets !== 'all');
  34. $remember_group = ($sel_groups === false && $last_sel_groups && $last_sel_groups != 0);
  35. $net_baseurl = '/network';
  36. $net_args = array();
  37. if ($remember_group) {
  38. $net_baseurl .= '/' . $last_sel_groups; // Note that the group number must come before the "/new" tab selection
  39. } elseif($sel_groups !== false) {
  40. $net_baseurl .= '/' . $sel_groups;
  41. }
  42. if($remember_tab) {
  43. // redirect if current selected tab is '/network' and
  44. // last selected tab is _not_ '/network?f=&order=comment'.
  45. // and this isn't a date query
  46. $tab_baseurls = array(
  47. '', //all
  48. '', //postord
  49. '', //conv
  50. '/new', //new
  51. '', //starred
  52. '', //bookmarked
  53. '', //spam
  54. );
  55. $tab_args = array(
  56. 'f=&order=comment', //all
  57. 'f=&order=post', //postord
  58. 'f=&conv=1', //conv
  59. '', //new
  60. 'f=&star=1', //starred
  61. 'f=&bmark=1', //bookmarked
  62. 'f=&spam=1', //spam
  63. );
  64. $k = array_search('active', $last_sel_tabs);
  65. $net_baseurl .= $tab_baseurls[$k];
  66. // parse out tab queries
  67. $dest_qa = array();
  68. $dest_qs = $tab_args[$k];
  69. parse_str( $dest_qs, $dest_qa);
  70. $net_args = array_merge($net_args, $dest_qa);
  71. }
  72. else if($sel_tabs[4] === 'active') {
  73. // The '/new' tab is selected
  74. $net_baseurl .= '/new';
  75. }
  76. if($remember_net) {
  77. $net_args['nets'] = $last_sel_nets;
  78. }
  79. else if($sel_nets!==false) {
  80. $net_args['nets'] = $sel_nets;
  81. }
  82. if($remember_tab || $remember_net || $remember_group) {
  83. $net_args = array_merge($query_array, $net_args);
  84. $net_queries = build_querystring($net_args);
  85. $redir_url = ($net_queries ? $net_baseurl."?".$net_queries : $net_baseurl);
  86. goaway(App::get_baseurl() . $redir_url);
  87. }
  88. }
  89. // If nets is set to all, unset it
  90. if (x($_GET, 'nets') && $_GET['nets'] === 'all') {
  91. unset($_GET['nets']);
  92. }
  93. $group_id = (($a->argc > 1 && is_numeric($a->argv[1])) ? intval($a->argv[1]) : 0);
  94. set_pconfig(local_user(), 'network.view', 'group.selected', $group_id);
  95. require_once 'include/group.php';
  96. require_once 'include/contact_widgets.php';
  97. require_once 'include/items.php';
  98. require_once 'include/ForumManager.php';
  99. if (! x($a->page, 'aside')) {
  100. $a->page['aside'] = '';
  101. }
  102. $search = ((x($_GET, 'search')) ? escape_tags($_GET['search']) : '');
  103. if (x($_GET, 'save')) {
  104. $r = qu("SELECT * FROM `search` WHERE `uid` = %d AND `term` = '%s' LIMIT 1",
  105. intval(local_user()),
  106. dbesc($search)
  107. );
  108. if (! dbm::is_result($r)) {
  109. q("INSERT INTO `search` ( `uid`,`term` ) VALUES ( %d, '%s') ",
  110. intval(local_user()),
  111. dbesc($search)
  112. );
  113. }
  114. }
  115. if (x($_GET, 'remove')) {
  116. q("DELETE FROM `search` WHERE `uid` = %d AND `term` = '%s'",
  117. intval(local_user()),
  118. dbesc($search)
  119. );
  120. }
  121. // search terms header
  122. if (x($_GET, 'search')) {
  123. $a->page['content'] .= replace_macros(get_markup_template("section_title.tpl"),array(
  124. '$title' => sprintf( t('Results for: %s'), $search)
  125. ));
  126. }
  127. $a->page['aside'] .= (feature_enabled(local_user(),'groups') ? group_side('network/0','network','standard',$group_id) : '');
  128. $a->page['aside'] .= (feature_enabled(local_user(),'forumlist_widget') ? ForumManager::widget(local_user(),$cid) : '');
  129. $a->page['aside'] .= posted_date_widget('network',local_user(),false);
  130. $a->page['aside'] .= networks_widget('network',(x($_GET, 'nets') ? $_GET['nets'] : ''));
  131. $a->page['aside'] .= saved_searches($search);
  132. $a->page['aside'] .= fileas_widget('network',(x($_GET, 'file') ? $_GET['file'] : ''));
  133. }
  134. function saved_searches($search) {
  135. if (! feature_enabled(local_user(),'savedsearch')) {
  136. return '';
  137. }
  138. $a = get_app();
  139. $srchurl = '/network?f='
  140. . ((x($_GET,'cid')) ? '&cid=' . $_GET['cid'] : '')
  141. . ((x($_GET,'star')) ? '&star=' . $_GET['star'] : '')
  142. . ((x($_GET,'bmark')) ? '&bmark=' . $_GET['bmark'] : '')
  143. . ((x($_GET,'conv')) ? '&conv=' . $_GET['conv'] : '')
  144. . ((x($_GET,'nets')) ? '&nets=' . $_GET['nets'] : '')
  145. . ((x($_GET,'cmin')) ? '&cmin=' . $_GET['cmin'] : '')
  146. . ((x($_GET,'cmax')) ? '&cmax=' . $_GET['cmax'] : '')
  147. . ((x($_GET,'file')) ? '&file=' . $_GET['file'] : '');
  148. ;
  149. $o = '';
  150. $r = qu("SELECT `id`,`term` FROM `search` WHERE `uid` = %d",
  151. intval(local_user())
  152. );
  153. $saved = array();
  154. if (dbm::is_result($r)) {
  155. foreach ($r as $rr) {
  156. $saved[] = array(
  157. 'id' => $rr['id'],
  158. 'term' => $rr['term'],
  159. 'encodedterm' => urlencode($rr['term']),
  160. 'delete' => t('Remove term'),
  161. 'selected' => ($search==$rr['term']),
  162. );
  163. }
  164. }
  165. $tpl = get_markup_template("saved_searches_aside.tpl");
  166. $o = replace_macros($tpl, array(
  167. '$title' => t('Saved Searches'),
  168. '$add' => t('add'),
  169. '$searchbox' => search($search,'netsearch-box',$srchurl,true),
  170. '$saved' => $saved,
  171. ));
  172. return $o;
  173. }
  174. /**
  175. * Return selected tab from query
  176. *
  177. * urls -> returns
  178. * '/network' => $no_active = 'active'
  179. * '/network?f=&order=comment' => $comment_active = 'active'
  180. * '/network?f=&order=post' => $postord_active = 'active'
  181. * '/network?f=&conv=1', => $conv_active = 'active'
  182. * '/network/new', => $new_active = 'active'
  183. * '/network?f=&star=1', => $starred_active = 'active'
  184. * '/network?f=&bmark=1', => $bookmarked_active = 'active'
  185. * '/network?f=&spam=1', => $spam_active = 'active'
  186. *
  187. * @return Array ( $no_active, $comment_active, $postord_active, $conv_active, $new_active, $starred_active, $bookmarked_active, $spam_active );
  188. */
  189. function network_query_get_sel_tab(App $a) {
  190. $no_active='';
  191. $starred_active = '';
  192. $new_active = '';
  193. $bookmarked_active = '';
  194. $all_active = '';
  195. $search_active = '';
  196. $conv_active = '';
  197. $spam_active = '';
  198. $postord_active = '';
  199. if(($a->argc > 1 && $a->argv[1] === 'new')
  200. || ($a->argc > 2 && $a->argv[2] === 'new')) {
  201. $new_active = 'active';
  202. }
  203. if(x($_GET,'search')) {
  204. $search_active = 'active';
  205. }
  206. if(x($_GET,'star')) {
  207. $starred_active = 'active';
  208. }
  209. if(x($_GET,'bmark')) {
  210. $bookmarked_active = 'active';
  211. }
  212. if(x($_GET,'conv')) {
  213. $conv_active = 'active';
  214. }
  215. if(x($_GET,'spam')) {
  216. $spam_active = 'active';
  217. }
  218. if (($new_active == '')
  219. && ($starred_active == '')
  220. && ($bookmarked_active == '')
  221. && ($conv_active == '')
  222. && ($search_active == '')
  223. && ($spam_active == '')) {
  224. $no_active = 'active';
  225. }
  226. if ($no_active=='active' && x($_GET,'order')) {
  227. switch($_GET['order']){
  228. case 'post': $postord_active = 'active'; $no_active=''; break;
  229. case 'comment' : $all_active = 'active'; $no_active=''; break;
  230. }
  231. }
  232. return array($no_active, $all_active, $postord_active, $conv_active, $new_active, $starred_active, $bookmarked_active, $spam_active);
  233. }
  234. /**
  235. * @brief Return selected network from query
  236. * @return string Name of the selected network
  237. */
  238. function network_query_get_sel_net() {
  239. $network = false;
  240. if(x($_GET,'nets')) {
  241. $network = $_GET['nets'];
  242. }
  243. return $network;
  244. }
  245. function network_query_get_sel_group(App $a) {
  246. $group = false;
  247. if($a->argc >= 2 && is_numeric($a->argv[1])) {
  248. $group = $a->argv[1];
  249. }
  250. return $group;
  251. }
  252. function network_content(App $a, $update = 0) {
  253. require_once('include/conversation.php');
  254. if (! local_user()) {
  255. $_SESSION['return_url'] = $a->query_string;
  256. return login(false);
  257. }
  258. // Rawmode is used for fetching new content at the end of the page
  259. $rawmode = (isset($_GET["mode"]) AND ($_GET["mode"] == "raw"));
  260. /// @TODO Is this really necessary? $a is already available to hooks
  261. $arr = array('query' => $a->query_string);
  262. call_hooks('network_content_init', $arr);
  263. $datequery = $datequery2 = '';
  264. $group = 0;
  265. $nouveau = false;
  266. if($a->argc > 1) {
  267. for($x = 1; $x < $a->argc; $x ++) {
  268. if(is_a_date_arg($a->argv[$x])) {
  269. if($datequery)
  270. $datequery2 = escape_tags($a->argv[$x]);
  271. else {
  272. $datequery = escape_tags($a->argv[$x]);
  273. $_GET['order'] = 'post';
  274. }
  275. }
  276. elseif($a->argv[$x] === 'new') {
  277. $nouveau = true;
  278. }
  279. elseif(intval($a->argv[$x])) {
  280. $group = intval($a->argv[$x]);
  281. $def_acl = array('allow_gid' => '<' . $group . '>');
  282. }
  283. }
  284. }
  285. $o = '';
  286. $contact_id = $a->cid;
  287. require_once('include/acl_selectors.php');
  288. $cid = ((x($_GET,'cid')) ? intval($_GET['cid']) : 0);
  289. $star = ((x($_GET,'star')) ? intval($_GET['star']) : 0);
  290. $bmark = ((x($_GET,'bmark')) ? intval($_GET['bmark']) : 0);
  291. $order = ((x($_GET,'order')) ? notags($_GET['order']) : 'comment');
  292. $liked = ((x($_GET,'liked')) ? intval($_GET['liked']) : 0);
  293. $conv = ((x($_GET,'conv')) ? intval($_GET['conv']) : 0);
  294. $spam = ((x($_GET,'spam')) ? intval($_GET['spam']) : 0);
  295. $nets = ((x($_GET,'nets')) ? $_GET['nets'] : '');
  296. $cmin = ((x($_GET,'cmin')) ? intval($_GET['cmin']) : 0);
  297. $cmax = ((x($_GET,'cmax')) ? intval($_GET['cmax']) : 99);
  298. $file = ((x($_GET,'file')) ? $_GET['file'] : '');
  299. if(x($_GET,'search') || x($_GET,'file'))
  300. $nouveau = true;
  301. if($cid)
  302. $def_acl = array('allow_cid' => '<' . intval($cid) . '>');
  303. if($nets) {
  304. $r = qu("SELECT `id` FROM `contact` WHERE `uid` = %d AND network = '%s' AND `self` = 0",
  305. intval(local_user()),
  306. dbesc($nets)
  307. );
  308. $str = '';
  309. if (dbm::is_result($r))
  310. foreach($r as $rr)
  311. $str .= '<' . $rr['id'] . '>';
  312. if(strlen($str))
  313. $def_acl = array('allow_cid' => $str);
  314. }
  315. set_pconfig(local_user(), 'network.view', 'net.selected', ($nets ? $nets : 'all'));
  316. if(!$update AND !$rawmode) {
  317. $tabs = network_tabs($a);
  318. $o .= $tabs;
  319. if($group) {
  320. if(($t = group_public_members($group)) && (! get_pconfig(local_user(),'system','nowarn_insecure'))) {
  321. notice(sprintf(tt("Warning: This group contains %s member from a network that doesn't allow non public messages.",
  322. "Warning: This group contains %s members from a network that doesn't allow non public messages.",
  323. $t), $t).EOL);
  324. notice(t("Messages in this group won't be send to these receivers.").EOL);
  325. }
  326. }
  327. nav_set_selected('network');
  328. $content = "";
  329. if ($cid) {
  330. // If $cid belongs to a communitity forum or a privat goup,.add a mention to the status editor
  331. $contact = qu("SELECT `nick` FROM `contact` WHERE `id` = %d AND `uid` = %d AND (`forum` OR `prv`) ",
  332. intval($cid),
  333. intval(local_user())
  334. );
  335. if ($contact)
  336. $content = "@".$contact[0]["nick"]."+".$cid;
  337. }
  338. $x = array(
  339. 'is_owner' => true,
  340. 'allow_location' => $a->user['allow_location'],
  341. 'default_location' => $a->user['default-location'],
  342. 'nickname' => $a->user['nickname'],
  343. 'lockstate'=> ((($group) || ($cid) || ($nets) || (is_array($a->user) &&
  344. ((strlen($a->user['allow_cid'])) || (strlen($a->user['allow_gid'])) ||
  345. (strlen($a->user['deny_cid'])) || (strlen($a->user['deny_gid']))))) ? 'lock' : 'unlock'),
  346. 'default_perms' => get_acl_permissions($a->user),
  347. 'acl' => populate_acl((($group || $cid || $nets) ? $def_acl : $a->user), true),
  348. 'bang' => (($group || $cid || $nets) ? '!' : ''),
  349. 'visitor' => 'block',
  350. 'profile_uid' => local_user(),
  351. 'acl_data' => construct_acl_data($a, $a->user), // For non-Javascript ACL selector
  352. 'content' => $content,
  353. );
  354. $o .= status_editor($a,$x);
  355. }
  356. // We don't have to deal with ACLs on this page. You're looking at everything
  357. // that belongs to you, hence you can see all of it. We will filter by group if
  358. // desired.
  359. $sql_post_table = "";
  360. $sql_options = (($star) ? " AND `thread`.`starred` " : '');
  361. $sql_options .= (($bmark) ? " AND `thread`.`bookmark` " : '');
  362. $sql_extra = $sql_options;
  363. $sql_extra2 = "";
  364. $sql_extra3 = "";
  365. $sql_table = "`thread`";
  366. $sql_parent = "`iid`";
  367. if ($nouveau OR strlen($file) OR $update) {
  368. $sql_table = "`item`";
  369. $sql_parent = "`parent`";
  370. $sql_post_table = " INNER JOIN `thread` ON `thread`.`iid` = `item`.`parent`";
  371. }
  372. $sql_nets = (($nets) ? sprintf(" and $sql_table.`network` = '%s' ", dbesc($nets)) : '');
  373. if($group) {
  374. $r = qu("SELECT `name`, `id` FROM `group` WHERE `id` = %d AND `uid` = %d LIMIT 1",
  375. intval($group),
  376. intval($_SESSION['uid'])
  377. );
  378. if (! dbm::is_result($r)) {
  379. if($update)
  380. killme();
  381. notice( t('No such group') . EOL );
  382. goaway('network/0');
  383. // NOTREACHED
  384. }
  385. $contacts = expand_groups(array($group));
  386. $gcontacts = expand_groups(array($group), false, true);
  387. if((is_array($contacts)) && count($contacts)) {
  388. $contact_str_self = "";
  389. $gcontact_str_self = "";
  390. $contact_str = implode(',',$contacts);
  391. $gcontact_str = implode(',',$gcontacts);
  392. $self = qu("SELECT `contact`.`id`, `gcontact`.`id` AS `gid` FROM `contact`
  393. INNER JOIN `gcontact` ON `gcontact`.`nurl` = `contact`.`nurl`
  394. WHERE `uid` = %d AND `self`", intval($_SESSION['uid']));
  395. if (count($self)) {
  396. $contact_str_self = $self[0]["id"];
  397. $gcontact_str_self = $self[0]["gid"];
  398. }
  399. $sql_post_table .= " INNER JOIN `item` AS `temp1` ON `temp1`.`id` = ".$sql_table.".".$sql_parent;
  400. $sql_extra3 .= " AND (`thread`.`contact-id` IN ($contact_str) ";
  401. $sql_extra3 .= " OR (`thread`.`contact-id` = '$contact_str_self' AND `temp1`.`allow_gid` LIKE '".protect_sprintf('%<'.intval($group).'>%')."' AND `temp1`.`private`))";
  402. } else {
  403. $sql_extra3 .= " AND false ";
  404. info( t('Group is empty'));
  405. }
  406. $o = replace_macros(get_markup_template("section_title.tpl"),array(
  407. '$title' => sprintf( t('Group: %s'), $r[0]['name'])
  408. )) . $o;
  409. }
  410. elseif($cid) {
  411. $r = qu("SELECT `id`,`name`,`network`,`writable`,`nurl`, `forum`, `prv`, `contact-type`, `addr`, `thumb`, `location` FROM `contact` WHERE `id` = %d
  412. AND (NOT `blocked` OR `pending`) LIMIT 1",
  413. intval($cid)
  414. );
  415. if (dbm::is_result($r)) {
  416. $sql_extra = " AND ".$sql_table.".`contact-id` = ".intval($cid);
  417. $entries[0] = array(
  418. 'id' => 'network',
  419. 'name' => htmlentities($r[0]['name']),
  420. 'itemurl' => (($r[0]['addr']) ? ($r[0]['addr']) : ($r[0]['nurl'])),
  421. 'thumb' => proxy_url($r[0]['thumb'], false, PROXY_SIZE_THUMB),
  422. 'details' => $r[0]['location'],
  423. );
  424. $entries[0]["account_type"] = account_type($r[0]);
  425. $o = replace_macros(get_markup_template("viewcontact_template.tpl"),array(
  426. 'contacts' => $entries,
  427. 'id' => 'network',
  428. )) . $o;
  429. if($r[0]['network'] === NETWORK_OSTATUS && $r[0]['writable'] && (! get_pconfig(local_user(),'system','nowarn_insecure'))) {
  430. notice( t('Private messages to this person are at risk of public disclosure.') . EOL);
  431. }
  432. }
  433. else {
  434. notice( t('Invalid contact.') . EOL);
  435. goaway('network');
  436. // NOTREACHED
  437. }
  438. }
  439. if((! $group) && (! $cid) && (! $update) && (! get_config('theme','hide_eventlist'))) {
  440. $o .= get_birthdays();
  441. $o .= get_events();
  442. }
  443. if($datequery) {
  444. $sql_extra3 .= protect_sprintf(sprintf(" AND $sql_table.created <= '%s' ", dbesc(datetime_convert(date_default_timezone_get(),'',$datequery))));
  445. }
  446. if($datequery2) {
  447. $sql_extra3 .= protect_sprintf(sprintf(" AND $sql_table.created >= '%s' ", dbesc(datetime_convert(date_default_timezone_get(),'',$datequery2))));
  448. }
  449. //$sql_extra2 = (($nouveau) ? '' : " AND `item`.`parent` = `item`.`id` ");
  450. $sql_extra2 = (($nouveau) ? '' : $sql_extra2);
  451. $sql_extra3 = (($nouveau) ? '' : $sql_extra3);
  452. $sql_order = "";
  453. $order_mode = "received";
  454. $tag = false;
  455. if(x($_GET,'search')) {
  456. $search = escape_tags($_GET['search']);
  457. if(strpos($search,'#') === 0) {
  458. $tag = true;
  459. $search = substr($search,1);
  460. }
  461. if (get_config('system','only_tag_search'))
  462. $tag = true;
  463. if($tag) {
  464. $sql_extra = "";
  465. $sql_post_table .= sprintf("INNER JOIN (SELECT `oid` FROM `term` WHERE `term` = '%s' AND `otype` = %d AND `type` = %d AND `uid` = %d ORDER BY `tid` DESC) AS `term` ON `item`.`id` = `term`.`oid` ",
  466. dbesc(protect_sprintf($search)), intval(TERM_OBJ_POST), intval(TERM_HASHTAG), intval(local_user()));
  467. $sql_order = "`item`.`id`";
  468. $order_mode = "id";
  469. } else {
  470. // Disabled until final decision what to do with this
  471. //if (get_config('system','use_fulltext_engine'))
  472. // $sql_extra = sprintf(" AND MATCH (`item`.`body`, `item`.`title`) AGAINST ('%s' in boolean mode) ", dbesc(protect_sprintf($search)));
  473. //else
  474. $sql_extra = sprintf(" AND `item`.`body` REGEXP '%s' ", dbesc(protect_sprintf(preg_quote($search))));
  475. $sql_order = "`item`.`id`";
  476. $order_mode = "id";
  477. }
  478. }
  479. if(strlen($file)) {
  480. $sql_post_table .= sprintf("INNER JOIN (SELECT `oid` FROM `term` WHERE `term` = '%s' AND `otype` = %d AND `type` = %d AND `uid` = %d ORDER BY `tid` DESC) AS `term` ON `item`.`id` = `term`.`oid` ",
  481. dbesc(protect_sprintf($file)), intval(TERM_OBJ_POST), intval(TERM_FILE), intval(local_user()));
  482. $sql_order = "`item`.`id`";
  483. $order_mode = "id";
  484. }
  485. if($conv)
  486. $sql_extra3 .= " AND $sql_table.`mention`";
  487. if($update) {
  488. // only setup pagination on initial page view
  489. $pager_sql = '';
  490. } else {
  491. // check if we serve a mobile device and get the user settings
  492. // accordingly
  493. if ($a->is_mobile) {
  494. $itemspage_network = get_pconfig(local_user(),'system','itemspage_mobile_network');
  495. $itemspage_network = ((intval($itemspage_network)) ? $itemspage_network : 20);
  496. } else {
  497. $itemspage_network = get_pconfig(local_user(),'system','itemspage_network');
  498. $itemspage_network = ((intval($itemspage_network)) ? $itemspage_network : 40);
  499. }
  500. // now that we have the user settings, see if the theme forces
  501. // a maximum item number which is lower then the user choice
  502. if(($a->force_max_items > 0) && ($a->force_max_items < $itemspage_network))
  503. $itemspage_network = $a->force_max_items;
  504. $a->set_pager_itemspage($itemspage_network);
  505. $pager_sql = sprintf(" LIMIT %d, %d ",intval($a->pager['start']), intval($a->pager['itemspage']));
  506. }
  507. if($nouveau) {
  508. $simple_update = (($update) ? " AND `item`.`unseen` " : '');
  509. if ($sql_order == "")
  510. $sql_order = "`item`.`id`";
  511. // "New Item View" - show all items unthreaded in reverse created date order
  512. $items = qu("SELECT %s FROM $sql_table $sql_post_table %s
  513. WHERE %s AND `item`.`uid` = %d
  514. $simple_update
  515. $sql_extra $sql_nets
  516. ORDER BY $sql_order DESC $pager_sql ",
  517. item_fieldlists(), item_joins(), item_condition(),
  518. intval($_SESSION['uid'])
  519. );
  520. $update_unseen = ' WHERE uid = ' . intval($_SESSION['uid']) . " AND unseen = 1 $sql_extra $sql_nets";
  521. } else {
  522. // Normal conversation view
  523. if($order === 'post') {
  524. $ordering = "`created`";
  525. if ($sql_order == "")
  526. $order_mode = "created";
  527. } else {
  528. $ordering = "`commented`";
  529. if ($sql_order == "")
  530. $order_mode = "commented";
  531. }
  532. if ($sql_order == "")
  533. $sql_order = "$sql_table.$ordering";
  534. if (($_GET["offset"] != ""))
  535. $sql_extra3 .= sprintf(" AND $sql_order <= '%s'", dbesc($_GET["offset"]));
  536. // Fetch a page full of parent items for this page
  537. if($update) {
  538. if (get_config("system", "like_no_comment"))
  539. $sql_extra4 = " AND `item`.`verb` = '".ACTIVITY_POST."'";
  540. else
  541. $sql_extra4 = "";
  542. $r = qu("SELECT `item`.`parent` AS `item_id`, `item`.`network` AS `item_network`, `contact`.`uid` AS `contact_uid`
  543. FROM $sql_table $sql_post_table INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
  544. AND (NOT `contact`.`blocked` OR `contact`.`pending`)
  545. WHERE `item`.`uid` = %d AND `item`.`visible` AND NOT `item`.`deleted` $sql_extra4
  546. AND NOT `item`.`moderated` AND `item`.`unseen`
  547. $sql_extra3 $sql_extra $sql_nets
  548. ORDER BY `item_id` DESC LIMIT 100",
  549. intval(local_user())
  550. );
  551. } else {
  552. $r = qu("SELECT `thread`.`iid` AS `item_id`, `thread`.`network` AS `item_network`, `contact`.`uid` AS `contact_uid`
  553. FROM $sql_table $sql_post_table STRAIGHT_JOIN `contact` ON `contact`.`id` = `thread`.`contact-id`
  554. AND (NOT `contact`.`blocked` OR `contact`.`pending`)
  555. WHERE `thread`.`uid` = %d AND `thread`.`visible` AND NOT `thread`.`deleted`
  556. AND NOT `thread`.`moderated`
  557. $sql_extra2 $sql_extra3 $sql_extra $sql_nets
  558. ORDER BY $sql_order DESC $pager_sql ",
  559. intval(local_user())
  560. );
  561. }
  562. // Then fetch all the children of the parents that are on this page
  563. $parents_arr = array();
  564. $parents_str = '';
  565. $date_offset = "";
  566. if (dbm::is_result($r)) {
  567. foreach($r as $rr)
  568. if(! in_array($rr['item_id'],$parents_arr))
  569. $parents_arr[] = $rr['item_id'];
  570. $parents_str = implode(", ", $parents_arr);
  571. // splitted into separate queries to avoid the problem with very long threads
  572. // so always the last X comments are loaded
  573. // This problem can occur expecially with imported facebook posts
  574. $max_comments = get_config("system", "max_comments");
  575. if ($max_comments == 0)
  576. $max_comments = 100;
  577. $items = array();
  578. foreach ($parents_arr AS $parents) {
  579. $thread_items = qu(item_query()." AND `item`.`uid` = %d
  580. AND `item`.`parent` = %d
  581. ORDER BY `item`.`commented` DESC LIMIT %d",
  582. intval(local_user()),
  583. intval($parents),
  584. intval($max_comments + 1)
  585. );
  586. if (dbm::is_result($thread_items))
  587. $items = array_merge($items, $thread_items);
  588. }
  589. $items = conv_sort($items,$ordering);
  590. } else {
  591. $items = array();
  592. }
  593. if ($_GET["offset"] == "")
  594. $date_offset = $items[0][$order_mode];
  595. else
  596. $date_offset = $_GET["offset"];
  597. $a->page_offset = $date_offset;
  598. if($parents_str)
  599. $update_unseen = ' WHERE uid = ' . intval(local_user()) . ' AND unseen = 1 AND parent IN ( ' . dbesc($parents_str) . ' )';
  600. }
  601. // We aren't going to try and figure out at the item, group, and page
  602. // level which items you've seen and which you haven't. If you're looking
  603. // at the top level network page just mark everything seen.
  604. // The $update_unseen is a bit unreliable if you have stuff coming into your stream from a new contact -
  605. // and other feeds that bring in stuff from the past. One can't find it all.
  606. // I'm reviving this block to mark everything seen on page 1 of the network as a temporary measure.
  607. // The correct solution is to implement a network notifications box just like the system notifications popup
  608. // with the ability in the popup to "mark all seen".
  609. // Several people are complaining because there are unseen messages they can't find and as time goes
  610. // on they just get buried deeper. It has happened to me a couple of times also.
  611. if (!$group && !$cid && !$star) {
  612. $unseen = q("SELECT `id` FROM `item` WHERE `unseen` AND `uid` = %d LIMIT 1",
  613. intval(local_user()));
  614. if (dbm::is_result($unseen)) {
  615. $r = q("UPDATE `item` SET `unseen` = 0
  616. WHERE `unseen` = 1 AND `uid` = %d",
  617. intval(local_user())
  618. );
  619. }
  620. } elseif ($update_unseen) {
  621. $unseen = q("SELECT `id` FROM `item` ".$update_unseen. " LIMIT 1");
  622. if (dbm::is_result($unseen)) {
  623. $r = q("UPDATE `item` SET `unseen` = 0 $update_unseen");
  624. }
  625. }
  626. // Set this so that the conversation function can find out contact info for our wall-wall items
  627. $a->page_contact = $a->contact;
  628. $mode = (($nouveau) ? 'network-new' : 'network');
  629. $o .= conversation($a, $items, $mode, $update);
  630. if (!$update) {
  631. if (get_pconfig(local_user(), 'system', 'infinite_scroll')) {
  632. $o .= scroll_loader();
  633. } else {
  634. $o .= alt_pager($a, count($items));
  635. }
  636. }
  637. return $o;
  638. }
  639. /**
  640. * @brief Get the network tabs menu
  641. *
  642. * @param app $a The global App
  643. * @return string Html of the networktab
  644. */
  645. function network_tabs(App $a) {
  646. // item filter tabs
  647. /// @TODO fix this logic, reduce duplication
  648. /// $a->page['content'] .= '<div class="tabs-wrapper">';
  649. list($no_active, $all_active, $postord_active, $conv_active, $new_active, $starred_active, $bookmarked_active, $spam_active) = network_query_get_sel_tab($a);
  650. // if no tabs are selected, defaults to comments
  651. if ($no_active=='active') $all_active='active';
  652. $cmd = (($datequery) ? '' : $a->cmd);
  653. $len_naked_cmd = strlen(str_replace('/new','',$cmd));
  654. // tabs
  655. $tabs = array(
  656. array(
  657. 'label' => t('Commented Order'),
  658. 'url' => str_replace('/new', '', $cmd) . '?f=&order=comment' . ((x($_GET,'cid')) ? '&cid=' . $_GET['cid'] : ''),
  659. 'sel' => $all_active,
  660. 'title' => t('Sort by Comment Date'),
  661. 'id' => 'commented-order-tab',
  662. 'accesskey' => "e",
  663. ),
  664. array(
  665. 'label' => t('Posted Order'),
  666. 'url' => str_replace('/new', '', $cmd) . '?f=&order=post' . ((x($_GET,'cid')) ? '&cid=' . $_GET['cid'] : ''),
  667. 'sel' => $postord_active,
  668. 'title' => t('Sort by Post Date'),
  669. 'id' => 'posted-order-tab',
  670. 'accesskey' => "t",
  671. ),
  672. );
  673. if(feature_enabled(local_user(),'personal_tab')) {
  674. $tabs[] = array(
  675. 'label' => t('Personal'),
  676. 'url' => str_replace('/new', '', $cmd) . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : '/?f=') . '&conv=1',
  677. 'sel' => $conv_active,
  678. 'title' => t('Posts that mention or involve you'),
  679. 'id' => 'personal-tab',
  680. 'accesskey' => "r",
  681. );
  682. }
  683. if(feature_enabled(local_user(),'new_tab')) {
  684. $tabs[] = array(
  685. 'label' => t('New'),
  686. 'url' => str_replace('/new', '', $cmd) . ($len_naked_cmd ? '/' : '') . 'new' . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : ''),
  687. 'sel' => $new_active,
  688. 'title' => t('Activity Stream - by date'),
  689. 'id' => 'activitiy-by-date-tab',
  690. 'accesskey' => "w",
  691. );
  692. }
  693. if(feature_enabled(local_user(),'link_tab')) {
  694. $tabs[] = array(
  695. 'label' => t('Shared Links'),
  696. 'url' => str_replace('/new', '', $cmd) . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : '/?f=') . '&bmark=1',
  697. 'sel' => $bookmarked_active,
  698. 'title' => t('Interesting Links'),
  699. 'id' => 'shared-links-tab',
  700. 'accesskey' => "b",
  701. );
  702. }
  703. if(feature_enabled(local_user(),'star_posts')) {
  704. $tabs[] = array(
  705. 'label' => t('Starred'),
  706. 'url' => str_replace('/new', '', $cmd) . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : '/?f=') . '&star=1',
  707. 'sel' => $starred_active,
  708. 'title' => t('Favourite Posts'),
  709. 'id' => 'starred-posts-tab',
  710. 'accesskey' => "m",
  711. );
  712. }
  713. // save selected tab, but only if not in search or file mode
  714. if(!x($_GET,'search') && !x($_GET,'file')) {
  715. set_pconfig( local_user(), 'network.view','tab.selected',array($all_active, $postord_active, $conv_active, $new_active, $starred_active, $bookmarked_active, $spam_active) );
  716. }
  717. $arr = array('tabs' => $tabs);
  718. call_hooks('network_tabs', $arr);
  719. $tpl = get_markup_template('common_tabs.tpl');
  720. return replace_macros($tpl, array('$tabs' => $arr['tabs']));
  721. // --- end item filter tabs
  722. }