Results 1 to 10 of 10

Thread: [SOLVED] qview.php - php script to handle quarantined emails

  1. #1
    durango99 is offline Junior Member
    Join Date
    Sep 2008
    Posts
    5
    Rep Power
    6

    Default [SOLVED] qview.php - php script to handle quarantined emails

    [possible repost: my previous post was flagged as spam, so i'll post with the php in the thread instead of as an attachment]

    Until the RFE comes through, I created a simple php script to work with the quarantined email in a web interface. It's a first draft and I spent an hour working on it, so I won't be surprised if there might be some issues with it Not ideal but I think it's better than hunting around in the quarantine folder.

    Also, currently there is not security provided at the moment. So anyone could get to this page. .htaccess or a web function to tie into the ldap system in zimbra should do the trick.

    At the moment, you can view the raw email content or download the quarantine file but not release it back into the mail system to a recipient. The downloaded file will have a .eml extension so a email client should be able to render it.

    Hope this helps others out!

    Instructions:
    • Copy and paste the code below. Put it in a file called qview.php in /opt/zimbra/httpd/htdocs in your zimbra installation.
    • Access it http://<your zimbra server url>:7780/qview.php


    Code:
    PHP Code:
    <?php

    // Code by durango99
    // 10/21/2008
    // 

    $getflag $_GET['getflag'];

    $getfile $_GET['getfile'];
    $getfile urldecode($getfile);

    $dirname "/opt/zimbra/data/amavisd/quarantine/";

    //download it
    if ($getflag == 1){

        
    header('Content-Description: File Transfer');
        
    header('Content-Type: application/octet-stream');
        
    header('Content-Disposition: attachment; filename='.basename($getfile).'.eml');
        
    header('Content-Transfer-Encoding: binary');
        
    header('Expires: 0');
        
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        
    header('Pragma: public');
        
    header('Content-Length: ' filesize($getfile));
        
    ob_clean();
        
    flush();
        
    readfile($getfile);

        die;

    }


    ?>

    <html>
    <title>
    Qview - Quarantine viewer
    </title>

    <head>
    <style type="text/css">
    body { 
    font-family: arial;
    font-size: normal;
     }

    table { 
    font-family: arial;
    font-size: small;
     }
     
    </style>
    </head>

    <body>
    <?

    echo "<p>Current Quarantine View:</p>\n";

    //view it
    if ($getflag == 2){


        $file = file_get_contents ($getfile); 

        echo "<pre>$file</pre>";
        
        die;
        
    }


    $qfiles = dirList($dirname);

    echo "<table border=1>\n";

    echo "<tr>
    <td>#</td>
    <td>From:</td>
    <td>To</td>
    <td>Subject</td>
    <td>View</td>
    <td><font color=red><b>(CAREFUL!)</font></b></td>
    </tr>";

    $i=1;
    foreach ($qfiles as $qfile){

    $YourFile = $dirname . $qfile;
    $file = file_get_contents ($YourFile); 

    echo "<tr><td>\n";

    echo "$i";


    echo "</td><td>\n";

    preg_match( "/^From:(.*)$/m", $file, $fromline );
    echo ($fromline[1]);

    echo "</td><td>\n";

    preg_match( "/^To:(.*)$/m", $file, $toline );
    echo ($toline[1]);

    echo "</td><td>\n";

    preg_match( "/^Subject:(.*)$/m", $file, $subjline );
    echo ($subjline[1]);

    echo "</td><td>\n";

    $ue=str_replace('+','%2B', $YourFile);
    $ue=urlencode($ue);

    echo "<a href=qview.php?getfile=$ue&getflag=2 target=_blank><font size=1em>$qfile</font></a>";

    echo "</td><td>\n";

    echo "<a href=qview.php?getfile=$ue&getflag=1 target=_blank><font color=grey>download</font></a>";

    echo "</td></tr>\n";

    $i++;

    }

    echo "</table>\n";


    function dirList ($directory) 
    {

        // create an array to hold directory list
        $results = array();

        // create a handler for the directory
        $handler = opendir($directory);

        // keep going until all files in directory have been read
        while ($file = readdir($handler)) {

            // if $file isn't this directory or its parent, 
            // add it to the results array
            if ($file != '.' && $file != '..')
                $results[] = $file;
        }

        // tidy up: close the handler
        closedir($handler);

        // done!
        return $results;

    }

    ?>
    </body>
    </html>

  2. #2
    mmorse's Avatar
    mmorse is offline Moderator
    Join Date
    May 2006
    Location
    USA
    Posts
    6,242
    Rep Power
    21

  3. #3
    durango99 is offline Junior Member
    Join Date
    Sep 2008
    Posts
    5
    Rep Power
    6

    Default

    Quote Originally Posted by mmorse View Post
    Very nice:
    Thanks Mike for the kind words and providing a screenshot of the interface. Glad it's working for you.

    I'll probably add a date column and a sort feature so it's easier to read.

  4. #4
    Aderium is offline Project Contributor
    Join Date
    Nov 2005
    Location
    Parkland, Fl.
    Posts
    108
    Rep Power
    9

    Default

    Is this php ment to show daily results ? It would be nice to see feedback for more then just daily captures

  5. #5
    snake_eyes's Avatar
    snake_eyes is offline Advanced Member
    Join Date
    Nov 2008
    Posts
    237
    Rep Power
    6

    Default

    Nice script, I've checked your code now, but Is this code for each user he can mange his quarantine mails or it must be check through the admin consol?

    Regards

  6. #6
    uxbod's Avatar
    uxbod is offline Moderator
    Join Date
    Nov 2006
    Location
    UK
    Posts
    8,017
    Rep Power
    24

    Default

    The script is for all users ... time permitting I would like to try and write a zimlet for this, but need to see how to write the backend code.

  7. #7
    snake_eyes's Avatar
    snake_eyes is offline Advanced Member
    Join Date
    Nov 2008
    Posts
    237
    Rep Power
    6

    Default

    I want to know how to write the backend code in order to write a zmilets, with reference to my experience with the development world, I have to many ideas to be written by the zimlet in order to utilize each user separated.

    I wish studing this.. and I hope there is an document for the beginers.

    Yours Sincerely,

  8. #8
    Hivos's Avatar
    Hivos is offline Advanced Member
    Join Date
    Aug 2009
    Location
    The Hague -- The Netherlands
    Posts
    213
    Rep Power
    5

    Default Added sort by date

    Thanks for the script durango99. We've slightly adapted it to our needs. We've added a date column and display the array sorted by date.
    PHP Code:
    <?php

    // Code by durango99
    // 10/21/2008
    // Adapted by Hivos
    // 11/02/2009

    $getflag $_GET['getflag'];

    $getfile $_GET['getfile'];
    $getfile urldecode($getfile);

    $dirname "";//"/opt/zimbra/data/amavisd/quarantine/";

    //download it
    if ($getflag == 1){

        
    header('Content-Description: File Transfer');
        
    header('Content-Type: application/octet-stream');
        
    header('Content-Disposition: attachment; filename='.basename($getfile).'.eml');
        
    header('Content-Transfer-Encoding: binary');
        
    header('Expires: 0');
        
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        
    header('Pragma: public');
        
    header('Content-Length: ' filesize($getfile));
        
    ob_clean();
        
    flush();
        
    readfile($getfile);

        die;

    }


    ?>

    <html>
    <title>
    Zimbra quarantine viewer
    </title>

    <head>
    <style type="text/css">
    body { 
    font-family: arial;
    font-size: normal;
     }

    table { 
    font-family: arial;
    font-size: small;
     }
     
    </style>
    </head>

    <body>
    <?php

    echo "<p>Current Quarantine View:</p>\n";

    //view it
    if ($getflag == 2){


        
    $file file_get_contents ($getfile); 

        echo 
    "<pre>$file</pre>";
        
        die;
        
    }


    $qfiles dirList($dirname);

    echo 
    "<table border=1>\n";

    echo 
    "<tr>
    <td>#</td>
    <td>Date:</td>
    <td>From:</td>
    <td>To</td>
    <td>Subject</td>
    <td>View</td>
    <td><font color=red><b>(CAREFUL!)</font></b></td>
    </tr>"
    ;

    $i=1;
    foreach (
    $qfiles as $qfile){
        
    $YourFile $dirname $qfile;
        
    $file file_get_contents ($YourFile); 

        echo 
    "<tr><td>\n";
        echo 
    "$i";

        echo 
    "</td><td>\n";
        
    preg_match"/^Date:(.*)$/m"$file$date );
        echo 
    "$date[1]";

        echo 
    "</td><td>\n";
        
    preg_match"/^From:(.*)$/m"$file$fromline );
        echo (
    $fromline[1]);

        echo 
    "</td><td>\n";
        
    preg_match"/^To:(.*)$/m"$file$toline );
        echo (
    $toline[1]);

        echo 
    "</td><td>\n";
        
    preg_match"/^Subject:(.*)$/m"$file$subjline );
        echo (
    $subjline[1]);

        echo 
    "</td><td>\n";
        
    $ue=str_replace('+','%2B'$YourFile);
        
    $ue=urlencode($ue);
        echo 
    "<a href=qview.php?getfile=$ue&getflag=2 target=_blank><font size=1em>$qfile</font></a>";

        echo 
    "</td><td>\n";
        echo 
    "<a href=qview.php?getfile=$ue&getflag=1 target=_blank><font color=grey>download</font></a>";
        echo 
    "</td></tr>\n";

        
    $i++;

    }

    echo 
    "</table>\n";


    function 
    dirList($directory)
    {
        
    $results glob"/opt/zimbra/data/amavisd/quarantine/*" );

        
    // Sort files by modified time, latest to earliest
        // Use SORT_ASC in place of SORT_DESC for earliest to latest
        
    array_multisort(
        
    array_map'filemtime'$results ),
        
    SORT_NUMERIC,
        
    SORT_DESC,
        
    $results
        
    );
        return 
    $results;
    }
    ?>

  9. #9
    sgrzy01 is offline Active Member
    Join Date
    Dec 2008
    Posts
    25
    Rep Power
    6

    Default

    Thanx for the script.. much easier than the uudeview method we used....

    Two thins got in my way of using the above modded script... I fond that subject lines that had gibberish or the like would confuse redering and cause the formatting to mess up. You will see the offending text right where the break in formatting happens, do a grep throuhg the content of the files in the direcrtory, whack it...

    Also, some the the files I think were a bit big for the:

    $file = file_get_contents ($YourFile);

    and the script would stop with only a partial listing. Changed it to:

    $file = file_get_contents ($YourFile, NULL, NULL, -1, 5000);

    and that fixed it... I'm only quessing the 5000 characters is enough, you may have to adjust...

    Thanx!

  10. #10
    thunder04 is offline Special Member
    Join Date
    Dec 2007
    Location
    Stockton, CA
    Posts
    164
    Rep Power
    7

    Default

    This is awesome! This makes retrieving quaranteined e-mail much easier than via the CLI! If I'm bored, I may play around with making it "pretty". :-)

    Thank you!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 658
    Last Post: 04-04-2014, 09:01 AM
  2. PHP Script Auth
    By mirkocoz in forum Developers
    Replies: 20
    Last Post: 07-15-2011, 02:27 AM
  3. Replies: 8
    Last Post: 11-30-2007, 11:12 AM

Posting Permissions

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