| 1 |
<?php if (! defined('ABSPATH')) exit; // Exit if accessed directly |
| 2 |
interface HC3_Acl_ |
| 3 |
{ |
| 4 |
public function register( $slug, $handler ); |
| 5 |
public function getHandlerArgs( $slug ); |
| 6 |
public function check( $slug, $params = array() ); |
| 7 |
} |
| 8 |
|
| 9 |
class HC3_Acl implements HC3_Acl_ |
| 10 |
{ |
| 11 |
public $dic; |
| 12 |
protected $handlers = array(); |
| 13 |
|
| 14 |
public function __construct( HC3_Dic $dic ) |
| 15 |
{ |
| 16 |
// echo "INIT ACL!"; |
| 17 |
$this->dic = $dic; |
| 18 |
} |
| 19 |
|
| 20 |
public function check( $slug, $params = array() ) |
| 21 |
{ |
| 22 |
$return = TRUE; |
| 23 |
|
| 24 |
list( $handler, $args ) = $this->getHandlerArgs( $slug ); |
| 25 |
list( $handler, $method ) = is_array($handler) ? $handler : array( $handler, 'check' ); |
| 26 |
|
| 27 |
if( $handler ){ |
| 28 |
$handler = $this->dic |
| 29 |
->make( $handler ) |
| 30 |
; |
| 31 |
|
| 32 |
if( $params ){ |
| 33 |
$args[] = $params; |
| 34 |
} |
| 35 |
|
| 36 |
$checkResult = call_user_func_array( array($handler, $method), $args ); |
| 37 |
if( $checkResult === FALSE ){ |
| 38 |
if( defined('HC3_DEV_INSTALL') ){ |
| 39 |
// echo "ACL NOT ALLOWING: " . $handler->_getClass() . '->' . $method . '()' . '<br>'; |
| 40 |
} |
| 41 |
$return = FALSE; |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
return $return; |
| 46 |
} |
| 47 |
|
| 48 |
public function register( $slug, $handler ) |
| 49 |
{ |
| 50 |
$this->handlers[$slug] = $handler; |
| 51 |
return $this; |
| 52 |
} |
| 53 |
|
| 54 |
public function getHandlerArgs( $slug ) |
| 55 |
{ |
| 56 |
// echo "GETTING FOR '$slug'<br><br>"; |
| 57 |
// print_r( array_keys($this->handlers) ); |
| 58 |
list( $handler, $args ) = $this->_findHandlerArgs( $slug, $this->handlers ); |
| 59 |
$return = array( $handler, $args ); |
| 60 |
return $return; |
| 61 |
} |
| 62 |
|
| 63 |
protected function _findHandlerArgs( $slug, $array ) |
| 64 |
{ |
| 65 |
$handler = NULL; |
| 66 |
$args = array(); |
| 67 |
|
| 68 |
// exact match |
| 69 |
if( isset($array[$slug]) ){ |
| 70 |
$handler = $array[$slug]; |
| 71 |
} |
| 72 |
// wildcards |
| 73 |
else { |
| 74 |
// if we have wildcards |
| 75 |
$config_keys = array_keys($array); |
| 76 |
|
| 77 |
if( strpos($slug, ':') !== FALSE ){ |
| 78 |
$slug = str_replace(':', '/', $slug); |
| 79 |
} |
| 80 |
|
| 81 |
$sluga = explode('/', $slug); |
| 82 |
$count_sluga = count($sluga); |
| 83 |
// echo "SLUG: '$slug'<br>"; |
| 84 |
// _print_r( $sluga ); |
| 85 |
|
| 86 |
$parametered_keys = array(); |
| 87 |
foreach( $config_keys as $k ){ |
| 88 |
if( strpos($k, '{') === FALSE ){ |
| 89 |
continue; |
| 90 |
} |
| 91 |
$parametered_keys[] = $k; |
| 92 |
} |
| 93 |
|
| 94 |
reset( $parametered_keys ); |
| 95 |
foreach( $parametered_keys as $k ){ |
| 96 |
$kk = $k; |
| 97 |
if( strpos($kk, ':') !== FALSE ){ |
| 98 |
$kk = str_replace(':', '/', $kk); |
| 99 |
} |
| 100 |
$ka = explode('/', $kk); |
| 101 |
|
| 102 |
// check if this one matches |
| 103 |
if( count($ka) != $count_sluga ){ |
| 104 |
continue; |
| 105 |
} |
| 106 |
|
| 107 |
$match = TRUE; |
| 108 |
$parametered_args = array(); |
| 109 |
for( $ii = 0; $ii < $count_sluga; $ii++ ){ |
| 110 |
if( strpos($ka[$ii], '{') !== FALSE ){ |
| 111 |
$parametered_args[] = $sluga[$ii]; |
| 112 |
} |
| 113 |
else { |
| 114 |
if( $ka[$ii] != $sluga[$ii] ){ |
| 115 |
$match = FALSE; |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
if( $match ){ |
| 121 |
$handler = $array[$k]; |
| 122 |
foreach( $parametered_args as $parametered_arg ){ |
| 123 |
$args[] = $parametered_arg; |
| 124 |
} |
| 125 |
break; |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
$return = array( $handler, $args ); |
| 131 |
return $return; |
| 132 |
} |
| 133 |
} |