update.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /*
  3. *
  4. * OGP - Open Game Panel
  5. * Copyright (C) 2008 - 2017 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 and preg_match("/exec_ogp_module/", $remote_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 exec_ogp_module()
  48. {
  49. if ($_SESSION['users_group'] != "admin")
  50. {
  51. print_failure(get_lang('no_access'));
  52. return;
  53. }
  54. global $db, $settings;
  55. define('REPONAME', 'OGP-Website');
  56. // GitHub URL
  57. if(function_exists("getOGPGitHubURL") && function_exists("getOGPGitHubURLUnstrict") && function_exists("getGitHubOrganization")){
  58. $gitHubUsername = $settings["custom_github_update_username"];
  59. $gitHubURL = getOGPGitHubURL($gitHubUsername, REPONAME);
  60. $gitHubOrganization = getGitHubOrganization($gitHubURL);
  61. }else{
  62. $gitHubURL = "https://github.com/OpenGamePanel/";
  63. }
  64. define('RSS_REMOTE_PATH', $gitHubURL . REPONAME . '/commits/master.atom');
  65. define('MODULE_PATH', 'modules/'.$_GET['m'].'/');
  66. define('RSS_LOCAL_PATH', MODULE_PATH.'master.atom');
  67. if( is_writable(MODULE_PATH) )
  68. {
  69. if( ! file_put_contents(RSS_LOCAL_PATH, file_get_contents(RSS_REMOTE_PATH)))
  70. {
  71. print_failure('Unable to download : ' . RSS_REMOTE_PATH);
  72. }
  73. }
  74. if( file_exists(RSS_LOCAL_PATH) )
  75. {
  76. try {
  77. $feedXml = new SimpleXMLElement(file_get_contents(RSS_LOCAL_PATH), LIBXML_NOCDATA);
  78. $seed = basename( (string) $feedXml->entry[0]->link['href'] );
  79. unlink(RSS_LOCAL_PATH);
  80. } catch (Exception $e) {
  81. print_failure('Unable to update: '.$e->getMessage());
  82. return;
  83. }
  84. }
  85. else
  86. {
  87. print_failure('Unable to read : ' . RSS_LOCAL_PATH);
  88. return;
  89. }
  90. if(isset($seed))
  91. {
  92. /// Checking for changes in the main update files:
  93. $main_update_files = array( 'modules/update/update.php' => 'https://raw.githubusercontent.com/' . $gitHubOrganization . '/'.REPONAME.'/'.$seed.'/modules/update/update.php',
  94. 'modules/update/updating.php' => 'https://raw.githubusercontent.com/' . $gitHubOrganization . '/'.REPONAME.'/'.$seed.'/modules/update/updating.php' );
  95. $refresh = False;
  96. foreach($main_update_files as $local_path => $remote_url)
  97. {
  98. $result = check_file($local_path, $remote_url);
  99. if ($result === 'nochange')
  100. {
  101. continue;
  102. }
  103. elseif($result)
  104. {
  105. $refresh = True;
  106. }
  107. else
  108. {
  109. return;
  110. }
  111. }
  112. if($refresh)
  113. {
  114. header("Refresh:0");
  115. return;
  116. }
  117. echo "<h2>".get_lang('update')."</h2>";
  118. $pversion = $settings['ogp_version'];
  119. echo '<a href="?m='.$_GET['m'].'&p=blacklist" >'.get_lang('blacklist_files')."</a>&nbsp;".get_lang('blacklist_files_info')."</br></br>";
  120. echo get_lang('panel_version').": ".$pversion."</br></br>";
  121. echo get_lang('latest_version').": $seed</br></br>";
  122. if ( $seed != $pversion )
  123. {
  124. $dwl = $gitHubURL . REPONAME . '/archive/'.$seed.'.zip';
  125. $dwlHeaders = get_headers($dwl);
  126. if($dwlHeaders[0] != 'HTTP/1.1 302 Found')
  127. print_failure('The generated URL for the download returned a bad response code: ' . $dwlHeaders[0]);
  128. else
  129. echo "<form action='?m=".$_GET['m']."&amp;p=updating&amp;version=".$seed."' method='post'>\n".
  130. "<input type='submit' value='".get_lang('update_now')."' /></form><br><br>\n";
  131. }
  132. else
  133. {
  134. print_success(get_lang('the_panel_is_up_to_date'));
  135. }
  136. }
  137. }
  138. ?>