1: <?php
2: /**
3: * XOOPS Editor Abstract class
4: *
5: * You may not change or alter any portion of this comment or credits
6: * of supporting developers from this source code or any supporting source code
7: * which is considered copyrighted (c) material of the original comment or credit authors.
8: * This program is distributed in the hope that it will be useful,
9: * but WITHOUT ANY WARRANTY; without even the implied warranty of
10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11: *
12: * @copyright (c) 2000-2016 XOOPS Project (www.xoops.org)
13: * @license GNU GPL 2 (https://www.gnu.org/licenses/gpl-2.0.html)
14: * @package core
15: * @since 2.3.0
16: * @author Taiwen Jiang <phppp@users.sourceforge.net>
17: */
18: defined('XOOPS_ROOT_PATH') || exit('Restricted access');
19:
20: xoops_load('XoopsFormTextArea');
21:
22: /**
23: * Class XoopsEditor
24: */
25: class XoopsEditor extends XoopsFormTextArea
26: {
27: public $isEnabled;
28: public $configs;
29: public $rootPath;
30: public $_rows = 5;
31: public $_cols = 50;
32:
33: /**
34: * Constructor
35: */
36: public function __construct()
37: {
38: $args = func_get_args();
39: // For backward compatibility
40: if (!is_array($args[0])) {
41: $i = 0;
42: foreach (array('caption', 'name', 'value', 'rows', 'cols', 'hiddentext') as $key) {
43: if (isset($args[$i])) {
44: $configs[$key] = $args[$i];
45: }
46: ++$i;
47: }
48: $configs = (isset($args[$i]) && \is_array($args[$i])) ? array_merge($configs, $args[$i]) : $configs;
49: } else {
50: $configs = $args[0];
51: }
52: // TODO: switch to property_exists() as of PHP 5.1.0
53: $vars = get_class_vars(__CLASS__);
54: foreach ($configs as $key => $val) {
55: if (method_exists($this, 'set' . ucfirst($key))) {
56: $this->{'set' . ucfirst($key)}($val);
57: } elseif (array_key_exists("_{$key}", $vars)) {
58: $this->{"_{$key}"} = $val;
59: } elseif (array_key_exists($key, $vars)) {
60: $this->{$key} = $val;
61: } else {
62: $this->configs[$key] = $val;
63: }
64: }
65: $this->isActive();
66: }
67:
68: /**
69: * @return bool
70: */
71: public function isActive()
72: {
73: $this->isEnabled = true;
74:
75: return $this->isEnabled;
76: }
77: }
78:
79: /**
80: * Editor handler
81: *
82: * @copyright (c) 2000-2016 XOOPS Project (www.xoops.org)
83: * @license GNU GPL 2 (https://www.gnu.org/licenses/gpl-2.0.html)
84: * @package core
85: * @since 2.3.0
86: * @author Taiwen Jiang <phppp@users.sourceforge.net>
87: */
88: class XoopsEditorHandler
89: {
90: // static $instance;
91: public $root_path = '';
92: public $nohtml = false;
93: public $allowed_editors = array();
94:
95: /**
96: * Enter description here...
97: *
98: */
99: public function __construct()
100: {
101: $this->root_path = XOOPS_ROOT_PATH . '/class/xoopseditor';
102: }
103:
104: /**
105: * Access the only instance of this class
106: *
107: * @return object
108: * @static
109: * @staticvar object
110: */
111: public static function getInstance()
112: {
113: static $instance;
114: if (!isset($instance)) {
115: $class = __CLASS__;
116: $instance = new $class();
117: }
118:
119: return $instance;
120: }
121:
122: /**
123: *
124: * @param string $name Editor name which is actually the folder name
125: * @param array $options editor options: $key => $val
126: * @param string $OnFailure a pre-validated editor that will be used if the required editor is failed to create
127: * @param bool $noHtml dohtml disabled
128: *
129: * @return null
130: */
131: public function get($name = '', $options = null, $noHtml = false, $OnFailure = '')
132: {
133: if (array_key_exists($name, $this->getList($noHtml)) && $editor = $this->_loadEditor($name, $options)) {
134: return $editor;
135: }
136: $list = array_keys($this->getList($noHtml));
137: if (empty($OnFailure) || !in_array($OnFailure, $list)) {
138: $OnFailure = $list[0];
139: }
140: $editor = $this->_loadEditor($OnFailure, $options);
141:
142: return $editor;
143: }
144:
145: /**
146: * XoopsEditorHandler::getList()
147: *
148: * @param mixed $noHtml
149: *
150: * @return array
151: */
152: public function getList($noHtml = false)
153: {
154: /*
155: Do NOT use this method statically, please use
156: $editor_handler = XoopsEditorHandler::getInstance();
157: $result = array_flip($editor_handler->getList());
158: */
159: if (!isset($this->root_path)) {
160: $this->root_path = XOOPS_ROOT_PATH . '/class/xoopseditor';
161: $GLOBALS['xoopsLogger']->addDeprecated(__METHOD__ . '() should not be called statically.');
162: }
163:
164: xoops_load('XoopsCache');
165: $list = XoopsCache::read('editorlist');
166: if (empty($list)) {
167: $list = array();
168: $order = array();
169: xoops_load('XoopsLists');
170: $_list = XoopsLists::getDirListAsArray($this->root_path . '/');
171: foreach ($_list as $item) {
172: if (file_exists($file = $this->root_path . '/' . $item . '/language/' . $GLOBALS['xoopsConfig']['language'] . '.php')) {
173: include_once $file;
174: } elseif (file_exists($file = $this->root_path . '/' . $item . '/language/english.php')) {
175: include_once $file;
176: }
177: if (file_exists($file = $this->root_path . '/' . $item . '/editor_registry.php')) {
178: include $file;
179: if (empty($config['order'])) {
180: continue;
181: }
182: $order[] = $config['order'];
183: $list[$item] = array('title' => $config['title'], 'nohtml' => $config['nohtml']);
184: }
185: }
186: array_multisort($order, $list);
187: XoopsCache::write('editorlist', $list);
188: }
189:
190: $editors = array_keys($list);
191: if (!empty($this->allowed_editors)) {
192: $editors = array_intersect($editors, $this->allowed_editors);
193: }
194: $_list = array();
195: foreach ($editors as $name) {
196: if (!empty($noHtml) && empty($list[$name]['nohtml'])) {
197: continue;
198: }
199: $_list[$name] = $list[$name]['title'];
200: }
201:
202: return $_list;
203: }
204:
205: /**
206: * XoopsEditorHandler::render()
207: *
208: * @param mixed $editor
209: * @return
210: */
211: public function render($editor)
212: {
213: $GLOBALS['xoopsLogger']->addDeprecated(__METHOD__ . ' is deprecated');
214:
215: return $editor->render();
216: }
217:
218: /**
219: * XoopsEditorHandler::setConfig()
220: *
221: * @param mixed $editor
222: * @param mixed $options
223: * @return void
224: */
225: public function setConfig($editor, $options)
226: {
227: if (method_exists($editor, 'setConfig')) {
228: $editor->setConfig($options);
229: } else {
230: foreach ($options as $key => $val) {
231: $editor->$key = $val;
232: }
233: }
234: }
235:
236: /**
237: * XoopsEditorHandler::_loadEditor()
238: *
239: * @param mixed $name
240: * @param mixed $options
241: * @return
242: */
243: public function _loadEditor($name, $options = null)
244: {
245: $editor = null;
246: if (empty($name) || !array_key_exists($name, $this->getList())) {
247: return $editor;
248: }
249: $editor_path = $this->root_path . '/' . $name;
250: if (file_exists($file = $editor_path . '/language/' . $GLOBALS['xoopsConfig']['language'] . '.php')) {
251: include_once $file;
252: } elseif (file_exists($file = $editor_path . '/language/english.php')) {
253: include_once $file;
254: }
255: if (file_exists($file = $editor_path . '/editor_registry.php')) {
256: include $file;
257: } else {
258: return $editor;
259: }
260: if (empty($config['order'])) {
261: return $editor;
262: }
263: include_once $config['file'];
264: $editor = new $config['class']($options);
265:
266: return $editor;
267: }
268: }
269: