| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package dompdf |
| 4 |
* @link https://github.com/dompdf/dompdf |
| 5 |
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License |
| 6 |
*/ |
| 7 |
namespace Dompdf; |
| 8 |
|
| 9 |
class Options |
| 10 |
{ |
| 11 |
/** |
| 12 |
* The root of your DOMPDF installation |
| 13 |
* |
| 14 |
* @var string |
| 15 |
*/ |
| 16 |
private $rootDir; |
| 17 |
|
| 18 |
/** |
| 19 |
* The location of a temporary directory. |
| 20 |
* |
| 21 |
* The directory specified must be writable by the executing process. |
| 22 |
* The temporary directory is required to download remote images and when |
| 23 |
* using the PFDLib back end. |
| 24 |
* |
| 25 |
* @var string |
| 26 |
*/ |
| 27 |
private $tempDir; |
| 28 |
|
| 29 |
/** |
| 30 |
* The location of the DOMPDF font directory |
| 31 |
* |
| 32 |
* The location of the directory where DOMPDF will store fonts and font metrics |
| 33 |
* Note: This directory must exist and be writable by the executing process. |
| 34 |
* |
| 35 |
* @var string |
| 36 |
*/ |
| 37 |
private $fontDir; |
| 38 |
|
| 39 |
/** |
| 40 |
* The location of the DOMPDF font cache directory |
| 41 |
* |
| 42 |
* This directory contains the cached font metrics for the fonts used by DOMPDF. |
| 43 |
* This directory can be the same as $fontDir |
| 44 |
* |
| 45 |
* Note: This directory must exist and be writable by the executing process. |
| 46 |
* |
| 47 |
* @var string |
| 48 |
*/ |
| 49 |
private $fontCache; |
| 50 |
|
| 51 |
/** |
| 52 |
* dompdf's "chroot" |
| 53 |
* |
| 54 |
* Utilized by Dompdf's default file:// protocol URI validation rule. |
| 55 |
* All local files opened by dompdf must be in a subdirectory of the directory |
| 56 |
* or directories specified by this option. |
| 57 |
* DO NOT set this value to '/' since this could allow an attacker to use dompdf to |
| 58 |
* read any files on the server. This should be an absolute path. |
| 59 |
* |
| 60 |
* ==== IMPORTANT ==== |
| 61 |
* This setting may increase the risk of system exploit. Do not change |
| 62 |
* this settings without understanding the consequences. Additional |
| 63 |
* documentation is available on the dompdf wiki at: |
| 64 |
* https://github.com/dompdf/dompdf/wiki |
| 65 |
* |
| 66 |
* @var array |
| 67 |
*/ |
| 68 |
private $chroot; |
| 69 |
|
| 70 |
/** |
| 71 |
* Protocol whitelist |
| 72 |
* |
| 73 |
* Protocols and PHP wrappers allowed in URIs, and the validation rules |
| 74 |
* that determine if a resouce may be loaded. Full support is not guaranteed |
| 75 |
* for the protocols/wrappers specified |
| 76 |
* by this array. |
| 77 |
* |
| 78 |
* @var array |
| 79 |
*/ |
| 80 |
private $allowedProtocols = [ |
| 81 |
"file://" => ["rules" => []], |
| 82 |
"http://" => ["rules" => []], |
| 83 |
"https://" => ["rules" => []] |
| 84 |
]; |
| 85 |
|
| 86 |
/** |
| 87 |
* @var string |
| 88 |
*/ |
| 89 |
private $logOutputFile; |
| 90 |
|
| 91 |
/** |
| 92 |
* Styles targeted to this media type are applied to the document. |
| 93 |
* This is on top of the media types that are always applied: |
| 94 |
* all, static, visual, bitmap, paged, dompdf |
| 95 |
* |
| 96 |
* @var string |
| 97 |
*/ |
| 98 |
private $defaultMediaType = "screen"; |
| 99 |
|
| 100 |
/** |
| 101 |
* The default paper size. |
| 102 |
* |
| 103 |
* North America standard is "letter"; other countries generally "a4" |
| 104 |
* @see \Dompdf\Adapter\CPDF::PAPER_SIZES for valid sizes |
| 105 |
* |
| 106 |
* @var string |
| 107 |
*/ |
| 108 |
private $defaultPaperSize = "letter"; |
| 109 |
|
| 110 |
/** |
| 111 |
* The default paper orientation. |
| 112 |
* |
| 113 |
* The orientation of the page (portrait or landscape). |
| 114 |
* |
| 115 |
* @var string |
| 116 |
*/ |
| 117 |
private $defaultPaperOrientation = "portrait"; |
| 118 |
|
| 119 |
/** |
| 120 |
* The default font family |
| 121 |
* |
| 122 |
* Used if no suitable fonts can be found. This must exist in the font folder. |
| 123 |
* |
| 124 |
* @var string |
| 125 |
*/ |
| 126 |
private $defaultFont = "serif"; |
| 127 |
|
| 128 |
/** |
| 129 |
* Image DPI setting |
| 130 |
* |
| 131 |
* This setting determines the default DPI setting for images and fonts. The |
| 132 |
* DPI may be overridden for inline images by explicitly setting the |
| 133 |
* image's width & height style attributes (i.e. if the image's native |
| 134 |
* width is 600 pixels and you specify the image's width as 72 points, |
| 135 |
* the image will have a DPI of 600 in the rendered PDF. The DPI of |
| 136 |
* background images can not be overridden and is controlled entirely |
| 137 |
* via this parameter. |
| 138 |
* |
| 139 |
* For the purposes of DOMPDF, pixels per inch (PPI) = dots per inch (DPI). |
| 140 |
* If a size in html is given as px (or without unit as image size), |
| 141 |
* this tells the corresponding size in pt at 72 DPI. |
| 142 |
* This adjusts the relative sizes to be similar to the rendering of the |
| 143 |
* html page in a reference browser. |
| 144 |
* |
| 145 |
* In pdf, always 1 pt = 1/72 inch |
| 146 |
* |
| 147 |
* @var int |
| 148 |
*/ |
| 149 |
private $dpi = 96; |
| 150 |
|
| 151 |
/** |
| 152 |
* A ratio applied to the fonts height to be more like browsers' line height |
| 153 |
* |
| 154 |
* @var float |
| 155 |
*/ |
| 156 |
private $fontHeightRatio = 1.1; |
| 157 |
|
| 158 |
/** |
| 159 |
* Enable embedded PHP |
| 160 |
* |
| 161 |
* If this setting is set to true then DOMPDF will automatically evaluate |
| 162 |
* embedded PHP contained within <script type="text/php"> ... </script> tags. |
| 163 |
* |
| 164 |
* ==== IMPORTANT ==== |
| 165 |
* Enabling this for documents you do not trust (e.g. arbitrary remote html |
| 166 |
* pages) is a security risk. Embedded scripts are run with the same level of |
| 167 |
* system access available to dompdf. Set this option to false (recommended) |
| 168 |
* if you wish to process untrusted documents. |
| 169 |
* |
| 170 |
* This setting may increase the risk of system exploit. Do not change |
| 171 |
* this settings without understanding the consequences. Additional |
| 172 |
* documentation is available on the dompdf wiki at: |
| 173 |
* https://github.com/dompdf/dompdf/wiki |
| 174 |
* |
| 175 |
* @var bool |
| 176 |
*/ |
| 177 |
private $isPhpEnabled = false; |
| 178 |
|
| 179 |
/** |
| 180 |
* Enable remote file access |
| 181 |
* |
| 182 |
* If this setting is set to true, DOMPDF will access remote sites for |
| 183 |
* images and CSS files as required. |
| 184 |
* |
| 185 |
* ==== IMPORTANT ==== |
| 186 |
* This can be a security risk, in particular in combination with isPhpEnabled and |
| 187 |
* allowing remote html code to be passed to $dompdf = new DOMPDF(); $dompdf->load_html(...); |
| 188 |
* This allows anonymous users to download legally doubtful internet content which on |
| 189 |
* tracing back appears to being downloaded by your server, or allows malicious php code |
| 190 |
* in remote html pages to be executed by your server with your account privileges. |
| 191 |
* |
| 192 |
* This setting may increase the risk of system exploit. Do not change |
| 193 |
* this settings without understanding the consequences. Additional |
| 194 |
* documentation is available on the dompdf wiki at: |
| 195 |
* https://github.com/dompdf/dompdf/wiki |
| 196 |
* |
| 197 |
* @var bool |
| 198 |
*/ |
| 199 |
private $isRemoteEnabled = false; |
| 200 |
|
| 201 |
/** |
| 202 |
* Enable inline JavaScript |
| 203 |
* |
| 204 |
* If this setting is set to true then DOMPDF will automatically insert |
| 205 |
* JavaScript code contained within <script type="text/javascript"> ... </script> |
| 206 |
* tags as written into the PDF. |
| 207 |
* |
| 208 |
* NOTE: This is PDF-based JavaScript to be executed by the PDF viewer, |
| 209 |
* not browser-based JavaScript executed by Dompdf. |
| 210 |
* |
| 211 |
* @var bool |
| 212 |
*/ |
| 213 |
private $isJavascriptEnabled = true; |
| 214 |
|
| 215 |
/** |
| 216 |
* Use the HTML5 Lib parser |
| 217 |
* |
| 218 |
* @deprecated |
| 219 |
* @var bool |
| 220 |
*/ |
| 221 |
private $isHtml5ParserEnabled = true; |
| 222 |
|
| 223 |
/** |
| 224 |
* Whether to enable font subsetting or not. |
| 225 |
* |
| 226 |
* @var bool |
| 227 |
*/ |
| 228 |
private $isFontSubsettingEnabled = true; |
| 229 |
|
| 230 |
/** |
| 231 |
* @var bool |
| 232 |
*/ |
| 233 |
private $debugPng = false; |
| 234 |
|
| 235 |
/** |
| 236 |
* @var bool |
| 237 |
*/ |
| 238 |
private $debugKeepTemp = false; |
| 239 |
|
| 240 |
/** |
| 241 |
* @var bool |
| 242 |
*/ |
| 243 |
private $debugCss = false; |
| 244 |
|
| 245 |
/** |
| 246 |
* @var bool |
| 247 |
*/ |
| 248 |
private $debugLayout = false; |
| 249 |
|
| 250 |
/** |
| 251 |
* @var bool |
| 252 |
*/ |
| 253 |
private $debugLayoutLines = true; |
| 254 |
|
| 255 |
/** |
| 256 |
* @var bool |
| 257 |
*/ |
| 258 |
private $debugLayoutBlocks = true; |
| 259 |
|
| 260 |
/** |
| 261 |
* @var bool |
| 262 |
*/ |
| 263 |
private $debugLayoutInline = true; |
| 264 |
|
| 265 |
/** |
| 266 |
* @var bool |
| 267 |
*/ |
| 268 |
private $debugLayoutPaddingBox = true; |
| 269 |
|
| 270 |
/** |
| 271 |
* The PDF rendering backend to use |
| 272 |
* |
| 273 |
* Valid settings are 'PDFLib', 'CPDF', 'GD', and 'auto'. 'auto' will |
| 274 |
* look for PDFLib and use it if found, or if not it will fall back on |
| 275 |
* CPDF. 'GD' renders PDFs to graphic files. {@link Dompdf\CanvasFactory} |
| 276 |
* ultimately determines which rendering class to instantiate |
| 277 |
* based on this setting. |
| 278 |
* |
| 279 |
* @var string |
| 280 |
*/ |
| 281 |
private $pdfBackend = "CPDF"; |
| 282 |
|
| 283 |
/** |
| 284 |
* PDFlib license key |
| 285 |
* |
| 286 |
* If you are using a licensed, commercial version of PDFlib, specify |
| 287 |
* your license key here. If you are using PDFlib-Lite or are evaluating |
| 288 |
* the commercial version of PDFlib, comment out this setting. |
| 289 |
* |
| 290 |
* @link http://www.pdflib.com |
| 291 |
* |
| 292 |
* If pdflib present in web server and auto or selected explicitly above, |
| 293 |
* a real license code must exist! |
| 294 |
* |
| 295 |
* @var string |
| 296 |
*/ |
| 297 |
private $pdflibLicense = ""; |
| 298 |
|
| 299 |
/** |
| 300 |
* HTTP context created with stream_context_create() |
| 301 |
* Will be used for file_get_contents |
| 302 |
* |
| 303 |
* @link https://www.php.net/manual/context.php |
| 304 |
* |
| 305 |
* @var resource |
| 306 |
*/ |
| 307 |
private $httpContext; |
| 308 |
|
| 309 |
/** |
| 310 |
* @param array $attributes |
| 311 |
*/ |
| 312 |
public function __construct(array $attributes = null) |
| 313 |
{ |
| 314 |
$rootDir = realpath(__DIR__ . "/../"); |
| 315 |
$this->setChroot(array($rootDir)); |
| 316 |
$this->setRootDir($rootDir); |
| 317 |
$this->setTempDir(sys_get_temp_dir()); |
| 318 |
$this->setFontDir($rootDir . "/lib/fonts"); |
| 319 |
$this->setFontCache($this->getFontDir()); |
| 320 |
|
| 321 |
$ver = ""; |
| 322 |
$versionFile = realpath(__DIR__ . '/../VERSION'); |
| 323 |
if (($version = file_get_contents($versionFile)) !== false) { |
| 324 |
$version = trim($version); |
| 325 |
if ($version !== '$Format:<%h>$') { |
| 326 |
$ver = "/$version"; |
| 327 |
} |
| 328 |
} |
| 329 |
$this->setHttpContext([ |
| 330 |
"http" => [ |
| 331 |
"follow_location" => false, |
| 332 |
"user_agent" => "Dompdf$ver https://github.com/dompdf/dompdf" |
| 333 |
] |
| 334 |
]); |
| 335 |
|
| 336 |
$this->setAllowedProtocols(["file://", "http://", "https://"]); |
| 337 |
|
| 338 |
if (null !== $attributes) { |
| 339 |
$this->set($attributes); |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
/** |
| 344 |
* @param array|string $attributes |
| 345 |
* @param null|mixed $value |
| 346 |
* @return $this |
| 347 |
*/ |
| 348 |
public function set($attributes, $value = null) |
| 349 |
{ |
| 350 |
if (!is_array($attributes)) { |
| 351 |
$attributes = [$attributes => $value]; |
| 352 |
} |
| 353 |
foreach ($attributes as $key => $value) { |
| 354 |
if ($key === 'tempDir' || $key === 'temp_dir') { |
| 355 |
$this->setTempDir($value); |
| 356 |
} elseif ($key === 'fontDir' || $key === 'font_dir') { |
| 357 |
$this->setFontDir($value); |
| 358 |
} elseif ($key === 'fontCache' || $key === 'font_cache') { |
| 359 |
$this->setFontCache($value); |
| 360 |
} elseif ($key === 'chroot') { |
| 361 |
$this->setChroot($value); |
| 362 |
} elseif ($key === 'allowedProtocols') { |
| 363 |
$this->setAllowedProtocols($value); |
| 364 |
} elseif ($key === 'logOutputFile' || $key === 'log_output_file') { |
| 365 |
$this->setLogOutputFile($value); |
| 366 |
} elseif ($key === 'defaultMediaType' || $key === 'default_media_type') { |
| 367 |
$this->setDefaultMediaType($value); |
| 368 |
} elseif ($key === 'defaultPaperSize' || $key === 'default_paper_size') { |
| 369 |
$this->setDefaultPaperSize($value); |
| 370 |
} elseif ($key === 'defaultPaperOrientation' || $key === 'default_paper_orientation') { |
| 371 |
$this->setDefaultPaperOrientation($value); |
| 372 |
} elseif ($key === 'defaultFont' || $key === 'default_font') { |
| 373 |
$this->setDefaultFont($value); |
| 374 |
} elseif ($key === 'dpi') { |
| 375 |
$this->setDpi($value); |
| 376 |
} elseif ($key === 'fontHeightRatio' || $key === 'font_height_ratio') { |
| 377 |
$this->setFontHeightRatio($value); |
| 378 |
} elseif ($key === 'isPhpEnabled' || $key === 'is_php_enabled' || $key === 'enable_php') { |
| 379 |
$this->setIsPhpEnabled($value); |
| 380 |
} elseif ($key === 'isRemoteEnabled' || $key === 'is_remote_enabled' || $key === 'enable_remote') { |
| 381 |
$this->setIsRemoteEnabled($value); |
| 382 |
} elseif ($key === 'isJavascriptEnabled' || $key === 'is_javascript_enabled' || $key === 'enable_javascript') { |
| 383 |
$this->setIsJavascriptEnabled($value); |
| 384 |
} elseif ($key === 'isHtml5ParserEnabled' || $key === 'is_html5_parser_enabled' || $key === 'enable_html5_parser') { |
| 385 |
$this->setIsHtml5ParserEnabled($value); |
| 386 |
} elseif ($key === 'isFontSubsettingEnabled' || $key === 'is_font_subsetting_enabled' || $key === 'enable_font_subsetting') { |
| 387 |
$this->setIsFontSubsettingEnabled($value); |
| 388 |
} elseif ($key === 'debugPng' || $key === 'debug_png') { |
| 389 |
$this->setDebugPng($value); |
| 390 |
} elseif ($key === 'debugKeepTemp' || $key === 'debug_keep_temp') { |
| 391 |
$this->setDebugKeepTemp($value); |
| 392 |
} elseif ($key === 'debugCss' || $key === 'debug_css') { |
| 393 |
$this->setDebugCss($value); |
| 394 |
} elseif ($key === 'debugLayout' || $key === 'debug_layout') { |
| 395 |
$this->setDebugLayout($value); |
| 396 |
} elseif ($key === 'debugLayoutLines' || $key === 'debug_layout_lines') { |
| 397 |
$this->setDebugLayoutLines($value); |
| 398 |
} elseif ($key === 'debugLayoutBlocks' || $key === 'debug_layout_blocks') { |
| 399 |
$this->setDebugLayoutBlocks($value); |
| 400 |
} elseif ($key === 'debugLayoutInline' || $key === 'debug_layout_inline') { |
| 401 |
$this->setDebugLayoutInline($value); |
| 402 |
} elseif ($key === 'debugLayoutPaddingBox' || $key === 'debug_layout_padding_box') { |
| 403 |
$this->setDebugLayoutPaddingBox($value); |
| 404 |
} elseif ($key === 'pdfBackend' || $key === 'pdf_backend') { |
| 405 |
$this->setPdfBackend($value); |
| 406 |
} elseif ($key === 'pdflibLicense' || $key === 'pdflib_license') { |
| 407 |
$this->setPdflibLicense($value); |
| 408 |
} elseif ($key === 'httpContext' || $key === 'http_context') { |
| 409 |
$this->setHttpContext($value); |
| 410 |
} |
| 411 |
} |
| 412 |
return $this; |
| 413 |
} |
| 414 |
|
| 415 |
/** |
| 416 |
* @param string $key |
| 417 |
* @return mixed |
| 418 |
*/ |
| 419 |
public function get($key) |
| 420 |
{ |
| 421 |
if ($key === 'tempDir' || $key === 'temp_dir') { |
| 422 |
return $this->getTempDir(); |
| 423 |
} elseif ($key === 'fontDir' || $key === 'font_dir') { |
| 424 |
return $this->getFontDir(); |
| 425 |
} elseif ($key === 'fontCache' || $key === 'font_cache') { |
| 426 |
return $this->getFontCache(); |
| 427 |
} elseif ($key === 'chroot') { |
| 428 |
return $this->getChroot(); |
| 429 |
} elseif ($key === 'allowedProtocols') { |
| 430 |
return $this->getAllowedProtocols(); |
| 431 |
} elseif ($key === 'logOutputFile' || $key === 'log_output_file') { |
| 432 |
return $this->getLogOutputFile(); |
| 433 |
} elseif ($key === 'defaultMediaType' || $key === 'default_media_type') { |
| 434 |
return $this->getDefaultMediaType(); |
| 435 |
} elseif ($key === 'defaultPaperSize' || $key === 'default_paper_size') { |
| 436 |
return $this->getDefaultPaperSize(); |
| 437 |
} elseif ($key === 'defaultPaperOrientation' || $key === 'default_paper_orientation') { |
| 438 |
return $this->getDefaultPaperOrientation(); |
| 439 |
} elseif ($key === 'defaultFont' || $key === 'default_font') { |
| 440 |
return $this->getDefaultFont(); |
| 441 |
} elseif ($key === 'dpi') { |
| 442 |
return $this->getDpi(); |
| 443 |
} elseif ($key === 'fontHeightRatio' || $key === 'font_height_ratio') { |
| 444 |
return $this->getFontHeightRatio(); |
| 445 |
} elseif ($key === 'isPhpEnabled' || $key === 'is_php_enabled' || $key === 'enable_php') { |
| 446 |
return $this->getIsPhpEnabled(); |
| 447 |
} elseif ($key === 'isRemoteEnabled' || $key === 'is_remote_enabled' || $key === 'enable_remote') { |
| 448 |
return $this->getIsRemoteEnabled(); |
| 449 |
} elseif ($key === 'isJavascriptEnabled' || $key === 'is_javascript_enabled' || $key === 'enable_javascript') { |
| 450 |
return $this->getIsJavascriptEnabled(); |
| 451 |
} elseif ($key === 'isHtml5ParserEnabled' || $key === 'is_html5_parser_enabled' || $key === 'enable_html5_parser') { |
| 452 |
return $this->getIsHtml5ParserEnabled(); |
| 453 |
} elseif ($key === 'isFontSubsettingEnabled' || $key === 'is_font_subsetting_enabled' || $key === 'enable_font_subsetting') { |
| 454 |
return $this->getIsFontSubsettingEnabled(); |
| 455 |
} elseif ($key === 'debugPng' || $key === 'debug_png') { |
| 456 |
return $this->getDebugPng(); |
| 457 |
} elseif ($key === 'debugKeepTemp' || $key === 'debug_keep_temp') { |
| 458 |
return $this->getDebugKeepTemp(); |
| 459 |
} elseif ($key === 'debugCss' || $key === 'debug_css') { |
| 460 |
return $this->getDebugCss(); |
| 461 |
} elseif ($key === 'debugLayout' || $key === 'debug_layout') { |
| 462 |
return $this->getDebugLayout(); |
| 463 |
} elseif ($key === 'debugLayoutLines' || $key === 'debug_layout_lines') { |
| 464 |
return $this->getDebugLayoutLines(); |
| 465 |
} elseif ($key === 'debugLayoutBlocks' || $key === 'debug_layout_blocks') { |
| 466 |
return $this->getDebugLayoutBlocks(); |
| 467 |
} elseif ($key === 'debugLayoutInline' || $key === 'debug_layout_inline') { |
| 468 |
return $this->getDebugLayoutInline(); |
| 469 |
} elseif ($key === 'debugLayoutPaddingBox' || $key === 'debug_layout_padding_box') { |
| 470 |
return $this->getDebugLayoutPaddingBox(); |
| 471 |
} elseif ($key === 'pdfBackend' || $key === 'pdf_backend') { |
| 472 |
return $this->getPdfBackend(); |
| 473 |
} elseif ($key === 'pdflibLicense' || $key === 'pdflib_license') { |
| 474 |
return $this->getPdflibLicense(); |
| 475 |
} elseif ($key === 'httpContext' || $key === 'http_context') { |
| 476 |
return $this->getHttpContext(); |
| 477 |
} |
| 478 |
return null; |
| 479 |
} |
| 480 |
|
| 481 |
/** |
| 482 |
* @param string $pdfBackend |
| 483 |
* @return $this |
| 484 |
*/ |
| 485 |
public function setPdfBackend($pdfBackend) |
| 486 |
{ |
| 487 |
$this->pdfBackend = $pdfBackend; |
| 488 |
return $this; |
| 489 |
} |
| 490 |
|
| 491 |
/** |
| 492 |
* @return string |
| 493 |
*/ |
| 494 |
public function getPdfBackend() |
| 495 |
{ |
| 496 |
return $this->pdfBackend; |
| 497 |
} |
| 498 |
|
| 499 |
/** |
| 500 |
* @param string $pdflibLicense |
| 501 |
* @return $this |
| 502 |
*/ |
| 503 |
public function setPdflibLicense($pdflibLicense) |
| 504 |
{ |
| 505 |
$this->pdflibLicense = $pdflibLicense; |
| 506 |
return $this; |
| 507 |
} |
| 508 |
|
| 509 |
/** |
| 510 |
* @return string |
| 511 |
*/ |
| 512 |
public function getPdflibLicense() |
| 513 |
{ |
| 514 |
return $this->pdflibLicense; |
| 515 |
} |
| 516 |
|
| 517 |
/** |
| 518 |
* @param array|string $chroot |
| 519 |
* @return $this |
| 520 |
*/ |
| 521 |
public function setChroot($chroot, $delimiter = ',') |
| 522 |
{ |
| 523 |
if (is_string($chroot)) { |
| 524 |
$this->chroot = explode($delimiter, $chroot); |
| 525 |
} elseif (is_array($chroot)) { |
| 526 |
$this->chroot = $chroot; |
| 527 |
} |
| 528 |
return $this; |
| 529 |
} |
| 530 |
|
| 531 |
/** |
| 532 |
* @return array |
| 533 |
*/ |
| 534 |
public function getAllowedProtocols() |
| 535 |
{ |
| 536 |
return $this->allowedProtocols; |
| 537 |
} |
| 538 |
|
| 539 |
/** |
| 540 |
* @param array $allowedProtocols The protocols to allow, as an array |
| 541 |
* formatted as ["protocol://" => ["rules" => [callable]], ...] |
| 542 |
* or ["protocol://", ...] |
| 543 |
* |
| 544 |
* @return $this |
| 545 |
*/ |
| 546 |
public function setAllowedProtocols(array $allowedProtocols) |
| 547 |
{ |
| 548 |
$protocols = []; |
| 549 |
foreach ($allowedProtocols as $protocol => $config) { |
| 550 |
if (is_string($protocol)) { |
| 551 |
$protocols[$protocol] = []; |
| 552 |
if (is_array($config)) { |
| 553 |
$protocols[$protocol] = $config; |
| 554 |
} |
| 555 |
} elseif (is_string($config)) { |
| 556 |
$protocols[$config] = []; |
| 557 |
} |
| 558 |
} |
| 559 |
$this->allowedProtocols = []; |
| 560 |
foreach ($protocols as $protocol => $config) { |
| 561 |
$this->addAllowedProtocol($protocol, ...($config["rules"] ?? [])); |
| 562 |
} |
| 563 |
return $this; |
| 564 |
} |
| 565 |
|
| 566 |
/** |
| 567 |
* Adds a new protocol to the allowed protocols collection |
| 568 |
* |
| 569 |
* @param string $protocol The scheme to add (e.g. "http://") |
| 570 |
* @param callable $rule A callable that validates the protocol |
| 571 |
* @return $this |
| 572 |
*/ |
| 573 |
public function addAllowedProtocol(string $protocol, callable ...$rules) |
| 574 |
{ |
| 575 |
$protocol = strtolower($protocol); |
| 576 |
if (empty($rules)) { |
| 577 |
$rules = []; |
| 578 |
switch ($protocol) { |
| 579 |
case "file://": |
| 580 |
$rules[] = [$this, "validateLocalUri"]; |
| 581 |
break; |
| 582 |
case "http://": |
| 583 |
case "https://": |
| 584 |
$rules[] = [$this, "validateRemoteUri"]; |
| 585 |
break; |
| 586 |
case "phar://": |
| 587 |
$rules[] = [$this, "validatePharUri"]; |
| 588 |
break; |
| 589 |
} |
| 590 |
} |
| 591 |
$this->allowedProtocols[$protocol] = ["rules" => $rules]; |
| 592 |
return $this; |
| 593 |
} |
| 594 |
|
| 595 |
/** |
| 596 |
* @return array |
| 597 |
*/ |
| 598 |
public function getChroot() |
| 599 |
{ |
| 600 |
$chroot = []; |
| 601 |
if (is_array($this->chroot)) { |
| 602 |
$chroot = $this->chroot; |
| 603 |
} |
| 604 |
return $chroot; |
| 605 |
} |
| 606 |
|
| 607 |
/** |
| 608 |
* @param boolean $debugCss |
| 609 |
* @return $this |
| 610 |
*/ |
| 611 |
public function setDebugCss($debugCss) |
| 612 |
{ |
| 613 |
$this->debugCss = $debugCss; |
| 614 |
return $this; |
| 615 |
} |
| 616 |
|
| 617 |
/** |
| 618 |
* @return boolean |
| 619 |
*/ |
| 620 |
public function getDebugCss() |
| 621 |
{ |
| 622 |
return $this->debugCss; |
| 623 |
} |
| 624 |
|
| 625 |
/** |
| 626 |
* @param boolean $debugKeepTemp |
| 627 |
* @return $this |
| 628 |
*/ |
| 629 |
public function setDebugKeepTemp($debugKeepTemp) |
| 630 |
{ |
| 631 |
$this->debugKeepTemp = $debugKeepTemp; |
| 632 |
return $this; |
| 633 |
} |
| 634 |
|
| 635 |
/** |
| 636 |
* @return boolean |
| 637 |
*/ |
| 638 |
public function getDebugKeepTemp() |
| 639 |
{ |
| 640 |
return $this->debugKeepTemp; |
| 641 |
} |
| 642 |
|
| 643 |
/** |
| 644 |
* @param boolean $debugLayout |
| 645 |
* @return $this |
| 646 |
*/ |
| 647 |
public function setDebugLayout($debugLayout) |
| 648 |
{ |
| 649 |
$this->debugLayout = $debugLayout; |
| 650 |
return $this; |
| 651 |
} |
| 652 |
|
| 653 |
/** |
| 654 |
* @return boolean |
| 655 |
*/ |
| 656 |
public function getDebugLayout() |
| 657 |
{ |
| 658 |
return $this->debugLayout; |
| 659 |
} |
| 660 |
|
| 661 |
/** |
| 662 |
* @param boolean $debugLayoutBlocks |
| 663 |
* @return $this |
| 664 |
*/ |
| 665 |
public function setDebugLayoutBlocks($debugLayoutBlocks) |
| 666 |
{ |
| 667 |
$this->debugLayoutBlocks = $debugLayoutBlocks; |
| 668 |
return $this; |
| 669 |
} |
| 670 |
|
| 671 |
/** |
| 672 |
* @return boolean |
| 673 |
*/ |
| 674 |
public function getDebugLayoutBlocks() |
| 675 |
{ |
| 676 |
return $this->debugLayoutBlocks; |
| 677 |
} |
| 678 |
|
| 679 |
/** |
| 680 |
* @param boolean $debugLayoutInline |
| 681 |
* @return $this |
| 682 |
*/ |
| 683 |
public function setDebugLayoutInline($debugLayoutInline) |
| 684 |
{ |
| 685 |
$this->debugLayoutInline = $debugLayoutInline; |
| 686 |
return $this; |
| 687 |
} |
| 688 |
|
| 689 |
/** |
| 690 |
* @return boolean |
| 691 |
*/ |
| 692 |
public function getDebugLayoutInline() |
| 693 |
{ |
| 694 |
return $this->debugLayoutInline; |
| 695 |
} |
| 696 |
|
| 697 |
/** |
| 698 |
* @param boolean $debugLayoutLines |
| 699 |
* @return $this |
| 700 |
*/ |
| 701 |
public function setDebugLayoutLines($debugLayoutLines) |
| 702 |
{ |
| 703 |
$this->debugLayoutLines = $debugLayoutLines; |
| 704 |
return $this; |
| 705 |
} |
| 706 |
|
| 707 |
/** |
| 708 |
* @return boolean |
| 709 |
*/ |
| 710 |
public function getDebugLayoutLines() |
| 711 |
{ |
| 712 |
return $this->debugLayoutLines; |
| 713 |
} |
| 714 |
|
| 715 |
/** |
| 716 |
* @param boolean $debugLayoutPaddingBox |
| 717 |
* @return $this |
| 718 |
*/ |
| 719 |
public function setDebugLayoutPaddingBox($debugLayoutPaddingBox) |
| 720 |
{ |
| 721 |
$this->debugLayoutPaddingBox = $debugLayoutPaddingBox; |
| 722 |
return $this; |
| 723 |
} |
| 724 |
|
| 725 |
/** |
| 726 |
* @return boolean |
| 727 |
*/ |
| 728 |
public function getDebugLayoutPaddingBox() |
| 729 |
{ |
| 730 |
return $this->debugLayoutPaddingBox; |
| 731 |
} |
| 732 |
|
| 733 |
/** |
| 734 |
* @param boolean $debugPng |
| 735 |
* @return $this |
| 736 |
*/ |
| 737 |
public function setDebugPng($debugPng) |
| 738 |
{ |
| 739 |
$this->debugPng = $debugPng; |
| 740 |
return $this; |
| 741 |
} |
| 742 |
|
| 743 |
/** |
| 744 |
* @return boolean |
| 745 |
*/ |
| 746 |
public function getDebugPng() |
| 747 |
{ |
| 748 |
return $this->debugPng; |
| 749 |
} |
| 750 |
|
| 751 |
/** |
| 752 |
* @param string $defaultFont |
| 753 |
* @return $this |
| 754 |
*/ |
| 755 |
public function setDefaultFont($defaultFont) |
| 756 |
{ |
| 757 |
if (!($defaultFont === null || trim($defaultFont) === "")) { |
| 758 |
$this->defaultFont = $defaultFont; |
| 759 |
} else { |
| 760 |
$this->defaultFont = "serif"; |
| 761 |
} |
| 762 |
return $this; |
| 763 |
} |
| 764 |
|
| 765 |
/** |
| 766 |
* @return string |
| 767 |
*/ |
| 768 |
public function getDefaultFont() |
| 769 |
{ |
| 770 |
return $this->defaultFont; |
| 771 |
} |
| 772 |
|
| 773 |
/** |
| 774 |
* @param string $defaultMediaType |
| 775 |
* @return $this |
| 776 |
*/ |
| 777 |
public function setDefaultMediaType($defaultMediaType) |
| 778 |
{ |
| 779 |
$this->defaultMediaType = $defaultMediaType; |
| 780 |
return $this; |
| 781 |
} |
| 782 |
|
| 783 |
/** |
| 784 |
* @return string |
| 785 |
*/ |
| 786 |
public function getDefaultMediaType() |
| 787 |
{ |
| 788 |
return $this->defaultMediaType; |
| 789 |
} |
| 790 |
|
| 791 |
/** |
| 792 |
* @param string $defaultPaperSize |
| 793 |
* @return $this |
| 794 |
*/ |
| 795 |
public function setDefaultPaperSize($defaultPaperSize) |
| 796 |
{ |
| 797 |
$this->defaultPaperSize = $defaultPaperSize; |
| 798 |
return $this; |
| 799 |
} |
| 800 |
|
| 801 |
/** |
| 802 |
* @param string $defaultPaperOrientation |
| 803 |
* @return $this |
| 804 |
*/ |
| 805 |
public function setDefaultPaperOrientation($defaultPaperOrientation) |
| 806 |
{ |
| 807 |
$this->defaultPaperOrientation = $defaultPaperOrientation; |
| 808 |
return $this; |
| 809 |
} |
| 810 |
|
| 811 |
/** |
| 812 |
* @return string |
| 813 |
*/ |
| 814 |
public function getDefaultPaperSize() |
| 815 |
{ |
| 816 |
return $this->defaultPaperSize; |
| 817 |
} |
| 818 |
|
| 819 |
/** |
| 820 |
* @return string |
| 821 |
*/ |
| 822 |
public function getDefaultPaperOrientation() |
| 823 |
{ |
| 824 |
return $this->defaultPaperOrientation; |
| 825 |
} |
| 826 |
|
| 827 |
/** |
| 828 |
* @param int $dpi |
| 829 |
* @return $this |
| 830 |
*/ |
| 831 |
public function setDpi($dpi) |
| 832 |
{ |
| 833 |
$this->dpi = $dpi; |
| 834 |
return $this; |
| 835 |
} |
| 836 |
|
| 837 |
/** |
| 838 |
* @return int |
| 839 |
*/ |
| 840 |
public function getDpi() |
| 841 |
{ |
| 842 |
return $this->dpi; |
| 843 |
} |
| 844 |
|
| 845 |
/** |
| 846 |
* @param string $fontCache |
| 847 |
* @return $this |
| 848 |
*/ |
| 849 |
public function setFontCache($fontCache) |
| 850 |
{ |
| 851 |
$this->fontCache = $fontCache; |
| 852 |
return $this; |
| 853 |
} |
| 854 |
|
| 855 |
/** |
| 856 |
* @return string |
| 857 |
*/ |
| 858 |
public function getFontCache() |
| 859 |
{ |
| 860 |
return $this->fontCache; |
| 861 |
} |
| 862 |
|
| 863 |
/** |
| 864 |
* @param string $fontDir |
| 865 |
* @return $this |
| 866 |
*/ |
| 867 |
public function setFontDir($fontDir) |
| 868 |
{ |
| 869 |
$this->fontDir = $fontDir; |
| 870 |
return $this; |
| 871 |
} |
| 872 |
|
| 873 |
/** |
| 874 |
* @return string |
| 875 |
*/ |
| 876 |
public function getFontDir() |
| 877 |
{ |
| 878 |
return $this->fontDir; |
| 879 |
} |
| 880 |
|
| 881 |
/** |
| 882 |
* @param float $fontHeightRatio |
| 883 |
* @return $this |
| 884 |
*/ |
| 885 |
public function setFontHeightRatio($fontHeightRatio) |
| 886 |
{ |
| 887 |
$this->fontHeightRatio = $fontHeightRatio; |
| 888 |
return $this; |
| 889 |
} |
| 890 |
|
| 891 |
/** |
| 892 |
* @return float |
| 893 |
*/ |
| 894 |
public function getFontHeightRatio() |
| 895 |
{ |
| 896 |
return $this->fontHeightRatio; |
| 897 |
} |
| 898 |
|
| 899 |
/** |
| 900 |
* @param boolean $isFontSubsettingEnabled |
| 901 |
* @return $this |
| 902 |
*/ |
| 903 |
public function setIsFontSubsettingEnabled($isFontSubsettingEnabled) |
| 904 |
{ |
| 905 |
$this->isFontSubsettingEnabled = $isFontSubsettingEnabled; |
| 906 |
return $this; |
| 907 |
} |
| 908 |
|
| 909 |
/** |
| 910 |
* @return boolean |
| 911 |
*/ |
| 912 |
public function getIsFontSubsettingEnabled() |
| 913 |
{ |
| 914 |
return $this->isFontSubsettingEnabled; |
| 915 |
} |
| 916 |
|
| 917 |
/** |
| 918 |
* @return boolean |
| 919 |
*/ |
| 920 |
public function isFontSubsettingEnabled() |
| 921 |
{ |
| 922 |
return $this->getIsFontSubsettingEnabled(); |
| 923 |
} |
| 924 |
|
| 925 |
/** |
| 926 |
* @deprecated |
| 927 |
* @param boolean $isHtml5ParserEnabled |
| 928 |
* @return $this |
| 929 |
*/ |
| 930 |
public function setIsHtml5ParserEnabled($isHtml5ParserEnabled) |
| 931 |
{ |
| 932 |
$this->isHtml5ParserEnabled = $isHtml5ParserEnabled; |
| 933 |
return $this; |
| 934 |
} |
| 935 |
|
| 936 |
/** |
| 937 |
* @deprecated |
| 938 |
* @return boolean |
| 939 |
*/ |
| 940 |
public function getIsHtml5ParserEnabled() |
| 941 |
{ |
| 942 |
return $this->isHtml5ParserEnabled; |
| 943 |
} |
| 944 |
|
| 945 |
/** |
| 946 |
* @deprecated |
| 947 |
* @return boolean |
| 948 |
*/ |
| 949 |
public function isHtml5ParserEnabled() |
| 950 |
{ |
| 951 |
return $this->getIsHtml5ParserEnabled(); |
| 952 |
} |
| 953 |
|
| 954 |
/** |
| 955 |
* @param boolean $isJavascriptEnabled |
| 956 |
* @return $this |
| 957 |
*/ |
| 958 |
public function setIsJavascriptEnabled($isJavascriptEnabled) |
| 959 |
{ |
| 960 |
$this->isJavascriptEnabled = $isJavascriptEnabled; |
| 961 |
return $this; |
| 962 |
} |
| 963 |
|
| 964 |
/** |
| 965 |
* @return boolean |
| 966 |
*/ |
| 967 |
public function getIsJavascriptEnabled() |
| 968 |
{ |
| 969 |
return $this->isJavascriptEnabled; |
| 970 |
} |
| 971 |
|
| 972 |
/** |
| 973 |
* @return boolean |
| 974 |
*/ |
| 975 |
public function isJavascriptEnabled() |
| 976 |
{ |
| 977 |
return $this->getIsJavascriptEnabled(); |
| 978 |
} |
| 979 |
|
| 980 |
/** |
| 981 |
* @param boolean $isPhpEnabled |
| 982 |
* @return $this |
| 983 |
*/ |
| 984 |
public function setIsPhpEnabled($isPhpEnabled) |
| 985 |
{ |
| 986 |
$this->isPhpEnabled = $isPhpEnabled; |
| 987 |
return $this; |
| 988 |
} |
| 989 |
|
| 990 |
/** |
| 991 |
* @return boolean |
| 992 |
*/ |
| 993 |
public function getIsPhpEnabled() |
| 994 |
{ |
| 995 |
return $this->isPhpEnabled; |
| 996 |
} |
| 997 |
|
| 998 |
/** |
| 999 |
* @return boolean |
| 1000 |
*/ |
| 1001 |
public function isPhpEnabled() |
| 1002 |
{ |
| 1003 |
return $this->getIsPhpEnabled(); |
| 1004 |
} |
| 1005 |
|
| 1006 |
/** |
| 1007 |
* @param boolean $isRemoteEnabled |
| 1008 |
* @return $this |
| 1009 |
*/ |
| 1010 |
public function setIsRemoteEnabled($isRemoteEnabled) |
| 1011 |
{ |
| 1012 |
$this->isRemoteEnabled = $isRemoteEnabled; |
| 1013 |
return $this; |
| 1014 |
} |
| 1015 |
|
| 1016 |
/** |
| 1017 |
* @return boolean |
| 1018 |
*/ |
| 1019 |
public function getIsRemoteEnabled() |
| 1020 |
{ |
| 1021 |
return $this->isRemoteEnabled; |
| 1022 |
} |
| 1023 |
|
| 1024 |
/** |
| 1025 |
* @return boolean |
| 1026 |
*/ |
| 1027 |
public function isRemoteEnabled() |
| 1028 |
{ |
| 1029 |
return $this->getIsRemoteEnabled(); |
| 1030 |
} |
| 1031 |
|
| 1032 |
/** |
| 1033 |
* @param string $logOutputFile |
| 1034 |
* @return $this |
| 1035 |
*/ |
| 1036 |
public function setLogOutputFile($logOutputFile) |
| 1037 |
{ |
| 1038 |
$this->logOutputFile = $logOutputFile; |
| 1039 |
return $this; |
| 1040 |
} |
| 1041 |
|
| 1042 |
/** |
| 1043 |
* @return string |
| 1044 |
*/ |
| 1045 |
public function getLogOutputFile() |
| 1046 |
{ |
| 1047 |
return $this->logOutputFile; |
| 1048 |
} |
| 1049 |
|
| 1050 |
/** |
| 1051 |
* @param string $tempDir |
| 1052 |
* @return $this |
| 1053 |
*/ |
| 1054 |
public function setTempDir($tempDir) |
| 1055 |
{ |
| 1056 |
$this->tempDir = $tempDir; |
| 1057 |
return $this; |
| 1058 |
} |
| 1059 |
|
| 1060 |
/** |
| 1061 |
* @return string |
| 1062 |
*/ |
| 1063 |
public function getTempDir() |
| 1064 |
{ |
| 1065 |
return $this->tempDir; |
| 1066 |
} |
| 1067 |
|
| 1068 |
/** |
| 1069 |
* @param string $rootDir |
| 1070 |
* @return $this |
| 1071 |
*/ |
| 1072 |
public function setRootDir($rootDir) |
| 1073 |
{ |
| 1074 |
$this->rootDir = $rootDir; |
| 1075 |
return $this; |
| 1076 |
} |
| 1077 |
|
| 1078 |
/** |
| 1079 |
* @return string |
| 1080 |
*/ |
| 1081 |
public function getRootDir() |
| 1082 |
{ |
| 1083 |
return $this->rootDir; |
| 1084 |
} |
| 1085 |
|
| 1086 |
/** |
| 1087 |
* Sets the HTTP context |
| 1088 |
* |
| 1089 |
* @param resource|array $httpContext |
| 1090 |
* @return $this |
| 1091 |
*/ |
| 1092 |
public function setHttpContext($httpContext) |
| 1093 |
{ |
| 1094 |
$this->httpContext = is_array($httpContext) ? stream_context_create($httpContext) : $httpContext; |
| 1095 |
return $this; |
| 1096 |
} |
| 1097 |
|
| 1098 |
/** |
| 1099 |
* Returns the HTTP context |
| 1100 |
* |
| 1101 |
* @return resource |
| 1102 |
*/ |
| 1103 |
public function getHttpContext() |
| 1104 |
{ |
| 1105 |
return $this->httpContext; |
| 1106 |
} |
| 1107 |
|
| 1108 |
public function validateLocalUri(string $uri) |
| 1109 |
{ |
| 1110 |
if ($uri === null || strlen($uri) === 0) { |
| 1111 |
return [false, "The URI must not be empty."]; |
| 1112 |
} |
| 1113 |
|
| 1114 |
$realfile = realpath(str_replace("file://", "", $uri)); |
| 1115 |
|
| 1116 |
$dirs = $this->chroot; |
| 1117 |
$dirs[] = $this->rootDir; |
| 1118 |
$chrootValid = false; |
| 1119 |
foreach ($dirs as $chrootPath) { |
| 1120 |
$chrootPath = realpath($chrootPath); |
| 1121 |
if ($chrootPath !== false && strpos($realfile, $chrootPath) === 0) { |
| 1122 |
$chrootValid = true; |
| 1123 |
break; |
| 1124 |
} |
| 1125 |
} |
| 1126 |
if ($chrootValid !== true) { |
| 1127 |
return [false, "Permission denied. The file could not be found under the paths specified by Options::chroot."]; |
| 1128 |
} |
| 1129 |
|
| 1130 |
if (!$realfile) { |
| 1131 |
return [false, "File not found."]; |
| 1132 |
} |
| 1133 |
|
| 1134 |
return [true, null]; |
| 1135 |
} |
| 1136 |
|
| 1137 |
public function validatePharUri(string $uri) |
| 1138 |
{ |
| 1139 |
if ($uri === null || strlen($uri) === 0) { |
| 1140 |
return [false, "The URI must not be empty."]; |
| 1141 |
} |
| 1142 |
|
| 1143 |
$file = substr(substr($uri, 0, strpos($uri, ".phar") + 5), 7); |
| 1144 |
return $this->validateLocalUri($file); |
| 1145 |
} |
| 1146 |
|
| 1147 |
public function validateRemoteUri(string $uri) |
| 1148 |
{ |
| 1149 |
if ($uri === null || strlen($uri) === 0) { |
| 1150 |
return [false, "The URI must not be empty."]; |
| 1151 |
} |
| 1152 |
|
| 1153 |
if (!$this->isRemoteEnabled) { |
| 1154 |
return [false, "Remote file requested, but remote file download is disabled."]; |
| 1155 |
} |
| 1156 |
|
| 1157 |
return [true, null]; |
| 1158 |
} |
| 1159 |
} |
| 1160 |
|