updating.php 7.5 KB

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