@@ -1,17 +1,17 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | class SiteTreeWhitelistExtension extends DataExtension { |
| 3 | 3 | |
| 4 | - public function onAfterWrite(){ |
|
| 5 | - //top level page change should trigger re-generation of whitelist |
|
| 6 | - if ($this->owner->getParentType() === 'root') { |
|
| 7 | - WhitelistGenerator::generateWhitelist(); |
|
| 8 | - } |
|
| 9 | - } |
|
| 4 | + public function onAfterWrite(){ |
|
| 5 | + //top level page change should trigger re-generation of whitelist |
|
| 6 | + if ($this->owner->getParentType() === 'root') { |
|
| 7 | + WhitelistGenerator::generateWhitelist(); |
|
| 8 | + } |
|
| 9 | + } |
|
| 10 | 10 | |
| 11 | - public function onAfterDelete(){ |
|
| 12 | - if ($this->owner->getParentType() === 'root') { |
|
| 13 | - WhitelistGenerator::generateWhitelist(); |
|
| 14 | - } |
|
| 15 | - } |
|
| 11 | + public function onAfterDelete(){ |
|
| 12 | + if ($this->owner->getParentType() === 'root') { |
|
| 13 | + WhitelistGenerator::generateWhitelist(); |
|
| 14 | + } |
|
| 15 | + } |
|
| 16 | 16 | |
| 17 | 17 | } |
| 18 | 18 | \ No newline at end of file |
@@ -1,14 +1,14 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | class SiteTreeWhitelistExtension extends DataExtension { |
| 3 | 3 | |
| 4 | - public function onAfterWrite(){ |
|
| 4 | + public function onAfterWrite() { |
|
| 5 | 5 | //top level page change should trigger re-generation of whitelist |
| 6 | 6 | if ($this->owner->getParentType() === 'root') { |
| 7 | 7 | WhitelistGenerator::generateWhitelist(); |
| 8 | 8 | } |
| 9 | 9 | } |
| 10 | 10 | |
| 11 | - public function onAfterDelete(){ |
|
| 11 | + public function onAfterDelete() { |
|
| 12 | 12 | if ($this->owner->getParentType() === 'root') { |
| 13 | 13 | WhitelistGenerator::generateWhitelist(); |
| 14 | 14 | } |
@@ -1,179 +1,179 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | class WhitelistGenerator extends Object implements Flushable { |
| 3 | 3 | |
| 4 | - public static function generateWhitelist(){ |
|
| 5 | - $whitelist = self::generateWhitelistRules(); |
|
| 6 | - self::syncCacheFilesystem($whitelist); |
|
| 7 | - } |
|
| 8 | - |
|
| 9 | - public static function generateWhitelistRules(){ |
|
| 10 | - //get all URL rules |
|
| 11 | - $rules = Config::inst()->get('Director', 'rules'); |
|
| 12 | - |
|
| 13 | - $allTopLevelRules = array(); |
|
| 14 | - foreach ($rules as $pattern => $controllerOptions) { |
|
| 15 | - //match first portion of the URL, either delimited by slash or colon or end-of-line |
|
| 16 | - if (preg_match('/^(.*?)(\/|:|$)/', $pattern, $matches)){ |
|
| 17 | - if (!empty($matches[1])){ |
|
| 18 | - array_push($allTopLevelRules, $matches[1]); |
|
| 19 | - } |
|
| 20 | - } |
|
| 21 | - } |
|
| 22 | - |
|
| 23 | - $filteredRules = array('home'); //add 'home' url, as default |
|
| 4 | + public static function generateWhitelist(){ |
|
| 5 | + $whitelist = self::generateWhitelistRules(); |
|
| 6 | + self::syncCacheFilesystem($whitelist); |
|
| 7 | + } |
|
| 8 | + |
|
| 9 | + public static function generateWhitelistRules(){ |
|
| 10 | + //get all URL rules |
|
| 11 | + $rules = Config::inst()->get('Director', 'rules'); |
|
| 12 | + |
|
| 13 | + $allTopLevelRules = array(); |
|
| 14 | + foreach ($rules as $pattern => $controllerOptions) { |
|
| 15 | + //match first portion of the URL, either delimited by slash or colon or end-of-line |
|
| 16 | + if (preg_match('/^(.*?)(\/|:|$)/', $pattern, $matches)){ |
|
| 17 | + if (!empty($matches[1])){ |
|
| 18 | + array_push($allTopLevelRules, $matches[1]); |
|
| 19 | + } |
|
| 20 | + } |
|
| 21 | + } |
|
| 22 | + |
|
| 23 | + $filteredRules = array('home'); //add 'home' url, as default |
|
| 24 | 24 | |
| 25 | - $addToWhitelist = Config::inst()->get('WhitelistGenerator', 'addToWhitelist'); |
|
| 26 | - if ($addToWhitelist && is_array($addToWhitelist)) { |
|
| 27 | - $filteredRules = array_merge($filteredRules, $addToWhitelist); |
|
| 28 | - } |
|
| 25 | + $addToWhitelist = Config::inst()->get('WhitelistGenerator', 'addToWhitelist'); |
|
| 26 | + if ($addToWhitelist && is_array($addToWhitelist)) { |
|
| 27 | + $filteredRules = array_merge($filteredRules, $addToWhitelist); |
|
| 28 | + } |
|
| 29 | 29 | |
| 30 | - foreach($allTopLevelRules as $rule) { |
|
| 31 | - if (strpos($rule, '$') !== false) { |
|
| 32 | - if ($rule === '$Controller') { |
|
| 33 | - //special case for Controllers, add all possible controllers |
|
| 34 | - $subControllers = ClassInfo::subclassesFor(new Controller()); |
|
| 35 | - |
|
| 36 | - foreach ($subControllers as $controller){ |
|
| 37 | - array_push($filteredRules, $controller); //add the controller name as a link |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - } elseif ($rule === '$URLSegment') { |
|
| 41 | - $topLevelPagesArray = array(); //temporary array to store top-level pages |
|
| 42 | - |
|
| 43 | - //special case for SiteTree, add all possible top Level Pages |
|
| 44 | - $topLevelPages = SiteTree::get()->filter('ParentID', 0); |
|
| 45 | - |
|
| 46 | - foreach ($topLevelPages as $page) { |
|
| 47 | - $link = $page->RelativeLink(); |
|
| 48 | - array_push($topLevelPagesArray, trim($link, '\/ ')); //remove trailing or leading slashes from links |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - //fetch old top-level pages URLs from the SiteTree_versions table |
|
| 52 | - if (Config::inst()->get('WhitelistGenerator', 'includeSiteTreeVersions')) { |
|
| 53 | - $oldTopLevelPagesArray = self::find_old_top_level_pages($topLevelPagesArray); |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - $filteredRules = array_merge($filteredRules, $topLevelPagesArray, $oldTopLevelPagesArray); |
|
| 57 | - } else { |
|
| 58 | - user_error('Unknown wildcard URL match found: '.$rule, E_WARNING); |
|
| 59 | - } |
|
| 60 | - } else { |
|
| 61 | - //add the rule to a new list of rules |
|
| 62 | - array_push($filteredRules, $rule); |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - //filter duplicates (order doesn't matter here, as we are only interested in the first level of the rules) |
|
| 68 | - $filteredRules = array_unique($filteredRules); |
|
| 69 | - |
|
| 70 | - $removeFromWhitelist = Config::inst()->get('WhitelistGenerator', 'removeFromWhitelist'); |
|
| 71 | - if ($removeFromWhitelist && is_array($removeFromWhitelist)) { |
|
| 72 | - $filteredRules = array_merge(array_diff($filteredRules, $removeFromWhitelist)); |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - return $filteredRules; |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - protected static function array_delete($array, $element) { |
|
| 30 | + foreach($allTopLevelRules as $rule) { |
|
| 31 | + if (strpos($rule, '$') !== false) { |
|
| 32 | + if ($rule === '$Controller') { |
|
| 33 | + //special case for Controllers, add all possible controllers |
|
| 34 | + $subControllers = ClassInfo::subclassesFor(new Controller()); |
|
| 35 | + |
|
| 36 | + foreach ($subControllers as $controller){ |
|
| 37 | + array_push($filteredRules, $controller); //add the controller name as a link |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + } elseif ($rule === '$URLSegment') { |
|
| 41 | + $topLevelPagesArray = array(); //temporary array to store top-level pages |
|
| 42 | + |
|
| 43 | + //special case for SiteTree, add all possible top Level Pages |
|
| 44 | + $topLevelPages = SiteTree::get()->filter('ParentID', 0); |
|
| 45 | + |
|
| 46 | + foreach ($topLevelPages as $page) { |
|
| 47 | + $link = $page->RelativeLink(); |
|
| 48 | + array_push($topLevelPagesArray, trim($link, '\/ ')); //remove trailing or leading slashes from links |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + //fetch old top-level pages URLs from the SiteTree_versions table |
|
| 52 | + if (Config::inst()->get('WhitelistGenerator', 'includeSiteTreeVersions')) { |
|
| 53 | + $oldTopLevelPagesArray = self::find_old_top_level_pages($topLevelPagesArray); |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + $filteredRules = array_merge($filteredRules, $topLevelPagesArray, $oldTopLevelPagesArray); |
|
| 57 | + } else { |
|
| 58 | + user_error('Unknown wildcard URL match found: '.$rule, E_WARNING); |
|
| 59 | + } |
|
| 60 | + } else { |
|
| 61 | + //add the rule to a new list of rules |
|
| 62 | + array_push($filteredRules, $rule); |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + //filter duplicates (order doesn't matter here, as we are only interested in the first level of the rules) |
|
| 68 | + $filteredRules = array_unique($filteredRules); |
|
| 69 | + |
|
| 70 | + $removeFromWhitelist = Config::inst()->get('WhitelistGenerator', 'removeFromWhitelist'); |
|
| 71 | + if ($removeFromWhitelist && is_array($removeFromWhitelist)) { |
|
| 72 | + $filteredRules = array_merge(array_diff($filteredRules, $removeFromWhitelist)); |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + return $filteredRules; |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + protected static function array_delete($array, $element) { |
|
| 79 | 79 | $elementArray = array($element); |
| 80 | - return array_diff($array, $elementArray); |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * Sync the list of all top-level routes with the file system whitelist cache |
|
| 85 | - */ |
|
| 86 | - protected static function syncCacheFilesystem($whitelist) { |
|
| 87 | - $dir = BASE_PATH . DIRECTORY_SEPARATOR . Config::inst()->get('WhitelistGenerator', 'dir'); |
|
| 88 | - |
|
| 89 | - $whitelistFolderContents = scandir($dir); |
|
| 90 | - |
|
| 91 | - //create list of files to create |
|
| 92 | - $toCreate = array(); |
|
| 93 | - foreach ($whitelist as $listItem){ |
|
| 94 | - if (!in_array($listItem, $whitelistFolderContents)) { |
|
| 95 | - if (!empty($listItem)) { //don't include empty files, such as the file for / |
|
| 96 | - array_push($toCreate, $listItem); |
|
| 97 | - } |
|
| 98 | - } |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - //create list of files to delete |
|
| 102 | - $toDelete = array(); |
|
| 103 | - foreach ($whitelistFolderContents as $file){ |
|
| 104 | - if (!in_array($file, array('','..','.','.htaccess'))) { //exclude things that should stay in the folder |
|
| 105 | - if (!in_array($file, $whitelist)) { |
|
| 106 | - array_push($toDelete, $file); |
|
| 107 | - } |
|
| 108 | - } |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - //delete files which are no longer necessary |
|
| 112 | - foreach ($toDelete as $delete) { |
|
| 113 | - unlink($dir . DIRECTORY_SEPARATOR . $delete); |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - //create new whitelist items as files |
|
| 117 | - foreach ($toCreate as $create) { |
|
| 118 | - touch($dir . DIRECTORY_SEPARATOR . $create); |
|
| 119 | - } |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * Does a database query searching through past URLs of top-level pages, returning any URLs previously used for |
|
| 124 | - * pages in the SiteTree. This is to ensure that OldPageRedirector rules still apply correctly. That is, to ensure |
|
| 125 | - * that pages that have been renamed continue to redirect to their current versions, we add the pages' old URLs |
|
| 126 | - * to the whitelist. |
|
| 127 | - * @param $currentTopLevelPages |
|
| 128 | - * @return array URLs of past top-level pages |
|
| 129 | - */ |
|
| 130 | - protected static function find_old_top_level_pages($currentTopLevelPages){ |
|
| 131 | - $oldPageURLs = array(); |
|
| 132 | - |
|
| 133 | - $queryClass = 'SQLSelect'; |
|
| 134 | - if (!class_exists($queryClass) && class_exists('SQLQuery')){ |
|
| 135 | - $queryClass = 'SQLQuery'; |
|
| 136 | - } |
|
| 80 | + return array_diff($array, $elementArray); |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * Sync the list of all top-level routes with the file system whitelist cache |
|
| 85 | + */ |
|
| 86 | + protected static function syncCacheFilesystem($whitelist) { |
|
| 87 | + $dir = BASE_PATH . DIRECTORY_SEPARATOR . Config::inst()->get('WhitelistGenerator', 'dir'); |
|
| 88 | + |
|
| 89 | + $whitelistFolderContents = scandir($dir); |
|
| 90 | + |
|
| 91 | + //create list of files to create |
|
| 92 | + $toCreate = array(); |
|
| 93 | + foreach ($whitelist as $listItem){ |
|
| 94 | + if (!in_array($listItem, $whitelistFolderContents)) { |
|
| 95 | + if (!empty($listItem)) { //don't include empty files, such as the file for / |
|
| 96 | + array_push($toCreate, $listItem); |
|
| 97 | + } |
|
| 98 | + } |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + //create list of files to delete |
|
| 102 | + $toDelete = array(); |
|
| 103 | + foreach ($whitelistFolderContents as $file){ |
|
| 104 | + if (!in_array($file, array('','..','.','.htaccess'))) { //exclude things that should stay in the folder |
|
| 105 | + if (!in_array($file, $whitelist)) { |
|
| 106 | + array_push($toDelete, $file); |
|
| 107 | + } |
|
| 108 | + } |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + //delete files which are no longer necessary |
|
| 112 | + foreach ($toDelete as $delete) { |
|
| 113 | + unlink($dir . DIRECTORY_SEPARATOR . $delete); |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + //create new whitelist items as files |
|
| 117 | + foreach ($toCreate as $create) { |
|
| 118 | + touch($dir . DIRECTORY_SEPARATOR . $create); |
|
| 119 | + } |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * Does a database query searching through past URLs of top-level pages, returning any URLs previously used for |
|
| 124 | + * pages in the SiteTree. This is to ensure that OldPageRedirector rules still apply correctly. That is, to ensure |
|
| 125 | + * that pages that have been renamed continue to redirect to their current versions, we add the pages' old URLs |
|
| 126 | + * to the whitelist. |
|
| 127 | + * @param $currentTopLevelPages |
|
| 128 | + * @return array URLs of past top-level pages |
|
| 129 | + */ |
|
| 130 | + protected static function find_old_top_level_pages($currentTopLevelPages){ |
|
| 131 | + $oldPageURLs = array(); |
|
| 132 | + |
|
| 133 | + $queryClass = 'SQLSelect'; |
|
| 134 | + if (!class_exists($queryClass) && class_exists('SQLQuery')){ |
|
| 135 | + $queryClass = 'SQLQuery'; |
|
| 136 | + } |
|
| 137 | 137 | |
| 138 | - $query = new $queryClass( |
|
| 139 | - 'DISTINCT (URLSegment)', |
|
| 140 | - 'SiteTree_versions', |
|
| 141 | - array( |
|
| 142 | - 'ParentID = 0', |
|
| 143 | - 'WasPublished = 1', |
|
| 144 | - 'URLSegment NOT IN (\''.implode("','",array_filter($currentTopLevelPages)).'\')' |
|
| 145 | - ) |
|
| 146 | - ); |
|
| 147 | - |
|
| 148 | - $records = $query->execute(); |
|
| 149 | - if ($records) { |
|
| 150 | - foreach($records as $record) { |
|
| 151 | - array_push($oldPageURLs, $record['URLSegment']); |
|
| 152 | - } |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - return $oldPageURLs; |
|
| 156 | - } |
|
| 138 | + $query = new $queryClass( |
|
| 139 | + 'DISTINCT (URLSegment)', |
|
| 140 | + 'SiteTree_versions', |
|
| 141 | + array( |
|
| 142 | + 'ParentID = 0', |
|
| 143 | + 'WasPublished = 1', |
|
| 144 | + 'URLSegment NOT IN (\''.implode("','",array_filter($currentTopLevelPages)).'\')' |
|
| 145 | + ) |
|
| 146 | + ); |
|
| 147 | + |
|
| 148 | + $records = $query->execute(); |
|
| 149 | + if ($records) { |
|
| 150 | + foreach($records as $record) { |
|
| 151 | + array_push($oldPageURLs, $record['URLSegment']); |
|
| 152 | + } |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + return $oldPageURLs; |
|
| 156 | + } |
|
| 157 | 157 | |
| 158 | 158 | public static function ensureWhitelistFolderExists(){ |
| 159 | - $dir = BASE_PATH . DIRECTORY_SEPARATOR . Config::inst()->get('WhitelistGenerator', 'dir'); |
|
| 160 | - if (!file_exists($dir)) { |
|
| 161 | - mkdir($dir); //create a new whitelist dir |
|
| 162 | - chmod($dir,0777); //make sure it is readable by the web-server user |
|
| 163 | - //create a htaccess file to ensure that the whitelist cache directory is not web-accessible |
|
| 164 | - file_put_contents($dir.DIRECTORY_SEPARATOR.'.htaccess', "Deny from all\n"); |
|
| 165 | - } |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - public static function clearWhitelist(){ |
|
| 169 | - $dir = BASE_PATH . DIRECTORY_SEPARATOR . Config::inst()->get('WhitelistGenerator', 'dir'); |
|
| 170 | - if (file_exists($dir)) { |
|
| 171 | - array_map('unlink', glob($dir."/*")); |
|
| 172 | - } |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - public static function flush() { |
|
| 176 | - self::ensureWhitelistFolderExists(); //only create folder on flush, not on sitetree changes |
|
| 177 | - self::generateWhitelist(); |
|
| 178 | - } |
|
| 159 | + $dir = BASE_PATH . DIRECTORY_SEPARATOR . Config::inst()->get('WhitelistGenerator', 'dir'); |
|
| 160 | + if (!file_exists($dir)) { |
|
| 161 | + mkdir($dir); //create a new whitelist dir |
|
| 162 | + chmod($dir,0777); //make sure it is readable by the web-server user |
|
| 163 | + //create a htaccess file to ensure that the whitelist cache directory is not web-accessible |
|
| 164 | + file_put_contents($dir.DIRECTORY_SEPARATOR.'.htaccess', "Deny from all\n"); |
|
| 165 | + } |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + public static function clearWhitelist(){ |
|
| 169 | + $dir = BASE_PATH . DIRECTORY_SEPARATOR . Config::inst()->get('WhitelistGenerator', 'dir'); |
|
| 170 | + if (file_exists($dir)) { |
|
| 171 | + array_map('unlink', glob($dir."/*")); |
|
| 172 | + } |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + public static function flush() { |
|
| 176 | + self::ensureWhitelistFolderExists(); //only create folder on flush, not on sitetree changes |
|
| 177 | + self::generateWhitelist(); |
|
| 178 | + } |
|
| 179 | 179 | } |
@@ -1,20 +1,20 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | class WhitelistGenerator extends Object implements Flushable { |
| 3 | 3 | |
| 4 | - public static function generateWhitelist(){ |
|
| 4 | + public static function generateWhitelist() { |
|
| 5 | 5 | $whitelist = self::generateWhitelistRules(); |
| 6 | 6 | self::syncCacheFilesystem($whitelist); |
| 7 | 7 | } |
| 8 | 8 | |
| 9 | - public static function generateWhitelistRules(){ |
|
| 9 | + public static function generateWhitelistRules() { |
|
| 10 | 10 | //get all URL rules |
| 11 | 11 | $rules = Config::inst()->get('Director', 'rules'); |
| 12 | 12 | |
| 13 | 13 | $allTopLevelRules = array(); |
| 14 | 14 | foreach ($rules as $pattern => $controllerOptions) { |
| 15 | 15 | //match first portion of the URL, either delimited by slash or colon or end-of-line |
| 16 | - if (preg_match('/^(.*?)(\/|:|$)/', $pattern, $matches)){ |
|
| 17 | - if (!empty($matches[1])){ |
|
| 16 | + if (preg_match('/^(.*?)(\/|:|$)/', $pattern, $matches)) { |
|
| 17 | + if (!empty($matches[1])) { |
|
| 18 | 18 | array_push($allTopLevelRules, $matches[1]); |
| 19 | 19 | } |
| 20 | 20 | } |
@@ -27,18 +27,18 @@ discard block |
||
| 27 | 27 | $filteredRules = array_merge($filteredRules, $addToWhitelist); |
| 28 | 28 | } |
| 29 | 29 | |
| 30 | - foreach($allTopLevelRules as $rule) { |
|
| 30 | + foreach ($allTopLevelRules as $rule) { |
|
| 31 | 31 | if (strpos($rule, '$') !== false) { |
| 32 | 32 | if ($rule === '$Controller') { |
| 33 | 33 | //special case for Controllers, add all possible controllers |
| 34 | 34 | $subControllers = ClassInfo::subclassesFor(new Controller()); |
| 35 | 35 | |
| 36 | - foreach ($subControllers as $controller){ |
|
| 37 | - array_push($filteredRules, $controller); //add the controller name as a link |
|
| 36 | + foreach ($subControllers as $controller) { |
|
| 37 | + array_push($filteredRules, $controller); //add the controller name as a link |
|
| 38 | 38 | } |
| 39 | 39 | |
| 40 | 40 | } elseif ($rule === '$URLSegment') { |
| 41 | - $topLevelPagesArray = array(); //temporary array to store top-level pages |
|
| 41 | + $topLevelPagesArray = array(); //temporary array to store top-level pages |
|
| 42 | 42 | |
| 43 | 43 | //special case for SiteTree, add all possible top Level Pages |
| 44 | 44 | $topLevelPages = SiteTree::get()->filter('ParentID', 0); |
@@ -55,7 +55,7 @@ discard block |
||
| 55 | 55 | |
| 56 | 56 | $filteredRules = array_merge($filteredRules, $topLevelPagesArray, $oldTopLevelPagesArray); |
| 57 | 57 | } else { |
| 58 | - user_error('Unknown wildcard URL match found: '.$rule, E_WARNING); |
|
| 58 | + user_error('Unknown wildcard URL match found: ' . $rule, E_WARNING); |
|
| 59 | 59 | } |
| 60 | 60 | } else { |
| 61 | 61 | //add the rule to a new list of rules |
@@ -90,7 +90,7 @@ discard block |
||
| 90 | 90 | |
| 91 | 91 | //create list of files to create |
| 92 | 92 | $toCreate = array(); |
| 93 | - foreach ($whitelist as $listItem){ |
|
| 93 | + foreach ($whitelist as $listItem) { |
|
| 94 | 94 | if (!in_array($listItem, $whitelistFolderContents)) { |
| 95 | 95 | if (!empty($listItem)) { //don't include empty files, such as the file for / |
| 96 | 96 | array_push($toCreate, $listItem); |
@@ -100,8 +100,8 @@ discard block |
||
| 100 | 100 | |
| 101 | 101 | //create list of files to delete |
| 102 | 102 | $toDelete = array(); |
| 103 | - foreach ($whitelistFolderContents as $file){ |
|
| 104 | - if (!in_array($file, array('','..','.','.htaccess'))) { //exclude things that should stay in the folder |
|
| 103 | + foreach ($whitelistFolderContents as $file) { |
|
| 104 | + if (!in_array($file, array('', '..', '.', '.htaccess'))) { //exclude things that should stay in the folder |
|
| 105 | 105 | if (!in_array($file, $whitelist)) { |
| 106 | 106 | array_push($toDelete, $file); |
| 107 | 107 | } |
@@ -127,11 +127,11 @@ discard block |
||
| 127 | 127 | * @param $currentTopLevelPages |
| 128 | 128 | * @return array URLs of past top-level pages |
| 129 | 129 | */ |
| 130 | - protected static function find_old_top_level_pages($currentTopLevelPages){ |
|
| 130 | + protected static function find_old_top_level_pages($currentTopLevelPages) { |
|
| 131 | 131 | $oldPageURLs = array(); |
| 132 | 132 | |
| 133 | 133 | $queryClass = 'SQLSelect'; |
| 134 | - if (!class_exists($queryClass) && class_exists('SQLQuery')){ |
|
| 134 | + if (!class_exists($queryClass) && class_exists('SQLQuery')) { |
|
| 135 | 135 | $queryClass = 'SQLQuery'; |
| 136 | 136 | } |
| 137 | 137 | |
@@ -141,13 +141,13 @@ discard block |
||
| 141 | 141 | array( |
| 142 | 142 | 'ParentID = 0', |
| 143 | 143 | 'WasPublished = 1', |
| 144 | - 'URLSegment NOT IN (\''.implode("','",array_filter($currentTopLevelPages)).'\')' |
|
| 144 | + 'URLSegment NOT IN (\'' . implode("','", array_filter($currentTopLevelPages)) . '\')' |
|
| 145 | 145 | ) |
| 146 | 146 | ); |
| 147 | 147 | |
| 148 | 148 | $records = $query->execute(); |
| 149 | 149 | if ($records) { |
| 150 | - foreach($records as $record) { |
|
| 150 | + foreach ($records as $record) { |
|
| 151 | 151 | array_push($oldPageURLs, $record['URLSegment']); |
| 152 | 152 | } |
| 153 | 153 | } |
@@ -155,25 +155,25 @@ discard block |
||
| 155 | 155 | return $oldPageURLs; |
| 156 | 156 | } |
| 157 | 157 | |
| 158 | - public static function ensureWhitelistFolderExists(){ |
|
| 158 | + public static function ensureWhitelistFolderExists() { |
|
| 159 | 159 | $dir = BASE_PATH . DIRECTORY_SEPARATOR . Config::inst()->get('WhitelistGenerator', 'dir'); |
| 160 | 160 | if (!file_exists($dir)) { |
| 161 | 161 | mkdir($dir); //create a new whitelist dir |
| 162 | - chmod($dir,0777); //make sure it is readable by the web-server user |
|
| 162 | + chmod($dir, 0777); //make sure it is readable by the web-server user |
|
| 163 | 163 | //create a htaccess file to ensure that the whitelist cache directory is not web-accessible |
| 164 | - file_put_contents($dir.DIRECTORY_SEPARATOR.'.htaccess', "Deny from all\n"); |
|
| 164 | + file_put_contents($dir . DIRECTORY_SEPARATOR . '.htaccess', "Deny from all\n"); |
|
| 165 | 165 | } |
| 166 | 166 | } |
| 167 | 167 | |
| 168 | - public static function clearWhitelist(){ |
|
| 168 | + public static function clearWhitelist() { |
|
| 169 | 169 | $dir = BASE_PATH . DIRECTORY_SEPARATOR . Config::inst()->get('WhitelistGenerator', 'dir'); |
| 170 | 170 | if (file_exists($dir)) { |
| 171 | - array_map('unlink', glob($dir."/*")); |
|
| 171 | + array_map('unlink', glob($dir . "/*")); |
|
| 172 | 172 | } |
| 173 | 173 | } |
| 174 | 174 | |
| 175 | 175 | public static function flush() { |
| 176 | - self::ensureWhitelistFolderExists(); //only create folder on flush, not on sitetree changes |
|
| 176 | + self::ensureWhitelistFolderExists(); //only create folder on flush, not on sitetree changes |
|
| 177 | 177 | self::generateWhitelist(); |
| 178 | 178 | } |
| 179 | 179 | } |
@@ -1,34 +1,34 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | class WhitelistGeneratorTest extends SapphireTest { |
| 3 | 3 | |
| 4 | - protected static $fixture_file = 'WhitelistTest.yml'; |
|
| 4 | + protected static $fixture_file = 'WhitelistTest.yml'; |
|
| 5 | 5 | |
| 6 | 6 | public function setUp() { |
| 7 | 7 | WhitelistGenerator::ensureWhitelistFolderExists(); |
| 8 | 8 | parent::setUp(); |
| 9 | 9 | } |
| 10 | 10 | |
| 11 | - public function testGenerateWhitelist(){ |
|
| 12 | - $whitelist = WhitelistGenerator::generateWhitelistRules(); |
|
| 13 | - |
|
| 14 | - $top1 = $this->objFromFixture('SiteTree', 'top1'); |
|
| 15 | - $top2 = $this->objFromFixture('SiteTree', 'top2'); |
|
| 16 | - $top3 = $this->objFromFixture('SiteTree', 'top3'); |
|
| 17 | - $child1 = $this->objFromFixture('SiteTree', 'child1'); |
|
| 18 | - $child2 = $this->objFromFixture('SiteTree', 'child2'); |
|
| 19 | - $child3 = $this->objFromFixture('SiteTree', 'childchild1'); |
|
| 20 | - $child4 = $this->objFromFixture('SiteTree', 'childchild2'); |
|
| 21 | - $child5 = $this->objFromFixture('SiteTree', 'childchildchild1'); |
|
| 22 | - |
|
| 23 | - $this->assertContains(trim($top1->relativeLink(), '/'), $whitelist); |
|
| 24 | - $this->assertContains(trim($top2->relativeLink(), '/'), $whitelist); |
|
| 25 | - $this->assertContains(trim($top3->relativeLink(), '/'), $whitelist); |
|
| 26 | - $this->assertNotContains(trim($child1->relativeLink(), '/'), $whitelist); |
|
| 27 | - $this->assertNotContains(trim($child2->relativeLink(), '/'), $whitelist); |
|
| 28 | - $this->assertNotContains(trim($child3->relativeLink(), '/'), $whitelist); |
|
| 29 | - $this->assertNotContains(trim($child4->relativeLink(), '/'), $whitelist); |
|
| 30 | - $this->assertNotContains(trim($child5->relativeLink(), '/'), $whitelist); |
|
| 31 | - } |
|
| 11 | + public function testGenerateWhitelist(){ |
|
| 12 | + $whitelist = WhitelistGenerator::generateWhitelistRules(); |
|
| 13 | + |
|
| 14 | + $top1 = $this->objFromFixture('SiteTree', 'top1'); |
|
| 15 | + $top2 = $this->objFromFixture('SiteTree', 'top2'); |
|
| 16 | + $top3 = $this->objFromFixture('SiteTree', 'top3'); |
|
| 17 | + $child1 = $this->objFromFixture('SiteTree', 'child1'); |
|
| 18 | + $child2 = $this->objFromFixture('SiteTree', 'child2'); |
|
| 19 | + $child3 = $this->objFromFixture('SiteTree', 'childchild1'); |
|
| 20 | + $child4 = $this->objFromFixture('SiteTree', 'childchild2'); |
|
| 21 | + $child5 = $this->objFromFixture('SiteTree', 'childchildchild1'); |
|
| 22 | + |
|
| 23 | + $this->assertContains(trim($top1->relativeLink(), '/'), $whitelist); |
|
| 24 | + $this->assertContains(trim($top2->relativeLink(), '/'), $whitelist); |
|
| 25 | + $this->assertContains(trim($top3->relativeLink(), '/'), $whitelist); |
|
| 26 | + $this->assertNotContains(trim($child1->relativeLink(), '/'), $whitelist); |
|
| 27 | + $this->assertNotContains(trim($child2->relativeLink(), '/'), $whitelist); |
|
| 28 | + $this->assertNotContains(trim($child3->relativeLink(), '/'), $whitelist); |
|
| 29 | + $this->assertNotContains(trim($child4->relativeLink(), '/'), $whitelist); |
|
| 30 | + $this->assertNotContains(trim($child5->relativeLink(), '/'), $whitelist); |
|
| 31 | + } |
|
| 32 | 32 | |
| 33 | 33 | public function testWhitelistAfterDelete() { |
| 34 | 34 | $dir = BASE_PATH . DIRECTORY_SEPARATOR . Config::inst()->get('WhitelistGenerator', 'dir'); |
@@ -98,30 +98,30 @@ discard block |
||
| 98 | 98 | $this->assertEquals(1, sizeof($files)); |
| 99 | 99 | } |
| 100 | 100 | |
| 101 | - public function testCustomControllerWhitelist() { |
|
| 102 | - $whitelist = WhitelistGenerator::generateWhitelistRules(); |
|
| 101 | + public function testCustomControllerWhitelist() { |
|
| 102 | + $whitelist = WhitelistGenerator::generateWhitelistRules(); |
|
| 103 | 103 | |
| 104 | - //test that custom class defined below is included in the whitelist |
|
| 105 | - $this->assertContains('WhitelistTestController', $whitelist); |
|
| 106 | - } |
|
| 104 | + //test that custom class defined below is included in the whitelist |
|
| 105 | + $this->assertContains('WhitelistTestController', $whitelist); |
|
| 106 | + } |
|
| 107 | 107 | |
| 108 | - function testSiteTreeVersionsIncludedInWhitelist() { |
|
| 109 | - $top1 = $this->objFromFixture('SiteTree', 'top1'); |
|
| 110 | - $top1->publish('Stage', 'Live'); //publish the page so it has been live and needs redirecting to |
|
| 108 | + function testSiteTreeVersionsIncludedInWhitelist() { |
|
| 109 | + $top1 = $this->objFromFixture('SiteTree', 'top1'); |
|
| 110 | + $top1->publish('Stage', 'Live'); //publish the page so it has been live and needs redirecting to |
|
| 111 | 111 | |
| 112 | - $newSegment = 'new-url-segment'; |
|
| 113 | - $oldSegment = $top1->URLSegment; |
|
| 114 | - $top1->URLSegment = $newSegment; |
|
| 115 | - $top1->write(); |
|
| 116 | - $top1->publish('Stage', 'Live'); //publish again with a new URL |
|
| 112 | + $newSegment = 'new-url-segment'; |
|
| 113 | + $oldSegment = $top1->URLSegment; |
|
| 114 | + $top1->URLSegment = $newSegment; |
|
| 115 | + $top1->write(); |
|
| 116 | + $top1->publish('Stage', 'Live'); //publish again with a new URL |
|
| 117 | 117 | |
| 118 | 118 | |
| 119 | - $whitelist = WhitelistGenerator::generateWhitelistRules(); |
|
| 119 | + $whitelist = WhitelistGenerator::generateWhitelistRules(); |
|
| 120 | 120 | |
| 121 | - //ensure both the old and the new URLs are included in the whitelist |
|
| 122 | - $this->assertContains($newSegment, $whitelist); |
|
| 123 | - $this->assertContains($oldSegment, $whitelist); |
|
| 124 | - } |
|
| 121 | + //ensure both the old and the new URLs are included in the whitelist |
|
| 122 | + $this->assertContains($newSegment, $whitelist); |
|
| 123 | + $this->assertContains($oldSegment, $whitelist); |
|
| 124 | + } |
|
| 125 | 125 | |
| 126 | 126 | public function testFlush() { |
| 127 | 127 | $dir = BASE_PATH . DIRECTORY_SEPARATOR . Config::inst()->get('WhitelistGenerator', 'dir'); |
@@ -139,32 +139,32 @@ discard block |
||
| 139 | 139 | $this->assertGreaterThan(90, sizeof($files)); |
| 140 | 140 | } |
| 141 | 141 | |
| 142 | - public function testAddToWhitelist() { |
|
| 143 | - $addToList = array('test', 'create', 'this-is-top-level', 'random-URL-string', '____', '-----'); |
|
| 144 | - $removeFromList = array('home', 'create', 'xzy', 'about-us'); |
|
| 142 | + public function testAddToWhitelist() { |
|
| 143 | + $addToList = array('test', 'create', 'this-is-top-level', 'random-URL-string', '____', '-----'); |
|
| 144 | + $removeFromList = array('home', 'create', 'xzy', 'about-us'); |
|
| 145 | 145 | |
| 146 | - Config::inst()->update('WhitelistGenerator', 'addToWhitelist', $addToList); |
|
| 146 | + Config::inst()->update('WhitelistGenerator', 'addToWhitelist', $addToList); |
|
| 147 | 147 | |
| 148 | - $whitelist = WhitelistGenerator::generateWhitelistRules(); |
|
| 148 | + $whitelist = WhitelistGenerator::generateWhitelistRules(); |
|
| 149 | 149 | |
| 150 | - foreach($addToList as $add) { |
|
| 151 | - $this->assertContains($add, $whitelist); |
|
| 152 | - } |
|
| 153 | - } |
|
| 150 | + foreach($addToList as $add) { |
|
| 151 | + $this->assertContains($add, $whitelist); |
|
| 152 | + } |
|
| 153 | + } |
|
| 154 | 154 | |
| 155 | - public function testRemoveFromWhitelist() { |
|
| 156 | - $addToList = array('create'); |
|
| 157 | - $removeFromList = array('home', 'create', 'xzy', 'about-us'); |
|
| 155 | + public function testRemoveFromWhitelist() { |
|
| 156 | + $addToList = array('create'); |
|
| 157 | + $removeFromList = array('home', 'create', 'xzy', 'about-us'); |
|
| 158 | 158 | |
| 159 | - Config::inst()->update('WhitelistGenerator', 'addToWhitelist', $addToList); |
|
| 160 | - Config::inst()->update('WhitelistGenerator', 'removeFromWhitelist', $removeFromList); |
|
| 159 | + Config::inst()->update('WhitelistGenerator', 'addToWhitelist', $addToList); |
|
| 160 | + Config::inst()->update('WhitelistGenerator', 'removeFromWhitelist', $removeFromList); |
|
| 161 | 161 | |
| 162 | - $whitelist = WhitelistGenerator::generateWhitelistRules(); |
|
| 162 | + $whitelist = WhitelistGenerator::generateWhitelistRules(); |
|
| 163 | 163 | |
| 164 | - foreach($removeFromList as $remove) { |
|
| 165 | - $this->assertNotContains($remove, $whitelist); |
|
| 166 | - } |
|
| 167 | - } |
|
| 164 | + foreach($removeFromList as $remove) { |
|
| 165 | + $this->assertNotContains($remove, $whitelist); |
|
| 166 | + } |
|
| 167 | + } |
|
| 168 | 168 | |
| 169 | 169 | } |
| 170 | 170 | |
@@ -8,7 +8,7 @@ discard block |
||
| 8 | 8 | parent::setUp(); |
| 9 | 9 | } |
| 10 | 10 | |
| 11 | - public function testGenerateWhitelist(){ |
|
| 11 | + public function testGenerateWhitelist() { |
|
| 12 | 12 | $whitelist = WhitelistGenerator::generateWhitelistRules(); |
| 13 | 13 | |
| 14 | 14 | $top1 = $this->objFromFixture('SiteTree', 'top1'); |
@@ -107,13 +107,13 @@ discard block |
||
| 107 | 107 | |
| 108 | 108 | function testSiteTreeVersionsIncludedInWhitelist() { |
| 109 | 109 | $top1 = $this->objFromFixture('SiteTree', 'top1'); |
| 110 | - $top1->publish('Stage', 'Live'); //publish the page so it has been live and needs redirecting to |
|
| 110 | + $top1->publish('Stage', 'Live'); //publish the page so it has been live and needs redirecting to |
|
| 111 | 111 | |
| 112 | 112 | $newSegment = 'new-url-segment'; |
| 113 | 113 | $oldSegment = $top1->URLSegment; |
| 114 | 114 | $top1->URLSegment = $newSegment; |
| 115 | 115 | $top1->write(); |
| 116 | - $top1->publish('Stage', 'Live'); //publish again with a new URL |
|
| 116 | + $top1->publish('Stage', 'Live'); //publish again with a new URL |
|
| 117 | 117 | |
| 118 | 118 | |
| 119 | 119 | $whitelist = WhitelistGenerator::generateWhitelistRules(); |
@@ -147,7 +147,7 @@ discard block |
||
| 147 | 147 | |
| 148 | 148 | $whitelist = WhitelistGenerator::generateWhitelistRules(); |
| 149 | 149 | |
| 150 | - foreach($addToList as $add) { |
|
| 150 | + foreach ($addToList as $add) { |
|
| 151 | 151 | $this->assertContains($add, $whitelist); |
| 152 | 152 | } |
| 153 | 153 | } |
@@ -161,7 +161,7 @@ discard block |
||
| 161 | 161 | |
| 162 | 162 | $whitelist = WhitelistGenerator::generateWhitelistRules(); |
| 163 | 163 | |
| 164 | - foreach($removeFromList as $remove) { |
|
| 164 | + foreach ($removeFromList as $remove) { |
|
| 165 | 165 | $this->assertNotContains($remove, $whitelist); |
| 166 | 166 | } |
| 167 | 167 | } |