I'm faced with a task to retrofit an existing application written to work within PHP 4.3.2 to work under PHP 4.1.2. The following sections fail to work under PHP 4.1.2 (description after code):
/**
* Perform mailing
*
* @access private
* @param object $result;
* @param mixed $emailTo
* @see file_get_contents
* @see file_put_contents
*/
function &mail($result, $emailTo) { // STATIC VOID METHOD
global $willUseEmailBundling, $senderEmailAddress, $isBogusSenderEmailAddress, $bundleEmailAddress, $bundleEmailFilePath, $bundleEmailFileName;
// NEW 6/29/2005: IF SOME ERROR OCCURRED SOMEWHERE THEN YOU MUST REOBTAIN THE VALUES WITHIN $preservedValuesArray
if (!$this->isSuccessful) $this->setPOSTEmailValues();
if ($this->isSuccessful) foreach ($_POST as $key => $val) if (!isset(${$key})) ${$key} = $val;
if ($this->isSuccessful && !$willUseEmailBundling) {
// SET $this->result FROM HERE, YOU WILL BE HANDLING EMAILING USING THIS RESULTSET OUTSIDE THE SCOPE OF THIS METHOD
$this->setResult($result);
} elseif ($this->isSuccessful) {
// REOBTAIN FOR $fileID
$fileID =& $this->getResource();
@fputs($fileID, $emailTo);
@fflush($fileID);
if (!file_get_contents("$bundleEmailFilePath/$bundleEmailFileName")) @file_put_contents("$bundleEmailFilePath/$bundleEmailFileName", $emailTo);
if (!file_get_contents("$bundleEmailFilePath/$bundleEmailFileName")) {
$this->isSuccessful = false;
$this->setErrorArray(array('action' => "Could not write onto bundling file \"$bundleEmailFilePath/$bundleEmailFileName\""));
}
if ($this->isSuccessful && !$this->unlock($fileID)) {
$this->isSuccessful = false;
$this->setErrorArray(array('action' => "Could not unlock resource for \"$bundleEmailFileName\""));
} else {
@fclose($fileID);
}
if ($this->isSuccessful) {
// NEW 6/29/2005: ENSURE THAT IF USING email_select() OPTION THAT SENDING TO CC AND/OR BCC IS DONE ONLY ONCE VIA BOOLEAN SESSION VAR
if (!$this->emailHandler->getHasSentCCBCC() && (${$section . '_sender_cc'} || ${$section . '_sender_bcc'})) {
$this->emailHandler->setHasSentCCBCC(true);
if (${$section . '_sender_cc'}) $cc = "\nCC:${$section . '_sender_cc'}";
if ((int)$phpVersionInt > 412 && ${$section . '_sender_bcc'}) $bcc = "\nBCC:${$section . '_sender_bcc'}";
// NEW 6/29/2005: PRESERVE CC AND BCC VALUES INTO INHERITED $preservedValuesArray IN CASE ERROR OCCURS SOMETIME LATER
$this->preserveDestroyPOSTEmailValues();
unset(${$section . '_email_cc'}, ${$section . '_email_bcc'});
}
$sender = (${$section . '_email_sender'}) ? ${$section . '_email_sender'} : $senderEmailAddress;
if ($isBogusSenderEmailAddress) ${$section . '_email_body'} = "\n*** DO NOT REPLY TO THIS ADDRESS***\n${$section . '_email_body'}";
/*-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
New 4/9/2005: You will be sending to $bundleEmailAddress, which will not be found within any of the $section database tables. You cannot
produce a meaningful $id which is required within the $this->email() method. You will have to handle mail internally via PHP mail() command
instead
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
mail($bundleEmailAddress, ${$section . '_email_subject'}, ${$section . '_email_body'}, "From:$sender$cc$bcc\r\n");
}
}
}
if (!function_exists('file_get_contents')) {
if (preg_match('/^5\.[1-9]+\.?[0-9]*$/i', trim(phpversion()))) { // file_get_contents FOR PHP VERSION 5.1.0+ IF FUNCTION UNAVAILABLE
/**
* Retrieve file contents into a string. This function exists only in PHP versions 4.3.0+
*
* @access public
* @param mixed $filePath
* @param boolean $willUseIncludePath (default false)
* @param resource $context (optional)
* @param int $offset (optional)
* @param int $maxLength (optional)
* @internal $context is available in PHP versions 5.0.0+. $offset and $maxLength is available in PHP versions 5.1.0+
* @link [url]http://us3.php.net/manual/en/function.file-get-contents.php[/url]
* @see link in PHP manual explaining file_get_contents
*/
function file_get_contents($filePath, $willUseIncludePath = false, $context = '', $offset = '', $maxLength = '') {
$fileID = @fopen($filePath, 'rb', $willUseIncludePath, $context);
if ($fileID && is_numeric($maxLength) && is_numeric($offset)) {
$contents = @fread($fileID, $maxLength - $offset);
} elseif ($fileID && is_numeric($maxLength)) {
$contents = @fread($fileID, $maxLength);
} elseif ($fileID) {
$contents = @fread($fileID, filesize($filePath));
}
if ($fileID) @fclose($fileID);
return $contents;
}
} elseif (preg_match('/^5\.0?\.?[0-9]*$/i', trim(phpversion()))) { // file_get_contents FOR PHP VERSION 5.0.0+ IF FUNCTION UNAVAILABLE
function file_get_contents($filePath, $willUseIncludePath = false, $context = '') {
$fileID = @fopen($filePath, 'rb', $willUseIncludePath, $context);
if ($fileID) {
$contents = @fread($fileID, filesize($filePath));
@fclose($fileID);
}
return $contents;
}
} else { // ALL OTHER VERSIONS OF file_get_contents
function file_get_contents($filePath, $willUseIncludePath = false) {
$fileID = @fopen($filePath, 'rb', $willUseIncludePath);
print_r($fileID);
if ($fileID) {
$contents = @fread($fileID, filesize($filePath));
@fclose($fileID);
}
print_r("contents = $contents<P>");
return $contents;
}
}
}
if (!function_exists('file_put_contents')) {
/**
* Place content into file. This function will exist in PHP 5.0+
*
* @access public
* @param mixed $filePath
* @param mixed $data
* @param int $flags (optional)
*/
function file_put_contents($filePath, $data, $flags = '') {
$flags = (FLAG_APPEND || 'a' || 'A' || 'a+' || 'A+') ? 'a+' : 'w+';
$fileID = fopen($filePath, $flags);
if (!$fileID) trigger_error("Unable to open \"$filePath\" for writing: ", E_USER_ERROR);
if ($fileID) {
@fputs($fileID, $data);
@fclose($fileID);
}
}
}
What is supposed to happen is that when someone submits an email to a select group of contacts, that an email bundler program set up to work within sendmail kicks in, reading the existing email addressed written into a flat file, email_bundle.txt.
However, in PHP 4.1.2 while it write onto email_bundle.txt, I keep getting error messages indicating that there is no content in email_bundle.txt, in spite of the fact that I can view the file using command line to verfiy that content is indeed found. This does not occur in PHP 4.3.2+.
Based on what you see so far, what should or could I do differently?
Thanks
Phil