bootstrap version of site
[Bans.Verniy.xyz-Modern.git] / CronJobs / backup.php
bloba8214394cceed3061bcfd30279ed421e621a5294
1 <?php
2 //Pushes files to github via bot.
3 //stores at https://api.github.com/repos/ECHibiki/Backup_bans.verniy.xyz
4 $start_time = microtime(true) * 1000;
5 $github_settings = file_get_contents("../Settings/github.ini");
6 $token = substr($github_settings , strpos($github_settings, "=") + 1);
8 include("Functions/RepoFunctions.php");
10 //read status file
11 $log_contents = json_decode(file_get_contents(__DIR__ . "/FileLog.json"), true);
12 var_dump($log_contents);
13 echo("<br>.........<br>");
14 //acquire files from root up.
15 $directory_hunt = new RecursiveDirectoryIterator("../", RecursiveDirectoryIterator::SKIP_DOTS);
16 $iterator = new RecursiveIteratorIterator($directory_hunt);
18 $rollback_files = Array();
19 foreach($iterator as $file_name){
21 //set url to send to
22 $file_name = substr($file_name, 3);
23 echo "$file_name<br>";
25 $url = "https://api.github.com/repos/ECHibiki/Backup_bans.verniy.xyz/contents/$file_name";
26 $curl = curl_init($url);
28 //Authenitcate
29 authenticate("https://api.github.com/user", "verniy-bot", $token, $curl);
31 //Check for appropriate action
32 $status = fileStatus($file_name, $log_contents);
33 //is not in log
34 echo($status[0] . " " . $status[1] . " :: ");
36 if ($status[0] == 0) {
37 createFile($curl, $file_name, $status[1], $log_contents);
38 echo"Create - $url ";
40 //in log, needs update
41 else if($status[0] == 1) {
42 if($file_name == NULL || strpos($file_name,".ini") !== false /*|| strpos($file_name,"RepoFunctions.php") !== false*/){
43 $log_contents["Seen"][$status[1]] = 1;
44 echo"Ignored - $url <br/><br/>";
45 continue;
47 updateFile($curl, $file_name, $status[1], $log_contents);
48 echo"Update - $url ";
50 //in log, no update
51 else if($status[0] == -1){
52 echo"Continue - $url <br/><br/>";
54 $log_contents["Seen"][$status[1]] = 1;
56 continue;
58 else{
59 echo"Unspecified Error<br/><br/>";
60 $log_contents["Seen"][$status[1]] = 1;
61 continue;
63 $response_status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
64 $response = curl_exec($curl);
65 curl_close($curl);
67 $sha = json_decode(substr($response, strpos($response, "{\"content\":")), true)["content"]["sha"];
69 $log_contents["File-Name"][$status[1]] = $file_name;
70 $log_contents["Recorded-Last-Update"][$status[1]] = time();
71 $log_contents["Sha"][$status[1]] = $sha;
72 $log_contents["Seen"][$status[1]] = 1;
74 $code = substr($response, 8, 38);
75 if(strpos($code, "4") === false){
76 echo " == " . substr($response, 8, 38) . "<br/><br/>";
77 array_push($rollback_files, $file_name);
79 else{
80 $fail = true;
81 if(strpos($code, "422") !== false || strpos($code, "409") !== false){
82 $file_loc = "https://api.github.com/repos/ECHibiki/Backup_bans.verniy.xyz/contents/$file_name";
83 echo $file_loc;
84 $ch = curl_init($file_loc);
85 authenticate("https://api.github.com/user", "verniy-bot", $token, $ch);
86 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
87 curl_setopt($ch,CURLOPT_USERAGENT, "Banlog-updater");
88 $result = curl_exec($ch);
89 $result = substr($result, strpos($result, '{'));
90 echo"+=+=+=";
91 echo($result);
92 $sha = json_decode($result, true)["sha"];
94 echo "||||||$file_name - $code RETRY::::Sha $sha||||||||";
95 $url = "https://api.github.com/repos/ECHibiki/Backup_bans.verniy.xyz/contents/$file_name";
96 $curl = curl_init($url);
98 //Authenitcate
99 authenticate("https://api.github.com/user", "verniy-bot", $token, $curl);
100 //update on retry
101 updateFileDirect($curl, $file_name, $status[1], $sha);
103 $response_status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
104 $response = curl_exec($curl);
105 curl_close($curl);
107 $sha = json_decode(substr($response, strpos($response, "{\"content\":")), true)["content"]["sha"];
109 $log_contents["File-Name"][$status[1]] = $file_name;
110 $log_contents["Recorded-Last-Update"][$status[1]] = time();
111 $log_contents["Sha"][$status[1]] = $sha;
112 $log_contents["Seen"][$status[1]] = 1;
114 $code = substr($response, 8, 38);
115 if(strpos($code, "4") === false){
116 echo " == " . substr($response, 8, 38) . "<br/><br/>";
117 $fail = false;
119 else $fail = true;
121 if($fail){
122 if(strpos($code, "409") !== false) echo "///Too Large To Fetch?";
123 else if(strpos($code, "400") !== false) echo "///Filename error";
124 echo "<br><br>" . $response . "<br/><br/>";
125 unset($log_contents["File-Name"][$status[1]]);
126 unset($log_contents["Recorded-Last-Update"][$status[1]]);
127 unset($log_contents["Sha"][$status[1]]);
128 unset($log_contents["Seen"][$status[1]]);
133 var_dump($log_contents);
134 echo "<br>";
135 $index_counter = 0;
137 foreach($log_contents["Seen"] as &$seen){
138 if($seen == 1)
139 $seen = 0;
140 else{
141 $file_name = $log_contents["File-Name"][$index_counter];
143 echo "<br><br>Deleting $file_name";
145 $url = "https://api.github.com/repos/ECHibiki/Backup_bans.verniy.xyz/contents/" . $file_name;
146 $curl = curl_init($url);
147 //Authenitcate
148 authenticate("https://api.github.com/user", "verniy-bot", $token, $curl);
149 //attempt to remove file if it exists
150 deleteFile($curl, $file_name, $index_counter, $log_contents["Sha"][$index_counter]);
151 $data = curl_exec($curl);
153 // //then remove from log
154 unset($log_contents["File-Name"][$index_counter]);
155 unset($log_contents["Recorded-Last-Update"][$index_counter]);
156 unset($log_contents["Sha"][$index_counter]);
157 unset($log_contents["Seen"][$index_counter]);
159 echo "==" . substr($data, 8, 38);
161 $index_counter++;
163 echo "<br>" ;
164 var_dump($log_contents);
166 $log_contents["File-Name"] = array_values($log_contents["File-Name"]);
167 $log_contents["Recorded-Last-Update"] = array_values($log_contents["Recorded-Last-Update"]);
168 $log_contents["Sha"] = array_values($log_contents["Sha"]);
169 $log_contents["Seen"] = array_values($log_contents["Seen"]);
171 $log = fopen(__DIR__ . "/FileLog.json", "w");
172 fwrite($log, json_encode($log_contents));
173 fclose($log);
175 echo "<br/><br/>" . (microtime(true) * 1000 - $start_time);