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();
144 if ($token !==
null) {
145 $expire = ($token->getEndOfLife() !== -9002 && $token->getEndOfLife() !== -9001 && time() > ($token->getEndOfLife() - 30));
149 if (($token !==
null) && $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') :
'')
173 if ($token !==
null) {
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();
303 if ($token !==
null) {
304 $expire = ($token->getEndOfLife() !== -9002 && $token->getEndOfLife() !== -9001 && time() > ($token->getEndOfLife() - 30));
308 if (($token !==
null) && $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();
322 $response = $apiService->request(self::PRINTERS_SEARCH_URL);
324 $this->errors[] = $e->getMessage();
325 print
'<pre>'.print_r($e->getMessage(),
true).
'</pre>';
328 $responsedata = json_decode($response,
true);
329 $printers = $responsedata[
'printers'];
331 if (is_array($printers) && count($printers) == 0) {
333 $ret[
'available'] = array();
336 $ret[
'available'] = $printers;
351 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
356 $fileprint =
$conf->{$module}->dir_output;
358 $fileprint .=
'/'.$subdir;
360 $fileprint .=
'/'.$file;
361 $mimetype = dol_mimetype($fileprint);
364 $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);
365 $result = $this->db->query($sql);
367 $obj = $this->db->fetch_object($result);
369 $printer_id = $obj->printer_id;
374 $this->errors[] =
'NoDefaultPrinterDefined';
384 $this->error =
'PRINTGCP: '.$ret[
'errormessage'];
385 if ($ret[
'status'] != 1) {
404 if (empty($printerid)) {
405 return array(
'status' => 0,
'errorcode' =>
'',
'errormessage' =>
'No provided printer ID');
408 $handle = fopen($filepath,
"rb");
410 return array(
'status' => 0,
'errorcode' =>
'',
'errormessage' =>
'Could not read the file.');
413 $contents = fread($handle, filesize($filepath));
416 $post_fields = array(
417 'printerid' => $printerid,
418 'title' => $printjobtitle,
419 'contentTransferEncoding' =>
'base64',
420 'content' => base64_encode($contents),
421 'contentType' => $contenttype,
424 $keyforprovider =
'googleprint';
427 $storage =
new DoliStorage($this->db,
$conf, $keyforprovider);
429 $credentials =
new Credentials(
431 $this->google_secret,
432 DOL_MAIN_URL_ROOT.
'/core/modules/oauth/google_oauthcallback.php?service=google'
434 $serviceFactory = new \OAuth\ServiceFactory();
435 $apiService = $serviceFactory->createService($this->OAUTH_SERVICENAME_GOOGLE, $credentials, $storage, array());
436 '@phan-var-force OAuth\OAuth2\Service\Google $apiService';
441 $token = $storage->retrieveAccessToken($this->OAUTH_SERVICENAME_GOOGLE);
443 $this->errors[] = $e->getMessage();
446 if ($token !==
null) {
449 $refreshtoken = $token->getRefreshToken();
450 $token = $apiService->refreshAccessToken($token);
451 $token->setRefreshToken($refreshtoken);
452 $storage->storeAccessToken($this->OAUTH_SERVICENAME_GOOGLE, $token);
454 $this->errors[] = $e->getMessage();
459 $response = json_decode($apiService->request(self::PRINT_URL,
'POST', $post_fields),
true);
461 return array(
'status' => $response[
'success'],
'errorcode' => $response[
'errorCode'],
'errormessage' => $response[
'message']);
474 global
$conf, $langs;
479 $keyforprovider =
'googleprint';
482 $storage =
new DoliStorage($this->db,
$conf, $keyforprovider);
484 $credentials =
new Credentials(
486 $this->google_secret,
487 DOL_MAIN_URL_ROOT.
'/core/modules/oauth/google_oauthcallback.php'
489 $serviceFactory = new \OAuth\ServiceFactory();
490 $apiService = $serviceFactory->createService($this->OAUTH_SERVICENAME_GOOGLE, $credentials, $storage, array());
491 '@phan-var-force OAuth\OAuth2\Service\Google $apiService';
495 $token = $storage->retrieveAccessToken($this->OAUTH_SERVICENAME_GOOGLE);
497 $this->errors[] = $e->getMessage();
503 if ($token !==
null) {
504 $expire = ($token->getEndOfLife() !== -9002 && $token->getEndOfLife() !== -9001 && time() > ($token->getEndOfLife() - 30));
508 if (($token !==
null) && $expire) {
511 $refreshtoken = $token->getRefreshToken();
512 $token = $apiService->refreshAccessToken($token);
513 $token->setRefreshToken($refreshtoken);
514 $storage->storeAccessToken($this->OAUTH_SERVICENAME_GOOGLE, $token);
516 $this->errors[] = $e->getMessage();
524 $response = $apiService->request(self::PRINTERS_GET_JOBS);
526 $this->errors[] = $e->getMessage();
529 $responsedata = json_decode($response,
true);
531 $html .=
'<div class="div-table-responsive">';
532 $html .=
'<table width="100%" class="noborder">';
533 $html .=
'<tr class="liste_titre">';
534 $html .=
'<td>'.$langs->trans(
"Id").
'</td>';
535 $html .=
'<td>'.$langs->trans(
"Date").
'</td>';
536 $html .=
'<td>'.$langs->trans(
"Owner").
'</td>';
537 $html .=
'<td>'.$langs->trans(
"Printer").
'</td>';
538 $html .=
'<td>'.$langs->trans(
"Filename").
'</td>';
539 $html .=
'<td>'.$langs->trans(
"Status").
'</td>';
540 $html .=
'<td>'.$langs->trans(
"Cancel").
'</td>';
541 $html .=
'</tr>'.
"\n";
543 $jobs = $responsedata[
'jobs'];
545 if (is_array($jobs)) {
546 foreach ($jobs as $value) {
547 $html .=
'<tr class="oddeven">';
548 $html .=
'<td>'.$value[
'id'].
'</td>';
549 $dates =
dol_print_date((
int) substr($value[
'createTime'], 0, 10),
'dayhour');
550 $html .=
'<td>'.$dates.
'</td>';
551 $html .=
'<td>'.$value[
'ownerId'].
'</td>';
552 $html .=
'<td>'.$value[
'printerName'].
'</td>';
553 $html .=
'<td>'.$value[
'title'].
'</td>';
554 $html .=
'<td>'.$value[
'status'].
'</td>';
555 $html .=
'<td> </td>';
559 $html .=
'<tr class="oddeven">';
560 $html .=
'<td colspan="7" class="opacitymedium">'.$langs->trans(
"None").
'</td>';
566 $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_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)