Linux::_distro PHP Method

_distro() protected method

Distribution
protected _distro ( ) : void
return void
    protected function _distro()
    {
        $this->sys->setDistribution("Linux");
        $list = @parse_ini_file(APP_ROOT . "/data/distros.ini", true);
        if (!$list) {
            return;
        }
        // We have the '2>/dev/null' because Ubuntu gives an error on this command which causes the distro to be unknown
        if (CommonFunctions::executeProgram('lsb_release', '-a 2>/dev/null', $distro_info, PSI_DEBUG) && strlen($distro_info) > 0) {
            $distro_tmp = preg_split("/\n/", $distro_info, -1, PREG_SPLIT_NO_EMPTY);
            foreach ($distro_tmp as $info) {
                $info_tmp = preg_split('/:/', $info, 2);
                if (isset($distro_tmp[0]) && !is_null($distro_tmp[0]) && trim($distro_tmp[0]) != "" && isset($distro_tmp[1]) && !is_null($distro_tmp[1]) && trim($distro_tmp[1]) != "") {
                    $distro[trim($info_tmp[0])] = trim($info_tmp[1]);
                }
            }
            if (!isset($distro['Distributor ID']) && !isset($distro['Description'])) {
                // Systems like StartOS
                if (isset($distro_tmp[0]) && !is_null($distro_tmp[0]) && trim($distro_tmp[0]) != "") {
                    $this->sys->setDistribution(trim($distro_tmp[0]));
                    if (preg_match('/^(\\S+)\\s*/', $distro_tmp[0], $id_buf) && isset($list[trim($id_buf[1])]['Image'])) {
                        $this->sys->setDistributionIcon($list[trim($id_buf[1])]['Image']);
                    }
                }
            } else {
                if (isset($distro['Description']) && preg_match('/^NAME=\\s*"?([^"\\n]+)"?\\s*$/', $distro['Description'], $name_tmp)) {
                    $distro['Description'] = $name_tmp[1];
                }
                if (isset($distro['Description']) && $distro['Description'] != "n/a" && (!isset($distro['Distributor ID']) || $distro['Distributor ID'] != "n/a" && $distro['Description'] != $distro['Distributor ID'])) {
                    $this->sys->setDistribution($distro['Description']);
                    if (isset($distro['Release']) && $distro['Release'] != "n/a" && $distro['Release'] != $distro['Description'] && strstr($distro['Release'], ".")) {
                        if (preg_match("/^(\\d+)\\.[0]+\$/", $distro['Release'], $match_buf)) {
                            $tofind = $match_buf[1];
                        } else {
                            $tofind = $distro['Release'];
                        }
                        if (!preg_match("/^" . $tofind . "[\\s\\.]|[\\(\\[]" . $tofind . "[\\.\\)\\]]|\\s" . $tofind . "\$|\\s" . $tofind . "[\\s\\.]/", $distro['Description'])) {
                            $this->sys->setDistribution($this->sys->getDistribution() . " " . $distro['Release']);
                        }
                    }
                } elseif (isset($distro['Distributor ID']) && $distro['Distributor ID'] != "n/a") {
                    $this->sys->setDistribution($distro['Distributor ID']);
                    if (isset($distro['Release']) && $distro['Release'] != "n/a") {
                        $this->sys->setDistribution($this->sys->getDistribution() . " " . $distro['Release']);
                    }
                    if (isset($distro['Codename']) && $distro['Codename'] != "n/a") {
                        $this->sys->setDistribution($this->sys->getDistribution() . " (" . $distro['Codename'] . ")");
                    }
                }
                if (isset($distro['Distributor ID']) && $distro['Distributor ID'] != "n/a" && isset($list[$distro['Distributor ID']]['Image'])) {
                    $this->sys->setDistributionIcon($list[$distro['Distributor ID']]['Image']);
                } elseif (isset($distro['Description']) && $distro['Description'] != "n/a") {
                    $this->sys->setDistribution($distro['Description']);
                    if (isset($list[$distro['Description']]['Image'])) {
                        $this->sys->setDistributionIcon($list[$distro['Description']]['Image']);
                    }
                }
            }
        } else {
            /* default error handler */
            if (function_exists('errorHandlerPsi')) {
                restore_error_handler();
            }
            /* fatal errors only */
            $old_err_rep = error_reporting();
            error_reporting(E_ERROR);
            // Fall back in case 'lsb_release' does not exist but exist /etc/lsb-release
            if (CommonFunctions::fileexists($filename = "/etc/lsb-release") && CommonFunctions::rfts($filename, $buf, 0, 4096, false) && preg_match('/^DISTRIB_ID="?([^"\\n]+)"?/m', $buf, $id_buf)) {
                if (preg_match('/^DISTRIB_DESCRIPTION="?([^"\\n]+)"?/m', $buf, $desc_buf) && trim($desc_buf[1]) != trim($id_buf[1])) {
                    $this->sys->setDistribution(trim($desc_buf[1]));
                    if (preg_match('/^DISTRIB_RELEASE="?([^"\\n]+)"?/m', $buf, $vers_buf) && trim($vers_buf[1]) != trim($desc_buf[1]) && strstr($vers_buf[1], ".")) {
                        if (preg_match("/^(\\d+)\\.[0]+\$/", trim($vers_buf[1]), $match_buf)) {
                            $tofind = $match_buf[1];
                        } else {
                            $tofind = trim($vers_buf[1]);
                        }
                        if (!preg_match("/^" . $tofind . "[\\s\\.]|[\\(\\[]" . $tofind . "[\\.\\)\\]]|\\s" . $tofind . "\$|\\s" . $tofind . "[\\s\\.]/", trim($desc_buf[1]))) {
                            $this->sys->setDistribution($this->sys->getDistribution() . " " . trim($vers_buf[1]));
                        }
                    }
                } else {
                    if (isset($list[trim($id_buf[1])]['Name'])) {
                        $this->sys->setDistribution(trim($list[trim($id_buf[1])]['Name']));
                    } else {
                        $this->sys->setDistribution(trim($id_buf[1]));
                    }
                    if (preg_match('/^DISTRIB_RELEASE="?([^"\\n]+)"?/m', $buf, $vers_buf)) {
                        $this->sys->setDistribution($this->sys->getDistribution() . " " . trim($vers_buf[1]));
                    }
                    if (preg_match('/^DISTRIB_CODENAME="?([^"\\n]+)"?/m', $buf, $vers_buf)) {
                        $this->sys->setDistribution($this->sys->getDistribution() . " (" . trim($vers_buf[1]) . ")");
                    }
                }
                if (isset($list[trim($id_buf[1])]['Image'])) {
                    $this->sys->setDistributionIcon($list[trim($id_buf[1])]['Image']);
                }
            } else {
                // otherwise find files specific for distribution
                foreach ($list as $section => $distribution) {
                    if (!isset($distribution['Files'])) {
                        continue;
                    } else {
                        foreach (preg_split("/;/", $distribution['Files'], -1, PREG_SPLIT_NO_EMPTY) as $filename) {
                            if (CommonFunctions::fileexists($filename)) {
                                $distro = $distribution;
                                if (isset($distribution['Mode']) && strtolower($distribution['Mode']) == "detection") {
                                    $buf = "";
                                } elseif (isset($distribution['Mode']) && strtolower($distribution['Mode']) == "execute") {
                                    if (!CommonFunctions::executeProgram($filename, '2>/dev/null', $buf, PSI_DEBUG)) {
                                        $buf = "";
                                    }
                                } else {
                                    if (!CommonFunctions::rfts($filename, $buf, 1, 4096, false)) {
                                        $buf = "";
                                    } elseif (isset($distribution['Mode']) && strtolower($distribution['Mode']) == "analyse") {
                                        if (preg_match('/^(\\S+)\\s*/', preg_replace('/^Red\\s+/', 'Red', $buf), $id_buf) && isset($list[trim($id_buf[1])]['Image'])) {
                                            $distro = $list[trim($id_buf[1])];
                                        }
                                    }
                                }
                                if (isset($distro['Image'])) {
                                    $this->sys->setDistributionIcon($distro['Image']);
                                }
                                if (isset($distribution['Name'])) {
                                    if (is_null($buf) || trim($buf) == "") {
                                        $this->sys->setDistribution($distribution['Name']);
                                    } else {
                                        $this->sys->setDistribution($distribution['Name'] . " " . trim($buf));
                                    }
                                } else {
                                    if (is_null($buf) || trim($buf) == "") {
                                        $this->sys->setDistribution($section);
                                    } else {
                                        $this->sys->setDistribution(trim($buf));
                                    }
                                }
                                if (isset($distribution['Files2'])) {
                                    foreach (preg_split("/;/", $distribution['Files2'], -1, PREG_SPLIT_NO_EMPTY) as $filename2) {
                                        if (CommonFunctions::fileexists($filename2) && CommonFunctions::rfts($filename2, $buf, 0, 4096, false)) {
                                            if (preg_match('/^majorversion="?([^"\\n]+)"?/m', $buf, $maj_buf) && preg_match('/^minorversion="?([^"\\n]+)"?/m', $buf, $min_buf)) {
                                                $distr2 = $maj_buf[1] . '.' . $min_buf[1];
                                                if (preg_match('/^buildphase="?([^"\\n]+)"?/m', $buf, $pha_buf) && $pha_buf[1] !== "0") {
                                                    $distr2 .= '.' . $pha_buf[1];
                                                }
                                                if (preg_match('/^buildnumber="?([^"\\n]+)"?/m', $buf, $num_buf)) {
                                                    $distr2 .= '-' . $num_buf[1];
                                                }
                                                if (preg_match('/^builddate="?([^"\\n]+)"?/m', $buf, $dat_buf)) {
                                                    $distr2 .= ' (' . $dat_buf[1] . ')';
                                                }
                                                $this->sys->setDistribution($this->sys->getDistribution() . " " . $distr2);
                                            } else {
                                                $distr2 = trim(substr($buf, 0, strpos($buf, "\n")));
                                                if (!is_null($distr2) && $distr2 != "") {
                                                    $this->sys->setDistribution($this->sys->getDistribution() . " " . $distr2);
                                                }
                                            }
                                            break;
                                        }
                                    }
                                }
                                break 2;
                            }
                        }
                    }
                }
            }
            // if the distribution is still unknown
            if ($this->sys->getDistribution() == "Linux") {
                if (CommonFunctions::fileexists($filename = "/etc/DISTRO_SPECS") && CommonFunctions::rfts($filename, $buf, 0, 4096, false) && preg_match('/^DISTRO_NAME=\'(.+)\'/m', $buf, $id_buf)) {
                    if (isset($list[trim($id_buf[1])]['Name'])) {
                        $dist = trim($list[trim($id_buf[1])]['Name']);
                    } else {
                        $dist = trim($id_buf[1]);
                    }
                    if (preg_match('/^DISTRO_VERSION=(.+)/m', $buf, $vers_buf)) {
                        $this->sys->setDistribution(trim($dist . " " . trim($vers_buf[1])));
                    } else {
                        $this->sys->setDistribution($dist);
                    }
                    if (isset($list[trim($id_buf[1])]['Image'])) {
                        $this->sys->setDistributionIcon($list[trim($id_buf[1])]['Image']);
                    } else {
                        if (isset($list['Puppy']['Image'])) {
                            $this->sys->setDistributionIcon($list['Puppy']['Image']);
                        }
                    }
                } elseif (CommonFunctions::fileexists($filename = "/etc/distro-release") && CommonFunctions::rfts($filename, $buf, 1, 4096, false) && !is_null($buf) && trim($buf) != "" || CommonFunctions::fileexists($filename = "/etc/system-release") && CommonFunctions::rfts($filename, $buf, 1, 4096, false) && !is_null($buf) && trim($buf) != "") {
                    $this->sys->setDistribution(trim($buf));
                    if (preg_match('/^(\\S+)\\s*/', preg_replace('/^Red\\s+/', 'Red', $buf), $id_buf) && isset($list[trim($id_buf[1])]['Image'])) {
                        $this->sys->setDistributionIcon($list[trim($id_buf[1])]['Image']);
                    }
                } elseif (CommonFunctions::fileexists($filename = "/etc/solydxk/info") && CommonFunctions::rfts($filename, $buf, 0, 4096, false) && preg_match('/^DISTRIB_ID="?([^"\\n]+)"?/m', $buf, $id_buf)) {
                    if (preg_match('/^DESCRIPTION="?([^"\\n]+)"?/m', $buf, $desc_buf) && trim($desc_buf[1]) != trim($id_buf[1])) {
                        $this->sys->setDistribution(trim($desc_buf[1]));
                    } else {
                        if (isset($list[trim($id_buf[1])]['Name'])) {
                            $this->sys->setDistribution(trim($list[trim($id_buf[1])]['Name']));
                        } else {
                            $this->sys->setDistribution(trim($id_buf[1]));
                        }
                        if (preg_match('/^RELEASE="?([^"\\n]+)"?/m', $buf, $vers_buf)) {
                            $this->sys->setDistribution($this->sys->getDistribution() . " " . trim($vers_buf[1]));
                        }
                        if (preg_match('/^CODENAME="?([^"\\n]+)"?/m', $buf, $vers_buf)) {
                            $this->sys->setDistribution($this->sys->getDistribution() . " (" . trim($vers_buf[1]) . ")");
                        }
                    }
                    if (isset($list[trim($id_buf[1])]['Image'])) {
                        $this->sys->setDistributionIcon($list[trim($id_buf[1])]['Image']);
                    } else {
                        $this->sys->setDistributionIcon($list['SolydXK']['Image']);
                    }
                } elseif (CommonFunctions::fileexists($filename = "/etc/os-release") && CommonFunctions::rfts($filename, $buf, 0, 4096, false) && (preg_match('/^TAILS_VERSION_ID="?([^"\\n]+)"?/m', $buf, $tid_buf) || preg_match('/^NAME="?([^"\\n]+)"?/m', $buf, $id_buf))) {
                    if (preg_match('/^TAILS_VERSION_ID="?([^"\\n]+)"?/m', $buf, $tid_buf)) {
                        if (preg_match('/^TAILS_PRODUCT_NAME="?([^"\\n]+)"?/m', $buf, $desc_buf)) {
                            $this->sys->setDistribution(trim($desc_buf[1]) . " " . trim($tid_buf[1]));
                        } else {
                            if (isset($list['Tails']['Name'])) {
                                $this->sys->setDistribution(trim($list['Tails']['Name']) . " " . trim($tid_buf[1]));
                            } else {
                                $this->sys->setDistribution('Tails' . " " . trim($tid_buf[1]));
                            }
                        }
                        $this->sys->setDistributionIcon($list['Tails']['Image']);
                    } else {
                        if (preg_match('/^PRETTY_NAME="?([^"\\n]+)"?/m', $buf, $desc_buf) && !preg_match('/\\$/', $desc_buf[1])) {
                            //if is not defined by variable
                            $this->sys->setDistribution(trim($desc_buf[1]));
                        } else {
                            if (isset($list[trim($id_buf[1])]['Name'])) {
                                $this->sys->setDistribution(trim($list[trim($id_buf[1])]['Name']));
                            } else {
                                $this->sys->setDistribution(trim($id_buf[1]));
                            }
                            if (preg_match('/^VERSION="?([^"\\n]+)"?/m', $buf, $vers_buf)) {
                                $this->sys->setDistribution($this->sys->getDistribution() . " " . trim($vers_buf[1]));
                            } elseif (preg_match('/^VERSION_ID="?([^"\\n]+)"?/m', $buf, $vers_buf)) {
                                $this->sys->setDistribution($this->sys->getDistribution() . " " . trim($vers_buf[1]));
                            }
                        }
                        if (isset($list[trim($id_buf[1])]['Image'])) {
                            $this->sys->setDistributionIcon($list[trim($id_buf[1])]['Image']);
                        }
                    }
                } elseif (CommonFunctions::fileexists($filename = "/etc/debian_version")) {
                    if (!CommonFunctions::rfts($filename, $buf, 1, 4096, false)) {
                        $buf = "";
                    }
                    if (isset($list['Debian']['Image'])) {
                        $this->sys->setDistributionIcon($list['Debian']['Image']);
                    }
                    if (isset($list['Debian']['Name'])) {
                        if (is_null($buf) || trim($buf) == "") {
                            $this->sys->setDistribution($list['Debian']['Name']);
                        } else {
                            $this->sys->setDistribution($list['Debian']['Name'] . " " . trim($buf));
                        }
                    } else {
                        if (is_null($buf) || trim($buf) == "") {
                            $this->sys->setDistribution('Debian');
                        } else {
                            $this->sys->setDistribution(trim($buf));
                        }
                    }
                } elseif (CommonFunctions::fileexists($filename = "/etc/config/uLinux.conf") && CommonFunctions::rfts($filename, $buf, 0, 4096, false) && preg_match("/^Rsync\\sModel\\s*=\\s*QNAP/m", $buf) && preg_match("/^Version\\s*=\\s*([\\d\\.]+)\r?\nBuild\\sNumber\\s*=\\s*(\\S+)/m", $buf, $ver_buf)) {
                    $buf = $ver_buf[1] . "-" . $ver_buf[2];
                    if (isset($list['QTS']['Image'])) {
                        $this->sys->setDistributionIcon($list['QTS']['Image']);
                    }
                    if (isset($list['QTS']['Name'])) {
                        $this->sys->setDistribution($list['QTS']['Name'] . " " . trim($buf));
                    } else {
                        $this->sys->setDistribution(trim($buf));
                    }
                }
            }
            /* restore error level */
            error_reporting($old_err_rep);
            /* restore error handler */
            if (function_exists('errorHandlerPsi')) {
                set_error_handler('errorHandlerPsi');
            }
        }
    }

Usage Example

Beispiel #1
0
 public function build()
 {
     parent::_distro();
 }