26 include_once DOL_DOCUMENT_ROOT.
'/core/modules/printing/modules_printing.php';
27 require_once DOL_DOCUMENT_ROOT.
'/includes/OAuth/bootstrap.php';
29 use OAuth\Common\Storage\DoliStorage;
30 use OAuth\Common\Consumer\Credentials;
31 use 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, $this->
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;
260 $keyforprovider =
'';
263 $storage =
new DoliStorage($this->
db, $this->
conf, $keyforprovider);
265 $credentials =
new Credentials(
267 $this->google_secret,
268 DOL_MAIN_URL_ROOT.
'/core/modules/oauth/google_oauthcallback.php'
270 $serviceFactory = new \OAuth\ServiceFactory();
271 $apiService = $serviceFactory->createService($this->OAUTH_SERVICENAME_GOOGLE, $credentials, $storage, array());
275 $token = $storage->retrieveAccessToken($this->OAUTH_SERVICENAME_GOOGLE);
277 $this->errors[] = $e->getMessage();
283 $expire = ($token->getEndOfLife() !== -9002 && $token->getEndOfLife() !== -9001 && time() > ($token->getEndOfLife() - 30));
287 if ($token_ok && $expire) {
290 $refreshtoken = $token->getRefreshToken();
291 $token = $apiService->refreshAccessToken($token);
292 $token->setRefreshToken($refreshtoken);
293 $storage->storeAccessToken($this->OAUTH_SERVICENAME_GOOGLE, $token);
295 $this->errors[] = $e->getMessage();
300 $response = $apiService->request(self::PRINTERS_SEARCH_URL);
302 $this->errors[] = $e->getMessage();
303 print
'<pre>'.print_r($e->getMessage(),
true).
'</pre>';
306 $responsedata = json_decode($response,
true);
307 $printers = $responsedata[
'printers'];
309 if (is_array($printers) && count($printers) == 0) {
311 $ret[
'available'] = array();
314 $ret[
'available'] = $printers;
329 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
334 $fileprint = $conf->{$module}->dir_output;
336 $fileprint .=
'/'.$subdir;
338 $fileprint .=
'/'.$file;
342 $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);
343 $result = $this->
db->query(
$sql);
345 $obj = $this->
db->fetch_object($result);
347 $printer_id = $obj->printer_id;
349 if (!empty($conf->global->PRINTING_GCP_DEFAULT)) {
350 $printer_id = $conf->global->PRINTING_GCP_DEFAULT;
352 $this->errors[] =
'NoDefaultPrinterDefined';
362 $this->error =
'PRINTGCP: '.$ret[
'errormessage'];
363 if ($ret[
'status'] != 1) {
381 if (empty($printerid)) {
382 return array(
'status' =>0,
'errorcode' =>
'',
'errormessage'=>
'No provided printer ID');
385 $handle = fopen($filepath,
"rb");
387 return array(
'status' =>0,
'errorcode' =>
'',
'errormessage'=>
'Could not read the file.');
390 $contents = fread($handle, filesize($filepath));
393 $post_fields = array(
394 'printerid' => $printerid,
395 'title' => $printjobtitle,
396 'contentTransferEncoding' =>
'base64',
397 'content' => base64_encode($contents),
398 'contentType' => $contenttype,
401 $keyforprovider =
'';
404 $storage =
new DoliStorage($this->
db, $this->
conf, $keyforprovider);
406 $credentials =
new Credentials(
408 $this->google_secret,
409 DOL_MAIN_URL_ROOT.
'/core/modules/oauth/google_oauthcallback.php?service=google'
411 $serviceFactory = new \OAuth\ServiceFactory();
412 $apiService = $serviceFactory->createService($this->OAUTH_SERVICENAME_GOOGLE, $credentials, $storage, array());
417 $token = $storage->retrieveAccessToken($this->OAUTH_SERVICENAME_GOOGLE);
419 $this->errors[] = $e->getMessage();
425 $refreshtoken = $token->getRefreshToken();
426 $token = $apiService->refreshAccessToken($token);
427 $token->setRefreshToken($refreshtoken);
428 $storage->storeAccessToken($this->OAUTH_SERVICENAME_GOOGLE, $token);
430 $this->errors[] = $e->getMessage();
435 $response = json_decode($apiService->request(self::PRINT_URL,
'POST', $post_fields),
true);
437 return array(
'status' => $response[
'success'],
'errorcode' => $response[
'errorCode'],
'errormessage' => $response[
'message']);
448 global $conf, $langs;
453 $keyforprovider =
'';
456 $storage =
new DoliStorage($this->
db, $this->
conf, $keyforprovider);
458 $credentials =
new Credentials(
460 $this->google_secret,
461 DOL_MAIN_URL_ROOT.
'/core/modules/oauth/google_oauthcallback.php'
463 $serviceFactory = new \OAuth\ServiceFactory();
464 $apiService = $serviceFactory->createService($this->OAUTH_SERVICENAME_GOOGLE, $credentials, $storage, array());
468 $token = $storage->retrieveAccessToken($this->OAUTH_SERVICENAME_GOOGLE);
470 $this->errors[] = $e->getMessage();
477 $expire = ($token->getEndOfLife() !== -9002 && $token->getEndOfLife() !== -9001 && time() > ($token->getEndOfLife() - 30));
481 if ($token_ok && $expire) {
484 $refreshtoken = $token->getRefreshToken();
485 $token = $apiService->refreshAccessToken($token);
486 $token->setRefreshToken($refreshtoken);
487 $storage->storeAccessToken($this->OAUTH_SERVICENAME_GOOGLE, $token);
489 $this->errors[] = $e->getMessage();
496 $response = $apiService->request(self::PRINTERS_GET_JOBS);
498 $this->errors[] = $e->getMessage();
501 $responsedata = json_decode($response,
true);
503 $html .=
'<div class="div-table-responsive">';
504 $html .=
'<table width="100%" class="noborder">';
505 $html .=
'<tr class="liste_titre">';
506 $html .=
'<td>'.$langs->trans(
"Id").
'</td>';
507 $html .=
'<td>'.$langs->trans(
"Date").
'</td>';
508 $html .=
'<td>'.$langs->trans(
"Owner").
'</td>';
509 $html .=
'<td>'.$langs->trans(
"Printer").
'</td>';
510 $html .=
'<td>'.$langs->trans(
"Filename").
'</td>';
511 $html .=
'<td>'.$langs->trans(
"Status").
'</td>';
512 $html .=
'<td>'.$langs->trans(
"Cancel").
'</td>';
513 $html .=
'</tr>'.
"\n";
515 $jobs = $responsedata[
'jobs'];
517 if (is_array($jobs)) {
518 foreach ($jobs as $value) {
519 $html .=
'<tr class="oddeven">';
520 $html .=
'<td>'.$value[
'id'].
'</td>';
521 $dates =
dol_print_date((
int) substr($value[
'createTime'], 0, 10),
'dayhour');
522 $html .=
'<td>'.$dates.
'</td>';
523 $html .=
'<td>'.$value[
'ownerId'].
'</td>';
524 $html .=
'<td>'.$value[
'printerName'].
'</td>';
525 $html .=
'<td>'.$value[
'title'].
'</td>';
526 $html .=
'<td>'.$value[
'status'].
'</td>';
527 $html .=
'<td> </td>';
531 $html .=
'<tr class="oddeven">';
532 $html .=
'<td colspan="7" class="opacitymedium">'.$langs->trans(
"None").
'</td>';
538 $this->resprint = $html;