PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / trunk
WP STAGING – WordPress Backup, Restore, Migration & Clone vtrunk
4.9.1 4.9.0 4.8.1 trunk 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.10.0 3.2.0 3.3.1 3.3.2 3.3.3 3.4.1 3.4.3 3.5.0 3.6.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 3.8.7 3.9.0 3.9.1 3.9.2 3.9.3 3.9.4 4.0.0 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.0 4.7.0 4.7.1 4.7.2 4.7.3 4.8.0
wp-staging / Framework / Filesystem / WpUploadsFolderSymlinker.php
wp-staging / Framework / Filesystem Last commit date
Filters 7 months ago Scanning 5 years ago AbstractFileObject.php 1 year ago AbstractFilesystemScanner.php 2 months ago DebugLogReader.php 2 years ago DirectoryListing.php 5 months ago DiskWriteCheck.php 5 months ago FileObject.php 1 year ago Filesystem.php 7 months ago FilesystemExceptions.php 5 years ago FilesystemScanner.php 1 week ago FilesystemScannerDto.php 1 week ago FilterableDirectoryIterator.php 1 year ago LegacyFileRulesTrait.php 1 week ago LogCleanup.php 5 months ago LogFiles.php 1 year ago MissingFileException.php 3 years ago OPcache.php 5 months ago PartIdentifier.php 8 months ago PathChecker.php 2 years ago PathIdentifier.php 7 months ago Permissions.php 1 week ago WpUploadsFolderSymlinker.php 1 week ago
WpUploadsFolderSymlinker.php
139 lines
1 <?php
2
3 namespace WPStaging\Framework\Filesystem;
4
5 use WPStaging\Backend\Modules\Jobs\Exceptions\FatalException;
6 use WPStaging\Framework\Utils\WpDefaultDirectories;
7
8 /*
9 * This is a service class to symlink the upload folder of production site
10 * to staging site
11 * Symlink will only work if staging site is on same hosting as production site
12 */
13 class WpUploadsFolderSymlinker
14 {
15 /**
16 * @var string
17 */
18 protected $stagingWpPath;
19
20 /**
21 * @var string
22 */
23 protected $stagingUploadPath;
24
25 /**
26 * @var WpDefaultDirectories
27 */
28 protected $wpDirectories;
29
30 /**
31 * @var string
32 */
33 protected $error;
34
35 public function __construct(WpDefaultDirectories $wpDirectories)
36 {
37 $this->wpDirectories = $wpDirectories;
38 }
39
40 /**
41 * @param string $stagingWpPath
42 * @return void
43 */
44 public function setStagingPath(string $stagingWpPath)
45 {
46 $this->stagingWpPath = trailingslashit($stagingWpPath);
47 $this->stagingUploadPath = rtrim($this->stagingWpPath . $this->wpDirectories->getRelativeUploadPath(), '/');
48 }
49
50 /**
51 * @param string $stagingUploadPath
52 * @return void
53 */
54 public function setStagingSiteUploadPath(string $stagingUploadPath)
55 {
56 $this->stagingUploadPath = rtrim($stagingUploadPath, '/');
57 }
58
59 /**
60 * @return bool
61 */
62 public function trySymlink()
63 {
64 if (is_link($this->stagingUploadPath)) {
65 $this->error = __("Link already exists", 'wp-staging');
66 return false;
67 }
68
69 if (file_exists($this->stagingUploadPath)) {
70 $this->error = __("Path exists at link path", 'wp-staging');
71 return false;
72 }
73
74 $uploadPath = rtrim($this->wpDirectories->getUploadsPath(), '/\\');
75
76 (new Filesystem())->mkdir(dirname($this->stagingUploadPath));
77
78 // try symlink with exec(ln) if exec is enabled and user is on windows
79 if ((stripos(PHP_OS, 'WIN') === 0) && $this->isExecEnabled()) {
80 return $this->linkWithExec($uploadPath, $this->stagingUploadPath);
81 }
82
83 return $this->link($uploadPath, $this->stagingUploadPath);
84 }
85
86 /**
87 * Return error
88 */
89 public function getError()
90 {
91 return $this->error;
92 }
93
94 /**
95 * Try symlinking with exec
96 *
97 * @param string $source
98 * @param string $destination
99 * @return boolean
100 */
101 private function linkWithExec($source, $destination)
102 {
103 try {
104 exec('mklink /D "' . $destination . '" "' . $source . '"');
105 return true;
106 } catch (FatalException $ex) {
107 $this->error = sprintf(__("Can not symlink %s. Error: ", 'wp-staging'), $destination, $ex->getMessage());
108 return false;
109 }
110 }
111
112 /**
113 * Try symlinking with php function
114 *
115 * @param string $source
116 * @param string $destination
117 * @return boolean
118 */
119 private function link($source, $destination)
120 {
121 try {
122 return symlink($source, $destination);
123 } catch (FatalException $ex) {
124 $this->error = sprintf(__("Can not symlink %s. Error: ", 'wp-staging'), $destination, $ex->getMessage());
125 return false;
126 }
127 }
128
129 private function isExecEnabled()
130 {
131 if (!function_exists('exec')) {
132 return false;
133 }
134
135 $disabled = explode(',', ini_get('disable_functions'));
136 return !in_array('exec', $disabled);
137 }
138 }
139