|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | +Copyright (c) 2013, Marius Cramer, pixcept KG |
| 5 | +All rights reserved. |
| 6 | +
|
| 7 | +Redistribution and use in source and binary forms, with or without modification, |
| 8 | +are permitted provided that the following conditions are met: |
| 9 | +
|
| 10 | + * Redistributions of source code must retain the above copyright notice, |
| 11 | + this list of conditions and the following disclaimer. |
| 12 | + * Redistributions in binary form must reproduce the above copyright notice, |
| 13 | + this list of conditions and the following disclaimer in the documentation |
| 14 | + and/or other materials provided with the distribution. |
| 15 | + * Neither the name of ISPConfig nor the names of its contributors |
| 16 | + may be used to endorse or promote products derived from this software without |
| 17 | + specific prior written permission. |
| 18 | +
|
| 19 | +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 20 | +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 21 | +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 22 | +IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
| 23 | +INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 24 | +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 26 | +OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 27 | +NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 28 | +EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | +*/ |
| 30 | + |
| 31 | +class cronjob_jkupdate extends cronjob { |
| 32 | + |
| 33 | + // job schedule |
| 34 | + protected $_schedule = '45 22 * * *'; |
| 35 | + protected $_run_at_new = true; |
| 36 | + |
| 37 | + /* this function is optional if it contains no custom code */ |
| 38 | + public function onPrepare() { |
| 39 | + global $app; |
| 40 | + |
| 41 | + parent::onPrepare(); |
| 42 | + } |
| 43 | + |
| 44 | + /* this function is optional if it contains no custom code */ |
| 45 | + public function onBeforeRun() { |
| 46 | + global $app; |
| 47 | + |
| 48 | + return parent::onBeforeRun(); |
| 49 | + } |
| 50 | + |
| 51 | + public function onRunJob() { |
| 52 | + global $app, $conf; |
| 53 | + |
| 54 | + $app->uses('getconf'); |
| 55 | + $jailkit_conf = $app->getconf->get_server_config($conf['server_id'], 'jailkit'); |
| 56 | + $jailkit_programs = explode(' ', $jailkit_conf['jailkit_chroot_app_programs']); |
| 57 | + |
| 58 | + $sites = $app->db->queryAllRecords('SELECT domain_id, document_root FROM web_domain WHERE jailkit_jkupdate_cron = \'y\''); |
| 59 | + |
| 60 | + foreach($sites as $site) { |
| 61 | + $users = $app->db->queryOneRecord('SELECT COUNT(*) AS user_count FROM shell_user WHERE parent_domain_id = ? AND active=\'y\' AND chroot=\'jailkit\'', $site['domain_id']); |
| 62 | + $crons = $app->db->queryOneRecord('SELECT COUNT(*) AS cron_count FROM cron WHERE parent_domain_id = ? AND active=\'y\' AND type=\'chrooted\'', $site['domain_id']); |
| 63 | + if ($users['user_count'] > 0 || $crons['cron_count'] > 0) { |
| 64 | + if (!is_dir($site['document_root'])) { |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + $app->log('Running jailkit updates for '.$site['document_root']); |
| 69 | + |
| 70 | + $this->run_jk_update($site['document_root']); |
| 71 | + $this->run_jk_cp($site['document_root'], $jailkit_programs); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + parent::onRunJob(); |
| 76 | + } |
| 77 | + |
| 78 | + private function run_jk_update($document_root) { |
| 79 | + global $app; |
| 80 | + |
| 81 | + $return_var = $this->exec_log('/usr/sbin/jk_update -j '.escapeshellarg($document_root)); |
| 82 | + |
| 83 | + if ($return_var > 0) { |
| 84 | + $app->log('jk_update failed with -j, trying again without -j', LOGLEVEL_DEBUG); |
| 85 | + $return_var = $this->exec_log('/usr/sbin/jk_update '.escapeshellarg($document_root)); |
| 86 | + |
| 87 | + if ($return_var > 0) { |
| 88 | + $app->log('jk_update failed (with and without -j parameter)', LOGLEVEL_WARN); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private function run_jk_cp($document_root, $programs) { |
| 94 | + global $app; |
| 95 | + |
| 96 | + foreach($programs as $program) { |
| 97 | + if (!file_exists($program)) { |
| 98 | + continue; |
| 99 | + } |
| 100 | + |
| 101 | + $return_var = $this->exec_log('/usr/sbin/jk_cp '.escapeshellarg($document_root).' '.escapeshellarg($program)); |
| 102 | + |
| 103 | + if ($return_var > 0) { |
| 104 | + $app->log('jk_cp failed with -j, trying again with -j', LOGLEVEL_DEBUG); |
| 105 | + $return_var = $this->exec_log('/usr/sbin/jk_cp '.escapeshellarg($document_root).' '.escapeshellarg($program)); |
| 106 | + |
| 107 | + if ($return_var > 0) { |
| 108 | + $app->log('jk_cp failed (without and with -j parameter)', LOGLEVEL_WARN); |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + private function exec_log($cmd) { |
| 115 | + global $app; |
| 116 | + |
| 117 | + $app->log("Running $cmd", LOGLEVEL_DEBUG); |
| 118 | + |
| 119 | + exec($cmd, $output, $return_var); |
| 120 | + |
| 121 | + if (count($output) > 0) { |
| 122 | + $app->log("Output:\n" . implode("\n", $output), LOGLEVEL_DEBUG); |
| 123 | + } |
| 124 | + |
| 125 | + return $return_var; |
| 126 | + } |
| 127 | + |
| 128 | + /* this function is optional if it contains no custom code */ |
| 129 | + public function onAfterRun() { |
| 130 | + global $app; |
| 131 | + |
| 132 | + parent::onAfterRun(); |
| 133 | + } |
| 134 | + |
| 135 | +} |
| 136 | + |
| 137 | +?> |
0 commit comments