1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* WellCommerce Open-Source E-Commerce Platform |
4
|
|
|
* |
5
|
|
|
* This file is part of the WellCommerce package. |
6
|
|
|
* |
7
|
|
|
* (c) Adam Piotrowski <[email protected]> |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, |
10
|
|
|
* please view the LICENSE file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace WellCommerce\Bundle\TemplateEditorBundle\Controller\Admin; |
14
|
|
|
|
15
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
16
|
|
|
use Symfony\Component\Finder\Finder; |
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
18
|
|
|
use Symfony\Component\HttpFoundation\Response; |
19
|
|
|
use WellCommerce\Bundle\CoreBundle\Controller\Admin\AbstractAdminController; |
20
|
|
|
use WellCommerce\Bundle\CoreBundle\Entity\EntityInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class TemplateEditorController |
24
|
|
|
* |
25
|
|
|
* @author Adam Piotrowski <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class TemplateEditorController extends AbstractAdminController |
28
|
|
|
{ |
29
|
|
|
const ALLOWED_FILE_EXTENSIONS = ['html', 'twig', 'css', 'less', 'js', 'png', 'jpg', 'jpeg', 'gif', 'xml', 'json']; |
30
|
|
|
|
31
|
|
|
public function editAction(int $id): Response |
32
|
|
|
{ |
33
|
|
|
$resource = $this->getManager()->getRepository()->find($id); |
34
|
|
|
|
35
|
|
|
if (!$resource instanceof EntityInterface) { |
36
|
|
|
return $this->redirectToAction('index'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
return $this->displayTemplate('edit', [ |
40
|
|
|
'resource' => $resource, |
41
|
|
|
]); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function listFilesAction(Request $request, string $theme): Response |
45
|
|
|
{ |
46
|
|
|
$treeContent = $this->renderView('WellCommerceTemplateEditorBundle:Admin/TemplateEditor:tree.html.twig', [ |
47
|
|
|
'finder' => $this->createFinder($theme, $request->get('dir')), |
48
|
|
|
'dir' => $request->get('dir'), |
49
|
|
|
]); |
50
|
|
|
|
51
|
|
|
return new Response($treeContent); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function getFileContentAction(Request $request): Response |
55
|
|
|
{ |
56
|
|
|
$fileName = $request->get('file'); |
57
|
|
|
$theme = $request->get('theme'); |
58
|
|
|
$path = $this->getFilePath($theme, $fileName); |
59
|
|
|
$content = file_get_contents($path); |
60
|
|
|
|
61
|
|
|
return $this->jsonResponse([ |
62
|
|
|
'theme' => $theme, |
63
|
|
|
'file' => $fileName, |
64
|
|
|
'path' => $path, |
65
|
|
|
'content' => $content, |
66
|
|
|
'extension' => pathinfo($path, PATHINFO_EXTENSION), |
67
|
|
|
]); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function saveFileAction(Request $request): Response |
71
|
|
|
{ |
72
|
|
|
$fileName = $request->get('file'); |
73
|
|
|
$theme = $request->get('theme'); |
74
|
|
|
$content = $request->get('content'); |
75
|
|
|
$path = $this->getFilePath($theme, $fileName); |
76
|
|
|
$filesystem = new Filesystem(); |
77
|
|
|
|
78
|
|
|
if (false === $filesystem->exists($path)) { |
79
|
|
|
return $this->jsonResponse([ |
80
|
|
|
'error' => 'Cannot save file', |
81
|
|
|
]); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
try { |
85
|
|
|
$filesystem->dumpFile($path, $content); |
86
|
|
|
} catch (\Exception $e) { |
87
|
|
|
return $this->jsonResponse([ |
88
|
|
|
'error' => $e->getMessage(), |
89
|
|
|
]); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $this->jsonResponse([ |
93
|
|
|
'success' => true, |
94
|
|
|
]); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function uploadFileAction(Request $request): Response |
|
|
|
|
98
|
|
|
{ |
99
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
private function getFilePath(string $theme, string $fileName): string |
103
|
|
|
{ |
104
|
|
|
if (strpos($fileName, '..') !== false) { |
105
|
|
|
throw new \Exception('Wrong file path'); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return sprintf('%s/../web/themes/%s/%s', $this->get('kernel')->getRootDir(), $theme, $fileName); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
private function createFinder(string $theme, string $rootDir): Finder |
112
|
|
|
{ |
113
|
|
|
$directory = $this->getFilePath($theme, $rootDir); |
114
|
|
|
$finder = new Finder(); |
115
|
|
|
|
116
|
|
|
$finder->in($directory)->ignoreVCS(true)->sortByType()->depth(0)->filter(function (\SplFileInfo $file) { |
117
|
|
|
if ($this->isValidFile($file) || $this->isValidDirectory($file)) { |
118
|
|
|
return true; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return false; |
122
|
|
|
}); |
123
|
|
|
|
124
|
|
|
return $finder; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
private function isValidFile(\SplFileInfo $file): bool |
128
|
|
|
{ |
129
|
|
|
return $file->isFile() && $file->isWritable() && in_array($file->getExtension(), self::ALLOWED_FILE_EXTENSIONS); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
private function isValidDirectory(\SplFileInfo $file): bool |
133
|
|
|
{ |
134
|
|
|
return $file->isDir() && $file->isReadable(); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.