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

Freegle / iznik-server / 49885719-c516-447d-96c8-ce59b253cca2

03 Jan 2025 06:28PM UTC coverage: 92.309% (-0.01%) from 92.321%
49885719-c516-447d-96c8-ce59b253cca2

push

circleci

edwh
Test fixes.

25504 of 27629 relevant lines covered (92.31%)

31.52 hits per line

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

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

4
function group() {
5
    global $dbhr, $dbhm;
9✔
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,
8✔
35
                    'status' => 'Invalid group id'
8✔
36
                ];
8✔
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,
8✔
44
                        'status' => 'Success',
8✔
45
                        'group' => $g->getPublic()
8✔
46
                    ];
8✔
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 Facebook 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
                        $uids = GroupFacebook::listForGroup($dbhr, $dbhm, $id);
7✔
68
                        $ret['group']['facebook'] = [];
7✔
69

70
                        foreach ($uids as $uid) {
7✔
71
                            $f = new GroupFacebook($dbhr, $dbhm, $uid);
1✔
72
                            $atts = $f->getPublic();
1✔
73
                            unset($atts['token']);
1✔
74
                            $atts['authdate'] = Utils::ISODate($atts['authdate']);
1✔
75
                            $ret['group']['facebook'][] =  $atts;
1✔
76
                        }
77
                    }
78

79
                    if (Utils::presdef('polygon', $_REQUEST, FALSE)) {
8✔
80
                        $ret['group']['cga'] = $g->getPrivate('polyofficial');
2✔
81
                        $ret['group']['dpa'] = $g->getPrivate('poly');
2✔
82
                        $ret['group']['polygon'] = $ret['group']['dpa'] ? $ret['group']['dpa'] : $ret['group']['cga'];
2✔
83
                        $ret['group']['postvisibility'] = $g->getPrivate('postvisibility');
2✔
84
                    }
85

86
                    if (Utils::presdef('tnkey', $_REQUEST, FALSE) && $me && $me->isModerator()) {
8✔
87
                        # Get the link that we could use to access TN settings.
88
                        $tnurl = 'https://trashnothing.com/modtools/api/group-settings-url?key=' . TNKEY . '&moderator_email=' . urlencode($me->getEmailPreferred()) . '&group_id=' . urlencode($ret['group']['nameshort']);
×
89
                        $url = file_get_contents($tnurl);
×
90
                        $ret['group']['tnkey'] = json_decode($url, TRUE);
×
91
                        $ret['group']['tnur'] = $tnurl;
×
92
                    }
93

94
                    if (Utils::presdef('sponsors', $_REQUEST, FALSE)) {
8✔
95
                        $ret['group']['sponsors'] = $g->getSponsorships();
1✔
96
                    }
97

98
                    if (Utils::presdef('affiliationconfirmedby', $_REQUEST, FALSE)) {
8✔
99
                        $by = $g->getPrivate('affiliationconfirmedby');
1✔
100

101
                        if ($by) {
1✔
102
                            $byu = User::get($dbhr, $dbhm, $by);
1✔
103
                            $ret['group']['affiliationconfirmedby'] = [
1✔
104
                                'id' => $by,
1✔
105
                                'displayname' => $byu->getName()
1✔
106
                            ];
1✔
107
                        }
108
                    }
109

110
                    if ($showmods) {
8✔
111
                        # We want the list of visible mods.
112
                        $ctx = NULL;
1✔
113
                        $mods = $g->getMembers(100, NULL, $ctx, NULL, MembershipCollection::APPROVED, NULL, NULL, NULL, NULL, Group::FILTER_MODERATORS);
1✔
114
                        $toshow = [];
1✔
115

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

133
                        $ret['group']['showmods'] = $toshow;
1✔
134
                    }
135
                }
136
                break;
8✔
137
            }
138

