PluginProbe ʕ •ᴥ•ʔ
Backup Migration / 2.1.2
Backup Migration v2.1.2
2.1.6 2.1.5.2 trunk 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.6.1 1.4.7 1.4.8 1.4.9 1.4.9.1 2.0.0 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.5.1
backup-backup / includes / check / class-domain-parser.php
backup-backup / includes / check Last commit date
checker.php 3 months ago class-domain-parser.php 3 months ago compatibility.php 3 months ago system_info.php 3 months ago
class-domain-parser.php
157 lines
1 <?php
2
3 namespace BMI\Plugin\Checker;
4
5 /**
6 * Class DomainParser
7 *
8 * Handles domain string parsing and comparison operations.
9 * Extracts domain from URLs and handles protocol/www prefix removal.
10 */
11 class DomainParser
12 {
13 const HTTPS_PREFIX = 'https://';
14 const HTTP_PREFIX = 'http://';
15 const WWW_PREFIX = 'www.';
16
17 /**
18 * Parses a domain string to remove protocol and optionally 'www.'.
19 *
20 * @param string $domain The domain/URL to parse.
21 * @param bool $removeWWW Whether to remove 'www.' prefix. Default true.
22 * @return string The parsed domain.
23 */
24 public function parse($domain, $removeWWW = true)
25 {
26 // Remove https://
27 if ($this->startsWith($domain, self::HTTPS_PREFIX)) {
28 $domain = substr($domain, strlen(self::HTTPS_PREFIX));
29 }
30
31 // Remove http://
32 if ($this->startsWith($domain, self::HTTP_PREFIX)) {
33 $domain = substr($domain, strlen(self::HTTP_PREFIX));
34 }
35
36 // Remove www. if requested
37 if ($removeWWW && $this->startsWith($domain, self::WWW_PREFIX)) {
38 $domain = substr($domain, strlen(self::WWW_PREFIX));
39 }
40
41 // Remove trailing slash
42 return rtrim($domain, '/');
43 }
44
45 /**
46 * Compares two domains for equality after parsing.
47 *
48 * @param string $domain1 First domain to compare.
49 * @param string $domain2 Second domain to compare.
50 * @param bool $removeWWW Whether to remove 'www.' before comparing.
51 * @return bool True if domains are equal, false otherwise.
52 */
53 public function areEqual($domain1, $domain2, $removeWWW = true)
54 {
55 $parsed1 = $this->parse($domain1, $removeWWW);
56 $parsed2 = $this->parse($domain2, $removeWWW);
57
58 return strcasecmp($parsed1, $parsed2) === 0;
59 }
60
61 /**
62 * Extracts the protocol from a URL.
63 *
64 * @param string $url The URL to extract protocol from.
65 * @return string The protocol ('https://' or 'http://'), or empty string if none found.
66 */
67 public function extractProtocol($url)
68 {
69 if ($this->startsWith($url, self::HTTPS_PREFIX)) {
70 return self::HTTPS_PREFIX;
71 }
72
73 if ($this->startsWith($url, self::HTTP_PREFIX)) {
74 return self::HTTP_PREFIX;
75 }
76
77 return '';
78 }
79
80 /**
81 * Checks if a domain has www prefix.
82 *
83 * @param string $domain The domain to check.
84 * @return bool True if has www prefix.
85 */
86 public function hasWwwPrefix($domain)
87 {
88 $withoutProtocol = $this->parse($domain, false);
89 return $this->startsWith($withoutProtocol, self::WWW_PREFIX);
90 }
91
92 /**
93 * Builds search and replace arrays for domain migration.
94 *
95 * @param string $sourceDomain The source domain (from backup).
96 * @param string $targetDomain The target domain (current site).
97 * @param string $sourceAbspath The source ABSPATH.
98 * @param string $targetAbspath The target ABSPATH.
99 * @param bool $useSSL Whether the target site uses SSL.
100 * @return array An array with 'search' and 'replace' keys containing the arrays.
101 */
102 public function buildSearchReplacePairs(
103 $sourceDomain,
104 $targetDomain,
105 $sourceAbspath,
106 $targetAbspath,
107 $useSSL
108 ) {
109 $parsedSource = $this->parse($sourceDomain);
110 $parsedTarget = $this->parse($targetDomain, false);
111 $ssl = $useSSL ? self::HTTPS_PREFIX : self::HTTP_PREFIX;
112
113 $searchArray = [
114 $sourceAbspath,
115 self::HTTPS_PREFIX . self::WWW_PREFIX . $parsedSource,
116 self::HTTP_PREFIX . self::WWW_PREFIX . $parsedSource,
117 self::HTTPS_PREFIX . $parsedSource,
118 self::HTTP_PREFIX . $parsedSource,
119 self::WWW_PREFIX . $parsedSource,
120 $parsedSource
121 ];
122
123 $replaceArray = [
124 $targetAbspath,
125 $ssl . self::WWW_PREFIX . $parsedTarget,
126 $ssl . self::WWW_PREFIX . $parsedTarget,
127 $ssl . $parsedTarget,
128 $ssl . $parsedTarget,
129 self::WWW_PREFIX . $parsedTarget,
130 $parsedTarget
131 ];
132
133 // Remove ABSPATH pair if they are the same
134 if ($sourceAbspath === $targetAbspath) {
135 array_shift($searchArray);
136 array_shift($replaceArray);
137 }
138
139 return [
140 'search' => $searchArray,
141 'replace' => $replaceArray
142 ];
143 }
144
145 /**
146 * Checks if string starts with a prefix.
147 *
148 * @param string $string The string to check.
149 * @param string $prefix The prefix to look for.
150 * @return bool True if string starts with prefix.
151 */
152 private function startsWith($string, $prefix)
153 {
154 return strncmp($string, $prefix, strlen($prefix)) === 0;
155 }
156 }
157