Completed
Push — master ( e22ad1...12e8ae )
by Michael
02:11
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
 * @version $Id$
11
 *
12
 * Manage thumbnail cache. Expects gwiki_page_images keyword as page and
13
 * image_name as name, also optional maximal pixel dimension as size.
14
 *
15
 * Thumbnails are generated for requested size on use, and then served
16
 * from cache until source image is changed.
17
 *
18
 * Images which are smaller than requested size, or of an unsupported
19
 * format (currently only jpeg, png and gif are supported,) are served
20
 * as original source.
21
 *
22
 */
23
24
include dirname(dirname(__DIR__)) . '/mainfile.php';
25
$xoopsLogger->activated = false;
26
// provide error logging for our sanity in debugging (won't see xoops logger)
27
restore_error_handler();
28
error_reporting(-1);
29
30
//$xoopsOption['template_main'] = 'gwiki_view.tpl';
31
//include XOOPS_ROOT_PATH."/header.php";
32
33
$dir = basename(__DIR__);
34
include_once XOOPS_ROOT_PATH . '/modules/' . $dir . '/class/gwikiPage.php';
35
$wikiPage = new gwikiPage;
36
37
$default_thumb_size = $wikiPage->defaultThumbSize;
38
39
global $xoopsDB;
40
41
/**
42
 * @param $msg
43
 */
44
function errorExit($msg)
45
{
46
    header("Status: 500 Internal Error - " . $msg);
47
    echo $msg;
48
    exit;
49
}
50
51
/**
52
 * @param $string
53
 *
54
 * @return string
55
 */
56 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 (L27-38) 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...
57
{
58
    $string = stripcslashes($string);
59
    $string = html_entity_decode($string);
60
    $string = strip_tags($string); // DANGER -- kills wiki text
61
    $string = trim($string);
62
    $string = stripslashes($string);
63
64
    return $string;
65
}
66
67
/**
68
 * @param      $name
69
 * @param      $mime
70
 * @param      $modtime
71
 * @param bool $nocache
72
 */
