19
19
20
20
namespace phpMyFAQ \Controller \Api ;
21
21
22
+ use DateTime ;
23
+ use DateTimeInterface ;
22
24
use OpenApi \Attributes as OA ;
23
25
use phpMyFAQ \Controller \AbstractController ;
24
26
use phpMyFAQ \Enums \PermissionType ;
27
+ use phpMyFAQ \Translation ;
25
28
use Symfony \Component \HttpFoundation \Response ;
26
29
use Symfony \Component \HttpKernel \Exception \UnauthorizedHttpException ;
30
+ use Symfony \Contracts \HttpClient \Exception \DecodingExceptionInterface ;
31
+ use Symfony \Contracts \HttpClient \Exception \TransportExceptionInterface ;
27
32
28
33
class UpdateController extends AbstractController
29
34
{
@@ -36,6 +41,9 @@ public function __construct()
36
41
}
37
42
}
38
43
44
+ /**
45
+ * @throws \Exception
46
+ */
39
47
#[OA \Post(path: '/api/v3.1/update ' , operationId: 'triggerUpdate ' , tags: ['Endpoints with Authentication ' ])]
40
48
#[OA \Header(
41
49
header: 'x-pmf-token ' ,
@@ -55,6 +63,44 @@ public function index(): Response
55
63
{
56
64
$ this ->userHasPermission (PermissionType::CONFIGURATION_EDIT );
57
65
66
+ $ upgrade = $ this ->container ->get ('phpmyfaq.setup.upgrade ' );
67
+ $ branch = $ this ->configuration ->get ('upgrade.releaseEnvironment ' );
68
+
69
+ // Check if the maintenance mode is enabled
70
+ if (!$ upgrade ->isMaintenanceEnabled ()) {
71
+ return $ this ->json (['error ' => Translation::get ('msgNotInMaintenanceMode ' )], Response::HTTP_CONFLICT );
72
+ }
73
+
74
+ // Fetch latest version
75
+ try {
76
+ $ versions = $ this ->container ->get ('phpmyfaq.admin.api ' )->getVersions ();
77
+
78
+ if (version_compare ($ versions ['installed ' ], $ versions [$ branch ], '< ' )) {
79
+ $ versionNumber = $ versions [$ branch ];
80
+ }
81
+ } catch (TransportExceptionInterface | DecodingExceptionInterface $ e ) {
82
+ return $ this ->json (['error ' => $ e ->getMessage ()], Response::HTTP_BAD_REQUEST );
83
+ }
84
+
85
+ // Fetch a package version
86
+ $ pathToPackage = $ upgrade ->downloadPackage ($ versionNumber );
87
+
88
+ if ($ pathToPackage === false ) {
89
+ return $ this ->json (['error ' => Translation::get ('downloadFailure ' )], Response::HTTP_BAD_GATEWAY );
90
+ }
91
+
92
+ if (!$ upgrade ->isNightly ()) {
93
+ $ result = $ upgrade ->verifyPackage ($ pathToPackage , $ versionNumber );
94
+ if ($ result === false ) {
95
+ return $ this ->json (['error ' => Translation::get ('verificationFailure ' )], Response::HTTP_BAD_GATEWAY );
96
+ }
97
+ }
98
+
99
+ $ this ->configuration ->set ('upgrade.lastDownloadedPackage ' , urlencode ($ pathToPackage ));
100
+
101
+ // Extract package
102
+ // WORK IN PROGESS
103
+
58
104
return $ this ->json ($ this ->configuration ->getVersion ());
59
105
}
60
106
}
0 commit comments