ossn.lib.users.php
Go to the documentation of this file.
1 <?php
2 /**
3  * Open Source Social Network
4  *
5  * @package (Informatikon.com).ossn
6  * @author OSSN Core Team <[email protected]>
7  * @copyright 2014 iNFORMATIKON TECHNOLOGIES
8  * @license General Public Licence http://www.opensource-socialnetwork.org/licence
9  * @link http://www.opensource-socialnetwork.org/licence
10  */
11 
12 /**
13  * Initialize user class
14  *
15  * @return bool;
16  */
17 function ossn_user() {
18  $user = new OssnUser;
19  return $user;
20 }
21 
22 /**
23  * Initialize library
24  *
25  * @return bool
26  */
27 function ossn_users() {
28  ossn_register_page('uservalidate', 'ossn_uservalidate_pagehandler');
29 
30  /**
31  * Logout outuser if user didn't exists
32  */
33  if(ossn_isLoggedin()){
34  $user = ossn_user_by_guid(ossn_loggedin_user()->guid);
35  if(!$user){
36  ossn_logout();
37  redirect();
38  }
39  }
40 }
41 
42 /**
43  * Check if the user is logged in or not
44  *
45  * @return bool
46  */
47 function ossn_isLoggedin() {
48  $user = forceObject($_SESSION['OSSN_USER']);
49  if (isset($user) && is_object($user) && $user instanceof OssnUser) {
50  return true;
51  }
52  return false;
53 }
54 
55 /**
56  * Check if the admin is logged in or not
57  *
58  * @return bool
59  */
61  $user = forceObject($_SESSION['OSSN_USER']);
62  if (isset($user) && is_object($user) && $user instanceof OssnUser) {
63  if ($user->type == 'admin') {
64  return true;
65  }
66  }
67  return false;
68 }
69 
70 /**
71  * Get a logged in user entity
72  *
73  * @return object
74  */
75 function ossn_loggedin_user() {
76  if (ossn_isLoggedin()) {
77  return forceObject($_SESSION['OSSN_USER']);
78  }
79  return false;
80 }
81 
82 /**
83  * Get a user by username
84  *
85  * @param $username 'username' of user
86  *
87  * @return object
88  */
89 function ossn_user_by_username($username) {
90  $user = new OssnUser;
91  $user->username = $username;
92  return $user->getUser();
93 }
94 
95 /**
96  * Get a user by user id
97  *
98  * @param $guid 'guid' of user
99  *
100  * @return object
101  */
102 function ossn_user_by_guid($guid) {
103  $user = new OssnUser;
104  $user->guid = $guid;
105  return $user->getUser();
106 }
107 
108 /**
109  * Get a user by email id
110  *
111  * @param $guid 'guid' of user
112  *
113  * @return object
114  */
115 function ossn_user_by_email($email) {
116  $user = new OssnUser;
117  $user->email = $email;
118  return $user->getUser();
119 }
120 
121 /**
122  * Get a user friends
123  *
124  * @param $guid 'guid' of user
125  *
126  * @return object
127  */
128 function get_user_friends($guid) {
129  $friends = new OssnUser;
130  return $friends->getFriends($guid);
131 }
132 
133 /**
134  * Check if the user is from with other user
135  *
136  * @param $guid 'guid' of user
137  * $friend guid of other user
138  *
139  * @return bool
140  */
141 function ossn_user_is_friend($guid, $friend) {
142  $friends = new OssnUser;
143  if ($friends->isFriend($guid, $friend)) {
144  return true;
145  }
146  return false;
147 }
148 
149 /**
150  * Add user a friend
151  *
152  * @param $form 'guid' of user
153  * $to guid of other user
154  *
155  * @return bool
156  */
157 function ossn_add_friend($from, $to) {
158  $add = new OssnUser;
159  if ($add->sendRequest($from, $to)) {
160  return true;
161  }
162  return false;
163 }
164 
165 /**
166  * Remove user from friend list
167  *
168  * @param $form 'guid' of user
169  * $to guid of other user
170  *
171  * @return bool
172  */
173 function ossn_remove_friend($from, $to) {
174  $remove = new OssnUser;
175  if ($remove->deleteFriend($from, $to)) {
176  return true;
177  }
178  return false;
179 }
180 
181 /**
182  * Get total site users
183  *
184  * @return object
185  */
187  $users = new OssnUser;
188  return count(get_object_vars($users->getSiteUsers()));
189 }
190 
191 /**
192  * Get total online users
193  *
194  * @return int
195  */
196 function ossn_total_online() {
197  $users = new OssnUser;
198  return $users->online_total();
199 }
200 
201 /**
202  * Get friends suggestion
203  *
204  * @param $guid 'guid' of user
205  *
206  * @return bool
207  */
208 function ossn_friends_suggestion($guid) {
209  $user = new OssnUser;
210  $friends = $user->getFriends($guid);
211  if (!$friends) {
212  return false;
213  }
214  foreach ($friends as $friend) {
215  $friends_friend[] = $user->getFriends($friend->guid);
216  }
217  return $friends_friend;
218 }
219 
220 /**
221  * Update user last activity time
222  *
223  * @return void
224  */
226  $update = new OssnUser;
227  $update->update_last_activity();
228 }
229 
230 /**
231  * Convert time to to user recognize from
232  *
233  * @param $tm => time stamp
234  *
235  * @return bool
236  */
237 function ossn_user_friendly_time($tm, $rcs = 0) {
238  $cur_tm = time();
239  $dif = $cur_tm - $tm;
240  // get language dependend items for display
241  $passedtime = ossn_print('site:timepassed:data');
242  // put them into array
243  // 0 = second
244  // 15 = decades
245  $pds = explode('|',$passedtime);
246 
247  // BETTER DO explode ONLY ONCE on start and when language changes
248  // and get already prepared array from there
249  // don't know how and where to do this correctly ?!?
250  $lngh = array(
251  1,
252  60,
253  3600,
254  86400,
255  604800,
256  2630880,
257  31570560,
258  315705600
259  );
260  for ($v = count($lngh) - 1; ($v >= 0) && (($no = $dif / $lngh[$v]) <= 1); $v--)
261  ;
262  if ($v < 0)
263  $v = 0;
264  $_tm = $cur_tm - ($dif % $lngh[$v]);
265  $no = ($rcs ? floor($no) : round($no)); // if last denomination, round
266  // since our array now has 16 time elements instead of 8, we need to skip odd entries and fetch the next even one (the singular)
267  $v = $v * 2;
268 
269  if ($no != 1)
270  // $pds[$v] .= 's';
271  // in case of plural we need the current element's index + 1
272  $v++;
273  $x = $no . ' ' . $pds[$v];
274  if (($rcs > 0) && ($v >= 1))
275  $x .= ' ' . ossn_user_friendly_time($_tm, $rcs - 1);
276  return ossn_print('site:timepassed:text', $x);
277 }
278 
279 /**
280  * Register a uservalidation page
281  * @pages:
282  * uservalidate,
283  *
284  * @return bool
285  */
287  $page = $pages[0];
288  if (empty($page)) {
289  echo ossn_error_page();
290  }
291  switch ($page) {
292  case 'activate':
293  if (!empty($pages[1]) && !empty($pages[2])) {
294  $user = new OssnUser;
295  $user->guid = $pages[1];
296  if ($user->ValidateRegistration($pages[2])) {
297  ossn_trigger_message(ossn_print('user:account:validated'), 'success');
298  redirect();
299  } else {
300  ossn_trigger_message(ossn_print('user:account:validate:fail'), 'success');
301  redirect();
302  }
303  }
304  break;
305 
306  }
307 
308 }
309 /**
310  * Load a site language
311  *
312  * If user have different language then site language it will return user language
313  * What a hack lol its was not easy to override a site lanuage with user custom language
314  *
315  * @return string
316  */
317  function ossn_site_user_lang_code($hook, $type, $return, $params){
318  $lang = $return;
319  if(ossn_isLoggedin()){
320  $user = ossn_loggedin_user();
321  if(isset($user->language)){
322  $lang = $user->language;
323  }
324  }
325  return $lang;
326  }
327  /**
328  * Logout user from system
329  *
330  * @return boolean
331  */
332  function ossn_logout(){
333  unset($_SESSION['OSSN_USER']);
334  @session_destroy();
335  if(!isset($_SESSION['OSSN_USER'])){
336  return true;
337  }
338  return false;
339  }
340 ossn_register_callback('ossn', 'init', 'ossn_users');
341 ossn_add_hook('load:settings', 'language', 'ossn_site_user_lang_code');