/customcss.php

https://gitlab.com/kernapps/kern-es-showdown · PHP · 103 lines · 76 code · 16 blank · 11 comment · 21 complexity · fe2e77279c1fa0b2ce324cb7963c611b MD5 · raw file

  1. <?php
  2. include '../pokemonshowdown.com/config/servers.inc.php';
  3. $server = @$_REQUEST['server'];
  4. if ($server === 'showdown' || $server === 'smogtours') die();
  5. if (empty($PokemonServers[$server])) {
  6. header('Content-Type: text/plain; charset=utf-8');
  7. die('server not found');
  8. }
  9. $invalidate = isset($_REQUEST['invalidate']);
  10. if (!$invalidate) header('Content-Type: text/css'); // the CSS file should specify a charset
  11. $serverdata =& $PokemonServers[$server];
  12. $customcssuri = @$serverdata['customcss'];
  13. if (empty($customcssuri)) {
  14. $protocol = ($serverdata['port'] === 443) ? 'https' : 'http';
  15. $customcssuri = $protocol . '://'.$serverdata['server'].':'.$serverdata['port'].'/custom.css';
  16. }
  17. // No need to sanitise $server because it should be safe already.
  18. $cssfile = '../pokemonshowdown.com/config/customcss/' . $server . '.css';
  19. $lastmodified = @filemtime($cssfile);
  20. $timenow = time();
  21. $expiration = ($lastmodified ? $lastmodified : $timenow) + 3600;
  22. header('Expires: ' . gmdate('D, d M Y H:i:s T', $expiration));
  23. // echo '/* ', $customcssuri, ' */';
  24. if (!$invalidate && $lastmodified && (($timenow - $lastmodified) < 3600)) {
  25. // Don't check for modifications more than once an hour.
  26. readfile($cssfile);
  27. die();
  28. }
  29. $curl = curl_init($customcssuri);
  30. if ($lastmodified && !$invalidate) {
  31. curl_setopt($curl, CURLOPT_HTTPHEADER, array(
  32. 'If-Modified-Since: ' . gmdate('D, d M Y H:i:s T', $lastmodified),
  33. 'User-Agent: PSCustomCSS/0.1 (server=' . $server . ($invalidate ? '; invalidate=1' : '') . ')',
  34. // 'X-Forwarded-For: ' . @$_SERVER['HTTP_X_FORWARDED_FOR'],
  35. ));
  36. } else {
  37. curl_setopt($curl, CURLOPT_HTTPHEADER, array(
  38. 'User-Agent: PSCustomCSS/0.1 (server=' . $server . ($invalidate ? '; invalidate=1' : '') . ')',
  39. // 'X-Forwarded-For: ' . @$_SERVER['HTTP_X_FORWARDED_FOR'],
  40. ));
  41. }
  42. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
  43. curl_setopt($curl, CURLOPT_MAXREDIRS, 5);
  44. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  45. $curlret = curl_exec($curl);
  46. if ($curlret) {
  47. $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  48. if ($code === 200) {
  49. // Sanitise the CSS.
  50. require '../pokemonshowdown.com/lib/htmlpurifier/HTMLPurifier.auto.php';
  51. require '../pokemonshowdown.com/lib/csstidy/class.csstidy.php';
  52. $config = HTMLPurifier_Config::createDefault();
  53. $config->set('Filter.ExtractStyleBlocks', true);
  54. $config->set('CSS.Proprietary', true);
  55. $config->set('CSS.AllowImportant', true);
  56. $config->set('CSS.AllowTricky', true);
  57. $level = error_reporting(E_ALL & ~E_STRICT);
  58. // $purifier = new HTMLPurifier($config);
  59. // $html = $purifier->purify('<style>' . $curlret . '</style>');
  60. // error_reporting($level);
  61. // list($outputcss) = $purifier->context->get('StyleBlocks');
  62. $context = new HTMLPurifier_Context();
  63. $filter = new HTMLPurifier_Filter_ExtractStyleBlocks();
  64. $outputcss = $filter->cleanCSS($curlret, $config, $context);
  65. file_put_contents($cssfile, $outputcss);
  66. if (!$invalidate) echo $outputcss;
  67. } else {
  68. // Either no modifications (status: 304) or an error condition.
  69. if ($invalidate) die('Error: custom CSS file not found');
  70. if ($lastmodified) readfile($cssfile);
  71. }
  72. touch($cssfile, $timenow); // Don't check again for an hour.
  73. } else if (file_exists($cssfile)) {
  74. if ($invalidate) die('Error: custom CSS file not found');
  75. readfile($cssfile);
  76. }
  77. curl_close($curl);
  78. if ($invalidate) {
  79. ?>
  80. <p>
  81. Done: <?= htmlspecialchars($customcssuri) ?> was reloaded.
  82. </p>
  83. <p>
  84. <a href="http://pokemonshowdown.com/servers/<?= $server ?>">Back to server management</a>
  85. </p>
  86. <?php
  87. }