1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168 | <?php
echo download_wordpress_zip();
// echo download_wordpress_zip('tr_TR');
function download_wordpress_zip( $language = false ){
/**
* Initialize wordpress for installation with one click.
*
* @param string $language ( Optional ) Wordpress setup language.
* false : (Default) English - en_US
* tr_TR : Turkish
* pt_PT : Portuguese
* ja : Japanese
* … See full list of languages:
* https://make.wordpress.org/polyglots/teams/
*
* @return string Returns process messages.
*
* usage
* download_wordpress_zip();
* download_wordpress_zip('tr_TR');
*
* https://atakanau.blogspot.com
*/
$logs = '';
// Generate download link
// https://wordpress.org/latest.zip
// https://tr.wordpress.org/latest-tr_TR.zip
$sub_dir = $language ? explode('_',$language)[0].'.' : '';
$url = 'https://'.$sub_dir.'wordpress.org/';
$file_name = 'latest' . ( $language ? '-'.$language : '' ) . '.zip';
$file_name_local = 'wordpress' . ( $language ? '-'.$language : '' ) . '.zip';
$logs .= "<pre>Download started: $url.$file_name </pre>";
// Download zip file from wordpress.org to our server
file_put_contents($file_name_local, fopen($url.$file_name, 'r'));
$fileSizeMB = number_format((filesize($file_name_local) / 1024 / 1024), 2);
$logs .= "<pre>Zip file saved as $file_name_local ($fileSizeMB MB) </pre>";
// Unzip downloaded file
if( class_exists('ZipArchive') ){
$path = pathinfo(realpath($file_name_local), PATHINFO_DIRNAME);
$zip = new ZipArchive;
$res = $zip->open($file_name_local);
if ($res === TRUE) {
// extract it to the path we determined above
$zip->extractTo($path);
$zip->close();
$logs .= "<pre>$file_name_local extracted to $path</pre>";
} else {
$logs .= "<pre>Error, file can not open: $file_name_local</pre>";
}
}elseif( function_exists('unzip') ){
$dir = getcwd();
unzip($dir,$file_name_local);
$logs .= "<pre>$file_name_local extracted to $path</pre>";
}else{
$logs .= "<pre>Error, file can not unzip: $file_name_local</pre>";
}
// Move all files and directories from extracted folder to current directory
$sourceDir = __DIR__ . DIRECTORY_SEPARATOR . 'wordpress';
$targetDir = __DIR__ . DIRECTORY_SEPARATOR . '.';
rmove($sourceDir , $targetDir);
return $logs;
}
/**
* A Recursive directory move that allows exclusions. The excluded items in the src will be deleted
* rather than moved.
* https://gist.github.com/bubba-h57/5117694
*
* @param string $sourceDir The fully qualified source directory to copy
* @param string $targetDir The fully qualified destination directory to copy to
* @param array $exclusions An array of preg_match patterns to ignore in the copy process
* @throws InvalidArgumentException
* @throws ErrorException
* @return boolean Returns TRUE on success, throws an error otherwise.
*/
function rmove($src, $dest, $exclusions = array()){
// If source is not a directory stop processing
if(!is_dir($src)) throw new InvalidArgumentException('The source passed in does not appear to be a valid directory: ['.$src.']', 1);
// If the destination directory does not exist create it
if(!is_dir($dest)) {
if(!mkdir($dest, 0, true)){
throw new InvalidArgumentException('The destination does not exist, and I can not create it: ['.$dest.']', 2);
}
}
// Ensure enclusions parameter is an array.
if (! is_array($exclusions)) throw new InvalidArgumentException('The exclustion parameter is not an array, it MUST be an array.', 3);
$emptiedDirs = array();
// Open the source directory to read in files
foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($src, FilesystemIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST) as $f) {
// Check to see if we should ignore this file or directory
foreach ($exclusions as $pattern){
if (preg_match($pattern, $f->getRealPath())){
if ($f->isFile()){
if (! unlink($f->getRealPath())) throw new ErrorException("Failed to delete file [{$f->getRealPath()}] ", 4);
}elseif($f->isDir()){
// we will attempt deleting these after we have moved all the files.
array_push($emptiedDirs, $f->getRealPath());
}
// Because we have to jump up two foreach levels
continue 2;
}
}
// We need to get a path relative to where we are copying from
$relativePath = str_replace($src, '', $f->getRealPath());
// And we can create a destination now.
$destination = $dest . $relativePath;
// if it is a file, lets just move that sucker over
if($f->isFile()) {
$path_parts = pathinfo($destination);
// If we don't have a directory for this yet
if (! is_dir($path_parts['dirname'])){
// Lets create one!
if (! mkdir($path_parts['dirname'], 0, true)) throw new ErrorException("Failed to create the destination directory: [{$path_parts['dirname']}]", 5);
}
if (! rename($f->getRealPath(), $destination)) throw new ErrorException("Failed to rename file [{$f->getRealPath()}] to [$destination]", 6);
// if it is a directory, lets handle it
}elseif($f->isDir()){
// Check to see if the destination directory already exists
if (! is_dir($destination)){
if (! mkdir($destination, 0, true)) throw new ErrorException("Failed to create the destination directory: [$destination]", 7);
}
// we will attempt deleting these after we have moved all the files.
array_push($emptiedDirs, $f->getRealPath());
// if it is something else, throw a fit. Symlinks can potentially end up here. I haven't tested them yet, but I think isFile() will typically
// just pick them up and work
}else{
throw new ErrorException("I found [{$f->getRealPath()}] yet it appears to be neither a directory nor a file. [{$f->isDot()}] I don't know what to do with that!", 8);
}
}
foreach ($emptiedDirs as $emptyDir){
// print "Deleting $emptyDir\n";
if (realpath($emptyDir) == realpath($src)){
continue;
}
if (!is_readable($emptyDir)) throw new ErrorException("The source directory: [$emptyDir] is not Readable", 9);
// Delete the old directory
if (! rmdir($emptyDir)){
// The directory is empty, we should have successfully deleted it.
if ((count(scandir($emptyDir)) == 2)){
throw new ErrorException("Failed to delete the source directory: [$emptyDir]", 10);
}
}
}
// Finally, delete the base of the source directory we just recursed through
if (! rmdir($src)) throw new ErrorException("Failed to delete the base source directory: [$src]", 11);
return true;
}
|