') !== false); $dir = FileHelper::dirName($filename); $dirId = Paths::getAbsDirId($dir); if ($dirId !== false) { if ($containsRules) { self::addToActiveHTAccessDirsArray($dirId); } else { self::removeFromActiveHTAccessDirsArray($dirId); } } } return $success; } public static function saveHTAccessRules($rootId, $rules, $createIfMissing = true) { $filename = Paths::getAbsDirById($rootId) . '/.htaccess'; return self::saveHTAccessRulesToFile($filename, $rules, $createIfMissing); } /* only called in this file */ public static function saveHTAccessRulesToFirstWritableHTAccessDir($dirs, $rules) { foreach ($dirs as $dir) { if (self::saveHTAccessRulesToFile($dir . '/.htaccess', $rules, true)) { return $dir; } } return false; } /** * Try to deactivate all .htaccess rules. * If success, we return true. * If we fail, we return an array of filenames that have problems * @return true|array */ public static function deactivateHTAccessRules($comment = '# Plugin is deactivated') { $rootsToClean = Paths::getImageRootIds(); $rootsToClean[] = 'home'; $rootsToClean[] = 'cache'; $failures = []; $successes = []; foreach ($rootsToClean as $imageRootId) { $dir = Paths::getAbsDirById($imageRootId); $filename = $dir . '/.htaccess'; if (!FileHelper::fileExists($filename)) { //error_log('exists not:' . $filename); continue; } else { if (self::haveWeRulesInThisHTAccessBestGuess($filename)) { if (self::saveHTAccessRulesToFile($filename, $comment, false)) { $successes[] = $imageRootId; } else { $failures[] = $imageRootId; } } else { //error_log('no rules:' . $filename); } } } $success = (count($failures) == 0); return [$success, $failures, $successes]; } public static function testLinks($config) { /* if (isset($_SERVER['HTTP_ACCEPT']) && (strpos($_SERVER['HTTP_ACCEPT'], 'image/webp') !== false )) { if ($config['operation-mode'] != 'no-conversion') { if ($config['image-types'] != 0) { $webpExpressRoot = Paths::getWebPExpressPluginUrlPath(); $links = ''; if ($config['enable-redirection-to-converter']) { $links = '
'; $links .= 'Convert test image (show debug)
'; $links .= 'Convert test image
'; } // TODO: webp-realizer test links (to missing webp) if ($config['enable-redirection-to-webp-realizer']) { } // TODO: test link for testing redirection to existing if ($config['redirect-to-existing-in-htaccess']) { } return $links; } } }*/ return ''; } public static function getHTAccessDirRequirements() { $minRequired = 'index'; if (Paths::isWPContentDirMovedOutOfAbsPath()) { $minRequired = 'wp-content'; $pluginToo = Paths::isPluginDirMovedOutOfWpContent() ? 'yes' : 'no'; $uploadToo = Paths::isUploadDirMovedOutOfWPContentDir() ? 'yes' : 'no'; } else { // plugin requirement depends... // - if user grants access to 'index', the requirement is Paths::isPluginDirMovedOutOfAbsPath() // - if user grants access to 'wp-content', the requirement is Paths::isPluginDirMovedOutOfWpContent() $pluginToo = 'depends'; // plugin requirement depends... // - if user grants access to 'index', we should be fine, as UPLOADS is always in ABSPATH. // - if user grants access to 'wp-content', the requirement is Paths::isUploadDirMovedOutOfWPContentDir() $uploadToo = 'depends'; } // We need upload too for rewrite rules when destination structure is image-roots. // but it is also good otherwise. So lets always do it. $uploadToo = 'yes'; return [ $minRequired, $pluginToo, // 'yes', 'no' or 'depends' $uploadToo ]; } public static function saveRules($config, $showMessage = true) { list($success, $failedDeactivations, $successfulDeactivations) = self::deactivateHTAccessRules('# The rules have left the building'); $rootsToPutRewritesIn = $config['scope']; if ($config['destination-structure'] == 'doc-root') { // Commented out to quickfix #338 // $rootsToPutRewritesIn = Paths::filterOutSubRoots($rootsToPutRewritesIn); } $dirsContainingWebps = []; $mingled = ($config['destination-folder'] == 'mingled'); if ($mingled) { $dirsContainingWebps[] = 'uploads'; } $scopeOtherThanUpload = (str_replace('uploads', '', implode(',', $config['scope'])) != ''); if ($scopeOtherThanUpload || (!$mingled)) { $dirsContainingWebps[] = 'cache'; } $dirsToPutRewritesIn = array_unique(array_merge($rootsToPutRewritesIn, $dirsContainingWebps)); $failedWrites = []; $successfullWrites = []; foreach ($dirsToPutRewritesIn as $rootId) { $dirContainsSourceImages = in_array($rootId, $rootsToPutRewritesIn); $dirContainsWebPImages = in_array($rootId, $dirsContainingWebps); $rules = HTAccessRules::generateHTAccessRulesFromConfigObj( $config, $rootId, $dirContainsSourceImages, $dirContainsWebPImages ); $success = self::saveHTAccessRules( $rootId, $rules, true ); if ($success) { $successfullWrites[] = $rootId; // Remove it from $successfulDeactivations (if it is there) if (($key = array_search($rootId, $successfulDeactivations)) !== false) { unset($successfulDeactivations[$key]); } } else { $failedWrites[] = $rootId; // Remove it from $failedDeactivations (if it is there) if (($key = array_search($rootId, $failedDeactivations)) !== false) { unset($failedDeactivations[$key]); } } } $success = ((count($failedDeactivations) == 0) && (count($failedWrites) == 0)); $return = [$success, $successfullWrites, $successfulDeactivations, $failedWrites, $failedDeactivations]; if ($showMessage) { self::showSaveRulesMessages($return); } return $return; } public static function showSaveRulesMessages($saveRulesResult) { list($success, $successfullWrites, $successfulDeactivations, $failedWrites, $failedDeactivations) = $saveRulesResult; $msg = ''; if (count($successfullWrites) > 0) { $msg .= '

Rewrite rules were saved to the following files:

'; foreach ($successfullWrites as $rootId) { $rootIdName = $rootId; if ($rootIdName == 'cache') { $rootIdName = 'webp folder'; } $msg .= '' . Paths::getAbsDirById($rootId) . '/.htaccess (' . $rootIdName . ')
'; } } if (count($successfulDeactivations) > 0) { $msg .= '

Rewrite rules were removed from the following files:

'; foreach ($successfulDeactivations as $rootId) { $rootIdName = $rootId; if ($rootIdName == 'cache') { $rootIdName = 'webp folder'; } $msg .= '' . Paths::getAbsDirById($rootId) . '/.htaccess (' . $rootIdName . ')
'; } } if ($msg != '') { Messenger::addMessage( ($success ? 'success' : 'info'), $msg ); } if (count($failedWrites) > 0) { $msg = '

Failed writing rewrite rules to the following files:

'; foreach ($failedWrites as $rootId) { $msg .= '' . Paths::getAbsDirById($rootId) . '/.htaccess (' . $rootId . ')
'; } $msg .= 'You need to change the file permissions to allow WebP Express to save the rules.'; Messenger::addMessage('error', $msg); } else { if (count($failedDeactivations) > 0) { $msg = '

Failed deleting unused rewrite rules in the following files:

'; foreach ($failedDeactivations as $rootId) { $msg .= '' . Paths::getAbsDirById($rootId) . '/.htaccess (' . $rootId . ')
'; } $msg .= 'You need to change the file permissions to allow WebP Express to remove the rules or ' . 'remove them manually'; Messenger::addMessage('error', $msg); } } } }