DereferenceShortcodeVars

The Contact Form DB DereferenceShortcodeVars class.

Defined (1)

The class is defined in the following location(s).

/DereferenceShortcodeVars.php  
  1. class DereferenceShortcodeVars implements CFDBValueConverter { 
  2.  
  3. public function convert($varString) { 
  4. if ($varString == null) { 
  5. return $varString; 
  6. $retValue = $varString; // Default return 
  7.  
  8. if (is_user_logged_in()) { 
  9. // user is logged in 
  10. $current_user = wp_get_current_user(); // WP_User 
  11. $retValue = str_replace('$ID', $current_user->ID, $retValue); 
  12. //$retValue = str_replace('$id', @$current_user->id, $retValue); // deprecated 
  13. $retValue = str_replace('$first_name', $current_user->first_name, $retValue); 
  14. $retValue = str_replace('$last_name', $current_user->last_name, $retValue); 
  15. $retValue = str_replace('$user_login', $current_user->user_login, $retValue); 
  16. $retValue = str_replace('$user_nicename', $current_user->user_nicename, $retValue); 
  17. $retValue = str_replace('$user_email', $current_user->user_email, $retValue); 
  18. $retValue = str_replace('$user_firstname', $current_user->user_firstname, $retValue); 
  19. $retValue = str_replace('$user_lastname', $current_user->user_lastname, $retValue); 
  20.  
  21.  
  22. if (strpos($retValue, '$_POST') !== false) { 
  23. $matches = $this->getMatches('_POST', $retValue); 
  24. foreach ($matches as $aMatch) { 
  25. $paramName = $this->extractParamName('_POST', $aMatch); 
  26. $replace = ''; 
  27. if ($paramName != '' && isset($_POST[$paramName])) { 
  28. $replace = $_POST[$paramName]; 
  29. $retValue = str_replace($aMatch, $replace, $retValue); 
  30.  
  31. if (strpos($retValue, '$_GET') !== false) { 
  32. $matches = $this->getMatches('_GET', $retValue); 
  33. foreach ($matches as $aMatch) { 
  34. $paramName = $this->extractParamName('_GET', $aMatch); 
  35. $replace = ''; 
  36. if ($paramName != '' && isset($_GET[$paramName])) { 
  37. $replace = $_GET[$paramName]; 
  38. $retValue = str_replace($aMatch, $replace, $retValue); 
  39.  
  40. if (strpos($retValue, '$_COOKIE') !== false) { 
  41. $matches = $this->getMatches('_COOKIE', $retValue); 
  42. foreach ($matches as $aMatch) { 
  43. $paramName = $this->extractParamName('_COOKIE', $aMatch); 
  44. $replace = ''; 
  45. if ($paramName != '' && isset($_COOKIE[$paramName])) { 
  46. $replace = $_COOKIE[$paramName]; 
  47. $retValue = str_replace($aMatch, $replace, $retValue); 
  48.  
  49. return $retValue; 
  50.  
  51. /** 
  52. * Extract expressions from string. E.g. extact all _POST(xxx) from 'xxx=yy&&_POST(zzz)=someval&&_POST(aaa)=othervalue' 
  53. * @param $varName string = '_POST'|'_GET'|'_COOKIE' 
  54. * @param $fullExpressionString string like 'xxx=yy&&_POST(zzz)=someval&&_POST(aaa)=othervalue' 
  55. * @return array of string expressions like array ( '_POST(zzz)', '_POST(aaa)' ) 
  56. */ 
  57. public function &getMatches($varName, $fullExpressionString) { 
  58. $matches = array(); 
  59. preg_match_all('/\$'. $varName . '\s*\(\s*[\'"]?[^\'"\)]+[\'"]?\s*\)/', $fullExpressionString, $matches); 
  60. return $matches[0]; 
  61.  
  62. /** 
  63. * See if variable name in the form of $varName('ParamName') appears in the $varString 
  64. * (quotes optional or can be double-quotes) 
  65. * Intended to detect $varString is of the form $_POST['param-name'] given $varName = '_POST' 
  66. * @param $varName string name of the variable (without the "$") 
  67. * @param $varString string to search 
  68. * @return string inside the brackets and quotes or '' if there is no match or 
  69. */ 
  70. public function extractParamName($varName, $varString) 
  71. $singleQuotes = '/^\$%s\\s*\(\\s*\'([^\']*)\'\\s*\)$/'; 
  72. $doubleQuotes = '/^\$%s\\s*\(\\s*"([^"]*)"\\s*\)$/'; 
  73. $noQuotes = '/^\$%s\\s*\(([^\)]+)\)$/'; 
  74.  
  75. $templates = array($singleQuotes, $doubleQuotes, $noQuotes); 
  76. foreach ($templates as $template) { 
  77.  
  78. $matches = array(); 
  79. if (preg_match(sprintf($template, $varName), $varString, $matches)) { 
  80. //print_r($matches); // debug 
  81. if (count($matches) > 1) { 
  82. return $matches[1]; 
  83.  
  84. return '';