Skip to content

Commit 59ecd48

Browse files
committed
Begin Twig
1 parent 7e347db commit 59ecd48

File tree

94 files changed

+8630
-112
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+8630
-112
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
"slim/flash": "^0.1.0",
3131
"gettext/gettext": "^4.2",
3232
"s9e/text-formatter": "^0.9",
33-
"featherbb/php-utf-8": "dev-master"
33+
"featherbb/php-utf-8": "dev-master",
34+
"twig/twig": "1.31"
3435
},
3536
"support": {
3637
"email": "[email protected]",

featherbb/Controller/Index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function display($req, $res, $args)
3535
'online' => $this->model->usersOnline(),
3636
'forum_actions' => $this->model->forumActions(),
3737
'cur_cat' => 0
38-
])->addTemplate('index.php')->display();
38+
])->addTemplate('@forum/index')->display();
3939
}
4040

4141
public function rules()

featherbb/Core/RunBBTwig.php

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
<?php
2+
/**
3+
* Copyright 2017 [email protected]
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace FeatherBB\Core;
19+
20+
class RunBBTwig extends \Twig_Extension
21+
{
22+
23+
public function getName()
24+
{
25+
return 'runBB_Twig';
26+
}
27+
28+
public function getFunctions()
29+
{
30+
return [
31+
/**
32+
* fire RunBB hook with or without arguments
33+
*/
34+
new \Twig_SimpleFunction('fireHook', function ($name) {
35+
if (is_array($name)) {
36+
call_user_func_array([Container::get('hooks'), 'fire'], $name);
37+
} else {
38+
Container::get('hooks')->fire($name);
39+
}
40+
}, ['is_safe' => ['html']]),
41+
42+
/**
43+
* return RunBB settings value
44+
*/
45+
new \Twig_SimpleFunction('settings', function ($name) {
46+
return ForumSettings::get($name);
47+
}, ['is_safe' => ['html']]),
48+
49+
/**
50+
* Return the translation of a string with or without arguments
51+
*/
52+
new \Twig_SimpleFunction('trans', function ($str) {
53+
if (is_array($str)) {
54+
var_dump($str);
55+
return call_user_func_array('__', $str);
56+
} else {
57+
return __($str);
58+
}
59+
}, ['is_safe' => ['html']]),
60+
61+
/**
62+
* Return the translation of a string in a specific domain
63+
* where first argument in array must be domain name
64+
*/
65+
new \Twig_SimpleFunction('transd', function ($str) {
66+
if (is_array($str)) {
67+
return call_user_func_array('d__', $str);
68+
} else {
69+
return $str;
70+
}
71+
}, ['is_safe' => ['html']]),
72+
73+
/**
74+
* return Url::baseStatic() value
75+
*/
76+
new \Twig_SimpleFunction('baseStatic', function () {
77+
return Url::baseStatic();
78+
}, ['is_safe' => ['html']]),
79+
80+
/**
81+
* return Url::base() value
82+
*/
83+
new \Twig_SimpleFunction('urlBase', function () {
84+
return Url::base();
85+
}, ['is_safe' => ['html']]),
86+
87+
/**
88+
* return Url::slug() value
89+
*/
90+
new \Twig_SimpleFunction('slug', function ($url) {
91+
return Url::slug($url);
92+
}, ['is_safe' => ['html']]),
93+
94+
/**
95+
* return Router::pathFor() value
96+
*/
97+
new \Twig_SimpleFunction('pathFor', function ($name, array $data = [], array $queryParams = []) {
98+
return Router::pathFor($name, $data, $queryParams);
99+
}, ['is_safe' => ['html']]),
100+
101+
/**
102+
* return User::get()->value
103+
*/
104+
new \Twig_SimpleFunction('userGet', function ($val) {
105+
if (User::get()) {
106+
return User::get()->$val;
107+
} else {
108+
return $val;
109+
}
110+
}, ['is_safe' => ['html']]),
111+
112+
/**
113+
* return token FIXME ???
114+
*/
115+
new \Twig_SimpleFunction('getToken', function () {
116+
return Random::hash(User::get()->id.Random::hash(Utils::getIp()));
117+
}, ['is_safe' => ['html']]),
118+
119+
/**
120+
* return given type hash
121+
*/
122+
new \Twig_SimpleFunction('getHash', function ($type, $var) {
123+
if ($type === 'md5') {
124+
return md5($var);
125+
}// TODO add types
126+
}, ['is_safe' => ['html']]),
127+
128+
/**
129+
* return Container::get('utils')->timeFormat($var) result
130+
* Container::get('utils')->timeFormat(
131+
* $timestamp, $date_only, $date_format, $time_format, $time_only, $no_text
132+
* )
133+
*/
134+
new \Twig_SimpleFunction('formatTime', function (
135+
$timestamp,
136+
$date_only = false,
137+
$date_format = null,
138+
$time_format = null,
139+
$time_only = false,
140+
$no_text = false
141+
) {
142+
return Container::get('utils')->formatTime(
143+
$timestamp,
144+
$date_only,
145+
$date_format,
146+
$time_format,
147+
$time_only,
148+
$no_text
149+
);
150+
}, ['is_safe' => ['html']]),
151+
152+
/**
153+
* return Utils::numberFormat($var) result
154+
*/
155+
new \Twig_SimpleFunction('formatNumber', function ($var) {
156+
return Utils::forumNumberFormat($var);
157+
}, ['is_safe' => ['html']]),
158+
159+
/**
160+
* return Input::post() result
161+
* from Request::getParsedBodyParam
162+
*/
163+
new \Twig_SimpleFunction('inputPost', function ($var) {
164+
return Input::post($var);
165+
}, ['is_safe' => ['html']]),
166+
167+
/**
168+
* Format user title
169+
* return Utils::getTitle($title, $name, $groupTitle, $gid) result
170+
*/
171+
new \Twig_SimpleFunction('formatTitle', function ($title, $name = '', $groupTitle = '', $gid = '') {
172+
return Utils::getTitleTwig($title, $name, $groupTitle, $gid);
173+
}, ['is_safe' => ['html']]),
174+
175+
/**
176+
* Get forum environment var
177+
* TODO merge with settings???
178+
* return ForumEnv::get($var) result
179+
*/
180+
new \Twig_SimpleFunction('getEnv', function ($var) {
181+
return ForumEnv::get($var);
182+
}, ['is_safe' => ['html']]),
183+
184+
/**
185+
* Generate breadcrumbs from an array of name and URLs
186+
* return AdminUtils::breadcrumbsAdmin($links) result
187+
*/
188+
new \Twig_SimpleFunction('breadcrumbsAdmin', function (array $links) {
189+
return AdminUtils::breadcrumbsAdmin($links);
190+
}, ['is_safe' => ['html']]),
191+
192+
/**
193+
* Generate increment
194+
* return incremented index
195+
*/
196+
new \Twig_SimpleFunction('getIndex', function () {
197+
static $index = 0;
198+
return ++$index;
199+
}, ['is_safe' => ['html']]),
200+
201+
/**
202+
* Unserializer
203+
* return unserialized array
204+
*/
205+
new \Twig_SimpleFunction('unSerialize', function ($var) {
206+
return unserialize($var);
207+
}, ['is_safe' => ['html']]),
208+
];
209+
}
210+
}

