mirror of
https://github.com/ZoneMinder/zoneminder.git
synced 2026-10-03 16:05:24 -04:00
auth.php ended in a 130-line block at file scope, so merely including the file authenticated the current request: it read $_REQUEST, opened a session, queried the database, and on a login could rewrite the user's stored password hash and populate $_SESSION. Any caller that wanted one of the functions in the file got all of that as a side effect, and the order of includes decided when it ran. HostController requires auth.php twice purely to reach generateAuthHash() and validateToken(). Move the block into zm_authenticate_request() and call it explicitly from the two places that want it, web/index.php and AppController::beforeFilter(). The function returns the ZM\User or null and still sets the global $user, so the views, ajax handlers and API controllers that read that global are unaffected. HostController now gets only the function definitions from its requires, which is all it ever wanted. Inside a function the five `unset($user)` calls would drop the local binding and leave the global set, so they become `$user = null` - the idiom the rest of the file already uses for this, and one that keeps isset($user) false for the gate at index.php:255. The block's other locals ($ret, $username, $password, $sql) no longer leak into the caller's scope, which in beforeFilter() means they can no longer collide with the variables of the same name it assigns just after. The body is otherwise unchanged; `git diff -w` shows only the wrapper, those five assignments and the return. Tests: tests/php/test_auth_no_include_side_effects.php tokenises auth.php and asserts nothing executes at file scope, with a fixture check so a broken detector cannot pass vacuously. 4 assertions, all pass. Verified it reports the pre-refactor file's file-scope block, so it would have caught this. Not covered by tests: the login, logout, auth-hash and API token flows this touches. auth.php cannot be included without a database (User.php pulls in database.php, which connects at include time), so the check is structural. Needs manual testing on an installed tree before merging. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01477mR97vfnK6zczbHgzq6T
ZoneMinder API
This is the ZoneMinder API. It should be, for now, installed under the webroot e.g. /api.
app/Config/database.php.default must be configured and copied to app/Config/database.php
In addition, Security.salt and Security.cipherSeed in app/Config/core.php should be changed.
The API can run on a dedicated / separate instance, so long as it can access the database as configured in app/Config/database.php