Subversion Repositories ALCASAR

Rev

Go to most recent revision | Details | Last modification | View Log

Rev Author Line No. Line
2168 tom.houday 1
<?php
2
/**
3
 * ALCASAR tickets generator
4
 *
5
 * Generate tickets of users.
6
 * Use wkhtmltopdf to convert HTML to PDF.
7
 *
8
 * @author    Tom Houdayer
9
 * @copyright Copyright (C) ALCASAR (http://www.alcasar.net)
10
 * @license   GPL-3.0
11
 * @version   $Id: TicketsGenerator.php 2168 2017-04-18 17:39:54Z tom.houdayer $
12
 */
13
 
14
class TicketsGenerator
15
{
16
	/**
17
	 * @var string Path to wkhtmltopdf executable.
18
	 */
19
	private $wkhtmltopdfPath = 'wkhtmltopdf';
20
 
21
	/**
22
	 * @var object[] Tickets of users.
23
	 */
24
	private $tickets = [];
25
 
26
	/**
27
	 * @var string Language of tickets.
28
	 */
29
	private $language = 'en';
30
 
31
	/**
32
	 * @var string HTML template filename.
33
	 */
34
	private $template = __DIR__ . '/' . 'tickets.template.php';
35
 
36
	/**
37
	 * @var string|null HTML generated filename (null if not generated).
38
	 */
39
	private $htmlGeneratedFilename;
40
 
41
	/**
42
	 * Constructor.
43
	 *
44
	 * @param array $options Options of the instance.
45
	 */
46
	public function __construct($options = [])
47
	{
48
		if (isset($options['wkhtmltopdfPath'])) {
49
			$this->wkhtmltopdfPath = $options['wkhtmltopdfPath'];
50
		}
51
		if (isset($options['language'])) {
52
			$this->language = $options['language'];
53
		}
54
		if (isset($options['template'])) {
55
			$this->template = $options['template'];
56
		}
57
	}
58
 
59
	/**
60
	 * Add a ticket.
61
	 *
62
	 * @param array $user      User.
63
	 * @param bool  $duplicate Print a duplicated ticket if true.
64
	 */
65
	public function addTicket($user, $duplicate = true)
66
	{
67
		$this->tickets[] = (object) [
68
			'username'        => $user['username'],
69
			'password'        => $user['password'],
70
			'maxAllSession'   => $user['maxAllSession'],
71
			'sessionTimeout'  => $user['sessionTimeout'],
72
			'maxDailySession' => $user['maxDailySession'],
73
			'expiration'      => ((isset($user['expiration'])) ? $user['expiration'] : '-'),
74
			'isDuplicate'     => false
75
		];
76
		if ($duplicate) {
77
			$this->tickets[] = (object) [
78
				'username'        => $user['username'],
79
				'password'        => $user['password'],
80
				'maxAllSession'   => $user['maxAllSession'],
81
				'sessionTimeout'  => $user['sessionTimeout'],
82
				'maxDailySession' => $user['maxDailySession'],
83
				'expiration'      => ((isset($user['expiration'])) ? $user['expiration'] : '-'),
84
				'isDuplicate'     => true
85
			];
86
		}
87
	}
88
 
89
	/**
90
	 * Generate and save the PDF to the filesystem.
91
	 *
92
	 * @param string $filename File name of the generated PDF.
93
	 *
94
	 * @return bool Result of the convertion (true if success).
95
	 */
96
	public function saveAs($filename)
97
	{
98
		if (file_exists($filename)) {
99
			return false;
100
		}
101
 
102
		// TODO: Regex validation of $filename
103
 
104
		if (!$this->generateHtml("$filename.html")) {
105
			return false;
106
		}
107
 
108
		$command = $this->wkhtmltopdfPath . ' --quiet --disable-smart-shrinking --footer-font-size 8 --footer-left "ALCASAR" --footer-center "[page] / [toPage]" --footer-right "' . date('Y-m-d H:i:s') . '" ' . escapeshellarg("$filename.html") . ' ' . escapeshellarg($filename);
109
		$output;
110
		$exitCode;
111
		exec($command, $output, $exitCode);
112
 
113
		unlink("$filename.html");
114
 
115
		if ($exitCode !== 0) {
116
			return false;
117
		}
118
 
119
		return true;
120
	}
121
 
122
	/**
123
	 * Send the PDF to the  browser.
124
	 *
125
	 * @return bool Result of the convertion (true if success).
126
	 */
127
	public function output()
128
	{
129
		$filename = tempnam('/tmp', 'ALCASAR-PDF_');
130
		unlink($filename);
131
 
132
		if (!$this->saveAs($filename)) {
133
			return false;
134
		}
135
 
136
		header('Content-Type: application/pdf');
137
		header('Content-Disposition: inline; filename="tickets.pdf"');
138
		header('Cache-Control: private, max-age=0, must-revalidate');
139
		header('Pragma: public');
140
		readfile($filename);
141
 
142
		unlink($filename);
143
 
144
		return true;
145
	}
146
 
147
	/**
148
	 * Generate HTML document from the template.
149
	 *
150
	 * @param string $output File name of the generated template.
151
	 *
152
	 * @return bool Result of the generation (true if success).
153
	 */
154
	private function generateHtml($output)
155
	{
156
		if (file_exists($output)) {
157
			return false;
158
		}
159
 
160
		if (!file_exists($this->template)) {
161
			return false;
162
		}
163
 
164
		$language = $this->language;
165
		$users    = $this->tickets;
166
 
167
		ob_start();
168
		require($this->template);
169
		$content = ob_get_clean();
170
 
171
		$ret = file_put_contents($output, $content);
172
		if ($ret === false) {
173
			return false;
174
		}
175
 
176
		return true;
177
	}
178
}