index.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. $BZ_WR_COOKIE_PATH = "/";
  31. $BZ_WR_COOKIE_DOMAIN = "bugs.5mode.com";
  32. // SCRIPT_NAME
  33. $f = filter_input(INPUT_GET, 'perl_script')??"";
  34. $f = strip_tags($f);
  35. if ($f === "" || $f === "/") {
  36. $f = "/index.cgi";
  37. }
  38. if (substr($f,0,1)!=="/") {
  39. $f = "/".$f;
  40. }
  41. // QUERY_STRING
  42. $s = filter_input(INPUT_SERVER, 'QUERY_STRING');
  43. $s = explode("perl_script=$f", $s)[1];
  44. // Rebuilding the $_POST parameters..
  45. $formData = $_POST;
  46. foreach($formData as $key=>$val) {
  47. if ($f!=="/index.cgi" && ($key==="Bugzilla_login" || $key==="Bugzilla_password" || $key ==="Bugzilla_login_token" || $key==="GoAheadAndLogIn")) {
  48. } else {
  49. $s .= "&$key=".urlencode($val);
  50. }
  51. }
  52. if (substr($s,0,1) === "&") {
  53. $s = substr($s,1);
  54. }
  55. // SERVER VARIABLES
  56. putenv("QUERY_STRING=$s");
  57. putenv("local_timezone=".BZ_WR_TIMEZONE);
  58. putenv("SERVER_SOFTWARE=".BZ_WR_WEBSERVER);
  59. putenv("SERVER_NAME=" . $_SERVER['SERVER_NAME']);
  60. putenv("REQUEST_METHOD=" . $_SERVER['REQUEST_METHOD']);
  61. putenv("REMOTE_ADDR=" . $_SERVER['REMOTE_ADDR']);
  62. // Rebuilding the rest of $_SERVER variables environment..
  63. $serverEnvs = $_SERVER;
  64. foreach($serverEnvs as $key=>$val) {
  65. if (($key !== "QUERY_STRING") && ($key !== "SERVER_SOFTWARE") && ($key !== "SERVER_NAME") && ($key !== "REQUEST_METHOD") && ($key !== "REMOTE_ADDR")) {
  66. putenv("$key=".$val);
  67. }
  68. }
  69. $output = [];
  70. $r = exec("perl -T " . __DIR__ . $f, $output);
  71. if ($f==="/buglist.cgi" && array_count_values($output)['</html>']>1) {
  72. foreach($output as &$row) {
  73. if ($row === "</html>") {
  74. $row = "#### BLANK ####";
  75. break;
  76. } else {
  77. $row = "#### BLANK ####";
  78. }
  79. }
  80. }
  81. if ($f==="/for_debugging.cgi") {
  82. header("Content-Type: plain/html");
  83. echo("<html><body>");
  84. print_r($output);
  85. echo("</body></html>");
  86. exit(0);
  87. } else {
  88. $docParsing = false;
  89. foreach($output as $row) {
  90. if ($row!=="#### BLANK ####") {
  91. if ($row === "<!DOCTYPE html>") {
  92. $docParsing = true;
  93. }
  94. if ($docParsing) {
  95. echo($row."\n");
  96. } else {
  97. //echo("header=$row<br>");
  98. if (mb_strpos(strtolower($row), "set-cookie: bugzilla_login=") !== false) {
  99. // Parsing for the UserID on the Cookie request
  100. $userID = explode("=", explode(";", $row)[0])[1];
  101. // Creating the login cookie..
  102. $ret=@setcookie("Bugzilla_login", $userID, time() + 999999999, $BZ_WR_COOKIE_PATH, $BZ_WR_COOKIE_DOMAIN, true, true);
  103. //echo("ret=$ret<br>");
  104. if ($ret) {
  105. $_COOKIE["Bugzilla_login"] = $userID;
  106. }
  107. } else {
  108. if ((mb_strpos($row, "--------- =") === false) && (mb_strpos($row, "WARNING:") === false)) {
  109. header($row);
  110. }
  111. }
  112. }
  113. }
  114. }
  115. }