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.

405 lines
9.9 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
5 years ago
5 years ago
5 years ago
  1. <?php
  2. function group_add($uid,$name) {
  3. $ret = false;
  4. if(x($uid) && x($name)) {
  5. $r = group_byname($uid,$name); // check for dups
  6. if($r !== false) {
  7. // This could be a problem.
  8. // Let's assume we've just created a group which we once deleted
  9. // all the old members are gone, but the group remains so we don't break any security
  10. // access lists. What we're doing here is reviving the dead group, but old content which
  11. // was restricted to this group may now be seen by the new group members.
  12. $z = q("SELECT * FROM `group` WHERE `id` = %d LIMIT 1",
  13. intval($r)
  14. );
  15. if(count($z) && $z[0]['deleted']) {
  16. $r = q("UPDATE `group` SET `deleted` = 0 WHERE `uid` = %d AND `name` = '%s'",
  17. intval($uid),
  18. dbesc($name)
  19. );
  20. notice( t('A deleted group with this name was revived. Existing item permissions <strong>may</strong> apply to this group and any future members. If this is not what you intended, please create another group with a different name.') . EOL);
  21. }
  22. return true;
  23. }
  24. $r = q("INSERT INTO `group` ( `uid`, `name` )
  25. VALUES( %d, '%s' ) ",
  26. intval($uid),
  27. dbesc($name)
  28. );
  29. $ret = $r;
  30. }
  31. return $ret;
  32. }
  33. function group_rmv($uid,$name) {
  34. $ret = false;
  35. if(x($uid) && x($name)) {
  36. $r = q("SELECT id FROM `group` WHERE `uid` = %d AND `name` = '%s' LIMIT 1",
  37. intval($uid),
  38. dbesc($name)
  39. );
  40. if(count($r))
  41. $group_id = $r[0]['id'];
  42. if(! $group_id)
  43. return false;
  44. // remove group from default posting lists
  45. $r = q("SELECT def_gid, allow_gid, deny_gid FROM user WHERE uid = %d LIMIT 1",
  46. intval($uid)
  47. );
  48. if($r) {
  49. $user_info = $r[0];
  50. $change = false;
  51. if($user_info['def_gid'] == $group_id) {
  52. $user_info['def_gid'] = 0;
  53. $change = true;
  54. }
  55. if(strpos($user_info['allow_gid'], '<' . $group_id . '>') !== false) {
  56. $user_info['allow_gid'] = str_replace('<' . $group_id . '>', '', $user_info['allow_gid']);
  57. $change = true;
  58. }
  59. if(strpos($user_info['deny_gid'], '<' . $group_id . '>') !== false) {
  60. $user_info['deny_gid'] = str_replace('<' . $group_id . '>', '', $user_info['deny_gid']);
  61. $change = true;
  62. }
  63. if($change) {
  64. q("UPDATE user SET def_gid = %d, allow_gid = '%s', deny_gid = '%s' WHERE uid = %d",
  65. intval($user_info['def_gid']),
  66. dbesc($user_info['allow_gid']),
  67. dbesc($user_info['deny_gid']),
  68. intval($uid)
  69. );
  70. }
  71. }
  72. // remove all members
  73. $r = q("DELETE FROM `group_member` WHERE `uid` = %d AND `gid` = %d ",
  74. intval($uid),
  75. intval($group_id)
  76. );
  77. // remove group
  78. $r = q("UPDATE `group` SET `deleted` = 1 WHERE `uid` = %d AND `name` = '%s'",
  79. intval($uid),
  80. dbesc($name)
  81. );
  82. $ret = $r;
  83. }
  84. return $ret;
  85. }
  86. function group_byname($uid,$name) {
  87. if((! $uid) || (! strlen($name)))
  88. return false;
  89. $r = q("SELECT * FROM `group` WHERE `uid` = %d AND `name` = '%s' LIMIT 1",
  90. intval($uid),
  91. dbesc($name)
  92. );
  93. if(count($r))
  94. return $r[0]['id'];
  95. return false;
  96. }
  97. function group_rmv_member($uid,$name,$member) {
  98. $gid = group_byname($uid,$name);
  99. if(! $gid)
  100. return false;
  101. if(! ( $uid && $gid && $member))
  102. return false;
  103. $r = q("DELETE FROM `group_member` WHERE `uid` = %d AND `gid` = %d AND `contact-id` = %d",
  104. intval($uid),
  105. intval($gid),
  106. intval($member)
  107. );
  108. return $r;
  109. }
  110. function group_add_member($uid,$name,$member,$gid = 0) {
  111. if(! $gid)
  112. $gid = group_byname($uid,$name);
  113. if((! $gid) || (! $uid) || (! $member))
  114. return false;
  115. $r = q("SELECT * FROM `group_member` WHERE `uid` = %d AND `gid` = %d AND `contact-id` = %d LIMIT 1",
  116. intval($uid),
  117. intval($gid),
  118. intval($member)
  119. );
  120. if(count($r))
  121. return true; // You might question this, but
  122. // we indicate success because the group member was in fact created
  123. // -- It was just created at another time
  124. if(! count($r))
  125. $r = q("INSERT INTO `group_member` (`uid`, `gid`, `contact-id`)
  126. VALUES( %d, %d, %d ) ",
  127. intval($uid),
  128. intval($gid),
  129. intval($member)
  130. );
  131. return $r;
  132. }
  133. function group_get_members($gid) {
  134. $ret = array();
  135. if(intval($gid)) {
  136. $r = q("SELECT `group_member`.`contact-id`, `contact`.* FROM `group_member`
  137. INNER JOIN `contact` ON `contact`.`id` = `group_member`.`contact-id`
  138. WHERE `gid` = %d AND `group_member`.`uid` = %d AND
  139. NOT `contact`.`self` AND NOT `contact`.`blocked` AND NOT `contact`.`pending`
  140. ORDER BY `contact`.`name` ASC ",
  141. intval($gid),
  142. intval(local_user())
  143. );
  144. if(count($r))
  145. $ret = $r;
  146. }
  147. return $ret;
  148. }
  149. function group_public_members($gid) {
  150. $ret = 0;
  151. if(intval($gid)) {
  152. $r = q("SELECT `contact`.`id` AS `contact-id` FROM `group_member`
  153. INNER JOIN `contact` ON `contact`.`id` = `group_member`.`contact-id`
  154. WHERE `gid` = %d AND `group_member`.`uid` = %d
  155. AND `contact`.`network` = '%s' AND `contact`.`notify` != '' ",
  156. intval($gid),
  157. intval(local_user()),
  158. dbesc(NETWORK_OSTATUS)
  159. );
  160. if(count($r))
  161. $ret = count($r);
  162. }
  163. return $ret;
  164. }
  165. function mini_group_select($uid,$gid = 0, $label = "") {
  166. $grps = array();
  167. $o = '';
  168. $r = q("SELECT * FROM `group` WHERE `deleted` = 0 AND `uid` = %d ORDER BY `name` ASC",
  169. intval($uid)
  170. );
  171. $grps[] = array('name' => '', 'id' => '0', 'selected' => '');
  172. if(count($r)) {
  173. foreach($r as $rr) {
  174. $grps[] = array('name' => $rr['name'], 'id' => $rr['id'], 'selected' => (($gid == $rr['id']) ? 'true' : ''));
  175. }
  176. }
  177. logger('groups: ' . print_r($grps,true));
  178. if ($label == "")
  179. $label = t('Default privacy group for new contacts');
  180. $o = replace_macros(get_markup_template('group_selection.tpl'), array(
  181. '$label' => $label,
  182. '$groups' => $grps
  183. ));
  184. return $o;
  185. }
  186. /**
  187. * @brief Create group sidebar widget
  188. *
  189. * @param string $every
  190. * @param string $each
  191. * @param string $editmode
  192. * 'standard' => include link 'Edit groups'
  193. * 'extended' => include link 'Create new group'
  194. * 'full' => include link 'Create new group' and provide for each group a link to edit this group
  195. * @param int $group_id
  196. * @param int $cid
  197. * @return string
  198. */
  199. function group_side($every="contacts",$each="group",$editmode = "standard", $group_id = 0, $cid = 0) {
  200. $o = '';
  201. if(! local_user())
  202. return '';
  203. $groups = array();
  204. $groups[] = array(
  205. 'text' => t('Everybody'),
  206. 'id' => 0,
  207. 'selected' => (($group_id == 0) ? 'group-selected' : ''),
  208. 'href' => $every,
  209. );
  210. $r = q("SELECT * FROM `group` WHERE `deleted` = 0 AND `uid` = %d ORDER BY `name` ASC",
  211. intval($_SESSION['uid'])
  212. );
  213. $member_of = array();
  214. if($cid) {
  215. $member_of = groups_containing(local_user(),$cid);
  216. }
  217. if(count($r)) {
  218. foreach($r as $rr) {
  219. $selected = (($group_id == $rr['id']) ? ' group-selected' : '');
  220. if ($editmode == "full") {
  221. $groupedit = array(
  222. 'href' => "group/".$rr['id'],
  223. 'title' => t('edit'),
  224. );
  225. } else {
  226. $groupedit = null;
  227. }
  228. $groups[] = array(
  229. 'id' => $rr['id'],
  230. 'cid' => $cid,
  231. 'text' => $rr['name'],
  232. 'selected' => $selected,
  233. 'href' => $each."/".$rr['id'],
  234. 'edit' => $groupedit,
  235. 'ismember' => in_array($rr['id'],$member_of),
  236. );
  237. }
  238. }
  239. $tpl = get_markup_template("group_side.tpl");
  240. $o = replace_macros($tpl, array(
  241. '$title' => t('Groups'),
  242. 'newgroup' => (($editmode == "extended") || ($editmode == "full") ? 1 : ''),
  243. '$editgroupstext' => t('Edit groups'),
  244. 'grouppage' => "group/",
  245. '$edittext' => t('Edit group'),
  246. '$createtext' => t('Create a new group'),
  247. '$creategroup' => t('Group Name: '),
  248. '$form_security_token' => get_form_security_token("group_edit"),
  249. '$ungrouped' => (($every === 'contacts') ? t('Contacts not in any group') : ''),
  250. '$groups' => $groups,
  251. '$add' => t('add'),
  252. ));
  253. return $o;
  254. }
  255. function expand_groups($a,$check_dead = false, $use_gcontact = false) {
  256. if(! (is_array($a) && count($a)))
  257. return array();
  258. $groups = implode(',', $a);
  259. $groups = dbesc($groups);
  260. if ($use_gcontact)
  261. $r = q("SELECT `gcontact`.`id` AS `contact-id` FROM `group_member`
  262. INNER JOIN `contact` ON `contact`.`id` = `group_member`.`contact-id`
  263. INNER JOIN `gcontact` ON `gcontact`.`nurl` = `contact`.`nurl`
  264. WHERE `gid` IN ($groups)");
  265. else
  266. $r = q("SELECT `contact-id` FROM `group_member` WHERE `gid` IN ( $groups )");
  267. $ret = array();
  268. if(count($r))
  269. foreach($r as $rr)
  270. $ret[] = $rr['contact-id'];
  271. if($check_dead AND !$use_gcontact) {
  272. require_once('include/acl_selectors.php');
  273. $ret = prune_deadguys($ret);
  274. }
  275. return $ret;
  276. }
  277. function member_of($c) {
  278. $r = q("SELECT `group`.`name`, `group`.`id` FROM `group` INNER JOIN `group_member` ON `group_member`.`gid` = `group`.`id` WHERE `group_member`.`contact-id` = %d AND `group`.`deleted` = 0 ORDER BY `group`.`name` ASC ",
  279. intval($c)
  280. );
  281. return $r;
  282. }
  283. function groups_containing($uid,$c) {
  284. $r = q("SELECT `gid` FROM `group_member` WHERE `uid` = %d AND `group_member`.`contact-id` = %d ",
  285. intval($uid),
  286. intval($c)
  287. );
  288. $ret = array();
  289. if(count($r)) {
  290. foreach($r as $rr)
  291. $ret[] = $rr['gid'];
  292. }
  293. return $ret;
  294. }
  295. /**
  296. * @brief count unread group items
  297. *
  298. * Count unread items of each groups
  299. *
  300. * @return array
  301. * 'id' => group id
  302. * 'name' => group name
  303. * 'count' => counted unseen group items
  304. *
  305. */
  306. function groups_count_unseen() {
  307. $r = q("SELECT `group`.`id`, `group`.`name`,
  308. (SELECT COUNT(*) FROM `item` FORCE INDEX (`uid_unseen_contactid`)
  309. WHERE `uid` = %d AND `unseen` AND
  310. `contact-id` IN (SELECT `contact-id` FROM `group_member`
  311. WHERE `group_member`.`gid` = `group`.`id` AND `group_member`.`uid` = %d)) AS `count`
  312. FROM `group` WHERE `group`.`uid` = %d;",
  313. intval(local_user()),
  314. intval(local_user()),
  315. intval(local_user())
  316. );
  317. return $r;
  318. }
  319. /**
  320. * @brief Returns the default group for a given user and network
  321. *
  322. * @param int $uid User id
  323. * @param string $network network name
  324. *
  325. * @return int group id
  326. */
  327. function get_default_group($uid, $network = "") {
  328. $default_group = 0;
  329. if ($network == NETWORK_OSTATUS)
  330. $default_group = get_pconfig($uid, "ostatus", "default_group");
  331. if ($default_group != 0)
  332. return $default_group;
  333. $g = q("SELECT `def_gid` FROM `user` WHERE `uid` = %d LIMIT 1", intval($uid));
  334. if($g && intval($g[0]["def_gid"]))
  335. $default_group = $g[0]["def_gid"];
  336. return $default_group;
  337. }