G_false reference and all explicit pass by references removed
[brisk.git] / web / Obj / auth.phh
1 <?php
2   /*
3    *  brisk - auth.phh
4    *
5    *  Copyright (C) 2006-2011 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/dbase_${G_dbasetype}.phh");
26
27 define(CHAL_SHM_DIMS_MIN, 16384);
28 define(CHAL_SHM_DIMS_MAX, 65536);
29 define(CHAL_SHM_DIMS_DLT, 16384);
30 define(CHAL_VALID_TIME,      15);
31 define(CHAL_GARBAGE_TIMEOUT,  5);
32
33
34 class Challenge {
35     var $login;
36     var $token;
37     var $ip;
38     var $tstamp;
39     
40     function Challenge($login, $token, $ip, $tstamp)
41     {
42         $this->login  = $login;
43         $this->token  = $token;
44         $this->ip     = $ip;
45         $this->tstamp = $tstamp + CHAL_VALID_TIME;
46     }
47 }
48
49 class Challenges {
50     var $item;
51     var $item_n;
52     var $mod;
53     var $shm_sz;
54     
55     var $garbage_timeout;
56     
57     
58     function Challenges()
59     {
60         $this->item = array();
61         $this->item_n = 0;
62         $this->garbage_timeout = 0;
63         $this->mod = FALSE;
64     }
65     
66     function add($login, $token, $ip, $tstamp) 
67     {
68         $chal = null;
69         
70         log_auth("xxx", sprintf("Challenges::add [%s]\n", $login));
71         // FIXME Checks here
72         if ($login == '') {
73             return (FALSE);
74         }
75         
76         // log_auth("xxx", "LOOPI tstamp: ".$this->item[$i]->tstamp."  curtime: ".$curtime);
77         
78         if (($chal = new Challenge($login, $token, $ip, $tstamp)) == FALSE) {
79             return (FALSE);
80         }
81         
82         $this->item[$this->item_n] = $chal;
83         $this->item_n++;
84
85         $this->mod = TRUE;
86
87         return ($chal);
88     }
89
90
91     /* remove all istances related to $login */
92
93     function rem($login)
94     {
95         $ismod  = FALSE;
96
97         for ($i = 0 ; $i < $this->item_n ; $i++) {
98             if ($this->item[$i]->login == $login) {
99                 $ismod = TRUE;
100                 for ($e = $i ; $e  < ($this->item_n - 1) ; $e++) {
101                     $this->item[$e] = $this->item[$e + 1];
102                 }
103         
104                 $i--;
105                 $this->item_n--;
106                 unset($this->item[$this->item_n]);
107                 $this->mod = TRUE;
108             }
109         }
110
111         return ($ismod);
112     }
113
114     function garbage_manager()
115     {
116         $curtime = time();
117
118         // FIXME remove set to 0
119         $this->garbage_timeout = 0;
120         if ($this->garbage_timeout > $curtime)
121             return (FALSE);
122
123         $ismod = FALSE;
124     
125         for ($i = 0 ; $i < $this->item_n ; $i++) {
126             log_auth("xxx", "LOOPI item: ".$i." tstamp: ".$this->item[$i]->tstamp."  curtime: ".$curtime);
127             if ($this->item[$i]->tstamp < $curtime) {
128                 for ($e = $i ; $e  < ($this->item_n - 1) ; $e++) {
129                     $this->item[$e] = $this->item[$e + 1];
130                 }
131         
132                 $i--;
133                 $this->item_n--;
134                 log_auth("xxx", "LOOPI unset: ".$this->item_n);
135                 unset($this->item[$this->item_n]);
136                 $ismod = TRUE;
137                 $this->mod = TRUE;
138             }
139         }
140     
141         log_auth("xxx", "LOOPI AFTER: ".count($this->item)." _n:" .$this->item_n );
142     
143         $this->garbage_timeout = $curtime + CHAL_GARBAGE_TIMEOUT;
144     
145         return ($ismod);
146     }
147   
148     function ismod()
149     {
150         return ($this->mod);
151     }
152
153     // Static functions
154     static function create()
155         {
156             $chal =& new Challenges();
157     
158             $chal->mod = TRUE;
159
160             return $chal;
161         }
162
163     function load_data() 
164         {
165             GLOBAL $sess;
166             $doexit = FALSE;
167             do {
168                 if (($tok = @ftok(FTOK_PATH."/challenges", "B")) == -1) {
169                     log_main("ftok failed");
170                     $doexit = TRUE;
171                     break;
172                 }
173     
174                 if (($shm_sz = sharedmem_sz($tok)) == -1) {
175                     log_main("shmop_open failed");
176                 }
177         
178                 if ($shm_sz == -1)
179                     $shm_sz = CHAL_SHM_DIMS_MIN;
180
181                 if ($shm = shm_attach($tok, $shm_sz)) {
182                     $chals = @shm_get_var($shm, $tok);
183         
184                     log_only("challenges ==  ".($chals == FALSE ?   "FALSE" : "TRUE")."  challenges ===  ".($chals === FALSE ? "FALSE" : "TRUE")."  challenges isset ".(isset($chals) ?   "TRUE" : "FALSE"));
185         
186                     if ($chals == FALSE) {
187                         log_only("INIT CHALLENGES DATA");
188           
189                         $chals =& Challenges::create();
190                         if (@shm_put_var($shm, $tok, $chals) == FALSE) {
191                             log_only("PUT_VAR FALLITA ".strlen(serialize($chals)));
192                             log_only(serialize($chals));
193                         }
194                     }
195                     $chals->shm_sz = $shm_sz;
196         
197                     shm_detach($shm);
198                 }
199
200                 $chals->garbage_manager();
201
202                 return ($chals);
203             } while (0);
204     
205             if ($doexit)
206                 exit();
207     
208             return (FALSE);
209         }
210   
211
212     function save_data($chals) 
213     {
214         $shm =   FALSE;
215         $oldmod = $chals->mod;
216
217         if (($tok = @ftok(FTOK_PATH."/challenges", "B")) == -1) 
218             return (FALSE);
219     
220         while ($chals->shm_sz < CHAL_SHM_DIMS_MAX) {
221             if (($shm = shm_attach($tok, $chals->shm_sz)) == FALSE)
222                 break;
223       
224             if (isset($chals)) 
225                 log_only("challenges count ".count($chals->item)."  _n: ".$chals->item_n);
226
227             $chals->mod = FALSE;
228             if (shm_put_var($shm, $tok, $chals) != FALSE) {
229                 shm_detach($shm);
230                 return (TRUE);
231             }
232             $chals->mod = $oldmod;
233
234             if (shm_remove($shm) === FALSE) {
235                 log_only("REMOVE FALLITA");
236                 break;
237             }
238             shm_detach($shm);
239             $chals->shm_sz += CHAL_SHM_DIMS_DLT;
240         } 
241
242         if ($shm)
243             shm_detach($shm);
244     
245         return (FALSE);
246     }
247
248     function lock_data()
249     {
250         if (($tok = @ftok(FTOK_PATH."/challenges", "B")) == -1) {
251             return (FALSE);
252         }
253         // echo "FTOK ".$tok."<br>";
254         if (($res = sem_get($tok)) == FALSE) {
255             return (FALSE);
256         }
257         if (sem_acquire($res)) {   
258             log_lock("LOCK challenges");
259             return ($res);
260         }
261         else
262             return (FALSE);
263     }
264   
265     function unlock_data($res)
266     {
267         GLOBAL $sess; 
268     
269         log_lock("UNLOCK challenges");
270
271         return (sem_release($res));
272     }
273 } // End CLASS Challenges
274
275 ?>