Browse Source

\#46: Mailbox-Status mit AJAX implementiert

Moritz Schmidt 10 năm trước cách đây
mục cha
commit
237c5aa17b
5 tập tin đã thay đổi với 175 bổ sung12 xóa
  1. 9 4
      ajax.php
  2. 58 6
      includes/mailbox.inc.php
  3. 49 0
      scripts/custom.js
  4. 54 0
      styles/style.css
  5. 5 2
      templates/settings.php

+ 9 - 4
ajax.php

@@ -50,7 +50,6 @@ switch($_REQUEST['action']) {
                 break;
 
         }
-        //$db->updateQuery("UPDATE `mailboxes` SET x=y WHERE id=;")
         break;
     case 'updateMailfolder':
         switch($_POST['name']) {
@@ -108,13 +107,12 @@ switch($_REQUEST['action']) {
         break;
     case 'getMailAccountsByUid':
         header("Status: 200 OK");
-        $mailboxes = Mailbox::getMailboxesByUserId($_REQUEST['uId'], false);
+        $mailboxes = Mailbox::getMailboxesByUserId($_REQUEST['uId']);
         $mbArray = array();
         foreach($mailboxes as $mailbox) {
             $mbArray[$mailbox->getId()] = $mailbox->getUsername();
         }
-        $jsonOut = json_encode($mbArray);
-        echo $jsonOut;
+        echo json_encode($mbArray);
         break;
     case 'getNewDocumentBox':
         $boxHtml = 'Vorlage: ';
@@ -135,6 +133,13 @@ switch($_REQUEST['action']) {
         //$documentHandler->saveFile();
         echo $boxHtml;
         break;
+    case 'getMailboxStatus':
+        $mailbox = Mailbox::getMailboxById($_REQUEST['mailboxId'], true);
+        $mailboxStatus = array(
+            "connected" => $mailbox->getConnected()
+        );
+        echo json_encode($mailboxStatus);
+        break;
     default:
         header("Status: 400 No Action Defined");
         echo 'error';

+ 58 - 6
includes/mailbox.inc.php

@@ -12,6 +12,7 @@ class Mailbox {
     private $password       = NULL;
     private $mailbox        = NULL;
     private $folders        = NULL;
+    private $connected      = false;
 
     public function __construct($id, $hostname, $port, $protocol, $useSsl, $noValidCert, $username, $password, $connect) {
         $this->id           = $id;
@@ -22,11 +23,17 @@ class Mailbox {
         $this->noValidCert  = $noValidCert;
         $this->username     = $username;
         $this->password     = $password;
-        // '{' . $mailbox->server . ':' . $mailbox->port . '/' . $mailbox->protocol . $useSsl . $noValidCert . '}'
-        $this->server   = '{' . $this->hostname . ':' . $this->port . '/' . $this->protocol . $this->useSsl . $this->noValidCert . '}';
+        $this->server       = '{' . $this->hostname . ':' . $this->port . '/' . $this->protocol . $this->useSsl . $this->noValidCert . '}';
 
         if($connect) {
-            $this->mailbox = imap_open($this->server . 'INBOX', $this->username, $this->password) or error('Failed to connect to IMAP: ' . $this->username . '@' . $this->hostname);
+            $this->mailbox = imap_open($this->server . 'INBOX', $this->username, $this->password); //  or error('Failed to connect to IMAP: ' . $this->username . '@' . $this->hostname)
+            imap_errors();
+            imap_alerts();
+            if($this->mailbox == false) {
+                $this->connected = false;
+            } else {
+                $this->connected = true;
+            }
         }
     }
 
@@ -85,6 +92,18 @@ class Mailbox {
         return $this->username;
     }
 
+    public function getConnected() {
+        return $this->connected;
+    }
+
+    public function getStatus() {
+        return imap_status($this->mailbox, $this->server, SA_ALL);
+    }
+
+    public function setConnected($connected) {
+        $this->connected = $connected;
+    }
+
     /**
      * Get all Mailboxes
      *
@@ -118,18 +137,51 @@ class Mailbox {
         return $return;
     }
 
+    /**
+     * Get Mailbox by ID
+     *
+     *
+     * @param int $userId  ID of Mailbox
+     * @param bool $connect Whether to connect or just to store data. Default: false
+     *
+     * @return Mailbox  Selected Mailboxes
+     *
+     */
+
+    public static function getMailboxById($mailboxId, $connect = false) {
+        global $db;
+
+        $return = array();
+        $mailboxes = $db->selectQuery("SELECT * FROM `mailboxes` WHERE `id` = " . $mailboxId . ";");
+
+        foreach($mailboxes as $mailbox) {
+            $useSsl = '';
+            $noValidCert = '';
+
+            if($mailbox->use_ssl) {
+                $useSsl = '/ssl';
+            }
+
+            if(!$mailbox->valid_ssl) {
+                $noValidCert = '/novalidate-cert';
+            }
+            $return[] = new Mailbox($mailbox->id, $mailbox->server, $mailbox->port, $mailbox->protocol, $useSsl, $noValidCert, $mailbox->username, $mailbox->password, $connect); // TODO: Encrypt password
+        }
+
+        return $return[0];
+    }
+
     /**
      * Get Mailboxes by User ID
      *
      *
      * @param int $userId  ID of user
-     * @param bool $connect Whether to connect or just to store data
      *
      * @return Array(Mailbox)  Array with selected Mailboxes
      *
      */
 
-    public static function getMailboxesByUserId($userId, $connect = true) {
+    public static function getMailboxesByUserId($userId) {
         global $db;
 
         $return = array();
@@ -146,7 +198,7 @@ class Mailbox {
             if(!$mailbox->valid_ssl) {
                 $noValidCert = '/novalidate-cert';
             }
-            $return[] = new Mailbox($mailbox->id, $mailbox->server, $mailbox->port, $mailbox->protocol, $useSsl, $noValidCert, $mailbox->username, $mailbox->password, $connect); // TODO: Encrypt password
+            $return[] = new Mailbox($mailbox->id, $mailbox->server, $mailbox->port, $mailbox->protocol, $useSsl, $noValidCert, $mailbox->username, $mailbox->password, false); // TODO: Encrypt password
         }
 
         return $return;

+ 49 - 0
scripts/custom.js

@@ -1,5 +1,45 @@
 $(document).ready(function() {
 
+    function getUrlGetParameter(val) {
+        var result = "Not found",
+            tmp = [];
+        location.search
+        .substr(1)
+            .split("&")
+            .forEach(function (item) {
+            tmp = item.split("=");
+            if (tmp[0] === val) result = decodeURIComponent(tmp[1]);
+        });
+        return result;
+    }
+
+    function addSpinner(element) {
+        element.append("<div class=\"spinner\"><div class=\"bounce1\"></div><div class=\"bounce2\"></div><div class=\"bounce3\"></div></div>");
+    }
+
+    function removeSpinner(element) {
+        element.children(".spinner").remove();
+    }
+
+    function getMailboxStatus() {
+        $("#settings-mailboxes tr").each(function(item) {
+            var thisTr = this;
+
+            if(this.attributes.length > 0) {
+                addSpinner($(this).children("td:first"));
+                $.getJSON("ajax.php?action=getMailboxStatus&mailboxId=" + $(this).attr("data-mailbox-id"), function(r) {
+                    if(r['connected'] == true) {
+                        removeSpinner($(thisTr).children("td:first"));
+                        $(thisTr).children("td:first").append("<i class=\"fa fa-check-circle\"></i>");
+                    } else {
+                        removeSpinner($(thisTr).children("td:first"));
+                        $(thisTr).children("td:first").append("<i class=\"fa fa-exclamation-circle\"></i>");
+                    }
+                });
+            }
+        });
+    }
+
     $.fn.editable.defaults.mode = 'inline';
     $.fn.editableform.buttons =
   '<button type="submit" class="btn btn-primary btn-sm editable-submit">'+
@@ -257,4 +297,13 @@ $(document).ready(function() {
         closeEffect	: 'none'
     });
 
+    switch(getUrlGetParameter("action")) {
+        case 'settings':
+            getMailboxStatus();
+            break;
+        default:
+
+            break;
+    }
+
 });

+ 54 - 0
styles/style.css

@@ -61,3 +61,57 @@ table td {
 	height: 60px;
 	background-color: #f5f5f5;
 }*/
+
+/* Spinner (http://tobiasahlin.com/spinkit/) */
+
+.spinner {
+	margin: 0 auto;
+	width: 15px;
+	text-align: left;
+	float: left
+}
+
+.spinner > div {
+	width: 5px;
+	height: 5px;
+	background-color: #333;
+
+	border-radius: 100%;
+	display: inline-block;
+	-webkit-animation: bouncedelay 1.4s infinite ease-in-out;
+	animation: bouncedelay 1.4s infinite ease-in-out;
+	-webkit-animation-fill-mode: both;
+	animation-fill-mode: both;
+}
+
+.spinner .bounce1 {
+	-webkit-animation-delay: -0.32s;
+	animation-delay: -0.32s;
+}
+
+.spinner .bounce2 {
+	-webkit-animation-delay: -0.16s;
+	animation-delay: -0.16s;
+}
+
+@-webkit-keyframes bouncedelay {
+	0%, 80%, 100% {
+		-webkit-transform: scale(0.0)
+	}
+
+	40% {
+		-webkit-transform: scale(1.0)
+	}
+}
+
+@keyframes bouncedelay {
+	0%, 80%, 100% {
+		transform: scale(0.0);
+		-webkit-transform: scale(0.0);
+	}
+	
+	40% {
+		transform: scale(1.0);
+		-webkit-transform: scale(1.0);
+	}
+}

+ 5 - 2
templates/settings.php

@@ -8,9 +8,10 @@
             $mailboxes = Mailbox::getMailboxesByUserId($user->getUserId()); // TODO: MVC
         ?>
 
-        <table class="table table-striped">
+        <table id="settings-mailboxes" class="table table-striped">
             <thead>
                 <tr>
+                    <th>Status</th>
                     <th>Hostname</th>
                     <th>Port</th>
                     <th>Protocol</th>
@@ -23,7 +24,8 @@
             </thead>
             <?php
             foreach($mailboxes as $mailbox) {
-                echo '<tr>';
+                echo '<tr data-mailbox-id="' . $mailbox->getId() . '">';
+                echo '<td></td>';
                 echo '<td>' . getEditableLink('hostname', 'text', $mailbox->getId(), 'Click to edit', $mailbox->getHostname()) . '</td>';
                 echo '<td>' . getEditableLink('port', 'text', $mailbox->getId(), 'Click to edit', $mailbox->getPort()) . '</td>'; // TODO: alternative inputs
                 echo '<td>' . getEditableLink('protocol', 'select', $mailbox->getId(), 'Click to edit', $mailbox->getProtocol()) . '</td>';
@@ -44,6 +46,7 @@
                 <td></td>
                 <td></td>
                 <td></td>
+                <td></td>
             </tr>
         </table>
     </div>