OpenSencillo  2015.009
Long live the simplicity of PHP
 All Data Structures Namespaces Files Functions Pages
login.management.logman.php
1 <?php
11 class logMan extends mysqlEdit
12 {
13  protected $log=array();
14  protected $status=array();
15  protected $mysqlInterface;
16 
17  public function __construct()
18  {
19  parent::__construct(database::host,database::name,database::user,database::pass);
20  $this->mysqlInterface = new mysqlInterface;
21  $this->mysqlInterface->config();
22  $this->mysqlInterface->connect();
23 
24  $this->log['server']=$_SERVER['SERVER_NAME'];
25  $this->log['request']=$_SERVER['REQUEST_URI'];
26  $this->log['port']=$_SERVER['REMOTE_PORT'];
27  $this->log['agent']=$_SERVER['HTTP_USER_AGENT'];
28  $this->log['referer']=$_SERVER['HTTP_REFERER'];
29  $this->log['external_ip']=$_SERVER['REMOTE_ADDR'];
30  $this->status=array(
31  'called'=>$_POST['atype'],
32  'date'=>date('Y-m-d'),
33  'time'=>date('H:i:s')
34  );
35  $this->log['database']=array('host'=>database::host,
36  'name'=>database::name,
37  'user'=>database::user,
38  'pass'=>database::pass);
39  $this->install();
40  }
41 
48  final public function install()
49  {
50  try
51  {
52  $this->newColumn("user_id","INT(1)");
53  $this->newColumn("code","VARCHAR(5)");
54  $this->newColumn("param","INT(1)");
55  $this->newColumn("expire","DATETIME");
56  $this->createTable("usersPasswordCodes");
57 
58  $this->newColumn("sign","TEXT");
59  $this->newColumn("active","INT(1)");
60  $this->newColumn("login","VARCHAR(255)");
61  $this->newColumn("pass","VARCHAR(255)");
62  $this->newColumn("email","VARCHAR(255)");
63  $this->newColumn("fname","VARCHAR(255)");
64  $this->newColumn("lname","VARCHAR(255)");
65  $this->newColumn("perm","INT(4)");
66  $this->newColumn("ip","VARCHAR(20)");
67  $this->newColumn("agent","TEXT");
68  $this->newColumn("date","VARCHAR(20)");
69  $this->newColumn("time","VARCHAR(20)");
70  $this->createTable("users");
71  $email=$this->output("`function`='superemail'","`id` ASC",1);
72  $name=$this->output("`function`='superuser'","`id` ASC",1);
73  $pass=$this->output("`function`='superpass'","`id` ASC",1);
74  $this->createSuperUser($email['line'][0][0],$name['line'][0][0],$pass['line'][0][0]);
75  return true;
76  }
77  catch(Exception $e)
78  {
79  return false;
80  }
81  }
82 
91  final public function editPerm($login=null,$perm=null)
92  {
93  if(isset($login))
94  {
95  if((is_numeric($perm))&&($perm<=1111))
96  {
97  $this->set("perm",$perm);
98  $this->update("`login`=".$this->log['user']);
99  unset($this->log['perm']);
100  $this->log['perm']=$perm;
101  return $this->log['perm'];
102  }
103  else
104  {
105  return false;
106  }
107  }
108  else
109  {
110  $this->set("perm",$perm);
111  $this->update("`login`=".$login);
112  }
113  }
114 
120  final public function getPerm()
121  {
122  return $this->log['perm'];
123  }
124 
132  final public function createSuperUser($email,$name,$pass)
133  {
134  $this->openTable('users');
135  if(filter_var($email,FILTER_VALIDATE_EMAIL))
136  {
137  $user=$this->output("`login`='".$name."'","`id` ASC",1);
138  if($user['line'][1][0]==null)
139  {
140  try
141  {
142  $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())");
143  $this->status['status']='ok';
144  $this->status['code']=200;
145  }
146  catch(Exception $e)
147  {
148  $this->status['status']='failed';
149  $this->status['code']=417;
150  }
151  }
152  else
153  {
154  $this->status['status']='exist';
155  $this->status['code']=409;
156  }
157  }
158  else
159  {
160  $this->status['status']='invalid';
161  $this->status['code']=403;
162  }
163  return $this->status;
164  }
165 
174  final public function ereg($onlyCheckUser=false)
175  {
176  $this->openTable('users');
177  if(filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
178  {
179  $user=$this->output("`login`='".$_POST['email']."'","`id` ASC",1);
180  if($user['line'][1][0]==null)
181  {
182  try
183  {
184  if($onlyCheckUser===false)
185  {
186  $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())");
187  $this->status['status']='ok';
188  $this->status['code']=200;
189  }
190  else
191  {
192  $this->status['status']='email not found';
193  $this->status['code']=404;
194  }
195  }
196  catch(Exception $e)
197  {
198  $this->status['status']='failed';
199  $this->status['code']=417;
200  }
201  }
202  else
203  {
204  if($onlyCheckUser===true)
205  {
206  $this->status['user_array']=$user['line'][1];
207  $this->status['status']='exist';
208  $this->status['code']=200;
209  }
210  else
211  {
212  $this->status['status']='exist';
213  $this->status['code']=409;
214  }
215  }
216  }
217  else
218  {
219  $this->status['status']='invalid';
220  $this->status['code']=403;
221  }
222  return $this->status;
223  }
224 
232  final public function login($ajax)
233  {
234  $this->openTable('users');
235  if(filter_var($ajax['email'],FILTER_VALIDATE_EMAIL))
236  {
237  if($this->output("`login`='".strtolower($ajax['email'])."' AND `pass`=MD5('".$ajax['pass']."')","`id` ASC",1)!=false)
238  {
239  $this->status['status']='authorized';
240  $this->status['code']=202;
241  $this->status['user']=$this->output("`login`='".strtolower($ajax['email'])."' AND `pass`=MD5('".$ajax['pass']."')","`id` ASC",1);
242 
243  $this->addSessionData('userid',$this->status['user']['line'][1][0]);
244  $this->addSessionData('login',$this->status['user']['line'][1][3]);
245  $this->addSessionData('email',$this->status['user']['line'][1][5]);
246  $this->addSessionData('perm',$this->status['user']['line'][1][8]);
247  $this->addSessionData('sessionid',$this->log['session']['id']);
248  $this->addSessionData('start',date('Y-m-d H:i:s'));
249  if($this->status['user']['line'][1][1]=='first_use')
250  {
251  $this->addSessionData('tutorial',true);
252  }
253  else
254  {
255  $this->addSessionData('tutorial',false);
256  }
257  $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']."'");
258 
259  unset($this->status['user']['line']);
260  }
261  else
262  {
263  $this->status['status']='unauthorized';
264  $this->status['code']=404;
265  }
266  }
267  else
268  {
269  $this->status['status']='invalid';
270  $this->status['code']=403;
271  }
272  return $this->status;
273  }
274 
281  final public function checkSession($signal=false)
282  {
283  $this->openTable('users');
284  $browser = ($this->getSessionData('sessionid') ? array("code"=>200) : $this->login($_POST));
285  $server = $this->output("`id`=".$this->getSessionData('userid'));
286 
287  if(!$signal)
288  {
289  return (($server['line'][1][1]===$this->getSessionData('sessionid'))&&($browser["code"]<300)&&($server['line'][1][8]===$this->getSessionData('perm')) ? true : false);
290  }
291  else
292  {
293  return (($server['line'][1][1]===$this->getSessionData('sessionid'))&&($browser["code"]<300)&&($server['line'][1][8]===$this->getSessionData('perm')) ? $browser : array("code"=>404));
294  }
295  }
296 
303  public function basicLogin($translate,$seo)
304  {
305  $this->createSession();
306  if((is_object($translate))&&(is_object($seo)))
307  {
308  switch($_GET['p'])
309  {
310  case 'logout':
311  $this->destroySession();
312  case '':
313  define('LOGIN_ERRMSG',"000:".$_SESSION['sessionid']);
314  define('LOGIN_ACTION','/login');
315  echo $seo->save();
316  require_once 'fw_templates/login.default.screen.php';
317  break;
318  case 'login':
319  $status = $this->checkSession(true);
320  $seo->custom('<script type="text/javascript">console.log("Login status:'.$status["code"].'");</script>');
321  switch($status['code'])
322  {
323  case 200:
324  case 202:
325  //login success
326  define('LOGIN_ERRMSG',$status['code'].":".$_SESSION['sessionid'].":ok:user:".$this->getSessionData('userid'));
327  echo $seo->save();
328  require_once 'fw_templates/account.dafault.screen.php';
329  break;
330  default:
331  //login failed
332  $this->destroySession();
333  define('LOGIN_ERRMSG',$status['code'].":".$_SESSION['sessionid'].":failed");
334  define('LOGIN_ACTION','/login');
335  echo $seo->save();
336  require_once 'fw_templates/login.default.screen.php';
337  }
338  break;
339  case 'ereg':
340  case 'registration':
341  //ereg
342  $this->destroySession();
343  define('LOGIN_ACTION','/registration');
344  $status = $this->ereg();
345  define('LOGIN_ERRMSG',$status['code'].":ereg");
346  echo $seo->save();
347  require_once 'fw_templates/ereg.default.screen.php';
348  break;
349  }
350  return $status['code'];
351  }
352  else
353  {
354  return 500;
355  }
356  }
357 
364  public function adminLogin($translate,$seo)
365  {
366  $this->createSession();
367  if((is_object($translate))&&(is_object($seo)))
368  {
369  switch($_GET['p'])
370  {
371  case 'logout':
372  $this->destroySession();
373  case 'admin':
374  $status = $this->checkSession(true);
375  $seo->custom('<script type="text/javascript">console.log("Login status:'.$status["code"].'");</script>');
376  switch($status['code'])
377  {
378  case 200:
379  case 202:
380  //login success
381  define('LOGIN_ERRMSG',$status['code'].":".$_SESSION['sessionid'].":ok:user:".$this->getSessionData('userid'));
382  echo $seo->save();
383  require_once 'fw_templates/account.dafault.screen.php';
384  break;
385  default:
386  //login failed
387  $this->destroySession();
388  define('LOGIN_ERRMSG',$status['code'].":".$_SESSION['sessionid'].":failed");
389  define('LOGIN_ACTION','/login');
390  echo $seo->save();
391  require_once 'fw_templates/login.default.screen.php';
392  }
393  break;
394  }
395  return $status['code'];
396  }
397  else
398  {
399  return 500;
400  }
401  }
402 
408  final public function addToMainArray($name,$data)
409  {
410  $this->status[$name]=$data;
411  }
412 
416  final public function ajaxSendJson()
417  {
418  print json_encode($this->status);
419  }
420 
421  final public function addNewUser($pass,$perm)
422  {
423 
424  }
425 
430  final public function createSession()
431  {
432  $this->log['session']=array('exist'=>session_start(),
433  'id'=>hash("sha512",session_id().date("YmdHis")),
434  'date'=>date('Y-m-d'),
435  'time'=>date('H:i:s'));
436  return $this->log['session'];
437  }
438 
442  final public function destroySession()
443  {
444  $this->update('`id`='.$this->getSessionData('userid'),"`sign`=NULL");
445  unset($this->log['session']);
446  session_destroy();
447  }
448 
455  final public function addSessionData($name,$data=null)
456  {
457  $_SESSION[$name]=$data;
458  return $data;
459  }
460 
466  final public function getSessionData($name)
467  {
468  return $_SESSION[$name];
469  }
470 
475  final public function signIn($pass)
476  {
477  //TODO
478  }
479 
485  final public function getSignedUser()
486  {
487  return $this->log;
488  }
489 
493  final public function signOut()
494  {
495  //TODO
496  }
497 
502  final public function forgot()
503  {
504  $this->status = $this->ereg(true);
505  $this->status['confirm-code'] = $this->clean(substr(hash('crc32b',date('YmdHis')),0,5));
506  $this->mysqlInterface->delete('`user_id`='.$this->status['user_array'][0]);
507  $this->mysqlInterface->delete('`expire`<NOW()');
508  if($this->status['code']===200)
509  {
510  $this->mysqlInterface->insert(array('usersPasswordCodes'=> array('user_id' => $this->status['user_array'][0],
511  'code' => $this->status['confirm-code'],
512  'param' => 0,
513  'expire' => date('Y-m-d H:i:s',strtotime('+1 hour')))),true);
514  $this->mysqlInterface->execute();
515  }
516  return $this->status;
517  }
518 
524  final public function clean($string)
525  {
526  return preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
527  }
528 }
529 ?>