eb4b12e933eb704b2b8f0b14bee4d6a8e042afbe
[brisk.git] / web / Obj / provider_proxy.phh
1 <?php
2 /*
3  *  brisk - Obj/provider_proxy.phh
4  *
5  *  Copyright (C) 2015      Matteo Nastasi
6  *                          mailto: nastasi@alternativeoutput.it
7  *                                  matteo.nastasi@milug.org
8  *                          web: http://www.alternativeoutput.it
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * General Public License for more details. You should have received a
19  * copy of the GNU General Public License along with this program; if
20  * not, write to the Free Software Foundation, Inc, 59 Temple Place -
21  * Suite 330, Boston, MA 02111-1307, USA.
22  *
23  */
24
25 require_once("${G_base}Obj/ipclass.phh");
26
27 class ProviderProxyItem
28 {
29     var $name;
30     var $headitem;
31     var $ipclass;
32
33     function ProviderProxyItem($pp_name, $pp_descr)
34     {
35         $this->name     = $pp_name;
36         $this->headitem = $pp_descr['headitem'];
37         $this->ipclass  = IPClass::create($pp_descr['ipclass']);
38     }
39 }
40
41
42 class ProviderProxy
43 {
44     var $pp;
45
46     function ProviderProxy()
47     {
48         $this->pp = NULL;
49     }
50
51     static function create($pproxy = NULL)
52     {
53         $thiz = new ProviderProxy();
54         
55         if ($pproxy != NULL)
56             $thiz->update($pproxy);
57
58         return ($thiz);
59     }
60
61     function clean()
62     {
63         if ($this->pp != NULL) {
64             foreach ($this->pp as $pp_key => $pp_value) {
65                 fprintf(STDERR, "PHP PPN: %s\n", $pp_key);
66                 unset($this->pp[$pp_key]);
67             }
68             unset($this->pp);
69             $this->pp = NULL;
70         }
71     }
72
73     function update($pproxy)
74     {
75         $this->clean();
76
77         $this->pp = array();
78         foreach ($pproxy as $pp_key => $pp_value) {
79             $this->pp[$pp_key] = new ProviderProxyItem($pp_key, $pp_value);
80         }
81     }
82
83     function realip($header, $ip)
84     {
85         if ($this->pp != NULL) {
86             foreach ($this->pp as $pp_name => $pp_item) {
87                 if ($pp_item->ipclass->check($ip)) {
88                     if (isset($header[$pp_item->headitem])) {
89                         fprintf(STDERR, "Match public proxy [%s][%s]\n", $pp_name, $header[$pp_item->headitem]);
90                         return ($header[$pp_item->headitem]);
91                     }
92                 }
93             }
94         }
95         return ($ip);
96     }
97 }
98
99 ?>