Open Sencillo  2015.003
Long live the simplicity of PHP
 All Data Structures Namespaces Functions
login.management.logman.php
1 <?php
11 class logMan extends mysqlEdit
12 {
13  protected $log=array();
14  protected $status=array();
15 
16  public function __construct($DBHost,$DBName,$DBUser,$DBPass)
17  {
18  parent::__construct($DBHost,$DBName,$DBUser,$DBPass);
19  $this->log['server']=$_SERVER['SERVER_NAME'];
20  $this->log['request']=$_SERVER['REQUEST_URI'];
21  $this->log['port']=$_SERVER['REMOTE_PORT'];
22  $this->log['agent']=$_SERVER['HTTP_USER_AGENT'];
23  $this->log['referer']=$_SERVER['HTTP_REFERER'];
24  $this->log['external_ip']=$_SERVER['REMOTE_ADDR'];
25  $this->status=array(
26  'called'=>$_POST['atype'],
27  'date'=>date('Y-m-d'),
28  'time'=>date('H:i:s')
29  );
30  if($this->test())
31  {
32  $this->log['database']=array('host'=>$this->DBHost,
33  'name'=>$this->DBName,
34  'user'=>$this->DBUser,
35  'pass'=>$this->DBPass);
36  $this->install();
37  }
38  else
39  {
40  die("<b>login.management.logman: MySQL connection failed!</b>".mysql_error());
41  }
42  }
43 
50  final public function install()
51  {
52  try
53  {
54  $this->newColumn("sign","TEXT");
55  $this->newColumn("active","INT(1)");
56  $this->newColumn("login","VARCHAR(255)");
57  $this->newColumn("pass","VARCHAR(255)");
58  $this->newColumn("email","VARCHAR(255)");
59  $this->newColumn("fname","VARCHAR(255)");
60  $this->newColumn("lname","VARCHAR(255)");
61  $this->newColumn("perm","INT(4)");
62  $this->newColumn("ip","VARCHAR(20)");
63  $this->newColumn("agent","TEXT");
64  $this->newColumn("date","VARCHAR(20)");
65  $this->newColumn("time","VARCHAR(20)");
66  $this->createTable("users");
67  $email=$this->output("`function`='superemail'","`id` ASC",1);
68  $name=$this->output("`function`='superuser'","`id` ASC",1);
69  $pass=$this->output("`function`='superpass'","`id` ASC",1);
70  $this->createSuperUser($email['line'][0][0],$name['line'][0][0],$pass['line'][0][0]);
71  return true;
72  }
73  catch(Exception $e)
74  {
75  return false;
76  }
77  }
78 
87  final public function editPerm($login=null,$perm=null)
88  {
89  if(isset($login))
90  {
91  if((is_numeric($perm))&&($perm<=1111))
92  {
93  $this->set("perm",$perm);
94  $this->update("`login`=".$this->log['user']);
95  unset($this->log['perm']);
96  $this->log['perm']=$perm;
97  return $this->log['perm'];
98  }
99  else
100  {
101  return false;
102  }
103  }
104  else
105  {
106  $this->set("perm",$perm);
107  $this->update("`login`=".$login);
108  }
109  }
110 
116  final public function getPerm()
117  {
118  return $this->log['perm'];
119  }
120 
128  final public function createSuperUser($email,$name,$pass)
129  {
130  $this->openTable('users');
131  if(filter_var($email,FILTER_VALIDATE_EMAIL))
132  {
133  $user=$this->output("`login`='".$name."'","`id` ASC",1);
134  if($user['line'][1][0]==null)
135  {
136  try
137  {
138  $this->insert("'first_use',0,'".strtolower($name)."','".$pass."','".strtolower($email)."','','',1111,'".$this->log['external_ip'].":".$this->log['port']."','".$this->log['agent']."',DATE(NOW()),TIME(NOW())");
139  $this->status['status']='ok';
140  $this->status['code']=200;
141  }
142  catch(Exception $e)
143  {
144  $this->status['status']='failed';
145  $this->status['code']=417;
146  }
147  }
148  else
149  {
150  $this->status['status']='exist';
151  $this->status['code']=409;
152  }
153  }
154  else
155  {
156  $this->status['status']='invalid';
157  $this->status['code']=403;
158  }
159  return $this->status;
160  }
161 
169  final public function ereg()
170  {
171  $this->openTable('users');
172  if(filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
173  {
174  $user=$this->output("`login`='".$_POST['email']."'","`id` ASC",1);
175  if($user['line'][1][0]==null)
176  {
177  try
178  {
179  $this->insert("'first_use',0,'".strtolower($_POST['email'])."',MD5('".$_POST['pass']."'),'".strtolower($_POST['email'])."','".$this->clean(ucwords(strtolower($_POST['fname'])))."','".$this->clean(ucwords(strtolower($_POST['lname'])))."',1000,'".$this->log['external_ip'].":".$this->log['port']."','".$this->log['agent']."',DATE(NOW()),TIME(NOW())");
180  $this->status['status']='ok';
181  $this->status['code']=200;
182  }
183  catch(Exception $e)
184  {
185  $this->status['status']='failed';
186  $this->status['code']=417;
187  }
188  }
189  else
190  {
191  $this->status['status']='exist';
192  $this->status['code']=409;
193  }
194  }
195  else
196  {
197  $this->status['status']='invalid';
198  $this->status['code']=403;
199  }
200  return $this->status;
201  }
202 
210  final public function login($ajax)
211  {
212  $this->openTable('users');
213  if(filter_var($ajax['email'],FILTER_VALIDATE_EMAIL))
214  {
215  if($this->output("`login`='".strtolower($ajax['email'])."' AND `pass`=MD5('".$ajax['pass']."')","`id` ASC",1)!=false)
216  {
217  $this->status['status']='authorized';
218  $this->status['code']=202;
219  $this->status['user']=$this->output("`login`='".strtolower($ajax['email'])."' AND `pass`=MD5('".$ajax['pass']."')","`id` ASC",1);
220 
221  $this->addSessionData('userid',$this->status['user']['line'][1][0]);
222  $this->addSessionData('login',$this->status['user']['line'][1][3]);
223  $this->addSessionData('email',$this->status['user']['line'][1][5]);
224  $this->addSessionData('perm',$this->status['user']['line'][1][8]);
225  $this->addSessionData('sessionid',$this->log['session']['id']);
226  $this->addSessionData('start',date('Y-m-d H:i:s'));
227  if($this->status['user']['line'][1][1]=='first_use')
228  {
229  $this->addSessionData('tutorial',true);
230  }
231  else
232  {
233  $this->addSessionData('tutorial',false);
234  }
235  $this->update('`id`='.$this->status['user']['line'][1][0],"`sign`='".$this->getSessionData('sessionid')."',`ip`='".$this->log['external_ip'].":".$this->log['port']."',`agent`='".$this->log['agent']."',`date`='".$this->status['date']."',`time`='".$this->status['time']."'");
236 
237  unset($this->status['user']['line']);
238  }
239  else
240  {
241  $this->status['status']='unauthorized';
242  $this->status['code']=404;
243  }
244  }
245  else
246  {
247  $this->status['status']='invalid';
248  $this->status['code']=403;
249  }
250  return $this->status;
251  }
252 
259  final public function checkSession($signal=false)
260  {
261  $this->openTable('users');
262  $browser = ($this->getSessionData('sessionid') ? array("code"=>200) : $this->login($_POST));
263  $server = $this->output("`id`=".$this->getSessionData('userid'));
264 
265  if(!$signal)
266  {
267  return (($server['line'][1][1]===$this->getSessionData('sessionid'))&&($browser["code"]<300)&&($server['line'][1][8]===$this->getSessionData('perm')) ? true : false);
268  }
269  else
270  {
271  return (($server['line'][1][1]===$this->getSessionData('sessionid'))&&($browser["code"]<300)&&($server['line'][1][8]===$this->getSessionData('perm')) ? $browser : array("code"=>404));
272  }
273  }
274 
281  public function basicLogin($translate,$seo)
282  {
283  $this->createSession();
284  if((is_object($translate))&&(is_object($seo)))
285  {
286  switch($_GET['p'])
287  {
288  case 'logout':
289  $this->destroySession();
290  case '':
291  define('LOGIN_ERRMSG',"000:".$_SESSION['sessionid']);
292  define('LOGIN_ACTION','/login');
293  echo $seo->save();
294  require_once 'fw_templates/login.default.screen.php';
295  break;
296  case 'login':
297  $status = $this->checkSession(true);
298  $seo->custom('<script type="text/javascript">console.log("Login status:'.$status["code"].'");</script>');
299  switch($status['code'])
300  {
301  case 200:
302  case 202:
303  //login success
304  define('LOGIN_ERRMSG',$status['code'].":".$_SESSION['sessionid'].":ok:user:".$this->getSessionData('userid'));
305  echo $seo->save();
306  require_once 'fw_templates/account.dafault.screen.php';
307  break;
308  default:
309  //login failed
310  $this->destroySession();
311  define('LOGIN_ERRMSG',$status['code'].":".$_SESSION['sessionid'].":failed");
312  define('LOGIN_ACTION','/login');
313  echo $seo->save();
314  require_once 'fw_templates/login.default.screen.php';
315  }
316  break;
317  case 'ereg':
318  case 'registration':
319  //ereg
320  $this->destroySession();
321  define('LOGIN_ACTION','/registration');
322  $status = $this->ereg();
323  define('LOGIN_ERRMSG',$status['code'].":ereg");
324  echo $seo->save();
325  require_once 'fw_templates/ereg.default.screen.php';
326  break;
327  }
328  return $status['code'];
329  }
330  else
331  {
332  return 500;
333  }
334  }
335 
342  public function adminLogin($translate,$seo)
343  {
344  $this->createSession();
345  if((is_object($translate))&&(is_object($seo)))
346  {
347  switch($_GET['p'])
348  {
349  case 'logout':
350  $this->destroySession();
351  case 'admin':
352  $status = $this->checkSession(true);
353  $seo->custom('<script type="text/javascript">console.log("Login status:'.$status["code"].'");</script>');
354  switch($status['code'])
355  {
356  case 200:
357  case 202:
358  //login success
359  define('LOGIN_ERRMSG',$status['code'].":".$_SESSION['sessionid'].":ok:user:".$this->getSessionData('userid'));
360  echo $seo->save();
361  require_once 'fw_templates/account.dafault.screen.php';
362  break;
363  default:
364  //login failed
365  $this->destroySession();
366  define('LOGIN_ERRMSG',$status['code'].":".$_SESSION['sessionid'].":failed");
367  define('LOGIN_ACTION','/login');
368  echo $seo->save();
369  require_once 'fw_templates/login.default.screen.php';
370  }
371  break;
372  }
373  return $status['code'];
374  }
375  else
376  {
377  return 500;
378  }
379  }
380 
386  final public function addToMainArray($name,$data)
387  {
388  $this->status[$name]=$data;
389  }
390 
394  final public function ajaxSendJson()
395  {
396  print json_encode($this->status);
397  }
398 
399  final public function addNewUser($pass,$perm)
400  {
401 
402  }
403 
408  final public function createSession()
409  {
410  $this->log['session']=array('exist'=>session_start(),
411  'id'=>hash("sha512",session_id().date("YmdHis")),
412  'date'=>date('Y-m-d'),
413  'time'=>date('H:i:s'));
414  return $this->log['session'];
415  }
416 
420  final public function destroySession()
421  {
422  $this->update('`id`='.$this->getSessionData('userid'),"`sign`=NULL");
423  unset($this->log['session']);
424  session_destroy();
425  }
426 
433  final public function addSessionData($name,$data=null)
434  {
435  $_SESSION[$name]=$data;
436  return $data;
437  }
438 
444  final public function getSessionData($name)
445  {
446  return $_SESSION[$name];
447  }
448 
449  final public function signIn($pass)
450  {
451 
452  }
453 
459  final public function getSignedUser()
460  {
461  return $this->log;
462  }
463 
464  final public function signOut()
465  {
466  //TODO
467  }
468 
474  final public function clean($string)
475  {
476  return preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
477  }
478 }
479 ?>
adminLogin($translate, $seo)
addSessionData($name, $data=null)
test()
Definition: core_sql.php:76
createSuperUser($email, $name, $pass)
newColumn($name, $type="INT")
Definition: core_sql.php:138
basicLogin($translate, $seo)
editPerm($login=null, $perm=null)
addToMainArray($name, $data)
openTable($name)
Definition: core_sql.php:176
checkSession($signal=false)
set($column, $value)
Definition: core_sql.php:205