Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
Service.php
1<?php
2
4
7
14{
15 public const GROUP = 'webmaster';
16 public const TYPE_GOOGLE = 'google';
17 public const METHOD_PREFIX = 'webmaster';
18
24 public static function getTypes(): array
25 {
26 return [
27 static::TYPE_GOOGLE,
28 ];
29 }
30
34 public static function getMethodPrefix(): string
35 {
36 return self::METHOD_PREFIX;
37 }
38
44 public static function getSites(): array
45 {
46 $engine = new Engine\Google();
47 $engine->setService(static::getInstance());
48 $response = $engine->getSites();
49
50 if (!$response->isSuccess())
51 {
52 return ['error' => $response->getErrors()];
53 }
54
55 $result = [];
56 $sites = $response->getData();
57
58 $sites = $sites['siteEntry'] ?? [];
59 foreach ($sites as $siteInfo)
60 {
61 $siteUrlInfo = parse_url($siteInfo['siteUrl']);
62 if ($siteUrlInfo)
63 {
64 $errors = [];
65 $hostKey = \CBXPunycode::toASCII($siteUrlInfo["host"], $errors);
66 if (count($errors) > 0)
67 {
68 $hostKey = $siteUrlInfo["host"];
69 }
70
71 $result[$hostKey] = [
72 'binded' => $siteInfo["permissionLevel"] !== "siteRestrictedUser",
73 'verified' => (
74 $siteInfo["permissionLevel"] !== "siteRestrictedUser"
75 && $siteInfo["permissionLevel"] !== "siteUnverifiedUser"
76 ),
77 ];
78 }
79 }
80
81 return $result;
82 }
83
91 public static function addSite(string $domain, string $dir = '/'): array
92 {
93 $request = Application::getInstance()->getContext()->getRequest();
94 $protocol = $request->isHttps() ? "https://" : "http://";
95
96 $engine = new Engine\Google();
97 $engine->setService(static::getInstance());
98 $response = $engine->addSite($protocol . $domain . $dir);
99
100 if (!$response->isSuccess())
101 {
102 return ['error' => implode(',', $response->getErrorMessages())];
103 }
104
105 $result = $response->getData();
106 if ($result['errors'])
107 {
108 return ['error' => $result['errors']['message']];
109 }
110
111 return ['result' => true];
112 }
113
122 public static function getVerifyToken(string $domain, string $dir = '/'): array
123 {
124 $request = Application::getInstance()->getContext()->getRequest();
125 $protocol = $request->isHttps() ? "https://" : "http://";
126 $data = [
127 "site" => [
128 "identifier" => $protocol . $domain . $dir,
129 "type" => "SITE",
130 ],
131 "verificationMethod" => "FILE",
132 ];
133
134 $engine = new Engine\Google();
135 $engine->setService(static::getInstance());
136 $response = $engine->getVerifyToken($data);
137
138 if (!$response->isSuccess())
139 {
140 return ['error' => implode(',', $response->getErrorMessages())];
141 }
142
143 $result = $response->getData();
144 if (!$result || !$result["token"])
145 {
146 return ['error' => 'empty response'];
147 }
148 if ($result['errors'])
149 {
150 return ['error' => $result['errors']['message']];
151 }
152
153 return ['token' => $result["token"]];
154 }
155
164 public static function verifySite(string $domain, string $dir = '/'): array
165 {
166 $request = Application::getInstance()->getContext()->getRequest();
167 $protocol = $request->isHttps() ? "https://" : "http://";
168 $data = [
169 "site" => [
170 "identifier" => $protocol . $domain . $dir,
171 "type" => "SITE",
172 ],
173 ];
174
175 $engine = new Engine\Google();
176 $engine->setService(static::getInstance());
177 $response = $engine->verifySite($data);
178
179 if (!$response->isSuccess())
180 {
181 return ['error' => implode(',', $response->getErrorMessages())];
182 }
183
184 $result = $response->getData();
185 if (!$result || !$result["token"])
186 {
187 return ['error' => 'empty response'];
188 }
189 if ($result['errors'])
190 {
191 return ['error' => $result['errors']['message']];
192 }
193
194 return ['result' => true];
195 }
196
197 public static function canUseMultipleClients()
198 {
199 return false;
200 }
201}
202
static addSite(string $domain, string $dir='/')
Definition Service.php:91
static verifySite(string $domain, string $dir='/')
Definition Service.php:164
static getVerifyToken(string $domain, string $dir='/')
Definition Service.php:122