Преглед изворни кода

pulled out all the login stuff and error checking from each individual php file
and put it in functions/imap.php

nehresma пре 26 година
родитељ
комит
f4b5e0bed0
8 измењених фајлова са 34 додато и 98 уклоњено
  1. 22 0
      functions/imap.php
  2. 3 17
      src/empty_trash.php
  3. 2 11
      src/folders.php
  4. 1 11
      src/folders_create.php
  5. 1 16
      src/folders_delete.php
  6. 1 10
      src/left_main.php
  7. 3 18
      src/move_messages.php
  8. 1 15
      src/right_main.php

+ 22 - 0
functions/imap.php

@@ -31,6 +31,28 @@
       return strrev($temp);
    }
 
+   // handles logging onto an imap server.
+   function loginToImapServer($username, $key, $imapServerAddress) {
+      $imapConnection = fsockopen($imapServerAddress, 143, &$errorNumber, &$errorString);
+      if (!$imapConnection) {
+         echo "Error connecting to IMAP Server.<br>";
+         echo "$errorNumber : $errorString<br>";
+         exit;
+      }
+      $serverInfo = fgets($imapConnection, 256);
+ 
+      // login
+      fputs($imapConnection, "1 login $username $key\n");
+      $read = fgets($imapConnection, 1024);
+ 
+      if (strpos($read, "NO")) {
+         error_username_password_incorrect();
+         exit;
+      }
+      
+      return $imapConnection;
+   }
+
    /** must be sent in the form:  user.<USER>.<FOLDER> **/
    function createFolder($imapConnection, $folder) {
       fputs($imapConnection, "1 create \"$folder\"\n");

+ 3 - 17
src/empty_trash.php

@@ -4,23 +4,9 @@
    include("../functions/strings.php");
    include("../functions/page_header.php");
    include("../functions/display_messages.php");
+   include("../functions/imap.php");
 
-   $imapConnection = fsockopen($imapServerAddress, 143, &$errorNumber, &$errorString);
-   if (!$imapConnection) {
-      echo "Error connecting to IMAP Server.<br>";
-      echo "$errorNumber : $errorString<br>";
-      exit;
-   }
-   $serverInfo = fgets($imapConnection, 256);
-
-   // login
-   fputs($imapConnection, "1 login $username $key\n");
-   $read = fgets($imapConnection, 1024);
-
-   if (strpos($read, "NO")) {
-      error_username_password_incorrect();
-      exit;
-   }
+   $imapConnection = loginToImapServer($username, $key, $imapServerAddress);
 
    // switch to the mailbox, and get the number of messages in it.
    selectMailbox($imapConnection, $mailbox, $numMessages);
@@ -41,4 +27,4 @@
    displayPageHeader($mailbox);
 
    messages_deleted_message($mailbox, $sort, $startMessage);
-?>
+?>

+ 2 - 11
src/folders.php

@@ -13,16 +13,7 @@
    echo "   </TD></TR>\n";
    echo "</TABLE>\n";
 
-   $imapConnection = fsockopen($imapServerAddress, 143, &$errorNumber, &$errorString);
-   if (!$imapConnection) {
-      echo "Error connecting to IMAP Server.<br>";
-      echo "$errorNumber : $errorString<br>";
-      exit;
-   }
-   $serverInfo = fgets($imapConnection, 256);
-
-   fputs($imapConnection, "1 login $username $key\n");
-   $read = fgets($imapConnection, 1024);
+   $imapConnection = loginToImapServer($username, $key, $imapServerAddress);
 
    fputs($imapConnection, "1 list \"\" *\n");
    $str = imapReadData($imapConnection);
@@ -91,4 +82,4 @@
    echo "</FORM><BR>\n";
 
 ?>
-</BODY></HTML>
+</BODY></HTML>

+ 1 - 11
src/folders_create.php

@@ -4,17 +4,7 @@
    include("../functions/page_header.php");
    include("../functions/imap.php");
 
-   $imapConnection = fsockopen($imapServerAddress, 143, &$errorNumber, &$errorString);
-   if (!$imapConnection) {
-      echo "Error connecting to IMAP Server.<br>";
-      echo "$errorNumber : $errorString<br>";
-      exit;
-   }
-   $serverInfo = fgets($imapConnection, 256);
-
-   fputs($imapConnection, "1 login $username $key\n");
-   $read = fgets($imapConnection, 1024);
-   echo $read;
+   $imapConnection = loginToImapServer($username, $key, $imapServerAddress);
 
    if ($subfolder == "INBOX")
       fputs($imapConnection, "1 create \"user.$username.$folder_name\"\n");

+ 1 - 16
src/folders_delete.php

@@ -4,22 +4,7 @@
    include("../functions/page_header.php");
    include("../functions/imap.php");
 
-   $imapConnection = fsockopen($imapServerAddress, 143, &$errorNumber, &$errorString);
-   if (!$imapConnection) {
-      echo "Error connecting to IMAP Server.<br>";
-      echo "$errorNumber : $errorString<br>";
-      exit;
-   }
-   $serverInfo = fgets($imapConnection, 256);
-
-   fputs($imapConnection, "1 login $username $key\n");
-   $read = fgets($imapConnection, 1024);
-   echo $read;
-
-   if (strpos($read, "NO")) {
-      error_username_password_incorrect();
-      exit;
-   }
+   $imapConnection = loginToImapServer($username, $key, $imapServerAddress);
 
    // switch to the mailbox, and get the number of messages in it.
    selectMailbox($imapConnection, $mailbox, $numMessages);

+ 1 - 10
src/left_main.php

@@ -39,16 +39,7 @@
    include("../functions/imap.php");
 
    // open a connection on the imap port (143)
-   $imapConnection = fsockopen($imapServerAddress, 143, &$errorNumber, &$errorString);
-   if (!$imapConnection) {
-      echo "Error connecting to IMAP Server.<br>";
-      echo "$errorNumber : $errorString<br>";
-      exit;
-   }
-   $serverInfo = fgets($imapConnection, 256);
-
-   fputs($imapConnection, "1 login $username $key\n");
-   $read = fgets($imapConnection, 1024);
+   $imapConnection = loginToImapServer($username, $key, $imapServerAddress);
 
    fputs($imapConnection, "1 list \"\" *\n");
    $str = imapReadData($imapConnection);

+ 3 - 18
src/move_messages.php

@@ -5,25 +5,10 @@
    include("../functions/page_header.php");
    include("../functions/display_messages.php");
 
-   $imapConnection = fsockopen($imapServerAddress, 143, &$errorNumber, &$errorString);
-   if (!$imapConnection) {
-      echo "Error connecting to IMAP Server.<br>";
-      echo "$errorNumber : $errorString<br>";
-      exit;
-   }
-   $serverInfo = fgets($imapConnection, 256);
-
-   // login
-   fputs($imapConnection, "1 login $username $key\n");
-   $read = fgets($imapConnection, 1024);
-
-   if (strpos($read, "NO")) {
-      error_username_password_incorrect();
-      exit;
-   }
+   $imapConnection = loginToImapServer($username, $key);
 
    // switch to the mailbox, and get the number of messages in it.
-   selectMailbox($imapConnection, $mailbox, $numMessages);
+   selectMailbox($imapConnection, $mailbox, $numMessages, $imapServerAddress);
 
    // Marks the selected messages ad 'Deleted'
    $j = 0;
@@ -54,4 +39,4 @@
    displayPageHeader($mailbox);
 
    messages_deleted_message($mailbox, $sort, $startMessage);
-?>
+?>

+ 1 - 15
src/right_main.php

@@ -47,21 +47,7 @@
 
 
    // open a connection on the imap port (143)
-   $imapConnection = fsockopen($imapServerAddress, 143, &$errorNumber, &$errorString);
-   if (!$imapConnection) {
-      echo "Error connecting to IMAP Server.<br>";
-      echo "$errorNumber : $errorString<br>";
-      exit;
-   }
-   $serverInfo = fgets($imapConnection, 256);
-
-   // login
-   fputs($imapConnection, "1 login $username $key\n");
-   $read = fgets($imapConnection, 1024);
-   if (strpos($read, "NO")) {
-      error_username_password_incorrect();
-      exit;
-   }
+   $imapConnection = loginToImapServer($username, $key, $imapServerAddress);
 
    // If the page has been loaded without a specific mailbox,
    //    just show a page of general info.