Skip to content

Commit

Permalink
Add option for CORS headers
Browse files Browse the repository at this point in the history
Rebased version of c4b4d04

Bump version

Rebased version of eafb979

Listen to OPTIONS requesut

Rebased version of 0168442
  • Loading branch information
Tommaso Lanza authored and nitriques committed Mar 21, 2016
1 parent b5c56fa commit fa71743
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
10 changes: 10 additions & 0 deletions extension.driver.php
Expand Up @@ -434,6 +434,12 @@ public function appendPreferences($context){
$label->setValue($input->generate() . ' ' . __('Prevent ISP proxy transformation'));
$group->appendChild($label);

// text input to allow external request origins
$label = Widget::Label(__('Add Cross-Origin Header'));
$input = Widget::Input('settings[image][allow_origin]', Symphony::Configuration()->get('allow_origin', 'image'));
$label->appendChild($input);
$group->appendChild($label);

// textarea for trusted sites
$label = Widget::Label(__('Trusted Sites'));
$label->appendChild(Widget::Textarea('jit_image_manipulation[trusted_external_sites]', 5, 50, $this->trusted()));
Expand All @@ -459,6 +465,10 @@ public function __SavePreferences($context){
$context['settings']['image']['disable_proxy_transform'] = 'no';
}

if (!isset($context['settings']['image']['allow_origin'])) {
$context['settings']['image']['allow_origin'] = '"null"';
}

// save trusted sites
$trusted_saved = $this->saveTrusted(stripslashes($_POST['jit_image_manipulation']['trusted_external_sites']));
// there were errors saving the trusted files
Expand Down
4 changes: 4 additions & 0 deletions extension.meta.xml
Expand Up @@ -16,6 +16,10 @@
</author>
</authors>
<releases>
<release version="1.45" date="2016-03-21" min="2.6.0" max="2.x.x">
- Add CORS header support (@tmslnz)
- Added 'max-age' hidden configuration
</release>
<release version="1.44" date="2016-01-12" min="2.6.0" max="2.6.x">
- Fix when no bytes are written but everything worked (#116)
</release>
Expand Down
19 changes: 19 additions & 0 deletions lib/image.php
Expand Up @@ -267,6 +267,25 @@ function __errorHandler($errno=NULL, $errstr, $errfile=NULL, $errline=NULL, $err
$etag = NULL;
}

// Allow CORS
// respond to preflights
if ($settings['image']['allow_origin'] !== null) {
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
// return only the headers and not the content
// only allow CORS if we're doing a GET - i.e. no sending for now.
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']) && $_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD'] == 'GET') {
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: X-Requested-With');
}
exit;
} else {
header('Origin: ' . $settings['image']['allow_origin']);
header('Access-Control-Allow-Origin: ' . $settings['image']['allow_origin']);
header('Access-Control-Allow-Methods: GET');
header('Access-Control-Max-Age: 3000');
}
}

// Check to see if the requested image needs to be generated or if a 304
// can just be returned to the browser to use it's cached version.
if(CACHING === true && (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) || isset($_SERVER['HTTP_IF_NONE_MATCH']))){
Expand Down

0 comments on commit fa71743

Please sign in to comment.