Conditions | 141 |
Paths | > 20000 |
Total Lines | 624 |
Lines | 69 |
Ratio | 11.06 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
589 | function ldProcessRequest($AR_PATH_INFO=null) { |
||
590 | global $AR; |
||
591 | global $ARCurrent; |
||
592 | global $store_config; |
||
593 | global $auth_config; |
||
594 | global $cache_config; |
||
595 | global $store; |
||
596 | global $context; |
||
597 | global $DB; |
||
598 | global $path; |
||
599 | global $function; |
||
600 | global $nls; |
||
601 | |||
602 | $writecache = false; |
||
603 | |||
604 | // go check for a sessionid |
||
605 | $root=$AR->root; |
||
606 | $session_id=0; |
||
607 | $re="^/-(.{4})-/"; |
||
608 | |||
609 | $originalPathInfo = $AR_PATH_INFO; // Store this to pass to the refresh cache on shutdown function; |
||
610 | |||
611 | if (preg_match( '|'.$re.'|' , $AR_PATH_INFO , $matches )) { |
||
612 | $session_id=$matches[1]; |
||
613 | $AR_PATH_INFO=substr($AR_PATH_INFO,strlen($matches[0])-1); |
||
614 | $AR->hideSessionIDfromURL=false; |
||
615 | } elseif ($AR->hideSessionIDfromURL) { |
||
616 | $cookies = (array) ldGetCredentials(); |
||
617 | $current = ldGetCookieSession(); |
||
618 | if ( array_key_exists( $current, $cookies ) ) { |
||
619 | $session_id = $current; |
||
620 | } |
||
621 | } |
||
622 | |||
623 | // set the default user (public) |
||
624 | $AR->login="public"; |
||
625 | |||
626 | |||
627 | // look for the template |
||
628 | $split=strrpos($AR_PATH_INFO, "/"); |
||
629 | $path=substr($AR_PATH_INFO,0,$split+1); |
||
630 | $function=substr($AR_PATH_INFO,$split+1); |
||
631 | if (!$function ) { |
||
632 | if (!isset($arDefaultFunction) || $arDefaultFunction == '' ) { |
||
633 | $arDefaultFunction="view.html"; |
||
634 | } |
||
635 | $function=$arDefaultFunction; |
||
636 | if (isset($arFunctionPrefix) && $arFunctionPrefix != '' ) { |
||
637 | $function=$arFunctionPrefix.$function; |
||
638 | } |
||
639 | $AR_PATH_INFO.=$function; |
||
640 | } |
||
641 | |||
642 | // yes, the extra '=' is needed, don't remove it. trust me. |
||
643 | $ldCacheFilename=strtolower($AR_PATH_INFO)."="; |
||
644 | // for the new multiple domains per site option (per language), we need this |
||
645 | // since the nls isn't literaly in the url anymore. |
||
646 | $ldCacheFilename.=str_replace(':','=',str_replace('/','',$AR->host)).'='; |
||
647 | |||
648 | $qs = ldGetServerVar("QUERY_STRING"); |
||
649 | if ($qs != '') { |
||
650 | $ldCacheFilename.=sha1($qs); |
||
651 | } |
||
652 | |||
653 | if ( $session_id ) { |
||
654 | $cachedimage=$store_config["files"]."cache/session".$ldCacheFilename; |
||
655 | $cachedheader=$store_config["files"]."cacheheaders/session".$ldCacheFilename; |
||
656 | } else { |
||
657 | $cachedimage=$store_config["files"]."cache/normal".$ldCacheFilename; |
||
658 | $cachedheader=$store_config["files"]."cacheheaders/normal".$ldCacheFilename; |
||
659 | } |
||
660 | |||
661 | if ($AR->ESI) { |
||
662 | ob_start(); |
||
663 | } |
||
664 | |||
665 | $timecheck=time(); |
||
666 | |||
667 | if (file_exists($cachedimage)) { |
||
668 | $staleTotalTime = filemtime($cachedimage) - filectime($cachedimage); |
||
669 | $staleCurrent = $timecheck - filectime($cachedimage); |
||
670 | if( $staleTotalTime != 0) { |
||
671 | $stalePercentage = sprintf("%.2f", 100 * $staleCurrent / $staleTotalTime); |
||
672 | } else { |
||
673 | $stalePercentage = 100; |
||
674 | } |
||
675 | if ($stalePercentage < 0) { |
||
676 | $stalePercentage = 0; |
||
677 | } else if ($stalePercentage > 100) { |
||
678 | $stalePercentage = 100; |
||
679 | } |
||
680 | if (!headers_sent()) { |
||
681 | header("X-Ariadne-Cache-Stale: $stalePercentage%"); |
||
682 | } |
||
683 | } |
||
684 | |||
685 | // add min-fresh if the client asked for it |
||
686 | if (isset($ARCurrent->RequestCacheControl["min-fresh"])) { |
||
687 | $timecheck += $ARCurrent->RequestCacheControl["min-fresh"]; |
||
688 | } |
||
689 | |||
690 | if ( |
||
691 | file_exists($cachedimage) && |
||
692 | ((($mtime=@filemtime($cachedimage)) > $timecheck) || ($mtime==0)) && |
||
693 | ($_SERVER["REQUEST_METHOD"]!="POST") && |
||
694 | ($ARCurrent->RequestCacheControl["no-cache"] != true ) && |
||
695 | ($ARCurrent->refreshCacheOnShutdown !== true) |
||
696 | ) { |
||
697 | $ctime=filemtime($cachedimage); // FIXME: Waarom moet dit mtime zijn? Zonder mtime werkt de if-modified-since niet; |
||
698 | |||
699 | if (rand(20,80) < $stalePercentage) { |
||
700 | header("X-Ariadne-Cache-Refresh: refreshing on shutdown"); |
||
701 | register_shutdown_function("ldCacheRequest", $originalPathInfo); // Rerun the request with the original path info; |
||
702 | } else { |
||
703 | header("X-Ariadne-Cache-Refresh: skipped, still fresh enough"); |
||
704 | } |
||
705 | |||
706 | if (!$AR->ESI && $_SERVER['HTTP_IF_MODIFIED_SINCE'] && strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= $ctime) { |
||
707 | // the mtime is used as expiration time, the ctime is the correct last modification time. |
||
708 | // as an object clears the cache upon a save. |
||
709 | |||
710 | // Send the original headers - they will already contain the correct max-age and expires values; |
||
711 | View Code Duplication | if (file_exists($cachedheader)) { |
|
712 | $filedata = file($cachedheader); |
||
713 | if (is_array($filedata)) { |
||
714 | while (list($key, $header)=each($filedata)) { |
||
715 | ldHeader($header); |
||
716 | } |
||
717 | } |
||
718 | } |
||
719 | header("X-Ariadne-Cache: Hit"); |
||
720 | ldHeader("HTTP/1.1 304 Not Modified"); |
||
721 | } else { |
||
722 | View Code Duplication | if (file_exists($cachedheader)) { |
|
723 | // Cache header file also contains information about Cache-control; |
||
724 | $filedata = file($cachedheader); |
||
725 | if (is_array($filedata)) { |
||
726 | while (list($key, $header)=each($filedata)) { |
||
727 | ldHeader($header); |
||
728 | } |
||
729 | } |
||
730 | } |
||
731 | header("X-Ariadne-Cache: Hit"); // Send this after the cached headers to overwrite the cached cache-miss header; |
||
732 | |||
733 | if ($AR->ESI) { |
||
734 | if (false && $_SERVER['HTTP_IF_MODIFIED_SINCE'] && (strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= $ctime)) { |
||
735 | ldHeader("HTTP/1.1 304 Not modified"); |
||
736 | } else { |
||
737 | $data = file_get_contents($cachedimage); |
||
738 | include_once($store_config['code']."modules/mod_esi.php"); |
||
739 | |||
740 | // Replace the session IDs before the ESI process call to pass the correct session ID information... |
||
741 | View Code Duplication | if ($session_id && !$AR->hideSessionIDfromURL) { |
|
742 | $tag = '{arSessionID}'; |
||
743 | $data = str_replace($tag, "-$session_id-", $data); |
||
744 | } |
||
745 | |||
746 | $data = ESI::esiProcess($data); |
||
747 | |||
748 | // ... and then replace the session IDs that were generated in de ESI case; |
||
749 | $tag = '{arSessionID}'; |
||
750 | if ($session_id && !$AR->hideSessionIDfromURL) { |
||
751 | $data = str_replace($tag, "-$session_id-", $data); |
||
752 | } else if ($session_id && $AR->hideSessionIDfromURL ) { |
||
753 | $data = str_replace($tag, '', $data); |
||
754 | } |
||
755 | echo $data; |
||
756 | } |
||
757 | |||
758 | } else if ($session_id) { |
||
759 | $tag = '{arSessionID}'; |
||
760 | $data = file_get_contents($cachedimage); |
||
761 | $tag = '{arSessionID}'; |
||
762 | View Code Duplication | if (!$AR->hideSessionIDfromURL) { |
|
763 | $data = str_replace($tag, "-$session_id-", $data); |
||
764 | } else { |
||
765 | $data = str_replace($tag, '', $data); |
||
766 | } |
||
767 | echo $data; |
||
768 | } else { |
||
769 | readfile($cachedimage); |
||
770 | } |
||
771 | } |
||
772 | $writecache = false; // Prevent recaching cached image; |
||
773 | } else { |
||
774 | if (!headers_sent()) { |
||
775 | header("X-Ariadne-Cache: Miss"); |
||
776 | } |
||
777 | |||
778 | /* |
||
779 | start output buffering |
||
780 | */ |
||
781 | ob_start(); |
||
782 | global $ldOutputBufferActive; |
||
783 | $ldOutputBufferActive = true; |
||
784 | ob_implicit_flush(0); |
||
785 | |||
786 | // look for the language |
||
787 | $split=strpos(substr($AR_PATH_INFO, 1), "/"); |
||
788 | $ARCurrent->nls=substr($path, 1, $split); |
||
789 | if (!isset($AR->nls->list[$ARCurrent->nls]) ) { |
||
790 | // not a valid language |
||
791 | $ARCurrent->nls=""; |
||
792 | $nls=$AR->nls->default; |
||
793 | // but we can find out if the user has any preferences |
||
794 | preg_match_all("%([a-zA-Z]{2}|\\*)[a-zA-Z-]*(?:;q=([0-9.]+))?%", $_SERVER["HTTP_ACCEPT_LANGUAGE"], $regs, PREG_SET_ORDER); |
||
795 | $ARCurrent->acceptlang=array(); |
||
796 | $otherlangs=array(); |
||
797 | $otherq=false; |
||
798 | View Code Duplication | foreach ($regs as $reg) { |
|
799 | if (!isset($reg[2])) { |
||
800 | $reg[2]=1; |
||
801 | } |
||
802 | if ($reg[1]=="*") { |
||
803 | $otherq=$reg[2]; |
||
804 | } else if ($AR->nls->list[$reg[1]]) { |
||
805 | $otherlangs[]=$reg[1]; |
||
806 | $ARCurrent->acceptlang[$reg[1]]=$reg[2]; |
||
807 | } |
||
808 | } |
||
809 | View Code Duplication | if ($otherq !== false) { |
|
810 | $otherlangs=array_diff(array_keys($AR->nls->list), $otherlangs); |
||
811 | foreach ($otherlangs as $lang) { |
||
812 | $ARCurrent->acceptlang[$lang]=$otherq; |
||
813 | } |
||
814 | } |
||
815 | arsort($ARCurrent->acceptlang); |
||
816 | } else { |
||
817 | // valid language |
||
818 | $path=substr($path, $split+1); |
||
819 | // ldSetNls($ARCurrent->nls); |
||
820 | $nls=$ARCurrent->nls; |
||
821 | } |
||
822 | |||
823 | $args=array_merge($_GET,$_POST); |
||
824 | |||
825 | // instantiate the store |
||
826 | $inst_store = $store_config["dbms"]."store"; |
||
827 | $store=new $inst_store($root,$store_config); |
||
828 | //$store->rootoptions = $rootoptions; |
||
829 | |||
830 | if ($session_id) { |
||
831 | ldStartSession($session_id); |
||
832 | } |
||
833 | |||
834 | // instantiate the ARnls |
||
835 | if( $ARCurrent->nls != "" ) { |
||
836 | ldSetNls($nls); |
||
837 | } |
||
838 | |||
839 | |||
840 | if (substr($function, -6)==".phtml") { |
||
841 | // system template: no language check |
||
842 | $ARCurrent->nolangcheck=1; |
||
843 | } |
||
844 | $ext = pathinfo($function, PATHINFO_EXTENSION); |
||
845 | switch ( $ext ) { |
||
846 | case 'css': |
||
847 | ldSetContent('text/css; charset=utf-8'); |
||
848 | break; |
||
849 | case 'js': |
||
850 | ldSetContent('application/javascript; charset=utf-8'); |
||
851 | break; |
||
852 | case 'json': |
||
853 | ldSetContent('application/json; charset=utf-8'); |
||
854 | break; |
||
855 | case 'xml': |
||
856 | ldSetContent('text/xml; charset=utf-8'); |
||
857 | break; |
||
858 | case 'jpg': |
||
859 | ldSetContent('image/jpeg'); |
||
860 | break; |
||
861 | case 'gif': |
||
862 | ldSetContent('image/gif'); |
||
863 | break; |
||
864 | case 'png': |
||
865 | ldSetContent('image/png'); |
||
866 | break; |
||
867 | case 'svg': |
||
868 | ldSetContent('image/svg+xml'); |
||
869 | break; |
||
870 | default: |
||
871 | ldSetContent('text/html; charset=utf-8'); |
||
872 | break; |
||
873 | } |
||
874 | $ARCurrent->arContentTypeSent = true; |
||
875 | |||
876 | register_shutdown_function("ldOnFinish"); |
||
877 | |||
878 | $auth_class = "mod_auth_".$auth_config["method"]; |
||
879 | $mod_auth = new $auth_class($auth_config); |
||
880 | $username = ( isset($args["ARLogin"]) ? $args["ARLogin"] : null ); |
||
881 | $password = ( isset($args["ARPassword"]) ? $args["ARPassword"] : null ); |
||
882 | $result = $mod_auth->checkLogin($username, $password, $path); |
||
883 | if ($result!==true) { |
||
884 | if ($result == LD_ERR_ACCESS) { |
||
885 | ldAccessDenied($path, $ARnls["accessdenied"], $args, $function); |
||
886 | $function = false; |
||
887 | } else if ($result == LD_ERR_SESSION && !$AR->hideSessionIDfromURL ) { |
||
888 | ldAccessTimeout($path, $ARnls["sessiontimeout"], $args, $function); |
||
889 | $function = false; |
||
890 | } else if ($result == LD_ERR_EXPIRED) { |
||
891 | ldAccessPasswordExpired($path, $ARnls["sessionpasswordexpired"], $args, $function); |
||
892 | $function = false; |
||
893 | } |
||
894 | } |
||
895 | |||
896 | // valid new login, without a session, morph to login.redirect.php to redirect to a session containing url |
||
897 | if( !$session_id && $args["ARLogin"] && $args["ARPassword"] && $function !== false && !$AR->hideSessionIDfromURL ) { |
||
898 | View Code Duplication | if (!$ARCurrent->session->get("oldArCallArgs", 1)) { |
|
899 | $ARCurrent->session->put("oldGET", $_GET, 1); |
||
900 | $ARCurrent->session->put("oldPOST", $_POST, 1); |
||
901 | $ARCurrent->session->put("oldArCallArgs", $args, 1); |
||
902 | $ARCurrent->session->save(0, true); |
||
903 | } |
||
904 | if ($arDefaultFunction !== $function) { |
||
905 | $args["arRequestedTemplate"] = $function; |
||
906 | } else { |
||
907 | $args["arRequestedTemplate"] = ""; |
||
908 | } |
||
909 | $function = "login.redirect.php"; |
||
910 | } else if( $session_id ) { |
||
911 | if ($ARCurrent->session->get("ARSessionTimedout", 1)) { |
||
912 | View Code Duplication | if (!$ARCurrent->session->get("oldArCallArgs", 1)) { |
|
913 | $ARCurrent->session->put("oldGET", $_GET, 1); |
||
914 | $ARCurrent->session->put("oldPOST", $_POST, 1); |
||
915 | $ARCurrent->session->put("oldArCallArgs", $args, 1); |
||
916 | $ARCurrent->session->save(0, true); |
||
917 | } |
||
918 | } else { |
||
919 | if ($ARCurrent->session->get("oldArCallArgs", 1)) { |
||
920 | $_GET = array_merge( $_GET, (array)$ARCurrent->session->get("oldGET", 1) ); |
||
921 | $_POST = array_merge( $_POST, (array)$ARCurrent->session->get("oldPOST", 1) ); |
||
922 | $args = $ARCurrent->session->get("oldArCallArgs", 1); |
||
923 | $args = array_merge( $_GET, $_POST, $args); // $args, $_GET, $_POST ); |
||
924 | $ARCurrent->session->put("oldArCallArgs", "", 1); |
||
925 | $ARCurrent->session->put("oldGET", "", 1); |
||
926 | $ARCurrent->session->put("oldPOST", "", 1); |
||
927 | } |
||
928 | } |
||
929 | } |
||
930 | |||
931 | $xss_vars = array(); |
||
932 | ldGatherXSSInput($xss_vars, $_GET); |
||
933 | ldGatherXSSInput($xss_vars, $_POST); |
||
934 | |||
935 | ldGatherXSSInput( $xss_vars, $function ); |
||
936 | ldGatherXSSInput( $xss_vars, $path ); |
||
937 | global $ldXSSProtectionActive; |
||
938 | if (count($xss_vars)) { |
||
939 | $ldXSSProtectionActive = true; |
||
940 | } |
||
941 | |||
942 | if ($function!==false) { |
||
943 | // finally call the requested object |
||
944 | unset($store->total); |
||
945 | if (ldCheckAllowedTemplate($function) ) { |
||
946 | $store->call($function, $args, $store->get($path)); |
||
947 | $writecache = true; |
||
948 | } |
||
949 | if (!$store->total) { |
||
950 | ldObjectNotFound($path, $function, $args); |
||
951 | } |
||
952 | } |
||
953 | |||
954 | if (count($xss_vars)) { |
||
955 | $image = ob_get_contents(); |
||
956 | ob_clean(); |
||
957 | |||
958 | $header = $ARCurrent->ldHeaders["content-type"]; |
||
959 | $xssDetected = false; |
||
960 | preg_match('/^content-type:\s+([^ ;]+)/i', $header, $matches); |
||
961 | $mimetype = strtolower($matches[1]); |
||
962 | if (substr($mimetype, 0, 5) == 'text/') { |
||
963 | krsort($xss_vars, SORT_NUMERIC); |
||
964 | foreach ($xss_vars as $values) { |
||
965 | if (is_array($values)) { |
||
966 | foreach ($values as $value) { |
||
967 | $occurances = substr_count($image, $value); |
||
968 | if ($occurances > 0 ) { |
||
969 | $xssDetected = true; |
||
970 | break 2; |
||
971 | } |
||
972 | } |
||
973 | } |
||
974 | } |
||
975 | } |
||
976 | |||
977 | if ($xssDetected) { |
||
978 | $newargs = array(); |
||
979 | $newargs["arRequestedArgs"] = $args; |
||
980 | $newargs["arRequestedTemplate"] = $function; |
||
981 | $newargs["arSuspectedArgs"] = $xss_vars; |
||
982 | $newargs["arResultOutput"] = $image; |
||
983 | $store->call('user.xss.html', $newargs, $store->get($path)); |
||
984 | } else { |
||
985 | echo $image; |
||
986 | } |
||
987 | } |
||
988 | } |
||
989 | |||
990 | // now check for outputbuffering (caching) |
||
991 | if ($image=ob_get_contents()) { |
||
992 | // Calculate browser side cache settings based on settings collected in the call chain; |
||
993 | // |
||
994 | // Rules: do not cache wins. short cache time wins over longer cache time. Unset values don't get to play. |
||
995 | // |
||
996 | // Overlord rule: if the request method was not a get, or debugging was used, do not cache. Ever. |
||
997 | // |
||
998 | // If pinp says arDontCache, then do not cache; |
||
999 | // |
||
1000 | // If ESI was used and hit a cached image, use the cache settings from the cache image; |
||
1001 | if ($_SERVER['REQUEST_METHOD']!='GET' || ($DB["wasUsed"] > 0)) { |
||
1002 | // Do not cache on client. |
||
1003 | ldSetBrowserCache(false); |
||
1004 | } else if (is_array($ARCurrent->cache) && ($file=array_pop($ARCurrent->cache))) { |
||
1005 | // This will generate an error, do not cache on client; |
||
1006 | ldSetBrowserCache(false); |
||
1007 | } else if ($ARCurrent->arDontCache) { |
||
1008 | // PINP told us not to cache; |
||
1009 | ldSetBrowserCache(false); |
||
1010 | } else if (!$writecache) { |
||
1011 | // Image came from the cache, it already has browser cache headers; |
||
1012 | } else { |
||
1013 | // Defaults for browser caching; |
||
1014 | // Calls without session: public, max-age 1800; |
||
1015 | // Calls with session without call chain (disk templates): private, no-cache no-store must-revalidate max-age=0 |
||
1016 | // Calls with session with call chain (pinp templates): private, max-age=1800; |
||
1017 | // FIXME: Make the calls with session less trigger happy on not caching; |
||
1018 | |||
1019 | /* if ($session_id && sizeof($ARCurrent->cacheCallChainSettings)) { |
||
1020 | // With session and pinp templates; |
||
1021 | $browserCachePrivate = true; |
||
1022 | $browserCacheMaxAge = 1800; |
||
1023 | $browserCacheNoStore = false; |
||
1024 | $browserCacheNoCache = false; |
||
1025 | $browserCacheMustRevalidate = false; |
||
1026 | } else */ |
||
1027 | if ($session_id) { |
||
1028 | // With session, disk templates only |
||
1029 | $browserCachePrivate = true; |
||
1030 | $browserCacheMaxAge = 0; |
||
1031 | $browserCacheNoStore = true; |
||
1032 | $browserCacheNoCache = true; |
||
1033 | $browserCacheMustRevalidate = true; |
||
1034 | } else { |
||
1035 | // Without session and all other corner cases; |
||
1036 | $browserCachePrivate = false; |
||
1037 | $defaultMaxAge = 1800; |
||
1038 | $browserCacheNoStore = false; |
||
1039 | $browserCacheNoCache = false; |
||
1040 | $browserCacheMustRevalidate = false; |
||
1041 | } |
||
1042 | |||
1043 | $browserCachecacheSetting = 0; // Default = inherit; |
||
1044 | |||
1045 | // FIXME: The defaults for with session ID are now to not cache; |
||
1046 | if(is_array($ARCurrent->cacheCallChainSettings) ) { |
||
1047 | foreach ($ARCurrent->cacheCallChainSettings as $objectId => $pathCacheSetting) { |
||
1048 | $browserCachePrivate = $browserCachePrivate || $pathCacheSetting['browserCachePrivate']; // If anyone says 'private', make it so. |
||
1049 | $browserCacheNoStore = $browserCacheNoStore || $pathCacheSetting['browserCacheNoStore']; // If anyone says 'no-store', make it so. |
||
1050 | $browserCacheNoCache = $browserCacheNoCache || $pathCacheSetting['browserCacheNoCache']; // If anyone says 'no-cache', make it so. |
||
1051 | $browserCacheMustRevalidate = $browserCacheMustRevalidate || $pathCacheSetting['browserCacheMustRevalidate']; // If anyone says 'must-revalidate', make it so. |
||
1052 | $browserCacheNoTransform = $browserCacheNoTransform || $pathCacheSetting['browserCacheNoTransform']; // If anyone says 'no-transform', make it so. |
||
1053 | $browserCacheProxyRevalidate = $browserCacheProxyRevalidate || $pathCacheSetting['browserCacheProxyRevalidate']; // If anyone says 'proxy-revalidate', make it so. |
||
1054 | |||
1055 | View Code Duplication | if (isset($pathCacheSetting['browserCacheMaxAge']) && is_numeric($pathCacheSetting['browserCacheMaxAge'])) { |
|
1056 | if (isset($browserCacheMaxAge)) { |
||
1057 | $browserCacheMaxAge = min($browserCacheMaxAge, $pathCacheSetting['browserCacheMaxAge']); |
||
1058 | } else { |
||
1059 | $browserCacheMaxAge = $pathCacheSetting['browserCacheMaxAge']; |
||
1060 | } |
||
1061 | } |
||
1062 | |||
1063 | View Code Duplication | if (isset($pathCacheSetting['browserCacheSMaxAge']) && is_numeric($pathCacheSetting['browserCacheMaxAge'])) { |
|
1064 | if (isset($browserCacheSMaxAge)) { |
||
1065 | $browserCacheSMaxAge = min($browserCacheSMaxAge, $pathCacheSetting['browserCacheSMaxAge']); |
||
1066 | } else { |
||
1067 | $browserCacheSMaxAge = $pathCacheSetting['browserCacheSMaxAge']; |
||
1068 | } |
||
1069 | } |
||
1070 | } |
||
1071 | |||
1072 | if (!isset($browserCacheMaxAge) && isset($defaultMaxAge)) { |
||
1073 | $browserCacheMaxAge = $defaultMaxAge; |
||
1074 | } |
||
1075 | } |
||
1076 | |||
1077 | ldSetBrowserCache( |
||
1078 | array( |
||
1079 | "browserCachePrivate" => $browserCachePrivate, |
||
1080 | "browserCacheNoStore" => $browserCacheNoStore, |
||
1081 | "browserCacheNoCache" => $browserCacheNoCache, |
||
1082 | "browserCacheMustRevalidate" => $browserCacheMustRevalidate, |
||
1083 | "browserCacheNoTransform" => $browserCacheNoTransform, |
||
1084 | "browserCacheProxyRevalidate" => $browserCacheProxyRevalidate, |
||
1085 | "browserCacheMaxAge" => $browserCacheMaxAge, |
||
1086 | "browserCacheSMaxAge" => $browserCacheSMaxAge |
||
1087 | ) |
||
1088 | ); |
||
1089 | } |
||
1090 | |||
1091 | |||
1092 | $image_len = strlen($image); |
||
1093 | |||
1094 | if ($ARCurrent->session && $ARCurrent->session->id) { |
||
1095 | $ldCacheFilename = "/session".$ldCacheFilename; |
||
1096 | $image = str_replace('-'.$ARCurrent->session->id.'-', '{arSessionID}', $image); |
||
1097 | } else { |
||
1098 | $ldCacheFilename = "/normal".$ldCacheFilename; |
||
1099 | } |
||
1100 | // because we have the full content, we can now also calculate the content length |
||
1101 | ldHeader("Content-Length: ".$image_len); |
||
1102 | |||
1103 | |||
1104 | // flush the buffer, this will send the contents to the browser |
||
1105 | ob_end_flush(); |
||
1106 | debug("loader: ob_end_flush()","all"); |
||
1107 | |||
1108 | |||
1109 | // Calculate server side cache settings based on settings collected in the call chain; |
||
1110 | // |
||
1111 | // Rules: do not cache wins. short cache time wins over longer cache time. Unset values don't get to play. |
||
1112 | // |
||
1113 | // Overlord rule: if the request method was not a get, or debugging was used, do not cache. Ever. |
||
1114 | // |
||
1115 | // If pinp says arDontCache, then do not cache; |
||
1116 | // |
||
1117 | // If ESI was used and hit a cached image, do not write the image; |
||
1118 | |||
1119 | if ($_SERVER['REQUEST_METHOD']!='GET' || ($DB["wasUsed"] > 0)) { |
||
1120 | // Do not cache on server. |
||
1121 | // header("X-Ariadne-Cache-Skipped: DB Used"); |
||
1122 | } else if (is_array($ARCurrent->cache) && ($file=array_pop($ARCurrent->cache))) { |
||
1123 | error("cached() opened but not closed with savecache()"); |
||
1124 | // header("X-Ariadne-Cache-Skipped: cached problem."); |
||
1125 | } else if ($ARCurrent->arDontCache) { |
||
1126 | // PINP told us not to cache; |
||
1127 | // header("X-Ariadne-Cache-Skipped: arDontCache"); |
||
1128 | } else if (!$writecache) { |
||
1129 | // ESI was used and hit a cached image, do not write the image; |
||
1130 | // header("X-Ariadne-Cache-Skipped: cached image used"); |
||
1131 | } else { |
||
1132 | // header("X-Ariadne-Cache-Skipped: Writing cache now"); |
||
1133 | // Cache setting values: |
||
1134 | // -2 = Refresh on change; Set the cache time on server to 999 hours (unlimited); |
||
1135 | // -1 = Do not cache |
||
1136 | // 0 = Inherit |
||
1137 | // > 0: Refresh on request. The number is the amount of hours that the cache is 'fresh'. This can be a fraction/float value; |
||
1138 | |||
1139 | $cacheSetting = 0; // Default = inherit; |
||
1140 | $serverCachePrivate = 0; // do not allow caching of sessions |
||
1141 | if( is_array($ARCurrent->cacheCallChainSettings)) { |
||
1142 | foreach ($ARCurrent->cacheCallChainSettings as $objectId => $pathCacheSetting) { |
||
1143 | // FIXME: also 'resolve' $serverCachePrivate |
||
1144 | $serverCache = $pathCacheSetting['serverCache']; |
||
1145 | |||
1146 | if ($serverCache == 0 || !isset($serverCache)) { |
||
1147 | // This path does not want to play; |
||
1148 | $serverCache = $pathCacheSetting['serverCacheDefault']; |
||
1149 | } |
||
1150 | |||
1151 | if ($serverCache == -2) { |
||
1152 | // Sorry, we meant that the cache image should be valid forever; |
||
1153 | $serverCache = 999; |
||
1154 | } |
||
1155 | |||
1156 | if ($cacheSetting == 0) { |
||
1157 | $cacheSetting = $serverCache; |
||
1158 | } else { |
||
1159 | $cacheSetting = min($serverCache, $cacheSetting); |
||
1160 | } |
||
1161 | |||
1162 | if ($cacheSetting == -1) { |
||
1163 | // If someone told us to not cache, skip checking because nothing anyone else tells us will change this fact. |
||
1164 | break; |
||
1165 | } |
||
1166 | } |
||
1167 | } |
||
1168 | // header("X-Ariadne-Cache-Setting: $cacheSetting"); |
||
1169 | if ($ARCurrent->session->id && $cacheSetting > 0) { |
||
1170 | // we have a session id, can we cache ? |
||
1171 | // FIXME: add support for $serverCachePrivate in the config and cache dialog |
||
1172 | if ( ! ( $serverCachePrivate === 1 || $ARCurrent->arDoCachePrivate != false ) ) { |
||
1173 | $cacheSetting = -1; |
||
1174 | } |
||
1175 | } |
||
1176 | |||
1177 | if ($cacheSetting > 0) { |
||
1178 | // If we are allowed to cache, write the image now. |
||
1179 | if ($store) { // Sanity check to only write cache images if a store was initialized; |
||
1180 | // FIXME: cacheCallChainSettings contains the objects that were called for this cache image; |
||
1181 | // FIXME: cacheTemplateChain containers the templates that were called for this cache image; |
||
1182 | |||
1183 | ldSetCache($ldCacheFilename, $cacheSetting, $image, @implode("\n",$ARCurrent->ldHeaders)); |
||
1184 | $cachestore=new cache($cache_config); |
||
1185 | $cachestore->save($ldCacheFilename, $ARCurrent->cacheCallChainSettings, $ARCurrent->cacheTemplateChain); |
||
1186 | } |
||
1187 | } |
||
1188 | } |
||
1189 | |||
1190 | } |
||
1191 | |||
1192 | if ($AR->ESI > 0) { |
||
1193 | // Prevent ESI from looping when the ESI result has ESI tags in them. |
||
1194 | // Reducing the AR->ESI number by 1 gives the flexibility to allow 2 or 3 ESI loops if desired. |
||
1195 | // Setting it to false would mean you only get 1 ESI loop, which might not be the desired effect. |
||
1196 | $AR->ESI = (int) $AR->ESI; |
||
1197 | $AR->ESI--; |
||
1198 | |||
1199 | $image = ob_get_contents(); |
||
1200 | ob_end_clean(); |
||
1201 | include_once($store_config['code']."modules/mod_esi.php"); |
||
1202 | $image = ESI::esiProcess($image); |
||
1203 | $image_len = strlen($image); |
||
1204 | |||
1205 | if ($ARCurrent->arDontCache) { |
||
1206 | // FIXME: ook de cachetime 'niet cachen' uit het cachedialoog werkend maken... || $ARCurrent->cachetime == 0) { |
||
1207 | ldSetBrowserCache(false); |
||
1208 | } |
||
1209 | ldHeader("Content-Length: ".$image_len); |
||
1210 | echo $image; |
||
1211 | } |
||
1212 | } |
||
1213 |
An exit expression should only be used in rare cases. For example, if you write a short command line script.
In most cases however, using an
exit
expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.