This repository was archived by the owner on Dec 14, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass.wowApi.php
More file actions
1993 lines (1870 loc) · 76.2 KB
/
Copy pathclass.wowApi.php
File metadata and controls
1993 lines (1870 loc) · 76.2 KB
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
/**
* Provides easy access to the Battle.net World of Warcraft API.
* The goal of this library is provide a simple method to access
* the API with built-in caching, and only a single file to maintain.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* @package WoW API Client
* @name PHP World of Warcraft API Client Class
* @version 0.5 Beta (Tagged: Community Preview, Code Review, Quality Assurance, Expiremental) (Accepting: Bugs, Feature Requests)
* @author Mysticell of Stormrage (US) <mysticell@warcraft365.com>
* @copyright 2011 All Rights Reserved
* @license Simple Public License (SimPL-2.0) http://opensource.org/licenses/Simple-2.0
* @link http://summit.warcraft365.com/
* @location R:\PHP World of Warcraft API Class\wowApi.php
* @location http://warcraft365.com/~mysticell/wowapi/wowApi.php.txt
* @note Most cache options need extensive testing. Please email any bugs to <mysticell@warcraft365.com>.
* @todo Ensure all times are GMT, in S not MS.
* @todo Item data API.
* @todo Bother Straton for an auth key to test with.
* @todo Auction data API (this will be super fun).
* @todo Better API error response handling.
* @todo Recipe information API (http://us.battle.net/api/wow/recipe/33994)
* @todo Switch _fetchData to HttpRequestPool.
* @note If using MySQL or SQLite for caching, drop all tables (or recreate the database) before upgrading to version 0.5.
*/
class wowApi
{
/**
* Main Battle.net URL Part
*
* @constant string
*/
const BATTLE_NET_URL_API = '.battle.net/api/wow/';
/**
* Main Battle.net URL Part (China)
*
* @constant string
*/
const BATTLE_NET_URL_API_CN = 'battlenet.com.cn/api/wow/';
/**
* User Agent for API Requests (Extend the class to override this, and include your site's contact URL)
*
* @constant string
*/
const CLIENT_USER_AGENT = 'libcurl phpWowApi/0.5 (I LOVE YOU, %s!) +http://warcraft365.com/~mysticell/wowapi/wowApi.php.txt';
/**
* API Forum Posters
*
* @access protected
* @var array
*/
protected $_bnetDevs = array( 'Straton', 'Peratryn', 'Grotako', 'Osundir' );
/**
* Battle.net Region (Use setRegion to change)
*
* @access protected
* @var string
*/
protected $_region;
/**
* Use SSL for API requests?
*
* @access private
* @var boolean
*/
private $_ssl;
/**
* API Authentication Credentials
*
* @access private
* @var array
*/
private $_authCredentials;
/**
* Cache method (set in cacheInit)
*
* @access private
* @var string
*/
private $_cacheMethod;
/**
* Cache Settings
*
* @access private
* @var array
*/
private $_cacheSettings;
/**
* Remember that loot chest back in Ulduar, the Cache of Winter? Well, this is a different kind of cache.
*
* @access private
* @var object
*/
private $_cacheObject;
/**
* Imaging settings for setImagingDirs.
*
* @access protected
* @var array
*/
protected $_imagingSettings;
/**
* Trigger non-fatal errors?
*
* @access public
* @var boolean
* @todo Do not show non-fatal errors when this variable is set to false.
*/
public $errorReporting = TRUE;
/**
* File for logging all requests to the Battle.net service.
*
* @access public
* @var string
* @todo Log all cURL requests when this variable is set to a filename.
*/
public $requestLog;
/**
* Constructor
*
* @access public
* @param string $region Initial region: us, eu, kr, tw
* @param boolean $ssl Set to true to use SSL for API requests.
* @return void
*/
public function __construct($region, $ssl = FALSE)
{
if( !is_bool( $ssl ) )
{
/**
* @error_id 1
* @severity Fatal -> Security Exception
* @explanation $ssl parameter is not set properly.
*/
throw new Exception( '[#1] ' . __CLASS__ . ' $ssl parameter should be boolean.', 1 );
}
if( !function_exists( 'json_decode' ) )
{
/**
* @error_id 2
* @severity Fatal -> Missing Function
* @explanation JSON functions are not present in the PHP compilation.
*/
throw new Exception( '[#2] ' . __CLASS__ . ' requires JSON functions.', 2 );
}
if( !function_exists( 'curl_init' ) )
{
/**
* @error_id 3
* @severity Fatal -> Missing Functions
* @explanation CURL functions are not present in the PHP compilation.
*/
throw new Exception( '[#3] ' . __CLASS__ . ' requires CURL functions.', 3 );
}
$this->setRegion( $region );
if( !isset( $this->_region ) )
{
/**
* @error_id 8
* @severity Fatal -> Invalid Region
* @explanation An invalid region was specified when instantiating the class. Region should be either us, eu, kr, or tw.
*/
throw new Exception( '[#8] ' . __CLASS__ . ' invalid region specified.', 8 );
}
$this->_ssl = $ssl;
$this->_authCredentials = array();
$this->_getAllRealms();
}
/**
* Destructor
*
* @access public
* @return void
*/
public function __destruct()
{
if( isset( $this->_cacheSettings ) )
{
$this->_cacheShutdown( FALSE );
}
}
/**
* Set up caching.
*
* <pre>
* $cacheOpts should be an array of options, some of which are specific to certain caching methods.
* Note: If your application uses its own MySQL database, you should implement your own caching method using the same connection for increased performance.
* --------- --------- ------------------------ ----------------------------------------------------------------------------------
* Method Type Name Description
* --------- --------- ------------------------ ----------------------------------------------------------------------------------
* <ALL> (integer) $cacheOpts['ttl'] Default cache time to live in seconds. Certain caches multiply by this value.
* FILE (string) $cacheOpts['file'] Location of the cache file to use.
* CACHELITE (string) $cacheOpts['dir'] Directory to store cache files in.
* MEMCACHED (boolean) $cacheOpts['persistent'] Use persistent Memcached connection?
* MEMCACHED (array) $cacheOpts['servers'] Array of Memcached servers to use.
* MEMCACHED (boolean) $cacheOpts['skipCheck'] Skip checking of Memcached servers if you know the connection details are correct.
* SQLITE (string) $cacheOpts['file'] Location of the database file to use.
* SQLITE (string) $cacheOpts['prefix'] Database table prefix to use.
* MYSQL (string) $cacheOpts['host'] MySQL server hostname or IP. Prefix with 'p:' to create a persistent connection.
* MYSQL (string) $cacheOpts['user'] MySQL username.
* MYSQL (string) $cacheOpts['pass'] MySQL password.
* MYSQL (string) $cacheOpts['database'] MySQL database name.
* MYSQL (integer) $cacheOpts['port'] MySQL server port, if not default (3306).
* MYSQL (string) $cacheOpts['prefix'] Database table prefix to use.
* MYSQL (string) $cacheOpts['engine'] MySQL storage engine to use. Server default if left blank.
* MYSQL (boolean) $cacheOpts['useMemory'] If true, the MEMORY storage engine will be used for realms and misc cache tables.
* </pre>
*
* @access public
* @param string $method Caching method to use.
* @param array $cacheOpts Array of options specific to the caching method.
* @return boolean
*/
public function cacheInit($method, $cacheOpts)
{
$method = strtolower( $method );
if( isset( $this->_cacheSettings ) )
{
/**
* @error_id 23
* @severity Warning -> Halting Method Execution
* @explanation cacheInit has already been called successfully, and cannot be called again.
*/
if( $this->errorReporting )
{
trigger_error( '[#23] Cannot change cache settings after initial setup.', E_USER_NOTICE );
}
return FALSE;
}
if( !in_array( $method, array( 'pageload', 'file', 'cachelite', 'apc', 'memcached', 'sqlite', 'mysql' ) ) )
{
/**
* @error_id 22
* @severity Warning -> Halting Method Execution
* @explanation $method should be one of the above options.
*/
if( $this->errorReporting )
{
trigger_error( '[#22] Invalid $method for cacheSetOpts.', E_USER_NOTICE );
}
return FALSE;
}
$this->_cacheMethod = $method;
$this->_cacheSettings = $cacheOpts;
switch( $this->_cacheMethod )
{
case 'pageload':
$this->_cacheInstall();
break;
case 'file':
if( file_exists( $this->_cacheSettings['file'] ) )
{
$this->_cacheObject = unserialize( file_get_contents( $this->_cacheSettings['file'] ) );
}
else
{
$this->_cacheInstall();
}
break;
case 'cachelite':
if( !file_exists( 'class.cacheLite.php' ) )
{
/**
* @error_id 24
* @severity Fatal -> Cache Initialization Error
* @explanation Cache method is set to cachelite, but the class file was not found.
*/
throw new Exception( '[#24] CacheLite class file not found.', 24 );
}
require_once( 'class.cacheLite.php' );
$optsCompiled = array( 'cacheDir' => $this->_cacheSettings['dir'],
'caching' => TRUE,
'lifeTime' => $this->_cacheSettings['ttl'],
'fileLocking' => TRUE,
'writeControl' => TRUE,
'readControl' => TRUE,
'readControlType' => 'strlen',
'fileNameProtection' => TRUE,
'automaticSerialization' => TRUE );
$this->_cacheObject = new Cache_Lite( $optsCompiled );
break;
case 'apc':
if( !function_exists( 'apc_fetch' ) )
{
/**
* @error_id 25
* @severity Fatal -> Cache Initialization Error
* @explanation Cache method is set to apc, but APC functions are not present.
*/
throw new Exception( '[#25] APC functions not available.', 25 );
}
break;
case 'memcached':
if( $cacheOpts['persistent'] == TRUE )
{
$this->_cacheObject = new Memcached( 'wowApiCache' );
}
else
{
$this->_cacheObject = new Memcached();
}
$serversCount = 0;
foreach( $this->_cacheObject->getServersList as $server )
{
$serversCount++;
}
if( isset( $this->_cacheSettings['servers'] ) && $serversCount < 1 )
{
$this->_cacheObject->addServers( $this->_cacheSettings['servers'] );
}
if( $this->_cacheSettings['skipCheck'] != TRUE )
{
if( !isset( $this->_cacheObject->getStats['pid'] ) )
{
/**
* @error_id 26
* @severity Fatal -> Cache Initialization Error
* @explanation Something went wrong with Memcached.
*/
throw new Exception( '[#26] Memcached error.', 26 );
}
}
break;
case 'sqlite':
$this->_cacheObject = new SQLite3( $this->_cacheSettings['file'] );
if( !$this->_cacheObject )
{
/**
* @error_id 28
* @severity Fatal -> Cache Initialization Error
* @explanation Something went wrong while initializing the SQLite database.
*/
throw new Exception( '[#28] SQLite error.', 28 );
}
$this->_cacheObject->busyTimeout( 100 );
if( $this->_cacheObject->querySingle( 'SELECT name FROM sqlite_master WHERE type=\'table\' AND name=\'' . $this->_cacheSettings['prefix'] . 'realms\'' ) != 'realms' )
{
$this->_cacheInstall();
}
break;
case 'mysql':
/**
* @note If $cacheOpts['host'] begins with 'p:', the connection will be persistent.
*/
if( isset( $this->_cacheSettings['port'] ) )
{
$this->_cacheObject = new mysqli( $this->_cacheSettings['host'], $this->_cacheSettings['user'], $this->_cacheSettings['pass'], $this->_cacheSettings['database'], $this->_cacheSettings['port'] );
}
else
{
$this->_cacheObject = new mysqli( $this->_cacheSettings['host'], $this->_cacheSettings['user'], $this->_cacheSettings['pass'], $this->_cacheSettings['database'] );
}
if( $this->_cacheObject->connect_error )
{
/**
* @error_id 27
* @severity Fatal -> Cache Initialization Error
* @explanation Something went wrong while connecting to the MySQL database.
*/
throw new Exception( '[#27] MySQL error: (' . $this->_cacheObject->connect_errno . ') ' . $this->_cacheObject->connect_error, 27 );
}
if( $this->_cacheObject->query( 'SHOW TABLES LIKE \'' . $this->_cacheSettings['prefix'] . 'realms\'' ) )
{
$queryResult = $this->_cacheObject->use_result();
if( $queryResult->num_rows < 1 )
{
$this->_cacheInstall();
}
}
else
{
/**
* @error_id 29
* @severity Fatal -> Cache Initialization Error
* @explanation Could not execute installation check query.
*/
throw new Exception( '[#29] Could not execute MySQL installation check query.', 29 );
}
break;
}
}
/**
* Shutdown the cache.
*
* @access public
* @return void
*/
public function _cacheShutdown($unset = TRUE)
{
switch( $this->_cacheMethod )
{
case 'pageload':
// Nothing to do here.
break;
case 'file':
file_put_contents( $this->_cacheSettings['file'], serialize( $this->_cacheObject ) );
break;
case 'cachelite':
// Nothing to do here.
break;
case 'apc':
// Nothing to do here.
break;
case 'memcached':
// Nothing to do here.
break;
case 'sqlite':
break;
case 'mysql':
break;
}
if( $unset )
{
unset( $this->_cacheMethod );
unset( $this->_cacheSettings );
unset( $this->_cacheObject );
}
}
/**
* Run first-time installation for caching methods that need it.
*
* @access protected
* @return void
* @todo sqlite, mysql
*/
protected function _cacheInstall()
{
switch( $this->_cacheMethod )
{
case 'pageload':
$this->_cacheObject = new stdClass();
$this->_cacheObject->realms = array();
$this->_cacheObject->characters = array();
$this->_cacheObject->guilds = array();
$this->_cacheObject->arenaTeams = array();
$this->_cacheObject->auctionData = array();
$this->_cacheObject->misc = array();
break;
case 'file':
$this->_cacheObject = new stdClass();
$this->_cacheObject->realms = array();
$this->_cacheObject->characters = array();
$this->_cacheObject->guilds = array();
$this->_cacheObject->arenaTeams = array();
$this->_cacheObject->auctionData = array();
$this->_cacheObject->misc = array();
break;
case 'cachelite':
// Nothing to do here.
break;
case 'apc':
// Nothing to do here.
break;
case 'memcached':
// Nothing to do here.
break;
case 'sqlite':
// Todo
break;
case 'mysql':
if( isset( $this->_cacheSettings['engine'] ) )
{
$this->_cacheObject->query( 'CREATE TABLE `' . $this->_cacheSettings['prefix'] . 'misc`( `key` varchar(255) NOT NULL , `data` longtext , `expires` int UNSIGNED , PRIMARY KEY (`key`) ) ENGINE=' . $this->_cacheSettings['engine'] );
$this->_cacheObject->query( 'CREATE TABLE `' . $this->_cacheSettings['prefix'] . 'realms`( `key` varchar(255) NOT NULL , `data` longtext , `expires` int UNSIGNED , PRIMARY KEY (`key`) ) ENGINE=' . $this->_cacheSettings['engine'] );
$this->_cacheObject->query( 'CREATE TABLE `' . $this->_cacheSettings['prefix'] . 'characters`( `key` varchar(255) NOT NULL , `data` longtext , `expires` int UNSIGNED , PRIMARY KEY (`key`) ) ENGINE=' . $this->_cacheSettings['engine'] );
$this->_cacheObject->query( 'CREATE TABLE `' . $this->_cacheSettings['prefix'] . 'guilds`( `key` varchar(255) NOT NULL , `data` longtext , `expires` int UNSIGNED , PRIMARY KEY (`key`) ) ENGINE=' . $this->_cacheSettings['engine'] );
$this->_cacheObject->query( 'CREATE TABLE `' . $this->_cacheSettings['prefix'] . 'arenaTeams`( `key` varchar(255) NOT NULL , `data` longtext , `expires` int UNSIGNED , PRIMARY KEY (`key`) ) ENGINE=' . $this->_cacheSettings['engine'] );
$this->_cacheObject->query( 'CREATE TABLE `' . $this->_cacheSettings['prefix'] . 'auctionData`( `key` varchar(255) NOT NULL , `data` longtext , `expires` int UNSIGNED , PRIMARY KEY (`key`) ) ENGINE=' . $this->_cacheSettings['engine'] );
}
elseif( $this->_cacheSettings['useMemory'] == TRUE )
{
$this->_cacheObject->query( 'CREATE TABLE `' . $this->_cacheSettings['prefix'] . 'misc`( `key` varchar(255) NOT NULL , `data` longtext , `expires` int UNSIGNED , PRIMARY KEY (`key`) ) ENGINE=MEMORY' );
$this->_cacheObject->query( 'CREATE TABLE `' . $this->_cacheSettings['prefix'] . 'realms`( `key` varchar(255) NOT NULL , `data` longtext , `expires` int UNSIGNED , PRIMARY KEY (`key`) ) ENGINE=MEMORY' );
$this->_cacheObject->query( 'CREATE TABLE `' . $this->_cacheSettings['prefix'] . 'characters`( `key` varchar(255) NOT NULL , `data` longtext , `expires` int UNSIGNED , PRIMARY KEY (`key`) )' );
$this->_cacheObject->query( 'CREATE TABLE `' . $this->_cacheSettings['prefix'] . 'guilds`( `key` varchar(255) NOT NULL , `data` longtext , `expires` int UNSIGNED , PRIMARY KEY (`key`) )' );
$this->_cacheObject->query( 'CREATE TABLE `' . $this->_cacheSettings['prefix'] . 'arenaTeams`( `key` varchar(255) NOT NULL , `data` longtext , `expires` int UNSIGNED , PRIMARY KEY (`key`) )' );
$this->_cacheObject->query( 'CREATE TABLE `' . $this->_cacheSettings['prefix'] . 'auctionData`( `key` varchar(255) NOT NULL , `data` longtext , `expires` int UNSIGNED , PRIMARY KEY (`key`) )' );
}
else
{
$this->_cacheObject->query( 'CREATE TABLE `' . $this->_cacheSettings['prefix'] . 'misc`( `key` varchar(255) NOT NULL , `data` longtext , `expires` int UNSIGNED , PRIMARY KEY (`key`) )' );
$this->_cacheObject->query( 'CREATE TABLE `' . $this->_cacheSettings['prefix'] . 'realms`( `key` varchar(255) NOT NULL , `data` longtext , `expires` int UNSIGNED , PRIMARY KEY (`key`) )' );
$this->_cacheObject->query( 'CREATE TABLE `' . $this->_cacheSettings['prefix'] . 'characters`( `key` varchar(255) NOT NULL , `data` longtext , `expires` int UNSIGNED , PRIMARY KEY (`key`) )' );
$this->_cacheObject->query( 'CREATE TABLE `' . $this->_cacheSettings['prefix'] . 'guilds`( `key` varchar(255) NOT NULL , `data` longtext , `expires` int UNSIGNED , PRIMARY KEY (`key`) )' );
$this->_cacheObject->query( 'CREATE TABLE `' . $this->_cacheSettings['prefix'] . 'arenaTeams`( `key` varchar(255) NOT NULL , `data` longtext , `expires` int UNSIGNED , PRIMARY KEY (`key`) )' );
$this->_cacheObject->query( 'CREATE TABLE `' . $this->_cacheSettings['prefix'] . 'auctionData`( `key` varchar(255) NOT NULL , `data` longtext , `expires` int UNSIGNED , PRIMARY KEY (`key`) )' );
}
break;
}
}
/**
* Generate a file-safe cache name.
*
* @deprecated
* @access protected
* @param mixed $input String to clean.
* @return string
*/
protected function _cacheGenerateName($input)
{
if( is_array( $input ) )
{
$input = implode( '__', $input );
}
return utf8_encode( str_replace( ' ', '_', $input ) );
}
/**
* Generate the timestamp when a cache will be removed.
*
* @access protected
* @param integer $ttl Time to live.
* @return integer
*/
protected function _cacheGenerateTtl($ttl = NULL)
{
if( $ttl != NULL )
{
return time() + $ttl;
}
return time() + $this->_cacheSettings['ttl'];
}
/**
* Add an item to the cache.
*
* @access protected
* @param string $type Cache group (characters, guilds, arenaTeams, realms, or misc).
* @param string $key Cache key (ex: name of a character, guild, etc).
* @param mixed $data Data to store (any datatype will be accepted).
* @param integer $ttl Time to live.
* @return void
* @todo Error checking
*/
protected function _cacheStore($type, $key, $data, $ttl = NULL)
{
if( !is_int( $ttl ) )
{
/**
* @error_id 30
* @severity Warning -> Variable Defaulted
* @explanation $ttl must be integer.
*/
trigger_error( '[#30] $ttl must be integer, using default.', E_USER_NOTICE );
$ttl = NULL;
}
if( $ttl == NULL )
{
$ttl = $this->_cacheSettings['ttl'];
}
if( !isset( $this->_cacheSettings ) )
{
return TRUE;
}
switch( $this->_cacheMethod )
{
case 'pageload':
$data['dropTime'] = $this->_cacheGenerateTtl( $ttl );
$this->_cacheObject->{$type}[$key] = $data;
break;
case 'file':
$data['dropTime'] = $this->_cacheGenerateTtl( $ttl );
$this->_cacheObject->{$type}[$key] = $data;
break;
case 'cachelite':
$this->_cacheObject->setLifeTime( $ttl );
$this->_cacheObject->save( $data, $key, $type );
break;
case 'apc':
$dataString = serialize( $data );
apc_store( $type . '++' . $key, $dataString, $ttl );
break;
case 'memcached':
$this->_cacheObject->set( $type . '++' . $key, $data, $this->_cacheGenerateTtl( $ttl ) );
break;
case 'sqlite':
$table = $this->_cacheSettings['file'] . '.' . $this->_cacheSettings['prefix'] . $type;
$dataString = $this->_cacheObject->escapeString( serialize( $data ) );
$expires = time() + $ttl;
$this->_cacheObject->exec( 'INSERT OR REPLACE INTO ' . $table . ' (key, data, expires) VALUES (' . $key . ', ' . $dataString . ', ' . $expires . ')' );
break;
case 'mysql':
$dataString = $this->_cacheObject->real_escape_string( serialize( $data ) );
$expires = time() + $ttl;
$this->_cacheObject->query( 'REPLACE INTO ' . $this->_cacheSettings['prefix'] . $type . ' (key, data, expires) VALUES (' . $key . ', ' . $dataString . ', ' . $expires . ')' );
break;
}
}
/**
* Fetch an item from the cache.
*
* @access protected
* @param string $type Cache group (characters, guilds, arenaTeams, realms, or misc).
* @param string $key Cache key (ex: name of a character, guild, etc).
* @return mixed Data in the cache if exists, or false.
*/
protected function _cacheFetch($type, $key)
{
if( !isset( $this->_cacheSettings ) )
{
return FALSE;
}
switch( $this->_cacheMethod )
{
case 'pageload':
if( isset( $this->_cacheObject->{$type}[$key] ) )
{
if( $this->_cacheObject->{$type}[$key]['dropTime'] > time() )
{
return $this->_cacheObject->{$type}[$key];
}
else
{
$this->_cacheDrop($type, $key);
}
}
return FALSE;
break;
case 'file':
if( isset( $this->_cacheObject->{$type}[$key] ) )
{
if( $this->_cacheObject->{$type}[$key]['dropTime'] > time() )
{
return $this->_cacheObject->{$type}[$key];
}
else
{
$this->_cacheDrop( $type, $key );
return FALSE;
}
}
return FALSE;
break;
case 'cachelite':
return $this->_cacheObject->get( $key, $type );
break;
case 'apc':
return unserialize( apc_fetch( $type . '++' . $key ) );
break;
case 'memcached':
return $this->_cacheObject->get( $type . '++' . $key );
break;
case 'sqlite':
$table = $this->_cacheSettings['file'] . '.' . $this->_cacheSettings['prefix'] . $type;
$queryReturn = $this->_cacheObject->querySingle( 'SELECT data, expires FROM ' . $table . ' WHERE key=' . $key );
if( $queryReturn )
{
if( $queryReturn['expires'] > time() )
{
return unserialize( $queryReturn['data'] );
}
else
{
$this->_cacheDrop( $type, $key );
}
}
return FALSE;
break;
case 'mysql':
if( $this->_cacheObject->query( 'SELECT data, expires FROM ' . $this->_cacheSettings['prefix'] . $type . ' WHERE key=' . $key ) )
{
$result = $this->_cacheObject->use_result();
if( $result )
{
$queryReturn = $result->fetch_assoc();
if( $queryReturn['expires'] > time () )
{
return unserialize( $queryReturn['data'] );
}
else
{
$this->_cacheDrop( $type, $key );
}
}
}
return FALSE;
break;
}
}
/**
* Drop an item from the cache.
*
* @access protected
* @param string $type Cache group (characters, guilds, arenaTeams, realms, or misc).
* @param string $key Cache key (ex: name of a character, guild, etc).
* @return boolean True if dropped successfully, false if error or not
* @todo Return checks for SQLite and MySQL
*/
protected function _cacheDrop($type, $key)
{
if( !isset( $this->_cacheSettings ) )
{
return TRUE;
}
switch( $this->_cacheMethod )
{
case 'pageload':
if( isset( $this->_cacheObject->{$type}[$key] ) )
{
unset( $this->_cacheObject->{$type}[$key] );
return TRUE;
}
return FALSE;
break;
case 'file':
if( isset( $this->_cacheObject->{$type}[$key] ) )
{
unset( $this->_cacheObject->{$type}[$key] );
return TRUE;
}
return FALSE;
break;
case 'cachelite':
return $this->_cacheObject->remove( $key, $type );
break;
case 'apc':
return apc_delete( $type . '++' . $key );
break;
case 'memcached':
return $this->_cacheObject->delete( $type . '++' . $key );
break;
case 'sqlite':
$table = $this->_cacheSettings['file'] . '.' . $this->_cacheSettings['prefix'] . $type;
$this->_cacheObject->exec( 'DELETE FROM ' . $table . ' WHERE key=' . $key );
return TRUE;
break;
case 'mysql':
$this->_cacheObject->query( 'DELETE FROM ' . $this->_cacheSettings['prefix'] . $type . ' WHERE key=' . $key );
return TRUE;
break;
}
}
/**
* Dump the entire cache.
*
* @access public
* @return boolean True on success.
* @todo Return checks
*/
public function cacheDropAll()
{
switch( $this->_cacheMethod )
{
case 'pageload':
unset( $this->_cacheObject );
$this->_cacheInstall();
break;
case 'file':
unlink( $this->_cacheSettings['file'] );
unset( $this->_cacheObject );
$this->_cacheInstall();
break;
case 'cachelite':
$this->_cacheObject->clean();
break;
case 'apc':
apc_clear_cache();
break;
case 'memcached':
$this->_cacheObject->flush();
break;
case 'sqlite':
$this->_cacheObject->exec( 'DELETE FROM ' . $this->_cacheSettings['prefix'] . 'realms' );
$this->_cacheObject->exec( 'DELETE FROM ' . $this->_cacheSettings['prefix'] . 'characters' );
$this->_cacheObject->exec( 'DELETE FROM ' . $this->_cacheSettings['prefix'] . 'guilds' );
$this->_cacheObject->exec( 'DELETE FROM ' . $this->_cacheSettings['prefix'] . 'arenaTeams' );
$this->_cacheObject->exec( 'DELETE FROM ' . $this->_cacheSettings['prefix'] . 'misc' );
break;
case 'mysql':
$this->_cacheObject->query( 'TRUNCATE TABLE ' . $this->_cacheSettings['prefix'] . 'realms' );
$this->_cacheObject->query( 'TRUNCATE TABLE ' . $this->_cacheSettings['prefix'] . 'characters' );
$this->_cacheObject->query( 'TRUNCATE TABLE ' . $this->_cacheSettings['prefix'] . 'guilds' );
$this->_cacheObject->query( 'TRUNCATE TABLE ' . $this->_cacheSettings['prefix'] . 'arenaTeams' );
$this->_cacheObject->query( 'TRUNCATE TABLE ' . $this->_cacheSettings['prefix'] . 'misc' );
break;
}
$this->_getAllRealms();
return TRUE;
}
/**
* Check if a key exists in the cache, and execute a function if not, then return the data.
*
* @access protected
* @param string $type Cache group (characters, guilds, arenaTeams, realms, or misc).
* @param string $key Cache key (ex: name of a character, guild, etc).
* @param string $execute String that will be eval'd, stored, and returned if nothing is in the cache for the key.
* @param integer $ttl Time to live.
* @return mixed Requested cache data.
*/
protected function _cacheFetchOrExecute($type, $key, $execute, $ttl = NULL)
{
$check = $this->_cacheFetch( $type, $key );
if( $check )
{
return $check;
}
else
{
$data = exec( $execute );
$this->_cacheStore( $type, $key, $data, $ttl );
return $data;
}
}
/**
* Generate an API url based on the region, method, and query string.
*
* @access protected
* @param string $method API resource
* @param mixed $realm
* @param string $name
* @param mixed $options
* @return string
*/
protected function _generateApiUrl($method, $realm = NULL, $name = NULL, $options = NULL)
{
if( strpos( 'realm', $method ) )
{
if( is_string( $realm ) )
{
$query = '?realm=' . $realm;
}
elseif( is_array( $realm ) )
{
$query = '?realms=' . implode( ',', $realm );
}
else
{
$query = NULL;
}
unset( $realm );
}
else
{
if( is_string( $options ) )
{
$query = '?fields=' . $options;
}
elseif( is_array( $options ) )
{
$query = '?fields=' . implode( ',', $options );
}
else
{
$query = NULL;
}
}
if( $realm && $name )
{
if( $this->_ssl == TRUE )
{
return 'https://' . $this->_region . self::BATTLE_NET_URL_API . $method . '/' . $realm . '/' . $name . $query;
}
else
{
return 'http://' . $this->_region . self::BATTLE_NET_URL_API . $method . '/' . $realm . '/' . $name . $query;
}
}
elseif( $realm )
{
if( $this->_ssl == TRUE )
{
return 'https://' . $this->_region . self::BATTLE_NET_URL_API . $method . '/' . $realm . $query;
}
else
{
return 'http://' . $this->_region . self::BATTLE_NET_URL_API . $method . '/' . $realm . $query;
}
}
else
{
if( $this->_ssl == TRUE )
{
return 'https://' . $this->_region . self::BATTLE_NET_URL_API . $method . $query;
}
else
{
return 'http://' . $this->_region . self::BATTLE_NET_URL_API . $method . $query;
}
}
}
/**
* Generate the Battle.net API authorization request header.
*
* @access private
* @param string $path
* @param string $verb
* @return string
*/
private function _generateAuthHeader($link, $verb = 'GET')
{
$head = 'Authorization: BNET ';
$head .= $this->_authCredentials['publicKey'];
$head .= ':';
$link = str_replace( 'http://' . $this->_region . '.battle.net', '', $link );
$link = explode( '?', $link, -1 );
$link = implode( '?', $link );
$stringToSign = $verb . '\n';
$stringToSign .= date( 'r' ) . '\n';
$stringToSign .= $link . '\n';
$head .= base64_encode( hash_hmac( 'sha1', utf8_encode( $stringToSign ), utf8_encode( $this->_authCredentials['privateKey'] ) ) );
return $head;
}
/**
* Fetch data from the API.
*
* @access protected
* @param string $link
* @param integer $cacheTime
* @param boolean $ignoreAuth
* @return string
* @todo Error response handling
*/
protected function _fetchData($link, $cacheTime = NULL, $ignoreAuth = FALSE)
{
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $link );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_HEADER, 0 );
curl_setopt( $ch, CURLOPT_TIMEOUT, 6 );
curl_setopt( $ch, CURLOPT_ENCODING, '' );
curl_setopt( $ch, CURLOPT_USERAGENT, sprintf( self::CLIENT_USER_AGENT, $this->_bnetDevs[ rand( 0, count( $this->_bnetDevs ) ) ] ) );
if( isset( $cacheTime ) )
{
curl_setopt( $ch, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE );
curl_setopt( $ch, CURLOPT_TIMEVALUE, $cacheTime );
}
if( $ignoreAuth == FALSE )
{
if( isset( $this->_authCredentials['publicKey'] ) && isset( $this->_authCredentials['privateKey'] ) )
{
curl_setopt( $ch, CURLOPT_HTTPHEADER, array( $this->_generateAuthHeader( $link ) ) );
}
if( $this->_ssl == TRUE )
{
curl_setopt( $ch, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, TRUE );
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 2 );
}
}
$data = curl_exec( $ch );
$error = curl_errno( $ch );
curl_close( $ch );
if( $error )
{
/**
* @error_id 7
* @severity Execution Halt -> No Return Data