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.

827 lines
26 KiB

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