Overview
Comment:If content type is application/pdf then output a PDF file!
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk | stable | 1.1.27
Files: files | file ages | folders
SHA3-256: 565e1f91008ac84013581fae94c5d659d8db0e7698a978f74fa3f94c9f8b1972
User & Date: bohwaz on 2022-07-05 00:33:59
Other Links: manifest | tags
Context
2022-07-05
00:57
Add parameter to :http function to force download using a filename check-in: c152f1c84a user: bohwaz tags: trunk, stable, 1.1.27
00:33
If content type is application/pdf then output a PDF file! check-in: 565e1f9100 user: bohwaz tags: trunk, stable, 1.1.27
00:20
Allow skeletons to generate PDF check-in: 7b190b6233 user: bohwaz tags: trunk
Changes

Modified src/include/lib/Garradin/UserTemplate/Functions.php from [84e39d0433] to [7a60fdebc2].

69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
		}

		$params['included_from'] = array_merge($from, [$params['file']]);

		$s->display($params);
	}

	static public function http(array $params): void
	{
		if (headers_sent()) {
			return;
		}

		if (isset($params['code'])) {
			static $codes = [







|







69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
		}

		$params['included_from'] = array_merge($from, [$params['file']]);

		$s->display($params);
	}

	static public function http(array $params, UserTemplate $tpl): void
	{
		if (headers_sent()) {
			return;
		}

		if (isset($params['code'])) {
			static $codes = [
145
146
147
148
149
150
151

152
153
154
155
156
157
			header(sprintf('HTTP/1.1 %d %s', $params['code'], $codes[$params['code']]), true);
		}
		elseif (isset($params['redirect'])) {
			Utils::redirect($params['redirect']);
		}
		elseif (isset($params['type'])) {
			header('Content-Type: ' . $params['type'], true);

		}
		else {
			throw new Brindille_Exception('No valid parameter found for http function');
		}
	}
}







>






145
146
147
148
149
150
151
152
153
154
155
156
157
158
			header(sprintf('HTTP/1.1 %d %s', $params['code'], $codes[$params['code']]), true);
		}
		elseif (isset($params['redirect'])) {
			Utils::redirect($params['redirect']);
		}
		elseif (isset($params['type'])) {
			header('Content-Type: ' . $params['type'], true);
			$tpl->setContentType($params['type']);
		}
		else {
			throw new Brindille_Exception('No valid parameter found for http function');
		}
	}
}

Modified src/include/lib/Garradin/UserTemplate/UserTemplate.php from [a447e071ab] to [591daa0c99].

28
29
30
31
32
33
34


35
36
37
38
39
40
41
	protected $file = null;
	protected $code = null;
	protected $cache_path = USER_TEMPLATES_CACHE_ROOT;

	protected $escape_default = 'html';

	static protected $root_variables;



	static public function getRootVariables()
	{
		if (null !== self::$root_variables) {
			return self::$root_variables;
		}








>
>







28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
	protected $file = null;
	protected $code = null;
	protected $cache_path = USER_TEMPLATES_CACHE_ROOT;

	protected $escape_default = 'html';

	static protected $root_variables;

	protected $content_type = null;

	static public function getRootVariables()
	{
		if (null !== self::$root_variables) {
			return self::$root_variables;
		}

270
271
272
273
274
275
276
277





















		if ($filename) {
			header(sprintf('Content-Disposition: attachment; filename="%s"', Utils::safeFileName($filename)));
		}

		Utils::streamPDF($this->fetch());
	}
}



























|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299

		if ($filename) {
			header(sprintf('Content-Disposition: attachment; filename="%s"', Utils::safeFileName($filename)));
		}

		Utils::streamPDF($this->fetch());
	}

	public function setContentType(string $type): void
	{
		$this->content_type = $type;
	}

	public function displayWeb(): void
	{
		$content = $this->fetch();

		$type = $this->content_type ?: 'text/html';
		header(sprintf('Content-Type: %s;charset=utf-8', $type), true);

		if ($type == 'application/pdf') {
			Utils::streamPDF($content);
		}
		else {
			echo $content;
		}
	}
}

Modified src/include/lib/Garradin/Web/Skeleton.php from [d48e7ab5de] to [9f94904a8e].

10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
use KD2\Brindille_Exception;
use KD2\DB\EntityManager as EM;

use const Garradin\ROOT;

class Skeleton
{
	const TEMPLATE_TYPES = '!^(?:text/(?:html|plain)|\w+/(?:\w+\+)?xml|application/pdf)$!';

	protected $name;
	protected $file;

	public function __construct(string $tpl)
	{
		if (!preg_match('!^[\w\d_-]+(?:\.[\w\d_-]+)*$!i', $tpl)) {







|







10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
use KD2\Brindille_Exception;
use KD2\DB\EntityManager as EM;

use const Garradin\ROOT;

class Skeleton
{
	const TEMPLATE_TYPES = '!^(?:text/(?:html|plain)|\w+/(?:\w+\+)?xml)$!';

	protected $name;
	protected $file;

	public function __construct(string $tpl)
	{
		if (!preg_match('!^[\w\d_-]+(?:\.[\w\d_-]+)*$!i', $tpl)) {
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
		if (preg_match(self::TEMPLATE_TYPES, $this->type())) {
			$ut = new UserTemplate($this->file);

			if (!$this->file) {
				$ut->setSource($this->defaultPath());
			}

			$type = $this->type();

			try {
				$ut->assignArray($params);
				if ($type == 'application/pdf') {
					$ut->displayPDF();
				}
				else {
					header(sprintf('Content-Type: %s;charset=utf-8', $type));
					$ut->display();
				}
			}
			catch (Brindille_Exception $e) {
				if (!headers_sent()) {
					header('Content-Type: text/html; charset=utf-8', true);
				}

				printf('<div style="border: 5px solid orange; padding: 10px; background: yellow;"><h2>Erreur dans le squelette</h2><p>%s</p></div>', nl2br(htmlspecialchars($e->getMessage())));







<
<


<
|
<
<
<
<
<







57
58
59
60
61
62
63


64
65

66





67
68
69
70
71
72
73
		if (preg_match(self::TEMPLATE_TYPES, $this->type())) {
			$ut = new UserTemplate($this->file);

			if (!$this->file) {
				$ut->setSource($this->defaultPath());
			}



			try {
				$ut->assignArray($params);

				$ut->displayWeb();





			}
			catch (Brindille_Exception $e) {
				if (!headers_sent()) {
					header('Content-Type: text/html; charset=utf-8', true);
				}

				printf('<div style="border: 5px solid orange; padding: 10px; background: yellow;"><h2>Erreur dans le squelette</h2><p>%s</p></div>', nl2br(htmlspecialchars($e->getMessage())));
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
		}
		elseif ($ext == 'html') {
			return 'text/html';
		}
		elseif ($ext == 'js') {
			return 'text/javascript';
		}
		elseif ($ext == 'pdf') {
			return 'application/pdf';
		}

		if ($this->file) {
			return $this->file->mime;
  		}

		$finfo = \finfo_open(\FILEINFO_MIME_TYPE);
		return finfo_file($finfo, $this->defaultPath());







<
<
<







171
172
173
174
175
176
177



178
179
180
181
182
183
184
		}
		elseif ($ext == 'html') {
			return 'text/html';
		}
		elseif ($ext == 'js') {
			return 'text/javascript';
		}




		if ($this->file) {
			return $this->file->mime;
  		}

		$finfo = \finfo_open(\FILEINFO_MIME_TYPE);
		return finfo_file($finfo, $this->defaultPath());