Completed
Push — master ( 2ec91e...839f4b )
by
unknown
12:59
created
code/SiteTreeWhitelistExtension.php 2 patches
Indentation   +11 added lines, -11 removed lines patch added patch discarded remove patch
@@ -1,17 +1,17 @@
 block discarded – undo
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
Please login to merge, or discard this patch.
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -1,14 +1,14 @@
 block discarded – undo
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
 		}
Please login to merge, or discard this patch.
tests/WhitelistGeneratorTest.php 2 patches
Indentation   +59 added lines, -59 removed lines patch added patch discarded remove patch
@@ -1,34 +1,34 @@  discard block
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 
Please login to merge, or discard this patch.
Spacing   +11 added lines, -11 removed lines patch added patch discarded remove patch
@@ -8,7 +8,7 @@  discard block
 block discarded – undo
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');
@@ -31,10 +31,10 @@  discard block
 block discarded – undo
31 31
 	}
32 32
 
33 33
     public function testWhitelistAfterDelete() {
34
-        $dir = BASE_PATH . DIRECTORY_SEPARATOR . Config::inst()->get('WhitelistGenerator', 'dir');
34
+        $dir = BASE_PATH.DIRECTORY_SEPARATOR.Config::inst()->get('WhitelistGenerator', 'dir');
35 35
         $whitelist = WhitelistGenerator::generateWhitelistRules();
36 36
         $top1 = $this->objFromFixture('SiteTree', 'top1');
37
-        $path = $dir . '/' . $top1->URLSegment;
37
+        $path = $dir.'/'.$top1->URLSegment;
38 38
 
39 39
         //Check that relevant file exists in cache directory of checks
40 40
         $this->assertTrue(file_exists($path));
@@ -45,7 +45,7 @@  discard block
 block discarded – undo
45 45
     }
46 46
 
47 47
     private function getFilesFromCacheDir() {
48
-        $dir = BASE_PATH . DIRECTORY_SEPARATOR . Config::inst()->get('WhitelistGenerator', 'dir');
48
+        $dir = BASE_PATH.DIRECTORY_SEPARATOR.Config::inst()->get('WhitelistGenerator', 'dir');
49 49
         $files = array();
50 50
         if ($handle = opendir($dir)) {
51 51
             while (false !== ($entry = readdir($handle))) {
@@ -72,10 +72,10 @@  discard block
 block discarded – undo
72 72
     }
73 73
 
74 74
     public function testWhitelistAfterUnpublish() {
75
-        $dir = BASE_PATH . DIRECTORY_SEPARATOR . Config::inst()->get('WhitelistGenerator', 'dir');
75
+        $dir = BASE_PATH.DIRECTORY_SEPARATOR.Config::inst()->get('WhitelistGenerator', 'dir');
76 76
         $whitelist = WhitelistGenerator::generateWhitelistRules();
77 77
         $top1 = $this->objFromFixture('SiteTree', 'top1');
78
-        $path = $dir . '/' . $top1->URLSegment;
78
+        $path = $dir.'/'.$top1->URLSegment;
79 79
 
80 80
         //Check that relevant file exists in cache directory of checks
81 81
         $this->assertTrue(file_exists($path));
@@ -107,13 +107,13 @@  discard block
 block discarded – undo
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();
@@ -124,7 +124,7 @@  discard block
 block discarded – undo
124 124
 	}
125 125
 
126 126
     public function testFlush() {
127
-        $dir = BASE_PATH . DIRECTORY_SEPARATOR . Config::inst()->get('WhitelistGenerator', 'dir');
127
+        $dir = BASE_PATH.DIRECTORY_SEPARATOR.Config::inst()->get('WhitelistGenerator', 'dir');
128 128
 
129 129
         //Delete cache directory
130 130
         WhitelistGenerator::clearWhitelist();
@@ -147,7 +147,7 @@  discard block
 block discarded – undo
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
 block discarded – undo
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
 	}
Please login to merge, or discard this patch.
code/WhitelistGenerator.php 2 patches
Indentation   +171 added lines, -171 removed lines patch added patch discarded remove patch
@@ -1,181 +1,181 @@
 block discarded – undo
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
-			//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
-
45
-					//special case for SiteTree, add all possible top Level Pages
46
-					$topLevelPages = SiteTree::get()->filter('ParentID', 0);
47
-
48
-					foreach ($topLevelPages as $page) {
49
-						$link = $page->RelativeLink();
50
-						array_push($topLevelPagesArray, trim($link, '\/ ')); //remove trailing or leading slashes from links
51
-					}
52
-
53
-					//fetch old top-level pages URLs from the SiteTree_versions table
54
-					if (Config::inst()->get('WhitelistGenerator', 'includeSiteTreeVersions')) {
55
-						$oldTopLevelPagesArray = self::find_old_top_level_pages($topLevelPagesArray);
56
-					}
57
-
58
-					$filteredRules = array_merge($filteredRules, $topLevelPagesArray, $oldTopLevelPagesArray);
59
-				} else {
60
-					user_error('Unknown wildcard URL match found: '.$rule, E_WARNING);
61
-				}
62
-			} else {
63
-				//add the rule to a new list of rules
64
-				array_push($filteredRules, $rule);
65
-			}
66
-
67
-		}
68
-
69
-		//filter duplicates (order doesn't matter here, as we are only interested in the first level of the rules)
70
-		$filteredRules = array_unique($filteredRules);
71
-
72
-		$removeFromWhitelist = Config::inst()->get('WhitelistGenerator', 'removeFromWhitelist');
73
-		if ($removeFromWhitelist && is_array($removeFromWhitelist)) {
74
-			$filteredRules = array_merge(array_diff($filteredRules, $removeFromWhitelist));
75
-		}
76
-
77
-		return $filteredRules;
78
-	}
79
-
80
-	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
+
45
+                    //special case for SiteTree, add all possible top Level Pages
46
+                    $topLevelPages = SiteTree::get()->filter('ParentID', 0);
47
+
48
+                    foreach ($topLevelPages as $page) {
49
+                        $link = $page->RelativeLink();
50
+                        array_push($topLevelPagesArray, trim($link, '\/ ')); //remove trailing or leading slashes from links
51
+                    }
52
+
53
+                    //fetch old top-level pages URLs from the SiteTree_versions table
54
+                    if (Config::inst()->get('WhitelistGenerator', 'includeSiteTreeVersions')) {
55
+                        $oldTopLevelPagesArray = self::find_old_top_level_pages($topLevelPagesArray);
56
+                    }
57
+
58
+                    $filteredRules = array_merge($filteredRules, $topLevelPagesArray, $oldTopLevelPagesArray);
59
+                } else {
60
+                    user_error('Unknown wildcard URL match found: '.$rule, E_WARNING);
61
+                }
62
+            } else {
63
+                //add the rule to a new list of rules
64
+                array_push($filteredRules, $rule);
65
+            }
66
+
67
+        }
68
+
69
+        //filter duplicates (order doesn't matter here, as we are only interested in the first level of the rules)
70
+        $filteredRules = array_unique($filteredRules);
71
+
72
+        $removeFromWhitelist = Config::inst()->get('WhitelistGenerator', 'removeFromWhitelist');
73
+        if ($removeFromWhitelist && is_array($removeFromWhitelist)) {
74
+            $filteredRules = array_merge(array_diff($filteredRules, $removeFromWhitelist));
75
+        }
76
+
77
+        return $filteredRules;
78
+    }
79
+
80
+    protected static function array_delete($array, $element) {
81 81
         $elementArray = array($element);
82
-		return array_diff($array, $elementArray);
83
-	}
84
-
85
-	/**
86
-	 * Sync the list of all top-level routes with the file system whitelist cache
87
-	 */
88
-	protected static function syncCacheFilesystem($whitelist) {
89
-		$dir = BASE_PATH . DIRECTORY_SEPARATOR . Config::inst()->get('WhitelistGenerator', 'dir');
90
-
91
-		$whitelistFolderContents = scandir($dir);
92
-
93
-		//create list of files to create
94
-		$toCreate = array();
95
-		foreach ($whitelist as $listItem){
96
-			if (!in_array($listItem, $whitelistFolderContents)) {
97
-				if (!empty($listItem)) {    //don't include empty files, such as the file for /
98
-					array_push($toCreate, $listItem);
99
-				}
100
-			}
101
-		}
102
-
103
-		//create list of files to delete
104
-		$toDelete = array();
105
-		foreach ($whitelistFolderContents as $file){
106
-			if (!in_array($file, array('','..','.','.htaccess'))) {    //exclude things that should stay in the folder
107
-				if (!in_array($file, $whitelist)) {
108
-					array_push($toDelete, $file);
109
-				}
110
-			}
111
-		}
112
-
113
-		//delete files which are no longer necessary
114
-		foreach ($toDelete as $delete) {
115
-			unlink($dir . DIRECTORY_SEPARATOR . $delete);
116
-		}
117
-
118
-		//create new whitelist items as files
119
-		foreach ($toCreate as $create) {
120
-			touch($dir . DIRECTORY_SEPARATOR . $create);
121
-		}
122
-	}
123
-
124
-	/**
125
-	 * Does a database query searching through past URLs of top-level pages, returning any URLs previously used for
126
-	 * pages in the SiteTree. This is to ensure that OldPageRedirector rules still apply correctly. That is, to ensure
127
-	 * that pages that have been renamed continue to redirect to their current versions, we add the pages' old URLs
128
-	 * to the whitelist.
129
-	 * @param $currentTopLevelPages
130
-	 * @return array URLs of past top-level pages
131
-	 */
132
-	protected static function find_old_top_level_pages($currentTopLevelPages){
133
-		$oldPageURLs = array();
134
-
135
-		$queryClass = 'SQLSelect';
136
-		if (!class_exists($queryClass) && class_exists('SQLQuery')){
137
-			$queryClass = 'SQLQuery';
138
-		}
82
+        return array_diff($array, $elementArray);
83
+    }
84
+
85
+    /**
86
+     * Sync the list of all top-level routes with the file system whitelist cache
87
+     */
88
+    protected static function syncCacheFilesystem($whitelist) {
89
+        $dir = BASE_PATH . DIRECTORY_SEPARATOR . Config::inst()->get('WhitelistGenerator', 'dir');
90
+
91
+        $whitelistFolderContents = scandir($dir);
92
+
93
+        //create list of files to create
94
+        $toCreate = array();
95
+        foreach ($whitelist as $listItem){
96
+            if (!in_array($listItem, $whitelistFolderContents)) {
97
+                if (!empty($listItem)) {    //don't include empty files, such as the file for /
98
+                    array_push($toCreate, $listItem);
99
+                }
100
+            }
101
+        }
102
+
103
+        //create list of files to delete
104
+        $toDelete = array();
105
+        foreach ($whitelistFolderContents as $file){
106
+            if (!in_array($file, array('','..','.','.htaccess'))) {    //exclude things that should stay in the folder
107
+                if (!in_array($file, $whitelist)) {
108
+                    array_push($toDelete, $file);
109
+                }
110
+            }
111
+        }
112
+
113
+        //delete files which are no longer necessary
114
+        foreach ($toDelete as $delete) {
115
+            unlink($dir . DIRECTORY_SEPARATOR . $delete);
116
+        }
117
+
118
+        //create new whitelist items as files
119
+        foreach ($toCreate as $create) {
120
+            touch($dir . DIRECTORY_SEPARATOR . $create);
121
+        }
122
+    }
123
+
124
+    /**
125
+     * Does a database query searching through past URLs of top-level pages, returning any URLs previously used for
126
+     * pages in the SiteTree. This is to ensure that OldPageRedirector rules still apply correctly. That is, to ensure
127
+     * that pages that have been renamed continue to redirect to their current versions, we add the pages' old URLs
128
+     * to the whitelist.
129
+     * @param $currentTopLevelPages
130
+     * @return array URLs of past top-level pages
131
+     */
132
+    protected static function find_old_top_level_pages($currentTopLevelPages){
133
+        $oldPageURLs = array();
134
+
135
+        $queryClass = 'SQLSelect';
136
+        if (!class_exists($queryClass) && class_exists('SQLQuery')){
137
+            $queryClass = 'SQLQuery';
138
+        }
139 139
 		
140
-		$query = new $queryClass(
141
-			'DISTINCT (URLSegment)',
142
-			'SiteTree_versions',
143
-			array(
144
-				'ParentID = 0',
145
-				'WasPublished = 1',
146
-				'URLSegment NOT IN (\''.implode("','",array_filter($currentTopLevelPages)).'\')'
147
-			)
148
-		);
149
-
150
-		$records = $query->execute();
151
-		if ($records) {
152
-			foreach($records as $record) {
153
-				array_push($oldPageURLs, $record['URLSegment']);
154
-			}
155
-		}
156
-
157
-		return $oldPageURLs;
158
-	}
140
+        $query = new $queryClass(
141
+            'DISTINCT (URLSegment)',
142
+            'SiteTree_versions',
143
+            array(
144
+                'ParentID = 0',
145
+                'WasPublished = 1',
146
+                'URLSegment NOT IN (\''.implode("','",array_filter($currentTopLevelPages)).'\')'
147
+            )
148
+        );
149
+
150
+        $records = $query->execute();
151
+        if ($records) {
152
+            foreach($records as $record) {
153
+                array_push($oldPageURLs, $record['URLSegment']);
154
+            }
155
+        }
156
+
157
+        return $oldPageURLs;
158
+    }
159 159
 
160 160
     public static function ensureWhitelistFolderExists(){
161
-		$dir = BASE_PATH . DIRECTORY_SEPARATOR . Config::inst()->get('WhitelistGenerator', 'dir');
162
-		if (!file_exists($dir)) {
163
-			mkdir($dir); //create a new whitelist dir
164
-			chmod($dir,0777);    //make sure it is readable by the web-server user
165
-			//create a htaccess file to ensure that the whitelist cache directory is not web-accessible
166
-			file_put_contents($dir.DIRECTORY_SEPARATOR.'.htaccess', "Deny from all\n");
167
-		}
168
-	}
169
-
170
-	public static function clearWhitelist(){
171
-		$dir = BASE_PATH . DIRECTORY_SEPARATOR . Config::inst()->get('WhitelistGenerator', 'dir');
172
-		if (file_exists($dir)) {
173
-			array_map('unlink', glob($dir."/*"));
174
-		}
175
-	}
176
-
177
-	public static function flush() {
178
-		self::ensureWhitelistFolderExists();    //only create folder on flush, not on sitetree changes
179
-		self::generateWhitelist();
180
-	}
161
+        $dir = BASE_PATH . DIRECTORY_SEPARATOR . Config::inst()->get('WhitelistGenerator', 'dir');
162
+        if (!file_exists($dir)) {
163
+            mkdir($dir); //create a new whitelist dir
164
+            chmod($dir,0777);    //make sure it is readable by the web-server user
165
+            //create a htaccess file to ensure that the whitelist cache directory is not web-accessible
166
+            file_put_contents($dir.DIRECTORY_SEPARATOR.'.htaccess', "Deny from all\n");
167
+        }
168
+    }
169
+
170
+    public static function clearWhitelist(){
171
+        $dir = BASE_PATH . DIRECTORY_SEPARATOR . Config::inst()->get('WhitelistGenerator', 'dir');
172
+        if (file_exists($dir)) {
173
+            array_map('unlink', glob($dir."/*"));
174
+        }
175
+    }
176
+
177
+    public static function flush() {
178
+        self::ensureWhitelistFolderExists();    //only create folder on flush, not on sitetree changes
179
+        self::generateWhitelist();
180
+    }
181 181
 }
Please login to merge, or discard this patch.
Spacing   +24 added lines, -24 removed lines patch added patch discarded remove patch
@@ -1,12 +1,12 @@  discard block
 block discarded – undo
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
 
@@ -16,8 +16,8 @@  discard block
 block discarded – undo
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
 block discarded – undo
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
 
45 45
 					//special case for SiteTree, add all possible top Level Pages
46 46
 					$topLevelPages = SiteTree::get()->filter('ParentID', 0);
@@ -86,13 +86,13 @@  discard block
 block discarded – undo
86 86
 	 * Sync the list of all top-level routes with the file system whitelist cache
87 87
 	 */
88 88
 	protected static function syncCacheFilesystem($whitelist) {
89
-		$dir = BASE_PATH . DIRECTORY_SEPARATOR . Config::inst()->get('WhitelistGenerator', 'dir');
89
+		$dir = BASE_PATH.DIRECTORY_SEPARATOR.Config::inst()->get('WhitelistGenerator', 'dir');
90 90
 
91 91
 		$whitelistFolderContents = scandir($dir);
92 92
 
93 93
 		//create list of files to create
94 94
 		$toCreate = array();
95
-		foreach ($whitelist as $listItem){
95
+		foreach ($whitelist as $listItem) {
96 96
 			if (!in_array($listItem, $whitelistFolderContents)) {
97 97
 				if (!empty($listItem)) {    //don't include empty files, such as the file for /
98 98
 					array_push($toCreate, $listItem);
@@ -102,8 +102,8 @@  discard block
 block discarded – undo
102 102
 
103 103
 		//create list of files to delete
104 104
 		$toDelete = array();
105
-		foreach ($whitelistFolderContents as $file){
106
-			if (!in_array($file, array('','..','.','.htaccess'))) {    //exclude things that should stay in the folder
105
+		foreach ($whitelistFolderContents as $file) {
106
+			if (!in_array($file, array('', '..', '.', '.htaccess'))) {    //exclude things that should stay in the folder
107 107
 				if (!in_array($file, $whitelist)) {
108 108
 					array_push($toDelete, $file);
109 109
 				}
@@ -112,12 +112,12 @@  discard block
 block discarded – undo
112 112
 
113 113
 		//delete files which are no longer necessary
114 114
 		foreach ($toDelete as $delete) {
115
-			unlink($dir . DIRECTORY_SEPARATOR . $delete);
115
+			unlink($dir.DIRECTORY_SEPARATOR.$delete);
116 116
 		}
117 117
 
118 118
 		//create new whitelist items as files
119 119
 		foreach ($toCreate as $create) {
120
-			touch($dir . DIRECTORY_SEPARATOR . $create);
120
+			touch($dir.DIRECTORY_SEPARATOR.$create);
121 121
 		}
122 122
 	}
123 123
 
@@ -129,11 +129,11 @@  discard block
 block discarded – undo
129 129
 	 * @param $currentTopLevelPages
130 130
 	 * @return array URLs of past top-level pages
131 131
 	 */
132
-	protected static function find_old_top_level_pages($currentTopLevelPages){
132
+	protected static function find_old_top_level_pages($currentTopLevelPages) {
133 133
 		$oldPageURLs = array();
134 134
 
135 135
 		$queryClass = 'SQLSelect';
136
-		if (!class_exists($queryClass) && class_exists('SQLQuery')){
136
+		if (!class_exists($queryClass) && class_exists('SQLQuery')) {
137 137
 			$queryClass = 'SQLQuery';
138 138
 		}
139 139
 		
@@ -143,13 +143,13 @@  discard block
 block discarded – undo
143 143
 			array(
144 144
 				'ParentID = 0',
145 145
 				'WasPublished = 1',
146
-				'URLSegment NOT IN (\''.implode("','",array_filter($currentTopLevelPages)).'\')'
146
+				'URLSegment NOT IN (\''.implode("','", array_filter($currentTopLevelPages)).'\')'
147 147
 			)
148 148
 		);
149 149
 
150 150
 		$records = $query->execute();
151 151
 		if ($records) {
152
-			foreach($records as $record) {
152
+			foreach ($records as $record) {
153 153
 				array_push($oldPageURLs, $record['URLSegment']);
154 154
 			}
155 155
 		}
@@ -157,25 +157,25 @@  discard block
 block discarded – undo
157 157
 		return $oldPageURLs;
158 158
 	}
159 159
 
160
-    public static function ensureWhitelistFolderExists(){
161
-		$dir = BASE_PATH . DIRECTORY_SEPARATOR . Config::inst()->get('WhitelistGenerator', 'dir');
160
+    public static function ensureWhitelistFolderExists() {
161
+		$dir = BASE_PATH.DIRECTORY_SEPARATOR.Config::inst()->get('WhitelistGenerator', 'dir');
162 162
 		if (!file_exists($dir)) {
163 163
 			mkdir($dir); //create a new whitelist dir
164
-			chmod($dir,0777);    //make sure it is readable by the web-server user
164
+			chmod($dir, 0777); //make sure it is readable by the web-server user
165 165
 			//create a htaccess file to ensure that the whitelist cache directory is not web-accessible
166 166
 			file_put_contents($dir.DIRECTORY_SEPARATOR.'.htaccess', "Deny from all\n");
167 167
 		}
168 168
 	}
169 169
 
170
-	public static function clearWhitelist(){
171
-		$dir = BASE_PATH . DIRECTORY_SEPARATOR . Config::inst()->get('WhitelistGenerator', 'dir');
170
+	public static function clearWhitelist() {
171
+		$dir = BASE_PATH.DIRECTORY_SEPARATOR.Config::inst()->get('WhitelistGenerator', 'dir');
172 172
 		if (file_exists($dir)) {
173 173
 			array_map('unlink', glob($dir."/*"));
174 174
 		}
175 175
 	}
176 176
 
177 177
 	public static function flush() {
178
-		self::ensureWhitelistFolderExists();    //only create folder on flush, not on sitetree changes
178
+		self::ensureWhitelistFolderExists(); //only create folder on flush, not on sitetree changes
179 179
 		self::generateWhitelist();
180 180
 	}
181 181
 }
Please login to merge, or discard this patch.