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->authByToken()) { } else { throw new HttpException('Unauthorized', 401); } } public function generateCliAuthToken($cliKey, $task, $ttl = 300) { $generatedToken = uniqid(); $parts = array(); $parts[] = $generatedToken; $parts[] = $task; $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 CONF_VAL = :token "); $sth->bindValue(':cliKey', $sqlCliKey, PDO::PARAM_STR); $sth->bindValue(':token', $token, PDO::PARAM_STR); $sth->execute(); return $generatedToken; } public function authByToken() { $cliKey = V::get('_key', '', $_REQUEST); $cliToken = V::get('_token', '', $_REQUEST); $sqlCliKey = "CronCliAuthToken:{$cliKey}"; $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['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() { SE_Layout::gora(); ?>
Sending to {$who} reminders {" . json_encode($userReminders) . "}
" . "\n"; //$notify->sendUserReminders($who, $userReminders, $forceMail = 'plabudda@biall-net.pl');// TEST $notify->sendUserReminders($who, $userReminders); foreach ($userReminders as $when => $listWhat) { if (!empty($listWhat)) { // $reminders = array_keys($listWhat); // echo "Sending to {$who} reminders [" . implode(",", $reminders) . "] at '{$when}'
" . "\n"; // $notify->send($who, $listWhat, $when, $forceMail = 'plabudda@biall-net.pl'); $notify->markAsSent($who, $listWhat, $when); } } } if (V::get('DBG_CRON', null, $_GET) > 0) DBG::table("reminders state - after", DB::getPDO()->fetchAll("select * from CRM_NOTIFY order by last_exec_time limit 20"), __CLASS__, __FUNCTION__, __LINE__); } catch (Exception $e) { SE_Layout::alert('danger', "#" . $e->getLine() . ":" . $e->getMessage()); } echo "\n" . '