-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfunctions.php
149 lines (123 loc) · 4.74 KB
/
functions.php
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
<?php
use Composer\Semver\Comparator;
autoload_add_namespace(__DIR__ . '/src/', 'MicroweberPackages\\Modules\\StandaloneUpdater\\');
function mw_standalone_updater_get_latest_version()
{
return cache()->remember('standalone_updater_latest_version', 1440, function () {
$updateApi = 'http://updater.microweberapi.com/builds/master/version.txt';
$version = app()->url_manager->download($updateApi);
if ($version) {
$version = trim($version);
return $version;
}
});
}
function mw_standalone_updater_get_latest_composer_json()
{
return cache()->remember('standalone_updater_latest_version_composer_json', 1440, function () {
$updateApi = 'http://updater.microweberapi.com/builds/master/composer.json';
$json = app()->url_manager->download($updateApi);
if ($json) {
$json = @json_decode($json,true);
return $json;
}
});
}
function mw_standalone_updater_has_curl_errors()
{
$requestUrl = 'http://updater.microweberapi.com/builds/master/composer.json';
$ch = curl_init($requestUrl);
curl_setopt($ch, CURLOPT_COOKIEJAR, mw_cache_path() . 'global/cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, mw_cache_path() . 'global/cookie.txt');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Microweber ' . MW_VERSION . ';)');
curl_setopt($ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($ch, CURLOPT_DNS_USE_GLOBAL_CACHE, false );
curl_setopt($ch, CURLOPT_DNS_CACHE_TIMEOUT, 2 );
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 300);
curl_setopt($ch, CURLOPT_TIMEOUT, 400);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$result = curl_exec($ch);
curl_close($ch);
$curl_error = curl_error($ch);
if($curl_error){
return $curl_error;
}
}
function mw_standalone_updater_get_url_content($url)
{
// get the content with curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$content = curl_exec($ch);
if (curl_errno($ch)) {
return false;
}
curl_close($ch);
return $content;
}
function mw_standalone_updater_delete_recursive($dir)
{
if (!is_dir($dir)) {
return;
}
try {
$files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::CHILD_FIRST);
foreach ($files as $fileinfo) {
$todo = ($fileinfo->isDir() ? 'rmdir' : 'unlink');
@$todo($fileinfo->getRealPath());
}
} catch (\Exception $e) {
// Cant remove files from this path
}
@rmdir($dir);
}
function mw_standalone_updater_is_enabled()
{
if (mw()->ui->disable_marketplace != true) {
return false;
}
if (is_link(mw_root_path() . DS . 'src')) {
return false;
}
if (is_link(mw_root_path() . DS . 'vendor')) {
return false;
}
return true;
}
event_bind('mw.admin', function ($params = false) {
if (mw_standalone_updater_is_enabled()) {
// Show new update on dashboard
$lastUpdateCheckTime = get_option('last_update_check_time', 'standalone-updater');
if (!$lastUpdateCheckTime) {
$lastUpdateCheckTime = \Carbon\Carbon::now();
}
$showDashboardNotice = \Carbon\Carbon::now()->greaterThan(\Carbon\Carbon::parse($lastUpdateCheckTime));
if ($showDashboardNotice) {
$newVersionNumber = mw_standalone_updater_get_latest_version();
if (Comparator::equalTo($newVersionNumber, MW_VERSION)) {
save_option('last_update_check_time', \Carbon\Carbon::parse('+24 hours'), 'standalone-updater');
return;
}
$mustUpdate = false;
if (Comparator::greaterThan($newVersionNumber, MW_VERSION)) {
$mustUpdate = true;
}
if ($mustUpdate) {
event_bind('mw.admin.dashboard.start', function ($item) use ($newVersionNumber) {
echo '<div type="standalone-updater/dashboard_notice" new-version="' . $newVersionNumber . '" class="mw-lazy-load-module"></div>';
});
} else {
save_option('last_update_check_time', \Carbon\Carbon::parse('+24 hours'), 'standalone-updater');
return;
}
}
}
});