1: <?php
2: /**
3: * Initial functions
4: *
5: * @copyright (c) 2000-2016 XOOPS Project (www.xoops.org)
6: * @license GNU GPL 2 (https://www.gnu.org/licenses/gpl-2.0.html)
7: * @author Taiwen Jiang <phppp@users.sourceforge.net>
8: * @since 1.00
9: * @package Frameworks
10: * @subpackage art
11: */
12:
13: if (substr(XOOPS_VERSION, 0, 9) < 'XOOPS 2.3') {
14: trigger_error('The package only works for XOOPS 2.3+', E_USER_ERROR);
15: }
16:
17: if (!defined('FRAMEWORKS_ART_FUNCTIONS_INI')):
18: define('FRAMEWORKS_ART_FUNCTIONS_INI', true);
19:
20: define('FRAMEWORKS_ROOT_PATH', XOOPS_ROOT_PATH . '/Frameworks');
21:
22: /**
23: * Load declaration of an object handler
24: *
25: *
26: * @param string $handler handler name, optional
27: * @param string $dirname
28: *
29: * @return bool
30: */
31: function load_objectHandler($handler = '', $dirname = 'art')
32: {
33: if (empty($handler)) {
34: $handlerClass = 'ArtObject';
35: $fileName = 'object.php';
36: } else {
37: $handlerClass = 'ArtObject' . ucfirst($handler) . 'Handler';
38: $fileName = "object.{$handler}.php";
39: }
40:
41: class_exists($handlerClass) || require_once FRAMEWORKS_ROOT_PATH . "/{$dirname}/{$fileName}";
42:
43: return class_exists($handlerClass);
44: }
45:
46: /**
47: * @return bool
48: */
49: function load_object()
50: {
51: return load_objectHandler();
52: }
53:
54: /**
55: * Load a collective functions of Frameworks
56: *
57: * @param string $group name of the collective functions, empty for functions.php
58: * @param string $dirname
59: * @return bool
60: */
61: function load_functions($group = '', $dirname = 'art')
62: {
63: $dirname = ('' == $dirname) ? 'art' : $dirname;
64: $constant = strtoupper("frameworks_{$dirname}_functions" . ($group ? "_{$group}" : ''));
65: if (defined($constant)) {
66: return true;
67: }
68:
69: return include_once FRAMEWORKS_ROOT_PATH . "/{$dirname}/functions.{$group}" . (empty($group) ? '' : '.') . 'php';
70: }
71:
72: /**
73: * Load a collective functions of a module
74: *
75: * The function file should be located in /modules/MODULE/functions.{$group}.php
76: * To avoid slowdown caused by include_once, a constant is suggested in the corresponding file: capitalized {$dirname}_{functions}[_{$group}]
77: *
78: * The function is going to be formulated to use xos_kernel_Xoops2::loadService() in XOOPS 2.3+
79: *
80: * @param string $group name of the collective functions, empty for functions.php
81: * @param string $dirname module dirname, optional
82: * @return bool
83: */
84: function mod_loadFunctions($group = '', $dirname = '')
85: {
86: $dirname = !empty($dirname) ? $dirname : $GLOBALS['xoopsModule']->getVar('dirname', 'n');
87: $constant = strtoupper("{$dirname}_functions" . ($group ? "_{$group}" : '') . '_loaded');
88: if (defined($constant)) {
89: return true;
90: }
91: $filename = XOOPS_ROOT_PATH . "/modules/{$dirname}/include/functions.{$group}" . (empty($group) ? '' : '.') . 'php';
92:
93: return include_once $filename;
94: }
95:
96: /**
97: * Load renderer for a class
98: *
99: * The class file should be located in /modules/MODULE/{$class}.renderer.php
100: * The classf name should be defined as Capitalized(module_dirname)Capitalized(class_name)Renderer
101: *
102: * @param string $class name of the classname
103: * @param string $dirname module dirname, optional
104: * @return bool
105: */
106: function mod_loadRenderer($class, $dirname = '')
107: {
108: $dirname = !empty($dirname) ? $dirname : $GLOBALS['xoopsModule']->getVar('dirname', 'n');
109: $renderer = ucfirst($dirname) . ucfirst($class) . 'Renderer';
110: if (!class_exists($renderer)) {
111: require_once XOOPS_ROOT_PATH . "/modules/{$dirname}/class/{$class}.renderer.php";
112: }
113: $instance = eval("{$renderer}::instance()");
114:
115: return $instance;
116: }
117:
118: /**
119: * Get localized string if it is defined
120: *
121: * @param string $name string to be localized
122: */
123: if (!function_exists('mod_constant')) {
124: /**
125: * @param $name
126: *
127: * @return mixed
128: */
129: function mod_constant($name)
130: {
131: if (!empty($GLOBALS['VAR_PREFIXU']) && @defined($GLOBALS['VAR_PREFIXU'] . '_' . strtoupper($name))) {
132: return constant($GLOBALS['VAR_PREFIXU'] . '_' . strtoupper($name));
133: } elseif (!empty($GLOBALS['xoopsModule']) && @defined(strtoupper($GLOBALS['xoopsModule']->getVar('dirname', 'n') . '_' . $name))) {
134: return constant(strtoupper($GLOBALS['xoopsModule']->getVar('dirname', 'n') . '_' . $name));
135: } elseif (defined(strtoupper($name))) {
136: return constant(strtoupper($name));
137: } else {
138: return str_replace('_', ' ', strtolower($name));
139: }
140: }
141: }
142:
143: /**
144: * Get completed DB prefix if it is defined
145: *
146: * @param string $name string to be completed
147: * @param boolean $isRel relative - do not add XOOPS->DB prefix
148: */
149: if (!function_exists('mod_DB_prefix')) {
150: /**
151: * @param $name
152: * @param bool $isRel
153: *
154: * @return string
155: */
156: function mod_DB_prefix($name, $isRel = false)
157: {
158: $relative_name = $GLOBALS['MOD_DB_PREFIX'] . '_' . $name;
159: if ($isRel) {
160: return $relative_name;
161: }
162:
163: return $GLOBALS['xoopsDB']->prefix($relative_name);
164: }
165: }
166:
167: /**
168: * Display contents of a variable, an array or an object or an array of objects
169: *
170: * @param mixed $message variable/array/object
171: */
172: if (!function_exists('xoops_message')):
173: /**
174: * @param $message
175: * @param int $userlevel
176: */
177: function xoops_message($message, $userlevel = 0)
178: {
179: global $xoopsUser;
180:
181: $level = 1;
182: if (!$xoopsUser) {
183: $level = 0;
184: } elseif ($xoopsUser->isAdmin()) {
185: $level = 99;
186: }
187: if ($userlevel > $level) {
188: return null;
189: }
190:
191: echo "<div style=\"clear:both;\"> </div>";
192: if (is_array($message) || is_object($message)) {
193: echo '<div><pre>';
194: print_r($message);
195: echo '</pre></div>';
196: } else {
197: echo "<div>{$message}</div>";
198: }
199: echo "<div style=\"clear:both;\"> </div>";
200: }
201: endif;
202: /**
203: * @param $message
204: *
205: * @return bool
206: */
207: function mod_message($message)
208: {
209: global $xoopsModuleConfig;
210: if (!empty($xoopsModuleConfig['do_debug'])) {
211: if (is_array($message) || is_object($message)) {
212: echo '<div><pre>';
213: print_r($message);
214: echo '</pre></div>';
215: } else {
216: echo "<div>$message</div>";
217: }
218: }
219:
220: return true;
221: }
222:
223: /**
224: * Get dirname of a module according to current path
225: *
226: * @param string $current_path path to where the function is called
227: * @return string $dirname
228: */
229: function mod_getDirname($current_path = null)
230: {
231: if (DIRECTORY_SEPARATOR !== '/') {
232: $current_path = str_replace(strpos($current_path, '\\\\', 2) ? '\\\\' : DIRECTORY_SEPARATOR, '/', $current_path);
233: }
234: $url_arr = explode('/', strstr($current_path, '/modules/'));
235:
236: return $url_arr[2];
237: }
238:
239: /**
240: * Is a module being installed, updated or uninstalled
241: * Used for setting module configuration default values or options
242: *
243: * The function should be in functions.admin.php, however it requires extra inclusion in xoops_version.php if so
244: *
245: * @param string $dirname dirname of current module
246: * @return bool
247: */
248: function mod_isModuleAction($dirname = 'system')
249: {
250: $ret = @(// action module "system"
251: !empty($GLOBALS['xoopsModule']) && 'system' === $GLOBALS['xoopsModule']->getVar('dirname', 'n') && // current dirname
252: ($dirname == $_POST['dirname'] || $dirname == $_POST['module']) && // current op
253: ('update_ok' === $_POST['op'] || 'install_ok' === $_POST['op'] || 'uninstall_ok' === $_POST['op']) && // current action
254: 'modulesadmin' === $_POST['fct']);
255:
256: return $ret;
257: }
258:
259: endif;
260: