Cron.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 sendNofityAction() {
  85. $notify = Router::getRoute('Notify');
  86. $todoReminders = array();
  87. echo '<div class="container">' . "\n";
  88. echo '<h1>Cron</h1>' . "\n";
  89. try {
  90. {// limit send time to 8 - 20
  91. $timeNow = time();
  92. $timeSendLimitFrom = mktime(8, 0, 0, date('n'), date('j'), date('Y'));
  93. $timeSendLimitTo = mktime(20, 0, 0, date('n'), date('j'), date('Y'));
  94. if ($timeNow > $timeSendLimitFrom && $timeNow < $timeSendLimitTo) {
  95. $todoReminders = $notify->getTodoList(2);
  96. }
  97. }
  98. DBG::_('DBG_CRON', '>0', 'todoReminders', $todoReminders, __CLASS__, __FUNCTION__, __LINE__);
  99. foreach ($todoReminders as $who => $listWhen) {
  100. foreach ($listWhen as $when => $listWhat) {
  101. if (!empty($listWhat)) {
  102. $reminders = array_keys($listWhat);
  103. echo "<p>Sending to {$who} reminders [" . implode(",", $reminders) . "]</p>" . "\n";
  104. $notify->send($who, $reminders, $forceMail = 'plabudda@biall-net.pl');
  105. $notify->markAsSent($who, $reminders);
  106. }
  107. }
  108. }
  109. } catch (Exception $e) {
  110. SE_Layout::alert('danger', "#" . $e->getLine() . ":" . $e->getMessage());
  111. }
  112. echo "\n" . '</div>';// .container
  113. echo "\n.EOF\n";
  114. }
  115. }