akaunting/app/Utilities/Updater.php

295 lines
8.3 KiB
PHP
Raw Normal View History

2017-09-14 22:21:00 +03:00
<?php
namespace App\Utilities;
use App\Models\Module\Module as Model;
use App\Models\Module\ModuleHistory as ModelHistory;
use App\Traits\SiteApi;
use Cache;
use Date;
use File;
2017-09-19 12:32:06 +03:00
use ZipArchive;
2018-12-18 18:30:23 +03:00
use Artisan;
2018-05-25 06:11:53 +03:00
use GuzzleHttp\Exception\RequestException;
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-11-16 10:21:14 +03:00
public static function download($alias, $version, $installed)
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') {
$url = 'core/download/' . $version . '/' . $info['php'] . '/' . $info['mysql'];
} else {
$url = 'apps/' . $alias . '/download/' . $version . '/' . $info['akaunting'] . '/' . $info['token'];
2017-09-14 22:21:00 +03:00
}
2019-11-16 10:21:14 +03:00
$response = static::getRemote($url, 'GET', ['timeout' => 50, 'track_redirects' => true]);
2017-09-14 22:21:00 +03:00
2018-05-25 06:11:53 +03:00
// Exception
if ($response instanceof RequestException) {
return [
'success' => false,
'errors' => trans('modules.errors.download', ['module' => $name]),
'data' => [
'path' => $path
]
];
2018-05-25 06:11:53 +03:00
}
if ($response && ($response->getStatusCode() == 200)) {
2017-09-14 22:21:00 +03:00
$file = $response->getBody()->getContents();
$path = 'temp-' . md5(mt_rand());
$temp_path = storage_path('app/temp') . '/' . $path;
$file_path = $temp_path . '/upload.zip';
// Create tmp directory
if (!File::isDirectory($temp_path)) {
File::makeDirectory($temp_path);
}
// Add content to the Zip file
$uploaded = is_int(file_put_contents($file_path, $file)) ? true : false;
if (!$uploaded) {
return [
'success' => false,
2019-11-16 10:21:14 +03:00
'errors' => trans('modules.errors.zip', ['module' => $alias]),
'data' => [
'path' => $path
]
];
}
2019-11-16 10:21:14 +03:00
try {
event(new \App\Events\Install\UpdateDownloaded($alias, $version, $installed));
2019-11-16 10:21:14 +03:00
return [
'success' => true,
'errors' => false,
'data' => [
'path' => $path
]
];
} catch (\Exception $e) {
return [
'success' => false,
'errors' => trans('modules.errors.download', ['module' => $alias]),
'data' => []
];
}
2017-09-14 22:21:00 +03:00
}
return [
'success' => false,
2019-11-16 10:21:14 +03:00
'errors' => trans('modules.errors.download', ['module' => $alias]),
'data' => [
'path' => $path
]
];
2017-09-14 22:21:00 +03:00
}
2019-11-16 10:21:14 +03:00
public static function unzip($path, $alias, $version, $installed)
2018-09-27 17:08:00 +03:00
{
$temp_path = storage_path('app/temp') . '/' . $path;
$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)) {
return [
'success' => false,
2019-11-16 10:21:14 +03:00
'errors' => trans('modules.errors.unzip', ['module' => $alias]),
'data' => [
'path' => $path
]
];
2018-09-27 17:08:00 +03:00
}
$zip->close();
// Delete zip file
File::delete($file);
2019-11-16 10:21:14 +03:00
try {
event(new \App\Events\Install\UpdateUnzipped($alias, $version, $installed));
return [
'success' => true,
'errors' => false,
'data' => [
'path' => $path
]
];
} catch (\Exception $e) {
return [
'success' => false,
'errors' => trans('modules.errors.unzip', ['module' => $alias]),
'data' => []
];
}
2018-09-27 17:08:00 +03:00
}
2019-11-16 10:21:14 +03:00
public static function fileCopy($path, $alias, $version, $installed)
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())) {
return [
'success' => false,
2019-11-16 10:21:14 +03:00
'errors' => trans('modules.errors.file_copy', ['module' => $alias]),
'data' => [
'path' => $path
]
];
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)) {
return [
'success' => false,
2019-11-16 10:21:14 +03:00
'errors' => trans('modules.errors.file_copy', ['module' => $alias]),
'data' => [
'path' => $path
]
];
2018-09-27 17:08:00 +03:00
}
2019-01-07 16:13:40 +03:00
$model = Model::where('alias', $alias)->first();
if (!empty($model)) {
// Add history
ModelHistory::create([
'company_id' => session('company_id'),
'module_id' => $model->id,
'category' => $module->get('category', 'payment-method'),
2019-01-07 16:13:40 +03:00
'version' => $version,
2019-11-16 10:21:14 +03:00
'description' => trans('modules.history.updated', ['module' => $module->get('alias')]),
2019-01-07 16:13:40 +03:00
]);
}
2018-09-27 17:08:00 +03:00
}
// Delete temp directory
File::deleteDirectory($temp_path);
Artisan::call('cache:clear');
2019-11-16 10:21:14 +03:00
try {
event(new \App\Events\Install\UpdateCopied($alias, $version, $installed));
return [
'success' => true,
'errors' => false,
'data' => [
'path' => $path
]
];
} catch (\Exception $e) {
return [
'success' => false,
'errors' => trans('modules.errors.file_copy', ['module' => $alias]),
'data' => []
];
}
}
2019-11-16 10:21:14 +03:00
public static function finish($alias, $version, $installed)
{
// Check if the file mirror was successful
if (($alias == 'core') && (version('short') != $version)) {
return [
'success' => false,
2019-11-16 10:21:14 +03:00
'errors' => trans('modules.errors.file_copy', ['module' => $alias]),
'data' => []
];
}
// Clear cache after update
Artisan::call('cache:clear');
try {
2019-11-16 10:21:14 +03:00
event(new \App\Events\Install\UpdateFinished($alias, $installed, $version));
return [
'success' => true,
'errors' => false,
'data' => []
];
} catch (\Exception $e) {
return [
'success' => false,
2019-11-16 10:21:14 +03:00
'errors' => trans('modules.errors.finish', ['module' => $alias]),
'data' => []
];
}
2018-09-27 17:08:00 +03:00
}
2017-09-14 22:21:00 +03:00
public static function all()
{
// Get data from cache
$data = Cache::get('updates');
if (!empty($data)) {
2017-09-14 22:21:00 +03:00
return $data;
}
// No data in cache, grab them from remote
$data = array();
2019-11-16 10:21:14 +03:00
$modules = module()->all();
2017-09-14 22:21:00 +03:00
$versions = Versions::latest($modules);
foreach ($versions as $alias => $version) {
// Modules come as array
if ($alias == 'core') {
if (version_compare(version('short'), $version) != 0) {
$data['core'] = $version;
}
} else {
2019-11-16 10:21:14 +03:00
$module = module($alias);
2017-09-14 22:21:00 +03:00
// Up-to-date
if (version_compare($module->get('version'), $version) == 0) {
continue;
}
$data[$alias] = $version;
}
}
Cache::put('updates', $data, Date::now()->addHour(6));
return $data;
}
2018-09-27 17:08:00 +03:00
}