functions.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038
  1. <?php
  2. /**
  3. * All helper functions for this plugin can be found here.
  4. *
  5. * @package group_tools
  6. */
  7. /**
  8. * Check if a invitation code results in a group
  9. *
  10. * @param string $invite_code the invite code
  11. * @param int $group_guid (optional) the group to check
  12. *
  13. * @return boolean|ElggGroup a group for the invitation or false
  14. */
  15. function group_tools_check_group_email_invitation($invite_code, $group_guid = 0) {
  16. $result = false;
  17. if (empty($invite_code)) {
  18. return false;
  19. }
  20. // note not using elgg_get_entities_from_annotations
  21. // due to performance issues with LIKE wildcard search
  22. // prefetch metastring ids for use in lighter joins instead
  23. $name_id = elgg_get_metastring_id("email_invitation");
  24. $code_id = elgg_get_metastring_id($invite_code);
  25. $sanitized_invite_code = sanitize_string($invite_code);
  26. $options = array(
  27. "limit" => 1,
  28. "wheres" => array(
  29. "n_table.name_id = {$name_id} AND (n_table.value_id = {$code_id} OR v.string LIKE '{$sanitized_invite_code}|%')"
  30. )
  31. );
  32. if (!empty($group_guid)) {
  33. $options["annotation_owner_guids"] = array($group_guid);
  34. }
  35. // find hidden groups
  36. $ia = elgg_set_ignore_access(true);
  37. $annotations = elgg_get_annotations($options);
  38. if (!$annotations) {
  39. // restore access
  40. elgg_set_ignore_access($ia);
  41. return false;
  42. }
  43. $group = $annotations[0]->getEntity();
  44. if ($group) {
  45. $result = $group;
  46. }
  47. // restore access
  48. elgg_set_ignore_access($ia);
  49. return $result;
  50. }
  51. /**
  52. * Invite a user to a group
  53. *
  54. * @param ElggGroup $group the group to be invited for
  55. * @param ElggUser $user the user to be invited
  56. * @param string $text (optional) extra text in the invitation
  57. * @param boolean $resend should existing invitations be resend
  58. *
  59. * @return boolean true if the invitation was send
  60. */
  61. function group_tools_invite_user(ElggGroup $group, ElggUser $user, $text = "", $resend = false) {
  62. $result = false;
  63. $loggedin_user = elgg_get_logged_in_user_entity();
  64. if (!empty($user) && ($user instanceof ElggUser) && !empty($group) && ($group instanceof ElggGroup) && !empty($loggedin_user)) {
  65. // Create relationship
  66. $relationship = add_entity_relationship($group->getGUID(), "invited", $user->getGUID());
  67. if ($relationship || $resend) {
  68. // Send email
  69. $url = elgg_get_site_url() . "groups/invitations/" . $user->username;
  70. $subject = elgg_echo("groups:invite:subject", array(
  71. $user->name,
  72. $group->name
  73. ));
  74. $msg = elgg_echo("group_tools:groups:invite:body", array(
  75. $user->name,
  76. $loggedin_user->name,
  77. $group->name,
  78. $text,
  79. $url
  80. ));
  81. if (notify_user($user->getGUID(), $group->getOwnerGUID(), $subject, $msg, array(), "email")) {
  82. $result = true;
  83. }
  84. }
  85. }
  86. return $result;
  87. }
  88. /**
  89. * Add a user to a group
  90. *
  91. * @param ElggGroup $group the group to add the user to
  92. * @param ElggUser $user the user to be added
  93. * @param string $text (optional) extra text for the notification
  94. *
  95. * @return boolean true if successfull
  96. */
  97. function group_tools_add_user(ElggGroup $group, ElggUser $user, $text = "") {
  98. $result = false;
  99. $loggedin_user = elgg_get_logged_in_user_entity();
  100. if (!empty($user) && ($user instanceof ElggUser) && !empty($group) && ($group instanceof ElggGroup) && !empty($loggedin_user)) {
  101. // make sure all goes well
  102. $ia = elgg_set_ignore_access(true);
  103. if ($group->join($user)) {
  104. // Remove any invite or join request flags
  105. remove_entity_relationship($group->getGUID(), "invited", $user->getGUID());
  106. remove_entity_relationship($user->getGUID(), "membership_request", $group->getGUID());
  107. // notify user
  108. $subject = elgg_echo("group_tools:groups:invite:add:subject", array($group->name));
  109. $msg = elgg_echo("group_tools:groups:invite:add:body", array(
  110. $user->name,
  111. $loggedin_user->name,
  112. $group->name,
  113. $text,
  114. $group->getURL()
  115. ));
  116. $params = array(
  117. "group" => $group,
  118. "inviter" => $loggedin_user,
  119. "invitee" => $user
  120. );
  121. $msg = elgg_trigger_plugin_hook("invite_notification", "group_tools", $params, $msg);
  122. if (notify_user($user->getGUID(), $group->getOwnerGUID(), $subject, $msg, array(), "email")) {
  123. $result = true;
  124. }
  125. }
  126. // restore access
  127. elgg_set_ignore_access($ia);
  128. }
  129. return $result;
  130. }
  131. /**
  132. * Invite a new user by email to a group
  133. *
  134. * @param ElggGroup $group the group to be invited for
  135. * @param string $email the email address to be invited
  136. * @param string $text (optional) extra text in the invitation
  137. * @param boolean $resend should existing invitations be resend
  138. *
  139. * @return boolean|NULL true is invited, false on failure, null when already send
  140. */
  141. function group_tools_invite_email(ElggGroup $group, $email, $text = "", $resend = false) {
  142. $result = false;
  143. $loggedin_user = elgg_get_logged_in_user_entity();
  144. if (!empty($group) && ($group instanceof ElggGroup) && !empty($email) && is_email_address($email) && !empty($loggedin_user)) {
  145. // generate invite code
  146. $invite_code = group_tools_generate_email_invite_code($group->getGUID(), $email);
  147. if (!empty($invite_code)) {
  148. $found_group = group_tools_check_group_email_invitation($invite_code, $group->getGUID());
  149. if (empty($found_group) || $resend) {
  150. // make site email
  151. $site = elgg_get_site_entity();
  152. if (!empty($site->email)) {
  153. if (!empty($site->name)) {
  154. $site_from = $site->name . " <" . $site->email . ">";
  155. } else {
  156. $site_from = $site->email;
  157. }
  158. } else {
  159. // no site email, so make one up
  160. if (!empty($site->name)) {
  161. $site_from = $site->name . " <noreply@" . $site->getDomain() . ">";
  162. } else {
  163. $site_from = "noreply@" . $site->getDomain();
  164. }
  165. }
  166. if (empty($found_group)) {
  167. // register invite with group
  168. $group->annotate("email_invitation", $invite_code . "|" . $email, ACCESS_LOGGED_IN, $group->getGUID());
  169. }
  170. // make subject
  171. $subject = elgg_echo("group_tools:groups:invite:email:subject", array($group->name));
  172. // make body
  173. $body = elgg_echo("group_tools:groups:invite:email:body", array(
  174. $loggedin_user->name,
  175. $group->name,
  176. $site->name,
  177. $text,
  178. $site->name,
  179. elgg_get_site_url() . "register?group_invitecode=" . $invite_code,
  180. elgg_get_site_url() . "groups/invitations/?invitecode=" . $invite_code,
  181. $invite_code
  182. ));
  183. $params = array(
  184. "group" => $group,
  185. "inviter" => $loggedin_user,
  186. "invitee" => $email
  187. );
  188. $body = elgg_trigger_plugin_hook("invite_notification", "group_tools", $params, $body);
  189. $result = elgg_send_email($site_from, $email, $subject, $body);
  190. } else {
  191. $result = null;
  192. }
  193. }
  194. }
  195. return $result;
  196. }
  197. /**
  198. * Verify that all supplied user_guids are a member of the group
  199. *
  200. * @param int $group_guid the GUID of the group
  201. * @param array $user_guids an array of user GUIDs to check
  202. *
  203. * @return boolean|int[] returns all user_guids that are a member
  204. */
  205. function group_tools_verify_group_members($group_guid, $user_guids) {
  206. $result = false;
  207. if (!empty($group_guid) && !empty($user_guids)) {
  208. if (!is_array($user_guids)) {
  209. $user_guids = array($user_guids);
  210. }
  211. $group = get_entity($group_guid);
  212. if (!empty($group) && ($group instanceof ElggGroup)) {
  213. $options = array(
  214. "type" => "user",
  215. "limit" => false,
  216. "relationship" => "member",
  217. "relationship_guid" => $group->getGUID(),
  218. "inverse_relationship" => true,
  219. "callback" => "group_tools_guid_only_callback"
  220. );
  221. $member_guids = elgg_get_entities_from_relationship($options);
  222. if (!empty($member_guids)) {
  223. $result = array();
  224. foreach ($user_guids as $user_guid) {
  225. if (in_array($user_guid, $member_guids)) {
  226. $result[] = $user_guid;
  227. }
  228. }
  229. }
  230. }
  231. }
  232. return $result;
  233. }
  234. /**
  235. * Custom callback function to only return the GUID from a database row
  236. *
  237. * @param stdClass $row the database row
  238. *
  239. * @return int the GUID
  240. */
  241. function group_tools_guid_only_callback($row) {
  242. return (int) $row->guid;
  243. }
  244. /**
  245. * Check if group creation is limited to site administrators
  246. * Also this function caches the result
  247. *
  248. * @return boolean true if limited
  249. */
  250. function group_tools_is_group_creation_limited() {
  251. static $result;
  252. if (!isset($result)) {
  253. $result = false;
  254. if (elgg_get_plugin_setting("admin_create", "group_tools") == "yes") {
  255. $result = true;
  256. }
  257. }
  258. return $result;
  259. }
  260. /**
  261. * Get all the groups this email address is invited for
  262. *
  263. * @param string $email the email address
  264. * @param int $site_guid (optional) site_guid
  265. *
  266. * @return boolean|ElggGroup[] array of groups or false on failure
  267. */
  268. function group_tools_get_invited_groups_by_email($email, $site_guid = 0) {
  269. $result = false;
  270. if (!empty($email)) {
  271. $dbprefix = elgg_get_config("dbprefix");
  272. $site_secret = get_site_secret();
  273. $email = sanitise_string(strtolower($email));
  274. $email_invitation_id = elgg_get_metastring_id("email_invitation");
  275. if ($site_guid === 0) {
  276. $site_guid = elgg_get_site_entity()->getGUID();
  277. }
  278. $options = array(
  279. "type" => "group",
  280. "limit" => false,
  281. "site_guids" => $site_guid,
  282. "joins" => array(
  283. "JOIN " . $dbprefix . "annotations a ON a.owner_guid = e.guid",
  284. "JOIN " . $dbprefix . "metastrings msv ON a.value_id = msv.id"
  285. ),
  286. "wheres" => array(
  287. "(a.name_id = " . $email_invitation_id . " AND
  288. (msv.string = md5(CONCAT('" . $site_secret . $email . "', e.guid))
  289. OR msv.string LIKE CONCAT(md5(CONCAT('" . $site_secret . $email . "', e.guid)), '|%')
  290. )
  291. )"
  292. )
  293. );
  294. // make sure we can see all groups
  295. $ia = elgg_set_ignore_access(true);
  296. $groups = elgg_get_entities($options);
  297. if (!empty($groups)) {
  298. $result = $groups;
  299. }
  300. // restore access
  301. elgg_set_ignore_access($ia);
  302. }
  303. return $result;
  304. }
  305. /**
  306. * Generate a unique code to be used in email invitations
  307. *
  308. * @param int $group_guid the group GUID
  309. * @param string $email the email address
  310. *
  311. * @return boolean|string the invite code, or false on failure
  312. */
  313. function group_tools_generate_email_invite_code($group_guid, $email) {
  314. $result = false;
  315. if (!empty($group_guid) && !empty($email)) {
  316. // get site secret
  317. $site_secret = get_site_secret();
  318. // generate code
  319. $result = md5($site_secret . strtolower($email) . $group_guid);
  320. }
  321. return $result;
  322. }
  323. /**
  324. * Get all the users who are missing from the ACLs of their groups
  325. *
  326. * @param int $group_guid (optional) a group GUID to check, otherwise all groups will be checked
  327. *
  328. * @return stdClass[] all the found database rows
  329. */
  330. function group_tools_get_missing_acl_users($group_guid = 0) {
  331. $dbprefix = elgg_get_config("dbprefix");
  332. $group_guid = sanitise_int($group_guid, false);
  333. $query = "SELECT ac.id AS acl_id, ac.owner_guid AS group_guid, er.guid_one AS user_guid";
  334. $query .= " FROM " . $dbprefix . "access_collections ac";
  335. $query .= " JOIN " . $dbprefix . "entities e ON e.guid = ac.owner_guid";
  336. $query .= " JOIN " . $dbprefix . "entity_relationships er ON ac.owner_guid = er.guid_two";
  337. $query .= " JOIN " . $dbprefix . "entities e2 ON er.guid_one = e2.guid";
  338. $query .= " WHERE";
  339. if ($group_guid > 0) {
  340. // limit to the provided group
  341. $query .= " e.guid = " . $group_guid;
  342. } else {
  343. // all groups
  344. $query .= " e.type = 'group'";
  345. }
  346. $query .= " AND e2.type = 'user'";
  347. $query .= " AND er.relationship = 'member'";
  348. $query .= " AND er.guid_one NOT IN (";
  349. $query .= " SELECT acm.user_guid";
  350. $query .= " FROM " . $dbprefix . "access_collections ac2";
  351. $query .= " JOIN " . $dbprefix . "access_collection_membership acm ON ac2.id = acm.access_collection_id";
  352. $query .= " WHERE ac2.owner_guid = ac.owner_guid";
  353. $query .= " )";
  354. return get_data($query);
  355. }
  356. /**
  357. * Get all users who are in a group ACL but no longer member of the group
  358. *
  359. * @param int $group_guid (optional) a group GUID to check, otherwise all groups will be checked
  360. *
  361. * @return stdClass[] all the found database rows
  362. */
  363. function group_tools_get_excess_acl_users($group_guid = 0) {
  364. $dbprefix = elgg_get_config("dbprefix");
  365. $group_guid = sanitise_int($group_guid, false);
  366. $query = "SELECT ac.id AS acl_id, ac.owner_guid AS group_guid, acm.user_guid AS user_guid";
  367. $query .= " FROM " . $dbprefix . "access_collections ac";
  368. $query .= " JOIN " . $dbprefix . "access_collection_membership acm ON ac.id = acm.access_collection_id";
  369. $query .= " JOIN " . $dbprefix . "entities e ON ac.owner_guid = e.guid";
  370. $query .= " WHERE";
  371. if ($group_guid > 0) {
  372. // limit to the provided group
  373. $query .= " e.guid = " . $group_guid;
  374. } else {
  375. // all groups
  376. $query .= " e.type = 'group'";
  377. }
  378. $query .= " AND acm.user_guid NOT IN (";
  379. $query .= " SELECT r.guid_one";
  380. $query .= " FROM " . $dbprefix . "entity_relationships r";
  381. $query .= " WHERE r.relationship = 'member'";
  382. $query .= " AND r.guid_two = ac.owner_guid";
  383. $query .= " )";
  384. return get_data($query);
  385. }
  386. /**
  387. * Get all groups that don't have an ACL
  388. *
  389. * @return ElggGroup[] an array of all the found groups
  390. */
  391. function group_tools_get_groups_without_acl() {
  392. $dbprefix = elgg_get_config("dbprefix");
  393. $options = array(
  394. "type" => "group",
  395. "limit" => false,
  396. "wheres" => array("e.guid NOT IN (
  397. SELECT ac.owner_guid
  398. FROM " . $dbprefix . "access_collections ac
  399. JOIN " . $dbprefix . "entities e ON ac.owner_guid = e.guid
  400. WHERE e.type = 'group'
  401. )")
  402. );
  403. return elgg_get_entities($options);
  404. }
  405. /**
  406. * Remove a user from an access collection,
  407. * can't use remove_user_from_access_collection() because user might not exists any more
  408. *
  409. * @param int $user_guid the user GUID to remove
  410. * @param int $collection_id the ID of the ACL to be removed from
  411. *
  412. * @return boolean true on success
  413. */
  414. function group_tools_remove_user_from_access_collection($user_guid, $collection_id) {
  415. $collection_id = sanitise_int($collection_id, false);
  416. $user_guid = sanitise_int($user_guid, false);
  417. $collection = get_access_collection($collection_id);
  418. if (empty($user_guid) || !$collection) {
  419. return false;
  420. }
  421. $params = array(
  422. "collection_id" => $collection_id,
  423. "user_guid" => $user_guid
  424. );
  425. if (!elgg_trigger_plugin_hook("access:collections:remove_user", "collection", $params, true)) {
  426. return false;
  427. }
  428. $dbprefix = elgg_get_config("dbprefix");
  429. $query = "DELETE";
  430. $query .= " FROM " . $dbprefix . "access_collection_membership";
  431. $query .= " WHERE access_collection_id = " . $collection_id;
  432. $query .= " AND user_guid = " . $user_guid;
  433. return (bool) delete_data($query);
  434. }
  435. /**
  436. * Custom callback to save memory and queries for group admin transfer
  437. *
  438. * @param stdClass $row from elgg_get_* function
  439. *
  440. * @return array
  441. */
  442. function group_tool_admin_transfer_callback($row) {
  443. return array(
  444. "guid" => (int) $row->guid,
  445. "name" => $row->name
  446. );
  447. }
  448. /**
  449. * Are group members allowed to invite new members to the group
  450. *
  451. * @param ElggGroup $group The group to check the settings
  452. *
  453. * @return boolean true is allowed
  454. */
  455. function group_tools_allow_members_invite(ElggGroup $group) {
  456. $result = false;
  457. if (!empty($group) && elgg_instanceof($group, "group")) {
  458. // only for group members
  459. if ($group->isMember(elgg_get_logged_in_user_entity())) {
  460. // is this even allowed
  461. $setting = elgg_get_plugin_setting("invite_members", "group_tools");
  462. if (!empty($setting) && in_array($setting, array("yes_off", "yes_on"))) {
  463. $invite_members = $group->invite_members;
  464. if (empty($invite_members)) {
  465. $invite_members = "no";
  466. if ($setting == "yes_on") {
  467. $invite_members = "yes";
  468. }
  469. }
  470. if ($invite_members == "yes") {
  471. $result = true;
  472. }
  473. }
  474. }
  475. }
  476. return $result;
  477. }
  478. /**
  479. * Custom annotations delete function because logged out users can't delete annotations
  480. *
  481. * @param array $annotations annotations to delete
  482. *
  483. * @return void
  484. */
  485. function group_tools_delete_annotations($annotations) {
  486. if (!empty($annotations) && is_array($annotations)) {
  487. $dbprefix = elgg_get_config("dbprefix");
  488. foreach ($annotations as $annotation) {
  489. if (elgg_trigger_event("delete", "annotation", $annotation)) {
  490. delete_data("DELETE from {$dbprefix}annotations where id=" . $annotation->id);
  491. }
  492. }
  493. }
  494. }
  495. /**
  496. * Returns suggested groups
  497. *
  498. * @param ElggUser $user (optional) the user to get the groups for, defaults to the current user
  499. * @param int $limit (optional) the number of suggested groups to return, default = 10
  500. *
  501. * @return ElggGroup[] all the suggested groups
  502. */
  503. function group_tools_get_suggested_groups($user = null, $limit = null) {
  504. $result = array();
  505. if (!elgg_instanceof($user, "user")) {
  506. $user = elgg_get_logged_in_user_entity();
  507. }
  508. if (is_null($limit)) {
  509. $limit = get_input("limit", 10);
  510. }
  511. $limit = sanitize_int($limit, false);
  512. if ($user && ($limit > 0)) {
  513. $dbprefix = elgg_get_config("dbprefix");
  514. $group_membership_where = "e.guid NOT IN (SELECT er.guid_two FROM {$dbprefix}entity_relationships er where er.guid_one = {$user->getGUID()} and er.relationship IN ('member', 'membership_request'))";
  515. if (elgg_get_plugin_setting("auto_suggest_groups","group_tools") !== "no") {
  516. $tag_names = elgg_get_registered_tag_metadata_names();
  517. if (!empty($tag_names)) {
  518. $user_metadata_options = array(
  519. "guid" => $user->getGUID(),
  520. "limit" => false,
  521. "metadata_names" => $tag_names
  522. );
  523. // get metadata
  524. $user_values = elgg_get_metadata($user_metadata_options);
  525. if (!empty($user_values)) {
  526. // transform to values
  527. $user_values = metadata_array_to_values($user_values);
  528. // find group with these metadatavalues
  529. $group_options = array(
  530. "type" => "group",
  531. "metadata_names" => $tag_names,
  532. "metadata_values" => $user_values,
  533. "wheres" => $group_membership_where,
  534. "group_by" => "e.guid",
  535. "order_by" => "count(msn.id) DESC",
  536. "limit" => $limit
  537. );
  538. $groups = elgg_get_entities_from_metadata($group_options);
  539. if (!empty($groups)) {
  540. foreach ($groups as $group) {
  541. $result[$group->getGUID()] = $group;
  542. $limit--;
  543. }
  544. }
  545. }
  546. }
  547. }
  548. // get admin defined suggested groups
  549. $group_guids = string_to_tag_array(elgg_get_plugin_setting("suggested_groups","group_tools"));
  550. if (!empty($group_guids) && ($limit > 0)) {
  551. $group_options = array(
  552. "guids" => $group_guids,
  553. "type" => "group",
  554. "wheres" => array($group_membership_where),
  555. "limit" => $limit
  556. );
  557. if (!empty($result)) {
  558. $suggested_guids = array_keys($result);
  559. $group_options["wheres"][] = "e.guid NOT IN (" . implode(",", $suggested_guids) . ")";
  560. }
  561. $groups = elgg_get_entities($group_options);
  562. if (!empty($groups)) {
  563. foreach ($groups as $group) {
  564. $result[$group->getGUID()] = $group;
  565. }
  566. }
  567. }
  568. }
  569. return $result;
  570. }
  571. /**
  572. * Show an indicator if the group is hidden
  573. *
  574. * @param ElggGroup $group The group to check
  575. *
  576. * @return boolean true if an indicator should be shown
  577. */
  578. function group_tools_show_hidden_indicator(ElggGroup $group) {
  579. static $check_required;
  580. $result = false;
  581. if (!isset($check_required)) {
  582. $check_required = false;
  583. $groups_setting = elgg_get_plugin_setting("hidden_groups", "groups");
  584. $setting = elgg_get_plugin_setting("show_hidden_group_indicator", "group_tools");
  585. if (($groups_setting == "yes") && !empty($setting) && ($setting != "no")) {
  586. $check_required = $setting;
  587. }
  588. }
  589. if ($check_required !== false) {
  590. // when to show
  591. if ($check_required == "group_acl") {
  592. // only if group is limited to members
  593. if (($group->access_id != ACCESS_PUBLIC) && ($group->access_id != ACCESS_LOGGED_IN)) {
  594. $result = true;
  595. }
  596. } else {
  597. // for all non public groups
  598. if ($group->access_id != ACCESS_PUBLIC) {
  599. $result = true;
  600. }
  601. }
  602. }
  603. return $result;
  604. }
  605. /**
  606. * Check the plugin setting which enables domain based groups
  607. *
  608. * @return boolean
  609. */
  610. function group_tools_domain_based_groups_enabled() {
  611. static $result;
  612. if (!isset($result)) {
  613. $result = false;
  614. $setting = elgg_get_plugin_setting("domain_based", "group_tools");
  615. if ($setting == "yes") {
  616. $result = true;
  617. }
  618. }
  619. return $result;
  620. }
  621. /**
  622. * Check if the domain based settings for this group match the user
  623. *
  624. * @param ElggGroup $group the group to match to
  625. * @param ElggUser $user the user to check (defaults to current user)
  626. *
  627. * @return boolean true if the domain of the user is found in the group settings
  628. */
  629. function group_tools_check_domain_based_group(ElggGroup $group, ElggUser $user = null) {
  630. $result = false;
  631. if (group_tools_domain_based_groups_enabled()) {
  632. if (empty($user)) {
  633. $user = elgg_get_logged_in_user_entity();
  634. }
  635. if (!empty($group) && elgg_instanceof($group, "group") && !empty($user) && elgg_instanceof($user, "user")) {
  636. $domains = $group->getPrivateSetting("domain_based");
  637. if (!empty($domains)) {
  638. $domains = explode("|", strtolower(trim($domains, "|")));
  639. list(,$domain) = explode("@", strtolower($user->email));
  640. if (in_array($domain, $domains)) {
  641. $result = true;
  642. }
  643. }
  644. }
  645. }
  646. return $result;
  647. }
  648. /**
  649. * Get all groups based on the email domain of the user from the group settings
  650. *
  651. * @param ElggUser $user The user used to base the search
  652. * @param int $site_guid (optional) the site guid to limit the search to, defaults to current site
  653. *
  654. * @return bool|ElggGroup[] false or an array of found groups
  655. */
  656. function group_tools_get_domain_based_groups(ElggUser $user, $site_guid = 0) {
  657. $result = false;
  658. if (group_tools_domain_based_groups_enabled()) {
  659. if (empty($site_guid)) {
  660. $site_guid = elgg_get_site_entity()->getGUID();
  661. }
  662. if (!empty($user) && elgg_instanceof($user, "user")) {
  663. list(, $domain) = explode("@", strtolower($user->email));
  664. $options = array(
  665. "type" => "group",
  666. "limit" => false,
  667. "site_guids" => $site_guid,
  668. "private_setting_name_value_pairs" => array(
  669. "name" => "domain_based",
  670. "value" => "%|" . $domain . "|%",
  671. "operand" => "LIKE"
  672. )
  673. );
  674. $groups = elgg_get_entities_from_private_settings($options);
  675. if (!empty($groups)) {
  676. $result = $groups;
  677. }
  678. }
  679. }
  680. return $result;
  681. }
  682. /**
  683. * Enable registration on the site if disabled and a valid group invite code in provided
  684. *
  685. * @return void
  686. */
  687. function group_tools_enable_registration() {
  688. $registration_allowed = (bool) elgg_get_config("allow_registration");
  689. if (!$registration_allowed) {
  690. // check for a group invite code
  691. $group_invitecode = get_input("group_invitecode");
  692. if (!empty($group_invitecode)) {
  693. // check if the code is valid
  694. if (group_tools_check_group_email_invitation($group_invitecode)) {
  695. // we have a valid code, so allow registration
  696. elgg_set_config("allow_registration", true);
  697. }
  698. }
  699. }
  700. }
  701. /**
  702. * Helper function to transfer the ownership of a group to a new user
  703. *
  704. * @param ElggGroup $group the group to transfer
  705. * @param ElggUser $new_owner the new owner
  706. *
  707. * @return boolean
  708. */
  709. function group_tools_transfer_group_ownership(ElggGroup $group, ElggUser $new_owner) {
  710. $result = false;
  711. if (empty($group) || !elgg_instanceof($group, "group") || !$group->canEdit()) {
  712. return $result;
  713. }
  714. if (empty($new_owner) || !elgg_instanceof($new_owner, "user")) {
  715. return $result;
  716. }
  717. $loggedin_user = elgg_get_logged_in_user_entity();
  718. // register plugin hook to make sure transfer can complete
  719. elgg_register_plugin_hook_handler("permissions_check", "group", "group_tools_admin_transfer_permissions_hook");
  720. $old_owner = $group->getOwnerEntity();
  721. // transfer ownership
  722. $group->owner_guid = $new_owner->getGUID();
  723. $group->container_guid = $new_owner->getGUID();
  724. // make sure user is added to the group
  725. $group->join($new_owner);
  726. if ($group->save()) {
  727. // remove existing group administrator role for new owner
  728. remove_entity_relationship($new_owner->getGUID(), "group_admin", $group->getGUID());
  729. // check for group icon
  730. if (!empty($group->icontime)) {
  731. $prefix = "groups/" . $group->getGUID();
  732. $sizes = array("", "tiny", "small", "medium", "large");
  733. $ofh = new ElggFile();
  734. $ofh->owner_guid = $old_owner->getGUID();
  735. $nfh = new ElggFile();
  736. $nfh->owner_guid = $group->getOwnerGUID();
  737. foreach ($sizes as $size) {
  738. // set correct file to handle
  739. $ofh->setFilename($prefix . $size . ".jpg");
  740. $nfh->setFilename($prefix . $size . ".jpg");
  741. // open files
  742. $ofh->open("read");
  743. $nfh->open("write");
  744. // copy file
  745. $nfh->write($ofh->grabFile());
  746. // close file
  747. $ofh->close();
  748. $nfh->close();
  749. // cleanup old file
  750. $ofh->delete();
  751. }
  752. $group->icontime = time();
  753. }
  754. // move metadata of the group to the new owner
  755. $options = array(
  756. "guid" => $group->getGUID(),
  757. "limit" => false
  758. );
  759. $metadata = elgg_get_metadata($options);
  760. if (!empty($metadata)) {
  761. foreach ($metadata as $md) {
  762. if ($md->owner_guid == $old_owner->getGUID()) {
  763. $md->owner_guid = $new_owner->getGUID();
  764. $md->save();
  765. }
  766. }
  767. }
  768. // notify new owner
  769. if ($new_owner->getGUID() != $loggedin_user->getGUID()) {
  770. $subject = elgg_echo("group_tools:notify:transfer:subject", array($group->name));
  771. $message = elgg_echo("group_tools:notify:transfer:message", array(
  772. $new_owner->name,
  773. $loggedin_user->name,
  774. $group->name,
  775. $group->getURL())
  776. );
  777. notify_user($new_owner->getGUID(), $group->getGUID(), $subject, $message);
  778. }
  779. $result = true;
  780. }
  781. // unregister plugin hook to make sure transfer can complete
  782. elgg_unregister_plugin_hook_handler("permissions_check", "group", "group_tools_admin_transfer_permissions_hook");
  783. return $result;
  784. }
  785. /**
  786. * Get the tool presets from the plugin settings
  787. *
  788. * @return bool|array
  789. */
  790. function group_tools_get_tool_presets() {
  791. $result = false;
  792. $presets = elgg_get_plugin_setting("group_tool_presets", "group_tools");
  793. if (!empty($presets)) {
  794. $result = json_decode($presets, true);
  795. }
  796. return $result;
  797. }
  798. /**
  799. * Get the time_created from the group membership relation
  800. *
  801. * @param ElggUser $user the user to check
  802. * @param ElggGroup $group the group to check
  803. *
  804. * @return int
  805. */
  806. function group_tools_get_membership_information(ElggUser $user, ElggGroup $group) {
  807. $result = 0;
  808. if (!empty($user) && !empty($group)) {
  809. $query = "SELECT *";
  810. $query .= " FROM " . elgg_get_config("dbprefix") . "entity_relationships";
  811. $query .= " WHERE guid_one = " . $user->getGUID();
  812. $query .= " AND guid_two = " . $group->getGUID();
  813. $query .= " AND relationship = 'member'";
  814. $row = get_data_row($query);
  815. if (!empty($row)) {
  816. $result = $row->time_created;
  817. }
  818. }
  819. return $result;
  820. }
  821. /**
  822. * Check the plugin setting to allow multiple group admins
  823. *
  824. * @return bool
  825. */
  826. function group_tools_multiple_admin_enabled() {
  827. static $result;
  828. if (!isset($result)) {
  829. $result = false;
  830. if (elgg_get_plugin_setting("multiple_admin", "group_tools") == "yes") {
  831. $result = true;
  832. }
  833. }
  834. return $result;
  835. }
  836. /**
  837. * Check if the group allows multiple admins
  838. *
  839. * @param ElggGroup $group the group to check
  840. * @param int $user_guid the user to check with
  841. *
  842. * @return bool
  843. */
  844. function group_tools_group_multiple_admin_enabled(ElggGroup $group, $user_guid = 0) {
  845. $result = false;
  846. if (empty($group) || !elgg_instanceof($group, "group")) {
  847. return $result;
  848. }
  849. $user_guid = sanitise_int($user_guid, false);
  850. if (empty($user_guid)) {
  851. $user_guid = elgg_get_logged_in_user_guid();
  852. }
  853. if (empty($user_guid)) {
  854. return $result;
  855. }
  856. if (!group_tools_multiple_admin_enabled()) {
  857. return $result;
  858. }
  859. if (($group->getOwnerGUID() == $user_guid) || elgg_is_admin_logged_in()) {
  860. $result = true;
  861. } elseif (($group->group_multiple_admin_allow_enable == "yes") && $group->canEdit($user_guid)) {
  862. $result = true;
  863. }
  864. return $result;
  865. }