1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 
<?php
    declare(strict_types=1);
    /**
     *  +------------------------------------------------------------+
     *  | apnscp                                                     |
     *  +------------------------------------------------------------+
     *  | Copyright (c) Apis Networks                                |
     *  +------------------------------------------------------------+
     *  | Licensed under Artistic License 2.0                        |
     *  +------------------------------------------------------------+
     *  | Author: Matt Saladna (msaladna@apisnetworks.com)           |
     *  +------------------------------------------------------------+
     */

    /**
     * Microsoft FrontPage Server Extensions 2002
     *
     * @author Matt Saladna <matt@apisnetworks.com>
     */
    class Frontpage_Module extends Module_Skeleton
    {

        public $exportedFunctions = array(
            '*' => PRIVILEGE_SITE,
        );

        public function disable($domain = null)
        {
            if (!IS_CLI) {
                return $this->query('frontpage_disable', $domain);
            }
            $domain = $this->_canonicalizeDomain($domain);

            $path = $this->web_get_docroot($domain);

            if (!$path) {
                return error("unknown domain `$domain'");
            }

            $enabled = $this->enabled($domain);
            if (!$enabled) {
                return true;
            }

            $paths = (array)$this->getConfig('frontpage', 'paths');
            $domains = (array)$this->getConfig('frontpage', 'domains');

            Util_Process_Safe::exec('/usr/local/frontpage/currentversion/bin/owsadm.exe ' .
                '-o uninstall -p 80 -m www.%s',
                $domain);
            $status = Util_Process_Safe::exec('find %s -maxdepth 1  -type d -a -regex ' .
                '\'%s\' -exec rm -rf {} \\; ' .
                '-printf "Removed %%P\n"',
                $this->domain_fs_path() . '/' . $path,
                '.*/\\(_vti_\\(cnf\\|log\\|pvt\\|bin\\|txt\\)\\)\\|.*/_private'
            );
            // remove fp web
            $key = array_search($path, $paths);
            if ($key !== false) {
                unset($paths[$key]);
            }
            $key = array_search($domain, $domains);
            if ($key !== false) {
                unset($domains[$key]);
            }
            $link = '/usr/local/frontpage/' . $domain . ':80.cnf';
            if (file_exists($link)) {
                unlink($link);
            }

            $link = '/usr/local/frontpage/www.' . $domain . ':80.cnf';
            if (file_exists($link)) {
                unlink($link);
            }

            $this->setConfig('frontpage', 'enabled', count($domains) > 0);
            $this->setConfig('frontpage', 'paths', $paths);
            $this->setConfig('frontpage', 'domains', $domains);

            return $status['success'];
        }

        private function _canonicalizeDomain($domain)
        {
            $domain = strtolower($domain);
            if (substr($domain, 0, 4) == "www.") {
                $domain = substr($domain, 4);
            }

            return $domain;
        }

        /**
         * bool get_frontpage_status()
         *
         * @return bool
         */
        public function enabled($domain = null)
        {
            if (!$domain) {
                return $this->getServiceValue('frontpage', 'enabled');
            }
            if (!$domain) {
                $domain = $this->domain;
            }

            return in_array($domain, (array)$this->getServiceValue('frontpage', 'domains'));

        }

        /**
         *  Toggle Microsoft FrontPage Server Extensions per domain
         *
         * @param string $domain
         * @return bool
         */
        public function enable($domain = null)
        {
            if (!IS_CLI) {
                return $this->query('frontpage_enable', $domain);
            }
            $domain = $this->_canonicalizeDomain($domain);

            $path = $this->web_get_docroot($domain);

            if (!$path) {
                return error("unknown domain `$domain'");
            }

            $enabled = $this->enabled($domain);
            if ($enabled) {
                return true;
            }

            $paths = (array)$this->getConfig('frontpage', 'paths');
            $domains = (array)$this->getConfig('frontpage', 'domains');
            $status = Util_Process_Safe::exec('/usr/local/sbin/enable_fp.sh %s %s',
                $domain,
                $path
            );
            $link = '/usr/local/frontpage/' . $domain . ':80.cnf';
            symlink('/usr/local/frontpage/www.' . $domain . ':80.cnf', $link);
            $domains[] = $domain;
            $paths[] = $path;

            $this->setConfig('frontpage', 'enabled', count($domains) > 0);
            $this->setConfig('frontpage', 'paths', $paths);
            $this->setConfig('frontpage', 'domains', $domains);

            return $status['success'];
        }

        public function get_active_domains()
        {
            return (array)$this->getConfig('frontpage', 'domains');
        }

        public function _reset(Util_Account_Editor &$editor = null)
        {
            if (version_compare(platform_version(), '7', '>=')) {
                return [];
            }
            $module = 'frontpage';
            $params = array(
                'paths'   => array(),
                'enabled' => 0,
                'domains' => array()
            );
            if ($editor) {
                foreach ($params as $k => $v) {
                    $editor->setConfig($module, $k, $v);
                }
            }

            return array($module => $params);
        }
    }

?>