printipp.modules.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. <?php
  2. /*
  3. * Copyright (C) 2014-2020 Frederic France <frederic.france@netlogic.fr>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. * or see https://www.gnu.org/
  18. */
  19. /**
  20. * \file htdocs/core/modules/printing/printipp.modules.php
  21. * \ingroup printing
  22. * \brief File to provide printing with PrintIPP
  23. */
  24. include_once DOL_DOCUMENT_ROOT.'/core/modules/printing/modules_printing.php';
  25. /**
  26. * Class to provide printing with PrintIPP
  27. */
  28. class printing_printipp extends PrintingDriver
  29. {
  30. /**
  31. * @var string module name
  32. */
  33. public $name = 'printipp';
  34. /**
  35. * @var string module description
  36. */
  37. public $desc = 'PrintIPPDesc';
  38. /**
  39. * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png
  40. */
  41. public $picto = 'printer';
  42. /**
  43. * @var string Constant name
  44. */
  45. public $active = 'PRINTING_PRINTIPP';
  46. /**
  47. * @var array array of setup value
  48. */
  49. public $conf = array();
  50. /**
  51. * @var string host
  52. */
  53. public $host;
  54. /**
  55. * @var string port
  56. */
  57. public $port;
  58. /**
  59. * @var string username
  60. */
  61. public $userid;
  62. /**
  63. * @var string login for printer host
  64. */
  65. public $user;
  66. /**
  67. * @var string password for printer host
  68. */
  69. public $password;
  70. /**
  71. * @var string Error code (or message)
  72. */
  73. public $error = '';
  74. /**
  75. * @var string[] Error codes (or messages)
  76. */
  77. public $errors = array();
  78. /**
  79. * @var DoliDB Database handler.
  80. */
  81. public $db;
  82. const LANGFILE = 'printipp';
  83. /**
  84. * Constructor
  85. *
  86. * @param DoliDB $db Database handler
  87. */
  88. public function __construct($db)
  89. {
  90. global $conf;
  91. $this->db = $db;
  92. $this->host = $conf->global->PRINTIPP_HOST;
  93. $this->port = $conf->global->PRINTIPP_PORT;
  94. $this->user = $conf->global->PRINTIPP_USER;
  95. $this->password = $conf->global->PRINTIPP_PASSWORD;
  96. $this->conf[] = array('varname'=>'PRINTIPP_HOST', 'required'=>1, 'example'=>'localhost', 'type'=>'text');
  97. $this->conf[] = array('varname'=>'PRINTIPP_PORT', 'required'=>1, 'example'=>'631', 'type'=>'text');
  98. $this->conf[] = array('varname'=>'PRINTIPP_USER', 'required'=>0, 'example'=>'', 'type'=>'text', 'moreattributes'=>'autocomplete="off"');
  99. $this->conf[] = array('varname'=>'PRINTIPP_PASSWORD', 'required'=>0, 'example'=>'', 'type'=>'password', 'moreattributes'=>'autocomplete="off"');
  100. $this->conf[] = array('enabled'=>1, 'type'=>'submit');
  101. }
  102. /**
  103. * Print selected file
  104. *
  105. * @param string $file file
  106. * @param string $module module
  107. * @param string $subdir subdirectory of document like for expedition subdir is sendings
  108. *
  109. * @return int 0 if OK, >0 if KO
  110. */
  111. public function printFile($file, $module, $subdir = '')
  112. {
  113. global $conf, $user;
  114. $error = 0;
  115. include_once DOL_DOCUMENT_ROOT.'/includes/printipp/CupsPrintIPP.php';
  116. $ipp = new CupsPrintIPP();
  117. $ipp->setLog(DOL_DATA_ROOT.'/dolibarr_printipp.log', 'file', 3); // logging very verbose
  118. $ipp->setHost($this->host);
  119. $ipp->setPort($this->port);
  120. $ipp->setJobName($file, true);
  121. $ipp->setUserName($this->userid);
  122. // Set default number of copy
  123. $ipp->setCopies(1);
  124. if (!empty($this->user)) {
  125. $ipp->setAuthentication($this->user, $this->password);
  126. }
  127. // select printer uri for module order, propal,...
  128. $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);
  129. $result = $this->db->query($sql);
  130. if ($result) {
  131. $obj = $this->db->fetch_object($result);
  132. if ($obj) {
  133. dol_syslog("Found a default printer for user ".$user->id." = ".$obj->printer_id);
  134. $ipp->setPrinterURI($obj->printer_id);
  135. // Set number of copy
  136. $ipp->setCopies($obj->copy);
  137. } else {
  138. if (!empty($conf->global->PRINTIPP_URI_DEFAULT)) {
  139. dol_syslog("Will use default printer conf->global->PRINTIPP_URI_DEFAULT = ".$conf->global->PRINTIPP_URI_DEFAULT);
  140. $ipp->setPrinterURI($conf->global->PRINTIPP_URI_DEFAULT);
  141. } else {
  142. $this->errors[] = 'NoDefaultPrinterDefined';
  143. $error++;
  144. return $error;
  145. }
  146. }
  147. } else {
  148. dol_print_error($this->db);
  149. }
  150. $fileprint = $conf->{$module}->dir_output;
  151. if ($subdir != '') {
  152. $fileprint .= '/'.$subdir;
  153. }
  154. $fileprint .= '/'.$file;
  155. $ipp->setData($fileprint);
  156. try {
  157. $ipp->printJob();
  158. } catch (Exception $e) {
  159. $this->errors[] = $e->getMessage();
  160. $error++;
  161. }
  162. if ($error == 0) {
  163. $this->errors[] = 'PRINTIPP: Job added';
  164. }
  165. return $error;
  166. }
  167. /**
  168. * Return list of available printers
  169. *
  170. * @return int 0 if OK, >0 if KO
  171. */
  172. public function listAvailablePrinters()
  173. {
  174. global $conf, $langs;
  175. $error = 0;
  176. $html = '<tr class="liste_titre">';
  177. $html .= '<td>'.$langs->trans('IPP_Uri').'</td>';
  178. $html .= '<td>'.$langs->trans('IPP_Name').'</td>';
  179. $html .= '<td>'.$langs->trans('IPP_State').'</td>';
  180. $html .= '<td>'.$langs->trans('IPP_State_reason').'</td>';
  181. $html .= '<td>'.$langs->trans('IPP_State_reason1').'</td>';
  182. $html .= '<td>'.$langs->trans('IPP_BW').'</td>';
  183. $html .= '<td>'.$langs->trans('IPP_Color').'</td>';
  184. //$html.= '<td>'.$langs->trans('IPP_Device').'</td>';
  185. $html .= '<td>'.$langs->trans('IPP_Media').'</td>';
  186. $html .= '<td>'.$langs->trans('IPP_Supported').'</td>';
  187. $html .= '<td class="center">'.$langs->trans("Select").'</td>';
  188. $html .= "</tr>\n";
  189. $list = $this->getlistAvailablePrinters();
  190. foreach ($list as $value) {
  191. $printer_det = $this->getPrinterDetail($value);
  192. $html .= '<tr class="oddeven">';
  193. $html .= '<td>'.$value.'</td>';
  194. //$html.= '<td><pre>'.print_r($printer_det,true).'</pre></td>';
  195. $html .= '<td>'.$printer_det->printer_name->_value0.'</td>';
  196. $html .= '<td>'.$langs->trans('STATE_IPP_'.$printer_det->printer_state->_value0).'</td>';
  197. $html .= '<td>'.$langs->trans('STATE_IPP_'.$printer_det->printer_state_reasons->_value0).'</td>';
  198. $html .= '<td>'.(!empty($printer_det->printer_state_reasons->_value1) ? $langs->trans('STATE_IPP_'.$printer_det->printer_state_reasons->_value1) : '').'</td>';
  199. $html .= '<td>'.$langs->trans('IPP_COLOR_'.$printer_det->printer_type->_value2).'</td>';
  200. $html .= '<td>'.$langs->trans('IPP_COLOR_'.$printer_det->printer_type->_value3).'</td>';
  201. //$html.= '<td>'.$printer_det->device_uri->_value0.'</td>';
  202. $html .= '<td>'.$printer_det->media_default->_value0.'</td>';
  203. $html .= '<td>'.$langs->trans('MEDIA_IPP_'.$printer_det->media_type_supported->_value1).'</td>';
  204. // Defaut
  205. $html .= '<td class="center">';
  206. if ($conf->global->PRINTIPP_URI_DEFAULT == $value) {
  207. $html .= img_picto($langs->trans("Default"), 'on');
  208. } else {
  209. $html .= '<a href="'.$_SERVER["PHP_SELF"].'?action=setvalue&amp;token='.newToken().'&amp;mode=test&amp;varname=PRINTIPP_URI_DEFAULT&amp;driver=printipp&amp;value='.urlencode($value).'" alt="'.$langs->trans("Default").'">'.img_picto($langs->trans("Disabled"), 'off').'</a>';
  210. }
  211. $html .= '</td>';
  212. $html .= '</tr>'."\n";
  213. }
  214. $this->resprint = $html;
  215. return $error;
  216. }
  217. /**
  218. * Return list of available printers
  219. *
  220. * @return array list of printers
  221. */
  222. public function getlistAvailablePrinters()
  223. {
  224. global $conf, $db;
  225. include_once DOL_DOCUMENT_ROOT.'/includes/printipp/CupsPrintIPP.php';
  226. $ipp = new CupsPrintIPP();
  227. $ipp->setLog(DOL_DATA_ROOT.'/dolibarr_printipp.log', 'file', 3); // logging very verbose
  228. $ipp->setHost($this->host);
  229. $ipp->setPort($this->port);
  230. $ipp->setUserName($this->userid);
  231. if (!empty($this->user)) {
  232. $ipp->setAuthentication($this->user, $this->password);
  233. }
  234. $ipp->getPrinters();
  235. return $ipp->available_printers;
  236. }
  237. /**
  238. * Get printer detail
  239. *
  240. * @param string $uri URI
  241. * @return array List of attributes
  242. */
  243. private function getPrinterDetail($uri)
  244. {
  245. global $conf, $db;
  246. include_once DOL_DOCUMENT_ROOT.'/includes/printipp/CupsPrintIPP.php';
  247. $ipp = new CupsPrintIPP();
  248. $ipp->setLog(DOL_DATA_ROOT.'/dolibarr_printipp.log', 'file', 3); // logging very verbose
  249. $ipp->setHost($this->host);
  250. $ipp->setPort($this->port);
  251. $ipp->setUserName($this->userid);
  252. if (!empty($this->user)) {
  253. $ipp->setAuthentication($this->user, $this->password);
  254. }
  255. $ipp->setPrinterURI($uri);
  256. $ipp->getPrinterAttributes();
  257. return $ipp->printer_attributes;
  258. }
  259. /**
  260. * List jobs print
  261. *
  262. * @param string $module module
  263. *
  264. * @return int 0 if OK, >0 if KO
  265. */
  266. public function listJobs($module)
  267. {
  268. global $conf;
  269. $error = 0;
  270. $html = '';
  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. // select printer uri for module order, propal,...
  281. $sql = "SELECT rowid,printer_uri,printer_name FROM ".MAIN_DB_PREFIX."printer_ipp WHERE module = '".$this->db->escape($module)."'";
  282. $result = $this->db->query($sql);
  283. if ($result) {
  284. $obj = $this->db->fetch_object($result);
  285. if ($obj) {
  286. $ipp->setPrinterURI($obj->printer_uri);
  287. } else {
  288. // All printers
  289. $ipp->setPrinterURI("ipp://localhost:631/printers/");
  290. }
  291. }
  292. // Getting Jobs
  293. try {
  294. $ipp->getJobs(false, 0, 'completed', false);
  295. } catch (Exception $e) {
  296. $this->errors[] = $e->getMessage();
  297. $error++;
  298. }
  299. $html .= '<table width="100%" class="noborder">';
  300. $html .= '<tr class="liste_titre">';
  301. $html .= '<td>Id</td>';
  302. $html .= '<td>Owner</td>';
  303. $html .= '<td>Printer</td>';
  304. $html .= '<td>File</td>';
  305. $html .= '<td>Status</td>';
  306. $html .= '<td>Cancel</td>';
  307. $html .= '</tr>'."\n";
  308. $jobs = $ipp->jobs_attributes;
  309. //$html .= '<pre>'.print_r($jobs,true).'</pre>';
  310. foreach ($jobs as $value) {
  311. $html .= '<tr class="oddeven">';
  312. $html .= '<td>'.$value->job_id->_value0.'</td>';
  313. $html .= '<td>'.$value->job_originating_user_name->_value0.'</td>';
  314. $html .= '<td>'.$value->printer_uri->_value0.'</td>';
  315. $html .= '<td>'.$value->job_name->_value0.'</td>';
  316. $html .= '<td>'.$value->job_state->_value0.'</td>';
  317. $html .= '<td>'.$value->job_uri->_value0.'</td>';
  318. $html .= '</tr>';
  319. }
  320. $html .= "</table>";
  321. $this->resprint = $html;
  322. return $error;
  323. }
  324. }