akaunting/app/Utilities/Updater.php

169 lines
4.4 KiB
PHP
Raw Normal View History

2017-09-14 22:21:00 +03:00
<?php
namespace App\Utilities;
2019-12-03 15:41:56 +03:00
use App\Events\Install\UpdateCopied;
use App\Events\Install\UpdateDownloaded;
use App\Events\Install\UpdateUnzipped;
2019-12-04 18:10:38 +03:00
use App\Utilities\Console;
2017-09-14 22:21:00 +03:00
use App\Traits\SiteApi;
2019-12-04 18:10:38 +03:00
use Artisan;
2017-09-14 22:21:00 +03:00
use Cache;
use Date;
use File;
2017-09-19 12:32:06 +03:00
use ZipArchive;
2017-09-14 22:21:00 +03:00
class Updater
{
use SiteApi;
public static function clear()
{
2018-12-18 18:30:23 +03:00
Artisan::call('cache:clear');
2017-09-14 22:21:00 +03:00
return true;
}
2019-12-03 15:41:56 +03:00
public static function download($alias, $new, $old)
2017-09-14 22:21:00 +03:00
{
$file = null;
$path = null;
2017-09-14 22:21:00 +03:00
// Check core first
$info = Info::all();
if ($alias == 'core') {
2019-12-03 15:41:56 +03:00
$url = 'core/download/' . $new . '/' . $info['php'] . '/' . $info['mysql'];
2017-09-14 22:21:00 +03:00
} else {
2019-12-18 16:06:27 +03:00
$url = 'apps/' . $alias . '/download/' . $new . '/' . $info['akaunting'] . '/' . $info['api_key'];
2017-09-14 22:21:00 +03:00
}
2019-12-04 18:10:38 +03:00
if (!$response = static::getResponse('GET', $url, ['timeout' => 50, 'track_redirects' => true])) {
2019-12-03 15:41:56 +03:00
throw new \Exception(trans('modules.errors.download', ['module' => $alias]));
2018-05-25 06:11:53 +03:00
}
2019-12-03 15:41:56 +03:00
$file = $response->getBody()->getContents();
2019-12-03 15:41:56 +03:00
$path = 'temp-' . md5(mt_rand());
$temp_path = storage_path('app/temp') . '/' . $path;
2019-12-04 18:10:38 +03:00
$file_path = $temp_path . '/upload.zip';
2019-12-03 15:41:56 +03:00
// Create tmp directory
if (!File::isDirectory($temp_path)) {
File::makeDirectory($temp_path);
}
2019-12-03 15:41:56 +03:00
// Add content to the Zip file
$uploaded = is_int(file_put_contents($file_path, $file)) ? true : false;
2019-12-03 15:41:56 +03:00
if (!$uploaded) {
throw new \Exception(trans('modules.errors.zip', ['module' => $alias]));
2017-09-14 22:21:00 +03:00
}
2019-12-03 15:41:56 +03:00
event(new UpdateDownloaded($alias, $new, $old));
return $path;
2017-09-14 22:21:00 +03:00
}
2019-12-03 15:41:56 +03:00
public static function unzip($path, $alias, $new, $old)
2018-09-27 17:08:00 +03:00
{
$temp_path = storage_path('app/temp') . '/' . $path;
2019-12-04 18:10:38 +03:00
$file = $temp_path . '/upload.zip';
2018-09-27 17:08:00 +03:00
// Unzip the file
$zip = new ZipArchive();
if (($zip->open($file) !== true) || !$zip->extractTo($temp_path)) {
2019-12-03 15:41:56 +03:00
throw new \Exception(trans('modules.errors.unzip', ['module' => $alias]));
2018-09-27 17:08:00 +03:00
}
$zip->close();
// Delete zip file
File::delete($file);
2019-12-03 15:41:56 +03:00
event(new UpdateUnzipped($alias, $new, $old));
return $path;
2018-09-27 17:08:00 +03:00
}
2019-12-03 15:41:56 +03:00
public static function copyFiles($path, $alias, $new, $old)
2018-09-27 17:08:00 +03:00
{
$temp_path = storage_path('app/temp') . '/' . $path;
2018-09-27 17:08:00 +03:00
if ($alias == 'core') {
// Move all files/folders from temp path
if (!File::copyDirectory($temp_path, base_path())) {
2019-12-03 15:41:56 +03:00
throw new \Exception(trans('modules.errors.file_copy', ['module' => $alias]));
2018-09-27 17:08:00 +03:00
}
} else {
// Get module instance
2019-11-16 10:21:14 +03:00
$module = module($alias);
2018-09-27 17:08:00 +03:00
2019-11-16 10:21:14 +03:00
$module_path = $module->getPath();
// Create module directory
if (!File::isDirectory($module_path)) {
File::makeDirectory($module_path);
}
2018-09-27 17:08:00 +03:00
// Move all files/folders from temp path
if (!File::copyDirectory($temp_path, $module_path)) {
2019-12-03 15:41:56 +03:00
throw new \Exception(trans('modules.errors.file_copy', ['module' => $alias]));
2019-01-07 16:13:40 +03:00
}
2018-09-27 17:08:00 +03:00
}
// Delete temp directory
File::deleteDirectory($temp_path);
2019-12-03 15:41:56 +03:00
event(new UpdateCopied($alias, $new, $old));
2019-12-03 15:41:56 +03:00
return $path;
}
2019-12-03 15:41:56 +03:00
public static function finish($alias, $new, $old)
{
2019-12-03 15:41:56 +03:00
$company_id = session('company_id');
$command = "php artisan update:finish {$alias} {$company_id} {$new} {$old}";
2019-12-12 11:10:55 +03:00
if (true !== $result = Console::run($command)) {
$message = !empty($result) ? $result : trans('modules.errors.finish', ['module' => $alias]);
throw new \Exception($message);
}
2018-09-27 17:08:00 +03:00
}
2017-09-14 22:21:00 +03:00
public static function all()
{
// Get data from cache
2019-12-17 16:27:50 +03:00
$updates = Cache::get('updates');
2017-09-14 22:21:00 +03:00
2019-12-17 16:27:50 +03:00
if (!empty($updates)) {
return $updates;
2017-09-14 22:21:00 +03:00
}
2019-12-17 16:27:50 +03:00
$updates = [];
2017-09-14 22:21:00 +03:00
2019-11-16 10:21:14 +03:00
$modules = module()->all();
2017-09-14 22:21:00 +03:00
$versions = Versions::latest($modules);
2019-12-17 16:27:50 +03:00
foreach ($versions as $alias => $latest_version) {
$installed_version = ($alias == 'core') ? version('short') : module($alias)->get('version');
if (version_compare($installed_version, $latest_version, '>=')) {
continue;
2017-09-14 22:21:00 +03:00
}
2019-12-17 16:27:50 +03:00
$updates[$alias] = $latest_version;
2017-09-14 22:21:00 +03:00
}
2019-12-17 16:27:50 +03:00
Cache::put('updates', $updates, Date::now()->addHour(6));
2017-09-14 22:21:00 +03:00
2019-12-17 16:27:50 +03:00
return $updates;
2017-09-14 22:21:00 +03:00
}
2018-09-27 17:08:00 +03:00
}