forked from projectsend/projectsend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.templates.php
197 lines (165 loc) · 5.17 KB
/
functions.templates.php
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
/**
* Functions related to the files list templates
*/
/**
* Get the template and author information from template.php
* Based on the WordPress (LOVE IT!) function get_file_data()
*
* @param [type] $template_file
* @return array
*/
function extract_template_info($template_directory)
{
if (empty($template_directory)) {
return false;
}
$folder = str_replace(TEMPLATES_DIR . DS, '', $template_directory);
$read_file = $template_directory . DS . 'template.php';
$fp = fopen( $read_file, 'r' );
$file_info = fread( $fp, 8192 );
fclose( $fp );
$file_info = str_replace( "\r", "\n", $file_info );
$template_info = array(
'name' => 'Template name',
'themeuri' => 'URI',
'author' => 'Author',
'authoruri' => 'Author URI',
'authoremail' => 'Author e-mail',
'domain' => 'Domain',
'description' => 'Description',
);
foreach ( $template_info as $data => $regex ) {
if ( preg_match( '/^[ \t\/*#@]*' . preg_quote( $regex, '/' ) . ':(.*)$/mi', $file_info, $match ) && $match[1] )
$template_info[ $data ] = html_output($match[1]);
else
$template_info[ $data ] = '';
}
if ( empty( $template_info['name'] ) ) {
$template_info['name'] = $template_directory;
}
// Location is the value saved on the DB.
$template_info['location'] = $folder;
// Currently active template
if ( $folder == get_option('selected_clients_template') ) {
$template_info['active'] = 1;
}
// Look for the screenshot
$screenshot_file = $template_directory . DS . 'screenshot.png';
$cover_file = $template_directory . DS . 'cover.png';
$screenshot_url = TEMPLATES_URL . DS . $folder . DS . 'screenshot.png';
$cover_url = TEMPLATES_URL . DS . $folder . DS . 'cover.png';
$template_info['screenshot'] = ( file_exists( $screenshot_file ) ) ? $screenshot_url : ASSETS_IMG_URL . 'template-screenshot.png';
if ( file_exists( $cover_file ) ) {
$template_info['cover'] = $cover_url;
}
return $template_info;
}
/**
* Generates an array of valid templates to use on the options page.
*
* The template name must be defined on line 4 of template.php
*
* @return array
*/
function look_for_templates()
{
// Get all folders under the templates directory
$templates = [];
$templates_error = [];
$ignore = array('.', '..');
$base_directory = TEMPLATES_DIR . DS;
$directories = glob($base_directory . "*");
foreach ($directories as $directory) {
if (is_dir($directory) && !in_array($directory,$ignore)) {
if (check_template_integrity($directory)) {
$template_info = extract_template_info($directory);
// Generate the valid templates array
$templates[] = $template_info;
}
else {
// Generate another array with the templates that are not complete
$templates_error[] = [
'templates_error' => $directory
];
}
}
}
// Put active template as first element of the array
foreach ($templates as $index => $template) {
if (array_key_exists('active', $template) ) {
unset($templates[$index]);
array_unshift($templates, $template);
}
}
//print_array($templates);
return $templates;
}
/**
* Define the basic files that each template must have to be considered valid
*
* Each template must have at least two files:
* template.php and main.css
*
* @param [type] $folder
* @return bool
*/
function check_template_integrity($folder)
{
$required_files = [
'template.php',
];
$miss = 0;
$found = glob($folder . "/*");
foreach ($required_files as $required) {
$this_file = $folder . '/' . $required;
if (!in_array($this_file, $found)) {
$miss++;
}
}
if ($miss == 0) {
return true;
}
unset($miss);
return false;
}
/**
* Prepare the current files template and show it
*
* @return void
*/
function set_up_template()
{
// Load values from the config file
// If config file doesn't exist, set default values
// Include the common functions
// Include the main template file
}
function template_load_translation($template)
{
$lang = (isset($_SESSION['lang'])) ? $_SESSION['lang'] : SITE_LANG;
if(!isset($ld)) { $ld = 'cftp_admin'; }
$mo_file = ROOT_DIR.DS."templates".DS.$template.DS."lang".DS."{$lang}.mo";
ProjectSend\Classes\I18n::LoadDomain($mo_file, $ld);
}
function get_template_file_location($file)
{
$location = get_selected_template_path() . $file;
if (file_exists($location)) {
return $location;
}
$default_location = get_default_template_path() . $file;
if (file_exists($default_location)) {
return $default_location;
}
}
function get_selected_template_path()
{
$path = ROOT_DIR.DS.'templates'.DS.get_option('selected_clients_template').DS;
return $path;
}
function get_default_template_path()
{
$path = ROOT_DIR.DS.'templates'.DS.'default'.DS;
return $path;
}