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';
96 global $conf, $langs, $dolibarr_main_url_root;
99 $urlwithouturlroot = preg_replace(
'/'.preg_quote(DOL_URL_ROOT,
'/').
'$/i',
'', trim($dolibarr_main_url_root));
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;
255 $keyforprovider =
'';
258 $storage =
new DoliStorage($this->db,
$conf, $keyforprovider);
260 $credentials =
new Credentials(
262 $this->google_secret,
263 DOL_MAIN_URL_ROOT.
'/core/modules/oauth/google_oauthcallback.php'
265 $serviceFactory = new \OAuth\ServiceFactory();
266 $apiService = $serviceFactory->createService($this->OAUTH_SERVICENAME_GOOGLE, $credentials, $storage, array());
267 '@phan-var-force OAuth\OAuth2\Service\Google $apiService';
271 $token = $storage->retrieveAccessToken($this->OAUTH_SERVICENAME_GOOGLE);
273 $this->errors[] = $e->getMessage();
279 $expire = ($token->getEndOfLife() !== -9002 && $token->getEndOfLife() !== -9001 && time() > ($token->getEndOfLife() - 30));
283 if ($token_ok && $expire) {
286 $refreshtoken = $token->getRefreshToken();
287 $token = $apiService->refreshAccessToken($token);
288 $token->setRefreshToken($refreshtoken);
289 $storage->storeAccessToken($this->OAUTH_SERVICENAME_GOOGLE, $token);
291 $this->errors[] = $e->getMessage();
296 $response = $apiService->request(self::PRINTERS_SEARCH_URL);
298 $this->errors[] = $e->getMessage();
299 print
'<pre>'.print_r($e->getMessage(),
true).
'</pre>';
302 $responsedata = json_decode($response,
true);
303 $printers = $responsedata[
'printers'];
305 if (is_array($printers) && count($printers) == 0) {
307 $ret[
'available'] = array();
310 $ret[
'available'] = $printers;
325 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
330 $fileprint =
$conf->{$module}->dir_output;
332 $fileprint .=
'/'.$subdir;
334 $fileprint .=
'/'.$file;
338 $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);
339 $result = $this->db->query($sql);
341 $obj = $this->db->fetch_object($result);
343 $printer_id = $obj->printer_id;
348 $this->errors[] =
'NoDefaultPrinterDefined';
358 $this->error =
'PRINTGCP: '.$ret[
'errormessage'];
359 if ($ret[
'status'] != 1) {
378 if (empty($printerid)) {
379 return array(
'status' => 0,
'errorcode' =>
'',
'errormessage' =>
'No provided printer ID');
382 $handle = fopen($filepath,
"rb");
384 return array(
'status' => 0,
'errorcode' =>
'',
'errormessage' =>
'Could not read the file.');
387 $contents = fread($handle, filesize($filepath));
390 $post_fields = array(
391 'printerid' => $printerid,
392 'title' => $printjobtitle,
393 'contentTransferEncoding' =>
'base64',
394 'content' => base64_encode($contents),
395 'contentType' => $contenttype,
398 $keyforprovider =
'';
401 $storage =
new DoliStorage($this->db,
$conf, $keyforprovider);
403 $credentials =
new Credentials(
405 $this->google_secret,
406 DOL_MAIN_URL_ROOT.
'/core/modules/oauth/google_oauthcallback.php?service=google'
408 $serviceFactory = new \OAuth\ServiceFactory();
409 $apiService = $serviceFactory->createService($this->OAUTH_SERVICENAME_GOOGLE, $credentials, $storage, array());
410 '@phan-var-force OAuth\OAuth2\Service\Google $apiService';
415 $token = $storage->retrieveAccessToken($this->OAUTH_SERVICENAME_GOOGLE);
417 $this->errors[] = $e->getMessage();
423 $refreshtoken = $token->getRefreshToken();
424 $token = $apiService->refreshAccessToken($token);
425 $token->setRefreshToken($refreshtoken);
426 $storage->storeAccessToken($this->OAUTH_SERVICENAME_GOOGLE, $token);
428 $this->errors[] = $e->getMessage();
433 $response = json_decode($apiService->request(self::PRINT_URL,
'POST', $post_fields),
true);
435 return array(
'status' => $response[
'success'],
'errorcode' => $response[
'errorCode'],
'errormessage' => $response[
'message']);
448 global
$conf, $langs;
453 $keyforprovider =
'';
456 $storage =
new DoliStorage($this->db,
$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());
465 '@phan-var-force OAuth\OAuth2\Service\Google $apiService';
469 $token = $storage->retrieveAccessToken($this->OAUTH_SERVICENAME_GOOGLE);
471 $this->errors[] = $e->getMessage();
478 $expire = ($token->getEndOfLife() !== -9002 && $token->getEndOfLife() !== -9001 && time() > ($token->getEndOfLife() - 30));
482 if ($token_ok && $expire) {
485 $refreshtoken = $token->getRefreshToken();
486 $token = $apiService->refreshAccessToken($token);
487 $token->setRefreshToken($refreshtoken);
488 $storage->storeAccessToken($this->OAUTH_SERVICENAME_GOOGLE, $token);
490 $this->errors[] = $e->getMessage();
497 $response = $apiService->request(self::PRINTERS_GET_JOBS);
499 $this->errors[] = $e->getMessage();
502 $responsedata = json_decode($response,
true);
504 $html .=
'<div class="div-table-responsive">';
505 $html .=
'<table width="100%" class="noborder">';
506 $html .=
'<tr class="liste_titre">';
507 $html .=
'<td>'.$langs->trans(
"Id").
'</td>';
508 $html .=
'<td>'.$langs->trans(
"Date").
'</td>';
509 $html .=
'<td>'.$langs->trans(
"Owner").
'</td>';
510 $html .=
'<td>'.$langs->trans(
"Printer").
'</td>';
511 $html .=
'<td>'.$langs->trans(
"Filename").
'</td>';
512 $html .=
'<td>'.$langs->trans(
"Status").
'</td>';
513 $html .=
'<td>'.$langs->trans(
"Cancel").
'</td>';
514 $html .=
'</tr>'.
"\n";
516 $jobs = $responsedata[
'jobs'];
518 if (is_array($jobs)) {
519 foreach ($jobs as $value) {
520 $html .=
'<tr class="oddeven">';
521 $html .=
'<td>'.$value[
'id'].
'</td>';
522 $dates =
dol_print_date((
int) substr($value[
'createTime'], 0, 10),
'dayhour');
523 $html .=
'<td>'.$dates.
'</td>';
524 $html .=
'<td>'.$value[
'ownerId'].
'</td>';
525 $html .=
'<td>'.$value[
'printerName'].
'</td>';
526 $html .=
'<td>'.$value[
'title'].
'</td>';
527 $html .=
'<td>'.$value[
'status'].
'</td>';
528 $html .=
'<td> </td>';
532 $html .=
'<tr class="oddeven">';
533 $html .=
'<td colspan="7" class="opacitymedium">'.$langs->trans(
"None").
'</td>';
539 $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)
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...