-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRedactorUpdateController.php
141 lines (121 loc) · 3.76 KB
/
RedactorUpdateController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
/**
* @author Serge Postrash aka SDKiller <[email protected]>
* @link https://github.com/SDKiller
* @copyright Copyright (c) 2015 Serge Postrash
* @license BSD 3-Clause, see LICENSE.md
*/
namespace console\controllers;
use Yii;
use yii\console\Controller;
use yii\helpers\FileHelper;
class RedactorUpdateController extends Controller
{
public $pluginsUrl = 'http://imperavi.com/webdownload/redactor/plugin/?plugin=';
public $plugins = [
'clips',
'counter',
'definedlinks',
'filemanager',
'fontcolor',
'fontfamily',
'fontsize',
'fullscreen',
'imagemanager',
'limiter',
'table',
'textdirection',
'textexpander',
'video',
];
protected $tmpDir = '@runtime/download';
/**
* @inheritdoc
*/
public function init()
{
$this->tmpDir = Yii::getAlias($this->tmpDir);
FileHelper::createDirectory($this->tmpDir . '/zip');
}
/**
* @return int
*/
public function actionDownloadPlugins()
{
foreach ($this->plugins as $pluginName) {
$msg = ' ';
try {
$buff = file_get_contents($this->pluginsUrl . $pluginName);
} catch (\Exception $e) {
$buff = false;
$msg = $e->getMessage();
}
if ($buff === false) {
$this->stdout('Error downloading plugin ' . $pluginName . $msg . PHP_EOL);
} else {
try {
$res = file_put_contents($this->tmpDir . '/zip' . '/' . $pluginName . '.zip', $buff, FILE_BINARY);
} catch (\Exception $e) {
$res = false;
$msg = $e->getMessage();
}
if ($res === false) {
$this->stdout('Error copying file ' . $pluginName . '.zip' . $msg . PHP_EOL);
} else {
$this->stdout('OK dowloaded plugin ' . $pluginName . PHP_EOL);
}
}
}
return 0;
}
/**
* @return int
* @throws \yii\base\Exception
*/
public function actionUnpackPlugins()
{
$files = FileHelper::findFiles($this->tmpDir . '/zip', ['only' => ['*.zip']]);
if (empty($files)) {
$this->stdout('No files found' . PHP_EOL);
return 1;
}
foreach ($files as $file) {
$pluginName = pathinfo($file, PATHINFO_FILENAME);
if (!in_array($pluginName, $this->plugins, true)) {
continue;
}
if ($pluginName == 'clips') {
// clips is packed with folder
$extractDir = $this->tmpDir . '/plugins';
} else {
$extractDir = $this->tmpDir . '/plugins/' . $pluginName;
}
FileHelper::createDirectory($extractDir);
$zip = new \ZipArchive();
if ($zip->open($file) !== true) {
$this->stdout('Error opening file ' . $pluginName . '.zip' . PHP_EOL);
continue;
}
if ($zip->extractTo($extractDir) !== true) {
$this->stdout('Error extracting file ' . $pluginName . '.zip' . PHP_EOL);
$zip->close();
continue;
}
$zip->close();
$garbage = $extractDir . '/__MACOSX';
if (is_dir($garbage)) {
FileHelper::removeDirectory($garbage);
}
$this->stdout('OK extracted plugin ' . $pluginName . PHP_EOL);
}
return 0;
}
/**
* @return int
*/
public function actionCleanup()
{
FileHelper::removeDirectory($this->tmpDir);
return 0;
}
}