I want to write a postProcess hook to post submitted form info to a Simple Machines Forum. How do I call SMF functions from Civi?
In a standalone test script, I could call the SMF createPost() function by first requiring some SMF files:
require_once('/path/to/forum/index.php'); require_once('/path/to/forum/Sources/Subs-Post.php'); createPost($msgOptions, $topicOptions, $posterOptions);
But I ran into php variable scope problems when trying to include index.php from my hooks plugin in the middle of Civi's chain of processing code. index.php include()'s Settings.php which defines important variables, but they don't wind up in the global scope, so when index.php then calls various other functions they don't have access to the variables.
I hacked around this based on something I found somewhere. I used this code within the postProcess hook to manually put all the variables defined in Settings.php into the global scope:
$__defined_vars = array_keys(get_defined_vars()); require_once('/path/to/forum/Settings.php'); $__newly_defined_vars = array_diff( array_keys(get_defined_vars()), $__defined_vars, array('__defined_vars') ); foreach ($__newly_defined_vars as $var) $GLOBALS[$var] = &$$var; $GLOBALS['smcFunc'] = array(); //also need to define this in GLOBALS require_once('/path/to/forum/index.php'); //this and next line needed to call SMF createPost function require_once('/path/to/forum/Sources/Subs-Post.php'); createPost($msgOptions, $topicOptions, $posterOptions);
I welcome other input on better ways to do this...but it works!