dolibarr 23.0.3
printipp.modules.php
Go to the documentation of this file.
1<?php
2/*
3 * Copyright (C) 2014-2025 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 // Tell CUPS what we are sending so it picks the right filter chain (otherwise ODT/PDF arrive as raw and print as ASCII garbage).
173 $extension = strtolower(pathinfo($fileprint, PATHINFO_EXTENSION));
174 $mimebyext = array(
175 'pdf' => 'application/pdf',
176 'odt' => 'application/vnd.oasis.opendocument.text',
177 'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
178 'odp' => 'application/vnd.oasis.opendocument.presentation',
179 'ps' => 'application/postscript',
180 'eps' => 'application/postscript',
181 'txt' => 'text/plain',
182 'png' => 'image/png',
183 'jpg' => 'image/jpeg',
184 'jpeg' => 'image/jpeg',
185 );
186 if (isset($mimebyext[$extension])) {
187 $ipp->setMimeMediaType($mimebyext[$extension]);
188 }
189 try {
190 $ipp->printJob();
191 } catch (Exception $e) {
192 $this->errors[] = $e->getMessage();
193 $error++;
194 }
195 if ($error == 0) {
196 $this->errors[] = 'PRINTIPP: Job added';
197 }
198
199 return $error;
200 }
201
209 public function listAvailablePrinters()
210 {
211 global $conf, $langs;
212 $error = 0;
213
214 $html = '<tr class="liste_titre">';
215 $html .= '<td>'.$langs->trans('IPP_Uri').'</td>';
216 $html .= '<td>'.$langs->trans('IPP_Name').'</td>';
217 $html .= '<td>'.$langs->trans('IPP_State').'</td>';
218 $html .= '<td>'.$langs->trans('IPP_State_reason').'</td>';
219 $html .= '<td>'.$langs->trans('IPP_State_reason1').'</td>';
220 $html .= '<td>'.$langs->trans('IPP_BW').'</td>';
221 $html .= '<td>'.$langs->trans('IPP_Color').'</td>';
222 //$html.= '<td>'.$langs->trans('IPP_Device').'</td>';
223 $html .= '<td>'.$langs->trans('IPP_Media').'</td>';
224 $html .= '<td>'.$langs->trans('IPP_Supported').'</td>';
225 $html .= '<td class="center">'.$langs->trans("Select").'</td>';
226 $html .= "</tr>\n";
227 $list = $this->getlistAvailablePrinters();
228 foreach ($list as $value) {
229 $printer_det = $this->getPrinterDetail($value);
230 '@phan-var-force stdClass $printer_det';
231 $html .= '<tr class="oddeven">';
232 $html .= '<td>'.$value.'</td>';
233 //$html.= '<td><pre>'.print_r($printer_det,true).'</pre></td>';
234 $html .= '<td>'.$printer_det->printer_name->_value0.'</td>';
235 $html .= '<td>'.$langs->trans('STATE_IPP_'.$printer_det->printer_state->_value0).'</td>';
236 $html .= '<td>'.$langs->trans('STATE_IPP_'.$printer_det->printer_state_reasons->_value0).'</td>';
237 $html .= '<td>'.(!empty($printer_det->printer_state_reasons->_value1) ? $langs->trans('STATE_IPP_'.$printer_det->printer_state_reasons->_value1) : '').'</td>';
238 $html .= '<td>'.$langs->trans('IPP_COLOR_'.$printer_det->printer_type->_value2).'</td>';
239 $html .= '<td>'.$langs->trans('IPP_COLOR_'.$printer_det->printer_type->_value3).'</td>';
240 //$html.= '<td>'.$printer_det->device_uri->_value0.'</td>';
241 $html .= '<td>'.$printer_det->media_default->_value0.'</td>';
242 $html .= '<td>'.$langs->trans('MEDIA_IPP_'.$printer_det->media_type_supported->_value1).'</td>';
243 // Default
244 $html .= '<td class="center">';
245 if (getDolGlobalString('PRINTIPP_URI_DEFAULT') == $value) {
246 $html .= img_picto($langs->trans("Default"), 'on');
247 } else {
248 $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>';
249 }
250 $html .= '</td>';
251 $html .= '</tr>'."\n";
252 }
253 $this->resprint = $html;
254 return $error;
255 }
256
262 public function getlistAvailablePrinters()
263 {
264 include_once DOL_DOCUMENT_ROOT.'/includes/printipp/CupsPrintIPP.php';
265 $ipp = new CupsPrintIPP();
266 $ipp->setLog(DOL_DATA_ROOT.'/dolibarr_printipp.log', 'file', 3); // logging very verbose
267 $ipp->setHost($this->host);
268 $ipp->setPort($this->port);
269 $ipp->setUserName($this->userid);
270 if (!empty($this->user)) {
271 $ipp->setAuthentication($this->user, $this->password);
272 }
273 try {
274 $ipp->getPrinters();
275 } catch (Exception $e) {
276 setEventMessage($e->getMessage(), 'errors');
277 }
278
279 return $ipp->available_printers;
280 }
281
288 private function getPrinterDetail($uri)
289 {
290 global $conf, $db;
291
292 include_once DOL_DOCUMENT_ROOT.'/includes/printipp/CupsPrintIPP.php';
293 $ipp = new CupsPrintIPP();
294 $ipp->setLog(DOL_DATA_ROOT.'/dolibarr_printipp.log', 'file', 3); // logging very verbose
295 $ipp->setHost($this->host);
296 $ipp->setPort($this->port);
297 $ipp->setUserName($this->userid);
298 if (!empty($this->user)) {
299 $ipp->setAuthentication($this->user, $this->password);
300 }
301 $ipp->setPrinterURI($uri);
302 $ipp->getPrinterAttributes();
303 return $ipp->printer_attributes;
304 }
305
313 public function listJobs($module = null)
314 {
315 global $langs;
316
317 $error = 0;
318 $html = '';
319 include_once DOL_DOCUMENT_ROOT.'/includes/printipp/CupsPrintIPP.php';
320 $ipp = new CupsPrintIPP();
321 $ipp->setLog(DOL_DATA_ROOT.'/dolibarr_printipp.log', 'file', 3); // logging very verbose
322 $ipp->setHost($this->host);
323 $ipp->setPort($this->port);
324 $ipp->setUserName($this->userid);
325 if (!empty($this->user)) {
326 $ipp->setAuthentication($this->user, $this->password);
327 }
328 // select printer uri for module order, propal,...
329 $sql = "SELECT rowid, printer_uri, printer_name";
330 $sql .= " FROM ".MAIN_DB_PREFIX."printer_ipp WHERE module = '".$this->db->escape((string) $module)."'";
331 $result = $this->db->query($sql);
332 if ($result) {
333 $obj = $this->db->fetch_object($result);
334 if ($obj) {
335 $ipp->setPrinterURI($obj->printer_uri);
336 } else {
337 // All printers
338 $ipp->setPrinterURI("ipp://localhost:631/printers/");
339 }
340 }
341 // Getting Jobs
342 try {
343 $ipp->getJobs(false, 0, 'completed', false);
344 } catch (Exception $e) {
345 $this->errors[] = $e->getMessage();
346 $error++;
347 }
348 $html .= '<table width="100%" class="noborder">';
349 $html .= '<tr class="liste_titre">';
350 $html .= '<td>ID</td>';
351 $html .= '<td>'.$langs->trans("Date").'</td>';
352 $html .= '<td>'.$langs->trans("Owner").'</td>';
353 $html .= '<td>'.$langs->trans("Printer").'</td>';
354 $html .= '<td>'.$langs->trans("File").'</td>';
355 $html .= '<td>'.$langs->trans("Status").'</td>';
356 $html .= '<td>Job URI</td>';
357 $html .= '</tr>'."\n";
358
359 $jobs = $ipp->jobs_attributes;
360
361 //$html .= '<pre>'.print_r($jobs, true).'</pre>';
362
363 foreach ($jobs as $value) {
364 $html .= '<tr class="oddeven">';
365 $html .= '<td>'.$value->job_id->_value0.'</td>';
366 $html .= '<td>'.$value->date_time_at_creation->_value0.'</td>';
367 $html .= '<td>'.$value->job_originating_user_name->_value0.'</td>';
368 $html .= '<td>'.$value->job_printer_uri->_value0.'</td>';
369 $file = $value->job_name->_value0;
370 $html .= '<td class="tdoverflowmax200" title="'.dolPrintHTMLForAttribute($file).'">'.dolPrintHTML($file).'</td>';
371 $html .= '<td>'.$value->job_state->_value0.'</td>';
372 $html .= '<td>'.$value->job_uri->_value0.'</td>';
373 $html .= '</tr>';
374 }
375 $html .= "</table>";
376 $this->resprint = $html;
377 return $error;
378 }
379}
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, $allowothertags=array())
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.
setEventMessage($mesgs, $style='mesgs', $noduplicate=0, $attop=0)
Set event message in dol_events session object.
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:426
$conf db user
Active Directory does not allow anonymous connections.
Definition repair.php:129