@@ -11,37 +11,37 @@ |
||
11 | 11 | |
12 | 12 | abstract class StopWordsFilter implements Filter |
13 | 13 | { |
14 | - protected $tokens; |
|
15 | - protected $stopWords = array(); |
|
14 | + protected $tokens; |
|
15 | + protected $stopWords = array(); |
|
16 | 16 | |
17 | - /** |
|
18 | - * StopWordsFilter constructor. |
|
19 | - * @param array $tokens |
|
20 | - */ |
|
21 | - public function __construct($tokens) |
|
22 | - { |
|
23 | - $this->tokens = $tokens; |
|
24 | - } |
|
17 | + /** |
|
18 | + * StopWordsFilter constructor. |
|
19 | + * @param array $tokens |
|
20 | + */ |
|
21 | + public function __construct($tokens) |
|
22 | + { |
|
23 | + $this->tokens = $tokens; |
|
24 | + } |
|
25 | 25 | |
26 | - /** |
|
27 | - * @return array |
|
28 | - */ |
|
29 | - public function getFilterResults() |
|
30 | - { |
|
31 | - foreach ($this->stopWords as $stopWord) { |
|
32 | - foreach ($this->tokens as $field => $tokens) { |
|
33 | - if (isset($tokens[$stopWord])) { |
|
34 | - $tokens[$stopWord] = null; |
|
35 | - unset($tokens[$stopWord]); |
|
36 | - $tokens = array_filter($tokens); |
|
37 | - asort($tokens); |
|
38 | - } |
|
26 | + /** |
|
27 | + * @return array |
|
28 | + */ |
|
29 | + public function getFilterResults() |
|
30 | + { |
|
31 | + foreach ($this->stopWords as $stopWord) { |
|
32 | + foreach ($this->tokens as $field => $tokens) { |
|
33 | + if (isset($tokens[$stopWord])) { |
|
34 | + $tokens[$stopWord] = null; |
|
35 | + unset($tokens[$stopWord]); |
|
36 | + $tokens = array_filter($tokens); |
|
37 | + asort($tokens); |
|
38 | + } |
|
39 | 39 | |
40 | - $this->tokens[$field] = $tokens; |
|
41 | - } |
|
42 | - } |
|
43 | - $this->tokens = array_filter($this->tokens); |
|
44 | - asort($this->tokens); |
|
45 | - return $this->tokens; |
|
46 | - } |
|
40 | + $this->tokens[$field] = $tokens; |
|
41 | + } |
|
42 | + } |
|
43 | + $this->tokens = array_filter($this->tokens); |
|
44 | + asort($this->tokens); |
|
45 | + return $this->tokens; |
|
46 | + } |
|
47 | 47 | } |
48 | 48 | \ No newline at end of file |
@@ -1,22 +1,22 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | namespace CloudControl\Cms\storage { |
3 | 3 | |
4 | - use CloudControl\Cms\storage\factories\DocumentFolderFactory; |
|
5 | - use CloudControl\Cms\storage\storage\ApplicationComponentsStorage; |
|
6 | - use CloudControl\Cms\storage\storage\BricksStorage; |
|
7 | - use CloudControl\Cms\storage\storage\DocumentStorage; |
|
8 | - use CloudControl\Cms\storage\storage\DocumentTypesStorage; |
|
9 | - use CloudControl\Cms\storage\storage\FilesStorage; |
|
10 | - use CloudControl\Cms\storage\storage\ImageSetStorage; |
|
11 | - use CloudControl\Cms\storage\storage\ImagesStorage; |
|
12 | - use CloudControl\Cms\storage\storage\RedirectsStorage; |
|
13 | - use CloudControl\Cms\storage\storage\SitemapStorage; |
|
14 | - use CloudControl\Cms\storage\storage\UsersStorage; |
|
15 | - use CloudControl\Cms\storage\storage\ValuelistsStorage; |
|
4 | + use CloudControl\Cms\storage\factories\DocumentFolderFactory; |
|
5 | + use CloudControl\Cms\storage\storage\ApplicationComponentsStorage; |
|
6 | + use CloudControl\Cms\storage\storage\BricksStorage; |
|
7 | + use CloudControl\Cms\storage\storage\DocumentStorage; |
|
8 | + use CloudControl\Cms\storage\storage\DocumentTypesStorage; |
|
9 | + use CloudControl\Cms\storage\storage\FilesStorage; |
|
10 | + use CloudControl\Cms\storage\storage\ImageSetStorage; |
|
11 | + use CloudControl\Cms\storage\storage\ImagesStorage; |
|
12 | + use CloudControl\Cms\storage\storage\RedirectsStorage; |
|
13 | + use CloudControl\Cms\storage\storage\SitemapStorage; |
|
14 | + use CloudControl\Cms\storage\storage\UsersStorage; |
|
15 | + use CloudControl\Cms\storage\storage\ValuelistsStorage; |
|
16 | 16 | |
17 | - /** |
|
17 | + /** |
|
18 | 18 | * Class JsonStorage |
19 | - * @package CloudControl\Cms\storage |
|
19 | + * @package CloudControl\Cms\storage |
|
20 | 20 | */ |
21 | 21 | class Storage |
22 | 22 | { |
@@ -61,20 +61,20 @@ discard block |
||
61 | 61 | * @var DocumentStorage |
62 | 62 | */ |
63 | 63 | protected $documents; |
64 | - /** |
|
65 | - * @var RedirectsStorage |
|
66 | - */ |
|
67 | - protected $redirects; |
|
68 | - /** |
|
69 | - * @var String |
|
70 | - */ |
|
71 | - protected $imagesDir; |
|
72 | - /** |
|
73 | - * @var String |
|
74 | - */ |
|
75 | - protected $filesDir; |
|
64 | + /** |
|
65 | + * @var RedirectsStorage |
|
66 | + */ |
|
67 | + protected $redirects; |
|
68 | + /** |
|
69 | + * @var String |
|
70 | + */ |
|
71 | + protected $imagesDir; |
|
72 | + /** |
|
73 | + * @var String |
|
74 | + */ |
|
75 | + protected $filesDir; |
|
76 | 76 | |
77 | - /** |
|
77 | + /** |
|
78 | 78 | * @var String |
79 | 79 | */ |
80 | 80 | private $storageDir; |
@@ -83,18 +83,18 @@ discard block |
||
83 | 83 | */ |
84 | 84 | private $repository; |
85 | 85 | |
86 | - /** |
|
87 | - * JsonStorage constructor. |
|
88 | - * |
|
89 | - * @param string $storageDir |
|
90 | - * @param $imagesDir |
|
91 | - * @param $filesDir |
|
92 | - */ |
|
93 | - public function __construct($storageDir, $imagesDir, $filesDir) |
|
86 | + /** |
|
87 | + * JsonStorage constructor. |
|
88 | + * |
|
89 | + * @param string $storageDir |
|
90 | + * @param $imagesDir |
|
91 | + * @param $filesDir |
|
92 | + */ |
|
93 | + public function __construct($storageDir, $imagesDir, $filesDir) |
|
94 | 94 | { |
95 | 95 | $this->storageDir = $storageDir; |
96 | - $this->imagesDir = $imagesDir; |
|
97 | - $this->filesDir = $filesDir; |
|
96 | + $this->imagesDir = $imagesDir; |
|
97 | + $this->filesDir = $filesDir; |
|
98 | 98 | $this->config(); |
99 | 99 | } |
100 | 100 | |
@@ -238,7 +238,7 @@ discard block |
||
238 | 238 | { |
239 | 239 | if (!$this->images instanceof ImagesStorage) { |
240 | 240 | |
241 | - $this->images = new ImagesStorage($this->repository, $this->imagesDir); |
|
241 | + $this->images = new ImagesStorage($this->repository, $this->imagesDir); |
|
242 | 242 | } |
243 | 243 | return $this->images; |
244 | 244 | } |
@@ -337,16 +337,16 @@ discard block |
||
337 | 337 | return $this->valuelists; |
338 | 338 | } |
339 | 339 | |
340 | - /** |
|
341 | - * @return \CloudControl\Cms\storage\storage\RedirectsStorage |
|
342 | - */ |
|
343 | - public function getRedirects() |
|
344 | - { |
|
345 | - if (!$this->redirects instanceof RedirectsStorage) { |
|
346 | - $this->redirects = new RedirectsStorage($this->repository); |
|
347 | - } |
|
348 | - return $this->redirects; |
|
349 | - } |
|
340 | + /** |
|
341 | + * @return \CloudControl\Cms\storage\storage\RedirectsStorage |
|
342 | + */ |
|
343 | + public function getRedirects() |
|
344 | + { |
|
345 | + if (!$this->redirects instanceof RedirectsStorage) { |
|
346 | + $this->redirects = new RedirectsStorage($this->repository); |
|
347 | + } |
|
348 | + return $this->redirects; |
|
349 | + } |
|
350 | 350 | |
351 | 351 | } |
352 | 352 | } |
353 | 353 | \ No newline at end of file |
@@ -20,60 +20,60 @@ discard block |
||
20 | 20 | */ |
21 | 21 | class Document |
22 | 22 | { |
23 | - public $id; |
|
24 | - public $path; |
|
25 | - public $title; |
|
26 | - public $slug; |
|
27 | - public $type; |
|
28 | - public $documentType; |
|
29 | - public $documentTypeSlug; |
|
30 | - public $state; |
|
31 | - public $lastModificationDate; |
|
32 | - public $creationDate; |
|
33 | - public $lastModifiedBy; |
|
34 | - protected $fields; |
|
35 | - protected $bricks; |
|
36 | - protected $dynamicBricks; |
|
37 | - protected $content; |
|
23 | + public $id; |
|
24 | + public $path; |
|
25 | + public $title; |
|
26 | + public $slug; |
|
27 | + public $type; |
|
28 | + public $documentType; |
|
29 | + public $documentTypeSlug; |
|
30 | + public $state; |
|
31 | + public $lastModificationDate; |
|
32 | + public $creationDate; |
|
33 | + public $lastModifiedBy; |
|
34 | + protected $fields; |
|
35 | + protected $bricks; |
|
36 | + protected $dynamicBricks; |
|
37 | + protected $content; |
|
38 | 38 | |
39 | - protected $dbHandle; |
|
39 | + protected $dbHandle; |
|
40 | 40 | |
41 | - protected $jsonEncodedFields = array('fields', 'bricks', 'dynamicBricks'); |
|
42 | - protected $orderableFields = array('title', 'slug', 'type', 'documentType', 'documentTypeSlug', 'state', 'lastModificationDate', 'creationDate', 'lastModifiedBy'); |
|
41 | + protected $jsonEncodedFields = array('fields', 'bricks', 'dynamicBricks'); |
|
42 | + protected $orderableFields = array('title', 'slug', 'type', 'documentType', 'documentTypeSlug', 'state', 'lastModificationDate', 'creationDate', 'lastModifiedBy'); |
|
43 | 43 | |
44 | - public static $DOCUMENT_STATES = array('published', 'unpublished'); |
|
44 | + public static $DOCUMENT_STATES = array('published', 'unpublished'); |
|
45 | 45 | |
46 | - public function __get($name) { |
|
47 | - if (in_array($name, $this->jsonEncodedFields)) { |
|
48 | - if (is_string($this->$name)) { |
|
49 | - return json_decode($this->$name); |
|
50 | - } else { |
|
51 | - return $this->$name; |
|
52 | - } |
|
53 | - } elseif ($name === 'content') { |
|
54 | - if ($this->dbHandle === null) { |
|
55 | - throw new \Exception('Document doesnt have a dbHandle handle. (path: ' . $this->path . ')'); |
|
56 | - } else { |
|
57 | - if ($this->content === null) { |
|
58 | - return $this->getContent(); |
|
59 | - } |
|
60 | - } |
|
61 | - } elseif ($name === 'dbHandle') { |
|
62 | - throw new \Exception('Trying to get protected property repository.'); |
|
63 | - } |
|
64 | - return $this->$name; |
|
65 | - } |
|
46 | + public function __get($name) { |
|
47 | + if (in_array($name, $this->jsonEncodedFields)) { |
|
48 | + if (is_string($this->$name)) { |
|
49 | + return json_decode($this->$name); |
|
50 | + } else { |
|
51 | + return $this->$name; |
|
52 | + } |
|
53 | + } elseif ($name === 'content') { |
|
54 | + if ($this->dbHandle === null) { |
|
55 | + throw new \Exception('Document doesnt have a dbHandle handle. (path: ' . $this->path . ')'); |
|
56 | + } else { |
|
57 | + if ($this->content === null) { |
|
58 | + return $this->getContent(); |
|
59 | + } |
|
60 | + } |
|
61 | + } elseif ($name === 'dbHandle') { |
|
62 | + throw new \Exception('Trying to get protected property repository.'); |
|
63 | + } |
|
64 | + return $this->$name; |
|
65 | + } |
|
66 | 66 | |
67 | - public function __set($name, $value) { |
|
68 | - if (in_array($name, $this->jsonEncodedFields)) { |
|
69 | - $this->$name = json_encode($value); |
|
70 | - } elseif ($name === 'content') { |
|
71 | - // Dont do anything for now.. |
|
72 | - return; |
|
73 | - } |
|
67 | + public function __set($name, $value) { |
|
68 | + if (in_array($name, $this->jsonEncodedFields)) { |
|
69 | + $this->$name = json_encode($value); |
|
70 | + } elseif ($name === 'content') { |
|
71 | + // Dont do anything for now.. |
|
72 | + return; |
|
73 | + } |
|
74 | 74 | |
75 | - $this->$name = $value; |
|
76 | - } |
|
75 | + $this->$name = $value; |
|
76 | + } |
|
77 | 77 | |
78 | 78 | /** |
79 | 79 | * @param string $orderBy |
@@ -82,15 +82,15 @@ discard block |
||
82 | 82 | * @return array |
83 | 83 | * @throws \Exception |
84 | 84 | */ |
85 | - public function getContent($orderBy = 'title', $order = 'ASC') |
|
86 | - { |
|
87 | - if (empty($this->content)) { |
|
88 | - $docs = $this->documentStorage->getDocumentsWithState($this->path); |
|
89 | - $this->content = $docs; |
|
90 | - } |
|
85 | + public function getContent($orderBy = 'title', $order = 'ASC') |
|
86 | + { |
|
87 | + if (empty($this->content)) { |
|
88 | + $docs = $this->documentStorage->getDocumentsWithState($this->path); |
|
89 | + $this->content = $docs; |
|
90 | + } |
|
91 | 91 | |
92 | - return $this->content; |
|
93 | - } |
|
92 | + return $this->content; |
|
93 | + } |
|
94 | 94 | |
95 | 95 | /** |
96 | 96 | * @return string |
@@ -10,26 +10,26 @@ |
||
10 | 10 | |
11 | 11 | class RedirectsFactory |
12 | 12 | { |
13 | - /** |
|
14 | - * Create a new redirect object from postvalues |
|
15 | - * |
|
16 | - * @param $postValues |
|
17 | - * @return \stdClass |
|
18 | - * @throws \Exception |
|
19 | - */ |
|
20 | - public static function createRedirectFromPostValues($postValues) |
|
21 | - { |
|
22 | - if (isset($postValues['title'], $postValues['fromUrl'], $postValues['toUrl'])) { |
|
23 | - $redirectObject = new \stdClass(); |
|
24 | - $redirectObject->title = $postValues['title']; |
|
25 | - $redirectObject->slug = StringUtil::slugify($postValues['title']); |
|
26 | - $redirectObject->fromUrl = $postValues['fromUrl']; |
|
27 | - $redirectObject->toUrl = $postValues['toUrl']; |
|
28 | - $redirectObject->type = $postValues['type']; |
|
13 | + /** |
|
14 | + * Create a new redirect object from postvalues |
|
15 | + * |
|
16 | + * @param $postValues |
|
17 | + * @return \stdClass |
|
18 | + * @throws \Exception |
|
19 | + */ |
|
20 | + public static function createRedirectFromPostValues($postValues) |
|
21 | + { |
|
22 | + if (isset($postValues['title'], $postValues['fromUrl'], $postValues['toUrl'])) { |
|
23 | + $redirectObject = new \stdClass(); |
|
24 | + $redirectObject->title = $postValues['title']; |
|
25 | + $redirectObject->slug = StringUtil::slugify($postValues['title']); |
|
26 | + $redirectObject->fromUrl = $postValues['fromUrl']; |
|
27 | + $redirectObject->toUrl = $postValues['toUrl']; |
|
28 | + $redirectObject->type = $postValues['type']; |
|
29 | 29 | |
30 | - return $redirectObject; |
|
31 | - } else { |
|
32 | - throw new \Exception('Trying to create valuelist with invalid data.'); |
|
33 | - } |
|
34 | - } |
|
30 | + return $redirectObject; |
|
31 | + } else { |
|
32 | + throw new \Exception('Trying to create valuelist with invalid data.'); |
|
33 | + } |
|
34 | + } |
|
35 | 35 | } |
36 | 36 | \ No newline at end of file |
@@ -12,34 +12,34 @@ |
||
12 | 12 | |
13 | 13 | class ValuelistFactory |
14 | 14 | { |
15 | - /** |
|
16 | - * Create a sitemap item from post values |
|
17 | - * |
|
18 | - * @param $postValues |
|
19 | - * |
|
20 | - * @return \stdClass |
|
21 | - * @throws \Exception |
|
22 | - */ |
|
23 | - public static function createValuelistFromPostValues($postValues) |
|
24 | - { |
|
25 | - if (isset($postValues['title'])) { |
|
26 | - $valuelistObject = new \stdClass(); |
|
27 | - $valuelistObject->title = $postValues['title']; |
|
28 | - $valuelistObject->slug = StringUtil::slugify($postValues['title']); |
|
29 | - $valuelistObject->pairs = new \stdClass(); |
|
30 | - if (isset($postValues['keys'], $postValues['values'])) { |
|
31 | - foreach ($postValues['keys'] as $key => $value) { |
|
32 | - $valuelistObject->pairs->$value = $postValues['values'][$key]; |
|
33 | - } |
|
34 | - } |
|
35 | - $object_vars = get_object_vars($valuelistObject->pairs); |
|
36 | - ksort($object_vars); |
|
37 | - $valuelistObject->pairs = (object) $object_vars; |
|
15 | + /** |
|
16 | + * Create a sitemap item from post values |
|
17 | + * |
|
18 | + * @param $postValues |
|
19 | + * |
|
20 | + * @return \stdClass |
|
21 | + * @throws \Exception |
|
22 | + */ |
|
23 | + public static function createValuelistFromPostValues($postValues) |
|
24 | + { |
|
25 | + if (isset($postValues['title'])) { |
|
26 | + $valuelistObject = new \stdClass(); |
|
27 | + $valuelistObject->title = $postValues['title']; |
|
28 | + $valuelistObject->slug = StringUtil::slugify($postValues['title']); |
|
29 | + $valuelistObject->pairs = new \stdClass(); |
|
30 | + if (isset($postValues['keys'], $postValues['values'])) { |
|
31 | + foreach ($postValues['keys'] as $key => $value) { |
|
32 | + $valuelistObject->pairs->$value = $postValues['values'][$key]; |
|
33 | + } |
|
34 | + } |
|
35 | + $object_vars = get_object_vars($valuelistObject->pairs); |
|
36 | + ksort($object_vars); |
|
37 | + $valuelistObject->pairs = (object) $object_vars; |
|
38 | 38 | |
39 | - return $valuelistObject; |
|
40 | - } else { |
|
41 | - throw new \Exception('Trying to create valuelist with invalid data.'); |
|
42 | - } |
|
43 | - } |
|
39 | + return $valuelistObject; |
|
40 | + } else { |
|
41 | + throw new \Exception('Trying to create valuelist with invalid data.'); |
|
42 | + } |
|
43 | + } |
|
44 | 44 | |
45 | 45 | } |
46 | 46 | \ No newline at end of file |
@@ -11,124 +11,124 @@ |
||
11 | 11 | |
12 | 12 | class ImagesStorage extends AbstractStorage |
13 | 13 | { |
14 | - protected $imagesDir; |
|
15 | - |
|
16 | - public function __construct($repository, $imagesDir) |
|
17 | - { |
|
18 | - parent::__construct($repository); |
|
19 | - $this->imagesDir = $imagesDir; |
|
20 | - } |
|
21 | - |
|
22 | - |
|
23 | - /** |
|
24 | - * @var ImageSetStorage |
|
25 | - */ |
|
26 | - protected $imageSet; |
|
27 | - |
|
28 | - /** |
|
29 | - * Get all images |
|
30 | - * |
|
31 | - * @return array |
|
32 | - */ |
|
33 | - public function getImages() |
|
34 | - { |
|
35 | - return $this->repository->images; |
|
36 | - } |
|
37 | - |
|
38 | - /** |
|
39 | - * @param $postValues |
|
40 | - * |
|
41 | - * @throws \Exception |
|
42 | - */ |
|
43 | - public function addImage($postValues) |
|
44 | - { |
|
45 | - $destinationPath = $this->getDestinationPath(); |
|
46 | - |
|
47 | - $filename = $this->validateFilename($postValues['name'], $destinationPath); |
|
48 | - $destination = $destinationPath . DIRECTORY_SEPARATOR . $filename; |
|
49 | - |
|
50 | - if ($postValues['error'] != '0') { |
|
51 | - throw new \Exception('Error uploading file. Error code: ' . $postValues['error']); |
|
52 | - } |
|
53 | - |
|
54 | - if (move_uploaded_file($postValues['tmp_name'], $destination)) { |
|
55 | - $imageResizer = new ImageResizer($this->getImageSet()->getImageSet()); |
|
56 | - $fileNames = $imageResizer->applyImageSetToImage($destination); |
|
57 | - $fileNames['original'] = $filename; |
|
58 | - $imageObject = ImageFactory::createImageFromPostValues($postValues, $filename, $fileNames); |
|
59 | - |
|
60 | - $images = $this->repository->images; |
|
61 | - $images[] = $imageObject; |
|
62 | - $this->repository->images = $images; |
|
63 | - |
|
64 | - $this->save(); |
|
65 | - } else { |
|
66 | - throw new \Exception('Error moving uploaded file'); |
|
67 | - } |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * Delete image by name |
|
72 | - * @param $filename |
|
73 | - */ |
|
74 | - public function deleteImageByName($filename) |
|
75 | - { |
|
76 | - $destinationPath = $this->getDestinationPath(); |
|
77 | - |
|
78 | - $images = $this->getImages(); |
|
79 | - |
|
80 | - foreach ($images as $key => $image) { |
|
81 | - if ($image->file == $filename) { |
|
82 | - foreach ($image->set as $imageSetFilename) { |
|
83 | - $destination = $destinationPath . '/' . $imageSetFilename; |
|
84 | - if (file_exists($destination)) { |
|
85 | - unlink($destination); |
|
86 | - } else { |
|
87 | - dump($destination); |
|
88 | - } |
|
89 | - } |
|
90 | - unset($images[$key]); |
|
91 | - } |
|
92 | - } |
|
93 | - |
|
94 | - $this->repository->images = $images; |
|
95 | - $this->save(); |
|
96 | - } |
|
97 | - |
|
98 | - /** |
|
99 | - * @param $filename |
|
100 | - * |
|
101 | - * @return null |
|
102 | - */ |
|
103 | - public function getImageByName($filename) |
|
104 | - { |
|
105 | - $images = $this->getImages(); |
|
106 | - foreach ($images as $image) { |
|
107 | - if ($image->file == $filename) { |
|
108 | - return $image; |
|
109 | - } |
|
110 | - } |
|
111 | - |
|
112 | - return null; |
|
113 | - } |
|
114 | - |
|
115 | - /** |
|
116 | - * @return \CloudControl\Cms\storage\storage\ImageSetStorage |
|
117 | - */ |
|
118 | - private function getImageSet() |
|
119 | - { |
|
120 | - if (!$this->imageSet instanceof ImageSetStorage) { |
|
121 | - $this->imageSet = new ImageSetStorage($this->repository); |
|
122 | - } |
|
123 | - return $this->imageSet; |
|
124 | - } |
|
125 | - |
|
126 | - /** |
|
127 | - * @return bool|string |
|
128 | - */ |
|
129 | - private function getDestinationPath() |
|
130 | - { |
|
131 | - $destinationPath = realpath($this->imagesDir . DIRECTORY_SEPARATOR); |
|
132 | - return $destinationPath; |
|
133 | - } |
|
14 | + protected $imagesDir; |
|
15 | + |
|
16 | + public function __construct($repository, $imagesDir) |
|
17 | + { |
|
18 | + parent::__construct($repository); |
|
19 | + $this->imagesDir = $imagesDir; |
|
20 | + } |
|
21 | + |
|
22 | + |
|
23 | + /** |
|
24 | + * @var ImageSetStorage |
|
25 | + */ |
|
26 | + protected $imageSet; |
|
27 | + |
|
28 | + /** |
|
29 | + * Get all images |
|
30 | + * |
|
31 | + * @return array |
|
32 | + */ |
|
33 | + public function getImages() |
|
34 | + { |
|
35 | + return $this->repository->images; |
|
36 | + } |
|
37 | + |
|
38 | + /** |
|
39 | + * @param $postValues |
|
40 | + * |
|
41 | + * @throws \Exception |
|
42 | + */ |
|
43 | + public function addImage($postValues) |
|
44 | + { |
|
45 | + $destinationPath = $this->getDestinationPath(); |
|
46 | + |
|
47 | + $filename = $this->validateFilename($postValues['name'], $destinationPath); |
|
48 | + $destination = $destinationPath . DIRECTORY_SEPARATOR . $filename; |
|
49 | + |
|
50 | + if ($postValues['error'] != '0') { |
|
51 | + throw new \Exception('Error uploading file. Error code: ' . $postValues['error']); |
|
52 | + } |
|
53 | + |
|
54 | + if (move_uploaded_file($postValues['tmp_name'], $destination)) { |
|
55 | + $imageResizer = new ImageResizer($this->getImageSet()->getImageSet()); |
|
56 | + $fileNames = $imageResizer->applyImageSetToImage($destination); |
|
57 | + $fileNames['original'] = $filename; |
|
58 | + $imageObject = ImageFactory::createImageFromPostValues($postValues, $filename, $fileNames); |
|
59 | + |
|
60 | + $images = $this->repository->images; |
|
61 | + $images[] = $imageObject; |
|
62 | + $this->repository->images = $images; |
|
63 | + |
|
64 | + $this->save(); |
|
65 | + } else { |
|
66 | + throw new \Exception('Error moving uploaded file'); |
|
67 | + } |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * Delete image by name |
|
72 | + * @param $filename |
|
73 | + */ |
|
74 | + public function deleteImageByName($filename) |
|
75 | + { |
|
76 | + $destinationPath = $this->getDestinationPath(); |
|
77 | + |
|
78 | + $images = $this->getImages(); |
|
79 | + |
|
80 | + foreach ($images as $key => $image) { |
|
81 | + if ($image->file == $filename) { |
|
82 | + foreach ($image->set as $imageSetFilename) { |
|
83 | + $destination = $destinationPath . '/' . $imageSetFilename; |
|
84 | + if (file_exists($destination)) { |
|
85 | + unlink($destination); |
|
86 | + } else { |
|
87 | + dump($destination); |
|
88 | + } |
|
89 | + } |
|
90 | + unset($images[$key]); |
|
91 | + } |
|
92 | + } |
|
93 | + |
|
94 | + $this->repository->images = $images; |
|
95 | + $this->save(); |
|
96 | + } |
|
97 | + |
|
98 | + /** |
|
99 | + * @param $filename |
|
100 | + * |
|
101 | + * @return null |
|
102 | + */ |
|
103 | + public function getImageByName($filename) |
|
104 | + { |
|
105 | + $images = $this->getImages(); |
|
106 | + foreach ($images as $image) { |
|
107 | + if ($image->file == $filename) { |
|
108 | + return $image; |
|
109 | + } |
|
110 | + } |
|
111 | + |
|
112 | + return null; |
|
113 | + } |
|
114 | + |
|
115 | + /** |
|
116 | + * @return \CloudControl\Cms\storage\storage\ImageSetStorage |
|
117 | + */ |
|
118 | + private function getImageSet() |
|
119 | + { |
|
120 | + if (!$this->imageSet instanceof ImageSetStorage) { |
|
121 | + $this->imageSet = new ImageSetStorage($this->repository); |
|
122 | + } |
|
123 | + return $this->imageSet; |
|
124 | + } |
|
125 | + |
|
126 | + /** |
|
127 | + * @return bool|string |
|
128 | + */ |
|
129 | + private function getDestinationPath() |
|
130 | + { |
|
131 | + $destinationPath = realpath($this->imagesDir . DIRECTORY_SEPARATOR); |
|
132 | + return $destinationPath; |
|
133 | + } |
|
134 | 134 | } |
135 | 135 | \ No newline at end of file |
@@ -12,83 +12,83 @@ |
||
12 | 12 | |
13 | 13 | class ValuelistsStorage extends AbstractStorage |
14 | 14 | { |
15 | - /** |
|
16 | - * Get all valuelists |
|
17 | - * |
|
18 | - * @return mixed |
|
19 | - */ |
|
20 | - public function getValuelists() |
|
21 | - { |
|
22 | - return $this->repository->valuelists; |
|
23 | - } |
|
15 | + /** |
|
16 | + * Get all valuelists |
|
17 | + * |
|
18 | + * @return mixed |
|
19 | + */ |
|
20 | + public function getValuelists() |
|
21 | + { |
|
22 | + return $this->repository->valuelists; |
|
23 | + } |
|
24 | 24 | |
25 | - public function addValuelist($postValues) |
|
26 | - { |
|
27 | - $valueListObject = ValuelistFactory::createValuelistFromPostValues($postValues); |
|
28 | - $valuelists = $this->repository->valuelists; |
|
29 | - $valuelists[] = $valueListObject; |
|
30 | - $this->repository->valuelists = $valuelists; |
|
31 | - $this->save(); |
|
32 | - } |
|
25 | + public function addValuelist($postValues) |
|
26 | + { |
|
27 | + $valueListObject = ValuelistFactory::createValuelistFromPostValues($postValues); |
|
28 | + $valuelists = $this->repository->valuelists; |
|
29 | + $valuelists[] = $valueListObject; |
|
30 | + $this->repository->valuelists = $valuelists; |
|
31 | + $this->save(); |
|
32 | + } |
|
33 | 33 | |
34 | - /** |
|
35 | - * Save changes to a valuelist |
|
36 | - * |
|
37 | - * @param $slug |
|
38 | - * @param $postValues |
|
39 | - * |
|
40 | - * @throws \Exception |
|
41 | - */ |
|
42 | - public function saveValuelist($slug, $postValues) |
|
43 | - { |
|
44 | - $valuelistObject = ValuelistFactory::createValuelistFromPostValues($postValues); |
|
34 | + /** |
|
35 | + * Save changes to a valuelist |
|
36 | + * |
|
37 | + * @param $slug |
|
38 | + * @param $postValues |
|
39 | + * |
|
40 | + * @throws \Exception |
|
41 | + */ |
|
42 | + public function saveValuelist($slug, $postValues) |
|
43 | + { |
|
44 | + $valuelistObject = ValuelistFactory::createValuelistFromPostValues($postValues); |
|
45 | 45 | |
46 | - $valuelists = $this->repository->valuelists; |
|
47 | - foreach ($valuelists as $key => $valuelist) { |
|
48 | - if ($valuelist->slug == $slug) { |
|
49 | - $valuelists[$key] = $valuelistObject; |
|
50 | - } |
|
51 | - } |
|
52 | - $this->repository->valuelists = $valuelists; |
|
53 | - $this->save(); |
|
54 | - } |
|
46 | + $valuelists = $this->repository->valuelists; |
|
47 | + foreach ($valuelists as $key => $valuelist) { |
|
48 | + if ($valuelist->slug == $slug) { |
|
49 | + $valuelists[$key] = $valuelistObject; |
|
50 | + } |
|
51 | + } |
|
52 | + $this->repository->valuelists = $valuelists; |
|
53 | + $this->save(); |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * Get a valuelist by its slug |
|
58 | - * |
|
59 | - * @param $slug |
|
60 | - * |
|
61 | - * @return mixed |
|
62 | - */ |
|
63 | - public function getValuelistBySlug($slug) |
|
64 | - { |
|
65 | - $valuelists = $this->repository->valuelists; |
|
66 | - foreach ($valuelists as $valuelist) { |
|
67 | - if ($valuelist->slug == $slug) { |
|
68 | - return $valuelist; |
|
69 | - } |
|
70 | - } |
|
56 | + /** |
|
57 | + * Get a valuelist by its slug |
|
58 | + * |
|
59 | + * @param $slug |
|
60 | + * |
|
61 | + * @return mixed |
|
62 | + */ |
|
63 | + public function getValuelistBySlug($slug) |
|
64 | + { |
|
65 | + $valuelists = $this->repository->valuelists; |
|
66 | + foreach ($valuelists as $valuelist) { |
|
67 | + if ($valuelist->slug == $slug) { |
|
68 | + return $valuelist; |
|
69 | + } |
|
70 | + } |
|
71 | 71 | |
72 | - return null; |
|
73 | - } |
|
72 | + return null; |
|
73 | + } |
|
74 | 74 | |
75 | - /** |
|
76 | - * Delete a sitemap item by its slug |
|
77 | - * |
|
78 | - * @param $slug |
|
79 | - * |
|
80 | - * @throws \Exception |
|
81 | - */ |
|
82 | - public function deleteValuelistBySlug($slug) |
|
83 | - { |
|
84 | - $valuelists = $this->repository->valuelists; |
|
85 | - foreach ($valuelists as $key => $valuelist) { |
|
86 | - if ($valuelist->slug == $slug) { |
|
87 | - unset($valuelists[$key]); |
|
88 | - } |
|
89 | - } |
|
90 | - $valuelists = array_values($valuelists); |
|
91 | - $this->repository->valuelists = $valuelists; |
|
92 | - $this->save(); |
|
93 | - } |
|
75 | + /** |
|
76 | + * Delete a sitemap item by its slug |
|
77 | + * |
|
78 | + * @param $slug |
|
79 | + * |
|
80 | + * @throws \Exception |
|
81 | + */ |
|
82 | + public function deleteValuelistBySlug($slug) |
|
83 | + { |
|
84 | + $valuelists = $this->repository->valuelists; |
|
85 | + foreach ($valuelists as $key => $valuelist) { |
|
86 | + if ($valuelist->slug == $slug) { |
|
87 | + unset($valuelists[$key]); |
|
88 | + } |
|
89 | + } |
|
90 | + $valuelists = array_values($valuelists); |
|
91 | + $this->repository->valuelists = $valuelists; |
|
92 | + $this->save(); |
|
93 | + } |
|
94 | 94 | } |
95 | 95 | \ No newline at end of file |
@@ -1,18 +1,18 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | namespace CloudControl\Cms\components { |
3 | 3 | |
4 | - use CloudControl\Cms\components\cms\ConfigurationRouting; |
|
5 | - use CloudControl\Cms\components\cms\DocumentRouting; |
|
6 | - use CloudControl\Cms\components\cms\FilesRouting; |
|
7 | - use CloudControl\Cms\components\cms\ImagesRouting; |
|
8 | - use CloudControl\Cms\components\cms\SearchRouting; |
|
9 | - use CloudControl\Cms\components\cms\SitemapRouting; |
|
10 | - use CloudControl\Cms\crypt\Crypt; |
|
11 | - use CloudControl\Cms\storage\Storage; |
|
12 | - |
|
13 | - class CmsComponent extends BaseComponent |
|
4 | + use CloudControl\Cms\components\cms\ConfigurationRouting; |
|
5 | + use CloudControl\Cms\components\cms\DocumentRouting; |
|
6 | + use CloudControl\Cms\components\cms\FilesRouting; |
|
7 | + use CloudControl\Cms\components\cms\ImagesRouting; |
|
8 | + use CloudControl\Cms\components\cms\SearchRouting; |
|
9 | + use CloudControl\Cms\components\cms\SitemapRouting; |
|
10 | + use CloudControl\Cms\crypt\Crypt; |
|
11 | + use CloudControl\Cms\storage\Storage; |
|
12 | + |
|
13 | + class CmsComponent extends BaseComponent |
|
14 | 14 | { |
15 | - /** |
|
15 | + /** |
|
16 | 16 | * @var \CloudControl\Cms\storage\Storage |
17 | 17 | */ |
18 | 18 | public $storage; |
@@ -41,8 +41,8 @@ discard block |
||
41 | 41 | const PARAMETER_IMAGE_SET = 'imageSet'; |
42 | 42 | const PARAMETER_MAIN_NAV_CLASS = 'mainNavClass'; |
43 | 43 | const PARAMETER_MY_BRICK_SLUG = 'myBrickSlug'; |
44 | - const PARAMETER_REDIRECT = 'redirect'; |
|
45 | - const PARAMETER_REDIRECTS = 'redirects'; |
|
44 | + const PARAMETER_REDIRECT = 'redirect'; |
|
45 | + const PARAMETER_REDIRECTS = 'redirects'; |
|
46 | 46 | const PARAMETER_SEARCH = 'search'; |
47 | 47 | const PARAMETER_SEARCH_LOG = "searchLog"; |
48 | 48 | const PARAMETER_SEARCH_NEEDS_UPDATE = "searchNeedsUpdate"; |
@@ -57,14 +57,14 @@ discard block |
||
57 | 57 | const PARAMETER_VALUELISTS = "valuelists"; |
58 | 58 | const PARAMETER_WHITELIST_IPS = 'whitelistIps'; |
59 | 59 | |
60 | - const POST_PARAMETER_COMPONENT = 'component'; |
|
61 | - const POST_PARAMETER_FROM_URL = "fromUrl"; |
|
62 | - const POST_PARAMETER_PASSWORD = 'password'; |
|
63 | - const POST_PARAMETER_SAVE = 'save'; |
|
64 | - const POST_PARAMETER_TEMPLATE = 'template'; |
|
65 | - const POST_PARAMETER_TITLE = 'title'; |
|
66 | - const POST_PARAMETER_TO_URL = "toUrl"; |
|
67 | - const POST_PARAMETER_USERNAME = 'username'; |
|
60 | + const POST_PARAMETER_COMPONENT = 'component'; |
|
61 | + const POST_PARAMETER_FROM_URL = "fromUrl"; |
|
62 | + const POST_PARAMETER_PASSWORD = 'password'; |
|
63 | + const POST_PARAMETER_SAVE = 'save'; |
|
64 | + const POST_PARAMETER_TEMPLATE = 'template'; |
|
65 | + const POST_PARAMETER_TITLE = 'title'; |
|
66 | + const POST_PARAMETER_TO_URL = "toUrl"; |
|
67 | + const POST_PARAMETER_USERNAME = 'username'; |
|
68 | 68 | |
69 | 69 | const GET_PARAMETER_PATH = 'path'; |
70 | 70 | const GET_PARAMETER_SLUG = 'slug'; |
@@ -369,9 +369,9 @@ discard block |
||
369 | 369 | } |
370 | 370 | } |
371 | 371 | |
372 | - protected function getTemplateDir($template, $application = null) |
|
373 | - { |
|
374 | - return __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . $template . '.php'; |
|
375 | - } |
|
376 | - } |
|
372 | + protected function getTemplateDir($template, $application = null) |
|
373 | + { |
|
374 | + return __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . $template . '.php'; |
|
375 | + } |
|
376 | + } |
|
377 | 377 | } |
378 | 378 | \ No newline at end of file |
@@ -2,277 +2,277 @@ |
||
2 | 2 | |
3 | 3 | namespace CloudControl\Cms\cc { |
4 | 4 | |
5 | - use CloudControl\Cms\components\Component; |
|
6 | - use CloudControl\Cms\storage\Storage; |
|
7 | - use Whoops\Handler\PrettyPageHandler; |
|
8 | - use Whoops\Run; |
|
9 | - |
|
10 | - class Application |
|
11 | - { |
|
12 | - /** |
|
13 | - * @var \stdClass |
|
14 | - */ |
|
15 | - private $config; |
|
16 | - /** |
|
17 | - * @var \CloudControl\Cms\storage\Storage |
|
18 | - */ |
|
19 | - private $storage; |
|
20 | - |
|
21 | - /** |
|
22 | - * @var \CloudControl\Cms\cc\Request |
|
23 | - */ |
|
24 | - private $request; |
|
25 | - |
|
26 | - /** |
|
27 | - * @var array |
|
28 | - */ |
|
29 | - private $matchedSitemapItems = array(); |
|
30 | - |
|
31 | - /** |
|
32 | - * @var array |
|
33 | - */ |
|
34 | - private $applicationComponents = array(); |
|
35 | - |
|
36 | - /** |
|
37 | - * Application constructor. |
|
38 | - */ |
|
39 | - public function __construct() |
|
40 | - { |
|
41 | - $this->config(); |
|
42 | - $this->storage(); |
|
43 | - |
|
44 | - $this->request = new Request(); |
|
45 | - |
|
46 | - $whoops = new Run; |
|
47 | - $whoops->pushHandler(new PrettyPageHandler); |
|
48 | - $whoops->register(); |
|
49 | - |
|
50 | - $this->redirectMatching($this->request); |
|
51 | - $this->sitemapMatching($this->request); |
|
52 | - |
|
53 | - $this->getApplicationComponents(); |
|
54 | - |
|
55 | - $this->runApplicationComponents(); |
|
56 | - $this->runSitemapComponents(); |
|
57 | - |
|
58 | - $this->renderApplicationComponents(); |
|
59 | - $this->renderSitemapComponents(); |
|
60 | - } |
|
61 | - |
|
62 | - /** |
|
63 | - * Initialize the config |
|
64 | - * |
|
65 | - * @throws \Exception |
|
66 | - */ |
|
67 | - private function config() |
|
68 | - { |
|
69 | - $configPath = __DIR__ . '/../../config.json'; |
|
70 | - if (realpath($configPath) !== false) { |
|
71 | - $json = file_get_contents($configPath); |
|
72 | - $this->config = json_decode($json); |
|
73 | - } else { |
|
74 | - throw new \Exception('Framework not initialized yet. Consider running composer install'); |
|
75 | - } |
|
76 | - } |
|
77 | - |
|
78 | - /** |
|
79 | - * Initialize the storage |
|
80 | - */ |
|
81 | - private function storage() |
|
82 | - { |
|
83 | - $this->storage = new Storage($this->config->rootDir . DIRECTORY_SEPARATOR . $this->config->storageDir, $this->config->imagesDir, $this->config->filesDir); |
|
84 | - } |
|
85 | - |
|
86 | - private function redirectMatching($request) |
|
87 | - { |
|
88 | - $redirects = $this->storage->getRedirects()->getRedirects(); |
|
89 | - $relativeUri = '/' . $request::$relativeUri; |
|
90 | - |
|
91 | - foreach ($redirects as $redirect) { |
|
92 | - if (preg_match_all($redirect->fromUrl, $relativeUri, $matches)) { |
|
93 | - $toUrl = preg_replace($redirect->fromUrl, $redirect->toUrl, $relativeUri); |
|
94 | - if (substr($toUrl, 0, 1) == '/') { |
|
95 | - $toUrl = substr($toUrl, 1); |
|
96 | - } |
|
97 | - if ($redirect->type == '301') { |
|
98 | - header('HTTP/1.1 301 Moved Permanently'); |
|
99 | - header('Location: ' . $request::$subfolders . $toUrl); |
|
100 | - exit; |
|
101 | - } elseif ($redirect->type == '302') { |
|
102 | - header('Location: ' . $request::$subfolders . $toUrl, true, 302); |
|
103 | - exit; |
|
104 | - } else { |
|
105 | - throw new \Exception('Invalid redirect type.'); |
|
106 | - } |
|
107 | - } |
|
108 | - } |
|
109 | - } |
|
110 | - |
|
111 | - /** |
|
112 | - * Loop through sitemap items and see if one matches the requestUri. |
|
113 | - * If it does, add it tot the matchedSitemapItems array |
|
114 | - * |
|
115 | - * @param $request |
|
116 | - */ |
|
117 | - private function sitemapMatching($request) |
|
118 | - { |
|
119 | - $sitemap = $this->storage->getSitemap()->getSitemap(); |
|
120 | - $relativeUri = '/' . $request::$relativeUri; |
|
121 | - |
|
122 | - foreach ($sitemap as $sitemapItem) { |
|
123 | - if ($sitemapItem->regex) { |
|
124 | - $matches = array(); |
|
125 | - if (preg_match_all($sitemapItem->url, $relativeUri, $matches)) { |
|
126 | - // Make a clone, so it doesnt add the matches to the original |
|
127 | - $matchedClone = clone $sitemapItem; |
|
128 | - $matchedClone->matches = $matches; |
|
129 | - $this->matchedSitemapItems[] = $matchedClone; |
|
130 | - return; |
|
131 | - } |
|
132 | - } else { |
|
133 | - if ($sitemapItem->url == $relativeUri) { |
|
134 | - $this->matchedSitemapItems[] = $sitemapItem; |
|
135 | - return; |
|
136 | - } |
|
137 | - } |
|
138 | - } |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * Loop through all application components and run them |
|
143 | - * |
|
144 | - * @throws \Exception |
|
145 | - */ |
|
146 | - private function runApplicationComponents() |
|
147 | - { |
|
148 | - foreach ($this->applicationComponents as $key => $applicationComponent) { |
|
149 | - $class = $applicationComponent->component; |
|
150 | - $parameters = $applicationComponent->parameters; |
|
151 | - $this->applicationComponents[$key]->{'object'} = $this->getComponentObject($class, null, $parameters, null); |
|
152 | - $this->applicationComponents[$key]->{'object'}->run($this->storage); |
|
153 | - } |
|
154 | - } |
|
155 | - |
|
156 | - /** |
|
157 | - * Loop through all (matched) sitemap components and run them |
|
158 | - * |
|
159 | - * @throws \Exception |
|
160 | - */ |
|
161 | - private function runSitemapComponents() |
|
162 | - { |
|
163 | - foreach ($this->matchedSitemapItems as $key => $sitemapItem) { |
|
164 | - $class = $sitemapItem->component; |
|
165 | - $template = $sitemapItem->template; |
|
166 | - $parameters = $sitemapItem->parameters; |
|
167 | - |
|
168 | - $this->matchedSitemapItems[$key]->object = $this->getComponentObject($class, $template, $parameters, $sitemapItem); |
|
169 | - |
|
170 | - $this->matchedSitemapItems[$key]->object->run($this->storage); |
|
171 | - } |
|
172 | - } |
|
173 | - |
|
174 | - /** |
|
175 | - * @param string $class |
|
176 | - * @param string $template |
|
177 | - * @param array $parameters |
|
178 | - * @param \stdClass|null $matchedSitemapItem |
|
179 | - * |
|
180 | - * @return mixed |
|
181 | - * @throws \Exception |
|
182 | - */ |
|
183 | - private function getComponentObject($class = '', $template = '', $parameters = array(), $matchedSitemapItem) |
|
184 | - { |
|
185 | - $libraryComponentName = '\\CloudControl\Cms\\components\\' . $class; |
|
186 | - $userComponentName = '\\components\\' . $class; |
|
187 | - |
|
188 | - if (!class_exists($libraryComponentName, false)) { |
|
189 | - $component = new $libraryComponentName($template, $this->request, $parameters, $matchedSitemapItem); |
|
190 | - } elseif (!class_exists($userComponentName, false)) { |
|
191 | - $component = new $userComponentName($template, $this->request, $parameters, $matchedSitemapItem); |
|
192 | - } else { |
|
193 | - throw new \Exception('Could not load component ' . $class); |
|
194 | - } |
|
195 | - |
|
196 | - if (!$component instanceof Component) { |
|
197 | - throw new \Exception('Component not of type Component. Must inherit \CloudControl\Cms\components\Component'); |
|
198 | - } |
|
199 | - |
|
200 | - return $component; |
|
201 | - } |
|
202 | - |
|
203 | - /** |
|
204 | - * Loop through all application components and render them |
|
205 | - */ |
|
206 | - private function renderApplicationComponents() |
|
207 | - { |
|
208 | - foreach ($this->applicationComponents as $applicationComponent) { |
|
209 | - $applicationComponent->{'object'}->render(); |
|
210 | - } |
|
211 | - } |
|
212 | - |
|
213 | - /** |
|
214 | - * Loop through all (matched) sitemap components and render them |
|
215 | - */ |
|
216 | - private function renderSitemapComponents() |
|
217 | - { |
|
218 | - foreach ($this->matchedSitemapItems as $sitemapItem) { |
|
219 | - $this->setCachingHeaders(); |
|
220 | - $sitemapItem->object->render($this); |
|
221 | - ob_clean(); |
|
222 | - echo $sitemapItem->object->get(); |
|
223 | - ob_end_flush(); |
|
224 | - exit; |
|
225 | - } |
|
226 | - } |
|
227 | - |
|
228 | - public function getAllApplicationComponentParameters() |
|
229 | - { |
|
230 | - $allParameters = array(); |
|
231 | - foreach ($this->applicationComponents as $applicationComponent) { |
|
232 | - $parameters = $applicationComponent->{'object'}->getParameters(); |
|
233 | - $allParameters[] = $parameters; |
|
234 | - } |
|
235 | - return $allParameters; |
|
236 | - } |
|
237 | - |
|
238 | - public function unlockApplicationComponentParameters() |
|
239 | - { |
|
240 | - foreach ($this->applicationComponents as $applicationComponent) { |
|
241 | - $parameters = $applicationComponent->{'object'}->getParameters(); |
|
242 | - extract($parameters); |
|
243 | - } |
|
244 | - } |
|
245 | - |
|
246 | - /** |
|
247 | - * Set the default caching of pages to 2 days |
|
248 | - */ |
|
249 | - public function setCachingHeaders() |
|
250 | - { |
|
251 | - header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', time() + (60 * 60 * 24 * 2))); // 2 days |
|
252 | - header("Cache-Control: max-age=" . (60 * 60 * 24 * 2)); |
|
253 | - } |
|
254 | - |
|
255 | - /** |
|
256 | - * @return string |
|
257 | - */ |
|
258 | - public function getTemplateDir() |
|
259 | - { |
|
260 | - return $this->config->templateDir; |
|
261 | - } |
|
262 | - |
|
263 | - public function getStorageDir() |
|
264 | - { |
|
265 | - return $this->config->storageDir; |
|
266 | - } |
|
267 | - |
|
268 | - public function getApplicationComponents() |
|
269 | - { |
|
270 | - $this->applicationComponents = $this->storage->getApplicationComponents()->getApplicationComponents(); |
|
271 | - } |
|
272 | - |
|
273 | - public function getRootDir() |
|
274 | - { |
|
275 | - return $this->config->rootDir; |
|
276 | - } |
|
277 | - } |
|
5 | + use CloudControl\Cms\components\Component; |
|
6 | + use CloudControl\Cms\storage\Storage; |
|
7 | + use Whoops\Handler\PrettyPageHandler; |
|
8 | + use Whoops\Run; |
|
9 | + |
|
10 | + class Application |
|
11 | + { |
|
12 | + /** |
|
13 | + * @var \stdClass |
|
14 | + */ |
|
15 | + private $config; |
|
16 | + /** |
|
17 | + * @var \CloudControl\Cms\storage\Storage |
|
18 | + */ |
|
19 | + private $storage; |
|
20 | + |
|
21 | + /** |
|
22 | + * @var \CloudControl\Cms\cc\Request |
|
23 | + */ |
|
24 | + private $request; |
|
25 | + |
|
26 | + /** |
|
27 | + * @var array |
|
28 | + */ |
|
29 | + private $matchedSitemapItems = array(); |
|
30 | + |
|
31 | + /** |
|
32 | + * @var array |
|
33 | + */ |
|
34 | + private $applicationComponents = array(); |
|
35 | + |
|
36 | + /** |
|
37 | + * Application constructor. |
|
38 | + */ |
|
39 | + public function __construct() |
|
40 | + { |
|
41 | + $this->config(); |
|
42 | + $this->storage(); |
|
43 | + |
|
44 | + $this->request = new Request(); |
|
45 | + |
|
46 | + $whoops = new Run; |
|
47 | + $whoops->pushHandler(new PrettyPageHandler); |
|
48 | + $whoops->register(); |
|
49 | + |
|
50 | + $this->redirectMatching($this->request); |
|
51 | + $this->sitemapMatching($this->request); |
|
52 | + |
|
53 | + $this->getApplicationComponents(); |
|
54 | + |
|
55 | + $this->runApplicationComponents(); |
|
56 | + $this->runSitemapComponents(); |
|
57 | + |
|
58 | + $this->renderApplicationComponents(); |
|
59 | + $this->renderSitemapComponents(); |
|
60 | + } |
|
61 | + |
|
62 | + /** |
|
63 | + * Initialize the config |
|
64 | + * |
|
65 | + * @throws \Exception |
|
66 | + */ |
|
67 | + private function config() |
|
68 | + { |
|
69 | + $configPath = __DIR__ . '/../../config.json'; |
|
70 | + if (realpath($configPath) !== false) { |
|
71 | + $json = file_get_contents($configPath); |
|
72 | + $this->config = json_decode($json); |
|
73 | + } else { |
|
74 | + throw new \Exception('Framework not initialized yet. Consider running composer install'); |
|
75 | + } |
|
76 | + } |
|
77 | + |
|
78 | + /** |
|
79 | + * Initialize the storage |
|
80 | + */ |
|
81 | + private function storage() |
|
82 | + { |
|
83 | + $this->storage = new Storage($this->config->rootDir . DIRECTORY_SEPARATOR . $this->config->storageDir, $this->config->imagesDir, $this->config->filesDir); |
|
84 | + } |
|
85 | + |
|
86 | + private function redirectMatching($request) |
|
87 | + { |
|
88 | + $redirects = $this->storage->getRedirects()->getRedirects(); |
|
89 | + $relativeUri = '/' . $request::$relativeUri; |
|
90 | + |
|
91 | + foreach ($redirects as $redirect) { |
|
92 | + if (preg_match_all($redirect->fromUrl, $relativeUri, $matches)) { |
|
93 | + $toUrl = preg_replace($redirect->fromUrl, $redirect->toUrl, $relativeUri); |
|
94 | + if (substr($toUrl, 0, 1) == '/') { |
|
95 | + $toUrl = substr($toUrl, 1); |
|
96 | + } |
|
97 | + if ($redirect->type == '301') { |
|
98 | + header('HTTP/1.1 301 Moved Permanently'); |
|
99 | + header('Location: ' . $request::$subfolders . $toUrl); |
|
100 | + exit; |
|
101 | + } elseif ($redirect->type == '302') { |
|
102 | + header('Location: ' . $request::$subfolders . $toUrl, true, 302); |
|
103 | + exit; |
|
104 | + } else { |
|
105 | + throw new \Exception('Invalid redirect type.'); |
|
106 | + } |
|
107 | + } |
|
108 | + } |
|
109 | + } |
|
110 | + |
|
111 | + /** |
|
112 | + * Loop through sitemap items and see if one matches the requestUri. |
|
113 | + * If it does, add it tot the matchedSitemapItems array |
|
114 | + * |
|
115 | + * @param $request |
|
116 | + */ |
|
117 | + private function sitemapMatching($request) |
|
118 | + { |
|
119 | + $sitemap = $this->storage->getSitemap()->getSitemap(); |
|
120 | + $relativeUri = '/' . $request::$relativeUri; |
|
121 | + |
|
122 | + foreach ($sitemap as $sitemapItem) { |
|
123 | + if ($sitemapItem->regex) { |
|
124 | + $matches = array(); |
|
125 | + if (preg_match_all($sitemapItem->url, $relativeUri, $matches)) { |
|
126 | + // Make a clone, so it doesnt add the matches to the original |
|
127 | + $matchedClone = clone $sitemapItem; |
|
128 | + $matchedClone->matches = $matches; |
|
129 | + $this->matchedSitemapItems[] = $matchedClone; |
|
130 | + return; |
|
131 | + } |
|
132 | + } else { |
|
133 | + if ($sitemapItem->url == $relativeUri) { |
|
134 | + $this->matchedSitemapItems[] = $sitemapItem; |
|
135 | + return; |
|
136 | + } |
|
137 | + } |
|
138 | + } |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * Loop through all application components and run them |
|
143 | + * |
|
144 | + * @throws \Exception |
|
145 | + */ |
|
146 | + private function runApplicationComponents() |
|
147 | + { |
|
148 | + foreach ($this->applicationComponents as $key => $applicationComponent) { |
|
149 | + $class = $applicationComponent->component; |
|
150 | + $parameters = $applicationComponent->parameters; |
|
151 | + $this->applicationComponents[$key]->{'object'} = $this->getComponentObject($class, null, $parameters, null); |
|
152 | + $this->applicationComponents[$key]->{'object'}->run($this->storage); |
|
153 | + } |
|
154 | + } |
|
155 | + |
|
156 | + /** |
|
157 | + * Loop through all (matched) sitemap components and run them |
|
158 | + * |
|
159 | + * @throws \Exception |
|
160 | + */ |
|
161 | + private function runSitemapComponents() |
|
162 | + { |
|
163 | + foreach ($this->matchedSitemapItems as $key => $sitemapItem) { |
|
164 | + $class = $sitemapItem->component; |
|
165 | + $template = $sitemapItem->template; |
|
166 | + $parameters = $sitemapItem->parameters; |
|
167 | + |
|
168 | + $this->matchedSitemapItems[$key]->object = $this->getComponentObject($class, $template, $parameters, $sitemapItem); |
|
169 | + |
|
170 | + $this->matchedSitemapItems[$key]->object->run($this->storage); |
|
171 | + } |
|
172 | + } |
|
173 | + |
|
174 | + /** |
|
175 | + * @param string $class |
|
176 | + * @param string $template |
|
177 | + * @param array $parameters |
|
178 | + * @param \stdClass|null $matchedSitemapItem |
|
179 | + * |
|
180 | + * @return mixed |
|
181 | + * @throws \Exception |
|
182 | + */ |
|
183 | + private function getComponentObject($class = '', $template = '', $parameters = array(), $matchedSitemapItem) |
|
184 | + { |
|
185 | + $libraryComponentName = '\\CloudControl\Cms\\components\\' . $class; |
|
186 | + $userComponentName = '\\components\\' . $class; |
|
187 | + |
|
188 | + if (!class_exists($libraryComponentName, false)) { |
|
189 | + $component = new $libraryComponentName($template, $this->request, $parameters, $matchedSitemapItem); |
|
190 | + } elseif (!class_exists($userComponentName, false)) { |
|
191 | + $component = new $userComponentName($template, $this->request, $parameters, $matchedSitemapItem); |
|
192 | + } else { |
|
193 | + throw new \Exception('Could not load component ' . $class); |
|
194 | + } |
|
195 | + |
|
196 | + if (!$component instanceof Component) { |
|
197 | + throw new \Exception('Component not of type Component. Must inherit \CloudControl\Cms\components\Component'); |
|
198 | + } |
|
199 | + |
|
200 | + return $component; |
|
201 | + } |
|
202 | + |
|
203 | + /** |
|
204 | + * Loop through all application components and render them |
|
205 | + */ |
|
206 | + private function renderApplicationComponents() |
|
207 | + { |
|
208 | + foreach ($this->applicationComponents as $applicationComponent) { |
|
209 | + $applicationComponent->{'object'}->render(); |
|
210 | + } |
|
211 | + } |
|
212 | + |
|
213 | + /** |
|
214 | + * Loop through all (matched) sitemap components and render them |
|
215 | + */ |
|
216 | + private function renderSitemapComponents() |
|
217 | + { |
|
218 | + foreach ($this->matchedSitemapItems as $sitemapItem) { |
|
219 | + $this->setCachingHeaders(); |
|
220 | + $sitemapItem->object->render($this); |
|
221 | + ob_clean(); |
|
222 | + echo $sitemapItem->object->get(); |
|
223 | + ob_end_flush(); |
|
224 | + exit; |
|
225 | + } |
|
226 | + } |
|
227 | + |
|
228 | + public function getAllApplicationComponentParameters() |
|
229 | + { |
|
230 | + $allParameters = array(); |
|
231 | + foreach ($this->applicationComponents as $applicationComponent) { |
|
232 | + $parameters = $applicationComponent->{'object'}->getParameters(); |
|
233 | + $allParameters[] = $parameters; |
|
234 | + } |
|
235 | + return $allParameters; |
|
236 | + } |
|
237 | + |
|
238 | + public function unlockApplicationComponentParameters() |
|
239 | + { |
|
240 | + foreach ($this->applicationComponents as $applicationComponent) { |
|
241 | + $parameters = $applicationComponent->{'object'}->getParameters(); |
|
242 | + extract($parameters); |
|
243 | + } |
|
244 | + } |
|
245 | + |
|
246 | + /** |
|
247 | + * Set the default caching of pages to 2 days |
|
248 | + */ |
|
249 | + public function setCachingHeaders() |
|
250 | + { |
|
251 | + header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', time() + (60 * 60 * 24 * 2))); // 2 days |
|
252 | + header("Cache-Control: max-age=" . (60 * 60 * 24 * 2)); |
|
253 | + } |
|
254 | + |
|
255 | + /** |
|
256 | + * @return string |
|
257 | + */ |
|
258 | + public function getTemplateDir() |
|
259 | + { |
|
260 | + return $this->config->templateDir; |
|
261 | + } |
|
262 | + |
|
263 | + public function getStorageDir() |
|
264 | + { |
|
265 | + return $this->config->storageDir; |
|
266 | + } |
|
267 | + |
|
268 | + public function getApplicationComponents() |
|
269 | + { |
|
270 | + $this->applicationComponents = $this->storage->getApplicationComponents()->getApplicationComponents(); |
|
271 | + } |
|
272 | + |
|
273 | + public function getRootDir() |
|
274 | + { |
|
275 | + return $this->config->rootDir; |
|
276 | + } |
|
277 | + } |
|
278 | 278 | } |
279 | 279 | \ No newline at end of file |