Remove Google Cloud Print

This commit is contained in:
Andreas Palm 2024-04-17 16:42:57 +02:00
parent 57e79d442c
commit 44b8dc0fe0
6 changed files with 0 additions and 448 deletions

View File

@ -6,9 +6,6 @@ namespace Xentral\Modules\GoogleApi;
final class GoogleScope
{
/** @var string CLOUDPRINT */
public const CLOUDPRINT = 'https://www.googleapis.com/auth/cloudprint';
/** @var string CALENDAR */
public const CALENDAR = 'https://www.googleapis.com/auth/calendar';

View File

@ -213,25 +213,6 @@ final class GoogleAccountGateway
return array_values($pairs);
}
/**
* Will be removed after Dec 31. 2020
*
* @deprecated
*
* @codeCoverageIgnore
*
* @return GoogleAccountData
*/
public function getCloudPrintAccount(): GoogleAccountData
{
$accounts = $this->getAccountsByScope(GoogleScope::CLOUDPRINT);
if (count($accounts) < 1) {
throw new GoogleAccountNotFoundException('No cloud printing account available.');
}
return $accounts[0];
}
/**
* @param int $accountId
*

View File

@ -1,109 +0,0 @@
/**
* Für die Bedienung der Modul-Oberfläche
*/
var PrinterGoogleCloudPrint = (function ($) {
'use strict';
var me = {
isInitialized: false,
$apiSelect: null,
$printerSelect: null,
initApiValue: null,
initPrinterValue: null,
printercache: {},
/**
* @return void
*/
init: function () {
if (me.isInitialized === true) {
return;
}
me.$apiSelect = $('select[name="google_api"]');
me.$printerSelect = $('select[name="google_printer"]');
me.initApiValue = me.$apiSelect.val();
me.initPrinterValue = me.$printerSelect.val();
var options = {};
$('select[name="google_printer"] option').each( function () {
options[$(this).attr('value')] = $(this).text();
});
me.printercache[me.initApiValue] = options;
me.registerEvents();
me.isInitialized = true;
},
setPrinterSelectOptions: function(options, selected) {
me.$printerSelect.empty(); // remove old options
$.each(options, function(value, display) {
me.$printerSelect.append($('<option></option>').attr('value', value).text(display));
});
if (selected in options) {
me.$printerSelect.val(selected);
}
},
/**
* @return {void}
*/
registerEvents: function () {
me.$apiSelect.change(function () {
me.ajaxLoadPrinterOptions(this.value);
});
},
/**
* @param {number} fieldId
*
* @return {void}
*/
ajaxLoadPrinterOptions: function (apiName) {
if (apiName === '') {
return;
}
if (apiName in me.printercache) {
me.setPrinterSelectOptions(me.printercache[apiName], me.initPrinterValue);
return;
}
$.ajax({
url: 'index.php?module=googleapi&action=ajaxprinters',
data: {
api_name: apiName
},
method: 'post',
dataType: 'json',
beforeSend: function () {
me.$printerSelect.prop('disabled', true);
me.$apiSelect.prop('disabled', true);
App.loading.open();
},
complete: function() {
me.$printerSelect.prop('disabled', false);
me.$apiSelect.prop('disabled', false);
},
error: function (error) {
App.loading.close();
},
success: function (data) {
me.printercache[apiName] = data;
me.setPrinterSelectOptions(data, me.initPrinterValue);
App.loading.close();
}
});
},
};
return {
init: me.init,
};
})(jQuery);
$(document).ready(function () {
PrinterGoogleCloudPrint.init();
});

View File

