Open Sencillo  2015.107
Long live the simplicity of PHP
 All Data Structures Namespaces Functions Pages
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()
17  {
18  parent::__construct(database::host,database::name,database::user,database::pass);
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  $this->log['database']=array('host'=>database::host,
31  'name'=>database::name,
32  'user'=>database::user,
33  'pass'=>database::pass);
34  $this->install();
35  }
36 
43  final public function install()
44  {
45  try
46  {
47  $this->newColumn("sign","TEXT");
48  $this->newColumn("active","INT(1)");
49  $this->newColumn("login","VARCHAR(255)");
50  $this->newColumn("pass","VARCHAR(255)");
51  $this->newColumn("email","VARCHAR(255)");
52  $this->newColumn("fname","VARCHAR(255)");
53  $this->newColumn("lname","VARCHAR(255)");
54  $this->newColumn("perm","INT(4)");
55  $this->newColumn("ip","VARCHAR(20)");
56  $this->newColumn("agent","TEXT");
57  $this->newColumn("date","VARCHAR(20)");
58  $this->newColumn("time","VARCHAR(20)");
59  $this->createTable("users");
60  $email=$this->output("`function`='superemail'","`id` ASC",1);
61  $name=$this->output("`function`='superuser'","`id` ASC",1);
62  $pass=$this->output("`function`='superpass'","`id` ASC",1);
63  $this->createSuperUser($email['line'][0][0],$name['line'][0][0],$pass['line'][0][0]);
64  return true;
65  }
66  catch(Exception $e)
67  {
68  return false;
69  }
70  }
71 
80  final public function editPerm($login=null,$perm=null)
81  {
82  if(isset($login))
83  {
84  if((is_numeric($perm))&&($perm<=1111))
85  {
86  $this->set("perm",$perm);
87  $this->update("`login`=".$this->log['user']);
88  unset($this->log['perm']);
89  $this->log['perm']=$perm;
90  return $this->log['perm'];
91  }
92  else
93  {
94  return false;
95  }
96  }
97  else
98  {
99  $this->set("perm",$perm);
100  $this->update("`login`=".$login);
101  }
102  }
103 
109  final public function getPerm()
110  {
111  return $this->log['perm'];
112  }
113 
121  final public function createSuperUser($email,$name,$pass)
122  {
123  $this->openTable('users');
124  if(filter_var($email,FILTER_VALIDATE_EMAIL))
125  {
126  $user=$this->output("`login`='".$name."'","`id` ASC",1);
127  if($user['line'][1][0]==null)
128  {
129  try
130  {
131  $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())");
132  $this->status['status']='ok';
133  $this->status['code']=200;
134  }
135  catch(Exception $e)
136  {
137  $this->status['status']='failed';
138  $this->status['code']=417;
139  }
140  }
141  else
142  {
143  $this->status['status']='exist';
144  $this->status['code']=409;
145  }
146  }
147  else
148  {
149  $this->status['status']='invalid';
150  $this->status['code']=403;
151  }
152  return $this->status;
153  }
154 
162  final public function ereg()
163  {
164  $this->openTable('users');
165  if(filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
166  {
167  $user=$this->output("`login`='".$_POST['email']."'","`id` ASC",1);
168  if($user['line'][1][0]==null)
169  {
170  try
171  {
172  $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())");
173  $this->status['status']='ok';
174  $this->status['code']=200;
175  }
176  catch(Exception $e)
177  {
178  $this->status['status']='failed';
179  $this->status['code']=417;
180  }
181  }
182  else
183  {
184  $this->status['status']='exist';
185  $this->status['code']=409;
186  }
187  }
188  else
189  {
190  $this->status['status']='invalid';
191  $this->status['code']=403;
192  }
193  return $this->status;
194  }
195 
203  final public function login($ajax)
204  {
205  $this->openTable('users');
206  if(filter_var($ajax['email'],FILTER_VALIDATE_EMAIL))
207  {
208  if($this->output("`login`='".strtolower($ajax['email'])."' AND `pass`=MD5('".$ajax['pass']."')","`id` ASC",1)!=false)
209  {
210  $this->status['status']='authorized';
211  $this->status['code']=202;
212  $this->status['user']=$this->output("`login`='".strtolower($ajax['email'])."' AND `pass`=MD5('".$ajax['pass']."')","`id` ASC",1);
213 
214  $this->addSessionData('userid',$this->status['user']['line'][1][0]);
215  $this->addSessionData('login',$this->status['user']['line'][1][3]);
216  $this->addSessionData('email',$this->status['user']['line'][1][5]);
217  $this->addSessionData('perm',$this->status['user']['line'][1][8]);
218  $this->addSessionData('sessionid',$this->log['session']['id']);
219  $this->addSessionData('start',date('Y-m-d H:i:s'));
220  if($this->status['user']['line'][1][1]=='first_use')
221  {
222  $this->addSessionData('tutorial',true);
223  }
224  else
225  {
226  $this->addSessionData('tutorial',false);
227  }
228  $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']."'");
229 
230  unset($this->status['user']['line']);
231  }
232  else
233  {
234  $this->status['status']='unauthorized';
235  $this->status['code']=404;
236  }
237  }
238  else
239  {
240  $this->status['status']='invalid';
241  $this->status['code']=403;
242  }
243  return $this->status;
244  }
245 
252  final public function checkSession($signal=false)
253  {
254  $this->openTable('users');
255  $browser = ($this->getSessionData('sessionid') ? array("code"=>200) : $this->login($_POST));
256  $server = $this->output("`id`=".$this->getSessionData('userid'));
257 
258  if(!$signal)
259  {
260  return (($server['line'][1][1]===$this->getSessionData('sessionid'))&&($browser["code"]<300)&&($server['line'][1][8]===$this->getSessionData('perm')) ? true : false);
261  }
262  else
263  {
264  return (($server['line'][1][1]===$this->getSessionData('sessionid'))&&($browser["code"]<300)&&($server['line'][1][8]===$this->getSessionData('perm')) ? $browser : array("code"=>404));
265  }
266  }
267 
274  public function basicLogin($translate,$seo)
275  {
276  $this->createSession();
277  if((is_object($translate))&&(is_object($seo)))
278  {
279  switch($_GET['p'])
280  {
281  case 'logout':
282  $this->destroySession();
283  case '':
284  define('LOGIN_ERRMSG',"000:".$_SESSION['sessionid']);
285  define('LOGIN_ACTION','/login');
286  echo $seo->save();
287  require_once 'fw_templates/login.default.screen.php';
288  break;
289  case 'login':
290  $status = $this->checkSession(true);
291  $seo->custom('<script type="text/javascript">console.log("Login status:'.$status["code"].'");</script>');
292  switch($status['code'])
293  {
294  case 200:
295  case 202:
296  //login success
297  define('LOGIN_ERRMSG',$status['code'].":".$_SESSION['sessionid'].":ok:user:".$this->getSessionData('userid'));
298  echo $seo->save();
299  require_once 'fw_templates/account.dafault.screen.php';
300  break;
301  default:
302  //login failed
303  $this->destroySession();
304  define('LOGIN_ERRMSG',$status['code'].":".$_SESSION['sessionid'].":failed");
305  define('LOGIN_ACTION','/login');
306  echo $seo->save();
307  require_once 'fw_templates/login.default.screen.php';
308  }
309  break;
310  case 'ereg':
311  case 'registration':
312  //ereg
313  $this->destroySession();
314  define('LOGIN_ACTION','/registration');
315  $status = $this->ereg();
316  define('LOGIN_ERRMSG',$status['code'].":ereg");
317  echo $seo->save();
318  require_once 'fw_templates/ereg.default.screen.php';
319  break;
320  }
321  return $status['code'];
322  }
323  else
324  {
325  return 500;
326  }
327  }
328 
335  public function adminLogin($translate,$seo)
336  {
337  $this->createSession();
338  if((is_object($translate))&&(is_object($seo)))
339  {
340  switch($_GET['p'])
341  {
342  case 'logout':
343  $this->destroySession();
344  case 'admin':
345  $status = $this->checkSession(true);
346  $seo->custom('<script type="text/javascript">console.log("Login status:'.$status["code"].'");</script>');
347  switch($status['code'])
348  {
349  case 200:
350  case 202:
351  //login success
352  define('LOGIN_ERRMSG',$status['code'].":".$_SESSION['sessionid'].":ok:user:".$this->getSessionData('userid'));
353  echo $seo->save();
354  require_once 'fw_templates/account.dafault.screen.php';
355  break;
356  default:
357  //login failed
358  $this->destroySession();
359  define('LOGIN_ERRMSG',$status['code'].":".$_SESSION['sessionid'].":failed");
360  define('LOGIN_ACTION','/login');
361  echo $seo->save();
362  require_once 'fw_templates/login.default.screen.php';
363  }
364  break;
365  }
366  return $status['code'];
367  }
368  else
369  {
370  return 500;
371  }
372  }
373 
379  final public function addToMainArray($name,$data)
380  {
381  $this->status[$name]=$data;
382  }
383 
387  final public function ajaxSendJson()
388  {
389  print json_encode($this->status);
390  }
391 
392  final public function addNewUser($pass,$perm)
393  {
394 
395  }
396 
401  final public function createSession()
402  {
403  $this->log['session']=array('exist'=>session_start(),
404  'id'=>hash("sha512",session_id().date("YmdHis")),
405  'date'=>date('Y-m-d'),
406  'time'=>date('H:i:s'));
407  return $this->log['session'];
408  }
409 
413  final public function destroySession()
414  {
415  $this->update('`id`='.$this->getSessionData('userid'),"`sign`=NULL");
416  unset($this->log['session']);
417  session_destroy();
418  }
419 
426  final public function addSessionData($name,$data=null)
427  {
428  $_SESSION[$name]=$data;
429  return $data;
430  }
431 
437  final public function getSessionData($name)
438  {
439  return $_SESSION[$name];
440  }
441 
442  final public function signIn($pass)
443  {
444 
445  }
446 
452  final public function getSignedUser()
453  {
454  return $this->log;
455  }
456 
457  final public function signOut()
458  {
459  //TODO
460  }
461 
467  final public function clean($string)
468  {
469  return preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
470  }
471 }
472 ?>
adminLogin($translate, $seo)
addSessionData($name, $data=null)
createSuperUser($email, $name, $pass)
newColumn($name, $type="INT")
Definition: core_sql.php:144
basicLogin($translate, $seo)
editPerm($login=null, $perm=null)
addToMainArray($name, $data)
openTable($name)
Definition: core_sql.php:182
checkSession($signal=false)
set($column, $value)
Definition: core_sql.php:211