updating.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. 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( get_lang("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. $gitHubBranch = (!empty($settings['custom_github_update_branch_name']) ? $settings['custom_github_update_branch_name'] : 'master');
  50. $gitHubURL = getOGPGitHubURL($gitHubUsername, REPONAME, $gitHubBranch);
  51. }else{
  52. $gitHubURL = "https://github.com/OpenGamePanel/";
  53. }
  54. $vtype = "HubGit";
  55. echo "<h4>" . get_lang("dwl_update") . "</h4>\n";
  56. //This is usefull when you are downloading big files, as it
  57. //will prevent time out of the script
  58. set_time_limit(0);
  59. error_reporting(E_ALL);
  60. ini_set('display_errors',true);
  61. $baseDir = str_replace( "modules" . DIRECTORY_SEPARATOR . $_GET['m'],"",dirname(__FILE__) );
  62. if( !is_writable( $baseDir ) )
  63. {
  64. if ( ! @chmod( $baseDir, 0755 ) )
  65. {
  66. print_failure( get_lang_f( 'base_dir_not_writable', $baseDir ) );
  67. return;
  68. }
  69. }
  70. $temp = get_temp_dir(dirname(__FILE__));
  71. if( is_writable( $temp ) )
  72. {
  73. // Download file to temporary folder
  74. $temp_dwl = $temp . DIRECTORY_SEPARATOR . $_GET['version'] . '.zip';
  75. $dwl = $gitHubURL . REPONAME . '/archive/'.$_GET['version'].'.zip';
  76. $zip_raw_data = file_get_contents($dwl);
  77. if(! $zip_raw_data)
  78. {
  79. print_failure( get_lang_f( 'dwl_failed', $url ) );
  80. return;
  81. }
  82. file_put_contents($temp_dwl, $zip_raw_data);
  83. // Check if the file exists and the size is bigger than a 404 error page from sf.net
  84. if( file_exists( $temp_dwl ) )
  85. {
  86. $stat = stat( $temp_dwl );
  87. }
  88. else
  89. {
  90. print_failure( get_lang_f( 'dwl_failed', $url ) );
  91. return;
  92. }
  93. if( $stat['size'] > 1500 )
  94. {
  95. print_success(get_lang("dwl_complete"));
  96. }
  97. else
  98. {
  99. print_failure( get_lang_f( 'dwl_failed', $url ) );
  100. return;
  101. }
  102. echo "<h4>". get_lang("install_update") . "</h4>\n";
  103. // Set default values for file checkings before installing
  104. $not_writable = get_lang("can_not_update_non_writable_files") . " :<br>";
  105. $filename = "";
  106. $overwritten = 0;
  107. $new = 0;
  108. $all_writable = TRUE;
  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 = extractZipGitUpdateFile($temp_dwl, $extract_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. $i = 0;
  135. $i2 = 0;
  136. $newResult = array('ignored_files' => array(), 'extracted_files' => array());
  137. foreach( $result['extracted_files'] as $file )
  138. {
  139. $filename = $file['filename'];
  140. $filenameLocal = str_replace( $unwanted_path, "" , $filename );
  141. $temp_file = $extract_path . DIRECTORY_SEPARATOR . $filename;
  142. $web_file = $baseDir . $filenameLocal;
  143. if(file_exists($temp_file)){
  144. if(file_exists($web_file))
  145. {
  146. $temp = file_get_contents($temp_file);
  147. $web = file_get_contents($web_file);
  148. if( $temp != $web )
  149. {
  150. if( !is_writable( $web_file ) )
  151. {
  152. if ( ! @chmod( $web_file, 0775 ) )
  153. {
  154. $all_writable = FALSE;
  155. $not_writable .= $web_file."<br>";
  156. }
  157. else
  158. {
  159. $newResult["extracted_files"][$i]["filename"] = $filenameLocal;
  160. copy($temp_file, $web_file);
  161. $i++;
  162. $overwritten_files .= $filenameLocal . "<br>";
  163. $overwritten++;
  164. }
  165. }
  166. else
  167. {
  168. if( !in_array( $filenameLocal, $blacklist ) )
  169. {
  170. $newResult["extracted_files"][$i]["filename"] = $filenameLocal;
  171. copy($temp_file, $web_file);
  172. $i++;
  173. $overwritten_files .= $filenameLocal . "<br>";
  174. $overwritten++;
  175. }else{
  176. $newResult["ignored_files"][$i2] = $filenameLocal;
  177. $i2++;
  178. }
  179. }
  180. }
  181. }
  182. else
  183. {
  184. if( !in_array( $filenameLocal, $blacklist ) )
  185. {
  186. $newResult["extracted_files"][$i]["filename"] = $filenameLocal;
  187. $webDir = dirname($web_file);
  188. @mkdir($webDir, 0775, true);
  189. copy($temp_file, $web_file);
  190. $i++;
  191. $new_files .= $filenameLocal . "<br>";
  192. $new++;
  193. }else{
  194. $newResult["ignored_files"][$i2] = $filenameLocal;
  195. $i2++;
  196. }
  197. }
  198. }
  199. }
  200. }
  201. else
  202. {
  203. echo $result;
  204. $all_writable = FALSE;
  205. }
  206. // Once checking is done the temp folder is removed
  207. if( file_exists( $extract_path ) )
  208. {
  209. rmdir_recurse( $extract_path );
  210. }
  211. if( $all_writable )
  212. {
  213. if( is_array( $newResult['ignored_files'] ) and !empty( $newResult['ignored_files'] ) )
  214. {
  215. print_failure(get_lang_f('ignored_files',count($newResult['ignored_files'])));
  216. echo get_lang_f("not_updated_files_blacklisted", implode("<br>", $newResult['ignored_files']) );
  217. echo "<br><br><br>";
  218. }
  219. if( is_array( $newResult['extracted_files'] ) )
  220. {
  221. // Updated files
  222. if ( $overwritten > 0 )
  223. {
  224. print_success(get_lang_f('files_overwritten',$overwritten));
  225. echo get_lang_f( "updated_files", $overwritten_files );
  226. }
  227. if ( $new > 0 )
  228. {
  229. print_success(get_lang_f('new_files',$new));
  230. echo get_lang_f( "updated_files", $new_files );
  231. }
  232. // update version info in db
  233. $version = $db->real_escape_string($_GET['version']);
  234. $db->query("UPDATE OGP_DB_PREFIXsettings SET value = '$version' WHERE setting = 'ogp_version'");
  235. $db->query("UPDATE OGP_DB_PREFIXsettings SET value = '$vtype' WHERE setting = 'version_type'");
  236. // Remove the downloaded package
  237. if( file_exists( $temp_dwl ) )
  238. //unlink( $temp_dwl );
  239. // Remove files that are not related to the panel
  240. if( file_exists( $baseDir . DIRECTORY_SEPARATOR . $unwanted_path ) )
  241. {
  242. rmdir_recurse( $baseDir . DIRECTORY_SEPARATOR . $unwanted_path );
  243. }
  244. echo "<br>\n<h4>" . get_lang("updating_modules") ."</h4>\n";
  245. require_once('modules/modulemanager/module_handling.php');
  246. $modules = $db->getInstalledModules();
  247. // update module manager first
  248. foreach ( $modules as $row )
  249. {
  250. if($row['folder'] == 'modulemanager')
  251. {
  252. update_module($db, $row['id'], $row['folder']);
  253. break;
  254. }
  255. }
  256. foreach ( $modules as $row )
  257. {
  258. if($row['folder'] == 'modulemanager')//already updated
  259. continue;
  260. update_module($db, $row['id'], $row['folder']);
  261. }
  262. print_success(get_lang("update_complete"));
  263. // Inject AJAX to run post update operations again (which will reload functions and helpers in case there are changes there we need now that aren't available once this script finishes running
  264. echo '<script type="text/javascript">
  265. $.get("home.php?m=update&p=postupdate", function(msg){});
  266. </script>';
  267. // Run post update ops
  268. if(function_exists("runPostUpdateOperations")){
  269. runPostUpdateOperations();
  270. }
  271. }
  272. else
  273. {
  274. echo $result;
  275. }
  276. }
  277. else
  278. {
  279. print_failure($not_writable);
  280. }
  281. }
  282. else
  283. {
  284. print_failure( get_lang_f( 'temp_folder_not_writable', $temp ) );
  285. }
  286. }
  287. ?>