Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ Marketplace plugin `org.xoops.plugin.support`. Versions 0.4.0 to 0.7.2 below are
rebuild; it ships as **1.0.0 Alpha 4**, the successor of the 1.0.0 Alpha 3 release of the previous code base
(https://github.com/XOOPS/phpstorm-plugin).

## 1.0.0-alpha.6 — 1.0.0 Alpha 6 — NOT RELEASED

- **HttpStatus** offers `http_response_code(404)` as a quick fix only when every other `header()` call of the file
sends a plain `Name: value` field other than `Status:` and the file has no `http_response_code(n)` setter.
`http_response_code()` does not replace a status line that an earlier `header('HTTP/…')` or `header('Status: …')`
call stored in the same request, and some server APIs then send that earlier status (seen with CGI, and for an
`HTTP/` line with PHP's built-in server). A file with a second status line, a setter, or a computed `header()`
argument is reported without a fix. The description states that the status code sent by the replacement is the
same.

## 1.0.0-alpha.5 — 1.0.0 Alpha 5 — Marketplace release 2026-10-26

False positives from a run over three newbb trees (`280test1/htdocs/modules/02/01inspections`, 2026-10-26), each
Expand Down
9 changes: 8 additions & 1 deletion INSPECTIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,14 @@ without the tag (third-party MIT / GPL prose) is only noted, never rewritten.

### XoopsHttpStatus — Hard-coded HTTP status line
`header('HTTP/1.0 404 Not Found')` hard-codes the protocol version.
**Fix:** `http_response_code(404)`.
**Fix:** `http_response_code(404)`. The status code sent is the same (still 404); the protocol version is left to the
server and a custom reason phrase is not kept. One exception: `http_response_code()` does not replace a status line
that an earlier `header('HTTP/…')` or `header('Status: …')` call stored in the same request. On some server APIs
(seen with CGI, and for an `HTTP/` line with PHP's built-in server) that earlier status is then the one sent. The fix
is therefore offered only when every other `header()` call of the file sends a plain `Name: value` field other than
`Status:` and the file has no `http_response_code(n)` setter. A file with a second status line, a setter, or a
computed `header()` argument is reported without a fix: convert its status lines together. A status line set in
another file of the same request is not seen.

### XoopsUnlink — unlink() without is_file() check
`unlink($path)` with no `is_file()` / `file_exists()` on the same expression dominating it. A check in the enclosing
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.xoops.support.inspections

import com.intellij.codeInspection.LocalQuickFix
import com.intellij.codeInspection.ProblemDescriptor
import com.intellij.codeInspection.ProblemHighlightType
import com.intellij.codeInspection.ProblemsHolder
Expand Down Expand Up @@ -67,27 +68,48 @@ class AddAllowedClassesFix : XoopsFix() {
}
}

/** `header('HTTP/1.0 404 Not Found')` → `http_response_code(404)`. */
/**
* `header('HTTP/1.0 404 Not Found')` → `http_response_code(404)`.
*
* `http_response_code()` sets the code but keeps a status line an earlier `header('HTTP/…')` stored, and CGI and
* the built-in server then send that line. The fix is offered only where no other `header()` call of the file
* can store one.
*/
class XoopsHttpStatusInspection : XoopsInspection() {
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
return object : PhpElementVisitor() {
override fun visitPhpFunctionCall(reference: FunctionReference) {
if (!Php.isCall(reference, "header") || reference is MethodReference || skipConvention(reference.containingFile)) return
if (reference.parameters.size != 1) return
val code = statusOf(reference) ?: return
holder.registerProblem(
reference,
"XOOPS: hard-coded HTTP status line — use http_response_code($code)",
ProblemHighlightType.WEAK_WARNING,
HttpResponseCodeFix(),
)
val fixable = onlyStatusLine(reference)
val advice = if (fixable) "use http_response_code($code)"
else "this file sets the status elsewhere too and http_response_code($code) does not replace a stored status line; convert them together"
val fixes = if (fixable) arrayOf<LocalQuickFix>(HttpResponseCodeFix()) else emptyArray()
holder.registerProblem(reference, "XOOPS: hard-coded HTTP status line — $advice", ProblemHighlightType.WEAK_WARNING, *fixes)
}
}
}

companion object {
private val status = Regex("""(?i)^HTTP/\d(?:\.\d)?\s+(\d{3})\b""")
/**
* `Name: value`, the one argument shape that provably is not a status line (`"$proto 403"` is not provable).
* `Status:` is the CGI spelling of one and outlives `http_response_code()` the same way.
*/
private val field = Regex("""^(?!(?i:status)\s*:)[A-Za-z][A-Za-z0-9-]*\s*:""")
fun statusOf(call: FunctionReference): String? = Php.firstStringArg(call)?.let { status.find(it)?.groupValues?.get(1) }

/**
* Every other `header()` call of the file sends a plain `Name: value` field, and nothing sets the code with
* `http_response_code(n)`: after `header('HTTP/1.0 404 …')` that setter is ignored, after the fix it would win.
*/
fun onlyStatusLine(call: FunctionReference): Boolean =
Php.calls(call.containingFile, "header", "http_response_code").all { other ->
other === call || other is MethodReference ||
if (Php.isCall(other, "header")) Php.firstStringArg(other)?.let { field.containsMatchIn(it) } == true
else other.parameters.isEmpty()
}
}
}

Expand Down
12 changes: 11 additions & 1 deletion src/main/resources/inspectionDescriptions/XoopsHttpStatus.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@
<head><title>XOOPS: Http Status</title></head>
<body>
<code>header('HTTP/1.0 404 Not Found')</code> hard-codes the protocol version. Use
<code>http_response_code(404)</code>.
<code>http_response_code(404)</code>. The status code sent is the same (still 404); the protocol version is left
to the server and a custom reason phrase is not kept.
<p>One exception: <code>http_response_code()</code> does not replace a status line that an earlier
<code>header('HTTP/&hellip;')</code> or <code>header('Status: &hellip;')</code> call stored in the same request.
On some server APIs (seen with CGI, and for an <code>HTTP/</code> line with PHP's built-in server) that earlier
status is then the one sent.</p>
<p>The quick fix is therefore offered only when every other <code>header()</code> call of the file sends a plain
<code>Name: value</code> field other than <code>Status:</code> and the file has no
<code>http_response_code(n)</code> setter. A file with a second status line, a setter, or a computed
<code>header()</code> argument is reported without a fix: convert its status lines together. A status line set in another file of the
same request is not seen.</p>
</body>
</html>
25 changes: 25 additions & 0 deletions src/test/kotlin/org/xoops/support/XoopsQuickFixTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,31 @@ class XoopsQuickFixTest : BasePlatformTestCase() {
check(XoopsHttpStatusInspection(), "HttpStatus/status", "modules/demo/images/index.php", 1,
"Replace with http_response_code", "modules/demo/images/index.after.php")

/** `header('Content-Type: …')` calls beside the status line cannot store one: the fix stays. */
fun testHttpStatusLineBesidePlainFields() =
check(XoopsHttpStatusInspection(), "HttpStatus/guard", "modules/demo/include/feed.php", 1,
"Replace with http_response_code", "modules/demo/include/feed.after.php")

/** `http_response_code()` keeps an earlier `header('HTTP/1.1 200 OK')` line, so neither call gets the fix. */
fun testHttpStatusLineTwiceIsReportedWithoutFix() =
check(XoopsHttpStatusInspection(), "HttpStatus/twolines", "modules/demo/ajax.php", 2,
"Replace with http_response_code", "modules/demo/ajax.php", remaining = 2)

/** `header('Status: 403 Forbidden')` is the CGI status field and survives `http_response_code()` as well. */
fun testHttpStatusLineBesideStatusFieldIsReportedWithoutFix() =
check(XoopsHttpStatusInspection(), "HttpStatus/statusfield", "modules/demo/ajax.php", 1,
"Replace with http_response_code", "modules/demo/ajax.php", remaining = 1)

/** A later `http_response_code(500)` is ignored after the status line and would win after the fix. */
fun testHttpStatusLineBesideCodeSetterIsReportedWithoutFix() =
check(XoopsHttpStatusInspection(), "HttpStatus/latersetter", "modules/demo/ajax.php", 1,
"Replace with http_response_code", "modules/demo/ajax.php", remaining = 1)

/** A computed or interpolated `header()` argument may be a status line: reported without the fix. */
fun testHttpStatusLineBesideDynamicHeaderIsReportedWithoutFix() =
check(XoopsHttpStatusInspection(), "HttpStatus/dynamic", "modules/demo/ajax.php", 1,
"Replace with http_response_code", "modules/demo/ajax.php", remaining = 1)

fun testSetcookie() =
check(XoopsSetcookieInspection(), "Setcookie/cookie", "modules/demo/class/U.php", 1,
"Replace setcookie()", "modules/demo/class/U.after.php")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
require dirname(__DIR__, 2) . '/mainfile.php';
header($_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden');
header("$proto 500 Internal Server Error");
if (empty($items)) {
header('HTTP/1.0 404 Not Found');
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
if (!defined('XOOPS_ROOT_PATH')) {
http_response_code(404);
exit;
}
header('Content-Type: application/json');
header("Cache-Control: no-store");
echo json_encode($GLOBALS['xoopsConfig']['sitename']);
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
if (!defined('XOOPS_ROOT_PATH')) {
header('HTTP/1.0 404 Not Found');
exit;
}
header('Content-Type: application/json');
header("Cache-Control: no-store");
echo json_encode($GLOBALS['xoopsConfig']['sitename']);
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
require dirname(__DIR__, 2) . '/mainfile.php';
header('HTTP/1.0 404 Not Found');
if (empty($items)) {
http_response_code(500);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
require dirname(__DIR__, 2) . '/mainfile.php';
header('status: 403 Forbidden');
if (empty($items)) {
header('HTTP/1.0 404 Not Found');
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
require dirname(__DIR__, 2) . '/mainfile.php';
header('HTTP/1.1 200 OK');
if (empty($items)) {
header('HTTP/1.0 404 Not Found');
}
12 changes: 11 additions & 1 deletion tools/gen_descriptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,17 @@
""",
"XoopsHttpStatus": r"""
<code>header('HTTP/1.0 404 Not Found')</code> hard-codes the protocol version. Use
<code>http_response_code(404)</code>.
<code>http_response_code(404)</code>. The status code sent is the same (still 404); the protocol version is left
to the server and a custom reason phrase is not kept.
<p>One exception: <code>http_response_code()</code> does not replace a status line that an earlier
<code>header('HTTP/&hellip;')</code> or <code>header('Status: &hellip;')</code> call stored in the same request.
On some server APIs (seen with CGI, and for an <code>HTTP/</code> line with PHP's built-in server) that earlier
status is then the one sent.</p>
<p>The quick fix is therefore offered only when every other <code>header()</code> call of the file sends a plain
<code>Name: value</code> field other than <code>Status:</code> and the file has no
<code>http_response_code(n)</code> setter. A file with a second status line, a setter, or a computed
<code>header()</code> argument is reported without a fix: convert its status lines together. A status line set in another file of the
same request is not seen.</p>
""",
"XoopsIncludeOnceHeader": r"""
<p>Reports <code>include</code> or <code>require</code> of <code>header.php</code> / <code>footer.php</code>
Expand Down
Loading
Loading