byId($dirIdOfHtaccess)->getAbsPath() . '/' . $sourceRelHtaccess;
return $source;
}
private static function getSource() {
if (self::$usingDocRoot) {
$source = self::getSourceDocRoot();
} else {
$source = self::getSourceNoDocRoot();
}
return $source;
}
private static function processRequestNoTryCatch() {
self::loadConfig();
$options = self::$options;
$wodOptions = self::$wodOptions;
$serveOptions = $options['webp-convert'];
$convertOptions = &$serveOptions['convert'];
//echo '
' . print_r($wodOptions, true) . '
'; exit;
// Validate that WebPExpress was configured to redirect to this conversion script
// (but do not require that for Nginx)
// ------------------------------------------------------------------------------
self::$checking = 'settings';
if (stripos($_SERVER["SERVER_SOFTWARE"], 'nginx') === false) {
if (!isset($wodOptions['enable-redirection-to-converter']) || ($wodOptions['enable-redirection-to-converter'] === false)) {
throw new ValidateException('Redirection to conversion script is not enabled');
}
}
// Check source (the image to be converted)
// --------------------------------------------
self::$checking = 'source';
// Decode URL in case file contains encoded symbols (#413)
$source = urldecode(self::getSource());
//self::exitWithError($source);
$imageRoots = self::getImageRootsDef();
// Get upload dir
$uploadDirAbs = $imageRoots->byId('uploads')->getAbsPath();
// Check destination path
// --------------------------------------------
self::$checking = 'destination path';
$destination = ConvertHelperIndependent::getDestination(
$source,
$wodOptions['destination-folder'],
$wodOptions['destination-extension'],
self::$webExpressContentDirAbs,
$uploadDirAbs,
self::$usingDocRoot,
self::getImageRootsDef()
);
//$destination = SanityCheck::absPathIsInDocRoot($destination);
$destination = SanityCheck::pregMatch('#\.webp$#', $destination, 'Does not end with .webp');
//self::exitWithError($destination);
// Done with sanitizing, lets get to work!
// ---------------------------------------
self::$checking = 'done';
if (isset($wodOptions['success-response']) && ($wodOptions['success-response'] == 'original')) {
$serveOptions['serve-original'] = true;
$serveOptions['serve-image']['headers']['vary-accept'] = false;
} else {
$serveOptions['serve-image']['headers']['vary-accept'] = true;
}
//echo $source . '
' . $destination; exit;
/*
// No caching!
// - perhaps this will solve it for WP engine.
// but no... Perhaps a 302 redirect to self then? (if redirect to existing is activated).
// TODO: try!
//$serveOptions['serve-image']['headers']['vary-accept'] = false;
*/
/*
include_once __DIR__ . '/../../vendor/autoload.php';
$convertLogger = new \WebPConvert\Loggers\BufferLogger();
\WebPConvert\WebPConvert::convert($source, $destination, $serveOptions['convert'], $convertLogger);
header('Location: ?fresh' , 302);
*/
if (isset($_SERVER['WPENGINE_ACCOUNT'])) {
// Redirect to self rather than serve directly for WP Engine.
// This overcomes that Vary:Accept header set from PHP is lost on WP Engine.
// To prevent endless loop in case "redirect to existing webp" isn't set up correctly,
// only activate when destination is missing.
// (actually it does not prevent anything on wpengine as the first request is cached!
// -even though we try to prevent it:)
// Well well. Those users better set up "redirect to existing webp" as well!
$serveOptions['serve-image']['headers']['cache-control'] = true;
$serveOptions['serve-image']['headers']['expires'] = false;
$serveOptions['serve-image']['cache-control-header'] = 'no-store, no-cache, must-revalidate, max-age=0';
//header("Pragma: no-cache", true);
if (!@file_exists($destination)) {
$serveOptions['redirect-to-self-instead-of-serving'] = true;
}
}
$loggingEnabled = (isset($wodOptions['enable-logging']) ? $wodOptions['enable-logging'] : true);
$logDir = ($loggingEnabled ? self::$webExpressContentDirAbs . '/log' : null);
ConvertHelperIndependent::serveConverted(
$source,
$destination,
$serveOptions,
$logDir,
'Conversion triggered with the conversion script (wod/webp-on-demand.php)'
);
BiggerThanSourceDummyFiles::updateStatus(
$source,
$destination,
self::$webExpressContentDirAbs,
self::getImageRootsDef(),
$wodOptions['destination-folder'],
$wodOptions['destination-extension']
);
self::fixConfigIfEwwwDiscoveredNonFunctionalApiKeys();
}
public static function processRequest() {
try {
self::processRequestNoTryCatch();
} catch (SanityException $e) {
self::exitWithError('Sanity check failed for ' . self::$checking . ': '. $e->getMessage());
} catch (ValidateException $e) {
self::exitWithError('Validation failed for ' . self::$checking . ': '. $e->getMessage());
} catch (\Exception $e) {
if (self::$checking == 'done') {
self::exitWithError('Error occured during conversion/serving:' . $e->getMessage());
} else {
self::exitWithError('Error occured while calculating ' . self::$checking . ': '. $e->getMessage());
}
}
}
}