PluginProbe
JetBackup – Backup, Restore & Migrate / 1.4.6
JetBackup – Backup, Restore & Migrate v1.4.6
3.1.23.6 3.1.23.5 3.1.23.3 3.1.22.4 3.1.22.3 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.8.1 1.4.9 1.5.0 1.5.1 1.5.1.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 1.6.10 All 82 releases
backup / com / lib / BackupGuard / Helper.php
Helper.php
106 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace BackupGuard;
4
5 require_once(dirname(__FILE__).'/Exception/BadRequest.php');
6 require_once(dirname(__FILE__).'/Exception/Forbidden.php');
7 require_once(dirname(__FILE__).'/Exception/InternalServerError.php');
8 require_once(dirname(__FILE__).'/Exception/MethodNotAllowed.php');
9 require_once(dirname(__FILE__).'/Exception/NotFound.php');
10 require_once(dirname(__FILE__).'/Exception/Unauthorized.php');
11 require_once(dirname(__FILE__).'/Config.php');
12 require_once(dirname(__FILE__).'/RequestHandler.php');
13 require_once(dirname(__FILE__).'/Response.php');
14
15 require_once(SG_REQUEST_PATH.'SGRequest.php');
16 require_once(SG_REQUEST_PATH.'SGResponse.php');
17
18 class Helper
19 {
20 public static function requiredParam($name, $var)
21 {
22 if (empty($var)) {
23 throw new BadRequestException("Missing required argument: ".$name, 400);
24 }
25 }
26
27 public static function requiredParamInArray($arr, $key)
28 {
29 $var = null;
30
31 if (is_array($arr) && isset($arr[$key])) {
32 $var = $arr[$key];
33 }
34
35 self::requiredParam($key, $var);
36 }
37
38 private static function prepareRequest($path, $params = array(), $headers = array())
39 {
40 $url = Config::URL.$path;
41 $request = RequestHandler::createRequest($url);
42 $request->setParams($params);
43 $request->setHeaders($headers);
44 return $request;
45 }
46
47 public static function sendPostRequest($path, $params = array(), $headers = array())
48 {
49 // $request = self::prepareRequest($path, $params, $headers);
50 // return $request->post();
51 $url = Config::URL.$path;
52
53 $request = \SGRequest::getInstance();
54 $request->setUrl($url);
55 $request->setHeaders($headers);
56 $request->setParams($params);
57 $request->setGetWithQueryParams(false);
58 return $request->sendPostRequest();
59 }
60
61 public static function sendGetRequest($path, $params = array(), $headers = array())
62 {
63 // $request = self::prepareRequest($path, $params, $headers);
64 // return $request->get();
65 $url = Config::URL.$path;
66
67 $request = \SGRequest::getInstance();
68 $request->setUrl($url);
69 $request->setHeaders($headers);
70 $request->setParams($params);
71 $request->setGetWithQueryParams(false);
72 return $request->sendGetRequest();
73 }
74
75 public static function validateResponse($response)
76 {
77 if (!$response instanceof \SGResponse) {
78 throw new MethodNotAllowedException();
79 }
80
81 $code = (int)$response->getHttpStatus();
82 $error = $response->getBodyParam('error');
83 $errorMessage = $error&&isset($error['message'])?$error['message']:null;
84
85 //sometimes 'error' is just a string
86 if ($error && !$errorMessage) {
87 $errorMessage = $error;
88 }
89
90 switch ($code) {
91 case 405:
92 throw new MethodNotAllowedException($errorMessage, $code);
93 case 400:
94 throw new BadRequestException($errorMessage, $code);
95 case 404:
96 throw new NotFoundException($errorMessage, $code);
97 case 500:
98 throw new InternalServerErrorException($errorMessage, $code);
99 case 403:
100 throw new ForbiddenException($errorMessage, $code);
101 case 401:
102 throw new UnauthorizedException($errorMessage, $code);
103 }
104 }
105 }
106