139
            case 'PATCH': {
6✔
140
                $settings = Utils::presdef('settings', $_REQUEST, NULL);
3✔
141
                $rules = Utils::presdef('rules', $_REQUEST, NULL);
3✔
142
                $profile = (Utils::presint('profile', $_REQUEST, NULL));
3✔
143

144
                $ret = [
3✔
145
                    'ret' => 1,
3✔
146
                    'status' => 'Not logged in',
3✔
147
                ];
3✔
148

149
                if ($me) {
3✔
150
                    $ret = [
3✔
151
                        'ret' => 1,
3✔
152
                        'status' => 'Failed or permission denied'
3✔
153
                    ];
3✔
154

155
                    if ($me->isModOrOwner($id) || $me->isAdminOrSupport()) {
3✔
156
                        $ret = [
3✔
157
                            'ret' => 0,
3✔
158
                            'status' => 'Success'
3✔
159
                        ];
3✔
160

161
                        if ($settings) {
3✔
162
                            $g->setSettings($settings);
1✔
163
                        }
164

165
                        if ($rules) {
3✔
166
                            $g->setRules($rules);
×
167
                        }
168

169
                        if ($profile) {
3✔
170
                            # Set the profile picture.  Rescale if need be to 200x200 to save space in the DB and,
171
                            # more importantly, download time.
172
                            $g->setPrivate('profile', $profile);
1✔
173
                            $a = new Attachment($dbhr, $dbhm, $profile, Attachment::TYPE_GROUP);
1✔
174
                            $a->setPrivate('groupid', $id);
1✔
175
                        }
176

177
                        # Other settable attributes
178
                        foreach (['onhere', 'publish', 'microvolunteering', 'mentored', 'ontn', 'onlovejunk'] as $att) {
3✔
179
                            $val = Utils::presdef($att, $_REQUEST, NULL);
3✔
180
                            if (array_key_exists($att, $_REQUEST)) {
3✔
181
                                $g->setPrivate($att, $val);
×
182

183
                                if ($att ==  'affiliationconfirmed') {
×
184
                                    $g->setPrivate('affiliationconfirmedby', $me->getId());
×
185
                                }
186
                            }
187
                        }
188

189
                        foreach (['microvolunteeringoptions'] as $att) {
3✔
190
                            $val = Utils::presdef($att, $_REQUEST, NULL);
3✔
191
                            if (array_key_exists($att, $_REQUEST)) {
3✔
192
                                $g->setPrivate($att, json_encode($val));
×
193
                            }
194
                        }
195

196
                        foreach (['tagline', 'namefull', 'welcomemail', 'description', 'region', 'affiliationconfirmed'] as $att) {
3✔
197
                            $val = Utils::presdef($att, $_REQUEST, NULL);
3✔
198
                            if (array_key_exists($att, $_REQUEST) && $val != "1") {
3✔
199
                                $g->setPrivate($att, $val);
2✔
200

201
                                if ($att ==  'affiliationconfirmed') {
2✔
202
                                    $g->setPrivate('affiliationconfirmedby', $me->getId());
1✔
203
                                }
204
                            }
205
                        }
206

207
                        # Other support-settable attributes
208
                        if ($me->isAdminOrSupport()) {
3✔
209
                            foreach (['publish', 'licenserequired', 'lat', 'lng', 'altlat', 'altlng'] as $att) {
1✔
210
                                $val = Utils::presdef($att, $_REQUEST, NULL);
1✔
211
                                if (array_key_exists($att, $_REQUEST)) {
1✔
212
                                    $g->setPrivate($att, $val);
1✔
213
                                }
214
                            }
215

216
                            foreach (['poly', 'polyofficial'] as $att) {
1✔
217
                                $val = Utils::presdef($att, $_REQUEST, NULL);
1✔
218
                                if (array_key_exists($att, $_REQUEST)) {
1✔
219
                                    if (!$g->setPrivate($att, $val)) {
1✔
220
                                        $ret = [
1✔
221
                                            'ret' => 3,
1✔
222
                                            'status' => 'Invalid polygon data'
1✔
223
                                        ];
1✔
224
                                    }
225
                                }
226
                            }
227
                        }
228

229
                        $postvisibility = Utils::presdef('postvisibility', $_REQUEST, NULL);
3✔
230

231
                        if ($postvisibility) {
3✔
232
                            $g->setPrivate('postvisibility', $postvisibility);
1✔
233
                        }
234
                    }
235
                }
236
            }
237

238
            case 'POST': {
3✔
239
                switch ($action) {
240
                    case 'Create': {
6✔
241
                        $ret = [
1✔
242
                            'ret' => 1,
1✔
243
                            'status' => 'Not logged in'
1✔
244
                        ];
1✔
245

246
                        # Only mods can create.
247
                        if ($me && $me->isModerator()) {
1✔
248
                            $name = Utils::presdef('name', $_REQUEST, NULL);
1✔
249
                            $type = Utils::presdef('grouptype', $_REQUEST, NULL);
1✔
250
                            $lat = Utils::presfloat('lat', $_REQUEST, NULL);
1✔
251
                            $lng = Utils::presfloat('lng', $_REQUEST, NULL);
1✔
252
                            $core = Utils::presdef('corearea', $_REQUEST, NULL);
1✔
253
                            $catchment = Utils::presdef('atchmentarea', $_REQUEST, NULL);
1✔
254

255
                            $id = $g->create($name, $type);
1✔
256

257
                            $ret = ['ret' => 2, 'status' => 'Create failed'];
1✔
258

259
                            if ($id) {
1✔
260
                                $me->addMembership($id, User::ROLE_OWNER);
1✔
261

262
                                $ret = [
1✔
263
                                    'ret' => 0,
1✔
264
                                    'status' => 'Success',
1✔
265
                                    'id' => $id
1✔
266
                                ];
1✔
267

268
                                if ($me && $me->isAdminOrSupport()) {
1✔
269
                                    # Admin or support can say where a group is. Not normal mods otherwise people might
270
                                    # trample on each other's toes.
271
                                    $g->setPrivate('lat', $lat);
×
272
                                    $g->setPrivate('lng', $lng);
×
273
                                    $g->setPrivate('polyofficial', $core);
×
274
                                    $g->setPrivate('poly', $catchment);
×
275
                                }
276
                            }
277
                        }
278

279
                        break;
1✔
280
                    }
281

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

303
                        break;
1✔
304
                    }
305

306
                    case 'RemoveFacebook': {
4✔
307
                        $uid = (Utils::presint('uid', $_REQUEST, NULL));
1✔
308
                        $ret = ['ret' => 2, 'status' => 'Invalid parameters'];
1✔
309

310
                        if ($uid) {
1✔
311
                            $f = new GroupFacebook($dbhr, $dbhm);
1✔
312
                            $f->remove($uid);
1✔
313
                            $ret = ['ret' => 0, 'status' => 'Success'];
1✔
314
                        }
315

316
                        break;
1✔
317
                    }                }
318

319
                break;
9✔
320
            }
321
        }
322
    } else {
323
        $ret = [
1✔
324
            'ret' => 2,
1✔
325
            'status' => 'We don\'t host that group'
1✔
326
        ];
1✔
327
    }
328

329
    return($ret);
9✔
330
}
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