2017-09-14 22:21:00 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Traits;
|
|
|
|
|
2017-09-19 12:32:06 +03:00
|
|
|
use App\Utilities\Info;
|
2017-09-14 22:21:00 +03:00
|
|
|
use Artisan;
|
|
|
|
use File;
|
|
|
|
use GuzzleHttp\Client;
|
2017-09-19 12:32:06 +03:00
|
|
|
use Module;
|
|
|
|
use ZipArchive;
|
2017-09-14 22:21:00 +03:00
|
|
|
|
|
|
|
trait Modules
|
|
|
|
{
|
|
|
|
|
2018-04-25 16:56:01 +03:00
|
|
|
public function checkToken($token)
|
|
|
|
{
|
|
|
|
$data = [
|
|
|
|
'form_params' => [
|
|
|
|
'token' => $token,
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
|
|
|
$response = $this->getRemote('token/check', 'POST', $data);
|
|
|
|
|
|
|
|
if ($response->getStatusCode() == 200) {
|
|
|
|
$result = json_decode($response->getBody());
|
|
|
|
|
|
|
|
return ($result->success) ? true : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-09-14 22:21:00 +03:00
|
|
|
public function getModules()
|
|
|
|
{
|
2017-11-01 22:15:25 +03:00
|
|
|
$response = $this->getRemote('apps/items');
|
2017-09-14 22:21:00 +03:00
|
|
|
|
|
|
|
if ($response->getStatusCode() == 200) {
|
|
|
|
return json_decode($response->getBody())->data;
|
|
|
|
}
|
|
|
|
|
2017-10-11 16:01:23 +03:00
|
|
|
return [];
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getModule($alias)
|
|
|
|
{
|
2017-11-01 22:15:25 +03:00
|
|
|
$response = $this->getRemote('apps/' . $alias);
|
2017-09-14 22:21:00 +03:00
|
|
|
|
|
|
|
if ($response->getStatusCode() == 200) {
|
|
|
|
return json_decode($response->getBody())->data;
|
|
|
|
}
|
|
|
|
|
2017-10-11 16:01:23 +03:00
|
|
|
return [];
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getCategories()
|
|
|
|
{
|
2017-11-01 22:15:25 +03:00
|
|
|
$response = $this->getRemote('apps/categories');
|
2017-09-14 22:21:00 +03:00
|
|
|
|
|
|
|
if ($response->getStatusCode() == 200) {
|
|
|
|
return json_decode($response->getBody())->data;
|
|
|
|
}
|
|
|
|
|
2017-10-11 16:01:23 +03:00
|
|
|
return [];
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getModulesByCategory($alias)
|
|
|
|
{
|
2017-11-01 22:15:25 +03:00
|
|
|
$response = $this->getRemote('apps/categories/' . $alias);
|
2017-09-14 22:21:00 +03:00
|
|
|
|
|
|
|
if ($response->getStatusCode() == 200) {
|
|
|
|
return json_decode($response->getBody())->data;
|
|
|
|
}
|
|
|
|
|
2017-10-11 16:01:23 +03:00
|
|
|
return [];
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
|
2017-12-06 21:29:43 +03:00
|
|
|
public function getPaidModules($data = [])
|
2017-09-14 22:21:00 +03:00
|
|
|
{
|
2017-12-06 21:29:43 +03:00
|
|
|
$response = $this->getRemote('apps/paid', 'GET', $data);
|
2017-09-14 22:21:00 +03:00
|
|
|
|
|
|
|
if ($response->getStatusCode() == 200) {
|
|
|
|
return json_decode($response->getBody())->data;
|
|
|
|
}
|
|
|
|
|
2017-10-11 16:01:23 +03:00
|
|
|
return [];
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
|
2017-12-06 21:29:43 +03:00
|
|
|
public function getNewModules($data = [])
|
2017-09-14 22:21:00 +03:00
|
|
|
{
|
2017-12-06 21:29:43 +03:00
|
|
|
$response = $this->getRemote('apps/new', 'GET', $data);
|
2017-09-14 22:21:00 +03:00
|
|
|
|
|
|
|
if ($response->getStatusCode() == 200) {
|
|
|
|
return json_decode($response->getBody())->data;
|
|
|
|
}
|
|
|
|
|
2017-10-11 16:01:23 +03:00
|
|
|
return [];
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
|
2017-12-06 21:29:43 +03:00
|
|
|
public function getFreeModules($data = [])
|
2017-09-14 22:21:00 +03:00
|
|
|
{
|
2017-12-06 21:29:43 +03:00
|
|
|
$response = $this->getRemote('apps/free', 'GET', $data);
|
2017-09-14 22:21:00 +03:00
|
|
|
|
|
|
|
if ($response->getStatusCode() == 200) {
|
|
|
|
return json_decode($response->getBody())->data;
|
|
|
|
}
|
|
|
|
|
2018-04-25 19:28:10 +03:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getSearchModules($data = [])
|
|
|
|
{
|
|
|
|
$response = $this->getRemote('apps/search', 'GET', $data);
|
|
|
|
|
|
|
|
if ($response->getStatusCode() == 200) {
|
|
|
|
return json_decode($response->getBody())->data;
|
|
|
|
}
|
|
|
|
|
2017-10-11 16:01:23 +03:00
|
|
|
return [];
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getCoreVersion()
|
|
|
|
{
|
|
|
|
$data['query'] = Info::all();
|
|
|
|
|
|
|
|
$response = $this->getRemote('core/version', 'GET', $data);
|
|
|
|
|
|
|
|
if ($response->getStatusCode() == 200) {
|
|
|
|
return $response->json();
|
|
|
|
}
|
|
|
|
|
2017-10-11 16:01:23 +03:00
|
|
|
return [];
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function downloadModule($path)
|
|
|
|
{
|
|
|
|
$response = $this->getRemote($path);
|
|
|
|
|
|
|
|
if ($response->getStatusCode() == 200) {
|
|
|
|
$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 false;
|
|
|
|
}
|
|
|
|
|
2017-10-11 16:01:23 +03:00
|
|
|
$data = [
|
2017-09-14 22:21:00 +03:00
|
|
|
'path' => $path
|
2017-10-11 16:01:23 +03:00
|
|
|
];
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2017-10-11 16:01:23 +03:00
|
|
|
return [
|
2017-09-14 22:21:00 +03:00
|
|
|
'success' => true,
|
|
|
|
'errors' => false,
|
|
|
|
'data' => $data,
|
2017-10-11 16:01:23 +03:00
|
|
|
];
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
|
2017-10-11 16:01:23 +03:00
|
|
|
return [
|
2017-09-14 22:21:00 +03:00
|
|
|
'success' => false,
|
|
|
|
'errors' => true,
|
|
|
|
'data' => null,
|
2017-10-11 16:01:23 +03:00
|
|
|
];
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function unzipModule($path)
|
|
|
|
{
|
|
|
|
$temp_path = storage_path('app/temp') . '/' . $path;
|
|
|
|
|
|
|
|
$file = $temp_path . '/upload.zip';
|
|
|
|
|
|
|
|
// Unzip the file
|
2017-09-19 12:32:06 +03:00
|
|
|
$zip = new ZipArchive();
|
|
|
|
|
|
|
|
if (!$zip->open($file) || !$zip->extractTo($temp_path)) {
|
|
|
|
return [
|
2017-09-14 22:21:00 +03:00
|
|
|
'success' => false,
|
|
|
|
'errors' => true,
|
|
|
|
'data' => null,
|
2017-09-19 12:32:06 +03:00
|
|
|
];
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
|
2017-09-19 12:32:06 +03:00
|
|
|
$zip->close();
|
|
|
|
|
2017-09-14 22:21:00 +03:00
|
|
|
// Remove Zip
|
2017-09-19 12:32:06 +03:00
|
|
|
File::delete($file);
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2017-09-19 12:32:06 +03:00
|
|
|
$data = [
|
2017-09-14 22:21:00 +03:00
|
|
|
'path' => $path
|
2017-09-19 12:32:06 +03:00
|
|
|
];
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2017-09-19 12:32:06 +03:00
|
|
|
return [
|
2017-09-14 22:21:00 +03:00
|
|
|
'success' => true,
|
|
|
|
'errors' => false,
|
|
|
|
'data' => $data,
|
2017-09-19 12:32:06 +03:00
|
|
|
];
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function installModule($path)
|
|
|
|
{
|
|
|
|
$temp_path = storage_path('app/temp') . '/' . $path;
|
|
|
|
|
|
|
|
$modules_path = base_path() . '/modules';
|
|
|
|
|
|
|
|
// Create modules directory
|
|
|
|
if (!File::isDirectory($modules_path)) {
|
|
|
|
File::makeDirectory($modules_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
$module = json_decode(file_get_contents($temp_path . '/module.json'));
|
|
|
|
|
|
|
|
$module_path = $modules_path . '/' . $module->name;
|
|
|
|
|
|
|
|
// Create module directory
|
|
|
|
if (!File::isDirectory($module_path)) {
|
|
|
|
File::makeDirectory($module_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move all files/folders from temp path then delete it
|
|
|
|
File::copyDirectory($temp_path, $module_path);
|
|
|
|
File::deleteDirectory($temp_path);
|
|
|
|
|
2017-11-01 22:15:25 +03:00
|
|
|
Artisan::call('cache:clear');
|
|
|
|
|
2017-10-11 16:01:23 +03:00
|
|
|
$data = [
|
|
|
|
'path' => $path,
|
2017-11-01 22:15:25 +03:00
|
|
|
'name' => $module->name,
|
2017-10-11 16:01:23 +03:00
|
|
|
'alias' => $module->alias
|
|
|
|
];
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2017-10-11 16:01:23 +03:00
|
|
|
return [
|
2017-09-14 22:21:00 +03:00
|
|
|
'success' => true,
|
|
|
|
'installed' => true,
|
|
|
|
'errors' => false,
|
|
|
|
'data' => $data,
|
2017-10-11 16:01:23 +03:00
|
|
|
];
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function uninstallModule($alias)
|
|
|
|
{
|
|
|
|
$module = Module::findByAlias($alias);
|
|
|
|
|
2017-10-11 16:01:23 +03:00
|
|
|
$data = [
|
2017-09-14 22:21:00 +03:00
|
|
|
'name' => $module->get('name'),
|
|
|
|
'category' => $module->get('category'),
|
|
|
|
'version' => $module->get('version'),
|
2017-10-11 16:01:23 +03:00
|
|
|
];
|
2017-09-14 22:21:00 +03:00
|
|
|
|
|
|
|
$module->delete();
|
|
|
|
|
|
|
|
Artisan::call('cache:clear');
|
|
|
|
|
2017-10-11 16:01:23 +03:00
|
|
|
return [
|
2017-09-14 22:21:00 +03:00
|
|
|
'success' => true,
|
|
|
|
'errors' => false,
|
|
|
|
'data' => $data
|
2017-10-11 16:01:23 +03:00
|
|
|
];
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
|
2017-11-01 18:23:50 +03:00
|
|
|
public function enableModule($alias)
|
2017-09-14 22:21:00 +03:00
|
|
|
{
|
|
|
|
$module = Module::findByAlias($alias);
|
|
|
|
|
2017-10-11 16:01:23 +03:00
|
|
|
$data = [
|
2017-09-14 22:21:00 +03:00
|
|
|
'name' => $module->get('name'),
|
|
|
|
'category' => $module->get('category'),
|
|
|
|
'version' => $module->get('version'),
|
2017-10-11 16:01:23 +03:00
|
|
|
];
|
2017-09-14 22:21:00 +03:00
|
|
|
|
|
|
|
$module->enable();
|
|
|
|
|
|
|
|
Artisan::call('cache:clear');
|
|
|
|
|
2017-10-11 16:01:23 +03:00
|
|
|
return [
|
2017-09-14 22:21:00 +03:00
|
|
|
'success' => true,
|
|
|
|
'errors' => false,
|
|
|
|
'data' => $data
|
2017-10-11 16:01:23 +03:00
|
|
|
];
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
|
2017-11-01 18:23:50 +03:00
|
|
|
public function disableModule($alias)
|
2017-09-14 22:21:00 +03:00
|
|
|
{
|
|
|
|
$module = Module::findByAlias($alias);
|
|
|
|
|
2017-10-11 16:01:23 +03:00
|
|
|
$data = [
|
2017-09-14 22:21:00 +03:00
|
|
|
'name' => $module->get('name'),
|
|
|
|
'category' => $module->get('category'),
|
|
|
|
'version' => $module->get('version'),
|
2017-10-11 16:01:23 +03:00
|
|
|
];
|
2017-09-14 22:21:00 +03:00
|
|
|
|
|
|
|
$module->disable();
|
|
|
|
|
|
|
|
Artisan::call('cache:clear');
|
|
|
|
|
2017-10-11 16:01:23 +03:00
|
|
|
return [
|
2017-09-14 22:21:00 +03:00
|
|
|
'success' => true,
|
|
|
|
'errors' => false,
|
|
|
|
'data' => $data
|
2017-10-11 16:01:23 +03:00
|
|
|
];
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function getRemote($path, $method = 'GET', $data = array())
|
|
|
|
{
|
|
|
|
$base = 'https://akaunting.com/api/';
|
|
|
|
|
|
|
|
$client = new Client(['verify' => false, 'base_uri' => $base]);
|
|
|
|
|
2017-10-11 16:01:23 +03:00
|
|
|
$headers['headers'] = [
|
2017-09-14 22:21:00 +03:00
|
|
|
'Authorization' => 'Bearer ' . setting('general.api_token'),
|
|
|
|
'Accept' => 'application/json',
|
2017-11-01 18:30:12 +03:00
|
|
|
'Referer' => env('APP_URL'),
|
2017-10-11 16:01:23 +03:00
|
|
|
];
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2017-10-17 16:56:53 +03:00
|
|
|
$data['http_errors'] = false;
|
|
|
|
|
2017-09-14 22:21:00 +03:00
|
|
|
$data = array_merge($data, $headers);
|
|
|
|
|
|
|
|
$result = $client->request($method, $path, $data);
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
}
|