Projet

Général

Profil

Paste
Télécharger (4,7 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / rules / modules / php.eval.inc @ 950416da

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
/**
13
 * A class implementing a rules input evaluator processing PHP.
14
 */
15
class RulesPHPEvaluator extends RulesDataInputEvaluator {
16

    
17
  /**
18
   * Overrides RulesDataProcessor::editAccess().
19
   */
20
  public function editAccess() {
21
    return parent::editAccess() && (user_access('use PHP for settings') || drupal_is_cli());
22
  }
23

    
24
  /**
25
   * Helper function to find variables in PHP code.
26
   *
27
   * @param string $text
28
   *   The PHP code.
29
   * @param array $var_info
30
   *   Array with variable names as keys.
31
   */
32
  public static function getUsedVars($text, $var_info) {
33
    if (strpos($text, '<?') !== FALSE) {
34
      $used_vars = array();
35
      foreach ($var_info as $name => $info) {
36
        if (strpos($text, '$' . $name) !== FALSE) {
37
          $used_vars[] = $name;
38
        }
39
      }
40
      return $used_vars;
41
    }
42
  }
43

    
44
  /**
45
   * Overrides RulesDataInputEvaluator::prepare().
46
   */
47
  public function prepare($text, $var_info) {
48
    // A returned NULL skips the evaluator.
49
    $this->setting = self::getUsedVars($text, $var_info);
50
  }
51

    
52
  /**
53
   * Evaluates PHP code contained in $text.
54
   *
55
   * This method doesn't apply $options, thus the PHP code is responsible for
56
   * behaving appropriately.
57
   */
58
  public function evaluate($text, $options, RulesState $state) {
59
    $vars['eval_options'] = $options;
60
    foreach ($this->setting as $key => $var_name) {
61
      $vars[$var_name] = $state->get($var_name);
62
    }
63
    return rules_php_eval($text, rules_unwrap_data($vars));
64
  }
65

    
66
  /**
67
   * Overrides RulesDataInputEvaluator::help().
68
   */
69
  public static function help($var_info) {
70
    module_load_include('inc', 'rules', 'rules/modules/php.rules');
71

    
72
    $render = array(
73
      '#type' => 'fieldset',
74
      '#title' => t('PHP Evaluation'),
75
      '#collapsible' => TRUE,
76
      '#collapsed' => TRUE,
77
    ) + rules_php_evaluator_help($var_info);
78

    
79
    return $render;
80
  }
81

    
82
}
83

    
84
/**
85
 * A data processor using PHP.
86
 */
87
class RulesPHPDataProcessor extends RulesDataProcessor {
88

    
89
  /**
90
   * Overrides RulesDataProcessor::form().
91
   */
92
  protected static function form($settings, $var_info) {
93
    $settings += array('code' => '');
94
    $form = array(
95
      '#type' => 'fieldset',
96
      '#title' => t('PHP evaluation'),
97
      '#collapsible' => TRUE,
98
      '#collapsed' => empty($settings['code']),
99
      '#description' => t('Enter PHP code to process the selected argument value.'),
100
    );
101
    $form['code'] = array(
102
      '#type' => 'textarea',
103
      '#title' => t('Code'),
104
      '#description' => t('Enter PHP code without &lt;?php ?&gt; delimiters that returns the processed value. The selected value is available in the variable $value. Example: %code', array('%code' => 'return $value + 1;')),
105
      '#default_value' => $settings['code'],
106
      '#weight' => 5,
107
    );
108
    return $form;
109
  }
110

    
111
  /**
112
   * Overrides RulesDataProcessor::editAccess().
113
   */
114
  public function editAccess() {
115
    return parent::editAccess() && (user_access('use PHP for settings') || drupal_is_cli());
116
  }
117

    
118
  /**
119
   * Overrides RulesDataProcessor::process().
120
   */
121
  public function process($value, $info, RulesState $state, RulesPlugin $element) {
122
    $value = isset($this->processor) ? $this->processor->process($value, $info, $state, $element) : $value;
123
    return rules_php_eval_return($this->setting['code'], array('value' => $value));
124
  }
125

    
126
}
127

    
128
/**
129
 * Action and condition callback: Execute PHP code.
130
 */
131
function rules_execute_php_eval($code, $settings, $state, $element) {
132
  $data = array();
133
  if (!empty($settings['used_vars'])) {
134
    foreach ($settings['used_vars'] as $key => $var_name) {
135
      $data[$var_name] = $state->get($var_name);
136
    }
137
  }
138
  return rules_php_eval_return($code, rules_unwrap_data($data));
139
}
140

    
141
/**
142
 * Evaluates the given PHP code, with the given variables defined.
143
 *
144
 * @param string $code
145
 *   The PHP code to run, including <?php and ?>
146
 * @param array $arguments
147
 *   Array containing variables to be extracted to the code.
148
 *
149
 * @return
150
 *   The output of the php code.
151
 */
152
function rules_php_eval($code, $arguments = array()) {
153
  extract($arguments);
154

    
155
  ob_start();
156
  print eval('?>' . $code);
157
  $output = ob_get_contents();
158
  ob_end_clean();
159

    
160
  return $output;
161
}
162

    
163
/**
164
 * Evaluates the given PHP code, with the given variables defined.
165
 *
166
 * This is like rules_php_eval(), but does return the returned data from
167
 * the PHP code.
168
 *
169
 * @param string $code
170
 *   The PHP code to run, without <?php or ?>
171
 * @param array $arguments
172
 *   Array containing variables to be extracted to the code.
173
 *
174
 * @return
175
 *   The return value of the evaled code.
176
 */
177
function rules_php_eval_return($code, $arguments = array()) {
178
  extract($arguments);
179
  return eval($code);
180
}
181

    
182
/**
183
 * @}
184
 */