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.
 
 
 
 
 
 

532 lines
16 KiB

  1. <?php
  2. function network_init(&$a) {
  3. if(! local_user()) {
  4. notice( t('Permission denied.') . EOL);
  5. return;
  6. }
  7. $group_id = (($a->argc > 1 && intval($a->argv[1])) ? intval($a->argv[1]) : 0);
  8. require_once('include/group.php');
  9. require_once('include/contact_widgets.php');
  10. if(! x($a->page,'aside'))
  11. $a->page['aside'] = '';
  12. $search = ((x($_GET,'search')) ? escape_tags($_GET['search']) : '');
  13. if(x($_GET,'save')) {
  14. $r = q("select * from `search` where `uid` = %d and `term` = '%s' limit 1",
  15. intval(local_user()),
  16. dbesc($search)
  17. );
  18. if(! count($r)) {
  19. q("insert into `search` ( `uid`,`term` ) values ( %d, '%s') ",
  20. intval(local_user()),
  21. dbesc($search)
  22. );
  23. }
  24. }
  25. if(x($_GET,'remove')) {
  26. q("delete from `search` where `uid` = %d and `term` = '%s' limit 1",
  27. intval(local_user()),
  28. dbesc($search)
  29. );
  30. }
  31. // search terms header
  32. if(x($_GET,'search')) {
  33. $a->page['content'] .= '<h2>' . t('Search Results For:') . ' ' . $search . '</h2>';
  34. }
  35. $a->page['aside'] .= group_side('network','network',true,$group_id);
  36. $a->page['aside'] .= networks_widget($a->get_baseurl() . '/network',(x($_GET, 'nets') ? $_GET['nets'] : ''));
  37. $a->page['aside'] .= saved_searches($search);
  38. $a->page['aside'] .= fileas_widget($a->get_baseurl() . '/network',(x($_GET, 'file') ? $_GET['file'] : ''));
  39. }
  40. function saved_searches($search) {
  41. $srchurl = '/network?f='
  42. . ((x($_GET,'cid')) ? '&cid=' . $_GET['cid'] : '')
  43. . ((x($_GET,'star')) ? '&star=' . $_GET['star'] : '')
  44. . ((x($_GET,'bmark')) ? '&bmark=' . $_GET['bmark'] : '')
  45. . ((x($_GET,'conv')) ? '&conv=' . $_GET['conv'] : '')
  46. . ((x($_GET,'nets')) ? '&nets=' . $_GET['nets'] : '')
  47. . ((x($_GET,'cmin')) ? '&cmin=' . $_GET['cmin'] : '')
  48. . ((x($_GET,'cmax')) ? '&cmax=' . $_GET['cmax'] : '')
  49. . ((x($_GET,'file')) ? '&file=' . $_GET['file'] : '');
  50. ;
  51. $o = '';
  52. $r = q("select `id`,`term` from `search` WHERE `uid` = %d",
  53. intval(local_user())
  54. );
  55. $saved = array();
  56. if(count($r)) {
  57. foreach($r as $rr) {
  58. $saved[] = array(
  59. 'id' => $rr['id'],
  60. 'term' => $rr['term'],
  61. 'encodedterm' => urlencode($rr['term']),
  62. 'delete' => t('Remove term'),
  63. 'selected' => ($search==$rr['term']),
  64. );
  65. }
  66. }
  67. $tpl = get_markup_template("saved_searches_aside.tpl");
  68. $o = replace_macros($tpl, array(
  69. '$title' => t('Saved Searches'),
  70. '$add' => t('add'),
  71. '$searchbox' => search($search,'netsearch-box',$srchurl,true),
  72. '$saved' => $saved,
  73. ));
  74. return $o;
  75. }
  76. function network_content(&$a, $update = 0) {
  77. require_once('include/conversation.php');
  78. if(! local_user()) {
  79. $_SESSION['return_url'] = $a->query_string;
  80. return login(false);
  81. }
  82. $o = '';
  83. // item filter tabs
  84. // TODO: fix this logic, reduce duplication
  85. //$a->page['content'] .= '<div class="tabs-wrapper">';
  86. $starred_active = '';
  87. $new_active = '';
  88. $bookmarked_active = '';
  89. $all_active = '';
  90. $search_active = '';
  91. $conv_active = '';
  92. $spam_active = '';
  93. if(($a->argc > 1 && $a->argv[1] === 'new')
  94. || ($a->argc > 2 && $a->argv[2] === 'new')) {
  95. $new_active = 'active';
  96. }
  97. if(x($_GET,'search')) {
  98. $search_active = 'active';
  99. }
  100. if(x($_GET,'star')) {
  101. $starred_active = 'active';
  102. }
  103. if(x($_GET,'bmark')) {
  104. $bookmarked_active = 'active';
  105. }
  106. if(x($_GET,'conv')) {
  107. $conv_active = 'active';
  108. }
  109. if(x($_GET,'spam')) {
  110. $spam_active = 'active';
  111. }
  112. if (($new_active == '')
  113. && ($starred_active == '')
  114. && ($bookmarked_active == '')
  115. && ($conv_active == '')
  116. && ($search_active == '')
  117. && ($spam_active == '')) {
  118. $all_active = 'active';
  119. }
  120. $postord_active = '';
  121. if($all_active && x($_GET,'order') && $_GET['order'] !== 'comment') {
  122. $all_active = '';
  123. $postord_active = 'active';
  124. }
  125. // tabs
  126. $tabs = array(
  127. array(
  128. 'label' => t('Commented Order'),
  129. 'url'=>$a->get_baseurl() . '/' . str_replace('/new', '', $a->cmd) . ((x($_GET,'cid')) ? '?f=&cid=' . $_GET['cid'] : ''),
  130. 'sel'=>$all_active,
  131. ),
  132. array(
  133. 'label' => t('Posted Order'),
  134. 'url'=>$a->get_baseurl() . '/' . str_replace('/new', '', $a->cmd) . '?f=&order=post' . ((x($_GET,'cid')) ? '&cid=' . $_GET['cid'] : ''),
  135. 'sel'=>$postord_active,
  136. ),
  137. array(
  138. 'label' => t('Personal'),
  139. 'url' => $a->get_baseurl() . '/' . str_replace('/new', '', $a->cmd) . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : '') . '&conv=1',
  140. 'sel' => $conv_active,
  141. ),
  142. array(
  143. 'label' => t('New'),
  144. 'url' => $a->get_baseurl() . '/' . str_replace('/new', '', $a->cmd) . '/new' . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : ''),
  145. 'sel' => $new_active,
  146. ),
  147. array(
  148. 'label' => t('Starred'),
  149. 'url'=>$a->get_baseurl() . '/' . str_replace('/new', '', $a->cmd) . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : '') . '&star=1',
  150. 'sel'=>$starred_active,
  151. ),
  152. array(
  153. 'label' => t('Bookmarks'),
  154. 'url'=>$a->get_baseurl() . '/' . str_replace('/new', '', $a->cmd) . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : '') . '&bmark=1',
  155. 'sel'=>$bookmarked_active,
  156. ),
  157. // array(
  158. // 'label' => t('Spam'),
  159. // 'url'=>$a->get_baseurl() . '/network?f=&spam=1'
  160. // 'sel'=> $spam_active,
  161. // ),
  162. );
  163. $tpl = get_markup_template('common_tabs.tpl');
  164. $o .= replace_macros($tpl, array('$tabs'=>$tabs));
  165. // --- end item filter tabs
  166. $contact_id = $a->cid;
  167. $group = 0;
  168. $nouveau = false;
  169. require_once('include/acl_selectors.php');
  170. $cid = ((x($_GET,'cid')) ? intval($_GET['cid']) : 0);
  171. $star = ((x($_GET,'star')) ? intval($_GET['star']) : 0);
  172. $bmark = ((x($_GET,'bmark')) ? intval($_GET['bmark']) : 0);
  173. $order = ((x($_GET,'order')) ? notags($_GET['order']) : 'comment');
  174. $liked = ((x($_GET,'liked')) ? intval($_GET['liked']) : 0);
  175. $conv = ((x($_GET,'conv')) ? intval($_GET['conv']) : 0);
  176. $spam = ((x($_GET,'spam')) ? intval($_GET['spam']) : 0);
  177. $nets = ((x($_GET,'nets')) ? $_GET['nets'] : '');
  178. $cmin = ((x($_GET,'cmin')) ? intval($_GET['cmin']) : 0);
  179. $cmax = ((x($_GET,'cmax')) ? intval($_GET['cmax']) : 99);
  180. $file = ((x($_GET,'file')) ? $_GET['file'] : '');
  181. if(($a->argc > 2) && $a->argv[2] === 'new')
  182. $nouveau = true;
  183. if($a->argc > 1) {
  184. if($a->argv[1] === 'new')
  185. $nouveau = true;
  186. else {
  187. $group = intval($a->argv[1]);
  188. $def_acl = array('allow_gid' => '<' . $group . '>');
  189. }
  190. }
  191. if(x($_GET,'search') || x($_GET,'file'))
  192. $nouveau = true;
  193. if($cid)
  194. $def_acl = array('allow_cid' => '<' . intval($cid) . '>');
  195. if(! $update) {
  196. if($group) {
  197. if(($t = group_public_members($group)) && (! get_pconfig(local_user(),'system','nowarn_insecure'))) {
  198. notice( sprintf( tt('Warning: This group contains %s member from an insecure network.',
  199. 'Warning: This group contains %s members from an insecure network.',
  200. $t), $t ) . EOL);
  201. notice( t('Private messages to this group are at risk of public disclosure.') . EOL);
  202. }
  203. }
  204. nav_set_selected('network');
  205. $celeb = ((($a->user['page-flags'] == PAGE_SOAPBOX) || ($a->user['page-flags'] == PAGE_COMMUNITY)) ? true : false);
  206. $x = array(
  207. 'is_owner' => true,
  208. 'allow_location' => $a->user['allow_location'],
  209. 'default_location' => $a->user['default-location'],
  210. 'nickname' => $a->user['nickname'],
  211. 'lockstate' => ((($group) || (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'),
  212. 'acl' => populate_acl((($group || $cid) ? $def_acl : $a->user), $celeb),
  213. 'bang' => (($group || $cid) ? '!' : ''),
  214. 'visitor' => 'block',
  215. 'profile_uid' => local_user()
  216. );
  217. $o .= status_editor($a,$x);
  218. }
  219. // We don't have to deal with ACL's on this page. You're looking at everything
  220. // that belongs to you, hence you can see all of it. We will filter by group if
  221. // desired.
  222. $sql_options = (($star) ? " and starred = 1 " : '');
  223. $sql_options .= (($bmark) ? " and bookmark = 1 " : '');
  224. $sql_nets = (($nets) ? sprintf(" and `contact`.`network` = '%s' ", dbesc($nets)) : '');
  225. $sql_extra = " AND `item`.`parent` IN ( SELECT `parent` FROM `item` WHERE `id` = `parent` $sql_options ) ";
  226. if($group) {
  227. $r = q("SELECT `name`, `id` FROM `group` WHERE `id` = %d AND `uid` = %d LIMIT 1",
  228. intval($group),
  229. intval($_SESSION['uid'])
  230. );
  231. if(! count($r)) {
  232. if($update)
  233. killme();
  234. notice( t('No such group') . EOL );
  235. goaway($a->get_baseurl() . '/network');
  236. // NOTREACHED
  237. }
  238. $contacts = expand_groups(array($group));
  239. if((is_array($contacts)) && count($contacts)) {
  240. $contact_str = implode(',',$contacts);
  241. }
  242. else {
  243. $contact_str = ' 0 ';
  244. info( t('Group is empty'));
  245. }
  246. $sql_extra = " AND `item`.`parent` IN ( SELECT DISTINCT(`parent`) FROM `item` WHERE 1 $sql_options AND ( `contact-id` IN ( $contact_str ) OR `allow_gid` REGEXP '<" . intval($group) . ">' ) and deleted = 0 ) ";
  247. $o = '<h2>' . t('Group: ') . $r[0]['name'] . '</h2>' . $o;
  248. }
  249. elseif($cid) {
  250. $r = q("SELECT `id`,`name`,`network`,`writable`,`nurl` FROM `contact` WHERE `id` = %d
  251. AND `blocked` = 0 AND `pending` = 0 LIMIT 1",
  252. intval($cid)
  253. );
  254. if(count($r)) {
  255. $sql_extra = " AND `item`.`parent` IN ( SELECT DISTINCT(`parent`) FROM `item` WHERE 1 $sql_options AND `contact-id` = " . intval($cid) . " and deleted = 0 ) ";
  256. $o = '<h2>' . t('Contact: ') . $r[0]['name'] . '</h2>' . $o;
  257. if($r[0]['network'] === NETWORK_OSTATUS && $r[0]['writable'] && (! get_pconfig(local_user(),'system','nowarn_insecure'))) {
  258. notice( t('Private messages to this person are at risk of public disclosure.') . EOL);
  259. }
  260. }
  261. else {
  262. notice( t('Invalid contact.') . EOL);
  263. goaway($a->get_baseurl() . '/network');
  264. // NOTREACHED
  265. }
  266. }
  267. if((! $group) && (! $cid) && (! $update)) {
  268. $o .= get_birthdays();
  269. $o .= get_events();
  270. }
  271. if(! $update) {
  272. // The special div is needed for liveUpdate to kick in for this page.
  273. // We only launch liveUpdate if you aren't filtering in some incompatible
  274. // way and also you aren't writing a comment (discovered in javascript).
  275. $o .= '<div id="live-network"></div>' . "\r\n";
  276. $o .= "<script> var profile_uid = " . $_SESSION['uid']
  277. . "; var netargs = '" . substr($a->cmd,8)
  278. . '?f='
  279. . ((x($_GET,'cid')) ? '&cid=' . $_GET['cid'] : '')
  280. . ((x($_GET,'search')) ? '&search=' . $_GET['search'] : '')
  281. . ((x($_GET,'star')) ? '&star=' . $_GET['star'] : '')
  282. . ((x($_GET,'order')) ? '&order=' . $_GET['order'] : '')
  283. . ((x($_GET,'bmark')) ? '&bmark=' . $_GET['bmark'] : '')
  284. . ((x($_GET,'liked')) ? '&liked=' . $_GET['liked'] : '')
  285. . ((x($_GET,'conv')) ? '&conv=' . $_GET['conv'] : '')
  286. . ((x($_GET,'spam')) ? '&spam=' . $_GET['spam'] : '')
  287. . ((x($_GET,'nets')) ? '&nets=' . $_GET['nets'] : '')
  288. . ((x($_GET,'cmin')) ? '&cmin=' . $_GET['cmin'] : '')
  289. . ((x($_GET,'cmax')) ? '&cmax=' . $_GET['cmax'] : '')
  290. . ((x($_GET,'file')) ? '&file=' . $_GET['file'] : '')
  291. . "'; var profile_page = " . $a->pager['page'] . "; </script>\r\n";
  292. }
  293. $sql_extra2 = (($nouveau) ? '' : " AND `item`.`parent` = `item`.`id` ");
  294. if(x($_GET,'search')) {
  295. $search = escape_tags($_GET['search']);
  296. $sql_extra .= sprintf(" AND ( `item`.`body` REGEXP '%s' OR `item`.`tag` REGEXP '%s' ) ",
  297. dbesc(preg_quote($search)),
  298. dbesc('\\]' . preg_quote($search) . '\\[')
  299. );
  300. }
  301. if(strlen($file)) {
  302. $sql_extra .= file_tag_file_query('item',$file);
  303. }
  304. if($conv) {
  305. $myurl = $a->get_baseurl() . '/profile/'. $a->user['nickname'];
  306. $myurl = substr($myurl,strpos($myurl,'://')+3);
  307. $myurl = str_replace(array('www.','.'),array('','\\.'),$myurl);
  308. $diasp_url = str_replace('/profile/','/u/',$myurl);
  309. $sql_extra .= sprintf(" AND `item`.`parent` IN (SELECT distinct(`parent`) from item where ( `author-link` regexp '%s' or `tag` regexp '%s' or tag regexp '%s' )) ",
  310. dbesc($myurl . '$'),
  311. dbesc($myurl . '\\]'),
  312. dbesc($diasp_url . '\\]')
  313. );
  314. }
  315. if($update) {
  316. // only setup pagination on initial page view
  317. $pager_sql = '';
  318. }
  319. else {
  320. $r = q("SELECT COUNT(*) AS `total`
  321. FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
  322. WHERE `item`.`uid` = %d AND `item`.`visible` = 1 AND `item`.`deleted` = 0
  323. AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
  324. $sql_extra2
  325. $sql_extra $sql_nets ",
  326. intval($_SESSION['uid'])
  327. );
  328. if(count($r)) {
  329. $a->set_pager_total($r[0]['total']);
  330. $itemspage_network = get_pconfig(local_user(),'system','itemspage_network');
  331. $a->set_pager_itemspage(((intval($itemspage_network)) ? $itemspage_network : 40));
  332. }
  333. $pager_sql = sprintf(" LIMIT %d, %d ",intval($a->pager['start']), intval($a->pager['itemspage']));
  334. }
  335. $simple_update = (($update) ? " and `item`.`unseen` = 1 " : '');
  336. if($nouveau) {
  337. // "New Item View" - show all items unthreaded in reverse created date order
  338. $items = q("SELECT `item`.*, `item`.`id` AS `item_id`,
  339. `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`rel`, `contact`.`writable`,
  340. `contact`.`network`, `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
  341. `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
  342. FROM `item`, `contact`
  343. WHERE `item`.`uid` = %d AND `item`.`visible` = 1
  344. AND `item`.`deleted` = 0 and `item`.`moderated` = 0
  345. $simple_update
  346. AND `contact`.`id` = `item`.`contact-id`
  347. AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
  348. $sql_extra $sql_nets
  349. ORDER BY `item`.`received` DESC $pager_sql ",
  350. intval($_SESSION['uid'])
  351. );
  352. }
  353. else {
  354. // Normal conversation view
  355. if($order === 'post')
  356. $ordering = "`created`";
  357. else
  358. $ordering = "`commented`";
  359. // Fetch a page full of parent items for this page
  360. if($update) {
  361. $r = q("SELECT `parent` AS `item_id`, `contact`.`uid` AS `contact_uid`
  362. FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
  363. WHERE `item`.`uid` = %d AND `item`.`visible` = 1 AND `item`.`deleted` = 0
  364. and `item`.`moderated` = 0 and `item`.`unseen` = 1
  365. AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
  366. $sql_extra $sql_nets ",
  367. intval(local_user())
  368. );
  369. }
  370. else {
  371. $r = q("SELECT `item`.`id` AS `item_id`, `contact`.`uid` AS `contact_uid`
  372. FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
  373. WHERE `item`.`uid` = %d AND `item`.`visible` = 1 AND `item`.`deleted` = 0
  374. AND `item`.`moderated` = 0 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
  375. AND `item`.`parent` = `item`.`id`
  376. $sql_extra $sql_nets
  377. ORDER BY `item`.$ordering DESC $pager_sql ",
  378. intval(local_user())
  379. );
  380. }
  381. // Then fetch all the children of the parents that are on this page
  382. $parents_arr = array();
  383. $parents_str = '';
  384. if(count($r)) {
  385. foreach($r as $rr)
  386. if(! in_array($rr['item_id'],$parents_arr))
  387. $parents_arr[] = $rr['item_id'];
  388. $parents_str = implode(', ', $parents_arr);
  389. $items = q("SELECT `item`.*, `item`.`id` AS `item_id`,
  390. `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`rel`, `contact`.`writable`,
  391. `contact`.`network`, `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
  392. `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
  393. FROM `item`, `contact`
  394. WHERE `item`.`uid` = %d AND `item`.`visible` = 1 AND `item`.`deleted` = 0
  395. AND `item`.`moderated` = 0 AND `contact`.`id` = `item`.`contact-id`
  396. AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
  397. AND `item`.`parent` IN ( %s )
  398. $sql_extra ",
  399. intval(local_user()),
  400. dbesc($parents_str)
  401. );
  402. $items = conv_sort($items,$ordering);
  403. } else {
  404. $items = array();
  405. }
  406. }
  407. // We aren't going to try and figure out at the item, group, and page
  408. // level which items you've seen and which you haven't. If you're looking
  409. // at the top level network page just mark everything seen.
  410. if((! $group) && (! $cid) && (! $star)) {
  411. $r = q("UPDATE `item` SET `unseen` = 0
  412. WHERE `unseen` = 1 AND `uid` = %d",
  413. intval(local_user())
  414. );
  415. }
  416. // Set this so that the conversation function can find out contact info for our wall-wall items
  417. $a->page_contact = $a->contact;
  418. $mode = (($nouveau) ? 'network-new' : 'network');
  419. $o .= conversation($a,$items,$mode,$update);
  420. if(! $update) {
  421. $o .= paginate($a);
  422. }
  423. return $o;
  424. }