function_core.php函数libfile源码分析
1、$libpath = '/source/'.$folder; 拼接字符串。从代码右边可以看出变量$libpath都是以/source/开头。
2、if(strstr($libname, '/')) strstr查找字符串的首次出现。那么问题来了$libname=’function/debug’,strstr($libname, '/')返回字符串/debug。不是空字符串if条件里都是认为是true。在这里小编不得不吐槽一下,php不严格。
3、list($pre, $name) = explode('/', $libname);$path = "{$libpath}/{$pre}/{$pre}_{$name}"; explode—使用一个字符串分割数组。如$libname=’function/debug’;explode('/', $libname);后返回Array ( [0] => function [1] => debug )。 list—把数组中的值赋给一组变量,这不是真正的函数,而是语言结构。list($pre, $name)结果是:$pre=’function’;$name=’debug’; $path = "{$libpath}/{$pre}/{$pre}_{$name}";$path=’/source//function/function_debug’。
4、else {$path = "{$libpath}/{$libname}";}若$libname=’function’,执行else分支。$path=’/source//function’。
5、preg_match('/^[\w\d\/_]+$/i', $path) ? realpath(DISCUZ_ROOT.$path.'.php') : false; \w匹配包括下划线的任何单词字符,相当于[A-Za-z0-9_]。这个正则表达式重复了。上述正则表达式可以写成/^[\w\/]+$/i,少了\d和_。
6、 DISCUZ_ROOT在class_core.php开头定义。realpath返回规范化的绝对路径名,如文件不存在,返回false。