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;
105 if (!
$conf->oauth->enabled) {
106 $this->
conf[] = array(
107 'varname' =>
'PRINTGCP_INFO',
108 'info' => $langs->transnoentitiesnoconv(
"WarningModuleNotActive",
"OAuth"),
112 $keyforprovider =
'';
117 $storage =
new DoliStorage($this->db,
$conf, $keyforprovider);
120 $credentials =
new Credentials(
122 $this->google_secret,
123 $urlwithroot.
'/core/modules/oauth/google_oauthcallback.php'
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';
131 $token = $storage->retrieveAccessToken($this->OAUTH_SERVICENAME_GOOGLE);
133 $this->errors[] = $e->getMessage();
140 $expire = ($token->getEndOfLife() !== -9002 && $token->getEndOfLife() !== -9001 && time() > ($token->getEndOfLife() - 30));
144 if ($token_ok && $expire) {
147 $refreshtoken = $token->getRefreshToken();
148 $token = $apiService->refreshAccessToken($token);
149 $token->setRefreshToken($refreshtoken);
150 $storage->storeAccessToken($this->OAUTH_SERVICENAME_GOOGLE, $token);
152 $this->errors[] = $e->getMessage();
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',
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') :
'')
167 $refreshtoken = $token->getRefreshToken();
169 $endoflife = $token->getEndOfLife();
171 if ($endoflife == $token::EOL_NEVER_EXPIRES) {
172 $expiredat = $langs->trans(
"Never");
173 } elseif ($endoflife == $token::EOL_UNKNOWN) {
174 $expiredat = $langs->trans(
"Unknown");
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');
191 $this->
conf[] = array(
'varname' =>
'PRINTGCP_INFO',
'info' =>
'GoogleAuthNotConfigured',
'type' =>
'info');
195 $this->
conf[] = array(
'enabled' => 0,
'type' =>
'submit');
205 global $conf, $langs;
207 $langs->load(
'printing');
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";
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>';
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>';
231 $html .=
'<td class="center">';
232 if (
$conf->global->PRINTING_GCP_DEFAULT == $printer_det[
'id']) {
233 $html .=
img_picto($langs->trans(
"Default"),
'on');
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>';
238 $html .=
'</tr>'.
"\n";
240 $this->resprint = $html;
267 $keyforprovider =
'';
270 $storage =
new DoliStorage($this->db,
$conf, $keyforprovider);
272 $credentials =
new Credentials(
274 $this->google_secret,
275 DOL_MAIN_URL_ROOT.
'/core/modules/oauth/google_oauthcallback.php'
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';
283 $token = $storage->retrieveAccessToken($this->OAUTH_SERVICENAME_GOOGLE);
285 $this->errors[] = $e->getMessage();
291 $expire = ($token->getEndOfLife() !== -9002 && $token->getEndOfLife() !== -9001 && time() > ($token->getEndOfLife() - 30));
295 if ($token_ok && $expire) {
298 $refreshtoken = $token->getRefreshToken();
299 $token = $apiService->refreshAccessToken($token);
300 $token->setRefreshToken($refreshtoken);
301 $storage->storeAccessToken($this->OAUTH_SERVICENAME_GOOGLE, $token);
303 $this->errors[] = $e->getMessage();
308 $response = $apiService->request(self::PRINTERS_SEARCH_URL);
310 $this->errors[] = $e->getMessage();
311 print
'<pre>'.print_r($e->getMessage(),
true).
'</pre>';
314 $responsedata = json_decode($response,
true);
315 $printers = $responsedata[
'printers'];
317 if (is_array($printers) && count($printers) == 0) {
319 $ret[
'available'] = array();
322 $ret[
'available'] = $printers;
337 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
342 $fileprint =
$conf->{$module}->dir_output;
344 $fileprint .=
'/'.$subdir;
346 $fileprint .=
'/'.$file;
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);
353 $obj = $this->db->fetch_object($result);
355 $printer_id = $obj->printer_id;
360 $this->errors[] =
'NoDefaultPrinterDefined';
370 $this->error =
'PRINTGCP: '.$ret[
'errormessage'];
371 if ($ret[
'status'] != 1) {
390 if (empty($printerid)) {
391 return array(
'status' => 0,
'errorcode' =>
'',
'errormessage' =>
'No provided printer ID');
394 $handle = fopen($filepath,
"rb");
396 return array(
'status' => 0,
'errorcode' =>
'',
'errormessage' =>
'Could not read the file.');
399 $contents = fread($handle, filesize($filepath));
402 $post_fields = array(
403 'printerid' => $printerid,
404 'title' => $printjobtitle,
405 'contentTransferEncoding' =>
'base64',
406 'content' => base64_encode($contents),
407 'contentType' => $contenttype,
410 $keyforprovider =
'';
413 $storage =
new DoliStorage($this->db,
$conf, $keyforprovider);
415 $credentials =
new Credentials(
417 $this->google_secret,
418 DOL_MAIN_URL_ROOT.
'/core/modules/oauth/google_oauthcallback.php?service=google'
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';
427 $token = $storage->retrieveAccessToken($this->OAUTH_SERVICENAME_GOOGLE);
429 $this->errors[] = $e->getMessage();
435 $refreshtoken = $token->getRefreshToken();
436 $token = $apiService->refreshAccessToken($token);
437 $token->setRefreshToken($refreshtoken);
438 $storage->storeAccessToken($this->OAUTH_SERVICENAME_GOOGLE, $token);
440 $this->errors[] = $e->getMessage();
445 $response = json_decode($apiService->request(self::PRINT_URL,
'POST', $post_fields),
true);
447 return array(
'status' => $response[
'success'],
'errorcode' => $response[
'errorCode'],
'errormessage' => $response[
'message']);
460 global
$conf, $langs;
465 $keyforprovider =
'';
468 $storage =
new DoliStorage($this->db,
$conf, $keyforprovider);
470 $credentials =
new Credentials(
472 $this->google_secret,
473 DOL_MAIN_URL_ROOT.
'/core/modules/oauth/google_oauthcallback.php'
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';
481 $token = $storage->retrieveAccessToken($this->OAUTH_SERVICENAME_GOOGLE);
483 $this->errors[] = $e->getMessage();
490 $expire = ($token->getEndOfLife() !== -9002 && $token->getEndOfLife() !== -9001 && time() > ($token->getEndOfLife() - 30));
494 if ($token_ok && $expire) {
497 $refreshtoken = $token->getRefreshToken();
498 $token = $apiService->refreshAccessToken($token);
499 $token->setRefreshToken($refreshtoken);
500 $storage->storeAccessToken($this->OAUTH_SERVICENAME_GOOGLE, $token);
502 $this->errors[] = $e->getMessage();
509 $response = $apiService->request(self::PRINTERS_GET_JOBS);
511 $this->errors[] = $e->getMessage();
514 $responsedata = json_decode($response,
true);
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";
528 $jobs = $responsedata[
'jobs'];
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> </td>';
544 $html .=
'<tr class="oddeven">';
545 $html .=
'<td colspan="7" class="opacitymedium">'.$langs->trans(
"None").
'</td>';
551 $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.
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)
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...