Marius Cramer
2013-10-18 05beae43cfeb15ea89b984d36105dcdbdffd0975
Resolved differences from svn merge to trunk and git master
3 files modified
358 ■■■■■ changed files
interface/lib/classes/aps_guicontroller.inc.php 11 ●●●● patch | view | raw | blame | history
interface/lib/classes/tform.inc.php 174 ●●●● patch | view | raw | blame | history
server/mods-available/rescue_core_module.inc.php 173 ●●●● patch | view | raw | blame | history
interface/lib/classes/aps_guicontroller.inc.php
@@ -728,8 +728,9 @@
            // Using parse_url() to filter malformed URLs
            $path = dirname(parse_url($_SERVER['PHP_SELF'], PHP_URL_PATH)).'/'.
                    basename($this->interface_pkg_dir).'/'.$pkg['path'].'/'.basename((string)$icon);
            // nginx: if $_SERVER['PHP_SELF'] is doubled, remove /sites/aps_packagedetails_show.php from beginning of path
            $path = preg_replace('@^/sites/aps_packagedetails_show.php(.*)@', '$1', $path);
        // nginx: if $_SERVER['PHP_SELF'] is doubled, remove /sites/aps_packagedetails_show.php from beginning of path
        $path = preg_replace('@^/sites/aps_packagedetails_show.php(.*)@', '$1', $path);
            $pkg['Icon'] = $path;
        }
        else $pkg['Icon'] = '';
@@ -743,8 +744,8 @@
                // Using parse_url() to filter malformed URLs
                $path = dirname(parse_url($_SERVER['PHP_SELF'], PHP_URL_PATH)).'/'.
                        basename($this->interface_pkg_dir).'/'.$pkg['path'].'/'.basename((string)$screen['path']);
                // nginx: if $_SERVER['PHP_SELF'] is doubled, remove /sites/aps_packagedetails_show.php from beginning of path
                $path = preg_replace('@^/sites/aps_packagedetails_show.php(.*)@', '$1', $path);
        // nginx: if $_SERVER['PHP_SELF'] is doubled, remove /sites/aps_packagedetails_show.php from beginning of path
        $path = preg_replace('@^/sites/aps_packagedetails_show.php(.*)@', '$1', $path);
                $pkg['Screenshots'][] = array('ScreenPath' => $path,
                                              'ScreenDescription' => htmlspecialchars(trim((string)$screen->description)));
