$item) { if (substr($key, -$len) == $envName) { return $item; } } return false; } protected static function getWebPExpressContentDirWithDocRoot() { // Get relative path to wp-content // -------------------------------- self::$checking = 'Relative path to wp-content dir'; // Passed in env variable? $wpContentDirRel = self::getEnvPassedInRewriteRule('WPCONTENT'); if ($wpContentDirRel === false) { // Passed in QS? if (isset($_GET['wp-content'])) { $wpContentDirRel = SanityCheck::pathWithoutDirectoryTraversal($_GET['wp-content']); } else { // In case above fails, fall back to standard location $wpContentDirRel = 'wp-content'; } } // Check WebP Express content dir // --------------------------------- self::$checking = 'WebP Express content dir'; $webExpressContentDirAbs = SanityCheck::absPathExistsAndIsDir(self::$docRoot . '/' . $wpContentDirRel . '/webp-express'); return $webExpressContentDirAbs; } protected static function getWebPExpressContentDirNoDocRoot() { // Check wp-content // ---------------------- self::$checking = 'relative path between webp-express plugin dir and wp-content dir'; // From v0.22.0, we pass relative to webp-express dir rather than to the general plugin dir. // - this allows symlinking the webp-express dir. $wpContentDirRelToWEPluginDir = self::getEnvPassedInRewriteRule('WE_WP_CONTENT_REL_TO_WE_PLUGIN_DIR'); if (!$wpContentDirRelToWEPluginDir) { // Passed in QS? if (isset($_GET['xwp-content-rel-to-we-plugin-dir'])) { $xwpContentDirRelToWEPluginDir = SanityCheck::noControlChars($_GET['xwp-content-rel-to-we-plugin-dir']); $wpContentDirRelToWEPluginDir = SanityCheck::pathDirectoryTraversalAllowed(substr($xwpContentDirRelToWEPluginDir, 1)); } } // Old .htaccess rules from before 0.22.0 passed relative path to general plugin dir. // these rules must still be supported, which is what we do here: if (!$wpContentDirRelToWEPluginDir) { self::$checking = 'relative path between plugin dir and wp-content dir'; $wpContentDirRelToPluginDir = self::getEnvPassedInRewriteRule('WE_WP_CONTENT_REL_TO_PLUGIN_DIR'); if ($wpContentDirRelToPluginDir === false) { // Passed in QS? if (isset($_GET['xwp-content-rel-to-plugin-dir'])) { $xwpContentDirRelToPluginDir = SanityCheck::noControlChars($_GET['xwp-content-rel-to-plugin-dir']); $wpContentDirRelToPluginDir = SanityCheck::pathDirectoryTraversalAllowed(substr($xwpContentDirRelToPluginDir, 1)); } else { throw new \Exception('Path to wp-content was not received in any way'); } } $wpContentDirRelToWEPluginDir = $wpContentDirRelToPluginDir . '..'; } // Check WebP Express content dir // --------------------------------- self::$checking = 'WebP Express content dir'; $pathToWEPluginDir = dirname(dirname(__DIR__)); $webExpressContentDirAbs = SanityCheck::pathDirectoryTraversalAllowed($pathToWEPluginDir . '/' . $wpContentDirRelToWEPluginDir . '/webp-express'); //$pathToPluginDir = dirname(dirname(dirname(__DIR__))); //$webExpressContentDirAbs = SanityCheck::pathDirectoryTraversalAllowed($pathToPluginDir . '/' . $wpContentDirRelToPluginDir . '/webp-express'); //echo $webExpressContentDirAbs; exit; if (@!file_exists($webExpressContentDirAbs)) { throw new \Exception('Dir not found'); } $webExpressContentDirAbs = @realpath($webExpressContentDirAbs); if ($webExpressContentDirAbs === false) { throw new \Exception('WebP Express content dir is outside restricted open_basedir!'); } return $webExpressContentDirAbs; } protected static function getImageRootsDef() { if (!isset(self::$wodOptions['image-roots'])) { throw new \Exception('No image roots defined in config.'); } return new ImageRoots(self::$wodOptions['image-roots']); } protected static function loadConfig() { $usingDocRoot = !( isset($_GET['xwp-content-rel-to-we-plugin-dir']) || self::getEnvPassedInRewriteRule('WE_WP_CONTENT_REL_TO_WE_PLUGIN_DIR') || isset($_GET['xwp-content-rel-to-plugin-dir']) || self::getEnvPassedInRewriteRule('WE_WP_CONTENT_REL_TO_PLUGIN_DIR') ); self::$usingDocRoot = $usingDocRoot; if ($usingDocRoot) { // Check DOCUMENT_ROOT // ---------------------- self::$checking = 'DOCUMENT_ROOT'; $docRootAvailable = PathHelper::isDocRootAvailableAndResolvable(); if (!$docRootAvailable) { throw new \Exception( 'Document root is no longer available. It was available when the .htaccess rules was created and ' . 'the rules are based on that. You need to regenerate the rules (or fix your document root configuration)' ); } $docRoot = SanityCheck::absPath($_SERVER["DOCUMENT_ROOT"]); $docRoot = rtrim($docRoot, '/'); self::$docRoot = $docRoot; } if ($usingDocRoot) { self::$webExpressContentDirAbs = self::getWebPExpressContentDirWithDocRoot(); } else { self::$webExpressContentDirAbs = self::getWebPExpressContentDirNoDocRoot(); } // Check config file name // --------------------------------- self::$checking = 'config file'; $configFilename = self::$webExpressContentDirAbs . '/config/wod-options.json'; if (!file_exists($configFilename)) { throw new \Exception('Configuration file was not found (wod-options.json)'); } // Check config file // -------------------- $configLoadResult = file_get_contents($configFilename); if ($configLoadResult === false) { throw new \Exception('Cannot open config file'); } $json = SanityCheck::isJSONObject($configLoadResult); self::$options = json_decode($json, true); self::$wodOptions = self::$options['wod']; } /** * Must be called after conversion. */ protected static function fixConfigIfEwwwDiscoveredNonFunctionalApiKeys() { if (isset(Ewww::$nonFunctionalApiKeysDiscoveredDuringConversion)) { // We got an invalid or exceeded api key (at least one). //error_log('look:' . print_r(Ewww::$nonFunctionalApiKeysDiscoveredDuringConversion, true)); EwwwTools::markApiKeysAsNonFunctional( Ewww::$nonFunctionalApiKeysDiscoveredDuringConversion, self::$webExpressContentDirAbs . '/config' ); } } }