1: <?php
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: abstract class AlphaController {
49: 50: 51: 52: 53: 54:
55: protected $name;
56:
57: 58: 59: 60: 61: 62: 63:
64: protected $visibility = 'Public';
65:
66: 67: 68: 69: 70: 71: 72:
73: protected $BO = null;
74:
75: 76: 77: 78: 79: 80: 81:
82: protected $unitOfWork;
83:
84: 85: 86: 87: 88: 89:
90: protected $unitStartTime;
91:
92: 93: 94: 95: 96: 97:
98: protected $unitEndTime;
99:
100: 101: 102: 103: 104: 105:
106: protected $unitMAXDuration;
107:
108: 109: 110: 111: 112: 113:
114: protected $firstJob;
115:
116: 117: 118: 119: 120: 121:
122: protected $nextJob;
123:
124: 125: 126: 127: 128: 129:
130: protected $previousJob;
131:
132: 133: 134: 135: 136: 137:
138: protected $lastJob;
139:
140: 141: 142: 143: 144: 145: 146:
147: protected $dirtyObjects = array();
148:
149: 150: 151: 152: 153: 154: 155:
156: protected $newObjects = array();
157:
158: 159: 160: 161: 162: 163:
164: protected $title;
165:
166: 167: 168: 169: 170: 171:
172: protected $keywords;
173:
174: 175: 176: 177: 178: 179:
180: protected $description;
181:
182: 183: 184: 185: 186: 187: 188: 189:
190: protected $statusMessage;
191:
192: 193: 194: 195: 196: 197:
198: private static $logger = null;
199:
200: 201: 202: 203: 204: 205: 206: 207:
208: public function __construct($visibility='Public') {
209: self::$logger = new Logger('AlphaController');
210: self::$logger->debug('>>__construct(visibility=['.$visibility.'])');
211:
212: global $config;
213:
214:
215: if(!isset($_SESSION)) {
216: $url = parse_url($config->get('app.url'));
217: $hostname = $url['host'];
218: session_set_cookie_params(0, '/', $hostname, false, true);
219: session_start();
220: }
221:
222:
223: $this->visibility = $visibility;
224:
225:
226: if(!$this->checkRights()) {
227: $this->accessError();
228:
229: exit;
230: }
231:
232:
233: if($config->get('app.force.front.controller') && basename($_SERVER['PHP_SELF']) != 'index.php') {
234:
235: header('HTTP/1.1 301 Moved Permanently');
236:
237: if(empty($_SERVER['QUERY_STRING'])) {
238: header('Location: '.$config->get('app.url').'?act='.get_class($this));
239: self::$logger->debug('<<__construct');
240: exit;
241: }else{
242: header('Location: '.$config->get('app.url').'?act='.get_class($this).'&'.$_SERVER['QUERY_STRING']);
243: self::$logger->debug('<<__construct');
244: exit;
245: }
246:
247:
248: self::$logger->debug('<<__construct');
249: return;
250: }
251:
252: $this->unitStartTime = new Timestamp(date("Y-m-d H:i:s"));
253: $this->unitEndTime = new Timestamp();
254: $this->unitMAXDuration = new Integer();
255:
256:
257: if($this->name == '')
258: $this->setName(get_class($this));
259:
260: if(isset($_SESSION['unitOfWork']) && is_array($_SESSION['unitOfWork']))
261: $this->setUnitOfWork($_SESSION['unitOfWork']);
262:
263: if(isset($_SESSION['dirtyObjects']) && is_array($_SESSION['dirtyObjects']))
264: $this->dirtyObjects = $_SESSION['dirtyObjects'];
265:
266: if(isset($_SESSION['newObjects']) && is_array($_SESSION['newObjects']))
267: $this->newObjects = $_SESSION['newObjects'];
268:
269: if(isset($_SESSION['statusMessage']))
270: $this->setStatusMessage($_SESSION['statusMessage']);
271:
272: if($config->get('security.encrypt.http.fieldnames') && !empty($_POST))
273: $this->decryptFieldNames();
274:
275: self::$logger->debug('<<__construct');
276: }
277:
278: 279: 280: 281: 282: 283:
284: public function getBO() {
285: self::$logger->debug('>>getBO()');
286: self::$logger->debug('<<getBO ['.var_export($this->BO, true).']');
287: return $this->BO;
288: }
289:
290: 291: 292: 293: 294: 295:
296: public function setBO($BO) {
297: self::$logger->debug('>>setBO(BO=['.var_export($BO, true).'])');
298: $this->BO = $BO;
299:
300:
301: if($this->BO->isTagged()) {
302: $tags = $this->BO->getPropObject('tags')->getRelatedObjects();
303:
304: $keywords = '';
305:
306: if(count($tags) > 0) {
307: foreach($tags as $tag)
308: $keywords .= ','.$tag->get('content');
309: }
310:
311: $this->setKeywords(mb_substr($keywords, 1));
312: }
313:
314: self::$logger->debug('<<setBO');
315: }
316:
317: 318: 319: 320: 321: 322:
323: public function getName() {
324: self::$logger->debug('>>getName()');
325: self::$logger->debug('<<getName ['.$this->name.']');
326: return $this->name;
327: }
328:
329: 330: 331: 332: 333: 334:
335: public function setName($name) {
336: self::$logger->debug('>>setName(name=['.$name.'])');
337: $this->name = $name;
338: self::$logger->debug('<<setName');
339: }
340:
341: 342: 343: 344: 345: 346:
347: public function getVisibility() {
348: self::$logger->debug('>>getVisibility()');
349: self::$logger->debug('<<getVisibility ['.$this->visibility.']');
350: return $this->visibility;
351: }
352:
353: 354: 355: 356: 357: 358:
359: public function setVisibility($visibility) {
360: self::$logger->debug('>>setVisibility(visibility=['.$visibility.'])');
361: $this->visibility = $visibility;
362: self::$logger->debug('<<setVisibility');
363: }
364:
365: 366: 367: 368: 369: 370:
371: public function getFirstJob() {
372: self::$logger->debug('>>getFirstJob()');
373: self::$logger->debug('<<getFirstJob ['.$this->firstJob.']');
374: return $this->firstJob;
375: }
376:
377: 378: 379: 380: 381: 382:
383: public function getNextJob() {
384: self::$logger->debug('>>getNextJob()');
385: self::$logger->debug('<<getNextJob ['.$this->nextJob.']');
386: return $this->nextJob;
387: }
388:
389: 390: 391: 392: 393: 394:
395: public function getPreviousJob() {
396: self::$logger->debug('>>getPreviousJob()');
397: self::$logger->debug('<<getPreviousJob ['.$this->previousJob.']');
398: return $this->previousJob;
399: }
400:
401: 402: 403: 404: 405: 406:
407: public function getLastJob() {
408: self::$logger->debug('>>getLastJob()');
409: self::$logger->debug('<<getLastJob ['.$this->lastJob.']');
410: return $this->lastJob;
411: }
412:
413: 414: 415: 416: 417: 418: 419: 420:
421: public function setUnitOfWork($jobs) {
422: self::$logger->debug('>>setUnitOfWork(jobs=['.var_export($jobs, true).'])');
423:
424: if(method_exists($this, 'before_setUnitOfWork_callback'))
425: $this->before_setUnitOfWork_callback();
426:
427: if(!is_array($jobs)) {
428: throw new IllegalArguementException('Bad $jobs array ['.var_export($jobs, true).'] passed to setUnitOfWork method!');
429: self::$logger->debug('<<setUnitOfWork');
430: return;
431: }
432:
433:
434: foreach($jobs as $job) {
435: if(!AlphaController::checkControllerDefExists($job))
436: throw new IllegalArguementException('The controller name ['.$job.'] provided in the jobs array is not defined anywhere!');
437: }
438:
439:
440: $_SESSION['unitOfWork'] = null;
441: $this->dirtyObjects = array();
442: $this->newObjects = array();
443:
444: $numOfJobs = count($jobs);
445:
446: for($i=0; $i<$numOfJobs; $i++) {
447:
448: if($i==0) {
449: $this->firstJob = $jobs[$i];
450: self::$logger->debug('First job ['.$this->firstJob.']');
451: }
452:
453: if($this->name == $jobs[$i]) {
454: if(isset($jobs[$i-1])) {
455:
456: $this->previousJob = $jobs[$i-1];
457: self::$logger->debug('Previous job ['.$this->previousJob.']');
458: }
459: if(isset($jobs[$i+1])) {
460:
461: $this->nextJob = $jobs[$i+1];
462: self::$logger->debug('Next job ['.$this->nextJob.']');
463: }
464: }
465:
466: if($i==($numOfJobs-1)) {
467: $this->lastJob = $jobs[$i];
468: }
469: }
470:
471: $_SESSION['unitOfWork'] = $jobs;
472:
473: if(method_exists($this, 'after_setUnitOfWork_callback'))
474: $this->after_setUnitOfWork_callback();
475:
476: self::$logger->debug('<<setUnitOfWork');
477: }
478:
479: 480: 481: 482: 483: 484:
485: public function getStartTime() {
486: self::$logger->debug('>>getStartTime()');
487: self::$logger->debug('<<getStartTime ['.$this->unitStartTime.']');
488: return $this->unitStartTime;
489: }
490:
491: 492: 493: 494: 495: 496: 497: 498: 499: 500: 501:
502: public function setUnitStartTime($year, $month, $day, $hour, $minute, $second) {
503: self::$logger->debug('>>setUnitStartTime(year=['.$year.'], month=['.$month.'], day=['.$day.'], hour=['.$hour.'], minute=['.$minute.'],
504: second=['.$second.'])');
505:
506: $this->unitStartTime->setTimestampValue($year, $month, $day, $hour, $minute, $second);
507: $_SESSION['unitStartTime'] = $this->unitStartTime->getValue();
508:
509: self::$logger->debug('<<setUnitStartTime');
510: }
511:
512: 513: 514: 515: 516: 517:
518: public function getEndTime() {
519: self::$logger->debug('>>getEndTime()');
520: self::$logger->debug('<<getEndTime ['.$this->unitEndTime.']');
521: return $this->unitEndTime;
522: }
523:
524: 525: 526: 527: 528: 529: 530: 531: 532: 533: 534:
535: public function setUnitEndTime($year, $month, $day, $hour, $minute, $second) {
536: self::$logger->debug('>>setUnitEndTime(year=['.$year.'], month=['.$month.'], day=['.$day.'], hour=['.$hour.'], minute=['.$minute.'],
537: second=['.$second.'])');
538:
539: $this->unitEndTime->setTimestampValue($year, $month, $day, $hour, $minute, $second);
540: $_SESSION['unitEndTime'] = $this->unitEndTime->getValue();
541:
542: self::$logger->debug('<<setUnitEndTime');
543: }
544:
545: 546: 547: 548: 549: 550:
551: public function getMAXDuration() {
552: self::$logger->debug('>>getMAXDuration()');
553: self::$logger->debug('<<getMAXDuration ['.$this->unitMAXDuration.']');
554: return $this->unitMAXDuration;
555: }
556:
557: 558: 559: 560: 561: 562:
563: public function setUnitMAXDuration($duration) {
564: self::$logger->debug('>>setUnitMAXDuration(duration=['.$duration.'])');
565: $this->unitMAXDuration->setValue($duration);
566: self::$logger->debug('<<setUnitMAXDuration');
567: }
568:
569: 570: 571: 572: 573: 574:
575: public function getUnitDuration() {
576: self::$logger->debug('>>getUnitDuration()');
577:
578: $intStartTime = mktime(
579: $this->unitStartTime->getHour(),
580: $this->unitStartTime->getMinute(),
581: $this->unitStartTime->getSecond(),
582: $this->unitStartTime->getMonth(),
583: $this->unitStartTime->getDay(),
584: $this->unitStartTime->getYear()
585: );
586:
587: $intEndTime = mktime(
588: $this->unitEndTime->getHour(),
589: $this->unitEndTime->getMinute(),
590: $this->unitEndTime->getSecond(),
591: $this->unitEndTime->getMonth(),
592: $this->unitEndTime->getDay(),
593: $this->unitEndTime->getYear()
594: );
595:
596: self::$logger->debug('<<getUnitDuration ['.$intEndTime-$intStartTime.']');
597: return $intEndTime-$intStartTime;
598: }
599:
600: 601: 602: 603: 604: 605:
606: public function markDirty($object) {
607: self::$logger->debug('>>markDirty(object=['.var_export($object, true).'])');
608:
609: if(method_exists($this, 'before_markDirty_callback'))
610: $this->before_markDirty_callback();
611:
612: $this->dirtyObjects[count($this->dirtyObjects)] = $object;
613:
614: $_SESSION['dirtyObjects'] = $this->dirtyObjects;
615:
616: if(method_exists($this, 'after_markDirty_callback'))
617: $this->after_markDirty_callback();
618:
619: self::$logger->debug('<<markDirty');
620: }
621:
622: 623: 624: 625: 626: 627:
628: public function getDirtyObjects() {
629: self::$logger->debug('>>getDirtyObjects()');
630: self::$logger->debug('<<getDirtyObjects ['.var_export($this->dirtyObjects, true).']');
631: return $this->dirtyObjects;
632: }
633:
634: 635: 636: 637: 638: 639:
640: public function markNew($object) {
641: self::$logger->debug('>>markNew(object=['.var_export($object, true).'])');
642:
643: if(method_exists($this, 'before_markNew_callback'))
644: $this->before_markNew_callback();
645:
646: $this->newObjects[count($this->newObjects)] = $object;
647:
648: $_SESSION['newObjects'] = $this->newObjects;
649:
650: if(method_exists($this, 'after_markNew_callback'))
651: $this->after_markNew_callback();
652:
653: self::$logger->debug('<<markNew');
654: }
655:
656: 657: 658: 659: 660: 661:
662: public function getNewObjects() {
663: self::$logger->debug('>>getNewObjects()');
664: self::$logger->debug('<<getNewObjects ['.var_export($this->newObjects, true).']');
665: return $this->newObjects;
666: }
667:
668: 669: 670: 671: 672: 673:
674: public function commit() {
675: self::$logger->debug('>>commit()');
676:
677: if(method_exists($this, 'before_commit_callback'))
678: $this->before_commit_callback();
679:
680: AlphaDAO::begin();
681:
682: $newObjects = $this->getNewObjects();
683:
684: $count = count($newObjects);
685:
686: for ($i = 0; $i < $count; $i++) {
687: try {
688: $newObjects[$i]->save();
689: }catch (FailedSaveException $e) {
690: throw new FailedUnitCommitException($e->getMessage());
691: self::$logger->error('Failed to save new object of type ['.get_class($newObjects[$i]).'], aborting...');
692: $this->abort();
693: return;
694: }catch (LockingException $e) {
695: throw new FailedUnitCommitException($e->getMessage());
696: self::$logger->error('Failed to save new object of type ['.get_class($newObjects[$i]).'], aborting...');
697: $this->abort();
698: return;
699: }
700: }
701:
702: $dirtyObjects = $this->getDirtyObjects();
703:
704: $count = count($dirtyObjects);
705:
706: for ($i = 0; $i < $count; $i++) {
707: try {
708: $dirtyObjects[$i]->save();
709: }catch (FailedSaveException $e) {
710: throw new FailedUnitCommitException($e->getMessage());
711: self::$logger->error('Failed to save OID ['.$dirtyObjects[$i]->getID().'] of type ['.get_class($dirtyObjects[$i]).'], aborting...');
712: $this->abort();
713: return;
714: }catch (LockingException $e) {
715: throw new FailedUnitCommitException($e->getMessage());
716: self::$logger->error('Failed to save OID ['.$dirtyObjects[$i]->getID().'] of type ['.get_class($dirtyObjects[$i]).'], aborting...');
717: $this->abort();
718: return;
719: }
720: }
721:
722: try{
723: AlphaDAO::commit();
724:
725: $this->clearUnitOfWorkAttributes();
726:
727: if(method_exists($this, 'after_commit_callback'))
728: $this->after_commit_callback();
729:
730: self::$logger->debug('<<commit');
731: }catch(FailedSaveException $e) {
732: throw new FailedUnitCommitException('Failed to commit the transaction, error is ['.$e->getMessage().']');
733: self::$logger->debug('<<commit');
734: }
735: }
736:
737: 738: 739: 740: 741: 742:
743: public function abort() {
744: self::$logger->debug('>>abort()');
745:
746: if(method_exists($this, 'before_abort_callback'))
747: $this->before_abort_callback();
748:
749: try{
750: AlphaDAO::rollback();
751:
752: $this->clearUnitOfWorkAttributes();
753:
754: if(method_exists($this, 'after_abort_callback'))
755: $this->after_abort_callback();
756:
757: self::$logger->debug('<<abort');
758: }catch(AlphaException $e) {
759: throw new AlphaException('Failed to rollback the transaction, error is ['.$e->getMessage().']');
760: self::$logger->debug('<<abort');
761: }
762: }
763:
764: 765: 766:
767: private function clearUnitOfWorkAttributes() {
768: $_SESSION['unitOfWork'] = null;
769: $this->unitOfWork = null;
770: $_SESSION['dirtyObjects'] = null;
771: $this->dirtyObjects = array();
772: $_SESSION['newObjects'] = null;
773: $this->newObjects = array();
774: }
775:
776: 777: 778: 779: 780: 781:
782: public function getTitle() {
783: self::$logger->debug('>>getTitle()');
784: self::$logger->debug('<<getTitle ['.$this->title.']');
785: return $this->title;
786: }
787:
788: 789: 790: 791: 792: 793:
794: public function setTitle($title) {
795: self::$logger->debug('>>setTitle(title=['.$title.'])');
796: self::$logger->debug('<<setTitle');
797: $this->title = $title;
798: }
799:
800: 801: 802: 803: 804: 805:
806: public function getDescription() {
807: self::$logger->debug('>>getDescription()');
808: self::$logger->debug('<<getDescription ['.$this->description.']');
809: return $this->description;
810: }
811:
812: 813: 814: 815: 816: 817:
818: public function setDescription($description) {
819: self::$logger->debug('>>setDescription(description=['.$description.'])');
820: self::$logger->debug('<<setDescription');
821: $this->description = $description;
822: }
823:
824: 825: 826: 827: 828: 829:
830: public function getKeywords() {
831: self::$logger->debug('>>getKeywords()');
832: self::$logger->debug('<<getKeywords ['.$this->keywords.']');
833: return $this->keywords;
834: }
835:
836: 837: 838: 839: 840: 841:
842: public function setKeywords($keywords) {
843: self::$logger->debug('>>setKeywords(keywords=['.$keywords.'])');
844: self::$logger->debug('<<setKeywords');
845: $this->keywords = $keywords;
846: }
847:
848: 849: 850: 851: 852:
853: public function accessError() {
854: self::$logger->debug('>>accessError()');
855:
856: if(method_exists($this, 'before_accessError_callback'))
857: $this->before_accessError_callback();
858:
859: global $config;
860:
861: if(isset($_SESSION['currentUser']))
862: self::$logger->warn('The user ['.$_SESSION['currentUser']->get('email').'] attempted to access the resource ['.$_SERVER['REQUEST_URI'].'] but was denied due to insufficient rights');
863: else
864: self::$logger->warn('An unknown user attempted to access the resource ['.$_SERVER['REQUEST_URI'].'] but was denied due to insufficient rights');
865:
866: header('HTTP/1.1 403 Forbidden');
867: $front = new FrontController();
868: echo AlphaView::renderErrorPage(403, 'You do not have the correct access rights to view this page. If you have not logged in yet, try going back to the home page and logging in from there.');
869:
870: if(method_exists($this, 'after_accessError_callback'))
871: $this->after_accessError_callback();
872:
873: self::$logger->debug('<<accessError');
874: }
875:
876: 877: 878: 879: 880: 881: 882: 883:
884: public function checkRights() {
885: self::$logger->debug('>>checkRights()');
886:
887: if(method_exists($this, 'before_checkRights_callback'))
888: $this->before_checkRights_callback();
889:
890:
891: if ($this->getVisibility() == 'Public') {
892: if(method_exists($this, 'after_checkRights_callback'))
893: $this->after_checkRights_callback();
894:
895: self::$logger->debug('<<checkRights [true]');
896: return true;
897: }else{
898:
899: if (isset($_SESSION['currentUser'])) {
900:
901: if ($_SESSION['currentUser']->inGroup('Admin')) {
902: if(method_exists($this, 'after_checkRights_callback'))
903: $this->after_checkRights_callback();
904:
905: self::$logger->debug('<<checkRights [true]');
906: return true;
907: } elseif ($_SESSION['currentUser']->inGroup($this->getVisibility())) {
908: if(method_exists($this, 'after_checkRights_callback'))
909: $this->after_checkRights_callback();
910:
911: self::$logger->debug('<<checkRights [true]');
912: return true;
913:
914: } elseif (get_class($this->BO) == 'PersonObject' && $_SESSION['currentUser']->getDisplayName() == $this->BO->getDisplayName()) {
915: if(method_exists($this, 'after_checkRights_callback'))
916: $this->after_checkRights_callback();
917:
918: self::$logger->debug('<<checkRights [true]');
919: return true;
920: }else{
921: self::$logger->debug('<<checkRights [false]');
922: return false;
923: }
924: }else{
925: self::$logger->debug('<<checkRights [false]');
926: return false;
927: }
928: }
929: }
930:
931: 932: 933: 934: 935: 936: 937: 938:
939: public static function checkSecurityFields() {
940: if(self::$logger == null)
941: self::$logger = new Logger('AlphaController');
942: self::$logger->debug('>>checkSecurityFields()');
943:
944:
945: $var1 = base64_encode(AlphaSecurityUtils::encrypt($_SERVER['HTTP_HOST'].date("Ymd")));
946:
947: $var2 = base64_encode(AlphaSecurityUtils::encrypt($_SERVER['REMOTE_ADDR'].$var1));
948:
949: if(empty($_REQUEST['var1']) || empty($_REQUEST['var2'])) {
950: self::$logger->warn('The required var1/var2 params where not provided on the HTTP request');
951: self::$logger->debug('<<checkSecurityFields [false]');
952: return false;
953: }
954:
955: if ($var1 == $_REQUEST['var1'] && $var2 == $_REQUEST['var2']) {
956: self::$logger->debug('<<checkSecurityFields [true]');
957: return true;
958: }else{
959: 960: 961: 962: 963:
964:
965:
966: $var1 = base64_encode(AlphaSecurityUtils::encrypt($_SERVER['HTTP_HOST'].date("Ymd", (time()-3600))));
967:
968: $var2 = base64_encode(AlphaSecurityUtils::encrypt($_SERVER['REMOTE_ADDR'].$var1));
969:
970: if ($var1 == $_REQUEST['var1'] && $var2 == $_REQUEST['var2']) {
971: self::$logger->debug('<<checkSecurityFields [true]');
972: return true;
973: }else{
974: self::$logger->warn('The var1/var2 params provided are invalid, values: var1=['.$_REQUEST['var1'].'] var2=['.$_REQUEST['var2'].']');
975: self::$logger->debug('<<checkSecurityFields [false]');
976: return false;
977: }
978: }
979: }
980:
981: 982: 983: 984: 985: 986:
987: public static function generateSecurityFields() {
988: if(self::$logger == null)
989: self::$logger = new Logger('AlphaController');
990: self::$logger->debug('>>generateSecurityFields()');
991:
992:
993: $var1 = base64_encode(AlphaSecurityUtils::encrypt($_SERVER['HTTP_HOST'].date("Ymd")));
994:
995: $var2 = base64_encode(AlphaSecurityUtils::encrypt($_SERVER['REMOTE_ADDR'].$var1));
996:
997: self::$logger->debug('<<generateSecurityFields [array('.$var1.', '.$var2.')]');
998: return array($var1, $var2);
999: }
1000:
1001: 1002: 1003: 1004: 1005: 1006: 1007: 1008:
1009: public static function getCustomControllerName($BOName, $mode) {
1010: if(self::$logger == null)
1011: self::$logger = new Logger('AlphaController');
1012: self::$logger->debug('>>getCustomControllerName(BOName=['.$BOName.'], mode=['.$mode.'])');
1013:
1014: global $config;
1015:
1016:
1017: $BOName = mb_substr($BOName, 0, mb_strpos($BOName, 'Object'));
1018:
1019: $controllerName = ucwords($mode.' '.$BOName);
1020:
1021: $controllerName = str_replace(' ', '', $controllerName);
1022:
1023: self::$logger->debug('Custom controller name is ['.$controllerName.']');
1024:
1025: if (file_exists($config->get('app.root').'controller/'.$controllerName.'.php')) {
1026: self::$logger->debug('<<getCustomControllerName');
1027: return $controllerName;
1028: }elseif (file_exists($config->get('app.root').'alpha/controller/'.$controllerName.'.php')) {
1029: self::$logger->debug('<<getCustomControllerName');
1030: return $controllerName;
1031: }else{
1032: self::$logger->debug('<<getCustomControllerName');
1033: return null;
1034: }
1035: }
1036:
1037: 1038: 1039: 1040: 1041: 1042: 1043: 1044:
1045: protected function loadCustomController($BOName, $mode) {
1046: self::$logger->debug('>>loadCustomController(BOName=['.$BOName.'], mode=['.$mode.'])');
1047:
1048: global $config;
1049:
1050:
1051: $BOName = mb_substr($BOName, 0, mb_strpos($BOName, 'Object'));
1052:
1053: $controllerName = ucwords($mode.' '.$BOName);
1054:
1055: $controllerName = str_replace(' ', '', $controllerName);
1056:
1057: self::$logger->debug('Custom controller name is ['.$controllerName.']');
1058:
1059:
1060: if(get_class($this) != $controllerName) {
1061: if (file_exists($config->get('app.root').'controller/'.$controllerName.'.php')) {
1062: self::$logger->debug('Custom controller found, redirecting...');
1063:
1064: if(isset($_GET['tk'])) {
1065: $params = FrontController::decodeQueryParams($_GET['tk']);
1066: $params = preg_replace('/act=.*\&/', 'act='.$controllerName.'&', $params);
1067: self::$logger->debug('Params are ['.$params.']');
1068:
1069: $url = FrontController::generateSecureURL($params);
1070: self::$logger->debug('Redirecting to ['.$url.']');
1071: header('Location: '.$url);
1072: self::$logger->debug('<<loadCustomController');
1073: exit;
1074: }else{
1075: $url = $config->get('app.url').'controller/'.$controllerName.'.php?'.$_SERVER['QUERY_STRING'];
1076: self::$logger->debug('Redirecting to ['.$url.']');
1077: header('Location: '.$url);
1078: self::$logger->debug('<<loadCustomController');
1079: exit;
1080: }
1081: }elseif (file_exists($config->get('app.root').'alpha/controller/'.$controllerName.'.php')) {
1082: self::$logger->debug('Custom controller found, redirecting...');
1083:
1084: if(self::checkIfAccessingFromSecureURL()) {
1085: if(isset($_GET['tk'])) {
1086: $params = FrontController::decodeQueryParams($_GET['tk']);
1087: }else{
1088: $start = mb_strpos($_SERVER['REQUEST_URI'], '/tk/')+3;
1089: $end = mb_strlen($_SERVER['REQUEST_URI']);
1090: $tk = mb_substr($_SERVER['REQUEST_URI'], $start+1, $end-($start+1));
1091: $params = FrontController::decodeQueryParams($tk);
1092: }
1093:
1094: $params = preg_replace('/act=.*\&/', 'act='.$controllerName.'&', $params);
1095: self::$logger->debug('Params are ['.$params.']');
1096:
1097: $url = FrontController::generateSecureURL($params);
1098: self::$logger->debug('Redirecting to ['.$url.']');
1099: header('Location: '.$url);
1100: self::$logger->debug('<<loadCustomController');
1101: exit;
1102: }else{
1103: $url = $config->get('app.url').'alpha/controller/'.$controllerName.'.php?'.$_SERVER['QUERY_STRING'];
1104: self::$logger->debug('Redirecting to ['.$url.']');
1105: header('Location: '.$url);
1106: self::$logger->debug('<<loadCustomController');
1107: exit;
1108: }
1109: }else{
1110:
1111: throw new FileNotFoundException('The controller ['.$controllerName.'] could not be loaded as it does not exist');
1112: }
1113: }
1114:
1115: self::$logger->debug('<<loadCustomController');
1116: }
1117:
1118: 1119: 1120: 1121: 1122: 1123:
1124: public function setStatusMessage($message) {
1125: $this->statusMessage = $message;
1126: $_SESSION['statusMessage'] = $message;
1127: }
1128:
1129: 1130: 1131: 1132: 1133: 1134: 1135: 1136:
1137: public function getStatusMessage() {
1138: $_SESSION['statusMessage'] = null;
1139: return $this->statusMessage;
1140: }
1141:
1142: 1143: 1144: 1145: 1146: 1147: 1148: 1149:
1150: public static function checkControllerDefExists($controllerName) {
1151: if(self::$logger == null)
1152: self::$logger = new Logger('AlphaController');
1153: self::$logger->debug('>>checkControllerDefExists(controllerName=['.$controllerName.'])');
1154:
1155: global $config;
1156:
1157: $exists = false;
1158:
1159: if($controllerName == '/')
1160: $exists = true;
1161: if(file_exists($config->get('app.root').'controller/'.$controllerName.'.php'))
1162: $exists = true;
1163: if(file_exists($config->get('app.root').'alpha/controller/'.$controllerName.'.php'))
1164: $exists = true;
1165:
1166: self::$logger->debug('<<checkControllerDefExists ['.$exists.']');
1167: return $exists;
1168: }
1169:
1170: 1171: 1172: 1173: 1174: 1175: 1176:
1177: public static function loadControllerDef($controllerName) {
1178: if(self::$logger == null)
1179: self::$logger = new Logger('AlphaController');
1180: self::$logger->debug('>>loadControllerDef(controllerName=['.$controllerName.'])');
1181:
1182: global $config;
1183:
1184: if(file_exists($config->get('app.root').'controller/'.$controllerName.'.php'))
1185: require_once $config->get('app.root').'controller/'.$controllerName.'.php';
1186: elseif(file_exists($config->get('app.root').'alpha/controller/'.$controllerName.'.php'))
1187: require_once $config->get('app.root').'alpha/controller/'.$controllerName.'.php';
1188: else
1189: throw new IllegalArguementException('The class ['.$controllerName.'] is not defined anywhere!');
1190:
1191: self::$logger->debug('<<loadControllerDef');
1192: }
1193:
1194: 1195: 1196: 1197: 1198: 1199:
1200: public static function checkIfAccessingFromSecureURL() {
1201: if (isset($_GET['tk']) || mb_strpos($_SERVER['REQUEST_URI'], '/tk/') !== false)
1202: return true;
1203: else
1204: return false;
1205: }
1206:
1207: 1208: 1209: 1210: 1211:
1212: private function decryptFieldNames() {
1213: foreach(array_keys($_POST) as $fieldname) {
1214:
1215:
1216: if(AlphaValidator::isBase64($fieldname)) {
1217: $_REQUEST[trim(AlphaSecurityUtils::decrypt(base64_decode($fieldname)))] = $_POST[$fieldname];
1218: $_POST[trim(AlphaSecurityUtils::decrypt(base64_decode($fieldname)))] = $_POST[$fieldname];
1219: }
1220:
1221:
1222: unset($_POST[$fieldname]);
1223: unset($_REQUEST[$fieldname]);
1224:
1225: }
1226: }
1227: }
1228:
1229: ?>