first commit
This commit is contained in:
44
app/Utilities/Info.php
Normal file
44
app/Utilities/Info.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace App\Utilities;
|
||||
|
||||
use DB;
|
||||
use App\Models\Company\Company;
|
||||
|
||||
class Info
|
||||
{
|
||||
|
||||
public static function versions()
|
||||
{
|
||||
$v = array();
|
||||
|
||||
$v['akaunting'] = version('short');
|
||||
|
||||
$v['php'] = static::phpVersion();
|
||||
|
||||
$v['mysql'] = static::mysqlVersion();
|
||||
|
||||
return $v;
|
||||
}
|
||||
|
||||
public static function all()
|
||||
{
|
||||
$data = static::versions();
|
||||
|
||||
$data['token'] = setting('general.token');
|
||||
|
||||
$data['companies'] = Company::all()->count();
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
public static function phpVersion()
|
||||
{
|
||||
return phpversion();
|
||||
}
|
||||
|
||||
public static function mysqlVersion()
|
||||
{
|
||||
return DB::selectOne('select version() as mversion')->mversion;
|
||||
}
|
||||
}
|
||||
49
app/Utilities/Modules.php
Normal file
49
app/Utilities/Modules.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace App\Utilities;
|
||||
|
||||
use Cache;
|
||||
use Date;
|
||||
use Module;
|
||||
|
||||
use App\Events\PaymentGatewayListing;
|
||||
|
||||
class Modules
|
||||
{
|
||||
|
||||
public static function getPaymentMethods()
|
||||
{
|
||||
$payment_methods = Cache::get('payment_methods');
|
||||
|
||||
if (!empty($payment_methods)) {
|
||||
return $payment_methods;
|
||||
}
|
||||
|
||||
$gateways = array();
|
||||
|
||||
// Fire the event to extend the menu
|
||||
$results = event(new PaymentGatewayListing($gateways));
|
||||
|
||||
foreach ($results as $gateways) {
|
||||
foreach ($gateways as $gateway) {
|
||||
$methods[] = $gateway;
|
||||
}
|
||||
}
|
||||
|
||||
$sort_order = array();
|
||||
|
||||
foreach ($methods as $key => $value) {
|
||||
$sort_order[$key] = $value['order'];
|
||||
}
|
||||
|
||||
array_multisort($sort_order, SORT_ASC, $methods);
|
||||
|
||||
foreach ($methods as $method) {
|
||||
$payment_methods[$method['code']] = $method['name'];
|
||||
}
|
||||
|
||||
Cache::put('payment_methods', $payment_methods, Date::now()->addHour(6));
|
||||
|
||||
return $payment_methods;
|
||||
}
|
||||
}
|
||||
158
app/Utilities/Updater.php
Normal file
158
app/Utilities/Updater.php
Normal file
@@ -0,0 +1,158 @@
|
||||
<?php
|
||||
|
||||
namespace App\Utilities;
|
||||
|
||||
use App\Models\Module\Module as Model;
|
||||
use App\Models\Module\ModuleHistory as ModelHistory;
|
||||
use App\Traits\SiteApi;
|
||||
use Artisan;
|
||||
use Cache;
|
||||
use Date;
|
||||
use File;
|
||||
use Module;
|
||||
use Zipper;
|
||||
|
||||
class Updater
|
||||
{
|
||||
use SiteApi;
|
||||
|
||||
public static function clear()
|
||||
{
|
||||
Cache::forget('modules');
|
||||
Cache::forget('updates');
|
||||
Cache::forget('versions');
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Update
|
||||
public static function update($alias, $version)
|
||||
{
|
||||
if (!$data = static::download($alias, $version)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$path = 'temp-' . md5(mt_rand());
|
||||
$temp_path = storage_path('app/temp') . '/' . $path;
|
||||
|
||||
$file = $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, $data)) ? true : false;
|
||||
|
||||
if (!$uploaded) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Unzip the file
|
||||
try {
|
||||
Zipper::make($file)->extractTo($temp_path);
|
||||
} catch (\RuntimeException $e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Remove Zip
|
||||
Zipper::delete();
|
||||
|
||||
if ($alias == 'core') {
|
||||
// Move all files/folders from temp path then delete it
|
||||
File::copyDirectory($temp_path, base_path());
|
||||
File::deleteDirectory($temp_path);
|
||||
|
||||
// Update database
|
||||
Artisan::call('migrate', ['--force' => true]);
|
||||
|
||||
// Check if the file mirror was successful
|
||||
if (version('short') != $version) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
$module = Module::get($alias);
|
||||
$model = Model::where('alias', $alias)->first();
|
||||
|
||||
// Move all files/folders from temp path then delete it
|
||||
File::copyDirectory($temp_path, module_path($module->get('name')));
|
||||
File::deleteDirectory($temp_path);
|
||||
|
||||
// Update database
|
||||
Artisan::call('migrate', ['--force' => true]);
|
||||
|
||||
// Add history
|
||||
ModelHistory::create([
|
||||
'company_id' => session('company_id'),
|
||||
'module_id' => $model->id,
|
||||
'category' => $module->get('category'),
|
||||
'version' => $version,
|
||||
'description' => trans('modules.history.updated', ['module' => $module->get('name')]),
|
||||
]);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static function download($alias, $version)
|
||||
{
|
||||
$file = null;
|
||||
|
||||
// Check core first
|
||||
$info = Info::all();
|
||||
|
||||
if ($alias == 'core') {
|
||||
$url = 'core/download/' . $version . '/' . $info['php'] . '/' . $info['mysql'];
|
||||
} else {
|
||||
$url = 'modules/items/' . $alias . '/download/' . $version . '/' . $info['akaunting'] . '/' . $info['token'];
|
||||
}
|
||||
|
||||
$response = static::getRemote($url, ['timeout' => 30, 'referer' => true, 'track_redirects' => true]);
|
||||
|
||||
if ($response->getStatusCode() == 200) {
|
||||
$file = $response->getBody()->getContents();
|
||||
}
|
||||
|
||||
return $file;
|
||||
}
|
||||
|
||||
public static function all()
|
||||
{
|
||||
// Get data from cache
|
||||
$data = Cache::get('updates');
|
||||
|
||||
if (!empty($data) || !setting('general.api_token')) {
|
||||
return $data;
|
||||
}
|
||||
|
||||
// No data in cache, grab them from remote
|
||||
$data = array();
|
||||
|
||||
$modules = Module::all();
|
||||
|
||||
$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 {
|
||||
$module = Module::get($alias);
|
||||
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
110
app/Utilities/Versions.php
Normal file
110
app/Utilities/Versions.php
Normal file
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
|
||||
namespace App\Utilities;
|
||||
|
||||
use App\Traits\SiteApi;
|
||||
use Cache;
|
||||
use Date;
|
||||
use GuzzleHttp\Client;
|
||||
|
||||
class Versions
|
||||
{
|
||||
use SiteApi;
|
||||
|
||||
public static function changelog()
|
||||
{
|
||||
$output = '';
|
||||
|
||||
$url = 'https://api.github.com/repos/akaunting/akaunting/releases';
|
||||
|
||||
$http = new \GuzzleHttp\Client(['verify' => false]);
|
||||
|
||||
$json = $http->get($url, ['timeout' => 30])->getBody()->getContents();
|
||||
|
||||
if (empty($json)) {
|
||||
return $output;
|
||||
}
|
||||
|
||||
$github = new \cebe\markdown\GithubMarkdown();
|
||||
|
||||
$releases = json_decode($json);
|
||||
|
||||
foreach ($releases as $release) {
|
||||
if ($release->tag_name <= version('short')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($release->prerelease == true) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (empty($release->body)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$output .= '<h2><span class="label label-success">'.$release->tag_name.'</span></h2>';
|
||||
|
||||
// Parse markdown output
|
||||
$markdown = str_replace('## Changelog', '', $release->body);
|
||||
|
||||
$output .= $github->parse($markdown);
|
||||
|
||||
$output .= '<hr>';
|
||||
}
|
||||
|
||||
return $output;
|
||||
}
|
||||
|
||||
public static function latest($modules = array())
|
||||
{
|
||||
// Get data from cache
|
||||
$data = Cache::get('versions');
|
||||
|
||||
if (!empty($data) || !setting('general.api_token')) {
|
||||
return $data;
|
||||
}
|
||||
|
||||
$info = Info::all();
|
||||
|
||||
// No data in cache, grab them from remote
|
||||
$data = array();
|
||||
|
||||
// Check core first
|
||||
$url = 'core/version/' . $info['akaunting'] . '/' . $info['php'] . '/' . $info['mysql'] . '/' . $info['companies'];
|
||||
|
||||
$data['core'] = static::getLatestVersion($url);
|
||||
|
||||
// Then modules
|
||||
foreach ($modules as $module) {
|
||||
$alias = $module->get('alias');
|
||||
$version = $module->get('version');
|
||||
|
||||
$url = 'modules/items/' . $alias . '/version/' . $version . '/' . $info['akaunting'];
|
||||
|
||||
$data[$alias] = static::getLatestVersion($url);
|
||||
}
|
||||
|
||||
Cache::put('versions', $data, Date::now()->addHour(6));
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
public static function getLatestVersion($url)
|
||||
{
|
||||
$response = static::getRemote($url, ['timeout' => 30, 'referer' => true]);
|
||||
|
||||
if ($response->getStatusCode() == 200) {
|
||||
$version = json_decode($response->getBody())->data;
|
||||
|
||||
if (is_object($version)) {
|
||||
$latest = $version->latest;
|
||||
} else {
|
||||
$latest = '0.0.0';
|
||||
}
|
||||
} else {
|
||||
$latest = '0.0.0';
|
||||
}
|
||||
|
||||
return $latest;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user