Commit Graph
94 Commits
Author SHA1 Message Date
Isaac ConnorandClaude Opus 5 9c06eb6ba2 refactor: connect to the database on first use, not on include
database.php called dbConnect() at file scope, so including it opened a socket,
and on failure rendered views/no_database_connection.php and exit()ed from
inside a library include. Every model in web/includes requires this file, so
merely loading a class did both.

Connect on first use instead. $dbConn becomes tri-state - false for "not
attempted", null for "attempt failed", a PDO for connected - and two accessors
sit on top of it:

  zmDbConn()        opens if needed; on failure renders the error view and
                    stops, which is what the include used to do, just at the
                    point a query is actually attempted.
  zmDbConnOrNull()  opens if needed but returns null instead of ending the
                    request, for callers with a fallback.

dbQuery() is the funnel every fetch helper goes through, so routing it plus
dbEscape(), dbError() and dbInsertId() through the accessors covers the library.
The five callers that reached for the raw global are updated: config.php.in,
Event.php and ajax/console.php need a connection and take zmDbConn(); logger.php
takes zmDbConnOrNull() and falls through to its error_log target, so a logging
call can no longer end the request or open a connection by itself.

ZMSessionHandler captured $dbConn in its constructor. It is constructed while
session.php is being included, before anything has needed the database, so with
a lazy connection that captured false. It now resolves per call and its methods
return "no session" rather than dereferencing a bool.

Two smaller fixes fall out. The error view was included by a relative path that
only resolved when the cwd was web/, so it never worked for requests served out
of web/api/; it is now anchored with __DIR__. And dbDisconnect() set $dbConn to
null, which in the new tri-state means "connecting failed" and would send the
next query to the error page; it sets false so a later query can reconnect.
Nothing calls dbDisconnect() today.

This does NOT make database.php includable without a database. It requires
logger.php, which requires config.php, which reads ZoneMinder's configuration
out of the Config table at include time. Until that cycle is broken the
connection still happens during bootstrap, just from config.php rather than from
here.

Tests: tests/php/test_database_lazy_connect.php, 7 assertions, all pass. It
tokenises database.php and asserts nothing runs at include time, that dbQuery()
goes through the accessor, and that only the connection plumbing touches the
global. Verified it reports the pre-refactor file's `if ( !dbConnect() )` - an
earlier version of the check skipped tokens inside parentheses and so passed on
exactly the code it exists to reject.

