HTTPSEverywhereRuleset.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /*
  3. HTTPSEverywhere for Desktopd
  4. Copyright (C) 2015 Desktopd Developer(s)
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU Affero General Public License as
  7. published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Affero General Public License for more details.
  13. You should have received a copy of the GNU Affero General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. https://www.gnu.org/licenses/agpl.html
  16. */
  17. namespace Desktopd\HTTPSEverywhere;
  18. class HTTPSEverywhereRuleset {
  19. protected $name = '';
  20. protected $platforms = array();
  21. protected $exclusions = array();
  22. protected $patterns = array();
  23. protected $downgradePatterns = array();
  24. protected $disabled = false;
  25. public function __construct ($name) {
  26. $this->name = "$name";
  27. }
  28. public function disable () {
  29. $this->disabled = true;
  30. }
  31. public function enable () {
  32. $this->disabled = false;
  33. }
  34. public function addPattern ($from, $to, $allowDowngrade = false) {
  35. $this->patterns[$from] = $to;
  36. if ($allowDowngrade) {
  37. $this->downgradePatterns[$from] = true;
  38. }
  39. }
  40. public function addExclusion ($pattern) {
  41. $this->exclusions[] = $pattern;
  42. }
  43. public function addPlatform ($platform) {
  44. if ($platform) {
  45. $this->platforms[] = $platform;
  46. }
  47. }
  48. public function platformSupported ($platform) {
  49. if (count($this->platforms) == 0) {
  50. return true; // not specified
  51. }
  52. return in_array($platform, $this->platforms, true);
  53. }
  54. protected function isHTTPS ($uri) {
  55. return preg_match('~^https://.+$~', $uri);
  56. }
  57. public function rewrite ($uri) {
  58. if ($this->disabled) return $uri;
  59. // NOTE: \xff is used as a delimiter
  60. foreach ($this->exclusions as $pattern) {
  61. if (!$pattern) continue;
  62. if (preg_match("\xff{$pattern}\xff", $uri)) {
  63. return $uri; // no rewrite
  64. }
  65. }
  66. foreach ($this->patterns as $from => $to) {
  67. if (!$from) continue;
  68. if (!preg_match("\xff{$from}\xff", $uri)) continue;
  69. $rewritten = preg_replace("\xff{$from}\xff", $to, $uri);
  70. if (!$this->isHTTPS($rewritten)) {
  71. // downgrade rewrite
  72. if (isset($this->downgradePatterns[$from])) {
  73. // downgrade allowd
  74. $uri = $rewritten;
  75. }
  76. } else {
  77. // rewrite to HTTPS
  78. $uri = $rewritten;
  79. }
  80. }
  81. return $uri;
  82. }
  83. public function getName () {
  84. return $this->name;
  85. }
  86. }
  87. // vim: ts=4 et ai