@@ -83,6 +83,9 @@ discard block |
||
83 | 83 | $this->storage = new Storage($this->getStorageDir(), $this->config->imagesDir, $this->config->filesDir); |
84 | 84 | } |
85 | 85 | |
86 | + /** |
|
87 | + * @param Request $request |
|
88 | + */ |
|
86 | 89 | private function redirectMatching($request) |
87 | 90 | { |
88 | 91 | $redirects = $this->storage->getRedirects()->getRedirects(); |
@@ -112,7 +115,7 @@ discard block |
||
112 | 115 | * Loop through sitemap items and see if one matches the requestUri. |
113 | 116 | * If it does, add it tot the matchedSitemapItems array |
114 | 117 | * |
115 | - * @param $request |
|
118 | + * @param Request $request |
|
116 | 119 | */ |
117 | 120 | private function sitemapMatching($request) |
118 | 121 | { |
@@ -177,7 +180,7 @@ discard block |
||
177 | 180 | * @param array $parameters |
178 | 181 | * @param \stdClass|null $matchedSitemapItem |
179 | 182 | * |
180 | - * @return mixed |
|
183 | + * @return Component |
|
181 | 184 | * @throws \Exception |
182 | 185 | */ |
183 | 186 | private function getComponentObject($class = '', $template = '', $parameters = array(), $matchedSitemapItem) |
@@ -268,6 +271,9 @@ discard block |
||
268 | 271 | return $this->config->templateDir; |
269 | 272 | } |
270 | 273 | |
274 | + /** |
|
275 | + * @return string |
|
276 | + */ |
|
271 | 277 | public function getStorageDir() |
272 | 278 | { |
273 | 279 | return $this->config->storageDir; |
@@ -141,8 +141,8 @@ |
||
141 | 141 | } |
142 | 142 | |
143 | 143 | /** |
144 | - * @param $template |
|
145 | - * @param null $application |
|
144 | + * @param string $template |
|
145 | + * @param null|Application $application |
|
146 | 146 | * @return string |
147 | 147 | */ |
148 | 148 | protected function getTemplatePath($template, $application=null) |
@@ -6,139 +6,139 @@ |
||
6 | 6 | use CloudControl\Cms\storage\Storage; |
7 | 7 | |
8 | 8 | class BaseComponent implements Component |
9 | - { |
|
10 | - /** |
|
11 | - * @var string |
|
12 | - */ |
|
13 | - protected $template; |
|
14 | - /** |
|
15 | - * @var \CloudControl\Cms\cc\Request |
|
16 | - */ |
|
17 | - protected $request; |
|
18 | - /** |
|
19 | - * @var Storage |
|
20 | - */ |
|
21 | - protected $storage; |
|
22 | - /** |
|
23 | - * @var mixed |
|
24 | - */ |
|
25 | - protected $renderedContent; |
|
26 | - /** |
|
27 | - * @var array |
|
28 | - */ |
|
29 | - protected $parameters = array(); |
|
30 | - /*** |
|
9 | + { |
|
10 | + /** |
|
11 | + * @var string |
|
12 | + */ |
|
13 | + protected $template; |
|
14 | + /** |
|
15 | + * @var \CloudControl\Cms\cc\Request |
|
16 | + */ |
|
17 | + protected $request; |
|
18 | + /** |
|
19 | + * @var Storage |
|
20 | + */ |
|
21 | + protected $storage; |
|
22 | + /** |
|
23 | + * @var mixed |
|
24 | + */ |
|
25 | + protected $renderedContent; |
|
26 | + /** |
|
27 | + * @var array |
|
28 | + */ |
|
29 | + protected $parameters = array(); |
|
30 | + /*** |
|
31 | 31 | * @var \stdClass |
32 | 32 | */ |
33 | - protected $matchedSitemapItem; |
|
33 | + protected $matchedSitemapItem; |
|
34 | 34 | |
35 | - /** |
|
36 | - * BaseComponent constructor. |
|
37 | - * |
|
38 | - * @param string $template |
|
39 | - * @param Request $request |
|
40 | - * @param array $parameters |
|
41 | - * @param $matchedSitemapItem |
|
42 | - */ |
|
43 | - public function __construct($template='', Request $request, $parameters=array(), $matchedSitemapItem) |
|
44 | - { |
|
45 | - $this->template = $template; |
|
46 | - $this->request = $request; |
|
47 | - $this->parameters = (array) $parameters; |
|
48 | - $this->matchedSitemapItem = $matchedSitemapItem; |
|
49 | - } |
|
35 | + /** |
|
36 | + * BaseComponent constructor. |
|
37 | + * |
|
38 | + * @param string $template |
|
39 | + * @param Request $request |
|
40 | + * @param array $parameters |
|
41 | + * @param $matchedSitemapItem |
|
42 | + */ |
|
43 | + public function __construct($template='', Request $request, $parameters=array(), $matchedSitemapItem) |
|
44 | + { |
|
45 | + $this->template = $template; |
|
46 | + $this->request = $request; |
|
47 | + $this->parameters = (array) $parameters; |
|
48 | + $this->matchedSitemapItem = $matchedSitemapItem; |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * Hook for implementation in derived classes |
|
53 | - * |
|
54 | - * @param Storage $storage |
|
55 | - */ |
|
56 | - public function run(Storage $storage) |
|
57 | - { |
|
58 | - $this->storage = $storage; |
|
59 | - } |
|
51 | + /** |
|
52 | + * Hook for implementation in derived classes |
|
53 | + * |
|
54 | + * @param Storage $storage |
|
55 | + */ |
|
56 | + public function run(Storage $storage) |
|
57 | + { |
|
58 | + $this->storage = $storage; |
|
59 | + } |
|
60 | 60 | |
61 | - /** |
|
62 | - * Renders the template |
|
63 | - * |
|
64 | - * @param null|Application $application |
|
65 | - * |
|
66 | - * @throws \Exception |
|
67 | - */ |
|
68 | - public function render($application=null) |
|
69 | - { |
|
70 | - $this->renderedContent = $this->renderTemplate($this->template, true, $application); |
|
71 | - } |
|
61 | + /** |
|
62 | + * Renders the template |
|
63 | + * |
|
64 | + * @param null|Application $application |
|
65 | + * |
|
66 | + * @throws \Exception |
|
67 | + */ |
|
68 | + public function render($application=null) |
|
69 | + { |
|
70 | + $this->renderedContent = $this->renderTemplate($this->template, true, $application); |
|
71 | + } |
|
72 | 72 | |
73 | - /** |
|
74 | - * Returns the rendered content |
|
75 | - * |
|
76 | - * @return mixed |
|
77 | - */ |
|
78 | - public function get() |
|
79 | - { |
|
80 | - return $this->renderedContent; |
|
81 | - } |
|
73 | + /** |
|
74 | + * Returns the rendered content |
|
75 | + * |
|
76 | + * @return mixed |
|
77 | + */ |
|
78 | + public function get() |
|
79 | + { |
|
80 | + return $this->renderedContent; |
|
81 | + } |
|
82 | 82 | |
83 | - /** |
|
84 | - * Decoupled render method, for usage in derived classes |
|
85 | - * |
|
86 | - * @param string $template |
|
87 | - * |
|
88 | - * @param bool $obClean |
|
89 | - * @param null|Application $application |
|
90 | - * |
|
91 | - * @return string |
|
92 | - * @throws \Exception |
|
93 | - */ |
|
94 | - public function renderTemplate($template='', $obClean = true, $application=null) |
|
95 | - { |
|
96 | - $templatePath = $this->getTemplatePath($template, $application); |
|
97 | - if (realpath($templatePath) !== false) { |
|
98 | - if ($obClean) { |
|
99 | - ob_clean(); |
|
100 | - } |
|
101 | - $this->parameters['request'] = $this->request; |
|
102 | - if ($application !== null) { |
|
103 | - $acParameters = $application->getAllApplicationComponentParameters(); |
|
104 | - foreach ($acParameters as $parameters) { |
|
105 | - extract($parameters); |
|
106 | - } |
|
107 | - } |
|
108 | - extract($this->parameters); |
|
109 | - include($templatePath); |
|
110 | - return ob_get_contents(); |
|
111 | - } else { |
|
112 | - if ($template !== null) { // If template is null, its a application component, which doesnt have a template |
|
113 | - throw new \Exception('Couldnt find template ' . $templatePath); |
|
114 | - } |
|
115 | - } |
|
116 | - } |
|
83 | + /** |
|
84 | + * Decoupled render method, for usage in derived classes |
|
85 | + * |
|
86 | + * @param string $template |
|
87 | + * |
|
88 | + * @param bool $obClean |
|
89 | + * @param null|Application $application |
|
90 | + * |
|
91 | + * @return string |
|
92 | + * @throws \Exception |
|
93 | + */ |
|
94 | + public function renderTemplate($template='', $obClean = true, $application=null) |
|
95 | + { |
|
96 | + $templatePath = $this->getTemplatePath($template, $application); |
|
97 | + if (realpath($templatePath) !== false) { |
|
98 | + if ($obClean) { |
|
99 | + ob_clean(); |
|
100 | + } |
|
101 | + $this->parameters['request'] = $this->request; |
|
102 | + if ($application !== null) { |
|
103 | + $acParameters = $application->getAllApplicationComponentParameters(); |
|
104 | + foreach ($acParameters as $parameters) { |
|
105 | + extract($parameters); |
|
106 | + } |
|
107 | + } |
|
108 | + extract($this->parameters); |
|
109 | + include($templatePath); |
|
110 | + return ob_get_contents(); |
|
111 | + } else { |
|
112 | + if ($template !== null) { // If template is null, its a application component, which doesnt have a template |
|
113 | + throw new \Exception('Couldnt find template ' . $templatePath); |
|
114 | + } |
|
115 | + } |
|
116 | + } |
|
117 | 117 | |
118 | - /** |
|
119 | - * Alias for renderTemplate for usage to include templates in other templates |
|
120 | - * |
|
121 | - * @param string $template |
|
122 | - * |
|
123 | - * @param array $parameters |
|
124 | - * |
|
125 | - * @return string |
|
126 | - * @throws \Exception |
|
127 | - */ |
|
128 | - public function includeTemplate($template='', $parameters = array()) |
|
129 | - { |
|
130 | - if (is_array($parameters)) { |
|
131 | - foreach ($parameters as $name => $value) { |
|
132 | - $this->parameters[$name] = $value; |
|
133 | - } |
|
134 | - } |
|
135 | - return $this->renderTemplate($template, false); |
|
136 | - } |
|
118 | + /** |
|
119 | + * Alias for renderTemplate for usage to include templates in other templates |
|
120 | + * |
|
121 | + * @param string $template |
|
122 | + * |
|
123 | + * @param array $parameters |
|
124 | + * |
|
125 | + * @return string |
|
126 | + * @throws \Exception |
|
127 | + */ |
|
128 | + public function includeTemplate($template='', $parameters = array()) |
|
129 | + { |
|
130 | + if (is_array($parameters)) { |
|
131 | + foreach ($parameters as $name => $value) { |
|
132 | + $this->parameters[$name] = $value; |
|
133 | + } |
|
134 | + } |
|
135 | + return $this->renderTemplate($template, false); |
|
136 | + } |
|
137 | 137 | |
138 | - public function getParameters() |
|
139 | - { |
|
140 | - return $this->parameters; |
|
141 | - } |
|
138 | + public function getParameters() |
|
139 | + { |
|
140 | + return $this->parameters; |
|
141 | + } |
|
142 | 142 | |
143 | 143 | /** |
144 | 144 | * @param $template |
@@ -40,7 +40,7 @@ discard block |
||
40 | 40 | * @param array $parameters |
41 | 41 | * @param $matchedSitemapItem |
42 | 42 | */ |
43 | - public function __construct($template='', Request $request, $parameters=array(), $matchedSitemapItem) |
|
43 | + public function __construct($template = '', Request $request, $parameters = array(), $matchedSitemapItem) |
|
44 | 44 | { |
45 | 45 | $this->template = $template; |
46 | 46 | $this->request = $request; |
@@ -65,7 +65,7 @@ discard block |
||
65 | 65 | * |
66 | 66 | * @throws \Exception |
67 | 67 | */ |
68 | - public function render($application=null) |
|
68 | + public function render($application = null) |
|
69 | 69 | { |
70 | 70 | $this->renderedContent = $this->renderTemplate($this->template, true, $application); |
71 | 71 | } |
@@ -91,7 +91,7 @@ discard block |
||
91 | 91 | * @return string |
92 | 92 | * @throws \Exception |
93 | 93 | */ |
94 | - public function renderTemplate($template='', $obClean = true, $application=null) |
|
94 | + public function renderTemplate($template = '', $obClean = true, $application = null) |
|
95 | 95 | { |
96 | 96 | $templatePath = $this->getTemplatePath($template, $application); |
97 | 97 | if (realpath($templatePath) !== false) { |
@@ -125,7 +125,7 @@ discard block |
||
125 | 125 | * @return string |
126 | 126 | * @throws \Exception |
127 | 127 | */ |
128 | - public function includeTemplate($template='', $parameters = array()) |
|
128 | + public function includeTemplate($template = '', $parameters = array()) |
|
129 | 129 | { |
130 | 130 | if (is_array($parameters)) { |
131 | 131 | foreach ($parameters as $name => $value) { |
@@ -145,7 +145,7 @@ discard block |
||
145 | 145 | * @param null $application |
146 | 146 | * @return string |
147 | 147 | */ |
148 | - protected function getTemplatePath($template, $application=null) |
|
148 | + protected function getTemplatePath($template, $application = null) |
|
149 | 149 | { |
150 | 150 | $templatePath = $application->getTemplatePath(); |
151 | 151 | $templatePath = $templatePath . DIRECTORY_SEPARATOR . $template . '.php'; |
@@ -68,7 +68,7 @@ discard block |
||
68 | 68 | } |
69 | 69 | |
70 | 70 | /** |
71 | - * @param $request |
|
71 | + * @param Request $request |
|
72 | 72 | * @param CmsComponent $cmsComponent |
73 | 73 | */ |
74 | 74 | private function newRoute($request, $cmsComponent) |
@@ -83,7 +83,7 @@ discard block |
||
83 | 83 | } |
84 | 84 | |
85 | 85 | /** |
86 | - * @param $request |
|
86 | + * @param Request $request |
|
87 | 87 | * @param CmsComponent $cmsComponent |
88 | 88 | */ |
89 | 89 | private function deleteRoute($request, $cmsComponent) |
@@ -12,86 +12,86 @@ |
||
12 | 12 | |
13 | 13 | class FilesRouting implements CmsRouting |
14 | 14 | { |
15 | - /** |
|
16 | - * FilesRouting constructor. |
|
17 | - * |
|
18 | - * @param Request $request |
|
19 | - * @param $relativeCmsUri |
|
20 | - * @param CmsComponent $cmsComponent |
|
21 | - */ |
|
22 | - public function __construct($request, $relativeCmsUri, $cmsComponent) |
|
23 | - { |
|
24 | - if ($relativeCmsUri == '/files') { |
|
25 | - $this->overviewRoute($cmsComponent); |
|
26 | - } elseif ($relativeCmsUri == '/files/new') { |
|
27 | - $this->newRoute($request, $cmsComponent); |
|
28 | - } elseif ($relativeCmsUri == '/files/get' && isset($request::$get[CmsComponent::FILES_PARAMETER_FILE])) { |
|
29 | - $this->downloadRoute($request::$get[CmsComponent::FILES_PARAMETER_FILE], $cmsComponent); |
|
30 | - } elseif ($relativeCmsUri == '/files/delete' && isset($request::$get[CmsComponent::FILES_PARAMETER_FILE])) { |
|
31 | - $this->deleteRoute($request, $cmsComponent); |
|
32 | - } |
|
33 | - } |
|
15 | + /** |
|
16 | + * FilesRouting constructor. |
|
17 | + * |
|
18 | + * @param Request $request |
|
19 | + * @param $relativeCmsUri |
|
20 | + * @param CmsComponent $cmsComponent |
|
21 | + */ |
|
22 | + public function __construct($request, $relativeCmsUri, $cmsComponent) |
|
23 | + { |
|
24 | + if ($relativeCmsUri == '/files') { |
|
25 | + $this->overviewRoute($cmsComponent); |
|
26 | + } elseif ($relativeCmsUri == '/files/new') { |
|
27 | + $this->newRoute($request, $cmsComponent); |
|
28 | + } elseif ($relativeCmsUri == '/files/get' && isset($request::$get[CmsComponent::FILES_PARAMETER_FILE])) { |
|
29 | + $this->downloadRoute($request::$get[CmsComponent::FILES_PARAMETER_FILE], $cmsComponent); |
|
30 | + } elseif ($relativeCmsUri == '/files/delete' && isset($request::$get[CmsComponent::FILES_PARAMETER_FILE])) { |
|
31 | + $this->deleteRoute($request, $cmsComponent); |
|
32 | + } |
|
33 | + } |
|
34 | 34 | |
35 | - /** |
|
36 | - * @param $slug |
|
37 | - * @param CmsComponent $cmsComponent |
|
38 | - */ |
|
39 | - private function downloadRoute($slug, $cmsComponent) |
|
40 | - { |
|
41 | - $file = $cmsComponent->storage->getFiles()->getFileByName($slug); |
|
42 | - // TODO FIX THIS PATH |
|
43 | - $path = realpath(__DIR__ . '/../www/files/'); |
|
44 | - $quoted = sprintf('"%s"', addcslashes(basename($path . '/' . $file->file), '"\\')); |
|
45 | - $size = filesize($path . '/' . $file->file); |
|
35 | + /** |
|
36 | + * @param $slug |
|
37 | + * @param CmsComponent $cmsComponent |
|
38 | + */ |
|
39 | + private function downloadRoute($slug, $cmsComponent) |
|
40 | + { |
|
41 | + $file = $cmsComponent->storage->getFiles()->getFileByName($slug); |
|
42 | + // TODO FIX THIS PATH |
|
43 | + $path = realpath(__DIR__ . '/../www/files/'); |
|
44 | + $quoted = sprintf('"%s"', addcslashes(basename($path . '/' . $file->file), '"\\')); |
|
45 | + $size = filesize($path . '/' . $file->file); |
|
46 | 46 | |
47 | - header('Content-Description: File Transfer'); |
|
48 | - header('Content-Type: ' . $file->type); |
|
49 | - header('Content-Disposition: attachment; filename=' . $quoted); |
|
50 | - header('Content-Transfer-Encoding: binary'); |
|
51 | - header('Connection: Keep-Alive'); |
|
52 | - header('Expires: 0'); |
|
53 | - header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); |
|
54 | - header('Pragma: public'); |
|
55 | - header('Content-Length: ' . $size); |
|
47 | + header('Content-Description: File Transfer'); |
|
48 | + header('Content-Type: ' . $file->type); |
|
49 | + header('Content-Disposition: attachment; filename=' . $quoted); |
|
50 | + header('Content-Transfer-Encoding: binary'); |
|
51 | + header('Connection: Keep-Alive'); |
|
52 | + header('Expires: 0'); |
|
53 | + header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); |
|
54 | + header('Pragma: public'); |
|
55 | + header('Content-Length: ' . $size); |
|
56 | 56 | |
57 | - readfile($path . '/' . $file->file); |
|
58 | - exit; |
|
59 | - } |
|
57 | + readfile($path . '/' . $file->file); |
|
58 | + exit; |
|
59 | + } |
|
60 | 60 | |
61 | - /** |
|
62 | - * @param CmsComponent $cmsComponent |
|
63 | - */ |
|
64 | - private function overviewRoute($cmsComponent) |
|
65 | - { |
|
66 | - $cmsComponent->subTemplate = 'files'; |
|
67 | - $cmsComponent->setParameter(CmsComponent::PARAMETER_MAIN_NAV_CLASS, CmsComponent::PARAMETER_FILES); |
|
68 | - $cmsComponent->setParameter(CmsComponent::PARAMETER_FILES, $cmsComponent->storage->getFiles()->getFiles()); |
|
69 | - } |
|
61 | + /** |
|
62 | + * @param CmsComponent $cmsComponent |
|
63 | + */ |
|
64 | + private function overviewRoute($cmsComponent) |
|
65 | + { |
|
66 | + $cmsComponent->subTemplate = 'files'; |
|
67 | + $cmsComponent->setParameter(CmsComponent::PARAMETER_MAIN_NAV_CLASS, CmsComponent::PARAMETER_FILES); |
|
68 | + $cmsComponent->setParameter(CmsComponent::PARAMETER_FILES, $cmsComponent->storage->getFiles()->getFiles()); |
|
69 | + } |
|
70 | 70 | |
71 | - /** |
|
72 | - * @param $request |
|
73 | - * @param CmsComponent $cmsComponent |
|
74 | - */ |
|
75 | - private function newRoute($request, $cmsComponent) |
|
76 | - { |
|
77 | - $cmsComponent->subTemplate = 'files/form'; |
|
78 | - $cmsComponent->setParameter(CmsComponent::PARAMETER_MAIN_NAV_CLASS, CmsComponent::PARAMETER_FILES); |
|
79 | - if (isset($_FILES[CmsComponent::FILES_PARAMETER_FILE])) { |
|
80 | - $cmsComponent->storage->getFiles()->addFile($_FILES[CmsComponent::FILES_PARAMETER_FILE]); |
|
81 | - header('Location: ' . $request::$subfolders . $cmsComponent->getParameter(CmsComponent::PARAMETER_CMS_PREFIX) . '/files'); |
|
82 | - exit; |
|
83 | - } |
|
84 | - } |
|
71 | + /** |
|
72 | + * @param $request |
|
73 | + * @param CmsComponent $cmsComponent |
|
74 | + */ |
|
75 | + private function newRoute($request, $cmsComponent) |
|
76 | + { |
|
77 | + $cmsComponent->subTemplate = 'files/form'; |
|
78 | + $cmsComponent->setParameter(CmsComponent::PARAMETER_MAIN_NAV_CLASS, CmsComponent::PARAMETER_FILES); |
|
79 | + if (isset($_FILES[CmsComponent::FILES_PARAMETER_FILE])) { |
|
80 | + $cmsComponent->storage->getFiles()->addFile($_FILES[CmsComponent::FILES_PARAMETER_FILE]); |
|
81 | + header('Location: ' . $request::$subfolders . $cmsComponent->getParameter(CmsComponent::PARAMETER_CMS_PREFIX) . '/files'); |
|
82 | + exit; |
|
83 | + } |
|
84 | + } |
|
85 | 85 | |
86 | - /** |
|
87 | - * @param $request |
|
88 | - * @param CmsComponent $cmsComponent |
|
89 | - */ |
|
90 | - private function deleteRoute($request, $cmsComponent) |
|
91 | - { |
|
92 | - $cmsComponent->storage->getFiles()->deleteFileByName($request::$get[CmsComponent::FILES_PARAMETER_FILE]); |
|
93 | - header('Location: ' . $request::$subfolders . $cmsComponent->getParameter(CmsComponent::PARAMETER_CMS_PREFIX) . '/files'); |
|
94 | - exit; |
|
95 | - } |
|
86 | + /** |
|
87 | + * @param $request |
|
88 | + * @param CmsComponent $cmsComponent |
|
89 | + */ |
|
90 | + private function deleteRoute($request, $cmsComponent) |
|
91 | + { |
|
92 | + $cmsComponent->storage->getFiles()->deleteFileByName($request::$get[CmsComponent::FILES_PARAMETER_FILE]); |
|
93 | + header('Location: ' . $request::$subfolders . $cmsComponent->getParameter(CmsComponent::PARAMETER_CMS_PREFIX) . '/files'); |
|
94 | + exit; |
|
95 | + } |
|
96 | 96 | |
97 | 97 | } |
98 | 98 | \ No newline at end of file |
@@ -180,7 +180,7 @@ discard block |
||
180 | 180 | } |
181 | 181 | |
182 | 182 | /** |
183 | - * @param \cc\Request $request |
|
183 | + * @param \CloudControl\Cms\cc\Request $request |
|
184 | 184 | * @return array |
185 | 185 | */ |
186 | 186 | private function getPostValues($request) |
@@ -291,7 +291,7 @@ discard block |
||
291 | 291 | } |
292 | 292 | |
293 | 293 | /** |
294 | - * @return \cc\Request |
|
294 | + * @return \CloudControl\Cms\cc\Request |
|
295 | 295 | */ |
296 | 296 | private function setPathBackup() |
297 | 297 | { |
@@ -305,7 +305,7 @@ discard block |
||
305 | 305 | } |
306 | 306 | |
307 | 307 | /** |
308 | - * @param \cc\Request $request |
|
308 | + * @param \CloudControl\Cms\cc\Request $request |
|
309 | 309 | */ |
310 | 310 | private function resetPathBackup($request) |
311 | 311 | { |
@@ -317,7 +317,7 @@ discard block |
||
317 | 317 | } |
318 | 318 | |
319 | 319 | /** |
320 | - * @param $form |
|
320 | + * @param string|null $form |
|
321 | 321 | */ |
322 | 322 | private function setFormParameter($form) |
323 | 323 | { |
@@ -7,324 +7,324 @@ |
||
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 |
@@ -161,7 +161,7 @@ |
||
161 | 161 | if (isset($_SESSION[self::SESSION_PARAMETER_FORM_COMPONENT][$this->formParameterName][self::PARAMETER_FORM_ID])) { |
162 | 162 | $this->formId = $_SESSION[self::SESSION_PARAMETER_FORM_COMPONENT][$this->formParameterName][self::PARAMETER_FORM_ID]; |
163 | 163 | } else { |
164 | - $_SESSION[self::SESSION_PARAMETER_FORM_COMPONENT][$this->formParameterName][self::PARAMETER_FORM_ID] = (string)microtime(true); |
|
164 | + $_SESSION[self::SESSION_PARAMETER_FORM_COMPONENT][$this->formParameterName][self::PARAMETER_FORM_ID] = (string) microtime(true); |
|
165 | 165 | $_SESSION[self::SESSION_PARAMETER_FORM_COMPONENT][$this->formParameterName]['submitted'] = false; |
166 | 166 | $this->formId = $_SESSION[self::SESSION_PARAMETER_FORM_COMPONENT][$this->formParameterName][self::PARAMETER_FORM_ID]; |
167 | 167 | } |
@@ -76,7 +76,7 @@ discard block |
||
76 | 76 | * @param string $imagePath |
77 | 77 | * @param bool $getExtension |
78 | 78 | * |
79 | - * @return bool|int|string |
|
79 | + * @return integer |
|
80 | 80 | */ |
81 | 81 | public function getImageMimeType($imagePath, $getExtension = false) |
82 | 82 | { |
@@ -149,7 +149,7 @@ discard block |
||
149 | 149 | } |
150 | 150 | |
151 | 151 | /** |
152 | - * @param $pathToBitmapFile |
|
152 | + * @param string $pathToBitmapFile |
|
153 | 153 | * |
154 | 154 | * @return string |
155 | 155 | */ |
@@ -165,7 +165,7 @@ discard block |
||
165 | 165 | } |
166 | 166 | |
167 | 167 | /** |
168 | - * @param $header |
|
168 | + * @param string $header |
|
169 | 169 | * |
170 | 170 | * @return array |
171 | 171 | */ |
@@ -195,14 +195,14 @@ discard block |
||
195 | 195 | * Loop through the data in the body of the bitmap |
196 | 196 | * file and calculate each individual pixel based on the |
197 | 197 | * bytes |
198 | - * @param $bodySize |
|
199 | - * @param $x |
|
198 | + * @param integer $bodySize |
|
199 | + * @param integer $x |
|
200 | 200 | * @param $width |
201 | - * @param $usePadding |
|
202 | - * @param $y |
|
201 | + * @param boolean $usePadding |
|
202 | + * @param integer $y |
|
203 | 203 | * @param $height |
204 | - * @param $body |
|
205 | - * @param $image |
|
204 | + * @param string $body |
|
205 | + * @param resource $image |
|
206 | 206 | */ |
207 | 207 | private function loopThroughBodyAndCalculatePixels($bodySize, $x, $width, $usePadding, $y, $height, $body, $image) |
208 | 208 | { |
@@ -9,232 +9,232 @@ |
||
9 | 9 | */ |
10 | 10 | |
11 | 11 | namespace CloudControl\Cms\images { |
12 | - class Image |
|
13 | - { |
|
14 | - private $_imageResource; |
|
15 | - |
|
16 | - /** |
|
17 | - * Load the a image resource into $this->_imageResource |
|
18 | - * automagically :-) |
|
19 | - * |
|
20 | - * @param resource | string | path |
|
21 | - * |
|
22 | - * @throws \Exception |
|
23 | - */ |
|
24 | - public function loadImage($imageContainer) |
|
25 | - { |
|
26 | - if (is_resource($imageContainer) && get_resource_type($imageContainer) === 'gd') { |
|
27 | - $this->_imageResource = $imageContainer; |
|
28 | - } elseif (is_string($imageContainer) && file_exists($imageContainer)) { |
|
29 | - if ($this->getImageMimeType($imageContainer) == IMAGETYPE_BMP) { |
|
30 | - $this->_imageResource = $this->createImageFromBmp($imageContainer); |
|
31 | - } else { |
|
32 | - $imageContent = file_get_contents($imageContainer); |
|
33 | - $this->_imageResource = imagecreatefromstring($imageContent); |
|
34 | - } |
|
35 | - } elseif (is_string($imageContainer)) { |
|
36 | - $this->_imageResource = imagecreatefromstring($imageContainer); |
|
37 | - } else { |
|
38 | - throw new \Exception('Could not create image resource, accepted inputs are: "resource of type (gd)", path_to_image and "string". <br /><pre>' . var_export($imageContainer, true) . '</pre>'); |
|
39 | - } |
|
40 | - } |
|
41 | - |
|
42 | - /** |
|
43 | - * Saves the image to a file |
|
44 | - * |
|
45 | - * @param string $path |
|
46 | - * @param int $mimeTypeConstantValue |
|
47 | - * @param int $quality |
|
48 | - * @param null $imageResource If no resource is given, uses $this->_imageResource |
|
49 | - * |
|
50 | - * @return bool |
|
51 | - * @throws \Exception |
|
52 | - */ |
|
53 | - public function saveImage($path, $mimeTypeConstantValue, $quality = 100, $imageResource = null) |
|
54 | - { |
|
55 | - if ($imageResource == null) { |
|
56 | - $imageResource = $this->getImageResource(); |
|
57 | - } |
|
58 | - |
|
59 | - if ($mimeTypeConstantValue == IMAGETYPE_GIF) { |
|
60 | - return imagegif($imageResource, $path); |
|
61 | - } elseif ($mimeTypeConstantValue == IMAGETYPE_JPEG) { |
|
62 | - return imagejpeg($imageResource, $path, $quality); |
|
63 | - } elseif ($mimeTypeConstantValue == IMAGETYPE_PNG) { |
|
64 | - return imagepng($imageResource, $path, (intval($quality / 10) - 1)); |
|
65 | - } else { |
|
66 | - throw new \Exception('Not a valid mimetypeconstant given see function documentation'); |
|
67 | - } |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * Returns either the Mime-Type Constant value |
|
72 | - * or the default extension for the detected mime-type; |
|
73 | - * |
|
74 | - * @see http://www.php.net/manual/en/function.image-type-to-mime-type.php |
|
75 | - * |
|
76 | - * @param string $imagePath |
|
77 | - * @param bool $getExtension |
|
78 | - * |
|
79 | - * @return bool|int|string |
|
80 | - */ |
|
81 | - public function getImageMimeType($imagePath, $getExtension = false) |
|
82 | - { |
|
83 | - if (function_exists('exif_imagetype')) { |
|
84 | - $exif = exif_imagetype($imagePath); |
|
85 | - } else { |
|
86 | - if ((list($width, $height, $type, $attr) = getimagesize($imagePath)) !== false) { |
|
87 | - $exif = $type; |
|
88 | - } else { |
|
89 | - $exif = false; |
|
90 | - } |
|
91 | - } |
|
92 | - |
|
93 | - return $getExtension ? image_type_to_extension($exif) : $exif; |
|
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * Create Image resource from Bitmap |
|
98 | - * |
|
99 | - * @see http://www.php.net/manual/en/function.imagecreatefromwbmp.php#86214 |
|
100 | - * @author alexander at alexauto dot nl |
|
101 | - * |
|
102 | - * @param string $pathToBitmapFile |
|
103 | - * |
|
104 | - * @return resource |
|
105 | - */ |
|
106 | - public function createImageFromBmp($pathToBitmapFile) |
|
107 | - { |
|
108 | - $bitmapFileData = $this->getBitmapFileData($pathToBitmapFile); |
|
109 | - |
|
110 | - $temp = unpack("H*", $bitmapFileData); |
|
111 | - $hex = $temp[1]; |
|
112 | - $header = substr($hex, 0, 108); |
|
113 | - list($width, $height) = $this->calculateWidthAndHeight($header); |
|
114 | - |
|
115 | - // Define starting X and Y |
|
116 | - $x = 0; |
|
117 | - $y = 1; |
|
118 | - |
|
119 | - $image = imagecreatetruecolor($width, $height); |
|
120 | - |
|
121 | - // Grab the body from the image |
|
122 | - $body = substr($hex, 108); |
|
123 | - |
|
124 | - // Calculate if padding at the end-line is needed. Divided by two to keep overview. 1 byte = 2 HEX-chars |
|
125 | - $bodySize = (strlen($body) / 2); |
|
126 | - $headerSize = ($width * $height); |
|
127 | - |
|
128 | - // Use end-line padding? Only when needed |
|
129 | - $usePadding = ($bodySize > ($headerSize * 3) + 4); |
|
130 | - $this->loopThroughBodyAndCalculatePixels($bodySize, $x, $width, $usePadding, $y, $height, $body, $image); |
|
131 | - |
|
132 | - unset($body); |
|
133 | - |
|
134 | - return $image; |
|
135 | - } |
|
136 | - |
|
137 | - /** |
|
138 | - * Returns the image resource |
|
139 | - * @return resource |
|
140 | - * @throws \Exception |
|
141 | - */ |
|
142 | - final public function getImageResource() |
|
143 | - { |
|
144 | - if (is_resource($this->_imageResource) && get_resource_type($this->_imageResource) === 'gd') { |
|
145 | - return $this->_imageResource; |
|
146 | - } else { |
|
147 | - throw new \Exception('Image resource is not set. Use $this->LoadImage to load an image into the resource'); |
|
148 | - } |
|
149 | - } |
|
150 | - |
|
151 | - /** |
|
152 | - * @param $pathToBitmapFile |
|
153 | - * |
|
154 | - * @return string |
|
155 | - */ |
|
156 | - private function getBitmapFileData($pathToBitmapFile) |
|
157 | - { |
|
158 | - $fileHandle = fopen($pathToBitmapFile, "rb"); |
|
159 | - $bitmapFileData = fread($fileHandle, 10); |
|
160 | - while (!feof($fileHandle) && ($bitmapFileData <> "")) { |
|
161 | - $bitmapFileData .= fread($fileHandle, 1024); |
|
162 | - } |
|
163 | - |
|
164 | - return $bitmapFileData; |
|
165 | - } |
|
166 | - |
|
167 | - /** |
|
168 | - * @param $header |
|
169 | - * |
|
170 | - * @return array |
|
171 | - */ |
|
172 | - private function calculateWidthAndHeight($header) |
|
173 | - { |
|
174 | - $width = null; |
|
175 | - $height = null; |
|
176 | - |
|
177 | - // Structure: http://www.fastgraph.com/help/bmp_header_format.html |
|
178 | - if (substr($header, 0, 4) == "424d") { |
|
179 | - // Cut it in parts of 2 bytes |
|
180 | - $header_parts = str_split($header, 2); |
|
181 | - // Get the width 4 bytes |
|
182 | - $width = hexdec($header_parts[19] . $header_parts[18]); |
|
183 | - // Get the height 4 bytes |
|
184 | - $height = hexdec($header_parts[23] . $header_parts[22]); |
|
185 | - // Unset the header params |
|
186 | - unset($header_parts); |
|
187 | - |
|
188 | - return array($width, $height); |
|
189 | - } |
|
190 | - |
|
191 | - return array($width, $height); |
|
192 | - } |
|
193 | - |
|
194 | - /** |
|
195 | - * Loop through the data in the body of the bitmap |
|
196 | - * file and calculate each individual pixel based on the |
|
197 | - * bytes |
|
198 | - * @param $bodySize |
|
199 | - * @param $x |
|
200 | - * @param $width |
|
201 | - * @param $usePadding |
|
202 | - * @param $y |
|
203 | - * @param $height |
|
204 | - * @param $body |
|
205 | - * @param $image |
|
206 | - */ |
|
207 | - private function loopThroughBodyAndCalculatePixels($bodySize, $x, $width, $usePadding, $y, $height, $body, $image) |
|
208 | - { |
|
12 | + class Image |
|
13 | + { |
|
14 | + private $_imageResource; |
|
15 | + |
|
16 | + /** |
|
17 | + * Load the a image resource into $this->_imageResource |
|
18 | + * automagically :-) |
|
19 | + * |
|
20 | + * @param resource | string | path |
|
21 | + * |
|
22 | + * @throws \Exception |
|
23 | + */ |
|
24 | + public function loadImage($imageContainer) |
|
25 | + { |
|
26 | + if (is_resource($imageContainer) && get_resource_type($imageContainer) === 'gd') { |
|
27 | + $this->_imageResource = $imageContainer; |
|
28 | + } elseif (is_string($imageContainer) && file_exists($imageContainer)) { |
|
29 | + if ($this->getImageMimeType($imageContainer) == IMAGETYPE_BMP) { |
|
30 | + $this->_imageResource = $this->createImageFromBmp($imageContainer); |
|
31 | + } else { |
|
32 | + $imageContent = file_get_contents($imageContainer); |
|
33 | + $this->_imageResource = imagecreatefromstring($imageContent); |
|
34 | + } |
|
35 | + } elseif (is_string($imageContainer)) { |
|
36 | + $this->_imageResource = imagecreatefromstring($imageContainer); |
|
37 | + } else { |
|
38 | + throw new \Exception('Could not create image resource, accepted inputs are: "resource of type (gd)", path_to_image and "string". <br /><pre>' . var_export($imageContainer, true) . '</pre>'); |
|
39 | + } |
|
40 | + } |
|
41 | + |
|
42 | + /** |
|
43 | + * Saves the image to a file |
|
44 | + * |
|
45 | + * @param string $path |
|
46 | + * @param int $mimeTypeConstantValue |
|
47 | + * @param int $quality |
|
48 | + * @param null $imageResource If no resource is given, uses $this->_imageResource |
|
49 | + * |
|
50 | + * @return bool |
|
51 | + * @throws \Exception |
|
52 | + */ |
|
53 | + public function saveImage($path, $mimeTypeConstantValue, $quality = 100, $imageResource = null) |
|
54 | + { |
|
55 | + if ($imageResource == null) { |
|
56 | + $imageResource = $this->getImageResource(); |
|
57 | + } |
|
58 | + |
|
59 | + if ($mimeTypeConstantValue == IMAGETYPE_GIF) { |
|
60 | + return imagegif($imageResource, $path); |
|
61 | + } elseif ($mimeTypeConstantValue == IMAGETYPE_JPEG) { |
|
62 | + return imagejpeg($imageResource, $path, $quality); |
|
63 | + } elseif ($mimeTypeConstantValue == IMAGETYPE_PNG) { |
|
64 | + return imagepng($imageResource, $path, (intval($quality / 10) - 1)); |
|
65 | + } else { |
|
66 | + throw new \Exception('Not a valid mimetypeconstant given see function documentation'); |
|
67 | + } |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * Returns either the Mime-Type Constant value |
|
72 | + * or the default extension for the detected mime-type; |
|
73 | + * |
|
74 | + * @see http://www.php.net/manual/en/function.image-type-to-mime-type.php |
|
75 | + * |
|
76 | + * @param string $imagePath |
|
77 | + * @param bool $getExtension |
|
78 | + * |
|
79 | + * @return bool|int|string |
|
80 | + */ |
|
81 | + public function getImageMimeType($imagePath, $getExtension = false) |
|
82 | + { |
|
83 | + if (function_exists('exif_imagetype')) { |
|
84 | + $exif = exif_imagetype($imagePath); |
|
85 | + } else { |
|
86 | + if ((list($width, $height, $type, $attr) = getimagesize($imagePath)) !== false) { |
|
87 | + $exif = $type; |
|
88 | + } else { |
|
89 | + $exif = false; |
|
90 | + } |
|
91 | + } |
|
92 | + |
|
93 | + return $getExtension ? image_type_to_extension($exif) : $exif; |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * Create Image resource from Bitmap |
|
98 | + * |
|
99 | + * @see http://www.php.net/manual/en/function.imagecreatefromwbmp.php#86214 |
|
100 | + * @author alexander at alexauto dot nl |
|
101 | + * |
|
102 | + * @param string $pathToBitmapFile |
|
103 | + * |
|
104 | + * @return resource |
|
105 | + */ |
|
106 | + public function createImageFromBmp($pathToBitmapFile) |
|
107 | + { |
|
108 | + $bitmapFileData = $this->getBitmapFileData($pathToBitmapFile); |
|
109 | + |
|
110 | + $temp = unpack("H*", $bitmapFileData); |
|
111 | + $hex = $temp[1]; |
|
112 | + $header = substr($hex, 0, 108); |
|
113 | + list($width, $height) = $this->calculateWidthAndHeight($header); |
|
114 | + |
|
115 | + // Define starting X and Y |
|
116 | + $x = 0; |
|
117 | + $y = 1; |
|
118 | + |
|
119 | + $image = imagecreatetruecolor($width, $height); |
|
120 | + |
|
121 | + // Grab the body from the image |
|
122 | + $body = substr($hex, 108); |
|
123 | + |
|
124 | + // Calculate if padding at the end-line is needed. Divided by two to keep overview. 1 byte = 2 HEX-chars |
|
125 | + $bodySize = (strlen($body) / 2); |
|
126 | + $headerSize = ($width * $height); |
|
127 | + |
|
128 | + // Use end-line padding? Only when needed |
|
129 | + $usePadding = ($bodySize > ($headerSize * 3) + 4); |
|
130 | + $this->loopThroughBodyAndCalculatePixels($bodySize, $x, $width, $usePadding, $y, $height, $body, $image); |
|
131 | + |
|
132 | + unset($body); |
|
133 | + |
|
134 | + return $image; |
|
135 | + } |
|
136 | + |
|
137 | + /** |
|
138 | + * Returns the image resource |
|
139 | + * @return resource |
|
140 | + * @throws \Exception |
|
141 | + */ |
|
142 | + final public function getImageResource() |
|
143 | + { |
|
144 | + if (is_resource($this->_imageResource) && get_resource_type($this->_imageResource) === 'gd') { |
|
145 | + return $this->_imageResource; |
|
146 | + } else { |
|
147 | + throw new \Exception('Image resource is not set. Use $this->LoadImage to load an image into the resource'); |
|
148 | + } |
|
149 | + } |
|
150 | + |
|
151 | + /** |
|
152 | + * @param $pathToBitmapFile |
|
153 | + * |
|
154 | + * @return string |
|
155 | + */ |
|
156 | + private function getBitmapFileData($pathToBitmapFile) |
|
157 | + { |
|
158 | + $fileHandle = fopen($pathToBitmapFile, "rb"); |
|
159 | + $bitmapFileData = fread($fileHandle, 10); |
|
160 | + while (!feof($fileHandle) && ($bitmapFileData <> "")) { |
|
161 | + $bitmapFileData .= fread($fileHandle, 1024); |
|
162 | + } |
|
163 | + |
|
164 | + return $bitmapFileData; |
|
165 | + } |
|
166 | + |
|
167 | + /** |
|
168 | + * @param $header |
|
169 | + * |
|
170 | + * @return array |
|
171 | + */ |
|
172 | + private function calculateWidthAndHeight($header) |
|
173 | + { |
|
174 | + $width = null; |
|
175 | + $height = null; |
|
176 | + |
|
177 | + // Structure: http://www.fastgraph.com/help/bmp_header_format.html |
|
178 | + if (substr($header, 0, 4) == "424d") { |
|
179 | + // Cut it in parts of 2 bytes |
|
180 | + $header_parts = str_split($header, 2); |
|
181 | + // Get the width 4 bytes |
|
182 | + $width = hexdec($header_parts[19] . $header_parts[18]); |
|
183 | + // Get the height 4 bytes |
|
184 | + $height = hexdec($header_parts[23] . $header_parts[22]); |
|
185 | + // Unset the header params |
|
186 | + unset($header_parts); |
|
187 | + |
|
188 | + return array($width, $height); |
|
189 | + } |
|
190 | + |
|
191 | + return array($width, $height); |
|
192 | + } |
|
193 | + |
|
194 | + /** |
|
195 | + * Loop through the data in the body of the bitmap |
|
196 | + * file and calculate each individual pixel based on the |
|
197 | + * bytes |
|
198 | + * @param $bodySize |
|
199 | + * @param $x |
|
200 | + * @param $width |
|
201 | + * @param $usePadding |
|
202 | + * @param $y |
|
203 | + * @param $height |
|
204 | + * @param $body |
|
205 | + * @param $image |
|
206 | + */ |
|
207 | + private function loopThroughBodyAndCalculatePixels($bodySize, $x, $width, $usePadding, $y, $height, $body, $image) |
|
208 | + { |
|
209 | 209 | // Using a for-loop with index-calculation instead of str_split to avoid large memory consumption |
210 | - // Calculate the next DWORD-position in the body |
|
211 | - for ($i = 0; $i < $bodySize; $i += 3) { |
|
212 | - // Calculate line-ending and padding |
|
213 | - if ($x >= $width) { |
|
214 | - // If padding needed, ignore image-padding. Shift i to the ending of the current 32-bit-block |
|
215 | - if ($usePadding) { |
|
216 | - $i += $width % 4; |
|
217 | - } |
|
218 | - // Reset horizontal position |
|
219 | - $x = 0; |
|
220 | - // Raise the height-position (bottom-up) |
|
221 | - $y++; |
|
222 | - // Reached the image-height? Break the for-loop |
|
223 | - if ($y > $height) { |
|
224 | - break; |
|
225 | - } |
|
226 | - } |
|
227 | - // Calculation of the RGB-pixel (defined as BGR in image-data). Define $iPos as absolute position in the body |
|
228 | - $iPos = $i * 2; |
|
229 | - $r = hexdec($body[$iPos + 4] . $body[$iPos + 5]); |
|
230 | - $g = hexdec($body[$iPos + 2] . $body[$iPos + 3]); |
|
231 | - $b = hexdec($body[$iPos] . $body[$iPos + 1]); |
|
232 | - // Calculate and draw the pixel |
|
233 | - $color = imagecolorallocate($image, $r, $g, $b); |
|
234 | - imagesetpixel($image, $x, $height - $y, $color); |
|
235 | - // Raise the horizontal position |
|
236 | - $x++; |
|
237 | - } |
|
238 | - } |
|
239 | - } |
|
210 | + // Calculate the next DWORD-position in the body |
|
211 | + for ($i = 0; $i < $bodySize; $i += 3) { |
|
212 | + // Calculate line-ending and padding |
|
213 | + if ($x >= $width) { |
|
214 | + // If padding needed, ignore image-padding. Shift i to the ending of the current 32-bit-block |
|
215 | + if ($usePadding) { |
|
216 | + $i += $width % 4; |
|
217 | + } |
|
218 | + // Reset horizontal position |
|
219 | + $x = 0; |
|
220 | + // Raise the height-position (bottom-up) |
|
221 | + $y++; |
|
222 | + // Reached the image-height? Break the for-loop |
|
223 | + if ($y > $height) { |
|
224 | + break; |
|
225 | + } |
|
226 | + } |
|
227 | + // Calculation of the RGB-pixel (defined as BGR in image-data). Define $iPos as absolute position in the body |
|
228 | + $iPos = $i * 2; |
|
229 | + $r = hexdec($body[$iPos + 4] . $body[$iPos + 5]); |
|
230 | + $g = hexdec($body[$iPos + 2] . $body[$iPos + 3]); |
|
231 | + $b = hexdec($body[$iPos] . $body[$iPos + 1]); |
|
232 | + // Calculate and draw the pixel |
|
233 | + $color = imagecolorallocate($image, $r, $g, $b); |
|
234 | + imagesetpixel($image, $x, $height - $y, $color); |
|
235 | + // Raise the horizontal position |
|
236 | + $x++; |
|
237 | + } |
|
238 | + } |
|
239 | + } |
|
240 | 240 | } |
241 | 241 | \ No newline at end of file |
@@ -13,6 +13,10 @@ |
||
13 | 13 | { |
14 | 14 | protected $imagesDir; |
15 | 15 | |
16 | + /** |
|
17 | + * @param \CloudControl\Cms\storage\Repository $repository |
|
18 | + * @param string $imagesDir |
|
19 | + */ |
|
16 | 20 | public function __construct($repository, $imagesDir) |
17 | 21 | { |
18 | 22 | parent::__construct($repository); |
@@ -63,7 +63,7 @@ |
||
63 | 63 | |
64 | 64 | // Define the ratio and adjust the width and height |
65 | 65 | if ($this->_preserveAspectRatio) { |
66 | - $ratio = min($this->_width/$originalWidth, $this->_height/$originalHeight); |
|
66 | + $ratio = min($this->_width / $originalWidth, $this->_height / $originalHeight); |
|
67 | 67 | $this->_width = $originalWidth * $ratio; |
68 | 68 | $this->_height = $originalHeight * $ratio; |
69 | 69 | } |
@@ -14,73 +14,73 @@ |
||
14 | 14 | use CloudControl\Cms\images\IMethod; |
15 | 15 | |
16 | 16 | class Resize extends IMethod |
17 | - { |
|
18 | - protected $_width; |
|
19 | - protected $_height; |
|
20 | - protected $_preserveAspectRatio = true; |
|
17 | + { |
|
18 | + protected $_width; |
|
19 | + protected $_height; |
|
20 | + protected $_preserveAspectRatio = true; |
|
21 | 21 | |
22 | - /** |
|
23 | - * Set the width |
|
24 | - * |
|
25 | - * @param int $width |
|
26 | - * @return self |
|
27 | - */ |
|
28 | - public function SetWidth($width) |
|
29 | - { |
|
30 | - $this->_width = intval($width); |
|
31 | - return $this; |
|
32 | - } |
|
22 | + /** |
|
23 | + * Set the width |
|
24 | + * |
|
25 | + * @param int $width |
|
26 | + * @return self |
|
27 | + */ |
|
28 | + public function SetWidth($width) |
|
29 | + { |
|
30 | + $this->_width = intval($width); |
|
31 | + return $this; |
|
32 | + } |
|
33 | 33 | |
34 | - /** |
|
35 | - * Set the height |
|
36 | - * |
|
37 | - * @param int $height |
|
38 | - * @return self |
|
39 | - */ |
|
40 | - public function SetHeight($height) |
|
41 | - { |
|
42 | - $this->_height = intval($height); |
|
43 | - return $this; |
|
44 | - } |
|
34 | + /** |
|
35 | + * Set the height |
|
36 | + * |
|
37 | + * @param int $height |
|
38 | + * @return self |
|
39 | + */ |
|
40 | + public function SetHeight($height) |
|
41 | + { |
|
42 | + $this->_height = intval($height); |
|
43 | + return $this; |
|
44 | + } |
|
45 | 45 | |
46 | - /** |
|
47 | - * Sets wheter or not the aspect ratio of the original |
|
48 | - * image needs to preserved |
|
49 | - * |
|
50 | - * @param bool $bool |
|
51 | - * @return self |
|
52 | - */ |
|
53 | - public function SetPreserveAspectRatio($bool) |
|
54 | - { |
|
55 | - $this->_preserveAspectRatio = (bool) $bool; |
|
56 | - return $this; |
|
57 | - } |
|
46 | + /** |
|
47 | + * Sets wheter or not the aspect ratio of the original |
|
48 | + * image needs to preserved |
|
49 | + * |
|
50 | + * @param bool $bool |
|
51 | + * @return self |
|
52 | + */ |
|
53 | + public function SetPreserveAspectRatio($bool) |
|
54 | + { |
|
55 | + $this->_preserveAspectRatio = (bool) $bool; |
|
56 | + return $this; |
|
57 | + } |
|
58 | 58 | |
59 | - public function Execute($imageResource) |
|
60 | - { |
|
61 | - // Define the origial width and height |
|
62 | - $originalWidth = imagesx($imageResource); |
|
63 | - $originalHeight = imagesy($imageResource); |
|
59 | + public function Execute($imageResource) |
|
60 | + { |
|
61 | + // Define the origial width and height |
|
62 | + $originalWidth = imagesx($imageResource); |
|
63 | + $originalHeight = imagesy($imageResource); |
|
64 | 64 | |
65 | - // Define the ratio and adjust the width and height |
|
66 | - if ($this->_preserveAspectRatio) { |
|
67 | - $ratio = min($this->_width/$originalWidth, $this->_height/$originalHeight); |
|
68 | - $this->_width = $originalWidth * $ratio; |
|
69 | - $this->_height = $originalHeight * $ratio; |
|
70 | - } |
|
65 | + // Define the ratio and adjust the width and height |
|
66 | + if ($this->_preserveAspectRatio) { |
|
67 | + $ratio = min($this->_width/$originalWidth, $this->_height/$originalHeight); |
|
68 | + $this->_width = $originalWidth * $ratio; |
|
69 | + $this->_height = $originalHeight * $ratio; |
|
70 | + } |
|
71 | 71 | |
72 | - // Create the new image |
|
73 | - $new = imagecreatetruecolor($this->_width, $this->_height); |
|
72 | + // Create the new image |
|
73 | + $new = imagecreatetruecolor($this->_width, $this->_height); |
|
74 | 74 | |
75 | - // Preserve transparency |
|
76 | - imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127)); |
|
77 | - imagealphablending($new, false); |
|
78 | - imagesavealpha($new, true); |
|
75 | + // Preserve transparency |
|
76 | + imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127)); |
|
77 | + imagealphablending($new, false); |
|
78 | + imagesavealpha($new, true); |
|
79 | 79 | |
80 | - // Do the actual resizing |
|
81 | - imagecopyresampled($new, $imageResource, 0, 0, 0, 0, $this->_width, $this->_height, $originalWidth, $originalHeight); |
|
80 | + // Do the actual resizing |
|
81 | + imagecopyresampled($new, $imageResource, 0, 0, 0, 0, $this->_width, $this->_height, $originalWidth, $originalHeight); |
|
82 | 82 | |
83 | - return $new; |
|
84 | - } |
|
85 | - } |
|
83 | + return $new; |
|
84 | + } |
|
85 | + } |
|
86 | 86 | } |
87 | 87 | \ No newline at end of file |
@@ -26,14 +26,14 @@ |
||
26 | 26 | |
27 | 27 | // Define which ratio will be used, depending on which is the smallest side |
28 | 28 | $ratio = min($hRatio, $wRatio); |
29 | - if($ratio > 1) $ratio = 1; |
|
29 | + if ($ratio > 1) $ratio = 1; |
|
30 | 30 | |
31 | 31 | // Define sizes |
32 | 32 | $this->_destWidth = floor($originalWidth * $ratio); |
33 | 33 | $this->_destHeight = floor($originalHeight * $ratio); |
34 | 34 | |
35 | 35 | // Define margins |
36 | - $this->_destX = floor(($this->_width - $this->_destWidth) / 2); |
|
36 | + $this->_destX = floor(($this->_width - $this->_destWidth) / 2); |
|
37 | 37 | $this->_destY = floor(($this->_height - $this->_destHeight) / 2); |
38 | 38 | |
39 | 39 | // Execute the Crop method with the given parameters |
@@ -10,36 +10,36 @@ |
||
10 | 10 | |
11 | 11 | namespace CloudControl\Cms\images\methods |
12 | 12 | { |
13 | - class BoxCrop extends Crop |
|
14 | - { |
|
15 | - /** |
|
16 | - * @param resource $imageResource |
|
17 | - * @return resource |
|
13 | + class BoxCrop extends Crop |
|
14 | + { |
|
15 | + /** |
|
16 | + * @param resource $imageResource |
|
17 | + * @return resource |
|
18 | 18 | */ |
19 | - public function Execute($imageResource) |
|
20 | - { |
|
21 | - // Define the origial width and height |
|
22 | - $originalWidth = imagesx($imageResource); |
|
23 | - $originalHeight = imagesy($imageResource); |
|
19 | + public function Execute($imageResource) |
|
20 | + { |
|
21 | + // Define the origial width and height |
|
22 | + $originalWidth = imagesx($imageResource); |
|
23 | + $originalHeight = imagesy($imageResource); |
|
24 | 24 | |
25 | - // Define the ratios |
|
26 | - $wRatio = $this->_width / $originalWidth; |
|
27 | - $hRatio = $this->_height / $originalHeight; |
|
25 | + // Define the ratios |
|
26 | + $wRatio = $this->_width / $originalWidth; |
|
27 | + $hRatio = $this->_height / $originalHeight; |
|
28 | 28 | |
29 | - // Define which ratio will be used, depending on which is the smallest side |
|
30 | - $ratio = min($hRatio, $wRatio); |
|
31 | - if($ratio > 1) $ratio = 1; |
|
29 | + // Define which ratio will be used, depending on which is the smallest side |
|
30 | + $ratio = min($hRatio, $wRatio); |
|
31 | + if($ratio > 1) $ratio = 1; |
|
32 | 32 | |
33 | - // Define sizes |
|
34 | - $this->_destWidth = floor($originalWidth * $ratio); |
|
35 | - $this->_destHeight = floor($originalHeight * $ratio); |
|
33 | + // Define sizes |
|
34 | + $this->_destWidth = floor($originalWidth * $ratio); |
|
35 | + $this->_destHeight = floor($originalHeight * $ratio); |
|
36 | 36 | |
37 | - // Define margins |
|
38 | - $this->_destX = floor(($this->_width - $this->_destWidth) / 2); |
|
39 | - $this->_destY = floor(($this->_height - $this->_destHeight) / 2); |
|
37 | + // Define margins |
|
38 | + $this->_destX = floor(($this->_width - $this->_destWidth) / 2); |
|
39 | + $this->_destY = floor(($this->_height - $this->_destHeight) / 2); |
|
40 | 40 | |
41 | - // Execute the Crop method with the given parameters |
|
42 | - return parent::Execute($imageResource); |
|
43 | - } |
|
44 | - } |
|
41 | + // Execute the Crop method with the given parameters |
|
42 | + return parent::Execute($imageResource); |
|
43 | + } |
|
44 | + } |
|
45 | 45 | } |
46 | 46 | \ No newline at end of file |
@@ -28,7 +28,9 @@ |
||
28 | 28 | |
29 | 29 | // Define which ratio will be used, depending on which is the smallest side |
30 | 30 | $ratio = min($hRatio, $wRatio); |
31 | - if($ratio > 1) $ratio = 1; |
|
31 | + if($ratio > 1) { |
|
32 | + $ratio = 1; |
|
33 | + } |
|
32 | 34 | |
33 | 35 | // Define sizes |
34 | 36 | $this->_destWidth = floor($originalWidth * $ratio); |
@@ -78,12 +78,15 @@ |
||
78 | 78 | */ |
79 | 79 | public static function humanFileSize($size, $unit = "") |
80 | 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"; |
|
81 | + if ((!$unit && $size >= 1 << 30) || $unit == "GB") { |
|
82 | + return number_format($size / (1 << 30), 2) . "GB"; |
|
83 | + } |
|
84 | + if ((!$unit && $size >= 1 << 20) || $unit == "MB") { |
|
85 | + return number_format($size / (1 << 20), 2) . "MB"; |
|
86 | + } |
|
87 | + if ((!$unit && $size >= 1 << 10) || $unit == "KB") { |
|
88 | + return number_format($size / (1 << 10), 2) . "KB"; |
|
89 | + } |
|
87 | 90 | |
88 | 91 | return number_format($size) . " bytes"; |
89 | 92 | } |
@@ -8,123 +8,123 @@ |
||
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 |
@@ -20,7 +20,7 @@ |
||
20 | 20 | public static function slugify($str, $replace = array(), $delimiter = '-') |
21 | 21 | { |
22 | 22 | if (!empty($replace)) { |
23 | - $str = str_replace((array)$replace, ' ', $str); |
|
23 | + $str = str_replace((array) $replace, ' ', $str); |
|
24 | 24 | } |
25 | 25 | |
26 | 26 | $clean = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $str); |