dolibarr 21.0.0-beta
printipp.modules.php
Go to the documentation of this file.
1<?php
2/*
3 * Copyright (C) 2014-2024 Frédéric France <frederic.france@free.fr>
4 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 * or see https://www.gnu.org/
19 */
20
27include_once DOL_DOCUMENT_ROOT.'/core/modules/printing/modules_printing.php';
28
33{
37 public $name = 'printipp';
38
42 public $desc = 'PrintIPPDesc';
43
47 public $picto = 'printer';
48
52 public $active = 'PRINTING_PRINTIPP';
53
57 public $conf = array();
58
62 public $host;
63
67 public $port;
68
72 public $userid;
73
77 public $user;
78
82 public $password;
83
87 public $ssl;
88
89
95 public function __construct($db)
96 {
97 global $conf;
98
99 $this->db = $db;
100 $this->host = getDolGlobalString('PRINTIPP_HOST');
101 $this->port = getDolGlobalString('PRINTIPP_PORT');
102 $this->user = getDolGlobalString('PRINTIPP_USER');
103 $this->password = getDolGlobalString('PRINTIPP_PASSWORD');
104 $this->ssl = getDolGlobalInt('PRINTIPP_SSL');
105 $this->conf[] = array('varname' => 'PRINTIPP_HOST', 'required' => 1, 'example' => 'localhost', 'type' => 'text');
106 $this->conf[] = array('varname' => 'PRINTIPP_PORT', 'required' => 1, 'example' => '631', 'type' => 'text');
107 $this->conf[] = array('varname' => 'PRINTIPP_USER', 'required' => 0, 'example' => '', 'type' => 'text', 'moreattributes' => 'autocomplete="off"');
108 $this->conf[] = array('varname' => 'PRINTIPP_PASSWORD', 'required' => 0, 'example' => '', 'type' => 'password', 'moreattributes' => 'autocomplete="off"');
109 $this->conf[] = array('varname' => 'PRINTIPP_SSL', 'required' => 0, 'example' => '', 'type' => 'checkbox', 'moreattributes' => 'autocomplete="off"');
110 $this->conf[] = array('enabled' => 1, 'type' => 'submit');
111 }
112
122 public function printFile($file, $module, $subdir = '')
123 {
124 global $conf, $user;
125 $error = 0;
126
127 include_once DOL_DOCUMENT_ROOT.'/includes/printipp/CupsPrintIPP.php';
128
129 $ipp = new CupsPrintIPP();
130 $ipp->setLog(DOL_DATA_ROOT.'/dolibarr_printipp.log', 'file', 3); // logging very verbose
131 $ipp->setHost($this->host);
132 $ipp->setPort($this->port);
133 $ipp->ssl = $this->ssl;
134 $ipp->setJobName($file, true);
135 $ipp->setUserName($this->userid);
136 // Set default number of copy
137 $ipp->setCopies(1);
138 if (!empty($this->user)) {
139 $ipp->setAuthentication($this->user, $this->password);
140 }
141
142 // select printer uri for module order, propal,...
143 $sql = "SELECT rowid,printer_id,copy FROM ".MAIN_DB_PREFIX."printing WHERE module = '".$this->db->escape($module)."' AND driver = 'printipp' AND userid = ".((int) $user->id);
144 $result = $this->db->query($sql);
145 if ($result) {
146 $obj = $this->db->fetch_object($result);
147 if ($obj) {
148 dol_syslog("Found a default printer for user ".$user->id." = ".$obj->printer_id);
149 $ipp->setPrinterURI($obj->printer_id);
150 // Set number of copy
151 $ipp->setCopies($obj->copy);
152 } else {
153 if (getDolGlobalString('PRINTIPP_URI_DEFAULT')) {
154 dol_syslog("Will use default printer conf->global->PRINTIPP_URI_DEFAULT = ".getDolGlobalString('PRINTIPP_URI_DEFAULT'));
155 $ipp->setPrinterURI(getDolGlobalString('PRINTIPP_URI_DEFAULT'));
156 } else {
157 $this->errors[] = 'NoDefaultPrinterDefined';
158 $error++;
159 return $error;
160 }
161 }
162 } else {
163 dol_print_error($this->db);
164 }
165
166 $fileprint = $conf->{$module}->dir_output;
167 if ($subdir != '') {
168 $fileprint .= '/'.$subdir;
169 }
170 $fileprint .= '/'.$file;
171 $ipp->setData($fileprint);
172 try {
173 $ipp->printJob();
174 } catch (Exception $e) {
175 $this->errors[] = $e->getMessage();
176 $error++;
177 }
178 if ($error == 0) {
179 $this->errors[] = 'PRINTIPP: Job added';
180 }
181
182 return $error;
183 }
184
192 public function listAvailablePrinters()
193 {
194 global $conf, $langs;
195 $error = 0;
196
197 $html = '<tr class="liste_titre">';
198 $html .= '<td>'.$langs->trans('IPP_Uri').'</td>';
199 $html .= '<td>'.$langs->trans('IPP_Name').'</td>';
200 $html .= '<td>'.$langs->trans('IPP_State').'</td>';
201 $html .= '<td>'.$langs->trans('IPP_State_reason').'</td>';
202 $html .= '<td>'.$langs->trans('IPP_State_reason1').'</td>';
203 $html .= '<td>'.$langs->trans('IPP_BW').'</td>';
204 $html .= '<td>'.$langs->trans('IPP_Color').'</td>';
205 //$html.= '<td>'.$langs->trans('IPP_Device').'</td>';
206 $html .= '<td>'.$langs->trans('IPP_Media').'</td>';
207 $html .= '<td>'.$langs->trans('IPP_Supported').'</td>';
208 $html .= '<td class="center">'.$langs->trans("Select").'</td>';
209 $html .= "</tr>\n";
210 $list = $this->getlistAvailablePrinters();
211 foreach ($list as $value) {
212 $printer_det = $this->getPrinterDetail($value);
213 '@phan-var-force stdClass $printer_det';
214 $html .= '<tr class="oddeven">';
215 $html .= '<td>'.$value.'</td>';
216 //$html.= '<td><pre>'.print_r($printer_det,true).'</pre></td>';
217 $html .= '<td>'.$printer_det->printer_name->_value0.'</td>';
218 $html .= '<td>'.$langs->trans('STATE_IPP_'.$printer_det->printer_state->_value0).'</td>';
219 $html .= '<td>'.$langs->trans('STATE_IPP_'.$printer_det->printer_state_reasons->_value0).'</td>';
220 $html .= '<td>'.(!empty($printer_det->printer_state_reasons->_value1) ? $langs->trans('STATE_IPP_'.$printer_det->printer_state_reasons->_value1) : '').'</td>';
221 $html .= '<td>'.$langs->trans('IPP_COLOR_'.$printer_det->printer_type->_value2).'</td>';
222 $html .= '<td>'.$langs->trans('IPP_COLOR_'.$printer_det->printer_type->_value3).'</td>';
223 //$html.= '<td>'.$printer_det->device_uri->_value0.'</td>';
224 $html .= '<td>'.$printer_det->media_default->_value0.'</td>';
225 $html .= '<td>'.$langs->trans('MEDIA_IPP_'.$printer_det->media_type_supported->_value1).'</td>';
226 // Default
227 $html .= '<td class="center">';
228 if ($conf->global->PRINTIPP_URI_DEFAULT == $value) {
229 $html .= img_picto($langs->trans("Default"), 'on');
230 } else {
231 $html .= '<a href="'.$_SERVER["PHP_SELF"].'?action=setvalue&token='.newToken().'&mode=test&varname=PRINTIPP_URI_DEFAULT&driver=printipp&value='.urlencode($value).'" alt="'.$langs->trans("Default").'">'.img_picto($langs->trans("Disabled"), 'off').'</a>';
232 }
233 $html .= '</td>';
234 $html .= '</tr>'."\n";
235 }
236 $this->resprint = $html;
237 return $error;
238 }
239
245 public function getlistAvailablePrinters()
246 {
247 global $conf, $db;
248 include_once DOL_DOCUMENT_ROOT.'/includes/printipp/CupsPrintIPP.php';
249 $ipp = new CupsPrintIPP();
250 $ipp->setLog(DOL_DATA_ROOT.'/dolibarr_printipp.log', 'file', 3); // logging very verbose
251 $ipp->setHost($this->host);
252 $ipp->setPort($this->port);
253 $ipp->setUserName($this->userid);
254 if (!empty($this->user)) {
255 $ipp->setAuthentication($this->user, $this->password);
256 }
257 $ipp->getPrinters();
258 return $ipp->available_printers;
259 }
260
267 private function getPrinterDetail($uri)
268 {
269 global $conf, $db;
270
271 include_once DOL_DOCUMENT_ROOT.'/includes/printipp/CupsPrintIPP.php';
272 $ipp = new CupsPrintIPP();
273 $ipp->setLog(DOL_DATA_ROOT.'/dolibarr_printipp.log', 'file', 3); // logging very verbose
274 $ipp->setHost($this->host);
275 $ipp->setPort($this->port);
276 $ipp->setUserName($this->userid);
277 if (!empty($this->user)) {
278 $ipp->setAuthentication($this->user, $this->password);
279 }
280 $ipp->setPrinterURI($uri);
281 $ipp->getPrinterAttributes();
282 return $ipp->printer_attributes;
283 }
284
292 public function listJobs($module = null)
293 {
294 global $langs;
295
296 $error = 0;
297 $html = '';
298 include_once DOL_DOCUMENT_ROOT.'/includes/printipp/CupsPrintIPP.php';
299 $ipp = new CupsPrintIPP();
300 $ipp->setLog(DOL_DATA_ROOT.'/dolibarr_printipp.log', 'file', 3); // logging very verbose
301 $ipp->setHost($this->host);
302 $ipp->setPort($this->port);
303 $ipp->setUserName($this->userid);
304 if (!empty($this->user)) {
305 $ipp->setAuthentication($this->user, $this->password);
306 }
307 // select printer uri for module order, propal,...
308 $sql = "SELECT rowid, printer_uri, printer_name";
309 $sql .= " FROM ".MAIN_DB_PREFIX."printer_ipp WHERE module = '".$this->db->escape((string) $module)."'";
310 $result = $this->db->query($sql);
311 if ($result) {
312 $obj = $this->db->fetch_object($result);
313 if ($obj) {
314 $ipp->setPrinterURI($obj->printer_uri);
315 } else {
316 // All printers
317 $ipp->setPrinterURI("ipp://localhost:631/printers/");
318 }
319 }
320 // Getting Jobs
321 try {
322 $ipp->getJobs(false, 0, 'completed', false);
323 } catch (Exception $e) {
324 $this->errors[] = $e->getMessage();
325 $error++;
326 }
327 $html .= '<table width="100%" class="noborder">';
328 $html .= '<tr class="liste_titre">';
329 $html .= '<td>ID</td>';
330 $html .= '<td>'.$langs->trans("Date").'</td>';
331 $html .= '<td>'.$langs->trans("Owner").'</td>';
332 $html .= '<td>'.$langs->trans("Printer").'</td>';
333 $html .= '<td>'.$langs->trans("File").'</td>';
334 $html .= '<td>'.$langs->trans("Status").'</td>';
335 $html .= '<td>Job URI</td>';
336 $html .= '</tr>'."\n";
337
338 $jobs = $ipp->jobs_attributes;
339
340 //$html .= '<pre>'.print_r($jobs, true).'</pre>';
341
342 foreach ($jobs as $value) {
343 $html .= '<tr class="oddeven">';
344 $html .= '<td>'.$value->job_id->_value0.'</td>';
345 $html .= '<td>'.$value->date_time_at_creation->_value0.'</td>';
346 $html .= '<td>'.$value->job_originating_user_name->_value0.'</td>';
347 $html .= '<td>'.$value->job_printer_uri->_value0.'</td>';
348 $file = $value->job_name->_value0;
349 $html .= '<td class="tdoverflowmax200" title="'.dolPrintHTMLForAttribute($file).'">'.dolPrintHTML($file).'</td>';
350 $html .= '<td>'.$value->job_state->_value0.'</td>';
351 $html .= '<td>'.$value->job_uri->_value0.'</td>';
352 $html .= '</tr>';
353 }
354 $html .= "</table>";
355 $this->resprint = $html;
356 return $error;
357 }
358}
Parent class of emailing target selectors modules.
Class to provide printing with PrintIPP.
getPrinterDetail($uri)
Get printer detail.
listJobs($module=null)
List jobs print.
printFile($file, $module, $subdir='')
Print selected file.
__construct($db)
Constructor.
listAvailablePrinters()
Return list of available printers.
getlistAvailablePrinters()
Return list of available printers.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
dolPrintHTML($s, $allowiframe=0)
Return a string (that can be on several lines) ready to be output on a HTML page.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
newToken()
Return the value of token currently saved into session with name 'newtoken'.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
conf($dolibarr_main_document_root)
Load conf file (file must exists)
Definition inc.php:420
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79
$conf db user
Active Directory does not allow anonymous connections.
Definition repair.php:153