@ -1,156 +0,0 @@
<?php
use Xentral\Components\HttpClient\Exception\TransferErrorExceptionInterface;
use Xentral\Components\HttpClient\HttpClient;
use Xentral\Components\HttpClient\RequestOptions;
use Xentral\Modules\GoogleApi\Exception\GoogleAccountNotFoundException;
use Xentral\Modules\GoogleApi\Service\GoogleAccountGateway;
use Xentral\Modules\GoogleApi\Service\GoogleAuthorizationService;
class PrinterGoogleCloudPrint extends PrinterBase
{
/** @var string */
private $url = 'https://www.google.com/cloudprint/';
/** @var string URL_SEARCH */
private const URL_SEARCH = 'https://www.google.com/cloudprint/search';
/** @var string URL_PRINT */
private const URL_PRINT = 'https://www.google.com/cloudprint/submit';
/** @var string */
private const URL_JOBS = 'https://www.google.com/cloudprint/jobs';
/** @var string */
private const URL_PRINTER = 'https://www.google.com/cloudprint/printer';
/** @var HttpClient $client */
private $client;
/**
* PrinterGoogleCloudPrint constructor.
*
* @param Application $app
* @param int $id
*/
public function __construct($app, $id)
{
parent::__construct($app, $id);
$token = $this->getAuthToken();
$options = new RequestOptions();
$options->setHeader('Authorization', sprintf('Bearer %s', $token));
$this->client = new HttpClient($options);
$this->app->ModuleScriptCache->IncludeJavascriptFiles(
'drucker',
['./classes/Modules/GoogleCloudPrint/www/js/PrinterGoogleCloudPrint.js']
);
}
/**
* @return string
*/
public static function getName() {
return 'Google Cloud Print';
}
/**
* @return array
*/
public function getPrinters()
{
try {
$response = $this->client->request('GET', self::URL_SEARCH);
$result = json_decode($response->getBody()->getContents(), true);
return $result['printers'];
} catch (Exception $e) {
return [];
}
}
/**
* @return array
*/
public function SettingsStructure()
{
$googlePrinters = [];
try {
$googlePrinterArr = $this->getPrinters();
} catch (Exception $e) {
return [];
}
foreach($googlePrinterArr as $item) {
$googlePrinters[$item['id']] = sprintf('%s:%s', $item['displayName'], $item['connectionStatus']);
}
return [
'google_printer' => ['bezeichnung' => 'Drucker:','typ' => 'select', 'optionen' => $googlePrinters],
];
}
/**
* @param string $document
* @param int $count
*
* @return bool
*/
public function printDocument($document, $count = 1)
{
if(empty($this->settings['google_printer'])) {
return false;
}
if($count < 1) {
$count = 1;
}
$title = '';
$contenttype = 'application/pdf';
if(is_file($document)) {
$title = basename($document);
$document = file_get_contents($document);
}
$title .= date('YmdHis');
$titleFirst = $title;
for($i = 1; $i <= $count; $i++) {
if($i > 1) {
$title = $titleFirst.$i;
}
$postFields = array(
'printerid' => $this->settings['google_printer'],
'title' => $title,
'contentTransferEncoding' => 'base64',
'content' => base64_encode($document),
'contentType' => $contenttype
);
try {
$response = $this->client->request('POST', self::URL_PRINT, [], json_encode($postFields));
$data = json_decode($response->getBody()->getContents(), true);
return (isset($data['success']) && $data['success'] === true);
} catch (TransferErrorExceptionInterface $e) {
return false;
}
}
return true;
}
protected function getAuthToken()
{
try {
/** @var GoogleAccountGateway $gateway */
$gateway = $this->app->Container->get('GoogleAccountGateway');
$account = $gateway->getCloudPrintAccount();
$token = $gateway->getAccessToken($account->getId());
} catch (GoogleAccountNotFoundException $e) {
throw new GoogleAccountNotFoundException($e->getMessage(), $e->getCode(), $e);
} catch (Exception $e) {
$token = null;
}
if ($token === null || $token->getTimeToLive() < 10) {
/** @var GoogleAuthorizationService $auth */
$auth = $this->app->Container->get('GoogleAuthorizationService');
$token = $auth->refreshAccessToken($account);
}
return $token->getToken();
}
}

View File

@ -1,57 +0,0 @@
<!-- gehort zu tabview -->
<div id="tabs">
<ul>
<li><a href="#tabs-1"></a></li>
</ul>
<!-- ende gehort zu tabview -->
<!-- erstes tab -->
<div id="tabs-1">
[MESSAGE]
<div class="col-xs-12 col-md-2 col-md-height">
<div class="inside inside-full-height">
<form action="?module=googleapi&action=print" method="POST">
<fieldset>
<legend>{|Google Cloud Print verwenden|}</legend>
<div></div>
<table width="100%" border="0" class="mkTableFormular">
<tbody>
<tr>
<td></td>
<td>
</td>
</tr>
<tr>
<td>Google Cloud Print:</td>
<td>
<i>
Um Google Cloud Print zu verwenden klicke auf Verbinden und wähle im nächsten Schritt, das Google
Konto aus, das für Cloud Printing verwendet werden soll.
</i>
</td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" name="authorize_cloudprint" value="{|Verbinden|}"/>
<input type="submit" name="unauthorize" value="{|Trennen|}"/>
</td>
</tr>
</tbody>
</table>
</fieldset>
</form>
</div>
</div>
[TAB1NEXT]
</div>
<!-- tab view schließen -->
</div>

View File

