function scan() { $templates = scandir(docroot('templates/' . $this->config->item('theme') . '/page')); $theme = $this->config->item('theme'); if (is_array($templates)) { db_delete('templates', array('theme' => $theme)); foreach ($templates as $templateRow) { if (!preg_match('#^[\\.]|^[_]#', $templateRow)) { $insert = false; $insert['theme'] = $theme; $insert['name'] = preg_replace('#\\.php$#', '', $templateRow); db_create('templates', $insert); } } } }
function create_config() { $config_file = file_get_contents('Config.inc.php'); $config_file = preg_replace("/(define\\('PROJECT_NAME',').*?('\\))/", "define('PROJECT_NAME','{$this->project_name}')", $config_file); $config_file = preg_replace("/(define\\('DB_SERVER',').*?('\\))/", "define('DB_SERVER','{$this->db_server}')", $config_file); $config_file = preg_replace("/(define\\('DB_NAME',').*?('\\))/", "define('DB_NAME','{$this->db_name}')", $config_file); $config_file = preg_replace("/(define\\('DB_USER',').*?('\\))/", "define('DB_USER','{$this->db_user}')", $config_file); $config_file = preg_replace("/(define\\('DB_PASS',').*?('\\))/", "define('DB_PASS','{$this->db_pass}')", $config_file); $config_file = preg_replace("/(define\\('FTP_SERVER',').*?('\\))/", "define('FTP_SERVER','{$this->ftp_server}')", $config_file); $config_file = preg_replace("/(define\\('FTP_USER',').*?('\\))/", "define('FTP_USER','{$this->ftp_user}')", $config_file); $config_file = preg_replace("/(define\\('FTP_PASS',').*?('\\))/", "define('FTP_PASS','{$this->ftp_pass}')", $config_file); $config_file = preg_replace("/(define\\('FTP_ROOTDIR',').*?('\\))/", "define('FTP_ROOTDIR','{$this->ftp_root}')", $config_file); define('USE_FTP', true); define('FTP_SERVER', $this->ftp_server); define('FTP_USER', $this->ftp_user); define('FTP_PASS', $this->ftp_pass); define('FTP_ROOTDIR', $this->ftp_root); include_once 'System/RheinaufFile.php'; $this->docroot = docroot(); return RheinaufFile::write_file($this->docroot . $this->install_path . '/Config.inc.php', $config_file); }
function server2ftppath($path) { if (defined('FTP_ROOTDIR')) { $ftp_root = FTP_ROOTDIR; } return $ftp_root . str_replace(docroot(), '', $path); }
function letsencrypt($db, $ssldomain, $argv) { $sql = "SELECT web_domain.document_root, web_domain.is_subdomainwww, web_domain.ssl, apache_directives FROM web_domain WHERE web_domain.domain = '{$ssldomain}'"; $result = mysqli_query($db, $sql) or die("Strange: query failed.\n"); $num = mysqli_num_rows($result); if ($num != 1) { return "Found {$num} matches in database.\n"; } $res = mysqli_fetch_array($result) or die("Strange shit happens: can't fetch array.\n"); mysqli_free_result($result); $docroot = $res[0] . "/web"; $sslroot = $res[0] . "/ssl"; $apachedir = $res[3]; $webroot = docroot($docroot, $apachedir); if (file_exists($webroot)) { echo "Found DocumentRoot for {$ssldomain}: " . $webroot . "\n"; } else { return "Fatal error: DocumentRoot {$webroot} doesn't exist\n"; } if ($res[2] == 'n' and !isset($argv[2])) { return "Error: SSL not enabled for this domain. Use -f to forcebly enable SSL in database.\n"; } else { if (isset($argv[2]) and $argv[2] !== "-f") { return "Error: SSL not enabled for this domain. Use only -f to forcebly enable SSL in database.\n"; } } if (file_exists($sslroot . "/" . $ssldomain . ".crt") and file_exists($sslroot . "/" . $ssldomain . ".key") and file_exists($sslroot . "/" . $ssldomain . ".bundle")) { if (!file_exists("/etc/letsencrypt/live/{$ssldomain}/cert.pem") or !file_exists("/etc/letsencrypt/live/{$ssldomain}/privkey.pem") or !file_exists("/etc/letsencrypt/live/{$ssldomain}/chain.pem")) { return "This site don't use LetsEncrypt certificate, can't continue.\n"; } else { if (!is_link("{$sslroot}/{$ssldomain}.crt") or !is_link("{$sslroot}/{$ssldomain}.key") or !is_link("{$sslroot}/{$ssldomain} .bundle")) { $output = shell_exec("rm -f {$sslroot}/{$ssldomain}.crt {$sslroot}/{$ssldomain}.key {$sslroot}/{$ssldomain}.bundle"); $output = shell_exec("ln -s /etc/letsencrypt/live/{$ssldomain}/cert.pem {$sslroot}/{$ssldomain}.crt"); $output = shell_exec("ln -s /etc/letsencrypt/live/{$ssldomain}/privkey.pem {$sslroot}/{$ssldomain}.key"); $output = shell_exec("ln -s /etc/letsencrypt/live/{$ssldomain}/chain.pem {$sslroot}/{$ssldomain}.bundle"); echo $output; } } } if (isset($argv[3]) and $argv[3] == '-r') { $slash = "/"; $apachedirdefault = "RewriteEngine On\nRewriteCond %" . "{" . "HTTPS} off\nRewriteRule (.*) https:{$slash}{$slash}%{" . "HTTP_HOST" . "}%" . "{" . "REQUEST_URI" . "}" . "\n"; if (empty($apachedir)) { $apachedir = $apachedirdefault; } else { if ($apachedir !== $apachedirdefault) { echo "Apache directive section contains:\n\n{$apachedir}\n\n You should manually update Apache Directives via WEB GUI:\n\n{$apachedirdefault}"; } } } $dir1 = $webroot . "/.well-known/"; $dir2 = $webroot . "/.well-known/acme-challenge/"; $account = "*****@*****.**"; if ($res[1] = 1) { $wwwdomain = " -d www.{$ssldomain}"; } else { $wwwdomain = ""; } if (!mkdir($dir1)) { echo "Creation of {$dir1} failed.\n"; } if (!mkdir($dir2)) { echo "Creation of {$dir2} failed.\n"; } if (file_exists("{$webroot}/.htaccess")) { rename("{$webroot}/.htaccess", "{$webroot}/letsencrypt.htaccess"); } $output = shell_exec("/root/letsencrypt/letsencrypt-auto certonly -m {$account} --renew-by-default -a webroot -w {$webroot} --agree-tos -d {$ssldomain}{$wwwdomain}"); if (file_exists("{$webroot}/letsencrypt.htaccess")) { rename("{$webroot}/letsencrypt.htaccess", "{$webroot}/.htaccess"); } echo "Running {$output}\n"; rmdir($dir2); rmdir($dir1); if (!file_exists("/etc/letsencrypt/live/{$ssldomain}/cert.pem") or !is_file("/etc/letsencrypt/live/{$ssldomain}/cert.pem")) { return "Something wrong: Cert file /etc/letsencrypt/live/{$ssldomain}/cert.pem not found.\n"; } if (time() - filemtime("/etc/letsencrypt/live/{$ssldomain}/cert.pem") > 100) { return "Something wrong: Cert file /etc/letsencrypt/live/{$ssldomain}/cert.pem older than 100 seconds.\n"; } $sslcert = file_get_contents("/etc/letsencrypt/live/{$ssldomain}/cert.pem"); if (file_exists("/etc/letsencrypt/live/{$ssldomain}/privkey.pem")) { $sslkey = file_get_contents("/etc/letsencrypt/live/{$ssldomain}/privkey.pem"); } else { return "Something wrong: Key file /etc/letsencrypt/live/{$ssldomain}/privkey.pem not found.\n"; } if (file_exists("/etc/letsencrypt/live/{$ssldomain}/chain.pem")) { $sslchain = file_get_contents("/etc/letsencrypt/live/{$ssldomain}/chain.pem"); } else { return "Something wrong: Chain file /etc/letsencrypt/live/{$ssldomain}/chain.pem not found.\n"; } if ($argv[1] == 'cron') { $sslaction = " "; } else { $sslaction = "ssl_action='save', "; } $sql = "UPDATE web_domain SET web_domain.ssl='y', {$sslaction} ssl_cert='{$sslcert}', ssl_bundle='{$sslchain}', ssl_key='{$sslkey}', apache_directives='{$apachedir}' WHERE web_domain.domain = '{$ssldomain}';"; if (!mysqli_query($db, $sql)) { return "Strange: UPDATE query failed.\n"; } # mysqli_free_result($result); echo "Looks like everything is ok.\n"; }