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.
 
 
 
 
 
 

886 lines
28 KiB

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