Completed
Push — master ( 7e38e2...812d3d )
by Michael
02:25
created

getthumb.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/*
3
 * getthumb
4
 *
5
 * @copyright   Geekwright, LLC http://geekwright.com
6
 * @license GNU General Public License (GPL)
7
 * @since   1.0
8
 * @author  Richard Griffith [email protected]
9
 * @package gwiki
10
 *
11
 * Manage thumbnail cache. Expects gwiki_page_images keyword as page and
12
 * image_name as name, also optional maximal pixel dimension as size.
13
 *
14
 * Thumbnails are generated for requested size on use, and then served
15
 * from cache until source image is changed.
16
 *
17
 * Images which are smaller than requested size, or of an unsupported
18
 * format (currently only jpeg, png and gif are supported,) are served
19
 * as original source.
20
 *
21
 */
22
23
include __DIR__ . '/../../mainfile.php';
24
$xoopsLogger->activated = false;
25
// provide error logging for our sanity in debugging (won't see xoops logger)
26
restore_error_handler();
27
error_reporting(-1);
28
29
//$GLOBALS['xoopsOption']['template_main'] = 'gwiki_view.tpl';
30
//include XOOPS_ROOT_PATH."/header.php";
31
32
$dir = basename(__DIR__);
33
include_once XOOPS_ROOT_PATH . '/modules/' . $dir . '/class/gwikiPage.php';
34
$wikiPage = new GwikiPage;
35
36
$default_thumb_size = $wikiPage->defaultThumbSize;
37
38
global $xoopsDB;
39
40
/**
41
 * @param $msg
42
 */
43
function errorExit($msg)
44
{
45
    header('Status: 500 Internal Error - ' . $msg);
46
    echo $msg;
47
    exit;
48
}
49
50
/**
51
 * @param $string
52
 *
53
 * @return string
54
 */
55 View Code Duplication
function cleaner($string)
0 ignored issues
show
The function cleaner() has been defined more than once; this definition is ignored, only the first definition in admin/attachments.php (L22-33) is considered.

This check looks for functions that have already been defined in other files.

Some Codebases, like WordPress, make a practice of defining functions multiple times. This may lead to problems with the detection of function parameters and types. If you really need to do this, you can mark the duplicate definition with the @ignore annotation.

/**
 * @ignore
 */
function getUser() {

}

function getUser($id, $realm) {

}

See also the PhpDoc documentation for @ignore.

Loading history...
56
{
57
    $string = stripcslashes($string);
58
    $string = html_entity_decode($string);
59
    $string = strip_tags($string); // DANGER -- kills wiki text
60
    $string = trim($string);
61
    $string = stripslashes($string);
62
63
    return $string;
64
}
65
66
/**
67
 * @param      $name
68
 * @param      $mime
69
 * @param      $modtime
70
 * @param bool $nocache
71
 */
