e2de66576c7ff0c0588add33806f7c3cc66aa529
[fa-stable.git] / reporting / includes / printer_class.inc
1 <?php
2 /*
3  * remote_printer class.
4  * All needed filters should be set for the printer in printercap file.
5  * Based on PrintSendLPR class by Mick Sear, eCreate
6  */
7  
8 class remote_printer {
9
10     var $host;
11     var $port;
12     var $timeout;
13         var $queue;
14         //
15         //      Setting connection parameters
16         //
17     function remote_printer($queue, $host='', $port=515, $timeout=20){
18                 if ($host == '')
19                         $host = $_SERVER['REMOTE_ADDR']; // default is user's host
20         $this->host = $host;
21         $this->port = $port;
22         $this->timeout = $timeout;
23         $this->queue = $queue;
24     }
25         //
26         //      Send file to remote network printer.
27         // 
28     function print_file($fname){
29         
30                 $queue = $this->queue;
31
32         //Private static function prints waiting jobs on the queue.
33         $ret = $this->flush_queue($queue);
34 //              if($ret) return $ret;
35
36         //Open a new connection to send the control file and data.
37                 $stream = @fsockopen("tcp://".$this->host, $this->port, $errNo, $errStr, $this->timeout);
38         if(!$stream){
39             return _('Cannot open connection to printer').":<br>$errStr";
40         }
41                 if (!isset($_SESSION['_print_job'])) {
42                         $_SESSION['print_job'] = 0;
43                 }
44         $job = $_SESSION['print_job']++;
45             //Set printer to receive file
46         fwrite($stream, chr(2).$queue."\n");
47                 $ack = fread($stream, 1);
48         if ($ack != 0) {
49                         fclose($stream);
50                 return _('Printer does not acept the job').' ('.ord($ack).')';
51                 }
52             
53         // Send Control file.
54         $server = $_SERVER['SERVER_NAME'];
55         $ctrl = "H".$server."\nP". substr($_SESSION["wa_current_user"]->loginname,0,31) 
56                         ."\nfdfA".$job.$server."\n";
57         fwrite($stream, chr(2).strlen($ctrl)." cfA".$job.$server."\n");
58         $ack = fread($stream, 1);
59         if ($ack != 0) {
60                         fclose($stream);
61             return _('Error sending print job control file').' ('.ord($ack).')';
62                 }
63            
64         fwrite($stream, $ctrl.chr(0)); //Write null to indicate end of stream
65         $ack = fread($stream, 1);
66         if ($ack != 0) {
67                         fclose($stream);
68                 return _('Print control file not accepted').' ('.ord($ack).')';
69                 }
70                        
71         $data = fopen($fname, "rb");    
72         fwrite($stream, chr(3).filesize($fname)." dfA".$job.$server."\n");
73         $ack = fread($stream, 1);
74         if ($ack != 0) {
75                         fclose($stream);
76                 return _('Cannot send report to printer').' ('.ord($ack).')';
77                 }
78                 
79         while(!feof($data)){
80                 if (fwrite($stream, fread($data, 8192))<8192) break;
81         }
82         fwrite($stream, chr(0)); //Write null to indicate end of stream
83         $ack = fread($stream, 1);
84         if ($ack != 0) {
85                         fclose($stream);
86             return _('No ack after report printout').' ('.ord($ack).')';
87                 }
88              
89         fclose($data);                
90                 fclose($stream);
91
92                 return '';
93     }
94     //
95         //      Print all waiting jobs on remote printer queue.
96         //
97     function flush_queue($queue){
98                 $stream = @fsockopen("tcp://".$this->host, $this->port,$errNo, $errStr, $this->timeout);
99         if (!$stream){
100             return _('Cannot flush printing queue').":<br>$errStr";
101                         // .':<br>' . $errNo." (".$errStr.")"; return 0 (success) even on failure
102         } else {
103             //Print any waiting jobs
104             fwrite($stream, chr(1).$queue."\n");            
105             while(!feof($stream)){
106                fread($stream, 1);
107             }
108         }
109                 return false;
110     }
111
112 }
113