-
Notifications
You must be signed in to change notification settings - Fork 0
/
hammer.php
787 lines (669 loc) · 18.1 KB
/
hammer.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
<?php
/**
* Hammer
*
* @package
* @author Max
* @copyright Copyright (c) 2008
* @version $Id: hammer.php 482 2010-01-04 14:26:49Z keloran $
* @access public
*/
class Hammer {
//Traits
use Address, Cookie;
/**
* @var object $oHammer
*/
public static $oHammer;
/**
* @var string $cSiteTitle
*/
public static $cSiteTitle;
/**
* @var string $cWebSite
*/
public static $cWebSite;
/**
* @var string $cSiteaddy
*/
public static $cSiteAddy;
public static $cSite;
public $cSited;
/**
* @var object $oConfig
*/
private static $oConfig;
/**
* @var string $cDatabase
*/
public $cDatabase;
/**
* @var object $oDB
*/
public $oDB;
/**
* @var array $aData
*/
private $aData = array();
/**
* @var array $aAddress
*/
private $aAddress = false;
/**
* @var object $oNails
*/
private $oNails;
/**
* @var string $cError
*/
public $cError;
/**
* @var array $aBaseConfig
*/
public $aBaseConfig = false;
/**
* @var string $cTitle
*/
public $cTitle = false;
/**
* @var int $iHostLocal
*/
public $iHostsLocal = false;
/**
* @var int $iHostRemote
*/
public $iHostsRemote = false;
/** @var array $aFilters */
private $aFilters;
private static $aFilterd;
/** @var string $cSkin */
public $cSkin = false;
//registry
public $aRegistry = array();
/**
* Hammer::__construct()
*
* @param string $cSite
* @param string $cSkin
* @param array $aFilter
*
* @return null
*/
public function __construct($cSite = false, $aFilter = null) {
//Since there is an error throw it, this is very unlikelly to ever be called
if ($this->cError) { throw new Spanner($this->cError, 50); }
//Set the site stuff
//Will be used in cache eventually
if ($cSite) {
self::$cWebSite = $cSite;
$this->cSited = $cSite;
}
//do the filters exist
if ($aFilter) {
$this->aFilters = $aFilter;
self::$aFilterd = $aFilter;
}
//set the address
$this->setAddress($aFilter);
}
/**
* Hammer::getHammer()
*
* @desc Gets the object of Hammer
*
* @param string $cSite Not Really used but will be
* @param string $cSkin Used for getting different Styles
* @param array $aFilter The filters
*
* @return object self::$oHammer
*/
public static function getHammer($cSite = null, $aFilter = null) {
if (function_exists("Config")) {
$aHead = Config("head");
$cSiteAddy = $aHead['address'];
} else {
$cSiteAddy = self::getConfigStat("address");
}
//get the timezone
$cTimeZone = self::getConfigStat("timezone");
if (!$cTimeZone) { $cTimeZone = "Europe/London"; }
//set the timezone
date_default_timezone_set($cTimeZone);
self::$cSiteAddy = $cSiteAddy;
self::$cSite = $cSite;
if (!$cSite) {
if (!$cSiteAddy) {
throw new Spanner("Edit the loader script please", 1);
}
}
//get the object
if (is_null(self::$oHammer)) {
if ($cSite) {
self::$oHammer = new Hammer($cSite, $aFilter);
} else {
self::$oHammer = new Hammer($cSiteAddy, $aFilter);
}
}
//see if this fixs it
$oHammer = self::$oHammer;
$oHammer->setAddress($aFilter, $cSiteAddy);
return $oHammer;
}
/**
* Hammer::getHammerObject()
*
* @param string $cSite
* @param array $aFilter
* @return object
*/
public static function getHammerObject($cSite = null, $aFilter = null) {
if (is_null(self::$oHammer)) { self::$oHammer = new Hammer($cSite, $aFilter); }
return self::$oHammer;
}
/**
* Hammer::rootConfig()
*
* @param string $cKey
* @param string $cElement
* @return string
*/
private static function rootConfig($cElement, $cKey = null) {
$mReturn = false;
$oConfig = New XML();
$oConfig->cRoot = "config";
if (defined("STAGED")) {
$oConfig->setFile("config." . STAGED);
$oConfig->cType = STAGED;
} else {
$oConfig->setFile("config");
if (defined("LIVE")) {
$oConfig->cType = "live";
} else {
$oConfig->cType = "dev";
}
}
//Different things require different things, of course should really just do the element stuff
if ($cKey) {
switch($cKey){
case "contact":
$mReturn = $oConfig->getElement("contact");
break;
case "skinSetting":
$mReturn = $oConfig->getElement("skinSetting");
break;
default:
$mReturn = $oConfig->getElement($cElement, $cKey);
break;
}
} else {
$mReturn = $oConfig->getElement($cElement);
}
return $mReturn;
}
/**
* Hammer::getConfigStat();
*
* @desc This is only for use by spanner or getHammer, dont use this manually
* @todo Proberlyl would be a good idea to set something if want from root not dev/live
*
* @param string $cKey
* @param string $cElement
* @param string $cAttribute
* @return mixed
*/
public static function getConfigStat($cElement, $cKey = null, $cAttribute = null) {
$mReturn = false;
//Dont do this
if (function_exists("Config")) { return false; }
//Its a root element, this is pretty much always going to work, pointless doing it again
$mRoot = self::rootConfig($cElement, $cKey);
if ($mRoot) { $mReturn = $mRoot; }
return $mReturn;
}
/**
* Hammer::setAddress()
*
* @desc This gets the addresss e.g. /page/ = ?page
*
* @param array $aFilters This is if we dont use mod_rewrite
* @return null
*/
public function setAddress($aFilters = null, $cSite = false) {
$cSiteAddress = isset($this->cSiteAddy) ? $this->cSiteAddy : $cSite; //This sets the address, used for language/brand
$aFilter = $this->aFilters ?: $aFilters; //do the filters already exist
$oScrewDriver = new ScrewDriver($aFilter, $cSiteAddress);
$aAddress = $oScrewDriver->finalAddress();
//Set the variables
if ($aAddress) {
foreach ($aAddress as $cName => $cValue) { $this->$cName = $cValue; }
}
}
/**
* Hammer::debugAddress()
*
* @desc This is for debugging purposes only
* @return string
*/
public function debugAddress() {
$cReturn = "<hr />";
$cReturn .= "Page: " . trim($this->cPage) . "<br />";
$cReturn .= "Action: " . $this->cAction . "<br />";
$cReturn .= "Choice: " . $this->cChoice . "<br />";
$cReturn .= "Num: " . $this->iPage . "<br />";
$cReturn .= "Item: " . $this->iItem . "<br />";
$cReturn .= "SubChoice: " . $this->cSubChoice . "<br />";
$cReturn .= "Others-a: " . print_r($this->aOthers, true) . "<br />";
$cReturn .= "Others-c: " . $this->cOthers . "<br />";
$cReturn .= "Others-a2: " . print_r($this->aOther, true) . "<br />";
$cReturn .= "More: <pre>" . print_r($this->aData, true) . "</pre><br />";
$cReturn .= "Address Stuff: <pre>" . print_r($this->aAddress, true) . "</pre><br />";
return $cReturn;
}
/**
* Hammer::getDatabase()
*
* @desc This gets the database object
* @param array $aConfig Gets the config to be used, e.g. read/write
* @return object
*/
public function getDatabase($aConfig = false) {
$oDB = Database::getInstance($aConfig);
return $oDB;
}
/**
* Hammer::getConfigKey()
*
* @return string
*/
public function getConfigKey() {
$cReturn = false;
if (defined("LIVE")) {
$cReturn = "live";
} else if (defined("DEV")) {
$cReturn = "dev";
}
return $cReturn;
}
/**
* Hammer::getConfig()
*
* @desc This gets teh config for use inside a different process, e.g. Database usage
* this Config() method inside shouldnt be used anymore, we should always be using xml now
* @param string $cKey This gets the parent element e.g Database
* @param string $cElement This gets the child, e.g. Database->Read
* @param string $cAttribute This is used to get an attribute of the element, e.g. <title case="lower">Stuff</title>
* @return mixed
*/
public function getConfig($cElement = false, $cKey = false, $cAttribute = false, $bSingle = false) {
$mReturn = false;
//are we using the old method
if (function_exists("Config")) {
//Fixes for old naming method
switch ($cKey) {
case "databases":
$cKey = "database";
break;
case "skinSetting": //Since this will never exist in the old method
return false;
break;
}
//now do we need to change the name of some things
switch ($cElement) {
case "javascript":
$cElement = "js";
break;
}
$mConfig = Config($cKey, $cElement);
if ($cElement) {
if (isset($mConfig[$cElement])) {
$mReturn = $mConfig[$cElement];
} else {
$mReturn = $mConfig;
}
} else {
$mReturn = $mConfig;
}
} else {
$mReturn = self::getConfigStat($cElement, $cKey, $cAttribute);
}
//is there a return, and does it have a single value
if ($mReturn && $bSingle) {
if (count($mReturn) == 1) {
$mReturn1 = $mReturn;
foreach ($mReturn1 as $cKey => $mValue) {
$mReturn = $mReturn1[$cKey];
}
}
}
return $mReturn;
}
/**
* Hammer::setConfig()
*
* @param string $cKey
* @param string $cValue
* @return null
*/
public function setConfig($cKey, $cValue) {
$oConfig = new XML();
$oConfig->cRoot = "config";
if (defined("STAGED")) {
$oConfig->setFile("config." . STAGED);
$oConfig->cType = STAGED;
} else {
$oConfig->setFile("config");
if (defined("LIVE")) { $oConfig->cType = "live"; }
}
//set the element
$oConfig->addElement($cKey, $cValue);
}
/**
* Hammer::getHead()
*
* @desc Gets the head object
* @param string cStyle This is to specify the style, e.g. Black
* @return object
*/
public function getHead($cStyle = false, $bNoInstall = null) {
$oHead = $this->getNail("Head", $cStyle, $bNoInstall);
$oHead->cTitle = $this->cTitle;
return $oHead;
}
/**
* Hammer::getNail()
*
* @param string $cName
* @param mixed $mParams
* @param bool $bNails
* @return object
*/
public function getNail($cName, $mParams = false) {
$cNail = ucfirst($cName);
//for the registry
$oNail = $cNail . "_class";
//is it in the registry
if (!isset($this->aRegistry[$oNail])) {
//Since we need to send the Nails object to pretty much everything
if (is_null($this->oNails)) { $this->oNails = new Nails($this->aFilters); }
//add to the registry and then return
if (is_callable(array($cNail, "getInstance"))) {
try {
$this->aRegistry[$oNail] = $cNail::getInstance($this->oNails, $mParams);
} catch (ErrorException $e) {
throw new Spanner($e->getMessage(), 9999);
} catch (Exception $e) {
throw new Spanner($e->getMessage(), 9999);
}
} else {
try {
$this->aRegistry[$oNail] = new $cNail($this->oNails, $mParams);
} catch (ErrorException $e) {
throw new Spanner($e->getMessage(), 9999);
} catch (Exception $e) {
throw new Spanner($e->getMessage(), 9999);
}
}
}
return $this->aRegistry[$oNail];
}
/**
* Hammer::getNails()
*
* @desc another clone of getNail for backwards compat
* @param string $cName
* @param mixed $mParams
* @return object
*/
public function getNails($cName, $mParams = false) {
return $this->getNail($cName, $mParams);
}
/**
* Hammer::getTemplate()
*
* @desc This is just a pointer to addTemplate use that
* @param string $cTemplate
* @return object
*/
public function getTemplate($cTemplate = null) {
return $this->addTemplate($cTemplate);
}
/**
* Hammer::getTemplates()
*
* @param string $cTemplate
* @return string
*/
public function getTemplates($cTemplate = null) {
return $this->addTemplate($cTemplate);
}
/**
* Hammer:getCache()
*
* @return object
*/
public function getCache() {
return $this->getNail("cache", $this->aData);
}
/**
* Hammer::addTemplate()
*
* @desc Gets the template object
* @param string $cTemplate If you already know what template your going to use, you can set it here
* @return object
*/
public function addTemplate($cTemplate = null) {
$cSkinSetting = false;
$cSkin = $cSkinSetting ?: "brand";
$bDebug = $this->getConfig("template", "debug", false, true);
//skin settings
$this->cSkinSetting = $cSkin;
$this->cSiteCalled = $this->cSited;
//get the template object
$oReturn = Template::getInstance($this->aData, $this->cSited, $cSkinSetting, $bDebug);
if ($cTemplate) { $oReturn->getContent($cTemplate); }
return $oReturn;
}
/**
* Hammer::addForm()
*
* @return object
*/
public function addForm() {
$oTemplate = $this->addTemplate();
$oForm = $oTemplate->addForm();
return $oForm;
}
/**
* Hammer::getSession()
*
* @desc Gets session object, or it gets the session cookie
* @param string $cName if this isset then it retrives the $_SESSION
* @return mixed
*/
public function getSession($cName = false) {
//This is so that 2 functions dont need to be created, and can keep
//the naming convention
if ($cName) {
if (isset($_SESSION[$cName])) {
return $_SESSION[$cName];
}
return false;
}
//this returns the session if ther eisnt one set by name
return $this->getNail("session");
}
/**
* Hammer::sendLocation()
*
* @desc Will send the user to a specified location e.g. /home
* @param string $cLocation Where to send to
* @param int $iTime how long is the refresh time, e.g. for forums
* @param bool $bForced do we want a / on the end or not, on means force the one i say
* @return null
*/
public static function sendLocation($cLocation = false, $iTime = null, $bForced = false) {
if ($iTime) {
$cLoc = "refresh: " . $iTime;
$cLoc .= "; url=";
} else {
$cLoc = "Location: ";
}
if (!$cLocation) {
#header("HTTP/1.1 301 Moved Permanently");
http_response_code(301);
$cLoc .= "/";
} else {
if (is_null(self::$oHammer)) {
$oHammer = self::getHammer();
} else {
$oHammer = self::$oHammer;
}
$oUser = $oHammer->getUser();
if ($cLocation == "refer") {
//now we have hammer, and user
#header("HTTP/1.1 302 Found");
http_response_code(302);
//refer is login and your username is correct
if (isset($_SERVER['HTTP_REFERER']) && $oUser->getUserID() && (strstr($_SERVER['HTTP_REFERER'], "login"))) {
if (!strstr($_SERVER['HTTP_REFERER'], "logout")) {
$oHammer->sendLocation();
}
//there is a referer, and your username could be wrong
} else if (isset($_SERVER['HTTP_REFERER'])) {
if (strstr($_SERVER['HTTP_REFERER'], "login")) { //your username must have been wrong
$cLoc .= "/login/";
} else { //nothing special
$cLoc .= strtolower($_SERVER['HTTP_REFERER']);
}
}
//Install
} else if ($cLocation == "install") {
#header("HTTP/1.1 200 Ok");
http_response_code(200);
$cLoc .= "/";
//Other
} else {
$iLength = strlen($cLocation) - 1;
$iPos = strrpos($cLocation, "/");
//the last slash is missing
if (!$bForced) {
if ($iLength !== $iPos) { $cLocation .= "/"; }
}
#header("HTTP/1.1 303 See Other");
http_response_code(303);
$cLoc .= $cLocation;
}
}
if ($iTime) { printRead($cLoc);die(); }
//now do the location
header($cLoc);
die();
}
/**
* Hammer::getCurrentURL()
*
* @desc Where is the user atm not really used
* @return array
*/
public function getCurrentURL() {
$cPattern = '/([a-zA-Z0-9-]+)(\.[a-zA-Z0-9-]+)(\.[a-zA-Z.]{2,7})/is';
preg_match($cPattern, $_SERVER['HTTP_HOST'], $aMatches);
return $aMatches;
}
/**
* Hammer::setSession()
*
* @desc Sets teh session uses session cookie
* @param string $cName name of it so that it can be retrieved
* @param string $cValue value of the session variable
* @todo would this be better actually in the session lib, possibly
* but at least if its in the core you dont need to load the sesssion lib
* if really not needed
*
* @return bool
*/
public function setSession($cName, $cValue = false) {
//delete hte session
if ($cValue == false) { unset($_SESSION[$cName]); }
if (isset($_SESSION[$cName])) { //if its set your proberlly want it to return the same thing
if ($_SESSION[$cName] == $cValue) {
return true;
} else { //the value isnt the same, so it needs changing
$this->setSession($cName);
$this->setSession($cName, $cValue);
}
} else { //now we know its not set, so set it to this
$_SESSION[$cName] = $cValue;
}
return false;
}
/**
* Hammer::getSetup()
*
* @return object
*/
public function getSetup() {
$oSetup = Setup::getSetup();
return $oSetup;
}
/** MAGIC STUFF **/
/**
* Hammer::__get()
*
* @desc This is an experiment, to see how much time changes by doing it though magic
*
* @param string $cName
* @return string
*/
public function __get($cName) {
$cReturn = false;
if (array_key_exists($cName, $this->aData)) {
$cReturn = $this->aData[$cName];
} else if (isset($this->$cName)) {
$cReturn = $this->$cName;
}
return $cReturn;
}
/**
* Hammer::__set()
*
* @desc This is an experiment
*
* @param string $cName
* @param string $cValue
* @return null
*/
public function __set($cName, $cValue) {
$this->aData[$cName] = $cValue;
}
/**
* Hammer::__isset()
*
* @param string $cName
* @return bool
*/
public function __isset($cName) {
return isset($this->aData[$cName]);
}
/**
* Hammer::__unset()
*
* @param string $cName
* @return null
*/
public function __unset($cName) {
unset($this->aData[$cName]);
}
/**
* Hammer::__call()
*
* @param string $cFunction
* @param mixed $mArgs
* @return object
*/
public function __call($cFunction, $mArgs = false) {
$cFuncName = $cFunction;
if (strstr($cFunction, "get")) { $cFuncName = substr($cFunction, 3); }
return $this->getNail($cFuncName, $mArgs);
}
}