72
function serveFile($name, $mime, $modtime, $nocache = false)
73
{
74
    if (!$nocache && (getenv('HTTP_IF_MODIFIED_SINCE') === gmdate('D, d M Y H:i:s', $modtime) . ' GMT')) {
75
        header('HTTP/1.0 304 Not Modified');
76
        exit;
77
    }
78
79
    $fp = fopen($name, 'rb');
80
81
    header('Content-Type: ' . $mime);
82
    header('Content-Disposition: inline; filename=' . urlencode(basename($name)));
83
    header('Content-Length: ' . filesize($name));
84
85
    $seconds_to_cache = 3600;
86
    $ts               = gmdate('D, d M Y H:i:s', time() + $seconds_to_cache) . ' GMT';
87
    header("Expires: $ts");
88
    header('Pragma: cache');
89
    header("Cache-Control: max-age=$seconds_to_cache");
90
    header('last-modified: ' . gmdate('D, d M Y H:i:s', $modtime) . ' GMT');
91
92
    fpassthru($fp);
93
    fclose($fp);
94
    exit;
95
}
96
97
unset($page, $name, $size);
98
if (isset($_GET['page'])) {
99
    $page = cleaner($_GET['page']);
100
}
101
if (isset($_GET['name'])) {
102
    $name = cleaner($_GET['name']);
103
}
104
if (isset($_GET['size'])) {
105
    $size = (int)$_GET['size'];
106
}
107
if (empty($page) || empty($name)) {
108
    errorExit('parameter missing');
109
}
110
if (empty($size) || $size === 0) {
111
    $size = $default_thumb_size;
112
}
113
114
$strategy           = 0;
115
$strategy_no_thumb  = 1; // no thumb possible or needed - pass original image
116
$strategy_old_thumb = 2; // send existing thumbnail image
117
$strategy_new_thumb = 3; // generate and pass new thumbnail
118
119
$image = $wikiPage->getPageImage($page, $name);
120
if (!$image) {
121
    errorExit('invalid parameters');
122
}
123
124
$file = $image['image_file'];
125
$i    = strrpos($file, '/');
126
if ($i === false) {
127
    errorExit('malformed path');
128
}
129
$file_pre  = substr($file, 0, $i);
130
$file_post = substr($file, $i);
131
132
$filename  = XOOPS_ROOT_PATH . '/uploads/' . $dir . '/' . $file;
133
$thumbpath = XOOPS_ROOT_PATH . '/uploads/' . $dir . '/' . $file_pre . '/' . $size;
134
$thumbname = $thumbpath . $file_post;
135
//echo $filename.'<br>'.$thumbpath.'<br>'.$thumbname;
136
137
$modtime = filemtime($filename);
138
139
if (file_exists($thumbname) && (filemtime($thumbname) > $modtime)) {
140
    $strategy   = $strategy_old_thumb;
141
    $info       = getimagesize($thumbname);
142
    $img_width  = $info[0];
143
    $img_height = $info[1];
144
    $img_mime   = $info['mime'];
145
} else { // (!file_exists($thumbname) || (file_exists($thumbname) && (filemtime($filename) > filemtime($thumbname))))
146
    $info       = getimagesize($filename);
147
    $img_width  = $info[0];
148
    $img_height = $info[1];
149
    $img_mime   = $info['mime'];
150
151
    if (($size >= $img_width) && ($size >= $img_height)) {
152
        $thumb_width  = $img_width;
153
        $thumb_height = $img_height;
154
        $strategy     = $strategy_no_thumb;
155
    } else {
156
        $ratio        = max($img_width, $img_height) / $size;
157
        $thumb_width  = ceil($img_width / $ratio);
158
        $thumb_height = ceil($img_height / $ratio);
159
        $strategy     = $strategy_new_thumb;
160
    }
161
162
    switch ($info[2]) {
163
        case IMAGETYPE_JPEG:
164
            $img_type = 'jpg';
165
            break;
166
        case IMAGETYPE_PNG:
167
            $img_type = 'png';
168
            break;
169
        case IMAGETYPE_GIF:
170
            $img_type = 'gif';
171
            break;
172
        default:
173
            $img_type = 'unsupported';
174
            $strategy = $strategy_no_thumb;
175
            break;
176
    }
177
    /*
178
        echo '<br>Image Width: '.$img_width;
179
        echo '<br>Image Height: '.$img_height;
180
        echo '<br>Type: '.$info[2].' '.$img_type.' '.$img_mime;
181
182
        echo '<br>Thumb Width: '.$thumb_width;
183
        echo '<br>Thumb Height: '.$thumb_height;
184
    */
185
}
186
187
switch ($strategy) {
188
    case $strategy_new_thumb:
189
        $oldUmask = umask(0);
190
        @mkdir($thumbpath, 0755, true);
191
        umask($oldUmask);
192
        $data = file_get_contents($filename);
193
        $im   = imagecreatefromstring($data);
194
        unset($data);
195
        $ti = imagecreatetruecolor($thumb_width, $thumb_height);
196
        imagealphablending($ti, false);
197
        imagesavealpha($ti, true);
198
        imagecopyresampled($ti, $im, 0, 0, 0, 0, $thumb_width, $thumb_height, $img_width, $img_height);
199
        imagedestroy($im);
200
        if ($img_type === 'jpg') {
201
            imagejpeg($ti, $thumbname, 80);
202
        }
203
        if ($img_type === 'png') {
204
            imagepng($ti, $thumbname);
205
        }
206
        if ($img_type === 'git') {
207
            imagegif($ti, $thumbname);
208
        }
209
        imagedestroy($ti);
210
        serveFile($thumbname, $img_mime, $modtime, true);
211
        break;
212
    case $strategy_old_thumb:
213
        serveFile($thumbname, $img_mime, $modtime);
214
        break;
215
    default:
216
        serveFile($filename, $img_mime, $modtime);
217
        break;
218
}
219
220
errorExit('unknown condition');
221
//include XOOPS_ROOT_PATH."/footer.php";
222