|
|
@@ -0,0 +1,65 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+Lib::loadClass('RouteBase');
|
|
|
+
|
|
|
+/**
|
|
|
+ * usage example - cli script:
|
|
|
+ * $token = Router::getRoute('Cron')->generateCliAuthToken('bach_sync_perms', 300);
|
|
|
+ * file_get_contents("https://{$baseUrl}/index.php?_route=Cron&_key=bach_sync_perms&_token={$token}&_task=run");
|
|
|
+ */
|
|
|
+class Route_Cron extends RouteBase {
|
|
|
+
|
|
|
+ public function handleAuth() {
|
|
|
+ if (User::logged()) {
|
|
|
+
|
|
|
+ } else if ($this->authByCliToken()) {
|
|
|
+
|
|
|
+ } else {
|
|
|
+ throw new HttpException('Unauthorized', 401);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public function generateCliAuthToken($cliKey, $ttl = 300) {
|
|
|
+ $generatedToken = uniqid();
|
|
|
+ $parts = array();
|
|
|
+ $parts[] = $generatedToken;
|
|
|
+ $parts[] = $ttl;
|
|
|
+ $parts[] = time();
|
|
|
+ $token = implode(",", $parts);
|
|
|
+ $sqlCliKey = "CronCliAuthToken:{$cliKey}";
|
|
|
+ $sth = DB::getPDO()->prepare("
|
|
|
+ insert into CRM_CONFIG (CONF_KEY, CONF_VAL)
|
|
|
+ values ( :cliKey, :token )
|
|
|
+ on duplicate key update set CONF_VAL = :token
|
|
|
+ ");
|
|
|
+ $sth->bindValue(':cliKey', $sqlCliKey, PDO::PARAM_STR);
|
|
|
+ $sth->bindValue(':token', $token, PDO::PARAM_STR);
|
|
|
+ $sth->execute();
|
|
|
+ return $generatedToken;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function authByCliAuthToken() {
|
|
|
+ $cliKey = V::get('_key', '', $_REQUEST);
|
|
|
+ $cliToken = V::get('_token', '', $_REQUEST);
|
|
|
+
|
|
|
+ $sqlCliKey = "CronCliAuthToken:{$cliKey}";
|
|
|
+ // select from CRM_CONFIG where CONF_KEY = $sqlCliKey
|
|
|
+ // unpack token
|
|
|
+ // check ttl
|
|
|
+
|
|
|
+ session_write_close();// changes in $_SESSION visible only in current process
|
|
|
+ //$_SESSION[''] = '';
|
|
|
+ }
|
|
|
+
|
|
|
+ public function defaultAction() {
|
|
|
+ SE_Layout::gora();
|
|
|
+ ?>
|
|
|
+<div class="container">
|
|
|
+ <h1>Cron</h1>
|
|
|
+ ...
|
|
|
+</div>
|
|
|
+ <?php
|
|
|
+ SE_Layout::dol();
|
|
|
+ }
|
|
|
+
|
|
|
+}
|