1: <?php
2: /*
3: You may not change or alter any portion of this comment or credits
4: of supporting developers from this source code or any supporting source code
5: which is considered copyrighted (c) material of the original comment or credit authors.
6:
7: This program is distributed in the hope that it will be useful,
8: but WITHOUT ANY WARRANTY; without even the implied warranty of
9: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10: */
11:
12: namespace Xoops\Core\Theme;
13:
14: /**
15: * XoopsTheme component class file
16: *
17: * @category Xoops\Core
18: * @package Theme
19: * @author Skalpa Keo <skalpa@xoops.org>
20: * @copyright 2008-2015 XOOPS Project (http://xoops.org)
21: * @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
22: * @link http://xoops.org
23: */
24: class Factory
25: {
26: /**
27: * @var string
28: */
29: public $xoBundleIdentifier = 'XoopsThemeFactory';
30:
31: /**
32: * Currently enabled themes (if empty, all the themes in themes/ are allowed)
33: *
34: * @var array
35: */
36: public $allowedThemes = array();
37:
38: /**
39: * Default theme to instantiate if none specified
40: *
41: * @var string
42: */
43: public $defaultTheme = 'default';
44:
45: /**
46: * If users are allowed to choose a custom theme
47: *
48: * @var bool
49: */
50: public $allowUserSelection = true;
51:
52: /**
53: * Instantiate the specified theme
54: *
55: * @param array $options options array
56: *
57: * @return XoopsTheme
58: */
59: public function createInstance($options = array())
60: {
61: $xoops = \Xoops::getInstance();
62: // Grab the theme folder from request vars if present
63: if (empty($options['folderName'])) {
64: if (($req = @$_REQUEST['xoops_theme_select']) && $this->isThemeAllowed($req)) {
65: $options['folderName'] = $req;
66: if (isset($_SESSION) && $this->allowUserSelection) {
67: $_SESSION[$this->xoBundleIdentifier]['defaultTheme'] = $req;
68: }
69: } else {
70: if (isset($_SESSION[$this->xoBundleIdentifier]['defaultTheme'])) {
71: $options['folderName'] = $_SESSION[$this->xoBundleIdentifier]['defaultTheme'];
72: } else {
73: if (empty($options['folderName']) || !$this->isThemeAllowed($options['folderName'])) {
74: $options['folderName'] = $this->defaultTheme;
75: }
76: }
77: }
78: $xoops->setConfig('theme_set', $options['folderName']);
79: }
80: $options['path'] = \XoopsBaseConfig::get('themes-path') . '/' . $options['folderName'];
81: $inst = new XoopsTheme();
82: foreach ($options as $k => $v) {
83: $inst->$k = $v;
84: }
85: $inst->xoInit();
86: return $inst;
87: }
88:
89: /**
90: * Checks if the specified theme is enabled or not
91: *
92: * @param string $name theme name
93: *
94: * @return bool
95: */
96: public function isThemeAllowed($name)
97: {
98: return (empty($this->allowedThemes) || in_array($name, $this->allowedThemes));
99: }
100: }
101: