Re: Mail_Mbox 0.1.3
| From: | Jon Parise | Date: | Sat, 28 Dec 2002 22:25:38 +0000 |
| Subject: | Re: Mail_Mbox 0.1.3 | ||
| References: | 1 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-11898@lists.php.net to get a copy of this message | ||
On Sat, Dec 28, 2002 at 08:00:56PM -0300, Roberto Bert wrote:
> I put MBox online at PEAR:
>
> http://pear.php.net/package-info.php?pacid=150
>
> You can download the latest release and take a look on it.
I suggest the attach set of stylistic improvements.
--
Jon Parise (jon@php.net) :: The PHP Project (http://www.php.net/)
--- mbox.php.orig Sat Dec 28 17:30:24 2002 +++ mbox.php Sat Dec 28 17:29:06 2002 @@ -99,7 +99,7 @@ * @var bool * @access public */ - var $debug = false; + var $debug = false; /** * Open a Mbox @@ -119,22 +119,22 @@ if (!file_exists($file)) { return PEAR::raiseError("Cannot open the mbox file: file doesnt exists."); } - + // getting next resource it to set - $resourceId = sizeof($this->_resources) + 1; - + $resourceId = sizeof($this->_resources) + 1; + // setting filename to the resource id - $this->_resources[$resourceId]["filename"] = $file; + $this->_resources[$resourceId]["filename"] = $file; - // oppening the file - $this->_resources[$resourceId]["fresource"] = fopen($file, "r"); + // opening the file + $this->_resources[$resourceId]["fresource"] = fopen($file, "r"); if (!is_resource($this->_resources[$resourceId]["fresource"])) { return PEAR::raiseError("Cannot open the mbox file: maybe without permission."); } - // process the file and get the messages bytes offsets + // process the file and get the messages bytes offsets $this->_process($resourceId); - + return $resourceId; } @@ -152,12 +152,12 @@ if (!is_resource($this->_resources[$resourceId]["fresource"])) { return PEAR::raiseError("Cannot close the mbox file because it wanst open."); } - - if (fclose($this->_resources[$resourceId]["fresource"]) == true) { - return true; - } else { + + if (!fclose($this->_resources[$resourceId]["fresource"])) { return PEAR::raiseError("Cannot close the mbox, maybe file is being used (?)"); } + + return true; } /** @@ -166,7 +166,8 @@ * Get Mbox Number of Messages * * @param int $resourceId Mbox resouce id created by open - * @return int Number of messages on Mbox (starting on 1, 0 if no message exists) + * @return int Number of messages on Mbox (starting on 1, + * 0 if no message exists) * @access public */ function size($resourceId) @@ -192,24 +193,26 @@ if (!is_array($this->_resources[$resourceId]["messages"][$message])) { return PEAR::raiseError("Message doesnt exists."); } - + // getting bytes locations - $bytesStart = $this->_resources[$resourceId]["messages"][$message][0]; - $bytesEnd = $this->_resources[$resourceId]["messages"][$message][1]; - + $bytesStart = $this->_resources[$resourceId]["messages"][$message][0]; + $bytesEnd = $this->_resources[$resourceId]["messages"][$message][1]; + // a debug feature to show the bytes locations - if ($this->debug == true) { + if ($this->debug) { printf("%08d=%08d<br />", $bytesStart, $bytesEnd); } - + // seek to start of message if (@fseek($this->_resources[$resourceId]["fresource"], $bytesStart) == -1) { return PEAR::raiseError("Cannot read message bytes"); } - + if ($bytesEnd - $bytesStart > 0) { // reading and returning message (bytes to read = difference of bytes locations) - return fread($this->_resources[$resourceId]["fresource"], $bytesEnd - $bytesStart) . "\n"; + $msg = fread($this->_resources[$resourceId]["fresource"], + $bytesEnd - $bytesStart) . "\n"; + return $msg; } } @@ -233,21 +236,21 @@ } // creating temp file - $ftempname = tempnam ("/tmp", rand(0, 9)); - $ftemp = fopen($ftempname, "w"); + $ftempname = tempnam ("/tmp", rand(0, 9)); + $ftemp = fopen($ftempname, "w"); if ($ftemp == false) { return PEAR::raiseError("Cannot create a temp file. Cannot handle this error."); } - + // writting only undeleted messages $messages = $this->size($resourceId); - - for ($x = 0; $x < $messages; $x++) { + + for ($x = 0; $x < $messages; $x++) { if ($x == $message) { continue; } - $messageThis = $this->get($resourceId, $x); + $messageThis = $this->get($resourceId, $x); if (is_string($messageThis)) { fwrite($ftemp, $messageThis, strlen($messageThis)); } @@ -264,7 +267,7 @@ /** * Update a message * - * Note: PEAR::Mail_Mbox auto adds \n\n at end of the message + * Note: Mail_Mbox auto adds \n\n at end of the message * * Note: messages start with 0. * @@ -283,7 +286,7 @@ // creating temp file $ftempname = tempnam ("/tmp", rand(0, 9)); - $ftemp = fopen($ftempname, "w"); + $ftemp = fopen($ftempname, "w"); if ($ftemp == false) { return PEAR::raiseError("Cannot create a temp file. Cannot handle this error."); } @@ -291,11 +294,11 @@ // writting only undeleted messages $messages = $this->size($resourceId); - for ($x = 0; $x < $messages; $x++) { + for ($x = 0; $x < $messages; $x++) { if ($x == $message) { - $messageThis = $content . "\n\n"; + $messageThis = $content . "\n\n"; } else { - $messageThis = $this->get($resourceId, $x); + $messageThis = $this->get($resourceId, $x); } if (is_string($messageThis)) { @@ -330,14 +333,13 @@ function insert($resourceId, $content, $offset = NULL) { // checking if we have bytes locations for this message - if (!is_array($this->_resources[$resourceId])) { return PEAR::raiseError("ResourceId doesnt exists."); } // creating temp file $ftempname = tempnam ("/tmp", rand(0, 9)); - $ftemp = fopen($ftempname, "w"); + $ftemp = fopen($ftempname, "w"); if ($ftemp == false) { return PEAR::raiseError("Cannot create a temp file. Cannot handle this error."); } @@ -353,15 +355,15 @@ if ($offset !== NULL && $x == $offset) { fwrite($ftemp, $content, strlen($content)); } - $messageThis = $this->get($resourceId, $x); + $messageThis = $this->get($resourceId, $x); if (is_string($messageThis)) { fwrite($ftemp, $messageThis, strlen($messageThis)); } } } - if ($offset === NULL) - { + + if ($offset === NULL) { fwrite($ftemp, $content, strlen($content)); } @@ -373,8 +375,6 @@ return $this->_move($resourceId, $ftempname, $filename); } - - /** * Copy a file to another * @@ -388,7 +388,7 @@ function _move($resourceId, $ftempname, $filename) { // opening ftemp to read - $ftemp = fopen($ftempname, "r"); + $ftemp = fopen($ftempname, "r"); if ($ftemp == false) { return PEAR::raiseError("Cannot open temp file."); @@ -400,23 +400,22 @@ return PEAR::raiseError("Cannot write on mbox file."); } - while (feof($ftemp) != true) - { + while (feof($ftemp) != true) { $strings = fread($ftemp, 4096); if (!fwrite($fp, $strings, strlen($strings))) { return PEAR::raiseError("Cannot write to file."); } } + fclose($fp); - fclose($ftemp); - unlink($ftempname); - + // open another resource and substitute it to the old one $mid = $this->open($filename); $this->_resources[$resourceId] = $this->_resources[$mid]; unset($this->_resources[$mid]); + return true; } @@ -433,7 +432,6 @@ function _process($resourceId) { // sanit check - if (!is_resource($this->_resources[$resourceId]["fresource"])) { return PEAR::raiseError("Resource isn't valid."); } @@ -444,49 +442,48 @@ } // starting values - $bytes = 0; + $bytes = 0; + $lines = 0; + unset($lineThis); unset($lineLast); - $lines = 0; while (feof($this->_resources[$resourceId]["fresource"]) != true) { // getting char by char - $c = fgetc($this->_resources[$resourceId]["fresource"]); - $lineThis .= $c; + $c = fgetc($this->_resources[$resourceId]["fresource"]); + $lineThis .= $c; // each \n we will check things if ($c === "\n") { // checking if start with From if (substr($lineThis, 0, 5) === "From ") { // this line byte count is last line more 1 byte - $bytesStart = $bytesEnd + 1; + $bytesStart = $bytesEnd + 1; // last line byte count is this line bytes minus this line length $bytesEnd = $bytes - strlen($lineThis); // we will check messages after they end if ($bytesStart != 1) { - if ($this->debug == true) { - printf("#################### from byte %08d to byte %08d ################### <br />", $bytesStart, $bytesEnd); - } - // setting new message points - $messagesCount = $this->size($resourceId); - $this->_resources[$resourceId]["messages"][$messagesCount][0] = $bytesStart; - $this->_resources[$resourceId]["messages"][$messagesCount][1] = $bytesEnd; - } - - } - + if ($this->debug) { + printf("#################### from byte %08d to byte %08d ################### <br />", $bytesStart, $bytesEnd); + } + // setting new message points + $messagesCount = $this->size($resourceId); + $this->_resources[$resourceId]["messages"][$messagesCount][0] = $bytesStart; + $this->_resources[$resourceId]["messages"][$messagesCount][1] = $bytesEnd; + } + } - // increasing number of lines (doesnt matter) - if ($this->debug == true) { + // increasing number of lines (doesn't matter) + if ($this->debug) { $lines++; } - + // last line is this line - $lineLast = $lineThis; + $lineLast = $lineThis; // this line is blank now unset($lineThis); - if ($this->debug == true) { + if ($this->debug) { printf("%08d:%08d %s<br/>", $lines, $bytes, $lineLast); } } @@ -499,11 +496,10 @@ // last line byte count is this line bytes minus this line length $bytesEnd = $bytes - strlen($lineThis) - 2; // we will check messages after they end - - $messagesCount = $this->size($resourceId); - $this->_resources[$resourceId]["messages"][$messagesCount][0] = $bytesStart; - $this->_resources[$resourceId]["messages"][$messagesCount][1] = $bytesEnd; - + + $messagesCount = $this->size($resourceId); + $this->_resources[$resourceId]["messages"][$messagesCount][0] = $bytesStart; + $this->_resources[$resourceId]["messages"][$messagesCount][1] = $bytesEnd; } }
--- mbox.php.orig Sat Dec 28 17:30:24 2002 +++ mbox.php Sat Dec 28 17:29:06 2002 @@ -99,7 +99,7 @@ * @var bool * @access public */ - var $debug = false; + var $debug = false; /** * Open a Mbox @@ -119,22 +119,22 @@ if (!file_exists($file)) { return PEAR::raiseError("Cannot open the mbox file: file doesnt exists."); } - + // getting next resource it to set - $resourceId = sizeof($this->_resources) + 1; - + $resourceId = sizeof($this->_resources) + 1; + // setting filename to the resource id - $this->_resources[$resourceId]["filename"] = $file; + $this->_resources[$resourceId]["filename"] = $file; - // oppening the file - $this->_resources[$resourceId]["fresource"] = fopen($file, "r"); + // opening the file + $this->_resources[$resourceId]["fresource"] = fopen($file, "r"); if (!is_resource($this->_resources[$resourceId]["fresource"])) { return PEAR::raiseError("Cannot open the mbox file: maybe without permission."); } - // process the file and get the messages bytes offsets + // process the file and get the messages bytes offsets $this->_process($resourceId); - + return $resourceId; } @@ -152,12 +152,12 @@ if (!is_resource($this->_resources[$resourceId]["fresource"])) { return PEAR::raiseError("Cannot close the mbox file because it wanst open."); } - - if (fclose($this->_resources[$resourceId]["fresource"]) == true) { - return true; - } else { + + if (!fclose($this->_resources[$resourceId]["fresource"])) { return PEAR::raiseError("Cannot close the mbox, maybe file is being used (?)"); } + + return true; } /** @@ -166,7 +166,8 @@ * Get Mbox Number of Messages * * @param int $resourceId Mbox resouce id created by open - * @return int Number of messages on Mbox (starting on 1, 0 if no message exists) + * @return int Number of messages on Mbox (starting on 1, + * 0 if no message exists) * @access public */ function size($resourceId) @@ -192,24 +193,26 @@ if (!is_array($this->_resources[$resourceId]["messages"][$message])) { return PEAR::raiseError("Message doesnt exists."); } - + // getting bytes locations - $bytesStart = $this->_resources[$resourceId]["messages"][$message][0]; - $bytesEnd = $this->_resources[$resourceId]["messages"][$message][1]; - + $bytesStart = $this->_resources[$resourceId]["messages"][$message][0]; + $bytesEnd = $this->_resources[$resourceId]["messages"][$message][1]; + // a debug feature to show the bytes locations - if ($this->debug == true) { + if ($this->debug) { printf("%08d=%08d<br />", $bytesStart, $bytesEnd); } - + // seek to start of message if (@fseek($this->_resources[$resourceId]["fresource"], $bytesStart) == -1) { return PEAR::raiseError("Cannot read message bytes"); } - + if ($bytesEnd - $bytesStart > 0) { // reading and returning message (bytes to read = difference of bytes locations) - return fread($this->_resources[$resourceId]["fresource"], $bytesEnd - $bytesStart) . "\n"; + $msg = fread($this->_resources[$resourceId]["fresource"], + $bytesEnd - $bytesStart) . "\n"; + return $msg; } } @@ -233,21 +236,21 @@ } // creating temp file - $ftempname = tempnam ("/tmp", rand(0, 9)); - $ftemp = fopen($ftempname, "w"); + $ftempname = tempnam ("/tmp", rand(0, 9)); + $ftemp = fopen($ftempname, "w"); if ($ftemp == false) { return PEAR::raiseError("Cannot create a temp file. Cannot handle this error."); } - + // writting only undeleted messages $messages = $this->size($resourceId); - - for ($x = 0; $x < $messages; $x++) { + + for ($x = 0; $x < $messages; $x++) { if ($x == $message) { continue; } - $messageThis = $this->get($resourceId, $x); + $messageThis = $this->get($resourceId, $x); if (is_string($messageThis)) { fwrite($ftemp, $messageThis, strlen($messageThis)); } @@ -264,7 +267,7 @@ /** * Update a message * - * Note: PEAR::Mail_Mbox auto adds \n\n at end of the message + * Note: Mail_Mbox auto adds \n\n at end of the message * * Note: messages start with 0. * @@ -283,7 +286,7 @@ // creating temp file $ftempname = tempnam ("/tmp", rand(0, 9)); - $ftemp = fopen($ftempname, "w"); + $ftemp = fopen($ftempname, "w"); if ($ftemp == false) { return PEAR::raiseError("Cannot create a temp file. Cannot handle this error."); } @@ -291,11 +294,11 @@ // writting only undeleted messages $messages = $this->size($resourceId); - for ($x = 0; $x < $messages; $x++) { + for ($x = 0; $x < $messages; $x++) { if ($x == $message) { - $messageThis = $content . "\n\n"; + $messageThis = $content . "\n\n"; } else { - $messageThis = $this->get($resourceId, $x); + $messageThis = $this->get($resourceId, $x); } if (is_string($messageThis)) { @@ -330,14 +333,13 @@ function insert($resourceId, $content, $offset = NULL) { // checking if we have bytes locations for this message - if (!is_array($this->_resources[$resourceId])) { return PEAR::raiseError("ResourceId doesnt exists."); } // creating temp file $ftempname = tempnam ("/tmp", rand(0, 9)); - $ftemp = fopen($ftempname, "w"); + $ftemp = fopen($ftempname, "w"); if ($ftemp == false) { return PEAR::raiseError("Cannot create a temp file. Cannot handle this error."); } @@ -353,15 +355,15 @@ if ($offset !== NULL && $x == $offset) { fwrite($ftemp, $content, strlen($content)); } - $messageThis = $this->get($resourceId, $x); + $messageThis = $this->get($resourceId, $x); if (is_string($messageThis)) { fwrite($ftemp, $messageThis, strlen($messageThis)); } } } - if ($offset === NULL) - { + + if ($offset === NULL) { fwrite($ftemp, $content, strlen($content)); } @@ -373,8 +375,6 @@ return $this->_move($resourceId, $ftempname, $filename); } - - /** * Copy a file to another * @@ -388,7 +388,7 @@ function _move($resourceId, $ftempname, $filename) { // opening ftemp to read - $ftemp = fopen($ftempname, "r"); + $ftemp = fopen($ftempname, "r"); if ($ftemp == false) { return PEAR::raiseError("Cannot open temp file."); @@ -400,23 +400,22 @@ return PEAR::raiseError("Cannot write on mbox file."); } - while (feof($ftemp) != true) - { + while (feof($ftemp) != true) { $strings = fread($ftemp, 4096); if (!fwrite($fp, $strings, strlen($strings))) { return PEAR::raiseError("Cannot write to file."); } } + fclose($fp); - fclose($ftemp); - unlink($ftempname); - + // open another resource and substitute it to the old one $mid = $this->open($filename); $this->_resources[$resourceId] = $this->_resources[$mid]; unset($this->_resources[$mid]); + return true; } @@ -433,7 +432,6 @@ function _process($resourceId) { // sanit check - if (!is_resource($this->_resources[$resourceId]["fresource"])) { return PEAR::raiseError("Resource isn't valid."); } @@ -444,49 +442,48 @@ } // starting values - $bytes = 0; + $bytes = 0; + $lines = 0; + unset($lineThis); unset($lineLast); - $lines = 0; while (feof($this->_resources[$resourceId]["fresource"]) != true) { // getting char by char - $c = fgetc($this->_resources[$resourceId]["fresource"]); - $lineThis .= $c; + $c = fgetc($this->_resources[$resourceId]["fresource"]); + $lineThis .= $c; // each \n we will check things if ($c === "\n") { // checking if start with From if (substr($lineThis, 0, 5) === "From ") { // this line byte count is last line more 1 byte - $bytesStart = $bytesEnd + 1; + $bytesStart = $bytesEnd + 1; // last line byte count is this line bytes minus this line length $bytesEnd = $bytes - strlen($lineThis); // we will check messages after they end if ($bytesStart != 1) { - if ($this->debug == true) { - printf("#################### from byte %08d to byte %08d ################### <br />", $bytesStart, $bytesEnd); - } - // setting new message points - $messagesCount = $this->size($resourceId); - $this->_resources[$resourceId]["messages"][$messagesCount][0] = $bytesStart; - $this->_resources[$resourceId]["messages"][$messagesCount][1] = $bytesEnd; - } - - } - + if ($this->debug) { + printf("#################### from byte %08d to byte %08d ################### <br />", $bytesStart, $bytesEnd); + } + // setting new message points + $messagesCount = $this->size($resourceId); + $this->_resources[$resourceId]["messages"][$messagesCount][0] = $bytesStart; + $this->_resources[$resourceId]["messages"][$messagesCount][1] = $bytesEnd; + } + } - // increasing number of lines (doesnt matter) - if ($this->debug == true) { + // increasing number of lines (doesn't matter) + if ($this->debug) { $lines++; } - + // last line is this line - $lineLast = $lineThis; + $lineLast = $lineThis; // this line is blank now unset($lineThis); - if ($this->debug == true) { + if ($this->debug) { printf("%08d:%08d %s<br/>", $lines, $bytes, $lineLast); } } @@ -499,11 +496,10 @@ // last line byte count is this line bytes minus this line length $bytesEnd = $bytes - strlen($lineThis) - 2; // we will check messages after they end - - $messagesCount = $this->size($resourceId); - $this->_resources[$resourceId]["messages"][$messagesCount][0] = $bytesStart; - $this->_resources[$resourceId]["messages"][$messagesCount][1] = $bytesEnd; - + + $messagesCount = $this->size($resourceId); + $this->_resources[$resourceId]["messages"][$messagesCount][0] = $bytesStart; + $this->_resources[$resourceId]["messages"][$messagesCount][1] = $bytesEnd; } }