Cron.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. class Route_Cron extends RouteBase {
  9. public function handleAuth() {
  10. if (User::logged()) {
  11. } else if ($this->authByCliToken()) {
  12. } else {
  13. throw new HttpException('Unauthorized', 401);
  14. }
  15. }
  16. public function generateCliAuthToken($cliKey, $ttl = 300) {
  17. $generatedToken = uniqid();
  18. $parts = array();
  19. $parts[] = $generatedToken;
  20. $parts[] = $ttl;
  21. $parts[] = time();
  22. $token = implode(",", $parts);
  23. $sqlCliKey = "CronCliAuthToken:{$cliKey}";
  24. $sth = DB::getPDO()->prepare("
  25. insert into CRM_CONFIG (CONF_KEY, CONF_VAL)
  26. values ( :cliKey, :token )
  27. on duplicate key update set CONF_VAL = :token
  28. ");
  29. $sth->bindValue(':cliKey', $sqlCliKey, PDO::PARAM_STR);
  30. $sth->bindValue(':token', $token, PDO::PARAM_STR);
  31. $sth->execute();
  32. return $generatedToken;
  33. }
  34. public function authByCliAuthToken() {
  35. $cliKey = V::get('_key', '', $_REQUEST);
  36. $cliToken = V::get('_token', '', $_REQUEST);
  37. $sqlCliKey = "CronCliAuthToken:{$cliKey}";
  38. // select from CRM_CONFIG where CONF_KEY = $sqlCliKey
  39. // unpack token
  40. // check ttl
  41. session_write_close();// changes in $_SESSION visible only in current process
  42. //$_SESSION[''] = '';
  43. }
  44. public function defaultAction() {
  45. SE_Layout::gora();
  46. ?>
  47. <div class="container">
  48. <h1>Cron</h1>
  49. ...
  50. </div>
  51. <?php
  52. SE_Layout::dol();
  53. }
  54. }