1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Contains rules integration for the php module needed during evaluation.
|
6
|
*
|
7
|
* @addtogroup rules
|
8
|
* @{
|
9
|
*/
|
10
|
|
11
|
/**
|
12
|
* A class implementing a rules input evaluator processing PHP.
|
13
|
*/
|
14
|
class RulesPHPEvaluator extends RulesDataInputEvaluator {
|
15
|
|
16
|
public static function access() {
|
17
|
return user_access('use PHP for settings');
|
18
|
}
|
19
|
|
20
|
public static function getUsedVars($text, $var_info) {
|
21
|
if (strpos($text, '<?') !== FALSE) {
|
22
|
$used_vars = array();
|
23
|
foreach ($var_info as $name => $info) {
|
24
|
if (strpos($text, '$' . $name) !== FALSE) {
|
25
|
$used_vars[] = $name;
|
26
|
}
|
27
|
}
|
28
|
return $used_vars;
|
29
|
}
|
30
|
}
|
31
|
|
32
|
public function prepare($text, $var_info) {
|
33
|
// A returned NULL skips the evaluator.
|
34
|
$this->setting = self::getUsedVars($text, $var_info);
|
35
|
}
|
36
|
|
37
|
/**
|
38
|
* Evaluates PHP code contained in $text. This doesn't apply $options, thus
|
39
|
* the PHP code is responsible for behaving appropriately.
|
40
|
*/
|
41
|
public function evaluate($text, $options, RulesState $state) {
|
42
|
$vars['eval_options'] = $options;
|
43
|
foreach ($this->setting as $key => $var_name) {
|
44
|
$vars[$var_name] = $state->get($var_name);
|
45
|
}
|
46
|
return rules_php_eval($text, rules_unwrap_data($vars));
|
47
|
}
|
48
|
|
49
|
public static function help($var_info) {
|
50
|
module_load_include('inc', 'rules', 'rules/modules/php.rules');
|
51
|
|
52
|
$render = array(
|
53
|
'#type' => 'fieldset',
|
54
|
'#title' => t('PHP Evaluation'),
|
55
|
'#collapsible' => TRUE,
|
56
|
'#collapsed' => TRUE,
|
57
|
) + rules_php_evaluator_help($var_info);
|
58
|
|
59
|
return $render;
|
60
|
}
|
61
|
}
|
62
|
|
63
|
/**
|
64
|
* A data processor using PHP.
|
65
|
*/
|
66
|
class RulesPHPDataProcessor extends RulesDataProcessor {
|
67
|
|
68
|
protected static function form($settings, $var_info) {
|
69
|
$settings += array('code' => '');
|
70
|
$form = array(
|
71
|
'#type' => 'fieldset',
|
72
|
'#title' => t('PHP evaluation'),
|
73
|
'#collapsible' => TRUE,
|
74
|
'#collapsed' => empty($settings['code']),
|
75
|
'#description' => t('Enter PHP code to process the selected argument value.'),
|
76
|
);
|
77
|
$form['code'] = array(
|
78
|
'#type' => 'textarea',
|
79
|
'#title' => t('Code'),
|
80
|
'#description' => t('Enter PHP code without <?php ?> delimiters that returns the processed value. The selected value is available in the variable $value. Example: %code', array('%code' => 'return $value + 1;')),
|
81
|
'#default_value' => $settings['code'],
|
82
|
'#weight' => 5,
|
83
|
);
|
84
|
return $form;
|
85
|
}
|
86
|
|
87
|
public static function access() {
|
88
|
return user_access('use PHP for settings');
|
89
|
}
|
90
|
|
91
|
public function process($value, $info, RulesState $state, RulesPlugin $element) {
|
92
|
$value = isset($this->processor) ? $this->processor->process($value, $info, $state, $element) : $value;
|
93
|
return rules_php_eval_return($this->setting['code'], array('value' => $value));
|
94
|
}
|
95
|
}
|
96
|
|
97
|
/**
|
98
|
* Action and condition callback: Execute PHP code.
|
99
|
*/
|
100
|
function rules_execute_php_eval($code, $settings, $state, $element) {
|
101
|
$data = array();
|
102
|
if (!empty($settings['used_vars'])) {
|
103
|
foreach ($settings['used_vars'] as $key => $var_name) {
|
104
|
$data[$var_name] = $state->get($var_name);
|
105
|
}
|
106
|
}
|
107
|
return rules_php_eval_return($code, rules_unwrap_data($data));
|
108
|
}
|
109
|
|
110
|
/**
|
111
|
* Evalutes the given PHP code, with the given variables defined.
|
112
|
*
|
113
|
* @param $code
|
114
|
* The PHP code to run, with <?php ?>
|
115
|
* @param $arguments
|
116
|
* Array containing variables to be extracted to the code.
|
117
|
*
|
118
|
* @return
|
119
|
* The output of the php code.
|
120
|
*/
|
121
|
function rules_php_eval($code, $arguments = array()) {
|
122
|
extract($arguments);
|
123
|
|
124
|
ob_start();
|
125
|
print eval('?>'. $code);
|
126
|
$output = ob_get_contents();
|
127
|
ob_end_clean();
|
128
|
|
129
|
return $output;
|
130
|
}
|
131
|
|
132
|
/**
|
133
|
* Evalutes the given PHP code, with the given variables defined. This is like
|
134
|
* rules_php_eval() but does return the returned data from the PHP code.
|
135
|
*
|
136
|
* @param $code
|
137
|
* The PHP code to run, without <?php ?>
|
138
|
* @param $arguments
|
139
|
* Array containing variables to be extracted to the code.
|
140
|
*
|
141
|
* @return
|
142
|
* The return value of the evaled code.
|
143
|
*/
|
144
|
function rules_php_eval_return($code, $arguments = array()) {
|
145
|
extract($arguments);
|
146
|
return eval($code);
|
147
|
}
|
148
|
|
149
|
/**
|
150
|
* @}
|
151
|
*/
|