@ -22,9 +22,6 @@ class GoogleApi
/** @var string MODULE_NAME */
public const MODULE_NAME = 'GoogleApi';
/** @var string GOOGLE_API_TESTURL_PRINT */
//private const TESTURL_PRINT = 'https://www.google.com/cloudprint/search';
/** @var string GOOGLE_API_TESTURL_CALENDAR */
//private const TESTURL_CALENDAR = 'https://www.googleapis.com/calendar/v3/users/me/calendarList';
@ -64,9 +61,7 @@ class GoogleApi
// ab hier alle Action Handler definieren die das Modul hat
$this->app->ActionHandler('list', 'GoogleApiEdit');
$this->app->ActionHandler('edit', 'GoogleApiEdit');
$this->app->ActionHandler('print', 'GoogleApiPrint');
$this->app->ActionHandler('redirect', 'GoogleApiRedirect');
$this->app->ActionHandler('ajaxprinters', 'GoogleApiAjaxPrinters');
$this->app->ActionHandlerListen($app);
}
@ -135,7 +130,6 @@ class GoogleApi
$sql = "SELECT SQL_CALC_FOUND_ROWS g.id, g.id_name, g.description,
(CASE g.type
WHEN 'print' THEN 'Google Cloud Print'
WHEN 'mail' THEN 'Google Mail'
WHEN 'calendar' THEN 'Google Calendar'
ELSE ''
@ -319,41 +313,6 @@ class GoogleApi
$this->app->Tpl->Parse('PAGE', 'googleapi_edit.tpl');
}
/**
* @return void
*/
public function GoogleApiPrint(): void
{
/** @var Request $request */
$request = $this->app->Container->get('Request');
if ($request->post->has('authorize_cloudprint')) {
/** @var Session $session */
$session = $this->app->Container->get('Session');
$redirect = $this->auth->requestScopeAuthorization(
$session,
[GoogleScope::CLOUDPRINT],
'index.php?module=googleapi&action=print'
);
SessionHandler::commitSession($session);
$redirect->send();
$this->app->ExitXentral();
}
if ($request->post->has('unauthorize')) {
$this->unauthorize();
}
$this->app->Tpl->Add(
'MESSAGE',
'<div class="warning">
Hinweis: Google wird den Cloud Print Dienst am 31.12.2020 abschalten.</div>'
);
$this->app->erp->Headlines('Google Cloud Print');
$this->createMenu();
$this->app->Tpl->Parse('PAGE', 'googleapi_print.tpl');
}
/**
* After user confirmed access to Google API manually, Google API
* redirects user to this action sending authentication code and API scope
@ -382,47 +341,6 @@ class GoogleApi
}
/**
* Action for the printer setup gui to get printer options from api connection
*
* @return void
*/
public function GoogleApiAjaxPrinters()
{
$data = [];
$apiName = $this->app->DB->real_escape_string(trim($this->app->Secure->GetPOST('api_name')));
if(!empty($apiName)) {
$id = $this->getGoogleApiByName($apiName);
$token = $this->getAccessToken($id);
$authHeader = sprintf('Authorization: Bearer %s', $token);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, [$authHeader]);
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION,true);
curl_setopt( $ch, CURLOPT_HEADER,false);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt( $ch, CURLOPT_HTTPAUTH,CURLAUTH_ANY);
curl_setopt( $ch, CURLOPT_URL, 'https://www.google.com/cloudprint/search');
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, ['printerid' => $id]);
$response = curl_exec($ch);
$result = json_decode($response, true);
$printers = $result['printers'];
if(!empty($printers)) {
foreach ($printers as $item) {
$data[$item['id']] = sprintf('%s:%s', $item['displayName'], $item['connectionStatus']);
}
}
}
$responseData = json_encode($data);
header('Content-Type: application/json');
echo $responseData;
$this->app->ExitXentral();
}
/**
* Automatically calls getAccessTokenByRederect if needed.
*
@ -682,11 +600,6 @@ class GoogleApi
}
switch ($type) {
case 'print':
$scope = self::GOOGLE_API_SCOPE_PRINT;
break;
case 'mail':
$scope = self::GOOGLE_API_SCOPE_MAIL;
@ -855,10 +768,6 @@ class GoogleApi
$type = $this->app->DB->Select(sprintf("SELECT g.type FROM googleapi AS g WHERE g.id = '%s';", $id));
switch ($type) {
case 'print':
$testurl = self::GOOGLE_API_TESTURL_PRINT;
break;
case 'mail':
$this->app->Tpl->Add(
$messageTarget,
@ -893,18 +802,6 @@ class GoogleApi
return false;
}
if ($type === 'print' &&
(!isset($info['content_type']) || explode(';', $info['content_type'])[0] !== 'text/plain')
) {
$error = 'Test fehlgeschlagen: Fehlerhafte Antwort vom Server';
$this->app->Tpl->Add(
$messageTarget,
sprintf('<div class="error">%s</div>', $error)
);
return false;
}
if ($type === 'mail' &&
(!isset($info['content_type']) || explode(';', $info['content_type'])[0] !== 'text/plain')
) {
@ -977,6 +874,5 @@ class GoogleApi
$this->app->erp->MenuEintrag('index.php?module=googleapi&action=list',
'Zur&uuml;ck zur &Uuml;bersicht');
$this->app->erp->MenuEintrag('index.php?module=googleapi&action=list', 'Übersicht');
$this->app->erp->MenuEintrag('index.php?module=googleapi&action=print', 'Google Cloud Print');
}
}