Subject: webmail - with errors
Posted By: bad_demon Post Date: 11/4/2003 3:57:43 AM
these codes were from professional php 4, wrox

i have compiled it few times...

and in return, having two errors..

can someone explains?

webmail_class_test2.php

<?php

include "./webmail_class_ver2.php";

class My_Webmail extends Webmail
{
    function start()
    {
        $msgs = $this->getMsgList('readMsg', 'mailFrom');
        if (!$msgs) return false;
        $ret_str = '';
        foreach ($msgs as $msg)
            $ret_str .= $msg["subject"] . " - " . $msg["from"] . "<br>";
        return $ret_str;
    }
}

$host = "localhost";
$protocol = "imap";
$port = 143;
$userid = "chira";
$userpassword = "96422";
$mailbox = "INBOX";

$wmail = new My_Webmail();

if (!$wmail->init($host, $protocol, $port, $userid, $userpassword))
    echo($wmail->errorMsg());

$list = $wmail->start();

if (!$list)
    echo($wmail->errorMsg());
else
    echo($list);

$wmail->end();

?>


webmail_class_ver1.php

<?php

class Webmail
{
    var $host= '';
    var $protocol = 'imap';
    var $supported_protocols = array('imap', 'pop3');
    var $port = 143;
    var $userid = '';
    var $userpassword = '';
    
    var $stream = 0;
    var $mailbox = '';
    
    var $auto_expunge = true;
    
    var $ERR_ARGS_DELIMITER = " ";
    var $ERROR_MSG = '';
    
    var $ERR_STR_CONNECTION_FAILED = 'Connection failed!';
    var $ERR_STR_PROTOCOL_NOT_SUPPORTED = 'Protocol not supported!';
    var $ERR_STR_CLOSE_FAILED = 'Error closing the stream!';
    var $ERR_STR_MAILBOX_NOT_AVAILABLE = 'Mailbox not available!';
    var $ERR_STR_OVERRIDE_START = "Override start() method!";
    
    var $sort = 'SORTDATE';
    var $reverse = 0;
    
    var $ERR_STR_MAILBOX_STATUS_ERROR = 'Cannot get stat of mailbox!';
    var $STR_NO_SUBJECT = 'NO SUBJECT';
    var $STR_NO_FROM = 'UNKNOWN';
    
    
    function init($host, $protocol='imap', $port=143, $userid='', $userpassword='')
    {
        $this->host = $host;
        $this->protocol = $protocol;
        $this->port = $port;
        $this->userid = $userid;
        $this->userpassword = $userpassword;
        
        $this->mailbox = $GLOBALS["mailbox"];
        
        if (isset($GLOBALS["sort"])) $this->sort = $GLOBALS["sort"];
        if (isset($GLOBALS["reverse"])) $this->reverse = $GLOBALS["reverse"];
        
        if (!in_array($this->protocol, $this->supported_protocols))
        {
            $this->buildErrorMsg($this->ERR_STR_PROTOCOL_NOT_SUPPORTED,
                             $this->protocol);
            return false;
        }
        
        $mode = false;
        
        $this->stream = @imap_open("\{$this->host/$this->protocol:$this->port}$this->mailbox",
                              $this->userid, $this->userpassword, $mode);
        
        if (!$this->stream) {
            $this->buildErrorMsg($this->ERR_STR_CONNECTION_FAILED, imap_last_error());
            return false;
        }
        
        return true;
    }
    
    
    function start()
    {
        $this->buildErrorMsg($this->ERR_STR_OVERRIDE_START);
        return false;
    }
    
    
    function end()
    {
        if ($this->auto_expunge)
            $ret = @imap_close($this->stream, CL_EXPUNGE);
        else
            $ret = @imap_close($this->stream);
        
        if (!$ret){
            $this->buildErrorMsg($this->ERR_STR_CLOSE_FAILED, imap_last_error());
            return false;
        }
        
        return true;
    }
    
    
    function getMSgList($read_action, $mail_action)
    {
        $msgs = @imap_sort($this->stream, $this->sort, $this->reverse, SE_NOPREFETCH);
        
        if (!is_array($msgs))
            return false;
        
        for ($i = 0; $i < count($msgs); $i++){
            
            $msg = @imap_header($this->stream, $msg_no);
            $arr[$i]["no"] = $msg_no = $msgs[$i];
            $arr[$i]["uid"] = $msgs_uid = imap_uid($this->stream, $msg_no);
            
            if ($msg->Unseen == 'U' || $msg->Recent == 'R'){
                $arr[$i]["unseen"] = true;
            }
            else {
                $arr[$i]["unseen"] = false;
            }
            
            $arr[$i]["date"] = gmstrftime("%b %d %Y", strtotime($msg->date));
            
            $struct = imap_fetchstructure($this->stream, $msg_no);
            
            $num_parts = count($struct->parts) - 1;
            
            if ($num_parts > 0)
                $msg_prefix = "@";
            else
                $msg_prefix = '';
            
            if (empty($msg->subject)) {
                $arr[$i]["subject"] = $this->buildUrl("$read_action&msg_uid=$msg_uid&mailbox=$this->mailbox", $this->STR_NO_SUBJECT);
            }
            else {
                $msg_subject = $this->decodeHeader($msg->subject);
                $arr[$i]["subject"] = $this->buildUrl("$read_action&msg_uid=$msg_uid&mailbox=$this->mailbox", "$msg_prefix$msg_subject");
            }
            
            if (empty($msg->from)) {
                $arr[$i]["from"] = $this->STR_NO_FROM;
            }
            else {
                $arr[$i]["from"] = $this->makeAddress($msg->from, "$mail_action&msg_uid=$msg_uid&mailbox=$this->mailbox");
            }
        
        }
        return $arr;
    }
    
    
    function makeAddress($emails, $action)
    {
        if (!is_array($emails)) return;
        
        foreach ($emails as $email) {
            $personal = $this->decodeHeader($email->personal);
            $address = $email->mailbox . "@" . $email->host;
            
            if (!empty($personal)) {
                $arr[] = $this->buildUrl("action&email=$address", $personal);
            }
            else {
                $arr[] = $this->buildurl("action&email=$address", $address);
            }
            
        }
        
        return implode(',', $arr);
    }
    
    function decodeHeader($arg)
    {
        $dec_array = imap_mime_header_decode($arg);
        
        foreach ($dec_array as $obj)
            $arr[] = $obj->text;
        
        if (count ($arr) > 0)
            return implode('', $arr);
        else
            return $arg;
    }
    
    function buildUrl($option, $link, $onclick='')
    {
        global $PHP_SELF;
        
        if (!empty($on_click)) $onclick = " OnClick=\"$onclick\"";
        
        return "<a href=\"$PHP_SELF?$options\"$onclick>$link</a>";
    }
    
    
    function buildErrorMsg($err_msg, $err_arg='')
    {
        $this->ERROR_MSG = $err_msg . $this->ERR_ARGS_DELIMITER . $err_arg;
    }
    
    
    function errorMsg()
    {
        return $this->ERROR_MSG;
    }
}

?>

and errors are:

first time error:
Notice: Undefined variable: arr in C:\Web Page\htdocs\VandoriaMail\webmail_class_ver2.php on line 138

second time error:
Notice: strtotime(): Called with empty time parameter in C:\Web Page\htdocs\VandoriaMail\webmail_class_ver2.php on line 111

Notice: Undefined property: parts in C:\Web Page\htdocs\VandoriaMail\webmail_class_ver2.php on line 115

Notice: Undefined variable: msg_uid in C:\Web Page\htdocs\VandoriaMail\webmail_class_ver2.php on line 123

Notice: Undefined variable: options in C:\Web Page\htdocs\VandoriaMail\webmail_class_ver2.php on line 181
NO SUBJECT - UNKNOWN

hope there is someone who can do something regarding these problems...
i cant figure out the solutiosn

Reply By: nikolai Reply Date: 11/4/2003 12:37:32 PM
Well, I'm assuming that you have at least ONE typo in your post -- your errors are all in webmail_class_ver2.php, but your post says that we're looking at ver1.

Anyway, assuming that's just a typo, let's take a look at your first error.  Undefined variable: arr on line 138.  If you look at your function, $arr is never defined before it's used.  You jump in the loop if $i < count($msgs) and start appending to the $arr array.

The first iteration of the loop happens when $i is zero... so what happens when $msgs is an empty array?  You never add anything to $arr.  Thus, $arr is never created, and you get the error on line 138 when you try to return $arr.

Second error, line 111:  $arr[$i]["date"] = gmstrftime("%b %d %Y", strtotime($msg->date));

Well, if $msg->date isn't a valid time string, strtotime will complain about it.  Nuff said, I guess... You should check to see if that string is nonempty before you pass it to a function that accepts something specific.

Rather than continue figuring out someone else's problem, I suggest the following:  It seems to me that you're much better off reporting these problems to the author(s) of the class.  They're much more familiar with it and might be actively supporting and maintaining it.


Take care,

Nik
http://www.bigaction.org/
Reply By: bad_demon Reply Date: 11/14/2003 7:22:49 AM
im so sorry.. here is the ver2

webmail_class_ver2.php

<?php

class Webmail
{
    var $host= '';
    var $protocol = 'imap';
    var $supported_protocols = array('imap', 'pop3');
    var $port = 143;
    var $userid = '';
    var $userpassword = '';
    
    var $stream = 0;
    var $mailbox = '';
    
    var $auto_expunge = true;
    
    var $ERR_ARGS_DELIMITER = " ";
    var $ERROR_MSG = '';
    
    var $ERR_STR_CONNECTION_FAILED = 'Connection failed!';
    var $ERR_STR_PROTOCOL_NOT_SUPPORTED = 'Protocol not supported!';
    var $ERR_STR_CLOSE_FAILED = 'Error closing the stream!';
    var $ERR_STR_MAILBOX_NOT_AVAILABLE = 'Mailbox not available!';
    var $ERR_STR_OVERRIDE_START = "Override start() method!";
    
    var $sort = 'SORTDATE';
    var $reverse = 0;
    
