akaunting/app/Traits/Modules.php

561 lines
13 KiB
PHP
Raw Normal View History

2017-09-14 22:21:00 +03:00
<?php
namespace App\Traits;
2017-09-19 12:32:06 +03:00
use App\Utilities\Info;
2018-12-12 20:28:16 +03:00
use App\Models\Module\Module as Model;
2017-09-14 22:21:00 +03:00
use Artisan;
2018-12-12 20:28:16 +03:00
use Cache;
use Date;
2017-09-14 22:21:00 +03:00
use File;
use GuzzleHttp\Client;
2018-05-25 06:11:53 +03:00
use GuzzleHttp\Exception\RequestException;
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);
2018-05-25 06:11:53 +03:00
if ($response && ($response->getStatusCode() == 200)) {
2018-04-25 16:56:01 +03:00
$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
2018-05-25 06:11:53 +03:00
if ($response && ($response->getStatusCode() == 200)) {
2017-09-14 22:21:00 +03:00
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
2018-05-25 06:11:53 +03:00
if ($response && ($response->getStatusCode() == 200)) {
2017-09-14 22:21:00 +03:00
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 getDocumentation($alias)
{
$response = $this->getRemote('apps/docs/' . $alias);
if ($response && ($response->getStatusCode() == 200)) {
return json_decode($response->getBody())->data;
}
return [];
}
2018-09-20 18:35:24 +03:00
public function getModuleReviews($alias, $data = [])
{
$response = $this->getRemote('apps/' . $alias . '/reviews', 'GET', $data);
if ($response && ($response->getStatusCode() == 200)) {
return json_decode($response->getBody())->data;
}
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
2018-05-25 06:11:53 +03:00
if ($response && ($response->getStatusCode() == 200)) {
2017-09-14 22:21:00 +03:00
return json_decode($response->getBody())->data;
}
2017-10-11 16:01:23 +03:00
return [];
2017-09-14 22:21:00 +03:00
}
2018-09-20 11:16:56 +03:00
public function getModulesByCategory($alias, $data = [])
2017-09-14 22:21:00 +03:00
{
2018-09-20 11:16:56 +03:00
$response = $this->getRemote('apps/categories/' . $alias, 'GET', $data);
2017-09-14 22:21:00 +03:00
2018-05-25 06:11:53 +03:00
if ($response && ($response->getStatusCode() == 200)) {
2017-09-14 22:21:00 +03:00
return json_decode($response->getBody())->data;
}
2017-10-11 16:01:23 +03:00
return [];
2017-09-14 22:21:00 +03:00
}
2018-10-03 12:10:59 +03:00
public function getVendors()
{
$response = $this->getRemote('apps/vendors');
if ($response && ($response->getStatusCode() == 200)) {
return json_decode($response->getBody())->data;
}
return [];
}
public function getModulesByVendor($alias, $data = [])
{
$response = $this->getRemote('apps/vendors/' . $alias, 'GET', $data);
if ($response && ($response->getStatusCode() == 200)) {
return json_decode($response->getBody())->data;
}
return [];
}
public function getMyModules($data = [])
{
$response = $this->getRemote('apps/my', 'GET', $data);
if ($response && ($response->getStatusCode() == 200)) {
return json_decode($response->getBody())->data;
}
return [];
}
public function getInstalledModules($data = [])
{
$company_id = session('company_id');
$cache = 'installed.' . $company_id . '.module';
$installed = Cache::get($cache);
if ($installed) {
return $installed;
}
$installed = [];
$modules = Module::all();
2018-12-12 20:28:16 +03:00
$installed_modules = Model::where('company_id', '=', session('company_id'))->pluck('status', 'alias')->toArray();
foreach ($modules as $module) {
if (!array_key_exists($module->alias, $installed_modules)) {
continue;
}
$result = $this->getModule($module->alias);
if ($result) {
$installed[] = $result;
}
}
Cache::put($cache, $installed, Date::now()->addHour(6));
return $installed;
}
public function getPaidModules($data = [])
2017-09-14 22:21:00 +03:00
{
$response = $this->getRemote('apps/paid', 'GET', $data);
2017-09-14 22:21:00 +03:00
2018-05-25 06:11:53 +03:00
if ($response && ($response->getStatusCode() == 200)) {
2017-09-14 22:21:00 +03:00
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 getNewModules($data = [])
2017-09-14 22:21:00 +03:00
{
$response = $this->getRemote('apps/new', 'GET', $data);
2017-09-14 22:21:00 +03:00
2018-05-25 06:11:53 +03:00
if ($response && ($response->getStatusCode() == 200)) {
2017-09-14 22:21:00 +03:00
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 getFreeModules($data = [])
2017-09-14 22:21:00 +03:00
{
$response = $this->getRemote('apps/free', 'GET', $data);
2017-09-14 22:21:00 +03:00
2018-05-25 06:11:53 +03:00
if ($response && ($response->getStatusCode() == 200)) {
2017-09-14 22:21:00 +03:00
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);
2018-05-25 06:11:53 +03:00
if ($response && ($response->getStatusCode() == 200)) {
2018-04-25 19:28:10 +03:00
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 getFeaturedModules($data = [])
{
$response = $this->getRemote('apps/featured', 'GET', $data);
if ($response && ($response->getStatusCode() == 200)) {
return json_decode($response->getBody())->data;
}
return [];
}
2017-09-14 22:21:00 +03:00
public function getCoreVersion()
{
$data['query'] = Info::all();
$response = $this->getRemote('core/version', 'GET', $data);
2018-05-25 06:11:53 +03:00
if ($response && ($response->getStatusCode() == 200)) {
2017-09-14 22:21:00 +03:00
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);
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 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' => url("apps/post/" . $module->alias),
2017-09-14 22:21:00 +03:00
'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
2019-01-14 15:41:51 +03:00
Artisan::call('cache:clear');
2017-09-14 22:21:00 +03:00
$module->delete();
2019-01-14 15:41:51 +03:00
// Cache Data clear
File::deleteDirectory(storage_path('framework/cache/data'));
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
}
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
}
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
}
2018-12-24 16:53:10 +03:00
public function moduleExists($alias)
{
$status = false;
if (Module::findByAlias($alias) instanceof \Nwidart\Modules\Module) {
$status = true;
}
return $status;
}
2018-05-25 16:57:58 +03:00
public function loadSuggestions()
{
// Get data from cache
$data = Cache::get('suggestions');
if (!empty($data)) {
return $data;
}
$data = [];
$url = 'apps/suggestions';
$response = $this->getRemote($url, 'GET', ['timeout' => 30, 'referer' => true]);
// Exception
if ($response instanceof RequestException) {
return false;
}
2018-05-25 16:57:58 +03:00
// Bad response
if (!$response || ($response->getStatusCode() != 200)) {
2018-05-25 16:57:58 +03:00
return false;
}
$suggestions = json_decode($response->getBody())->data;
foreach ($suggestions as $suggestion) {
$data[$suggestion->path] = $suggestion;
}
Cache::put('suggestions', $data, Date::now()->addHour(6));
return $data;
}
2018-11-16 18:33:39 +03:00
public function loadNotifications()
{
// Get data from cache
$data = Cache::get('notifications');
if (!empty($data)) {
return $data;
}
$data = [];
$url = 'apps/notifications';
$response = $this->getRemote($url, 'GET', ['timeout' => 30, 'referer' => true]);
// Exception
if ($response instanceof RequestException) {
return false;
}
// Bad response
if (!$response || ($response->getStatusCode() != 200)) {
return false;
}
$notifications = json_decode($response->getBody())->data;
foreach ($notifications as $notification) {
$data[$notification->path][] = $notification;
}
Cache::put('notifications', $data, Date::now()->addHour(6));
return $data;
}
2018-05-25 16:57:58 +03:00
public function getSuggestions($path)
{
// Get data from cache
$data = Cache::get('suggestions');
if (empty($data)) {
$data = $this->loadSuggestions();
}
2018-06-13 15:07:43 +03:00
if (!empty($data) && array_key_exists($path, $data)) {
2018-05-25 16:57:58 +03:00
return $data[$path];
}
return false;
}
2018-11-16 18:33:39 +03:00
public function getNotifications($path)
{
// Get data from cache
$data = Cache::get('notifications');
if (empty($data)) {
$data = $this->loadNotifications();
}
if (!empty($data) && array_key_exists($path, $data)) {
return $data[$path];
}
return false;
}
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',
'Referer' => env('APP_URL'),
2018-04-26 13:21:43 +03:00
'Akaunting' => version('short'),
2018-12-19 15:36:40 +03:00
'Language' => language()->getShortCode()
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);
2018-05-25 06:11:53 +03:00
try {
$result = $client->request($method, $path, $data);
} catch (RequestException $e) {
$result = false;
}
2017-09-14 22:21:00 +03:00
return $result;
}
}