SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Helpers/Ssl.php

  1. <?php
  2. /*
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  */
  13.  
  14. namespace Sprout\Helpers;
  15.  
  16. use Kohana;
  17.  
  18.  
  19. /**
  20.  * Used to force an SSL (i.e. HTTPS) connection for configured controllers and methods.
  21.  */
  22. class Ssl
  23. {
  24.  
  25. /**
  26.   * Checks the current request against the list of require_ssl controllers/actions and force SSL for them.
  27.   * If the user accesses a page not on the list, in SSL mode, we let them anyway.
  28.   **/
  29. public static function check()
  30. {
  31. $nonssl = true;
  32.  
  33. if (PHP_SAPI === 'cli') return;
  34.  
  35. // Iterate through the controllers, looking for a match
  36. foreach (Kohana::config('require_ssl.require_ssl') as $controller => $methods) {
  37. if (Router::$controller == $controller) {
  38.  
  39. if ($methods == '*') {
  40. // Match any method
  41. if (Request::protocol() != 'https') {
  42. Url::redirect(Url::base(false, 'https') . Url::current(true));
  43. }
  44. $nonssl = false;
  45.  
  46. } else {
  47. if (in_array(Router::$method, $methods)) {
  48. if (Request::protocol() != 'https') {
  49. Url::redirect(Url::base(false, 'https') . Url::current(true));
  50. }
  51. $nonssl = false;
  52. }
  53. }
  54. }
  55. }
  56. }
  57.  
  58. }
  59.