forked from nbdd0121/MW-Avatar
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathavatar.php
122 lines (100 loc) · 3.04 KB
/
avatar.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
// For some configurations, extensions are symbolic linked
// This is the workaround for ../..
$dir = dirname(dirname(dirname($_SERVER['SCRIPT_FILENAME'])));
// This switches working directory to the root directory of MediaWiki.
// This is essential for the page to work
chdir($dir);
// Start up MediaWiki
require_once 'includes/PHPVersionCheck.php';
wfEntryPointCheck('avatar.php');
require 'includes/WebStart.php';
// URL safety checks
if (!$wgRequest->checkUrlExtension()) {
return;
}
$query = $wgRequest->getQueryValues();
$path = null;
if (isset($query['user'])) {
$username = $query['user'];
if (isset($query['res'])) {
$res = \Avatar\Avatars::normalizeResolution($query['res']);
} else {
global $wgDefaultAvatarRes;
$res = $wgDefaultAvatarRes;
}
$user = User::newFromName($username);
if ($user) {
$path = \Avatar\Avatars::getAvatar($user, $res);
}
}
$response = $wgRequest->response();
// In order to maximize cache hit and due to
// fact that default avatar might be external,
// always redirect
if ($path === null) {
// We use send custom header, in order to control cache
$response->statusHeader('302');
if (!isset($query['nocache'])) {
// Cache longer time if it is not the default avatar
// As it is unlikely to be deleted
$response->header('Cache-Control: public, max-age=3600');
}
global $wgDefaultAvatar;
$response->header('Location: ' . $wgDefaultAvatar);
$mediawiki = new MediaWiki();
$mediawiki->doPostOutputShutdown('fast');
exit;
}
switch($wgAvatarServingMethod) {
case 'readfile':
global $wgAvatarUploadDirectory;
$response->header('Cache-Control: public, max-age=86400');
$response->header('Content-Type: image/png');
readfile($wgAvatarUploadDirectory . $path);
break;
case 'accel':
global $wgAvatarUploadPath;
$response->header('Cache-Control: public, max-age=86400');
$response->header('Content-Type: image/png');
$response->header('X-Accel-Redirect: ' . $wgAvatarUploadPath . $path);
break;
case 'sendfile':
global $wgAvatarUploadDirectory;
$response->header('Cache-Control: public, max-age=86400');
$response->header('Content-Type: image/png');
$response->header('X-SendFile: ' . $wgAvatarUploadDirectory . $path);
break;
case 'redirection':
default:
$ver = '';
// ver will be propagated to the relocated image
if (isset($query['ver'])) {
$ver = $query['ver'];
} else {
global $wgVersionAvatar;
if ($wgVersionAvatar) {
global $wgAvatarUploadDirectory;
$ver = filemtime($wgAvatarUploadDirectory . $path);
}
}
if ($ver) {
if (strpos($path, '?') !== false) {
$path .= '&ver=' . $ver;
} else {
$path .= '?ver=' . $ver;
}
}
// We use send custom header, in order to control cache
$response->statusHeader('302');
if (!isset($query['nocache'])) {
// Cache longer time if it is not the default avatar
// As it is unlikely to be deleted
$response->header('Cache-Control: public, max-age=86400');
}
global $wgAvatarUploadPath;
$response->header('Location: ' . $wgAvatarUploadPath . $path);
break;
}
$mediawiki = new MediaWiki();
$mediawiki->doPostOutputShutdown('fast');