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