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 =
'';
78 private $OAUTH_SERVICENAME_GOOGLE =
'Google';
80 const LOGIN_URL =
'https://accounts.google.com/o/oauth2/token';
81 const PRINTERS_SEARCH_URL =
'https://www.google.com/cloudprint/search';
82 const PRINTERS_GET_JOBS =
'https://www.google.com/cloudprint/jobs';
83 const PRINT_URL =
'https://www.google.com/cloudprint/submit';
93 global $conf, $langs, $dolibarr_main_url_root;
96 $urlwithouturlroot = preg_replace(
'/'.preg_quote(DOL_URL_ROOT,
'/').
'$/i',
'', trim($dolibarr_main_url_root));
97 $urlwithroot = $urlwithouturlroot.DOL_URL_ROOT;
102 if (!$conf->oauth->enabled) {
103 $this->
conf[] = array(
104 'varname' =>
'PRINTGCP_INFO',
105 'info' => $langs->transnoentitiesnoconv(
"WarningModuleNotActive",
"OAuth"),
109 $keyforprovider =
'';
114 $storage =
new DoliStorage($this->db, $conf, $keyforprovider);
117 $credentials =
new Credentials(
119 $this->google_secret,
120 $urlwithroot.
'/core/modules/oauth/google_oauthcallback.php'
122 $access = ($storage->hasAccessToken($this->OAUTH_SERVICENAME_GOOGLE) ?
'HasAccessToken' :
'NoAccessToken');
123 $serviceFactory = new \OAuth\ServiceFactory();
124 $apiService = $serviceFactory->createService($this->OAUTH_SERVICENAME_GOOGLE, $credentials, $storage, array());
125 '@phan-var-force OAuth\OAuth2\Service\Google $apiService';
128 $token = $storage->retrieveAccessToken($this->OAUTH_SERVICENAME_GOOGLE);
130 $this->errors[] = $e->getMessage();
137 $expire = ($token->getEndOfLife() !== -9002 && $token->getEndOfLife() !== -9001 && time() > ($token->getEndOfLife() - 30));
141 if ($token_ok && $expire) {
144 $refreshtoken = $token->getRefreshToken();
145 $token = $apiService->refreshAccessToken($token);
146 $token->setRefreshToken($refreshtoken);
147 $storage->storeAccessToken($this->OAUTH_SERVICENAME_GOOGLE, $token);
149 $this->errors[] = $e->getMessage();
152 if ($this->google_id !=
'' && $this->google_secret !=
'') {
153 $this->
conf[] = array(
'varname' =>
'PRINTGCP_INFO',
'info' =>
'GoogleAuthConfigured',
'type' =>
'info');
154 $this->
conf[] = array(
155 'varname' =>
'PRINTGCP_TOKEN_ACCESS',
158 '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'),
159 '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') :
'')
164 $refreshtoken = $token->getRefreshToken();
166 $endoflife = $token->getEndOfLife();
168 if ($endoflife == $token::EOL_NEVER_EXPIRES) {
169 $expiredat = $langs->trans(
"Never");
170 } elseif ($endoflife == $token::EOL_UNKNOWN) {
171 $expiredat = $langs->trans(
"Unknown");
176 $this->
conf[] = array(
'varname' =>
'TOKEN_REFRESH',
'info' => ((!empty($refreshtoken)) ?
'Yes' :
'No'),
'type' =>
'info');
177 $this->
conf[] = array(
'varname' =>
'TOKEN_EXPIRED',
'info' => ($expire ?
'Yes' :
'No'),
'type' =>
'info');
178 $this->
conf[] = array(
'varname' =>
'TOKEN_EXPIRE_AT',
'info' => ($expiredat),
'type' =>
'info');
188 $this->
conf[] = array(
'varname' =>
'PRINTGCP_INFO',
'info' =>
'GoogleAuthNotConfigured',
'type' =>
'info');
192 $this->
conf[] = array(
'enabled' => 0,
'type' =>
'submit');
202 global $conf, $langs;
204 $langs->load(
'printing');
206 $html =
'<tr class="liste_titre">';
207 $html .=
'<td>'.$langs->trans(
'GCP_Name').
'</td>';
208 $html .=
'<td>'.$langs->trans(
'GCP_displayName').
'</td>';
209 $html .=
'<td>'.$langs->trans(
'GCP_Id').
'</td>';
210 $html .=
'<td>'.$langs->trans(
'GCP_OwnerName').
'</td>';
211 $html .=
'<td>'.$langs->trans(
'GCP_State').
'</td>';
212 $html .=
'<td>'.$langs->trans(
'GCP_connectionStatus').
'</td>';
213 $html .=
'<td>'.$langs->trans(
'GCP_Type').
'</td>';
214 $html .=
'<td class="center">'.$langs->trans(
"Select").
'</td>';
215 $html .=
'</tr>'.
"\n";
218 foreach ($list[
'available'] as $printer_det) {
219 $html .=
'<tr class="oddeven">';
220 $html .=
'<td>'.$printer_det[
'name'].
'</td>';
221 $html .=
'<td>'.$printer_det[
'displayName'].
'</td>';
222 $html .=
'<td>'.$printer_det[
'id'].
'</td>';
223 $html .=
'<td>'.$printer_det[
'ownerName'].
'</td>';
224 $html .=
'<td>'.$printer_det[
'status'].
'</td>';
225 $html .=
'<td>'.$langs->trans(
'STATE_'.$printer_det[
'connectionStatus']).
'</td>';
226 $html .=
'<td>'.$langs->trans(
'TYPE_'.$printer_det[
'type']).
'</td>';
228 $html .=
'<td class="center">';
229 if ($conf->global->PRINTING_GCP_DEFAULT == $printer_det[
'id']) {
230 $html .=
img_picto($langs->trans(
"Default"),
'on');
232 $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>';
235 $html .=
'</tr>'.
"\n";
237 $this->resprint = $html;
252 $keyforprovider =
'';
255 $storage =
new DoliStorage($this->db, $conf, $keyforprovider);
257 $credentials =
new Credentials(
259 $this->google_secret,
260 DOL_MAIN_URL_ROOT.
'/core/modules/oauth/google_oauthcallback.php'
262 $serviceFactory = new \OAuth\ServiceFactory();
263 $apiService = $serviceFactory->createService($this->OAUTH_SERVICENAME_GOOGLE, $credentials, $storage, array());
264 '@phan-var-force OAuth\OAuth2\Service\Google $apiService';
268 $token = $storage->retrieveAccessToken($this->OAUTH_SERVICENAME_GOOGLE);
270 $this->errors[] = $e->getMessage();
276 $expire = ($token->getEndOfLife() !== -9002 && $token->getEndOfLife() !== -9001 && time() > ($token->getEndOfLife() - 30));
280 if ($token_ok && $expire) {
283 $refreshtoken = $token->getRefreshToken();
284 $token = $apiService->refreshAccessToken($token);
285 $token->setRefreshToken($refreshtoken);
286 $storage->storeAccessToken($this->OAUTH_SERVICENAME_GOOGLE, $token);
288 $this->errors[] = $e->getMessage();
293 $response = $apiService->request(self::PRINTERS_SEARCH_URL);
295 $this->errors[] = $e->getMessage();
296 print
'<pre>'.print_r($e->getMessage(),
true).
'</pre>';
299 $responsedata = json_decode($response,
true);
300 $printers = $responsedata[
'printers'];
302 if (is_array($printers) && count($printers) == 0) {
304 $ret[
'available'] = array();
307 $ret[
'available'] = $printers;
322 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
327 $fileprint = $conf->{$module}->dir_output;
329 $fileprint .=
'/'.$subdir;
331 $fileprint .=
'/'.$file;
335 $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);
336 $result = $this->db->query($sql);
338 $obj = $this->db->fetch_object($result);
340 $printer_id = $obj->printer_id;
345 $this->errors[] =
'NoDefaultPrinterDefined';
355 $this->error =
'PRINTGCP: '.$ret[
'errormessage'];
356 if ($ret[
'status'] != 1) {
375 if (empty($printerid)) {
376 return array(
'status' => 0,
'errorcode' =>
'',
'errormessage' =>
'No provided printer ID');
379 $handle = fopen($filepath,
"rb");
381 return array(
'status' => 0,
'errorcode' =>
'',
'errormessage' =>
'Could not read the file.');
384 $contents = fread($handle, filesize($filepath));
387 $post_fields = array(
388 'printerid' => $printerid,
389 'title' => $printjobtitle,
390 'contentTransferEncoding' =>
'base64',
391 'content' => base64_encode($contents),
392 'contentType' => $contenttype,
395 $keyforprovider =
'';
398 $storage =
new DoliStorage($this->db, $conf, $keyforprovider);
400 $credentials =
new Credentials(
402 $this->google_secret,
403 DOL_MAIN_URL_ROOT.
'/core/modules/oauth/google_oauthcallback.php?service=google'
405 $serviceFactory = new \OAuth\ServiceFactory();
406 $apiService = $serviceFactory->createService($this->OAUTH_SERVICENAME_GOOGLE, $credentials, $storage, array());
407 '@phan-var-force OAuth\OAuth2\Service\Google $apiService';
412 $token = $storage->retrieveAccessToken($this->OAUTH_SERVICENAME_GOOGLE);
414 $this->errors[] = $e->getMessage();
420 $refreshtoken = $token->getRefreshToken();
421 $token = $apiService->refreshAccessToken($token);
422 $token->setRefreshToken($refreshtoken);
423 $storage->storeAccessToken($this->OAUTH_SERVICENAME_GOOGLE, $token);
425 $this->errors[] = $e->getMessage();
430 $response = json_decode($apiService->request(self::PRINT_URL,
'POST', $post_fields),
true);
432 return array(
'status' => $response[
'success'],
'errorcode' => $response[
'errorCode'],
'errormessage' => $response[
'message']);
445 global $conf, $langs;
450 $keyforprovider =
'';
453 $storage =
new DoliStorage($this->db, $conf, $keyforprovider);
455 $credentials =
new Credentials(
457 $this->google_secret,
458 DOL_MAIN_URL_ROOT.
'/core/modules/oauth/google_oauthcallback.php'
460 $serviceFactory = new \OAuth\ServiceFactory();
461 $apiService = $serviceFactory->createService($this->OAUTH_SERVICENAME_GOOGLE, $credentials, $storage, array());
462 '@phan-var-force OAuth\OAuth2\Service\Google $apiService';
466 $token = $storage->retrieveAccessToken($this->OAUTH_SERVICENAME_GOOGLE);
468 $this->errors[] = $e->getMessage();
475 $expire = ($token->getEndOfLife() !== -9002 && $token->getEndOfLife() !== -9001 && time() > ($token->getEndOfLife() - 30));
479 if ($token_ok && $expire) {
482 $refreshtoken = $token->getRefreshToken();
483 $token = $apiService->refreshAccessToken($token);
484 $token->setRefreshToken($refreshtoken);
485 $storage->storeAccessToken($this->OAUTH_SERVICENAME_GOOGLE, $token);
487 $this->errors[] = $e->getMessage();
494 $response = $apiService->request(self::PRINTERS_GET_JOBS);
496 $this->errors[] = $e->getMessage();
499 $responsedata = json_decode($response,
true);
501 $html .=
'<div class="div-table-responsive">';
502 $html .=
'<table width="100%" class="noborder">';
503 $html .=
'<tr class="liste_titre">';
504 $html .=
'<td>'.$langs->trans(
"Id").
'</td>';
505 $html .=
'<td>'.$langs->trans(
"Date").
'</td>';
506 $html .=
'<td>'.$langs->trans(
"Owner").
'</td>';
507 $html .=
'<td>'.$langs->trans(
"Printer").
'</td>';
508 $html .=
'<td>'.$langs->trans(
"Filename").
'</td>';
509 $html .=
'<td>'.$langs->trans(
"Status").
'</td>';
510 $html .=
'<td>'.$langs->trans(
"Cancel").
'</td>';
511 $html .=
'</tr>'.
"\n";
513 $jobs = $responsedata[
'jobs'];
515 if (is_array($jobs)) {
516 foreach ($jobs as $value) {
517 $html .=
'<tr class="oddeven">';
518 $html .=
'<td>'.$value[
'id'].
'</td>';
519 $dates =
dol_print_date((
int) substr($value[
'createTime'], 0, 10),
'dayhour');
520 $html .=
'<td>'.$dates.
'</td>';
521 $html .=
'<td>'.$value[
'ownerId'].
'</td>';
522 $html .=
'<td>'.$value[
'printerName'].
'</td>';
523 $html .=
'<td>'.$value[
'title'].
'</td>';
524 $html .=
'<td>'.$value[
'status'].
'</td>';
525 $html .=
'<td> </td>';
529 $html .=
'<tr class="oddeven">';
530 $html .=
'<td colspan="7" class="opacitymedium">'.$langs->trans(
"None").
'</td>';
536 $this->resprint = $html;
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.
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 a Dolibarr global constant string value.
conf($dolibarr_main_document_root)
Load conf file (file must exists)