diff --git a/bin/CronServiceWindow.css b/bin/CronServiceWindow.css index bc79b1c0627276bcde38d9e11982bc595ff35fd0..07e20ae99496e3f9c3c4cb35cefa298f442cad16 100644 --- a/bin/CronServiceWindow.css +++ b/bin/CronServiceWindow.css @@ -5,8 +5,8 @@ } .quiqqer-cron-cronserviceWindow h2 { - font-weight: normal; - margin-bottom: 10px; + font-weight : normal; + margin-bottom : 10px; } .quiqqer-cron-cronservicewindow-title { @@ -28,9 +28,29 @@ } .quiqqer-cron-cronservicewindow-buttons { - text-align: center; - margin-top: 40px; - width: 100%; + text-align : center; + margin-top : 40px; + width : 100%; +} + +.quiqqer-cron-cronservicewindow-stat-title { + font-weight : bold; +} + +.quiqqer-cron-cronservice-table-stats { + width : 100%; +} + +.quiqqer-cron-cronservice-table-stats td { + width : 50%; +} + +.quiqqer-cron-cronservicewindow-stat-negative { + color : red; +} + +.quiqqer-cron-cronservicewindow-stat-positive { + color : green; } /* Registration Sheet */ diff --git a/bin/CronServiceWindow.html b/bin/CronServiceWindow.html index 27f3a472e8da4d9452e9fa0513e3f5b1005cd6c3..3d276447969a0f2ce8bfa438ff145abb26ecc9db 100644 --- a/bin/CronServiceWindow.html +++ b/bin/CronServiceWindow.html @@ -15,28 +15,42 @@ {{cron_window_cronservice_content_status_text}} - <div class="quiqqer-cron-cronservicewindow-stat"> - <span class="quiqqer-cron-cronservicewindow-stat-title"> - {{cron_window_cronservice_content_register_lbl_stats_status}} - </span> - {{statusText}} - </div> - - {{#registered}} - <div class="quiqqer-cron-cronservicewindow-stat"> - <span class="quiqqer-cron-cronservicewindow-stat-title"> - {{cron_window_cronservice_content_register_lbl_stats_errors}} - </span> - {{statusErrors}} - </div> - <div class="quiqqer-cron-cronservicewindow-stat"> - <span class="quiqqer-cron-cronservicewindow-stat-title"> - {{cron_window_cronservice_content_register_lbl_stats_lastExecution}} - </span> - {{statusLastExecution}} - </div> - {{/registered}} - + <table class="quiqqer-cron-cronservice-table-stats"> + <tr> + <td> + <span class="quiqqer-cron-cronservicewindow-stat-title"> + {{cron_window_cronservice_content_register_lbl_stats_status}} + </span> + </td> + <td> + <span class="{{#active}}quiqqer-cron-cronservicewindow-stat-positive{{/active}}{{^active}}quiqqer-cron-cronservicewindow-stat-negative{{/active}}"> + {{statusText}} + </span> + </td> + </tr> + {{#registered}} + <tr> + <td> + <span class="quiqqer-cron-cronservicewindow-stat-title"> + {{cron_window_cronservice_content_register_lbl_stats_errors}} + </span> + </td> + <td> + {{statusErrors}} + </td> + </tr> + <tr> + <td> + <span class="quiqqer-cron-cronservicewindow-stat-title"> + {{cron_window_cronservice_content_register_lbl_stats_lastExecution}} + </span> + </td> + <td> + {{statusLastExecution}} + </td> + </tr> + {{/registered}} + </table> </div> <div class="quiqqer-cron-cronservicewindow-buttons"></div> \ No newline at end of file diff --git a/bin/CronServiceWindow.js b/bin/CronServiceWindow.js index c8dbd8cf7302e8167917919d52190b8ae9f5fef1..e12e93b7113cf6c48f0854ba5bfb248830504f3c 100644 --- a/bin/CronServiceWindow.js +++ b/bin/CronServiceWindow.js @@ -43,6 +43,8 @@ define('package/quiqqer/cron/bin/CronServiceWindow', [ initialize: function (options) { this.parent(options); + this.registered = false; + this.addEvents({ onOpen: this.$onOpen }); @@ -94,24 +96,37 @@ define('package/quiqqer/cron/bin/CronServiceWindow', [ status : status['status'], statusErrors : status['errors'], statusLastExecution : status['last_execution'], - registered : (status['status'] != 0) + registered : (status['status'] != 0), + active : (status['status'] == 1), + inactive : (status['status'] == 2) })); + self.registered = (status['status'] != 0); + var Buttons = Content.getElement('.quiqqer-cron-cronservicewindow-buttons'); + // get the button text : register or unregister + var btnText = QUILocale.get(lg, 'cron.window.cronservice.content.btn.register'); + if (self.registered) { + btnText = QUILocale.get(lg, 'cron.window.cronservice.content.btn.unregister'); + } + new QUIButton({ - text : QUILocale.get(lg, 'cron.window.cronservice.content.btn.register'), + text : btnText, textimage: 'fa fa-arrow-right', events : { - onClick: function(Button) { - if (reg) { + onClick: function (Button) { + if (!self.registered) { self.showRegistration(); return; } - - Button.setAttribute('text', 'Sind Sie sicher?'); - - self.showUnRegistration() + Button.setAttribute('text', QUILocale.get('quiqqer/cron', 'cron.window.cronservice.content.btn.unregister.confirm')); + if (Button.getAttribute('clickcnt') == 1) { + self.unregister().then(function () { + self.refresh(); + }); + } + Button.setAttribute('clickcnt', 1); } }, styles : { @@ -121,17 +136,6 @@ define('package/quiqqer/cron/bin/CronServiceWindow', [ } }).inject(Buttons); - // - // Content.getElements('.quiqqer-cron-cronservicewindow-btn-openRegistration') - // .addEvent('click', function () { - // self.showRegistration(); - // }); - // - // Content.getElements('.quiqqer-cron-cronservicewindow-btn-unregister') - // .addEvent('click', function () { - // self.unregister(); - // }); - // self.Loader.hide(); }, { @@ -150,8 +154,8 @@ define('package/quiqqer/cron/bin/CronServiceWindow', [ new QUISheets({ header : true, - icon: 'fa fa-cloud', - title: QUILocale.get(lg, 'cron.window.cronservice.title'), + icon : 'fa fa-cloud', + title : QUILocale.get(lg, 'cron.window.cronservice.title'), buttons: false, events : { onOpen: function (Sheet) { @@ -170,7 +174,7 @@ define('package/quiqqer/cron/bin/CronServiceWindow', [ self.Loader.show(); self.register(Email.value).then(function () { self.refresh(); - Sheet.close(); + Sheet.destroy(); }); }); }, diff --git a/database.xml b/database.xml index 1e8c91341a462c5f974751df2853ce38daa3a448..daf288184fad34e5dcd36c622ea3ebdc234fba8e 100644 --- a/database.xml +++ b/database.xml @@ -21,6 +21,12 @@ <field type="INT( 11 ) NOT NULL">uid</field> <field type="DATETIME NULL">lastexec</field> </table> + + <table name="cron_cronservice"> + <field type="INT( 3 ) NOT NULL">cronid</field> + <field type="INT( 11 ) NOT NULL">uid</field> + <field type="DATETIME NULL">lastexec</field> + </table> </global> </database> diff --git a/locale.xml b/locale.xml index 83524cf8ff24dba0a93e4136a2517a243fd8872c..db7cdf1815e1f46e86dddbfd8ca5207341d13f5e 100644 --- a/locale.xml +++ b/locale.xml @@ -58,6 +58,19 @@ <de><![CDATA[Der Administrator bekommt eine Nachricht wenn die Crons die letzten 24h nicht liefen.]]></de> <en><![CDATA[The administrator receives a message when the Cron did not run the last 24 hours]]></en> </locale> + <locale name="exception.registration.empty.domain"> + <de><![CDATA[Es muss eine Domain angegeben sein]]></de> + <en><![CDATA[Domain can not be empty]]></en> + </locale> + <locale name="exception.registration.empty.email"> + <de><![CDATA[Es muss eine Email angegeben sein.]]></de> + <en><![CDATA[A email must be given.]]></en> + </locale> + <locale name="exception.registration.empty.packageDir"> + <de><![CDATA[Konnte den "package" Pfad nicht auslesen!]]></de> + <en><![CDATA[Could not parse the "package" folder path.]]></en> + </locale> + </groups> <groups name="quiqqer/cron" datatype="js"> <locale name="cron.id"> @@ -239,6 +252,14 @@ This is works with an external server on which QUIQQER systems can register them <de><![CDATA[Letzte Ausführung: ]]></de> <en><![CDATA[Last execution: ]]></en> </locale> + <locale name="cron.window.cronservice.content.btn.unregister.confirm"> + <de><![CDATA[Sind Sie sicher?]]></de> + <en><![CDATA[Are you sure?]]></en> + </locale> + <locale name="cron.window.cronservice.content.btn.unregister"> + <de><![CDATA[Registrierung entfernen]]></de> + <en><![CDATA[Unregister]]></en> + </locale> <locale name="permission.quiqqer.cron._header"> <de><![CDATA[Aufgaben / Tasks]]></de> diff --git a/src/QUI/Cron/CronService.php b/src/QUI/Cron/CronService.php index 43cd0eb438602e5f8bbd52aa471fc90511ab5140..eed64529ca2fed255830161aafc10bd1f91b084e 100644 --- a/src/QUI/Cron/CronService.php +++ b/src/QUI/Cron/CronService.php @@ -68,9 +68,12 @@ class CronService return $status; } + /** + * Revoked the registration for this quiqqer instance + */ public function revokeRegistration() { - $token = "0VamlwcIlNUgE79ocOgpUKTvjhS8I4cr"; + $token = $this->readRevokeToken(); $this->makeServerAjaxCall('package_pcsg_cronservice_ajax_revokeRegistration', array( 'domain' => $this->domain, @@ -85,9 +88,23 @@ class CronService * @param $email - The Email that should be used for communication. * @param $packageDir - The package url dir * @param $https - wether or not http secure should be used to call the cron.php + * @throws Exception */ private function sendRegistrationRequest($domain, $email, $packageDir, $https) { + if (empty($domain)) { + throw new Exception(array("quiqqer/cron", "exception.registration.empty.domain")); + } + + if (empty($email)) { + throw new Exception(array("quiqqer/cron", "exception.registration.empty.email")); + } + + if (empty($packageDir)) { + throw new Exception(array("quiqqer/cron", "exception.registration.empty.packageDir")); + } + + $url = self::CRONSERVICE_URL . "/admin/ajax.php?" . "_rf=" . urlencode("[\"package_pcsg_cronservice_ajax_register\"]") . "&package=" . urlencode("pcsg/cronservice") . @@ -97,7 +114,6 @@ class CronService "&packageDir=" . urlencode($packageDir) . "&https=" . ($https ? "1" : "0"); - Log::addDebug($url); $curl = curl_init(); curl_setopt_array($curl, array( @@ -107,9 +123,29 @@ class CronService )); $response = curl_exec($curl); - curl_close($curl); + $response = substr($response, 9, -10); + $data = json_decode($response, true); + if (json_last_error() != JSON_ERROR_NONE) { + Log::addDebug($response); + throw new Exception(json_last_error_msg()); + } - Log::addDebug($response); + if (!isset($data['package_pcsg_cronservice_ajax_register']['result'])) { + throw new Exception("Something went wrong!"); + } + + $data = $data['package_pcsg_cronservice_ajax_register']['result']; + if (!isset($data['status']) || $data['status'] != 1) { + Log::addDebug($response); + Log::writeRecursive($data); + throw new Exception("Something went wrong!"); + } + + + $revokeCode = $data['revokeCode']; + $this->saveRevokeToken($revokeCode); + + curl_close($curl); } /** @@ -158,4 +194,43 @@ class CronService return $response[$function]['result']; } + + /** + * Saves the revoke token into a file + * @param $token + */ + private function saveRevokeToken($token) + { + $varDir = QUI::getPackage('quiqqer/cron')->getVarDir() . '/cronservice'; + $fileName = $varDir . '/.revoketoken'; + + if (!is_dir($varDir)) { + mkdir($varDir, 0700, true); + } + + file_put_contents($fileName, $token); + } + + /** + * Reads the revoke token from the filesystem + * @return string + * @throws Exception + */ + private function readRevokeToken() + { + $varDir = QUI::getPackage('quiqqer/cron')->getVarDir() . '/cronservice'; + $fileName = $varDir . '/.revoketoken'; + + if (!file_exists($fileName)) { + throw new Exception("Tokenfile not present"); + } + + $token = file_get_contents($fileName); + + if ($token === false) { + throw new Exception("Could not read tokenfile."); + } + + return $token; + } } diff --git a/src/QUI/Cron/Exception.php b/src/QUI/Cron/Exception.php new file mode 100644 index 0000000000000000000000000000000000000000..e185f57b189361eba8fe3d037ef9c16537cd40c8 --- /dev/null +++ b/src/QUI/Cron/Exception.php @@ -0,0 +1,10 @@ +<?php + +namespace QUI\Cron; + +use QUI; + +class Exception extends QUI\Exception +{ + +}