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.

406 lines
10 KiB

11 years ago
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
11 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 (dbm::is_result($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 (dbm::is_result($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 (dbm::is_result($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 (! dbm::is_result($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. }
  132. return $r;
  133. }
  134. function group_get_members($gid) {
  135. $ret = array();
  136. if(intval($gid)) {
  137. $r = q("SELECT `group_member`.`contact-id`, `contact`.* FROM `group_member`
  138. INNER JOIN `contact` ON `contact`.`id` = `group_member`.`contact-id`
  139. WHERE `gid` = %d AND `group_member`.`uid` = %d AND
  140. NOT `contact`.`self` AND NOT `contact`.`blocked` AND NOT `contact`.`pending`
  141. ORDER BY `contact`.`name` ASC ",
  142. intval($gid),
  143. intval(local_user())
  144. );
  145. if (dbm::is_result($r))
  146. $ret = $r;
  147. }
  148. return $ret;
  149. }
  150. function group_public_members($gid) {
  151. $ret = 0;
  152. if(intval($gid)) {
  153. $r = q("SELECT `contact`.`id` AS `contact-id` FROM `group_member`
  154. INNER JOIN `contact` ON `contact`.`id` = `group_member`.`contact-id`
  155. WHERE `gid` = %d AND `group_member`.`uid` = %d
  156. AND `contact`.`network` = '%s' AND `contact`.`notify` != '' ",
  157. intval($gid),
  158. intval(local_user()),
  159. dbesc(NETWORK_OSTATUS)
  160. );
  161. if (dbm::is_result($r))
  162. $ret = count($r);
  163. }
  164. return $ret;
  165. }
  166. function mini_group_select($uid,$gid = 0, $label = "") {
  167. $grps = array();
  168. $o = '';
  169. $r = q("SELECT * FROM `group` WHERE `deleted` = 0 AND `uid` = %d ORDER BY `name` ASC",
  170. intval($uid)
  171. );
  172. $grps[] = array('name' => '', 'id' => '0', 'selected' => '');
  173. if (dbm::is_result($r)) {
  174. foreach ($r as $rr) {
  175. $grps[] = array('name' => $rr['name'], 'id' => $rr['id'], 'selected' => (($gid == $rr['id']) ? 'true' : ''));
  176. }
  177. }
  178. logger('groups: ' . print_r($grps,true));
  179. if ($label == "")
  180. $label = t('Default privacy group for new contacts');
  181. $o = replace_macros(get_markup_template('group_selection.tpl'), array(
  182. '$label' => $label,
  183. '$groups' => $grps
  184. ));
  185. return $o;
  186. }
  187. /**
  188. * @brief Create group sidebar widget
  189. *
  190. * @param string $every
  191. * @param string $each
  192. * @param string $editmode
  193. * 'standard' => include link 'Edit groups'
  194. * 'extended' => include link 'Create new group'
  195. * 'full' => include link 'Create new group' and provide for each group a link to edit this group
  196. * @param int $group_id
  197. * @param int $cid
  198. * @return string
  199. */
  200. function group_side($every="contacts",$each="group",$editmode = "standard", $group_id = 0, $cid = 0) {
  201. $o = '';
  202. if (! local_user())
  203. return '';
  204. $groups = array();
  205. $groups[] = array(
  206. 'text' => t('Everybody'),
  207. 'id' => 0,
  208. 'selected' => (($group_id == 0) ? 'group-selected' : ''),
  209. 'href' => $every,
  210. );
  211. $r = q("SELECT * FROM `group` WHERE `deleted` = 0 AND `uid` = %d ORDER BY `name` ASC",
  212. intval($_SESSION['uid'])
  213. );
  214. $member_of = array();
  215. if($cid) {
  216. $member_of = groups_containing(local_user(),$cid);
  217. }
  218. if (dbm::is_result($r)) {
  219. foreach ($r as $rr) {
  220. $selected = (($group_id == $rr['id']) ? ' group-selected' : '');
  221. if ($editmode == "full") {
  222. $groupedit = array(
  223. 'href' => "group/".$rr['id'],
  224. 'title' => t('edit'),
  225. );
  226. } else {
  227. $groupedit = null;
  228. }
  229. $groups[] = array(
  230. 'id' => $rr['id'],
  231. 'cid' => $cid,
  232. 'text' => $rr['name'],
  233. 'selected' => $selected,
  234. 'href' => $each."/".$rr['id'],
  235. 'edit' => $groupedit,
  236. 'ismember' => in_array($rr['id'],$member_of),
  237. );
  238. }
  239. }
  240. $tpl = get_markup_template("group_side.tpl");
  241. $o = replace_macros($tpl, array(
  242. '$title' => t('Groups'),
  243. 'newgroup' => (($editmode == "extended") || ($editmode == "full") ? 1 : ''),
  244. '$editgroupstext' => t('Edit groups'),
  245. 'grouppage' => "group/",
  246. '$edittext' => t('Edit group'),
  247. '$createtext' => t('Create a new group'),
  248. '$creategroup' => t('Group Name: '),
  249. '$form_security_token' => get_form_security_token("group_edit"),
  250. '$ungrouped' => (($every === 'contacts') ? t('Contacts not in any group') : ''),
  251. '$groups' => $groups,
  252. '$add' => t('add'),
  253. ));
  254. return $o;
  255. }
  256. function expand_groups($a,$check_dead = false, $use_gcontact = false) {
  257. if(! (is_array($a) && count($a)))
  258. return array();
  259. $groups = implode(',', $a);
  260. $groups = dbesc($groups);
  261. if ($use_gcontact)
  262. $r = q("SELECT `gcontact`.`id` AS `contact-id` FROM `group_member`
  263. INNER JOIN `contact` ON `contact`.`id` = `group_member`.`contact-id`
  264. INNER JOIN `gcontact` ON `gcontact`.`nurl` = `contact`.`nurl`
  265. WHERE `gid` IN ($groups)");
  266. else
  267. $r = q("SELECT `contact-id` FROM `group_member` WHERE `gid` IN ( $groups )");
  268. $ret = array();
  269. if (dbm::is_result($r))
  270. foreach($r as $rr)
  271. $ret[] = $rr['contact-id'];
  272. if($check_dead AND !$use_gcontact) {
  273. require_once('include/acl_selectors.php');
  274. $ret = prune_deadguys($ret);
  275. }
  276. return $ret;
  277. }
  278. function member_of($c) {
  279. $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 ",
  280. intval($c)
  281. );
  282. return $r;
  283. }
  284. function groups_containing($uid,$c) {
  285. $r = q("SELECT `gid` FROM `group_member` WHERE `uid` = %d AND `group_member`.`contact-id` = %d ",
  286. intval($uid),
  287. intval($c)
  288. );
  289. $ret = array();
  290. if (dbm::is_result($r)) {
  291. foreach($r as $rr)
  292. $ret[] = $rr['gid'];
  293. }
  294. return $ret;
  295. }
  296. /**
  297. * @brief count unread group items
  298. *
  299. * Count unread items of each groups
  300. *
  301. * @return array
  302. * 'id' => group id
  303. * 'name' => group name
  304. * 'count' => counted unseen group items
  305. *
  306. */
  307. function groups_count_unseen() {
  308. $r = q("SELECT `group`.`id`, `group`.`name`,
  309. (SELECT COUNT(*) FROM `item` FORCE INDEX (`uid_unseen_contactid`)
  310. WHERE `uid` = %d AND `unseen` AND
  311. `contact-id` IN (SELECT `contact-id` FROM `group_member`
  312. WHERE `group_member`.`gid` = `group`.`id` AND `group_member`.`uid` = %d)) AS `count`
  313. FROM `group` WHERE `group`.`uid` = %d;",
  314. intval(local_user()),
  315. intval(local_user()),
  316. intval(local_user())
  317. );
  318. return $r;
  319. }
  320. /**
  321. * @brief Returns the default group for a given user and network
  322. *
  323. * @param int $uid User id
  324. * @param string $network network name
  325. *
  326. * @return int group id
  327. */
  328. function get_default_group($uid, $network = "") {
  329. $default_group = 0;
  330. if ($network == NETWORK_OSTATUS)
  331. $default_group = get_pconfig($uid, "ostatus", "default_group");
  332. if ($default_group != 0)
  333. return $default_group;
  334. $g = q("SELECT `def_gid` FROM `user` WHERE `uid` = %d LIMIT 1", intval($uid));
  335. if($g && intval($g[0]["def_gid"]))
  336. $default_group = $g[0]["def_gid"];
  337. return $default_group;
  338. }