Cron.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. Lib::loadClass('RouteBase');
  3. /**
  4. * usage example - cli script:
  5. * $token = Router::getRoute('Cron')->generateCliAuthToken('bach_sync_perms', 300);
  6. * file_get_contents("https://{$baseUrl}/index.php?_route=Cron&_key=bach_sync_perms&_token={$token}&_task=run");
  7. *
  8. * TEST: $ php SE/se-lib/Route/Cron-test.php biuro.biall-net.pl
  9. */
  10. class Route_Cron extends RouteBase {
  11. public function handleAuth() {
  12. if (User::logged()) {
  13. } else if ($this->authByToken()) {
  14. } else {
  15. throw new HttpException('Unauthorized', 401);
  16. }
  17. }
  18. public function generateCliAuthToken($cliKey, $task, $ttl = 300) {
  19. $generatedToken = uniqid();
  20. $parts = array();
  21. $parts[] = $generatedToken;
  22. $parts[] = $task;
  23. $parts[] = $ttl;
  24. $parts[] = time();
  25. $token = implode(",", $parts);
  26. $sqlCliKey = "CronCliAuthToken:{$cliKey}";
  27. $sth = DB::getPDO()->prepare("
  28. insert into CRM_CONFIG (CONF_KEY, CONF_VAL)
  29. values ( :cliKey, :token )
  30. on duplicate key update CONF_VAL = :token
  31. ");
  32. $sth->bindValue(':cliKey', $sqlCliKey, PDO::PARAM_STR);
  33. $sth->bindValue(':token', $token, PDO::PARAM_STR);
  34. $sth->execute();
  35. return $generatedToken;
  36. }
  37. public function authByToken() {
  38. $cliKey = V::get('_key', '', $_REQUEST);
  39. $cliToken = V::get('_token', '', $_REQUEST);
  40. $sqlCliKey = "CronCliAuthToken:{$cliKey}";
  41. $sth = DB::getPDO()->prepare("
  42. select c.CONF_VAL
  43. from CRM_CONFIG c
  44. where CONF_KEY = :cliKey
  45. order by c.ID desc
  46. limit 1
  47. ");
  48. $sth->bindValue(':cliKey', $sqlCliKey, PDO::PARAM_STR);
  49. $sth->execute();
  50. $rawToken = $sth->fetch();
  51. if (!$rawToken || !$rawToken['CONF_VAL']) throw new HttpException("Unauthorized - token not found #1-" . __LINE__, 401);
  52. $rawToken = explode(',', $rawToken['CONF_VAL']);
  53. DBG::_('DBG_CRON', '>1', 'rawToken', $rawToken, __CLASS__, __FUNCTION__, __LINE__);
  54. if (4 != count($rawToken)) throw new HttpException("Unauthorized - token not found #2-" . __LINE__, 401);
  55. if ($cliToken != $rawToken[0]) throw new HttpException("Unauthorized - token not found #3-" . __LINE__, 401);
  56. $task = $rawToken[1];
  57. $ttl = (int)$rawToken[2];
  58. $createDateTimestamp = (int)$rawToken[3];
  59. if (!$ttl) throw new HttpException("Unauthorized - token not found #4-" . __LINE__, 401);
  60. if (!$createDateTimestamp) throw new HttpException("Unauthorized - token not found #5-" . __LINE__, 401);
  61. DBG::_('DBG_CRON', '>1', 'rawToken', array('createDateTimestamp'=>$createDateTimestamp, 'ttl'=>$ttl, 'cur'=>time()), __CLASS__, __FUNCTION__, __LINE__);
  62. if ($createDateTimestamp + $ttl < time()) {
  63. // TODO: remove record from table?
  64. throw new HttpException("Unauthorized - token expired #6-" . __LINE__, 401);
  65. }
  66. session_write_close();// changes in $_SESSION visible only in current process
  67. $_SESSION['AUTHORIZE_USER'] = 'anonymous';
  68. $_SESSION['ADM_NAME'] = 'Anonymous';
  69. $_SESSION['ADM_ACCOUNT'] = $_SERVER['REMOTE_ADDR'];
  70. $_SESSION['ADM_ADMIN_LEVEL'] = 10;
  71. $_SESSION['ADM_ID'] = 9999999999;
  72. DBG::_('DBG_CRON', '>1', 'rawToken', array('createDateTimestamp'=>$createDateTimestamp, 'ttl'=>$ttl, 'cur'=>time()), __CLASS__, __FUNCTION__, __LINE__);
  73. $this->runTask($task);
  74. }
  75. public function defaultAction() {
  76. Lib::loadClass('UI');
  77. UI::gora();
  78. echo UI::h('div', [ 'class' => "container" ], [
  79. UI::h('h1', [], "Cron"),
  80. "...",
  81. ]);
  82. UI::dol();
  83. }
  84. public function dbgClearNofityAction() {
  85. Lib::loadClass('UI');
  86. UI::gora();
  87. try {
  88. DB::getPDO()->execSql("update `CRM_NOTIFY` set `last_exec_time` = null");
  89. UI::alert('info', "Notify cleared");
  90. } catch (Exception $e) {
  91. UI::alert('danger', "Error: " . $e->getMessage());
  92. }
  93. UI::dol();
  94. }
  95. public function dbgChangeNofityToDayBeforeAction() {
  96. Lib::loadClass('UI');
  97. UI::gora();
  98. try {
  99. DB::getPDO()->execSql("update `CRM_NOTIFY` set `last_exec_time` = DATE_SUB(NOW(), INTERVAL 1 DAY)");
  100. UI::alert('info', "Notify cleared");
  101. } catch (Exception $e) {
  102. UI::alert('danger', "Error: " . $e->getMessage());
  103. }
  104. UI::dol();
  105. }
  106. public function sendNotifyAction() {
  107. Lib::loadClass('UI');
  108. $notify = Router::getRoute('Notify');
  109. $todoReminders = array();
  110. if (V::get('DBG_CRON', null, $_GET) > 0) UI::gora();
  111. echo '<div class="container">' . "\n";
  112. echo '<h1>Cron</h1>' . "\n";
  113. try {
  114. 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__);
  115. {// limit send time to 8 - 20
  116. $timeNow = time();
  117. $timeSendLimitFrom = mktime(8, 0, 0, date('n'), date('j'), date('Y'));
  118. $timeSendLimitTo = mktime(20, 0, 0, date('n'), date('j'), date('Y'));
  119. $dayNrNow = date('N');
  120. $daysAllowed = array(1, 2, 3, 4, 5);// 1 = Poniedziałek, ... , 6 - Sobota, 7 - Niedziela
  121. if ($timeNow > $timeSendLimitFrom && $timeNow < $timeSendLimitTo && in_array($dayNrNow, $daysAllowed)) {
  122. $todoReminders = $notify->getTodoList(2, array('once_a_day', 'immediately'));
  123. }
  124. }
  125. DBG::_('DBG_CRON', '>0', 'todoReminders', $todoReminders, __CLASS__, __FUNCTION__, __LINE__);
  126. foreach ($todoReminders as $who => $userReminders) {
  127. echo "<p>Sending to {$who} reminders {" . json_encode($userReminders) . "}</p>" . "\n";
  128. //$notify->sendUserReminders($who, $userReminders, $forceMail = 'plabudda@biall-net.pl');// TEST
  129. $notify->sendUserReminders($who, $userReminders);
  130. foreach ($userReminders as $when => $listWhat) {
  131. if (!empty($listWhat)) {
  132. // $reminders = array_keys($listWhat);
  133. // echo "<p>Sending to {$who} reminders [" . implode(",", $reminders) . "] at '{$when}'</p>" . "\n";
  134. // $notify->send($who, $listWhat, $when, $forceMail = 'plabudda@biall-net.pl');
  135. $notify->markAsSent($who, $listWhat, $when);
  136. }
  137. }
  138. }
  139. 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__);
  140. } catch (Exception $e) {
  141. UI::alert('danger', "#" . $e->getLine() . ":" . $e->getMessage());
  142. }
  143. echo "\n" . '</div>';// .container
  144. echo "\n.EOF\n";
  145. }
  146. public function checkInstallAction() {
  147. Lib::loadClass('Router');
  148. $routeToReinstallList = array();
  149. //$routeToReinstallList[] = 'Config';// Config must work before (url token)
  150. $routeToReinstallList[] = 'Msgs';
  151. $routeToReinstallList[] = 'FixProjectPath';
  152. $routeToReinstallList[] = 'FixZasobPath';
  153. $routeToReinstallList[] = 'FixCrmProcesInitIdx';
  154. $routeToReinstallList[] = 'Notify';
  155. $routeToReinstallList[] = 'UrlAction_WmsGenerate';
  156. // $routeToReinstallList[] = 'FileStorage';// reinstall in bash_install_check.php
  157. foreach ($routeToReinstallList as $routeName) {
  158. $route = Router::getRoute($routeName);
  159. $route->reinstall();
  160. echo " - {$routeName} checked\n";
  161. }
  162. echo "\n.EOF\n";
  163. }
  164. }