Subversion Repositories ALCASAR

Rev

Rev 2168 | Details | Compare with Previous | 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 2225 2017-05-14 15:24:35Z 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
	/**
2225 tom.houday 37
	 * @var bool Replace file if already exist.
2168 tom.houday 38
	 */
2225 tom.houday 39
	private $replace = true;
2168 tom.houday 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
		}
2225 tom.houday 57
		if (isset($options['replace'])) {
58
			$this->replace = $options['replace'];
59
		}
2168 tom.houday 60
	}
61
 
62
	/**
63
	 * Add a ticket.
64
	 *
65
	 * @param array $user      User.
66
	 * @param bool  $duplicate Print a duplicated ticket if true.
67
	 */
68
	public function addTicket($user, $duplicate = true)
69
	{
70
		$this->tickets[] = (object) [
71
			'username'        => $user['username'],
72
			'password'        => $user['password'],
73
			'maxAllSession'   => $user['maxAllSession'],
74
			'sessionTimeout'  => $user['sessionTimeout'],
75
			'maxDailySession' => $user['maxDailySession'],
76
			'expiration'      => ((isset($user['expiration'])) ? $user['expiration'] : '-'),
77
			'isDuplicate'     => false
78
		];
79
		if ($duplicate) {
80
			$this->tickets[] = (object) [
81
				'username'        => $user['username'],
82
				'password'        => $user['password'],
83
				'maxAllSession'   => $user['maxAllSession'],
84
				'sessionTimeout'  => $user['sessionTimeout'],
85
				'maxDailySession' => $user['maxDailySession'],
86
				'expiration'      => ((isset($user['expiration'])) ? $user['expiration'] : '-'),
87
				'isDuplicate'     => true
88
			];
89
		}
90
	}
91
 
92
	/**
93
	 * Generate and save the PDF to the filesystem.
94
	 *
95
	 * @param string $filename File name of the generated PDF.
96
	 *
97
	 * @return bool Result of the convertion (true if success).
98
	 */
99
	public function saveAs($filename)
100
	{
2225 tom.houday 101
		if ((!$this->replace) && (file_exists($filename))) {
2168 tom.houday 102
			return false;
103
		}
104
 
105
		// TODO: Regex validation of $filename
106
 
107
		if (!$this->generateHtml("$filename.html")) {
108
			return false;
109
		}
110
 
111
		$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);
112
		$output;
113
		$exitCode;
114
		exec($command, $output, $exitCode);
115
 
116
		unlink("$filename.html");
117
 
118
		if ($exitCode !== 0) {
119
			return false;
120
		}
121
 
122
		return true;
123
	}
124
 
125
	/**
126
	 * Send the PDF to the  browser.
127
	 *
128
	 * @return bool Result of the convertion (true if success).
129
	 */
130
	public function output()
131
	{
132
		$filename = tempnam('/tmp', 'ALCASAR-PDF_');
133
		unlink($filename);
134
 
135
		if (!$this->saveAs($filename)) {
136
			return false;
137
		}
138
 
139
		header('Content-Type: application/pdf');
140
		header('Content-Disposition: inline; filename="tickets.pdf"');
141
		header('Cache-Control: private, max-age=0, must-revalidate');
142
		header('Pragma: public');
143
		readfile($filename);
144
 
145
		unlink($filename);
146
 
147
		return true;
148
	}
149
 
150
	/**
151
	 * Generate HTML document from the template.
152
	 *
153
	 * @param string $output File name of the generated template.
154
	 *
155
	 * @return bool Result of the generation (true if success).
156
	 */
157
	private function generateHtml($output)
158
	{
2225 tom.houday 159
		if ((!$this->replace) && (file_exists($output))) {
2168 tom.houday 160
			return false;
161
		}
162
 
163
		if (!file_exists($this->template)) {
164
			return false;
165
		}
166
 
167
		$language = $this->language;
168
		$users    = $this->tickets;
169
 
170
		ob_start();
171
		require($this->template);
172
		$content = ob_get_clean();
173
 
174
		$ret = file_put_contents($output, $content);
175
		if ($ret === false) {
176
			return false;
177
		}
178
 
179
		return true;
180
	}
181
}