    var $ERR_STR_MAILBOX_STATUS_ERROR = 'Cannot get stat of mailbox!';
    var $STR_NO_SUBJECT = 'NO SUBJECT';
    var $STR_NO_FROM = 'UNKNOWN';
    
    
    function init($host, $protocol='imap', $port=143, $userid='', $userpassword='')
    {
        $this->host = $host;
        $this->protocol = $protocol;
        $this->port = $port;
        $this->userid = $userid;
        $this->userpassword = $userpassword;
        
        $this->mailbox = $GLOBALS["mailbox"];
        
        if (isset($GLOBALS["sort"])) $this->sort = $GLOBALS["sort"];
        if (isset($GLOBALS["reverse"])) $this->reverse = $GLOBALS["reverse"];
        
        if (!in_array($this->protocol, $this->supported_protocols))
        {
            $this->buildErrorMsg($this->ERR_STR_PROTOCOL_NOT_SUPPORTED,
                             $this->protocol);
            return false;
        }
        
        $mode = false;
        
        $this->stream = @imap_open("\{$this->host/$this->protocol:$this->port}$this->mailbox",
                              $this->userid, $this->userpassword, $mode);
        
        if (!$this->stream) {
            $this->buildErrorMsg($this->ERR_STR_CONNECTION_FAILED, imap_last_error());
            return false;
        }
        
        return true;
    }
    
    
    function start()
    {
        $this->buildErrorMsg($this->ERR_STR_OVERRIDE_START);
        return false;
    }
    
    
    function end()
    {
        if ($this->auto_expunge)
            $ret = @imap_close($this->stream, CL_EXPUNGE);
        else
            $ret = @imap_close($this->stream);
        
        if (!$ret){
            $this->buildErrorMsg($this->ERR_STR_CLOSE_FAILED, imap_last_error());
            return false;
        }
        
        return true;
    }
    
    
    function getMSgList($read_action, $mail_action)
    {
        $msgs = @imap_sort($this->stream, $this->sort, $this->reverse, SE_NOPREFETCH);
        
        if (!is_array($msgs))
            return false;
        
        for ($i = 0; $i < count($msgs); $i++){
            
            $msg = @imap_header($this->stream, $msg_no);
            $arr[$i]["no"] = $msg_no = $msgs[$i];
            $arr[$i]["uid"] = $msgs_uid = imap_uid($this->stream, $msg_no);
            
            if ($msg->Unseen == 'U' || $msg->Recent == 'R'){
                $arr[$i]["unseen"] = true;
            }
            else {
                $arr[$i]["unseen"] = false;
            }
            
            $arr[$i]["date"] = gmstrftime("%b %d %Y", strtotime($msg->date));
            
            $struct = imap_fetchstructure($this->stream, $msg_no);
            
            $num_parts = count($struct->parts) - 1;
            
            if ($num_parts > 0)
                $msg_prefix = "@";
            else
                $msg_prefix = '';
            
            if (empty($msg->subject)) {
                $arr[$i]["subject"] = $this->buildUrl("$read_action&msg_uid=$msg_uid&mailbox=$this->mailbox", $this->STR_NO_SUBJECT);
            }
            else {
                $msg_subject = $this->decodeHeader($msg->subject);
                $arr[$i]["subject"] = $this->buildUrl("$read_action&msg_uid=$msg_uid&mailbox=$this->mailbox", "$msg_prefix$msg_subject");
            }
            
            if (empty($msg->from)) {
                $arr[$i]["from"] = $this->STR_NO_FROM;
            }
            else {
                $arr[$i]["from"] = $this->makeAddress($msg->from, "$mail_action&msg_uid=$msg_uid&mailbox=$this->mailbox");
            }
        
        }
        return $arr;
    }
    
    
    function makeAddress($emails, $action)
    {
        if (!is_array($emails)) return;
        
        foreach ($emails as $email) {
            $personal = $this->decodeHeader($email->personal);
            $address = $email->mailbox . "@" . $email->host;
            
            if (!empty($personal)) {
                $arr[] = $this->buildUrl("action&email=$address", $personal);
            }
            else {
                $arr[] = $this->buildurl("action&email=$address", $address);
            }
            
        }
        
        return implode(',', $arr);
    }
    
    function decodeHeader($arg)
    {
        $dec_array = imap_mime_header_decode($arg);
        
        foreach ($dec_array as $obj)
            $arr[] = $obj->text;
        
        if (count ($arr) > 0)
            return implode('', $arr);
        else
            return $arg;
    }
    
    function buildUrl($option, $link, $onclick='')
    {
        global $PHP_SELF;
        
        if (!empty($on_click)) $onclick = " OnClick=\"$onclick\"";
        
        return "<a href=\"$PHP_SELF?$options\"$onclick>$link</a>";
    }
    
    
    function buildErrorMsg($err_msg, $err_arg='')
    {
        $this->ERROR_MSG = $err_msg . $this->ERR_ARGS_DELIMITER . $err_arg;
    }
    
    
    function errorMsg()
    {
        return $this->ERROR_MSG;
    }
}

?>


Go to topic 6380

Return to index page 1005
Return to index page 1004
Return to index page 1003
Return to index page 1002
Return to index page 1001
Return to index page 1000
Return to index page 999
Return to index page 998
Return to index page 997
Return to index page 996