Commit | Line | Data |
---|---|---|
6c80f0df AE |
1 | <?php |
2 | namespace wcf\acp\form; | |
3 | use wcf\data\style\StyleEditor; | |
4 | use wcf\form\AbstractForm; | |
5195702e | 5 | use wcf\system\cache\builder\StyleCacheBuilder; |
b168bf95 | 6 | use wcf\system\exception\SystemException; |
6c80f0df | 7 | use wcf\system\exception\UserInputException; |
b168bf95 MS |
8 | use wcf\system\package\PackageArchive; |
9 | use wcf\system\request\LinkHandler; | |
6c80f0df | 10 | use wcf\system\WCF; |
b168bf95 MS |
11 | use wcf\util\FileUtil; |
12 | use wcf\util\HeaderUtil; | |
6c80f0df AE |
13 | |
14 | /** | |
15 | * Shows the style import form. | |
16 | * | |
17 | * @author Alexander Ebert | |
ca4ba303 | 18 | * @copyright 2001-2014 WoltLab GmbH |
6c80f0df AE |
19 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> |
20 | * @package com.woltlab.wcf | |
21 | * @subpackage acp.form | |
22 | * @category Community Framework | |
23 | */ | |
24 | class StyleImportForm extends AbstractForm { | |
25 | /** | |
0ad90fc3 | 26 | * @see \wcf\page\AbstractPage::$activeMenuItem |
6c80f0df AE |
27 | */ |
28 | public $activeMenuItem = 'wcf.acp.menu.link.style.import'; | |
29 | ||
30 | /** | |
0ad90fc3 | 31 | * @see \wcf\page\AbstractPage::$neededPermissions |
6c80f0df | 32 | */ |
315c6dc0 | 33 | public $neededPermissions = array('admin.style.canManageStyle'); |
6c80f0df AE |
34 | |
35 | /** | |
36 | * upload data | |
37 | * @var array<string> | |
38 | */ | |
39 | public $source = array(); | |
40 | ||
41 | /** | |
42 | * style editor object | |
0ad90fc3 | 43 | * @var \wcf\data\style\StyleEditor |
6c80f0df AE |
44 | */ |
45 | public $style = null; | |
46 | ||
47 | /** | |
0ad90fc3 | 48 | * @see \wcf\form\IForm::readFormParameters() |
6c80f0df AE |
49 | */ |
50 | public function readFormParameters() { | |
51 | parent::readFormParameters(); | |
52 | ||
53 | if (isset($_FILES['source'])) $this->source = $_FILES['source']; | |
54 | } | |
55 | ||
56 | /** | |
0ad90fc3 | 57 | * @see \wcf\form\IForm::validate() |
6c80f0df AE |
58 | */ |
59 | public function validate() { | |
60 | parent::validate(); | |
5195702e | 61 | |
6c80f0df AE |
62 | if (empty($this->source['name'])) { |
63 | throw new UserInputException('source'); | |
64 | } | |
65 | ||
66 | if (empty($this->source['tmp_name'])) { | |
67 | throw new UserInputException('source', 'uploadFailed'); | |
68 | } | |
69 | ||
b168bf95 MS |
70 | try { |
71 | // check if the uploaded file is a package | |
72 | $archive = new PackageArchive($this->source['tmp_name']); | |
73 | $archive->openArchive(); | |
74 | ||
39286f71 | 75 | // check if the package is an application |
b168bf95 MS |
76 | if ($archive->getPackageInfo('isApplication')) { |
77 | throw new SystemException("Package is application"); | |
78 | } | |
79 | ||
39286f71 | 80 | // check if the package includes a style |
b168bf95 MS |
81 | $containsStyle = false; |
82 | $installInstructions = $archive->getInstallInstructions(); | |
83 | foreach ($installInstructions as $instruction) { | |
84 | if ($instruction['pip'] == 'style') { | |
85 | $containsStyle = true; | |
86 | break; | |
87 | } | |
88 | } | |
89 | ||
90 | if (!$containsStyle) { | |
91 | throw new SystemException("Package contains no style"); | |
92 | } | |
93 | ||
94 | $filename = FileUtil::getTemporaryFilename('package_', preg_replace('!^.*(?=\.(?:tar\.gz|tgz|tar)$)!i', '', basename($this->source['name']))); | |
95 | ||
96 | if (!@move_uploaded_file($this->source['tmp_name'], $filename)) { | |
97 | throw new SystemException("Cannot move uploaded file"); | |
98 | } | |
99 | ||
100 | WCF::getSession()->register('stylePackageImportLocation', $filename); | |
101 | ||
102 | HeaderUtil::redirect(LinkHandler::getInstance()->getLink('PackageStartInstall', array( | |
103 | 'action' => 'install' | |
104 | ))); | |
105 | exit; | |
106 | } | |
107 | catch (SystemException $e) { | |
108 | // ignore errors | |
109 | } | |
110 | ||
689f5bd0 MW |
111 | try { |
112 | $this->style = StyleEditor::import($this->source['tmp_name']); | |
113 | } | |
114 | catch (\Exception $e) { | |
115 | @unlink($this->source['tmp_name']); | |
116 | throw new UserInputException('source', 'importFailed'); | |
6c80f0df AE |
117 | } |
118 | } | |
119 | ||
120 | /** | |
0ad90fc3 | 121 | * @see \wcf\form\IForm::save() |
6c80f0df AE |
122 | */ |
123 | public function save() { | |
124 | parent::save(); | |
125 | ||
5195702e MS |
126 | StyleCacheBuilder::getInstance()->reset(); |
127 | ||
689f5bd0 | 128 | @unlink($this->source['tmp_name']); |
6c80f0df AE |
129 | $this->saved(); |
130 | ||
131 | WCF::getTPL()->assign('success', true); | |
132 | } | |
133 | } |