Skip to content

Commit a8231dc

Browse files
authored
Issue #1: check that functions exist (#6)
1 parent eb32385 commit a8231dc

6 files changed

+141
-51
lines changed

plugins/views/views_callback_handler_area.inc

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,17 @@ class views_callback_handler_area extends views_handler_area {
3939
// Execute output a custom callback.
4040
if ((!$empty || !empty($this->options['empty'])) && !empty($this->options['callback_output'])) {
4141
$function = $this->options['callback_output'];
42-
ob_start();
43-
echo $function($this->view, $this, $this->view->result);
44-
return ob_get_clean();
42+
if (function_exists($function)) {
43+
ob_start();
44+
echo $function($this->view, $this, $this->view->result);
45+
return ob_get_clean();
46+
}
47+
else {
48+
watchdog('views_callback', 'View: %view is calling undefined Area Output function %callback.', array(
49+
'%callback' => $function,
50+
'%view' => $this->view->name,
51+
), WATCHDOG_CRITICAL);
52+
}
4553
}
4654
return '';
4755
}

plugins/views/views_callback_handler_field.inc

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,17 @@ class views_callback_handler_field extends views_handler_field {
125125
// Execute static PHP code.
126126
if (!empty($this->options['use_callback_setup']) && !empty($this->options['callback_setup'])) {
127127
$function = $this->options['callback_setup'];
128-
ob_start();
129-
echo $function($this->view, $this, $this->callback_static_variable);
130-
ob_end_clean();
128+
if (function_exists($function)) {
129+
ob_start();
130+
$function($this->view, $this, $this->callback_static_variable);
131+
ob_end_clean();
132+
}
133+
else {
134+
watchdog('views_callback', 'View: %view is calling undefined Field Setup function %callback.', array(
135+
'%callback' => $function,
136+
'%view' => $this->view->name,
137+
), WATCHDOG_CRITICAL);
138+
}
131139
}
132140
}
133141

