Newer
Older
<?php
namespace QUI\Ckeditor\Plugins;
use QUI\Utils\Security\Orthos;
use QUI\Utils\System\File;
/**
* Class Manager
*
* @package QUI\Ckeditor\Plugins
*/
class Manager
{
protected $activePluginDir;
protected $installedPluginDir;
protected $dependencies;
/**
* List of plugins which should be installed
* @var array
*/
protected $blacklist = array(
"divarea",
"copyformatting"
);
public function __construct()
{
$this->activePluginDir = \QUI::getPackage("quiqqer/ckeditor4")->getVarDir() . "/plugins/bin";
$this->installedPluginDir = \QUI::getPackage("quiqqer/ckeditor4")->getVarDir() . "/plugins/installed";
if (!is_dir($this->activePluginDir)) {
mkdir($this->activePluginDir, 0755, true);
}
if (!is_dir($this->installedPluginDir)) {
mkdir($this->installedPluginDir, 0755, true);
}
}
#########################################
# Installation #
#########################################
$srcDirs = array(
OPT_DIR . "ckeditor/ckeditor/plugins",
OPT_DIR . "quiqqer/ckeditor4/plugins/quiqqer",
OPT_DIR . "quiqqer/ckeditor4/plugins/ckeditor4",
foreach ($srcDirs as $srcDir) {
if (!is_dir($srcDir)) {
return;
}
foreach (scandir($srcDir) as $entry) {
if ($entry == "." || $entry == "..") {
continue;
}
if (!is_dir($srcDir . "/" . $entry)) {
continue;
}
# Check if/where the plugin is installed
$targetDir = $this->installedPluginDir . "/" . $entry;
if (is_dir($this->activePluginDir . "/" . $entry)) {
$targetDir = $this->activePluginDir . "/" . $entry;
}
if (is_dir($targetDir)) {
File::deleteDir($targetDir);
}
File::dircopy(
$srcDir . "/" . $entry,
$targetDir
);
}
}
* Installs the plugins from the source packages quiqqer/ckeditor4 and ckeditor4/ckeditor4
public function installPluginsFromSource()
$srcDirs = array(
OPT_DIR . "ckeditor/ckeditor/plugins",
OPT_DIR . "quiqqer/ckeditor4/plugins/quiqqer",
OPT_DIR . "quiqqer/ckeditor4/plugins/ckeditor4"
$activePlugins = array();
$defaultStateFile = dirname(dirname(dirname(dirname(dirname(__FILE__))))) . "/plugins/activePlugins.json";
if (file_exists($defaultStateFile)) {
$json = file_get_contents($defaultStateFile);
$activePlugins = json_decode($json, true);
}
foreach ($srcDirs as $srcDir) {
if (!is_dir($srcDir)) {
return;
}
foreach (scandir($srcDir) as $entry) {
$targetDir = $this->installedPluginDir;
if (in_array($entry, $activePlugins)) {
$targetDir = $this->activePluginDir;
}
if ($entry == "." || $entry == "..") {
continue;
}
if (!is_dir($srcDir . "/" . $entry)) {
continue;
}
if (is_dir($targetDir . "/" . $entry)) {
continue;
}
if (is_dir($this->activePluginDir . "/" . $entry)) {
continue;
}
if (in_array($entry, $this->blacklist)) {
continue;
}
$this->copyDir(
$srcDir . "/" . $entry,
$targetDir . "/" . $entry
);
}
if (file_exists(OPT_DIR . "quiqqer/ckeditor4/plugins/dependencies.json")) {
copy(
OPT_DIR . "quiqqer/ckeditor4/plugins/dependencies.json",
$this->getPluginDir() . "/dependencies.json"
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
);
}
}
/**
* Returns all installed plugins
*
* @return string[] - array of plugin names
*/
public function getInstalledPlugins()
{
$result = array();
$content = scandir($this->installedPluginDir);
if ($content === false) {
return array();
}
foreach ($content as $entry) {
if ($entry == "." || $entry == "..") {
continue;
}
$fullpath = $this->installedPluginDir . "/" . $entry;
if (!is_dir($fullpath)) {
continue;
}
$result[] = $entry;
}
return $result;
/**
* Installs a plugin zip file from the given path
*
* @param $pluginpath - path to the plugins zip file
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
*
* @throws Exception
*/
public function installPlugin($pluginpath)
{
# Check if file exists
if (!file_exists($pluginpath)) {
throw new Exception(array("quiqqer/ckeditor4", "exception.install.file.not.found"));
}
$tmpDir = \QUI::getTemp()->createFolder();
copy(
$pluginpath,
$tmpDir . "/archive.zip"
);
$Zip = new \ZipArchive();
if ($Zip->open($tmpDir . "/archive.zip") === false) {
throw new Exception(array("quiqqer/ckeditor4", "exception.install.file.invalid.format"));
}
if ($Zip->extractTo($tmpDir . "/content") === false) {
throw new Exception(array("quiqqer/ckeditor4", "exception.install.file.extract.failed"));
}
foreach (scandir($tmpDir . "/content") as $entry) {
if ($entry == "." || $entry == "..") {
continue;
}
if (is_dir($this->installedPluginDir . "/" . $entry)) {
throw new Exception(array("quiqqer/ckeditor4", "exception.install.file.exists"));
}
if (is_dir($this->activePluginDir . "/" . $entry)) {
throw new Exception(array("quiqqer/ckeditor4", "exception.install.file.exists"));
}
rename(
$tmpDir . "/content/" . $entry,
$this->installedPluginDir . "/" . $entry
);
}
File::deleteDir($tmpDir);
\QUI::getMessagesHandler()->addSuccess(
\QUI::getLocale()->get(
"quiqqer/ckeditor4",
"message.plugin.install.success"
)
);
}
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#########################################
# Enable/Disable #
#########################################
/**
* Activates the given plugin name
*
* @param $pluginName
*
* @throws Exception
*/
public function activate($pluginName)
{
$pluginName = Orthos::clearPath($pluginName);
$pluginName = str_replace("/", "", $pluginName);
if (!is_dir($this->installedPluginDir . "/" . $pluginName)) {
throw new Exception(array("quiqqer/ckeditor4", "exception.plugin.activate.plugin.not.found"));
}
if (is_dir($this->activePluginDir . "/" . $pluginName)) {
throw new Exception(array("quiqqer/ckeditor4", "exception.plugin.already.active"));
}
$deps = $this->getDependencies($pluginName);
foreach ($deps as $dep) {
try {
$this->activate($dep);
} catch (\Exception $Exception) {
}
}
rename($this->installedPluginDir . "/" . $pluginName, $this->activePluginDir . "/" . $pluginName);
\QUI\Cache\Manager::clear("quiqqer/ckeditor/plugins/data");
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
}
/**
* Deactivates the given plugin name
*
* @param $pluginName
*
* @throws Exception
*/
public function deactivate($pluginName)
{
$pluginName = Orthos::clearPath($pluginName);
$pluginName = str_replace("/", "", $pluginName);
if (!is_dir($this->activePluginDir . "/" . $pluginName)) {
throw new Exception(array("quiqqer/ckeditor4", "exception.plugin.activate.plugin.not.active"));
}
if (is_dir($this->installedPluginDir . "/" . $pluginName)) {
File::deleteDir($this->activePluginDir . "/" . $pluginName);
return;
}
foreach ($this->getDependentPlugins($pluginName) as $depName) {
try {
$this->deactivate($depName);
} catch (\Exception $Exception) {
}
}
rename($this->activePluginDir . "/" . $pluginName, $this->installedPluginDir . "/" . $pluginName);
\QUI\Cache\Manager::clear("quiqqer/ckeditor/plugins/data");
/**
* Returns all active plugins
*
* @return string[] - Array of active plugin names
*/
public function getActivePlugins()
{
$result = array();
$content = scandir($this->activePluginDir);
if ($content === false) {
return array();
}
foreach ($content as $entry) {
if ($entry == "." || $entry == "..") {
continue;
}
$fullpath = $this->activePluginDir . "/" . $entry;
if (!is_dir($fullpath)) {
continue;
}
$result[] = $entry;
}
return $result;
#########################################
# Dependencies #
#########################################
* Gets all dependencies for the given plugin.
* Including dependencies fo dependencies
* Returns false on error
* @param $pluginName
*
* @return array|false
public function getDependencies($pluginName)
try {
$this->loadDependencies();
} catch (\Exception $Exception) {
return false;
}
if (!isset($this->dependencies[$pluginName])) {
return array();
}
$deps = $this->dependencies[$pluginName];
foreach ($deps as $dep) {
$result[] = $dep;
$subDeps = $this->getDependencies($dep);
$result = array_merge($result, $subDeps);
}
/**
* Returns an array of packages that depend on the given plugin
* Returns false on error
*
* @param $pluginName
*
* @return array|bool
*/
public function getDependentPlugins($pluginName)
{
$result = array();
try {
$this->loadDependencies();
} catch (\Exception $Exception) {
return false;
}
foreach ($this->dependencies as $pkg => $deps) {
if (in_array($pluginName, $deps)) {
$result[] = $pkg;
$result = array_unique($result);
return $result;
* Loads the dependencies for the installed modules
*
* @throws Exception
protected function loadDependencies()
if (isset($this->dependencies) && !empty($this->dependencies)) {
return;
}
if (!file_exists($this->getPluginDir() . "/dependencies.json")) {
Log::addWarning("Missing dependency file: " . $this->getPluginDir() . "/dependencies.json");
throw new Exception("missing.dependency.file");
}
$json = file_get_contents($this->getPluginDir() . "/dependencies.json");
$deps = json_decode($json, true);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new Exception(json_last_error_msg());
}
#########################################
# Helper #
#########################################
/**
* Returns a list of all plugins and their details
* Format:
* array(
* [0] => array(
* ["name"] => "pluginname",
* ["state"] => 0|1 (1 for active; 0 for inactive)
* )
* )
*
* @return array
*/
public function getAllPlugins()
{
$result = array();
foreach ($this->getActivePlugins() as $plugin) {
$result[] = array(
'name' => $plugin,
'state' => 1
);
}
foreach ($this->getInstalledPlugins() as $plugin) {
$result[] = array(
'name' => $plugin,
'state' => 0
);
/**
* Returns the plugin dir
*
* @return string
*/
public function getPluginDir()
{
return \QUI::getPackage("quiqqer/ckeditor4")->getVarDir() . "/plugins";
}
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
/**
* Recursively copies the target directory to the target location
*
* @param $src
* @param $target
*/
public function copyDir($src, $target)
{
if (!is_dir($target)) {
mkdir($target, 0755);
}
$entries = scandir($src);
foreach ($entries as $entry) {
if ($entry == "." || $entry == "..") {
continue;
}
$fullpath = $src . "/" . $entry;
if (is_dir($fullpath)) {
$this->copyDir($fullpath, $target . "/" . $entry);
continue;
}
copy($fullpath, $target . "/" . $entry);
}
}