Example Plugin

Example Plugin

The following is a complete plugin that verifies that each new account is created using a strong password (specifically: that it contains at least one digit, one symbol, one uppercase letter, and one lowercase letter).

If the password appears to be weak, it the plugin instructs Centova Cast to refuse to create the account and return a descriptive error message.

<?php
/* save as /usr/local/centovacast/system/plugins/passwordstrength/hooks.php */
class PluginHooks_passwordstrength extends PluginHooks {

    public function install_hooks() {
        PluginHooks::register('pre-create-account',array(&$this,'handle_precreateaccount'));
    }

    /**
     * Handles the pre-create-account event
     *
     * @param string $username the username of the account
     * @param string $password the password for the account
     * @param string $email the email address for the account
     * @param string $ipaddress the IP address for the account
     * @param int $port the port number for the account
     * @param int $maxclients the listener limit for the account
     * @param int $maxbitrate the maximum bit rate for the account
     * @param int $transferlimit the data transfer limit for the account
     * @param int $diskquota the disk quota for the account
     * @param int $usesource 1 if the autoDJ is enabled, otherwise 0
     * @param int $mountlimit the mount point limit for the account
     *
     * @return int returns PluginHooks::OK on success, PluginHooks::ERROR on error
     */
    public function handle_precreateaccount($username,$password,$email,$ipaddress,$port,$maxclients,$maxbitrate,$transferlimit,$diskquota,$usesource,$mountlimit) {
        try {
            if (!preg_match('/[A-Z]/',$password)) throw new Exception('Missing uppercase characters.');
            if (!preg_match('/[a-z]/',$password)) throw new Exception('Missing lowercase characters.');
            if (!preg_match('/[0-9]/',$password)) throw new Exception('Missing digits.');
            if (preg_match('/^[A-Za-z0-9]+$/',$password)) throw new Exception('Missing symbol characters.');
        } catch (Exception $e) {
            $this->set_error('Weak password: must contain at least one digit, one symbol, and both uppercase and lowercase letters.  '.$e->getMessage);
            return PluginHooks::ERROR;
        }
        return PluginHooks::OK;
    }

}

Section: Plugins
Next: Plugins API