dolibarr 24.0.0-beta
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 = getMultidirOutput(null, $module) . '/' . $file;
167 $ipp->setData($fileprint);
168 // Tell CUPS what we are sending so it picks the right filter chain (otherwise ODT/PDF arrive as raw and print as ASCII garbage).
169 $extension = strtolower(pathinfo($fileprint, PATHINFO_EXTENSION));
170 $mimebyext = array(
171 'pdf' => 'application/pdf',
172 'odt' => 'application/vnd.oasis.opendocument.text',
173 'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
174 'odp' => 'application/vnd.oasis.opendocument.presentation',
175 'ps' => 'application/postscript',
176 'eps' => 'application/postscript',
177 'txt' => 'text/plain',
178 'png' => 'image/png',
179 'jpg' => 'image/jpeg',
180 'jpeg' => 'image/jpeg',
181 );
182 if (isset($mimebyext[$extension])) {
183 $ipp->setMimeMediaType($mimebyext[$extension]);
184 }
185 try {
186 $ipp->printJob();
187 } catch (Exception $e) {
188 $this->errors[] = $e->getMessage();
189 $error++;
190 }
191 if ($error == 0) {
192 $this->errors[] = 'PRINTIPP: Job added';
193 }
194
195 return $error;
196 }
197
205 public function listAvailablePrinters()
206 {
207 global $conf, $langs;
208 $error = 0;
209
210 $html = '<tr class="liste_titre">';
211 $html .= '<td>'.$langs->trans('IPP_Uri').'</td>';
212 $html .= '<td>'.$langs->trans('IPP_Name').'</td>';
213 $html .= '<td>'.$langs->trans('IPP_State').'</td>';
214 $html .= '<td>'.$langs->trans('IPP_State_reason').'</td>';
215 $html .= '<td>'.$langs->trans('IPP_State_reason1').'</td>';
216 $html .= '<td>'.$langs->trans('IPP_BW').'</td>';
217 $html .= '<td>'.$langs->trans('IPP_Color').'</td>';
218 //$html.= '<td>'.$langs->trans('IPP_Device').'</td>';
219 $html .= '<td>'.$langs->trans('IPP_Media').'</td>';
220 $html .= '<td>'.$langs->trans('IPP_Supported').'</td>';
221 $html .= '<td class="center">'.$langs->trans("Select").'</td>';
222 $html .= "</tr>\n";
223 $list = $this->getlistAvailablePrinters();
224 foreach ($list as $value) {
225 $printer_det = $this->getPrinterDetail($value);
226 '@phan-var-force stdClass $printer_det';
227 $html .= '<tr class="oddeven">';
228 $html .= '<td>'.$value.'</td>';
229 //$html.= '<td><pre>'.print_r($printer_det,true).'</pre></td>';
230 $html .= '<td>'.$printer_det->printer_name->_value0.'</td>';
231 $html .= '<td>'.$langs->trans('STATE_IPP_'.$printer_det->printer_state->_value0).'</td>';
232 $html .= '<td>'.$langs->trans('STATE_IPP_'.$printer_det->printer_state_reasons->_value0).'</td>';
233 $html .= '<td>'.(!empty($printer_det->printer_state_reasons->_value1) ? $langs->trans('STATE_IPP_'.$printer_det->printer_state_reasons->_value1) : '').'</td>';
234 $html .= '<td>'.(!empty($printer_det->printer_type->_value2) ? $langs->trans('IPP_COLOR_'.$printer_det->printer_type->_value2) : '').'</td>';
235 $html .= '<td>'.(!empty($printer_det->printer_type->_value3) ? $langs->trans('IPP_COLOR_'.$printer_det->printer_type->_value3) : '').'</td>';
236 //$html.= '<td>'.$printer_det->device_uri->_value0.'</td>';
237 $html .= '<td>'.$printer_det->media_default->_value0.'</td>';
238 $html .= '<td>'.(!empty($printer_det->media_type_supported->_value1) ? $langs->trans('MEDIA_IPP_'.$printer_det->media_type_supported->_value1) : '').'</td>';
239 // Default
240 $html .= '<td class="center">';
241 if (getDolGlobalString('PRINTIPP_URI_DEFAULT') == $value) {
242 $html .= img_picto($langs->trans("Default"), 'on');
243 } else {
244 $html .= '<a href="'.dolBuildUrl($_SERVER["PHP_SELF"], ['action' => 'setvalue', 'mode' => 'test', 'varname' => 'PRINTIPP_URI_DEFAULT', 'driver' => 'printipp', 'value' => $value], true).'" alt="'.$langs->trans("Default").'">'.img_picto($langs->trans("Disabled"), 'off').'</a>';
245 }
246 $html .= '</td>';
247 $html .= '</tr>'."\n";
248 }
249 $this->resprint = $html;
250 return $error;
251 }
252
258 public function getlistAvailablePrinters()
259 {
260 include_once DOL_DOCUMENT_ROOT.'/includes/printipp/CupsPrintIPP.php';
261 $ipp = new CupsPrintIPP();
262 $ipp->setLog(DOL_DATA_ROOT.'/dolibarr_printipp.log', 'file', 3); // logging very verbose
263 $ipp->setHost($this->host);
264 $ipp->setPort($this->port);
265 $ipp->setUserName($this->userid);
266 if (!empty($this->user)) {
267 $ipp->setAuthentication($this->user, $this->password);
268 }
269 try {
270 $ipp->getPrinters();
271 } catch (Exception $e) {
272 setEventMessage($e->getMessage(), 'errors');
273 }
274
275 return $ipp->available_printers;
276 }
277
284 private function getPrinterDetail($uri)
285 {
286 global $conf, $db;
287
288 include_once DOL_DOCUMENT_ROOT.'/includes/printipp/CupsPrintIPP.php';
289 $ipp = new CupsPrintIPP();
290 $ipp->setLog(DOL_DATA_ROOT.'/dolibarr_printipp.log', 'file', 3); // logging very verbose
291 $ipp->setHost($this->host);
292 $ipp->setPort($this->port);
293 $ipp->setUserName($this->userid);
294 if (!empty($this->user)) {
295 $ipp->setAuthentication($this->user, $this->password);
296 }
297 $ipp->setPrinterURI($uri);
298 $ipp->getPrinterAttributes();
299 return $ipp->printer_attributes;
300 }
301
309 public function listJobs($module = null)
310 {
311 global $langs;
312
313 $error = 0;
314 $html = '';
315 include_once DOL_DOCUMENT_ROOT.'/includes/printipp/CupsPrintIPP.php';
316 $ipp = new CupsPrintIPP();
317 $ipp->setLog(DOL_DATA_ROOT.'/dolibarr_printipp.log', 'file', 3); // logging very verbose
318 $ipp->setHost($this->host);
319 $ipp->setPort($this->port);
320 $ipp->setUserName($this->userid);
321 if (!empty($this->user)) {
322 $ipp->setAuthentication($this->user, $this->password);
323 }
324 // select printer uri for module order, propal,...
325 $sql = "SELECT rowid, printer_uri, printer_name";
326 $sql .= " FROM ".MAIN_DB_PREFIX."printer_ipp WHERE module = '".$this->db->escape((string) $module)."'";
327 $result = $this->db->query($sql);
328 if ($result) {
329 $obj = $this->db->fetch_object($result);
330 if ($obj) {
331 $ipp->setPrinterURI($obj->printer_uri);
332 } else {
333 // All printers
334 $ipp->setPrinterURI("ipp://localhost:631/printers/");
335 }
336 }
337 // Getting Jobs
338 try {
339 $ipp->getJobs(false, 0, 'completed', false);
340 } catch (Exception $e) {
341 $this->errors[] = $e->getMessage();
342 $error++;
343 }
344 $html .= '<table width="100%" class="noborder">';
345 $html .= '<tr class="liste_titre">';
346 $html .= '<td>ID</td>';
347 $html .= '<td>'.$langs->trans("Date").'</td>';
348 $html .= '<td>'.$langs->trans("Owner").'</td>';
349 $html .= '<td>'.$langs->trans("Printer").'</td>';
350 $html .= '<td>'.$langs->trans("File").'</td>';
351 $html .= '<td>'.$langs->trans("Status").'</td>';
352 $html .= '<td>Job URI</td>';
353 $html .= '</tr>'."\n";
354
355 $jobs = $ipp->jobs_attributes;
356
357 //$html .= '<pre>'.print_r($jobs, true).'</pre>';
358
359 foreach ($jobs as $value) {
360 $html .= '<tr class="oddeven">';
361 $html .= '<td>'.$value->job_id->_value0.'</td>';
362 $html .= '<td>'.$value->date_time_at_creation->_value0.'</td>';
363 $html .= '<td>'.$value->job_originating_user_name->_value0.'</td>';
364 $html .= '<td>'.$value->job_printer_uri->_value0.'</td>';
365 $file = $value->job_name->_value0;
366 $html .= '<td class="tdoverflowmax200" title="'.dolPrintHTMLForAttribute($file).'">'.dolPrintHTML($file).'</td>';
367 $html .= '<td>'.$value->job_state->_value0.'</td>';
368 $html .= '<td>'.$value->job_uri->_value0.'</td>';
369 $html .= '</tr>';
370 }
371 $html .= "</table>";
372 $this->resprint = $html;
373 return $error;
374 }
375}
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, $moreallowedtags=array())
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.
getMultidirOutput($object, $module='', $forobject=0, $mode='output')
Return the full path of the directory where a module (or an object of a module) stores its files.
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:134