api.crontabeditor.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * System crontab editor class
  4. */
  5. class CrontabEditor {
  6. /**
  7. * Contains billing.ini config file as key=>value
  8. *
  9. * @var array
  10. */
  11. protected $billingCfg = array();
  12. /**
  13. * Contains current crontab state
  14. *
  15. * @var string
  16. */
  17. protected $currentCrontab = '';
  18. /**
  19. * Contains temporary file path used for crontab IO
  20. */
  21. const TMP_FILE_PATH = 'exports/crontab_tmp';
  22. /**
  23. * Contains basic module routing URL
  24. */
  25. const URL_ME = '?module=crontabeditor';
  26. /**
  27. * Contains default back URL
  28. */
  29. const URL_BACK = '?module=sysconf';
  30. /**
  31. * Creates new crontab editor instance
  32. */
  33. public function __construct() {
  34. $this->loadConfigs();
  35. $this->loadCrontab();
  36. }
  37. /**
  38. * Loads required configs into protected properties
  39. *
  40. * @global object $ubillingConfig
  41. *
  42. * @return void
  43. */
  44. protected function loadConfigs() {
  45. global $ubillingConfig;
  46. $this->billingCfg = $ubillingConfig->getBilling();
  47. }
  48. /**
  49. * Loads current crontab state into protected property
  50. *
  51. * @return void
  52. */
  53. protected function loadCrontab() {
  54. $command = $this->billingCfg['SUDO'] . ' crontab -l ';
  55. $this->currentCrontab = shell_exec($command);
  56. file_put_contents(self::TMP_FILE_PATH, $this->currentCrontab);
  57. }
  58. /**
  59. * Returns current host system name
  60. *
  61. * @return string
  62. */
  63. public function getSystemName() {
  64. $result = '';
  65. $command = 'uname';
  66. $hostSystem = shell_exec($command);
  67. $result = trim($hostSystem);
  68. if ($result == 'Linux') {
  69. $linuxDistro = shell_exec('hostnamectl | grep System');
  70. if (ispos($linuxDistro, 'Debian GNU/Linux 11 (bullseye)')) {
  71. $result = 'Debian11';
  72. }
  73. if (ispos($linuxDistro, 'Debian GNU/Linux 12 (bookworm)')) {
  74. $result = 'Debian12';
  75. }
  76. }
  77. return($result);
  78. }
  79. /**
  80. * Returns current crontab state
  81. *
  82. * @return string
  83. */
  84. public function getCurrentCrontab() {
  85. return($this->currentCrontab);
  86. }
  87. /**
  88. * Renders crontab editing interface form
  89. *
  90. * @return string
  91. */
  92. public function renderEditForm() {
  93. $result = '';
  94. $result .= web_FileEditorForm(self::TMP_FILE_PATH, htmlentities($this->currentCrontab, ENT_COMPAT, "UTF-8")); // OMG OMG OMG!!!!!
  95. return($result);
  96. }
  97. /**
  98. * Saves received editor form content into temporary file
  99. *
  100. * @return void
  101. */
  102. public function saveTempCrontab() {
  103. if (ubRouting::checkPost(array('editfilepath'))) {
  104. if (ubRouting::post('editfilepath') == self::TMP_FILE_PATH) {
  105. $newCrontab = ubRouting::post('editfilecontent');
  106. if (ispos($newCrontab, "\r\n")) {
  107. //cleanup to unix EOL
  108. $newCrontab = str_replace("\r\n", "\n", $newCrontab);
  109. }
  110. //appending some log data
  111. $newCrontab .= "\n" . '# updated with crontabeditor by ' . whoami() . ' on ' . curdatetime() . "\n";
  112. file_put_contents(self::TMP_FILE_PATH, $newCrontab);
  113. }
  114. }
  115. }
  116. /**
  117. * Installs new crontab jobs into system crontab
  118. *
  119. * @return void/string on error
  120. */
  121. public function installNewCrontab() {
  122. $result = '';
  123. if (file_exists(self::TMP_FILE_PATH)) {
  124. $tempFileContants = file_get_contents(self::TMP_FILE_PATH);
  125. //is something changed?
  126. if ($tempFileContants != $this->currentCrontab) {
  127. $command = $this->billingCfg['SUDO'] . ' crontab ' . self::TMP_FILE_PATH;
  128. $installResult = shell_exec($command);
  129. log_register('CRONTABEDITOR NEW CRONTAB INSTALLED');
  130. } else {
  131. $result .= __('Nothing changed');
  132. }
  133. } else {
  134. $result .= __('File') . ' ' . self::TMP_FILE_PATH . ' ' . __('Not exists');
  135. }
  136. return($result);
  137. }
  138. }