Skip to content

Commit

Permalink
improv: fix #35, update plugins/themes
Browse files Browse the repository at this point in the history
  • Loading branch information
Eywek committed Apr 29, 2018
1 parent 12bce65 commit f88970e
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 19 deletions.
29 changes: 21 additions & 8 deletions app/Controller/Component/EyPluginComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -538,8 +538,8 @@ public function download($slug, $install = false)
return 'ERROR__PLUGIN_CANT_BE_DOWNLOADED';

// Temporary file
$filename = ROOT . DS . 'app' . DS . 'tmp' . DS . 'plugin-' . $slug . '.zip';
$file = fopen($filename, 'w+');
$zipFile = ROOT . DS . 'app' . DS . 'tmp' . DS . 'plugin-' . $slug . '.zip';
$file = fopen($zipFile, 'w+');
if (!fwrite($file, $zip)) {
$this->log('Error when downloading plugin, save files failed.');
return 'ERROR__PLUGIN_PERMISSIONS';
Expand All @@ -548,17 +548,31 @@ public function download($slug, $install = false)

// Set into plugin folder
$zip = new ZipArchive;
$res = $zip->open($filename);
$res = $zip->open($zipFile);
if ($res !== TRUE) {
$this->log('Error when downloading plugin, unable to open zip.');
$this->log('Error when downloading plugin, unable to open zip. (CODE: ' . $res . ')');
return 'ERROR__PLUGIN_PERMISSIONS';
}
$zip->extractTo($this->pluginsFolder . DS);
if (!file_exists(ROOT . DS . 'app' . DS . 'Plugin' . DS . $slug) && !mkdir(ROOT . DS . 'app' . DS . 'Plugin' . DS . $slug))
return 'ERROR__PLUGIN_PERMISSIONS';
for($i = 0; $i < $zip->numFiles; $i++) {
$filename = $zip->getNameIndex($i);
$fileinfo = pathinfo($filename);
$stat = $zip->statIndex($i);
if ($fileinfo['basename'] === 'Plugin-' . $slug . '-master') continue;

$target = "zip://".$zipFile."#".$filename;
$dest = ROOT . DS . 'app' . DS . 'Plugin' . DS . $slug . substr($filename, strlen('Plugin-' . $slug . '-master'));
if ($stat['size'] === 0 && strpos($filename, '.') === false) {
if (!file_exists($dest)) mkdir($dest);
continue;
}
if (!copy($target, $dest)) return 'ERROR__PLUGIN_PERMISSIONS';
}
$zip->close();
rename(ROOT . DS . 'app' . DS . 'Plugin' . DS . 'Plugin-' . $slug . '-master', ROOT . DS . 'app' . DS . 'Plugin' . DS . $slug);

// Delete temporary file
unlink($filename);
unlink($zipFile);

// Delete MacOS hidden files
App::uses('Folder', 'Utility');
Expand Down Expand Up @@ -621,7 +635,6 @@ public function update($slug)
{
// get config from api
$config = $this->getPluginFromAPI($slug);
$slug = $config->name;
// check requirements
if (!$config || empty($config))
return 'ERROR__PLUGIN_REQUIREMENTS';
Expand Down
61 changes: 50 additions & 11 deletions app/Controller/Component/ThemeComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,10 @@ public function getThemesOnAPI($all = true, $deleteInstalledThemes = false)
$installed = $this->getThemesInstalled();
$themeInstalledID = [];
foreach ($installed as $themeInstalled) {
$themeInstalledID[] = $themeInstalled->slug;
$themeInstalledID[] = strtolower($themeInstalled->slug);
}
foreach ($themes as $key => $theme) {
if (in_array($theme['slug'], $themeInstalledID))
if (in_array(strtolower($theme['slug']), $themeInstalledID))
unset($themes[$key]);
}
}
Expand Down Expand Up @@ -329,20 +329,59 @@ public function checkSupported($slug)
return $errors;
}

private function download($slug) {
$zip = $this->controller->sendGetRequest('https://github.com/MineWeb/Theme-' . $slug . '/archive/master.zip');
if (!$zip)
return 'THEME__ERROR_INSTALL_DOWNLOAD_FAILED';

// Temporary file
$zipFile = ROOT . DS . 'app' . DS . 'tmp' . DS . 'theme-' . $slug . '.zip';
$file = fopen($zipFile, 'w+');
if (!fwrite($file, $zip)) {
$this->log('Error when downloading theme, save files failed.');
return 'THEME__ERROR_INSTALL_UNZIP';
}
fclose($file);

// Set into plugin folder
$zip = new ZipArchive;
$res = $zip->open($zipFile);
if ($res !== TRUE) {
$this->log('Error when downloading theme, unable to open zip. (CODE: ' . $res . ')');
return 'THEME__ERROR_INSTALL_UNZIP';
}
if (!file_exists(ROOT . DS . 'app' . DS . 'View' . DS . 'Themed' . DS . $slug) && !mkdir(ROOT . DS . 'app' . DS . 'View' . DS . 'Themed' . DS . $slug))
return 'THEME__ERROR_INSTALL_UNZIP';
for($i = 0; $i < $zip->numFiles; $i++) {
$filename = $zip->getNameIndex($i);
$fileinfo = pathinfo($filename);
$stat = $zip->statIndex($i);
if ($fileinfo['basename'] === 'Theme-' . $slug . '-master') continue;

$target = "zip://".$zipFile."#".$filename;
$dest = ROOT . DS . 'app' . DS . 'View' . DS . 'Themed' . DS . $slug . substr($filename, strlen('Theme-' . $slug . '-master'));
if ($stat['size'] === 0 && strpos($filename, '.') === false) {
if (!file_exists($dest)) mkdir($dest);
continue;
}
if (!copy($target, $dest)) return 'THEME__ERROR_INSTALL_UNZIP';
}
$zip->close();

// Delete temporary file
unlink($zipFile);
return true;
}

// install plugin
public function install($slug, $update = false)
{
// ask to api
$zip = $this->controller->sendGetRequest('https://github.com/MineWeb/Theme-' . $slug . '/archive/master.zip');
$download = $this->download($slug);
if ($download !== true)
return $download;
if ($update)
$oldConfig = $this->getCustomData($slug)[0];
// unzip files
if (!unzip($zip, $this->themesFolder, 'theme-' . $slug . '-zip', true)) {
$this->log('[Install theme] Couldn\'t unzip files.');
return 'THEME__ERROR_INSTALL_UNZIP';
}
rename(ROOT . DS . 'app' . DS . 'View' . DS . 'Themed' . DS . 'Theme-' . $slug . '-master', ROOT . DS . 'app' . DS . 'View' . DS . 'Themed' . DS . $slug);
// delete mac os files (fuck hidden files)
$oldConfig = $this->getCustomData($slug)[0];
App::uses('Folder', 'Utility');
$folder = new Folder($this->themesFolder . DS . '__MACOSX');
$folder->delete();
Expand Down

0 comments on commit f88970e

Please sign in to comment.