Cron.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. DBG::_('DBG_CRON', '>1', 'rawToken', array('createDateTimestamp'=>$createDateTimestamp, 'ttl'=>$ttl, 'cur'=>time()), __CLASS__, __FUNCTION__, __LINE__);
  72. $this->runTask($task);
  73. }
  74. public function defaultAction() {
  75. SE_Layout::gora();
  76. ?>
  77. <div class="container">
  78. <h1>Cron</h1>
  79. ...
  80. </div>
  81. <?php
  82. SE_Layout::dol();
  83. }
  84. public function dbgClearNofityAction() {
  85. SE_Layout::gora();
  86. try {
  87. DB::getPDO()->exec("update `CRM_NOTIFY` set `last_exec_time` = null");
  88. SE_Layout::alert('info', "Notify cleared");
  89. } catch (Exception $e) {
  90. SE_Layout::alert('danger', "Error: " . $e->getMessage());
  91. }
  92. SE_Layout::dol();
  93. }
  94. public function dbgChangeNofityToDayBeforeAction() {
  95. SE_Layout::gora();
  96. try {
  97. DB::getPDO()->exec("update `CRM_NOTIFY` set `last_exec_time` = DATE_SUB(NOW(), INTERVAL 1 DAY)");
  98. SE_Layout::alert('info', "Notify cleared");
  99. } catch (Exception $e) {
  100. SE_Layout::alert('danger', "Error: " . $e->getMessage());
  101. }
  102. SE_Layout::dol();
  103. }
  104. public function sendNofityAction() {
  105. $notify = Router::getRoute('Notify');
  106. $todoReminders = array();
  107. if (V::get('DBG_CRON', null, $_GET) > 0) SE_Layout::gora();
  108. echo '<div class="container">' . "\n";
  109. echo '<h1>Cron</h1>' . "\n";
  110. try {
  111. 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__);
  112. {// limit send time to 8 - 20
  113. $timeNow = time();
  114. $timeSendLimitFrom = mktime(8, 0, 0, date('n'), date('j'), date('Y'));
  115. $timeSendLimitTo = mktime(20, 0, 0, date('n'), date('j'), date('Y'));
  116. $dayNrNow = date('N');
  117. $daysAllowed = array(1, 2, 3, 4, 5);// 1 = Poniedziałek, ... , 6 - Sobota, 7 - Niedziela
  118. if ($timeNow > $timeSendLimitFrom && $timeNow < $timeSendLimitTo && in_array($dayNrNow, $daysAllowed)) {
  119. $todoReminders = $notify->getTodoList(2, array('once_a_day', 'immediately'));
  120. }
  121. }
  122. DBG::_('DBG_CRON', '>0', 'todoReminders', $todoReminders, __CLASS__, __FUNCTION__, __LINE__);
  123. foreach ($todoReminders as $who => $userReminders) {
  124. echo "<p>Sending to {$who} reminders {" . json_encode($userReminders) . "}</p>" . "\n";
  125. //$notify->sendUserReminders($who, $userReminders, $forceMail = 'plabudda@biall-net.pl');// TEST
  126. $notify->sendUserReminders($who, $userReminders);
  127. foreach ($userReminders as $when => $listWhat) {
  128. if (!empty($listWhat)) {
  129. // $reminders = array_keys($listWhat);
  130. // echo "<p>Sending to {$who} reminders [" . implode(",", $reminders) . "] at '{$when}'</p>" . "\n";
  131. // $notify->send($who, $listWhat, $when, $forceMail = 'plabudda@biall-net.pl');
  132. $notify->markAsSent($who, $listWhat, $when);
  133. }
  134. }
  135. }
  136. 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__);
  137. } catch (Exception $e) {
  138. SE_Layout::alert('danger', "#" . $e->getLine() . ":" . $e->getMessage());
  139. }
  140. echo "\n" . '</div>';// .container
  141. echo "\n.EOF\n";
  142. }
  143. public function checkInstallAction() {
  144. Lib::loadClass('Router');
  145. $routeToReinstallList = array();
  146. //$routeToReinstallList[] = 'Config';// Config must work before (url token)
  147. $routeToReinstallList[] = 'Msgs';
  148. $routeToReinstallList[] = 'FixProjectPath';
  149. $routeToReinstallList[] = 'FixZasobPath';
  150. $routeToReinstallList[] = 'FixCrmProcesInitIdx';
  151. $routeToReinstallList[] = 'Notify';
  152. $routeToReinstallList[] = 'UrlAction_WmsGenerate';
  153. // $routeToReinstallList[] = 'FileStorage';// reinstall in bash_install_check.php
  154. foreach ($routeToReinstallList as $routeName) {
  155. $route = Router::getRoute($routeName);
  156. $route->reinstall();
  157. echo " - {$routeName} checked\n";
  158. }
  159. echo "\n.EOF\n";
  160. }
  161. }