73
function serveFile($name, $mime, $modtime, $nocache = false)
74
{
75
    if (!($nocache) && (getenv("HTTP_IF_MODIFIED_SINCE") === gmdate("D, d M Y H:i:s", $modtime) . " GMT")) {
76
        header("HTTP/1.0 304 Not Modified");
77
        exit;
78
    }
79
80
    $fp = fopen($name, 'rb');
81
82
    header('Content-Type: ' . $mime);
83
    header('Content-Disposition: inline; filename=' . urlencode(basename($name)));
84
    header('Content-Length: ' . filesize($name));
85
86
    $seconds_to_cache = 3600;
87
    $ts               = gmdate("D, d M Y H:i:s", time() + $seconds_to_cache) . " GMT";
88
    header("Expires: $ts");
89
    header("Pragma: cache");
90
    header("Cache-Control: max-age=$seconds_to_cache");
91
    header("last-modified: " . gmdate("D, d M Y H:i:s", $modtime) . " GMT");
92
93
    fpassthru($fp);
94
    fclose($fp);
95
    exit;
96
}
97
98
unset($page, $name, $size);
99
if (isset($_GET['page'])) {
100
    $page = cleaner($_GET['page']);
101
}
102
if (isset($_GET['name'])) {
103
    $name = cleaner($_GET['name']);
104
}
105
if (isset($_GET['size'])) {
106
    $size = (int)($_GET['size']);
107
}
108
if (empty($page) || empty($name)) {
109
    errorExit("parameter missing");
110
}
111
if (empty($size) || $size === 0) {
112
    $size = $default_thumb_size;
113
}
114
115
$strategy           = 0;
116
$strategy_no_thumb  = 1; // no thumb possible or needed - pass original image
117
$strategy_old_thumb = 2; // send existing thumbnail image
118
$strategy_new_thumb = 3; // generate and pass new thumbnail
119
120
$image = $wikiPage->getPageImage($page, $name);
121
if (!$image) {
122
    errorExit("invalid parameters");
123
}
124
125
$file = $image['image_file'];
126
$i    = strrpos($file, '/');
127
if ($i === false) {
128
    errorExit("malformed path");
129
}
130
$file_pre  = substr($file, 0, $i);
131
$file_post = substr($file, $i);
132
133
$filename  = XOOPS_ROOT_PATH . '/uploads/' . $dir . '/' . $file;
134
$thumbpath = XOOPS_ROOT_PATH . '/uploads/' . $dir . '/' . $file_pre . '/' . $size;
135
$thumbname = $thumbpath . $file_post;
136
//echo $filename.'<br />'.$thumbpath.'<br />'.$thumbname;
137
138
$modtime = filemtime($filename);
139
140
if (file_exists($thumbname) && (filemtime($thumbname) > $modtime)) {
141
    $strategy   = $strategy_old_thumb;
142
    $info       = getimagesize($thumbname);
143
    $img_width  = $info[0];
144
    $img_height = $info[1];
145
    $img_mime   = $info['mime'];
146
} else { // (!file_exists($thumbname) || (file_exists($thumbname) && (filemtime($filename) > filemtime($thumbname))))
147
    $info       = getimagesize($filename);
148
    $img_width  = $info[0];
149
    $img_height = $info[1];
150
    $img_mime   = $info['mime'];
151
152
    if (($size >= $img_width) && ($size >= $img_height)) {
153
        $thumb_width  = $img_width;
154
        $thumb_height = $img_height;
155
        $strategy     = $strategy_no_thumb;
156
    } else {
157
        $ratio        = max($img_width, $img_height) / $size;
158
        $thumb_width  = ceil($img_width / $ratio);
159
        $thumb_height = ceil($img_height / $ratio);
160
        $strategy     = $strategy_new_thumb;
161
    }
162
163
    switch ($info[2]) {
164
        case IMAGETYPE_JPEG:
165
            $img_type = 'jpg';
166
            break;
167
        case IMAGETYPE_PNG:
168
            $img_type = 'png';
169
            break;
170
        case IMAGETYPE_GIF:
171
            $img_type = 'gif';
172
            break;
173
        default:
174
            $img_type = 'unsupported';
175
            $strategy = $strategy_no_thumb;
176
            break;
177
    }
178
    /*
179
        echo '<br />Image Width: '.$img_width;
180
        echo '<br />Image Height: '.$img_height;
181
        echo '<br />Type: '.$info[2].' '.$img_type.' '.$img_mime;
182
183
        echo '<br />Thumb Width: '.$thumb_width;
184
        echo '<br />Thumb Height: '.$thumb_height;
185
    */
186
}
187
188
switch ($strategy) {
189
    case $strategy_new_thumb:
190
        $oldUmask = umask(0);
191
        @mkdir($thumbpath, 0755, true);
192
        umask($oldUmask);
193
        $data = file_get_contents($filename);
194
        $im   = imagecreatefromstring($data);
195
        unset($data);
196
        $ti = imagecreatetruecolor($thumb_width, $thumb_height);
197
        imagealphablending($ti, false);
198
        imagesavealpha($ti, true);
199
        imagecopyresampled($ti, $im, 0, 0, 0, 0, $thumb_width, $thumb_height, $img_width, $img_height);
200
        imagedestroy($im);
201
        if ($img_type === 'jpg') {
202
            imagejpeg($ti, $thumbname, 80);
203
        }
204
        if ($img_type === 'png') {
205
            imagepng($ti, $thumbname);
206
        }
207
        if ($img_type === 'git') {
208
            imagegif($ti, $thumbname);
209
        }
210
        imagedestroy($ti);
211
        serveFile($thumbname, $img_mime, $modtime, true);
212
        break;
213
    case $strategy_old_thumb:
214
        serveFile($thumbname, $img_mime, $modtime);
215
        break;
216
    default:
217
        serveFile($filename, $img_mime, $modtime);
218
        break;
219
}
220
221
errorExit('unknown condition');
222
//include XOOPS_ROOT_PATH."/footer.php";
223
224