/
OTS_Account.php
1174 lines (1038 loc) · 33 KB
/
OTS_Account.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
/**#@+
* @version 0.0.1
*/
/**
* @package POT
* @version 0.1.5
* @author Wrzasq <wrzasq@gmail.com>
* @copyright 2007 - 2008 (C) by Wrzasq
* @license http://www.gnu.org/licenses/lgpl-3.0.txt GNU Lesser General Public License, Version 3
*/
/**
* OTServ account abstraction.
*
* @package POT
* @version 0.1.5
* @property string $name Account name.
* @property string $password Password.
* @property string $eMail Email address.
* @property int $premiumEnd Timestamp of PACC end.
* @property bool $blocked Blocked flag state.
* @property bool $deleted Deleted flag state.
* @property bool $warned Warned flag state.
* @property bool $banned Ban state.
* @property-read int $id Account number.
* @property-read bool $loaded Loaded state.
* @property-read OTS_Players_List $playersList Characters of this account.
* @property-read int $access Access level.
* @tutorial POT/Accounts.pkg
*/
class OTS_Account extends OTS_Row_DAO implements IteratorAggregate, Countable
{
/**
* Account data.
*
* @var array
* @version 0.1.5
*/
private $data = array('email' => '', 'blocked' => false, 'rlname' => '','location' => '', 'country' => '','web_flags' => 0, 'lastday' => 0, 'premdays' => 0, 'created' => 0);
public static $cache = array();
/**
* Creates new account.
*
* <p>
* This method creates new account with given name. Account number is generated automaticly and saved into {@link OTS_Account::getId() ID field}.
* </p>
*
* <p>
* If you won't specify account name then random one will be generated.
* </p>
*
* <p>
* If you use own account name then it will be returned after success, and exception will be generated if it will be alredy used as name will be simply used in query with account create attempt.
* </p>
*
* @version 0.1.5
* @since 0.1.5
* @param string $name Account name.
* @return string Account name.
* @throws PDOException On PDO operation error.
* @example examples/create.php create.php
* @tutorial POT/Accounts.pkg#create
*/
public function createNamed($name = null)
{
// if name is not passed then it will be generated randomly
if( !isset($name) )
{
// reads already existing names
foreach( $this->db->query('SELECT ' . $this->db->fieldName('name') . ' FROM ' . $this->db->tableName('accounts') )->fetchAll() as $account)
{
$exist[] = $account['name'];
}
// initial name
$name = uniqid();
// repeats until name is unique
while( in_array($name, $exist) )
{
$name .= '_';
}
// resets array for account numbers loop
$exist = array();
}
// saves blank account info
$this->db->exec('INSERT INTO ' . $this->db->tableName('accounts') . ' (' . $this->db->fieldName('name') . ', ' . $this->db->fieldName('password') . ', ' . $this->db->fieldName('email') . ') VALUES (' . $this->db->quote($name) . ', \'\', \'\')');
// reads created account's ID
$this->data['id'] = $this->db->lastInsertId();
// return name of newly created account
return $name;
}
/**
* Creates new account.
*
* <p>
* Create new account in given range (1 - 9999999 by default).
* </p>
*
* <p>
* Note: If account name won't be speciffied random will be created.
* </p>
*
* <p>
* Note: Since 0.0.3 version this method doesn't require buffered queries.
* </p>
*
* <p>
* Note: Since 0.1.5 version you should use {@link OTS_Account::createNamed() createNamed() method} since OTServ now uses account names.
* </p>
*
* <p>
* Note: Since 0.1.1 version this method throws {@link E_OTS_Generic E_OTS_Generic} exceptions instead of general Exception class objects. Since all exception classes are child classes of Exception class so your old code will still handle all exceptions.
* </p>
*
* <p>
* Note: Since 0.1.5 version this method no longer creates account as blocked.
* </p>
*
* @version 0.1.5
* @param string $name Account name.
* @param int $id Account id.
* @return int Created account number.
* @throws PDOException On PDO operation error.
* @throws Exception ON lastInsertId error.
* @deprecated 0.1.5 Use createNamed().
*/
public function create($name = NULL, $id = NULL)
{
// saves blank account info
$this->db->exec('INSERT INTO `accounts` (' . (isset($id) ? '`id`,' : '') . (isset($name) ? '`name`,' : '') . '`password`, `email`, `created`) VALUES (' . (isset($id) ? $id . ',' : '') . (isset($name) ? $this->db->quote($name) . ',' : '') . ' \'\', \'\',' . time() . ')');
if(isset($name))
$this->data['name'] = $name;
$lastInsertId = $this->db->lastInsertId();
if($lastInsertId != 0) {
$this->data['id'] = $lastInsertId;
}
elseif (isset($id)) {
$this->data['id'] = $id;
}
else {
throw new Exception(__CLASS__ . ':' . __METHOD__ . ' unexpected error. Please report to MyAAC Developers.');
}
return $this->data['id'];
}
/**
* @version 0.0.6
* @since 0.0.4
* @param OTS_Group $group Group to be assigned to account.
* @param int $min Minimum number.
* @param int $max Maximum number.
* @return int Created account number.
* @deprecated 0.0.6 There is no more group_id field in database, use create().
*/
public function createEx(OTS_Group $group, $min = 1, $max = 9999999)
{
return $this->create($min, $max);
}
/**
* Loads account with given number.
*
* @version 0.0.6
* @param int $id Account number.
* @throws PDOException On PDO operation error.
*/
public function load($id, $fresh = false)
{
if(!$fresh && isset(self::$cache[$id])) {
$this->data = self::$cache[$id];
return;
}
// SELECT query on database
$this->data = $this->db->query('SELECT `id`, ' . ($this->db->hasColumn('accounts', 'name') ? '`name`,' : '') . '`password`, `email`, `blocked`, `rlname`, `location`, `country`, `web_flags`, ' . ($this->db->hasColumn('accounts', 'premdays') ? '`premdays`, ' : '') . ($this->db->hasColumn('accounts', 'lastday') ? '`lastday`, ' : ($this->db->hasColumn('accounts', 'premend') ? '`premend`,' : ($this->db->hasColumn('accounts', 'premium_ends_at') ? '`premium_ends_at`,' : ''))) . '`created` FROM `accounts` WHERE `id` = ' . (int) $id)->fetch();
self::$cache[$id] = $this->data;
}
/**
* Loads account by it's name.
*
* <p>
* Note: Since 0.1.5 version this method loads account by it's name not by e-mail address. To find account by it's e-mail address use {@link OTS_Account::findByEMail() findByEMail() method}.
* </p>
*
* @version 0.1.5
* @since 0.0.2
* @param string $name Account's name.
* @throws PDOException On PDO operation error.
*/
public function find($name)
{
// finds player's ID
$id = $this->db->query('SELECT `id` FROM `accounts` WHERE `name` = ' . $this->db->quote($name) )->fetch();
// if anything was found
if( isset($id['id']) )
{
$this->load($id['id']);
}
}
/**
* Loads account by it's e-mail address.
*
* @version 0.1.5
* @since 0.1.5
* @param string $email Account's e-mail address.
* @throws PDOException On PDO operation error.
*/
public function findByEMail($email)
{
// finds player's ID
$id = $this->db->query('SELECT `id` FROM `accounts` WHERE `email` = ' . $this->db->quote($email) )->fetch();
// if anything was found
if( isset($id['id']) )
{
$this->load($id['id']);
}
}
/**
* Checks if object is loaded.
*
* @return bool Load state.
*/
public function isLoaded()
{
return isset($this->data['id']);
}
/**
* Updates account in database.
*
* <p>
* Unlike other DAO objects account can't be saved without ID being set. It means that you can't just save unexisting account to automaticly create it. First you have to create record by using {@link OTS_Account::createName() createNamed() method}
* </p>
*
* <p>
* Note: Since 0.0.3 version this method throws {@link E_OTS_NotLoaded E_OTS_NotLoaded exception} instead of triggering E_USER_WARNING.
* </p>
*
* @version 0.1.5
* @throws E_OTS_NotLoaded If account doesn't have ID assigned.
* @throws PDOException On PDO operation error.
*/
public function save()
{
if( !isset($this->data['id']) )
{
throw new E_OTS_NotLoaded();
}
$field = 'lastday';
if($this->db->hasColumn('accounts', 'premend')) { // othire
$field = 'premend';
if(!isset($this->data['premend'])) {
$this->data['premend'] = 0;
}
}
else if($this->db->hasColumn('accounts', 'premium_ends_at')) {
$field = 'premium_ends_at';
if(!isset($this->data['premium_ends_at'])) {
$this->data['premium_ends_at'] = 0;
}
}
// UPDATE query on database
$this->db->exec('UPDATE `accounts` SET ' . ($this->db->hasColumn('accounts', 'name') ? '`name` = ' . $this->db->quote($this->data['name']) . ',' : '') . '`password` = ' . $this->db->quote($this->data['password']) . ', `email` = ' . $this->db->quote($this->data['email']) . ', `blocked` = ' . (int) $this->data['blocked'] . ', `rlname` = ' . $this->db->quote($this->data['rlname']) . ', `location` = ' . $this->db->quote($this->data['location']) . ', `country` = ' . $this->db->quote($this->data['country']) . ', `web_flags` = ' . (int) $this->data['web_flags'] . ', ' . ($this->db->hasColumn('accounts', 'premdays') ? '`premdays` = ' . (int) $this->data['premdays'] . ',' : '') . '`' . $field . '` = ' . (int) $this->data[$field] . ' WHERE `id` = ' . $this->data['id']);
}
/**
* Account number.
*
* <p>
* Note: Since 0.0.3 version this method throws {@link E_OTS_NotLoaded E_OTS_NotLoaded} exception instead of triggering E_USER_WARNING.
* </p>
*
* @version 0.0.3
* @return int Account number.
* @throws E_OTS_NotLoaded If account is not loaded.
*/
public function getId()
{
if( !isset($this->data['id']) )
{
throw new E_OTS_NotLoaded();
}
return $this->data['id'];
}
public function getRLName()
{
if( !isset($this->data['rlname']) )
{
throw new E_OTS_NotLoaded();
}
return $this->data['rlname'];
}
public function getLocation()
{
if( !isset($this->data['location']) )
{
throw new E_OTS_NotLoaded();
}
return $this->data['location'];
}
public function getCountry()
{
if( !isset($this->data['country']) )
{
throw new E_OTS_NotLoaded();
}
return $this->data['country'];
}
public function getWebFlags()
{
if( !isset($this->data['web_flags']) )
{
throw new E_OTS_NotLoaded();
}
return $this->data['web_flags'];
}
public function hasFlag($flag)
{
if(!isset($this->data['web_flags'])) {
throw new E_OTS_NotLoaded();
}
return ($this->data['web_flags'] & $flag) == $flag;
}
public function isAdmin()
{
return $this->hasFlag(FLAG_ADMIN) || $this->isSuperAdmin();
}
public function isSuperAdmin()
{
return $this->hasFlag(FLAG_SUPER_ADMIN);
}
public function getPremDays()
{
if(!isset($this->data['lastday']) && !isset($this->data['premend'])) {
throw new E_OTS_NotLoaded();
}
if(isset($this->data['premium_ends_at'])) {
return round(($this->data['premium_ends_at'] - time()) / (24 * 60 * 60), 2);
}
if(isset($this->data['premend'])) {
return round(($this->data['premend'] - time()) / (24 * 60 * 60), 2);
}
if($this->data['premdays'] == 0) {
return 0;
}
global $config;
if(isset($config['lua']['freePremium']) && getBoolean($config['lua']['freePremium'])) return -1;
return $this->data['premdays'] - (date("z", time()) + (365 * (date("Y", time()) - date("Y", $this->data['lastday']))) - date("z", $this->data['lastday']));
}
public function getLastLogin()
{
if( !isset($this->data['lastday']) )
{
throw new E_OTS_NotLoaded();
}
return $this->data['lastday'];
}
public function isPremium()
{
global $config;
if(isset($config['lua']['freePremium']) && getBoolean($config['lua']['freePremium'])) return true;
if(isset($this->data['premium_ends_at'])) {
return $this->data['premium_ends_at'] > time();
}
if(isset($this->data['premend'])) {
return $this->data['premend'] > time();
}
return ($this->data['premdays'] - (date("z", time()) + (365 * (date("Y", time()) - date("Y", $this->data['lastday']))) - date("z", $this->data['lastday'])) > 0);
}
public function getCreated()
{
if( !isset($this->data['created']) )
{
throw new E_OTS_NotLoaded();
}
return $this->data['created'];
}
/**
* Name.
*
* @version 0.7.5
* @since 0.7.5
* @throws E_OTS_NotLoaded If account is not loaded.
*/
public function setPremDays($premdays)
{
$this->data['premdays'] = (int) $premdays;
$this->data['premend'] = time() + ($premdays * 24 * 60 * 60);
}
public function setRLName($name)
{
$this->data['rlname'] = (string) $name;
}
public function setLocation($location)
{
$this->data['location'] = (string) $location;
}
public function setCountry($country)
{
$this->data['country'] = (string) $country;
}
public function setLastLogin($lastlogin)
{
$this->data['lastday'] = (int) $lastlogin;
}
public function setWebFlags($webflags)
{
$this->data['web_flags'] = (int) $webflags;
}
/**
* Name.
*
* @version 0.1.5
* @since 0.1.5
* @return string Name.
* @throws E_OTS_NotLoaded If account is not loaded.
*/
public function getName()
{
if( !isset($this->data['name']) )
{
throw new E_OTS_NotLoaded();
}
return $this->data['name'];
}
/**
* Sets account's name.
*
* <p>
* This method only updates object state. To save changes in database you need to use {@link OTS_Account::save() save() method} to flush changed to database.
* </p>
*
* @version 0.1.5
* @since 0.1.5
* @param string $name Account name.
*/
public function setName($name)
{
$this->data['name'] = (string) $name;
}
/**
* Account's password.
*
* <p>
* Doesn't matter what password hashing mechanism is used by OTServ - this method will just return RAW database content. It is not possible to "decrypt" hashed strings, so it even wouldn't be possible to return real password string.
* </p>
*
* <p>
* Note: Since 0.0.3 version this method throws {@link E_OTS_NotLoaded E_OTS_NotLoaded} exception instead of triggering E_USER_WARNING.
* </p>
*
* @version 0.0.3
* @return string Password.
* @throws E_OTS_NotLoaded If account is not loaded.
*/
public function getPassword()
{
if( !isset($this->data['password']) )
{
throw new E_OTS_NotLoaded();
}
return $this->data['password'];
}
/**
* Sets account's password.
*
* <p>
* This method only updates object state. To save changes in database you need to use {@link OTS_Account::save() save() method} to flush changed to database.
* </p>
*
* <p>
* Remember that this method just sets database field's content. It doesn't apply any hashing/encryption so if OTServ uses hashing for passwords you have to apply it by yourself before passing string to this method.
* </p>
*
* @param string $password Password.
*/
public function setPassword($password)
{
$this->data['password'] = (string) $password;
}
/**
* E-mail address.
*
* <p>
* Note: Since 0.0.3 version this method throws {@link E_OTS_NotLoaded E_OTS_NotLoaded} exception instead of triggering E_USER_WARNING.
* </p>
*
* @version 0.0.3
* @return string E-mail.
* @throws E_OTS_NotLoaded If account is not loaded.
*/
public function getEMail()
{
if( !isset($this->data['email']) )
{
throw new E_OTS_NotLoaded();
}
return $this->data['email'];
}
/**
* Sets account's email.
*
* <p>
* This method only updates object state. To save changes in database you need to use {@link OTS_Account::save() save() method} to flush changed to database.
* </p>
*
* @param string $email E-mail address.
*/
public function setEMail($email)
{
$this->data['email'] = (string) $email;
}
/**
* Checks if account is blocked.
*
* <p>
* Note: Since 0.0.3 version this method throws {@link E_OTS_NotLoaded E_OTS_NotLoaded} exception instead of triggering E_USER_WARNING.
* </p>
*
* @version 0.0.3
* @return bool Blocked state.
* @throws E_OTS_NotLoaded If account is not loaded.
*/
public function isBlocked()
{
if( !isset($this->data['blocked']) )
{
throw new E_OTS_NotLoaded();
}
return $this->data['blocked'];
}
/**
* Unblocks account.
*
* <p>
* This method only updates object state. To save changes in database you need to use {@link OTS_Account::save() save() method} to flush changed to database.
* </p>
*/
public function unblock()
{
$this->data['blocked'] = false;
}
/**
* Blocks account.
*
* <p>
* This method only updates object state. To save changes in databaseed to use {@link OTS_Account::save() save() method} to flush changed to database.
* </p>
*/
public function block()
{
$this->data['blocked'] = true;
}
/**
* Reads custom field.
*
* <p>
* Reads field by it's name. Can read any field of given record that exists in database.
* </p>
*
* <p>
* Note: You should use this method only for fields that are not provided in standard setters/getters (SVN fields). This method runs SQL query each time you call it so it highly overloads used resources.
* </p>
*
* @version 0.0.5
* @since 0.0.3
* @param string $field Field name.
* @return string Field value.
* @throws E_OTS_NotLoaded If account is not loaded.
* @throws PDOException On PDO operation error.
*/
public function getCustomField($field)
{
if( !isset($this->data['id']) )
{
throw new E_OTS_NotLoaded();
}
$value = $this->db->query('SELECT ' . $this->db->fieldName($field) . ' FROM ' . $this->db->tableName('accounts') . ' WHERE ' . $this->db->fieldName('id') . ' = ' . $this->data['id'])->fetch();
return $value[$field];
}
/**
* Writes custom field.
*
* <p>
* Write field by it's name. Can write any field of given record that exists in database.
* </p>
*
* <p>
* Note: You should use this method only for fields that are not provided in standard setters/getters (SVN fields). This method runs SQL query each time you call it so it highly overloads used resources.
* </p>
*
* <p>
* Note: Make sure that you pass $value argument of correct type. This method determinates whether to quote field name. It is safe - it makes you sure that no unproper queries that could lead to SQL injection will be executed, but it can make your code working wrong way. For example: $object->setCustomField('foo', '1'); will quote 1 as as string ('1') instead of passing it as a integer.
* </p>
*
* @version 0.0.5
* @since 0.0.3
* @param string $field Field name.
* @param mixed $value Field value.
* @throws E_OTS_NotLoaded If account is not loaded.
* @throws PDOException On PDO operation error.
*/
public function setCustomField($field, $value)
{
if( !isset($this->data['id']) )
{
throw new E_OTS_NotLoaded();
}
// quotes value for SQL query
if(!( is_int($value) || is_float($value) ))
{
$value = $this->db->quote($value);
}
$this->db->exec('UPDATE ' . $this->db->tableName('accounts') . ' SET ' . $this->db->fieldName($field) . ' = ' . $value . ' WHERE ' . $this->db->fieldName('id') . ' = ' . $this->data['id']);
}
/**
* @version 0.1.0
* @return array Array of OTS_Player objects from given account.
* @throws E_OTS_NotLoaded If account is not loaded.
* @deprecated 0.0.5 Use getPlayersList().
*/
public function getPlayers()
{
if( !isset($this->data['id']) )
{
throw new E_OTS_NotLoaded();
}
$players = array();
foreach( $this->db->query('SELECT ' . $this->db->fieldName('id') . ' FROM ' . $this->db->tableName('players') . ' WHERE ' . $this->db->fieldName('account_id') . ' = ' . $this->data['id'])->fetchAll() as $player)
{
// creates new object
$object = new OTS_Player();
$object->load($player['id']);
$players[] = $object;
}
return $players;
}
/**
* List of characters on account.
*
* <p>
* In difference to {@link OTS_Account::getPlayers() getPlayers() method} this method returns filtered {@link OTS_Players_List OTS_Players_List} object instead of array of {@link OTS_Player OTS_Player} objects. It is more effective since OTS_Player_List doesn't perform all rows loading at once.
* </p>
*
* <p>
* Note: Returned object is only prepared, but not initialised. When using as parameter in foreach loop it doesn't matter since it will return it's iterator, but if you will wan't to execute direct operation on that object you will need to call {@link OTS_Base_List::rewind() rewind() method} first.
* </p>
*
* @version 0.1.4
* @since 0.0.5
* @return OTS_Players_List List of players from current account.
* @throws E_OTS_NotLoaded If account is not loaded.
*/
public function getPlayersList()
{
if( !isset($this->data['id']) )
{
throw new E_OTS_NotLoaded();
}
// creates filter
$filter = new OTS_SQLFilter();
$filter->compareField('account_id', (int) $this->data['id']);
// creates list object
$list = new OTS_Players_List();
$list->setFilter($filter);
return $list;
}
/**
* @version 0.1.5
* @since 0.0.5
* @param int $time Time for time until expires (0 - forever).
* @throws PDOException On PDO operation error.
* @deprecated 0.1.5 Use OTS_AccountBan class.
*/
public function ban($time = 0)
{
// can't ban nothing
if( !$this->isLoaded() )
{
throw new E_OTS_NotLoaded();
}
// creates ban entry
$ban = new OTS_AccountBan();
$ban->setValue($this->data['id']);
$ban->setExpires($time);
$ban->setAdded( time() );
$ban->activate();
$ban->save();
}
/**
* @version 0.1.5
* @since 0.0.5
* @throws PDOException On PDO operation error.
* @deprecated 0.1.5 Use OTS_AccountBan class.
*/
public function unban()
{
// can't unban nothing
if( !$this->isLoaded() )
{
throw new E_OTS_NotLoaded();
}
// deletes ban entry
$ban = new OTS_AccountBan();
$ban->find($this->data['id']);
$ban->delete();
}
/**
* @version 0.1.5
* @since 0.0.5
* @return bool True if account is banned, false otherwise.
* @throws PDOException On PDO operation error.
* @deprecated 0.1.5 Use OTS_AccountBan class.
*/
public function isBanned()
{
// nothing can't be banned
if( !$this->isLoaded() )
{
throw new E_OTS_NotLoaded();
}
if( !isset($this->data['banned']) )
$this->loadBan();
return ($this->data['banned'] === true);
}
public function getBanTime()
{
// nothing can't be banned
if( !$this->isLoaded() )
{
throw new E_OTS_NotLoaded();
}
if( !isset($this->data['banned_time']) )
$this->loadBan();
return $this->data['banned_time'];
}
public function loadBan()
{
// nothing can't be banned
if( !$this->isLoaded() )
{
throw new E_OTS_NotLoaded();
}
if($this->db->hasTable('account_bans')) {
$ban = $this->db->query('SELECT `expires_at` FROM `account_bans` WHERE `account_id` = ' . $this->data['id'] . ' AND (`expires_at` > ' . time() .' OR `expires_at` = -1) ORDER BY `expires_at` DESC')->fetch();
$this->data['banned'] = isset($ban['expires_at']);
$this->data['banned_time'] = isset($ban['expires_at']) ? $ban['expires_at'] : 0;
}
else if($this->db->hasTable('bans')) {
if($this->db->hasColumn('bans', 'active')) {
$ban = $this->db->query('SELECT `active`, `expires` FROM `bans` WHERE (`type` = 3 OR `type` = 5) AND `active` = 1 AND `value` = ' . $this->data['id'] . ' AND (`expires` > ' . time() .' OR `expires` = -1) ORDER BY `expires` DESC')->fetch();
$this->data['banned'] = isset($ban['active']);
$this->data['banned_time'] = isset($ban['expires']) ? $ban['expires'] : 0;
}
else { // tfs 0.2
$ban = $this->db->query('SELECT `time` FROM `bans` WHERE (`type` = 3 OR `type` = 5) AND `account` = ' . $this->data['id'] . ' AND (`time` > ' . time() .' OR `time` = -1) ORDER BY `time` DESC')->fetch();
$this->data['banned'] = isset($ban['time']) && ($ban['time'] == -1 || $ban['time'] > 0);
$this->data['banned_time'] = isset($ban['time']) ? $ban['time'] : 0;
}
}
else {
$this->data['banned'] = false;
$this->data['banned_time'] = 0;
}
}
/**
* Deletes account.
*
* <p>
* This method physicly deletes account from database! To set <i>deleted</i> flag use {@link OTS_Account::setDeleted() setDeleted() method}.
* </p>
*
* @version 0.0.5
* @since 0.0.5
* @throws E_OTS_NotLoaded If account is not loaded.
* @throws PDOException On PDO operation error.
*/
public function delete()
{
if( !isset($this->data['id']) )
{
throw new E_OTS_NotLoaded();
}
// deletes row from database
$this->db->exec('DELETE FROM ' . $this->db->tableName('accounts') . ' WHERE ' . $this->db->fieldName('id') . ' = ' . $this->data['id']);
// resets object handle
unset($this->data['id']);
}
/**
* Checks highest access level of account.
*
* @return int Access level (highest access level of all characters).
* @throws PDOException On PDO operation error.
*/
public function getAccess()
{
return $this->getGroupId();
}
public function getGroupId()
{
if(isset($this->data['group_id'])) {
return $this->data['group_id'];
}
global $db;
if($db->hasColumn('accounts', 'group_id')) {
$query = $this->db->query('SELECT `group_id` FROM `accounts` WHERE `id` = ' . (int) $this->getId())->fetch();
// if anything was found
if(isset($query['group_id'])) {
$this->data['group_id'] = $query['group_id'];
return $query['group_id'];
}
}
$query = $this->db->query('SELECT `group_id` FROM `players` WHERE `account_id` = ' . (int) $this->getId() . ' ORDER BY `group_id` DESC LIMIT 1');
if($query->rowCount() == 1)
{
$query = $query->fetch();
$this->data['group_id'] = $query['group_id'];
return $query['group_id'];
}
return 0;
}
public function getAccGroupId()
{
if(isset($this->data['group_id'])) {
return $this->data['group_id'];
}
global $db;
if($db->hasColumn('accounts', 'group_id')) {
$query = $this->db->query('SELECT `group_id` FROM `accounts` WHERE `id` = ' . (int) $this->getId())->fetch();
// if anything was found
if(isset($query['group_id'])) {
$this->data['group_id'] = $query['group_id'];
return $query['group_id'];
}
}
if($db->hasColumn('accounts', 'type')) {
$query = $this->db->query('SELECT `type` FROM `accounts` WHERE `id` = ' . (int) $this->getId())->fetch();
// if anything was found
if(isset($query['type'])) {
$this->data['type'] = $query['type'];
return $query['type'];
}
}
return 0;
}
/**
/**
* Checks highest access level of account in given guild.
*
* @param OTS_Guild $guild Guild in which access should be checked.
* @return int Access level (highest access level of all characters).
* @throws PDOException On PDO operation error.
*/
public function getGuildAccess(OTS_Guild $guild)
{
// by default
$access = 0;
// finds ranks of all characters
foreach($this->getPlayersList() as $player)
{
$rank = $player->getRank();
// checks if rank's access level is higher then previouls found highest
if( isset($rank) && $rank->isLoaded() && $rank->getGuild()->getId() == $guild->getId() && $rank->getLevel() > $access)
{
$access = $rank->getLevel();
}
}
return $access;
}
public function logAction($action)
{
$ip = get_browser_real_ip();
if(strpos($ip, ":") === false) {
$ipv6 = '0';
}
else {
$ipv6 = $ip;
$ip = '';
}
return $this->db->exec('INSERT INTO `' . TABLE_PREFIX . 'account_actions` (`account_id`, `ip`, `ipv6`, `date`, `action`) VALUES (' . $this->db->quote($this->getId()).', ' . ($ip == '' ? '0' : $this->db->quote(ip2long($ip))) . ', (' . ($ipv6 == '0' ? $this->db->quote('') : $this->db->quote(inet_pton($ipv6))) . '), UNIX_TIMESTAMP(NOW()), ' . $this->db->quote($action).')');
}
public function getActionsLog($limit1, $limit2)
{
$actions = array();
foreach($this->db->query('SELECT `ip`, `ipv6`, `date`, `action` FROM `' . TABLE_PREFIX . 'account_actions` WHERE `account_id` = ' . $this->data['id'] . ' ORDER by `date` DESC LIMIT ' . $limit1 . ', ' . $limit2 . '')->fetchAll() as $a)
$actions[] = array('ip' => $a['ip'], 'ipv6' => $a['ipv6'], 'date' => $a['date'], 'action' => $a['action']);
return $actions;
}
/**
* Returns players iterator.
*
* <p>
* There is no need to implement entire Iterator interface since we have {@link OTS_Players_List players list class} for it.
* </p>
*
* @version 0.0.5