|
|
@@ -6,23 +6,26 @@ 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");
|
|
|
+ *
|
|
|
+ * TEST: $ php SE/se-lib/Route/Cron-test.php biuro.biall-net.pl
|
|
|
*/
|
|
|
class Route_Cron extends RouteBase {
|
|
|
|
|
|
public function handleAuth() {
|
|
|
if (User::logged()) {
|
|
|
|
|
|
- } else if ($this->authByCliToken()) {
|
|
|
+ } else if ($this->authByToken()) {
|
|
|
|
|
|
} else {
|
|
|
throw new HttpException('Unauthorized', 401);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public function generateCliAuthToken($cliKey, $ttl = 300) {
|
|
|
+ public function generateCliAuthToken($cliKey, $task, $ttl = 300) {
|
|
|
$generatedToken = uniqid();
|
|
|
$parts = array();
|
|
|
$parts[] = $generatedToken;
|
|
|
+ $parts[] = $task;
|
|
|
$parts[] = $ttl;
|
|
|
$parts[] = time();
|
|
|
$token = implode(",", $parts);
|
|
|
@@ -30,7 +33,7 @@ class Route_Cron extends RouteBase {
|
|
|
$sth = DB::getPDO()->prepare("
|
|
|
insert into CRM_CONFIG (CONF_KEY, CONF_VAL)
|
|
|
values ( :cliKey, :token )
|
|
|
- on duplicate key update set CONF_VAL = :token
|
|
|
+ on duplicate key update CONF_VAL = :token
|
|
|
");
|
|
|
$sth->bindValue(':cliKey', $sqlCliKey, PDO::PARAM_STR);
|
|
|
$sth->bindValue(':token', $token, PDO::PARAM_STR);
|
|
|
@@ -38,17 +41,46 @@ class Route_Cron extends RouteBase {
|
|
|
return $generatedToken;
|
|
|
}
|
|
|
|
|
|
- public function authByCliAuthToken() {
|
|
|
+ public function authByToken() {
|
|
|
$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
|
|
|
+ $sth = DB::getPDO()->prepare("
|
|
|
+ select c.CONF_VAL
|
|
|
+ from CRM_CONFIG c
|
|
|
+ where CONF_KEY = :cliKey
|
|
|
+ order by c.ID desc
|
|
|
+ limit 1
|
|
|
+ ");
|
|
|
+ $sth->bindValue(':cliKey', $sqlCliKey, PDO::PARAM_STR);
|
|
|
+ $sth->execute();
|
|
|
+ $rawToken = $sth->fetch();
|
|
|
+ if (!$rawToken || !$rawToken['CONF_VAL']) throw new HttpException("Unauthorized - token not found #1-" . __LINE__, 401);
|
|
|
+ $rawToken = explode(',', $rawToken['CONF_VAL']);
|
|
|
+ DBG::_('DBG_CRON', '>1', 'rawToken', $rawToken, __CLASS__, __FUNCTION__, __LINE__);
|
|
|
+ if (4 != count($rawToken)) throw new HttpException("Unauthorized - token not found #2-" . __LINE__, 401);
|
|
|
+ if ($cliToken != $rawToken[0]) throw new HttpException("Unauthorized - token not found #3-" . __LINE__, 401);
|
|
|
+
|
|
|
+ $task = $rawToken[1];
|
|
|
+ $ttl = (int)$rawToken[2];
|
|
|
+ $createDateTimestamp = (int)$rawToken[3];
|
|
|
+
|
|
|
+ if (!$ttl) throw new HttpException("Unauthorized - token not found #4-" . __LINE__, 401);
|
|
|
+ if (!$createDateTimestamp) throw new HttpException("Unauthorized - token not found #5-" . __LINE__, 401);
|
|
|
+ DBG::_('DBG_CRON', '>1', 'rawToken', array('createDateTimestamp'=>$createDateTimestamp, 'ttl'=>$ttl, 'cur'=>time()), __CLASS__, __FUNCTION__, __LINE__);
|
|
|
+ if ($createDateTimestamp + $ttl < time()) {
|
|
|
+ // TODO: remove record from table?
|
|
|
+ throw new HttpException("Unauthorized - token expired #6-" . __LINE__, 401);
|
|
|
+ }
|
|
|
|
|
|
session_write_close();// changes in $_SESSION visible only in current process
|
|
|
- //$_SESSION[''] = '';
|
|
|
+ $_SESSION['AUTHORIZE_USER'] = 'anonymous';
|
|
|
+ $_SESSION['ADM_NAME'] = 'Anonymous';
|
|
|
+ $_SESSION['ADM_ACCOUNT'] = $_SERVER['REMOTE_ADDR'];
|
|
|
+ $_SESSION['ADM_ADMIN_LEVEL'] = 10;
|
|
|
+ DBG::_('DBG_CRON', '>1', 'rawToken', array('createDateTimestamp'=>$createDateTimestamp, 'ttl'=>$ttl, 'cur'=>time()), __CLASS__, __FUNCTION__, __LINE__);
|
|
|
+ $this->runTask($task);
|
|
|
}
|
|
|
|
|
|
public function defaultAction() {
|
|
|
@@ -62,4 +94,36 @@ class Route_Cron extends RouteBase {
|
|
|
SE_Layout::dol();
|
|
|
}
|
|
|
|
|
|
+ public function testAction() {
|
|
|
+ $notify = Router::getRoute('Notify');
|
|
|
+ $todoReminders = array();
|
|
|
+
|
|
|
+ {// limit send time to 8 - 20
|
|
|
+ $timeNow = time();
|
|
|
+ $timeSendLimitFrom = mktime(8, 0, 0, date('n'), date('j'), date('Y'));
|
|
|
+ $timeSendLimitTo = mktime(20, 0, 0, date('n'), date('j'), date('Y'));
|
|
|
+ if ($timeNow > $timeSendLimitFrom && $timeNow < $timeSendLimitTo) {
|
|
|
+ $todoReminders = $notify->getTodoList(100);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ ?>
|
|
|
+<div class="container">
|
|
|
+ <h1>Cron</h1>
|
|
|
+ <?php DBG::_(true, true, 'todoReminders', $todoReminders, __CLASS__, __FUNCTION__, __LINE__); ?>
|
|
|
+</div>
|
|
|
+ <?php
|
|
|
+
|
|
|
+ foreach ($todoReminders as $who => $listWhen) {
|
|
|
+ foreach ($listWhen as $when => $listWhat) {
|
|
|
+ if (!empty($listWhat)) {
|
|
|
+ $notify->send($who, array_keys($listWhat));// , $forceMail = 'plabudda@biall-net.pl'
|
|
|
+ $notify->markAsSent($usrLogin, array_keys($listWhat));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ echo "\n.EOF\n";
|
|
|
+ }
|
|
|
+
|
|
|
}
|