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 $this->google_id = $conf->global->OAUTH_GOOGLE_ID;
120 $this->google_secret = $conf->global->OAUTH_GOOGLE_SECRET;
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();
145 $expire = ($token->getEndOfLife() !== -9002 && $token->getEndOfLife() !== -9001 && time() > ($token->getEndOfLife() - 30));
149 if ($token_ok && $expire) {
152 $refreshtoken = $token->getRefreshToken();
153 $token = $apiService->refreshAccessToken($token);
154 $token->setRefreshToken($refreshtoken);
155 $storage->storeAccessToken($this->OAUTH_SERVICENAME_GOOGLE, $token);
157 $this->errors[] = $e->getMessage();
160 if ($this->google_id !=
'' && $this->google_secret !=
'') {
161 $this->
conf[] = array(
'varname'=>
'PRINTGCP_INFO',
'info'=>
'GoogleAuthConfigured',
'type'=>
'info');
162 $this->
conf[] = array(
163 'varname'=>
'PRINTGCP_TOKEN_ACCESS',
166 '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'),
167 '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') :
'')
172 $refreshtoken = $token->getRefreshToken();
174 $endoflife = $token->getEndOfLife();
176 if ($endoflife == $token::EOL_NEVER_EXPIRES)
178 $expiredat = $langs->trans(
"Never");
179 } elseif ($endoflife == $token::EOL_UNKNOWN)
181 $expiredat = $langs->trans(
"Unknown");
186 $this->
conf[] = array(
'varname'=>
'TOKEN_REFRESH',
'info'=>((!empty($refreshtoken)) ?
'Yes' :
'No'),
'type'=>
'info');
187 $this->
conf[] = array(
'varname'=>
'TOKEN_EXPIRED',
'info'=>($expire ?
'Yes' :
'No'),
'type'=>
'info');
188 $this->
conf[] = array(
'varname'=>
'TOKEN_EXPIRE_AT',
'info'=>($expiredat),
'type'=>
'info');
198 $this->
conf[] = array(
'varname'=>
'PRINTGCP_INFO',
'info'=>
'GoogleAuthNotConfigured',
'type'=>
'info');
202 $this->
conf[] = array(
'enabled'=>0,
'type'=>
'submit');
212 global $conf, $langs;
214 $langs->load(
'printing');
216 $html =
'<tr class="liste_titre">';
217 $html .=
'<td>'.$langs->trans(
'GCP_Name').
'</td>';
218 $html .=
'<td>'.$langs->trans(
'GCP_displayName').
'</td>';
219 $html .=
'<td>'.$langs->trans(
'GCP_Id').
'</td>';
220 $html .=
'<td>'.$langs->trans(
'GCP_OwnerName').
'</td>';
221 $html .=
'<td>'.$langs->trans(
'GCP_State').
'</td>';
222 $html .=
'<td>'.$langs->trans(
'GCP_connectionStatus').
'</td>';
223 $html .=
'<td>'.$langs->trans(
'GCP_Type').
'</td>';
224 $html .=
'<td class="center">'.$langs->trans(
"Select").
'</td>';
225 $html .=
'</tr>'.
"\n";
228 foreach ($list[
'available'] as $printer_det)
230 $html .=
'<tr class="oddeven">';
231 $html .=
'<td>'.$printer_det[
'name'].
'</td>';
232 $html .=
'<td>'.$printer_det[
'displayName'].
'</td>';
233 $html .=
'<td>'.$printer_det[
'id'].
'</td>';
234 $html .=
'<td>'.$printer_det[
'ownerName'].
'</td>';
235 $html .=
'<td>'.$printer_det[
'status'].
'</td>';
236 $html .=
'<td>'.$langs->trans(
'STATE_'.$printer_det[
'connectionStatus']).
'</td>';
237 $html .=
'<td>'.$langs->trans(
'TYPE_'.$printer_det[
'type']).
'</td>';
239 $html .=
'<td class="center">';
240 if ($conf->global->PRINTING_GCP_DEFAULT == $printer_det[
'id'])
242 $html .=
img_picto($langs->trans(
"Default"),
'on');
243 }
else $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>';
245 $html .=
'</tr>'.
"\n";
247 $this->resprint = $html;
261 $storage =
new DoliStorage($this->
db, $this->
conf);
263 $credentials =
new Credentials(
265 $this->google_secret,
266 DOL_MAIN_URL_ROOT.
'/core/modules/oauth/google_oauthcallback.php'
268 $serviceFactory = new \OAuth\ServiceFactory();
269 $apiService = $serviceFactory->createService($this->OAUTH_SERVICENAME_GOOGLE, $credentials, $storage, array());
273 $token = $storage->retrieveAccessToken($this->OAUTH_SERVICENAME_GOOGLE);
275 $this->errors[] = $e->getMessage();
281 $expire = ($token->getEndOfLife() !== -9002 && $token->getEndOfLife() !== -9001 && time() > ($token->getEndOfLife() - 30));
285 if ($token_ok && $expire) {
288 $refreshtoken = $token->getRefreshToken();
289 $token = $apiService->refreshAccessToken($token);
290 $token->setRefreshToken($refreshtoken);
291 $storage->storeAccessToken($this->OAUTH_SERVICENAME_GOOGLE, $token);
293 $this->errors[] = $e->getMessage();
298 $response = $apiService->request(self::PRINTERS_SEARCH_URL);
300 $this->errors[] = $e->getMessage();
301 print '<pre>'.print_r($e->getMessage(),
true).
'</pre>';
304 $responsedata = json_decode($response,
true);
305 $printers = $responsedata[
'printers'];
307 if (is_array($printers) && count($printers) == 0) {
309 $ret[
'available'] = array();
312 $ret[
'available'] = $printers;
327 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
332 $fileprint = $conf->{$module}->dir_output;
334 $fileprint .=
'/'.$subdir;
336 $fileprint .=
'/'.$file;
339 $sql =
"SELECT rowid, printer_id, copy FROM ".MAIN_DB_PREFIX.
"printing WHERE module='".$this->
db->escape($module).
"' AND driver='printgcp' AND userid=".$user->id;
340 $result = $this->
db->query($sql);
343 $obj = $this->
db->fetch_object($result);
346 $printer_id = $obj->printer_id;
348 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 $storage =
new DoliStorage($this->
db, $this->
conf);
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());
414 $token = $storage->retrieveAccessToken($this->OAUTH_SERVICENAME_GOOGLE);
416 $this->errors[] = $e->getMessage();
422 $refreshtoken = $token->getRefreshToken();
423 $token = $apiService->refreshAccessToken($token);
424 $token->setRefreshToken($refreshtoken);
425 $storage->storeAccessToken($this->OAUTH_SERVICENAME_GOOGLE, $token);
427 $this->errors[] = $e->getMessage();
432 $response = json_decode($apiService->request(self::PRINT_URL,
'POST', $post_fields),
true);
434 return array(
'status' => $response[
'success'],
'errorcode' => $response[
'errorCode'],
'errormessage' => $response[
'message']);
445 global $conf, $langs;
450 $storage =
new DoliStorage($this->
db, $this->
conf);
452 $credentials =
new Credentials(
454 $this->google_secret,
455 DOL_MAIN_URL_ROOT.
'/core/modules/oauth/google_oauthcallback.php'
457 $serviceFactory = new \OAuth\ServiceFactory();
458 $apiService = $serviceFactory->createService($this->OAUTH_SERVICENAME_GOOGLE, $credentials, $storage, array());
462 $token = $storage->retrieveAccessToken($this->OAUTH_SERVICENAME_GOOGLE);
464 $this->errors[] = $e->getMessage();
471 $expire = ($token->getEndOfLife() !== -9002 && $token->getEndOfLife() !== -9001 && time() > ($token->getEndOfLife() - 30));
475 if ($token_ok && $expire) {
478 $refreshtoken = $token->getRefreshToken();
479 $token = $apiService->refreshAccessToken($token);
480 $token->setRefreshToken($refreshtoken);
481 $storage->storeAccessToken($this->OAUTH_SERVICENAME_GOOGLE, $token);
483 $this->errors[] = $e->getMessage();
490 $response = $apiService->request(self::PRINTERS_GET_JOBS);
492 $this->errors[] = $e->getMessage();
495 $responsedata = json_decode($response,
true);
497 $html .=
'<div class="div-table-responsive">';
498 $html .=
'<table width="100%" class="noborder">';
499 $html .=
'<tr class="liste_titre">';
500 $html .=
'<td>'.$langs->trans(
"Id").
'</td>';
501 $html .=
'<td>'.$langs->trans(
"Date").
'</td>';
502 $html .=
'<td>'.$langs->trans(
"Owner").
'</td>';
503 $html .=
'<td>'.$langs->trans(
"Printer").
'</td>';
504 $html .=
'<td>'.$langs->trans(
"Filename").
'</td>';
505 $html .=
'<td>'.$langs->trans(
"Status").
'</td>';
506 $html .=
'<td>'.$langs->trans(
"Cancel").
'</td>';
507 $html .=
'</tr>'.
"\n";
509 $jobs = $responsedata[
'jobs'];
511 if (is_array($jobs)) {
512 foreach ($jobs as $value) {
513 $html .=
'<tr class="oddeven">';
514 $html .=
'<td>'.$value[
'id'].
'</td>';
515 $dates =
dol_print_date((
int) substr($value[
'createTime'], 0, 10),
'dayhour');
516 $html .=
'<td>'.$dates.
'</td>';
517 $html .=
'<td>'.$value[
'ownerId'].
'</td>';
518 $html .=
'<td>'.$value[
'printerName'].
'</td>';
519 $html .=
'<td>'.$value[
'title'].
'</td>';
520 $html .=
'<td>'.$value[
'status'].
'</td>';
521 $html .=
'<td> </td>';
525 $html .=
'<tr class="oddeven">';
526 $html .=
'<td colspan="7" class="opacitymedium">'.$langs->trans(
"None").
'</td>';
532 $this->resprint = $html;
sendPrintToPrinter($printerid, $printjobtitle, $filepath, $contenttype)
Sends document to the printer.
conf($dolibarr_main_document_root)
Load conf file (file must exists)
printFile($file, $module, $subdir= '')
Print selected file.
listJobs()
List jobs print.
Parent class of emailing target selectors modules.
$conf db
API class for accounts.
dol_mimetype($file, $default= 'application/octet-stream', $mode=0)
Return mime type of a file.
listAvailablePrinters()
Return list of available printers.
img_picto($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt= '', $morecss= '', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
Class to provide printing with Google Cloud Print.
print
Draft customers invoices.
dol_print_date($time, $format= '', $tzoutput= 'auto', $outputlangs= '', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
newToken()
Return the value of token currently saved into session with name 'newtoken'.
getlistAvailablePrinters()
Return list of available printers.
__construct($db)
Constructor.