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();
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) {
177 $expiredat = $langs->trans(
"Never");
178 } elseif ($endoflife == $token::EOL_UNKNOWN) {
179 $expiredat = $langs->trans(
"Unknown");
184 $this->
conf[] = array(
'varname'=>
'TOKEN_REFRESH',
'info'=>((!empty($refreshtoken)) ?
'Yes' :
'No'),
'type'=>
'info');
185 $this->
conf[] = array(
'varname'=>
'TOKEN_EXPIRED',
'info'=>($expire ?
'Yes' :
'No'),
'type'=>
'info');
186 $this->
conf[] = array(
'varname'=>
'TOKEN_EXPIRE_AT',
'info'=>($expiredat),
'type'=>
'info');
196 $this->
conf[] = array(
'varname'=>
'PRINTGCP_INFO',
'info'=>
'GoogleAuthNotConfigured',
'type'=>
'info');
200 $this->
conf[] = array(
'enabled'=>0,
'type'=>
'submit');
210 global $conf, $langs;
212 $langs->load(
'printing');
214 $html =
'<tr class="liste_titre">';
215 $html .=
'<td>'.$langs->trans(
'GCP_Name').
'</td>';
216 $html .=
'<td>'.$langs->trans(
'GCP_displayName').
'</td>';
217 $html .=
'<td>'.$langs->trans(
'GCP_Id').
'</td>';
218 $html .=
'<td>'.$langs->trans(
'GCP_OwnerName').
'</td>';
219 $html .=
'<td>'.$langs->trans(
'GCP_State').
'</td>';
220 $html .=
'<td>'.$langs->trans(
'GCP_connectionStatus').
'</td>';
221 $html .=
'<td>'.$langs->trans(
'GCP_Type').
'</td>';
222 $html .=
'<td class="center">'.$langs->trans(
"Select").
'</td>';
223 $html .=
'</tr>'.
"\n";
226 foreach ($list[
'available'] as $printer_det) {
227 $html .=
'<tr class="oddeven">';
228 $html .=
'<td>'.$printer_det[
'name'].
'</td>';
229 $html .=
'<td>'.$printer_det[
'displayName'].
'</td>';
230 $html .=
'<td>'.$printer_det[
'id'].
'</td>';
231 $html .=
'<td>'.$printer_det[
'ownerName'].
'</td>';
232 $html .=
'<td>'.$printer_det[
'status'].
'</td>';
233 $html .=
'<td>'.$langs->trans(
'STATE_'.$printer_det[
'connectionStatus']).
'</td>';
234 $html .=
'<td>'.$langs->trans(
'TYPE_'.$printer_det[
'type']).
'</td>';
236 $html .=
'<td class="center">';
237 if ($conf->global->PRINTING_GCP_DEFAULT == $printer_det[
'id']) {
238 $html .=
img_picto($langs->trans(
"Default"),
'on');
240 $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>';
243 $html .=
'</tr>'.
"\n";
245 $this->resprint = $html;
259 $storage =
new DoliStorage($this->
db, $this->
conf);
261 $credentials =
new Credentials(
263 $this->google_secret,
264 DOL_MAIN_URL_ROOT.
'/core/modules/oauth/google_oauthcallback.php'
266 $serviceFactory = new \OAuth\ServiceFactory();
267 $apiService = $serviceFactory->createService($this->OAUTH_SERVICENAME_GOOGLE, $credentials, $storage, array());
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;
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;
sendPrintToPrinter($printerid, $printjobtitle, $filepath, $contenttype)
Sends document to the printer.
conf($dolibarr_main_document_root)
Load conf file (file must exists)
$conf db
API class for accounts.
printFile($file, $module, $subdir= '')
Print selected file.
if(!function_exists('utf8_encode')) if(!function_exists('utf8_decode')) getDolGlobalString($key, $default= '')
Return dolibarr global constant string value.
listJobs()
List jobs print.
Parent class of emailing target selectors modules.
dol_mimetype($file, $default= 'application/octet-stream', $mode=0)
Return MIME type of a file from its name with extension.
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.
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.