worker.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /* Load the framework. */
  3. include '../framework/zand/router.class.php';
  4. include '../framework/control.class.php';
  5. include '../framework/model.class.php';
  6. include '../framework/helper.class.php';
  7. $app = zandRouter::createApp('pms', dirname(dirname(__FILE__)), 'zandRouter');
  8. $common = $app->loadCommon();
  9. if($_SERVER['RR_MODE'] === 'jobs')
  10. {
  11. /* Jobs. */
  12. while($task = $app->consumer->waitTask())
  13. {
  14. if($task->getQueue() == 'crons')
  15. {
  16. $id = $task->getValue('id');
  17. $type = $task->getValue('type');
  18. $cmd = $task->getValue('command');
  19. if(!$id || !$type || !$cmd) continue;
  20. $output = '';
  21. $return = 0;
  22. if($type == 'zentao')
  23. {
  24. try
  25. {
  26. ob_start();
  27. parse_str($cmd, $params);
  28. if(!isset($params['moduleName']) || !isset($params['methodName'])) continue;
  29. $app->initRequest();
  30. $common->setUserConfig();
  31. $app->moduleName = $params['moduleName'];
  32. $app->methodName = $params['methodName'];
  33. $app->rawModule = $params['moduleName'];
  34. $app->rawMethod = $params['methodName'];
  35. $app->setControlFile();
  36. $app->loadModule();
  37. $output = ob_get_clean();
  38. $app->closeRequest();
  39. }
  40. catch(EndResponseException $e)
  41. {
  42. $output = helper::removeUTF8Bom(ob_get_clean());
  43. $output .= $e->getContent();
  44. }
  45. catch(Exception $e)
  46. {
  47. $return = 1;
  48. $output = helper::removeUTF8Bom(ob_get_clean());
  49. $output .= $e->getMessage();
  50. }
  51. }
  52. elseif($type == 'system')
  53. {
  54. exec($cmd, $output, $return);
  55. if($output) $output = implode("\n", $output);
  56. }
  57. $log = date('H:m:s') . "task " . $id . " executed,\ncommand: $cmd.\nreturn : $return.\noutput : $output\n";
  58. logCron($app->getLogRoot(), $log);
  59. }
  60. $task->complete();
  61. }
  62. }
  63. else
  64. {
  65. /* HTTP. */
  66. while(true)
  67. {
  68. try
  69. {
  70. $app->worker->waitRequest();
  71. $body = '';
  72. ob_start();
  73. /* installed or not. */
  74. if(!isset($config->installed) or !$config->installed) helper::header('location', 'install.php');
  75. /* Check for need upgrade. */
  76. $config->installedVersion = $app->getInstalledVersion();
  77. if($config->version != $config->installedVersion) helper::header('location', 'upgrade.php');
  78. $app->initRequest();
  79. $common->setUserConfig();
  80. /* Check the request is getconfig or not. */
  81. if(isset($_GET['mode']) and $_GET['mode'] == 'getconfig') helper::end(helper::removeUTF8Bom($app->exportConfig()));
  82. /* Remove install.php and upgrade.php. */
  83. if(file_exists('install.php') or file_exists('upgrade.php'))
  84. {
  85. $undeletedFiles = array();
  86. if(file_exists('install.php')) $undeletedFiles[] = '<strong style="color:#ed980f">install.php</strong>';
  87. if(file_exists('upgrade.php')) $undeletedFiles[] = '<strong style="color:#ed980f">upgrade.php</strong>';
  88. $wwwDir = dirname(__FILE__);
  89. if($undeletedFiles)
  90. {
  91. echo "<html><head><meta charset='utf-8'></head>
  92. <body><table align='center' style='width:700px; margin-top:100px; border:1px solid gray; font-size:14px;'><tr><td style='padding:8px'>";
  93. echo "<div style='margin-bottom:8px;'>安全起见,请删除 <strong style='color:#ed980f'>{$wwwDir}</strong> 目录下的 " . join(' 和 ', $undeletedFiles) . " 文件。</div>";
  94. echo "<div>Please remove " . join(' and ', $undeletedFiles) . " under <strong style='color:#ed980f'>$wwwDir</strong> dir for security reason.</div>";
  95. helper::end("</td></tr></table></body></html>");
  96. }
  97. }
  98. /* If client device is mobile and version is pro, set the default view as mthml. */
  99. if($app->clientDevice == 'mobile' and (strpos($config->version, 'pro') === 0 or strpos($config->version, 'biz') === 0 or strpos($config->version, 'max') === 0) and $config->default->view == 'html') $config->default->view = 'mhtml';
  100. if(!empty($_GET['display']) && $_GET['display'] == 'card') $config->default->view = 'xhtml';
  101. $app->parseRequest();
  102. if(!$app->setParams()) return;
  103. $common->checkPriv();
  104. if(!$common->checkIframe()) return;
  105. $app->loadModule();
  106. $body = helper::removeUTF8Bom(ob_get_clean());
  107. /* Flush the buffer. */
  108. $app->worker->respond($body);
  109. }
  110. catch(EndResponseException $e)
  111. {
  112. $body = helper::removeUTF8Bom(ob_get_clean());
  113. $body .= $e->getContent();
  114. $app->worker->respond($body);
  115. }
  116. catch(Exception $e)
  117. {
  118. $app->worker->error($e);
  119. }
  120. $app->closeRequest();
  121. }
  122. }
  123. /**
  124. * Log cron.
  125. *
  126. * @param string $log
  127. * @access public
  128. * @return void
  129. */
  130. function logCron($root, $log)
  131. {
  132. if(!is_writable($root)) return false;
  133. $file = $root . 'cron.' . date('Ymd') . '.log.php';
  134. if(!is_file($file)) $log = "<?php\n die();\n" . $log;
  135. $fp = fopen($file, "a");
  136. fwrite($fp, $log);
  137. fclose($fp);
  138. }