extras.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. <script type="text/javascript" src="js/modules/extras.js"></script>
  2. <?php
  3. /*
  4. *
  5. * OGP - Open Game Panel
  6. * Copyright (C) Copyright (C) 2008 - 2013 The OGP Development Team
  7. *
  8. * http://www.opengamepanel.org/
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version 2
  13. * of the License, or any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  23. *
  24. */
  25. require('modules/update/unzip.php');
  26. require('modules/modulemanager/module_handling.php');
  27. function rmdir_recurse($path) {
  28. $path = rtrim($path, '/').'/';
  29. $handle = opendir($path);
  30. while(false !== ($file = readdir($handle))) {
  31. if($file != '.' and $file != '..' ) {
  32. $fullpath = $path.$file;
  33. if(is_dir($fullpath)) rmdir_recurse($fullpath); else unlink($fullpath);
  34. }
  35. }
  36. closedir($handle);
  37. rmdir($path);
  38. }
  39. function getMyFile($url,$destination)
  40. {
  41. $result = file_get_contents($url);
  42. if(!$result)
  43. return $result;
  44. return file_put_contents($destination, $result);
  45. }
  46. function installUpdate($info, $base_dir)
  47. {
  48. $tmp = get_temp_dir(dirname(__FILE__));
  49. $temp_dwl = $tmp . DIRECTORY_SEPARATOR . $info['file'];
  50. $_SESSION['link'] = $info['link'];
  51. if(!getMyFile($info['link'],$temp_dwl))
  52. {
  53. echo get_lang_f('unable_download',$info['title'])."\n";
  54. return;
  55. }
  56. // Set default values for file checkings before installing
  57. $not_writable = can_not_update_non_writable_files ." :\n";
  58. $filename = "";
  59. $overwritten = 0;
  60. $new = 0;
  61. $all_writable = TRUE;
  62. $filelist = "";
  63. $overwritten_files = "";
  64. $new_files = "";
  65. $temp_dir = $tmp . DIRECTORY_SEPARATOR . "OGP_Extras";
  66. if( !file_exists($temp_dir) )
  67. mkdir($temp_dir, 0775);
  68. $result = extractZip( $temp_dwl, $temp_dir . DIRECTORY_SEPARATOR, $info['remove_path'] );
  69. if ( is_array($result['extracted_files']) and count($result['extracted_files']) > 0 )
  70. {
  71. $nfo_file = DATA_PATH . str_replace(' ','_',$info['title']) . ".nfo";
  72. $install_nfo = $info['timestamp']."\n$nfo_file\n";
  73. // Check file by file if already exists, if it matches, compares both files
  74. // looking for changes determining if the file needs to be updated.
  75. // Also determines if the file is writable
  76. $filelist = array();
  77. $i = 0;
  78. foreach( $result['extracted_files'] as $file )
  79. {
  80. if( DIRECTORY_SEPARATOR == '\\')
  81. $filename = str_replace('/', '\\', $file['filename']);
  82. else
  83. $filename = $file['filename'];
  84. $filename = preg_replace( "/".preg_quote($info['remove_path'])."/", "", $filename);
  85. $install_nfo .= realpath($base_dir) . $filename . "\n";
  86. $temp_file = $temp_dir . DIRECTORY_SEPARATOR . $filename;
  87. $web_file = $base_dir . $filename;
  88. if( file_exists( $web_file ) )
  89. {
  90. $temp = file_get_contents($temp_file);
  91. $web = file_get_contents($web_file);
  92. if( $temp != $web )
  93. {
  94. if( !is_writable( $web_file ) )
  95. {
  96. if ( ! @chmod( $web_file, 0644 ) )
  97. {
  98. $all_writable = FALSE;
  99. $not_writable .= $web_file."\n";
  100. }
  101. else
  102. {
  103. $filelist[$i] = $file['filename'];
  104. $i++;
  105. $overwritten_files .= $filename . "\n";
  106. $overwritten++;
  107. }
  108. }
  109. else
  110. {
  111. $filelist[$i] = $file['filename'];
  112. $i++;
  113. $overwritten_files .= $filename . "\n";
  114. $overwritten++;
  115. }
  116. }
  117. }
  118. else
  119. {
  120. $filelist[$i] = $file['filename'];
  121. $i++;
  122. $new_files .= $filename . "\n";
  123. $new++;
  124. }
  125. }
  126. }
  127. else
  128. {
  129. echo $result;
  130. // Remove the downloaded package
  131. if( file_exists( $temp_dwl ) )
  132. unlink( $temp_dwl );
  133. return FALSE;
  134. }
  135. // Once checkings are done the temp folder is removed
  136. if( file_exists( $temp_dir ) )
  137. {
  138. rmdir_recurse( $temp_dir );
  139. }
  140. if( $all_writable )
  141. {
  142. // Extract the files that are set in $filelist, to the folder at $base_dir.
  143. $result = extractZip( $temp_dwl, $base_dir, $info['remove_path'], '', $filelist );
  144. if( is_array( $result['extracted_files'] ) )
  145. {
  146. // Updated files
  147. if ( $overwritten > 0 )
  148. {
  149. echo get_lang_f('files_overwritten',$overwritten).":\n".$overwritten_files;
  150. }
  151. if ( $new > 0 )
  152. {
  153. echo get_lang_f('new_files',$new).":\n".$new_files;
  154. }
  155. // Add install.nfo file to the module/theme directory so we can remove the installed files later and check the installed files timestamp.
  156. file_put_contents($nfo_file, $install_nfo);
  157. // Remove the downloaded package
  158. if( file_exists( $temp_dwl ) )
  159. unlink( $temp_dwl );
  160. return TRUE;
  161. }
  162. else
  163. {
  164. // Remove the downloaded package
  165. if( file_exists( $temp_dwl ) )
  166. unlink( $temp_dwl );
  167. return FALSE;
  168. }
  169. }
  170. else
  171. {
  172. echo $info['title'].":\n$not_writable";
  173. // Remove the downloaded package
  174. if( file_exists( $temp_dwl ) )
  175. unlink( $temp_dwl );
  176. return FALSE;
  177. }
  178. }
  179. function rglob($pattern, $flags = 0) {
  180. $files = glob($pattern, $flags);
  181. foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir) {
  182. $files = array_merge($files, rglob($dir.'/'.basename($pattern), $flags));
  183. }
  184. return $files;
  185. }
  186. function deeperPathFirst($a, $b)
  187. {
  188. $al = count(explode(DIRECTORY_SEPARATOR,$a));
  189. $bl = count(explode(DIRECTORY_SEPARATOR,$b));
  190. if ($al == $bl) {
  191. return strcmp($a,$b);
  192. }
  193. return ($al > $bl) ? -1 : +1;
  194. }
  195. function exec_ogp_module()
  196. {
  197. set_time_limit(0);
  198. $baseDir = str_replace( "modules" . DIRECTORY_SEPARATOR . $_GET['m'],"",dirname(__FILE__) );
  199. define('DATA_PATH', realpath('modules/'.$_GET['m'].'/') . DIRECTORY_SEPARATOR . "data" . DIRECTORY_SEPARATOR);
  200. if(!file_exists(DATA_PATH))
  201. {
  202. if(!mkdir(DATA_PATH))
  203. {
  204. print_failure("Need create folder: " . DATA_PATH . ' <br>But ' . dirname(DATA_PATH) . ' is not writable.<br>The command: <pre>chmod -R ' . dirname(DATA_PATH) . '</pre> would fix it.');
  205. return;
  206. }
  207. $back_compatibility = [ 'Util',
  208. 'RCON',
  209. 'DSi',
  210. 'Cron',
  211. 'LGSL_with_Img_Mod',
  212. 'Simple-billing',
  213. 'Support',
  214. 'TeamSpeak3',
  215. 'DarkNature',
  216. 'expand-soft',
  217. 'Katiuska',
  218. 'mobile',
  219. 'Light',
  220. 'Silver',
  221. 'Soft',
  222. 'Uprise' ];
  223. $installed = rglob('*/*/install.nfo');
  224. foreach($installed as $nfo)
  225. {
  226. $nfo_new = preg_replace('#^([m|t]{1})(odule|heme){1}s/([^?/]+)/install.nfo#','\3',$nfo);
  227. #echo $nfo_new.'<br>';
  228. $matches = preg_grep('#'.preg_quote($nfo_new).'#i',$back_compatibility);
  229. sort($matches);
  230. if($nfo_new == 'fastdl')
  231. $matches[0] = 'Fast_Download';
  232. if(isset($matches[0]))
  233. {
  234. #echo DATA_PATH.$matches[0].'.nfo<br>';
  235. file_put_contents(DATA_PATH.$matches[0].'.nfo', file_get_contents($nfo));
  236. }
  237. unlink($nfo);
  238. }
  239. }
  240. #return;
  241. define('REPO_FILE', DATA_PATH . "repos");
  242. define('URL', 'https://api.github.com/orgs/OpenGamePanel/repos'); // Returns detailed information of all repositories, and urls for more detailed informations about. Nice API GitHub! :)
  243. if(!file_exists(REPO_FILE) or isset($_GET['searchForUpdates']) or isset($_POST['update']))
  244. {
  245. # Without this $context the file_get_contents function was returning HTTP/1.0 403 Forbidden
  246. # Thanks: https://github.com/philsturgeon/codeigniter-oauth2/issues/57#issuecomment-29306192
  247. $options = array('http' => array('user_agent'=> $_SERVER['HTTP_USER_AGENT']));
  248. $context = stream_context_create($options);
  249. $response = file_get_contents(URL, false, $context);
  250. file_put_contents(REPO_FILE,$response);
  251. }
  252. else
  253. {
  254. $response = file_get_contents(REPO_FILE);
  255. }
  256. # Converting json string to array
  257. # http://php.net/manual/es/function.json-decode.php mixed json_decode ( string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0 ]]] )
  258. # *options - Bitmask of JSON decode options. Currently only JSON_BIGINT_AS_STRING is supported (default is to cast large integers as floats)
  259. $repos_info_array = json_decode($response, true);
  260. # Checking for contents while debbuging
  261. /* echo "<xmp>";
  262. print_r($repos_info_array);
  263. echo "</xmp>"; */
  264. if(isset($_POST['remove']))
  265. {
  266. $install_nfo = DATA_PATH . str_replace(' ','_',$_POST['folder']) . ".nfo";
  267. if( file_exists($install_nfo) )
  268. {
  269. $lines = file($install_nfo);
  270. unset($lines[0]);// timestamp
  271. unset($lines[1]);// nfo file
  272. usort($lines, "deeperPathFirst");
  273. foreach($lines as $file)
  274. {
  275. $file = trim($file);
  276. if(file_exists($file))
  277. {
  278. unlink($file);
  279. $parent_directory = dirname($file);
  280. while(count(scandir($parent_directory)) == 2)
  281. {
  282. if(realpath($parent_directory) == realpath($baseDir))
  283. break;
  284. if(is_writable($parent_directory))
  285. rmdir($parent_directory);
  286. else
  287. break;
  288. $parent_directory = dirname($parent_directory);
  289. }
  290. }
  291. }
  292. unlink($install_nfo);
  293. }
  294. return;
  295. }
  296. $m = 0;
  297. $modules = array();
  298. $t = 0;
  299. $themes = array();
  300. foreach($repos_info_array as $key => $repository)
  301. {
  302. if(preg_match('/^(OGP-Website|OGP-Agent-Linux|OGP-Agent-Windows)$/',$repository['name']))
  303. continue;
  304. $REMOTE_REPO_FILE = 'https://github.com/OpenGamePanel/'.$repository['name'].'/commits/master.atom';
  305. $LOCAL_REPO_FILE = DATA_PATH . $repository['name'] . '.atom';
  306. if(!file_exists($LOCAL_REPO_FILE) OR (isset($_GET['searchForUpdates']) and $_GET['searchForUpdates'] == $repository['name']) OR isset($_POST['update']))
  307. {
  308. $used_file = $REMOTE_REPO_FILE;
  309. $contents = file_get_contents($used_file);
  310. if(file_put_contents($LOCAL_REPO_FILE, $contents))
  311. touch($LOCAL_REPO_FILE);
  312. }
  313. else
  314. {
  315. $used_file = $LOCAL_REPO_FILE;
  316. $contents = file_get_contents($used_file);
  317. }
  318. if( ! $contents )
  319. {
  320. print_failure('Unable to get contents from : ' . $used_file);
  321. continue;
  322. }
  323. $feedXml = new SimpleXMLElement($contents, LIBXML_NOCDATA);
  324. $seed = basename( (string) $feedXml->entry[0]->link['href'] );
  325. /* echo "<xmp>";
  326. print_r($feedXml);
  327. echo "</xmp>"; */
  328. if($seed)
  329. {
  330. if(preg_match("/^Module-/",$repository['name']))
  331. {
  332. $module_title = preg_replace(array("/^Module-/i","/_/"),array(""," "),$repository['name']);
  333. $modules[$m]['title'] = $module_title;
  334. $modules[$m]['reponame'] = $repository['name'];
  335. $modules[$m]['file'] = $seed.'.zip';
  336. $modules[$m]['link'] = 'https://github.com/OpenGamePanel/'.$repository['name'].'/archive/'.$seed.'.zip';
  337. $modules[$m]['date'] = (string) $feedXml->entry[0]->updated;
  338. $modules[$m]['timestamp'] = strtotime((string) $feedXml->entry[0]->updated);
  339. $modules[$m]['remove_path'] = $repository['name']."-".$seed;
  340. $m++;
  341. }
  342. if(preg_match("/^Theme-/",$repository['name']))
  343. {
  344. $theme_title = preg_replace("/Theme-/i","",$repository['name']);
  345. $themes[$t]['title'] = $theme_title;
  346. $themes[$t]['reponame'] = $repository['name'];
  347. $themes[$t]['file'] = $seed.'.zip';
  348. $themes[$t]['link'] = 'https://github.com/OpenGamePanel/'.$repository['name'].'/archive/'.$seed.'.zip';
  349. $themes[$t]['date'] = (string) $feedXml->entry[0]->updated;
  350. $themes[$t]['timestamp'] = strtotime((string) $feedXml->entry[0]->updated);
  351. $themes[$t]['remove_path'] = $repository['name']."-".$seed;
  352. $t++;
  353. }
  354. }
  355. }
  356. global $db;
  357. $installed_modules = $db->getInstalledModules();
  358. if(isset($_POST['update']))
  359. {
  360. $baseDir = str_replace( "modules" . DIRECTORY_SEPARATOR . $_GET['m'],"",dirname(__FILE__) );
  361. $uMF = array();
  362. $tmpdir = get_temp_dir(dirname(__FILE__));
  363. if( !is_writable( $tmpdir ) )
  364. {
  365. echo get_lang_f('temp_folder_not_writable', $tmpdir);
  366. return;
  367. }
  368. foreach($_POST as $key => $value)
  369. {
  370. if($key == 'update')continue;
  371. if($key == 'module')
  372. {
  373. foreach($value as $m)
  374. {
  375. if(installUpdate($modules[$m], $baseDir))
  376. {
  377. $install_nfo = DATA_PATH . str_replace(' ','_',$modules[$m]['title']) . ".nfo";
  378. $nfo = file_get_contents($install_nfo);
  379. $modules_dir_preg = preg_quote(realpath('modules') . DIRECTORY_SEPARATOR);
  380. $modulephp_preg = preg_quote(DIRECTORY_SEPARATOR . 'module.php');
  381. $preg = '#'.$modules_dir_preg.'(.*)'.$modulephp_preg.'#';
  382. if(preg_match($preg, $nfo, $matches))
  383. $uMF[] = $matches[1];
  384. }
  385. }
  386. }
  387. if($key == 'theme')
  388. {
  389. foreach($value as $t)
  390. installUpdate($themes[$t], $baseDir);
  391. }
  392. }
  393. if(isset($_POST['module']))
  394. {
  395. foreach ( $installed_modules as $installed_module )
  396. {
  397. if(in_array($installed_module['folder'],$uMF))
  398. {
  399. update_module($db,$installed_module['id'],$installed_module['folder']);
  400. echo "\n";
  401. }
  402. }
  403. }
  404. return;
  405. }
  406. echo "<h2>".extras."</h2>";
  407. echo "<table style=\"width:100%;\">";
  408. echo "<tr><td style=\"width:50%;\">";
  409. # MODULES
  410. echo "<div class=\"dragbox bloc rounded\" style=\"margin:1%;\">".
  411. "<h4>".extra_modules."</h4>".
  412. "<div class=\"dragbox-content\" >";
  413. foreach ( $installed_modules as $installed_module )
  414. {
  415. $folder = $installed_module['folder'];
  416. $installed_modules_by_folder[$folder] = $installed_module['id'];
  417. }
  418. foreach($modules as $key => $module)
  419. {
  420. $local_repo_file = DATA_PATH . $module['reponame'] . '.atom';
  421. $install_nfo = DATA_PATH . str_replace(' ','_',$module['title']) . ".nfo";
  422. $on_disk = file_exists($install_nfo);
  423. $is_old = $on_disk && (strtotime('+1 hour', filemtime($local_repo_file)) <= time());
  424. //echo $install_nfo;
  425. $folder = str_replace(' ','_',strtolower($module['title']));
  426. $installed = array_key_exists($folder,$installed_modules_by_folder);
  427. $installed_str = $on_disk ? $installed ? "<a class='uninstall' style='color:blue;' data-module-folder='$folder' data-module-id='".
  428. $installed_modules_by_folder[$folder]."' href='#uninstall_$folder' >".uninstall."</a>" :
  429. "<a class='install' style='color:blue;' data-module-folder='$folder' href='#install_$folder' >".install."</a> - ".
  430. "<a class='remove' style='color:red;' data-module-folder='$module[title]' data-remove-mode='modules' href='#remove_$folder' >".remove."</a>" :
  431. "<b style='color:red;' >".not_installed."</b>";
  432. $uptodate = FALSE;
  433. if($on_disk)
  434. {
  435. $install_nfo = file_get_contents($install_nfo);
  436. list($timestamp, $files) = explode("\n", $install_nfo);
  437. $uptodate = ($timestamp == $module['timestamp']) ? TRUE : FALSE;
  438. }
  439. $updated_str = $on_disk ?
  440. $uptodate ?
  441. $is_old ? " - <a class='search' style='color:brown;' href='?m=".$_GET['m']."&searchForUpdates=".$module['reponame']."' >".search_for_updates."</a>" :
  442. " - <b style='color:green;' >".uptodate."</b>" :
  443. " - <b style='color:orange;' >".update_available."</b> (".$module['date'].")" :
  444. "";
  445. $disabled = $uptodate ? "disabled=disabled" : "";
  446. echo '<input type="checkbox" name="module" value="'.$key."\" $disabled>";
  447. echo '<b>'.$module['title']."</b> - $installed_str$updated_str <span id='loading' class='$folder' ></span><br>";
  448. }
  449. echo "</div></td><td></div>";
  450. # THEMES
  451. echo "<div class=\"dragbox bloc rounded\" style=\"margin:1%;\">".
  452. "<h4>".extra_themes."</h4>".
  453. "<div class=\"dragbox-content\" >";
  454. foreach($themes as $key => $theme)
  455. {
  456. $local_repo_file = DATA_PATH . $theme['reponame'] . '.atom';
  457. $install_nfo = DATA_PATH . str_replace(' ','_',$theme['title']) . ".nfo";
  458. $on_disk = file_exists($install_nfo);
  459. $is_old = $on_disk && (strtotime('+1 hour', filemtime($local_repo_file)) <= time());
  460. $installed_str = $on_disk ? "<b style='color:green;' >".installed."</b> - ".
  461. "<a class='remove' style='color:red;' data-module-folder='$theme[title]' data-remove-mode='themes' href='#remove_$folder' >".remove."</a>":
  462. "<b style='color:red;' >".not_installed."</b>";
  463. $uptodate = FALSE;
  464. if($on_disk)
  465. {
  466. $install_nfo = file_get_contents($install_nfo);
  467. list($timestamp, $files) = explode("\n", $install_nfo);
  468. $uptodate = ($timestamp == $theme['timestamp']) ? TRUE : FALSE;
  469. }
  470. $updated_str = $on_disk ?
  471. $uptodate ?
  472. $is_old ? " - <a class='search' style='color:brown;' href='?m=".$_GET['m']."&searchForUpdates=".$theme['reponame']."' >".search_for_updates."</a>" :
  473. " - <b style='color:green;' >".uptodate."</b>" :
  474. " - <b style='color:orange;' >".update_available."</b> (".$theme['date'].")" :
  475. "";
  476. $disabled = $uptodate ? "disabled=disabled" : "";
  477. echo '<input type="checkbox" name="theme" value="'.$key."\" $disabled>";
  478. echo '<b>'.$theme['title']."</b> - $installed_str$updated_str<br>";
  479. }
  480. echo "</div></div></td></tr>".
  481. "<tr><td colspan=2 ><span id=updateButton ><button name=update >".download_update."</button></span></td></tr></table><div id=resp ></div>";
  482. echo "<div id='dialog".
  483. "' data-uninstalling_module_dataloss='".uninstalling_module_dataloss.
  484. "' data-are_you_sure='".are_you_sure.
  485. "' data-remove_files_for='".remove_files_for.
  486. "' data-confirm='".confirm.
  487. "' data-cancel='".cancel.
  488. "' ></div>";
  489. }
  490. ?>