/
WebPage.php
1095 lines (926 loc) · 32.5 KB
/
WebPage.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
/**
*
* License, TERMS and CONDITIONS
*
* This software is lisensed under the GNU LESSER GENERAL PUBLIC LICENSE (LGPL) version 3
* Please read the license here : http://www.gnu.org/licenses/lgpl-3.0.txt
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* ATTRIBUTION REQUIRED
* 4. All web pages generated by the use of this software, or at least
* the page that lists the recent questions (usually home page) must include
* a link to the http://www.lampcms.com and text of the link must indicate that
* the website's Questions/Answers functionality is powered by lampcms.com
* An example of acceptable link would be "Powered by <a href="http://www.lampcms.com">LampCMS</a>"
* The location of the link is not important, it can be in the footer of the page
* but it must not be hidden by style attibutes
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This product includes GeoLite data created by MaxMind,
* available from http://www.maxmind.com/
*
*
* @author Dmitri Snytkine <cms@lampcms.com>
* @copyright 2005-2012 (or current year) Dmitri Snytkine
* @license http://www.gnu.org/licenses/lgpl-3.0.txt GNU LESSER GENERAL PUBLIC LICENSE (LGPL) version 3
* @link http://www.lampcms.com Lampcms.com project
* @version Release: @package_version@
*
*
*/
namespace Lampcms;
use \Lampcms\Forms\Form;
/**
* This abstract class is responsible for generating
* an output page.
*
* This is an abstract class and must be extended by the
* actual controller class
*
* The controller class must implement the main() method which
* will be called by this class automatically
* after some important pre-processing and initialization
* have taken place: for example that required variables have
* all been submitted (if any)
* Also if controller has a specific $permission
* set as instance variable
* this class will check that the current user
* (represented as Viewer object)
* has required access level to perform this 'permission'
* an ACL is used for permission check and is based on user group membership
* which is called 'role' in ACL jargon.
*
* @author Dmitri Snytkine
*
*/
abstract class WebPage extends Base
{
/**
* HTTP Response code to send
* the value of 200 is default
* This is useful only when handling certain
* exceptions that may indicate a '404 not found' error
* in which case we pass the 404 as error code
* of exception and then can set the http 404 response
*
* @var int
*/
protected $httpCode = 200;
/**
* Array of required GET or POST
* parameters.
* These must not be empty
*
* @var array
*/
protected $aRequired = array();
/**
* Object representing Array of QUERY_STRING params
* this is GET or POST array
*
* @var object of type Request
*/
protected $Request;
/**
* Flag indicates that
* the page being rendered for a mobile
* device. This means content should
* be in a short format - titles only
* for a list of articles, etc.
*
* @var bool
*/
protected $isMobile = false;
/**
* Array holds
* links generated by pager class
*
* @var
*/
protected $arrPagerLinks;
/**
* Links generated by Paginator
*
* @var string html to show pagination links
*/
protected $pagerLinks = '';
/**
* Flag indicates that REQUEST_METHOD
* MUST be POST
*
* @var bool
*/
protected $bRequirePost = false;
/**
* extra javascript(s) to add to this page
* if class extending this class has this property,
* then value will be added to the bottom
* of the page as value of script tag
*
* @var mixed string or array of strings
*/
protected $lastJs;
/**
* Extra css files to add for the page
*
* @var mixed string (path to .css file) or
* array of such strings
*/
protected $extraCss = array();
/**
* Flag indicates that
* we require to validate a form token
*
* @var bool
*/
protected $requireToken = false;
/**
* Name of template dir
* for mobile output it should
* be dynamically changed to 'mobile'
* it can also be changed to 'tablet'
* for tablet screens
*
* @var string
*/
protected $tplDir = 'www';
/**
*
* This is for skinning/styling support
* right now we only have 1 style,
* it has id = 1
*
* @var mixed int | numeric string
*/
protected $styleID = '1';
/**
*
* layoutID 1 means 1-pane page: no nav div, just one main div
* layoutID 2 means 2-pane page: main div and nav div
*
* @var mixed int | numeric string
*/
protected $layoutID = '2';
/**
* Array of replacement vars
* for the tplMain template
*
* @var array
*/
protected $aPageVars;
/**
* Controller may override this
* to skip initPageVars() method
* This is helpful when controller is called by Ajax only
* so int's not necessary to initialize page vars
*
* @var bool
*/
protected $bInitPageVars = true;
/**
* Translator object
*
* @var object of type \I18n\Translator
*/
protected $Tr;
protected $action;
/**
* @var object \Lampcms\Uri\Router
*/
protected $Router;
/**
* Constructor
*
* @return \Lampcms\WebPage
*
* @param \Lampcms\Registry|object $Registry
* @param \Lampcms\Request|null|object $Request
*/
public function __construct(Registry $Registry, Request $Request = null)
{
parent::__construct($Registry);
$this->Request = (null !== $Request) ? $Request : $Registry->Request;
$this->Router = $this->Registry->Router;
$this->action = $this->Request['a'];
$this->initParams()
->setTemplateDir()
->setLocale()
->loginByFacebookCookie()
->loginBySid()
->initPageVars()
->addJoinForm()
->addLangForm();
Cookie::sendFirstVisitCookie();
d('cp');
try {
$this->checkLoginStatus()
->checkAccessPermission()
->main();
} catch ( Exception $e ) {
/**
* Only Exceptions in the Lampcms Namespace
* (the ones that extend Lampcms\Exception)
* will be handled here, others bubble up to
* index.php
*/
$this->handleException($e);
}
d('cp');
/**
* Observer will be able to
* record access of current Viewer to
* the current page for the purpose of
* recording of who's online and at what url
*/
$this->Registry->Dispatcher->post($this, 'onPageView', $this->aPageVars);
//\Lampcms\Log::dump();
}
protected function setLocale()
{
$this->Registry->Locale->setLocale();
$this->Tr = $this->Registry->Tr;
return $this;
}
/**
* Translator method
* It's customary in many projects to
* use the single underscore
* symbol for translation function.
*
* @param string $string string to translate
*
* @param array $vars optional array of replacement vars for
* translation
*
* @return string translated string
*/
protected function _($string, array $vars = null)
{
return $this->Tr->get($string, $vars);
}
/**
* Magic method
* Must use try/catch here
* because php turns uncaught exception in __toString
* into a nasty error message
*
* @return string
*/
public function __toString()
{
try {
return $this->getResult();
} catch ( \Exception $e ) {
e('Exception in ' . __METHOD__ . ' ' . $e->getMessage());
return \Lampcms\Responder::makeErrorPage(Exception::formatException($e));
}
}
/**
* Must be implemented in sub-class
* this should contain the main
* logic of controller class.
*
* The purpose of this method is for a controller
* class to populate
* the $this->aPageVars array
*
* @return
*/
abstract protected function main();
/**
* Check Request object for required params
* as well as for required form token
*
* @throws Exception
* @return object $this
*/
protected function initParams()
{
if ($this->bRequirePost && ('POST' !== Request::getRequestMethod())) {
throw new Exception('POST method required');
}
$this->Request->setRequired($this->aRequired)->checkRequired();
if (true === $this->requireToken) {
\Lampcms\Forms\Form::validateToken($this->Registry);
}
d('cp');
return $this;
}
/**
* Setup initial
* $this->aPageVars variables.
* This variables are used by tplMain
* - a main web page templates, regardless of
* which controller is called - these vars are
* always added to the page
*
*
* @return object $this
*/
protected function initPageVars()
{
if (!$this->bInitPageVars
|| Request::isAjax()
|| 'logout' === $this->action
|| 'login' === $this->action
) {
d('special case: ' . $this->action);
return $this;
}
$Viewer = $this->Registry->Viewer;
$this->aPageVars = \tplMain::getVars();
$Ini = $this->Registry->Ini;
$this->aPageVars['site_title'] = $Ini->SITE_TITLE;
$this->aPageVars['site_url'] = $Ini->SITE_URL;
$this->aPageVars['description'] = $this->aPageVars['site_description'] = $Ini->SITE_NAME;
$this->aPageVars['show_comments'] = $Ini->SHOW_COMMENTS;
$this->aPageVars['max_comments'] = $Ini->MAX_COMMENTS;
$this->aPageVars['comments_timeout'] = $Ini->COMMENT_EDIT_TIME;
$this->aPageVars['layoutID'] = $this->layoutID;
$this->aPageVars['DISABLE_AUTOCOMPLETE'] = $Ini->DISABLE_AUTOCOMPLETE;
$this->aPageVars['JS_MIN_ID'] = JS_MIN_ID;
$this->aPageVars['home'] = $this->_('Home');
/**
* @todo later can change to something like
* $this->Registrty->Viewer->getStyleID()
*
*/
$css = (true === LAMPCMS_DEBUG) ? '/_main.css?t='.time() : '/main.css';
$this->aPageVars['main_css'] = $Ini->CSS_SITE . '{_DIR_}/style/' . STYLE_ID . '/' . VTEMPLATES_DIR . $css;
$aFacebookConf = $Ini->getSection('FACEBOOK');
if (!empty($aFacebookConf)) {
if (!empty($aFacebookConf['APP_ID'])) {
$this->addMetaTag('fbappid', $aFacebookConf['APP_ID']);
$this->addFacebookJs($aFacebookConf['APP_ID']);
if (!empty($aFacebookConf['EXTENDED_PERMS'])) {
$this->addMetaTag('fbperms', $aFacebookConf['EXTENDED_PERMS']);
}
}
}
$this->aPageVars['session_uid'] = $Viewer->getUid();
$this->aPageVars['role'] = $Viewer->getRoleId();
$this->aPageVars['rep'] = $Viewer->getReputation();
$this->aPageVars['version_id'] = Form::generateToken();
/**
* meta 'tw' will be set to string "1" if user has conneted Twitter
*/
$this->addMetaTag('tw', ('' !== (string)$Viewer->getTwitterSecret()));
/**
* meta 'tw' will be set to string "1" if user has conneted Facebook
*/
$this->addMetaTag('fb', ('' !== (string)$Viewer->getFacebookToken()));
$js = (true === LAMPCMS_DEBUG) ? '/qa.js?t='.time() : '/min/qa_' . JS_MIN_ID . '.js';
//$js = (true === LAMPCMS_DEBUG) ? '/temp1.js' : '/min/qa_'.JS_MIN_ID.'.js';
$src = $Ini->JS_SITE . '{_DIR_}/js' . $js;
$this->aPageVars['JS'] = $src;
/**
* @todo
* also add twitter id or username or just 'yes'
* of viewer so that we know viewer has twitter account
* and is capable of using twitter from our API
* Also we can ask use to add Twitter account
* if we know he does not have one connected yet
*/
return $this;
}
/**
*
* Add JavaScript for Facebook UI to the page
*
* @param string $appId value from !config.ini 'FACEBOOK' -> 'APP_ID'
* @return \Lampcms\WebPage (this object)
*/
protected function addFacebookJs($appId)
{
/**
* Do NOT add Facebook JS
* to the page with logout flag
* in order to prevent FB from adding
* fb cookie again
*/
$this->aPageVars['fb_js'] = \tplFbJs::parse(array($appId), false);
return $this;
}
/**
* Add extra meta tag to the page
*
* @param string $tag name of tag
* @param string $val value of tag
*
* @return object $this
*/
protected function addMetaTag($tag, $val)
{
$meta = CRLF . \sprintf('<meta name="%s" content="%s">', $tag, $val);
$this->aPageVars['extra_metas'] .= $meta;
return $this;
}
/**
* If user session did not
* contain data that allowed to
* treat user as logged in, then
* try to login user by uid/sid cookies
* This will work if user has previously
* logged in and selected the 'remember me'
* check box.
*
* @return object $this OR redirects back
* to the same page but with SESSION setup
* with user data, so user will be detected as logged-in
* after the redirect
*/
protected function loginBySid()
{
if ($this->isLoggedIn() || 'logout' === $this->action || 'login' === $this->action) {
d('cp');
return $this;
}
if (!isset($_COOKIE) || !isset($_COOKIE['uid']) || !isset($_COOKIE['sid'])) {
d('uid or sid cooke not set');
return $this;
}
try {
$oCheckLogin = new CookieAuth($this->Registry);
$User = $oCheckLogin->authByCookie();
d('aResult: ' . \print_r($User->getArrayCopy(), 1));
} catch ( CookieAuthException $e ) {
d('LampcmsError: login by sid failed with message: ' . $e->getMessage());
Cookie::delete(array('uid'));
return $this;
}
/**
* Login OK
* used to also
* ->setUserTimezone($this->oViewer)
* but its not necessary because user
* will be redirected anyway
*/
$this->processLogin($User);
$this->Registry->Dispatcher->post($this, 'onCookieLogin');
return $this;
}
/**
* Authenticate user if user has fbc_ cookie
* This cookie is set right after user clicks
* on login with facebook button
*
* @return object $this
*/
protected function loginByFacebookCookie()
{
$action = $this->action;
if ($this->isLoggedIn()
|| 'logout' === $action
|| 'connectfb' === $action
|| 'login' === $action
) {
d('skipping loginByFacebookCookie');
return $this;
}
try {
$oViewer = $this->Registry->Facebook->getFacebookUser();
$this->processLogin($oViewer);
d('logged in facebook user: ' . $this->Registry->Viewer->getUid());
$this->Registry->Dispatcher->post($this, 'onFacebookLogin');
} catch ( FacebookAuthException $e ) {
d('Facebook login failed. ' . $e->getMessage() . ' ' . $e->getFile() . ' ' . $e->getLine());
}
return $this;
}
/**
*
* Set User object as a currently logged in
* User, aka Viewer
* Set this Viewer as a value of $_SESSION['viewer']
* array
*
* @param User $User
* @param bool $bResetSession
*
* @return \Lampcms\WebPage
* @throws LoginException
*/
protected function processLogin(User $User, $bResetSession = false)
{
/**
* This little thing is not
* necessary for web-page type of request
* but just in case request was made by
* some other means like by email
*
*/
if (!isset($_SESSION)) {
$_SESSION = array();
}
/**
* This give a change for some sort of filter to examine twitter id, twitter name
* and possibly disallow the login
*
*/
if (false === $this->Registry->Dispatcher->post($User, 'onBeforeUserLogin')) {
d('onBeforeUserLogin returned false');
throw new LoginException('Access denied');
}
if ($bResetSession) {
session_regenerate_id();
}
$this->Registry->Viewer = $User;
$_SESSION['viewer'] = array('id' => $User->getUid(),
'class' => $User->getClass());
/**
* This is important otherwise
* the old stale value is used
* when checking isLoggedIn()
*/
if (isset($this->bLoggedIn)) {
unset($this->bLoggedIn);
}
/**
* Remove navlinks block from
* session because
* after user logged-in he suppose to see
* different links block
*/
if (!empty($_SESSION)) {
$_SESSION['navlinks'] = array();
$_SESSION['login_form'] = null;
$_SESSION['login_error'] = null;
$_SESSION['langs'] = null;
}
return $this;
}
/**
* Performs the last step in assembling
* the page
*
* @return string parsed tplMail template
* with the {timer} replaced with
* page generation time (if present)
*/
public function getResult()
{
if (404 === $this->httpCode) {
d('setting 404 error code');
header("HTTP/1.0 404 Not Found");
}
d('cp');
$this->addLoginBlock();
d('cp');
$this->addLastJs();
d('cp');
$this->addExtraCss();
d('cp');
$tpl = \tplMain::parse($this->aPageVars);
d('cp');
return $tpl;
}
/**
* Adds (appends) value to last_js element of page
*
* @todo check if relative path of src
* then also take into account
* config option JS
*
* @return object $this
*/
protected function addLastJs()
{
if (!empty($this->lastJs)) {
foreach ((array)$this->lastJs as $val) {
$this->aPageVars['last_js'] .= CRLF . sprintf('<script type="text/javascript" src="%s"></script>', $val);
}
}
return $this;
}
/**
*
* Adds extra stylesheet(s) to the page
*
* @return object $this
*/
protected function addExtraCss()
{
try {
if ($this->Registry->Ini->SHOW_FLAGS) {
$this->extraCss[] = $this->Registry->Ini->CSS_SITE . '{_DIR_}/css/flags.css';
}
} catch ( \Lampcms\IniException $e ) {
e($e->getMessage());
}
if (!empty($this->extraCss)) {
d('Got extra css to add');
$this->aPageVars['extra_css'] = '';
foreach ($this->extraCss as $val) {
$this->aPageVars['extra_css'] .= CRLF . \sprintf('<link rel="stylesheet" type="text/css" href="%s">', $val);
}
}
return $this;
}
/**
* If ENABLE_CODE_EDITOR is set to true
* in !config.ini then
* add additional 2 js and 1 css file to the page
* This will enable the "Code Editor" and "Code highlighter"
* widgets in the YUI Editor
*
* This method is called from 2 different controllers:
* Ask and Viewquestion
* That's why it's here in just one place - so it does
* not have to be duplicated in each controller
*
* @return object $this
*/
protected function configureEditor()
{
$a = $this->Registry->Ini->getSection('EDITOR');
if ($a['ENABLE_CODE_EDITOR']) {
d('enabling code highlighter');
$this->lastJs = array('{_DIR_}/js/min/shCoreMin.js', '{_DIR_}/js/min/dsBrushes.js');
$this->extraCss[] = '{_DIR_}/js/min/sh.css';
}
if ($a['ENABLE_YOUTUBE']) {
$this->addMetaTag('btn_yt', '1');
}
return $this;
}
/**
* Adds the Login forum or Welcome block
*
* @return object $this
*/
protected function addLoginBlock()
{
if ('logout' !== $this->action) {
$this->aPageVars['header'] = LoginForm::makeWelcomeMenu($this->Registry);
}
return $this;
}
/**
* Formats the exception, adding
* additional exception data like backtrace
* if running in debug mode
* then adds the 'error' to a page
*
* @return void
*
* @param \Lampcms\Exception $le
*
* @throws \OutOfBoundsException
* @throws \Exception|\OutOfBoundsException
* @internal param object $e Exception object
*/
public function handleException(\Lampcms\Exception $le)
{
try {
if ($le instanceof RedirectException) {
\session_write_close();
$newUrl = $le->getMessage();
/**
* If redirect url contains any URI placeholders
* (like {_WEB_ROOT_} for example)
* then use mapper callback function
* to replace those placeholders
*/
if (\strstr($newUrl, '{_')) {
$mapper = $this->Router->getCallback();
$newUrl = $mapper($newUrl);
}
/**
* If a relative url then turn into
* full url to comply with w3c standard that says
* redirect headers must point to complete url
*/
if (0 !== \strncasecmp('http', $newUrl, 4)) {
$newUrl = $this->Registry->Ini->SITE_URL . $newUrl;
}
header("Location: " . $newUrl, true, $le->getCode());
throw new \OutOfBoundsException;
}
if ($le instanceof CaptchaLimitException) {
d('Captcha limit reached.');
/**
* @todo add ip to CAPTCHA_HACKS collection
*
*/
}
/**
* In case of LampcmsAuthException
* the value of 'c' attribute in exception
* element will be set to "login"
* indicating to template that this
* is a 'must login' type of exception
* and to render the login form
*
*/
$class = ($le instanceof AuthException) ? 'login' : 'excsl';
/**
* Special case:
* the http error code can be
* passed in exception as third argument
* (in case where there are no second arg,
* the second arg must be passed as null)
*/
if (201 < $errCode = $le->getCode()) {
$this->httpCode = (int)$errCode;
}
if ($le instanceof Lampcms404Exception) {
$this->httpCode = 404;
}
/**
* e() will also email error to developer
* In case of Login and OutOfBoundsException we don't want
* to email developers
* We also don't want to email developers
* in special types of exceptions that have
* code set to -1
* which is a way to say
* "This exception is not severe enough for email to be sent out"
*/
if (
($errCode >= 0) &&
!($le instanceof Lampcms404Exception) &&
!($le instanceof AuthException) &&
!($le instanceof LoginException) &&
!($le instanceof NoticeException) &&
!($le instanceof MustLoginException) &&
!($le instanceof \OutOfBoundsException)
) {
e('Exception caught in: ' . $le->getFile() . ' on line: ' . $le->getLine() . ' ' . $le->getMessage() . ' trace: ' . $le->getTraceAsString());
}
/**
*
* Exception::formatException will correctly
* handle sending out JSON and exiting
* if the request isAjax
*
*/
$err = Exception::formatException($le, null, $this->Registry->Tr);
/**
* @todo if Login exception then present a login form!
*
*/
$this->aPageVars['layoutID'] = 1;
$this->aPageVars['body'] = \tplException::parse(array('message' => \nl2br($err),
'class' => $class,
'title' => $this->_('Alert')));
} catch ( \OutOfBoundsException $e ) {
throw $e;
} catch ( \Exception $e ) {
e('Exception object ' . $e->getMessage());
$err = Responder::makeErrorPage($le->getMessage() . ' in ' . $e->getFile());
echo $err;
fastcgi_finish_request();
throw new \OutOfBoundsException;
}
}
/**
* This method is called from Login and
* from wwwOauth, thus its here in one place
*
* @return array of user profile and welcome html div
* if user is logged in, or empty array otherwise
*/
protected function makeLoginArray()
{
$a = array();
d('cp');
if ($this->isLoggedIn()) {
$welcome = LoginForm::makeWelcomeMenu($this->Registry);
$a['welcome'] = $welcome;
}
return $a;
}
/**
* Validates the value of form token
* passed in form against the one stored in SESSION
*
* @todo validate (store it first) IP address
* of request that it must match ip when token is validate
* and throw special type of Exception so that a user will
* get explanation that IP address has changed
*
* @param string $token value as passed in the submitted form
*
* @throws TokenException
* @return true of success
*/
protected function validateToken($token = null)
{
$message = '';
$token = ((null === $token) && !empty($this->Request['token'])) ? $this->Request['token'] : $token;
if (empty($_SESSION['secret'])) {
d("No token in SESSION ");
/**
* @todo
* Translate String
*/
$message = 'Form_token_missing';
} elseif ($_SESSION['secret'] !== $token) {
d('session token: ' . $_SESSION['secret'] . ' supplied token: ' . $token);
$message = 'wrong form token';
}
if (!empty($message)) {
if (Request::isAjax()) {
Responder::sendJSON(array('exception' => $message));
}