1
|
|
|
<?php |
2
|
|
|
class ar_pinp extends arBase { |
3
|
|
|
|
4
|
|
|
private static $allowed; |
5
|
|
|
|
6
|
2 |
|
public static function allow($class, $methods = null) { |
7
|
2 |
|
if (isset($methods)) { |
8
|
2 |
|
self::$allowed[$class]['methods'] = array_combine( $methods, array_fill( 0, count($methods), true ) ); |
9
|
2 |
|
if (func_num_args()>2) { |
10
|
|
|
$args = array_slice(func_get_args(), 2); |
11
|
|
|
self::$allowed[$class]['implements'] = $args; |
12
|
|
|
} |
13
|
2 |
|
} else { |
14
|
1 |
|
self::$allowed[$class]['methods'] = true; |
15
|
|
|
} |
16
|
2 |
|
} |
17
|
|
|
|
18
|
|
|
public static function allowMatch($class, $methods) { |
19
|
|
|
self::$allowed[$class]['matches'] = array_combine( $methods, array_fill( 0, count($methods), true ) ); |
20
|
|
|
} |
21
|
|
|
|
22
|
3 |
|
public static function isAllowed($class, $method) { |
23
|
|
|
// FIXME: support interfaces? |
24
|
3 |
|
if (!is_string($class)) { |
25
|
3 |
|
$class = get_class($class); |
26
|
3 |
|
} |
27
|
3 |
|
$current = $class; |
28
|
|
|
do { |
29
|
3 |
|
if (isset(self::$allowed[$current]) ) { |
30
|
3 |
|
if (self::$allowed[$current]['methods']===true) { |
31
|
3 |
|
return true; |
32
|
1 |
|
} else if (isset(self::$allowed[$current]['methods'][$method]) |
33
|
1 |
|
&& self::$allowed[$current]['methods'][$method]) { |
34
|
1 |
|
return true; |
35
|
|
|
} else if (isset(self::$allowed[$current]['matches'])) { |
36
|
|
|
$result = preg_match( '/(' . implode ( '|', self::$allowed[$current]['matches']) . ')/is', $method ); |
37
|
|
|
if ($result) { |
38
|
|
|
return true; |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
} while ($current = get_parent_class($current)); |
43
|
|
|
return false; |
44
|
|
|
} |
45
|
|
|
|
46
|
1 |
|
public static function getCallback( $method, $params = array() ) { |
47
|
1 |
|
if (is_string($method)) { |
48
|
1 |
|
return function() use ( $params, $method ) { |
49
|
|
|
$me = ar::context()->getObject(); |
|
|
|
|
50
|
|
|
$args_in = func_get_args(); |
51
|
|
|
if (count($params)) { |
52
|
|
|
$args = array(); |
53
|
|
|
foreach($params as $key => $arg) { |
54
|
|
|
$args[$arg] = $args_in[$key]; |
55
|
|
|
} |
56
|
|
|
} else { |
57
|
|
|
$args = $args_in; |
58
|
|
|
} |
59
|
|
|
return ar::call($method, $args); |
60
|
1 |
|
}; |
61
|
|
|
} else { |
62
|
|
|
return false; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public static function load( $name, $library ) { |
67
|
|
|
$context = ar::context()->getObject(); |
68
|
|
|
return $context->loadLibrary( $name, $library ); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public static function loaded( $name = null, $library = null ) { |
72
|
|
|
global $ARConfig; |
73
|
|
|
// return if a library with that name is loaded |
74
|
|
|
// if library is set will also check if it is the same path |
75
|
|
|
$context = ar::context()->getObject(); |
76
|
|
|
$path = $context->path; |
77
|
|
|
$config = $ARConfig->cache[$path] ?: $context->loadConfig($path); |
78
|
|
|
if ( isset($name) ) { |
79
|
|
|
$libraryPath = $config->libraries[$name] ?: null; |
80
|
|
|
if ( isset($library) ) { |
81
|
|
|
return ($libraryPath == $library ); |
82
|
|
|
} else { |
83
|
|
|
return $libraryPath; |
84
|
|
|
} |
85
|
|
|
} else { |
86
|
|
|
return $config->libraries; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public static function exists( $template ) { |
91
|
|
|
// will check if a template with the given name is available to call |
92
|
|
|
// template has format "library:type::function" |
93
|
|
|
// FIXME: allow search for specific nls as well |
94
|
|
|
$context = ar::context()->getObject(); |
95
|
|
|
$templateData = $context->getPinpTemplate($template); |
96
|
|
|
if ( !$templateData || !$templateData['arTemplateId'] ) { |
97
|
|
|
return false; |
98
|
|
|
} else { |
99
|
|
|
return $templateData; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
ar_pinp::allow('ar_pinp', array('isAllowed', 'getCallback', 'load', 'loaded', 'exists')); |
105
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.