Passed
Push — master ( 221005...26261c )
by Rubén
03:12
created

StatusController::initialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * sysPass
4
 *
5
 * @author    nuxsmin
6
 * @link      https://syspass.org
7
 * @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org
8
 *
9
 * This file is part of sysPass.
10
 *
11
 * sysPass is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation, either version 3 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * sysPass is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 *  along with sysPass.  If not, see <http://www.gnu.org/licenses/>.
23
 */
24
25
namespace SP\Modules\Web\Controllers;
26
27
use GuzzleHttp\Client;
28
use GuzzleHttp\Exception\GuzzleException;
29
use SP\Core\AppInfoInterface;
30
use SP\Core\Exceptions\CheckException;
31
use SP\Http\JsonResponse;
32
use SP\Modules\Web\Controllers\Traits\JsonTrait;
33
use SP\Util\VersionUtil;
34
35
/**
36
 * Class StatusController
37
 *
38
 * @package SP\Modules\Web\Controllers
39
 */
40
final class StatusController extends SimpleControllerBase
41
{
42
    use JsonTrait;
43
44
    /**
45
     * checkReleaseAction
46
     *
47
     * @return bool
48
     * @throws \DI\DependencyException
49
     * @throws \DI\NotFoundException
50
     */
51
    public function checkReleaseAction()
52
    {
53
        try {
54
            $this->extensionChecker->checkCurlAvailable(true);
55
56
            $request = $this->dic->get(Client::class)
57
                ->request('GET', AppInfoInterface::APP_UPDATES_URL);
58
59
            if ($request->getStatusCode() === 200
60
                && strpos($request->getHeaderLine('content-type'), 'application/json') !== false
61
            ) {
62
                $requestData = json_decode($request->getBody());
63
64
                if ($requestData !== null && !isset($requestData->message)) {
65
                    // $updateInfo[0]->tag_name
66
                    // $updateInfo[0]->name
67
                    // $updateInfo[0]->body
68
                    // $updateInfo[0]->tarball_url
69
                    // $updateInfo[0]->zipball_url
70
                    // $updateInfo[0]->published_at
71
                    // $updateInfo[0]->html_url
72
73
                    if (preg_match('/v?(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)\.(?P<build>\d+)(?P<pre_release>\-[a-z0-9\.]+)?$/', $requestData->tag_name, $matches)) {
74
                        $pubVersion = $matches['major'] . $matches['minor'] . $matches['patch'] . '.' . $matches['build'];
75
76
                        if (VersionUtil::checkVersion(VersionUtil::getVersionStringNormalized(), $pubVersion)) {
77
                            return $this->returnJsonResponseData([
78
                                'version' => $requestData->tag_name,
79
                                'url' => $requestData->html_url,
80
                                'title' => $requestData->name,
81
                                'description' => $requestData->body,
82
                                'date' => $requestData->published_at
83
                            ]);
84
                        }
85
86
                        return $this->returnJsonResponseData([]);
87
                    }
88
                }
89
90
                logger($requestData->message);
91
            }
92
93
            return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('Version unavailable'));
94
        } catch (GuzzleException $e) {
95
            processException($e);
96
97
            return $this->returnJsonResponseException($e);
98
        } catch (CheckException $e) {
99
            return $this->returnJsonResponseException($e);
100
        }
101
    }
102
103
    /**
104
     * checkNoticesAction
105
     *
106
     * @return bool
107
     * @throws \DI\DependencyException
108
     * @throws \DI\NotFoundException
109
     */
110
    public function checkNoticesAction()
111
    {
112
        try {
113
            $this->extensionChecker->checkCurlAvailable(true);
114
115
            $request = $this->dic->get(Client::class)
116
                ->request('GET', AppInfoInterface::APP_NOTICES_URL);
117
118
            if ($request->getStatusCode() === 200
119
                && strpos($request->getHeaderLine('content-type'), 'application/json') !== false
120
            ) {
121
                $requestData = json_decode($request->getBody());
122
123
                if ($requestData !== null && !isset($requestData->message)) {
124
                    $notices = [];
125
126
                    foreach ($requestData as $notice) {
127
                        $notices[] = [
128
                            'title' => $notice->title,
129
                            'date' => $notice->created_at,
130
                            'text' => $notice->body
131
                        ];
132
                    }
133
134
                    return $this->returnJsonResponseData($notices);
135
                }
136
137
                logger($requestData->message);
138
            }
139
140
            return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('Notifications not available'));
141
        } catch (GuzzleException $e) {
142
            processException($e);
143
144
            return $this->returnJsonResponseException($e);
145
        } catch (CheckException $e) {
146
            return $this->returnJsonResponseException($e);
147
        }
148
    }
149
150
    /**
151
     * @return void
152
     */
153
    protected function initialize()
154
    {
155
        // TODO: Implement initialize() method.
156
    }
157
}