generateCliAuthToken($keyToken, $cronTaskName, 300 * 10); * $output = Router::getRoute('Cron')->executeCurlTastByToken($cronTaskName, $keyToken, $token); */ /* @usage - inside cli script with args: * $cronTaskName = 'forceTablePerms'; * $keyToken = 'bash_force_table_perms'; * $token = Router::getRoute('Cron')->generateCliAuthToken($keyToken, $cronTaskName, 300 * 10); * $output = Router::getRoute('Cron')->executeCurlTastByToken($cronTaskName, $keyToken, $token, [ 'idGroup' => 3, 'table' => 'CRM_PROCES' ]); */ // 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 - one time action? 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; $_SESSION['ADM_ID'] = 9999999999; DBG::_('DBG_CRON', '>1', 'rawToken', array('createDateTimestamp'=>$createDateTimestamp, 'ttl'=>$ttl, 'cur'=>time()), __CLASS__, __FUNCTION__, __LINE__); $this->runTask($task); exit; } public function executeCurlTastByToken($cronTaskName, $keyToken, $token, $args = []) { // @require $_SERVER['SERVER_NAME'] $baseUrl = "https://{$_SERVER['SERVER_NAME']}/SE"; if ('biuro.biall-net.pl' == $_SERVER['SERVER_NAME']) $baseUrl = "https://{$_SERVER['SERVER_NAME']}/SE/version-git"; // if ('biuro.biall-net.pl' == $_SERVER['SERVER_NAME']) $baseUrl = "https://{$_SERVER['SERVER_NAME']}/dev-pl/se-master"; // DBG //file_get_contents("https://{$baseUrl}/index.php?_route=Cron&_key={$keyToken}&_token={$token}&_task=sendNotify"); $url = "{$baseUrl}/index.php?_route=Cron&_key={$keyToken}&_token={$token}&_task={$cronTaskName}"; if (!empty($args)) $url .= "&" . implode("&", array_map(function ($key, $val) { return "{$key}={$val}"; }, array_keys($args), array_values($args))); $ch = curl_init(); curl_setopt($ch, CURLOPT_VERBOSE, true); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); $return = curl_exec($ch); curl_close($ch); return $return; } public function defaultAction() { Lib::loadClass('UI'); UI::gora(); echo UI::h('div', [ 'class' => "container" ], [ UI::h('h1', [], "Cron"), "...", ]); UI::dol(); } public function dbgClearNofityAction() { Lib::loadClass('UI'); UI::gora(); try { DB::getPDO()->execSql("update `CRM_NOTIFY` set `last_exec_time` = null"); UI::alert('info', "Notify cleared"); } catch (Exception $e) { UI::alert('danger', "Error: " . $e->getMessage()); } UI::dol(); } public function dbgChangeNofityToDayBeforeAction() { Lib::loadClass('UI'); UI::gora(); try { DB::getPDO()->execSql("update `CRM_NOTIFY` set `last_exec_time` = DATE_SUB(NOW(), INTERVAL 1 DAY)"); UI::alert('info', "Notify cleared"); } catch (Exception $e) { UI::alert('danger', "Error: " . $e->getMessage()); } UI::dol(); } public function sendNotifyAction() { Lib::loadClass('UI'); $notify = Router::getRoute('Notify'); $todoReminders = array(); if (V::get('DBG_CRON', null, $_GET) > 0) UI::gora(); echo '
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) { UI::alert('danger', "#" . $e->getLine() . ":" . $e->getMessage()); } echo "\n" . '