コード例 #1
0
function ReadApacheConf(&$Config, $Params)
{
    //Read in the apache config file
    require_once __DIR__ . '/cfile_get_contents.php';
    $AllowConfigOverride = $Config['AllowConfigOverride'];
    $ApacheConfPath =& $Config['ApacheConfPath'];
    if ($AllowConfigOverride && isset($Params['ApacheConf'])) {
        //Overwrite from parameters
        $ApacheConfPath = $Params['ApacheConf'];
    }
    $ApacheConfData = cfile_get_contents($ApacheConfPath);
    if (!$ApacheConfData) {
        return OL('Cannot read the apache configuration file at: ' . $ApacheConfPath . ' : ' . GetLastError());
    }
    //Find the IP of the domain’s VirtualHost. The domain cannot be an alias
    if (!preg_match('/<\\h*VirtualHost\\h+([\\d\\.:]+?):\\d+\\h*>\\s*ServerName\\h+' . preg_quote($Params['Domain']) . '\\h*$/iusmD', $ApacheConfData, $Matches)) {
        return OL('Cannot find the domain in the apache config as a primary virtual host domain. Make sure the domain is actually a virtual host primary domain, which will generally not include the www');
    }
    $HostIP = $Matches[1];
    OL('Found domain on IP: ' . $HostIP, 'Success');
    //Read in all the relevant VirtualHosts, determined by the found IP from above
    $Success = 1;
    $VirtualHostInfos = array();
    if (!preg_match_all('/<\\h*VirtualHost\\h+' . $HostIP . ':(\\d+)\\h*>.*?<\\h*\\/VirtualHost\\h*>/ius', $ApacheConfData, $VirtualHostConfs, PREG_SET_ORDER)) {
        return OL('Cannot find any matching virtual hosts');
    }
    //While this should never happen, it is possible if the apache conf has errors in it
    foreach ($VirtualHostConfs as $VHConf) {
        //Extract information from the found VirtualHosts
        $Success &= ProcessVirtualHostConf($VHConf[0], $VHConf[1], $VirtualHostInfos) ^ 1;
    }
    //Return the result
    if (!$Success) {
        //Status code if error occurred
        return 1;
    }
    return $VirtualHostInfos;
}
コード例 #2
0
function GetCertLoc($DocRoot, $LEReturn, &$CertDataPerDocRoot, $CertPathOverride, $Only1Cert)
{
    //Get the path to the “fullchain” file, which shares the path with the other certificates
    $FullChainPath = null;
    $ForDocRootStr = $Only1Cert ? '' : ' for ' . $DocRoot;
    //Add the document root to error messages
    $LEReturn = implode("\n", $LEReturn);
    if (!preg_match('/Congratulations.*?saved\\s+at\\s+(.*?)\\w+\\.pem\\s*\\.\\s+Your\\s+cert/ius', $LEReturn, $Matches)) {
        //Extract via a regex
        return OL("Cannot find path to certificate in return from letsencrypt{$ForDocRootStr} (They changed their return string):\n{$LEReturn}\n");
    }
    //If not found, throw error
    $FullChainPath = $Matches[1];
    //Transform the certificate path if parameter is given
    if (isset($CertPathOverride) && !($FullChainPath = @preg_replace($CertPathOverride[0], $CertPathOverride[1], $FullChainPath))) {
        //Transform occurs here
        return OL("Error while transforming certiciate path{$ForDocRootStr}: " . ($FullChainPath === NULL ? 'PREG Error: ' . GetLastError() : 'Result string is empty'));
    }
    //Load the certificates’ data
    $CertData = array();
    foreach (array('cert', 'privkey', 'chain') as $CertType) {
        if (!($CertData[$CertType] = cfile_get_contents("{$FullChainPath}{$CertType}.pem"))) {
            return OL("Cannot read {$CertType} certificate{$ForDocRootStr} at: {$FullChainPath}{$CertType}.pem : " . ($CertData[$CertType] === FALSE ? GetLastError() : 'File is empty'));
        }
    }
    $CertDataPerDocRoot[$DocRoot] = $CertData;
    return 0;
}