| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- <?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");
- *
- * 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();
- ?>
- <div class="container">
- <h1>Cron</h1>
- ...
- </div>
- <?php
- SE_Layout::dol();
- }
- public function dbgClearNofityAction() {
- SE_Layout::gora();
- try {
- DB::getPDO()->exec("update `CRM_NOTIFY` set `last_exec_time` = null");
- SE_Layout::alert('info', "Notify cleared");
- } catch (Exception $e) {
- SE_Layout::alert('danger', "Error: " . $e->getMessage());
- }
- SE_Layout::dol();
- }
- public function dbgChangeNofityToDayBeforeAction() {
- SE_Layout::gora();
- try {
- DB::getPDO()->exec("update `CRM_NOTIFY` set `last_exec_time` = DATE_SUB(NOW(), INTERVAL 1 DAY)");
- SE_Layout::alert('info', "Notify cleared");
- } catch (Exception $e) {
- SE_Layout::alert('danger', "Error: " . $e->getMessage());
- }
- SE_Layout::dol();
- }
- public function sendNofityAction() {
- $notify = Router::getRoute('Notify');
- $todoReminders = array();
- if (V::get('DBG_CRON', null, $_GET) > 0) SE_Layout::gora();
- echo '<div class="container">' . "\n";
- echo '<h1>Cron</h1>' . "\n";
- try {
- if (V::get('DBG_CRON', null, $_GET) > 0) DBG::table("reminders state - before", DB::getPDO()->fetchAll("select * from CRM_NOTIFY order by last_exec_time limit 20"), __CLASS__, __FUNCTION__, __LINE__);
- {// 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'));
- $dayNrNow = date('N');
- $daysAllowed = array(1, 2, 3, 4, 5);// 1 = Poniedziałek, ... , 6 - Sobota, 7 - Niedziela
- if ($timeNow > $timeSendLimitFrom && $timeNow < $timeSendLimitTo && in_array($dayNrNow, $daysAllowed)) {
- $todoReminders = $notify->getTodoList(2, array('once_a_day', 'immediately'));
- }
- }
- DBG::_('DBG_CRON', '>0', 'todoReminders', $todoReminders, __CLASS__, __FUNCTION__, __LINE__);
- foreach ($todoReminders as $who => $userReminders) {
- echo "<p>Sending to {$who} reminders {" . json_encode($userReminders) . "}</p>" . "\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 "<p>Sending to {$who} reminders [" . implode(",", $reminders) . "] at '{$when}'</p>" . "\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" . '</div>';// .container
- echo "\n.EOF\n";
- }
- public function checkInstallAction() {
- Lib::loadClass('Router');
- $routeToReinstallList = array();
- $routeToReinstallList[] = 'Config';
- $routeToReinstallList[] = 'Msgs';
- $routeToReinstallList[] = 'FixProjectPath';
- $routeToReinstallList[] = 'FixCrmProcesInitIdx';
- $routeToReinstallList[] = 'Notify';
- $routeToReinstallList[] = 'UrlAction_WmsGenerate';
- foreach ($routeToReinstallList as $routeName) {
- $route = Router::getRoute($routeName);
- $route->reinstall();
- echo " - {$routeName} checked\n";
- }
- echo "\n.EOF\n";
- }
- }
|