overwritethemebackgroundcss.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social 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. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Action with the CSS preferences
  18. *
  19. * @category Plugin
  20. * @package GNUsocial
  21. * @author Diogo Cordeiro <diogo@fc.up.pt>
  22. * @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
  23. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  24. */
  25. defined('GNUSOCIAL') || die();
  26. /**
  27. * Route with CSS that will come after theme's css in order to overwrite it with sysadmin's custom background preferences
  28. *
  29. * @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
  30. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  31. */
  32. class overwritethemebackgroundcssAction extends Action
  33. {
  34. protected $needLogin = false;
  35. protected $canPost = false;
  36. // Eventual custom background sysadmin may have set
  37. private $_theme_background_url = false;
  38. private $_background_colour = false;
  39. private $_background_colour_important = false;
  40. /**
  41. * Fills _theme_background_url if possible.
  42. *
  43. * @param array $args $_REQUEST array
  44. * @return bool true
  45. * @throws ClientException
  46. */
  47. protected function prepare(array $args = []): bool
  48. {
  49. parent::prepare($args);
  50. if (GNUsocial::isHTTPS()) {
  51. $background_url = common_config('overwritethemebackground', 'sslbackground-image');
  52. if (empty($background_url)) {
  53. // if background is an uploaded file, try to fall back to HTTPS file URL
  54. $http_url = common_config('overwritethemebackground', 'background-image');
  55. if (!empty($http_url)) {
  56. try {
  57. $f = File::getByUrl($http_url);
  58. if (!empty($f->filename)) {
  59. // this will handle the HTTPS case
  60. $background_url = File::url($f->filename);
  61. }
  62. } catch (NoResultException $e) {
  63. // no match
  64. }
  65. }
  66. }
  67. } else {
  68. $background_url = common_config('overwritethemebackground', 'background-image');
  69. }
  70. $this->_background_colour = common_config('overwritethemebackground', 'background-color');
  71. if (empty($background_url)) {
  72. if (!empty($this->_background_colour)) {
  73. $this->_background_colour_important = true; // We want the colour to override theme's default background
  74. return true;
  75. }
  76. /*if (file_exists(Theme::file('images/bg.png'))) {
  77. // This should handle the HTTPS case internally
  78. $background_url = Theme::path('images/bg.png');
  79. }
  80. if (!empty($background_url)) {
  81. $this->_theme_background_url = $background_url;
  82. }*/
  83. } else {
  84. $this->_theme_background_url = $background_url;
  85. }
  86. return true;
  87. }
  88. /**
  89. * Is this action read-only?
  90. *
  91. * @param array $args other arguments dummy
  92. * @return bool true
  93. */
  94. public function isReadOnly($args): bool
  95. {
  96. return true;
  97. }
  98. /**
  99. * Print the CSS
  100. */
  101. public function handle(): void
  102. {
  103. $background_position_options = [
  104. 'initial',
  105. 'left top',
  106. 'left center',
  107. 'left bottom',
  108. 'right top',
  109. 'right center',
  110. 'right bottom',
  111. 'center top',
  112. 'center center',
  113. 'center bottom'
  114. ];
  115. header("Content-type: text/css", true);
  116. $background_color = $this->_background_colour;
  117. $background_image = $this->_theme_background_url;
  118. $background_repeat = ['repeat', 'repeat-x', 'repeat-y', 'no-repeat'][common_config('overwritethemebackground', 'background-repeat')];
  119. $background_position = $background_position_options[common_config('overwritethemebackground', 'background-position')];
  120. $background_attachment = ['scroll', 'fixed'][common_config('overwritethemebackground', 'background-attachment')];
  121. $css = 'body {';
  122. if ($background_color) {
  123. if (!$this->_background_colour_important) {
  124. $css .= 'background-color: ' . $background_color . ';';
  125. } else {
  126. $css .= 'background: ' . $background_color . ' !important;';
  127. }
  128. }
  129. if ($background_image) {
  130. $css .= 'background-image: url(' . $background_image . ');';
  131. }
  132. if ($background_repeat) {
  133. $css .= 'background-repeat: ' . $background_repeat . ';';
  134. }
  135. if ($background_position) {
  136. $css .= 'background-position: ' . $background_position . ';';
  137. }
  138. if ($background_attachment) {
  139. $css .= 'background-attachment: ' . $background_attachment . ';';
  140. }
  141. $css .= '}';
  142. echo $css;
  143. }
  144. }