-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathPhpNetworkLprPrinter.php
330 lines (285 loc) · 7.37 KB
/
PhpNetworkLprPrinter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<?php
/*
* Class PhpLprPrinter
* Print your files via PHP with LPR network printer
* See http://www.faqs.org/rfcs/rfc1179.html to understand RFC 1179 - Line printer daemon protocol
*
* (C) Copyright 2011 Pedro Villena <[email protected]>
* Licensed under the GNU GPL v3 license. See file COPYRIGHT for details.
*/
class PhpNetworkLprPrinter{
/**
* Printer's host. Initialize by constructor
*
* @var string
* @access protected
* @since 1.0
*/
var $_host;
/**
* Printer's Port. Default port 515 (see constructor), but it can change with the function setPort
*
* @var integer
* @access protected
* @since 1.0
*/
var $_port;
/**
* Max seconds to connect to the printer. Default 20, but it can change with the function setTimeOut
*
* @var integer
* @access protected
* @since 1.0
*/
var $_timeout = 30;
/**
* Username for printer
*
* @var string
* @access protected
* @since 1.0
*/
var $_username = "PhpNetworkLprPrinter";
/**
* Error number if connection fails
*
* @var integer
* @access protected
* @since 1.0
*/
var $_error_number;
/**
* Error message if connection fails
*
* @var integer
* @access protected
* @since 1.0
*/
var $_error_msg;
/**
* Debug message
*
* @var string
* @access protected
* @since 1.0
*/
var $_debug = array();
/**
* Class constructor.
*
* @param string The printer's host
* @param integer The printer's port
* @since 1.0
*/
public function __construct($host, $port=515) {
$this->_host = $host;
$this->_port = $port;
}
/**
* Sets a message in the array $_debug
*
* @access public
* @param string $message Message
* @param string $type Message's type, for example "message" or "error"
* @since 1.0
*/
private function setMessage($message="", $type="message"){
$this->_debug[]=array("message"=>$message, "time"=>time(), "type"=>$type);
}
/**
* Sets an error message in the array $_debug
*
* @access public
* @param string $error Error message
* @since 1.0
*/
private function setError($error=""){
$this->_debug[]=array("message"=>$error, "time"=>time(), "type"=>"error");
$this->_error_msg=$error;
}
/**
* Sets the port
*
* @access public
* @param integer $port Printer's port
* @since 1.0
*/
public function setPort($port){
$this->_port = $port;
$this->setMessage("Setting port: ".$this->_port);
}
/**
* Sets the time out in seconds
*
* @access public
* @param integer $timeout Timeout in seconds
* @since 1.0
*/
public function setTimeOut($timeout){
$this->_timeout = $timeout;
$this->setMessage("Setting time out: ".$this->_timeout);
}
/**
* Gets the error number
*
* @access public
* @return integer Error number
* @since 1.0
*/
public function getErrNo(){
return $this->_errNo;
}
/**
* Gets the error message
*
* @access public
* @return string Error message
* @since 1.0
*/
public function getErrStr(){
return $this->_errStr;
}
/**
* Gets the debug message
*
* @access public
* @return array Debug message array
* @since 1.0
*/
public function getDebug(){
return $this->_debug;
}
/**
* Connect to printer
*
* @access private
* @return socket Connection
* @since 1.0
*/
private function connect(){
$this->setMessage("Connecting... Host: ".$this->_host.", Port: ".$this->_port);
return stream_socket_client("tcp://".$this->_host.":".$this->_port, $this->_error_number, $this->_error_msg, $this->_timeout);
}
/**
* Makes de cfA (control string)
*
* @access private
* @return string cfA control String
* @since 1.0
*/
private function makecfA($jobid){
$this->setMessage("Setting cfA control String");
$hostname = $_SERVER['REMOTE_ADDR'];
$cfa = "";
$cfa .= "H" . $hostname . "\n"; //hostname
$cfa .= "P" . $this->_username . "\n"; //user
$cfA .= "fdfA" + $jobid + $hostname + "\n";
//TODO: Add more parameters. See http://www.faqs.org/rfcs/rfc1179.html
return $cfa;
}
/**
* Print any waiting jobs
*
* @access private
* @return boolean cfA control String
* @since 1.0
*/
public function printWaitingJobs($queue) {
//Connecting to the network printer
$connection = $this->connect();
//If fail, exit with false
if(!$connection){
$this->setError("Error in connection. Please change HOST or PORT.");
return false;
}else{
//Print any waiting job
fwrite($connection, chr(1).$queue."\n");
$this->setMessage("Print any waiting job...");
//Checking errors
if (ord(fread($connection, 1)) != 0) {
$this->setError("Error while start print jobs on queue " . $queue);
//Close connection
fclose($connection);
return false;
}
//Close connection
fclose($connection);
}
return true;
}
/**
* Print a text message on network lpr printer
*
* @access public
* @param string $text The name of the property
* @return boolean True if success
* @since 1.0
*/
public function printText($text=""){
//Initial data
$queue="defaultQueue"; //TODO: Change default queue
$jobid=001; //TODO: Autoincrement $jobid
//Print any waiting job
$this->printWaitingJobs($queue);
//Connecting to the network printer
$connection = $this->connect();
//If fail, exit with false
if(!$connection){
$this->setError("Error in connection. Please change HOST or PORT.");
return false;
}else{
//Starting printer
fwrite($connection, chr(2).$queue."\n");
$this->setMessage("Starting printer...");
//Checking errors
if (ord(fread($connection, 1)) != 0) {
$this->setError("Error while start printing on queue");
//Close connection
fclose($connection);
return false;
}
//Write control file
$ctrl = $this->makecfA($jobid);
fwrite($connection, chr(2).strlen($ctrl)." cfA".$jobid.$this->_username."\n");
$this->setMessage("Sending control file...");
//Checking errors
if (ord(fread($connection, 1)) != 0) {
$this->setError("Error while start sending control file");
//Close connection
fclose($connection);
return false;
}
fwrite($connection, $ctrl.chr(0));
//Checking errors
if (ord(fread($connection, 1)) != 0) {
$this->setError("Error while sending control file");
//Close connection
fclose($connection);
return false;
}
//Send data string
fwrite($connection, chr(3).strlen($text)." dfA".$jobid."\n");
$this->setMessage("Sending data...");
//Checking errors
if (ord(fread($connection, 1)) != 0) {
$this->setError("Error while sending control file");
//Close connection
fclose($connection);
return false;
}
fwrite($connection, $text.chr(0));
//Checking errors
if (ord(fread($connection, 1)) != 0) {
$this->setError("Error while sending control file");
//Close connection
fclose($connection);
return false;
}else{
$this->setMessage("Data received!!!");
}
//Close connection
fclose($connection);
}
}
}
?>