PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / 3.3.1
WP STAGING – WordPress Backup, Restore, Migration & Clone v3.3.1
4.9.2 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 2 years ago Scanning 5 years ago DebugLogReader.php 2 years ago DirectoryListing.php 3 years ago DiskWriteCheck.php 3 years ago FileObject.php 2 years ago Filesystem.php 2 years ago FilesystemExceptions.php 5 years ago FilterableDirectoryIterator.php 2 years ago LogCleanup.php 2 years ago LogFiles.php 2 years ago MissingFileException.php 3 years ago PathChecker.php 2 years ago PathIdentifier.php 2 years ago Permissions.php 5 years ago WpUploadsFolderSymlinker.php 5 years ago
WpUploadsFolderSymlinker.php
127 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 /**
36 * @param string $stagingWpPath
37 */
38 public function __construct($stagingWpPath)
39 {
40 $this->stagingWpPath = $stagingWpPath;
41 // todo inject using dependency injection if possible
42 $this->wpDirectories = new WpDefaultDirectories();
43 $this->stagingUploadPath = rtrim($this->stagingWpPath . $this->wpDirectories->getRelativeUploadPath(), '/');
44 }
45
46 /**
47 * @return bool
48 */
49 public function trySymlink()
50 {
51 if (is_link($this->stagingUploadPath)) {
52 $this->error = __("Link already exists", 'wp-staging');
53 return false;
54 }
55
56 if (file_exists($this->stagingUploadPath)) {
57 $this->error = __("Path exists at link path", 'wp-staging');
58 return false;
59 }
60
61 $uploadPath = rtrim($this->wpDirectories->getUploadsPath(), '/\\');
62
63 (new Filesystem())->mkdir(dirname($this->stagingUploadPath));
64
65 // try symlink with exec(ln) if exec is enabled and user is on windows
66 if ((stripos(PHP_OS, 'WIN') === 0) && $this->isExecEnabled()) {
67 return $this->linkWithExec($uploadPath, $this->stagingUploadPath);
68 }
69
70 return $this->link($uploadPath, $this->stagingUploadPath);
71 }
72
73 /**
74 * Return error
75 */
76 public function getError()
77 {
78 return $this->error;
79 }
80
81 /**
82 * Try symlinking with exec
83 *
84 * @param string $source
85 * @param string $destination
86 * @return boolean
87 */
88 private function linkWithExec($source, $destination)
89 {
90 try {
91 exec('mklink /D "' . $destination . '" "' . $source . '"');
92 return true;
93 } catch (FatalException $ex) {
94 $this->error = sprintf(__("Can not symlink %s. Error: ", 'wp-staging'), $destination, $ex->getMessage());
95 return false;
96 }
97 }
98
99 /**
100 * Try symlinking with php function
101 *
102 * @param string $source
103 * @param string $destination
104 * @return boolean
105 */
106 private function link($source, $destination)
107 {
108 try {
109 symlink($source, $destination);
110 return true;
111 } catch (FatalException $ex) {
112 $this->error = sprintf(__("Can not symlink %s. Error: ", 'wp-staging'), $destination, $ex->getMessage());
113 return false;
114 }
115 }
116
117 private function isExecEnabled()
118 {
119 if (!function_exists('exec')) {
120 return false;
121 }
122
123 $disabled = explode(',', ini_get('disable_functions'));
124 return !in_array('exec', $disabled);
125 }
126 }
127