index.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?PHP
  2. /**
  3. * Copyright 2021, 2024 5 Mode
  4. *
  5. * This file is part of Bugzilla PHP Wrapper.
  6. *
  7. * Bugzilla PHP Wrapper is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * Bugzilla PHP Wrapper is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with Bugzilla PHP Wrapper. If not, see <https://www.gnu.org/licenses/>.
  19. *
  20. * index.php
  21. *
  22. * Bugzilla PHP Wrapper.
  23. *
  24. * @author Daniele Bonini <my25mb@aol.com>
  25. * @copyrights (c) 2016, 2024, 5 Mode
  26. */
  27. // Variables and constants
  28. define('BZ_WR_TIMEZONE', "Europe/Rome");
  29. define('BZ_WR_WEBSERVER', "nginx");
  30. define('BZ_WR_COOKIE_PATH', "/");
  31. define('BZ_WR_COOKIE_DOMAIN', "bugs.5mode.com");
  32. // SCRIPT_NAME
  33. $f = filter_input(INPUT_GET, 'perl_script');
  34. if ($f === "" || $f === "/") {
  35. $f = "/index.cgi";
  36. }
  37. if (substr($f,0,1)!=="/") {
  38. $f = "/".$f;
  39. }
  40. // QUERY_STRING
  41. $s = filter_input(INPUT_SERVER, 'QUERY_STRING');
  42. $s = explode("perl_script=$f", $s)[1];
  43. // Rebuilding the $_POST parameters..
  44. $formData = $_POST;
  45. foreach($formData as $key=>$val) {
  46. if ($f!=="/index.cgi" && ($key==="Bugzilla_login" || $key==="Bugzilla_password" || $key ==="Bugzilla_login_token" || $key==="GoAheadAndLogIn")) {
  47. } else {
  48. $s .= "&$key=".urlencode($val);
  49. }
  50. }
  51. if (substr($s,0,1) === "&") {
  52. $s = substr($s,1);
  53. }
  54. // SERVER VARIABLES
  55. putenv("QUERY_STRING=$s");
  56. putenv("local_timezone=".BZ_WR_TIMEZONE);
  57. putenv("SERVER_SOFTWARE=".BZ_WR_WEBSERVER);
  58. putenv("SERVER_NAME=" . $_SERVER['SERVER_NAME']);
  59. putenv("REQUEST_METHOD=" . $_SERVER['REQUEST_METHOD']);
  60. putenv("REMOTE_ADDR=" . $_SERVER['REMOTE_ADDR']);
  61. // Rebuilding the rest of $_SERVER variables environment..
  62. $serverEnvs = $_SERVER;
  63. foreach($serverEnvs as $key=>$val) {
  64. if (($key !== "QUERY_STRING") && ($key !== "SERVER_SOFTWARE") && ($key !== "SERVER_NAME") && ($key !== "REQUEST_METHOD") && ($key !== "REMOTE_ADDR")) {
  65. putenv("$key=".$val);
  66. }
  67. }
  68. $output = [];
  69. $r = exec("perl -T " . __DIR__ . $f, $output);
  70. if ($f==="/buglist.cgi" && array_count_values($output)['</html>']>1) {
  71. foreach($output as &$row) {
  72. if ($row === "</html>") {
  73. $row = "#### BLANK ####";
  74. break;
  75. } else {
  76. $row = "#### BLANK ####";
  77. }
  78. }
  79. }
  80. if ($f==="/for_debugging.cgi") {
  81. header("Content-Type: plain/html");
  82. echo("<html><body>");
  83. print_r($output);
  84. echo("</body></html>");
  85. exit(0);
  86. } else {
  87. $docParsing = false;
  88. foreach($output as $row) {
  89. if ($row!=="#### BLANK ####") {
  90. if ($row === "<!DOCTYPE html>") {
  91. $docParsing = true;
  92. }
  93. if ($docParsing) {
  94. echo($row."\n");
  95. } else {
  96. // echo("header=$row<br>");
  97. if (mb_strpos(strtolower($row), "set-cookie: bugzilla_login=") !== false) {
  98. // Parsing for the UserID on the Cookie request
  99. $userID = explode("=", explode(";", $row)[0])[1];
  100. // Creating the login cookie..
  101. setcookie("Bugzilla_login", $userID, time() + 999999999, BZ_WR_COOKIE_PATH, BZ_WR_COOKIE_DOMAIN, true, true);
  102. } else {
  103. if ((mb_strpos($row, "--------- =") === false) && (mb_strpos($row, "WARNING:") === false)) {
  104. header($row);
  105. }
  106. }
  107. }
  108. }
  109. }
  110. }