servnest/fn/ht.php

89 lines
2.7 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);
$dirs = array();
foreach ($absoluteDirs as $absoluteDir)
if (preg_match("/^[a-z0-9-]{1,32}$/", basename($absoluteDir)))
array_push($dirs, basename($absoluteDir));
return $dirs;
}
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);
$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();
return array_column($op->fetchAll(PDO::FETCH_ASSOC), 'site_dir');
}
function dirsStatuses($username, $domainType, $protocol) {
$dirs = array();
$fsDirs = listFsDirs($username);
$dbUsedDirs = listDbDirs($username, $domainType, $protocol);
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);
$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;
}