Issues (1027)

Sources/Subs-List.php (5 issues)

1
<?php
2
3
/**
4
 * This file contains a standard way of displaying lists for SMF.
5
 * Simple Machines Forum (SMF)
6
 *
7
 * @package SMF
8
 * @author Simple Machines http://www.simplemachines.org
9
 * @copyright 2019 Simple Machines and individual contributors
10
 * @license http://www.simplemachines.org/about/smf/license.php BSD
11
 *
12
 * @version 2.1 RC2
13
 */
14
15
if (!defined('SMF'))
16
	die('No direct access...');
17
18
/**
19
 * Create a new list
20
 *
21
 * @param array $listOptions An array of options for the list - 'id', 'columns', 'items_per_page', 'get_count', etc.
22
 */
23
function createList($listOptions)
24
{
25
	global $context, $smcFunc;
26
27
	assert(isset($listOptions['id']));
28
	assert(isset($listOptions['columns']));
29
	assert(is_array($listOptions['columns']));
30
	assert((empty($listOptions['items_per_page']) || (isset($listOptions['get_count']['function'], $listOptions['base_href']) && is_numeric($listOptions['items_per_page']))));
31
	assert((empty($listOptions['default_sort_col']) || isset($listOptions['columns'][$listOptions['default_sort_col']])));
32
	assert((!isset($listOptions['form']) || isset($listOptions['form']['href'])));
33
34
	call_integration_hook('integrate_' . $listOptions['id'], array(&$listOptions));
35
36
	// All the context data will be easily accessible by using a reference.
37
	$context[$listOptions['id']] = array();
38
	$list_context = &$context[$listOptions['id']];
39
40
	// Figure out the sort.
41
	if (empty($listOptions['default_sort_col']))
42
	{
43
		$list_context['sort'] = array();
44
		$sort = '1=1';
45
	}
46
	else
47
	{
48
		$request_var_sort = isset($listOptions['request_vars']['sort']) ? $listOptions['request_vars']['sort'] : 'sort';
49
		$request_var_desc = isset($listOptions['request_vars']['desc']) ? $listOptions['request_vars']['desc'] : 'desc';
50
		if (isset($_REQUEST[$request_var_sort], $listOptions['columns'][$_REQUEST[$request_var_sort]], $listOptions['columns'][$_REQUEST[$request_var_sort]]['sort']))
51
			$list_context['sort'] = array(
52
				'id' => $_REQUEST[$request_var_sort],
53
				'desc' => isset($_REQUEST[$request_var_desc]) && isset($listOptions['columns'][$_REQUEST[$request_var_sort]]['sort']['reverse']),
54
			);
55
		else
56
			$list_context['sort'] = array(
57
				'id' => $listOptions['default_sort_col'],
58
				'desc' => (!empty($listOptions['default_sort_dir']) && $listOptions['default_sort_dir'] == 'desc') || (!empty($listOptions['columns'][$listOptions['default_sort_col']]['sort']['default']) && substr($listOptions['columns'][$listOptions['default_sort_col']]['sort']['default'], -4, 4) == 'desc') ? true : false,
59
			);
60
61
		// Set the database column sort.
62
		$sort = $listOptions['columns'][$list_context['sort']['id']]['sort'][$list_context['sort']['desc'] ? 'reverse' : 'default'];
63
	}
64
65
	$list_context['start_var_name'] = isset($listOptions['start_var_name']) ? $listOptions['start_var_name'] : 'start';
66
	// In some cases the full list must be shown, regardless of the amount of items.
67
	if (empty($listOptions['items_per_page']))
68
	{
69
		$list_context['start'] = 0;
70
		$list_context['items_per_page'] = 0;
71
	}
72
	// With items per page set, calculate total number of items and page index.
73
	else
74
	{
75
		// First get an impression of how many items to expect.
76
		if (isset($listOptions['get_count']['file']))
77
			require_once($listOptions['get_count']['file']);
78
79
		$call = call_helper($listOptions['get_count']['function'], true);
80
		$list_context['total_num_items'] = call_user_func_array($call, empty($listOptions['get_count']['params']) ? array() : $listOptions['get_count']['params']);
0 ignored issues
show
It seems like $call can also be of type boolean; however, parameter $function of call_user_func_array() does only seem to accept callable, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

80
		$list_context['total_num_items'] = call_user_func_array(/** @scrutinizer ignore-type */ $call, empty($listOptions['get_count']['params']) ? array() : $listOptions['get_count']['params']);
Loading history...
81
82
		// Default the start to the beginning...sounds logical.
83
		$list_context['start'] = isset($_REQUEST[$list_context['start_var_name']]) ? (int) $_REQUEST[$list_context['start_var_name']] : 0;
84
		$list_context['items_per_page'] = $listOptions['items_per_page'];
85
86
		// Then create a page index.
87
		if ($list_context['total_num_items'] > $list_context['items_per_page'])
88
			$list_context['page_index'] = constructPageIndex($listOptions['base_href'] . (empty($list_context['sort']) ? '' : ';' . $request_var_sort . '=' . $list_context['sort']['id'] . ($list_context['sort']['desc'] ? ';' . $request_var_desc : '')) . ($list_context['start_var_name'] != 'start' ? ';' . $list_context['start_var_name'] . '=%1$d' : ''), $list_context['start'], $list_context['total_num_items'], $list_context['items_per_page'], $list_context['start_var_name'] != 'start');
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $request_var_sort does not seem to be defined for all execution paths leading up to this point.
Loading history...
Comprehensibility Best Practice introduced by
The variable $request_var_desc does not seem to be defined for all execution paths leading up to this point.
Loading history...
89
	}
90
91
	// Prepare the headers of the table.
92
	$list_context['headers'] = array();
93
	foreach ($listOptions['columns'] as $column_id => $column)
94
		$list_context['headers'][] = array(
95
			'id' => $column_id,
96
			'label' => isset($column['header']['eval']) ? eval($column['header']['eval']) : (isset($column['header']['value']) ? $column['header']['value'] : ''),
0 ignored issues
show
The use of eval() is discouraged.
Loading history...
97
			'href' => empty($listOptions['default_sort_col']) || empty($column['sort']) ? '' : $listOptions['base_href'] . ';' . $request_var_sort . '=' . $column_id . ($column_id === $list_context['sort']['id'] && !$list_context['sort']['desc'] && isset($column['sort']['reverse']) ? ';' . $request_var_desc : '') . (empty($list_context['start']) ? '' : ';' . $list_context['start_var_name'] . '=' . $list_context['start']),
98
			'sort_image' => empty($listOptions['default_sort_col']) || empty($column['sort']) || $column_id !== $list_context['sort']['id'] ? null : ($list_context['sort']['desc'] ? 'down' : 'up'),
99
			'class' => isset($column['header']['class']) ? $column['header']['class'] : '',
100
			'style' => isset($column['header']['style']) ? $column['header']['style'] : '',
101
			'colspan' => isset($column['header']['colspan']) ? $column['header']['colspan'] : '',
102
		);
103
104
	// We know the amount of columns, might be useful for the template.
105
	$list_context['num_columns'] = count($listOptions['columns']);
106
	$list_context['width'] = isset($listOptions['width']) ? $listOptions['width'] : '0';
107
108
	// Get the file with the function for the item list.
109
	if (isset($listOptions['get_items']['file']))
110
		require_once($listOptions['get_items']['file']);
111
112
	// Call the function and include which items we want and in what order.
113
	$call = call_helper($listOptions['get_items']['function'], true);
114
	$list_items = call_user_func_array($call, array_merge(array($list_context['start'], $list_context['items_per_page'], $sort), empty($listOptions['get_items']['params']) ? array() : $listOptions['get_items']['params']));
115
	$list_items = empty($list_items) ? array() : $list_items;
116
117
	// Loop through the list items to be shown and construct the data values.
118
	$list_context['rows'] = array();
119
	foreach ($list_items as $item_id => $list_item)
120
	{
121
		$cur_row = array();
122
		foreach ($listOptions['columns'] as $column_id => $column)
123
		{
124
			$cur_data = array();
125
126
			// A value straight from the database?
127
			if (isset($column['data']['db']))
128
				$cur_data['value'] = $list_item[$column['data']['db']];
129
130
			// Take the value from the database and make it HTML safe.
131
			elseif (isset($column['data']['db_htmlsafe']))
132
				$cur_data['value'] = $smcFunc['htmlspecialchars']($list_item[$column['data']['db_htmlsafe']]);
133
134
			// Using sprintf is probably the most readable way of injecting data.
135
			elseif (isset($column['data']['sprintf']))
136
			{
137
				$params = array();
138
				foreach ($column['data']['sprintf']['params'] as $sprintf_param => $htmlsafe)
139
					$params[] = $htmlsafe ? $smcFunc['htmlspecialchars']($list_item[$sprintf_param]) : $list_item[$sprintf_param];
140
				$cur_data['value'] = vsprintf($column['data']['sprintf']['format'], $params);
141
			}
142
143
			// The most flexible way probably is applying a custom function.
144
			elseif (isset($column['data']['function']))
145
				$cur_data['value'] = call_user_func_array($column['data']['function'], array($list_item));
146
147
			// A modified value (inject the database values).
148
			elseif (isset($column['data']['eval']))
149
				$cur_data['value'] = eval(preg_replace('~%([a-zA-Z0-9\-_]+)%~', '$list_item[\'$1\']', $column['data']['eval']));
0 ignored issues
show
The use of eval() is discouraged.
Loading history...
150
151
			// A literal value.
152
			elseif (isset($column['data']['value']))
153
				$cur_data['value'] = $column['data']['value'];
154
155
			// Empty value.
156
			else
157
				$cur_data['value'] = '';
158
159
			// Allow for basic formatting.
160
			if (!empty($column['data']['comma_format']))
161
				$cur_data['value'] = comma_format($cur_data['value']);
162
			elseif (!empty($column['data']['timeformat']))
163
				$cur_data['value'] = timeformat($cur_data['value']);
164
165
			// Set a style class for this column?
166
			if (isset($column['data']['class']))
167
				$cur_data['class'] = $column['data']['class'];
168
169
			// Fully customized styling for the cells in this column only.
170
			if (isset($column['data']['style']))
171
				$cur_data['style'] = $column['data']['style'];
172
173
			// Add the data cell properties to the current row.
174
			$cur_row[$column_id] = $cur_data;
175
		}
176
177
		// Maybe we wat set a custom class for the row based on the data in the row itself
178
		if (isset($listOptions['data_check']))
179
		{
180
			if (isset($listOptions['data_check']['class']))
181
				$list_context['rows'][$item_id]['class'] = $listOptions['data_check']['class']($list_item);
182
			if (isset($listOptions['data_check']['style']))
183
				$list_context['rows'][$item_id]['style'] = $listOptions['data_check']['style']($list_item);
184
		}
185
186
		// Insert the row into the list.
187
		$list_context['rows'][$item_id]['data'] = $cur_row;
188
	}
189
190
	// The title is currently optional.
191
	if (isset($listOptions['title']))
192
		$list_context['title'] = $listOptions['title'];
193
194
	// In case there's a form, share it with the template context.
195
	if (isset($listOptions['form']))
196
	{
197
		$list_context['form'] = $listOptions['form'];
198
199
		if (!isset($list_context['form']['hidden_fields']))
200
			$list_context['form']['hidden_fields'] = array();
201
202
		// Always add a session check field.
203
		$list_context['form']['hidden_fields'][$context['session_var']] = $context['session_id'];
204
205
		// Will this do a token check?
206
		if (isset($listOptions['form']['token']))
207
			$list_context['form']['hidden_fields'][$context[$listOptions['form']['token'] . '_token_var']] = $context[$listOptions['form']['token'] . '_token'];
208
209
		// Include the starting page as hidden field?
210
		if (!empty($list_context['form']['include_start']) && !empty($list_context['start']))
211
			$list_context['form']['hidden_fields'][$list_context['start_var_name']] = $list_context['start'];
212
213
		// If sorting needs to be the same after submitting, add the parameter.
214
		if (!empty($list_context['form']['include_sort']) && !empty($list_context['sort']))
215
		{
216
			$list_context['form']['hidden_fields']['sort'] = $list_context['sort']['id'];
217
			if ($list_context['sort']['desc'])
218
				$list_context['form']['hidden_fields']['desc'] = 1;
219
		}
220
	}
221
222
	// Wanna say something nice in case there are no items?
223
	if (isset($listOptions['no_items_label']))
224
	{
225
		$list_context['no_items_label'] = $listOptions['no_items_label'];
226
		$list_context['no_items_align'] = isset($listOptions['no_items_align']) ? $listOptions['no_items_align'] : '';
227
	}
228
229
	// A list can sometimes need a few extra rows above and below.
230
	if (isset($listOptions['additional_rows']))
231
	{
232
		$list_context['additional_rows'] = array();
233
		foreach ($listOptions['additional_rows'] as $row)
234
		{
235
			if (empty($row))
236
				continue;
237
238
			// Supported row positions: top_of_list, after_title,
239
			// above_column_headers, below_table_data, bottom_of_list.
240
			if (!isset($list_context['additional_rows'][$row['position']]))
241
				$list_context['additional_rows'][$row['position']] = array();
242
			$list_context['additional_rows'][$row['position']][] = $row;
243
		}
244
	}
245
246
	// Add an option for inline JavaScript.
247
	if (isset($listOptions['javascript']))
248
		$list_context['javascript'] = $listOptions['javascript'];
249
250
	// We want a menu.
251
	if (isset($listOptions['list_menu']))
252
		$list_context['list_menu'] = $listOptions['list_menu'];
253
254
	// Make sure the template is loaded.
255
	loadTemplate('GenericList');
256
}
257
258
?>