public function install()
 {
     $mod = new Modules();
     $mod->getModuleByName('fabriqmodules');
     $perms = array('manage modules');
     $perm_ids = FabriqModules::register_perms($mod->id, $perms);
     // map paths
     $pathmap =& FabriqModules::module('pathmap');
     $pathmap->register_path('fabriqmodules', 'fabriqmodules', 'manage', 'module');
     $pathmap->register_path('fabriqmodules/manage', 'fabriqmodules', 'manage', 'module');
     $pathmap->register_path('fabriqmodules/configure/!#', 'fabriqmodules', 'configure', 'module', null, 2);
     $pathmap->register_path('fabriqmodules/disable/!#', 'fabriqmodules', 'disable', 'module', null, 2);
     $pathmap->register_path('fabriqmodules/enable/!#', 'fabriqmodules', 'enable', 'module', null, 2);
     $pathmap->register_path('fabriqmodules/install/!#', 'fabriqmodules', 'install', 'module', null, 2);
     $pathmap->register_path('fabriqmodules/uninstall/!#', 'fabriqmodules', 'uninstall', 'module', null, 2);
     // give administrators the ability to manage modules
     $adminPerm = FabriqModules::new_model('roles', 'ModulePerms');
     $adminPerm->permission = $perm_ids[0];
     $adminRole = FabriqModules::new_model('roles', 'Roles');
     $adminRole->getRole('administrator');
     $adminPerm->role = $adminRole->id;
     $adminPerm->id = $adminPerm->create();
     // set module as installed
     $mod->installed = 1;
     $mod->update();
 }
 public function install()
 {
     $mod = new Modules();
     $mod->getModuleByName('roles');
     $perms = array('create roles', 'update roles', 'delete roles', 'manage roles');
     $perm_ids = FabriqModules::register_perms($mod->id, $perms);
     global $db;
     $sql = "CREATE TABLE IF NOT EXISTS `fabmod_roles_roles` (\n\t\t\t`id` INT(11) NOT NULL AUTO_INCREMENT,\n\t\t\t`role` VARCHAR(100) NOT NULL,\n\t\t\t`enabled` TINYINT(1) NOT NULL DEFAULT 1,\n\t\t\t`created` DATETIME NOT NULL,\n\t\t\t`updated` DATETIME NOT NULL,\n\t\t\tPRIMARY KEY (`id`)\n\t\t) ENGINE=INNODB;";
     $db->query($sql);
     $sql = "CREATE TABLE IF NOT EXISTS `fabmod_roles_moduleperms` (\n\t\t\t`id` INT(11) NOT NULL AUTO_INCREMENT,\n\t\t\t`permission` INT(11) NOT NULL,\n\t\t\t`role` INT(11) NOT NULL,\n\t\t\t`created` DATETIME NOT NULL,\n\t\t\t`updated` DATETIME NOT NULL,\n\t\t\tPRIMARY KEY (`id`),\n\t\t\tCONSTRAINT `fk_moduleperms_permission` FOREIGN KEY (`permission`) REFERENCES fabmods_perms(id) ON DELETE CASCADE,\n\t\t\tCONSTRAINT `fk_moduleperms_role` FOREIGN KEY (`role`) REFERENCES fabmod_roles_roles(id) ON DELETE CASCADE\n\t\t) ENGINE=INNODB;";
     $db->query($sql);
     // create base roles
     $role = FabriqModules::new_model('roles', 'Roles');
     $role->role = "unauthenticated";
     $role->enabled = 1;
     $role->id = $role->create();
     $role = FabriqModules::new_model('roles', 'Roles');
     $role->role = "authenticated";
     $role->enabled = 1;
     $role->id = $role->create();
     $role = FabriqModules::new_model('roles', 'Roles');
     $role->role = "administrator";
     $role->enabled = 1;
     $role->id = $role->create();
     // map paths
     $pathmap =& FabriqModules::module('pathmap');
     $pathmap->register_path('fabriqadmin/roles/manage', 'roles', 'index', 'module');
     $pathmap->register_path('fabriqadmin/roles/create', 'roles', 'create', 'module');
     $pathmap->register_path('fabriqadmin/roles/perms', 'roles', 'perms', 'module');
     // set module as installed
     $mod->installed = 1;
     $mod->update();
 }
 public function getAll($page = 0, $limit = 10)
 {
     global $db;
     $go = $page * $limit;
     if (FabriqModules::module('roles')->userHasPermission('update blog posts', 'roles')) {
         $query = "SELECT * FROM {$this->db_table} ORDER BY created DESC LIMIT ?, ?";
         $this->fill($db->prepare_select($query, $this->fields(), array($go, $limit)));
     } else {
         $query = "SELECT * FROM {$this->db_table} WHERE locked = ? ORDER BY created DESC LIMIT ?, ?";
         $this->fill($db->prepare_select($query, $this->fields(), array(0, $go, $limit)));
     }
     $found = array();
     for ($i = 0; $i < $this->count(); $i++) {
         // get user details
         if (!array_key_exists($this[$i]->user, $found)) {
             $user = FabriqModules::new_model('users', 'Users');
             $user->find($this[$i]->user);
             $found[$this[$i]->user] = $user;
             $this[$i]->user = $user;
         }
         // look for a custom url
         $this[$i]->customPath = FabriqModules::new_model('pathmap', 'Paths');
         $this[$i]->customPath->get_by_details('blog', 'show', $this[$i]->id);
     }
     // get terms if the module is enabled
     if (FabriqModules::enabled('taxonomy')) {
         for ($i = 0; $i < $this->count(); $i++) {
             $this[$i]->terms = FabriqModules::new_model('taxonomy', 'Terms');
             $this[$i]->terms->getMappedTerms($this->db_table, $this[$i]->id);
         }
     }
 }
 /**
  * Build this menu
  */
 public function buildMenu()
 {
     $this->items = FabriqModules::new_model('sitemenus', 'MenuItems')->getMenuItems($this->id);
     // flesh out all of the menu children
     for ($i = 0; $i < count($this->items); $i++) {
         $this->getItemChildren($this->items[$i]);
     }
 }
 /**
  * Get the items for a given menu
  * @param int $menu
  */
 public function getMenuItems($menu)
 {
     global $db;
     $query = "SELECT *\n\t\t\tFROM `{$this->db_table}`\n\t\t\tWHERE `menu` = ?\n\t\t\tAND (`parentItem` IS NULL OR `parentItem` = '')\n\t\t\tORDER BY `weight`, `itemName`;";
     $data = $db->prepare_select($query, $this->fields(), array($menu));
     $items = array();
     for ($i = 0; $i < count($data); $i++) {
         $items[$i] = FabriqModules::new_model('sitemenus', 'MenuItems');
         $items[$i]->fill(array($data[$i]));
     }
     return $items;
 }
 public function install()
 {
     $mod = new Modules();
     $mod->getModuleByName('fabriqinstall');
     $perms = array('update Fabriq');
     $perm_ids = FabriqModules::register_perms($mod->id, $perms);
     // map paths
     $pathmap =& FabriqModules::module('pathmap');
     $pathmap->register_path('fabriqinstall', 'fabriqinstall', 'install', 'module');
     $pathmap->register_path('fabriqinstall/install', 'fabriqinstall', 'install', 'module');
     $pathmap->register_path('fabriqinstall/install/!#', 'fabriqinstall', 'install', 'module', null, 2);
     $pathmap->register_path('fabriqinstall/update', 'fabriqinstall', 'update', 'module');
     $pathmap->register_path('fabriqinstall/update/!#', 'fabriqinstall', 'update', 'module', null, 2);
     // give administrators the ability to update the framework
     $adminPerm = FabriqModules::new_model('roles', 'ModulePerms');
     $adminPerm->permission = $perm_ids[0];
     $adminRole = FabriqModules::new_model('roles', 'Roles');
     $adminRole->getRole('administrator');
     $adminPerm->role = $adminRole->id;
     $adminPerm->id = $adminPerm->create();
     // set module as installed
     $mod->installed = 1;
     $mod->update();
 }
 public function show($entry)
 {
     if (FabriqModules::module('roles')->requiresPermission('view blog posts', 'blog')) {
         $blog = FabriqModules::new_model('blog', 'Blogs');
         if (!$entry || !is_numeric($entry)) {
             $blog->find(PathMap::arg(2));
         } else {
             $blog->find($entry);
         }
         if ($blog->title != '' && ($blog->locked == 0 || FabriqModules::module('roles')->requiresPermission('update blog posts', 'blog'))) {
             Fabriq::title('Blog - ' . $blog->title);
             $user = FabriqModules::new_model('users', 'Users');
             $user->find($blog->user);
             $blog->user = $user;
             $taxonomyEnabled = FabriqModules::enabled('taxonomy');
             FabriqModules::set_var('blog', 'taxonomyEnabled', $taxonomyEnabled);
             FabriqModules::set_var('blog', 'blog', $blog);
             $isAdmin = FabriqModules::module('roles')->userHasPermission('update blog posts', 'blog');
             FabriqModules::set_var('blog', 'isAdmin', $isAdmin);
         } else {
             FabriqModules::set_var('blog', 'notFound', true);
             Fabriq::title('Blog entry not found');
         }
     }
 }
 public function listItems($listMenu, $clear = false)
 {
     $menu = FabriqModules::new_model('sitemenus', 'Menus');
     $menu->getMenuByName($listMenu);
     $menu->buildMenu();
     FabriqModules::set_var('sitemenus', 'listMenu', $menu);
     FabriqModules::set_var('sitemenus', 'clear', $clear);
 }
 /**
  * Check whether or not the user is logged in
  */
 public static function checkUserStatus()
 {
     if ((!isset($_SESSION[Fabriq::siteTitle()]['FABMOD_USERS_roles']) || $_SESSION[Fabriq::siteTitle()]['FABMOD_USERS_roles'] == '') && Fabriq::installed()) {
         $role = FabriqModules::new_model('roles', 'Roles');
         $role->getRole('unauthenticated');
         $_SESSION[Fabriq::siteTitle()]['FABMOD_USERS_roles'] = serialize(array($role->id, $role->role));
     }
 }
 public function getRoles()
 {
     Fabriq::render('none');
     header('Content-type:application/json');
     if (FabriqModules::module('roles')->requiresPermission('administer users', $this->name)) {
         $r = FabriqModules::new_model('roles', 'Roles');
         $r->getAll();
         $roles = array();
         for ($i = 0; $i < $r->count(); $i++) {
             if ($r[$i]->role != 'unauthenticated' && $r[$i]->role != 'authenticated') {
                 $roles[] = $r[$i];
             }
         }
         echo json_encode(array('roles' => $roles));
     } else {
         echo json_encode(array('notLoggedIn' => true));
     }
 }
 public function perms()
 {
     if ($this->requiresPermission('manage roles', $this->name)) {
         Fabriq::title('Admin | Manage permissions');
         FabriqModules::add_css('roles', 'roles');
         $perms = new Perms();
         $perms->getAll();
         $modules = new Modules();
         $modules->getEnabled();
         $roles = FabriqModules::new_model('roles', 'Roles');
         $roles->getRoles();
         $modulePerms = FabriqModules::new_model('roles', 'ModulePerms');
         $modulePerms->getAll();
         $permissions = array();
         foreach ($perms as $perm) {
             $permissions[$perm->id] = array();
             foreach ($roles as $role) {
                 if (isset($modulePerms->perms[$perm->id][$role->id])) {
                     $permissions[$perm->id][$role->id] = 1;
                 } else {
                     $permissions[$perm->id][$role->id] = 0;
                 }
             }
         }
         if (isset($_POST['submit'])) {
             foreach ($perms as $perm) {
                 foreach ($roles as $role) {
                     if (isset($_POST['permission'][$perm->id][$role->id])) {
                         $permissions[$perm->id][$role->id] = 1;
                         // add to database if it's not already set
                         if (!isset($modulePerms->perms[$perm->id][$role->id])) {
                             $p = FabriqModules::new_model('roles', 'ModulePerms');
                             $p->permission = $perm->id;
                             $p->role = $role->id;
                             $p->id = $p->create();
                             $modulePerms->perms[$perm->id][$role->id] = $modulePerms->count();
                             $modulePerms->add($p);
                         }
                     } else {
                         $permissions[$perm->id][$role->id] = 0;
                         // remove from database if it is already set
                         if (isset($modulePerms->perms[$perm->id][$role->id])) {
                             $p = FabriqModules::new_model('roles', 'ModulePerms');
                             $p->find($modulePerms[$modulePerms->perms[$perm->id][$role->id]]->id);
                             $p->destroy();
                             $modulePerms->remove($modulePerms->perms[$perm->id][$role->id]);
                             $modulePerms->reindex();
                         }
                     }
                 }
             }
             Messaging::message('Permissions have been updated.', 'success');
         }
         FabriqModules::set_var($this->name, 'perms', $perms);
         FabriqModules::set_var($this->name, 'modules', $modules);
         FabriqModules::set_var($this->name, 'roles', $roles);
         FabriqModules::set_var($this->name, 'permissions', $permissions);
     }
 }
    /**
     * Install step 4
     * Install the core database tables and modules and create the
     * default administrator
     */
    protected function install_step4($continue = TRUE)
    {
        Fabriq::title('Core module configuration');
        FabriqTemplates::template('fabriqinstall');
        Messaging::message('Be sure to continue with module set up in order to complete the install process', 'warning');
        if (isset($_POST['submit'])) {
            $emailPattern = '/^([a-z0-9])(([-a-z0-9._])*([a-z0-9]))*\\@([a-z0-9])(([a-z0-9-])*([a-z0-9]))+' . '(\\.([a-z0-9])([-a-z0-9_-])?([a-z0-9])+)+$/i';
            $displayPattern = '/([A-z0-9]){6,24}/';
            $user = FabriqModules::new_model('users', 'Users');
            $user->display = $_POST['display'];
            $user->email = $_POST['email'];
            $user->encpwd = $_POST['pwd'];
            if (!preg_match($displayPattern, $user->display)) {
                Messaging::message("Display name is invalid");
            }
            if (!preg_match($emailPattern, $user->email)) {
                Messaging::message("e-mail address is invalid");
            }
            if (strlen($user->encpwd) < 8 || $user->encpwd == $user->display || $user->encpwd == $user->email || $user->encpwd != $_POST['confpwd']) {
                Messaging::message("Password is invalid");
            }
            if (!Messaging::has_messages()) {
                $user->status = 1;
                $user->banned = 0;
                $user->forcepwdreset = 0;
                $user->id = $user->create();
                $user->encpwd = crypt($user->encpwd, $user->id);
                $user->update();
                $role = FabriqModules::new_model('roles', 'Roles');
                $role->getRole('administrator');
                $userRole = FabriqModules::new_model('users', 'UserRoles');
                $userRole->user = $user->id;
                $userRole->role = $role->id;
                $userRole->id = $userRole->create();
                global $_FAPP;
                $url = $_FAPP['url'] . PathMap::build_path('users', 'login');
                $message = <<<EMAIL
Hello {$user->display},

Your account has been created on the {$_FAPP['title']} website.

You can log in by navigating to {$url} in your browser.

Thanks,
The {$_FAPP['title']} team


NOTE: Do not reply to this message. It was automatically generated.
EMAIL;
                mail($user->email, "Your account at {$_FAPP['title']}", $message, 'From: noreply@' . str_replace('http://', '', str_replace('https://', '', str_replace('www.', '', $_FAPP['url']))));
                if ($continue) {
                    // go to next step
                    header("Location: " . PathMap::build_path('fabriqinstall', 'install', 5));
                    exit;
                }
            }
            FabriqModules::set_var('fabriqinstall', 'submitted', true);
        }
    }
 public function destroy($map_id)
 {
     $map = FabriqModules::new_model('pathmap', 'Paths');
     $map->find($map_id);
     $map->destroy();
 }