-
Notifications
You must be signed in to change notification settings - Fork 8
/
class.lpr.php
140 lines (125 loc) · 4.16 KB
/
class.lpr.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
<?php
/*
* Class PrintSend - Abstract printing class.
* Models the actual sending of data to the printer.
*
* Author: Mick Sear
* eCreate, Aug 2005
*/
abstract class PrintSend
{
protected $data;
protected $debug;
abstract protected function printJob($queue);
abstract protected function interpret();
public function __construct() {}
public function setData($data){
//This can be a filename or some ASCII. A file check should be made in derived classes.
$this->data = $data;
echo "Data set\n";
}
public static function printerFactory($type){
//Return a new instance of a printer driver of type $type.
if (include_once 'Drivers/' . $type . '.php') {
return new $type;
} else {
throw new Exception ('Driver not found: $type');
}
}
public function getDebug(){
return $this->debug;
}
}
class PrintSendLPR extends PrintSend
{
private $host = "localhost";
private $port = "515";
private $timeout = "20"; //20 secs
private $errNo;
private $errStr;
public function __construct() {
parent::__construct();
}
public function setPort($port){
$this->port = $port;
echo "Port is ".$this->port."\n";
}
public function setHost($host){
$this->host = $host;
echo "Host is ".$this->host."\n";
}
public function setTimeout($timeout){
$this->timeout = $timeout;
}
public function getErrNo(){
return $this->errNo;
}
public function getErrStr(){
return $this->errStr;
}
public function printJob($queue){
//Private static function prints waiting jobs on the queue.
$this->printWaiting($queue);
//Open a new connection to send the control file and data.
$stream = stream_socket_client("tcp://".$this->host.":".$this->port, $this->errNo, $this->errStr, $this->timeout);
if(!$stream){
return $this->errNo." (".$this->errStr.")";
}
else {
$job = self::getJobId();//Get a new id for this job
//Set printer to receive file
fwrite($stream, chr(2).$queue."\n");
echo "Confirmation of receive cmd:".ord(fread($stream, 1))."\n";
//Send Control file.
(isset($_SERVER['SERVER_NAME'])) ? $server = $_SERVER['SERVER_NAME'] : $server = "me";//Might be CLI and not have _SERVER
$ctrl = "H".$server."\nPphp\nfdfA".$job.$server."\n";
fwrite($stream, chr(2).strlen($ctrl)." cfA".$job.$server."\n");
echo "Confirmation of sending of control file cmd:".ord(fread($stream, 1))."\n";
fwrite($stream, $ctrl.chr(0)); //Write null to indicate end of stream
echo "Confirmation of sending of control file itself:".ord(fread($stream, 1))."\n";
if (is_readable($this->data)){
//It's a filename, rather than just some ascii text that needs printing. Open and stream.
if (strstr(strtolower($_ENV["OS"]), "windows")){
echo "Operating system is Windows\n";
$data = fopen($this->data, "rb");//Force binary in Windows.
} else {
echo "Operating system is not Windows\n";
$data = fopen($this->data, "r");
}
fwrite($stream, chr(3).filesize($this->data)." dfA".$job.$server."\n");
echo "Confirmation of sending receive data cmd:".ord(fread($stream, 1))."\n";
while(!feof($data)){
fwrite($stream, fread($data, 8192));
}
fwrite($stream, chr(0));//Write null to indicate end of stream
echo "Confirmation of sending data:".ord(fread($stream, 1))."\n";
fclose($data);
}
else {
//Send data string
fwrite($stream, chr(3).strlen($this->data)." dfA".$job.$server."\n");
echo "Confirmation of sending receive data cmd:".ord(fread($stream, 1))."\n";
fwrite($stream, $this->data.chr(0)); //Write null to indicate end of stream
echo "Confirmation of sending data:".ord(fread($stream, 1))."\n";
}
}
}
public function interpret(){}
private function getJobId(){
return "001";
}
private function printWaiting($queue){
$stream = stream_socket_client("tcp://".$this->host.":".$this->port, $this->errNo, $this->errStr, $this->timeout);
if (!$stream){
return $this->errNo." (".$this->errStr.")";
} else {
//Print any waiting jobs
fwrite($stream, chr(1).$queue."\n");
echo "Confirmation of print waiting jobs cmd:";
while(!feof($stream)){
echo ord(fread($stream, 1));
}
echo "\n";
}
}
}