updating.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. function rmdir_recurse($path) {
  25. $path = rtrim($path, '/').'/';
  26. $handle = opendir($path);
  27. while(false !== ($file = readdir($handle))) {
  28. if($file != '.' and $file != '..' ) {
  29. $fullpath = $path.$file;
  30. if(is_dir($fullpath)) rmdir_recurse($fullpath); else unlink($fullpath);
  31. }
  32. }
  33. closedir($handle);
  34. rmdir($path);
  35. }
  36. function exec_ogp_module()
  37. {
  38. define('REPONAME', 'OGP-Website');
  39. if($_SESSION['users_group'] != "admin")
  40. {
  41. print_failure( no_access );
  42. return;
  43. }
  44. global $db, $settings;
  45. global $view;
  46. // GitHub URL
  47. if(function_exists("getOGPGitHubURL") && function_exists("getOGPGitHubURLUnstrict")){
  48. $gitHubUsername = $settings["custom_github_update_username"];
  49. $gitHubURL = getOGPGitHubURL($gitHubUsername, REPONAME);
  50. }else{
  51. $gitHubURL = "https://github.com/OpenGamePanel/";
  52. }
  53. $vtype = "HubGit";
  54. echo "<h4>" . dwl_update . "</h4>\n";
  55. //This is usefull when you are downloading big files, as it
  56. //will prevent time out of the script
  57. set_time_limit(0);
  58. error_reporting(E_ALL);
  59. ini_set('display_errors',true);
  60. $baseDir = str_replace( "modules" . DIRECTORY_SEPARATOR . $_GET['m'],"",dirname(__FILE__) );
  61. if( !is_writable( $baseDir ) )
  62. {
  63. if ( ! @chmod( $baseDir, 0755 ) )
  64. {
  65. print_failure( get_lang_f( 'base_dir_not_writable', $baseDir ) );
  66. return;
  67. }
  68. }
  69. $temp = get_temp_dir(dirname(__FILE__));
  70. if( is_writable( $temp ) )
  71. {
  72. // Download file to temporary folder
  73. $temp_dwl = $temp . DIRECTORY_SEPARATOR . $_GET['version'] . '.zip';
  74. $dwl = $gitHubURL . REPONAME . '/archive/'.$_GET['version'].'.zip';
  75. $zip_raw_data = file_get_contents($dwl);
  76. if(! $zip_raw_data)
  77. {
  78. print_failure( get_lang_f( 'dwl_failed', $url ) );
  79. return;
  80. }
  81. file_put_contents($temp_dwl, $zip_raw_data);
  82. // Check if the file exists and the size is bigger than a 404 error page from sf.net
  83. if( file_exists( $temp_dwl ) )
  84. {
  85. $stat = stat( $temp_dwl );
  86. }
  87. else
  88. {
  89. print_failure( get_lang_f( 'dwl_failed', $url ) );
  90. return;
  91. }
  92. if( $stat['size'] > 1500 )
  93. {
  94. print_success( dwl_complete );
  95. }
  96. else
  97. {
  98. print_failure( get_lang_f( 'dwl_failed', $url ) );
  99. return;
  100. }
  101. echo "<h4>". install_update . "</h4>\n";
  102. // Set default values for file checkings before installing
  103. $not_writable = can_not_update_non_writable_files . " :<br>";
  104. $filename = "";
  105. $overwritten = 0;
  106. $new = 0;
  107. $all_writable = TRUE;
  108. $filelist = "";
  109. $overwritten_files = "";
  110. $new_files = "";
  111. $unwanted_path = REPONAME . "-" . $_GET['version'];
  112. $extract_path = $temp . DIRECTORY_SEPARATOR . "OGP_update";
  113. if( !file_exists($extract_path) )
  114. mkdir($extract_path, 0775);
  115. $blacklist = array ('/install.php',
  116. '/modules/gamemanager/rsync_sites_local.list');
  117. $blacklisted_files = $db->resultQuery('SELECT file_path FROM `OGP_DB_PREFIXupdate_blacklist`;');
  118. if($blacklisted_files !== FALSE)
  119. {
  120. $current_blacklist = array();
  121. foreach($blacklisted_files as $blacklisted_file)
  122. {
  123. $current_blacklist[] = $blacklisted_file['file_path'];
  124. }
  125. $blacklist = array_merge($current_blacklist,$blacklist);
  126. }
  127. include ( 'unzip.php' ); // array|false extractZip( string $zipFile, string $extract_path [, string $remove_path, array $blacklist, array $whitelist] )
  128. $result = extractZip( $temp_dwl, $extract_path, $unwanted_path, '', '' );
  129. if ( is_array( $result['extracted_files'] ) and count($result['extracted_files']) > 0 )
  130. {
  131. // Check file by file if already exists, if it matches, compares both files
  132. // looking for changes determining if the file needs to be updated.
  133. // Also determines if the file is writable
  134. $filelist = array();
  135. $i = 0;
  136. foreach( $result['extracted_files'] as $file )
  137. {
  138. $filename = str_replace( $unwanted_path, "" , $file['filename'] );
  139. $temp_file = $extract_path . DIRECTORY_SEPARATOR . $filename;
  140. $web_file = $baseDir . $filename;
  141. if( file_exists( $web_file ) )
  142. {
  143. $temp = file_get_contents($temp_file);
  144. $web = file_get_contents($web_file);
  145. if( $temp != $web )
  146. {
  147. if( !is_writable( $web_file ) )
  148. {
  149. if ( ! @chmod( $web_file, 0775 ) )
  150. {
  151. $all_writable = FALSE;
  152. $not_writable .= $web_file."<br>";
  153. }
  154. else
  155. {
  156. $filelist[$i] = $file['filename'];
  157. $i++;
  158. $overwritten_files .= $filename . "<br>";
  159. $overwritten++;
  160. }
  161. }
  162. else
  163. {
  164. $filelist[$i] = $file['filename'];
  165. $i++;
  166. if( !in_array( $filename, $blacklist ) )
  167. {
  168. $overwritten_files .= $filename . "<br>";
  169. $overwritten++;
  170. }
  171. }
  172. }
  173. }
  174. else
  175. {
  176. $filelist[$i] = $file['filename'];
  177. $i++;
  178. if( !in_array( $filename, $blacklist ) )
  179. {
  180. $new_files .= $filename . "<br>";
  181. $new++;
  182. }
  183. }
  184. }
  185. }
  186. else
  187. {
  188. echo $result;
  189. $all_writable = FALSE;
  190. }
  191. // Once checking is done the temp folder is removed
  192. if( file_exists( $extract_path ) )
  193. {
  194. rmdir_recurse( $extract_path );
  195. }
  196. if( $all_writable )
  197. {
  198. // Extract the files that are set in $filelist, to the folder at $baseDir and removes 'upload' from the beginning of the path.
  199. $result = extractZip( $temp_dwl, preg_replace("/\/$/","",$baseDir), $unwanted_path, $blacklist, $filelist );
  200. if( is_array( $result['ignored_files'] ) and !empty( $result['ignored_files'] ) )
  201. {
  202. print_failure(get_lang_f('ignored_files',count($result['ignored_files'])));
  203. echo get_lang_f("not_updated_files_blacklisted", implode("<br>", $result['ignored_files']) );
  204. echo "<br><br><br>";
  205. }
  206. if( is_array( $result['extracted_files'] ) )
  207. {
  208. // Updated files
  209. if ( $overwritten > 0 )
  210. {
  211. print_success(get_lang_f('files_overwritten',$overwritten));
  212. echo get_lang_f( "updated_files", $overwritten_files );
  213. }
  214. if ( $new > 0 )
  215. {
  216. print_success(get_lang_f('new_files',$new));
  217. echo get_lang_f( "updated_files", $new_files );
  218. }
  219. // update version info in db
  220. $db->query("UPDATE OGP_DB_PREFIXsettings SET value = '$_GET[version]' WHERE setting = 'ogp_version'");
  221. $db->query("UPDATE OGP_DB_PREFIXsettings SET value = '$vtype' WHERE setting = 'version_type'");
  222. // Remove the downloaded package
  223. if( file_exists( $temp_dwl ) )
  224. //unlink( $temp_dwl );
  225. // Remove files that are not related to the panel
  226. if( file_exists( $baseDir . DIRECTORY_SEPARATOR . $unwanted_path ) )
  227. {
  228. rmdir_recurse( $baseDir . DIRECTORY_SEPARATOR . $unwanted_path );
  229. }
  230. echo "<br>\n<h4>" . updating_modules ."</h4>\n";
  231. require_once('modules/modulemanager/module_handling.php');
  232. $modules = $db->getInstalledModules();
  233. foreach ( $modules as $row )
  234. {
  235. update_module($db, $row['id'], $row['folder']);
  236. }
  237. print_success(update_complete);
  238. removeOldPanelFiles();
  239. }
  240. else
  241. {
  242. echo $result;
  243. }
  244. }
  245. else
  246. {
  247. print_failure($not_writable);
  248. }
  249. }
  250. else
  251. {
  252. print_failure( get_lang_f( 'temp_folder_not_writable', $temp ) );
  253. }
  254. }
  255. ?>