@@ -842,4 +843,4 @@
        return $pkg;
    }
}
?>
?>
interface/lib/classes/tform.inc.php
@@ -92,78 +92,78 @@
                        return $result;
                }
                }
        }
        }
        function getNextTab() {
                // Which tab is shown
                if($this->errorMessage == '') {
                    // If there is no error
                    if(isset($_REQUEST["next_tab"]) && $_REQUEST["next_tab"] != '') {
                                // If the next tab is known
                                $active_tab = $_REQUEST["next_tab"];
                    } else {
                        // else use the default tab
                        $active_tab = $this->formDef['tab_default'];
                    }
                } else {
                    // Show the same tab again in case of an error
                    $active_tab = $_SESSION["s"]["form"]["tab"];
                }
        function getNextTab() {
                // Which tab is shown
                if($this->errorMessage == '') {
                    // If there is no error
                    if(isset($_REQUEST["next_tab"]) && $_REQUEST["next_tab"] != '') {
                                // If the next tab is known
                                $active_tab = $_REQUEST["next_tab"];
                    } else {
                        // else use the default tab
                        $active_tab = $this->formDef['tab_default'];
                    }
                } else {
                    // Show the same tab again in case of an error
                    $active_tab = $_SESSION["s"]["form"]["tab"];
                }
                return $active_tab;
        }
                return $active_tab;
        }
        function getCurrentTab() {
                return $_SESSION["s"]["form"]["tab"];
        }
        function getCurrentTab() {
                return $_SESSION["s"]["form"]["tab"];
        }
        function isReadonlyTab($tab, $primary_id) {
            global $app, $conf;
            // Add backticks for incomplete table names.
            if(stristr($this->formDef['db_table'],'.')) {
                $escape = '';
            } else {
                $escape = '`';
            }
            if(stristr($this->formDef['db_table'],'.')) {
                $escape = '';
            } else {
                $escape = '`';
            }
            $sql = "SELECT sys_userid FROM ".$escape.$this->formDef['db_table'].$escape." WHERE ".$this->formDef['db_table_idx']." = ".$primary_id;
            $record = $app->db->queryOneRecord($sql);
            $record = $app->db->queryOneRecord($sql);
            // return true if the readonly flag of the form is set and the current loggedin user is not the owner of the record.
            if(isset($this->formDef['tabs'][$tab]['readonly']) && $this->formDef['tabs'][$tab]['readonly'] == true && $record['sys_userid'] != $_SESSION["s"]["user"]["userid"]) {
                return true;
            } else {
                return false;
            }
        }
        }
        // translation function for forms, tries the form wordbook first and if this fails, it tries the global wordbook
        function lng($msg) {
            global $app,$conf;
            if(isset($this->wordbook[$msg])) {
                return $this->wordbook[$msg];
            } else {
                return $app->lng($msg);
            }
        }
        function checkClientLimit($limit_name,$sql_where = '') {
            global $app;
            $check_passed = true;
            $limit_name = $app->db->quote($limit_name);
            if($limit_name == '') $app->error('Limit name missing in function checkClientLimit.');
            // Get the limits of the client that is currently logged in
            $client_group_id = $_SESSION["s"]["user"]["default_group"];
            $client = $app->db->queryOneRecord("SELECT $limit_name as number, parent_client_id FROM sys_group, client WHERE sys_group.client_id = client.client_id and sys_group.groupid = $client_group_id");
            // Check if the user may add another item
            if($client["number"] >= 0) {
                $sql = "SELECT count(".$this->formDef['db_table_idx'].") as number FROM ".$this->formDef['db_table']." WHERE ".$this->getAuthSQL('u');
@@ -171,33 +171,33 @@
                $tmp = $app->db->queryOneRecord($sql);
                if($tmp["number"] >= $client["number"]) $check_passed = false;
            }
            return $check_passed;
        }
        function checkResellerLimit($limit_name,$sql_where = '') {
            global $app;
            $check_passed = true;
            $limit_name = $app->db->quote($limit_name);
            if($limit_name == '') $app->error('Limit name missing in function checkClientLimit.');
            // Get the limits of the client that is currently logged in
            $client_group_id = $_SESSION["s"]["user"]["default_group"];
            $client = $app->db->queryOneRecord("SELECT parent_client_id FROM sys_group, client WHERE sys_group.client_id = client.client_id and sys_group.groupid = $client_group_id");
            //* If the client belongs to a reseller, we will check against the reseller Limit too
            if($client['parent_client_id'] != 0) {
                //* first we need to know the groups of this reseller
                $tmp = $app->db->queryOneRecord("SELECT userid, groups FROM sys_user WHERE client_id = ".$client['parent_client_id']);
                $reseller_groups = $tmp["groups"];
                $reseller_userid = $tmp["userid"];
                // Get the limits of the reseller of the logged in client
                $client_group_id = $_SESSION["s"]["user"]["default_group"];
                $reseller = $app->db->queryOneRecord("SELECT $limit_name as number FROM client WHERE client_id = ".$client['parent_client_id']);
                // Check if the user may add another item
                if($reseller["number"] >= 0) {
                    $sql = "SELECT count(".$this->formDef['db_table_idx'].") as number FROM ".$this->formDef['db_table']." WHERE (sys_groupid IN (".$reseller_groups.") or sys_userid = ".$reseller_userid.")";
@@ -206,13 +206,13 @@
                    if($tmp["number"] >= $reseller["number"]) $check_passed = false;
                }
            }
            return $check_passed;
        }
        //* get the difference record of two arrays
        function getDiffRecord($record_old,$record_new) {
            if(is_array($record_new) && count($record_new) > 0) {
            foreach($record_new as $key => $val) {
                if(@$record_old[$key] != $val) {
@@ -231,17 +231,17 @@
                    }
                }
            return $diffrec;
        }
        /**
         * Generate HTML for DATETIME fields.
         *
         *
         * @access private
         * @param string $form_element Name of the form element.
         * @param string $default_value Selected value for fields.
         * @param bool $display_secons Include seconds selection.
         * @return string HTML
         * @return string HTML
         */
        function _getDateTimeHTML($form_element, $default_value, $display_seconds=false)
        {
@@ -249,54 +249,54 @@
            $_showdate = ($_datetime === false) ? false : true;
            $dselect = array('day','month','year','hour','minute');
            if ($display_seconds === true) {
            if ($display_seconds === true) {
                 $dselect[] = 'second';
            }
            $out = '';
            foreach ($dselect as $dt_element)
            {
                 $dt_options = array();
                 $dt_space = 1;
                 switch ($dt_element) {
                     case 'day':
                         for ($i = 1; $i <= 31; $i++) {
                            $dt_options[] = array('name' =>  sprintf('%02d', $i),
                                                  'value' => sprintf('%d', $i));
                        }
                        $selected_value = date('d', $_datetime);
                            $dt_options[] = array('name' =>  sprintf('%02d', $i),
                                                  'value' => sprintf('%d', $i));
                        }
                        $selected_value = date('d', $_datetime);
                         break;
                     case 'month':
                         for ($i = 1; $i <= 12; $i++) {
                            $dt_options[] = array('name' => strftime('%b', mktime(0, 0, 0, $i, 1, 2000)),
                                                  'value' => strftime('%m', mktime(0, 0, 0, $i, 1, 2000)));
                        }
                        $selected_value = date('n', $_datetime);
                            $dt_options[] = array('name' => strftime('%b', mktime(0, 0, 0, $i, 1, 2000)),
                                                  'value' => strftime('%m', mktime(0, 0, 0, $i, 1, 2000)));
                        }
                        $selected_value = date('n', $_datetime);
                         break;
                     case 'year':
                         $start_year = strftime("%Y");
                        $years = range((int)$start_year, (int)($start_year+3));
                        foreach ($years as $year) {
                            $dt_options[] = array('name' => $year,
                                                 'value' => $year);
                        }
                        $selected_value = date('Y', $_datetime);
                        $dt_space = 2;
                        foreach ($years as $year) {
                            $dt_options[] = array('name' => $year,
                                                 'value' => $year);
                        }
                        $selected_value = date('Y', $_datetime);
                        $dt_space = 2;
                         break;
                     case 'hour':
                         foreach(range(0, 23) as $hour) {
                             $dt_options[] = array('name' =>  sprintf('%02d', $hour),
                                                  'value' => sprintf('%d', $hour));
                                                  'value' => sprintf('%d', $hour));
                         }
                         $selected_value = date('G', $_datetime);
                         break;
                     case 'minute':
                         foreach(range(0, 59) as $minute) {
                             if (($minute % 5) == 0) {
@@ -306,23 +306,23 @@
                         }
                         $selected_value = (int)floor(date('i', $_datetime));
                         break;
                     case 'second':
                     case 'second':
                         foreach(range(0, 59) as $second) {
                             $dt_options[] = array('name' =>  sprintf('%02d', $second),
                                                    'value' => sprintf('%d', $second));
                                                    'value' => sprintf('%d', $second));
                         }
                         $selected_value = (int)floor(date('s', $_datetime));
                         break;
                 }
                $out .= "<select name=\"".$form_element."[$dt_element]\" id=\"".$form_element."_$dt_element\" class=\"selectInput\" style=\"width: auto; float: none;\">";
                if (!$_showdate) {
                    $out .= "<option value=\"-\" selected=\"selected\">--</option>" . PHP_EOL;
                } else {
                    $out .= "<option value=\"-\">--</option>" . PHP_EOL;
                }
                foreach ($dt_options as $dt_opt) {
                    if ( $_showdate && ($selected_value == $dt_opt['value']) ) {
                        $out .= "<option value=\"{$dt_opt['value']}\" selected=\"selected\">{$dt_opt['name']}</option>" . PHP_EOL;
@@ -330,10 +330,10 @@
                        $out .= "<option value=\"{$dt_opt['value']}\">{$dt_opt['name']}</option>" . PHP_EOL;
                    }
                }
                $out .= '</select>' . str_repeat('&nbsp;', $dt_space);
            }
            return $out;
        }
}
server/mods-available/rescue_core_module.inc.php
@@ -73,7 +73,7 @@
         * do nothing, if the rescue-system is not enabled
         */
        global $conf;
        if ((!isset($conf['serverconfig']['rescue']['try_rescue'])) ||
        if ((!isset($conf['serverconfig']['rescue']['try_rescue'])) ||
                ((isset($conf['serverconfig']['rescue']['try_rescue'])) && ($conf['serverconfig']['rescue']['try_rescue'] !='y'))){
            return;
        }
@@ -87,17 +87,22 @@
         * Next we get the rescue data needed for rescuing the system
         */
        $this->_rescueData = $this->_getRescueData();
        /*
         * rescue MongoDB if needed
         */
        $this->_rescueMongoDB();
        /*
         * rescue mysql if needed (maybe httpd depends on mysql, so try this first!)
         */
        $this->_rescueMySql();
        /*
         * rescue httpd if needed
         */
        $this->_rescueHttpd();
        /*
         * The last step is to save the rescue-data
         */
@@ -112,12 +117,12 @@
     */
    private function _getMonitoringData() {
        global $app;
        $dataFilename = dirname(__FILE__) . "/../temp/rescue_module_monitoringdata.ser.txt";
        /*
         * If the file containing the data is too old (older than 5 minutes) it is better to
         * delete it, because it could be, that the server was down for some times and so the data
         * delete it, because it could be, that the server was down for some times and so the data
         * are outdated
         */
        if (file_exists($dataFilename) && (filemtime($dataFilename) < (time() - 5 * 60))) {
@@ -132,7 +137,7 @@
        } else {
            $data = array();
        }
        /*
         * $temp[0] was the data of the last monitoring (means 1 minute ago), $temp[1] is the data
         * 2 minutes ago and so on. Now we have make place for the newest data...
@@ -144,32 +149,32 @@
        for ($i = $max; $i > 0; $i--){
            $data[$i] = $data[$i -1];
        }
        /*
         * we need the monitoring tools
         */
        $app->load('monitor_tools');
        $tools = new monitor_tools();
        /*
         * Call the needed Monitoring-step and get the data
         */
        $tmp[0] = $tools->monitorServices();
        /* Add the data at the FIRST position of the history */
        $data[0] = $tmp;
        /*
         * We have the newest monitoring data. Save it!
         * (and protect it, because there may be sensible data in it)
         */
        file_put_contents($dataFilename, serialize($data));
        chmod($dataFilename, 0600);
        /* Thats it */
        return $data;
    }
    /**
     * This gets the rescue-Data, needed for rescuing the system.
     * Because we can not be 100% sure, that the mysql-DB is up and running, so we use the
@@ -181,7 +186,7 @@
        /*
         * If the file containing the data is too old (older than 5 minutes) it is better to
         * delete it, because it could be, that the server was down for some times and so the data
         * delete it, because it could be, that the server was down for some times and so the data
         * are outdated
         */
        if (file_exists($dataFilename) && (filemtime($dataFilename) < (time() - 5 * 60))) {
@@ -196,7 +201,7 @@
        } else {
            $data = array();
        }
        /* Thats it */
        return $data;
    }