featherbb/Core/Utils.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,47 @@ public static function getTitle($user)
261261
return $userTitle;
262262
}
263263

264+
/**
265+
* Determines the correct user title
266+
*
267+
* @param string $title
268+
* @param string $name
269+
* @param string $groupTitle
270+
* @param int $gid
271+
* @return string
272+
*/
273+
public static function getTitleTwig($title = '', $name = '', $groupTitle = '', $gid = 0)
274+
{
275+
static $ban_list;
276+
277+
// If not already built in a previous call, build an array of lowercase banned usernames
278+
if (empty($ban_list)) {
279+
$ban_list = [];
280+
foreach (Container::get('bans') as $cur_ban) {
281+
$ban_list[] = utf8_strtolower($cur_ban['username']);
282+
}
283+
}
284+
285+
// If the user has a custom title
286+
if ($title != '') {
287+
$user_title = self::escape($title);
288+
} // If the user is banned
289+
elseif (in_array(utf8_strtolower($name), $ban_list)) {
290+
$user_title = __('Banned');
291+
} // If the user group has a default user title
292+
elseif ($groupTitle != '') {
293+
$user_title = self::escape($groupTitle);
294+
} // If the user is a guest
295+
elseif ($gid == ForumEnv::get('FEATHER_GUEST')) {
296+
$user_title = __('Guest');
297+
} // If nothing else helps, we assign the default
298+
else {
299+
$user_title = __('Member');
300+
}
301+
302+
return $user_title;
303+
}
304+
264305
//
265306
// Replace censored words in $text
266307
//

0 commit comments

Comments
 (0)