1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* BoxBilling |
4
|
|
|
* |
5
|
|
|
* @copyright BoxBilling, Inc (http://www.boxbilling.com) |
6
|
|
|
* @license Apache-2.0 |
7
|
|
|
* |
8
|
|
|
* Copyright BoxBilling, Inc |
9
|
|
|
* This source file is subject to the Apache-2.0 License that is bundled |
10
|
|
|
* with this source code in the file LICENSE |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
class Box_Update |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var \Box_Di |
17
|
|
|
*/ |
18
|
|
|
protected $di = null; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param \Box_Di $di |
22
|
|
|
*/ |
23
|
1 |
|
public function setDi($di) |
24
|
|
|
{ |
25
|
1 |
|
$this->di = $di; |
26
|
1 |
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @return \Box_Di |
30
|
|
|
*/ |
31
|
|
|
public function getDi() |
32
|
|
|
{ |
33
|
|
|
return $this->di; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
private $_url = 'http://api.boxbilling.com/compare-version.php'; |
37
|
|
|
|
38
|
4 |
|
public function __construct() |
39
|
|
|
{ |
40
|
4 |
|
if(defined('BB_VERSION_URL') && BB_VERSION_URL) { |
41
|
|
|
$this->_url = BB_VERSION_URL; |
42
|
|
|
} |
43
|
4 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Returns latest information |
47
|
|
|
*/ |
48
|
|
|
private function _getLatestVersionInfo() |
49
|
|
|
{ |
50
|
|
|
return $this->di['tools']->cache_function(array($this, 'getJson'), array(), 86400); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Return latest version number |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
|
|
public function getLatestVersion() |
58
|
|
|
{ |
59
|
|
|
$response = $this->_getLatestVersionInfo(); |
60
|
|
|
if(!is_object($response)) { |
61
|
|
|
return Box_Version::VERSION; |
62
|
|
|
} |
63
|
|
|
return $response->version; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Latest version link |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
|
|
public function getLatestVersionDownloadLink() |
71
|
|
|
{ |
72
|
|
|
$response = $this->_getLatestVersionInfo(); |
73
|
|
|
if(isset($response->update)) { |
74
|
|
|
return $response->update; |
75
|
|
|
} |
76
|
|
|
return $response->link; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Check if we need to update current BoxBilling version |
81
|
|
|
* @return bool |
82
|
|
|
*/ |
83
|
|
|
public function getCanUpdate() |
84
|
|
|
{ |
85
|
|
|
$version = $this->getLatestVersion(); |
86
|
|
|
$result = Box_Version::compareVersion($version); |
87
|
|
|
return ($result > 0); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Check if given file is same as original |
92
|
|
|
* @param string $file - filepath |
93
|
|
|
* @return bool |
94
|
|
|
*/ |
95
|
|
|
private function isHashValid($file) |
|
|
|
|
96
|
|
|
{ |
97
|
|
|
if(!file_exists($file)) { |
98
|
|
|
return false; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$response = $this->_getLatestVersionInfo(); |
102
|
|
|
$hash = md5($response->version.filesize($file)); |
103
|
|
|
return ($hash == $response->hash); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function getJson() |
107
|
|
|
{ |
108
|
|
|
$url = $this->_url . '?current=' . Box_Version::VERSION; |
109
|
|
|
$curl = new Box_Curl($url); |
110
|
|
|
$curl->request(); |
111
|
|
|
$response = $curl->getBody(); |
112
|
|
|
return json_decode($response); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Perform update |
117
|
|
|
* |
118
|
|
|
* @throws Exception |
119
|
|
|
*/ |
120
|
|
|
public function performUpdate() |
121
|
|
|
{ |
122
|
|
|
if(!$this->getCanUpdate()) { |
123
|
|
|
throw new LogicException('You have latest version of BoxBilling. You do not need to update.'); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
error_log('Started BoxBilling auto-update script'); |
127
|
|
|
$latest_version = $this->getLatestVersion(); |
128
|
|
|
$latest_version_archive = BB_PATH_CACHE.DIRECTORY_SEPARATOR.$latest_version.'.zip'; |
129
|
|
|
|
130
|
|
|
// download latest archive from link |
131
|
|
|
$content = $this->di['tools']->file_get_contents($this->getLatestVersionDownloadLink()); |
132
|
|
|
$f = fopen($latest_version_archive,'wb'); |
133
|
|
|
fwrite($f,$content,strlen($content)); |
134
|
|
|
fclose($f); |
135
|
|
|
|
136
|
|
|
//@todo validate downloaded file hash |
137
|
|
|
|
138
|
|
|
// Extract latest archive on top of current version |
139
|
|
|
$ff = new Box_Zip($latest_version_archive); |
140
|
|
|
$ff->decompress(BB_PATH_ROOT); |
141
|
|
|
|
142
|
|
|
if(file_exists(BB_PATH_ROOT.'/bb-update.php')) { |
143
|
|
|
error_log('Calling bb-update.php script from auto-updater'); |
144
|
|
|
$this->di['tools']->file_get_contents(BB_URL.'bb-update.php'); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
// clean up things |
148
|
|
|
$this->di['tools']->emptyFolder(BB_PATH_CACHE); |
149
|
|
|
$this->di['tools']->emptyFolder(BB_PATH_ROOT.'/install'); |
150
|
|
|
rmdir(BB_PATH_ROOT.'/install'); |
151
|
|
|
return true; |
152
|
|
|
} |
153
|
|
|
} |