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;
31use OAuth\OAuth2\Service\Google;
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 public $errors = array();
88 private $OAUTH_SERVICENAME_GOOGLE =
'Google';
90 const LOGIN_URL =
'https://accounts.google.com/o/oauth2/token';
91 const PRINTERS_SEARCH_URL =
'https://www.google.com/cloudprint/search';
92 const PRINTERS_GET_JOBS =
'https://www.google.com/cloudprint/jobs';
93 const PRINT_URL =
'https://www.google.com/cloudprint/submit';
94 const LANGFILE =
'printgcp';
103 global $conf, $langs, $dolibarr_main_url_root;
106 $urlwithouturlroot = preg_replace(
'/'.preg_quote(DOL_URL_ROOT,
'/').
'$/i',
'', trim($dolibarr_main_url_root));
107 $urlwithroot = $urlwithouturlroot.DOL_URL_ROOT;
112 if (!$conf->oauth->enabled) {
113 $this->
conf[] = array(
114 'varname'=>
'PRINTGCP_INFO',
115 'info'=>$langs->transnoentitiesnoconv(
"WarningModuleNotActive",
"OAuth"),
119 $keyforprovider =
'';
124 $storage =
new DoliStorage($this->db, $conf, $keyforprovider);
127 $credentials =
new Credentials(
129 $this->google_secret,
130 $urlwithroot.
'/core/modules/oauth/google_oauthcallback.php'
132 $access = ($storage->hasAccessToken($this->OAUTH_SERVICENAME_GOOGLE) ?
'HasAccessToken' :
'NoAccessToken');
133 $serviceFactory = new \OAuth\ServiceFactory();
134 $apiService = $serviceFactory->createService($this->OAUTH_SERVICENAME_GOOGLE, $credentials, $storage, array());
137 $token = $storage->retrieveAccessToken($this->OAUTH_SERVICENAME_GOOGLE);
139 $this->errors[] = $e->getMessage();
146 $expire = ($token->getEndOfLife() !== -9002 && $token->getEndOfLife() !== -9001 && time() > ($token->getEndOfLife() - 30));
150 if ($token_ok && $expire) {
153 $refreshtoken = $token->getRefreshToken();
154 $token = $apiService->refreshAccessToken($token);
155 $token->setRefreshToken($refreshtoken);
156 $storage->storeAccessToken($this->OAUTH_SERVICENAME_GOOGLE, $token);
158 $this->errors[] = $e->getMessage();
161 if ($this->google_id !=
'' && $this->google_secret !=
'') {
162 $this->
conf[] = array(
'varname'=>
'PRINTGCP_INFO',
'info'=>
'GoogleAuthConfigured',
'type'=>
'info');
163 $this->
conf[] = array(
164 'varname'=>
'PRINTGCP_TOKEN_ACCESS',
167 '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'),
168 '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 $refreshtoken = $token->getRefreshToken();
175 $endoflife = $token->getEndOfLife();
177 if ($endoflife == $token::EOL_NEVER_EXPIRES) {
178 $expiredat = $langs->trans(
"Never");
179 } elseif ($endoflife == $token::EOL_UNKNOWN) {
180 $expiredat = $langs->trans(
"Unknown");
185 $this->
conf[] = array(
'varname'=>
'TOKEN_REFRESH',
'info'=>((!empty($refreshtoken)) ?
'Yes' :
'No'),
'type'=>
'info');
186 $this->
conf[] = array(
'varname'=>
'TOKEN_EXPIRED',
'info'=>($expire ?
'Yes' :
'No'),
'type'=>
'info');
187 $this->
conf[] = array(
'varname'=>
'TOKEN_EXPIRE_AT',
'info'=>($expiredat),
'type'=>
'info');
197 $this->
conf[] = array(
'varname'=>
'PRINTGCP_INFO',
'info'=>
'GoogleAuthNotConfigured',
'type'=>
'info');
201 $this->
conf[] = array(
'enabled'=>0,
'type'=>
'submit');
211 global $conf, $langs;
213 $langs->load(
'printing');
215 $html =
'<tr class="liste_titre">';
216 $html .=
'<td>'.$langs->trans(
'GCP_Name').
'</td>';
217 $html .=
'<td>'.$langs->trans(
'GCP_displayName').
'</td>';
218 $html .=
'<td>'.$langs->trans(
'GCP_Id').
'</td>';
219 $html .=
'<td>'.$langs->trans(
'GCP_OwnerName').
'</td>';
220 $html .=
'<td>'.$langs->trans(
'GCP_State').
'</td>';
221 $html .=
'<td>'.$langs->trans(
'GCP_connectionStatus').
'</td>';
222 $html .=
'<td>'.$langs->trans(
'GCP_Type').
'</td>';
223 $html .=
'<td class="center">'.$langs->trans(
"Select").
'</td>';
224 $html .=
'</tr>'.
"\n";
227 foreach ($list[
'available'] as $printer_det) {
228 $html .=
'<tr class="oddeven">';
229 $html .=
'<td>'.$printer_det[
'name'].
'</td>';
230 $html .=
'<td>'.$printer_det[
'displayName'].
'</td>';
231 $html .=
'<td>'.$printer_det[
'id'].
'</td>';
232 $html .=
'<td>'.$printer_det[
'ownerName'].
'</td>';
233 $html .=
'<td>'.$printer_det[
'status'].
'</td>';
234 $html .=
'<td>'.$langs->trans(
'STATE_'.$printer_det[
'connectionStatus']).
'</td>';
235 $html .=
'<td>'.$langs->trans(
'TYPE_'.$printer_det[
'type']).
'</td>';
237 $html .=
'<td class="center">';
238 if ($conf->global->PRINTING_GCP_DEFAULT == $printer_det[
'id']) {
239 $html .=
img_picto($langs->trans(
"Default"),
'on');
241 $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>';
244 $html .=
'</tr>'.
"\n";
246 $this->resprint = $html;
261 $keyforprovider =
'';
264 $storage =
new DoliStorage($this->db, $conf, $keyforprovider);
266 $credentials =
new Credentials(
268 $this->google_secret,
269 DOL_MAIN_URL_ROOT.
'/core/modules/oauth/google_oauthcallback.php'
271 $serviceFactory = new \OAuth\ServiceFactory();
272 $apiService = $serviceFactory->createService($this->OAUTH_SERVICENAME_GOOGLE, $credentials, $storage, array());
276 $token = $storage->retrieveAccessToken($this->OAUTH_SERVICENAME_GOOGLE);
278 $this->errors[] = $e->getMessage();
284 $expire = ($token->getEndOfLife() !== -9002 && $token->getEndOfLife() !== -9001 && time() > ($token->getEndOfLife() - 30));
288 if ($token_ok && $expire) {
291 $refreshtoken = $token->getRefreshToken();
292 $token = $apiService->refreshAccessToken($token);
293 $token->setRefreshToken($refreshtoken);
294 $storage->storeAccessToken($this->OAUTH_SERVICENAME_GOOGLE, $token);
296 $this->errors[] = $e->getMessage();
301 $response = $apiService->request(self::PRINTERS_SEARCH_URL);
303 $this->errors[] = $e->getMessage();
304 print
'<pre>'.print_r($e->getMessage(),
true).
'</pre>';
307 $responsedata = json_decode($response,
true);
308 $printers = $responsedata[
'printers'];
310 if (is_array($printers) && count($printers) == 0) {
312 $ret[
'available'] = array();
315 $ret[
'available'] = $printers;
330 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
335 $fileprint = $conf->{$module}->dir_output;
337 $fileprint .=
'/'.$subdir;
339 $fileprint .=
'/'.$file;
343 $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);
344 $result = $this->db->query($sql);
346 $obj = $this->db->fetch_object($result);
348 $printer_id = $obj->printer_id;
351 $printer_id = $conf->global->PRINTING_GCP_DEFAULT;
353 $this->errors[] =
'NoDefaultPrinterDefined';
363 $this->error =
'PRINTGCP: '.$ret[
'errormessage'];
364 if ($ret[
'status'] != 1) {
383 if (empty($printerid)) {
384 return array(
'status' =>0,
'errorcode' =>
'',
'errormessage'=>
'No provided printer ID');
387 $handle = fopen($filepath,
"rb");
389 return array(
'status' =>0,
'errorcode' =>
'',
'errormessage'=>
'Could not read the file.');
392 $contents = fread($handle, filesize($filepath));
395 $post_fields = array(
396 'printerid' => $printerid,
397 'title' => $printjobtitle,
398 'contentTransferEncoding' =>
'base64',
399 'content' => base64_encode($contents),
400 'contentType' => $contenttype,
403 $keyforprovider =
'';
406 $storage =
new DoliStorage($this->db, $conf, $keyforprovider);
408 $credentials =
new Credentials(
410 $this->google_secret,
411 DOL_MAIN_URL_ROOT.
'/core/modules/oauth/google_oauthcallback.php?service=google'
413 $serviceFactory = new \OAuth\ServiceFactory();
414 $apiService = $serviceFactory->createService($this->OAUTH_SERVICENAME_GOOGLE, $credentials, $storage, array());
419 $token = $storage->retrieveAccessToken($this->OAUTH_SERVICENAME_GOOGLE);
421 $this->errors[] = $e->getMessage();
427 $refreshtoken = $token->getRefreshToken();
428 $token = $apiService->refreshAccessToken($token);
429 $token->setRefreshToken($refreshtoken);
430 $storage->storeAccessToken($this->OAUTH_SERVICENAME_GOOGLE, $token);
432 $this->errors[] = $e->getMessage();
437 $response = json_decode($apiService->request(self::PRINT_URL,
'POST', $post_fields),
true);
439 return array(
'status' => $response[
'success'],
'errorcode' => $response[
'errorCode'],
'errormessage' => $response[
'message']);
450 global $conf, $langs;
455 $keyforprovider =
'';
458 $storage =
new DoliStorage($this->db, $conf, $keyforprovider);
460 $credentials =
new Credentials(
462 $this->google_secret,
463 DOL_MAIN_URL_ROOT.
'/core/modules/oauth/google_oauthcallback.php'
465 $serviceFactory = new \OAuth\ServiceFactory();
466 $apiService = $serviceFactory->createService($this->OAUTH_SERVICENAME_GOOGLE, $credentials, $storage, array());
470 $token = $storage->retrieveAccessToken($this->OAUTH_SERVICENAME_GOOGLE);
472 $this->errors[] = $e->getMessage();
479 $expire = ($token->getEndOfLife() !== -9002 && $token->getEndOfLife() !== -9001 && time() > ($token->getEndOfLife() - 30));
483 if ($token_ok && $expire) {
486 $refreshtoken = $token->getRefreshToken();
487 $token = $apiService->refreshAccessToken($token);
488 $token->setRefreshToken($refreshtoken);
489 $storage->storeAccessToken($this->OAUTH_SERVICENAME_GOOGLE, $token);
491 $this->errors[] = $e->getMessage();
498 $response = $apiService->request(self::PRINTERS_GET_JOBS);
500 $this->errors[] = $e->getMessage();
503 $responsedata = json_decode($response,
true);
505 $html .=
'<div class="div-table-responsive">';
506 $html .=
'<table width="100%" class="noborder">';
507 $html .=
'<tr class="liste_titre">';
508 $html .=
'<td>'.$langs->trans(
"Id").
'</td>';
509 $html .=
'<td>'.$langs->trans(
"Date").
'</td>';
510 $html .=
'<td>'.$langs->trans(
"Owner").
'</td>';
511 $html .=
'<td>'.$langs->trans(
"Printer").
'</td>';
512 $html .=
'<td>'.$langs->trans(
"Filename").
'</td>';
513 $html .=
'<td>'.$langs->trans(
"Status").
'</td>';
514 $html .=
'<td>'.$langs->trans(
"Cancel").
'</td>';
515 $html .=
'</tr>'.
"\n";
517 $jobs = $responsedata[
'jobs'];
519 if (is_array($jobs)) {
520 foreach ($jobs as $value) {
521 $html .=
'<tr class="oddeven">';
522 $html .=
'<td>'.$value[
'id'].
'</td>';
523 $dates =
dol_print_date((
int) substr($value[
'createTime'], 0, 10),
'dayhour');
524 $html .=
'<td>'.$dates.
'</td>';
525 $html .=
'<td>'.$value[
'ownerId'].
'</td>';
526 $html .=
'<td>'.$value[
'printerName'].
'</td>';
527 $html .=
'<td>'.$value[
'title'].
'</td>';
528 $html .=
'<td>'.$value[
'status'].
'</td>';
529 $html .=
'<td> </td>';
533 $html .=
'<tr class="oddeven">';
534 $html .=
'<td colspan="7" class="opacitymedium">'.$langs->trans(
"None").
'</td>';
540 $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.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
conf($dolibarr_main_document_root)
Load conf file (file must exists)