OssnUser.php
Go to the documentation of this file.
1 <?php
2 
3 /**
4  * Open Source Social Network
5  *
6  * @package (Informatikon.com).ossn
7  * @author OSSN Core Team <[email protected]>
8  * @copyright 2014 iNFORMATIKON TECHNOLOGIES
9  * @license General Public Licence http://www.opensource-socialnetwork.org/licence
10  * @link http://www.opensource-socialnetwork.org/licence
11  */
12 class OssnUser extends OssnEntities {
13  /**
14  * Add user to system.
15  *
16  * @return boolean
17  */
18  public function addUser() {
19  self::initAttributes();
20  if(empty($this->usertype)) {
21  $this->usertype = 'normal';
22  }
23  $user = $this->getUser();
24  if(empty($user->username) && $this->isPassword() && $this->isUsername()) {
25  $this->salt = $this->generateSalt();
26  $password = $this->generate_password($this->password, $this->salt);
27  $activation = md5($this->password . time() . rand());
28  $this->sendactiviation = ossn_call_hook('user', 'send:activation', false, $this->sendactiviation);
29  if($this->sendactiviation === false) {
30  //don't set null , set empty value for users created by admin
31  $activation = '';
32  }
33  $params['into'] = 'ossn_users';
34  $params['names'] = array(
35  'first_name',
36  'last_name',
37  'email',
38  'username',
39  'type',
40  'password',
41  'salt',
42  'activation',
43  'time_created'
44  );
45  $params['values'] = array(
46  $this->first_name,
47  $this->last_name,
48  $this->email,
49  $this->username,
50  $this->usertype,
51  $password,
52  $this->salt,
53  $activation,
54  time()
55  );
56  if($this->insert($params)) {
57  $guid = $this->getLastEntry();
58 
59  //define user extra profile fields
60  $fields = array(
61  'text' => array(
62  'birthdate'
63  ),
64  'radio' => array(
65  'gender'
66  )
67  );
68  if(!empty($guid) && is_int($guid)) {
69 
70  $this->owner_guid = $guid;
71  $this->type = 'user';
72 
73  //add user entities
74  $extra_fields = ossn_call_hook('user', 'signup:fields', $this, $fields);
75  if(!empty($extra_fields)) {
76  foreach($extra_fields as $type) {
77  foreach($type as $field) {
78  if(isset($this->$field)) {
79  $this->subtype = $field;
80  $this->value = $this->$field;
81  //add entity
82  $this->add();
83  }
84  }
85  }
86  }
87  }
88  //should i send activation?
89  if($this->sendactiviation === true) {
90  $link = ossn_site_url("uservalidate/activate/{$guid}/{$activation}");
91  $link = ossn_call_hook('user', 'validation:email:url', $this, $link);
92  $sitename = ossn_site_settings('site_name');
93  $activation = ossn_print('ossn:add:user:mail:body', array(
94  $sitename,
95  $link,
97  ));
98  $subject = ossn_print('ossn:add:user:mail:subject', array(
99  $this->first_name,
100  $sitename
101  ));
102  //notify users for activation
103  $this->notify->NotifiyUser($this->email, $subject, $activation);
104  }
105  return true;
106  }
107  }
108  return false;
109  }
110 
111  /**
112  * Check if username is exist in database or not.
113  *
114  * @return boolean
115  */
116  public function isOssnUsername() {
117  $user = $this->getUser();
118  if(!empty($user->guid) && $this->username == $user->username) {
119  return true;
120  }
121  return false;
122  }
123 
124  /**
125  * Check if email is exist in database or not.
126  *
127  * @return boolean
128  */
129  public function isOssnEmail() {
130  $user = $this->getUser();
131  if(!empty($user->guid) && $this->email == $user->email) {
132  return true;
133  }
134  return false;
135  }
136 
137  /**
138  * Initialize the objects.
139  *
140  * @return void
141  */
142  public function initAttributes() {
143  $this->OssnDatabase = new OssnDatabase;
144  $this->OssnAnnotation = new OssnAnnotation;
145  $this->notify = new OssnMail;
146  if(!isset($this->sendactiviation)) {
147  $this->sendactiviation = false;
148  }
149  }
150 
151  /**
152  * Get user with its entities.
153  *
154  * @return object
155  */
156  public function getUser() {
157  if(!empty($this->email)) {
158  $params['from'] = 'ossn_users';
159  $params['wheres'] = array(
160  "email='{$this->email}'"
161  );
162  $user = $this->select($params);
163  }
164  if(empty($user) && !empty($this->username)) {
165  $params['from'] = 'ossn_users';
166  $params['wheres'] = array(
167  "username='{$this->username}'"
168  );
169  $user = $this->select($params);
170  }
171  if(empty($user) && !empty($this->guid)) {
172  $params['from'] = 'ossn_users';
173  $params['wheres'] = array(
174  "guid='{$this->guid}'"
175  );
176  $user = $this->select($params);
177  }
178  if(!$user) {
179  return false;
180  }
181  $user->fullname = "{$user->first_name} {$user->last_name}";
182  $this->owner_guid = $user->guid;
183  $this->type = 'user';
184  $entities = $this->get_entities();
185  if(empty($entities)) {
186  $metadata = arrayObject($user, get_class($this));
187  return ossn_call_hook('user', 'get', false, $metadata);
188  }
189  foreach($entities as $entity) {
190  $fields[$entity->subtype] = $entity->value;
191  }
192  $data = array_merge(get_object_vars($user), $fields);
193  $metadata = arrayObject($data, get_class($this));
194  return ossn_call_hook('user', 'get', false, $metadata);
195  }
196 
197  /**
198  * Check if password is > 5 or not.
199  *
200  * @return boolean
201  */
202  public function isPassword() {
203  if(strlen($this->password) > 5) {
204  return true;
205  }
206  return false;
207  }
208  /**
209  * Check if password is > 5 or not.
210  *
211  * @return boolean
212  */
213  public function isEmail() {
214  if(filter_var($this->email, FILTER_VALIDATE_EMAIL)) {
215  return true;
216  }
217  return false;
218  }
219  /**
220  * Check if the user is correct or not.
221  *
222  * @return boolean
223  */
224  public function isUsername() {
225  if(preg_match("/^[a-zA-Z0-9]+$/", $this->username) && strlen($this->username) > 4) {
226  return true;
227  }
228  return false;
229  }
230 
231  /**
232  * Generate salt.
233  *
234  * @return string
235  */
236  public function generateSalt() {
237  return substr(uniqid(), 5);
238  }
239 
240  /**
241  * Generate password.
242  *
243  * @return string
244  */
245  public function generate_password($password = '', $salt = '') {
246  return md5($password . $salt);
247  }
248 
249  /**
250  * Login into site.
251  *
252  * @return boolean
253  */
254  public function Login() {
255  $user = $this->getUser();
256  $salt = $user->salt;
257  $password = $this->generate_password($this->password . $salt);
258  if($password == $user->password && $user->activation == NULL) {
259  unset($user->password);
260  unset($user->salt);
261  $_SESSION['OSSN_USER'] = $user;
262  $this->update_last_login();
263 
264  $vars = array();
265  $vars['user'] = $user;
266  $login = ossn_call_hook('user', 'login', $vars, true);
267  return $login;
268  }
269  return false;
270  }
271 
272  /**
273  * Update user last login time.
274  *
275  * @return boolean
276  */
277  public function update_last_login() {
278  $user = ossn_loggedin_user();
279  $guid = $user->guid;
280  $params['table'] = 'ossn_users';
281  $params['names'] = array(
282  'last_login'
283  );
284  $params['values'] = array(
285  time()
286  );
287  $params['wheres'] = array(
288  "guid='{$guid}'"
289  );
290  if($guid > 0 && $this->update($params)) {
291  return true;
292  }
293  return false;
294  }
295 
296  /**
297  * Get user friends requests.
298  *
299  * @return object
300  */
301  public function getFriendRequests($user = '') {
302  if(isset($this->guid)) {
303  $user = $this->guid;
304  }
305  $this->statement("SELECT * FROM ossn_relationships WHERE(
306  relation_to='{$user}' AND
307  type='friend:request'
308  );");
309  $this->execute();
310  $from = $this->fetch(true);
311  if(!is_object($from)) {
312  return false;
313  }
314  foreach($from as $fr) {
315  $this->statement("SELECT * FROM ossn_relationships WHERE(
316  relation_from='{$user}' AND
317  relation_to='{$fr->relation_from}' AND
318  type='friend:request'
319  );");
320  $this->execute();
321  $from = $this->fetch();
322  if(!isset($from->relation_id)) {
323  $uss[] = ossn_user_by_guid($fr->relation_from);
324  }
325  }
326  if(isset($uss)) {
327  return $uss;
328  }
329  return false;
330  }
331 
332  /**
333  * Check if the user is friend with other or not.
334  *
335  * @return boolean
336  */
337  public function isFriend($usera, $user2) {
338  $this->statement("SELECT * FROM ossn_relationships WHERE(
339  relation_from='{$usera}' AND
340  relation_to='{$user2}' AND
341  type='friend:request'
342  );");
343  $this->execute();
344  $from = $this->fetch();
345  $this->statement("SELECT * FROM ossn_relationships WHERE(
346  relation_from='{$user2}' AND
347  relation_to='{$usera}' AND
348  type='friend:request'
349  );");
350  $this->execute();
351  $to = $this->fetch();
352  if(isset($from->relation_id) && isset($to->relation_id)) {
353  return true;
354  }
355  return false;
356  }
357 
358  /**
359  * Get user friends.
360  *
361  * @return object
362  */
363  public function getFriends($user = '') {
364  if(isset($this->guid)) {
365  $user = $this->guid;
366  }
367  $this->statement("SELECT * FROM ossn_relationships WHERE(
368  relation_to='{$user}' AND
369  type='friend:request'
370  );");
371  $this->execute();
372  $from = $this->fetch(true);
373  if(!is_object($from)) {
374  return false;
375  }
376  foreach($from as $fr) {
377  if($this->isFriend($user, $fr->relation_from)) {
378  $uss[] = ossn_user_by_guid($fr->relation_from);
379  }
380  }
381  if(isset($uss)) {
382  return $uss;
383  }
384  return false;
385  }
386 
387  /**
388  * Send request to other user.
389  *
390  * @return boolean
391  */
392  public function sendRequest($from, $to) {
393  if($this->requestExists($from, $to)) {
394  return false;
395  }
396  if(ossn_add_relation($from, $to, 'friend:request')) {
397  return true;
398  }
399  return false;
400  }
401 
402  /**
403  * Check if the request already sent or not.
404  *
405  * @return boolean
406  */
407  public function requestExists($from, $user) {
408  if(isset($this->guid)) {
409  $user = $this->guid;
410  }
411  $this->statement("SELECT * FROM ossn_relationships WHERE(
412  relation_to='{$user}' AND
413  relation_from='{$from}' AND
414  type='friend:request'
415  );");
416  $this->execute();
417  $request = $this->fetch();
418  if(!empty($request->relation_id)) {
419  return true;
420  }
421  return false;
422  }
423 
424  /**
425  * Delete friend from list
426  *
427  * @return boolean
428  */
429  public function deleteFriend($from, $to) {
430  $this->statement("DELETE FROM ossn_relationships WHERE(
431  relation_from='{$from}' AND relation_to='{$to}' OR
432  relation_from='{$to}' AND relation_to='{$from}')");
433  if($this->execute()) {
434  return true;
435  }
436  return false;
437  }
438 
439  /**
440  * Get site users.
441  *
442  * @return object
443  */
444  public function getSiteUsers($params = array()) {
445  $vars = array();
446  $vars['from'] = 'ossn_users';
447 
448  $args = array_merge($vars, $params);
449  $users = $this->select($args, true);
450  return $users;
451 
452  }
453 
454  /**
455  * Update user last activity time
456  *
457  * @return boolean
458  */
459  public function update_last_activity() {
460  $user = ossn_loggedin_user();
461  if($user) {
462  $guid = $user->guid;
463  $params['table'] = 'ossn_users';
464  $params['names'] = array(
465  'last_activity'
466  );
467  $params['values'] = array(
468  time()
469  );
470  $params['wheres'] = array(
471  "guid='{$guid}'"
472  );
473  if($guid > 0 && $this->update($params)) {
474  return true;
475  }
476  }
477  return false;
478  }
479 
480  /**
481  * Count Total online site users.
482  *
483  * @return boolean
484  */
485  public function online_total() {
486  return count((array) $this->getOnline());
487  }
488 
489  /**
490  * Get online site users.
491  *
492  * @params integer $intervals Seconds
493  *
494  * @return object
495  */
496  public function getOnline($intervals = '100') {
497  $time = time();
498  $params['from'] = 'ossn_users';
499  $params['wheres'] = array(
500  "last_activity > {$time} - {$intervals}"
501  );
502  $data = (array) $this->select($params, true);
503  if($data) {
504  foreach($users as $user) {
505  $result[] = arrayObject($user, get_class($this));
506  }
507  return $result;
508  }
509  return false;
510  }
511 
512  /**
513  * Search site users with its entities
514  *
515  * @return boolean
516  */
517  public function searchUsers($q, $limit = '') {
518  $search = $this->SearchSiteUsers($q, $limit);
519  if(!$search) {
520  return false;
521  }
522  $users = new OssnUser;
523  foreach($search as $user) {
524  $users->guid = $user->guid;
525  $userentity[] = $users->getUser();
526  }
527  $data = $userentity;
528  return $data;
529  }
530 
531  /**
532  * Search users without entities.
533  *
534  * @return object
535  */
536  public function SearchSiteUsers($search, $limit = '') {
537  //don't listup all users if search query is empty
538  if(empty($search)) {
539  return false;
540  }
541  $params = array();
542  $wheres = array();
543  if(!empty($search)) {
544  $wheres[] = "CONCAT(first_name, ' ', last_name) LIKE '%$search%'";
545  $wheres[] = "username LIKE '%$search%'";
546  $wheres[] = "email LIKE '%$search%'";
547  }
548  if(!empty($limit)) {
549  $params['limit'] = $limit;
550  }
551  $params['from'] = 'ossn_users';
552  $params['wheres'] = array(
553  $this->constructWheres($wheres, 'OR')
554  );
555  $users = $this->select($params, true);
556  if($users) {
557  return $users;
558  }
559  return false;
560  }
561 
562  /**
563  * Validate User Registration
564  *
565  * @return boolean
566  */
567  public function ValidateRegistration($code) {
568  $user_activation = $this->getUser();
569  $guid = $user_activation->guid;
570  if($user_activation->activation == $code) {
571  $params['table'] = 'ossn_users';
572  $params['names'] = array(
573  'activation'
574  );
575  $params['values'] = array(
576  ''
577  );
578  $params['wheres'] = array(
579  "guid='{$guid}'"
580  );
581  if($this->update($params)) {
582  return true;
583  }
584  }
585  return false;
586  }
587 
588  /**
589  * View user icon url
590  *
591  * @return string
592  */
593  public function iconURL() {
594  $this->iconURLS = new stdClass;
595  foreach(ossn_user_image_sizes() as $size => $dimensions) {
596  $seo = md5($this->username . $size . $this->icon_time);
597  $url = ossn_site_url("avatar/{$this->username}/{$size}/{$seo}.jpeg");
598  $this->iconURLS->$size = $url;
599  }
600  return ossn_call_hook('user', 'icon:urls', $this, $this->iconURLS);
601  }
602 
603  /**
604  * View user profile url
605  *
606  * @return string
607  */
608  public function profileURL($extends = '') {
609  $this->profileurl = ossn_site_url("u/{$this->username}") . $extends;
610  return ossn_call_hook('user', 'profile:url', $this, $this->profileurl);
611  }
612 
613  /**
614  * Send user reset password link
615  *
616  * @return boolean
617  */
618  public function SendResetLogin() {
619  self::initAttributes();
620  $this->old_code = $this->getParam('login:reset:code');
621  $this->type = 'user';
622  $this->subtype = 'login:reset:code';
623  $this->owner_guid = $this->guid;
624  if(!isset($this->{'login:reset:code'}) && empty($this->old_code)) {
625  $this->value = md5(time() . $this->guid);
626  $this->add();
627  } else {
628  $this->value = md5(time() . $this->guid);
629  $this->data->{'login:reset:code'} = $this->value;
630  $this->save();
631  }
632  $emailreset_url = ossn_site_url("resetlogin?user={$this->username}&c={$this->value}");
633  $emailreset_url = ossn_call_hook('user', 'reset:login:url', $this, $emailreset_url);
634  $sitename = ossn_site_settings('site_name');
635 
636  $emailmessage = ossn_print('ossn:reset:password:body', array(
637  $this->first_name,
638  $emailreset_url,
639  $sitename
640  ));
641  $emailsubject = ossn_print('ossn:reset:password:subject');
642  if(!empty($this->value) && $this->notify->NotifiyUser($this->email, $emailsubject, $emailmessage)) {
643  return true;
644  }
645  return false;
646  }
647 
648  /**
649  * Reset user password
650  *
651  * @return boolean
652  */
653  public function resetPassword($password) {
654  if(!empty($password)) {
655  $this->salt = $this->generateSalt();
656  $password = $this->generate_password($password, $this->salt);
657  $reset['table'] = 'ossn_users';
658  $reset['names'] = array(
659  'password',
660  'salt'
661  );
662  $reset['values'] = array(
663  $password,
664  $this->salt
665  );
666  $reset['wheres'] = array(
667  "guid='{$this->guid}'"
668  );
669  if($this->update($reset)) {
670  return true;
671  }
672  }
673  return false;
674  }
675 
676  /**
677  * Remove user reset code
678  *
679  * @return boolean
680  */
681  public function deleteResetCode() {
682  $this->type = 'user';
683  $this->subtype = 'login:reset:code';
684  $this->owner_guid = $this->guid;
685  $code = $this->get_entities();
686  if($this->deleteEntity($code[0]->guid)) {
687  return true;
688  }
689  return false;
690  }
691 
692  /**
693  * Check if user is online or not
694  *
695  * @return boolean
696  */
697  public function isOnline($intervals = 100) {
698  if(isset($this->last_activity)) {
699  $time = time();
700  if($this->last_activity > $time - $intervals) {
701  return true;
702  }
703  }
704  return false;
705  }
706 
707  /**
708  * Delete user from syste,
709  *
710  * @return boolean
711  */
712  public function deleteUser() {
713  self::initAttributes();
714  if(!empty($this->guid) && !empty($this->username)) {
715  $params['from'] = 'ossn_users';
716  $params['wheres'] = array(
717  "guid='{$this->guid}'"
718  );
719  if($this->delete($params)) {
720  //delete user files
721  $datadir = ossn_get_userdata("user/{$this->guid}/");
722 
723  if(is_dir($datadir)) {
724  OssnFile::DeleteDir($datadir);
725  //From of v2.0 DeleteDir delete directory also #138
726  //rmdir($datadir);
727  }
728  //delete user entites also
729  $this->deleteByOwnerGuid($this->guid, 'user');
730 
731  //delete annotations
732  $this->OssnAnnotation->owner_guid = $this->guid;
733  $this->OssnAnnotation->deleteAnnotationByOwner($this->guid);
734  //trigger callback so other components can be notified when user deleted.
735 
736  //should delete relationships
738 
739  $vars['entity'] = $this;
740  ossn_trigger_callback('user', 'delete', $vars);
741  return true;
742  }
743  }
744  return false;
745  }
746  /**
747  * Check if user is validated or not
748  *
749  * @return boolean
750  */
751  public function isUserVALIDATED() {
752  if(isset($this->activation) && empty($this->activation)) {
753  return true;
754  }
755  return false;
756  }
757  /**
758  * Resend validation email to user
759  *
760  * @return boolean
761  */
762  public function resendValidationEmail() {
763  self::initAttributes();
764  if(!$this->isUserVALIDATED()) {
765  $link = ossn_site_url("uservalidate/activate/{$this->guid}/{$this->activation}");
766  $link = ossn_call_hook('user', 'validation:email:url', $this, $link);
767  $sitename = ossn_site_settings('site_name');
768  $activation = ossn_print('ossn:add:user:mail:body', array(
769  $sitename,
770  $link,
771  ossn_site_url()
772  ));
773  $subject = ossn_print('ossn:add:user:mail:subject', array(
774  $this->first_name,
775  $sitename
776  ));
777  return $this->notify->NotifiyUser($this->email, $subject, $activation);
778  }
779  return false;
780  }
781  /**
782  * Get list of unvalidated users
783  *
784  * @return false|object
785  */
786  public function getUnvalidatedUSERS($search = '', $count = false) {
787  $params = array();
788  $params['from'] = 'ossn_users';
789  if($count) {
790  $params['params'] = array(
791  "count(*) as total"
792  );
793  }
794  if(empty($search)) {
795  $params['wheres'] = array(
796  "activation <> ''"
797  );
798  } else {
799  $params['wheres'] = array(
800  "activation <> '' AND",
801  "CONCAT(first_name, ' ', last_name) LIKE '%$search%' AND activation <> '' OR
802  username LIKE '%$search%' AND activation <> '' OR email LIKE '%$search%' AND activation <> ''"
803  );
804  }
805  $users = $this->select($params, true);
806  if($users) {
807  if($count) {
808  return $users->{0}->total;
809  }
810  return $users;
811  }
812  return false;
813  }
814  /**
815  * Get a user last profile photo
816  *
817  * @return object|false
818  */
819  public function getProfilePhoto() {
820  if(!empty($this->guid)) {
821  $this->owner_guid = $this->guid;
822  $this->type = 'user';
823  $this->subtype = 'file:profile:photo';
824  $this->limit = 1;
825  $this->order_by = "guid DESC";
826  $entity = $this->get_entities();
827  if(isset($entity[0])) {
828  return $entity[0];
829  }
830  }
831  return false;
832  }
833  /**
834  * Get a user last coverphoto photo
835  *
836  * @return object|false
837  */
838  public function getProfileCover() {
839  if(!empty($this->guid)) {
840  $this->owner_guid = $this->guid;
841  $this->type = 'user';
842  $this->subtype = 'file:profile:cover';
843  $this->limit = 1;
844  $this->order_by = "guid DESC";
845  $entity = $this->get_entities();
846  if(isset($entity[0])) {
847  return $entity[0];
848  }
849  }
850  return false;
851  }
852  /**
853  * User logout
854  *
855  * @return void
856  */
857  public static function Logout() {
858  unset($_SESSION['OSSN_USER']);
859  session_destroy();
860  }
861  /**
862  * Get total users per month for each year
863  *
864  * @return object|false
865  * @access public
866  */
867  public function countByYearMonth() {
868 
869  $wheres = array();
870  $params = array();
871 
872  $wheres[] = "time_created > 0";
873 
874  $params['from'] = "ossn_users";
875  $params['params'] = array(
876  "DATE_FORMAT(FROM_UNIXTIME(time_created), '%Y') AS year",
877  "DATE_FORMAT(FROM_UNIXTIME(time_created) , '%m') AS month",
878  "COUNT(guid) AS total"
879  );
880  $params['group_by'] = "DATE_FORMAT(FROM_UNIXTIME(time_created) , '%Y%m')";
881  $params["wheres"] = array(
882  $this->constructWheres($wheres)
883  );
884  $data = $this->select($params, true);
885  if($data) {
886  return $data;
887  }
888  return false;
889  }
890  /**
891  * Gender types
892  *
893  * @return object|false
894  * @access public
895  */
896  public function genderTypes() {
897  $gender = array(
898  'male',
899  'female'
900  );
901  return ossn_call_hook('user', 'gender:types', $this, $gender);
902  }
903  /**
904  * Get total users per month for each year
905  *
906  * @return object|false
907  * @access public
908  */
909  public function countByGender($gender = 'male') {
910  if(!in_array($gender, $this->genderTypes())) {
911  return false;
912  }
913  $params = array();
914  $params['type'] = 'user';
915  $params['subtype'] = 'gender';
916  $params['page_limit'] = false;
917  $params['search_type'] = false;
918  $params['count'] = true;
919  $params['value'] = $gender;
920 
921  $data = $this->searchEntities($params);
922  if($data) {
923  return $data;
924  }
925  return false;
926  }
927  /**
928  * Get online users by gender
929  *
930  * @param string $gender Gender type
931  * @param boolean $count true or false
932  * @param integer $intervals Seconds
933  *
934  * @return object|false
935  * @access public
936  */
937  public function onlineByGender($gender = 'male', $count = false, $intervals = 100) {
938  if(empty($gender) || !in_array($gender, $this->genderTypes())) {
939  return false;
940  }
941  $time = time();
942  $params = array();
943  $wheres['wheres'] = array();
944  $params['joins'] = array();
945 
946  $params['params'] = array(
947  'u.*, emd.value as gender'
948  );
949  $params['from'] = 'ossn_users as u';
950 
951  $wheres['wheres'][] = "e.type='user'";
952  $wheres['wheres'][] = "e.subtype='gender'";
953  $wheres['wheres'][] = "emd.value='{$gender}'";
954  $wheres['wheres'][] = "last_activity > {$time} - {$intervals}";
955 
956  $params['joins'][] = "JOIN ossn_entities as e ON e.owner_guid=u.guid";
957  $params['joins'][] = "JOIN ossn_entities_metadata as emd ON emd.guid=e.guid";
958  $params['wheres'] = array(
959  $this->constructWheres($wheres['wheres'])
960  );
961 
962  if($count) {
963  $params['params'] = array(
964  "count(*) as total"
965  );
966  $count = $this->select($params);
967  return $count->total;
968  }
969 
970  $users = $this->select($params, true);
971  if($users) {
972  foreach($users as $user) {
973  $result[] = arrayObject($user, get_class($this));
974  }
975  return $result;
976  }
977  return false;
978  }
979  /**
980  * Save a user entity
981  *
982  * @return boolean
983  */
984  public function save() {
985  if(!isset($this->guid) || empty($this->guid)) {
986  return false;
987  }
988  $this->owner_guid = $this->guid;
989  $this->type = 'user';
990  if(parent::save()) {
991  //check if owner is loggedin user guid , if so update session
992  if(ossn_loggedin_user()->guid == $this->guid) {
993  $_SESSION['OSSN_USER'] = ossn_user_by_guid($this->guid);
994  }
995  return true;
996  }
997  return false;
998  }
999  /**
1000  * Can Moderate
1001  * Check if user can moderate the requested item or not
1002  *
1003  * @return boolean
1004  */
1005  public function canModerate() {
1006  $allowed = false;
1007  if(isset($this->guid) && $this instanceof OssnUser) {
1008  if(($this->type == 'normal' && $this->can_moderate == 'yes') || $this->type == 'admin') {
1009  $allowed = true;
1010  }
1011  }
1012  return ossn_call_hook('user', 'can:moderate', $this, $allowed);
1013  }
1014  /**
1015  * isAdmin
1016  * Check if user is admin or not
1017  *
1018  * @return boolean
1019  */
1020  public function isAdmin() {
1021  if(isset($this->guid) && $this->type == 'admin') {
1022  return true;
1023  }
1024  return false;
1025  }
1026 } //CLASS