Merge remote-tracking branch 'origin/Issue-#3878' into Issue-#3878
[friendica.git/.git] / mod / profperm.php
1 <?php
2
3 use Friendica\App;
4 use Friendica\Core\Config;
5 use Friendica\Core\PConfig;
6 use Friendica\Database\DBM;
7
8 function profperm_init(App $a) {
9
10         if (! local_user()) {
11                 return;
12         }
13
14         $which = $a->user['nickname'];
15         $profile = $a->argv[1];
16
17         profile_load($a,$which,$profile);
18
19 }
20
21
22 function profperm_content(App $a) {
23
24         if (! local_user()) {
25                 notice( t('Permission denied') . EOL);
26                 return;
27         }
28
29
30         if($a->argc < 2) {
31                 notice( t('Invalid profile identifier.') . EOL );
32                 return;
33         }
34
35         // Switch to text mod interface if we have more than 'n' contacts or group members
36
37         $switchtotext = PConfig::get(local_user(),'system','groupedit_image_limit');
38         if (is_null($switchtotext)) {
39                 $switchtotext = Config::get('system','groupedit_image_limit', 400);
40         }
41
42         if(($a->argc > 2) && intval($a->argv[1]) && intval($a->argv[2])) {
43                 $r = q("SELECT `id` FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `self` = 0
44                         AND `network` = '%s' AND `id` = %d AND `uid` = %d LIMIT 1",
45                         dbesc(NETWORK_DFRN),
46                         intval($a->argv[2]),
47                         intval(local_user())
48                 );
49                 if (DBM::is_result($r))
50                         $change = intval($a->argv[2]);
51         }
52
53
54         if(($a->argc > 1) && (intval($a->argv[1]))) {
55                 $r = q("SELECT * FROM `profile` WHERE `id` = %d AND `uid` = %d AND `is-default` = 0 LIMIT 1",
56                         intval($a->argv[1]),
57                         intval(local_user())
58                 );
59                 if (! DBM::is_result($r)) {
60                         notice( t('Invalid profile identifier.') . EOL );
61                         return;
62                 }
63                 $profile = $r[0];
64
65                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `profile-id` = %d",
66                         intval(local_user()),
67                         intval($a->argv[1])
68                 );
69
70                 $ingroup = array();
71                 if (DBM::is_result($r))
72                         foreach($r as $member)
73                                 $ingroup[] = $member['id'];
74
75                 $members = $r;
76
77                 if($change) {
78                         if(in_array($change,$ingroup)) {
79                                 q("UPDATE `contact` SET `profile-id` = 0 WHERE `id` = %d AND `uid` = %d",
80                                         intval($change),
81                                         intval(local_user())
82                                 );
83                         }
84                         else {
85                                 q("UPDATE `contact` SET `profile-id` = %d WHERE `id` = %d AND `uid` = %d",
86                                         intval($a->argv[1]),
87                                         intval($change),
88                                         intval(local_user())
89                                 );
90
91                         }
92
93                         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `profile-id` = %d",
94                                 intval(local_user()),
95                                 intval($a->argv[1])
96                         );
97
98                         $members = $r;
99
100                         $ingroup = array();
101                         if (DBM::is_result($r))
102                                 foreach($r as $member)
103                                         $ingroup[] = $member['id'];
104                 }
105
106                 $o .= '<h2>' . t('Profile Visibility Editor') . '</h2>';
107
108                 $o .= '<h3>' . t('Profile') . ' \'' . $profile['profile-name'] . '\'</h3>';
109
110                 $o .= '<div id="prof-edit-desc">' . t('Click on a contact to add or remove.') . '</div>';
111
112         }
113
114         $o .= '<div id="prof-update-wrapper">';
115         if($change)
116                 $o = '';
117
118         $o .= '<div id="prof-members-title">';
119         $o .= '<h3>' . t('Visible To') . '</h3>';
120         $o .= '</div>';
121         $o .= '<div id="prof-members">';
122
123         $textmode = (($switchtotext && (count($members) > $switchtotext)) ? true : false);
124
125         foreach($members as $member) {
126                 if($member['url']) {
127                         $member['click'] = 'profChangeMember(' . $profile['id'] . ',' . $member['id'] . '); return true;';
128                         $o .= micropro($member,true,'mpprof', $textmode);
129                 }
130         }
131         $o .= '</div><div id="prof-members-end"></div>';
132         $o .= '<hr id="prof-separator" />';
133
134         $o .= '<div id="prof-all-contcts-title">';
135         $o .= '<h3>' . t("All Contacts \x28with secure profile access\x29") . '</h3>';
136         $o .= '</div>';
137         $o .= '<div id="prof-all-contacts">';
138
139                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `blocked` = 0 and `pending` = 0 and `self` = 0
140                         AND `network` = '%s' ORDER BY `name` ASC",
141                         intval(local_user()),
142                         dbesc(NETWORK_DFRN)
143                 );
144
145                 if (DBM::is_result($r)) {
146                         $textmode = (($switchtotext && (count($r) > $switchtotext)) ? true : false);
147                         foreach($r as $member) {
148                                 if(! in_array($member['id'],$ingroup)) {
149                                         $member['click'] = 'profChangeMember(' . $profile['id'] . ',' . $member['id'] . '); return true;';
150                                         $o .= micropro($member,true,'mpprof',$textmode);
151                                 }
152                         }
153                 }
154
155                 $o .= '</div><div id="prof-all-contacts-end"></div>';
156
157         if($change) {
158                 echo $o;
159                 killme();
160         }
161         $o .= '</div>';
162         return $o;
163
164 }
165