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
28 KiB

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