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 Xmf;
13:
14: /**
15: * Loader
16: *
17: * @category Xmf\Module\Loader
18: * @package Xmf
19: * @author trabis <lusopoemas@gmail.com>
20: * @copyright 2011-2013 XOOPS Project (http://xoops.org)
21: * @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
22: * @version Release: 1.0
23: * @link http://xoops.org
24: * @since 1.0
25: */
26: class Loader
27: {
28: /**
29: * Load a file
30: *
31: * @param string $file filename to load
32: * @param bool $once true to use include_once
33: *
34: * @return bool true if file exists and was loaded
35: */
36: public static function loadFile($file, $once = true)
37: {
38: self::securityCheck($file);
39: if (file_exists($file)) {
40: if ($once) {
41: include_once $file;
42: } else {
43: include $file;
44: }
45:
46: return true;
47: }
48:
49: return false;
50: }
51:
52: /**
53: * Load a class file, part of old autoloader
54: *
55: * @param string $class name of class
56: *
57: * @return bool if class exists
58: */
59: public static function loadClass($class)
60: {
61: if (class_exists($class, false) || interface_exists($class, false)) {
62: return true;
63: }
64:
65: $file = str_replace('_', DIRECTORY_SEPARATOR, $class) . '.php';
66: if (!self::loadFile(dirname(__DIR__) . DIRECTORY_SEPARATOR . $file)) {
67: return false;
68: }
69:
70: if (!class_exists($class, false) && !interface_exists($class, false)) {
71: trigger_error(
72: "File \"$file\" does not exist or class \"$class\" was not found in the file",
73: E_USER_WARNING
74: );
75:
76: return false;
77: }
78:
79: return true;
80: }
81:
82: /**
83: * Ensure that filename does not contain exploits
84: *
85: * exit() if name does not pass check
86: *
87: * @param string $filename name of file to check
88: *
89: * @return void
90: */
91: protected static function securityCheck($filename)
92: {
93: /**
94: * Security check
95: */
96: if (preg_match('/[^a-z0-9\\/\\\\_.:-]/i', $filename)) {
97: exit('Security check: Illegal character in filename');
98: }
99: }
100: }
101: