xref: /plugin/authssocas/vendor/apereo/phpcas/source/CAS/ServiceBaseUrl/Base.php (revision d10b5556242e78d8a430c323b91984ec16415a46)
1*d10b5556SXylle<?php
2*d10b5556SXylle
3*d10b5556SXylle/**
4*d10b5556SXylle * Licensed to Jasig under one or more contributor license
5*d10b5556SXylle * agreements. See the NOTICE file distributed with this work for
6*d10b5556SXylle * additional information regarding copyright ownership.
7*d10b5556SXylle *
8*d10b5556SXylle * Jasig licenses this file to you under the Apache License,
9*d10b5556SXylle * Version 2.0 (the "License"); you may not use this file except in
10*d10b5556SXylle * compliance with the License. You may obtain a copy of the License at:
11*d10b5556SXylle *
12*d10b5556SXylle * http://www.apache.org/licenses/LICENSE-2.0
13*d10b5556SXylle *
14*d10b5556SXylle * Unless required by applicable law or agreed to in writing, software
15*d10b5556SXylle * distributed under the License is distributed on an "AS IS" BASIS,
16*d10b5556SXylle * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17*d10b5556SXylle * See the License for the specific language governing permissions and
18*d10b5556SXylle * limitations under the License.
19*d10b5556SXylle *
20*d10b5556SXylle * PHP Version 7
21*d10b5556SXylle *
22*d10b5556SXylle * @file     CAS/ServiceBaseUrl/Base.php
23*d10b5556SXylle * @category Authentication
24*d10b5556SXylle * @package  PhpCAS
25*d10b5556SXylle * @author   Henry Pan <git@phy25.com>
26*d10b5556SXylle * @license  http://www.apache.org/licenses/LICENSE-2.0  Apache License 2.0
27*d10b5556SXylle * @link     https://wiki.jasig.org/display/CASC/phpCAS
28*d10b5556SXylle */
29*d10b5556SXylle
30*d10b5556SXylle/**
31*d10b5556SXylle * Base class of CAS/ServiceBaseUrl that implements isHTTPS method.
32*d10b5556SXylle *
33*d10b5556SXylle * @class    CAS_ServiceBaseUrl_Base
34*d10b5556SXylle * @category Authentication
35*d10b5556SXylle * @package  PhpCAS
36*d10b5556SXylle * @author   Henry Pan <git@phy25.com>
37*d10b5556SXylle * @license  http://www.apache.org/licenses/LICENSE-2.0  Apache License 2.0
38*d10b5556SXylle * @link     https://wiki.jasig.org/display/CASC/phpCAS
39*d10b5556SXylle */
40*d10b5556SXylleabstract class CAS_ServiceBaseUrl_Base
41*d10b5556SXylleimplements CAS_ServiceBaseUrl_Interface
42*d10b5556SXylle{
43*d10b5556SXylle
44*d10b5556SXylle    /**
45*d10b5556SXylle     * Get PHP server name.
46*d10b5556SXylle     *
47*d10b5556SXylle     * @return string the server hostname and port of the server
48*d10b5556SXylle     */
49*d10b5556SXylle    abstract public function get();
50*d10b5556SXylle
51*d10b5556SXylle    /**
52*d10b5556SXylle     * Check whether HTTPS is used.
53*d10b5556SXylle     *
54*d10b5556SXylle     * This is used to construct the protocol in the URL.
55*d10b5556SXylle     *
56*d10b5556SXylle     * @return bool true if HTTPS is used
57*d10b5556SXylle     */
58*d10b5556SXylle    public function isHttps() {
59*d10b5556SXylle        if (!empty($_SERVER['HTTP_X_FORWARDED_PROTO'])) {
60*d10b5556SXylle            return ($_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https');
61*d10b5556SXylle        } elseif (!empty($_SERVER['HTTP_X_FORWARDED_PROTOCOL'])) {
62*d10b5556SXylle            return ($_SERVER['HTTP_X_FORWARDED_PROTOCOL'] === 'https');
63*d10b5556SXylle        } elseif ( isset($_SERVER['HTTPS'])
64*d10b5556SXylle            && !empty($_SERVER['HTTPS'])
65*d10b5556SXylle            && strcasecmp($_SERVER['HTTPS'], 'off') !== 0
66*d10b5556SXylle        ) {
67*d10b5556SXylle            return true;
68*d10b5556SXylle        }
69*d10b5556SXylle        return false;
70*d10b5556SXylle    }
71*d10b5556SXylle
72*d10b5556SXylle    /**
73*d10b5556SXylle     * Remove standard HTTP and HTTPS port for discovery and allowlist input.
74*d10b5556SXylle     *
75*d10b5556SXylle     * @param $url URL as https://domain:port without trailing slash
76*d10b5556SXylle     * @return standardized URL, or the original URL
77*d10b5556SXylle     * @throws CAS_InvalidArgumentException if the URL does not include the protocol
78*d10b5556SXylle     */
79*d10b5556SXylle    protected function removeStandardPort($url) {
80*d10b5556SXylle        if (strpos($url, "://") === false) {
81*d10b5556SXylle            throw new CAS_InvalidArgumentException(
82*d10b5556SXylle                "Configured base URL should include the protocol string: " . $url);
83*d10b5556SXylle        }
84*d10b5556SXylle
85*d10b5556SXylle        $url = rtrim($url, '/');
86*d10b5556SXylle
87*d10b5556SXylle        if (strpos($url, "https://") === 0 && substr_compare($url, ':443', -4) === 0) {
88*d10b5556SXylle            return substr($url, 0, -4);
89*d10b5556SXylle        }
90*d10b5556SXylle
91*d10b5556SXylle        if (strpos($url, "http://") === 0 && substr_compare($url, ':80', -3) === 0) {
92*d10b5556SXylle            return substr($url, 0, -3);
93*d10b5556SXylle        }
94*d10b5556SXylle
95*d10b5556SXylle        return $url;
96*d10b5556SXylle    }
97*d10b5556SXylle
98*d10b5556SXylle}
99