123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- <?php
- namespace App\Controllers;
- use Slim\Http\Request;
- use Slim\Http\Response;
- use ZipArchive;
- class UpgradeController extends Controller
- {
- const GITHUB_SOURCE_API = 'https://api.github.com/repos/SergiX44/XBackBone/releases';
- /**
- * @param Request $request
- * @param Response $response
- * @return Response
- */
- public function upgrade(Request $request, Response $response): Response
- {
- if (!is_writable(BASE_DIR)) {
- $this->session->alert(lang('path_not_writable', BASE_DIR), 'warning');
- return redirect($response, 'system');
- }
- try {
- $json = $this->getApiJson();
- } catch (\RuntimeException $e) {
- $this->session->alert($e->getMessage(), 'danger');
- return redirect($response, 'system');
- }
- if (version_compare($json[0]->tag_name, PLATFORM_VERSION, '<=')) {
- $this->session->alert(lang('already_latest_version'), 'warning');
- return redirect($response, 'system');
- }
- $tmpFile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'xbackbone_update.zip';
- if (file_put_contents($tmpFile, file_get_contents($json[0]->assets[0]->browser_download_url)) === false) {
- $this->session->alert(lang('cannot_retrieve_file'), 'danger');
- return redirect($response, 'system');
- };
- if (filesize($tmpFile) !== $json[0]->assets[0]->size) {
- $this->session->alert(lang('file_size_no_match'), 'danger');
- return redirect($response, 'system');
- }
- $currentFiles = array_merge(
- glob_recursive(BASE_DIR . 'app/*'),
- glob_recursive(BASE_DIR . 'bin/*'),
- glob_recursive(BASE_DIR . 'bootstrap/*'),
- glob_recursive(BASE_DIR . 'resources/templates/*'),
- glob_recursive(BASE_DIR . 'resources/lang/*'),
- glob_recursive(BASE_DIR . 'resources/schemas/*'),
- glob_recursive(BASE_DIR . 'static/*')
- );
- removeDirectory(BASE_DIR . 'vendor/');
- $updateZip = new ZipArchive();
- $updateZip->open($tmpFile);
- for ($i = 0; $i < $updateZip->numFiles; $i++) {
- $nameIndex = $updateZip->getNameIndex($i);
- $updateZip->extractTo(BASE_DIR, $nameIndex);
- if (($key = array_search(rtrim(BASE_DIR . $nameIndex, '/'), $currentFiles)) !== false) {
- unset($currentFiles[$key]);
- }
- }
- foreach ($currentFiles as $extraneous) {
- unlink($extraneous);
- }
- $updateZip->close();
- unlink($tmpFile);
- return redirect($response, '/install');
- }
- /**
- * @param Request $request
- * @param Response $response
- * @return Response
- */
- public function checkForUpdates(Request $request, Response $response): Response
- {
- $jsonResponse = [
- 'status' => null,
- 'message' => null,
- 'upgrade' => false,
- ];
- try {
- $json = $this->getApiJson();
- $jsonResponse['status'] = 'OK';
- if (version_compare($json[0]->tag_name, PLATFORM_VERSION, '>')) {
- $jsonResponse['message'] = lang('new_version_available', $json[0]->tag_name);
- $jsonResponse['upgrade'] = true;
- } else {
- $jsonResponse['message'] = lang('already_latest_version');
- $jsonResponse['upgrade'] = false;
- }
- return $response->withJson($jsonResponse, 200);
- } catch (\RuntimeException $e) {
- $jsonResponse['status'] = 'ERROR';
- $jsonResponse['message'] = $e->getMessage();
- return $response->withJson($jsonResponse, 503);
- }
- }
- protected function getApiJson()
- {
- $opts = [
- 'http' => [
- 'method' => 'GET',
- 'header' => [
- 'User-Agent: XBackBone-App',
- 'Accept: application/vnd.github.v3+json',
- ],
- ],
- ];
- $data = file_get_contents(self::GITHUB_SOURCE_API, false, stream_context_create($opts));
- if ($data === false) {
- throw new \RuntimeException('Cannot contact the Github API. Try again.');
- }
- return json_decode($data);
- }
- }
|