@@ -139,16 +147,24 @@ class views_callback_handler_field extends views_handler_field {
139147
// Execute value custom callback.
140148
if (!empty($this->options['use_callback_value']) && !empty($this->options['callback_value'])) {
141149
$function = $this->options['callback_value'];
142-
ob_start();
143-
foreach ($this->view->result as $i => &$row) {
144-
$normalized_row = new stdClass();
145-
foreach ($this->view->display_handler->get_handlers('field') as $field => $handler) {
146-
$normalized_row->$field = isset($row->{$handler->field_alias}) ? $row->{$handler->field_alias} : NULL;
150+
if (function_exists($function)) {
151+
ob_start();
152+
foreach ($this->view->result as $i => &$row) {
153+
$normalized_row = new stdClass();
154+
foreach ($this->view->display_handler->get_handlers('field') as $field => $handler) {
155+
$normalized_row->$field = isset($row->{$handler->field_alias}) ? $row->{$handler->field_alias} : NULL;
156+
}
157+
// Store the result, passed by reference to $this->view->result.
158+
$row->{$this->field_alias} = $function($this->view, $this, $this->callback_static_variable, $normalized_row, $row);
147159
}
148-
// Store the result, passed by reference to $this->view->result.
149-
$row->{$this->field_alias} = $function($this->view, $this, $this->callback_static_variable, $normalized_row, $row);
160+
ob_end_clean();
161+
}
162+
else {
163+
watchdog('views_callback', 'View: %view is calling undefined Field Value function %callback.', array(
164+
'%callback' => $function,
165+
'%view' => $this->view->name,
166+
), WATCHDOG_CRITICAL);
150167
}
151-
ob_end_clean();
152168
}
153169

154170
// If we're sorting, do the actual sorting then fix the results as per the pager info.
@@ -180,10 +196,18 @@ class views_callback_handler_field extends views_handler_field {
180196
*/
181197
public function callback_click_sort($row1, $row2) {
182198
$factor = strtoupper($this->callback_click_sort_order) == 'ASC' ? 1 : -1;
199+
$result = 0;
183200
$function = $this->callback_click_sort_function;
184201
if (is_bool($this->callback_click_sort_function)) {
185202
$function = $this->options['callback_click_sortable'];
186203
}
204+
if (!function_exists($function)) {
205+
watchdog('views_callback', 'View: %view is calling undefined Field Sort function %callback.', array(
206+
'%callback' => $function,
207+
'%view' => $this->view->name,
208+
), WATCHDOG_CRITICAL);
209+
return $factor * $result;
210+
}
187211

188212
if ($this->options['use_callback_click_sortable'] == self::CLICK_SORT_CALLBACK) {
189213
foreach (array('row1' => 'normalized_row1', 'row2' => 'normalized_row2') as $name => $normalized_name) {
@@ -194,7 +218,6 @@ class views_callback_handler_field extends views_handler_field {
194218
}
195219
ob_start();
196220
$result = (int) $function($this->view, $this, $this->callback_static_variable, $normalized_row1, $normalized_row2);
197-
echo $result;
198221
ob_end_clean();
199222
}
200223
else {
@@ -222,9 +245,17 @@ class views_callback_handler_field extends views_handler_field {
222245
}
223246

224247
$function = $this->options['callback_output'];
225-
ob_start();
226-
echo $function($this->view, $this, $this->callback_static_variable, $normalized_row, $values, isset($values->{$this->field_alias}) ? $values->{$this->field_alias} : NULL);
227-
$value = ob_get_clean();
248+
if (function_exists($function)) {
249+
ob_start();
250+
echo $function($this->view, $this, $this->callback_static_variable, $normalized_row, $values, isset($values->{$this->field_alias}) ? $values->{$this->field_alias} : NULL);
251+
$value = ob_get_clean();
252+
}
253+
else {
254+
watchdog('views_callback', 'View: %view is calling undefined Field Output function %callback.', array(
255+
'%callback' => $function,
256+
'%view' => $this->view->name,
257+
), WATCHDOG_CRITICAL);
258+
}
228259
}
229260
else {
230261
if (isset($values->{$this->field_alias})) {

plugins/views/views_callback_handler_filter.inc

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,17 @@ class views_callback_handler_filter extends views_handler_filter {
7373
// Execute static PHP code.
7474
if (!empty($this->options['use_callback_setup']) && !empty($this->options['callback_setup'])) {
7575
$function = $this->options['callback_setup'];
76-
ob_start();
77-
echo $function($this->view, $this, $this->callback_static_variable);
78-
ob_end_clean();
76+
if (function_exists($function)) {
77+
ob_start();
78+
$function($this->view, $this, $this->callback_static_variable);
79+
ob_end_clean();
80+
}
81+
else {
82+
watchdog('views_callback', 'View: %view is calling undefined Filter Setup function %callback.', array(
83+
'%callback' => $function,
84+
'%view' => $this->view->name,
85+
), WATCHDOG_CRITICAL);
86+
}
7987
}
8088
}
8189

@@ -87,18 +95,25 @@ class views_callback_handler_filter extends views_handler_filter {
8795
// Evaluate the callback function.
8896
if (!empty($this->options['callback_filter'])) {
8997
$function = $this->options['callback_filter'];
90-
ob_start();
91-
foreach ($this->view->result as $i => $row) {
92-
$normalized_row = new stdClass();
93-
foreach ($this->view->display_handler->get_handlers('field') as $field => $handler) {
94-
$normalized_row->$field = isset($row->{$handler->field_alias}) ? $row->{$handler->field_alias} : NULL;
95-
}
96-
if ($result = $function($this->view, $this, $this->callback_static_variable, $normalized_row, $row)) {
97-
echo $result;
98-
unset($this->view->result[$i]);
98+
if (function_exists($function)) {
99+
ob_start();
100+
foreach ($this->view->result as $i => $row) {
101+
$normalized_row = new stdClass();
102+
foreach ($this->view->display_handler->get_handlers('field') as $field => $handler) {
103+
$normalized_row->$field = isset($row->{$handler->field_alias}) ? $row->{$handler->field_alias} : NULL;
104+
}
105+
if ($function($this->view, $this, $this->callback_static_variable, $normalized_row, $row)) {
106+
unset($this->view->result[$i]);
107+
}
99108
}
109+
ob_end_clean();
110+
}
111+
else {
112+
watchdog('views_callback', 'View: %view is calling undefined Filter function %callback.', array(
113+
'%callback' => $function,
114+
'%view' => $this->view->name,
115+
), WATCHDOG_CRITICAL);
100116
}
101-
ob_end_clean();
102117
}
103118
}
104119

plugins/views/views_callback_handler_sort.inc

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,17 @@ class views_callback_handler_sort extends views_handler_sort {
6363
// Execute static PHP code.
6464
if (!empty($this->options['use_callback_setup']) && !empty($this->options['callback_setup'])) {
6565
$function = $this->options['callback_setup'];
66-
ob_start();
67-
echo $function($this->view, $this, $this->callback_static_variable);
68-
ob_end_clean();
66+
if (function_exists($function)) {
67+
ob_start();
68+
$function($this->view, $this, $this->callback_static_variable);
69+
ob_end_clean();
70+
}
71+
else {
72+
watchdog('views_callback', 'View: %view is calling undefined Sort Setup function %callback.', array(
73+
'%callback' => $function,
74+
'%view' => $this->view->name,
75+
), WATCHDOG_CRITICAL);
76+
}
6977
}
7078
}
7179

@@ -86,16 +94,25 @@ class views_callback_handler_sort extends views_handler_sort {
8694
*/
8795
public function callback_sort($row1, $row2) {
8896
$factor = strtoupper($this->options['order']) == 'ASC' ? 1 : -1;
97+
$result = 0;
8998

9099
$function = $this->options['callback_sort'];
91-
92-
foreach (array('row1' => 'normalized_row1', 'row2' => 'normalized_row2') as $name => $normalized_name) {
93-
$$normalized_name = new stdClass();
94-
foreach ($this->view->display_handler->get_handlers('field') as $field => $handler) {
95-
$$normalized_name->$field = isset($$name->{$handler->field_alias}) ? $$name->{$handler->field_alias} : NULL;
100+
if (function_exists($this->options['callback_sort'])) {
101+
foreach (array('row1' => 'normalized_row1', 'row2' => 'normalized_row2') as $name => $normalized_name) {
102+
$$normalized_name = new stdClass();
103+
foreach ($this->view->display_handler->get_handlers('field') as $field => $handler) {
104+
$$normalized_name->$field = isset($$name->{$handler->field_alias}) ? $$name->{$handler->field_alias} : NULL;
105+
}
96106
}
107+
$result = (int) $function($this->view, $this, $this->callback_static_variable, $normalized_row1, $normalized_row2);
108+
}
109+
else {
110+
watchdog('views_callback', 'View: %view is calling undefined Sort function %callback.', array(
111+
'%callback' => $function,
112+
'%view' => $this->view->name,
113+
), WATCHDOG_CRITICAL);
97114
}
98-
$result = (int) $function($this->view, $this, $this->callback_static_variable, $normalized_row1, $normalized_row2);
115+
99116
return $factor * $result;
100117
}
101118

plugins/views/views_callback_plugin_access.inc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class views_callback_plugin_access extends views_plugin_access {
5555
* Implements views_plugin_access#access().
5656
*/
5757
public function access($account) {
58-
if (!empty($this->options['callback_access'])) {
58+
if (!empty($this->options['callback_access']) && function_exists($this->options['callback_access'])) {
5959
return views_callback_check_access($this->options['callback_access'], $this->view->name, $this->display->id, $account);
6060
}
6161
return TRUE;
@@ -65,9 +65,15 @@ class views_callback_plugin_access extends views_plugin_access {
6565
* Implements views_plugin_access#get_access_callback().
6666
*/
6767
public function get_access_callback() {
68-
if (!empty($this->options['callback_access'])) {
68+
if (!empty($this->options['callback_access']) && function_exists($this->options['callback_access'])) {
6969
return array('views_callback_check_access', array($this->options['callback_access'], $this->view->name, $this->display->id));
7070
}
71+
else {
72+
watchdog('views_callback', 'View: %view is calling undefined Access Value function %callback.', array(
73+
'%callback' => $this->options['callback_access'],
74+
'%view' => $this->view->name,
75+
), WATCHDOG_CRITICAL);
76+
}
7177
return TRUE;
7278
}
7379

plugins/views/views_callback_plugin_cache.inc

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,17 @@ class views_callback_plugin_cache extends views_plugin_cache {
6969
$fresh = !empty($cache);
7070
if ($fresh && !empty($this->options['callback_cache_results'])) {
7171
$function = $this->options['callback_cache_results'];
72-
73-
ob_start();
74-
$fresh = $function($this->view, $this, $cache);
75-
echo $fresh;
76-
ob_end_clean();
72+
if (function_exists($function)) {
73+
ob_start();
74+
$fresh = $function($this->view, $this, $cache);
75+
ob_end_clean();
76+
}
77+
else {
78+
watchdog('views_callback', 'View: %view is calling undefined Cache Results function %callback.', array(
79+
'%callback' => $function,
80+
'%view' => $this->view->name,
81+
), WATCHDOG_CRITICAL);
82+
}
7783
}
7884
// Values to set: $view->result, $view->total_rows, $view->execute_time,
7985
// $view->current_page.
@@ -95,10 +101,17 @@ class views_callback_plugin_cache extends views_plugin_cache {
95101
$fresh = !empty($cache);
96102
if ($fresh && !empty($this->options['callback_cache_output'])) {
97103
$function = $this->options['callback_cache_output'];
98-
99-
ob_start();
100-
$fresh = $function($this->view, $this, $cache);
101-
ob_end_clean();
104+
if (function_exists($function)) {
105+
ob_start();
106+
$fresh = $function($this->view, $this, $cache);
107+
ob_end_clean();
108+
}
109+
else {
110+
watchdog('views_callback', 'View: %view is calling undefined Cache Output function %callback.', array(
111+
'%callback' => $function,
112+
'%view' => $this->view->name,
113+
), WATCHDOG_CRITICAL);
114+
}
102115
}
103116
if ($fresh) {
104117
// @code

0 commit comments

Comments
 (0)