Upgrade phpCAS
[piwik-CASLogin.git] / CAS / CAS / ProxyChain.php
1 <?php
2
3 /**
4  * Licensed to Jasig under one or more contributor license
5  * agreements. See the NOTICE file distributed with this work for
6  * additional information regarding copyright ownership.
7  *
8  * Jasig licenses this file to you under the Apache License,
9  * Version 2.0 (the "License"); you may not use this file except in
10  * compliance with the License. You may obtain a copy of the License at:
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  * PHP Version 5
21  *
22  * @file     CAS/ProxyChain.php
23  * @category Authentication
24  * @package  PhpCAS
25  * @author   Adam Franco <afranco@middlebury.edu>
26  * @license  http://www.apache.org/licenses/LICENSE-2.0  Apache License 2.0
27  * @link     https://wiki.jasig.org/display/CASC/phpCAS
28  */
29
30 /**
31  * A normal proxy-chain definition that lists each level of the chain as either
32  * a string or regular expression.
33  *
34  * @class    CAS_ProxyChain
35  * @category Authentication
36  * @package  PhpCAS
37  * @author   Adam Franco <afranco@middlebury.edu>
38  * @license  http://www.apache.org/licenses/LICENSE-2.0  Apache License 2.0
39  * @link     https://wiki.jasig.org/display/CASC/phpCAS
40  */
41
42 class CAS_ProxyChain
43 implements CAS_ProxyChain_Interface
44 {
45
46     protected $chain = array();
47
48     /**
49      * A chain is an array of strings or regexp strings that will be matched
50      * against. Regexp will be matched with preg_match and strings will be
51      * matched from the beginning. A string must fully match the beginning of
52      * an proxy url. So you can define a full domain as acceptable or go further
53      * down.
54      * Proxies have to be defined in reverse from the service to the user. If a
55      * user hits service A get proxied via B to service C the list of acceptable
56      * proxies on C would be array(B,A);
57      *
58      * @param array $chain A chain of proxies
59      */
60     public function __construct(array $chain)
61     {
62         $this->chain = array_values($chain);    // Ensure that we have an indexed array
63     }
64
65     /**
66      * Match a list of proxies.
67      *
68      * @param array $list The list of proxies in front of this service.
69      *
70      * @return bool
71      */
72     public function matches(array $list)
73     {
74         $list = array_values($list);  // Ensure that we have an indexed array
75         if ($this->isSizeValid($list)) {
76             $mismatch = false;
77             foreach ($this->chain as $i => $search) {
78                 $proxy_url = $list[$i];
79                 if (preg_match('/^\/.*\/[ixASUXu]*$/s', $search)) {
80                     if (preg_match($search, $proxy_url)) {
81                         phpCAS::trace("Found regexp " .  $search . " matching " . $proxy_url);
82                     } else {
83                         phpCAS::trace("No regexp match " .  $search . " != " . $proxy_url);
84                         $mismatch = true;
85                         break;
86                     }
87                 } else {
88                     if (strncasecmp($search, $proxy_url, strlen($search)) == 0) {
89                         phpCAS::trace("Found string " .  $search . " matching " . $proxy_url);
90                     } else {
91                         phpCAS::trace("No match " .  $search . " != " . $proxy_url);
92                         $mismatch = true;
93                         break;
94                     }
95                 }
96             }
97             if (!$mismatch) {
98                 phpCAS::trace("Proxy chain matches");
99                 return true;
100             }
101         } else {
102             phpCAS::trace("Proxy chain skipped: size mismatch");
103         }
104         return false;
105     }
106
107     /**
108      * Validate the size of the the list as compared to our chain.
109      *
110      * @param array $list List of proxies
111      *
112      * @return bool
113      */
114     protected function isSizeValid (array $list)
115     {
116         return (sizeof($this->chain) == sizeof($list));
117     }
118 }