SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Am I terminating my command correctly? (fwrite, fgets, imap etc)

    Hi all,

    I've just started working on PHP as I've switched to Linux. I'm a .Net developer by heart and I prefere to reinvent the wheel, that way I know the code is safe.

    Anyhoo...

    On connecting to my IMAP server, I am able to receive the capabilities but when I try to receive a response to the command ". login username password\r\n" the application hangs, and then finally timesout.

    I can connect to telnet using the same commands and have had no problems what so ever, so am I missing something?

    I have a feeling I am not terminating the data that I am sending to the server correctly.

    Anyone got any idea's?

    TIA

    Gav

    Code:
    <?php
    
       class imap
       {
          var $imap_folders;
          var $lastError;
          var $connection;
          var $capabilities;
          var $log;
          var $user;
          var $pass;
          
          function connect($host, $port, $user, $pass) {
          
             $this->user = $user;
             $this->pass = $pass;
             $this->connection = fsockopen($host, 143, $err_no, $err_str, 60);
             if(!$this->connection) {
             
                $this->appendLog('Error: ' . $err_no . ': ' . $err_str);
                return false;
             }
             
             $this->appendLog('Action: Connected to \'' . $host . '\'.');
    
             return true;
             
          }
          function disconnect() {
          
             if($this->connection)
             {
                fputs($this->connection, ". logout\r\n");
                $this->appendLog('Sent: \'. logout\'.');
                fclose($this->connection);
                $this->appendLog('Action: Disconnected.');
             }
          }
          function appendLog($msg) {
          
             $this->log[count($this->log)] = $msg;
          }
          function login($user, $pass) {
          
             if(!$this->connection)
                return false;
             $this->appendLog('Action: Beginging login procedure.');
             $req = fputs($this->connection, '. login ' . $user . ' ' . $pass . '\r\n');
             if(!$req) {
             
                $this->appendLog('Error: Unable to send command \'. login ' . $user . ' ' . $pass . '\\r\\n\'.');
                return false;
             }
             $this->appendLog('Sent: \'. login ' . $user . ' ' . $pass . '\r\n\'.');
             if(!feof($this->connection)) {
             
                $resp = fgets($this->connection);
                if(!$resp)
                {
                   $this->appendLog('Error: Unable to receive data.');
                }
                $this->appendLog('Received: ' . $resp);
             }
             $this->appendLog('Action: Completed login procedure.');
             
             return true;
          }
          function parseCapabilities() {
          
             if(!$this->connection)
                return false;
                
             $data = fgets($this->connection);
             if(!$data) {
             
                $this->appendLog('Error: Unable to retrieve data.');
                return false;
             
             }
             $this->appendLog('Action: Parsing Capabilities.');
             $this->appendLog('Received: ' . $data);
             
             preg_match_all('#\[([A-Za-z0-9 =]+)\]#', $data, $out, PREG_PATTERN_ORDER);
             $this->capabilities = explode(' ', $out[1][0]);
             $this->appendLog('Action: End Parsing Capabilities.');
             
             return true;
          }
       }
       
       $test = new imap();
       $test->connect("server", 143, "username", "password");
       echo "Connected<br />";
       $test->parseCapabilities();
       $test->login($test->user, $test->pass);
       $test->disconnect();
       echo "Disconnected<br />";
       
       echo "<pre>";
       print_r($test->log);
       echo "</pre>";
       
    ?>


  2. #2
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I haven't pulled out the RFC yet, but I'm pretty sure you're terminating your lines correctly. Check your log - where exactly is it hanging? Is it not returning from the login command, or is it dying waiting for the response? Is PHP giving you any errors or warnings at all?
    PHP questions? RTFM
    MySQL questions? RTFM

  3. #3
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It seems that it is waiting for the fgets to return some data. Which should be ". OK LOGIN Ok.".

    But it just sits there with nothing, and i'm just wondering whether it's because i'm not terminating the command correctly (usually \r\n).

    I'm at home at the moment and I don't know of any Linux based port logged software just to see whether there is any negotiation (handshakes) going on.

    I'm used to dealing with raw tcp sockets, and I am a complete php noob. I'm used to being able to debug my applications in real time (c# / asp.net).

    I made a few changes to echo the time it started and finished.

    The output of the page is:

    Code:
    Starting IMAP Process - 00:29:58
    Connected
    Disconnected
    
    Array
    (
        [0] => Action: Connected to 'host'.
        [1] => Action: Parsing Capabilities.
        [2] => Received: * OK [CAPABILITY IMAP4rev1 UIDPLUS CHILDREN NAMESPACE THREAD=ORDEREDSUBJECT THREAD=REFERENCES SORT QUOTA IDLE ACL ACL2=UNION STARTTLS] Courier-IMAP ready. Copyright 1998-2004 Double Precision, Inc.  See COPYING for distribution information.
    
        [3] => Action: End Parsing Capabilities.
        [4] => Action: Beginging login procedure.
        [5] => Sent: '. login user@domain.com userPwd\r\n'.
        [6] => Error: Unable to receive data.
        [7] => Received: 
        [8] => Action: Completed login procedure.
        [9] => Sent: '. logout'.
        [10] => Action: Disconnected.
    )
    
    Finished IMAP Process - 00:30:58


  4. #4
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this looks wrong
    PHP Code:
    $req fputs($this->connection'. login ' $user ' ' $pass '\r\n');
    // try
    $req fputs($this->connection'. login ' $user ' ' $pass "\r\n"); 
    php wont expand escape sequences inside of single quoted strings, gotta use double quotes.

    i also noticed the first character in the string is a .
    i dont know if thats intentional or not, kinda looks like you made a mistake and thought you were concatenating? you did that in other places in the code as well.

  5. #5
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    imap commands start with a full stop (.)

    also the login command is as follows:

    . login user@domain.com password\r\n

    Also, I have tried using fwrite to send the command.

    But I have just realised what you mean, and this could be a solution, I will give the "\r\n" a try when I get back home.

    Thanks

    Gav



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •