forked from ptejada/uFlex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.uFlex.php
1416 lines (1249 loc) · 39.2 KB
/
class.uFlex.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
Copyright (c) 2013 Pablo Tejada, http://ptejada.com/projects/uFlex/
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/**
* uFlex class file
*
* @author Pablo Tejada
* @package uFlex
*/
/**
* Class uFlex
*/
class uFlex{
/**
* Class Version
* @var int
*/
const version = 0.97;
/**
* @var PDO|array An array of database credentials or a PDO object if
* connected to the database
*/
var $db = array(
"host" => "",
"user" => "",
"pass" => "",
"name" => "", //Database name
"dsn" => "" //Alternative PDO DSN string
);
/**
* Holds a unique clone number of the instance clones
*
* @var int
* @ignore
*/
protected $clone = 0;
/**
* @var int Current user ID
*/
var $id;
/**
* @var string Current User PHP Session ID
*/
var $sid;
/**
* @var string Current user username
*/
var $username;
/**
* @var string Holds the user password hash
*/
var $pass;
/**
* @var bool Flag of weather a user is signed-in or not
*/
var $signed;
/**
* @var array Holds entire user database row as an associative array
*/
var $data;
/**
* @var array Container to hold errors and reports
*/
var $console;
/**
* @var string Pointer for tracking errors and reports in the console
*/
protected $log;
/**
* @var string Holds the hash for any type of confirmation
*/
var $confirm;
/**
* @var array Holds the temporary user information during registration and other methods
*/
var $tmp_data;
/**
* Array of Internal options:
*
* Array of internal options:
* <pre>
* [table_name]: Name of the users table
* [cookie_time]: Autologin cookie lifetime
* [cookie_name]: Autologin cookie name
* [cookie_path]: Autologin cookie path
* [cookie_host]: Autologin cookie host
* [user_session]: $_SESSION index to use
* [default_user]: An associative array with properties of the default array
* </pre>
* @var array
*/
var $opt = array(
"table_name" => "users",
"cookie_time" => "+30 days",
"cookie_name" => "auto",
"cookie_path" => "/",
"cookie_host" => false,
"user_session" => "userData",
"default_user" => array(
"username" => "Guess",
"user_id" => 0,
"password" => 0,
"signed" => false
)
);
/**
* @var array Array for default field validations
*/
var $validations = array(
"username" => array(
"limit" => "3-15",
"regEx" => '/^([a-zA-Z0-9_])+$/'
),
"password" => array(
"limit" => "3-15",
"regEx" => ''
),
"email" => array(
"limit" => "4-45",
"regEx" => '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/'
)
);
/**
* Required for the integer encoder and decoder functions
* @var array
* @access protected
* @ignore
*/
protected $encoder = array(
"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z",
"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",
0,2,3,4,5,6,7,8,9
);
/**
* @var array Array of errors text. Could use overwritten for multilingual support
*/
var $errorList = array(
//Database Error while calling register functions
1 => "New User Registration Failed",
//Database Error while calling update functions
2 => "The Changes Could not be made",
//Database Error while calling activate function
3 => "Account could not be activated",
//When calling pass_reset and the given email doesn't exist in database
4 => "We don't have an account with this email",
//When calling new_pass, the confirmation hash did not match the one in database
5 => "Password could not be changed. The request can't be validated",
6 => "Logging with cookies failed",
7 => "No Username or Password provided",
8 => "Your Account has not been Activated. Check your Email for instructions",
9 => "Your account has been deactivated. Please contact Administrator",
10 => "Wrong Username or Password",
//When calling check_hash with invalid hash
11 => "Confirmation hash is invalid",
//Calling check_hash hash failed database match test
12 => "Your identification could not be confirmed",
//When saving hash to database fails
13 => "Failed to save confirmation request",
14 => "You need to reset your password to login"
);
/**
* Login a user with username|email and password
*
* @access public
* @api
*
* @param string|bool $user username or email
* @param string|bool $pass password in plain text
* @param bool $auto boolean to remember or not the user
*
* @return bool True if user logged in successfully, false otherwise
*/
public function login($user=false,$pass=false,$auto=false){
//reconstruct object
return self::__construct($user,$pass,$auto);
}
/**
* Register A New User
*
* Takes two parameters, the first being required
*
* @access public
* @api
*
* @param array $info An associative array,
* the index being the field name(column in database)
* and the value its content(value)
* @param bool $activation Default is false, if true the user will need required further steps to activate account
* Otherwise the account will be activated if registration succeeds
*
* @return array|bool Returns activation hash if second parameter $activation is true
* Returns true if second parameter $activation is false
* Returns false on Error
*/
public function register($info,$activation = false){
$this->logger("registration"); //Index for Errors and Reports
//Saves Registration Data in Class
$this->tmp_data = $info;
//Validate All Fields
if(!$this->validateAll()) return false; //There are validations error
//Set Registration Date
$info['reg_date'] = $this->tmp_data['reg_date'] = time();
//Built in actions for special fields
//Hash Password
if(isset($info['password'])){
$this->hash_pass($info['password']);
$info['password'] = $this->pass;
}
//Check for Email in database
if(isset($info['email']))
if($this->check_field('email',$info['email'],"This Email is Already in Use"))
return false;
//Check for username in database
if(isset($info['username']))
if($this->check_field('username',$info['username'], "This Username is not available"))
return false;
//Check for errors
if($this->has_error()) return false;
//User Activation
if(!$activation){ //Activates user upon registration
$info['activated'] = 1;
}
//Prepare Info for SQL Insertion
$data = array();
$into = array();
foreach($info as $index => $val){
if(!preg_match("/2$/",$index)){ //Skips double fields
$into[] = $index;
//For the statement
$data[$index] = $val;
}
}
$intoStr = implode(", ",$into);
$values = ":" . implode(", :",$into);
//Prepare New User Query
$sql = "INSERT INTO :table ({$intoStr})
VALUES({$values})";
//Enter New user to Database
if($this->check_sql($sql, $data)){
$this->report("New User has been registered");
$this->id = $this->db->lastInsertId();
if($activation){
//Insert Validation Hash
$this->make_hash($this->id);
$this->save_hash();
return $this->confirm;
}
return true;
}else{
$this->error(1);
return false;
}
}
/**
* Validates and updates any field in the database for the current user
*
* Similar to the register method function in structure,
* this Method validates and updates any field in the database
*
* @access public
* @api
*
* @param array $info An associative array,
* the index being the field name(column in database)
* and the value its content(value)
*
* @return bool Returns true on success anf false on error
*/
public function update($info){
$this->logger("update"); //Index for Errors and Reports
//Saves Updates Data in Class
$this->tmp_data = $info;
//Validate All Fields
if(!$this->validateAll())
return false; //There are validations error
//Built in actions for special fields
//Hash Password
if(isset($info['password'])){
$info['password'] = $this->hash_pass($info['password']);
}
//Check for Email in database
if(isset($info['email']))
if($this->check_field('email',$info['email'],"This Email is Already in Use"))
return false;
//Check for errors
if($this->has_error()) return false;
//Prepare Info for SQL Insertion
$data = array();
$set = array();
foreach($info as $index => $val){
if(!preg_match("/2$/",$index)){ //Skips double fields
$set[] = "{$index}=:{$index}";
//For the statement
$data[$index] = $val;
}
}
$set = implode(", ",$set);
//Prepare User Update Query
$sql = "UPDATE :table SET $set
WHERE user_id={$this->id}";
//Check for Changes
if($this->check_sql($sql, $data)){
$this->report("Information Updated");
if($this->clone == 0)
$_SESSION['uFlex-' . $this->clone . '-update'] = true;
return true;
}else{
$this->error(2);
return false;
}
}
/**
* Adds validation to queue for either the Registration or Update Method
*
* Single Entry:
* <pre>
* Requires the first two parameters
* $name = string (name of the field to be validated)
* $limit = string (range of the accepted value length in the format of "5-10")
* - to make a field optional start with 0 (Ex. "0-10")
*
* Optional third parameter
* $regEx = string (Regular Expression to test the field)
* </pre>
* _____________________________________________________________________________________________________
*
* Multiple Entry:
* <pre>
* Takes only the first argument
* $name = Array Object (takes an object in the following format:
* array(
* "username" => array(
* "limit" => "3-15",
* "regEx" => "/^([a-zA-Z0-9_])+$/"
* ),
* "password" => array(
* "limit" => "3-15",
* "regEx" => false
* )
* );
* </pre>
*
* @access public
* @api
*
* @param string|array $name Name of the field to validate or an array of all the fields and their validations
* @param string $limit A range of the accepted value length in the format of "5-10",
* to make a field optional start with 0 (Ex. "0-10")
* @param string|bool $regEx Regular expression to the test the field with
* @return null
*/
public function addValidation($name,$limit = "0-1",$regEx = false){
$this->logger("registration");
if(is_array($name)){
if(!is_array($this->validations))
$this->validations = array(); //If is not an array yet, make it one
$new = array_merge($this->validations,$name);
$this->validations = $new;
$this->report("New Validation Object added");
}else{
$this->validations[$name]['limit'] = $limit;
$this->validations[$name]['regEx'] = $regEx;
$this->report("The $name field has been added for validation");
}
}
/**
* Activates Account with a hash
*
* Takes Only and Only the URL parameter of a confirmation page
* which would be the hash returned by the register method
*
* @access public
* @api
*
* @param string $hash Hash returned in the register method
* @return bool Returns true account activation and false on failure
*/
public function activate($hash){
$this->logger("activation");
if(!$this->check_hash($hash)) return false;
$sql = "UPDATE :table SET activated=1, confirmation='' WHERE user_id=:id AND confirmation=:hash";
$data = Array(
"hash" => $hash,
"id" => $this->id
);
if($this->check_sql($sql, $data)){
$this->report("Account has been Activated");
return true;
}else{
$this->error(3);
return false;
}
}
/**
* Method to reset password, Returns confirmation code to reset password
*
* @access public
* @api
*
* @param string $email User email to reset password
* @return array|bool On Success it returns an array(email,username,user_id,hash)
* which could then be use to construct the confirmation URL and Email.
* On Failure it returns false
*/
function pass_reset($email){
$this->logger("pass_reset");
$user = $this->getRow(Array("email" => $email));
if($user){
if(!$user['activated'] and !$user['confirmation']){
//The Account has been manually disabled and can't reset password
$this->error(9);
return false;
}
$this->make_hash($user['user_id']);
$this->id = $user['user_id'];
$this->save_hash(true);
$data = array(
"email" => $email,
"username" => $user['username'],
"user_id" => $user['user_id'],
"hash" => $this->confirm
);
return $data;
}else{
$this->error(4);
return false;
}
}
/**
* Changes a Password with a Confirmation hash from the pass_reset method
*
* This is for users that forget their passwords to change the signed in user password use ->update()
*
* @access public
* @api
*
* @param string $hash hash returned by the pass_reset() method
* @param array $newPass An array with indexes 'password' and 'password2'
* Example:
* array(
* [password] => pass123
* [password2] => pass123
* )
* @return bool Returns true on a successful password change.
* Returns false on error
*/
function new_pass($hash,$newPass){
$this->logger("new_pass");
if(!$this->check_hash($hash)) return false;
$this->tmp_data = $newPass;
if(!$this->validateAll()) return false; //There are validations error
$pass = $this->hash_pass($newPass['password']);
$sql = "UPDATE :table SET password=:pass, confirmation='', activated=1 WHERE confirmation=:hash AND user_id=:id";
$data = Array(
"id" => $this->id,
"pass" => $pass,
"hash" => $hash
);
if($this->check_sql($sql, $data)){
$this->report("Password has been changed");
return true;
}else{
//Error
$this->error(5);
return false;
}
}
/**
* Public function to start a delayed constructor
*
* When you initialize the class like `new uFlex(false)` the object
* construction will be halted until this method is called.
*
* @access public
* @api
*
* @return void
*/
function start(){
$this->__construct();
}
/**
* User factory
*
* Returns a clone of the uFlex instance which allows simple user managing
* capabilities such as updating a user field, resetting its password and so on.
*
* @api
*
* @param int $id
* @return bool|uFlex Returns false if user does not exists in database
*/
function manageUser( $id=0 ){
$user = clone $this;
$user->logger("Cloning");
if( $id > 0){
$user->report("Fetching user from database");
$data = $user->getRow(array("user_id"=>$id));
if($data){
$user->id = $data['user_id'];
$user->data = $data;
$user->username = $data['username'];
$user->pass = $data['password'];
$user->signed = true;
$user->report("User imported to object");
return $user;
}
}
return false;
}
/*////////////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*\
////////Protected and Secondary Methods below this line\\\\\\\\\\\\\
\*\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\/////////////////////////////////////////////*/
/**
* Object constructor
*
* @ignore
*/
function __construct($name='', $pass=false, $auto=false){
if($name === false) return false;
$this->logger("login"); //Index for Reports and Errors;
if(!isset($_SESSION) and !headers_sent()){
session_start();
$this->report("Session is been started...");
}elseif(isset($_SESSION)){
$this->report("Session has already been started");
}else{
$this->error("Session could not be started");
return false; //Finish Execution
}
$this->sid = session_id();
$result = $this->loginUser($name, $pass, $auto);
if(!$result){
$this->session($this->opt['default_user']);
$this->update_from_session();
$this->report("User is {$this->username}");
}else{
if(!$auto and isset($_SESSION['uFlex']['remember'])){
unset($_SESSION['uFlex']['remember']);
$this->setCookie();
}
}
return $result;
}
/**
* Protected Login processor function
*
* @param string|bool $user username or email
* @param string|bool $pass password in plain text
* @param bool $auto autologin cookie flag
* @ignore
* @protected
*
* @return bool True if user was successfully logged in, false otherwise
*/
protected function loginUser($user = false,$pass = false,$auto = false){
//Session Login
if($this->session("signed")){
$this->report("User Is signed in from session");
$this->update_from_session();
if(isset($_SESSION['uFlex-' . $this->clone . '-update'])){
$this->report("Updating Session from database");
//Get User From database because its info has change during current session
$update = $this->getRow(Array("user_id" => "$this->id"));
$this->update_session($update);
$this->log_login(); //Update last_login
//Cleaning session flag
unset($_SESSION['uFlex-' . $this->clone . '-update']);
}
return true;
}
//Cookies Login
if(isset($_COOKIE[$this->opt['cookie_name']]) and !$user and !$pass){
$c = $_COOKIE[$this->opt['cookie_name']];
$this->report("Attempting Login with cookies");
if($this->check_hash($c,true)){
$auto = true;
$getBy = "user_id";
$user = $this->id;
$this->signed = true;
}else{
$this->error(6);
$this->logout();
return false;
}
}else{
//Credentials Login
if($user && $pass){
if(preg_match($this->validations['email']['regEx'],$user)){
//Login using email
$getBy = "email";
}else{
//Login using username
$getBy = "username";
}
$this->report("Credentials received");
}else{
$this->error(7);
return false;
}
}
$this->report("Querying Database to authenticate user");
//Query Database for user
$userFile = $this->getRow(Array($getBy => $user));
if($userFile and !$this->signed){
$this->tmp_data = $userFile;
$this->hash_pass($pass);
$this->signed = $this->pass == $userFile["password"] ? true : false;
}else if($this->signed){
//Continue login from cookie
}else{
$this->form_error('password',$this->errorList[10]);
return false;
}
if($this->signed){
//If Account is not Activated
if($userFile['activated'] == 0){
if($userFile['last_login'] == 0){
//Account has not been activated
$this->error(8);
}else if(!$userFile['confirmation']){
//Account has been deactivated
$this->error(9);
}else{
//Account deactivated due to a password reset or reactivation request
$this->error(14);
}
return false;
}
//Account is Activated and user is logged in
$this->update_session($userFile);
//If auto Remember User
if($auto){
$this->setCookie();
}
//Update last_login
$this->log_login();
//Done
$this->report("User Logged in Successfully");
return true;
}else{
if(isset($_COOKIE[$this->opt['cookie_name']])){
$this->logout();
}
$this->form_error('password',$this->errorList[10]);
return false;
}
}
/**
* Logout the user
*
* Logs out the current user and deletes any autologin cookies
*
* @return void
*/
function logout(){
$this->logger("logout");
if(!$this->opt['cookie_host'])
$this->opt['cookie_host'] = $_SERVER['SERVER_NAME'];
$deleted = setcookie($this->opt['cookie_name'],"",time() - 3600,
$this->opt['cookie_path'],$this->opt['cookie_host']); //Deletes the Auto Cookie
$this->signed = 0;
//Import default user object
$this->session($this->data = $this->opt['default_user']);
if(!$deleted && !headers_sent()){
$this->report("The Autologin cookie could not be deleted");
}
$this->report("User Logged out");
}
/**
* Logs user last login in database
* @ignore
*/
protected function log_login(){
//Update last_login
$time = time();
$sql = "UPDATE :table SET last_login=:time WHERE user_id=:id";
if($this->check_sql($sql, Array("time" => $time, "id" => $this->id)))
$this->report("Last Login updated");
}
/**
* Set the autologin cookie for the current user
*/
protected function setCookie(){
if($this->pass and $this->id){
$cookied = false;
$code = $this->make_hash($this->id,$this->pass);
if(!$this->opt['cookie_host'])
$this->opt['cookie_host'] = $_SERVER['SERVER_NAME'];
if(!headers_sent()){
//echo "PHP";
$cookied = setcookie($this->opt['cookie_name'],$code,strtotime($this->opt['cookie_time']),
$this->opt['cookie_path'],$this->opt['cookie_host']);
}else{
//Headers have been sent use JavaScript to set cookie
$time = intval($this->opt['cookie_time']);
echo "<script>";
echo '
function setCookie(c_name,value,expiredays){
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : "; expires="+exdate.toUTCString()); path=escape("'.
$this->opt["cookie_path"].'");
}
';
echo "setCookie('{$this->opt['cookie_name']}','{$code}',{$time})";
echo "</script>";
$cookied = true;
}
if ($cookied){
$this->report("Cookies have been updated for auto login");
}else{
$this->error("Cookies could not be updated for auto login");
}
}else{
$this->error("Info required to set the cookie {$this->opt['cookie_name']} is not available");
}
}
/**
* Manage the session uFlex object variables
*
* This method has many functions:
* session([String]) => Get the value of the index
* session([String], [String|mixed]) => Set the specified index with the given value
* session([Array]) => Overwrite the whole uFlex session space with a given array
*
* @param string|array|bool $index Session index to get or set
* @param mixed[]|bool $val The value to the set the $index with
*
* @return bool|mixed[]
*/
function session($index=false, $val=false){
//Get uFlex session index value
if(is_string($index) and !$val){
return @$_SESSION[$this->opt['user_session']][$index];
}
//Set the value for a uFlex index
if(is_string($index) and $val){
$_SESSION[$this->opt['user_session']][$index] = $val;
return true;
}
//Overwrite the whole uFlex session space with a given array
if(is_array($index) and !$val){
$_SESSION[$this->opt['user_session']] = $index;
return true;
}
//return full session user data
return $_SESSION[$this->opt['user_session']];
}
/**
* Updates the session and the object with the provided array
* @ignore
*/
protected function update_session($d){
$this->session($d);
$this->session("signed",1);
$this->report("Session updated");
$this->update_from_session();
}
/**
* Update the object with the PHP session information
* @ignore
*/
protected function update_from_session(){
$d = $this->session();
$this->id = $d['user_id'];
$this->data = $d;
$this->username = $d['username'];
$this->pass = $d['password'];
$this->signed = $d['signed'];
$this->report("Session has been imported to the object");
}
/**
* The password hash maker
*
* Hashes a clear text password for the current user
*
* @ignore
*/
protected function hash_pass($pass){
$registrationDate = false;
if(isset($this->data['reg_date']))
$registrationDate = $this->data['reg_date'];
if(!$registrationDate and isset($this->tmp_data['reg_date']))
$registrationDate = $this->tmp_data['reg_date'];
$pre = $this->encode($registrationDate);
$pos = substr($registrationDate, 5, 1);
$post = $this->encode($registrationDate * (substr($registrationDate, $pos, 1)));
$this->pass = md5($pre.$pass.$post);
return $this->pass;
}
/**
* Log the type of request being initialized and log with the error amd report methods
*
* @param string $log Request name
* @return object Self $this object
*/
function logger($log){
$this->log = $log;
unset($this->console['errors'][$log]);
unset($this->console['form'][$log]);
$this->report(">> Starting new $log request");
return $this;
}
/**
* Add a report log entry
*
* @param string|bool $str Text information, default is false
*
* @return bool|mixed[] If $str is false returns the array of reports in the current logger
*/
function report($str = false){
$index = $this->log;
if($str){
if(is_string($str))
$str = ucfirst($str);
$this->console['reports'][$index][] = $str; //Store Report
return true;
}else{
if($index){
return $this->console['reports'][$index]; //Return the $index Reports Array
}else{
return $this->console['reports']; //Return the Full Reports Array
}
}
}
/**
* Add an error log entry
*
* @param string|bool $str Text information, default is false
* @return bool|array If $str is false returns the array of errors in the current logger
*/
function error($str = false){
$index = $this->log;
if($str){
$err = is_int($str) ? $this->errorList[$str] : $str;
$this->console['errors'][$index][] = $err; //Store Error
if(is_int($str)){
$this->report("Error[{$str}]: {$err}"); //Report The error
}else{
$this->report("Error: {$str}"); //Report The error
}
}else{
if($index){
if(!isset($this->console['errors'][$index]))
return false;
return $this->console['errors'][$index]; //Return the $index Errors Array
}else{
return $this->console['errors']; //Return the Full Error Array
}
}
return false;
}
/**
* Add a form field error log entry
*
* @param string|bool $field Field name, default is false
* @param string|bool $error Text information, default is false
* @return bool|array If $field and $error is false returns the array of form field error in the current logger
*/
function form_error($field = false,$error = false){
$index = $this->log;
if($field){
if($error){
$this->console['form'][$index][$field] = $error;
$this->error($error);
}else{
$this->console['form'][$index][] = $field;
}
}else{
if($index){
if(!isset($this->console['form'][$index]))