servnest/fn/ht.php

114 lines
3.2 KiB
PHP

<?php
function checkDomainFormat($domain) {
// If the domain must end without a dot
if (!filter_var($domain, FILTER_VALIDATE_DOMAIN) OR !preg_match("/^([a-z0-9_-]{1,63}\.){1,126}[a-z0-9]{1,63}$/", $domain))
userError("Wrong domain.");
}
function listFsDirs($username) {
$absoluteDirs = glob(CONF['ht']['ht_path'] . "/" . $username . "/*/", GLOB_ONLYDIR);
$relativeDirs = false;
foreach ($absoluteDirs as $i => $absoluteDir) {
if (preg_match("/^[a-z0-9-]{1,32}$/", basename($absoluteDir)))
$relativeDirs[$i] = basename($absoluteDir); // The name of the site dir is the before last key
}
return $relativeDirs;
}
function addSite($username, $siteDir, $domain, $domainType, $protocol) {
$db = new PDO('sqlite:' . DB_PATH);
$op = $db->prepare("INSERT INTO sites(username, site_dir, domain, domain_type, protocol, creation_date, le_enabled) VALUES(:username, :site_dir, :domain, :domain_type, :protocol, :creation_date, :le_enabled)");
$time = date("Y-m-d H:i:s");
if ($domainType === "dns" AND $protocol === "http")
$le_enabled = 0;
else
$le_enabled = NULL;
$op->bindParam(':username', $username);
$op->bindParam(':site_dir', $siteDir);
$op->bindParam(':domain', $domain);
$op->bindParam(':domain_type', $domainType);
$op->bindParam(':protocol', $protocol);
$op->bindParam(':creation_date', $time);
$op->bindParam(':le_enabled', $le_enabled);
$op->execute();
}
function listDbDirs($username, $domainType, $protocol) {
$db = new PDO('sqlite:' . DB_PATH);
$usernameArray[0] = $username;
$op = $db->prepare('SELECT site_dir FROM sites WHERE username = :username AND domain_type = :domain_type AND protocol = :protocol');
$op->bindParam(':username', $username);
$op->bindParam(':domain_type', $domainType);
$op->bindParam(':protocol', $protocol);
$op->execute();
$i = 0;
$data = $op->fetch();
if (isset($data['site_dir']))
$siteDir = $data['site_dir'];
else
$siteDir = NULL;
while ($siteDir != NULL) {
$siteDirs[$i] = $siteDir;
$i++;
$data = $op->fetch();
if (isset($data['site_dir']))
$siteDir = $data['site_dir'];
else
$siteDir = NULL;
}
if (isset($siteDirs))
return $siteDirs;
else
return false;
}
function dirsStatuses($username, $domainType, $protocol) {
$dirs = false;
$fsDirs = listFsDirs($username);
$dbUsedDirs = listDbDirs($username, $domainType, $protocol);
if ($fsDirs) {
foreach ($fsDirs as $fsDir) {
$dirs[$fsDir] = ($dbUsedDirs AND in_array($fsDir, $dbUsedDirs));
}
}
return $dirs;
}
function selectSites($username, $domainType, $protocol, $onlyLeAvailable) {
$db = new PDO('sqlite:' . DB_PATH);
$usernameArray[0] = $username;
$query = "SELECT site_dir,domain FROM sites WHERE username = :username AND domain_type = :domain_type AND protocol = :protocol";
if ($onlyLeAvailable === true)
$query = $query . " AND le_enabled = 0";
$op = $db->prepare($query);
$op->bindParam(':username', $username);
$op->bindParam(':domain_type', $domainType);
$op->bindParam(':protocol', $protocol);
$op->execute();
$i = 0;
$entry = $op->fetch();
while (isset($entry['site_dir'])) {
$result[$i]["siteDir"] = $entry['site_dir'];
$result[$i]["domain"] = $entry['domain'];
$i++;
$entry = $op->fetch();
}
if (isset($result))
return $result;
else
return false;
}