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"),
122 $storage =
new DoliStorage($this->
db, $this->
conf);
125 $credentials =
new Credentials(
127 $this->google_secret,
128 $urlwithroot.
'/core/modules/oauth/google_oauthcallback.php'
130 $access = ($storage->hasAccessToken($this->OAUTH_SERVICENAME_GOOGLE) ?
'HasAccessToken' :
'NoAccessToken');
131 $serviceFactory = new \OAuth\ServiceFactory();
132 $apiService = $serviceFactory->createService($this->OAUTH_SERVICENAME_GOOGLE, $credentials, $storage, array());
135 $token = $storage->retrieveAccessToken($this->OAUTH_SERVICENAME_GOOGLE);
137 $this->errors[] = $e->getMessage();
144 $expire = ($token->getEndOfLife() !== -9002 && $token->getEndOfLife() !== -9001 && time() > ($token->getEndOfLife() - 30));
148 if ($token_ok && $expire) {
151 $refreshtoken = $token->getRefreshToken();
152 $token = $apiService->refreshAccessToken($token);
153 $token->setRefreshToken($refreshtoken);
154 $storage->storeAccessToken($this->OAUTH_SERVICENAME_GOOGLE, $token);
156 $this->errors[] = $e->getMessage();
159 if ($this->google_id !=
'' && $this->google_secret !=
'') {
160 $this->
conf[] = array(
'varname'=>
'PRINTGCP_INFO',
'info'=>
'GoogleAuthConfigured',
'type'=>
'info');
161 $this->
conf[] = array(
162 'varname'=>
'PRINTGCP_TOKEN_ACCESS',
165 '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'),
166 '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') :
'')
171 $refreshtoken = $token->getRefreshToken();
173 $endoflife = $token->getEndOfLife();
175 if ($endoflife == $token::EOL_NEVER_EXPIRES) {
176 $expiredat = $langs->trans(
"Never");
177 } elseif ($endoflife == $token::EOL_UNKNOWN) {
178 $expiredat = $langs->trans(
"Unknown");
183 $this->
conf[] = array(
'varname'=>
'TOKEN_REFRESH',
'info'=>((!empty($refreshtoken)) ?
'Yes' :
'No'),
'type'=>
'info');
184 $this->
conf[] = array(
'varname'=>
'TOKEN_EXPIRED',
'info'=>($expire ?
'Yes' :
'No'),
'type'=>
'info');
185 $this->
conf[] = array(
'varname'=>
'TOKEN_EXPIRE_AT',
'info'=>($expiredat),
'type'=>
'info');
195 $this->
conf[] = array(
'varname'=>
'PRINTGCP_INFO',
'info'=>
'GoogleAuthNotConfigured',
'type'=>
'info');
199 $this->
conf[] = array(
'enabled'=>0,
'type'=>
'submit');
209 global $conf, $langs;
211 $langs->load(
'printing');
213 $html =
'<tr class="liste_titre">';
214 $html .=
'<td>'.$langs->trans(
'GCP_Name').
'</td>';
215 $html .=
'<td>'.$langs->trans(
'GCP_displayName').
'</td>';
216 $html .=
'<td>'.$langs->trans(
'GCP_Id').
'</td>';
217 $html .=
'<td>'.$langs->trans(
'GCP_OwnerName').
'</td>';
218 $html .=
'<td>'.$langs->trans(
'GCP_State').
'</td>';
219 $html .=
'<td>'.$langs->trans(
'GCP_connectionStatus').
'</td>';
220 $html .=
'<td>'.$langs->trans(
'GCP_Type').
'</td>';
221 $html .=
'<td class="center">'.$langs->trans(
"Select").
'</td>';
222 $html .=
'</tr>'.
"\n";
225 foreach ($list[
'available'] as $printer_det) {
226 $html .=
'<tr class="oddeven">';
227 $html .=
'<td>'.$printer_det[
'name'].
'</td>';
228 $html .=
'<td>'.$printer_det[
'displayName'].
'</td>';
229 $html .=
'<td>'.$printer_det[
'id'].
'</td>';
230 $html .=
'<td>'.$printer_det[
'ownerName'].
'</td>';
231 $html .=
'<td>'.$printer_det[
'status'].
'</td>';
232 $html .=
'<td>'.$langs->trans(
'STATE_'.$printer_det[
'connectionStatus']).
'</td>';
233 $html .=
'<td>'.$langs->trans(
'TYPE_'.$printer_det[
'type']).
'</td>';
235 $html .=
'<td class="center">';
236 if ($conf->global->PRINTING_GCP_DEFAULT == $printer_det[
'id']) {
237 $html .=
img_picto($langs->trans(
"Default"),
'on');
239 $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>';
242 $html .=
'</tr>'.
"\n";
244 $this->resprint = $html;
258 $storage =
new DoliStorage($this->
db, $this->
conf);
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());
270 $token = $storage->retrieveAccessToken($this->OAUTH_SERVICENAME_GOOGLE);
272 $this->errors[] = $e->getMessage();
278 $expire = ($token->getEndOfLife() !== -9002 && $token->getEndOfLife() !== -9001 && time() > ($token->getEndOfLife() - 30));
282 if ($token_ok && $expire) {
285 $refreshtoken = $token->getRefreshToken();
286 $token = $apiService->refreshAccessToken($token);
287 $token->setRefreshToken($refreshtoken);
288 $storage->storeAccessToken($this->OAUTH_SERVICENAME_GOOGLE, $token);
290 $this->errors[] = $e->getMessage();
295 $response = $apiService->request(self::PRINTERS_SEARCH_URL);
297 $this->errors[] = $e->getMessage();
298 print
'<pre>'.print_r($e->getMessage(),
true).
'</pre>';
301 $responsedata = json_decode($response,
true);
302 $printers = $responsedata[
'printers'];
304 if (is_array($printers) && count($printers) == 0) {
306 $ret[
'available'] = array();
309 $ret[
'available'] = $printers;
324 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
329 $fileprint = $conf->{$module}->dir_output;
331 $fileprint .=
'/'.$subdir;
333 $fileprint .=
'/'.$file;
337 $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);
338 $result = $this->
db->query($sql);
340 $obj = $this->
db->fetch_object($result);
342 $printer_id = $obj->printer_id;
344 if (!empty($conf->global->PRINTING_GCP_DEFAULT)) {
345 $printer_id = $conf->global->PRINTING_GCP_DEFAULT;
347 $this->errors[] =
'NoDefaultPrinterDefined';
357 $this->error =
'PRINTGCP: '.$ret[
'errormessage'];
358 if ($ret[
'status'] != 1) {
376 if (empty($printerid)) {
377 return array(
'status' =>0,
'errorcode' =>
'',
'errormessage'=>
'No provided printer ID');
380 $handle = fopen($filepath,
"rb");
382 return array(
'status' =>0,
'errorcode' =>
'',
'errormessage'=>
'Could not read the file.');
385 $contents = fread($handle, filesize($filepath));
388 $post_fields = array(
389 'printerid' => $printerid,
390 'title' => $printjobtitle,
391 'contentTransferEncoding' =>
'base64',
392 'content' => base64_encode($contents),
393 'contentType' => $contenttype,
396 $storage =
new DoliStorage($this->
db, $this->
conf);
398 $credentials =
new Credentials(
400 $this->google_secret,
401 DOL_MAIN_URL_ROOT.
'/core/modules/oauth/google_oauthcallback.php?service=google'
403 $serviceFactory = new \OAuth\ServiceFactory();
404 $apiService = $serviceFactory->createService($this->OAUTH_SERVICENAME_GOOGLE, $credentials, $storage, array());
409 $token = $storage->retrieveAccessToken($this->OAUTH_SERVICENAME_GOOGLE);
411 $this->errors[] = $e->getMessage();
417 $refreshtoken = $token->getRefreshToken();
418 $token = $apiService->refreshAccessToken($token);
419 $token->setRefreshToken($refreshtoken);
420 $storage->storeAccessToken($this->OAUTH_SERVICENAME_GOOGLE, $token);
422 $this->errors[] = $e->getMessage();
427 $response = json_decode($apiService->request(self::PRINT_URL,
'POST', $post_fields),
true);
429 return array(
'status' => $response[
'success'],
'errorcode' => $response[
'errorCode'],
'errormessage' => $response[
'message']);
440 global $conf, $langs;
445 $storage =
new DoliStorage($this->
db, $this->
conf);
447 $credentials =
new Credentials(
449 $this->google_secret,
450 DOL_MAIN_URL_ROOT.
'/core/modules/oauth/google_oauthcallback.php'
452 $serviceFactory = new \OAuth\ServiceFactory();
453 $apiService = $serviceFactory->createService($this->OAUTH_SERVICENAME_GOOGLE, $credentials, $storage, array());
457 $token = $storage->retrieveAccessToken($this->OAUTH_SERVICENAME_GOOGLE);
459 $this->errors[] = $e->getMessage();
466 $expire = ($token->getEndOfLife() !== -9002 && $token->getEndOfLife() !== -9001 && time() > ($token->getEndOfLife() - 30));
470 if ($token_ok && $expire) {
473 $refreshtoken = $token->getRefreshToken();
474 $token = $apiService->refreshAccessToken($token);
475 $token->setRefreshToken($refreshtoken);
476 $storage->storeAccessToken($this->OAUTH_SERVICENAME_GOOGLE, $token);
478 $this->errors[] = $e->getMessage();
485 $response = $apiService->request(self::PRINTERS_GET_JOBS);
487 $this->errors[] = $e->getMessage();
490 $responsedata = json_decode($response,
true);
492 $html .=
'<div class="div-table-responsive">';
493 $html .=
'<table width="100%" class="noborder">';
494 $html .=
'<tr class="liste_titre">';
495 $html .=
'<td>'.$langs->trans(
"Id").
'</td>';
496 $html .=
'<td>'.$langs->trans(
"Date").
'</td>';
497 $html .=
'<td>'.$langs->trans(
"Owner").
'</td>';
498 $html .=
'<td>'.$langs->trans(
"Printer").
'</td>';
499 $html .=
'<td>'.$langs->trans(
"Filename").
'</td>';
500 $html .=
'<td>'.$langs->trans(
"Status").
'</td>';
501 $html .=
'<td>'.$langs->trans(
"Cancel").
'</td>';
502 $html .=
'</tr>'.
"\n";
504 $jobs = $responsedata[
'jobs'];
506 if (is_array($jobs)) {
507 foreach ($jobs as $value) {
508 $html .=
'<tr class="oddeven">';
509 $html .=
'<td>'.$value[
'id'].
'</td>';
510 $dates =
dol_print_date((
int) substr($value[
'createTime'], 0, 10),
'dayhour');
511 $html .=
'<td>'.$dates.
'</td>';
512 $html .=
'<td>'.$value[
'ownerId'].
'</td>';
513 $html .=
'<td>'.$value[
'printerName'].
'</td>';
514 $html .=
'<td>'.$value[
'title'].
'</td>';
515 $html .=
'<td>'.$value[
'status'].
'</td>';
516 $html .=
'<td> </td>';
520 $html .=
'<tr class="oddeven">';
521 $html .=
'<td colspan="7" class="opacitymedium">'.$langs->trans(
"None").
'</td>';
527 $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)
newToken()
Return the value of token currently saved into session with name 'newtoken'.
if(!function_exists('utf8_encode')) if(!function_exists('utf8_decode')) getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
conf($dolibarr_main_document_root)
Load conf file (file must exists)
$conf db
API class for accounts.