1
+ <?php
2
+ /*
3
+ * Class Print_send_lpr - Sends a file to be printed remotely.
4
+ * File must be either preformatted or plain ASCII until some
5
+ * printer driver functionality has been incorpoated.
6
+ */
7
+
8
+ class Print_send_lpr extends Print_send{
9
+
10
+ private $ host = "192.168.1.1 " ;
11
+ private $ port = "6001 " ;
12
+ private $ timeout = "5 " ;
13
+ private $ err_no ;
14
+ private $ err_str ;
15
+
16
+ public function __construct () {
17
+ parent ::__construct ();
18
+ }
19
+
20
+ public function set_port ($ port ){
21
+ $ this ->port = $ port ;
22
+ $ this ->debug .= "Port is " .$ this ->port ."\n" ;
23
+ }
24
+
25
+ public function set_host ($ host ){
26
+ $ this ->host = $ host ;
27
+ $ this ->debug .= "Host is " .$ this ->host ."\n" ;
28
+ }
29
+
30
+ public function set_timeout ($ timeout ){
31
+ $ this ->timeout = $ timeout ;
32
+ }
33
+
34
+ public function get_err_no (){
35
+ return $ this ->err_no ;
36
+ }
37
+
38
+ public function get_err_str (){
39
+ return $ this ->err_str ;
40
+ }
41
+
42
+ public function print_job ($ queue ){
43
+ //Private static function prints waiting jobs on the queue.
44
+ $ this ->print_waiting ($ queue );
45
+
46
+ //Open a new connection to send the control file and data.
47
+ $ stream = stream_socket_client ("tcp:// " .$ this ->host .": " .$ this ->port , $ this ->err_no , $ this ->err_str , $ this ->timeout );
48
+
49
+ if (!$ stream ){
50
+ return $ this ->err_no ." ( " .$ this ->err_str .") " ;
51
+ } else {
52
+ $ job = self ::get_job_id (); // Get a new id for this job
53
+
54
+ //Set printer to receive file
55
+ fwrite ($ stream , chr (2 ).$ queue ."\n" );
56
+ $ this ->debug .= "Confirmation of receive cmd: " .ord (fread ($ stream , 1 ))."\n" ;
57
+
58
+ //Send Control file.
59
+ (isset ($ _SERVER ['SERVER_NAME ' ])) ? $ server = $ _SERVER ['SERVER_NAME ' ] : $ server = "me " ;//Might be CLI and not have _SERVER
60
+ $ ctrl = "H " .$ server ."\nPphp \nfdfA " .$ job .$ server ."\n" ;
61
+ fwrite ($ stream , chr (2 ).strlen ($ ctrl )." cfA " .$ job .$ server ."\n" );
62
+ $ this ->debug .= "Confirmation of sending of control file cmd: " .ord (fread ($ stream , 1 ))."\n" ;
63
+
64
+ fwrite ($ stream , $ ctrl .chr (0 )); //Write null to indicate end of stream
65
+ $ this ->debug .= "Confirmation of sending of control file itself: " .ord (fread ($ stream , 1 ))."\n" ;
66
+
67
+ if (is_readable ($ this ->data )){
68
+ // It's a filename, rather than just some ascii text that needs printing. Open and stream.
69
+ if (strstr (strtolower ($ _ENV ["OS " ]), "windows " )){
70
+ $ this ->debug .= "Operating system is Windows \n" ;
71
+ $ data = fopen ($ this ->data , "rb " ); // Force binary in Windows.
72
+ } else {
73
+ $ this ->debug .= "Operating system is not Windows \n" ;
74
+ $ data = fopen ($ this ->data , "r " );
75
+ }
76
+
77
+ fwrite ($ stream , chr (3 ).filesize ($ this ->data )." dfA " .$ job .$ server ."\n" );
78
+ $ this ->debug .= "Confirmation of sending receive data cmd: " .ord (fread ($ stream , 1 ))."\n" ;
79
+
80
+ while (!feof ($ data )){
81
+ fwrite ($ stream , fread ($ data , 8192 ));
82
+ }
83
+
84
+ fwrite ($ stream , chr (0 )); // Write null to indicate end of stream
85
+ $ this ->debug .= "Confirmation of sending data: " .ord (fread ($ stream , 1 ))."\n" ;
86
+
87
+ fclose ($ data );
88
+ } else {
89
+ //Send data string
90
+ fwrite ($ stream , chr (3 ).strlen ($ this ->data )." dfA " .$ job .$ server ."\n" );
91
+ $ this ->debug .= "Confirmation of sending receive data cmd: " .ord (fread ($ stream , 1 ))."\n" ;
92
+
93
+ fwrite ($ stream , $ this ->data .chr (0 )); //Write null to indicate end of stream
94
+ $ this ->debug .= "Confirmation of sending data: " .ord (fread ($ stream , 1 ))."\n" ;
95
+ }
96
+ }
97
+
98
+ }
99
+
100
+ //Placeholder to handle filters - this will call methods in printer driver classes
101
+ //instantiated by the printer_factory method..
102
+ public function interpret (){}
103
+
104
+ private function get_job_id (){
105
+ return "001 " ;
106
+
107
+ //You should implement your own internal storage and incrementing of job numbers here.
108
+ //This might be a database value, or some file on the file system.
109
+ }
110
+
111
+ private function print_waiting ($ queue ){
112
+ $ stream = stream_socket_client ("tcp:// " .$ this ->host .": " .$ this ->port , $ this ->err_no , $ this ->err_str , $ this ->timeout );
113
+
114
+ if (!$ stream ){
115
+ return $ this ->err_no ." ( " .$ this ->err_str .") " ;
116
+ } else {
117
+ //Print any waiting jobs
118
+ fwrite ($ stream , $ queue ."\n" );
119
+ $ this ->debug .= "Confirmation of print waiting jobs cmd: " ;
120
+
121
+ //while(!feof($stream)){
122
+ //$this->debug .= ord(fread($stream, 1));
123
+ //}
124
+
125
+ fclose ($ stream );
126
+ $ this ->debug .= "\n" ;
127
+ }
128
+ }
129
+
130
+ }
0 commit comments