dolibarr 22.0.5
printgcp.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-2025 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';
28require_once DOL_DOCUMENT_ROOT.'/includes/OAuth/bootstrap.php';
29
30use OAuth\Common\Storage\DoliStorage;
31use OAuth\Common\Consumer\Credentials;
32
37{
41 public $name = 'printgcp';
42
46 public $desc = 'PrintGCPDesc';
47
51 public $picto = 'printer';
52
56 public $active = 'PRINTING_PRINTGCP';
57
61 public $conf = array();
62
66 public $google_id = '';
67
71 public $google_secret = '';
72
76 public $db;
77
81 private $OAUTH_SERVICENAME_GOOGLE = 'Google';
82
83 const LOGIN_URL = 'https://accounts.google.com/o/oauth2/token';
84 const PRINTERS_SEARCH_URL = 'https://www.google.com/cloudprint/search';
85 const PRINTERS_GET_JOBS = 'https://www.google.com/cloudprint/jobs';
86 const PRINT_URL = 'https://www.google.com/cloudprint/submit';
87
88
94 public function __construct($db)
95 {
96 global $conf, $langs, $dolibarr_main_url_root;
97
98 // Define $urlwithroot
99 $urlwithouturlroot = preg_replace('/'.preg_quote(DOL_URL_ROOT, '/').'$/i', '', trim($dolibarr_main_url_root));
100 $urlwithroot = $urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
101 //$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
102
103 $this->db = $db;
104
105 if (!$conf->oauth->enabled) {
106 $this->conf[] = array(
107 'varname' => 'PRINTGCP_INFO',
108 'info' => $langs->transnoentitiesnoconv("WarningModuleNotActive", "OAuth"),
109 'type' => 'info',
110 );
111 } else {
112 $keyforprovider = ''; // @FIXME
113
114 $this->google_id = getDolGlobalString('OAUTH_GOOGLE_ID');
115 $this->google_secret = getDolGlobalString('OAUTH_GOOGLE_SECRET');
116 // Token storage
117 $storage = new DoliStorage($this->db, $conf, $keyforprovider);
118 //$storage->clearToken($this->OAUTH_SERVICENAME_GOOGLE);
119 // Setup the credentials for the requests
120 $credentials = new Credentials(
121 $this->google_id,
122 $this->google_secret,
123 $urlwithroot.'/core/modules/oauth/google_oauthcallback.php'
124 );
125 $access = ($storage->hasAccessToken($this->OAUTH_SERVICENAME_GOOGLE) ? 'HasAccessToken' : 'NoAccessToken');
126 $serviceFactory = new \OAuth\ServiceFactory();
127 $apiService = $serviceFactory->createService($this->OAUTH_SERVICENAME_GOOGLE, $credentials, $storage, array());
128 '@phan-var-force OAuth\OAuth2\Service\Google $apiService'; // createService is only ServiceInterface
129 $token_ok = true;
130 try {
131 $token = $storage->retrieveAccessToken($this->OAUTH_SERVICENAME_GOOGLE);
132 } catch (Exception $e) {
133 $this->errors[] = $e->getMessage();
134 $token_ok = false;
135 }
136
137 $expire = false;
138 // Is token expired or will token expire in the next 30 seconds
139 if ($token_ok) {
140 $expire = ($token->getEndOfLife() !== -9002 && $token->getEndOfLife() !== -9001 && time() > ($token->getEndOfLife() - 30));
141 }
142
143 // Token expired so we refresh it
144 if ($token_ok && $expire) {
145 try {
146 // il faut sauvegarder le refresh token car google ne le donne qu'une seule fois
147 $refreshtoken = $token->getRefreshToken();
148 $token = $apiService->refreshAccessToken($token);
149 $token->setRefreshToken($refreshtoken);
150 $storage->storeAccessToken($this->OAUTH_SERVICENAME_GOOGLE, $token);
151 } catch (Exception $e) {
152 $this->errors[] = $e->getMessage();
153 }
154 }
155 if ($this->google_id != '' && $this->google_secret != '') {
156 $this->conf[] = array('varname' => 'PRINTGCP_INFO', 'info' => 'GoogleAuthConfigured', 'type' => 'info');
157 $this->conf[] = array(
158 'varname' => 'PRINTGCP_TOKEN_ACCESS',
159 'info' => $access,
160 'type' => 'info',
161 'renew' => $urlwithroot.'/core/modules/oauth/google_oauthcallback.php?state=userinfo_email,userinfo_profile,cloud_print&backtourl='.urlencode(DOL_URL_ROOT.'/printing/admin/printing.php?mode=setup&driver=printgcp'),
162 'delete' => ($storage->hasAccessToken($this->OAUTH_SERVICENAME_GOOGLE) ? $urlwithroot.'/core/modules/oauth/google_oauthcallback.php?action=delete&token='.newToken().'&backtourl='.urlencode(DOL_URL_ROOT.'/printing/admin/printing.php?mode=setup&driver=printgcp') : '')
163 );
164 if ($token_ok) {
165 $expiredat = '';
166
167 $refreshtoken = $token->getRefreshToken();
168
169 $endoflife = $token->getEndOfLife();
170
171 if ($endoflife == $token::EOL_NEVER_EXPIRES) {
172 $expiredat = $langs->trans("Never");
173 } elseif ($endoflife == $token::EOL_UNKNOWN) {
174 $expiredat = $langs->trans("Unknown");
175 } else {
176 $expiredat = dol_print_date($endoflife, "dayhour");
177 }
178
179 $this->conf[] = array('varname' => 'TOKEN_REFRESH', 'info' => ((!empty($refreshtoken)) ? 'Yes' : 'No'), 'type' => 'info');
180 $this->conf[] = array('varname' => 'TOKEN_EXPIRED', 'info' => ($expire ? 'Yes' : 'No'), 'type' => 'info');
181 $this->conf[] = array('varname' => 'TOKEN_EXPIRE_AT', 'info' => ($expiredat), 'type' => 'info');
182 }
183 /*
184 if ($storage->hasAccessToken($this->OAUTH_SERVICENAME_GOOGLE)) {
185 $this->conf[] = array('varname'=>'PRINTGCP_AUTHLINK', 'link'=>$urlwithroot.'/core/modules/oauth/google_oauthcallback.php?backtourl='.urlencode(DOL_URL_ROOT.'/printing/admin/printing.php?mode=setup&driver=printgcp'), 'type'=>'authlink');
186 $this->conf[] = array('varname'=>'DELETE_TOKEN', 'link'=>$urlwithroot.'/core/modules/oauth/google_oauthcallback.php?action=delete&token='.newToken().'&backtourl='.urlencode(DOL_URL_ROOT.'/printing/admin/printing.php?mode=setup&driver=printgcp'), 'type'=>'delete');
187 } else {
188 $this->conf[] = array('varname'=>'PRINTGCP_AUTHLINK', 'link'=>$urlwithroot.'/core/modules/oauth/google_oauthcallback.php?backtourl='.urlencode(DOL_URL_ROOT.'/printing/admin/printing.php?mode=setup&driver=printgcp'), 'type'=>'authlink');
189 }*/
190 } else {
191 $this->conf[] = array('varname' => 'PRINTGCP_INFO', 'info' => 'GoogleAuthNotConfigured', 'type' => 'info');
192 }
193 }
194 // do not display submit button
195 $this->conf[] = array('enabled' => 0, 'type' => 'submit');
196 }
197
203 public function listAvailablePrinters()
204 {
205 global $conf, $langs;
206 $error = 0;
207 $langs->load('printing');
208
209 $html = '<tr class="liste_titre">';
210 $html .= '<td>'.$langs->trans('GCP_Name').'</td>';
211 $html .= '<td>'.$langs->trans('GCP_displayName').'</td>';
212 $html .= '<td>'.$langs->trans('GCP_Id').'</td>';
213 $html .= '<td>'.$langs->trans('GCP_OwnerName').'</td>';
214 $html .= '<td>'.$langs->trans('GCP_State').'</td>';
215 $html .= '<td>'.$langs->trans('GCP_connectionStatus').'</td>';
216 $html .= '<td>'.$langs->trans('GCP_Type').'</td>';
217 $html .= '<td class="center">'.$langs->trans("Select").'</td>';
218 $html .= '</tr>'."\n";
219 $list = $this->getlistAvailableGcpPrinters();
220 //$html.= '<td><pre>'.print_r($list,true).'</pre></td>';
221 foreach ($list['available'] as $printer_det) {
222 $html .= '<tr class="oddeven">';
223 $html .= '<td>'.$printer_det['name'].'</td>';
224 $html .= '<td>'.$printer_det['displayName'].'</td>';
225 $html .= '<td>'.$printer_det['id'].'</td>'; // id to identify printer to use
226 $html .= '<td>'.$printer_det['ownerName'].'</td>';
227 $html .= '<td>'.$printer_det['status'].'</td>';
228 $html .= '<td>'.$langs->trans('STATE_'.$printer_det['connectionStatus']).'</td>';
229 $html .= '<td>'.$langs->trans('TYPE_'.$printer_det['type']).'</td>';
230 // Default
231 $html .= '<td class="center">';
232 if ($conf->global->PRINTING_GCP_DEFAULT == $printer_det['id']) {
233 $html .= img_picto($langs->trans("Default"), 'on');
234 } else {
235 $html .= '<a href="'.$_SERVER["PHP_SELF"].'?action=setvalue&token='.newToken().'&mode=test&varname=PRINTING_GCP_DEFAULT&driver=printgcp&value='.urlencode($printer_det['id']).'" alt="'.$langs->trans("Default").'">'.img_picto($langs->trans("Disabled"), 'off').'</a>';
236 }
237 $html .= '</td>';
238 $html .= '</tr>'."\n";
239 }
240 $this->resprint = $html;
241 return $error;
242 }
243
244
250 public function getlistAvailablePrinters()
251 {
252 /* Compatible with paretn class signature */
253 return $this->getlistAvailableGcpPrinters()['available'];
254 }
255
256
263 {
264 global $conf;
265 $ret = array();
266
267 $keyforprovider = ''; // @FIXME
268
269 // Token storage
270 $storage = new DoliStorage($this->db, $conf, $keyforprovider);
271 // Setup the credentials for the requests
272 $credentials = new Credentials(
273 $this->google_id,
274 $this->google_secret,
275 DOL_MAIN_URL_ROOT.'/core/modules/oauth/google_oauthcallback.php'
276 );
277 $serviceFactory = new \OAuth\ServiceFactory();
278 $apiService = $serviceFactory->createService($this->OAUTH_SERVICENAME_GOOGLE, $credentials, $storage, array());
279 '@phan-var-force OAuth\OAuth2\Service\Google $apiService'; // createService is only ServiceInterface
280 // Check if we have auth token
281 $token_ok = true;
282 try {
283 $token = $storage->retrieveAccessToken($this->OAUTH_SERVICENAME_GOOGLE);
284 } catch (Exception $e) {
285 $this->errors[] = $e->getMessage();
286 $token_ok = false;
287 }
288 $expire = false;
289 // Is token expired or will token expire in the next 30 seconds
290 if ($token_ok) {
291 $expire = ($token->getEndOfLife() !== -9002 && $token->getEndOfLife() !== -9001 && time() > ($token->getEndOfLife() - 30));
292 }
293
294 // Token expired so we refresh it
295 if ($token_ok && $expire) {
296 try {
297 // il faut sauvegarder le refresh token car google ne le donne qu'une seule fois
298 $refreshtoken = $token->getRefreshToken();
299 $token = $apiService->refreshAccessToken($token);
300 $token->setRefreshToken($refreshtoken);
301 $storage->storeAccessToken($this->OAUTH_SERVICENAME_GOOGLE, $token);
302 } catch (Exception $e) {
303 $this->errors[] = $e->getMessage();
304 }
305 }
306 // Send a request with api
307 try {
308 $response = $apiService->request(self::PRINTERS_SEARCH_URL);
309 } catch (Exception $e) {
310 $this->errors[] = $e->getMessage();
311 print '<pre>'.print_r($e->getMessage(), true).'</pre>';
312 }
313 //print '<tr><td><pre>'.print_r($response, true).'</pre></td></tr>';
314 $responsedata = json_decode($response, true);
315 $printers = $responsedata['printers'];
316 // Check if we have printers?
317 if (is_array($printers) && count($printers) == 0) {
318 // We don't have printers so return blank array
319 $ret['available'] = array();
320 } else {
321 // We have printers so returns printers as array
322 $ret['available'] = $printers;
323 }
324 return $ret;
325 }
326
335 public function printFile($file, $module, $subdir = '')
336 {
337 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
338
339 global $conf, $user;
340 $error = 0;
341
342 $fileprint = $conf->{$module}->dir_output;
343 if ($subdir != '') {
344 $fileprint .= '/'.$subdir;
345 }
346 $fileprint .= '/'.$file;
347 $mimetype = dol_mimetype($fileprint);
348 $printer_id = '';
349 // select printer uri for module order, propal,...
350 $sql = "SELECT rowid, printer_id, copy FROM ".MAIN_DB_PREFIX."printing WHERE module='".$this->db->escape($module)."' AND driver='printgcp' AND userid=".((int) $user->id);
351 $result = $this->db->query($sql);
352 if ($result) {
353 $obj = $this->db->fetch_object($result);
354 if ($obj) {
355 $printer_id = $obj->printer_id;
356 } else {
357 if (getDolGlobalString('PRINTING_GCP_DEFAULT')) {
358 $printer_id = getDolGlobalString('PRINTING_GCP_DEFAULT');
359 } else {
360 $this->errors[] = 'NoDefaultPrinterDefined';
361 $error++;
362 return $error;
363 }
364 }
365 } else {
366 dol_print_error($this->db);
367 }
368
369 $ret = $this->sendPrintToPrinter($printer_id, $file, $fileprint, $mimetype);
370 $this->error = 'PRINTGCP: '.$ret['errormessage'];
371 if ($ret['status'] != 1) {
372 $error++;
373 }
374 return $error;
375 }
376
386 public function sendPrintToPrinter($printerid, $printjobtitle, $filepath, $contenttype)
387 {
388 global $conf;
389 // Check if printer id
390 if (empty($printerid)) {
391 return array('status' => 0, 'errorcode' => '', 'errormessage' => 'No provided printer ID');
392 }
393 // Open the file which needs to be print
394 $handle = fopen($filepath, "rb");
395 if (!$handle) {
396 return array('status' => 0, 'errorcode' => '', 'errormessage' => 'Could not read the file.');
397 }
398 // Read file content
399 $contents = fread($handle, filesize($filepath));
400 fclose($handle);
401 // Prepare post fields for sending print
402 $post_fields = array(
403 'printerid' => $printerid,
404 'title' => $printjobtitle,
405 'contentTransferEncoding' => 'base64',
406 'content' => base64_encode($contents), // encode file content as base64
407 'contentType' => $contenttype,
408 );
409
410 $keyforprovider = ''; // @FIXME
411
412 // Dolibarr Token storage
413 $storage = new DoliStorage($this->db, $conf, $keyforprovider);
414 // Setup the credentials for the requests
415 $credentials = new Credentials(
416 $this->google_id,
417 $this->google_secret,
418 DOL_MAIN_URL_ROOT.'/core/modules/oauth/google_oauthcallback.php?service=google'
419 );
420 $serviceFactory = new \OAuth\ServiceFactory();
421 $apiService = $serviceFactory->createService($this->OAUTH_SERVICENAME_GOOGLE, $credentials, $storage, array());
422 '@phan-var-force OAuth\OAuth2\Service\Google $apiService'; // createService is only ServiceInterface
423
424 // Check if we have auth token and refresh it
425 $token_ok = true;
426 try {
427 $token = $storage->retrieveAccessToken($this->OAUTH_SERVICENAME_GOOGLE);
428 } catch (Exception $e) {
429 $this->errors[] = $e->getMessage();
430 $token_ok = false;
431 }
432 if ($token_ok) {
433 try {
434 // il faut sauvegarder le refresh token car google ne le donne qu'une seule fois
435 $refreshtoken = $token->getRefreshToken();
436 $token = $apiService->refreshAccessToken($token);
437 $token->setRefreshToken($refreshtoken);
438 $storage->storeAccessToken($this->OAUTH_SERVICENAME_GOOGLE, $token);
439 } catch (Exception $e) {
440 $this->errors[] = $e->getMessage();
441 }
442 }
443
444 // Send a request with api
445 $response = json_decode($apiService->request(self::PRINT_URL, 'POST', $post_fields), true);
446 //print '<tr><td><pre>'.print_r($response, true).'</pre></td></tr>';
447 return array('status' => $response['success'], 'errorcode' => $response['errorCode'], 'errormessage' => $response['message']);
448 }
449
450
458 public function listJobs($module = null)
459 {
460 global $conf, $langs;
461
462 $error = 0;
463 $html = '';
464
465 $keyforprovider = ''; // @FIXME
466
467 // Token storage
468 $storage = new DoliStorage($this->db, $conf, $keyforprovider);
469 // Setup the credentials for the requests
470 $credentials = new Credentials(
471 $this->google_id,
472 $this->google_secret,
473 DOL_MAIN_URL_ROOT.'/core/modules/oauth/google_oauthcallback.php'
474 );
475 $serviceFactory = new \OAuth\ServiceFactory();
476 $apiService = $serviceFactory->createService($this->OAUTH_SERVICENAME_GOOGLE, $credentials, $storage, array());
477 '@phan-var-force OAuth\OAuth2\Service\Google $apiService'; // createService is only ServiceInterface
478 // Check if we have auth token
479 $token_ok = true;
480 try {
481 $token = $storage->retrieveAccessToken($this->OAUTH_SERVICENAME_GOOGLE);
482 } catch (Exception $e) {
483 $this->errors[] = $e->getMessage();
484 $token_ok = false;
485 $error++;
486 }
487 $expire = false;
488 // Is token expired or will token expire in the next 30 seconds
489 if ($token_ok) {
490 $expire = ($token->getEndOfLife() !== -9002 && $token->getEndOfLife() !== -9001 && time() > ($token->getEndOfLife() - 30));
491 }
492
493 // Token expired so we refresh it
494 if ($token_ok && $expire) {
495 try {
496 // il faut sauvegarder le refresh token car google ne le donne qu'une seule fois
497 $refreshtoken = $token->getRefreshToken();
498 $token = $apiService->refreshAccessToken($token);
499 $token->setRefreshToken($refreshtoken);
500 $storage->storeAccessToken($this->OAUTH_SERVICENAME_GOOGLE, $token);
501 } catch (Exception $e) {
502 $this->errors[] = $e->getMessage();
503 $error++;
504 }
505 }
506 // Getting Jobs
507 // Send a request with api
508 try {
509 $response = $apiService->request(self::PRINTERS_GET_JOBS);
510 } catch (Exception $e) {
511 $this->errors[] = $e->getMessage();
512 $error++;
513 }
514 $responsedata = json_decode($response, true);
515 //$html .= '<pre>'.print_r($responsedata,true).'</pre>';
516 $html .= '<div class="div-table-responsive">';
517 $html .= '<table width="100%" class="noborder">';
518 $html .= '<tr class="liste_titre">';
519 $html .= '<td>'.$langs->trans("Id").'</td>';
520 $html .= '<td>'.$langs->trans("Date").'</td>';
521 $html .= '<td>'.$langs->trans("Owner").'</td>';
522 $html .= '<td>'.$langs->trans("Printer").'</td>';
523 $html .= '<td>'.$langs->trans("Filename").'</td>';
524 $html .= '<td>'.$langs->trans("Status").'</td>';
525 $html .= '<td>'.$langs->trans("Cancel").'</td>';
526 $html .= '</tr>'."\n";
527
528 $jobs = $responsedata['jobs'];
529 //$html .= '<pre>'.print_r($jobs['0'],true).'</pre>';
530 if (is_array($jobs)) {
531 foreach ($jobs as $value) {
532 $html .= '<tr class="oddeven">';
533 $html .= '<td>'.$value['id'].'</td>';
534 $dates = dol_print_date((int) substr($value['createTime'], 0, 10), 'dayhour');
535 $html .= '<td>'.$dates.'</td>';
536 $html .= '<td>'.$value['ownerId'].'</td>';
537 $html .= '<td>'.$value['printerName'].'</td>';
538 $html .= '<td>'.$value['title'].'</td>';
539 $html .= '<td>'.$value['status'].'</td>';
540 $html .= '<td>&nbsp;</td>';
541 $html .= '</tr>';
542 }
543 } else {
544 $html .= '<tr class="oddeven">';
545 $html .= '<td colspan="7" class="opacitymedium">'.$langs->trans("None").'</td>';
546 $html .= '</tr>';
547 }
548 $html .= '</table>';
549 $html .= '</div>';
550
551 $this->resprint = $html;
552
553 return $error;
554 }
555}
global $dolibarr_main_url_root
Parent class of emailing target selectors modules.
Class to provide printing with Google Cloud Print.
listJobs($module=null)
List jobs print.
__construct($db)
Constructor.
listAvailablePrinters()
Return list of available printers.
printFile($file, $module, $subdir='')
Print selected file.
getlistAvailablePrinters()
Return list of available printers.
sendPrintToPrinter($printerid, $printjobtitle, $filepath, $contenttype)
Sends document to the printer.
getlistAvailableGcpPrinters()
Return list of available printers (internal format)
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)
dol_mimetype($file, $default='application/octet-stream', $mode=0)
Return MIME type of a file from its name with extension.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
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.
conf($dolibarr_main_document_root)
Load conf file (file must exists)
Definition inc.php:423
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79