27include_once DOL_DOCUMENT_ROOT.
'/core/modules/printing/modules_printing.php';
28require_once DOL_DOCUMENT_ROOT.
'/includes/OAuth/bootstrap.php';
30use OAuth\Common\Storage\DoliStorage;
31use OAuth\Common\Consumer\Credentials;
41 public $name =
'printgcp';
46 public $desc =
'PrintGCPDesc';
51 public $picto =
'printer';
56 public $active =
'PRINTING_PRINTGCP';
61 public $conf = array();
66 public $google_id =
'';
71 public $google_secret =
'';
81 private $OAUTH_SERVICENAME_GOOGLE =
'Google';
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';
100 $urlwithroot = $urlwithouturlroot.DOL_URL_ROOT;
106 $this->
conf[] = array(
107 'varname' =>
'PRINTGCP_INFO',
108 'info' => $langs->transnoentitiesnoconv(
"WarningModuleNotActive",
"OAuth"),
112 $keyforprovider =
'googleprint';
117 $storage =
new DoliStorage($this->db, $conf, $keyforprovider);
121 $credentials =
new Credentials(
123 $this->google_secret,
124 $urlwithroot.
'/core/modules/oauth/google_oauthcallback.php'
126 $access = ($storage->hasAccessToken($this->OAUTH_SERVICENAME_GOOGLE) ?
'HasAccessToken' :
'NoAccessToken');
128 $serviceFactory = new \OAuth\ServiceFactory();
130 $apiService = $serviceFactory->createService($this->OAUTH_SERVICENAME_GOOGLE, $credentials, $storage, array());
131 '@phan-var-force OAuth\OAuth2\Service\Google $apiService';
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(
162 'varname' =>
'PRINTGCP_INFO',
163 'info' =>
'GoogleAuthConfigured',
166 $this->
conf[] = array(
167 'varname' =>
'PRINTGCP_TOKEN_ACCESS',
170 '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'),
171 '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') :
'')
176 $refreshtoken = $token->getRefreshToken();
178 $endoflife = $token->getEndOfLife();
180 if ($endoflife == $token::EOL_NEVER_EXPIRES) {
181 $expiredat = $langs->trans(
"Never");
182 } elseif ($endoflife == $token::EOL_UNKNOWN) {
183 $expiredat = $langs->trans(
"Unknown");
188 $this->
conf[] = array(
'varname' =>
'TOKEN_REFRESH',
'info' => ((!empty($refreshtoken)) ?
'Yes' :
'No'),
'type' =>
'info');
189 $this->
conf[] = array(
'varname' =>
'TOKEN_EXPIRED',
'info' => ($expire ?
'Yes' :
'No'),
'type' =>
'info');
190 $this->
conf[] = array(
'varname' =>
'TOKEN_EXPIRE_AT',
'info' => ($expiredat),
'type' =>
'info');
200 $this->
conf[] = array(
201 'varname' =>
'PRINTGCP_INFO',
202 'info' =>
'GoogleAuthNotConfigured',
208 $this->
conf[] = array(
'enabled' => 0,
'type' =>
'submit');
218 global $conf, $langs;
220 $langs->load(
'printing');
222 $html =
'<tr class="liste_titre">';
223 $html .=
'<td>'.$langs->trans(
'GCP_Name').
'</td>';
224 $html .=
'<td>'.$langs->trans(
'GCP_displayName').
'</td>';
225 $html .=
'<td>'.$langs->trans(
'GCP_Id').
'</td>';
226 $html .=
'<td>'.$langs->trans(
'GCP_OwnerName').
'</td>';
227 $html .=
'<td>'.$langs->trans(
'GCP_State').
'</td>';
228 $html .=
'<td>'.$langs->trans(
'GCP_connectionStatus').
'</td>';
229 $html .=
'<td>'.$langs->trans(
'GCP_Type').
'</td>';
230 $html .=
'<td class="center">'.$langs->trans(
"Select").
'</td>';
231 $html .=
'</tr>'.
"\n";
234 foreach ($list[
'available'] as $printer_det) {
235 $html .=
'<tr class="oddeven">';
236 $html .=
'<td>'.$printer_det[
'name'].
'</td>';
237 $html .=
'<td>'.$printer_det[
'displayName'].
'</td>';
238 $html .=
'<td>'.$printer_det[
'id'].
'</td>';
239 $html .=
'<td>'.$printer_det[
'ownerName'].
'</td>';
240 $html .=
'<td>'.$printer_det[
'status'].
'</td>';
241 $html .=
'<td>'.$langs->trans(
'STATE_'.$printer_det[
'connectionStatus']).
'</td>';
242 $html .=
'<td>'.$langs->trans(
'TYPE_'.$printer_det[
'type']).
'</td>';
244 $html .=
'<td class="center">';
246 $html .=
img_picto($langs->trans(
"Default"),
'on');
248 $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>';
251 $html .=
'</tr>'.
"\n";
253 $this->resprint = $html;
280 $keyforprovider =
'googleprint';
283 $storage =
new DoliStorage($this->db, $conf, $keyforprovider);
285 $credentials =
new Credentials(
287 $this->google_secret,
288 DOL_MAIN_URL_ROOT.
'/core/modules/oauth/google_oauthcallback.php'
290 $serviceFactory = new \OAuth\ServiceFactory();
291 $apiService = $serviceFactory->createService($this->OAUTH_SERVICENAME_GOOGLE, $credentials, $storage, array());
292 '@phan-var-force OAuth\OAuth2\Service\Google $apiService';
296 $token = $storage->retrieveAccessToken($this->OAUTH_SERVICENAME_GOOGLE);
298 $this->errors[] = $e->getMessage();
304 $expire = ($token->getEndOfLife() !== -9002 && $token->getEndOfLife() !== -9001 && time() > ($token->getEndOfLife() - 30));
308 if ($token_ok && $expire) {
311 $refreshtoken = $token->getRefreshToken();
312 $token = $apiService->refreshAccessToken($token);
313 $token->setRefreshToken($refreshtoken);
314 $storage->storeAccessToken($this->OAUTH_SERVICENAME_GOOGLE, $token);
316 $this->errors[] = $e->getMessage();
321 $response = $apiService->request(self::PRINTERS_SEARCH_URL);
323 $this->errors[] = $e->getMessage();
324 print
'<pre>'.print_r($e->getMessage(),
true).
'</pre>';
327 $responsedata = json_decode($response,
true);
328 $printers = $responsedata[
'printers'];
330 if (is_array($printers) && count($printers) == 0) {
332 $ret[
'available'] = array();
335 $ret[
'available'] = $printers;
350 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
355 $fileprint = $conf->{$module}->dir_output;
357 $fileprint .=
'/'.$subdir;
359 $fileprint .=
'/'.$file;
363 $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);
364 $result = $this->db->query($sql);
366 $obj = $this->db->fetch_object($result);
368 $printer_id = $obj->printer_id;
373 $this->errors[] =
'NoDefaultPrinterDefined';
383 $this->error =
'PRINTGCP: '.$ret[
'errormessage'];
384 if ($ret[
'status'] != 1) {
403 if (empty($printerid)) {
404 return array(
'status' => 0,
'errorcode' =>
'',
'errormessage' =>
'No provided printer ID');
407 $handle = fopen($filepath,
"rb");
409 return array(
'status' => 0,
'errorcode' =>
'',
'errormessage' =>
'Could not read the file.');
412 $contents = fread($handle, filesize($filepath));
415 $post_fields = array(
416 'printerid' => $printerid,
417 'title' => $printjobtitle,
418 'contentTransferEncoding' =>
'base64',
419 'content' => base64_encode($contents),
420 'contentType' => $contenttype,
423 $keyforprovider =
'googleprint';
426 $storage =
new DoliStorage($this->db, $conf, $keyforprovider);
428 $credentials =
new Credentials(
430 $this->google_secret,
431 DOL_MAIN_URL_ROOT.
'/core/modules/oauth/google_oauthcallback.php?service=google'
433 $serviceFactory = new \OAuth\ServiceFactory();
434 $apiService = $serviceFactory->createService($this->OAUTH_SERVICENAME_GOOGLE, $credentials, $storage, array());
435 '@phan-var-force OAuth\OAuth2\Service\Google $apiService';
440 $token = $storage->retrieveAccessToken($this->OAUTH_SERVICENAME_GOOGLE);
442 $this->errors[] = $e->getMessage();
448 $refreshtoken = $token->getRefreshToken();
449 $token = $apiService->refreshAccessToken($token);
450 $token->setRefreshToken($refreshtoken);
451 $storage->storeAccessToken($this->OAUTH_SERVICENAME_GOOGLE, $token);
453 $this->errors[] = $e->getMessage();
458 $response = json_decode($apiService->request(self::PRINT_URL,
'POST', $post_fields),
true);
460 return array(
'status' => $response[
'success'],
'errorcode' => $response[
'errorCode'],
'errormessage' => $response[
'message']);
473 global $conf, $langs;
478 $keyforprovider =
'googleprint';
481 $storage =
new DoliStorage($this->db, $conf, $keyforprovider);
483 $credentials =
new Credentials(
485 $this->google_secret,
486 DOL_MAIN_URL_ROOT.
'/core/modules/oauth/google_oauthcallback.php'
488 $serviceFactory = new \OAuth\ServiceFactory();
489 $apiService = $serviceFactory->createService($this->OAUTH_SERVICENAME_GOOGLE, $credentials, $storage, array());
490 '@phan-var-force OAuth\OAuth2\Service\Google $apiService';
494 $token = $storage->retrieveAccessToken($this->OAUTH_SERVICENAME_GOOGLE);
496 $this->errors[] = $e->getMessage();
503 $expire = ($token->getEndOfLife() !== -9002 && $token->getEndOfLife() !== -9001 && time() > ($token->getEndOfLife() - 30));
507 if ($token_ok && $expire) {
510 $refreshtoken = $token->getRefreshToken();
511 $token = $apiService->refreshAccessToken($token);
512 $token->setRefreshToken($refreshtoken);
513 $storage->storeAccessToken($this->OAUTH_SERVICENAME_GOOGLE, $token);
515 $this->errors[] = $e->getMessage();
522 $response = $apiService->request(self::PRINTERS_GET_JOBS);
524 $this->errors[] = $e->getMessage();
527 $responsedata = json_decode($response,
true);
529 $html .=
'<div class="div-table-responsive">';
530 $html .=
'<table width="100%" class="noborder">';
531 $html .=
'<tr class="liste_titre">';
532 $html .=
'<td>'.$langs->trans(
"Id").
'</td>';
533 $html .=
'<td>'.$langs->trans(
"Date").
'</td>';
534 $html .=
'<td>'.$langs->trans(
"Owner").
'</td>';
535 $html .=
'<td>'.$langs->trans(
"Printer").
'</td>';
536 $html .=
'<td>'.$langs->trans(
"Filename").
'</td>';
537 $html .=
'<td>'.$langs->trans(
"Status").
'</td>';
538 $html .=
'<td>'.$langs->trans(
"Cancel").
'</td>';
539 $html .=
'</tr>'.
"\n";
541 $jobs = $responsedata[
'jobs'];
543 if (is_array($jobs)) {
544 foreach ($jobs as $value) {
545 $html .=
'<tr class="oddeven">';
546 $html .=
'<td>'.$value[
'id'].
'</td>';
547 $dates =
dol_print_date((
int) substr($value[
'createTime'], 0, 10),
'dayhour');
548 $html .=
'<td>'.$dates.
'</td>';
549 $html .=
'<td>'.$value[
'ownerId'].
'</td>';
550 $html .=
'<td>'.$value[
'printerName'].
'</td>';
551 $html .=
'<td>'.$value[
'title'].
'</td>';
552 $html .=
'<td>'.$value[
'status'].
'</td>';
553 $html .=
'<td> </td>';
557 $html .=
'<tr class="oddeven">';
558 $html .=
'<td colspan="7" class="opacitymedium">'.$langs->trans(
"None").
'</td>';
564 $this->resprint = $html;
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.
newToken()
Return the value of token currently saved into session with name 'newtoken'.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
Output date in a string format according to outputlangs (or langs if not defined).
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.
isModEnabled($module)
Is Dolibarr module enabled.
conf($dolibarr_main_document_root)
Load conf file (file must exists)