PageRenderTime 20ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/bitrix/modules/bitrixcloud/classes/general/cdn_config.php

https://gitlab.com/Rad1calDreamer/honey
PHP | 495 lines | 275 code | 11 blank | 209 comment | 24 complexity | 2917aded1e6bc79477b0a2360f6913c2 MD5 | raw file
  1. <?php
  2. IncludeModuleLangFile(__FILE__);
  3. class CBitrixCloudCDNConfig
  4. {
  5. private static $instance = /*.(CBitrixCloudCDNConfig).*/ null;
  6. private $active = 0;
  7. private $expires = 0; //timestamp
  8. private $domain = "";
  9. private $https = /*.(bool).*/null;
  10. private $optimize = /*.(bool).*/null;
  11. private $kernel_rewrite = /*.(bool).*/null;
  12. private $content_rewrite = /*.(bool).*/null;
  13. private $sites = /*.(array[string]string).*/ array();
  14. /** @var CBitrixCloudCDNQuota $quota */
  15. private $quota = /*.(CBitrixCloudCDNQuota).*/ null;
  16. /** @var CBitrixCloudCDNClasses $classes */
  17. private $classes = /*.(CBitrixCloudCDNClasses).*/ null;
  18. /** @var CBitrixCloudCDNServerGroups $server_groups */
  19. private $server_groups = /*.(CBitrixCloudCDNServerGroups).*/ null;
  20. /** @var CBitrixCloudCDNLocations $locations */
  21. private $locations = /*.(CBitrixCloudCDNLocations).*/ null;
  22. private $debug = false;
  23. /**
  24. *
  25. *
  26. */
  27. private function __construct()
  28. {
  29. }
  30. /**
  31. * Returns proxy class instance (singleton pattern)
  32. *
  33. * @return CBitrixCloudCDNConfig
  34. *
  35. */
  36. public static function getInstance()
  37. {
  38. if (!isset(self::$instance))
  39. self::$instance = new CBitrixCloudCDNConfig;
  40. return self::$instance;
  41. }
  42. /**
  43. *
  44. * @return CBitrixCloudCDNConfig
  45. * @throws CBitrixCloudException
  46. */
  47. public function updateQuota()
  48. {
  49. $web_service = new CBitrixCloudCDNWebService($this->domain);
  50. $obXML = $web_service->actionQuota();
  51. $node = $obXML->SelectNodes("/control/quota");
  52. if (is_object($node))
  53. $this->quota = CBitrixCloudCDNQuota::fromXMLNode($node);
  54. else
  55. throw new CBitrixCloudException(GetMessage("BCL_CDN_CONFIG_XML_PARSE", array(
  56. "#CODE#" => "6",
  57. )));
  58. $this->quota->saveOption(CBitrixCloudOption::getOption("cdn_config_quota"));
  59. return $this;
  60. }
  61. /**
  62. * Loads and parses xml
  63. *
  64. * @param boolean $sendAdditionalInfo Flag to send https and optimize parameters.
  65. *
  66. * @return CBitrixCloudCDNConfig
  67. * @throws CBitrixCloudException
  68. */
  69. public function loadRemoteXML($sendAdditionalInfo = false)
  70. {
  71. //Get configuration from remote service
  72. $this->sites = CBitrixCloudOption::getOption("cdn_config_site")->getArrayValue();
  73. $this->domain = CBitrixCloudOption::getOption("cdn_config_domain")->getStringValue();
  74. if ($sendAdditionalInfo)
  75. {
  76. $this->https = (CBitrixCloudOption::getOption("cdn_config_https")->getStringValue() === "true");
  77. $this->optimize = (CBitrixCloudOption::getOption("cdn_config_optimize")->getStringValue() === "true");
  78. }
  79. $web_service = new CBitrixCloudCDNWebService($this->domain, $this->optimize, $this->https);
  80. $web_service->setDebug($this->debug);
  81. $obXML = $web_service->actionGetConfig();
  82. //
  83. // Parse it
  84. //
  85. $node = $obXML->SelectNodes("/control");
  86. if (is_object($node))
  87. {
  88. $this->active = intval($node->getAttribute("active"));
  89. $this->expires = strtotime($node->getAttribute("expires"));
  90. }
  91. else
  92. {
  93. $this->active = 0;
  94. $this->expires = 0;
  95. }
  96. $node = $obXML->SelectNodes("/control/quota");
  97. if (is_object($node))
  98. $this->quota = CBitrixCloudCDNQuota::fromXMLNode($node);
  99. else
  100. throw new CBitrixCloudException(GetMessage("BCL_CDN_CONFIG_XML_PARSE", array(
  101. "#CODE#" => "2",
  102. )));
  103. $node = $obXML->SelectNodes("/control/classes");
  104. if (is_object($node))
  105. $this->classes = CBitrixCloudCDNClasses::fromXMLNode($node);
  106. else
  107. throw new CBitrixCloudException(GetMessage("BCL_CDN_CONFIG_XML_PARSE", array(
  108. "#CODE#" => "3",
  109. )));
  110. $node = $obXML->SelectNodes("/control/servergroups");
  111. if (is_object($node))
  112. $this->server_groups = CBitrixCloudCDNServerGroups::fromXMLNode($node);
  113. else
  114. throw new CBitrixCloudException(GetMessage("BCL_CDN_CONFIG_XML_PARSE", array(
  115. "#CODE#" => "4",
  116. )));
  117. $node = $obXML->SelectNodes("/control/locations");
  118. if (is_object($node))
  119. $this->locations = CBitrixCloudCDNLocations::fromXMLNode($node, $this);
  120. else
  121. throw new CBitrixCloudException(GetMessage("BCL_CDN_CONFIG_XML_PARSE", array(
  122. "#CODE#" => "5",
  123. )));
  124. return $this;
  125. }
  126. /**
  127. * Checks if it is active in webservice
  128. *
  129. * @return bool
  130. *
  131. */
  132. public function isActive()
  133. {
  134. return ($this->active > 0);
  135. }
  136. /**
  137. * Checks if it is time to update policy
  138. *
  139. * @return bool
  140. *
  141. */
  142. public function isExpired()
  143. {
  144. return ($this->expires < time());
  145. }
  146. /**
  147. * Sets the time to update policy
  148. *
  149. * @param int $time
  150. * @return void
  151. *
  152. */
  153. public function setExpired($time)
  154. {
  155. $this->expires = $time;
  156. CBitrixCloudOption::getOption("cdn_config_expire_time")->setStringValue((string)$this->expires);
  157. }
  158. /**
  159. * Returns resources domain name
  160. *
  161. * @return string
  162. *
  163. */
  164. public function getDomain()
  165. {
  166. return $this->domain;
  167. }
  168. /**
  169. * Sets resources domain name
  170. *
  171. * @param string $domain
  172. * @return CBitrixCloudCDNConfig
  173. *
  174. */
  175. public function setDomain($domain)
  176. {
  177. $this->domain = $domain;
  178. return $this;
  179. }
  180. /**
  181. * Returns flag of the kernel links (/bitrix/ or other) rewrite
  182. *
  183. * @param string $prefix
  184. * @return bool
  185. *
  186. */
  187. public function isKernelPrefix($prefix)
  188. {
  189. return preg_match("#^/bitrix/#", $prefix) > 0;
  190. }
  191. /**
  192. * Returns true if site operates by https.
  193. *
  194. * @return bool
  195. *
  196. */
  197. public function isHttpsEnabled()
  198. {
  199. //It is true by default
  200. if(!isset($this->https))
  201. $this->https = (CBitrixCloudOption::getOption("cdn_config_https")->getStringValue() === "true");
  202. return $this->https;
  203. }
  204. /**
  205. * Sets flag of the https.
  206. *
  207. * @param bool $https
  208. * @return CBitrixCloudCDNConfig
  209. *
  210. */
  211. public function setHttps($https = true)
  212. {
  213. $this->https = ($https != false);
  214. return $this;
  215. }
  216. /**
  217. * Returns true if resources optimization is enabled.
  218. *
  219. * @return bool
  220. *
  221. */
  222. public function isOptimizationEnabled()
  223. {
  224. //It is true by default
  225. if(!isset($this->optimize))
  226. $this->optimize = (CBitrixCloudOption::getOption("cdn_config_optimize")->getStringValue() === "true");
  227. return $this->optimize;
  228. }
  229. /**
  230. * Sets flag of the optimization.
  231. *
  232. * @param bool $optimize
  233. * @return CBitrixCloudCDNConfig
  234. *
  235. */
  236. public function setOptimization($optimize = true)
  237. {
  238. $this->optimize = ($optimize != false);
  239. return $this;
  240. }
  241. /**
  242. * Returns flag of the kernel links (/bitrix/ or other) rewrite
  243. *
  244. * @return bool
  245. *
  246. */
  247. public function isKernelRewriteEnabled()
  248. {
  249. //It is true by default
  250. if(!isset($this->kernel_rewrite))
  251. $this->kernel_rewrite = (CBitrixCloudOption::getOption("cdn_config_kernel_rewrite")->getStringValue() !== "false");
  252. return $this->kernel_rewrite;
  253. }
  254. /**
  255. * Sets flag of the kernel links (/bitrix/ or other) rewrite
  256. *
  257. * @param bool $rewrite
  258. * @return CBitrixCloudCDNConfig
  259. *
  260. */
  261. public function setKernelRewrite($rewrite = true)
  262. {
  263. $this->kernel_rewrite = ($rewrite != false);
  264. return $this;
  265. }
  266. /**
  267. * Returns flag of the content links (not kernel) rewrite
  268. *
  269. * @return bool
  270. *
  271. */
  272. public function isContentRewriteEnabled()
  273. {
  274. //It is false by default
  275. if(!isset($this->content_rewrite))
  276. $this->content_rewrite = (CBitrixCloudOption::getOption("cdn_config_content_rewrite")->getStringValue() !== "false");
  277. return $this->content_rewrite;
  278. }
  279. /**
  280. * Sets flag of the content links (not kernel) rewrite
  281. *
  282. * @param bool $rewrite
  283. * @return CBitrixCloudCDNConfig
  284. *
  285. */
  286. public function setContentRewrite($rewrite = true)
  287. {
  288. $this->content_rewrite = ($rewrite == true);
  289. return $this;
  290. }
  291. /**
  292. * Returns array of sites
  293. *
  294. * @return array[string]string
  295. *
  296. */
  297. public function getSites()
  298. {
  299. return $this->sites;
  300. }
  301. /**
  302. * Sets array of sites to enable CDN
  303. *
  304. * @param array[string]string $sites
  305. * @return CBitrixCloudCDNConfig
  306. *
  307. */
  308. public function setSites($sites)
  309. {
  310. $this->sites = /*.(array[string]string).*/ array();
  311. if (is_array($sites))
  312. {
  313. foreach ($sites as $site_id)
  314. $this->sites[$site_id] = $site_id;
  315. }
  316. return $this;
  317. }
  318. /**
  319. * Returns quota object
  320. *
  321. * @return CBitrixCloudCDNQuota
  322. *
  323. */
  324. public function getQuota()
  325. {
  326. return $this->quota;
  327. }
  328. /**
  329. * Returns file class object by it's name
  330. *
  331. * @param string $class_name
  332. * @return CBitrixCloudCDNClass
  333. *
  334. */
  335. public function getClassByName($class_name)
  336. {
  337. return $this->classes->getClass($class_name);
  338. }
  339. /**
  340. * Returns server group object by it's name
  341. *
  342. * @param string $server_group_name
  343. * @return CBitrixCloudCDNServerGroup
  344. *
  345. *
  346. */
  347. public function getServerGroupByName($server_group_name)
  348. {
  349. return $this->server_groups->getGroup($server_group_name);
  350. }
  351. /**
  352. * Returns configured locations
  353. *
  354. * @return CBitrixCloudCDNLocations
  355. *
  356. */
  357. public function getLocations()
  358. {
  359. return $this->locations;
  360. }
  361. /**
  362. * Returns unique array of all prefixes across all locations
  363. *
  364. * @param bool $bKernel
  365. * @param bool $bContent
  366. * @return array[int]string
  367. *
  368. */
  369. public function getLocationsPrefixes($bKernel = true, $bContent = false)
  370. {
  371. $arPrefixes = /*.(array[int]string).*/array();
  372. /* @var CBitrixCloudCDNLocation $location */
  373. $location = /*.(CBitrixCloudCDNLocation).*/ null;
  374. foreach ($this->locations as $location)
  375. {
  376. $arPrefixes = array_merge($arPrefixes, $location->getPrefixes());
  377. }
  378. foreach ($arPrefixes as $i => $prefix)
  379. {
  380. if ($this->isKernelPrefix($prefix))
  381. {
  382. if (!$bKernel)
  383. unset($arPrefixes[$i]);
  384. }
  385. else
  386. {
  387. if (!$bContent)
  388. unset($arPrefixes[$i]);
  389. }
  390. }
  391. return array_unique($arPrefixes);
  392. }
  393. /**
  394. * Returns unique array of all extensions across all locations
  395. *
  396. * @return array[int]string
  397. *
  398. */
  399. public function getLocationsExtensions()
  400. {
  401. $arExtensions = array();
  402. /* @var CBitrixCloudCDNLocation $location */
  403. $location = /*.(CBitrixCloudCDNLocation).*/ null;
  404. foreach ($this->locations as $location)
  405. {
  406. foreach ($location->getClasses() as $file_class)
  407. {
  408. /* @var CBitrixCloudCDNClass $file_class */
  409. $arExtensions = array_merge($arExtensions, $file_class->getExtensions());
  410. }
  411. }
  412. return array_unique($arExtensions);
  413. }
  414. /**
  415. * Saves configuration into CBitrixCloudOption
  416. *
  417. * @return CBitrixCloudCDNConfig
  418. *
  419. */
  420. public function saveToOptions()
  421. {
  422. CBitrixCloudOption::getOption("cdn_config_active")->setStringValue((string)$this->active);
  423. CBitrixCloudOption::getOption("cdn_config_expire_time")->setStringValue((string)$this->expires);
  424. CBitrixCloudOption::getOption("cdn_config_domain")->setStringValue($this->domain);
  425. CBitrixCloudOption::getOption("cdn_config_site")->setArrayValue($this->sites);
  426. if ($this->https !== null)
  427. CBitrixCloudOption::getOption("cdn_config_https")->setStringValue($this->https? "true": "false");
  428. if ($this->optimize !== null)
  429. CBitrixCloudOption::getOption("cdn_config_optimize")->setStringValue($this->optimize? "true": "false");
  430. if ($this->content_rewrite !== null)
  431. CBitrixCloudOption::getOption("cdn_config_content_rewrite")->setStringValue($this->content_rewrite? "true": "false");
  432. if ($this->kernel_rewrite !== null)
  433. CBitrixCloudOption::getOption("cdn_config_kernel_rewrite")->setStringValue($this->kernel_rewrite? "true": "false");
  434. $this->quota->saveOption(CBitrixCloudOption::getOption("cdn_config_quota"));
  435. $this->classes->saveOption(CBitrixCloudOption::getOption("cdn_class"));
  436. $this->server_groups->saveOption(CBitrixCloudOption::getOption("cdn_server_group"));
  437. $this->locations->saveOption(CBitrixCloudOption::getOption("cdn_location"));
  438. return $this;
  439. }
  440. /**
  441. * Loads configuration from CBitrixCloudOption
  442. *
  443. * @return CBitrixCloudCDNConfig
  444. *
  445. */
  446. public function loadFromOptions()
  447. {
  448. $this->active = intval(CBitrixCloudOption::getOption("cdn_config_active")->getStringValue());
  449. $this->expires = intval(CBitrixCloudOption::getOption("cdn_config_expire_time")->getStringValue());
  450. $this->domain = CBitrixCloudOption::getOption("cdn_config_domain")->getStringValue();
  451. $this->sites = CBitrixCloudOption::getOption("cdn_config_site")->getArrayValue();
  452. $this->quota = CBitrixCloudCDNQuota::fromOption(CBitrixCloudOption::getOption("cdn_config_quota"));
  453. $this->classes = CBitrixCloudCDNClasses::fromOption(CBitrixCloudOption::getOption("cdn_class"));
  454. $this->server_groups = CBitrixCloudCDNServerGroups::fromOption(CBitrixCloudOption::getOption("cdn_server_group"));
  455. $this->locations = CBitrixCloudCDNLocations::fromOption(CBitrixCloudOption::getOption("cdn_location"), $this);
  456. return $this;
  457. }
  458. /**
  459. * @return bool
  460. *
  461. */
  462. public function lock()
  463. {
  464. return CBitrixCloudOption::lock();
  465. }
  466. /**
  467. * @return void
  468. *
  469. */
  470. public function unlock()
  471. {
  472. CBitrixCloudOption::unlock();
  473. }
  474. /**
  475. *
  476. * @param bool $bActive
  477. * @return void
  478. *
  479. */
  480. public function setDebug($bActive)
  481. {
  482. $this->debug = $bActive === true;
  483. }
  484. }