1: <?php
2:
3: namespace Alpha\Controller;
4:
5: use Alpha\Exception\ResourceNotFoundException;
6: use Alpha\Exception\IllegalArguementException;
7: use Alpha\View\Widget\Image;
8: use Alpha\Util\Config\ConfigProvider;
9: use Alpha\Util\Logging\Logger;
10: use Alpha\Util\Http\Request;
11: use Alpha\Util\Http\Response;
12: use Alpha\Model\Type\Boolean;
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: class ImageController extends Controller implements ControllerInterface
57: {
58: 59: 60: 61: 62: 63: 64:
65: private static $logger = null;
66:
67: 68: 69: 70: 71: 72: 73:
74: public function __construct($visibility = 'Public')
75: {
76: self::$logger = new Logger('ImageController');
77: self::$logger->debug('>>__construct()');
78:
79:
80: parent::__construct($visibility);
81:
82: self::$logger->debug('<<__construct');
83: }
84:
85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96:
97: public function doGet($request)
98: {
99: self::$logger->debug('>>doGet(request=['.var_export($request, true).'])');
100:
101: $config = ConfigProvider::getInstance();
102:
103: $params = $request->getParams();
104:
105: try {
106: $imgSource = urldecode($params['source']);
107: $imgWidth = $params['width'];
108: $imgHeight = $params['height'];
109: $imgType = $params['type'];
110: $imgQuality = (double) $params['quality'];
111: $imgScale = new Boolean($params['scale']);
112: $imgSecure = new Boolean($params['secure']);
113: } catch (\Exception $e) {
114: self::$logger->error('Required param missing for ImageController controller['.$e->getMessage().']');
115: throw new ResourceNotFoundException('File not found');
116: }
117:
118: $modified = filemtime($imgSource);
119:
120: $responseHeaders = array();
121:
122: $responseHeaders['Last-Modified'] = date('D, d M Y H:i:s', $modified).' GMT';
123: $responseHeaders['Cache-Control'] = 'max-age=1800';
124:
125:
126: if ($request->getHeader('If-Modified-Since') != null) {
127: if (strtotime($request->getHeader('If-Modified-Since')) == $modified) {
128: return new Response(304, '', $responseHeaders);
129: }
130: }
131:
132:
133: if ($imgSecure->getBooleanValue() && $config->get('cms.images.widget.secure')) {
134: $valid = $this->checkSecurityFields();
135:
136:
137: if (!$valid) {
138: $im = imagecreatetruecolor($imgWidth, $imgHeight);
139: $bgc = imagecolorallocate($im, 0, 0, 0);
140: imagefilledrectangle($im, 0, 0, $imgWidth, $imgHeight, $bgc);
141:
142: if ($imgSource == 'png' && $config->get('cms.images.perserve.png')) {
143: ob_start();
144: imagepng($im);
145: $body = ob_get_contents();
146: $contentType = 'image/png';
147: ob_end_clean();
148: } else {
149: ob_start();
150: imagejpeg($im);
151: $body = ob_get_contents();
152: $contentType = 'image/jpeg';
153: ob_end_clean();
154: }
155:
156: imagedestroy($im);
157:
158: self::$logger->warn('The client ['.$request->getUserAgent().'] was blocked from accessing the file ['.$imgSource.'] due to bad security tokens being provided');
159:
160: $responseHeaders['Content-Type'] = $contentType;
161:
162: return new Response(200, $body, $responseHeaders);
163: }
164: }
165:
166: try {
167: $image = new Image($imgSource, $imgWidth, $imgHeight, $imgType, $imgQuality, $imgScale->getBooleanValue(), $imgSecure->getBooleanValue());
168: ob_start();
169: $image->renderImage();
170: $body = ob_get_contents();
171: ob_end_clean();
172: } catch (IllegalArguementException $e) {
173: self::$logger->error($e->getMessage());
174: throw new ResourceNotFoundException('File not found');
175: }
176:
177: self::$logger->debug('<<__doGet');
178:
179: if ($imgSource == 'png' && $config->get('cms.images.perserve.png')) {
180: $responseHeaders['Content-Type'] = 'image/png';
181: } else {
182: $responseHeaders['Content-Type'] = 'image/jpeg';
183: }
184:
185: return new Response(200, $body, $responseHeaders);
186: }
187: }
188: