Passed
Branch develop (96efe9)
by Jens
02:38
created
src/templates/documents/function.renderDocument.php 1 patch
Indentation   +13 added lines, -13 removed lines patch added patch discarded remove patch
@@ -31,26 +31,26 @@
 block discarded – undo
31 31
   <div class="documentActions grid-box-2">
32 32
       <? if ($document->state == 'unpublished' || $document->unpublishedChanges) : ?>
33 33
           <? renderAction('Publish',
34
-              'publish',
35
-              $request::$subfolders . $cmsPrefix . '/documents/publish-document?slug=' . $slugPrefix . $document->slug,
36
-              'check'); ?>
34
+                'publish',
35
+                $request::$subfolders . $cmsPrefix . '/documents/publish-document?slug=' . $slugPrefix . $document->slug,
36
+                'check'); ?>
37 37
       <? endif ?>
38 38
       <? if ($document->state == 'published') : ?>
39 39
           <? renderAction('Unpublish',
40
-              'unpublish',
41
-              $request::$subfolders . $cmsPrefix . '/documents/unpublish-document?slug=' . $slugPrefix . $document->slug,
42
-              'times'); ?>
40
+                'unpublish',
41
+                $request::$subfolders . $cmsPrefix . '/documents/unpublish-document?slug=' . $slugPrefix . $document->slug,
42
+                'times'); ?>
43 43
       <? endif ?>
44 44
       <? renderAction('Edit',
45
-          '',
46
-          $request::$subfolders . $cmsPrefix . '/documents/edit-document?slug=' . $slugPrefix . $document->slug,
47
-          'pencil'); ?>
45
+            '',
46
+            $request::$subfolders . $cmsPrefix . '/documents/edit-document?slug=' . $slugPrefix . $document->slug,
47
+            'pencil'); ?>
48 48
       <? if ($document->state == 'unpublished') : ?>
49 49
           <? renderAction('Delete',
50
-              'error',
51
-              $request::$subfolders . $cmsPrefix . '/documents/delete-document?slug=' . $slugPrefix . $document->slug,
52
-              'trash',
53
-              'return confirm(\'Are you sure you want to delete this document?\');'); ?>
50
+                'error',
51
+                $request::$subfolders . $cmsPrefix . '/documents/delete-document?slug=' . $slugPrefix . $document->slug,
52
+                'trash',
53
+                'return confirm(\'Are you sure you want to delete this document?\');'); ?>
54 54
       <? endif ?>
55 55
   </div>
56 56
 <? } ?>
Please login to merge, or discard this patch.
src/templates/documents/function.renderFolder.php 1 patch
Indentation   +7 added lines, -7 removed lines patch added patch discarded remove patch
@@ -17,14 +17,14 @@
 block discarded – undo
17 17
   </div>
18 18
   <div class="documentActions grid-box-4">
19 19
       <? renderAction('Edit',
20
-          '',
21
-          $request::$subfolders . $cmsPrefix . '/documents/edit-folder?slug=' . $slugPrefix . $document->slug,
22
-          'pencil'); ?>
20
+            '',
21
+            $request::$subfolders . $cmsPrefix . '/documents/edit-folder?slug=' . $slugPrefix . $document->slug,
22
+            'pencil'); ?>
23 23
       <? renderAction('Delete',
24
-          'error',
25
-          $request::$subfolders . $cmsPrefix . '/documents/delete-folder?slug=' . $slugPrefix . $document->slug,
26
-          'trash',
27
-          'return confirm(\'Are you sure you want to delete this document?\');'); ?>
24
+            'error',
25
+            $request::$subfolders . $cmsPrefix . '/documents/delete-folder?slug=' . $slugPrefix . $document->slug,
26
+            'trash',
27
+            'return confirm(\'Are you sure you want to delete this document?\');'); ?>
28 28
   </div>
29 29
   <ul class="documents grid-wrapper nested<?= $root ? ' root' : '' ?>">
30 30
       <? foreach ($document->getContent() as $subDocument) : ?>
Please login to merge, or discard this patch.
src/templates/documents/document-form-form.php 1 patch
Indentation   +37 added lines, -37 removed lines patch added patch discarded remove patch
@@ -22,11 +22,11 @@  discard block
 block discarded – undo
22 22
 				<div class="form-element">
23 23
 					<label for="<?=$field->slug?>"><?=$field->title?></label>
24 24
 					<? if (isset($document)) :
25
-						$fieldSlug = $field->slug;
26
-						$value = isset($document->fields->$fieldSlug) ? current($document->fields->$fieldSlug) : '';
27
-					else :
28
-						$value = '';
29
-					endif ?>
25
+                        $fieldSlug = $field->slug;
26
+                        $value = isset($document->fields->$fieldSlug) ? current($document->fields->$fieldSlug) : '';
27
+                    else :
28
+                        $value = '';
29
+                    endif ?>
30 30
 					<? if ($field->multiple == true && $field->type != 'Rich Text') : ?>
31 31
 					<ul class="grid-wrapper sortable">
32 32
 						<li class="grid-container">
@@ -54,10 +54,10 @@  discard block
 block discarded – undo
54 54
 							</div>
55 55
 						</li>
56 56
 						<? if (isset($document)) :
57
-							$fieldSlug = $field->slug;
58
-							$iterable = isset($document->fields->$fieldSlug) ? $document->fields->$fieldSlug : array();
59
-							array_shift($iterable);
60
-							?>
57
+                            $fieldSlug = $field->slug;
58
+                            $iterable = isset($document->fields->$fieldSlug) ? $document->fields->$fieldSlug : array();
59
+                            array_shift($iterable);
60
+                            ?>
61 61
 							<? foreach ($iterable as $value) : ?>
62 62
 
63 63
 							<li class="grid-container">
@@ -80,10 +80,10 @@  discard block
 block discarded – undo
80 80
 					<a class="btn js-addmultiple">+</a>
81 81
 					<? elseif ($field->multiple == true) : ?>
82 82
 						<? if (isset($document)) :
83
-							$fieldSlug = $field->slug;
84
-							$iterable = isset($document->fields->$fieldSlug) ? $document->fields->$fieldSlug : array();
85
-							array_shift($iterable);
86
-							?>
83
+                            $fieldSlug = $field->slug;
84
+                            $iterable = isset($document->fields->$fieldSlug) ? $document->fields->$fieldSlug : array();
85
+                            array_shift($iterable);
86
+                            ?>
87 87
 							<? foreach ($iterable as $value) : ?>
88 88
 
89 89
 							<li>
@@ -118,10 +118,10 @@  discard block
 block discarded – undo
118 118
 							<? foreach ($document->bricks as $currentBrickSlug => $brickArray) : ?>
119 119
 								<? foreach ($brickArray as $dynamicBrick) : ?>
120 120
 									<? foreach ($bricks as $brick) :
121
-										if (is_object($dynamicBrick) && isset($dynamicBrick->type) && $brick->slug === $dynamicBrick->type && $currentBrickSlug === $myBrickSlug) {
122
-											break;
123
-										}
124
-									endforeach ?>
121
+                                        if (is_object($dynamicBrick) && isset($dynamicBrick->type) && $brick->slug === $dynamicBrick->type && $currentBrickSlug === $myBrickSlug) {
122
+                                            break;
123
+                                        }
124
+                                    endforeach ?>
125 125
 									<? if (is_object($dynamicBrick) && isset($dynamicBrick->type) && $brick->slug === $dynamicBrick->type && $currentBrickSlug === $myBrickSlug) : ?>
126 126
 									<li class="brick form-element">
127 127
 
@@ -142,12 +142,12 @@  discard block
 block discarded – undo
142 142
 						<div class="form-element">
143 143
 						<label for="<?=$field->slug?>"><?=$field->title?></label>
144 144
 						<? if (isset($document)) :
145
-							$brickSlug = $brick->slug;
146
-							$fieldSlug = $field->slug;
147
-							$value = isset($document->bricks->$brickSlug->fields->$fieldSlug) ? current($document->bricks->$brickSlug->fields->$fieldSlug) : '';
148
-						else :
149
-							$value = '';
150
-						endif ?>
145
+                            $brickSlug = $brick->slug;
146
+                            $fieldSlug = $field->slug;
147
+                            $value = isset($document->bricks->$brickSlug->fields->$fieldSlug) ? current($document->bricks->$brickSlug->fields->$fieldSlug) : '';
148
+                        else :
149
+                            $value = '';
150
+                        endif ?>
151 151
 						<? if ($field->multiple == true && $field->type != 'Rich Text') : ?>
152 152
 						<ul class="grid-wrapper sortable">
153 153
 							<li class="grid-container">
@@ -175,11 +175,11 @@  discard block
 block discarded – undo
175 175
 								</div>
176 176
 							</li>
177 177
 							<? if (isset($document)) :
178
-								$brickSlug = $brick->slug;
179
-								$fieldSlug = $field->slug;
180
-								$iterable = isset($document->bricks->$brickSlug->fields->$fieldSlug) ? $document->bricks->$brickSlug->fields->$fieldSlug : array();
181
-								array_shift($iterable);
182
-								?>
178
+                                $brickSlug = $brick->slug;
179
+                                $fieldSlug = $field->slug;
180
+                                $iterable = isset($document->bricks->$brickSlug->fields->$fieldSlug) ? $document->bricks->$brickSlug->fields->$fieldSlug : array();
181
+                                array_shift($iterable);
182
+                                ?>
183 183
 								<? foreach ($iterable as $value) : ?>
184 184
 
185 185
 								<li class="grid-container">
@@ -202,11 +202,11 @@  discard block
 block discarded – undo
202 202
 						<a class="btn js-addmultiple">+</a>
203 203
 						<? elseif ($field->multiple == true) : ?>
204 204
 							<? if (isset($document)) :
205
-								$brickSlug = $brick->slug;
206
-								$fieldSlug = $field->slug;
207
-								$iterable = isset($document->bricks->$brickSlug->fields->$fieldSlug) ? $document->bricks->$brickSlug->fields->$fieldSlug : array();
208
-								array_shift($iterable);
209
-								?>
205
+                                $brickSlug = $brick->slug;
206
+                                $fieldSlug = $field->slug;
207
+                                $iterable = isset($document->bricks->$brickSlug->fields->$fieldSlug) ? $document->bricks->$brickSlug->fields->$fieldSlug : array();
208
+                                array_shift($iterable);
209
+                                ?>
210 210
 								<? foreach ($iterable as $value) : ?>
211 211
 
212 212
 								<li>
@@ -246,10 +246,10 @@  discard block
 block discarded – undo
246 246
 				<? if (isset($document)) : ?>
247 247
 					<? foreach ($document->dynamicBricks as $dynamicBrick) : ?>
248 248
 						<? foreach ($bricks as $brick) :
249
-						if ($brick->slug == $dynamicBrick->type) {
250
-							break;
251
-						}
252
-						endforeach ?>
249
+                        if ($brick->slug == $dynamicBrick->type) {
250
+                            break;
251
+                        }
252
+                        endforeach ?>
253 253
 						<li class="brick form-element">
254 254
 							<label><?=$brick->title?></label>
255 255
 							<?$static = false; ?>
Please login to merge, or discard this patch.
src/cc/StringUtil.php 1 patch
Indentation   +119 added lines, -119 removed lines patch added patch discarded remove patch
@@ -8,123 +8,123 @@
 block discarded – undo
8 8
 
9 9
 class StringUtil
10 10
 {
11
-	/**
12
-	 * Convert a string to url friendly slug
13
-	 *
14
-	 * @param string $str
15
-	 * @param array  $replace
16
-	 * @param string $delimiter
17
-	 *
18
-	 * @return mixed|string
19
-	 */
20
-	public static function slugify($str, $replace = array(), $delimiter = '-')
21
-	{
22
-		if (!empty($replace)) {
23
-			$str = str_replace((array)$replace, ' ', $str);
24
-		}
25
-
26
-		$clean = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $str);
27
-		$clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean);
28
-		$clean = strtolower(trim($clean, '-'));
29
-		$clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean);
30
-
31
-		return $clean;
32
-	}
33
-
34
-	/**
35
-	 * Selects the right font-awesome icon for each filetype
36
-	 *
37
-	 * @param $fileType
38
-	 *
39
-	 * @return string
40
-	 */
41
-	public static function iconByFileType($fileType)
42
-	{
43
-		$fileTypeIcons = array(
44
-			'image'                                                                   => 'file-image-o',
45
-			'pdf'                                                                     => 'file-pdf-o',
46
-			'audio'                                                                   => 'file-audio-o',
47
-			'x-msdownload'                                                            => 'windows',
48
-			'application/vnd.ms-excel'                                                => 'file-excel-o',
49
-			'application/msexcel'                                                     => 'file-excel-o',
50
-			'application/xls'                                                         => 'file-excel-o',
51
-			'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'       => 'file-excel-o',
52
-			'application/vnd.google-apps.spreadsheet'                                 => 'file-excel-o',
53
-			'application/msword'                                                      => 'file-word-o',
54
-			'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => 'file-word-o',
55
-			'application/x-rar-compressed'                                            => 'file-archive-o',
56
-			'application/x-zip-compressed'                                            => 'file-archive-o',
57
-			'application/zip'                                                         => 'file-archive-o',
58
-			'text'                                                                    => 'file-text-o',
59
-		);
60
-
61
-		foreach ($fileTypeIcons as $needle => $icon) {
62
-			if (strpos($fileType, $needle) !== false) {
63
-				return $icon;
64
-			}
65
-		}
66
-
67
-		return 'file-o';
68
-	}
69
-
70
-	/**
71
-	 * Converts an amount of bytes to a human readable
72
-	 * format
73
-	 *
74
-	 * @param        $size
75
-	 * @param string $unit
76
-	 *
77
-	 * @return string
78
-	 */
79
-	public static function humanFileSize($size, $unit = "")
80
-	{
81
-		if ((!$unit && $size >= 1 << 30) || $unit == "GB")
82
-			return number_format($size / (1 << 30), 2) . "GB";
83
-		if ((!$unit && $size >= 1 << 20) || $unit == "MB")
84
-			return number_format($size / (1 << 20), 2) . "MB";
85
-		if ((!$unit && $size >= 1 << 10) || $unit == "KB")
86
-			return number_format($size / (1 << 10), 2) . "KB";
87
-
88
-		return number_format($size) . " bytes";
89
-	}
90
-
91
-	/**
92
-	 * @param $ptime
93
-	 *
94
-	 * @return string
95
-	 */
96
-	public static function timeElapsedString($ptime)
97
-	{
98
-		$etime = time() - $ptime;
99
-
100
-		if ($etime < 1) {
101
-			return '0 seconds';
102
-		}
103
-
104
-		$a = array(365 * 24 * 60 * 60 => 'year',
105
-				   30 * 24 * 60 * 60  => 'month',
106
-				   24 * 60 * 60       => 'day',
107
-				   60 * 60            => 'hour',
108
-				   60                 => 'minute',
109
-				   1                  => 'second'
110
-		);
111
-		$a_plural = array('year'   => 'years',
112
-						  'month'  => 'months',
113
-						  'day'    => 'days',
114
-						  'hour'   => 'hours',
115
-						  'minute' => 'minutes',
116
-						  'second' => 'seconds'
117
-		);
118
-
119
-		foreach ($a as $secs => $str) {
120
-			$d = $etime / $secs;
121
-			if ($d >= 1) {
122
-				$r = round($d);
123
-
124
-				return $r . ' ' . ($r > 1 ? $a_plural[$str] : $str) . ' ago';
125
-			}
126
-		}
127
-
128
-		return 0;
129
-	}
11
+    /**
12
+     * Convert a string to url friendly slug
13
+     *
14
+     * @param string $str
15
+     * @param array  $replace
16
+     * @param string $delimiter
17
+     *
18
+     * @return mixed|string
19
+     */
20
+    public static function slugify($str, $replace = array(), $delimiter = '-')
21
+    {
22
+        if (!empty($replace)) {
23
+            $str = str_replace((array)$replace, ' ', $str);
24
+        }
25
+
26
+        $clean = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $str);
27
+        $clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean);
28
+        $clean = strtolower(trim($clean, '-'));
29
+        $clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean);
30
+
31
+        return $clean;
32
+    }
33
+
34
+    /**
35
+     * Selects the right font-awesome icon for each filetype
36
+     *
37
+     * @param $fileType
38
+     *
39
+     * @return string
40
+     */
41
+    public static function iconByFileType($fileType)
42
+    {
43
+        $fileTypeIcons = array(
44
+            'image'                                                                   => 'file-image-o',
45
+            'pdf'                                                                     => 'file-pdf-o',
46
+            'audio'                                                                   => 'file-audio-o',
47
+            'x-msdownload'                                                            => 'windows',
48
+            'application/vnd.ms-excel'                                                => 'file-excel-o',
49
+            'application/msexcel'                                                     => 'file-excel-o',
50
+            'application/xls'                                                         => 'file-excel-o',
51
+            'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'       => 'file-excel-o',
52
+            'application/vnd.google-apps.spreadsheet'                                 => 'file-excel-o',
53
+            'application/msword'                                                      => 'file-word-o',
54
+            'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => 'file-word-o',
55
+            'application/x-rar-compressed'                                            => 'file-archive-o',
56
+            'application/x-zip-compressed'                                            => 'file-archive-o',
57
+            'application/zip'                                                         => 'file-archive-o',
58
+            'text'                                                                    => 'file-text-o',
59
+        );
60
+
61
+        foreach ($fileTypeIcons as $needle => $icon) {
62
+            if (strpos($fileType, $needle) !== false) {
63
+                return $icon;
64
+            }
65
+        }
66
+
67
+        return 'file-o';
68
+    }
69
+
70
+    /**
71
+     * Converts an amount of bytes to a human readable
72
+     * format
73
+     *
74
+     * @param        $size
75
+     * @param string $unit
76
+     *
77
+     * @return string
78
+     */
79
+    public static function humanFileSize($size, $unit = "")
80
+    {
81
+        if ((!$unit && $size >= 1 << 30) || $unit == "GB")
82
+            return number_format($size / (1 << 30), 2) . "GB";
83
+        if ((!$unit && $size >= 1 << 20) || $unit == "MB")
84
+            return number_format($size / (1 << 20), 2) . "MB";
85
+        if ((!$unit && $size >= 1 << 10) || $unit == "KB")
86
+            return number_format($size / (1 << 10), 2) . "KB";
87
+
88
+        return number_format($size) . " bytes";
89
+    }
90
+
91
+    /**
92
+     * @param $ptime
93
+     *
94
+     * @return string
95
+     */
96
+    public static function timeElapsedString($ptime)
97
+    {
98
+        $etime = time() - $ptime;
99
+
100
+        if ($etime < 1) {
101
+            return '0 seconds';
102
+        }
103
+
104
+        $a = array(365 * 24 * 60 * 60 => 'year',
105
+                    30 * 24 * 60 * 60  => 'month',
106
+                    24 * 60 * 60       => 'day',
107
+                    60 * 60            => 'hour',
108
+                    60                 => 'minute',
109
+                    1                  => 'second'
110
+        );
111
+        $a_plural = array('year'   => 'years',
112
+                            'month'  => 'months',
113
+                            'day'    => 'days',
114
+                            'hour'   => 'hours',
115
+                            'minute' => 'minutes',
116
+                            'second' => 'seconds'
117
+        );
118
+
119
+        foreach ($a as $secs => $str) {
120
+            $d = $etime / $secs;
121
+            if ($d >= 1) {
122
+                $r = round($d);
123
+
124
+                return $r . ' ' . ($r > 1 ? $a_plural[$str] : $str) . ' ago';
125
+            }
126
+        }
127
+
128
+        return 0;
129
+    }
130 130
 }
131 131
\ No newline at end of file
Please login to merge, or discard this patch.
src/cc/Request.php 1 patch
Indentation   +68 added lines, -68 removed lines patch added patch discarded remove patch
@@ -5,79 +5,79 @@
 block discarded – undo
5 5
      * Class Request
6 6
      * @package CloudControl\Cms\cc
7 7
      */
8
-	class Request {
8
+    class Request {
9 9
 
10
-		/**
11
-		 * @var string
12
-		 */
13
-		public static $subfolders;
14
-		/**
15
-		 * @var string
16
-		 */
17
-		public static $requestUri;
18
-		/**
19
-		 * @var string
20
-		 */
21
-		public static $relativeUri;
22
-		/**
23
-		 * @var string
24
-		 */
25
-		public static $queryString;
26
-		/**
27
-		 * @var array
28
-		 */
29
-		public static $requestParameters;
30
-		/**
31
-		 * @var array
32
-		 */
33
-		public static $post = array();
34
-		/**
35
-		 * @var array
36
-		 */
37
-		public static $get = array();
38
-		/**
39
-		 * @var array
40
-		 */
41
-		private $statics = array();
10
+        /**
11
+         * @var string
12
+         */
13
+        public static $subfolders;
14
+        /**
15
+         * @var string
16
+         */
17
+        public static $requestUri;
18
+        /**
19
+         * @var string
20
+         */
21
+        public static $relativeUri;
22
+        /**
23
+         * @var string
24
+         */
25
+        public static $queryString;
26
+        /**
27
+         * @var array
28
+         */
29
+        public static $requestParameters;
30
+        /**
31
+         * @var array
32
+         */
33
+        public static $post = array();
34
+        /**
35
+         * @var array
36
+         */
37
+        public static $get = array();
38
+        /**
39
+         * @var array
40
+         */
41
+        private $statics = array();
42 42
 
43
-		/**
44
-		 * Request constructor.
45
-		 */
46
-		public function __construct() 
47
-		{
43
+        /**
44
+         * Request constructor.
45
+         */
46
+        public function __construct() 
47
+        {
48 48
             $rootPath = str_replace('\\', '/', realpath(str_replace('\\', '/', dirname(__FILE__)) . '/../../') . '/');
49 49
 			
50
-			self::$subfolders = '/' . str_replace('//', '/', str_replace(str_replace('\\', '/', $_SERVER['DOCUMENT_ROOT']), "", $rootPath));
51
-			self::$subfolders = str_replace('//', '/', self::$subfolders);
52
-			if (PHP_SAPI === 'cli') {
53
-				global $argv;
54
-				array_shift($argv);
55
-				self::$queryString = '';
56
-				self::$requestUri = self::$subfolders . implode('/', $argv);
57
-			} else {
58
-				self::$requestUri = $_SERVER['REQUEST_URI'];
59
-				self::$queryString = $_SERVER['QUERY_STRING'];
60
-			}
61
-			if (self::$subfolders === '/') {
62
-				self::$relativeUri = str_replace('?' . self::$queryString, '', substr(self::$requestUri,1));
63
-			} else {
64
-				self::$relativeUri = str_replace('?' . self::$queryString, '', str_replace(self::$subfolders, '', self::$requestUri));
65
-			}
50
+            self::$subfolders = '/' . str_replace('//', '/', str_replace(str_replace('\\', '/', $_SERVER['DOCUMENT_ROOT']), "", $rootPath));
51
+            self::$subfolders = str_replace('//', '/', self::$subfolders);
52
+            if (PHP_SAPI === 'cli') {
53
+                global $argv;
54
+                array_shift($argv);
55
+                self::$queryString = '';
56
+                self::$requestUri = self::$subfolders . implode('/', $argv);
57
+            } else {
58
+                self::$requestUri = $_SERVER['REQUEST_URI'];
59
+                self::$queryString = $_SERVER['QUERY_STRING'];
60
+            }
61
+            if (self::$subfolders === '/') {
62
+                self::$relativeUri = str_replace('?' . self::$queryString, '', substr(self::$requestUri,1));
63
+            } else {
64
+                self::$relativeUri = str_replace('?' . self::$queryString, '', str_replace(self::$subfolders, '', self::$requestUri));
65
+            }
66 66
 
67
-			self::$requestParameters = explode('/', self::$relativeUri);
67
+            self::$requestParameters = explode('/', self::$relativeUri);
68 68
 			
69
-			self::$get = $_GET;
70
-			self::$post = $_POST;
69
+            self::$get = $_GET;
70
+            self::$post = $_POST;
71 71
 			
72
-			$this->statics = array(
73
-				'subfolders' => self::$subfolders,
74
-				'requestUri' => self::$requestUri,
75
-				'relativeUri' => self::$relativeUri,
76
-				'queryString' => self::$queryString,
77
-				'requestParameters' => self::$requestParameters,
78
-				'post' => self::$post,
79
-				'get' => self::$get
80
-			);
81
-		}
82
-	}
72
+            $this->statics = array(
73
+                'subfolders' => self::$subfolders,
74
+                'requestUri' => self::$requestUri,
75
+                'relativeUri' => self::$relativeUri,
76
+                'queryString' => self::$queryString,
77
+                'requestParameters' => self::$requestParameters,
78
+                'post' => self::$post,
79
+                'get' => self::$get
80
+            );
81
+        }
82
+    }
83 83
 }
84 84
\ No newline at end of file
Please login to merge, or discard this patch.
src/cc/errortemplates/errorviewcli.php 1 patch
Indentation   +6 added lines, -6 removed lines patch added patch discarded remove patch
@@ -16,7 +16,7 @@  discard block
 block discarded – undo
16 16
 
17 17
 <?php
18 18
 foreach($lines as $nr => $currentLine) {
19
-	echo ($nr == $line ? '* ' : '  ' ) . str_pad($nr, 3, "0", STR_PAD_LEFT) . ' ' . $currentLine;
19
+    echo ($nr == $line ? '* ' : '  ' ) . str_pad($nr, 3, "0", STR_PAD_LEFT) . ' ' . $currentLine;
20 20
 }
21 21
 ?>
22 22
 
@@ -26,10 +26,10 @@  discard block
 block discarded – undo
26 26
 
27 27
 <?php
28 28
 foreach($trace as $row) {
29
-	echo (isset($row['file']) ? basename($row['file']) : '') . ':'
30
-		. (isset($row['line']) ? $row['line'] : '') . "\t\t\t"
31
-		. (isset($row['class']) ? $row['class'] : ' ') . "\t\t\t"
32
-		. (isset($row['type']) ? $row['type'] : ' ') . "\t\t\t"
33
-		. (isset($row['function']) ? $row['function'] : ' ') . PHP_EOL;
29
+    echo (isset($row['file']) ? basename($row['file']) : '') . ':'
30
+        . (isset($row['line']) ? $row['line'] : '') . "\t\t\t"
31
+        . (isset($row['class']) ? $row['class'] : ' ') . "\t\t\t"
32
+        . (isset($row['type']) ? $row['type'] : ' ') . "\t\t\t"
33
+        . (isset($row['function']) ? $row['function'] : ' ') . PHP_EOL;
34 34
 }
35 35
 ?>
36 36
\ No newline at end of file
Please login to merge, or discard this patch.
src/components/CmsComponent.php 1 patch
Indentation   +344 added lines, -344 removed lines patch added patch discarded remove patch
@@ -11,51 +11,51 @@  discard block
 block discarded – undo
11 11
     use CloudControl\Cms\storage\Storage;
12 12
 
13 13
     class CmsComponent extends BaseComponent
14
-	{
14
+    {
15 15
         /**
16
-		 * @var \CloudControl\Cms\storage\Storage
17
-		 */
18
-		public $storage;
19
-
20
-		const INVALID_CREDENTIALS_MESSAGE = 'Invalid username / password combination';
21
-
22
-		const MAIN_NAV_CLASS = 'default';
23
-
24
-		const PARAMETER_APPLICATION_COMPONENT = 'applicationComponent';
25
-		const PARAMETER_APPLICATION_COMPONENTS = 'applicationComponents';
26
-		const PARAMETER_BLACKLIST_IPS = 'blacklistIps';
27
-		const PARAMETER_BODY = 'body';
28
-		const PARAMETER_BRICK = 'brick';
29
-		const PARAMETER_BRICKS = 'bricks';
30
-		const PARAMETER_CMS_PREFIX = 'cmsPrefix';
31
-		const PARAMETER_CONFIGURATION = 'configuration';
32
-		const PARAMETER_DOCUMENT = 'document';
33
-		const PARAMETER_DOCUMENTS = 'documents';
34
-		const PARAMETER_DOCUMENT_TYPE = 'documentType';
35
-		const PARAMETER_DOCUMENT_TYPES = 'documentTypes';
36
-		const PARAMETER_ERROR_MESSAGE = 'errorMsg';
37
-		const PARAMETER_FILES = 'files';
38
-		const PARAMETER_FOLDER = 'folder';
39
-		const PARAMETER_IMAGE = 'image';
40
-		const PARAMETER_IMAGES = 'images';
41
-		const PARAMETER_IMAGE_SET = 'imageSet';
42
-		const PARAMETER_MAIN_NAV_CLASS = 'mainNavClass';
43
-		const PARAMETER_MY_BRICK_SLUG = 'myBrickSlug';
16
+         * @var \CloudControl\Cms\storage\Storage
17
+         */
18
+        public $storage;
19
+
20
+        const INVALID_CREDENTIALS_MESSAGE = 'Invalid username / password combination';
21
+
22
+        const MAIN_NAV_CLASS = 'default';
23
+
24
+        const PARAMETER_APPLICATION_COMPONENT = 'applicationComponent';
25
+        const PARAMETER_APPLICATION_COMPONENTS = 'applicationComponents';
26
+        const PARAMETER_BLACKLIST_IPS = 'blacklistIps';
27
+        const PARAMETER_BODY = 'body';
28
+        const PARAMETER_BRICK = 'brick';
29
+        const PARAMETER_BRICKS = 'bricks';
30
+        const PARAMETER_CMS_PREFIX = 'cmsPrefix';
31
+        const PARAMETER_CONFIGURATION = 'configuration';
32
+        const PARAMETER_DOCUMENT = 'document';
33
+        const PARAMETER_DOCUMENTS = 'documents';
34
+        const PARAMETER_DOCUMENT_TYPE = 'documentType';
35
+        const PARAMETER_DOCUMENT_TYPES = 'documentTypes';
36
+        const PARAMETER_ERROR_MESSAGE = 'errorMsg';
37
+        const PARAMETER_FILES = 'files';
38
+        const PARAMETER_FOLDER = 'folder';
39
+        const PARAMETER_IMAGE = 'image';
40
+        const PARAMETER_IMAGES = 'images';
41
+        const PARAMETER_IMAGE_SET = 'imageSet';
42
+        const PARAMETER_MAIN_NAV_CLASS = 'mainNavClass';
43
+        const PARAMETER_MY_BRICK_SLUG = 'myBrickSlug';
44 44
         const PARAMETER_REDIRECT = 'redirect';
45 45
         const PARAMETER_REDIRECTS = 'redirects';
46
-		const PARAMETER_SEARCH = 'search';
47
-		const PARAMETER_SEARCH_LOG = "searchLog";
48
-		const PARAMETER_SEARCH_NEEDS_UPDATE = "searchNeedsUpdate";
49
-		const PARAMETER_SITEMAP = 'sitemap';
50
-		const PARAMETER_SITEMAP_ITEM = 'sitemapItem';
51
-		const PARAMETER_SMALLEST_IMAGE = 'smallestImage';
52
-		const PARAMETER_STATIC = 'static';
53
-		const PARAMETER_USER = 'user';
54
-		const PARAMETER_USERS = 'users';
55
-		const PARAMETER_USER_RIGHTS = 'userRights';
56
-		const PARAMETER_VALUELIST = "valuelist";
57
-		const PARAMETER_VALUELISTS = "valuelists";
58
-		const PARAMETER_WHITELIST_IPS = 'whitelistIps';
46
+        const PARAMETER_SEARCH = 'search';
47
+        const PARAMETER_SEARCH_LOG = "searchLog";
48
+        const PARAMETER_SEARCH_NEEDS_UPDATE = "searchNeedsUpdate";
49
+        const PARAMETER_SITEMAP = 'sitemap';
50
+        const PARAMETER_SITEMAP_ITEM = 'sitemapItem';
51
+        const PARAMETER_SMALLEST_IMAGE = 'smallestImage';
52
+        const PARAMETER_STATIC = 'static';
53
+        const PARAMETER_USER = 'user';
54
+        const PARAMETER_USERS = 'users';
55
+        const PARAMETER_USER_RIGHTS = 'userRights';
56
+        const PARAMETER_VALUELIST = "valuelist";
57
+        const PARAMETER_VALUELISTS = "valuelists";
58
+        const PARAMETER_WHITELIST_IPS = 'whitelistIps';
59 59
 
60 60
         const POST_PARAMETER_COMPONENT = 'component';
61 61
         const POST_PARAMETER_FROM_URL = "fromUrl";
@@ -66,308 +66,308 @@  discard block
 block discarded – undo
66 66
         const POST_PARAMETER_TO_URL = "toUrl";
67 67
         const POST_PARAMETER_USERNAME = 'username';
68 68
 
69
-		const GET_PARAMETER_PATH = 'path';
70
-		const GET_PARAMETER_SLUG = 'slug';
71
-
72
-		const FILES_PARAMETER_FILE = 'file';
73
-
74
-		const SESSION_PARAMETER_CLOUD_CONTROL = 'cloudcontrol';
75
-
76
-		const LOGIN_TEMPLATE_PATH = 'login';
77
-
78
-		const CONTENT_TYPE_APPLICATION_JSON = 'Content-type:application/json';
79
-
80
-		public $subTemplate = null;
81
-
82
-
83
-		/**
84
-		 * @param Storage $storage
85
-		 *
86
-		 * @return void
87
-		 */
88
-		public function run(Storage $storage)
89
-		{
90
-			$this->parameters[self::PARAMETER_MAIN_NAV_CLASS] = self::MAIN_NAV_CLASS;
91
-			$this->storage = $storage;
92
-
93
-			$remoteAddress = $_SERVER['REMOTE_ADDR'];
94
-			$this->checkWhiteList($remoteAddress);
95
-			$this->checkBlackList($remoteAddress);
96
-
97
-			$this->checkLogin();
98
-
99
-			$this->parameters[self::PARAMETER_USER_RIGHTS] = $_SESSION[self::SESSION_PARAMETER_CLOUD_CONTROL]->rights;
100
-
101
-			$this->routing();
102
-		}
103
-
104
-		/**
105
-		 * See if a user is logged or wants to log in and
106
-		 * takes appropriate actions.
107
-		 *
108
-		 * @throws \Exception
109
-		 */
110
-		protected function checkLogin()
111
-		{
112
-			$request = $this->request;
113
-
114
-			if (!isset($_SESSION[self::SESSION_PARAMETER_CLOUD_CONTROL])) {
115
-				if (isset($request::$post[self::POST_PARAMETER_USERNAME], $request::$post[self::POST_PARAMETER_PASSWORD])) {
116
-					$this->checkLoginAttempt($request);
117
-				} else {
118
-					$this->showLogin();
119
-				}
120
-			}
121
-		}
122
-
123
-		/**
124
-		 * Overrides normal behaviour and only renders the
125
-		 * login screen
126
-		 *
127
-		 * @throws \Exception
128
-		 */
129
-		protected function showLogin()
130
-		{
131
-			$loginTemplatePath = self::LOGIN_TEMPLATE_PATH;
132
-			$this->renderTemplate($loginTemplatePath);
133
-			ob_end_flush();
134
-			exit;
135
-		}
136
-
137
-		/**
138
-		 * As an exception, to keep the initial file structure simple
139
-		 * the cms implements it's own routing, apart from the regular sitemap functionality
140
-		 *
141
-		 * @throws \Exception
142
-		 */
143
-		protected function routing()
144
-		{
145
-			$relativeCmsUri = $this->getRelativeCmsUri($this->request);
146
-
147
-			$userRights = $_SESSION[self::SESSION_PARAMETER_CLOUD_CONTROL]->rights;
148
-
149
-			$this->dashboardRouting($relativeCmsUri);
150
-			$this->logOffRouting($this->request, $relativeCmsUri);
151
-			$this->apiRouting($relativeCmsUri);
152
-			$this->documentRouting($userRights, $relativeCmsUri);
153
-			$this->sitemapRouting($userRights, $relativeCmsUri);
154
-			$this->imageRouting($userRights, $relativeCmsUri);
155
-			$this->filesRouting($userRights, $relativeCmsUri);
156
-			$this->configurationRouting($userRights, $relativeCmsUri);
157
-			$this->searchRouting($userRights, $relativeCmsUri);
158
-
159
-			$this->renderBody();
160
-		}
161
-
162
-		/**
163
-		 * @param $remoteAddress
164
-		 *
165
-		 * @throws \Exception
166
-		 */
167
-		private function checkWhiteList($remoteAddress)
168
-		{
169
-			if (isset($this->parameters[self::PARAMETER_WHITELIST_IPS])) {
170
-				$whitelistIps = explode(',', $this->parameters[self::PARAMETER_WHITELIST_IPS]);
171
-				$whitelistIps = array_map("trim", $whitelistIps);
172
-				if (!in_array($remoteAddress, $whitelistIps)) {
173
-					throw new \Exception('Ip address ' . $remoteAddress . ' is not on whitelist');
174
-				}
175
-			}
176
-		}
177
-
178
-		/**
179
-		 * @param $remoteAddress
180
-		 *
181
-		 * @throws \Exception
182
-		 */
183
-		private function checkBlackList($remoteAddress)
184
-		{
185
-			if (isset($this->parameters[self::PARAMETER_BLACKLIST_IPS])) {
186
-				$blacklistIps = explode(',', $this->parameters[self::PARAMETER_BLACKLIST_IPS]);
187
-				$blacklistIps = array_map("trim", $blacklistIps);
188
-				if (in_array($remoteAddress, $blacklistIps)) {
189
-					throw new \Exception('Ip address ' . $remoteAddress . ' is on blacklist');
190
-				}
191
-			}
192
-		}
193
-
194
-		/**
195
-		 * @param $request
196
-		 *
197
-		 * @return mixed|string
198
-		 */
199
-		private function getRelativeCmsUri($request)
200
-		{
201
-			// TODO Use regex match parameter instead of calculating relative uri
202
-			$pos = strpos($request::$relativeUri, $this->parameters[self::PARAMETER_CMS_PREFIX]);
203
-			$relativeCmsUri = '/';
204
-			if ($pos !== false) {
205
-				$relativeCmsUri = substr_replace($request::$relativeUri, '', $pos, strlen($this->parameters[self::PARAMETER_CMS_PREFIX]));
206
-			}
207
-
208
-			return $relativeCmsUri;
209
-		}
210
-
211
-		/**
212
-		 * @param $relativeCmsUri
213
-		 */
214
-		private function apiRouting($relativeCmsUri)
215
-		{
216
-			if ($relativeCmsUri == '/images.json') {
217
-				header(self::CONTENT_TYPE_APPLICATION_JSON);
218
-				die(json_encode($this->storage->getImages()->getImages()));
219
-			} elseif ($relativeCmsUri == '/files.json') {
220
-				header(self::CONTENT_TYPE_APPLICATION_JSON);
221
-				die(json_encode($this->storage->getFiles()->getFiles()));
222
-			} elseif ($relativeCmsUri == '/documents.json') {
223
-				header(self::CONTENT_TYPE_APPLICATION_JSON);
224
-				die(json_encode($this->storage->getDocuments()->getDocuments()));
225
-			}
226
-		}
227
-
228
-		private function logOffRouting($request, $relativeCmsUri)
229
-		{
230
-			if ($relativeCmsUri == '/log-off') {
231
-				$_SESSION[self::SESSION_PARAMETER_CLOUD_CONTROL] = null;
232
-				unset($_SESSION[self::SESSION_PARAMETER_CLOUD_CONTROL]);
233
-				header('Location: ' . $request::$subfolders . $this->parameters[self::PARAMETER_CMS_PREFIX]);
234
-				exit;
235
-			}
236
-		}
237
-
238
-		public function setParameter($parameterName, $parameterValue)
239
-		{
240
-			$this->parameters[$parameterName] = $parameterValue;
241
-		}
242
-
243
-		public function getParameter($parameterName)
244
-		{
245
-			return $this->parameters[$parameterName];
246
-		}
247
-
248
-		/**
249
-		 * @param $relativeCmsUri
250
-		 */
251
-		protected function dashboardRouting($relativeCmsUri)
252
-		{
253
-			if ($relativeCmsUri == '' || $relativeCmsUri == '/') {
254
-				$this->subTemplate = 'dashboard';
255
-			}
256
-		}
257
-
258
-		/**
259
-		 * @param $userRights
260
-		 * @param $relativeCmsUri
261
-		 */
262
-		protected function documentRouting($userRights, $relativeCmsUri)
263
-		{
264
-			if (in_array(self::PARAMETER_DOCUMENTS, $userRights)) {
265
-				new DocumentRouting($this->request, $relativeCmsUri, $this);
266
-			}
267
-		}
268
-
269
-		/**
270
-		 * @param $userRights
271
-		 * @param $relativeCmsUri
272
-		 */
273
-		protected function sitemapRouting($userRights, $relativeCmsUri)
274
-		{
275
-			if (in_array(self::PARAMETER_SITEMAP, $userRights)) {
276
-				new SitemapRouting($this->request, $relativeCmsUri, $this);
277
-			}
278
-		}
279
-
280
-		/**
281
-		 * @param $userRights
282
-		 * @param $relativeCmsUri
283
-		 */
284
-		protected function imageRouting($userRights, $relativeCmsUri)
285
-		{
286
-			if (in_array(self::PARAMETER_IMAGES, $userRights)) {
287
-				new ImagesRouting($this->request, $relativeCmsUri, $this);
288
-			}
289
-		}
290
-
291
-		/**
292
-		 * @param $userRights
293
-		 * @param $relativeCmsUri
294
-		 */
295
-		protected function filesRouting($userRights, $relativeCmsUri)
296
-		{
297
-			if (in_array(self::PARAMETER_FILES, $userRights)) {
298
-				new FilesRouting($this->request, $relativeCmsUri, $this);
299
-			}
300
-		}
301
-
302
-		/**
303
-		 * @param $userRights
304
-		 * @param $relativeCmsUri
305
-		 */
306
-		protected function configurationRouting($userRights, $relativeCmsUri)
307
-		{
308
-			if (in_array('configuration', $userRights)) {
309
-				new ConfigurationRouting($this->request, $relativeCmsUri, $this);
310
-			}
311
-		}
312
-
313
-		protected function renderBody()
314
-		{
315
-			if ($this->subTemplate !== null) {
316
-				$this->parameters[self::PARAMETER_BODY] = $this->renderTemplate($this->subTemplate);
317
-			}
318
-		}
319
-
320
-		/**
321
-		 * @param $crypt
322
-		 * @param $request
323
-		 */
324
-		protected function invalidCredentials($crypt, $request)
325
-		{
326
-			$crypt->encrypt($request::$post[self::POST_PARAMETER_PASSWORD], 16); // Buy time, to avoid brute forcing
327
-			$this->parameters[self::PARAMETER_ERROR_MESSAGE] = self::INVALID_CREDENTIALS_MESSAGE;
328
-			$this->showLogin();
329
-		}
330
-
331
-		/**
332
-		 * @param $user
333
-		 * @param $crypt
334
-		 * @param $request
335
-		 */
336
-		protected function checkPassword($user, $crypt, $request)
337
-		{
338
-			$salt = $user->salt;
339
-			$password = $user->password;
340
-
341
-			$passwordCorrect = $crypt->compare($request::$post[self::POST_PARAMETER_PASSWORD], $password, $salt);
342
-
343
-			if ($passwordCorrect) {
344
-				$_SESSION[self::SESSION_PARAMETER_CLOUD_CONTROL] = $user;
345
-			} else {
346
-				$this->parameters[self::PARAMETER_ERROR_MESSAGE] = self::INVALID_CREDENTIALS_MESSAGE;
347
-				$this->showLogin();
348
-			}
349
-		}
350
-
351
-		/**
352
-		 * @param $request
353
-		 */
354
-		protected function checkLoginAttempt($request)
355
-		{
356
-			$user = $this->storage->getUsers()->getUserByUsername($request::$post[self::POST_PARAMETER_USERNAME]);
357
-			$crypt = new Crypt();
358
-			if (empty($user)) {
359
-				$this->invalidCredentials($crypt, $request);
360
-			} else {
361
-				$this->checkPassword($user, $crypt, $request);
362
-			}
363
-		}
364
-
365
-		private function searchRouting($userRights, $relativeCmsUri)
366
-		{
367
-			if (in_array(self::PARAMETER_SEARCH, $userRights)) {
368
-				new SearchRouting($this->request, $relativeCmsUri, $this);
369
-			}
370
-		}
69
+        const GET_PARAMETER_PATH = 'path';
70
+        const GET_PARAMETER_SLUG = 'slug';
71
+
72
+        const FILES_PARAMETER_FILE = 'file';
73
+
74
+        const SESSION_PARAMETER_CLOUD_CONTROL = 'cloudcontrol';
75
+
76
+        const LOGIN_TEMPLATE_PATH = 'login';
77
+
78
+        const CONTENT_TYPE_APPLICATION_JSON = 'Content-type:application/json';
79
+
80
+        public $subTemplate = null;
81
+
82
+
83
+        /**
84
+         * @param Storage $storage
85
+         *
86
+         * @return void
87
+         */
88
+        public function run(Storage $storage)
89
+        {
90
+            $this->parameters[self::PARAMETER_MAIN_NAV_CLASS] = self::MAIN_NAV_CLASS;
91
+            $this->storage = $storage;
92
+
93
+            $remoteAddress = $_SERVER['REMOTE_ADDR'];
94
+            $this->checkWhiteList($remoteAddress);
95
+            $this->checkBlackList($remoteAddress);
96
+
97
+            $this->checkLogin();
98
+
99
+            $this->parameters[self::PARAMETER_USER_RIGHTS] = $_SESSION[self::SESSION_PARAMETER_CLOUD_CONTROL]->rights;
100
+
101
+            $this->routing();
102
+        }
103
+
104
+        /**
105
+         * See if a user is logged or wants to log in and
106
+         * takes appropriate actions.
107
+         *
108
+         * @throws \Exception
109
+         */
110
+        protected function checkLogin()
111
+        {
112
+            $request = $this->request;
113
+
114
+            if (!isset($_SESSION[self::SESSION_PARAMETER_CLOUD_CONTROL])) {
115
+                if (isset($request::$post[self::POST_PARAMETER_USERNAME], $request::$post[self::POST_PARAMETER_PASSWORD])) {
116
+                    $this->checkLoginAttempt($request);
117
+                } else {
118
+                    $this->showLogin();
119
+                }
120
+            }
121
+        }
122
+
123
+        /**
124
+         * Overrides normal behaviour and only renders the
125
+         * login screen
126
+         *
127
+         * @throws \Exception
128
+         */
129
+        protected function showLogin()
130
+        {
131
+            $loginTemplatePath = self::LOGIN_TEMPLATE_PATH;
132
+            $this->renderTemplate($loginTemplatePath);
133
+            ob_end_flush();
134
+            exit;
135
+        }
136
+
137
+        /**
138
+         * As an exception, to keep the initial file structure simple
139
+         * the cms implements it's own routing, apart from the regular sitemap functionality
140
+         *
141
+         * @throws \Exception
142
+         */
143
+        protected function routing()
144
+        {
145
+            $relativeCmsUri = $this->getRelativeCmsUri($this->request);
146
+
147
+            $userRights = $_SESSION[self::SESSION_PARAMETER_CLOUD_CONTROL]->rights;
148
+
149
+            $this->dashboardRouting($relativeCmsUri);
150
+            $this->logOffRouting($this->request, $relativeCmsUri);
151
+            $this->apiRouting($relativeCmsUri);
152
+            $this->documentRouting($userRights, $relativeCmsUri);
153
+            $this->sitemapRouting($userRights, $relativeCmsUri);
154
+            $this->imageRouting($userRights, $relativeCmsUri);
155
+            $this->filesRouting($userRights, $relativeCmsUri);
156
+            $this->configurationRouting($userRights, $relativeCmsUri);
157
+            $this->searchRouting($userRights, $relativeCmsUri);
158
+
159
+            $this->renderBody();
160
+        }
161
+
162
+        /**
163
+         * @param $remoteAddress
164
+         *
165
+         * @throws \Exception
166
+         */
167
+        private function checkWhiteList($remoteAddress)
168
+        {
169
+            if (isset($this->parameters[self::PARAMETER_WHITELIST_IPS])) {
170
+                $whitelistIps = explode(',', $this->parameters[self::PARAMETER_WHITELIST_IPS]);
171
+                $whitelistIps = array_map("trim", $whitelistIps);
172
+                if (!in_array($remoteAddress, $whitelistIps)) {
173
+                    throw new \Exception('Ip address ' . $remoteAddress . ' is not on whitelist');
174
+                }
175
+            }
176
+        }
177
+
178
+        /**
179
+         * @param $remoteAddress
180
+         *
181
+         * @throws \Exception
182
+         */
183
+        private function checkBlackList($remoteAddress)
184
+        {
185
+            if (isset($this->parameters[self::PARAMETER_BLACKLIST_IPS])) {
186
+                $blacklistIps = explode(',', $this->parameters[self::PARAMETER_BLACKLIST_IPS]);
187
+                $blacklistIps = array_map("trim", $blacklistIps);
188
+                if (in_array($remoteAddress, $blacklistIps)) {
189
+                    throw new \Exception('Ip address ' . $remoteAddress . ' is on blacklist');
190
+                }
191
+            }
192
+        }
193
+
194
+        /**
195
+         * @param $request
196
+         *
197
+         * @return mixed|string
198
+         */
199
+        private function getRelativeCmsUri($request)
200
+        {
201
+            // TODO Use regex match parameter instead of calculating relative uri
202
+            $pos = strpos($request::$relativeUri, $this->parameters[self::PARAMETER_CMS_PREFIX]);
203
+            $relativeCmsUri = '/';
204
+            if ($pos !== false) {
205
+                $relativeCmsUri = substr_replace($request::$relativeUri, '', $pos, strlen($this->parameters[self::PARAMETER_CMS_PREFIX]));
206
+            }
207
+
208
+            return $relativeCmsUri;
209
+        }
210
+
211
+        /**
212
+         * @param $relativeCmsUri
213
+         */
214
+        private function apiRouting($relativeCmsUri)
215
+        {
216
+            if ($relativeCmsUri == '/images.json') {
217
+                header(self::CONTENT_TYPE_APPLICATION_JSON);
218
+                die(json_encode($this->storage->getImages()->getImages()));
219
+            } elseif ($relativeCmsUri == '/files.json') {
220
+                header(self::CONTENT_TYPE_APPLICATION_JSON);
221
+                die(json_encode($this->storage->getFiles()->getFiles()));
222
+            } elseif ($relativeCmsUri == '/documents.json') {
223
+                header(self::CONTENT_TYPE_APPLICATION_JSON);
224
+                die(json_encode($this->storage->getDocuments()->getDocuments()));
225
+            }
226
+        }
227
+
228
+        private function logOffRouting($request, $relativeCmsUri)
229
+        {
230
+            if ($relativeCmsUri == '/log-off') {
231
+                $_SESSION[self::SESSION_PARAMETER_CLOUD_CONTROL] = null;
232
+                unset($_SESSION[self::SESSION_PARAMETER_CLOUD_CONTROL]);
233
+                header('Location: ' . $request::$subfolders . $this->parameters[self::PARAMETER_CMS_PREFIX]);
234
+                exit;
235
+            }
236
+        }
237
+
238
+        public function setParameter($parameterName, $parameterValue)
239
+        {
240
+            $this->parameters[$parameterName] = $parameterValue;
241
+        }
242
+
243
+        public function getParameter($parameterName)
244
+        {
245
+            return $this->parameters[$parameterName];
246
+        }
247
+
248
+        /**
249
+         * @param $relativeCmsUri
250
+         */
251
+        protected function dashboardRouting($relativeCmsUri)
252
+        {
253
+            if ($relativeCmsUri == '' || $relativeCmsUri == '/') {
254
+                $this->subTemplate = 'dashboard';
255
+            }
256
+        }
257
+
258
+        /**
259
+         * @param $userRights
260
+         * @param $relativeCmsUri
261
+         */
262
+        protected function documentRouting($userRights, $relativeCmsUri)
263
+        {
264
+            if (in_array(self::PARAMETER_DOCUMENTS, $userRights)) {
265
+                new DocumentRouting($this->request, $relativeCmsUri, $this);
266
+            }
267
+        }
268
+
269
+        /**
270
+         * @param $userRights
271
+         * @param $relativeCmsUri
272
+         */
273
+        protected function sitemapRouting($userRights, $relativeCmsUri)
274
+        {
275
+            if (in_array(self::PARAMETER_SITEMAP, $userRights)) {
276
+                new SitemapRouting($this->request, $relativeCmsUri, $this);
277
+            }
278
+        }
279
+
280
+        /**
281
+         * @param $userRights
282
+         * @param $relativeCmsUri
283
+         */
284
+        protected function imageRouting($userRights, $relativeCmsUri)
285
+        {
286
+            if (in_array(self::PARAMETER_IMAGES, $userRights)) {
287
+                new ImagesRouting($this->request, $relativeCmsUri, $this);
288
+            }
289
+        }
290
+
291
+        /**
292
+         * @param $userRights
293
+         * @param $relativeCmsUri
294
+         */
295
+        protected function filesRouting($userRights, $relativeCmsUri)
296
+        {
297
+            if (in_array(self::PARAMETER_FILES, $userRights)) {
298
+                new FilesRouting($this->request, $relativeCmsUri, $this);
299
+            }
300
+        }
301
+
302
+        /**
303
+         * @param $userRights
304
+         * @param $relativeCmsUri
305
+         */
306
+        protected function configurationRouting($userRights, $relativeCmsUri)
307
+        {
308
+            if (in_array('configuration', $userRights)) {
309
+                new ConfigurationRouting($this->request, $relativeCmsUri, $this);
310
+            }
311
+        }
312
+
313
+        protected function renderBody()
314
+        {
315
+            if ($this->subTemplate !== null) {
316
+                $this->parameters[self::PARAMETER_BODY] = $this->renderTemplate($this->subTemplate);
317
+            }
318
+        }
319
+
320
+        /**
321
+         * @param $crypt
322
+         * @param $request
323
+         */
324
+        protected function invalidCredentials($crypt, $request)
325
+        {
326
+            $crypt->encrypt($request::$post[self::POST_PARAMETER_PASSWORD], 16); // Buy time, to avoid brute forcing
327
+            $this->parameters[self::PARAMETER_ERROR_MESSAGE] = self::INVALID_CREDENTIALS_MESSAGE;
328
+            $this->showLogin();
329
+        }
330
+
331
+        /**
332
+         * @param $user
333
+         * @param $crypt
334
+         * @param $request
335
+         */
336
+        protected function checkPassword($user, $crypt, $request)
337
+        {
338
+            $salt = $user->salt;
339
+            $password = $user->password;
340
+
341
+            $passwordCorrect = $crypt->compare($request::$post[self::POST_PARAMETER_PASSWORD], $password, $salt);
342
+
343
+            if ($passwordCorrect) {
344
+                $_SESSION[self::SESSION_PARAMETER_CLOUD_CONTROL] = $user;
345
+            } else {
346
+                $this->parameters[self::PARAMETER_ERROR_MESSAGE] = self::INVALID_CREDENTIALS_MESSAGE;
347
+                $this->showLogin();
348
+            }
349
+        }
350
+
351
+        /**
352
+         * @param $request
353
+         */
354
+        protected function checkLoginAttempt($request)
355
+        {
356
+            $user = $this->storage->getUsers()->getUserByUsername($request::$post[self::POST_PARAMETER_USERNAME]);
357
+            $crypt = new Crypt();
358
+            if (empty($user)) {
359
+                $this->invalidCredentials($crypt, $request);
360
+            } else {
361
+                $this->checkPassword($user, $crypt, $request);
362
+            }
363
+        }
364
+
365
+        private function searchRouting($userRights, $relativeCmsUri)
366
+        {
367
+            if (in_array(self::PARAMETER_SEARCH, $userRights)) {
368
+                new SearchRouting($this->request, $relativeCmsUri, $this);
369
+            }
370
+        }
371 371
 
372 372
         protected function getTemplateDir($template, $application = null)
373 373
         {
Please login to merge, or discard this patch.
src/components/SearchComponent.php 1 patch
Indentation   +36 added lines, -36 removed lines patch added patch discarded remove patch
@@ -14,41 +14,41 @@
 block discarded – undo
14 14
 
15 15
 class SearchComponent extends BaseComponent
16 16
 {
17
-	protected $searchParameterName = 'q';
18
-	protected $searchResultsParameterName = 'searchResults';
19
-
20
-	/**
21
-	 * @param \CloudControl\Cms\storage\Storage $storage
22
-	 */
23
-	public function run(Storage $storage)
24
-	{
25
-		parent::run($storage);
26
-
27
-		$this->checkParameters();
28
-
29
-		$request = $this->request;
30
-		if (isset($request::$get[$this->searchParameterName])) {
31
-			$query = $request::$get[$this->searchParameterName];
32
-			$filteredQuery = new CharacterFilter($query);
33
-			$tokenizer = new Tokenizer($filteredQuery);
34
-			$search = new Search($storage);
35
-			$results = $search->getDocumentsForTokenizer($tokenizer);
36
-			$this->parameters[$this->searchResultsParameterName] = $results;
37
-		}
38
-	}
39
-
40
-	/**
41
-	 * Checks to see if any parameters were defined in the cms and acts according
42
-	 */
43
-	private function checkParameters()
44
-	{
45
-		if (isset($this->parameters['searchParameterName'])) {
46
-			$this->searchParameterName = $this->parameters['searchParameterName'];
47
-		}
48
-
49
-		if (isset($this->parameters['searchResultsParameterName'])) {
50
-			$this->searchParameterName = $this->parameters['searchResultsParameterName'];
51
-		}
52
-	}
17
+    protected $searchParameterName = 'q';
18
+    protected $searchResultsParameterName = 'searchResults';
19
+
20
+    /**
21
+     * @param \CloudControl\Cms\storage\Storage $storage
22
+     */
23
+    public function run(Storage $storage)
24
+    {
25
+        parent::run($storage);
26
+
27
+        $this->checkParameters();
28
+
29
+        $request = $this->request;
30
+        if (isset($request::$get[$this->searchParameterName])) {
31
+            $query = $request::$get[$this->searchParameterName];
32
+            $filteredQuery = new CharacterFilter($query);
33
+            $tokenizer = new Tokenizer($filteredQuery);
34
+            $search = new Search($storage);
35
+            $results = $search->getDocumentsForTokenizer($tokenizer);
36
+            $this->parameters[$this->searchResultsParameterName] = $results;
37
+        }
38
+    }
39
+
40
+    /**
41
+     * Checks to see if any parameters were defined in the cms and acts according
42
+     */
43
+    private function checkParameters()
44
+    {
45
+        if (isset($this->parameters['searchParameterName'])) {
46
+            $this->searchParameterName = $this->parameters['searchParameterName'];
47
+        }
48
+
49
+        if (isset($this->parameters['searchResultsParameterName'])) {
50
+            $this->searchParameterName = $this->parameters['searchResultsParameterName'];
51
+        }
52
+    }
53 53
 
54 54
 }
55 55
\ No newline at end of file
Please login to merge, or discard this patch.
src/components/FormComponent.php 1 patch
Indentation   +320 added lines, -320 removed lines patch added patch discarded remove patch
@@ -7,324 +7,324 @@
 block discarded – undo
7 7
 
8 8
 class FormComponent Extends BaseComponent
9 9
 {
10
-	const GET_PARAMETER_PATH = 'path';
11
-
12
-	const PARAMETER_CMS_PREFIX = 'cmsPrefix';
13
-	const PARAMETER_DOCUMENT_TYPE = 'documentType';
14
-	const PARAMETER_DOCUMENT_TYPES = 'documentTypes';
15
-	const PARAMETER_FORM_ID = 'formId';
16
-	const PARAMETER_FORM_PARAMETER_NAME = 'formParameterName';
17
-	const PARAMETER_HIDE_TITLE_AND_STATE = 'hideTitleAndState';
18
-	const PARAMETER_RESPONSE_FOLDER = 'responseFolder';
19
-	const PARAMETER_SMALLEST_IMAGE = 'smallestImage';
20
-	const PARAMETER_SUBMIT_ONCE_PER_SESSION = 'submitOncePerSession';
21
-	const PARAMETER_SUB_TEMPLATE = 'subTemplate';
22
-	const PARAMETER_THANK_YOU_MESSAGE = 'thankYouMessage';
23
-
24
-	const SESSION_PARAMETER_CLOUDCONTROL = 'cloudcontrol';
25
-	const SESSION_PARAMETER_FORM_COMPONENT = 'FormComponent';
26
-	/**
27
-	 * @var null|string
28
-	 */
29
-	protected $documentType = null;
30
-	/**
31
-	 * @var null|string
32
-	 */
33
-	protected $responseFolder = null;
34
-	/**
35
-	 * @var string
36
-	 */
37
-	protected $subTemplate = 'cms/documents/document-form-form';
38
-	/**
39
-	 * @var string
40
-	 */
41
-	protected $formParameterName = 'form';
42
-	/**
43
-	 * @var string
44
-	 */
45
-	protected $thankYouMessage = 'Thank you for sending us your response.';
46
-
47
-	/**
48
-	 * @var bool
49
-	 */
50
-	protected $submitOncePerSession = false;
51
-
52
-	/**
53
-	 * @var string
54
-	 */
55
-	private $formId;
56
-	/**
57
-	 * @var null|string
58
-	 */
59
-	private $getPathBackup = null;
60
-
61
-	/**
62
-	 * @var null|\stdClass
63
-	 */
64
-	private $userSessionBackup = null;
65
-
66
-	/**
67
-	 * @param Storage $storage
68
-	 *
69
-	 * @return void
70
-	 * @throws \Exception
71
-	 */
72
-	public function run(Storage $storage)
73
-	{
74
-		parent::run($storage);
75
-		$this->checkParameters();
76
-		$this->checkRequiredParameters();
77
-		$this->setFormId();
78
-		$this->initialize($storage);
79
-		$this->checkSubmit($storage);
80
-	}
81
-
82
-	/**
83
-	 * @param null|Application $application
84
-	 *
85
-	 * @throws \Exception
86
-	 */
87
-	public function render($application = null)
88
-	{
89
-		$request = $this->setPathBackup();
90
-		$form = $this->renderTemplate($this->subTemplate);
91
-		$this->resetPathBackup($request);
92
-		$this->setFormParameter($form);
93
-
94
-		parent::render($application);
95
-	}
96
-
97
-	/**
98
-	 * Checks if parameters were given in the CMS configuration and
99
-	 * sets them to their respective fields
100
-	 */
101
-	private function checkParameters()
102
-	{
103
-		$this->checkDocumentTypeParameter();
104
-		$this->checkResponseFolderParameter();
105
-		$this->checkSubTemplateParameter();
106
-		$this->checkFormParameterNameParameter();
107
-		$this->checkThankYouMessageParameter();
108
-		$this->checkSubmitOncePerSessionParameter();
109
-	}
110
-
111
-	/**
112
-	 * Sets variables needed for rendering the form template
113
-	 *
114
-	 * @param Storage $storage
115
-	 */
116
-	private function initialize($storage)
117
-	{
118
-		$this->parameters[self::PARAMETER_SMALLEST_IMAGE] = $storage->getImageSet()->getSmallestImageSet();
119
-		$this->parameters[self::PARAMETER_CMS_PREFIX] = '';
120
-
121
-		$this->parameters[self::PARAMETER_DOCUMENT_TYPE] = $this->storage->getDocumentTypes()->getDocumentTypeBySlug($this->documentType, true);
122
-		$this->parameters[self::PARAMETER_DOCUMENT_TYPES] = $this->storage->getDocumentTypes()->getDocumentTypes();
123
-		$this->parameters[self::PARAMETER_HIDE_TITLE_AND_STATE] = true;
124
-		$this->parameters[self::PARAMETER_FORM_ID] = $this->formId;
125
-	}
126
-
127
-	/**
128
-	 * If the form has been submitted, save the document
129
-	 * Calls $this->postSubmit() afterwards
130
-	 *
131
-	 * @param Storage $storage
132
-	 */
133
-	private function checkSubmit($storage)
134
-	{
135
-		if ($this->isFormSubmitted($this->request) && $this->isSubmitAllowed()) {
136
-			$postValues = $this->getPostValues($this->request);
137
-			$this->setUserSessionBackup();
138
-			$storage->getDocuments()->addDocument($postValues);
139
-			$this->restoreUserSessionBackup();
140
-			$this->setSubmitToSession();
141
-			$this->postSubmit($postValues, $storage);
142
-		}
143
-	}
144
-
145
-	/**
146
-	 * Hook for derived classes to take actions after
147
-	 * submitting the form
148
-	 *
149
-	 * @param $postValues
150
-	 * @param $storage
151
-	 */
152
-	protected function postSubmit($postValues, $storage)
153
-	{}
154
-
155
-	/**
156
-	 * Sets a unique id for this particular form, so it can recognize
157
-	 * it when a submit occurs
158
-	 */
159
-	private function setFormId()
160
-	{
161
-		if (isset($_SESSION[self::SESSION_PARAMETER_FORM_COMPONENT][$this->formParameterName][self::PARAMETER_FORM_ID])) {
162
-			$this->formId = $_SESSION[self::SESSION_PARAMETER_FORM_COMPONENT][$this->formParameterName][self::PARAMETER_FORM_ID];
163
-		} else {
164
-			$_SESSION[self::SESSION_PARAMETER_FORM_COMPONENT][$this->formParameterName][self::PARAMETER_FORM_ID] = (string)microtime(true);
165
-			$_SESSION[self::SESSION_PARAMETER_FORM_COMPONENT][$this->formParameterName]['submitted'] = false;
166
-			$this->formId = $_SESSION[self::SESSION_PARAMETER_FORM_COMPONENT][$this->formParameterName][self::PARAMETER_FORM_ID];
167
-		}
168
-	}
169
-
170
-	/**
171
-	 * Checks if this form has been submitted
172
-	 *
173
-	 * @param \CloudControl\Cms\cc\Request $request
174
-	 *
175
-	 * @return bool
176
-	 */
177
-	private function isFormSubmitted($request)
178
-	{
179
-		return !empty($request::$post) && isset($request::$post[self::PARAMETER_FORM_ID]) && $request::$post[self::PARAMETER_FORM_ID] === $this->formId && isset($_SESSION[self::SESSION_PARAMETER_FORM_COMPONENT][$this->formParameterName][self::PARAMETER_FORM_ID]) && $_SESSION[self::SESSION_PARAMETER_FORM_COMPONENT][$this->formParameterName][self::PARAMETER_FORM_ID] === $this->formId;
180
-	}
181
-
182
-	/**
183
-	 * @param \cc\Request $request
184
-	 * @return array
185
-	 */
186
-	private function getPostValues($request)
187
-	{
188
-		$postValues = $request::$post;
189
-		$postValues[self::PARAMETER_DOCUMENT_TYPE] = $this->documentType;
190
-		$postValues[self::GET_PARAMETER_PATH] = $this->responseFolder;
191
-		$postValues['title'] = date('r') . ' - From: ' . $request::$requestUri;
192
-
193
-		return $postValues;
194
-	}
195
-
196
-	/**
197
-	 * Temporarily stores the current user session in a backup variable
198
-	 * and sets a fake user instead
199
-	 */
200
-	private function setUserSessionBackup()
201
-	{
202
-		$this->userSessionBackup = isset($_SESSION[self::SESSION_PARAMETER_CLOUDCONTROL]) ? $_SESSION[self::SESSION_PARAMETER_CLOUDCONTROL] : null;
203
-		$fakeUser = new \stdClass();
204
-		$fakeUser->username = self::SESSION_PARAMETER_FORM_COMPONENT;
205
-		$_SESSION[self::SESSION_PARAMETER_CLOUDCONTROL] = $fakeUser;
206
-	}
207
-
208
-	/**
209
-	 * Removes the fake user and restores the existing user
210
-	 * session if it was there
211
-	 */
212
-	private function restoreUserSessionBackup()
213
-	{
214
-		if ($this->userSessionBackup === null) {
215
-			unset($_SESSION[self::SESSION_PARAMETER_CLOUDCONTROL]);
216
-		} else {
217
-			$_SESSION[self::SESSION_PARAMETER_CLOUDCONTROL] = $this->userSessionBackup;
218
-		}
219
-	}
220
-
221
-	private function setSubmitToSession()
222
-	{
223
-		$_SESSION[self::SESSION_PARAMETER_FORM_COMPONENT][$this->formParameterName]['submitted'] = true;
224
-	}
225
-
226
-	private function isSubmitAllowed()
227
-	{
228
-		if ($this->submitOncePerSession === true && $_SESSION[self::SESSION_PARAMETER_FORM_COMPONENT][$this->formParameterName]['submitted'] === true) {
229
-			return false;
230
-		} else {
231
-			return true;
232
-		}
233
-	}
234
-
235
-	private function checkDocumentTypeParameter()
236
-	{
237
-		if (isset($this->parameters[self::PARAMETER_DOCUMENT_TYPE])) {
238
-			$this->documentType = $this->parameters[self::PARAMETER_DOCUMENT_TYPE];
239
-			unset($this->parameters[self::PARAMETER_DOCUMENT_TYPE]);
240
-		}
241
-	}
242
-
243
-	private function checkResponseFolderParameter()
244
-	{
245
-		if (isset($this->parameters[self::PARAMETER_RESPONSE_FOLDER])) {
246
-			$this->responseFolder = $this->parameters[self::PARAMETER_RESPONSE_FOLDER];
247
-			unset($this->parameters[self::PARAMETER_RESPONSE_FOLDER]);
248
-		}
249
-	}
250
-
251
-	private function checkSubTemplateParameter()
252
-	{
253
-		if (isset($this->parameters[self::PARAMETER_SUB_TEMPLATE])) {
254
-			$this->subTemplate = $this->parameters[self::PARAMETER_SUB_TEMPLATE];
255
-			unset($this->parameters[self::PARAMETER_SUB_TEMPLATE]);
256
-		}
257
-	}
258
-
259
-	private function checkFormParameterNameParameter()
260
-	{
261
-		if (isset($this->parameters[self::PARAMETER_FORM_PARAMETER_NAME])) {
262
-			$this->formParameterName = $this->parameters[self::PARAMETER_FORM_PARAMETER_NAME];
263
-			unset($this->parameters[self::PARAMETER_FORM_PARAMETER_NAME]);
264
-		}
265
-	}
266
-
267
-	private function checkThankYouMessageParameter()
268
-	{
269
-		if (isset($this->parameters[self::PARAMETER_THANK_YOU_MESSAGE])) {
270
-			$this->thankYouMessage = $this->parameters[self::PARAMETER_THANK_YOU_MESSAGE];
271
-			unset($this->parameters[self::PARAMETER_THANK_YOU_MESSAGE]);
272
-		}
273
-	}
274
-
275
-	private function checkSubmitOncePerSessionParameter()
276
-	{
277
-		if (isset($this->parameters[self::PARAMETER_SUBMIT_ONCE_PER_SESSION])) {
278
-			$this->submitOncePerSession = $this->parameters[self::PARAMETER_SUBMIT_ONCE_PER_SESSION] === 'true';
279
-			unset($this->parameters[self::PARAMETER_SUBMIT_ONCE_PER_SESSION]);
280
-		}
281
-	}
282
-
283
-	/**
284
-	 * @throws \Exception
285
-	 */
286
-	private function checkRequiredParameters()
287
-	{
288
-		if ($this->documentType === null || $this->responseFolder === null) {
289
-			throw new \Exception('Parameters `documentType` and `responseFolder` are required for usage with this form');
290
-		}
291
-	}
292
-
293
-	/**
294
-	 * @return \cc\Request
295
-	 */
296
-	private function setPathBackup()
297
-	{
298
-		$request = $this->request;
299
-		if (isset($request::$get[self::GET_PARAMETER_PATH])) {
300
-			$this->getPathBackup = $request::$get[self::GET_PARAMETER_PATH];
301
-		}
302
-		$request::$get[self::GET_PARAMETER_PATH] = $this->responseFolder;
303
-
304
-		return $request;
305
-	}
306
-
307
-	/**
308
-	 * @param \cc\Request $request
309
-	 */
310
-	private function resetPathBackup($request)
311
-	{
312
-		if ($this->getPathBackup !== null) {
313
-			$request::$get[self::GET_PARAMETER_PATH] = $this->getPathBackup;
314
-		} else {
315
-			unset($request::$get[self::GET_PARAMETER_PATH]);
316
-		}
317
-	}
318
-
319
-	/**
320
-	 * @param $form
321
-	 */
322
-	private function setFormParameter($form)
323
-	{
324
-		if ($this->isFormSubmitted($this->request) || $this->isSubmitAllowed() === false) {
325
-			$this->parameters[$this->formParameterName] = '<a name="' . $this->formId . '"></a>' . $this->thankYouMessage;
326
-		} else {
327
-			$this->parameters[$this->formParameterName] = $form;
328
-		}
329
-	}
10
+    const GET_PARAMETER_PATH = 'path';
11
+
12
+    const PARAMETER_CMS_PREFIX = 'cmsPrefix';
13
+    const PARAMETER_DOCUMENT_TYPE = 'documentType';
14
+    const PARAMETER_DOCUMENT_TYPES = 'documentTypes';
15
+    const PARAMETER_FORM_ID = 'formId';
16
+    const PARAMETER_FORM_PARAMETER_NAME = 'formParameterName';
17
+    const PARAMETER_HIDE_TITLE_AND_STATE = 'hideTitleAndState';
18
+    const PARAMETER_RESPONSE_FOLDER = 'responseFolder';
19
+    const PARAMETER_SMALLEST_IMAGE = 'smallestImage';
20
+    const PARAMETER_SUBMIT_ONCE_PER_SESSION = 'submitOncePerSession';
21
+    const PARAMETER_SUB_TEMPLATE = 'subTemplate';
22
+    const PARAMETER_THANK_YOU_MESSAGE = 'thankYouMessage';
23
+
24
+    const SESSION_PARAMETER_CLOUDCONTROL = 'cloudcontrol';
25
+    const SESSION_PARAMETER_FORM_COMPONENT = 'FormComponent';
26
+    /**
27
+     * @var null|string
28
+     */
29
+    protected $documentType = null;
30
+    /**
31
+     * @var null|string
32
+     */
33
+    protected $responseFolder = null;
34
+    /**
35
+     * @var string
36
+     */
37
+    protected $subTemplate = 'cms/documents/document-form-form';
38
+    /**
39
+     * @var string
40
+     */
41
+    protected $formParameterName = 'form';
42
+    /**
43
+     * @var string
44
+     */
45
+    protected $thankYouMessage = 'Thank you for sending us your response.';
46
+
47
+    /**
48
+     * @var bool
49
+     */
50
+    protected $submitOncePerSession = false;
51
+
52
+    /**
53
+     * @var string
54
+     */
55
+    private $formId;
56
+    /**
57
+     * @var null|string
58
+     */
59
+    private $getPathBackup = null;
60
+
61
+    /**
62
+     * @var null|\stdClass
63
+     */
64
+    private $userSessionBackup = null;
65
+
66
+    /**
67
+     * @param Storage $storage
68
+     *
69
+     * @return void
70
+     * @throws \Exception
71
+     */
72
+    public function run(Storage $storage)
73
+    {
74
+        parent::run($storage);
75
+        $this->checkParameters();
76
+        $this->checkRequiredParameters();
77
+        $this->setFormId();
78
+        $this->initialize($storage);
79
+        $this->checkSubmit($storage);
80
+    }
81
+
82
+    /**
83
+     * @param null|Application $application
84
+     *
85
+     * @throws \Exception
86
+     */
87
+    public function render($application = null)
88
+    {
89
+        $request = $this->setPathBackup();
90
+        $form = $this->renderTemplate($this->subTemplate);
91
+        $this->resetPathBackup($request);
92
+        $this->setFormParameter($form);
93
+
94
+        parent::render($application);
95
+    }
96
+
97
+    /**
98
+     * Checks if parameters were given in the CMS configuration and
99
+     * sets them to their respective fields
100
+     */
101
+    private function checkParameters()
102
+    {
103
+        $this->checkDocumentTypeParameter();
104
+        $this->checkResponseFolderParameter();
105
+        $this->checkSubTemplateParameter();
106
+        $this->checkFormParameterNameParameter();
107
+        $this->checkThankYouMessageParameter();
108
+        $this->checkSubmitOncePerSessionParameter();
109
+    }
110
+
111
+    /**
112
+     * Sets variables needed for rendering the form template
113
+     *
114
+     * @param Storage $storage
115
+     */
116
+    private function initialize($storage)
117
+    {
118
+        $this->parameters[self::PARAMETER_SMALLEST_IMAGE] = $storage->getImageSet()->getSmallestImageSet();
119
+        $this->parameters[self::PARAMETER_CMS_PREFIX] = '';
120
+
121
+        $this->parameters[self::PARAMETER_DOCUMENT_TYPE] = $this->storage->getDocumentTypes()->getDocumentTypeBySlug($this->documentType, true);
122
+        $this->parameters[self::PARAMETER_DOCUMENT_TYPES] = $this->storage->getDocumentTypes()->getDocumentTypes();
123
+        $this->parameters[self::PARAMETER_HIDE_TITLE_AND_STATE] = true;
124
+        $this->parameters[self::PARAMETER_FORM_ID] = $this->formId;
125
+    }
126
+
127
+    /**
128
+     * If the form has been submitted, save the document
129
+     * Calls $this->postSubmit() afterwards
130
+     *
131
+     * @param Storage $storage
132
+     */
133
+    private function checkSubmit($storage)
134
+    {
135
+        if ($this->isFormSubmitted($this->request) && $this->isSubmitAllowed()) {
136
+            $postValues = $this->getPostValues($this->request);
137
+            $this->setUserSessionBackup();
138
+            $storage->getDocuments()->addDocument($postValues);
139
+            $this->restoreUserSessionBackup();
140
+            $this->setSubmitToSession();
141
+            $this->postSubmit($postValues, $storage);
142
+        }
143
+    }
144
+
145
+    /**
146
+     * Hook for derived classes to take actions after
147
+     * submitting the form
148
+     *
149
+     * @param $postValues
150
+     * @param $storage
151
+     */
152
+    protected function postSubmit($postValues, $storage)
153
+    {}
154
+
155
+    /**
156
+     * Sets a unique id for this particular form, so it can recognize
157
+     * it when a submit occurs
158
+     */
159
+    private function setFormId()
160
+    {
161
+        if (isset($_SESSION[self::SESSION_PARAMETER_FORM_COMPONENT][$this->formParameterName][self::PARAMETER_FORM_ID])) {
162
+            $this->formId = $_SESSION[self::SESSION_PARAMETER_FORM_COMPONENT][$this->formParameterName][self::PARAMETER_FORM_ID];
163
+        } else {
164
+            $_SESSION[self::SESSION_PARAMETER_FORM_COMPONENT][$this->formParameterName][self::PARAMETER_FORM_ID] = (string)microtime(true);
165
+            $_SESSION[self::SESSION_PARAMETER_FORM_COMPONENT][$this->formParameterName]['submitted'] = false;
166
+            $this->formId = $_SESSION[self::SESSION_PARAMETER_FORM_COMPONENT][$this->formParameterName][self::PARAMETER_FORM_ID];
167
+        }
168
+    }
169
+
170
+    /**
171
+     * Checks if this form has been submitted
172
+     *
173
+     * @param \CloudControl\Cms\cc\Request $request
174
+     *
175
+     * @return bool
176
+     */
177
+    private function isFormSubmitted($request)
178
+    {
179
+        return !empty($request::$post) && isset($request::$post[self::PARAMETER_FORM_ID]) && $request::$post[self::PARAMETER_FORM_ID] === $this->formId && isset($_SESSION[self::SESSION_PARAMETER_FORM_COMPONENT][$this->formParameterName][self::PARAMETER_FORM_ID]) && $_SESSION[self::SESSION_PARAMETER_FORM_COMPONENT][$this->formParameterName][self::PARAMETER_FORM_ID] === $this->formId;
180
+    }
181
+
182
+    /**
183
+     * @param \cc\Request $request
184
+     * @return array
185
+     */
186
+    private function getPostValues($request)
187
+    {
188
+        $postValues = $request::$post;
189
+        $postValues[self::PARAMETER_DOCUMENT_TYPE] = $this->documentType;
190
+        $postValues[self::GET_PARAMETER_PATH] = $this->responseFolder;
191
+        $postValues['title'] = date('r') . ' - From: ' . $request::$requestUri;
192
+
193
+        return $postValues;
194
+    }
195
+
196
+    /**
197
+     * Temporarily stores the current user session in a backup variable
198
+     * and sets a fake user instead
199
+     */
200
+    private function setUserSessionBackup()
201
+    {
202
+        $this->userSessionBackup = isset($_SESSION[self::SESSION_PARAMETER_CLOUDCONTROL]) ? $_SESSION[self::SESSION_PARAMETER_CLOUDCONTROL] : null;
203
+        $fakeUser = new \stdClass();
204
+        $fakeUser->username = self::SESSION_PARAMETER_FORM_COMPONENT;
205
+        $_SESSION[self::SESSION_PARAMETER_CLOUDCONTROL] = $fakeUser;
206
+    }
207
+
208
+    /**
209
+     * Removes the fake user and restores the existing user
210
+     * session if it was there
211
+     */
212
+    private function restoreUserSessionBackup()
213
+    {
214
+        if ($this->userSessionBackup === null) {
215
+            unset($_SESSION[self::SESSION_PARAMETER_CLOUDCONTROL]);
216
+        } else {
217
+            $_SESSION[self::SESSION_PARAMETER_CLOUDCONTROL] = $this->userSessionBackup;
218
+        }
219
+    }
220
+
221
+    private function setSubmitToSession()
222
+    {
223
+        $_SESSION[self::SESSION_PARAMETER_FORM_COMPONENT][$this->formParameterName]['submitted'] = true;
224
+    }
225
+
226
+    private function isSubmitAllowed()
227
+    {
228
+        if ($this->submitOncePerSession === true && $_SESSION[self::SESSION_PARAMETER_FORM_COMPONENT][$this->formParameterName]['submitted'] === true) {
229
+            return false;
230
+        } else {
231
+            return true;
232
+        }
233
+    }
234
+
235
+    private function checkDocumentTypeParameter()
236
+    {
237
+        if (isset($this->parameters[self::PARAMETER_DOCUMENT_TYPE])) {
238
+            $this->documentType = $this->parameters[self::PARAMETER_DOCUMENT_TYPE];
239
+            unset($this->parameters[self::PARAMETER_DOCUMENT_TYPE]);
240
+        }
241
+    }
242
+
243
+    private function checkResponseFolderParameter()
244
+    {
245
+        if (isset($this->parameters[self::PARAMETER_RESPONSE_FOLDER])) {
246
+            $this->responseFolder = $this->parameters[self::PARAMETER_RESPONSE_FOLDER];
247
+            unset($this->parameters[self::PARAMETER_RESPONSE_FOLDER]);
248
+        }
249
+    }
250
+
251
+    private function checkSubTemplateParameter()
252
+    {
253
+        if (isset($this->parameters[self::PARAMETER_SUB_TEMPLATE])) {
254
+            $this->subTemplate = $this->parameters[self::PARAMETER_SUB_TEMPLATE];
255
+            unset($this->parameters[self::PARAMETER_SUB_TEMPLATE]);
256
+        }
257
+    }
258
+
259
+    private function checkFormParameterNameParameter()
260
+    {
261
+        if (isset($this->parameters[self::PARAMETER_FORM_PARAMETER_NAME])) {
262
+            $this->formParameterName = $this->parameters[self::PARAMETER_FORM_PARAMETER_NAME];
263
+            unset($this->parameters[self::PARAMETER_FORM_PARAMETER_NAME]);
264
+        }
265
+    }
266
+
267
+    private function checkThankYouMessageParameter()
268
+    {
269
+        if (isset($this->parameters[self::PARAMETER_THANK_YOU_MESSAGE])) {
270
+            $this->thankYouMessage = $this->parameters[self::PARAMETER_THANK_YOU_MESSAGE];
271
+            unset($this->parameters[self::PARAMETER_THANK_YOU_MESSAGE]);
272
+        }
273
+    }
274
+
275
+    private function checkSubmitOncePerSessionParameter()
276
+    {
277
+        if (isset($this->parameters[self::PARAMETER_SUBMIT_ONCE_PER_SESSION])) {
278
+            $this->submitOncePerSession = $this->parameters[self::PARAMETER_SUBMIT_ONCE_PER_SESSION] === 'true';
279
+            unset($this->parameters[self::PARAMETER_SUBMIT_ONCE_PER_SESSION]);
280
+        }
281
+    }
282
+
283
+    /**
284
+     * @throws \Exception
285
+     */
286
+    private function checkRequiredParameters()
287
+    {
288
+        if ($this->documentType === null || $this->responseFolder === null) {
289
+            throw new \Exception('Parameters `documentType` and `responseFolder` are required for usage with this form');
290
+        }
291
+    }
292
+
293
+    /**
294
+     * @return \cc\Request
295
+     */
296
+    private function setPathBackup()
297
+    {
298
+        $request = $this->request;
299
+        if (isset($request::$get[self::GET_PARAMETER_PATH])) {
300
+            $this->getPathBackup = $request::$get[self::GET_PARAMETER_PATH];
301
+        }
302
+        $request::$get[self::GET_PARAMETER_PATH] = $this->responseFolder;
303
+
304
+        return $request;
305
+    }
306
+
307
+    /**
308
+     * @param \cc\Request $request
309
+     */
310
+    private function resetPathBackup($request)
311
+    {
312
+        if ($this->getPathBackup !== null) {
313
+            $request::$get[self::GET_PARAMETER_PATH] = $this->getPathBackup;
314
+        } else {
315
+            unset($request::$get[self::GET_PARAMETER_PATH]);
316
+        }
317
+    }
318
+
319
+    /**
320
+     * @param $form
321
+     */
322
+    private function setFormParameter($form)
323
+    {
324
+        if ($this->isFormSubmitted($this->request) || $this->isSubmitAllowed() === false) {
325
+            $this->parameters[$this->formParameterName] = '<a name="' . $this->formId . '"></a>' . $this->thankYouMessage;
326
+        } else {
327
+            $this->parameters[$this->formParameterName] = $form;
328
+        }
329
+    }
330 330
 }
331 331
\ No newline at end of file
Please login to merge, or discard this patch.