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.
 
 
 
 
 
 

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