Hubzilla core code
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.
 
 
 
 
 
 

502 lines
15 KiB

  1. <?php
  2. namespace Zotlabs\Module;
  3. use App;
  4. use Zotlabs\Web\Controller;
  5. use Zotlabs\Lib\PermissionDescription;
  6. use Zotlabs\Web\HTTPSig;
  7. use Zotlabs\Lib\Libzot;
  8. require_once('include/items.php');
  9. require_once('include/security.php');
  10. require_once('include/conversation.php');
  11. require_once('include/acl_selectors.php');
  12. require_once('include/opengraph.php');
  13. /**
  14. * @brief Channel Controller
  15. *
  16. */
  17. class Channel extends Controller {
  18. function init() {
  19. if(in_array(substr($_GET['search'],0,1),[ '@', '!', '?']))
  20. goaway('search' . '?f=&search=' . $_GET['search']);
  21. $which = null;
  22. if(argc() > 1)
  23. $which = argv(1);
  24. if(! $which) {
  25. if(local_channel()) {
  26. $channel = App::get_channel();
  27. if($channel && $channel['channel_address'])
  28. $which = $channel['channel_address'];
  29. }
  30. }
  31. if(! $which) {
  32. notice( t('You must be logged in to see this page.') . EOL );
  33. return;
  34. }
  35. $profile = 0;
  36. $channel = App::get_channel();
  37. if((local_channel()) && (argc() > 2) && (argv(2) === 'view')) {
  38. $which = $channel['channel_address'];
  39. $profile = argv(1);
  40. }
  41. $channel = channelx_by_nick($which);
  42. if(! $channel) {
  43. http_status_exit(404, 'Not found');
  44. }
  45. // handle zot6 channel discovery
  46. if(Libzot::is_zot_request()) {
  47. $sigdata = HTTPSig::verify(file_get_contents('php://input'));
  48. if($sigdata && $sigdata['signer'] && $sigdata['header_valid']) {
  49. $data = json_encode(Libzot::zotinfo([ 'address' => $channel['channel_address'], 'target_url' => $sigdata['signer'] ]));
  50. $s = q("select site_crypto, hubloc_sitekey from site left join hubloc on hubloc_url = site_url where hubloc_id_url = '%s' and hubloc_network = 'zot6' limit 1",
  51. dbesc($sigdata['signer'])
  52. );
  53. if($s) {
  54. $data = json_encode(crypto_encapsulate($data,$s[0]['hubloc_sitekey'],Libzot::best_algorithm($s[0]['site_crypto'])));
  55. }
  56. }
  57. else {
  58. $data = json_encode(Libzot::zotinfo([ 'address' => $channel['channel_address'] ]));
  59. }
  60. $headers = [
  61. 'Content-Type' => 'application/x-zot+json',
  62. 'Digest' => HTTPSig::generate_digest_header($data),
  63. '(request-target)' => strtolower($_SERVER['REQUEST_METHOD']) . ' ' . $_SERVER['REQUEST_URI']
  64. ];
  65. $h = HTTPSig::create_sig($headers,$channel['channel_prvkey'],channel_url($channel));
  66. HTTPSig::set_headers($h);
  67. echo $data;
  68. killme();
  69. }
  70. if((local_channel()) && (argc() > 2) && (argv(2) === 'view')) {
  71. $which = $channel['channel_address'];
  72. $profile = argv(1);
  73. }
  74. head_add_link( [
  75. 'rel' => 'alternate',
  76. 'type' => 'application/atom+xml',
  77. 'title' => t('Posts and comments'),
  78. 'href' => z_root() . '/feed/' . $which
  79. ]);
  80. head_add_link( [
  81. 'rel' => 'alternate',
  82. 'type' => 'application/atom+xml',
  83. 'title' => t('Only posts'),
  84. 'href' => z_root() . '/feed/' . $which . '?f=&top=1'
  85. ]);
  86. // Run profile_load() here to make sure the theme is set before
  87. // we start loading content
  88. profile_load($which,$profile);
  89. // Add Opengraph markup
  90. $mid = ((x($_REQUEST,'mid')) ? $_REQUEST['mid'] : '');
  91. if(strpos($mid,'b64.') === 0)
  92. $mid = @base64url_decode(substr($mid,4));
  93. if($mid)
  94. $r = q("SELECT * FROM item WHERE mid = '%s' AND uid = %d AND item_private = 0 LIMIT 1",
  95. dbesc($mid),
  96. intval($channel['channel_id'])
  97. );
  98. opengraph_add_meta($r ? $r[0] : [], $channel);
  99. }
  100. function get($update = 0, $load = false) {
  101. $noscript_content = get_config('system', 'noscript_content', '1');
  102. if($load)
  103. $_SESSION['loadtime'] = datetime_convert();
  104. $category = $datequery = $datequery2 = '';
  105. $mid = ((x($_REQUEST,'mid')) ? $_REQUEST['mid'] : '');
  106. if(strpos($mid,'b64.') === 0)
  107. $decoded = @base64url_decode(substr($mid,4));
  108. if($decoded)
  109. $mid = $decoded;
  110. $datequery = ((x($_GET,'dend') && is_a_date_arg($_GET['dend'])) ? notags($_GET['dend']) : '');
  111. $datequery2 = ((x($_GET,'dbegin') && is_a_date_arg($_GET['dbegin'])) ? notags($_GET['dbegin']) : '');
  112. if(observer_prohibited(true)) {
  113. return login();
  114. }
  115. $category = ((x($_REQUEST,'cat')) ? $_REQUEST['cat'] : '');
  116. $hashtags = ((x($_REQUEST,'tag')) ? $_REQUEST['tag'] : '');
  117. $order = ((x($_GET,'order')) ? notags($_GET['order']) : 'post');
  118. $static = ((array_key_exists('static',$_REQUEST)) ? intval($_REQUEST['static']) : 0);
  119. $search = ((x($_GET,'search')) ? $_GET['search'] : EMPTY_STR);
  120. $groups = array();
  121. $o = '';
  122. if($update) {
  123. // Ensure we've got a profile owner if updating.
  124. App::$profile['profile_uid'] = App::$profile_uid = $update;
  125. }
  126. $is_owner = (((local_channel()) && (App::$profile['profile_uid'] == local_channel())) ? true : false);
  127. $channel = App::get_channel();
  128. $observer = App::get_observer();
  129. $ob_hash = (($observer) ? $observer['xchan_hash'] : '');
  130. $perms = get_all_perms(App::$profile['profile_uid'],$ob_hash);
  131. if(! $perms['view_stream']) {
  132. // We may want to make the target of this redirect configurable
  133. if($perms['view_profile']) {
  134. notice( t('Insufficient permissions. Request redirected to profile page.') . EOL);
  135. goaway (z_root() . "/profile/" . App::$profile['channel_address']);
  136. }
  137. notice( t('Permission denied.') . EOL);
  138. return;
  139. }
  140. if(! $update) {
  141. nav_set_selected('Channel Home');
  142. $static = channel_manual_conv_update(App::$profile['profile_uid']);
  143. // search terms header
  144. if($search) {
  145. $o .= replace_macros(get_markup_template("section_title.tpl"),array(
  146. '$title' => t('Search Results For:') . ' ' . htmlspecialchars($search, ENT_COMPAT,'UTF-8')
  147. ));
  148. }
  149. if($channel && $is_owner) {
  150. $channel_acl = array(
  151. 'allow_cid' => $channel['channel_allow_cid'],
  152. 'allow_gid' => $channel['channel_allow_gid'],
  153. 'deny_cid' => $channel['channel_deny_cid'],
  154. 'deny_gid' => $channel['channel_deny_gid']
  155. );
  156. }
  157. else {
  158. $channel_acl = [ 'allow_cid' => '', 'allow_gid' => '', 'deny_cid' => '', 'deny_gid' => '' ];
  159. }
  160. if($perms['post_wall']) {
  161. $x = array(
  162. 'is_owner' => $is_owner,
  163. 'allow_location' => ((($is_owner || $observer) && (intval(get_pconfig(App::$profile['profile_uid'],'system','use_browser_location')))) ? true : false),
  164. 'default_location' => (($is_owner) ? App::$profile['channel_location'] : ''),
  165. 'nickname' => App::$profile['channel_address'],
  166. 'lockstate' => (((strlen(App::$profile['channel_allow_cid'])) || (strlen(App::$profile['channel_allow_gid'])) || (strlen(App::$profile['channel_deny_cid'])) || (strlen(App::$profile['channel_deny_gid']))) ? 'lock' : 'unlock'),
  167. 'acl' => (($is_owner) ? populate_acl($channel_acl,true, PermissionDescription::fromGlobalPermission('view_stream'), get_post_aclDialogDescription(), 'acl_dialog_post') : ''),
  168. 'permissions' => $channel_acl,
  169. 'showacl' => (($is_owner) ? 'yes' : ''),
  170. 'bang' => '',
  171. 'visitor' => (($is_owner || $observer) ? true : false),
  172. 'profile_uid' => App::$profile['profile_uid'],
  173. 'editor_autocomplete' => true,
  174. 'bbco_autocomplete' => 'bbcode',
  175. 'bbcode' => true,
  176. 'jotnets' => true,
  177. 'reset' => t('Reset form')
  178. );
  179. $o .= status_editor($a,$x,false,'Channel');
  180. }
  181. }
  182. /**
  183. * Get permissions SQL - if $remote_contact is true, our remote user has been pre-verified and we already have fetched his/her groups
  184. */
  185. $item_normal = item_normal();
  186. $item_normal_update = item_normal_update();
  187. $sql_extra = item_permissions_sql(App::$profile['profile_uid']);
  188. if(feature_enabled(App::$profile['profile_uid'], 'channel_list_mode') && (! $mid))
  189. $page_mode = 'list';
  190. else
  191. $page_mode = 'client';
  192. $abook_uids = " and abook.abook_channel = " . intval(App::$profile['profile_uid']) . " ";
  193. $simple_update = (($update) ? " AND item_unseen = 1 " : '');
  194. if($search) {
  195. $search = escape_tags($search);
  196. if(strpos($search,'#') === 0) {
  197. $sql_extra .= term_query('item',substr($search,1),TERM_HASHTAG,TERM_COMMUNITYTAG);
  198. }
  199. else {
  200. $sql_extra .= sprintf(" AND (item.body like '%s' OR item.title like '%s') ",
  201. dbesc(protect_sprintf('%' . $search . '%')),
  202. dbesc(protect_sprintf('%' . $search . '%'))
  203. );
  204. }
  205. }
  206. head_add_link([
  207. 'rel' => 'alternate',
  208. 'type' => 'application/json+oembed',
  209. 'href' => z_root() . '/oep?f=&url=' . urlencode(z_root() . '/' . App::$query_string),
  210. 'title' => 'oembed'
  211. ]);
  212. if($update && $_SESSION['loadtime'])
  213. $simple_update = " AND (( item_unseen = 1 AND item.changed > '" . datetime_convert('UTC','UTC',$_SESSION['loadtime']) . "' ) OR item.changed > '" . datetime_convert('UTC','UTC',$_SESSION['loadtime']) . "' ) ";
  214. if($load)
  215. $simple_update = '';
  216. if($static && $simple_update)
  217. $simple_update .= " and author_xchan = '" . protect_sprintf(get_observer_hash()) . "' ";
  218. if(($update) && (! $load)) {
  219. if($mid) {
  220. $r = q("SELECT parent AS item_id from item where mid like '%s' and uid = %d $item_normal_update
  221. AND item_wall = 1 $simple_update $sql_extra limit 1",
  222. dbesc($mid . '%'),
  223. intval(App::$profile['profile_uid'])
  224. );
  225. $_SESSION['loadtime'] = datetime_convert();
  226. }
  227. else {
  228. $r = q("SELECT parent AS item_id from item
  229. left join abook on ( item.owner_xchan = abook.abook_xchan $abook_uids )
  230. WHERE uid = %d $item_normal_update
  231. AND item_wall = 1 $simple_update
  232. AND (abook.abook_blocked = 0 or abook.abook_flags is null)
  233. $sql_extra
  234. ORDER BY created DESC",
  235. intval(App::$profile['profile_uid'])
  236. );
  237. $_SESSION['loadtime'] = datetime_convert();
  238. }
  239. }
  240. else {
  241. if(x($category)) {
  242. $sql_extra2 .= protect_sprintf(term_item_parent_query(App::$profile['profile_uid'],'item', $category, TERM_CATEGORY));
  243. }
  244. if(x($hashtags)) {
  245. $sql_extra2 .= protect_sprintf(term_item_parent_query(App::$profile['profile_uid'],'item', $hashtags, TERM_HASHTAG, TERM_COMMUNITYTAG));
  246. }
  247. if($datequery) {
  248. $sql_extra2 .= protect_sprintf(sprintf(" AND item.created <= '%s' ", dbesc(datetime_convert(date_default_timezone_get(),'',$datequery))));
  249. $order = 'post';
  250. }
  251. if($datequery2) {
  252. $sql_extra2 .= protect_sprintf(sprintf(" AND item.created >= '%s' ", dbesc(datetime_convert(date_default_timezone_get(),'',$datequery2))));
  253. }
  254. if($order === 'post')
  255. $ordering = "created";
  256. else
  257. $ordering = "commented";
  258. $itemspage = get_pconfig(local_channel(),'system','itemspage');
  259. App::set_pager_itemspage(((intval($itemspage)) ? $itemspage : 20));
  260. $pager_sql = sprintf(" LIMIT %d OFFSET %d ", intval(App::$pager['itemspage']), intval(App::$pager['start']));
  261. if($noscript_content || $load) {
  262. if($mid) {
  263. $r = q("SELECT parent AS item_id from item where mid like '%s' and uid = %d $item_normal
  264. AND item_wall = 1 $sql_extra limit 1",
  265. dbesc($mid . '%'),
  266. intval(App::$profile['profile_uid'])
  267. );
  268. if (! $r) {
  269. notice( t('Permission denied.') . EOL);
  270. }
  271. }
  272. else {
  273. $r = q("SELECT DISTINCT item.parent AS item_id, $ordering FROM item
  274. left join abook on ( item.author_xchan = abook.abook_xchan $abook_uids )
  275. WHERE true and item.uid = %d $item_normal
  276. AND (abook.abook_blocked = 0 or abook.abook_flags is null)
  277. AND item.item_wall = 1 AND item.item_thread_top = 1
  278. $sql_extra $sql_extra2
  279. ORDER BY $ordering DESC, item_id $pager_sql ",
  280. intval(App::$profile['profile_uid'])
  281. );
  282. }
  283. }
  284. else {
  285. $r = array();
  286. }
  287. }
  288. if($r) {
  289. $parents_str = ids_to_querystr($r,'item_id');
  290. $r = q("SELECT item.*, item.id AS item_id
  291. FROM item
  292. WHERE item.uid = %d $item_normal
  293. AND item.parent IN ( %s )
  294. $sql_extra ",
  295. intval(App::$profile['profile_uid']),
  296. dbesc($parents_str)
  297. );
  298. xchan_query($r);
  299. $items = fetch_post_tags($r, true);
  300. $items = conv_sort($items,$ordering);
  301. if($load && $mid && (! count($items))) {
  302. // This will happen if we don't have sufficient permissions
  303. // to view the parent item (or the item itself if it is toplevel)
  304. notice( t('Permission denied.') . EOL);
  305. }
  306. } else {
  307. $items = array();
  308. }
  309. if((! $update) && (! $load)) {
  310. if($decoded)
  311. $mid = 'b64.' . base64url_encode($mid);
  312. // This is ugly, but we can't pass the profile_uid through the session to the ajax updater,
  313. // because browser prefetching might change it on us. We have to deliver it with the page.
  314. $maxheight = get_pconfig(App::$profile['profile_uid'],'system','channel_divmore_height');
  315. if(! $maxheight)
  316. $maxheight = 400;
  317. $o .= '<div id="live-channel"></div>' . "\r\n";
  318. $o .= "<script> var profile_uid = " . App::$profile['profile_uid']
  319. . "; var netargs = '?f='; var profile_page = " . App::$pager['page']
  320. . "; divmore_height = " . intval($maxheight) . "; </script>\r\n";
  321. App::$page['htmlhead'] .= replace_macros(get_markup_template("build_query.tpl"),array(
  322. '$baseurl' => z_root(),
  323. '$pgtype' => 'channel',
  324. '$uid' => ((App::$profile['profile_uid']) ? App::$profile['profile_uid'] : '0'),
  325. '$gid' => '0',
  326. '$cid' => '0',
  327. '$cmin' => '(-1)',
  328. '$cmax' => '(-1)',
  329. '$star' => '0',
  330. '$liked' => '0',
  331. '$conv' => '0',
  332. '$spam' => '0',
  333. '$nouveau' => '0',
  334. '$wall' => '1',
  335. '$fh' => '0',
  336. '$static' => $static,
  337. '$page' => ((App::$pager['page'] != 1) ? App::$pager['page'] : 1),
  338. '$search' => $search,
  339. '$xchan' => '',
  340. '$order' => (($order) ? urlencode($order) : ''),
  341. '$list' => ((x($_REQUEST,'list')) ? intval($_REQUEST['list']) : 0),
  342. '$file' => '',
  343. '$cats' => (($category) ? urlencode($category) : ''),
  344. '$tags' => (($hashtags) ? urlencode($hashtags) : ''),
  345. '$mid' => (($mid) ? urlencode($mid) : ''),
  346. '$verb' => '',
  347. '$net' => '',
  348. '$dend' => $datequery,
  349. '$dbegin' => $datequery2
  350. ));
  351. }
  352. $update_unseen = '';
  353. if($page_mode === 'list') {
  354. /**
  355. * in "list mode", only mark the parent item and any like activities as "seen".
  356. * We won't distinguish between comment likes and post likes. The important thing
  357. * is that the number of unseen comments will be accurate. The SQL to separate the
  358. * comment likes could also get somewhat hairy.
  359. */
  360. if($parents_str) {
  361. $update_unseen = " AND ( id IN ( " . dbesc($parents_str) . " )";
  362. $update_unseen .= " OR ( parent IN ( " . dbesc($parents_str) . " ) AND verb in ( '" . dbesc(ACTIVITY_LIKE) . "','" . dbesc(ACTIVITY_DISLIKE) . "' ))) ";
  363. }
  364. }
  365. else {
  366. if($parents_str) {
  367. $update_unseen = " AND parent IN ( " . dbesc($parents_str) . " )";
  368. }
  369. }
  370. if($is_owner && $update_unseen) {
  371. $x = [ 'channel_id' => local_channel(), 'update' => 'unset' ];
  372. call_hooks('update_unseen',$x);
  373. if($x['update'] === 'unset' || intval($x['update'])) {
  374. $r = q("UPDATE item SET item_unseen = 0 where item_unseen = 1 and item_wall = 1 AND uid = %d $update_unseen",
  375. intval(local_channel())
  376. );
  377. }
  378. }
  379. $mode = (($search) ? 'search' : 'channel');
  380. if($update) {
  381. $o .= conversation($items,$mode,$update,$page_mode);
  382. }
  383. else {
  384. $o .= '<noscript>';
  385. if($noscript_content) {
  386. $o .= conversation($items,$mode,$update,'traditional');
  387. $o .= alt_pager(count($items));
  388. }
  389. else {
  390. $o .= '<div class="section-content-warning-wrapper">' . t('You must enable javascript for your browser to be able to view this content.') . '</div>';
  391. }
  392. $o .= '</noscript>';
  393. $o .= conversation($items,$mode,$update,$page_mode);
  394. if ($mid && $items[0]['title'])
  395. App::$page['title'] = $items[0]['title'] . " - " . App::$page['title'];
  396. }
  397. if($mid)
  398. $o .= '<div id="content-complete"></div>';
  399. return $o;
  400. }
  401. }