• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Freegle / iznik-server / 01a0045f-f8d0-490a-ab94-b493b0d7d3b8

pending completion
01a0045f-f8d0-490a-ab94-b493b0d7d3b8

push

circleci

Edward Hibbert
Various changes for server move.

4 of 4 new or added lines in 1 file covered. (100.0%)

19563 of 20557 relevant lines covered (95.16%)

32.33 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.27
/http/api/group.php
1
<?php
2
namespace Freegle\Iznik;
3

4
function group() {
5
    global $dbhr, $dbhm;
6

7
    $ret = [ 'ret' => 100, 'status' => 'Unknown verb' ];
9✔
8

9
    $me = Session::whoAmI($dbhr, $dbhm);
9✔
10

11
    # The id parameter can be an ID or a nameshort.
12
    $id = Utils::presdef('id', $_REQUEST, NULL);
9✔
13
    $nameshort = NULL;
9✔
14

15
    if (is_numeric($id)) {
9✔
16
        $id = intval($id);
9✔
17
    } else {
18
        $nameshort = $id;
3✔
19
    }
20

21
    $action = Utils::presdef('action', $_REQUEST, NULL);
9✔
22

23
    if ($nameshort) {
9✔
24
        $g = Group::get($dbhr, $dbhm);
1✔
25
        $id = $g->findByShortName($nameshort);
1✔
26
    }
27

28
    if ($id || ($action == 'Create') || ($action == 'Contact') || ($action == 'RecordFacebookShare' || ($action == 'RemoveFacebook'))) {
9✔
29
        $g = new Group($dbhr, $dbhm, $id);
9✔
30

31
        switch ($_REQUEST['type']) {
9✔
32
            case 'GET': {
9✔
33
                $ret = [
8✔
34
                    'ret' => 10,
35
                    'status' => 'Invalid group id'
36
                ];
37

38
                if ($id && $g->getId() == $id) {
8✔
39
                    $members = array_key_exists('members', $_REQUEST) ? filter_var($_REQUEST['members'], FILTER_VALIDATE_BOOLEAN) : FALSE;
8✔
40
                    $showmods = array_key_exists('showmods', $_REQUEST) ? filter_var($_REQUEST['showmods'], FILTER_VALIDATE_BOOLEAN) : FALSE;
8✔
41

42
                    $ret = [
8✔
43
                        'ret' => 0,
44
                        'status' => 'Success',
45
                        'group' => $g->getPublic()
8✔
46
                    ];
47

48
                    $ret['group']['myrole'] = $me ? $me->getRoleForGroup($id) : User::ROLE_NONMEMBER;
8✔
49
                    $ret['group']['mysettings'] = $me ? $me->getGroupSettings($id) : NULL;
8✔
50
                    $ctx = Utils::presdef('context', $_REQUEST, NULL);
8✔
51
                    $limit = Utils::presint('limit', $_REQUEST, 5);
8✔
52
                    $search = Utils::presdef('search', $_REQUEST, NULL);
8✔
53

54
                    if ($members && $me && $me->isModOrOwner($id)) {
8✔
55
                        $ret['group']['members'] = $g->getMembers($limit, $search, $ctx);
2✔
56
                        $ret['context'] = $ctx;
2✔
57
                    }
58

59
                    $partner = Utils::pres('partner', $_SESSION);
8✔
60

61
                    if ($me && $me->isModerator() || $partner) {
8✔
62
                        # Return info on Twitter status.  This isn't secret info - we don't put anything confidential
63
                        # in here - but it's of no interest to members so there's no point delaying them by
64
                        # fetching it.
65
                        #
66
                        # Similar code in session.php
67
                        $t = new Twitter($dbhr, $dbhm, $id);
7✔
68
                        $atts = $t->getPublic();
7✔
69
                        unset($atts['token']);
7✔
70
                        unset($atts['secret']);
7✔
71
                        $atts['authdate'] = Utils::ISODate($atts['authdate']);
7✔
72
                        $ret['group']['twitter'] =  $atts;
7✔
73

74
                        # Ditto Facebook.
75
                        $uids = GroupFacebook::listForGroup($dbhr, $dbhm, $id);
7✔
76
                        $ret['group']['facebook'] = [];
7✔
77

78
                        foreach ($uids as $uid) {
7✔
79
                            $f = new GroupFacebook($dbhr, $dbhm, $uid);
1✔
80
                            $atts = $f->getPublic();
1✔
81
                            unset($atts['token']);
1✔
82
                            $atts['authdate'] = Utils::ISODate($atts['authdate']);
1✔
83
                            $ret['group']['facebook'][] =  $atts;
1✔
84
                        }
85
                    }
86

87
                    if (Utils::presdef('polygon', $_REQUEST, FALSE)) {
8✔
88
                        $ret['group']['cga'] = $g->getPrivate('polyofficial');
2✔
89
                        $ret['group']['dpa'] = $g->getPrivate('poly');
2✔
90
                        $ret['group']['polygon'] = $ret['group']['dpa'] ? $ret['group']['dpa'] : $ret['group']['cga'];
2✔
91
                        $ret['group']['postvisibility'] = $g->getPrivate('postvisibility');
2✔
92
                    }
93

94
                    if (Utils::presdef('tnkey', $_REQUEST, FALSE) && $me && $me->isModerator()) {
8✔
95
                        # Get the link that we could use to access TN settings.
96
                        $tnurl = 'https://trashnothing.com/modtools/api/group-settings-url?key=' . TNKEY . '&moderator_email=' . urlencode($me->getEmailPreferred()) . '&group_id=' . urlencode($ret['group']['nameshort']);
×
97
                        $url = file_get_contents($tnurl);
×
98
                        $ret['group']['tnkey'] = json_decode($url, TRUE);
×
99
                        $ret['group']['tnur'] = $tnurl;
×
100
                    }
101

102
                    if (Utils::presdef('sponsors', $_REQUEST, FALSE)) {
8✔
103
                        $ret['group']['sponsors'] = $g->getSponsorships();
1✔
104
                    }
105

106
                    if (Utils::presdef('affiliationconfirmedby', $_REQUEST, FALSE)) {
8✔
107
                        $by = $g->getPrivate('affiliationconfirmedby');
1✔
108

109
                        if ($by) {
1✔
110
                            $byu = User::get($dbhr, $dbhm, $by);
1✔
111
                            $ret['group']['affiliationconfirmedby'] = [
1✔
112
                                'id' => $by,
113
                                'displayname' => $byu->getName()
1✔
114
                            ];
115
                        }
116
                    }
117

118
                    if ($showmods) {
8✔
119
                        # We want the list of visible mods.
120
                        $ctx = NULL;
1✔
121
                        $mods = $g->getMembers(100, NULL, $ctx, NULL, MembershipCollection::APPROVED, NULL, NULL, NULL, NULL, Group::FILTER_MODERATORS);
1✔
122
                        $toshow = [];
1✔
123

124
                        foreach ($mods as $mod) {
1✔
125
                            $u = User::get($dbhr, $dbhm, $mod['userid']);
1✔
126
                            $settings = $u->getPrivate('settings');
1✔
127
                            $settings = $settings ? json_decode($settings, TRUE) : [];
1✔
128
                            if (Utils::pres('showmod', $settings)) {
1✔
129
                                # We can show this mod.  Return basic info about them.
130
                                $atts = $u->getPublic(NULL, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE);
1✔
131
                                $toshow[] = [
1✔
132
                                    'id' => $mod['userid'],
1✔
133
                                    'firstname' => $atts['firstname'],
1✔
134
                                    'lastname' => $atts['lastname'],
1✔
135
                                    'displayname' => $atts['displayname'],
1✔
136
                                    'profile' => $atts['profile']
1✔
137
                                ];
138
                            }
139
                        }
140

141
                        $ret['group']['showmods'] = $toshow;
1✔
142
                    }
143
                }
144
                break;
8✔
145
            }
146

147
            case 'PATCH': {
6✔
148
                $settings = Utils::presdef('settings', $_REQUEST, NULL);
3✔
149
                $profile = (Utils::presint('profile', $_REQUEST, NULL));
3✔
150

151
                $ret = [
3✔
152
                    'ret' => 1,
153
                    'status' => 'Not logged in',
154
                ];
155

156
                if ($me) {
3✔
157
                    $ret = [
3✔
158
                        'ret' => 1,
159
                        'status' => 'Failed or permission denied'
160
                    ];
161

162
                    if ($me->isModOrOwner($id) || $me->isAdminOrSupport()) {
3✔
163
                        $ret = [
3✔
164
                            'ret' => 0,
165
                            'status' => 'Success'
166
                        ];
167

168
                        if ($settings) {
3✔
169
                            $g->setSettings($settings);
1✔
170
                        }
171

172
                        if ($profile) {
3✔
173
                            # Set the profile picture.  Rescale if need be to 200x200 to save space in the DB and,
174
                            # more importantly, download time.
175
                            $g->setPrivate('profile', $profile);
1✔
176
                            $a = new Attachment($dbhr, $dbhm, $profile, Attachment::TYPE_GROUP);
1✔
177
                            $data = $a->getData();
1✔
178
                            $i = new Image($data);
1✔
179
                            
180
                            if ($i->width() > 200 || $i->height() > 200) {
1✔
181
                                $i->scale(200, 200);
1✔
182
                                $data = $i->getData(100);
1✔
183
                                $a->setPrivate('data', $data);
1✔
184
                            }
185

186
                            $a->setPrivate('groupid', $id);
1✔
187
                        }
188

189
                        # Other settable attributes
190
                        foreach (['onhere', 'publish', 'microvolunteering', 'mentored'] as $att) {
3✔
191
                            $val = Utils::presdef($att, $_REQUEST, NULL);
3✔
192
                            if (array_key_exists($att, $_REQUEST)) {
3✔
193
                                $g->setPrivate($att, $val);
×
194

195
                                if ($att ==  'affiliationconfirmed') {
×
196
                                    $g->setPrivate('affiliationconfirmedby', $me->getId());
×
197
                                }
198
                            }
199
                        }
200

201
                        foreach (['microvolunteeringoptions'] as $att) {
3✔
202
                            $val = Utils::presdef($att, $_REQUEST, NULL);
3✔
203
                            if (array_key_exists($att, $_REQUEST)) {
3✔
204
                                $g->setPrivate($att, json_encode($val));
×
205
                            }
206
                        }
207

208
                        foreach (['tagline', 'namefull', 'welcomemail', 'description', 'region', 'affiliationconfirmed'] as $att) {
3✔
209
                            $val = Utils::presdef($att, $_REQUEST, NULL);
3✔
210
                            if (array_key_exists($att, $_REQUEST) && $val != "1") {
3✔
211
                                $g->setPrivate($att, $val);
2✔
212

213
                                if ($att ==  'affiliationconfirmed') {
2✔
214
                                    $g->setPrivate('affiliationconfirmedby', $me->getId());
1✔
215
                                }
216
                            }
217
                        }
218

219
                        # Other support-settable attributes
220
                        if ($me->isAdminOrSupport()) {
3✔
221
                            foreach (['publish', 'licenserequired', 'lat', 'lng', 'altlat', 'altlng'] as $att) {
1✔
222
                                $val = Utils::presdef($att, $_REQUEST, NULL);
1✔
223
                                if (array_key_exists($att, $_REQUEST)) {
1✔
224
                                    $g->setPrivate($att, $val);
1✔
225
                                }
226
                            }
227

228
                            foreach (['poly', 'polyofficial'] as $att) {
1✔
229
                                $val = Utils::presdef($att, $_REQUEST, NULL);
1✔
230
                                if (array_key_exists($att, $_REQUEST)) {
1✔
231
                                    if (!$g->setPrivate($att, $val)) {
1✔
232
                                        $ret = [
1✔
233
                                            'ret' => 3,
234
                                            'status' => 'Invalid polygon data'
235
                                        ];
236
                                    }
237
                                }
238
                            }
239
                        }
240

241
                        $postvisibility = Utils::presdef('postvisibility', $_REQUEST, NULL);
3✔
242

243
                        if ($postvisibility) {
3✔
244
                            $g->setPrivate('postvisibility', $postvisibility);
1✔
245
                        }
246
                    }
247
                }
248
            }
249

250
            case 'POST': {
3✔
251
                switch ($action) {
252
                    case 'Create': {
6✔
253
                        $ret = [
1✔
254
                            'ret' => 1,
255
                            'status' => 'Not logged in'
256
                        ];
257

258
                        # Only mods can create.
259
                        if ($me && $me->isModerator()) {
1✔
260
                            $name = Utils::presdef('name', $_REQUEST, NULL);
1✔
261
                            $type = Utils::presdef('grouptype', $_REQUEST, NULL);
1✔
262
                            $lat = Utils::presfloat('lat', $_REQUEST, NULL);
1✔
263
                            $lng = Utils::presfloat('lng', $_REQUEST, NULL);
1✔
264
                            $core = Utils::presdef('corearea', $_REQUEST, NULL);
1✔
265
                            $catchment = Utils::presdef('atchmentarea', $_REQUEST, NULL);
1✔
266

267
                            $id = $g->create($name, $type);
1✔
268

269
                            $ret = ['ret' => 2, 'status' => 'Create failed'];
1✔
270

271
                            if ($id) {
1✔
272
                                $me->addMembership($id, User::ROLE_OWNER);
1✔
273

274
                                $ret = [
1✔
275
                                    'ret' => 0,
276
                                    'status' => 'Success',
277
                                    'id' => $id
278
                                ];
279

280
                                if ($me && $me->isAdminOrSupport()) {
1✔
281
                                    # Admin or support can say where a group is. Not normal mods otherwise people might
282
                                    # trample on each other's toes.
283
                                    $g->setPrivate('lat', $lat);
1✔
284
                                    $g->setPrivate('lng', $lng);
1✔
285
                                    $g->setPrivate('polyofficial', $core);
1✔
286
                                    $g->setPrivate('poly', $catchment);
1✔
287
                                }
288
                            }
289
                        }
290

291
                        break;
1✔
292
                    }
293

294
                    case 'ConfirmKey': {
5✔
295
                        if ($me && $me->isAdminOrSupport()) {
1✔
296
                            # If we already have Admin or Support rights, we trust ourselves enough to add the
297
                            # membership immediately.  This helps with people who are on many groups, because
298
                            # it avoids having to wait for Yahoo invitation processing.
299
                            #
300
                            # If this is incorrect, and we're not actually a mod on Yahoo, then it will get
301
                            # downgraded on the next sync.
302
                            $me->addMembership($id, User::ROLE_MODERATOR);
1✔
303
                            $ret = [
1✔
304
                                'ret' => 100,
305
                                'status' => 'Added status on server.'
306
                            ];
307
                        } else {
308
                            $ret = [
1✔
309
                                'ret' => 0,
310
                                'status' => 'Success',
311
                                'key' => $g->getConfirmKey()
1✔
312
                            ];
313
                        }
314

315
                        break;
1✔
316
                    }
317

318
                    case 'RemoveFacebook': {
4✔
319
                        $uid = (Utils::presint('uid', $_REQUEST, NULL));
1✔
320
                        $ret = ['ret' => 2, 'status' => 'Invalid parameters'];
1✔
321

322
                        if ($uid) {
1✔
323
                            $f = new GroupFacebook($dbhr, $dbhm);
1✔
324
                            $f->remove($uid);
1✔
325
                            $ret = ['ret' => 0, 'status' => 'Success'];
1✔
326
                        }
327

328
                        break;
1✔
329
                    }                }
330

331
                break;
9✔
332
            }
333
        }
334
    } else {
335
        $ret = [
1✔
336
            'ret' => 2,
337
            'status' => 'We don\'t host that group'
338
        ];
339
    }
340

341
    return($ret);
9✔
342
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc