mitrobill/system/controllers/codecanyon.php

127 lines
5.0 KiB
PHP
Raw Normal View History

2024-01-18 15:41:24 +07:00
<?php
/**
* PHP Mikrotik Billing (https://github.com/hotspotbilling/phpnuxbill/)
* by https://t.me/ibnux
**/
_admin();
2024-01-18 17:24:21 +07:00
$ui->assign('_title', 'CodeCanyon.net');
2024-01-18 15:41:24 +07:00
$ui->assign('_system_menu', 'settings');
$plugin_repository = 'https://hotspotbilling.github.io/Plugin-Repository/repository.json';
$action = $routes['1'];
$admin = Admin::_info();
$ui->assign('_admin', $admin);
$cache = File::pathFixer('system/cache/codecanyon.json');
if ($admin['user_type'] != 'Admin') {
r2(U . "dashboard", 'e', $_L['Do_Not_Access']);
}
if (empty($config['envato_token'])) {
r2(U . 'settings/app', 'w', '<a href="' . U . 'settings/app#envato' . '">Envato Personal Access Token</a> is not set');
}
switch ($action) {
case 'install':
if (!is_writeable(File::pathFixer('system/cache/'))) {
r2(U . "codecanyon", 'e', 'Folder system/cache/ is not writable');
}
if (!is_writeable(File::pathFixer('system/plugin/'))) {
r2(U . "codecanyon", 'e', 'Folder system/plugin/ is not writable');
}
if (!is_writeable(File::pathFixer('system/paymentgateway/'))) {
r2(U . "codecanyon", 'e', 'Folder system/paymentgateway/ is not writable');
}
set_time_limit(-1);
$item_id = $routes['2'];
$tipe = $routes['3'];
$result = Http::getData('https://api.envato.com/v3/market/buyer/download?item_id=' . $item_id, ['Authorization: Bearer ' . $config['envato_token']]);
$json = json_decode($result, true);
if (!isset($json['download_url'])) {
r2(U . 'codecanyon', 'e', 'Failed to get download url. ' . $json['description']);
}
$file = File::pathFixer('system/cache/codecanyon/');
2024-01-18 17:24:21 +07:00
if (!file_exists($file)) {
2024-01-18 15:41:24 +07:00
mkdir($file);
}
$file .= $item_id . '.zip';
if (file_exists($file))
unlink($file);
2024-01-18 17:24:21 +07:00
//download
2024-01-18 15:41:24 +07:00
$fp = fopen($file, 'w+');
$ch = curl_init($json['download_url']);
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_exec($ch);
curl_close($ch);
fclose($fp);
2024-01-18 17:24:21 +07:00
//extract
$target = File::pathFixer('system/cache/codecanyon/' . $item_id . '/');
2024-01-18 15:41:24 +07:00
$zip = new ZipArchive();
$zip->open($file);
2024-01-18 17:24:21 +07:00
$zip->extractTo($target);
2024-01-18 15:41:24 +07:00
$zip->close();
2024-01-18 17:24:21 +07:00
//moving
if (file_exists($target . 'plugin')) {
2024-01-19 09:07:48 +07:00
File::copyFolder($target . 'plugin', File::pathFixer('system/plugin/'), ['license.txt', 'changelog.txt', 'install.txt']);
2024-01-18 17:24:21 +07:00
} else if (file_exists($target . 'paymentgateway')) {
2024-01-19 09:07:48 +07:00
File::copyFolder($target . 'paymentgateway', File::pathFixer('system/plugin/'), ['license.txt', 'changelog.txt', 'install.txt']);
} else if (file_exists($target . 'themes')) {
File::copyFolder($target . 'themes', File::pathFixer('ui/themes/'), ['license.txt', 'changelog.txt', 'install.txt']);
2024-01-18 17:24:21 +07:00
}
//Cleaning
File::deleteFolder($target);
unlink($file);
r2(U . "codecanyon", 's', 'Installation success');
2024-01-18 15:41:24 +07:00
case 'reload':
2024-01-18 17:24:21 +07:00
if (file_exists($cache))
unlink($cache);
2024-01-18 15:41:24 +07:00
default:
if (class_exists('ZipArchive')) {
$zipExt = true;
} else {
$zipExt = false;
}
$ui->assign('zipExt', $zipExt);
if (file_exists($cache) && time() - filemtime($cache) < (24 * 60 * 60)) {
$txt = file_get_contents($cache);
$plugins = json_decode($txt, true);
2024-01-18 17:24:21 +07:00
$ui->assign('chached_until', date($config['date_format'] . ' H:i', filemtime($cache) + (24 * 60 * 60)));
2024-01-18 15:41:24 +07:00
if (count($plugins) == 0) {
unlink($cache);
r2(U . 'codecanyon');
}
} else {
$plugins = [];
$page = _get('page', 1);
back:
$result = Http::getData('https://api.envato.com/v3/market/buyer/list-purchases?&page=' . $page, ['Authorization: Bearer ' . $config['envato_token']]);
$items = json_decode($result, true);
if ($items && count($items['results']) > 0) {
foreach ($items['results'] as $item) {
$name = strtolower($item['item']['name']);
2024-01-18 17:24:21 +07:00
if (strpos($name, 'phpnuxbill') !== false) {
$plugins[] = $item;
2024-01-18 15:41:24 +07:00
}
}
$page++;
goto back;
}
2024-01-18 17:24:21 +07:00
if (count($plugins) > 0) {
file_put_contents($cache, json_encode($plugins));
if (file_exists($cache)) {
$ui->assign('chached_until', date($config['date_format'] . ' H:i', filemtime($cache) + (24 * 60 * 60)));
}
2024-01-18 15:41:24 +07:00
}
}
$ui->assign('plugins', $plugins);
$ui->display('codecanyon.tpl');
}