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
|
<?php
/*
* remote_printer class.
* All needed filters should be set for the printer in printercap file.
* Based on PrintSendLPR class by Mick Sear, eCreate
*/
class remote_printer {
var $host;
var $port;
var $timeout;
var $queue;
//
// Setting connection parameters
//
function remote_printer($queue, $host='', $port=515, $timeout=20){
if ($host == '')
$host = $_SERVER['REMOTE_ADDR']; // default is user's host
$this->host = $host;
$this->port = $port;
$this->timeout = $timeout;
$this->queue = $queue;
}
//
// Send file to remote network printer.
//
function print_file($fname){
$queue = $this->queue;
//Private static function prints waiting jobs on the queue.
$ret = $this->flush_queue($queue);
// if($ret) return $ret;
//Open a new connection to send the control file and data.
$stream = fsockopen("tcp://".$this->host, $this->port, $errNo, $errStr, $this->timeout);
if(!$stream){
return _('Cannot open connection to printer');
}
if (!isset($_SESSION['_print_job'])) {
$_SESSION['print_job'] = 0;
}
$job = $_SESSION['print_job']++;
//Set printer to receive file
fwrite($stream, chr(2).$queue."\n");
$ack = fread($stream, 1);
if ($ack != 0) {
fclose($stream);
return _('Printer does not acept the job').' ('.ord($ack).')';
}
// Send Control file.
$server = $_SERVER['SERVER_NAME'];
$ctrl = "H".$server."\nP". substr($_SESSION["wa_current_user"]->loginname,0,31)
."\nfdfA".$job.$server."\n";
fwrite($stream, chr(2).strlen($ctrl)." cfA".$job.$server."\n");
$ack = fread($stream, 1);
if ($ack != 0) {
fclose($stream);
return _('Error sending print job control file').' ('.ord($ack).')';
}
fwrite($stream, $ctrl.chr(0)); //Write null to indicate end of stream
$ack = fread($stream, 1);
if ($ack != 0) {
fclose($stream);
return _('Print control file not accepted').' ('.ord($ack).')';
}
$data = fopen($fname, "rb");
fwrite($stream, chr(3).filesize($fname)." dfA".$job.$server."\n");
$ack = fread($stream, 1);
if ($ack != 0) {
fclose($stream);
return _('Cannot send report to printer').' ('.ord($ack).')';
}
while(!feof($data)){
if (fwrite($stream, fread($data, 8192))<8192) break;
}
fwrite($stream, chr(0)); //Write null to indicate end of stream
$ack = fread($stream, 1);
if ($ack != 0) {
fclose($stream);
return _('No ack after report printout').' ('.ord($ack).')';
}
fclose($data);
fclose($stream);
return '';
}
//
// Print all waiting jobs on remote printer queue.
//
function flush_queue($queue){
$stream = fsockopen("tcp://".$this->host, $this->port,$errNo, $errStr, $this->timeout);
if (!$stream){
return _('Cannot flush printing queue');
// .':<br>' . $errNo." (".$errStr.")"; return 0 (success) even on failure
} else {
//Print any waiting jobs
fwrite($stream, chr(1).$queue."\n");
while(!feof($stream)){
fread($stream, 1);
}
}
return false;
}
}
?>
|