php.eval.inc 4.7 KB

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