1+ <?php
2+ /**
3+ * Lib - less is more in PHP
4+ * @copyright Bill Rocha - http://google.com/+BillRocha
5+ * @license MIT
6+ * @author Bill Rocha - [email protected] 7+ * @version 0.0.1
8+ * @package Util
9+ * @access public
10+ * @since 0.3.0
11+ *
12+ * The MIT License
13+ *
14+ * Copyright 2015 http://google.com/+BillRocha.
15+ *
16+ * Permission is hereby granted, free of charge, to any person obtaining a copy
17+ * of this software and associated documentation files (the "Software"), to deal
18+ * in the Software without restriction, including without limitation the rights
19+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
20+ * copies of the Software, and to permit persons to whom the Software is
21+ * furnished to do so, subject to the following conditions:
22+ *
23+ * The above copyright notice and this permission notice shall be included in
24+ * all copies or substantial portions of the Software.
25+ *
26+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
31+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
32+ * THE SOFTWARE.
33+ */
34+
35+ namespace Lib ;
36+
37+ use Lib \Db ;
38+
39+
40+ class User
41+ {
42+ static $ node = null ;
43+
44+ private $ login = false ;
45+ private $ data = ['id ' =>null ,
46+ 'name ' =>null ,
47+ 'token ' =>null ,
48+ 'life ' =>null ,
49+ 'login ' =>null ,
50+ 'password ' =>null ,
51+ 'level ' =>null ,
52+ 'status ' =>null ];
53+
54+ private $ db = null ;
55+ private $ dbConfig = ['table ' =>'user ' ,
56+ 'id ' =>'id ' ,
57+ 'name ' =>'name ' ,
58+ 'token ' =>'token ' ,
59+ 'life ' =>'life ' ,
60+ 'login ' =>'login ' ,
61+ 'password ' =>'password ' ,
62+ 'level ' =>'level ' ,
63+ 'status ' =>'status ' ];
64+
65+
66+ function __construct ($ config = null )
67+ {
68+ if ($ config !== null ){
69+ foreach ($ config as $ i =>$ d ){
70+ if (isset ($ this ->dbConfig [$ i ])) $ this ->dbConfig [$ i ] = $ d ;
71+ }
72+ } elseif (method_exists ('Config\Database ' , 'getUserConfig ' )){
73+ $ this ->dbConfig = \Config \Database::getUserConfig ();
74+ }
75+
76+ $ this ->db = new Db ;
77+ }
78+
79+ /**
80+ * Singleton instance
81+ *
82+ */
83+ static function this ()
84+ {
85+ if (is_object (static ::$ node )) return static ::$ node ;
86+ //else...
87+ list ($ config ) = array_merge (func_get_args (), [null ]);
88+ return static ::$ node = new static ($ config );
89+ }
90+
91+ /**
92+ * Initialize user
93+ */
94+ function doLogin ($ login , $ password )
95+ {
96+ $ this ->db ->query ('SELECT * FROM ' .$ this ->dbConfig ['table ' ]
97+ .' WHERE ' .$ this ->dbConfig ['login ' ].' = :lg AND ' .$ this ->dbConfig ['password ' ].' = :pw ' , [':lg ' =>$ login , ':pw ' =>$ password ]);
98+
99+ $ row = $ this ->db ->result ();
100+ if (isset ($ row [0 ])){
101+ $ row = $ row [0 ]->getAll ();
102+
103+ foreach ($ this ->data as $ i =>$ d ){
104+ if (isset ($ row [$ this ->dbConfig [$ i ]])) $ this ->data [$ i ] = $ row [$ this ->dbConfig [$ i ]];
105+ }
106+ $ this ->login = true ;
107+ return true ;
108+ }
109+ $ this ->login = false ;
110+ return false ;
111+ }
112+
113+ /**
114+ * Performer LOGOUT
115+ */
116+ function logout ($ id = null ){
117+ if ($ id !== null ) $ this ->data ['id ' ] = $ id ;
118+
119+ //Reset
120+ $ this ->data ['life ' ] = 0 ;
121+ $ this ->data ['token ' ] = md5 (microtime ());
122+
123+ $ this ->db ->query ('UPDATE ' .$ this ->dbConfig ['table ' ]
124+ .' SET ' .$ this ->dbConfig ['token ' ].'=" '
125+ .$ this ->data ['token ' ].'", '
126+ .$ this ->dbConfig ['life ' ].' = " '
127+ .$ this ->data ['life ' ].'" WHERE '
128+ .$ this ->dbConfig ['id ' ].' = '
129+ .$ this ->data ['id ' ]);
130+ }
131+
132+ /**
133+ * Initialize user by ID
134+ */
135+ function getById ($ id )
136+ {
137+ $ this ->db ->query ('SELECT * FROM ' .$ this ->dbConfig ['table ' ]
138+ .' WHERE ' .$ this ->dbConfig ['id ' ].' = :id ' ,
139+ [':id ' =>$ id ]);
140+ $ row = $ this ->db ->result ();
141+ if (isset ($ row [0 ])){
142+ $ row = $ row [0 ]->getAll ();
143+
144+ $ this ->login = true ; //Setando LOGIN como válido/logado
145+
146+ foreach ($ this ->data as $ i =>$ d ){
147+ if (isset ($ row [$ this ->dbConfig [$ i ]])) $ this ->data [$ i ] = $ row [$ this ->dbConfig [$ i ]];
148+ }
149+ return $ this ->data ;
150+ }
151+ return false ;
152+ }
153+
154+
155+ /**
156+ * Set TOKEN data key
157+ */
158+ function saveToken ($ token )
159+ {
160+ $ rows = $ this ->db ->query ('UPDATE ' .$ this ->dbConfig ['table ' ]
161+ .' SET ' .$ this ->dbConfig ['token ' ].' = :tk '
162+ .' WHERE ' .$ this ->dbConfig ['id ' ].' = :id ' ,
163+ [':tk ' =>$ token , ':id ' =>$ this ->data ['id ' ]]);
164+ if ($ rows > 0 ){
165+ $ this ->data ['token ' ] = $ token ;
166+ return true ;
167+ }
168+ return false ;
169+ }
170+
171+ function getToken ($ id ){
172+ $ row = $ this ->db ->query ('SELECT token FROM ' .$ this ->dbConfig ['table ' ]
173+ .' WHERE ' .$ this ->dbConfig ['id ' ].' = :id ' ,
174+ [':id ' =>$ id ]);
175+ if (isset ($ row [0 ])){
176+ $ row = $ row [0 ]->getAll ();
177+ return $ row [$ this ->dbConfig ['token ' ]];
178+ }
179+ return false ;
180+ }
181+
182+ /**
183+ * Universal GET
184+ * If $node == null return ALL data (array)
185+ */
186+ function get ($ node = null )
187+ {
188+ if ($ node === null ) return $ this ->data ;
189+ return (isset ($ this ->data [$ node ])) ? $ this ->data [$ node ] : false ;
190+ }
191+
192+ /**
193+ * Universal SET
194+ *
195+ * @param array|string $node Se for um array, grava todos os dados
196+ * Se for um string e existir, grava $value
197+ * @param string|integer $value [optional] valor a ser gravado
198+ *
199+ * @return object (this)
200+ */
201+ function set ($ node , $ value = null )
202+ {
203+ if (!is_array ($ node )) $ node [$ node ] = $ value ;
204+ foreach ($ node as $ i =>$ d ){
205+ if (isset ($ this ->data [$ i ])) $ this ->data [$ i ] = $ d ;
206+ }
207+ return $ this ;
208+ }
209+
210+
211+ /**
212+ * Save this USER on DataBase
213+ *
214+ * @param Integer $id [optional] set a ID from this user
215+ * $id = "null" (or none) gera INSERT new user
216+ * @return Array action = INSERT/UPDATE
217+ * rows = 0/1 (0 => indica não salvo)
218+ */
219+ function save ($ id = null ){
220+ //update this user id value
221+ if ($ id !== null ) $ this ->data ['id ' ] = $ id ;
222+
223+ if ($ this ->data ['id ' ] !== null ){
224+ $ action = 'UPDATE ' ;
225+ $ where = ' WHERE ' .$ this ->dbConfig ['id ' ].' = :id ' ;
226+ } else {
227+ $ action = 'INSERT INTO ' ;
228+ $ where = '' ;
229+ }
230+
231+ $ cols = '' ;
232+ $ vals = [];
233+ foreach ($ this ->data as $ k =>$ v ){
234+ if ($ k !== 'id ' ) $ cols .= $ this ->dbConfig [$ k ].' = : ' .$ k .', ' ;
235+ $ vals [': ' .$ k ] = $ v ;
236+ }
237+
238+ $ cols = substr ($ cols , 0 , -1 ); //tirando a ultima vírgula
239+
240+ $ this ->db ->query ($ action .$ this ->dbConfig ['table ' ]
241+ .' SET ' .$ cols .$ where , $ vals );
242+ return ['action ' =>substr ($ action , 0 , 5 ),
243+ 'rows ' =>$ this ->db ->getRows ()];
244+ }
245+
246+
247+
248+
249+ }
0 commit comments