update.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. /*
  3. *
  4. * OGP - Open Game Panel
  5. * Copyright (C) 2008 - 2018 The OGP Development Team
  6. *
  7. * http://www.opengamepanel.org/
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. *
  23. */
  24. // todo, make checking and updating functions for updateing on the background.
  25. // todo, more specified updates in smaller packages
  26. function check_file($local_path, $remote_url)
  27. {
  28. // Load remote file contents in to variable
  29. $remote_file = file_get_contents($remote_url);
  30. // Load local file contents in to variable
  31. $local_file = file_get_contents($local_path);
  32. if( $remote_file != $local_file )
  33. {
  34. // The file have changes, save them:
  35. if( ! file_put_contents($local_path, $remote_file) )
  36. {
  37. print_failure($local_path . ' has been changed, but the file can not be updated, you should ensure that your panel files are writeable and try again.');
  38. return False;
  39. }
  40. return True;
  41. }
  42. else
  43. {
  44. return "nochange";
  45. }
  46. }
  47. function urlProperEncode($string) {
  48. $entities = array('%21', '%2A', '%27', '%28', '%29', '%3B', '%3A', '%40', '%26', '%3D', '%2B', '%24', '%2C', '%2F', '%3F', '%25', '%23', '%5B', '%5D');
  49. $replacements = array('!', '*', "'", "(", ")", ";", ":", "@", "&", "=", "+", "$", ",", "/", "?", "%", "#", "[", "]");
  50. return str_replace($entities, $replacements, urlencode($string));
  51. }
  52. function exec_ogp_module()
  53. {
  54. global $db, $settings;
  55. define('REPONAME', 'OGP-Website');
  56. if ($_SESSION['users_group'] != "admin")
  57. {
  58. print_failure(get_lang('no_access'));
  59. return;
  60. }
  61. // Check if PHP-ZIP is installed, otherwise we won't be able to extract the downloaded update.
  62. if (extension_loaded('zip') === false) {
  63. print_failure(get_lang('missing_zip_extension'));
  64. return;
  65. }
  66. // GitHub URL
  67. if(function_exists("getOGPGitHubURL") && function_exists("getOGPGitHubURLUnstrict") && function_exists("getGitHubOrganization")){
  68. $gitHubUsername = $settings["custom_github_update_username"];
  69. $gitHubBranch = (!empty($settings['custom_github_update_branch_name']) ? $settings['custom_github_update_branch_name'] : 'master');
  70. $gitHubURL = getOGPGitHubURL($gitHubUsername, REPONAME, $gitHubBranch);
  71. $gitHubOrganization = getGitHubOrganization($gitHubURL);
  72. }else{
  73. $gitHubURL = "https://github.com/OpenGamePanel/";
  74. }
  75. $gitHubBranchName = urlProperEncode(!empty($settings['custom_github_update_branch_name']) ? $settings['custom_github_update_branch_name'] : 'master');
  76. define('RSS_REMOTE_PATH', $gitHubURL . REPONAME . '/commits/' . $gitHubBranchName . '.atom');
  77. define('MODULE_PATH', 'modules/'.$_GET['m'].'/');
  78. define('RSS_LOCAL_PATH', MODULE_PATH . $gitHubBranchName . '.atom');
  79. if( is_writable(MODULE_PATH) )
  80. {
  81. if( ! file_put_contents(RSS_LOCAL_PATH, file_get_contents(RSS_REMOTE_PATH)))
  82. {
  83. print_failure('Unable to download: ' . RSS_REMOTE_PATH . '<br>Check your Administration --> Panel Settings --> Github username and branch settings.&nbsp; These settings should have no value (be blank) unless you\'re a developer and know how to use these settings!');
  84. }
  85. }
  86. if( file_exists(RSS_LOCAL_PATH) )
  87. {
  88. try {
  89. $feedXml = new SimpleXMLElement(file_get_contents(RSS_LOCAL_PATH), LIBXML_NOCDATA);
  90. $seed = basename( (string) $feedXml->entry[0]->link['href'] );
  91. unlink(RSS_LOCAL_PATH);
  92. } catch (Exception $e) {
  93. print_failure('Unable to update: '.$e->getMessage());
  94. return;
  95. }
  96. }
  97. else
  98. {
  99. print_failure('Unable to read : ' . RSS_LOCAL_PATH);
  100. return;
  101. }
  102. if(isset($seed))
  103. {
  104. /// Checking for changes in the main update files:
  105. $main_update_files = array(
  106. 'includes/functions.php' => 'https://raw.githubusercontent.com/' . $gitHubOrganization . '/'.REPONAME.'/'.$seed.'/includes/functions.php',
  107. 'includes/helpers.php' => 'https://raw.githubusercontent.com/' . $gitHubOrganization . '/'.REPONAME.'/'.$seed.'/includes/helpers.php',
  108. 'modules/update/update.php' => 'https://raw.githubusercontent.com/' . $gitHubOrganization . '/'.REPONAME.'/'.$seed.'/modules/update/update.php',
  109. 'modules/update/updating.php' => 'https://raw.githubusercontent.com/' . $gitHubOrganization . '/'.REPONAME.'/'.$seed.'/modules/update/updating.php',
  110. 'modules/update/unzip.php' => 'https://raw.githubusercontent.com/' . $gitHubOrganization . '/'.REPONAME.'/'.$seed.'/modules/update/unzip.php'
  111. );
  112. $refresh = False;
  113. foreach($main_update_files as $local_path => $remote_url)
  114. {
  115. $result = check_file($local_path, $remote_url);
  116. if ($result === 'nochange')
  117. {
  118. continue;
  119. }
  120. elseif($result)
  121. {
  122. $refresh = True;
  123. }
  124. else
  125. {
  126. return;
  127. }
  128. }
  129. if($refresh)
  130. {
  131. header("Refresh:0");
  132. return;
  133. }
  134. echo "<h2>".get_lang('update')."</h2>";
  135. $pversion = $settings['ogp_version'];
  136. echo '<a href="?m='.$_GET['m'].'&p=blacklist" >'.get_lang('blacklist_files')."</a>&nbsp;".get_lang('blacklist_files_info')."</br></br>";
  137. echo get_lang('panel_version').": ".$pversion."</br></br>";
  138. echo get_lang('latest_version').": $seed</br></br>";
  139. if ( $seed != $pversion )
  140. {
  141. $dwl = $gitHubURL . REPONAME . '/archive/'.$seed.'.zip';
  142. $dwlHeaders = get_headers($dwl);
  143. if($dwlHeaders[0] != 'HTTP/1.1 302 Found')
  144. print_failure('The generated URL for the download returned a bad response code: ' . $dwlHeaders[0]);
  145. else
  146. echo "<form action='?m=".$_GET['m']."&amp;p=updating&amp;version=".$seed."' method='post'>\n".
  147. "<input type='submit' value='".get_lang('update_now')."' /></form><br><br>\n";
  148. if(function_exists('curl_version')){
  149. echo "<h2>Latest Changes</h2>";
  150. $commitsStart = 0;
  151. $commitsToShow = 5;
  152. $gitHubUpdateName = (!empty($settings['custom_github_update_username']) ? $settings['custom_github_update_username'] : 'OpenGamePanel');
  153. if($gitHubBranchName != "master"){
  154. $commitsUrl = 'https://api.github.com/repos/'.$gitHubUpdateName.'/'.REPONAME.'/commits/' . $gitHubBranchName;
  155. }else{
  156. $commitsUrl = 'https://api.github.com/repos/'.$gitHubUpdateName.'/'.REPONAME.'/commits';
  157. }
  158. $ch = curl_init();
  159. curl_setopt($ch, CURLOPT_URL, $commitsUrl);
  160. curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:51.0) Gecko/20100101 Firefox/51.0');
  161. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  162. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  163. $data = curl_exec($ch);
  164. if ($data) {
  165. $json = json_decode($data, true);
  166. if(array_key_exists("0", $json)){
  167. if (!empty($json[0]['commit'])) {
  168. echo '<ul>';
  169. foreach ($json as $k=>$v) {
  170. if ($commitsStart >= $commitsToShow) {
  171. break;
  172. }
  173. echo '<li>'.substr($v['commit']['author']['date'],0,10).' - '.$v['commit']['author']['name'] .'</a> committed <a href="'.$v['html_url'].'" target="_blank">'.substr($v['sha'],0,7).'...</a><br>';
  174. echo '<b>'.str_replace("*", "<br><br>", $v['commit']['message']) .'</b></li><br>';
  175. ++$commitsStart;
  176. }
  177. echo '</ul><a href="https://github.com/'.$gitHubUpdateName.'/'.REPONAME.'/commits' . '" target="_blank">View more commits...</a>';
  178. }
  179. }else{
  180. if (!empty($json['commit'])) {
  181. echo '<ul>';
  182. $v = $json;
  183. echo '<li>'.substr($v['commit']['author']['date'],0,10).' - '.$v['commit']['author']['name'] .'</a> committed <a href="'.$v['html_url'].'" target="_blank">'.substr($v['sha'],0,7).'...</a><br>';
  184. echo '<b>'.str_replace("*", "<br><br>", $v['commit']['message']) .'</b></li><br>';
  185. echo '</ul>';
  186. }
  187. }
  188. }
  189. }
  190. }
  191. else
  192. {
  193. print_success(get_lang('the_panel_is_up_to_date'));
  194. }
  195. }
  196. }
  197. ?>