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

Freegle / iznik-server / 418053ef-1b85-44d6-aeef-204bc752e075

26 Jun 2024 12:25PM UTC coverage: 94.239% (-0.6%) from 94.811%
418053ef-1b85-44d6-aeef-204bc752e075

push

circleci

edwh
Test fixes.

0 of 1 new or added line in 1 file covered. (0.0%)

225 existing lines in 5 files now uncovered.

25190 of 26730 relevant lines covered (94.24%)

31.51 hits per line

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

84.48
/http/api/image.php
1
<?php
2
namespace Freegle\Iznik;
3

4
function image() {
5
    global $dbhr, $dbhm;
26✔
6

7
    $ret = [ 'ret' => 100, 'status' => 'Unknown verb' ];
26✔
8
    $id = (Utils::presint('id', $_REQUEST, 0));
26✔
9
    $msgid = Utils::presint('msgid', $_REQUEST, NULL);
26✔
10
    $ocr = array_key_exists('ocr', $_REQUEST) ? filter_var($_REQUEST['ocr'], FILTER_VALIDATE_BOOLEAN) : FALSE;
26✔
11
    $group = Utils::presdef('group', $_REQUEST, NULL);
26✔
12
    $newsletter = Utils::presdef('newsletter', $_REQUEST, NULL);
26✔
13
    $communityevent = Utils::presdef('communityevent', $_REQUEST, NULL);
26✔
14
    $volunteering = Utils::presdef('volunteering', $_REQUEST, NULL);
26✔
15
    $chatmessage = Utils::presdef('chatmessage', $_REQUEST, NULL);
26✔
16
    $user = Utils::presdef('user', $_REQUEST, NULL);
26✔
17
    $newsfeed = Utils::presdef('newsfeed', $_REQUEST, NULL);
26✔
18
    $story = Utils::presdef('story', $_REQUEST, NULL);
26✔
19
    $noticeboard = Utils::presdef('noticeboard', $_REQUEST, NULL);
26✔
20
    $circle = Utils::presdef('circle', $_REQUEST, NULL);
26✔
21
    $imgtype = Utils::presdef('imgtype', $_REQUEST, Attachment::TYPE_MESSAGE);
26✔
22
    $externaluid = Utils::presdef('externaluid', $_REQUEST, NULL);
26✔
23
    $externalmods = Utils::presdef('externalmods', $_REQUEST, NULL);
26✔
24

25
    $sizelimit = 800;
26✔
26
    
27
    if ($chatmessage) {
26✔
28
        $type = Attachment::TYPE_CHAT_MESSAGE;
2✔
29
    } else if ($communityevent) {
25✔
30
        $type = Attachment::TYPE_COMMUNITY_EVENT;
1✔
31
    } else if ($volunteering) {
25✔
32
        $type = Attachment::TYPE_VOLUNTEERING;
1✔
33
    } else if ($newsletter) {
25✔
34
        $type = Attachment::TYPE_NEWSLETTER;
1✔
35
    } else if ($group) {
25✔
36
        $type = Attachment::TYPE_GROUP;
1✔
37
    } else if ($user) {
25✔
38
        $type = Attachment::TYPE_USER;
1✔
39
    } else if ($newsfeed) {
25✔
40
        $type = Attachment::TYPE_NEWSFEED;
2✔
41
    } else if ($story) {
24✔
42
        $type = Attachment::TYPE_STORY;
1✔
43
    } else if ($noticeboard) {
24✔
UNCOV
44
        $type = Attachment::TYPE_NOTICEBOARD;
×
45
    } else {
46
        $type = Attachment::TYPE_MESSAGE;
24✔
47
    }
48

49
    $_REQUEST['type'] = Utils::pres('typeoverride', $_REQUEST) ? $_REQUEST['typeoverride'] : $_REQUEST['type'];
26✔
50

51
    switch ($_REQUEST['type']) {
26✔
52
        case 'GET': {
26✔
53
            $a = new Attachment($dbhr, $dbhm, $id, $type);
2✔
54
            $redirect = $a->canRedirect();
2✔
55

56
            if ($redirect) {
2✔
57
                $ret = [
2✔
58
                    'ret' => 0,
2✔
59
                    'redirectto' => $redirect
2✔
60
                ];
2✔
61
            } else {
UNCOV
62
                $data = $a->getData();
×
63

UNCOV
64
                $i = new Image($data);
×
65

UNCOV
66
                $ret = [
×
UNCOV
67
                    'ret' => 1,
×
UNCOV
68
                    'status' => "Failed to create image $id of type $type",
×
UNCOV
69
                ];
×
70

UNCOV
71
                if ($i->img) {
×
UNCOV
72
                    $w = (Utils::presint('w', $_REQUEST, $i->width()));
×
UNCOV
73
                    $h = (Utils::presint('h', $_REQUEST, $i->height()));
×
74

UNCOV
75
                    if (($w > 0) || ($h > 0)) {
×
76
                        # Need to resize
UNCOV
77
                        $i->scale($w, $h);
×
78
                    }
79

UNCOV
80
                    if ($circle) {
×
UNCOV
81
                        $i->circle($w);
×
UNCOV
82
                        $ret = [
×
UNCOV
83
                            'ret' => 0,
×
UNCOV
84
                            'status' => 'Success',
×
UNCOV
85
                            'img' => $i->getDataPNG()
×
UNCOV
86
                        ];
×
87
                    } else {
UNCOV
88
                        $ret = [
×
UNCOV
89
                            'ret' => 0,
×
UNCOV
90
                            'status' => 'Success',
×
UNCOV
91
                            'img' => $i->getData()
×
UNCOV
92
                        ];
×
93
                    }
94
                }
95
            }
96

97
            break;
2✔
98
        }
99

100
        case 'POST': {
25✔
101
            $ret = [ 'ret' => 1, 'status' => 'No photo provided' ];
25✔
102

103
            # This next line is to simplify UT.
104
            $rotate = Utils::presint('rotate', $_REQUEST, NULL);
25✔
105

106
            if (array_key_exists('rotate', $_REQUEST)) {
25✔
107
                if ($id) {
1✔
108
                    # We want to rotate.  Do so.
109
                    $a = new Attachment($dbhr, $dbhm, $id, $type);
1✔
110
                    $a->rotate($rotate);
1✔
111

112
                    $ret = [
1✔
113
                        'ret' => 0,
1✔
114
                        'status' => 'Success',
1✔
115
                    ];
1✔
116
                }
117
            } else if ($externaluid) {
25✔
118
                // This is an external image.  Typically it's uploaded to a service like Uploadcare.
119
                $a = new Attachment($dbhr, $dbhm, NULL, $imgtype);
1✔
120
                list ($id, $uid) = $a->create($msgid, NULL, $externaluid, NULL, TRUE, $externalmods);
1✔
121

122
                $ret = [
1✔
123
                    'ret' => 0,
1✔
124
                    'status' => 'Success',
1✔
125
                    'id' => $id,
1✔
126
                    'uid' => $uid,
1✔
127
                ];
1✔
128
            } else {
129
                $photo = Utils::presdef('photo', $_FILES, NULL) ? $_FILES['photo'] : $_REQUEST['photo'];
24✔
130

131
                # Make sure what we have looks plausible - the file upload plugin should ensure this is the case.
132
                if ($photo &&
24✔
133
                    Utils::pres('tmp_name', $photo)) {
24✔
134
                    try {
135
                        # We may need to rotate.
136
                        $ret = [ 'ret' => 2, 'status' => 'Invalid data provided' ];
24✔
137
                        $data = file_get_contents($photo['tmp_name']);
24✔
138
                        $image = @imagecreatefromstring($data);
24✔
139

140
                        if ($image) {
24✔
141
                            $exif = @exif_read_data($photo['tmp_name']);
23✔
142

143
                            if($exif && !empty($exif['Orientation'])) {
23✔
144
                                switch($exif['Orientation']) {
19✔
145
                                    case 2:
19✔
146
                                        imageflip($image , IMG_FLIP_HORIZONTAL);
1✔
147
                                        break;
1✔
148
                                    case 3:
18✔
149
                                        $image = imagerotate($image,180,0);
1✔
150
                                        break;
1✔
151
                                    case 4:
17✔
152
                                        $image = imagerotate($image,180,0);
1✔
153
                                        imageflip($image , IMG_FLIP_HORIZONTAL);
1✔
154
                                        break;
1✔
155
                                    case 5:
16✔
156
                                        $image = imagerotate($image,90,0);
1✔
157
                                        imageflip ($image , IMG_FLIP_VERTICAL);
1✔
158
                                        break;
1✔
159
                                    case 6:
15✔
160
                                        $image = imagerotate($image,-90,0);
1✔
161
                                        break;
1✔
162
                                    case 7:
14✔
163
                                        $image = imagerotate($image,-90,0);
1✔
164
                                        imageflip ($image , IMG_FLIP_VERTICAL);
1✔
165
                                        break;
1✔
166
                                    case 8:
13✔
167
                                        $image = imagerotate($image,90,0);
1✔
168
                                        break;
1✔
169
                                }
170

171
                                ob_start();
19✔
172
                                imagejpeg($image, NULL, 100);
19✔
173
                                $data = ob_get_contents();
19✔
174
                                ob_end_clean();
19✔
175
                            }
176

177
                            if ($data) {
23✔
178
                                $a = new Attachment($dbhr, $dbhm, NULL, $imgtype);
23✔
179
                                list ($id, $uid) = $a->create($msgid,$data);
23✔
180

181
                                # Make sure it's not too large, to keep DB size down.  Ought to have been resized by
182
                                # client, but you never know.
183
                                $data = $a->getData();
23✔
184
                                $i = new Image($data);
23✔
185

186
                                if ($i && $i->img) {
23✔
187
                                    $h = $i->height();
23✔
188
                                    $w = $i->width();
23✔
189

190
                                    if ($w > $sizelimit) {
23✔
191
                                        $h = $h * $sizelimit / $w;
11✔
192
                                        $w = $sizelimit;
11✔
193
                                        $i->scale($w, $h);
11✔
194
                                        $data = $i->getData(100);
11✔
195
                                        $a->setPrivate('data', $data);
11✔
196
                                    }
197
                                }
198

199
                                $ret = [
23✔
200
                                    'ret' => 0,
23✔
201
                                    'status' => 'Success',
23✔
202
                                    'id' => $id,
23✔
203
                                    'path' => $a->getPath(FALSE),
23✔
204
                                    'paththumb' => $a->getPath(TRUE)
23✔
205
                                ];
23✔
206

207
                                # Return a new thumbnail (which might be a different orientation).
208
                                $ret['initialPreview'] =  [
23✔
209
                                    '<img src="' . $a->getPath(TRUE) . '" class="file-preview-image img-responsive">',
23✔
210
                                ];
23✔
211

212
                                $ret['initialPreviewConfig'] = [
23✔
213
                                    [
23✔
214
                                        'key' => $id
23✔
215
                                    ]
23✔
216
                                ];
23✔
217

218
                                $ret['append'] = TRUE;
23✔
219

220
                                if ($ocr) {
23✔
UNCOV
221
                                    $a = new Attachment($dbhr, $dbhm, $id, $type);
×
222
                                    $ret['ocr'] = $a->ocr();
24✔
223
                                }
224
                            }
225
                        }
UNCOV
226
                    } catch (\Exception $e) {
×
UNCOV
227
                        $ret = [ 'ret' => 5, 'status' => "Image create failed " . $e->getMessage() ];
×
228
                    }
229
                }
230

231
                # Uploader code requires this field.
232
                $ret['error'] = $ret['ret'] == 0 ? NULL : $ret['status'];
24✔
233
            }
234

235
            break;
25✔
236
        }
237

238
        case 'DELETE': {
19✔
239
            # This is used by the client bootstrap-fileinput to delete images.  But we don't actually delete them,
240
            # in case we need them for debug.  The client will later patch the message to remove them.
241
            $ret = [
19✔
242
                'ret' => 0,
19✔
243
                'status' => 'Success'
19✔
244
            ];
19✔
245

246
            break;
19✔
247
        }
248
    }
249

250
    return($ret);
26✔
251
}
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