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
15 // Setting connection parameters
17 function __construct($queue, $host='', $port=515, $timeout=20){
19 $host = $_SERVER['REMOTE_ADDR']; // default is user's host
22 $this->timeout = $timeout;
23 $this->queue = $queue;
26 // Send file to remote network printer.
28 function print_file($fname){
30 $queue = $this->queue;
32 //Private static function prints waiting jobs on the queue.
33 $ret = $this->flush_queue($queue);
34 // if($ret) return $ret;
36 //Open a new connection to send the control file and data.
37 $stream = @fsockopen("tcp://".$this->host, $this->port, $errNo, $errStr, $this->timeout);
39 return _('Cannot open connection to printer').":<br>$errStr";
41 if (!isset($_SESSION['_print_job'])) {
42 $_SESSION['print_job'] = 0;
44 $job = $_SESSION['print_job']++;
45 //Set printer to receive file
46 fwrite($stream, chr(2).$queue."\n");
47 $ack = fread($stream, 1);
50 return _('Printer does not acept the job').' ('.ord($ack).')';
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);
61 return _('Error sending print job control file').' ('.ord($ack).')';
64 fwrite($stream, $ctrl.chr(0)); //Write null to indicate end of stream
65 $ack = fread($stream, 1);
68 return _('Print control file not accepted').' ('.ord($ack).')';
71 $data = fopen($fname, "rb");
72 fwrite($stream, chr(3).filesize($fname)." dfA".$job.$server."\n");
73 $ack = fread($stream, 1);
76 return _('Cannot send report to printer').' ('.ord($ack).')';
80 if (fwrite($stream, fread($data, 8192))<8192) break;
82 fwrite($stream, chr(0)); //Write null to indicate end of stream
83 $ack = fread($stream, 1);
86 return _('No ack after report printout').' ('.ord($ack).')';
95 // Print all waiting jobs on remote printer queue.
97 function flush_queue($queue){
98 $stream = @fsockopen("tcp://".$this->host, $this->port,$errNo, $errStr, $this->timeout);
100 return _('Cannot flush printing queue').":<br>$errStr";
101 // .':<br>' . $errNo." (".$errStr.")"; return 0 (success) even on failure
103 //Print any waiting jobs
104 fwrite($stream, chr(1).$queue."\n");
105 while(!feof($stream)){