Merge branch '2.0'
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / acp / form / StyleImportForm.class.php
1 <?php
2 namespace wcf\acp\form;
3 use wcf\data\style\StyleEditor;
4 use wcf\form\AbstractForm;
5 use wcf\system\cache\builder\StyleCacheBuilder;
6 use wcf\system\exception\SystemException;
7 use wcf\system\exception\UserInputException;
8 use wcf\system\package\PackageArchive;
9 use wcf\system\request\LinkHandler;
10 use wcf\system\WCF;
11 use wcf\util\FileUtil;
12 use wcf\util\HeaderUtil;
13
14 /**
15 * Shows the style import form.
16 *
17 * @author Alexander Ebert
18 * @copyright 2001-2014 WoltLab GmbH
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 /**
26 * @see \wcf\page\AbstractPage::$activeMenuItem
27 */
28 public $activeMenuItem = 'wcf.acp.menu.link.style.import';
29
30 /**
31 * @see \wcf\page\AbstractPage::$neededPermissions
32 */
33 public $neededPermissions = array('admin.style.canManageStyle');
34
35 /**
36 * upload data
37 * @var array<string>
38 */
39 public $source = array();
40
41 /**
42 * style editor object
43 * @var \wcf\data\style\StyleEditor
44 */
45 public $style = null;
46
47 /**
48 * @see \wcf\form\IForm::readFormParameters()
49 */
50 public function readFormParameters() {
51 parent::readFormParameters();
52
53 if (isset($_FILES['source'])) $this->source = $_FILES['source'];
54 }
55
56 /**
57 * @see \wcf\form\IForm::validate()
58 */
59 public function validate() {
60 parent::validate();
61
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
70 try {
71 // check if the uploaded file is a package
72 $archive = new PackageArchive($this->source['tmp_name']);
73 $archive->openArchive();
74
75 // check if the package is an application
76 if ($archive->getPackageInfo('isApplication')) {
77 throw new SystemException("Package is application");
78 }
79
80 // check if the package includes a style
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
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');
117 }
118 }
119
120 /**
121 * @see \wcf\form\IForm::save()
122 */
123 public function save() {
124 parent::save();
125
126 StyleCacheBuilder::getInstance()->reset();
127
128 @unlink($this->source['tmp_name']);
129 $this->saved();
130
131 WCF::getTPL()->assign('success', true);
132 }
133 }