-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdate_class.php
128 lines (126 loc) · 4.29 KB
/
update_class.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
<?php
class gitupdate {
public $url;
public $script;
public function __construct($user,$script) {
global $mysql_conn, $_POST;
$this->url = "https://api.github.com/repos/$user/$script/commits";
$this->script = $script;
if( mysqli_num_rows(mysqli_query($mysql_conn,"SHOW TABLES LIKE '{$this->script}_settings' ")) == 0 ){
$mi_table3= "CREATE TABLE {$this->script}_settings(
varname VARCHAR(65) NOT NULL UNIQUE,
varval VARCHAR(65) NOT NULL,
PRIMARY KEY(`varname`)
)ENGINE=MyISAM DEFAULT CHARSET=utf8;";
mysqli_query($mysql_conn, $mi_table3);
}
if (isset($_POST['update']) && $_POST['update'] == 'update') {
$this->doupdate();
}
}
private function doupdate() {
global $_POST;
shell_exec("cd /usr/local/src/{$this->script} && git pull && ./install");
unset($_POST);
echo <<<EOF
<script>
if ( window.history.replaceState ) {
window.history.replaceState( null, null, window.location.href );
}
</script>
EOF;
}
private function checkgit() {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $this->url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_USERAGENT, $this->script);
//curl_setopt($curl, CONNECTTIMEOUT, 1);
$content = curl_exec($curl);
curl_close($curl);
$data = json_decode($content,true);
if (isset($data[0]['sha'])) {
return $data[0]['sha'];
} else {
var_dump($data);
return false;
}
}
private function updatemessage() {
$msg = "<div style='position:absolute; top:80px;' class='alert alert-info'><button type='button' class='close' data-dismiss='alert'>×</button>";
$msg .= "<h3>A New Version is available</h3><p>Please follow the directions:<br><code>cd /usr/local/src/$this->script<br>git update && ./install</code>";
$msg .= '<h3>A New Version is available</h3><p><form method="post" action="index.php?module='.$this->script.'" class="inline">
<button type="submit" name="update" value="update" class="link-button">Update Now!</button></form>';
$msg .= "</p></div>";
return $msg;
}
private function setval($valname,$valval) {
global $mysql_conn;
if (mysqli_query($mysql_conn,"insert into {$this->script}_settings (varname, varval) values ('{$valname}','{$valval}') on duplicate key update {$valname}='{$valval}'") or die($mysqli_error($mysql_conn))) return true;
}
private function readval($valname) {
global $mysql_conn;
$resp = mysqli_query($mysql_conn,"select varval from {$this->script}_settings where varname='{$valname}'") or die(mysqli_error($mysql_conn));
if (mysqli_num_rows($resp) > 0) {
list($var) = mysqli_fetch_row($resp);
return $var;
} else {
return false;
}
}
public function checkupdate($force = "N") {
// need to check date last checked (varname, varval) $this->script_settings
if ($lastcheck = $this->readval('lastcheck') && $sha = $this->readval('sha')) {
if ($force != "N") {
$newsha = $this->checkgit();
if ($newsha === false) return false;
$date = date("Y-m-d H:i:s");
$this->setval('sha',$newsha);
$this->setval('lastcheck',$date);
if ($sha != $newsha) {
echo $this->updatemessage();
}
return true;
}
$start_date = new DateTime($lastcheck);
$since_start = $start_date->diff(new DateTime(date("Y-m-d H:i:s")));
if ($since_start->d >= 1) {
$newsha = $this->checkgit();
if ($newsha === false) return false;
$date = date("Y-m-d H:i:s");
$this->setval('sha',$newsha);
$this->setval('lastcheck',$date);
if ($sha != $newsha) {
echo $this->updatemessage();
}
}
/*
$since_start->days.' days total<br>';
$since_start->y.' years<br>';
$since_start->m.' months<br>';
$since_start->d.' days<br>';
$since_start->h.' hours<br>';
$since_start->i.' minutes<br>';
$since_start->s.' seconds<br>';
"LC: " . $lastcheck . "<br>";
"Sha: " .$sha;*/
}
else {
// No Response or table not created. We do the first check and create it.
$sha = $this->checkgit();
if ($sha === false) return false;
$date = date("Y-m-d H:i:s");
$this->setval('sha',$sha);
$this->setval('lastcheck',$date);
}
/**/
}
}
/*To Call
include_once "gitupdate.php";
$update = new gitupdate('rcschaff82','cwp_2fa');
$force = (isset($_GET['forceupdate']))?'Y':'N';
$update->checkupdate($force);
*/
?>