26include_once DOL_DOCUMENT_ROOT.
'/core/modules/printing/modules_printing.php';
27require_once DOL_DOCUMENT_ROOT.
'/includes/OAuth/bootstrap.php';
29use OAuth\Common\Storage\DoliStorage;
30use OAuth\Common\Consumer\Credentials;
40 public $name =
'printgcp';
45 public $desc =
'PrintGCPDesc';
50 public $picto =
'printer';
55 public $active =
'PRINTING_PRINTGCP';
60 public $conf = array();
65 public $google_id =
'';
70 public $google_secret =
'';
80 public $errors = array();
87 private $OAUTH_SERVICENAME_GOOGLE =
'Google';
89 const LOGIN_URL =
'https://accounts.google.com/o/oauth2/token';
90 const PRINTERS_SEARCH_URL =
'https://www.google.com/cloudprint/search';
91 const PRINTERS_GET_JOBS =
'https://www.google.com/cloudprint/jobs';
92 const PRINT_URL =
'https://www.google.com/cloudprint/submit';
102 global $conf, $langs, $dolibarr_main_url_root;
105 $urlwithouturlroot = preg_replace(
'/'.preg_quote(DOL_URL_ROOT,
'/').
'$/i',
'', trim($dolibarr_main_url_root));
106 $urlwithroot = $urlwithouturlroot.DOL_URL_ROOT;
111 if (!$conf->oauth->enabled) {
112 $this->
conf[] = array(
113 'varname'=>
'PRINTGCP_INFO',
114 'info'=>$langs->transnoentitiesnoconv(
"WarningModuleNotActive",
"OAuth"),
118 $keyforprovider =
'';
123 $storage =
new DoliStorage($this->db, $conf, $keyforprovider);
126 $credentials =
new Credentials(
128 $this->google_secret,
129 $urlwithroot.
'/core/modules/oauth/google_oauthcallback.php'
131 $access = ($storage->hasAccessToken($this->OAUTH_SERVICENAME_GOOGLE) ?
'HasAccessToken' :
'NoAccessToken');
132 $serviceFactory = new \OAuth\ServiceFactory();
133 $apiService = $serviceFactory->createService($this->OAUTH_SERVICENAME_GOOGLE, $credentials, $storage, array());
136 $token = $storage->retrieveAccessToken($this->OAUTH_SERVICENAME_GOOGLE);
138 $this->errors[] = $e->getMessage();
145 $expire = ($token->getEndOfLife() !== -9002 && $token->getEndOfLife() !== -9001 && time() > ($token->getEndOfLife() - 30));
149 if ($token_ok && $expire) {
152 $refreshtoken = $token->getRefreshToken();
153 $token = $apiService->refreshAccessToken($token);
154 $token->setRefreshToken($refreshtoken);
155 $storage->storeAccessToken($this->OAUTH_SERVICENAME_GOOGLE, $token);
157 $this->errors[] = $e->getMessage();
160 if ($this->google_id !=
'' && $this->google_secret !=
'') {
161 $this->
conf[] = array(
'varname'=>
'PRINTGCP_INFO',
'info'=>
'GoogleAuthConfigured',
'type'=>
'info');
162 $this->
conf[] = array(
163 'varname'=>
'PRINTGCP_TOKEN_ACCESS',
166 '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'),
167 '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') :
'')
172 $refreshtoken = $token->getRefreshToken();
174 $endoflife = $token->getEndOfLife();
176 if ($endoflife == $token::EOL_NEVER_EXPIRES) {
177 $expiredat = $langs->trans(
"Never");
178 } elseif ($endoflife == $token::EOL_UNKNOWN) {
179 $expiredat = $langs->trans(
"Unknown");
184 $this->
conf[] = array(
'varname'=>
'TOKEN_REFRESH',
'info'=>((!empty($refreshtoken)) ?
'Yes' :
'No'),
'type'=>
'info');
185 $this->
conf[] = array(
'varname'=>
'TOKEN_EXPIRED',
'info'=>($expire ?
'Yes' :
'No'),
'type'=>
'info');
186 $this->
conf[] = array(
'varname'=>
'TOKEN_EXPIRE_AT',
'info'=>($expiredat),
'type'=>
'info');
196 $this->
conf[] = array(
'varname'=>
'PRINTGCP_INFO',
'info'=>
'GoogleAuthNotConfigured',
'type'=>
'info');
200 $this->
conf[] = array(
'enabled'=>0,
'type'=>
'submit');
210 global $conf, $langs;
212 $langs->load(
'printing');
214 $html =
'<tr class="liste_titre">';
215 $html .=
'<td>'.$langs->trans(
'GCP_Name').
'</td>';
216 $html .=
'<td>'.$langs->trans(
'GCP_displayName').
'</td>';
217 $html .=
'<td>'.$langs->trans(
'GCP_Id').
'</td>';
218 $html .=
'<td>'.$langs->trans(
'GCP_OwnerName').
'</td>';
219 $html .=
'<td>'.$langs->trans(
'GCP_State').
'</td>';
220 $html .=
'<td>'.$langs->trans(
'GCP_connectionStatus').
'</td>';
221 $html .=
'<td>'.$langs->trans(
'GCP_Type').
'</td>';
222 $html .=
'<td class="center">'.$langs->trans(
"Select").
'</td>';
223 $html .=
'</tr>'.
"\n";
226 foreach ($list[
'available'] as $printer_det) {
227 $html .=
'<tr class="oddeven">';
228 $html .=
'<td>'.$printer_det[
'name'].
'</td>';
229 $html .=
'<td>'.$printer_det[
'displayName'].
'</td>';
230 $html .=
'<td>'.$printer_det[
'id'].
'</td>';
231 $html .=
'<td>'.$printer_det[
'ownerName'].
'</td>';
232 $html .=
'<td>'.$printer_det[
'status'].
'</td>';
233 $html .=
'<td>'.$langs->trans(
'STATE_'.$printer_det[
'connectionStatus']).
'</td>';
234 $html .=
'<td>'.$langs->trans(
'TYPE_'.$printer_det[
'type']).
'</td>';
236 $html .=
'<td class="center">';
237 if ($conf->global->PRINTING_GCP_DEFAULT == $printer_det[
'id']) {
238 $html .=
img_picto($langs->trans(
"Default"),
'on');
240 $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>';
243 $html .=
'</tr>'.
"\n";
245 $this->resprint = $html;
260 $keyforprovider =
'';
263 $storage =
new DoliStorage($this->db, $conf, $keyforprovider);
265 $credentials =
new Credentials(
267 $this->google_secret,
268 DOL_MAIN_URL_ROOT.
'/core/modules/oauth/google_oauthcallback.php'
270 $serviceFactory = new \OAuth\ServiceFactory();
271 $apiService = $serviceFactory->createService($this->OAUTH_SERVICENAME_GOOGLE, $credentials, $storage, array());
275 $token = $storage->retrieveAccessToken($this->OAUTH_SERVICENAME_GOOGLE);
277 $this->errors[] = $e->getMessage();
283 $expire = ($token->getEndOfLife() !== -9002 && $token->getEndOfLife() !== -9001 && time() > ($token->getEndOfLife() - 30));
287 if ($token_ok && $expire) {
290 $refreshtoken = $token->getRefreshToken();
291 $token = $apiService->refreshAccessToken($token);
292 $token->setRefreshToken($refreshtoken);
293 $storage->storeAccessToken($this->OAUTH_SERVICENAME_GOOGLE, $token);
295 $this->errors[] = $e->getMessage();
300 $response = $apiService->request(self::PRINTERS_SEARCH_URL);
302 $this->errors[] = $e->getMessage();
303 print
'<pre>'.print_r($e->getMessage(),
true).
'</pre>';
306 $responsedata = json_decode($response,
true);
307 $printers = $responsedata[
'printers'];
309 if (is_array($printers) && count($printers) == 0) {
311 $ret[
'available'] = array();
314 $ret[
'available'] = $printers;
329 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
334 $fileprint = $conf->{$module}->dir_output;
336 $fileprint .=
'/'.$subdir;
338 $fileprint .=
'/'.$file;
342 $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);
343 $result = $this->db->query($sql);
345 $obj = $this->db->fetch_object($result);
347 $printer_id = $obj->printer_id;
352 $this->errors[] =
'NoDefaultPrinterDefined';
362 $this->error =
'PRINTGCP: '.$ret[
'errormessage'];
363 if ($ret[
'status'] != 1) {
382 if (empty($printerid)) {
383 return array(
'status' =>0,
'errorcode' =>
'',
'errormessage'=>
'No provided printer ID');
386 $handle = fopen($filepath,
"rb");
388 return array(
'status' =>0,
'errorcode' =>
'',
'errormessage'=>
'Could not read the file.');
391 $contents = fread($handle, filesize($filepath));
394 $post_fields = array(
395 'printerid' => $printerid,
396 'title' => $printjobtitle,
397 'contentTransferEncoding' =>
'base64',
398 'content' => base64_encode($contents),
399 'contentType' => $contenttype,
402 $keyforprovider =
'';
405 $storage =
new DoliStorage($this->db, $conf, $keyforprovider);
407 $credentials =
new Credentials(
409 $this->google_secret,
410 DOL_MAIN_URL_ROOT.
'/core/modules/oauth/google_oauthcallback.php?service=google'
412 $serviceFactory = new \OAuth\ServiceFactory();
413 $apiService = $serviceFactory->createService($this->OAUTH_SERVICENAME_GOOGLE, $credentials, $storage, array());
418 $token = $storage->retrieveAccessToken($this->OAUTH_SERVICENAME_GOOGLE);
420 $this->errors[] = $e->getMessage();
426 $refreshtoken = $token->getRefreshToken();
427 $token = $apiService->refreshAccessToken($token);
428 $token->setRefreshToken($refreshtoken);
429 $storage->storeAccessToken($this->OAUTH_SERVICENAME_GOOGLE, $token);
431 $this->errors[] = $e->getMessage();
436 $response = json_decode($apiService->request(self::PRINT_URL,
'POST', $post_fields),
true);
438 return array(
'status' => $response[
'success'],
'errorcode' => $response[
'errorCode'],
'errormessage' => $response[
'message']);
449 global $conf, $langs;
454 $keyforprovider =
'';
457 $storage =
new DoliStorage($this->db, $conf, $keyforprovider);
459 $credentials =
new Credentials(
461 $this->google_secret,
462 DOL_MAIN_URL_ROOT.
'/core/modules/oauth/google_oauthcallback.php'
464 $serviceFactory = new \OAuth\ServiceFactory();
465 $apiService = $serviceFactory->createService($this->OAUTH_SERVICENAME_GOOGLE, $credentials, $storage, array());
469 $token = $storage->retrieveAccessToken($this->OAUTH_SERVICENAME_GOOGLE);
471 $this->errors[] = $e->getMessage();
478 $expire = ($token->getEndOfLife() !== -9002 && $token->getEndOfLife() !== -9001 && time() > ($token->getEndOfLife() - 30));
482 if ($token_ok && $expire) {
485 $refreshtoken = $token->getRefreshToken();
486 $token = $apiService->refreshAccessToken($token);
487 $token->setRefreshToken($refreshtoken);
488 $storage->storeAccessToken($this->OAUTH_SERVICENAME_GOOGLE, $token);
490 $this->errors[] = $e->getMessage();
497 $response = $apiService->request(self::PRINTERS_GET_JOBS);
499 $this->errors[] = $e->getMessage();
502 $responsedata = json_decode($response,
true);
504 $html .=
'<div class="div-table-responsive">';
505 $html .=
'<table width="100%" class="noborder">';
506 $html .=
'<tr class="liste_titre">';
507 $html .=
'<td>'.$langs->trans(
"Id").
'</td>';
508 $html .=
'<td>'.$langs->trans(
"Date").
'</td>';
509 $html .=
'<td>'.$langs->trans(
"Owner").
'</td>';
510 $html .=
'<td>'.$langs->trans(
"Printer").
'</td>';
511 $html .=
'<td>'.$langs->trans(
"Filename").
'</td>';
512 $html .=
'<td>'.$langs->trans(
"Status").
'</td>';
513 $html .=
'<td>'.$langs->trans(
"Cancel").
'</td>';
514 $html .=
'</tr>'.
"\n";
516 $jobs = $responsedata[
'jobs'];
518 if (is_array($jobs)) {
519 foreach ($jobs as $value) {
520 $html .=
'<tr class="oddeven">';
521 $html .=
'<td>'.$value[
'id'].
'</td>';
522 $dates =
dol_print_date((
int) substr($value[
'createTime'], 0, 10),
'dayhour');
523 $html .=
'<td>'.$dates.
'</td>';
524 $html .=
'<td>'.$value[
'ownerId'].
'</td>';
525 $html .=
'<td>'.$value[
'printerName'].
'</td>';
526 $html .=
'<td>'.$value[
'title'].
'</td>';
527 $html .=
'<td>'.$value[
'status'].
'</td>';
528 $html .=
'<td> </td>';
532 $html .=
'<tr class="oddeven">';
533 $html .=
'<td colspan="7" class="opacitymedium">'.$langs->trans(
"None").
'</td>';
539 $this->resprint = $html;
Parent class of emailing target selectors modules.
Class to provide printing with Google Cloud Print.
listJobs()
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.
dol_mimetype($file, $default='application/octet-stream', $mode=0)
Return MIME type of a file from its name with extension.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
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 dolibarr global constant string value.
conf($dolibarr_main_document_root)
Load conf file (file must exists)