2 require_once('mod/settings.php');
4 function delegate_init(App $a) {
5 return settings_init($a);
9 function delegate_content(App $a) {
12 notice( t('Permission denied.') . EOL);
16 if ($a->argc > 2 && $a->argv[1] === 'add' && intval($a->argv[2])) {
18 // delegated admins can view but not change delegation permissions
20 if (x($_SESSION,'submanage') && intval($_SESSION['submanage'])) {
21 goaway(App::get_baseurl() . '/delegate');
26 $r = q("SELECT `nickname` FROM `user` WHERE `uid` = %d LIMIT 1",
29 if (dbm::is_result($r)) {
30 $r = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' LIMIT 1",
32 dbesc(normalise_link(App::get_baseurl() . '/profile/' . $r[0]['nickname']))
34 if (dbm::is_result($r)) {
35 q("INSERT INTO `manage` ( `uid`, `mid` ) VALUES ( %d , %d ) ",
41 goaway(App::get_baseurl() . '/delegate');
44 if ($a->argc > 2 && $a->argv[1] === 'remove' && intval($a->argv[2])) {
46 // delegated admins can view but not change delegation permissions
47 if (x($_SESSION,'submanage') && intval($_SESSION['submanage'])) {
48 goaway(App::get_baseurl() . '/delegate');
51 q("DELETE FROM `manage` WHERE `uid` = %d AND `mid` = %d LIMIT 1",
55 goaway(App::get_baseurl() . '/delegate');
59 $full_managers = array();
61 // These people can manage this account/page with full privilege
63 $r = q("SELECT * FROM `user` WHERE `email` = '%s' AND `password` = '%s' ",
64 dbesc($a->user['email']),
65 dbesc($a->user['password'])
67 if (dbm::is_result($r)) {
73 // find everybody that currently has delegated management to this account/page
75 $r = q("SELECT * FROM `user` WHERE `uid` IN ( SELECT `uid` FROM `manage` WHERE `mid` = %d ) ",
79 if (dbm::is_result($r)) {
85 if (count($full_managers)) {
86 foreach ($full_managers as $rr) {
91 if (count($delegates)) {
92 foreach ($delegates as $rr) {
97 // find every contact who might be a candidate for delegation
99 $r = q("SELECT `nurl` FROM `contact` WHERE SUBSTRING_INDEX(`contact`.`nurl`,'/',3) = '%s'
100 AND `contact`.`uid` = %d AND `contact`.`self` = 0 AND `network` = '%s' ",
101 dbesc(normalise_link(App::get_baseurl())),
102 intval(local_user()),
106 if (! dbm::is_result($r)) {
107 notice( t('No potential page delegates located.') . EOL);
111 $nicknames = array();
113 if (dbm::is_result($r)) {
114 foreach ($r as $rr) {
115 $nicknames[] = "'" . dbesc(basename($rr['nurl'])) . "'";
119 $potentials = array();
121 $nicks = implode(',',$nicknames);
123 // get user records for all potential page delegates who are not already delegates or managers
125 $r = q("SELECT `uid`, `username`, `nickname` FROM `user` WHERE `nickname` IN ( $nicks )");
127 if (dbm::is_result($r)) {
128 foreach ($r as $rr) {
129 if (! in_array($rr['uid'],$uids)) {
135 require_once("mod/settings.php");
138 $o = replace_macros(get_markup_template('delegate.tpl'),array(
139 '$header' => t('Delegate Page Management'),
140 '$base' => App::get_baseurl(),
141 '$desc' => t('Delegates are able to manage all aspects of this account/page except for basic account settings. Please do not delegate your personal account to anybody that you do not trust completely.'),
142 '$head_managers' => t('Existing Page Managers'),
143 '$managers' => $full_managers,
144 '$head_delegates' => t('Existing Page Delegates'),
145 '$delegates' => $delegates,
146 '$head_potentials' => t('Potential Delegates'),
147 '$potentials' => $potentials,
148 '$remove' => t('Remove'),
150 '$none' => t('No entries.')