/
user.php
1184 lines (932 loc) · 35.5 KB
/
user.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
// The source code packaged with this file is Free Software, Copyright (C) 2005 by
// Ricardo Galli <gallir at uib dot es>.
// It's licensed under the AFFERO GENERAL PUBLIC LICENSE unless stated otherwise.
// You can get copies of the licenses here:
// http://www.affero.org/oagpl.html
// AFFERO GENERAL PUBLIC LICENSE is also included in the file called "COPYING".
global $globals;
// Following functions are related to users but not done as a class so can be easily used with User and UserAuth
define('FRIEND_YES', '<img src="'.$globals['base_static'].'img/common/icon_friend_00.png" alt="del" width="18" height="16" title="'._('amigo').'"/>');
define('FRIEND_BOTH', '<img src="'.$globals['base_static'].'img/common/icon_friend_bi_00.png" alt="del" width="18" height="16" title="'._('amigos').'"/>');
define('FRIEND_NO', '<img src="'.$globals['base_static'].'img/common/icon_friend_no_00.png" alt="add" width="18" height="16" title="'._('agregar lista amigos').'"/>');
define('FRIEND_OTHER', '<img src="'.$globals['base_static'].'img/common/icon_friend_other_00.png" alt="add" width="18" height="16" title="'._('elegido').'"/>');
define('FRIEND_IGNORE', '<img src="'.$globals['base_static'].'img/common/icon_friend_ignore_00.png" alt="add" width="18" height="16" title="'._('ignorar').'"/>');
class User
{
protected $meta = false; // Used to store further user's info
protected $meta_modified = false;
public $read = false;
public $id = 0;
public $username = '';
public $username_register = '';
public $level = 'normal';
public $admin = false;
public $modification = false;
public $date = false;
public $ip = '';
public $pass = '';
public $email = '';
public $email_register = '';
public $avatar = 0;
public $names = '';
public $lang = 1;
public $comment_pref = 0;
public $karma = 6;
public $url = '';
public $prefs = array();
private $friendship;
const SQL = '
`user_id` `id`, `user_login` `username`, `user_login_register` `username_register`,
`user_level` `level`, `user_comment_pref` `comment_pref`, UNIX_TIMESTAMP(`user_date`) `date`,
`user_ip` `ip`, UNIX_TIMESTAMP(`user_modification`) `modification`, `user_pass` `pass`,
`user_email` `email`, `user_email_register` `email_register`, `user_names` `names`,
`user_lang` `lang`, `user_karma` `karma`, `user_avatar` `avatar`, `user_public_info` `public_info`,
`user_url` `url`, `user_adcode` `adcode`, `user_adchannel` `adchannel`, `user_phone` `phone`
';
public static function get_notification($id, $type)
{
global $db;
$r = (int)$db->get_var('
SELECT `counter`
FROM `notifications`
WHERE (
`user` = "'.(int)$id.'"
AND `type` = "'.$db->escape($type).'"
);
');
if ($r >= 0) {
return $r;
}
User::reset_notification($id, $type);
return 0;
}
public static function add_notification($id, $type, $value = 1)
{
global $db;
$id = (int)$id;
$value = (int)$value;
$type = $db->escape($type);
if ($value < 0) {
$value = abs($value);
return $db->query('
UPDATE `notifications`
SET `counter` = IF(`counter` - '.$value.' < 0, 0, `counter` - '.$value.')
WHERE (
`user` = "'.$id.'"
AND `type` = "'.$type.'"
AND `counter` >= "'.$value.'"
);
');
}
return $db->query('
INSERT INTO `notifications`
(`user`, `type`, `counter`)
VALUES ("'.$id.'", "'.$type.'", "'.$value.'")
ON DUPLICATE KEY UPDATE `counter` = `counter` + '.$value.';
');
}
public static function reset_notification($id, $type, $value = 0)
{
global $db;
$id = (int)$id;
$value = (int)$value;
$type = $db->escape($type);
return $db->query('
REPLACE INTO `notifications`
(`user`, `type`, `counter`)
VALUES ("'.$id.'", "'.$type.'", "'.$value.'");
');
}
public static function get_valid_username($name)
{
$name = strip_tags($name);
$name = preg_replace('/&.+?;/', '', $name); // kill entities
$name = preg_replace('/[\s\'\"\(\)]/', '_', $name);
// Don't let start with a number
if (preg_match('/^\d/', $name)) {
$name = 'u_' . $name;
}
return substr($name, 0, 24);
}
public static function getById($id)
{
global $db;
return $db->get_row('
SELECT *
FROM `users`
WHERE `user_id` = "'.(int)$id.'"
LIMIT 1;
');
}
public static function get_username($id)
{
global $db;
return $db->get_var('
SELECT `user_login`
FROM `users`
WHERE `user_id` = "'.(int)$id.'"
LIMIT 1;
');
}
public static function get_user_id($name)
{
global $db;
return $db->get_var('
SELECT `user_id`
FROM `users`
WHERE `user_login` = "'.$db->escape($name).'"
LIMIT 1;
');
}
public static function calculate_affinity($uid, $min_karma = 200)
{
global $globals, $db;
if (!$globals['karma_user_affinity']) {
return false;
}
$affinity = array();
$log = new Annotation("affinity-$uid");
if ($log->read() && $log->time > time() - 3600*4) {
return unserialize($log->text);
}
// Check vote-to-links affinity
$link_ids = $db->get_col("SELECT SQL_NO_CACHE link_id FROM links WHERE link_date > date_sub(now(), interval 30 day) and link_author = $uid and link_karma > $min_karma");
$nlinks = count($link_ids);
if ($nlinks > 4) {
$links = implode(',', $link_ids);
$votes = $db->get_results("select SQL_NO_CACHE vote_user_id as id, sum(vote_value/abs(vote_value)) as count from votes where vote_link_id in ($links) and vote_type='links' group by vote_user_id");
if ($votes) {
foreach ($votes as $vote) {
if ($vote->id > 0 && $vote->id != $uid && abs($vote->count) > max(1, $nlinks/10)) {
$w = min(1, $nlinks/10);
$w = max(0.7, $w);
$c = $vote->count/$nlinks * $w;
if ($vote->count > 0) {
$affinity[$vote->id] = round((1 - $c)*100); // store as int (percent) to save space,
} else {
$affinity[$vote->id] = round((-1 - $c)*100); // store as int (percent) to save space,
}
}
}
}
}
// Check vote-to-comments affinity
$comment_ids = $db->get_col("SELECT SQL_NO_CACHE comment_id FROM comments WHERE comment_date > date_sub(now(), interval 3 day) and comment_user_id = $uid and comment_votes > 2");
$ncomments = count($comment_ids);
if ($ncomments > 4) {
$comments = implode(',', $comment_ids);
$votes = $db->get_results("select SQL_NO_CACHE vote_user_id as id, sum(vote_value/abs(vote_value)) as count from votes where vote_link_id in ($comments) and vote_type='comments' group by vote_user_id");
if ($votes) {
foreach ($votes as $vote) {
if ($vote->id > 0 && $vote->id != $uid && abs($vote->count) > max(1, $ncomments / 10)) {
$w = min(1, $ncomments / 15);
$w = max(0.5, $w);
$c = $vote->count / $ncomments * $w;
if ($vote->count > 0) {
$a = round((1 - $c) * 100);
if (!isset($affinity[$vote->id]) || $a < $affinity[$vote->id]) {
$affinity[$vote->id] = $a;
}
} else {
$a = round((-1 - $c) * 100);
if (!isset($affinity[$vote->id]) || ($affinity[$vote->id] < 0 && $a > $affinity[$vote->id])) {
$affinity[$vote->id] = $a;
}
}
}
}
}
}
if (count($affinity) > 0) {
$log->text = serialize($affinity);
} else {
$affinity = false;
$log->text = '';
}
$log->store(time() + 86400*15); // Expire in 15 days
return $affinity;
}
public static function get_new_friends($user = 0)
{
global $db, $globals, $current_user;
$key = 'last_friend';
if (!$user && $current_user->user_id > 0) {
$user = $current_user->user_id;
}
$last_read = intval($db->get_var("select pref_value from prefs where pref_user_id = $user and pref_key = '$key'"));
return $db->get_col("select friend_from from friends where friend_type = 'manual' and friend_to = $user and friend_value > 0 and friend_date > FROM_UNIXTIME($last_read)");
}
public static function update_new_friends_date($time = false)
{
global $db, $globals, $current_user;
$key = 'last_friend';
if (!$current_user->user_id) {
return false;
}
if (!$time) {
$time = $globals['now'];
}
$previous = (int) $db->get_var("select pref_value from prefs where pref_user_id = $current_user->user_id and pref_key = '$key'");
if ($time <= $previous) {
return true;
}
$db->transaction();
$db->query("delete from prefs where pref_user_id = $current_user->user_id and pref_key = '$key'");
$db->query("insert into prefs set pref_user_id = $current_user->user_id, pref_key = '$key', pref_value = $time");
$db->commit();
return true;
}
// $user_id is the key in annotations
public static function get_affinity($id, $from = false)
{
global $current_user, $globals;
if (!$globals['karma_user_affinity']) {
return false;
}
$log = new Annotation("affinity-$id");
if (!$log->read()) {
return false;
}
$dict = unserialize($log->text);
if (!$dict || ! is_array($dict)) {
return false; // Failed to unserialize
}
if (!$from) {
return $dict; // Asked for the whole dict
}
return (abs($dict[$from]) <= 100) ? intval($dict[$from]) : false;
}
// Functions to manage "meta variables" that willl be stored as annotations and read automatically
// Variables that are accepted as "meta" (to avoid storing all
public static function meta_valid($property)
{
return in_array($property, array('bio', 'karma_log', 'karma_calculated'));
}
// Return the items for the top menu
// Used by /user.php and /profile.php
public static function get_menu_items($view, $user)
{
global $globals, $current_user;
switch ($view) {
case 'articles':
case 'articles_private':
case 'articles_shaken':
case 'articles_favorites':
case 'articles_discard':
$id = _('Artículos');
break;
case 'subs':
case 'subs_follow':
$id = _('subs');
break;
case 'history':
case 'shaken':
case 'friends_shaken':
case 'favorites':
case 'discard':
$id = _('historias');
break;
case 'commented':
case 'favorite_comments':
case 'shaken_comments':
case 'conversation':
$id = _('comentarios');
break;
case 'friends':
case 'friend_of':
case 'ignored':
case 'friends_new':
$id = _('relaciones');
break;
case 'categories':
case 'profile':
$id = _('perfil');
break;
case 'notes':
case 'notes_friends':
case 'notes_favorites':
case 'notes_conversation':
case 'notes_votes':
$id = _('notas');
break;
case 'notes_privates':
$id = _('privados');
break;
default:
do_error(_('opción inexistente'), 404);
break;
}
$items = array();
if ($user->id == $current_user->user_id) {
$items[] = new MenuOption(_('Crear artículo'), $globals['base_url'] . 'submit?type=article&write=true', $id, _('enviar nueva historia'), 'submit_new_post');
}
$items[] = new MenuOption(_('perfil'), $user->get_uri('profile'), $id, _('Información de usuario'));
$items[] = new MenuOption(_('historias'), $user->get_uri('history'), $id, _('Información de envíos'));
$items[] = new MenuOption(_('subs'), $user->get_uri('subs'), $id, _('Sub menéames'));
$items[] = new MenuOption(_('comentarios'), $user->get_uri('commented'), $id, _('Información de comentarios'));
$items[] = new MenuOption(_('notas'), $user->get_uri('notes'), $id, _('Página de notas'));
$items[] = new MenuOption(_('relaciones'), $user->get_uri('friends'), $id, _('Amigos e ignorados'));
if ($user->id == $current_user->user_id) {
$items[] = new MenuOption(_('privados'), $user->get_uri('notes_privates'), $id, _('Notas privadas'));
}
return $items;
}
public function __get($property)
{
if (!$this->id > 0 || ! User::meta_valid($property)) {
return false;
}
if ($this->meta === false && ! $this->meta_read()) {
return false;
}
if (isset($this->meta[$property])) {
return $this->meta[$property];
}
return false;
}
public function __set($property, $value)
{
if (!$this->id > 0) {
return;
}
if (!User::meta_valid($property)) {
$this->$property = $value;
return;
}
if ($this->meta === false) {
$this->meta_read();
}
$this->meta[$property] = $value;
$this->meta_modified = true;
}
public function meta_read()
{
$m = new Annotation("user_meta-$this->id");
if ($m->read() && ($this->meta = json_decode($m->text, true))) {
return true;
}
$this->meta = array();
return false;
}
public function meta_store()
{
if (!is_array($this->meta)) {
return;
}
$m = new Annotation("user_meta-$this->id");
$m->text = json_encode($this->meta);
$m->store();
$this->meta_modified = false;
}
// END meta
public function __construct($id = 0)
{
/*
// For stats
$this->total_votes;
$this->total_links;
$this->published_links;
$this->total_comments;
$this->total_posts;
*/
if ($id > 0) {
$this->id = $id;
$this->read();
}
}
public function disabled()
{
return ($this->level === 'disabled') || ($this->level === 'autodisabled');
}
public function disable($auto = false)
{
global $db;
require_once(mnminclude.'avatars.php');
require_once(mnminclude.'geo.php');
avatars_remove($this->id);
geo_delete('user', $this->id);
$this->username = '--'.$this->id.'--';
$this->email = "$this->id@disabled";
$this->url = '';
$this->level = $auto ? 'autodisabled' : 'disabled';
$this->names = 'disabled';
$this->public_info = '';
$this->adcode = '';
$this->adchannel = '';
$this->phone = '';
$this->avatar = 0;
$this->karma = 6;
$this->store();
syslog(LOG_INFO, "User disabled: $this->id");
// Delete relationships
$db->query("DELETE FROM friends WHERE friend_type='manual' and (friend_from = $this->id or friend_to = $this->id)");
/*
// Delete posts' conversations
$db->query("delete from conversations where conversation_type = 'post' and conversation_user_to = $this->id");
$db->transaction();
$conv = $db->get_col("select post_id from posts where post_user_id = $this->id");
if ($conv) {
foreach ($conv as $id) {
$db->query("delete from conversations where conversation_type = 'post' and conversation_from = $id");
}
}
$db->commit();
*/
// Delete posts
$db->query("delete from posts where post_user_id = $this->id");
// Delete user's meta
$db->query("delete from annotations where annotation_key = 'user_meta-$this->id'");
// Delete preferences
$db->query("DELETE FROM prefs WHERE pref_user_id = $this->id");
return true;
}
public function friendship()
{
global $db, $current_user;
if (($this->friendship === null) && $this->id && $current_user->user_id) {
$this->friendship = self::friend_exists($current_user->user_id, $this->id);
}
return $this->friendship;
}
public function ignored()
{
global $current_user;
return !$current_user->admin && ($this->friendship() === -1);
}
public function friend()
{
return ($this->friendship() === 1);
}
public function read()
{
global $db, $current_user;
$id = $this->id;
if ($this->id > 0) {
$where = "user_id = $id";
} elseif (!empty($this->username)) {
$where = "user_login='".$db->escape(mb_substr($this->username, 0, 64))."'";
} elseif (!empty($this->email)) {
$where = "user_email='".$db->escape(mb_substr($this->email, 0, 64))."' and user_level != 'disabled' and user_level != 'autodisabled'";
}
$this->stats = false;
if (empty($where) || !($result = $db->get_row("SELECT ".User::SQL." FROM users WHERE $where limit 1"))) {
return $this->read = false;
}
foreach (get_object_vars($result) as $var => $value) {
$this->$var = $value;
}
$this->admin = (($this->level === 'admin') || ($this->level === 'god'));
return $this->read = true;
}
public function store($full_save = true)
{
global $db, $current_user, $globals;
if (!$this->date) {
$this->date = $globals['now'];
}
$user_login = $db->escape($this->username);
$user_login_register = $db->escape($this->username_register);
$user_level = $this->level;
$user_comment_pref = $this->comment_pref;
$user_karma = $this->karma;
$user_avatar = $this->avatar;
$user_date = $this->date;
$user_ip = $this->ip;
$user_pass = $db->escape($this->pass);
$user_lang = $this->lang;
$user_email = $db->escape($this->email);
$user_email_register = $db->escape($this->email_register);
$user_names = $db->escape($this->names);
$user_public_info = $db->escape(__($this->public_info));
$user_url = $db->escape(preg_replace('/\/+$/', '', htmlspecialchars($this->url))); // remove trailing "/s"
$user_adcode = $db->escape($this->adcode);
$user_adchannel = $db->escape($this->adchannel);
$user_phone = $db->escape($this->phone);
if (!$this->id) {
$db->query("INSERT INTO users (user_login, user_login_register, user_level, user_karma, user_date, user_ip, user_pass, user_lang, user_email, user_email_register, user_names, user_public_info, user_url, user_adcode, user_adchannel, user_phone) VALUES ('$user_login', '$user_login_register', '$user_level', $user_karma, FROM_UNIXTIME($user_date), '$user_ip', '$user_pass', $user_lang, '$user_email', '$user_email_register', '$user_names', '$user_public_info', '$user_url', '$user_adcode', '$user_adchannel', '$user_phone')");
$this->id = $db->insert_id;
} else {
if ($full_save) {
$modification = ', user_modification = now() ' ;
}
$db->query("UPDATE users set user_login='$user_login', user_level='$user_level', user_karma=$user_karma, user_avatar=$user_avatar, user_date=FROM_UNIXTIME($user_date), user_ip='$user_ip', user_pass='$user_pass', user_lang=$user_lang, user_comment_pref=$user_comment_pref, user_email='$user_email', user_email_register='$user_email_register', user_names='$user_names', user_public_info='$user_public_info', user_url='$user_url', user_adcode='$user_adcode', user_adchannel='$user_adchannel', user_phone='$user_phone' $modification WHERE user_id=$this->id");
}
if ($this->meta_modified) {
$this->meta_store();
}
}
public function all_stats()
{
global $db, $globals, $current_user;
if ($this->stats) {
return;
}
if (!$this->read) {
$this->read();
}
$do_cache = ($this->date < $globals['now'] - 86400); // Don't cache for new users
$cache_time = 7200;
$stats = new Annotation('user_stats-'.$this->id);
$time = (int)$db->get_var('
SELECT UNIX_TIMESTAMP(MAX(vote_date))
FROM votes
WHERE (
vote_user_id = "'.(int)$this->id.'"
AND vote_type IN ("links", "posts", "comments")
);
');
if (
$do_cache && $stats->read()
&& (
($stats->time > ($globals['now'] - $cache_time))
|| $globals['bot']
|| $current_user->user_id == 0
|| ($stats->time > $time)
)
) {
$obj = unserialize($stats->text);
} elseif ($globals['bot'] || !$current_user->user_id) {
return; // Don't calculate stats por bots
} else {
$obj = (object)[
'total_votes' => (int) $db->get_var("SELECT SQL_CACHE COUNT(*) FROM votes WHERE vote_type='links' and vote_user_id = $this->id;"),
'total_links' => (int) $db->get_var("SELECT SQL_CACHE COUNT(*) FROM links WHERE link_author = $this->id and link_votes > 0;"),
'published_links' => (int) $db->get_var("SELECT SQL_CACHE COUNT(*) FROM links WHERE link_author = $this->id AND link_status = 'published';"),
'total_comments' => (int) $db->get_var("SELECT SQL_CACHE COUNT(*) FROM comments WHERE comment_user_id = $this->id;"),
'total_posts' => (int) $db->get_var("SELECT SQL_CACHE COUNT(*) FROM posts WHERE post_user_id = $this->id;"),
'total_friends' => (int) $db->get_var("SELECT SQL_CACHE COUNT(*) FROM friends WHERE friend_to = $this->id;"),
'total_images' => Upload::user_uploads($this->id) - Upload::user_uploads($this->id, false, 'private'),
];
if ($do_cache) {
$stats->text = serialize($obj);
$stats->store($globals['now'] + 86400 * 90); // Expires in 90 days
}
}
foreach (get_object_vars($obj) as $var => $value) {
$this->$var = $value;
}
$this->stats = true;
}
public function getMedals()
{
global $globals, $db;
if (($this->level === 'disabled') || ($this->level === 'autodisabled')) {
return array();
}
$this->all_stats();
$medals = array();
if ($medal = $this->getMedalAntiquity()) {
$medals[] = $medal;
}
if ($medal = $this->getMedalRatio()) {
$medals[] = $medal;
}
if ($medal = $this->getMedalPublished()) {
$medals[] = $medal;
}
if ($medal = $this->getMedalFriends()) {
$medals[] = $medal;
}
return $medals;
}
private function getMedalAntiquity()
{
global $globals;
if (($this->total_votes <= 20) && ($this->total_links <= 20)) {
return;
}
$years = intval(($globals['now'] - $this->date) / (86400 * 365));
if ($years > 2) {
$type = 'gold';
} elseif ($years > 1) {
$type = 'silver';
} elseif ($years > 0) {
$type = 'bronze';
} else {
return;
}
return array(
'type' => $type,
'title' => __('antigüedad > %s años', $years)
);
}
private function getMedalRatio()
{
if (($this->total_links <= 20) || ($this->published_links <= 2)) {
return;
}
$ratio = round($this->published_links / $this->total_links, 2);
if ($ratio > 0.15) {
$type = 'gold';
} elseif ($ratio > 0.10) {
$type = 'silver';
} elseif ($ratio > 0.08) {
$type = 'bronze';
} else {
return;
}
return array(
'type' => $type,
'title' => __('porcentaje publicadas (%s)', $ratio)
);
}
private function getMedalPublished()
{
if (empty($this->published_links)) {
return;
}
$ratio = round($this->published_links / $this->total_links, 2);
if ($this->published_links > 50) {
$type = 'gold';
} elseif ($this->published_links > 20) {
$type = 'silver';
} elseif (($this->published_links > 2) || (($this->published_links > 10) && ($ratio > 0.05))) {
$type = 'bronze';
} else {
return;
}
return array(
'type' => $type,
'title' => __('publicadas (%s)', $this->published_links)
);
}
private function getMedalFriends()
{
if ($this->total_friends > 200) {
$type = 'gold';
} elseif ($this->total_friends > 100) {
$type = 'silver';
} elseif ($this->total_friends > 50) {
$type = 'bronze';
} else {
return;
}
return array(
'type' => $type,
'title' => __('amigos (%s)', $this->total_friends)
);
}
public function getMedalImage($type, $title = '')
{
global $globals;
return '<img src="'.$globals['base_static'].'img/common/medal_'.$type.'_1.png" alt="" title="'.$title.'"/>';
}
public function print_medals()
{
global $globals, $db;
if (($this->level === 'disabled') || ($this->level === 'autodisabled')) {
return;
}
foreach ($this->getMedals() as $medal) {
echo $this->getMedalImage($medal['type'], $medal['title']);
}
}
public function ranking($format = true)
{
global $db;
if (!$this->read) {
$this->read();
}
$value = (int)$db->get_var('
SELECT SQL_CACHE COUNT(*)
FROM users
WHERE user_karma > "'.(float)$this->karma.'";
') + 1;
return $format ? get_human_number($value) : $value;
}
public function blogs()
{
global $db;
return $db->get_var("select count(distinct link_blog) from links where link_author=$this->id");
}
public function get_api_key()
{
global $site_key;
return substr(md5($this->user.$this->date.$this->pass.$site_key), 0, 10);
}
public function get_api_token($version = 0)
{
global $site_key;
return "$version:".md5($this->username_register.$this->email_register.$this->date.$this->id.$site_key);
}
public function get_latlng()
{
require_once(mnminclude.'geo.php');
return geo_latlng('user', $this->id);
}
public function get_uri($view = '', $anchor = '')
{
global $globals;
$uri = $globals['base_url_general'].'user/'.htmlspecialchars($this->username);
$uri .= $view ? ('/'.$view) : '';
if (!empty($anchor)) {
$uri .= '#' . $anchor;
}
return $uri;
}
public function add_karma($inc, $log = false)
{
global $globals;
$this->karma = min($globals['max_karma'], $this->karma + $inc);
$this->karma = max($globals['min_karma'], $this->karma);
$this->karma = round($this->karma, 2);
if (!empty($log) && mb_strlen($log) > 5) {
$this->karma_log .= "$log: $inc, " . _('nuevo karma') . ": $this->karma\n";
$this->karma_calculated = time();
}
$this->store();
}
public function get_prefs($force = false)
{
if (empty($this->id)) {
return array();
}
if ($this->prefs && ($force === false)) {
return $this->prefs;
}
global $db;
$results = $db->get_results('SELECT pref_key, pref_value FROM prefs where pref_user_id = "'.$this->id.'";');
foreach ($results as $result) {
$this->prefs[$result->pref_key] = $result->pref_value;
}
return $this->prefs;
}
public static function friend_exists($from, $to)
{
global $db;
if ($from == $to) {
return 0;
}
return (int)$db->get_var('
SELECT SQL_NO_CACHE friend_value
FROM friends
WHERE (
friend_type = "manual"
AND friend_from = "'.(int)$from.'"
AND friend_to = "'.(int)$to.'"
)
LIMIT 1;
');
}
public static function friend_insert($from, $to, $value = 1)
{
global $db;
if ($from == $to) {
return 0;
}
if ((int)$db->get_var("SELECT SQL_NO_CACHE count(*) FROM users WHERE user_id IN ($from, $to)") !== 2) {
return false;
}
return $db->query("REPLACE INTO friends (friend_type, friend_from, friend_to, friend_value) VALUES ('manual', $from, $to, $value)");
}
public static function friend_delete($from, $to)
{
global $db;
return $db->query("DELETE FROM friends WHERE friend_type='manual' and friend_from = $from and friend_to = $to");
}
public static function friend_add_delete($from, $to)
{
if ($from == $to) {