| 1: | <?php |
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | namespace SimpleID; |
| 24: | |
| 25: | use SimpleID\Auth\AuthManager; |
| 26: | use SimpleID\Util\UI\Template; |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | |
| 35: | |
| 36: | |
| 37: | |
| 38: | |
| 39: | |
| 40: | abstract class Module extends \Prefab { |
| 41: | |
| 42: | |
| 43: | |
| 44: | protected $f3; |
| 45: | |
| 46: | |
| 47: | |
| 48: | |
| 49: | protected $logger; |
| 50: | |
| 51: | |
| 52: | |
| 53: | |
| 54: | |
| 55: | |
| 56: | |
| 57: | |
| 58: | |
| 59: | |
| 60: | |
| 61: | |
| 62: | |
| 63: | |
| 64: | public static function init($f3) { |
| 65: | |
| 66: | } |
| 67: | |
| 68: | |
| 69: | |
| 70: | |
| 71: | |
| 72: | |
| 73: | |
| 74: | |
| 75: | |
| 76: | public function __construct() { |
| 77: | $this->f3 = \Base::instance(); |
| 78: | $this->logger = $this->f3->get('logger'); |
| 79: | |
| 80: | $mgr = ModuleManager::instance(); |
| 81: | $info = $mgr->getModuleInfo(get_class($this)); |
| 82: | } |
| 83: | |
| 84: | |
| 85: | |
| 86: | |
| 87: | |
| 88: | |
| 89: | |
| 90: | |
| 91: | |
| 92: | public function beforeroute() { |
| 93: | $auth = AuthManager::instance(); |
| 94: | $auth->initSession(); |
| 95: | $auth->initUser(); |
| 96: | } |
| 97: | |
| 98: | |
| 99: | |
| 100: | |
| 101: | |
| 102: | |
| 103: | |
| 104: | |
| 105: | |
| 106: | |
| 107: | |
| 108: | |
| 109: | |
| 110: | protected function isHttps() { |
| 111: | return ($this->f3->get('SCHEME') == 'https') |
| 112: | || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && (strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']) == 'https')) |
| 113: | || (isset($_SERVER['HTTP_FRONT_END_HTTPS']) && ($_SERVER['HTTP_FRONT_END_HTTPS'] == 'on')); |
| 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: | protected function checkHttps($action = 'redirect', $allow_override = false, $redirect_url = null, $strict = true) { |
| 140: | if ($this->f3->get('CLI')) return; |
| 141: | |
| 142: | if ($this->isHttps()) { |
| 143: | if ($strict) header('Strict-Transport-Security: max-age=3600'); |
| 144: | return; |
| 145: | } |
| 146: | |
| 147: | $config = $this->f3->get('config'); |
| 148: | |
| 149: | if ($allow_override && $config['allow_plaintext']) return; |
| 150: | |
| 151: | if ($action == 'error') { |
| 152: | header('Upgrade: TLS/1.2, HTTP/1.1'); |
| 153: | header('Connection: Upgrade'); |
| 154: | $this->fatalError($this->f3->get('intl.common.require_https'), 426); |
| 155: | exit; |
| 156: | } |
| 157: | |
| 158: | if ($redirect_url == null) $redirect_url = $this->getCanonicalURL($this->f3->get('PATH'), $this->f3->get('SERVER.QUERY_STRING'), 'https'); |
| 159: | |
| 160: | $this->f3->status(301); |
| 161: | header('Location: ' . $redirect_url); |
| 162: | exit; |
| 163: | } |
| 164: | |
| 165: | |
| 166: | |
| 167: | |
| 168: | |
| 169: | |
| 170: | |
| 171: | |
| 172: | |
| 173: | |
| 174: | |
| 175: | |
| 176: | |
| 177: | public function getCanonicalURL($path = '', $query = '', $secure = null) { |
| 178: | $config = $this->f3->get('config'); |
| 179: | $canonical_base_path = $config['canonical_base_path']; |
| 180: | |
| 181: | if (preg_match('/^(?:@(\w+)(?:(\(.+?)\))*|https?:\/\/)/', $path, $parts, PREG_UNMATCHED_AS_NULL)) { |
| 182: | if (isset($parts[1])) { |
| 183: | $aliases = $this->f3->get('ALIASES'); |
| 184: | |
| 185: | if (!empty($aliases[$parts[1]])) { |
| 186: | $path = $aliases[$parts[1]]; |
| 187: | $path = $this->f3->build($path, isset($parts[2]) ? $this->f3->parse($parts[2]) : []); |
| 188: | $path = ltrim($path, '/'); |
| 189: | } |
| 190: | } |
| 191: | } |
| 192: | |
| 193: | |
| 194: | if ((substr($config['canonical_base_path'], -1) == '/')) { |
| 195: | $url = $config['canonical_base_path']; |
| 196: | } else { |
| 197: | $url = $config['canonical_base_path'] . '/'; |
| 198: | } |
| 199: | |
| 200: | if (($secure == 'https') && (stripos($url, 'http:') === 0)) { |
| 201: | $url = 'https:' . substr($url, 5); |
| 202: | } |
| 203: | if (($secure == 'http') && (stripos($url, 'https:') === 0)) { |
| 204: | $url = 'http:' . substr($url, 6); |
| 205: | } |
| 206: | if (($secure == 'detect') && ($this->isHttps()) && (stripos($url, 'http:') === 0)) { |
| 207: | $url = 'https:' . substr($url, 5); |
| 208: | } |
| 209: | if (($secure == 'detect') && (!$this->isHttps()) && (stripos($url, 'https:') === 0)) { |
| 210: | $url = 'http:' . substr($url, 6); |
| 211: | } |
| 212: | |
| 213: | $url .= $path . (($query == '') ? '' : '?' . $query); |
| 214: | |
| 215: | return $url; |
| 216: | } |
| 217: | |
| 218: | |
| 219: | |
| 220: | |
| 221: | |
| 222: | |
| 223: | |
| 224: | |
| 225: | |
| 226: | |
| 227: | |
| 228: | |
| 229: | |
| 230: | |
| 231: | public function getCanonicalHost($secure = null) { |
| 232: | $config = $this->f3->get('config'); |
| 233: | $canonical_base_path = $config['canonical_base_path']; |
| 234: | |
| 235: | $parts = parse_url($canonical_base_path); |
| 236: | if ($parts == false) return $canonical_base_path; |
| 237: | |
| 238: | if ($secure == 'https') { |
| 239: | $scheme = 'https'; |
| 240: | } elseif ($secure == 'http') { |
| 241: | $scheme = 'http'; |
| 242: | } else { |
| 243: | $scheme = $parts['scheme']; |
| 244: | } |
| 245: | |
| 246: | $url = $scheme . '://'; |
| 247: | if (isset($parts['user'])) { |
| 248: | $url .= $parts['user']; |
| 249: | if (isset($parts['pass'])) $url .= ':' . $parts['pass']; |
| 250: | $url .= '@'; |
| 251: | } |
| 252: | if (isset($parts['host'])) $url .= $parts['host']; |
| 253: | if (isset($parts['port'])) $url .= ':' . $parts['port']; |
| 254: | |
| 255: | return $url; |
| 256: | } |
| 257: | |
| 258: | |
| 259: | |
| 260: | |
| 261: | |
| 262: | |
| 263: | |
| 264: | |
| 265: | protected function getOrigin($uri) { |
| 266: | $parts = parse_url($uri); |
| 267: | if ($parts == false) return $uri; |
| 268: | |
| 269: | $origin = $parts['scheme'] . '://'; |
| 270: | $origin .= $parts['host']; |
| 271: | if (isset($parts['port'])) $origin .= ':' . $parts['port']; |
| 272: | |
| 273: | return $origin; |
| 274: | } |
| 275: | |
| 276: | |
| 277: | |
| 278: | |
| 279: | |
| 280: | |
| 281: | |
| 282: | |
| 283: | protected function fatalError(string $error, int $code = 500) { |
| 284: | |
| 285: | static $extra_status_codes = [ |
| 286: | 426 => 'Upgrade Required' |
| 287: | ]; |
| 288: | |
| 289: | if (isset($extra_status_codes[$code])) { |
| 290: | $title = $extra_status_codes[$code]; |
| 291: | if (!$this->f3->get('CLI') && !headers_sent()) |
| 292: | header($_SERVER['SERVER_PROTOCOL'] . ' ' . $code . ' ' . $title); |
| 293: | } else { |
| 294: | |
| 295: | $title = $this->f3->status($code); |
| 296: | } |
| 297: | |
| 298: | $this->f3->expire(-1); |
| 299: | $trace = $this->f3->trace(); |
| 300: | |
| 301: | if ($this->f3->get('CLI')) { |
| 302: | $exit_code = 1; |
| 303: | echo PHP_EOL.'===================================' |
| 304: | .PHP_EOL.'ERROR ' . $code . ' - '. $title |
| 305: | .PHP_EOL.$error; |
| 306: | } else { |
| 307: | $exit_code = 0; |
| 308: | $this->f3->set('error', $error); |
| 309: | if ($this->f3->get('DEBUG') > 0) $this->f3->set('trace', $trace); |
| 310: | |
| 311: | $this->f3->set('page_class', 'is-dialog-page'); |
| 312: | $this->f3->set('title', $title); |
| 313: | $this->f3->set('layout', 'fatal_error.html'); |
| 314: | |
| 315: | $tpl = Template::instance(); |
| 316: | print $tpl->render('page.html'); |
| 317: | } |
| 318: | exit($exit_code); |
| 319: | } |
| 320: | |
| 321: | |
| 322: | |
| 323: | |
| 324: | |
| 325: | |
| 326: | |
| 327: | |
| 328: | |
| 329: | |
| 330: | public function secureCompare($str1, $str2) { |
| 331: | if (function_exists('hash_equals')) return hash_equals($str1, $str2); |
| 332: | |
| 333: | $xor = $str1 ^ $str2; |
| 334: | $result = strlen($str1) ^ strlen($str2); |
| 335: | for ($i = strlen($xor) - 1; $i >= 0; $i--) $result += ord($xor[$i]); |
| 336: | return !$result; |
| 337: | } |
| 338: | } |
| 339: | |
| 340: | ?> |
| 341: | |