Update copyright
[friendica.git/.git] / view / theme / frio / php / scheme.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  * Get info header of the scheme
21  *
22  * This function parses the header of the schemename.php file for informations like
23  * Author, Description and Overwrites. Most of the code comes from the Addon::getInfo()
24  * function. We use this to get the variables which get overwritten through the scheme.
25  * All color variables which get overwritten through the theme have to be
26  * listed (comma separated) in the scheme header under Overwrites:
27  * This seems not to be the best solution. We need to investigate further.
28  *
29  * @param string $scheme Name of the scheme
30  * @return array With theme information
31  *    'author' => Author Name
32  *    'description' => Scheme description
33  *    'version' => Scheme version
34  *    'overwrites' => Variables which overwriting custom settings
35  */
36
37 use Friendica\DI;
38 use Friendica\Util\Strings;
39
40 function get_scheme_info($scheme)
41 {
42         $theme = DI::app()->getCurrentTheme();
43         $themepath = 'view/theme/' . $theme . '/';
44         if (empty($scheme)) {
45                 $scheme = DI::pConfig()->get(local_user(), 'frio', 'scheme', DI::pConfig()->get(local_user(), 'frio', 'schema'));
46         }
47
48         $scheme = Strings::sanitizeFilePathItem($scheme);
49
50         $info = [
51                 'name' => $scheme,
52                 'description' => '',
53                 'author' => [],
54                 'version' => '',
55                 'overwrites' => [],
56                 'accented' => false,
57         ];
58
59         if (!is_file($themepath . 'scheme/' . $scheme . '.php')) return $info;
60
61         $f = file_get_contents($themepath . 'scheme/' . $scheme . '.php');
62
63         $r = preg_match('|/\*.*\*/|msU', $f, $m);
64
65         if ($r) {
66                 $ll = explode("\n", $m[0]);
67                 foreach ($ll as $l) {
68                         $l = trim($l, "\t\n\r */");
69                         if ($l != '') {
70                                 $values = array_map('trim', explode(':', $l, 2));
71                                 if (count($values) < 2) {
72                                         continue;
73                                 }
74                                 list($k, $v) = $values;
75                                 $k = strtolower($k);
76                                 if ($k == 'author') {
77                                         $r = preg_match('|([^<]+)<([^>]+)>|', $v, $m);
78                                         if ($r) {
79                                                 $info['author'][] = ['name' => $m[1], 'link' => $m[2]];
80                                         } else {
81                                                 $info['author'][] = ['name' => $v];
82                                         }
83                                 } elseif ($k == 'overwrites') {
84                                         $theme_settings = explode(',', str_replace(' ', '', $v));
85                                         foreach ($theme_settings as $key => $value) {
86                                                 $info['overwrites'][$value] = true;
87                                         }
88                                 } elseif ($k == 'accented') {
89                                         $info['accented'] = $v && $v != 'false' && $v != 'no';
90                                 } else {
91                                         if (array_key_exists($k, $info)) {
92                                                 $info[$k] = $v;
93                                         }
94                                 }
95                         }
96                 }
97         }
98
99         return $info;
100 }