@@ -222,14 +227,14 @@
     */
    private function _rescueHttpd(){
        global $app, $conf;
        /*
         * do nothing, if it is not allowed to rescue httpd
         */
        if ((isset($conf['serverconfig']['rescue']['do_not_try_rescue_httpd']) && ($conf['serverconfig']['rescue']['do_not_try_rescue_httpd']) == 'y')){
            return;
        }
        /*
         * if the service is up and running, or the service is not installed there is nothing to do...
         */
@@ -238,42 +243,42 @@
            $this->_rescueData['webserver']['try_counter'] = 0;
            return;
        }
        /*
         * OK, the service is installed and down.
         * Maybe this is because of a restart of the service by the admin.
         * This means, we check the data 1 minute ago
         */
        if ((!isset($this->_monitoringData[1][0]['data']['webserver'])) ||
        if ((!isset($this->_monitoringData[1][0]['data']['webserver'])) ||
                ((isset($this->_monitoringData[1][0]['data']['webserver'])) && ($this->_monitoringData[1][0]['data']['webserver'] != 0))){
            /*
             * We do NOT have this data or we have this data, but the webserver was not down 1 minute ago.
             * This means, it could be, that the admin is restarting the server.
            /*
             * We do NOT have this data or we have this data, but the webserver was not down 1 minute ago.
             * This means, it could be, that the admin is restarting the server.
             * We wait one more minute...
             */
            return;
        }
        /*#####
         * The service is down and it was down 1 minute ago.
         * We try to rescue it
         *#####*/
        /* Get the try counter */
        $tryCount = (!isset($this->_rescueData['webserver']['try_counter']))? 1 : $this->_rescueData['webserver']['try_counter'] + 1;
        /* Set the new try counter */
        $this->_rescueData['webserver']['try_counter'] = $tryCount;
        /* if 5 times will not work, we have to give up... */
        if ($tryCount > 5){
            $app->log('httpd is down! Rescue will not help!', LOGLEVEL_ERROR);
            return;
        }
        $app->log('httpd is down! Try rescue httpd (try:' . $tryCount . ')...', LOGLEVEL_WARN);
        if($conf['serverconfig']['web']['server_type'] == 'nginx'){
            $daemon = 'nginx';
        } else {
@@ -285,23 +290,89 @@
                $daemon = 'apache2';
            }
        }
        $this->_rescueDaemon($daemon);
    }
    /**
     * restarts MongoDB, if needed
     */
    private function _rescueMongoDB(){
        global $app, $conf;
        /*
         * do nothing, if it is not allowed to rescue mysql
         */
        if ((isset($conf['serverconfig']['rescue']['do_not_try_rescue_mongodb']) && ($conf['serverconfig']['rescue']['do_not_try_rescue_mongodb']) == 'y')){
            return;
        }
        /*
         * if the service is up and running, or the service is not installed there is nothing to do...
         */
        if ($this->_monitoringData[0][0]['data']['mongodbserver'] != 0){
            /* Clear the try counter, because we do not have to try to rescue the service */
            $this->_rescueData['mongodbserver']['try_counter'] = 0;
            return;
        }
        /*
         * OK, the service is installed and down.
         * Maybe this is because of a restart of the service by the admin.
         * This means, we check the data 1 minute ago
         */
        if ((!isset($this->_monitoringData[1][0]['data']['mongodbserver'])) ||
                ((isset($this->_monitoringData[1][0]['data']['mongodbserver'])) && ($this->_monitoringData[1][0]['data']['mongodbserver'] != 0))){
            /*
             * We do NOT have this data or we have this data, but the webserver was not down 1 minute ago.
             * This means, it could be, that the admin is restarting the server.
             * We wait one more minute...
             */
            return;
        }
        /*#####
         * The service is down and it was down 1 minute ago.
         * We try to rescue it
         *#####*/
        /* Get the try counter */
        $tryCount = (!isset($this->_rescueData['mongodbserver']['try_counter']))? 1 : $this->_rescueData['mongodbserver']['try_counter'] + 1;
        /* Set the new try counter */
        $this->_rescueData['mongodbserver']['try_counter'] = $tryCount;
        /* if 5 times will not work, we have to give up... */
        if ($tryCount > 5){
            $app->log('MongoDB is down! Rescue will not help!', LOGLEVEL_ERROR);
            return;
        }
        $app->log('MongoDB is down! Try rescue MongoDB (try:' . $tryCount . ')...', LOGLEVEL_WARN);
        if(is_file($conf['init_scripts'] . '/' . 'mongodb')) {
            $daemon = 'mongodb';
        } else {
            $daemon = 'mongodb';
        }
        $this->_rescueDaemon($daemon);
    }
    /**
     * restarts mysql, if needed
     */
    private function _rescueMySql(){
        global $app, $conf;
        /*
         * do nothing, if it is not allowed to rescue mysql
         */
        if ((isset($conf['serverconfig']['rescue']['do_not_try_rescue_mysql']) && ($conf['serverconfig']['rescue']['do_not_try_rescue_mysql']) == 'y')){
            return;
        }
        /*
         * if the service is up and running, or the service is not installed there is nothing to do...
         */
@@ -310,40 +381,40 @@
            $this->_rescueData['mysqlserver']['try_counter'] = 0;
            return;
        }
        /*
         * OK, the service is installed and down.
         * Maybe this is because of a restart of the service by the admin.
         * This means, we check the data 1 minute ago
         */
        if ((!isset($this->_monitoringData[1][0]['data']['mysqlserver'])) ||
        if ((!isset($this->_monitoringData[1][0]['data']['mysqlserver'])) ||
                ((isset($this->_monitoringData[1][0]['data']['mysqlserver'])) && ($this->_monitoringData[1][0]['data']['mysqlserver'] != 0))){
            /*
             * We do NOT have this data or we have this data, but the webserver was not down 1 minute ago.
             * This means, it could be, that the admin is restarting the server.
            /*
             * We do NOT have this data or we have this data, but the webserver was not down 1 minute ago.
             * This means, it could be, that the admin is restarting the server.
             * We wait one more minute...
             */
            return;
        }
        /*#####
         * The service is down and it was down 1 minute ago.
         * We try to rescue it
         *#####*/
        /* Get the try counter */
        $tryCount = (!isset($this->_rescueData['mysqlserver']['try_counter']))? 1 : $this->_rescueData['mysqlserver']['try_counter'] + 1;
        /* Set the new try counter */
        $this->_rescueData['mysqlserver']['try_counter'] = $tryCount;
        /* if 5 times will not work, we have to give up... */
        if ($tryCount > 5){
            $app->log('MySQL is down! Rescue will not help!', LOGLEVEL_ERROR);
            return;
        }
        $app->log('MySQL is down! Try rescue mysql (try:' . $tryCount . ')...', LOGLEVEL_WARN);
        if(is_file($conf['init_scripts'] . '/' . 'mysqld')) {
@@ -351,23 +422,23 @@
        } else {
            $daemon = 'mysql';
        }
        $this->_rescueDaemon($daemon);
    }
    /**
     * Tries to stop and then restart the daemon
     *
     *
     * @param type $daemon the name of the daemon
     */
    private function _rescueDaemon($daemon){
        global $conf;
        // if you need to find all restarts search for "['init_scripts']"
        /*
         * First we stop the running service "normally"
         */
        /*
         * ATTENTION!
         * The service hangs. this means it could be, that "stop" will hang also.
@@ -375,13 +446,13 @@
         * of the service
         */
        exec($conf['init_scripts'] . '/' . $daemon . ' stop && (sleep 3; kill $!; sleep 2; kill -9 $!) &> /dev/null');
        /*
         * OK, we tryed to stop it normally, maybe this worked maybe not. So we have to look
         * if the service is already running or not. If so, we have to kill them hard
         */
        exec("kill -9 `ps -A | grep " . $daemon . "| grep -v grep | awk '{print $1}'` &> /dev/null");
        /*
         * There are no more zombies left. Lets start the service..
         */