Not covered by tests: behaviour when the database is genuinely unreachable, and
the session handler against a live database. Needs manual testing on an
installed tree, including stopping mysql to confirm the error view still renders
for both a web request and an API request.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01477mR97vfnK6zczbHgzq6T
2026-08-17 20:52:10 -04:00
Isaac ConnorandClaude Opus 4.7 e792020bb4 feat: support backslash line continuation in conf.d parsers fixes #4943
The C++ and PHP parsers for zm.conf and /etc/zm/conf.d/*.conf used fgets
with a 512-byte buffer, silently truncating any line longer than that.
There was also no way to split a value across multiple lines, so editors
hit the cap with no workaround.

Accept a trailing backslash (with optional whitespace before the newline)
as a line-continuation marker. Leading whitespace on continuation lines
is stripped so users can indent for readability without it leaking into
the value. Three parsers all read the same files and must agree:

- src/zm_config.cpp: switch to std::ifstream + std::getline so a single
  physical line is no longer capped at 512 bytes, then join continuation
  lines before running the existing pointer-based parser
- scripts/ZoneMinder/lib/ZoneMinder/Config.pm.in: accumulate a logical
  line across trailing-backslash physical lines
- web/includes/config.php.in: drop the 512-byte fgets cap and accumulate
  in the same way

tests/zm_config.cpp covers three cases against the C++ parser: a
three-segment continuation joins to "firstsecondthird" with leading
whitespace stripped, a bare backslash inside a value is preserved
(C:\Users\zm), and a 1500-byte single line survives intact.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-27 12:13:49 -04:00
Isaac Connor 18ef01bd8d Increase potential config line size. HTML snippets can easily be larger than 256 2026-03-12 09:28:37 -04:00
Isaac Connor 68f91acf10 Remove zm_configvals. Just use zm_config. Move code into loadConfig. 2025-12-22 13:17:01 -05:00
Isaac Connor 3b0feba15f Add db config entries to zm_configvals 2025-12-19 16:48:44 -05:00
Isaac Connor 4b5d7c11f0 Log wwhen defined server is not found 2025-11-04 10:26:56 -05:00
Isaac Connor cd8f3120c9 Rename global config to zm_config as we use config elsewhere and it conflicts 2025-06-17 09:45:44 -05:00
Isaac Connor a8c658aa50 Ensure that Servers is global. 2024-11-06 10:38:24 -05:00
Isaac Connor 3e1c5573ec Don't warn about no locale set, that is normal 2024-10-25 11:02:49 -04:00
Isaac Connor f02121fcc2 Check for validity of locale. Handle fatal crash when setting datetiemformatter when an invalid locale is used. Fixes #4179 2024-10-25 10:41:56 -04:00
Isaac Connor 9555455c5d Add defines for ANALYSING_NONE and ANALYSING_ALWAYS 2024-10-01 17:14:01 -04:00
Isaac Connor 34e1512916 Add TRIGGER defines 2024-10-01 12:18:18 -04:00
Isaac Connor d216c4cf47 Remove STATE_TAPE 2024-09-24 17:26:55 -04:00
Isaac Connor 799dcd5f51 Default thisServer to a default server object. 2024-02-22 12:52:39 -05:00
Isaac Connor 57e9cc2b16 In the case where locale = System... properly set locale for the Date*FOrmatter classes 2024-02-05 19:06:16 -05:00
Isaac Connor ebadc7a07b GIve default user the create permission 2024-01-14 08:41:12 -05:00
Isaac Connor a402217456 Correct state values so that Unknown starts at 0 2024-01-11 17:19:29 -05:00
Isaac Connor 36bdc3f8a9 Revert "Remove STATE_UNKNOWN"
This reverts commit 66e7179242.
2024-01-11 17:16:58 -05:00
Isaac Connor 66e7179242 Remove STATE_UNKNOWN 2024-01-11 17:13:09 -05:00
Isaac Connor 5887a1e853 State Unknown starts at -1 2024-01-03 16:52:44 -05:00
Isaac Connor 3ce96005af Load Type when loading Config 2023-11-20 13:26:28 -05:00
Isaac Connor d9ae4f8e04 Do not need to make servers global in this context. They are by default.| 2023-11-16 10:34:43 -05:00
Isaac Connor d54171c21c Be a little more verbose and robust when things like permission errors break config loading 2023-10-26 18:37:15 -04:00
Isaac Connor c02cbd86f8 Merge branch 'master' of github.com:ZoneMinder/zoneminder 2023-05-31 09:16:32 -04:00
Isaac Connor 8bd3e5a3b1 Can't use ZM\Logger functions in config as it loads before logger.php. 2023-05-31 09:09:17 -04:00
Isaac Connor eab4d95e81 Always return an array from process_configfile instead of false on failure 2023-05-30 20:47:05 -04:00
Isaac Connor 593c7ac902 Handle HomeView not being set, and default it so that it is set 2023-03-24 12:53:49 -04:00
Isaac Connor 8c35190b0a Move loading of global Servers array into config.php. 2023-01-03 13:16:03 -05:00
Isaac Connor 62b6171934 Only try to set TIMEZONE when loading dateTimeFormatter if it is set. Handle the exception when any of TIMEZONE or LOCALE are invalid. 2022-09-15 14:04:31 -04:00
ovargasp b01c75ab76 Modify config.php.in
Modifies config.php to use ZM_TIMEZONE if defined by user
2022-08-05 06:40:23 -06:00
Isaac Connor 56828f4d48 Add Id=0 for default user 2022-07-12 17:31:41 -04:00
Isaac Connor 4dbe66779a Implement CMD_ZOOMSTOP 2022-06-17 15:11:27 -04:00
Ingemārs af2f1cc6e6 Update config.php.in 2022-06-07 12:33:41 +03:00
Isaac Connor 4a6ad1f880 move dateFormatters to config. This is so that they get setup even if not using index.php. Like cakephp for example. 2022-06-02 20:13:37 -04:00
Isaac Connor eaa94ce370 update format string to date() format instead of strftime format 2022-06-01 15:48:50 -04:00
Isaac Connor 76f01647f1 define CMD_GET_IMAGE for completeness 2022-05-07 19:39:01 -04:00
Isaac Connor ce2d605b3d load Private as well as Name,Value from Config 2022-01-22 12:31:15 -05:00
Isaac Connor df0a0c7853 Add CMD_ANALYZE_ON and OFF 2022-01-07 13:50:40 -05:00
Isaac Connor 30e4d72c1d Implement CMD_MAXFPS 2021-12-16 16:36:12 -05:00
Isaac Connor a9ef5bf731 Add STATE_UNKNOWN and correct values of other states 2021-12-09 12:36:03 -05:00
Peter Keresztes Schmidt 6e96cbe14a Misc: Merge ZM_HAS_V4L with ZM_HAS_V4L2 2021-09-08 00:19:08 +02:00
Peter Keresztes Schmidt f2bee0704a LocalCamera: Remove support for V4L1
Kernel support for Video4Linux 1 was removed in 2.6.38 (March 2011). It's time to drop support on our side.
2021-05-31 23:29:29 +02:00
Isaac Connor 7e579fd851 Implement Snapshots user permission 2021-04-12 15:59:31 -04:00
Isaac Connor fe425302ad Merge branch 'master' of github.com:ZoneMinder/zoneminder 2020-08-21 10:46:13 -04:00
Isaac Connor 63ef15d247 spacing and quotes 2020-08-21 10:46:10 -04:00
Isaac Connor 80a6608733 Make values in conf.d files override values in db. Also handle the fact that they would already be defined in this case. 2020-08-21 10:36:49 -04:00
Isaac Connor 3bfcd8eb33 Check for definition of ZM_TIMEZONE before using it. Fixes #2895 2020-04-02 14:00:36 -04:00
Isaac Connor 821355c117 Only set date.timezone if we have a configured value in the Config 2020-02-11 13:21:28 -05:00
Isaac Connor e9815bf9c7 add ZM Namespace to Error calls 2020-01-02 16:54:50 -05:00
Isaac Connor b24e912050 Use ZM\Server::find_one for loading server so that it gets cached. Use a single regexp to determine config line validity instead of two for efficiency 2019-12-13 17:46:56 -05:00