Exemple #1
0
    /**
     * \brief Class configuration.
     * \param array		$_conf Global configuration array
     * \param array		$_pconf Plug-in configuration array
     */
    public static function configurePmHandler($_gconf, $_conf)
    {
        self::$_db = $_db = $_gconf['_db'];
        if (!\Kazoo\Cache::doOnce('kthreads', function () use($_db) {
            $sql = 'CREATE TABLE IF NOT EXISTS `_kthreads` (
				`_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
				`_subject` varchar(50) NOT NULL,
				`_users` text NOT NULL,
				`_read` text NOT NULL,
				PRIMARY KEY (`_id`)
				) ENGINE=MyISAM DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1 ;';
            if (self::$_db->exec($sql) === false) {
                return false;
            }
            return '_kthreads has been created. If not, please erase this file now.';
        })) {
            throw new \Exception('_kthreads table couldn\'t have been created.');
        }
        if (!\Kazoo\Cache::doOnce('kpms', function () use($_db) {
            $sql = 'CREATE TABLE IF NOT EXISTS `_kpms` (
				`_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
				`_author` int(11) unsigned NOT NULL,
				`_date` datetime NOT NULL,
				`_message` text NOT NULL,
				`_thread` int(11) unsigned NOT NULL,
				PRIMARY KEY (`_id`)
				) ENGINE=MyISAM DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1 ;';
            if (self::$_db->exec($sql) === false) {
                return false;
            }
            return '_kpms has been created. If not, please erase this file now.';
        })) {
            throw new \Exception('_pms table couldn\'t have been created.');
        }
    }
 /**
  * \brief Class' constructor
  * \param int	$id An id if you want to load another user's class
  */
 public function __construct($id = null)
 {
     if (!isset(self::$_conf['_user_database_configuration']['_active'])) {
         throw new \Exception('User database needs an "_active" field.');
     }
     if (!isset(self::$_conf['_user_database_configuration']['_banned'])) {
         throw new \Exception('User database needs an "_banned" field.');
     }
     if (!isset(self::$_conf['_user_database_configuration']['_email'])) {
         throw new \Exception('User database needs an "_email" field.');
     }
     if (!isset(self::$_conf['_user_database_configuration']['_http'])) {
         throw new \Exception('User database needs a "_http" field.');
     }
     if (!isset(self::$_conf['_user_database_configuration']['_id'])) {
         throw new \Exception('User database needs an "_id" field.');
     }
     if (!isset(self::$_conf['_user_database_configuration']['_key'])) {
         throw new \Exception('User database needs an "_key" field.');
     }
     if (!isset(self::$_conf['_user_database_configuration']['_lastlog'])) {
         throw new \Exception('User database needs an "_lastlog" field.');
     }
     if (!isset(self::$_conf['_user_database_configuration']['_pw'])) {
         throw new \Exception('User database needs an "_pw" field.');
     }
     if (!isset(self::$_conf['_user_database_configuration']['_regdate'])) {
         throw new \Exception('User database needs an "_regdate" field.');
     }
     foreach (self::$_conf['_user_database_configuration'] as $field => $useless) {
         $this->attributes[$field] = '';
     }
     $_db = self::$_db;
     $_conf = self::$_conf;
     // Check for modifications into database
     if (!\Kazoo\Cache::doOnce('kusers', function () use($_db, $_conf) {
         // The table doesn't already exist
         $sql = 'CREATE TABLE IF NOT EXISTS `_kusers` (';
         foreach ($_conf['_user_database_configuration'] as $field => $conf) {
             $type = $conf[0];
             if (isset($conf[1]) && is_int($conf[1])) {
                 $type .= '(' . $conf[1] . ')';
             }
             $options = '';
             if (isset($conf[2])) {
                 foreach (explode('-', $conf[2]) as $elem) {
                     $options .= ' ' . $elem;
                 }
             }
             $sql .= '`' . $field . '` ' . $type . $options . ', ';
         }
         $sql .= 'PRIMARY KEY (`_id`)) ENGINE=MyISAM DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1;';
         $_db->exec($sql);
         return base64_encode(serialize($_conf['_user_database_configuration']));
     }, function ($data) use($_db, $_conf) {
         $data = unserialize(base64_decode($data));
         if ($data !== $_conf['_user_database_configuration']) {
             $sql = 'ALTER TABLE _kusers ';
             // Check by index
             foreach ($_conf['_user_database_configuration'] as $field => $conf) {
                 if (!isset($data[$field])) {
                     $sql .= 'ADD ';
                     $type = $conf[0];
                     if (isset($conf[1]) && is_int($conf[1])) {
                         $type .= '(' . $conf[1] . ')';
                     }
                     $options = '';
                     if (isset($conf[2])) {
                         foreach (explode('-', $conf[2]) as $elem) {
                             $options .= ' ' . $elem;
                         }
                     }
                     $sql .= '`' . $field . '` ' . $type . $options . ' ';
                 }
                 // Same field
                 unset($data[$field]);
             }
             // If there's any other field on $data, erase'em
             foreach ($data as $field => $conf) {
                 if (strpos($field, '_')) {
                     continue;
                 }
                 // Protect critical fields
                 $sql .= 'DROP COLUMN `' . $field . '` ';
             }
             if ($_db->exec($sql) !== false) {
                 return base64_encode(serialize($_conf['_user_database_configuration']));
             }
             return false;
         }
     })) {
         throw new \Exception('_kusers table couldn\'t have been created.');
     }
     // If $id is null, we don't load data from database; in other words, our user is a guest.
     if ($id !== null) {
         $this->attributes['_id'] = (int) $id;
         $this->load();
     }
 }
    /**
     * \brief Class configuration.
     * \param array		$_conf Global configuration array
     * \param array		$_pconf Plug-in configuration array
     */
    public static function configureForumHandler($_conf, $_pconf)
    {
        if (!$_conf['_db_enable']) {
            throw new \Exception('Forum plug-in needs database enabled.');
        }
        self::$_db = $_db = $_conf['_db'];
        self::$_cookieName = $_pconf['_forum_cookie'];
        self::$_cookieUpdateName = $_pconf['_forum_cookie_update'];
        self::$_cookieDuration = $_pconf['_forum_cookie_duration'];
        # Add multilingual support to database configuration
        self::$_fields = array();
        foreach ($_pconf['_forum_database_configuration'] as $table => $data) {
            self::$_fields[$table] = array();
            foreach ($data as $field => $config) {
                if ($needle = strpos($field, '*')) {
                    unset($_pconf['_forum_database_configuration'][$table][$field]);
                    foreach ($_conf['_lang_allowed'] as $lg) {
                        $nField = str_replace('*', $lg, $field);
                        $_pconf['_forum_database_configuration'][$table][$nField] = $config;
                        self::$_fields[$table][] = $nField;
                    }
                } else {
                    self::$_fields[$table][] = $field;
                }
            }
        }
        # Now, let's create some databases...
        foreach (self::$_fields as $table => $data) {
            if (!\Kazoo\Cache::doOnce($table, function () use($_db, $table, $_pconf) {
                $sql = 'CREATE TABLE IF NOT EXISTS `' . $table . '` (';
                foreach ($_pconf['_forum_database_configuration'][$table] as $field => $conf) {
                    $type = $conf[0];
                    if (isset($conf[1]) && is_int($conf[1])) {
                        $type .= '(' . $conf[1] . ')';
                    }
                    $options = '';
                    if (isset($conf[2])) {
                        foreach (explode('-', $conf[2]) as $elem) {
                            $options .= ' ' . $elem;
                        }
                    }
                    $sql .= '`' . $field . '` ' . $type . $options . ', ';
                }
                $sql .= 'PRIMARY KEY (`_id`)
				) ENGINE=MyISAM DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1;
				ALTER TABLE `' . $table . '` ADD INDEX(`_id`);';
                if (self::$_db->exec($sql) === false) {
                    return false;
                }
                return base64_encode(serialize($_pconf['_forum_database_configuration'][$table]));
            }, function ($extracted) use($_db, $data, $table, $_pconf) {
                $extracted = unserialize(base64_decode($extracted));
                if ($extracted !== $_pconf['_forum_database_configuration'][$table]) {
                    $sql = 'ALTER TABLE `' . $table . '` ';
                    // Check by index
                    foreach ($_pconf['_forum_database_configuration'][$table] as $field => $conf) {
                        if (!isset($extracted[$field])) {
                            $sql .= 'ADD ';
                            $type = $conf[0];
                            if (isset($conf[1]) && is_int($conf[1])) {
                                $type .= '(' . $conf[1] . ')';
                            }
                            $options = '';
                            if (isset($conf[2])) {
                                foreach (explode('-', $conf[2]) as $elem) {
                                    $options .= ' ' . $elem;
                                }
                            }
                            $sql .= '`' . $field . '` ' . $type . $options . ' ';
                        }
                        // Same field
                        unset($extracted[$field]);
                    }
                    // If there's any other field on $data, erase'em
                    foreach ($extracted as $field => $conf) {
                        if (strpos($field, '_')) {
                            continue;
                        }
                        // Protect critical fields
                        $sql .= 'DROP COLUMN `' . $field . '` ';
                    }
                    if ($_db->exec($sql) !== false) {
                        return base64_encode(serialize($_pconf['_forum_database_configuration'][$table]));
                    }
                    return false;
                }
            })) {
                throw new \Exception($table . ' table couldn\'t have been created.');
            }
        }
    }
    /**
     * \brief Class configuration.
     * \param array		$_conf Global configuration array
     * \param array		$_pconf Plug-in configuration array
     */
    public static function configureRoleHandler($_conf, $_pconf)
    {
        if (!$_conf['_db_enable']) {
            throw new \Exception('User plug-in needs database enabled.');
        }
        self::$_db = $_db = $_conf['_db'];
        self::$_ptree = $_pconf['_user_permission'];
        if (self::$_db === null) {
            throw new \Exception('User plug-in needs a database connection.');
        }
        if (!\Kazoo\Cache::doOnce('kroles', function () use($_db, $_conf) {
            $nameFields = '';
            foreach ($_conf['_lang_allowed'] as $lang) {
                $nameFields .= '`_name_' . $lang . '` varchar(60) NOT NULL, ';
            }
            $sql = 'CREATE TABLE IF NOT EXISTS `_kroles` (
				`_id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
				' . $nameFields . '
				`_permission` text NOT NULL COMMENT \'A serialized array\',
				PRIMARY KEY (`_id`)
				) ENGINE=MyISAM DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1 ;';
            if (self::$_db->exec($sql) === false) {
                return false;
            }
            return base64_encode(serialize($_conf['_lang_allowed']));
        }, function ($data) use($_db, $_conf) {
            $data = unserialize(base64_decode($data));
            if ($data !== $_conf['_lang_allowed']) {
                $newData = $_conf['_lang_allowed'];
                foreach ($newData as $key => $lg) {
                    if (in_array($lg, $data, true)) {
                        unset($newData[$key]);
                    }
                }
                if (!empty($newData)) {
                    $sql = 'ALTER TABLE `_kroles` ';
                    foreach ($newData as $lg) {
                        $sql .= 'ADD `_name_' . $lg . '` varchar(60) NOT NULL, ';
                    }
                    $sql = substr($sql, 0, -2) . ';';
                }
                return base64_encode(serialize($_conf['_lang_allowed']));
            }
        })) {
            throw new \Exception('_kroles table couldn\'t have been created.');
        }
    }