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.

243 lines
7.1 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. <?php
  2. /**
  3. * @file mod/community.php
  4. */
  5. use Friendica\App;
  6. use Friendica\Content\Nav;
  7. use Friendica\Content\Pager;
  8. use Friendica\Core\ACL;
  9. use Friendica\Core\Config;
  10. use Friendica\Core\L10n;
  11. use Friendica\Core\PConfig;
  12. use Friendica\Core\Renderer;
  13. use Friendica\Database\DBA;
  14. use Friendica\Model\Contact;
  15. use Friendica\Model\Item;
  16. function community_init(App $a)
  17. {
  18. if (!local_user()) {
  19. unset($_SESSION['theme']);
  20. unset($_SESSION['mobile-theme']);
  21. }
  22. }
  23. function community_content(App $a, $update = 0)
  24. {
  25. $o = '';
  26. if (Config::get('system', 'block_public') && !local_user() && !remote_user()) {
  27. notice(L10n::t('Public access denied.') . EOL);
  28. return;
  29. }
  30. $page_style = Config::get('system', 'community_page_style');
  31. if ($page_style == CP_NO_INTERNAL_COMMUNITY) {
  32. notice(L10n::t('Access denied.') . EOL);
  33. return;
  34. }
  35. $accounttype = null;
  36. if ($a->argc > 2) {
  37. switch ($a->argv[2]) {
  38. case 'person':
  39. $accounttype = Contact::ACCOUNT_TYPE_PERSON;
  40. break;
  41. case 'organisation':
  42. $accounttype = Contact::ACCOUNT_TYPE_ORGANISATION;
  43. break;
  44. case 'news':
  45. $accounttype = Contact::ACCOUNT_TYPE_NEWS;
  46. break;
  47. case 'community':
  48. $accounttype = Contact::ACCOUNT_TYPE_COMMUNITY;
  49. break;
  50. }
  51. }
  52. if ($a->argc > 1) {
  53. $content = $a->argv[1];
  54. } else {
  55. if (!empty(Config::get('system', 'singleuser'))) {
  56. // On single user systems only the global page does make sense
  57. $content = 'global';
  58. } else {
  59. // When only the global community is allowed, we use this as default
  60. $content = $page_style == CP_GLOBAL_COMMUNITY ? 'global' : 'local';
  61. }
  62. }
  63. if (!in_array($content, ['local', 'global'])) {
  64. notice(L10n::t('Community option not available.') . EOL);
  65. return;
  66. }
  67. // Check if we are allowed to display the content to visitors
  68. if (!local_user()) {
  69. $available = $page_style == CP_USERS_AND_GLOBAL;
  70. if (!$available) {
  71. $available = ($page_style == CP_USERS_ON_SERVER) && ($content == 'local');
  72. }
  73. if (!$available) {
  74. $available = ($page_style == CP_GLOBAL_COMMUNITY) && ($content == 'global');
  75. }
  76. if (!$available) {
  77. notice(L10n::t('Not available.') . EOL);
  78. return;
  79. }
  80. }
  81. require_once 'include/conversation.php';
  82. if (!$update) {
  83. $tabs = [];
  84. if ((local_user() || in_array($page_style, [CP_USERS_AND_GLOBAL, CP_USERS_ON_SERVER])) && empty(Config::get('system', 'singleuser'))) {
  85. $tabs[] = [
  86. 'label' => L10n::t('Local Community'),
  87. 'url' => 'community/local',
  88. 'sel' => $content == 'local' ? 'active' : '',
  89. 'title' => L10n::t('Posts from local users on this server'),
  90. 'id' => 'community-local-tab',
  91. 'accesskey' => 'l'
  92. ];
  93. }
  94. if (local_user() || in_array($page_style, [CP_USERS_AND_GLOBAL, CP_GLOBAL_COMMUNITY])) {
  95. $tabs[] = [
  96. 'label' => L10n::t('Global Community'),
  97. 'url' => 'community/global',
  98. 'sel' => $content == 'global' ? 'active' : '',
  99. 'title' => L10n::t('Posts from users of the whole federated network'),
  100. 'id' => 'community-global-tab',
  101. 'accesskey' => 'g'
  102. ];
  103. }
  104. $tab_tpl = get_markup_template('common_tabs.tpl');
  105. $o .= Renderer::replaceMacros($tab_tpl, ['$tabs' => $tabs]);
  106. Nav::setSelected('community');
  107. // We need the editor here to be able to reshare an item.
  108. if (local_user()) {
  109. $x = [
  110. 'is_owner' => true,
  111. 'allow_location' => $a->user['allow_location'],
  112. 'default_location' => $a->user['default-location'],
  113. 'nickname' => $a->user['nickname'],
  114. 'lockstate' => (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'),
  115. 'acl' => ACL::getFullSelectorHTML($a->user, true),
  116. 'bang' => '',
  117. 'visitor' => 'block',
  118. 'profile_uid' => local_user(),
  119. ];
  120. $o .= status_editor($a, $x, 0, true);
  121. }
  122. }
  123. // check if we serve a mobile device and get the user settings accordingly
  124. if ($a->is_mobile) {
  125. $itemspage_network = PConfig::get(local_user(), 'system', 'itemspage_mobile_network', 20);
  126. } else {
  127. $itemspage_network = PConfig::get(local_user(), 'system', 'itemspage_network', 40);
  128. }
  129. // now that we have the user settings, see if the theme forces
  130. // a maximum item number which is lower then the user choice
  131. if (($a->force_max_items > 0) && ($a->force_max_items < $itemspage_network)) {
  132. $itemspage_network = $a->force_max_items;
  133. }
  134. $pager = new Pager($a->query_string, $itemspage_network);
  135. $r = community_getitems($pager->getStart(), $pager->getItemsPerPage(), $content, $accounttype);
  136. if (!DBA::isResult($r)) {
  137. info(L10n::t('No results.') . EOL);
  138. return $o;
  139. }
  140. $maxpostperauthor = (int) Config::get('system', 'max_author_posts_community_page');
  141. if (($maxpostperauthor != 0) && ($content == 'local')) {
  142. $count = 1;
  143. $previousauthor = "";
  144. $numposts = 0;
  145. $s = [];
  146. do {
  147. foreach ($r as $item) {
  148. if ($previousauthor == $item["author-link"]) {
  149. ++$numposts;
  150. } else {
  151. $numposts = 0;
  152. }
  153. $previousauthor = $item["author-link"];
  154. if (($numposts < $maxpostperauthor) && (count($s) < $pager->getItemsPerPage())) {
  155. $s[] = $item;
  156. }
  157. }
  158. if (count($s) < $pager->getItemsPerPage()) {
  159. $r = community_getitems($pager->getStart() + ($count * $pager->getItemsPerPage()), $pager->getItemsPerPage(), $content, $accounttype);
  160. }
  161. } while ((count($s) < $pager->getItemsPerPage()) && ( ++$count < 50) && (count($r) > 0));
  162. } else {
  163. $s = $r;
  164. }
  165. $o .= conversation($a, $s, $pager, 'community', $update, false, 'commented', local_user());
  166. if (!$update) {
  167. $o .= $pager->renderMinimal(count($r));
  168. }
  169. $t = get_markup_template("community.tpl");
  170. return Renderer::replaceMacros($t, [
  171. '$content' => $o,
  172. '$header' => '',
  173. '$show_global_community_hint' => ($content == 'global') && Config::get('system', 'show_global_community_hint'),
  174. '$global_community_hint' => L10n::t("This community stream shows all public posts received by this node. They may not reflect the opinions of this node’s users.")
  175. ]);
  176. }
  177. function community_getitems($start, $itemspage, $content, $accounttype)
  178. {
  179. if ($content == 'local') {
  180. if (!is_null($accounttype)) {
  181. $sql_accounttype = " AND `user`.`account-type` = ?";
  182. $values = [$accounttype, $start, $itemspage];
  183. } else {
  184. $sql_accounttype = "";
  185. $values = [$start, $itemspage];
  186. }
  187. $r = DBA::p("SELECT `item`.`uri`, `author`.`url` AS `author-link` FROM `thread`
  188. INNER JOIN `user` ON `user`.`uid` = `thread`.`uid` AND NOT `user`.`hidewall`
  189. INNER JOIN `item` ON `item`.`id` = `thread`.`iid`
  190. INNER JOIN `contact` AS `author` ON `author`.`id`=`item`.`author-id`
  191. WHERE `thread`.`visible` AND NOT `thread`.`deleted` AND NOT `thread`.`moderated`
  192. AND NOT `thread`.`private` AND `thread`.`wall` AND `thread`.`origin` $sql_accounttype
  193. ORDER BY `thread`.`commented` DESC LIMIT ?, ?", $values);
  194. return DBA::toArray($r);
  195. } elseif ($content == 'global') {
  196. if (!is_null($accounttype)) {
  197. $condition = ["`uid` = ? AND `owner`.`contact-type` = ?", 0, $accounttype];
  198. } else {
  199. $condition = ['uid' => 0];
  200. }
  201. $r = Item::selectThreadForUser(0, ['uri'], $condition, ['order' => ['commented' => true], 'limit' => [$start, $itemspage]]);
  202. return DBA::toArray($r);
  203. }
  204. // Should never happen
  205. return [];
  206. }