-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathUAController.php
executable file
·305 lines (251 loc) · 7.95 KB
/
UAController.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
<?php if (!defined('TL_ROOT')) die('You can not access this file directly!');
/**
* This program is free software: you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either
* version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program. If not, please visit the Free
* Software Foundation website at http://www.gnu.org/licenses/.
*
* PHP version 5
* @copyright Winans Creative 2010
* @author Blair Winans <[email protected]>
* @author Adam Fisher <[email protected]>
* @license http://opensource.org/licenses/lgpl-3.0.html
*/
abstract class UAController extends Controller
{
/**
* Boolean mobile
*/
protected $blnMobile = false;
/**
* User agent string
*/
protected $strAgent = 'regular';
/**
* Load UA or Browser object
*/
protected function __construct()
{
parent::__construct();
$this->import('Database');
$ua = $this->Environment->agent;
if($ua->mobile)
{
$this->blnMobile = true;
if(stripos($this->Environment->httpUserAgent,'iPad') !== false)
{
$this->strAgent = 'tablet';
}
else
{
$this->strAgent = 'mobile';
}
}
//Override - Look for an override to set/unset
if ($this->Input->get('m')=='true' && isset($_SESSION['MC-OVERRIDE']))
{
unset($_SESSION['MC-OVERRIDE']);
}
elseif($this->Input->get('m')=='false' || $_SESSION['MC-OVERRIDE'])
{
$_SESSION['MC-OVERRIDE'] = '1';
$this->strAgent = 'regular';
$this->blnMobile = false;
}
//HOOK to set another user agent string
if (isset($GLOBALS['TL_HOOKS']['setUAType']) && is_array($GLOBALS['TL_HOOKS']['setUAType']))
{
foreach ($GLOBALS['TL_HOOKS']['setUAType'] as $callback)
{
$this->import($callback[0]);
$this->strAgent = $this->$callback[0]->$callback[1]($this->strAgent, $this);
}
}
}
public function showElementByUA($objElement, $strBuffer)
{
$strOnly = $this->strAgent . 'hide';
if( TL_MODE=='FE' && $objElement->$strOnly)
{
$strBuffer = '';
}
return $strBuffer;
}
public function getUAImage($image, $width, $height, $mode, $strCacheName, $objFile)
{
$image = rawurldecode($image);
if (!$this->blnMobile || !is_file(TL_ROOT . '/' . $image))
return null;
// Return the path to the original image if the GDlib cannot handle it
if (!extension_loaded('gd') || !$objFile->isGdImage || $objFile->width > 3000 || $objFile->height > 3000 || (!$width && !$height) || $width > 1200 || $height > 1200)
{
return $image;
}
$intPositionX = 0;
$intPositionY = 0;
//Custom for mobile
//if width is more than MaxWidth we will need to resize proportionately
$intMaxWidth = $GLOBALS['UA_TYPES'][$this->strAgent]['maxImageWidth'];
$width = !strlen($width) ? $objFile->width : $width;
$blnMobileResize = (TL_MODE=='FE' && $objFile->width > $intMaxWidth && ($width > $intMaxWidth || !strlen($width))) ? true : false;
$mode = $blnMobileResize ? 'proportional' : $mode;
$intWidth = $blnMobileResize ? $intMaxWidth : $width; //setting the width to be no wider than maxwidth
$intHeight = $height;
// Mode-specific changes
if ($intWidth && $intHeight)
{
switch ($mode)
{
case 'proportional':
if ($objFile->width >= $objFile->height)
{
unset($height, $intHeight);
}
else
{
unset($width, $intWidth);
}
break;
case 'box':
if (ceil($objFile->height * $width / $objFile->width) <= $intHeight)
{
unset($height, $intHeight);
}
else
{
unset($width, $intWidth);
}
break;
}
}
// Resize width and height and crop the image if necessary
if ($intWidth && $intHeight)
{
if (($intWidth * $objFile->height) != ($intHeight * $objFile->width))
{
$intWidth = ceil($objFile->width * $intHeight / $objFile->height);
$intPositionX = -intval(($intWidth - $width) / 2);
if ($intWidth < $width)
{
$intWidth = $width;
$intHeight = ceil($objFile->height * $intWidth / $objFile->width);
$intPositionX = 0;
$intPositionY = -intval(($intHeight - $height) / 2);
}
}
$strNewImage = imagecreatetruecolor($width, $height);
}
// Calculate the height if only the width is given
elseif ($intWidth)
{
$intHeight = ceil($objFile->height * $intWidth / $objFile->width);
$strNewImage = imagecreatetruecolor($intWidth, $intHeight);
}
// Calculate the width if only the height is given
elseif ($intHeight)
{
$intWidth = ceil($objFile->width * $intHeight / $objFile->height);
$strNewImage = imagecreatetruecolor($intWidth, $intHeight);
}
$arrGdinfo = gd_info();
$strGdVersion = preg_replace('/[^0-9\.]+/', '', $arrGdinfo['GD Version']);
switch ($objFile->extension)
{
case 'gif':
if ($arrGdinfo['GIF Read Support'])
{
$strSourceImage = imagecreatefromgif(TL_ROOT . '/' . $image);
$intTranspIndex = imagecolortransparent($strSourceImage);
// Handle transparency
if ($intTranspIndex >= 0 && $intTranspIndex < imagecolorstotal($strSourceImage))
{
$arrColor = imagecolorsforindex($strSourceImage, $intTranspIndex);
$intTranspIndex = imagecolorallocate($strNewImage, $arrColor['red'], $arrColor['green'], $arrColor['blue']);
imagefill($strNewImage, 0, 0, $intTranspIndex);
imagecolortransparent($strNewImage, $intTranspIndex);
}
}
break;
case 'jpg':
case 'jpeg':
if ($arrGdinfo['JPG Support'] || $arrGdinfo['JPEG Support'])
{
$strSourceImage = imagecreatefromjpeg(TL_ROOT . '/' . $image);
}
break;
case 'png':
if ($arrGdinfo['PNG Support'])
{
$strSourceImage = imagecreatefrompng(TL_ROOT . '/' . $image);
// Handle transparency (GDlib >= 2.0 required)
if (version_compare($strGdVersion, '2.0', '>='))
{
imagealphablending($strNewImage, false);
$intTranspIndex = imagecolorallocatealpha($strNewImage, 0, 0, 0, 127);
imagefill($strNewImage, 0, 0, $intTranspIndex);
imagesavealpha($strNewImage, true);
}
}
break;
}
// The new image could not be created
if (!$strSourceImage)
{
$this->log('Image "' . $image . '" could not be processed', 'Controller getImage()', TL_ERROR);
return null;
}
imagecopyresampled($strNewImage, $strSourceImage, $intPositionX, $intPositionY, 0, 0, $intWidth, $intHeight, $objFile->width, $objFile->height);
// Fallback to PNG if GIF ist not supported
if ($objFile->extension == 'gif' && !$arrGdinfo['GIF Create Support'])
{
$objFile->extension = 'png';
}
// Create the new image
switch ($objFile->extension)
{
case 'gif':
imagegif($strNewImage, TL_ROOT . '/' . $strCacheName);
break;
case 'jpg':
case 'jpeg':
imagejpeg($strNewImage, TL_ROOT . '/' . $strCacheName, (!$GLOBALS['TL_CONFIG']['jpgQuality'] ? 80 : $GLOBALS['TL_CONFIG']['jpgQuality']));
break;
case 'png':
imagepng($strNewImage, TL_ROOT . '/' . $strCacheName);
break;
}
// Destroy the temporary images
imagedestroy($strSourceImage);
imagedestroy($strNewImage);
// Resize the original image
if ($target)
{
$this->import('Files');
$this->Files->rename($strCacheName, $target);
return $target;
}
// Set the file permissions when the Safe Mode Hack is used
if ($GLOBALS['TL_CONFIG']['useFTP'])
{
$this->import('Files');
$this->Files->chmod($strCacheName, 0644);
}
// Return the path to new image
return $strCacheName;
}
/**
* Abstract function - required by all subclasses
* This will come in handy down the line (*wink*)
*/
abstract public function parseUATemplate($strBuffer, $strTemplate);
}
?>