forked from fossar/selfoss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathView.php
More file actions
162 lines (136 loc) · 4.02 KB
/
View.php
File metadata and controls
162 lines (136 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?PHP
namespace helpers;
/**
* Helper class for rendering template
*
* @package helpers
* @copyright Copyright (c) Tobias Zeising (http://www.aditu.de)
* @license GPLv3 (http://www.gnu.org/licenses/gpl-3.0.html)
* @author Tobias Zeising <tobias.zeising@aditu.de>
*/
class View {
/**
* current base url
* @var string
*/
public $base = '';
/**
* set global view vars
*/
function __construct() {
$this->genMinifiedJsAndCss();
$this->base = $this->getBaseUrl();
}
/**
* Returns the base url of the page. If a base url was configured in the
* config.ini this will be used. Otherwise base url will be generated by
* globale server variables ($_SERVER).
*/
public function getBaseUrl() {
$base = '';
// base url in config.ini file
if(strlen(trim(\F3::get('base_url')))>0) {
$base = \F3::get('base_url');
$length = strlen($base);
if($length>0 && substr($base, $length-1, 1)!="/")
$base .= '/';
// auto generate base url
} else {
$lastSlash = strrpos($_SERVER['SCRIPT_NAME'], '/');
$subdir = $lastSlash!==false ? substr($_SERVER['SCRIPT_NAME'], 0, $lastSlash) : '';
$base = 'http' .
(isset($_SERVER["HTTPS"])=="on" ? 's' : '') .
'://' . $_SERVER["SERVER_NAME"] .
($_SERVER["SERVER_PORT"]!="80" ? ':'.$_SERVER["SERVER_PORT"] . '' : '') .
$subdir .
'/';
}
return $base;
}
/**
* render template
*
* @return string rendered html
* @param string $template file
*/
public function render($template) {
ob_start();
include $template;
$content = ob_get_contents();
ob_end_clean();
return $content;
}
/**
* send error message
*
* @return void
* @param string $message
*/
public function error($message) {
header("HTTP/1.0 400 Bad Request");
die($message);
}
/**
* send error message as json string
*
* @return void
* @param mixed $datan
*/
public function jsonError($data) {
$this->error( json_encode($data) );
}
/**
* send success message as json string
*
* @return void
* @param mixed $datan
*/
public function jsonSuccess($data) {
die(json_encode($data));
}
/**
* generate minified css and js
*
* @return void
*/
public function genMinifiedJsAndCss() {
// minify js
$targetJs = \F3::get('BASEDIR').'/public/all.js';
if(!file_exists($targetJs) || \F3::get('DEBUG')!=0) {
$js = "";
foreach(\F3::get('js') as $file)
$js = $js . "\n" . $this->minifyJs(file_get_contents(\F3::get('BASEDIR').'/'.$file));
file_put_contents($targetJs, $js);
}
// minify css
$targetCss = \F3::get('BASEDIR').'/public/all.css';
if(!file_exists($targetCss) || \F3::get('DEBUG')!=0) {
$css = "";
foreach(\F3::get('css') as $file)
$css = $css . "\n" . $this->minifyCss(file_get_contents(\F3::get('BASEDIR').'/'.$file));
file_put_contents($targetCss, $css);
}
}
/**
* minifies javascript if DEBUG mode is disabled
*
* @return minified javascript
* @param javascript to minify
*/
private function minifyJs($content) {
if(\F3::get('DEBUG')!=0)
return $content;
return \JSMin::minify($content);
}
/**
* minifies css if DEBUG mode is disabled
*
* @return minified css
* @param css to minify
*/
private function minifyCss($content) {
if(\F3::get('DEBUG')!=0)
return $content;
return \CssMin::minify($content);
}
}