require_once INCLUDES_DIR.DIR."class".DIR."Config.php"; # File: SQL # See Also: # require INCLUDES_DIR.DIR."class".DIR."SQL.php"; # File: Model # See Also: # require_once INCLUDES_DIR.DIR."class".DIR."Model.php"; # File: User # See Also: # require_once INCLUDES_DIR.DIR."model".DIR."User.php"; # File: Translation # See Also: # require_once INCLUDES_DIR.DIR."class".DIR."Translation.php"; # Register our autoloader. spl_autoload_register("autoload"); # Boolean: $installed # Has Chyrp Lite been installed? $installed = false; # Prepare the Config interface. $config = Config::current(); # Get the timezone. $timezone = get_timezone(); # Get the locale. $locale = get_locale(); # List of discovered drivers. $drivers = array(); # Currently selected adapter. $adapter = isset($_POST['adapter']) ? $_POST['adapter'] : "mysql" ; # Where are we? $url = preg_replace("/\/install\.php.*$/i", "", guess_url()); # Set the timezone. set_timezone($timezone); # Set the locale. set_locale($locale); # Try to load an appropriate translation. load_translator("chyrp", INCLUDES_DIR.DIR."locale"); # Already installed? if (file_exists(INCLUDES_DIR.DIR."config.json.php")) redirect($config->url); if (class_exists("PDO")) { $pdo_available_drivers = PDO::getAvailableDrivers(); if (in_array("sqlite", $pdo_available_drivers)) $drivers[] = "sqlite"; if (in_array("mysql", $pdo_available_drivers)) $drivers[] = "mysql"; if (in_array("pgsql", $pdo_available_drivers)) $drivers[] = "pgsql"; } # Test for basic database access requirements. if (empty($drivers)) alert( __("PDO is required for database access.")); # Test if we can write to MAIN_DIR (needed for the .htaccess file). if (!is_writable(MAIN_DIR)) alert( __("Please CHMOD or CHOWN the installation directory to make it writable.") ); # Test if we can write to INCLUDES_DIR (needed for config.json.php). if (!is_writable(INCLUDES_DIR)) alert( __("Please CHMOD or CHOWN the includes directory to make it writable.") ); # Test if we can write to CACHES_DIR (needed by some extensions). if (!is_writable(CACHES_DIR)) alert( __("Please CHMOD or CHOWN the caches directory to make it writable.") ); # Test if we can write to twig cache. if (!is_writable(CACHES_DIR.DIR."twig")) alert( __("Please CHMOD or CHOWN the twig directory to make it writable.") ); # Test if we can write to thumbs cache. if (!is_writable(CACHES_DIR.DIR."thumbs")) alert( __("Please CHMOD or CHOWN the thumbs directory to make it writable.") ); /** * Function: alert * Logs an alert message and returns the log to date. */ function alert($message = null): ?array { static $log = array(); if (isset($message)) $log[] = (string) $message; return empty($log) ? null : $log ; } /** * Function: guess_url * Returns a best guess of the current URL. */ function guess_url(): string { $scheme = (!empty($_SERVER['HTTPS']) and $_SERVER['HTTPS'] !== "off") ? "https" : "http" ; $host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'] ; return $scheme."://".$host.$_SERVER['REQUEST_URI']; } /** * Function: posted * Echoes a $_POST value if set, otherwise echoes the fallback value. * * Parameters: * $key - The key to test in the $_POST array. * $fallback - The value to echo if the $_POST value is not set. */ function posted($key, $fallback = ""): void { echo fix( isset($_POST[$key]) ? $_POST[$key] : $fallback, true ); } /** * Function: selected * Echoes " selected" HTML attribute if the supplied values are equal. * * Parameters: * $val1 - Compare this value... * $val2 - ... with this value. */ function selected($val1, $val2): void { if ($val1 == $val2) echo " selected"; } #--------------------------------------------- # Output Starts #--------------------------------------------- ?> <?php echo __("Chyrp Lite Installer"); ?>
 "",
                    "port"     => "",
                    "username" => "",
                    "password" => "",
                    "database" => $_POST['database'],
                    "prefix"   => "",
                    "adapter"  => $_POST['adapter']
                )
                :
                array(
                    "host"     => $_POST['host'],
                    "port"     => $_POST['port'],
                    "username" => $_POST['username'],
                    "password" => $_POST['password'],
                    "database" => $_POST['database'],
                    "prefix"   => $_POST['prefix'],
                    "adapter"  => $_POST['adapter']
                )
                ;

            # Configure the SQL interface.
            $sql = SQL::current($settings);

            # Test the database connection.
            if (!$sql->connect(true))
                alert(_f("Database error: %s", fix($sql->error, false, true)));
        }

        if (!alert()) {
            # Reconnect to the database.
            $sql->connect();

            # Posts table.
            $sql->create(
                table:"posts",
                cols:array(
                    "id INTEGER PRIMARY KEY AUTO_INCREMENT",
                    "feather VARCHAR(32) DEFAULT ''",
                    "clean VARCHAR(128) DEFAULT ''",
                    "url VARCHAR(128) DEFAULT ''",
                    "pinned BOOLEAN DEFAULT FALSE",
                    "status VARCHAR(32) DEFAULT 'public'",
                    "user_id INTEGER DEFAULT 0",
                    "created_at DATETIME DEFAULT NULL",
                    "updated_at DATETIME DEFAULT NULL"
                )
            );

            # Post attributes table.
            $sql->create(
                table:"post_attributes",
                cols:array(
                    "post_id INTEGER NOT NULL",
                    "name VARCHAR(100) DEFAULT ''",
                    "value LONGTEXT",
                    "PRIMARY KEY (post_id, name)"
                )
            );

            # Pages table.
            $sql->create(
                table:"pages",
                cols:array(
                    "id INTEGER PRIMARY KEY AUTO_INCREMENT",
                    "title VARCHAR(250) DEFAULT ''",
                    "body LONGTEXT",
                    "public BOOLEAN DEFAULT '1'",
                    "show_in_list BOOLEAN DEFAULT '1'",
                    "list_order INTEGER DEFAULT 0",
                    "clean VARCHAR(128) DEFAULT ''",
                    "url VARCHAR(128) DEFAULT ''",
                    "user_id INTEGER DEFAULT 0",
                    "parent_id INTEGER DEFAULT 0",
                    "created_at DATETIME DEFAULT NULL",
                    "updated_at DATETIME DEFAULT NULL"
                )
            );

            # Users table.
            $sql->create(
                table:"users",
                cols:array(
                    "id INTEGER PRIMARY KEY AUTO_INCREMENT",
                    "login VARCHAR(64) DEFAULT ''",
                    "password VARCHAR(128) DEFAULT ''",
                    "full_name VARCHAR(250) DEFAULT ''",
                    "email VARCHAR(128) DEFAULT ''",
                    "website VARCHAR(128) DEFAULT ''",
                    "group_id INTEGER DEFAULT 0",
                    "approved BOOLEAN DEFAULT '1'",
                    "joined_at DATETIME DEFAULT NULL",
                    "UNIQUE (login)"
                )
            );

            # Groups table.
            $sql->create(
                table:"groups",
                cols:array(
                    "id INTEGER PRIMARY KEY AUTO_INCREMENT",
                    "name VARCHAR(100) DEFAULT ''",
                    "UNIQUE (name)"
                )
            );

            # Permissions table.
            $sql->create(
                table:"permissions",
                cols:array(
                    "id VARCHAR(100) DEFAULT ''",
                    "name VARCHAR(100) DEFAULT ''",
                    "group_id INTEGER DEFAULT 0",
                    "PRIMARY KEY (id, group_id)"
                )
            );

            # Sessions table.
            $sql->create(
                table:"sessions",
                cols:array(
                    "id VARCHAR(40) DEFAULT ''",
                    "data LONGTEXT",
                    "user_id INTEGER DEFAULT 0",
                    "created_at DATETIME DEFAULT NULL",
                    "updated_at DATETIME DEFAULT NULL",
                    "PRIMARY KEY (id)"
                )
            );

            # Define and insert the default permissions.
            $names = array(
                "change_settings" => "Change Settings",
                "toggle_extensions" => "Toggle Extensions",
                "view_site" => "View Site",
                "view_private" => "View Private Posts",
                "view_scheduled" => "View Scheduled Posts",
                "view_draft" => "View Drafts",
                "view_own_draft" => "View Own Drafts",
                "add_post" => "Add Posts",
                "add_draft" => "Add Drafts",
                "edit_post" => "Edit Posts",
                "edit_draft" => "Edit Drafts",
                "edit_own_post" => "Edit Own Posts",
                "edit_own_draft" => "Edit Own Drafts",
                "delete_post" => "Delete Posts",
                "delete_draft" => "Delete Drafts",
                "delete_own_post" => "Delete Own Posts",
                "delete_own_draft" => "Delete Own Drafts",
                "view_page" => "View Pages",
                "add_page" => "Add Pages",
                "edit_page" => "Edit Pages",
                "delete_page" => "Delete Pages",
                "add_user" => "Add Users",
                "edit_user" => "Edit Users",
                "delete_user" => "Delete Users",
                "add_group" => "Add Groups",
                "edit_group" => "Edit Groups",
                "delete_group" => "Delete Groups",
                "import_content" => "Import Content",
                "export_content" => "Export Content"
            );

            # Delete all existing permissions.
            $sql->delete(
                table:"permissions",
                conds:false
            );

            # Insert the new default permissions.
            foreach ($names as $id => $name)
                $sql->insert(
                    table:"permissions",
                    data:array(
                        "id" => $id,
                        "name" => $name,
                        "group_id" => 0
                    )
                );

            # Define and insert the default groups.
            $groups = array(
                "Admin"  => array_keys($names),
                "Member" => array("view_site"),
                "Friend" => array(
                    "view_site",
                    "view_private",
                    "view_scheduled"
                ),
                "Banned" => array(),
                "Guest"  => array("view_site")
            );

            $group_id = array();

            foreach ($groups as $name => $permissions) {
                # Insert the group if it does not exist.
                if (
                    !$sql->count(
                        tables:"groups",
                        conds:array("name" => $name)
                    )
                )
                    $sql->insert(
                        table:"groups",
                        data:array("name" => $name)
                    );

                # Fetch the group's ID for permission creation.
                $group_id[$name] = $sql->select(
                    tables:"groups",
                    fields:"id",
                    conds:array("name" => $name),
                )->fetchColumn();

                # Insert the new permissions for this group.
                foreach ($permissions as $permission)
                    $sql->insert(
                        table:"permissions",
                        data:array(
                            "id" => $permission,
                            "name" => $names[$permission],
                            "group_id" => $group_id[$name]
                        )
                    );
            }

            # Add the admin user account if it does not exist.
            if (
                !$sql->count(
                    tables:"users",
                    conds:array("login" => $_POST['login'])
                )
            )
                $sql->insert(
                    table:"users",
                    data:array(
                        "login" => $_POST['login'],
                        "password" => User::hash_password($_POST['password1']),
                        "email" => $_POST['email'],
                        "group_id" => $group_id["Admin"],
                        "approved" => true,
                        "joined_at" => datetime()
                    )
                );

            # Normalize the Chyrp URL.
            $chyrp_url = rtrim(add_scheme($_POST['url']), "/");

            # Build the configuration file.
            $set = array(
                $config->set("sql", $settings),
                $config->set("name", strip_tags($_POST['name'])),
                $config->set("description", strip_tags($_POST['description'])),
                $config->set("url", $chyrp_url),
                $config->set("chyrp_url", $chyrp_url),
                $config->set("email", $_POST['email']),
                $config->set("timezone", $_POST['timezone']),
                $config->set("locale", $_POST['locale']),
                $config->set("monospace_font", false),
                $config->set("check_updates", true),
                $config->set("check_updates_last", 0),
                $config->set("theme", "blossom"),
                $config->set("posts_per_page", 5),
                $config->set("admin_per_page", 25),
                $config->set("feed_format", "AtomFeed"),
                $config->set("feed_items", 20),
                $config->set("uploads_path", DIR."uploads".DIR),
                $config->set("uploads_limit", 10),
                $config->set("search_pages", false),
                $config->set("send_pingbacks", false),
                $config->set("enable_emoji", true),
                $config->set("enable_markdown", true),
                $config->set("can_register", false),
                $config->set("email_activation", false),
                $config->set("email_correspondence", true),
                $config->set("default_group", $group_id["Member"]),
                $config->set("guest_group", $group_id["Guest"]),
                $config->set("clean_urls", false),
                $config->set("enable_homepage", false),
                $config->set("post_url", "(year)/(month)/(day)/(url)/"),
                $config->set("enabled_modules", array()),
                $config->set("enabled_feathers", array("text")),
                $config->set("routes", array()),
                $config->set("secure_hashkey", random(32))
            );

            if (in_array(false, $set, true))
                error(
                    __("Error"),
                    __("Could not write the configuration file.")
                );

            @unlink(INCLUDES_DIR.DIR."upgrading.lock");
            $installed = true;
        }
    }

    #---------------------------------------------
    # Installation Ends
    #---------------------------------------------

    foreach ((array) alert() as $message)
        echo ''.sanitize_html($message).''."\n";

          ?>

" id="host">

" id="port">

" id="username">

" id="password">

" id="database">

" id="prefix">

" id="url">

" id="name">

" id="description">

" id="login" maxlength="64">

" id="password1" maxlength="128">

" id="password2" maxlength="128">

" id="email" maxlength="128">

  1. install.php, you won't need it anymore."); ?>