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.
 
 
 
 
 
 

343 lines
8.4 KiB

  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) {
  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. $o = replace_macros(get_markup_template('group_selection.tpl'), array(
  179. '$label' => t('Default privacy group for new contacts'),
  180. '$groups' => $grps
  181. ));
  182. return $o;
  183. }
  184. /**
  185. * @brief Create group sidebar widget
  186. *
  187. * @param string $every
  188. * @param string $each
  189. * @param string $editmode
  190. * 'standard' => include link 'Edit groups'
  191. * 'extended' => include link 'Create new group'
  192. * 'full' => include link 'Create new group' and provide for each group a link to edit this group
  193. * @param int $group_id
  194. * @param int $cid
  195. * @return string
  196. */
  197. function group_side($every="contacts",$each="group",$editmode = "standard", $group_id = 0, $cid = 0) {
  198. $o = '';
  199. if(! local_user())
  200. return '';
  201. $groups = array();
  202. $groups[] = array(
  203. 'text' => t('Everybody'),
  204. 'id' => 0,
  205. 'selected' => (($group_id == 0) ? 'group-selected' : ''),
  206. 'href' => $every,
  207. );
  208. $r = q("SELECT * FROM `group` WHERE `deleted` = 0 AND `uid` = %d ORDER BY `name` ASC",
  209. intval($_SESSION['uid'])
  210. );
  211. $member_of = array();
  212. if($cid) {
  213. $member_of = groups_containing(local_user(),$cid);
  214. }
  215. if(count($r)) {
  216. foreach($r as $rr) {
  217. $selected = (($group_id == $rr['id']) ? ' group-selected' : '');
  218. if ($editmode == "full") {
  219. $groupedit = array(
  220. 'href' => "group/".$rr['id'],
  221. 'title' => t('edit'),
  222. );
  223. } else {
  224. $groupedit = null;
  225. }
  226. $groups[] = array(
  227. 'id' => $rr['id'],
  228. 'cid' => $cid,
  229. 'text' => $rr['name'],
  230. 'selected' => $selected,
  231. 'href' => $each."/".$rr['id'],
  232. 'edit' => $groupedit,
  233. 'ismember' => in_array($rr['id'],$member_of),
  234. );
  235. }
  236. }
  237. $tpl = get_markup_template("group_side.tpl");
  238. $o = replace_macros($tpl, array(
  239. '$title' => t('Groups'),
  240. 'newgroup' => (($editmode == "extended") || ($editmode == "full") ? 1 : ''),
  241. '$editgroupstext' => t('Edit groups'),
  242. 'grouppage' => "group/",
  243. '$edittext' => t('Edit group'),
  244. '$createtext' => t('Create a new group'),
  245. '$creategroup' => t('Group Name: '),
  246. '$form_security_token' => get_form_security_token("group_edit"),
  247. '$ungrouped' => (($every === 'contacts') ? t('Contacts not in any group') : ''),
  248. '$groups' => $groups,
  249. '$add' => t('add'),
  250. ));
  251. return $o;
  252. }
  253. function expand_groups($a,$check_dead = false) {
  254. if(! (is_array($a) && count($a)))
  255. return array();
  256. $groups = implode(',', $a);
  257. $groups = dbesc($groups);
  258. $r = q("SELECT `contact-id` FROM `group_member` WHERE `gid` IN ( $groups )");
  259. $ret = array();
  260. if(count($r))
  261. foreach($r as $rr)
  262. $ret[] = $rr['contact-id'];
  263. if($check_dead) {
  264. require_once('include/acl_selectors.php');
  265. $ret = prune_deadguys($ret);
  266. }
  267. return $ret;
  268. }
  269. function member_of($c) {
  270. $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 ",
  271. intval($c)
  272. );
  273. return $r;
  274. }
  275. function groups_containing($uid,$c) {
  276. $r = q("SELECT `gid` FROM `group_member` WHERE `uid` = %d AND `group_member`.`contact-id` = %d ",
  277. intval($uid),
  278. intval($c)
  279. );
  280. $ret = array();
  281. if(count($r)) {
  282. foreach($r as $rr)
  283. $ret[] = $rr['gid'];
  284. }
  285. return $ret;
  286. }