fix: guard K_CURLOPTS constant to prevent fatal error when not defined#865
fix: guard K_CURLOPTS constant to prevent fatal error when not defined#865WayneRocha wants to merge 1 commit intotecnickcom:mainfrom
Conversation
|
Thank you very much, @WayneRocha, for the guard on TCPDF 6 is now deprecated and no further changes will be merged into this repository. Please see the pinned issue Thank You - and the Future of TCPDF for the full explanation and the path forward. In tc-lib-pdf, cURL constants are never used in class-level constant initializers, so this class of fatal error cannot occur. If you encounter a related issue there, feel free to open a PR or issue. I am leaving this PR open in case you or anyone else wishes to continue the discussion, but it is unlikely that this PR will ever be merged. Thank you again for your patience and understanding. |
|
Thanks for the answer @nicolaasuni! I see this specific issue is not needed to be fixed in the new I will be looking for the new one now. Happy for contributing! |
Background
This fix was identified while investigating a fatal error reported in a real-world production environment where TCPDF is vendored inside Event Tickets Plus (a WordPress plugin by StellarWP) alongside WooCommerce PDF Vouchers.
The affected flow:
The workaround that unblocked the affected site was to manually define
K_CURLOPTSbefore TCPDF executes. This PR makes that workaround unnecessary by fixing the root cause in TCPDF itself.Problem
When TCPDF is used as a vendored dependency with
K_TCPDF_EXTERNAL_CONFIGset totrue, the entiretcpdf_config.phpis skipped. If the external config does not defineK_CURLOPTS, PHP 8+ throws a fatal error at runtime:The guard added in
tcpdf_autoconfig.php(commit aab43ab) does not cover this scenario — whenK_TCPDF_EXTERNAL_CONFIGistrue,tcpdf_autoconfig.phpconstants are also bypassed, soK_CURLOPTSmay never be defined before it is used.Fix
Added a
defined()check at the two points of use insideinclude/tcpdf_static.php— inurl_exists()andfileGetContents()— so thatK_CURLOPTSfalls back to an empty array when not defined, preserving the existingCURLOPT_DEFAULTdefaults:This is the correct place for the guard: defensive at the point of use, guaranteed to execute regardless of which configuration path was taken.
Why not fix it in
tcpdf_config.phportcpdf_autoconfig.php?Both of those files are entirely skipped when
K_TCPDF_EXTERNAL_CONFIGistrue, which is a common pattern for projects that vendor TCPDF and supply their own configuration. The only reliable place to handle a missingK_CURLOPTSis where it is consumed.Changes
include/tcpdf_static.php: 2 lines changed (lines 1860 and 1995) — one inurl_exists(), one infileGetContents()