View file wapirate/xhtml/email.php

File size: 30.91Kb
<?php

include("captainsafeconfig.php");

include("captainsafecore.php");

connectdb();



$sid = $_GET["sid"];

$pwd = $_GET["logpwd"];

$usr = getnick_sid($sid);





// Number of messages per page (you can change this if you like)

$num = 9;



// Maximum message byte size (you can change this if you like)

// 3600 is best for wap devices with a 5.6k limit (like most popular mobile phones)

$max_chars = 3600;



// Enable the Send Mail feature by setting this to true

$sendMail = true; // can either be true or false



// connection data //



/* How to enter the connection data

   Look at the connection arrays below (they are called $serv_#).

   Exchange the information in the arrays so that they reflect your situation.

     "server" should be the path to the mailserver together with some protocol information.

       Examples for the different protocols:

         - imap non-secure: {server:143/imap/notls}Inbox

         - imap secure:  {server:993/imap/ssl/novalidate-cert}Inbox

         - pop3 non-secure: {server:110/pop3/notls}Inbox

         - pop3 secure:  {server:995/pop3/ssl/novalidate-cert}Inbox

       See http://www.php.net/imap_open for some more examples

     "nickname" is an arbitrary name that you choose to refer to the e-mail provider

     "emailaddress" is your e-mail address that belongs together with the e-mail account

     "name" should reflect the username that you have to use to logon to the mailserver

     "password" comes with the username that was provided to you by the e-mail provider

   If you add or remove an array be sure you number them nicely

   Do not forget to change the $number_of_arrays parameter that is directly below this section

   Be sure to check all the quotation-marks (") and comma's (,) when you are done or the script will not work

*/

$nick = "DMPWap";

$emailadd = $usr + "dmpwap-net.net";

$name = $emailadd;

$pass = $pwd;

 $number_of_arrays = 1; // this is the number of arrays that are listed below



 $serv_1 = array(

  "server" => "{mail.dmp-wap.net:110/pop3/notls}Inbox",

  "nickname" => $nick,

  "emailaddress" => $emailadd,

  "name" => $emailadd,

  "password" => $pwd

 );



 //  do not forget to change the $number_of_arrays if

 //  you add or delete any of the arrays above here!!



///////////////////////////////////////////////////////////////////////////

// that's it, no more variables to consider beyond this point, have fun! //

///////////////////////////////////////////////////////////////////////////



/***********************************************************************************\

 * URL variables

   These variables can be expected to travel inside the URL (or URI for that matter)

   c : message offset number when displaying the inbox

   d : parameters for message deletion

   f : message string fetch offset

   m : message number

   p : $serv_# parameter

   w : write message

 

 * Message functions

   These functions operate on an e-mail message

   - mes_del  : delete message

   - mes_incl : include message in a reply

   - mes_prop : retrieve message properties

 

 * Display functions

   These produce output to the browser

   - disp_prov      : e-mail accounts

   - disp_inbox     : inbox

   - disp_mes       : e-mail message

   - disp_login     : login screen

   - disp_sendmail  : display links to write e-mail

   - disp_delmes    : delete message dialog



 * Other functions

   - send_mail      : display send mail interface and send mail handling

   - buidl_wml      : build up of wml page

\***********************************************************************************/



// delete message

function mes_del()

{

  // set globals

  global $HTTP_GET_VARS, $prov, $conn;



  // get initial message count

  $status_old = imap_status($conn , $prov["server"] , SA_MESSAGES);

  $msg_ini_count = $status_old -> messages;



  // split $d to get msgno and date

  list($del_msgno , $del_date) = split(";" , $HTTP_GET_VARS["d"]);



  // get date of message by msgno, check hash of date against $del_date

  // if the two dates match delete message

  $del_headers = @imap_header($conn , $del_msgno);

  if(md5($del_headers -> date) == $del_date)

  {

    imap_delete($conn , $del_msgno);

    imap_expunge($conn);

  }



  // get final message count

  $status_new = imap_status($conn , $prov["server"] , SA_MESSAGES);

  $msg_final_count = $status_new -> messages;



  // display whether the deletion was succesfull or not

  if($msg_ini_count > $msg_final_count)

  {

    echo "Message&nbsp;deleted<br />\n";

  }

  else

  {

    echo "Message was not deleted<br />\n";

  }



  // reset or unset variables that may have changed

  if($del_msgno == $HTTP_GET_VARS["c"]) $HTTP_GET_VARS["c"] -= 1;

  if($msg_final_count <= 0) unset($HTTP_GET_VARS["c"]);

}



// include message

function mes_incl()

{

}



// retrieve message properties

function mes_prop($action , $id)

{

  // set globals

  global $conn;

  

  // get message headers

  $headers = imap_header($conn , $id);

  

  // get message structure

  $structure = imap_fetchstructure($conn , $id);



  // what action?

  switch($action)

  {

    // get sender

    case "sender":

      $sender = "";

      $from = $headers -> from;



      if ($from)

      {

        if(!$from[0] -> personal)

        {

          // emailaddress

          $sender = $from[0] -> mailbox . "@" . $from[0] -> host;

        }

        else

        {

          // realname

          $sender = $from[0] -> personal;

        }

      }

      else

      {

        // extract address from unfiltered format header when there is no $from

        $alt_headers = strtolower(imap_fetchheader($conn , $id));

        $return_path = strstr($alt_headers , "return-path");

        $end_pos = strpos($alt_headers , "\r\n" , $return_path);

        $sender = trim(substr($return_path , 12 , $end_pos-13));

        $sender = preg_replace("/<|>/" , "" , $sender);

      }

      return $sender;

   

    // get date 

    case "date":

      $date = "date: not found";

      if($headers -> date)

      {

        $date = $headers -> date;

      }

      else

      {

        $alt_headers = strtolower(imap_fetchheader($conn , $id));

        $date_set = strstr($alt_headers, "delivery-date");

        if($date_set)

        {

          $end_pos = strpos($alt_headers , "\r\n" , $date_set);

          $date = trim(substr($date_set, 15, $end_pos-4));

          $date = ucwords($date);

        }

      }

      return $date;



    // get subject

    case "subject":

      $subject = "No subject";

      if($headers -> subject) $subject = parse_preg($headers -> subject);  // function parse_preg() is located elsewhere

      return $subject;

   

    // get size

    case "size":

      $size = "?";

      if($headers -> Size) $size = round(($headers -> Size / 1024 ), 1);

      return $size;



    // get message body   

    case "message":

      $message = "<empty>";

      return $message;

 

    // get message number

    case "number":

      $message_n = trim($headers -> Msgno);

      return $message_n;



    // get attachment info

    case "attachments":

      $attached = "";

      $attachment_info = "";

      $n_attachments = 0;

      $parts = count($structure -> parts);

      for($i = 0 ; $i <= $parts ; $i++)

      {

        $part = $structure -> parts[$i];

        if($part -> ifdisposition && strtolower($part -> disposition) == "attachment" && $part -> ifdparameters)

        {

          $file = htmlspecialchars($part -> dparameters[0] -> value);

          $size = round($structure -> parts[$i] -> bytes / 1024 , 1);

          $attached .= $file . "&nbsp;(" . $size . "Kb)<br />\n";

          $n_attachments += 1;

        }

      }

      if($n_attachments > 0)

      {

        $attachment_info  = "<br />\nAttachments:<br />\n";

        $attachment_info .= $attached . "<br />\n";

      }

      return $attachment_info;

  }

}



// display e-mail accounts

function disp_prov()

{

  // set globals (except $$serv which is set later in this function)

  global $HTTP_SERVER_VARS, $number_of_arrays;



  echo "Select provider<br />\n";

  for ($i = 1 ; $i <= $number_of_arrays ; $i++)

  {

    // set provider

    $serv = "serv_" . $i;

    global $$serv;

    $prov_array = $$serv;



    // get number of unseen (imap) / total (pop3) messages on server

    imap_timeout(1,1);

    imap_timeout(2,1);

    $conn = @imap_open ($prov_array["server"] , $prov_array["name"] , $prov_array["password"]);

    $new_messages = "?";

    if ($conn)

    {

      $inbox = imap_status($conn , $prov_array["server"] , SA_UNSEEN);

      if($inbox) $new_messages = $inbox -> unseen;

    }



    // display provider's nickname and number of new messages

    $nickname = $prov_array["nickname"];

    echo "<a href=\"" . $HTTP_SERVER_VARS['PHP_SELF'] . "?p=" . $serv . "\">" . $nickname . "&nbsp;(" . $new_messages . ")</a><br />\n";

  }

}



// display inbox

function disp_inbox()

{

  // set globals

  global $HTTP_GET_VARS

       , $HTTP_SERVER_VARS

       , $session_urls

       , $num

       , $conn

       , $number_of_arrays

  ;

 

  // get number of messages in inbox

  $check = imap_check($conn);

  if($check) $Nmsgs = $check -> Nmsgs;



  // set first and number of messages to be displayed

  if(!ISSET($HTTP_GET_VARS["c"]) || $HTTP_GET_VARS["c"] > $Nmsgs || $HTTP_GET_VARS["c"] < 1) $HTTP_GET_VARS["c"] = $Nmsgs;

  $end = $HTTP_GET_VARS["c"] - $num; // $num is set at the beginning of this script

  if($end < 1) $end = 1;



  // build navigation

  $nav_url = "";

  $nav_frm = "";

 

  // navigation to previous page

  if ($HTTP_GET_VARS["c"] != $Nmsgs)

  {

    $prev = ($HTTP_GET_VARS["c"] + $num + 1);

    if ($prev > $Nmsgs) $prev = $Nmsgs;

    $href = $HTTP_SERVER_VARS['PHP_SELF'] . "?p=" . $HTTP_GET_VARS["p"] . "&amp;c=" . $prev;

    $nav_url .= "<a href=\"" . $href . "\">Previous</a>\n";

    $nav_frm .= "<do name=\"previous\" type=\"accept\" label=\"Previous\">\n";

    $nav_frm .= "<go href=\"" . $href . $session_urls["amp"] . "\" />\n";

    $nav_frm .= "</do>\n";

  }

 

  // navigation to next page

  if($end != 1)

  {

    $next = ($HTTP_GET_VARS["c"] - ($num + 1));

    if ($next < 1) $next = 1;

    $href = $HTTP_SERVER_VARS['PHP_SELF'] . "?p=" . $HTTP_GET_VARS["p"] . "&amp;c=" . $next;

    $nav_url .= "<a href=\"" . $href . "\">Next</a>\n";

    $nav_frm .= "<do name=\"next\" type=\"accept\" label=\"Next\">\n";

    $nav_frm .= "<go href=\"" . $href . $session_urls["amp"] . "\"/>\n";

    $nav_frm .= "</do>\n";

  }



  // navigation to providers page

  $nav_url .= "<a href=\"" . $HTTP_SERVER_VARS['PHP_SELF'] . "\">Provider</a>\n";

  $nav_frm .= "<do name=\"provider\" type=\"accept\" label=\"Provider\">\n";

  $nav_frm .= "<go href=\"" . $HTTP_SERVER_VARS['PHP_SELF'] . "\" />\n";

  $nav_frm .= "</do>\n";



  if($Nmsgs != 0)

  {

    // display navigation

    echo $nav_url . "<br />\n<br />\n";



    // display messages

    for ($i = $HTTP_GET_VARS["c"] ; $i >= $end ; $i--)

    {

      // get message properties

      $sender  = mes_prop("sender" , $i);

      $subject = mes_prop("subject" , $i);

      $date    = mes_prop("date" , $i);

      $number  = mes_prop("number" , $i);

      $size    = mes_prop("size" , $i);

   

      // construct output

      $output  = "<a href=\"". $HTTP_SERVER_VARS["PHP_SELF"];

      $output .= "?p=" . $HTTP_GET_VARS["p"] . "&amp;m=" . $number;

      $output .= "&amp;c=" . $HTTP_GET_VARS["c"] . "\">" . $sender . "</a><br />\n";

      $output .= $date . "<br />\n";

      $output .= $subject . "&nbsp;(" . $size . "Kb)<br />\n<br />\n";

   

      // echo output

      echo $output;

    }



    // display navigation

    echo $nav_url;

    echo $nav_frm;

  }

  else

  {

    // display empty inbox

    echo "Inbox is empty<br />\n";

    echo $nav_url;

    echo $nav_frm;

  }

}



// display e-mail message

function disp_mes()

{

  // set globals

  global $HTTP_GET_VARS, $HTTP_SERVER_VARS, $session_urls, $Nmsgs, $conn, $max_chars;

  

  // initialize navigation strings

  $nav_url = "";

  $nav_frm = "";



  // output message

  if($HTTP_GET_VARS["m"] <= $Nmsgs && $HTTP_GET_VARS["m"] >= 1)

  {

    // get sender

    $from_var = mes_prop("sender", $HTTP_GET_VARS["m"]);

    $from_var .= " wrote:<br />\n";



    // check for multipart message and extract message body

    $structure = imap_fetchstructure($conn , $HTTP_GET_VARS["m"]);

    $part = 1;

    if($structure -> type == 1)

    {

      $part = "1.1";

      if($structure -> parts[0] -> type == 1) $part = "1.1.1";

    }

    if(imap_fetchbody($conn , $HTTP_GET_VARS["m"] , $part) == "") $part = 1;

    $text = imap_fetchbody($conn , $HTTP_GET_VARS["m"] , $part);



    // convert base64 encoded messages

    $base64 = false;

    $headers = strtolower(imap_fetchheader($conn, $HTTP_GET_VARS["m"]));

    if(strstr($headers, "content-transfer-encoding: base64")) $base64 = true;

    if($base64) $text = base64_decode($text);

 

    // parse preg

    $text = parse_preg($text);



    // truncate message if necessary

    $text_length = strlen($text);

    if($text_length > $max_chars)

    {

      // set length of string to fetch

      if(!ISSET($HTTP_GET_VARS["f"]))

      {

        $HTTP_GET_VARS["f"] = 0;

      }

      $length = $max_chars;

      if(($HTTP_GET_VARS["f"] + $max_chars) > $text_length) $length = ($text_length - $HTTP_GET_VARS["f"]);



      // get part of the message text

      $text = substr($text , $HTTP_GET_VARS["f"] , $length);



      // get position of last "." or <br /> in message part

      $offset = 0;

      $line_end = ".";

      if(!strpos($text , "." , $offset)) $line_end = "<br />";

      if(strpos($text , $line_end , $offset))

      {

        while(strpos($text , $line_end , $offset))

        {

          $pos = strpos($text , $line_end , $offset);

          $offset = $pos + strlen($line_end);

        }

      }

      else

      {

        // set $offset to $max_char when the $line_end was not found

        $offset = $max_char;

      }



      // get adjusted part of part of message

      $text = substr($text , 0 , $offset);

    }



    // append any useful attachment-info to message string

    $attachments = mes_prop("attachments" , $HTTP_GET_VARS["m"]);

    if($attachments) $text .= $attachments;



    // links to display links to delete message, inbox and provider list

    $href = $HTTP_SERVER_VARS['PHP_SELF'] . "?p=" . $HTTP_GET_VARS["p"] . "&amp;c=" . $HTTP_GET_VARS["c"];

    $nav_url .= "<a href=\"#del\">Delete</a>\n";

    $nav_frm .= "<do name=\"delete\" type=\"accept\" label=\"Delete\">\n";

    $nav_frm .= "<go href=\"#del\" />\n";

    $nav_frm .= "</do>\n";



    $nav_url .= "<a href=\"" . $href . "\">Inbox</a>\n";

    $nav_frm .= "<do name=\"back\" type=\"accept\" label=\"Inbox\">\n";

    $nav_frm .= "<go href=\"" . $HTTP_SERVER_VARS["PHP_SELF"] . "?p=" . $HTTP_GET_VARS["p"] . "&amp;c=" . $HTTP_GET_VARS["c"] . $session_urls["amp"] . "\" />\n";

    $nav_frm .= "</do>\n";

  

    $nav_url .= "<a href=\"" . $HTTP_SERVER_VARS["PHP_SELF"] . "\">Provider</a>\n";

    $nav_frm .= "<do name=\"provider\" type=\"accept\" label=\"Provider\">\n";

    $nav_frm .= "<go href=\"" . $HTTP_SERVER_VARS["PHP_SELF"] . "\" />\n";

    $nav_frm .= "</do>\n";

   

    // build links to next part of messages for message longer than $max_chars

    if(($HTTP_GET_VARS["f"] + $max_chars) < $text_length)

    {

      $offset = $offset + $HTTP_GET_VARS["f"];

      $href = $HTTP_SERVER_VARS["PHP_SELF"]

            . "?p=" . $HTTP_GET_VARS["p"]

            . "&amp;m=" . $HTTP_GET_VARS["m"]

            . "&amp;c=" . $HTTP_GET_VARS["c"]

            . "&amp;f=" . $offset;

      $nav_url .= "<a href=\"$href\">Rest of message</a>\n";

      $nav_frm .= "<do name=\"rest\" type=\"accept\" label=\"Rest message\"><go href=\"$href" . $session_urls["amp"] . "\"/></do><br />\n";

    }



    // preappend sender information

    $text = $from_var . $text;



    // preappend urls

    $text = $nav_url . "<br />\n" . $text;

  

    // display message

    echo utf8_encode($text);

  

    // append links

    echo $nav_url;

    echo $nav_frm;

  }

  else

  {

    // display error message not on server

    echo "Message is not on server<br />\n";

    echo "<a href=\"" . $HTTP_SERVER_VARS['PHP_SELF'] . "\">Inbox</a>";

    echo "<do name=\"back\" type=\"accept\" label=\"Inbox\"><go href=\"" . $HTTP_SERVER_VARS['PHP_SELF'] . $session_urls["question"];"\"/></do>";

  }

}



// display login screen

function disp_login()

{

  // set globals

  global $HTTP_GET_VARS, $HTTP_SERVER_VARS, $prov;

    

  ?>

  Enter login details for <?php echo $nick; ?><br />

  Username: <input name="name" value="<?php echo $name; ?>"/><br />

  Password: <input name="ww" type="password" value="<?php echo $pwd; ?>"/><br />

  <anchor>Connect

    <go href="<?php echo $HTTP_SERVER_VARS['PHP_SELF'] . "?p=" . $HTTP_GET_VARS["p"]; ?>" method="post">

      <postfield name="name" value="$(name)" />

      <postfield name="ww" value="$(ww)" />

    </go>

  </anchor>

  <do type="accept" label="Connect">

    <go href="<?php echo $HTTP_SERVER_VARS['PHP_SELF'] . "?p=" . $HTTP_GET_VARS["p"]; ?>" method="post">

      <postfield name="name" value="$(name)" />

      <postfield name="passw" value="$(ww)" />

    </go>

  </do>

  <a href="<?php echo $HTTP_SERVER_VARS['PHP_SELF']; ?>">Change provider</a>

  <do name="provider" type="accept" label="Provider"><go href="<?php echo $HTTP_SERVER_VARS['PHP_SELF']; ?>" /></do>

  <?php

}



// display link to write new mail

function disp_sendmail($action , $prov , $id)

{

  // set globals

  global $HTTP_SERVER_VARS, $sendMail;

  

  // stop disp_sendmail if sending mail is disabled

  if (!$sendMail) return false;



  // stop disp_sendmail if mail() function is available

  if (!function_exists("mail"))

  {

    echo "Your server doesn't support sending emails with PHP.";

    return false;

  }

    

  // create url (action can be 'new', 'reply' or 'replyall')

  if ($action == "new")

  {

    echo "<a href=\"" . $HTTP_SERVER_VARS['PHP_SELF'] . "?w=" . $action . "\">Write new mail</a>\n";

  }

  else

  {

    echo "<a href=\"" . $HTTP_SERVER_VARS['PHP_SELF'] . "?w=" . $action . "&amp;p=" . $prov . "&amp;m=" . $id . "\">Reply</a>\n";

    # Reply to all is not yet implemented

  }

}



// display card to delete message

function disp_delmes()

{

  // set globals

  global $conn, $HTTP_SERVER_VARS, $HTTP_GET_VARS;

  

  // display delete message-card

  $del_headers = imap_header($conn , $HTTP_GET_VARS["m"]);

  

  // hash message date to transfer it succesfully in the query string

  $href_del = $HTTP_SERVER_VARS['PHP_SELF']

            . "?p=" . $HTTP_GET_VARS["p"]

            . "&amp;c=" . $HTTP_GET_VARS["c"]

            . "&amp;d=" . $HTTP_GET_VARS["m"]



            . ";" . md5($del_headers -> date);



  ?>

  </p>

  </card>

  <card id="del" title="Delete message">

  <p>Are your sure?<br />

  <a href="#main">No</a><br />

  <a href="<?php echo $href_del; ?>">Yes</a><br />

  <?php

}



/*

 * Parse_preg

 * This function parses a string and converts that

 * string to a character set that is wml compatible

 */

function parse_preg($text)

{

  // rid html-only messages of tags and content inside certain tags

  $tags_and_content_to_strip = Array("head" , "title" , "style" , "script");

  foreach ($tags_and_content_to_strip as $tag)

  {

    // this was taken from a message posted anonymously at php.net website and was adapted to fit momail

    $text = preg_replace("/<" . $tag . "(.|\s)*?>(.|\s)*?<\/" . $tag . ">/i" , "" , $text);

  }



  // convert some special characters, like <br /> and &, to a different character set

  $text = preg_replace("/<br\/?>/i" , "\r\n" , $text);

  $text = preg_replace("/&nbsp;/i" , "=20" , $text);

  $text = str_replace("&" , "&amp;" , $text);



  // strip remaining html tags

  $text = strip_tags($text);



  // encode characters like & < > " before adding new ones

  $text = htmlspecialchars($text , ENT_COMPAT , "ISO-8859-1");



  // replace quoted-printable by decimal reference

  $text = preg_replace_callback("/(=)([0-9a-fA-F]{2})/" , "parse_hexdec" , $text); // transform function is written below



# $text = imap_qprint($text);  // is buggie



  // replace dollar sign by ... dollar

  $text = preg_replace_callback("/(\\$)((([0-9])(\\.|,)?){2,})/" , "parse_dollar" , $text); // dollar_transform is written below



  // modify output to comply to xml standard and compact output (rids output of unnecessary <br />'s)

  $find = array(

    0 => "/=\r(\n)?/",

    1 => "/\r(\n)?/",

    2 => "/((\s|&#32;|&nbsp;)*<br(\s)*\/?>(\r)?(\n)?){3,}/i",

  );

  $replace = array(

    0 => "",

    1 => "<br />\r\n",

    2 => "<br />\r\n<br />\r\n",

  );

  ksort($find);

  ksort($replace);

  $text = preg_replace($find , $replace , $text);

 

  // quick 'n dirty bug fix to &amp;amp;

  $text = str_replace("&amp;amp;" , "&amp;" , $text);

 

  // return $text

  return $text;

}



  // additional functions to preg

  function parse_hexdec($matches)

  {

    $hex = $matches[2];

    return "&#" . hexdec($hex) . ";";

  }

 

  function parse_dollar($matches)

  {

    $amount = $matches[2];

    return $amount . " dollar";

  }

  // end of additional functions



// send mail

function send_mail()

{

  // set globals

  global $HTTP_SERVER_VARS, $HTTP_GET_VARS, $HTTP_POST_VARS, $number_of_arrays;

  

  // select whether to write or send a message

  if ($HTTP_GET_VARS["w"] != "send")

  {

    // build interface

    if (ISSET($HTTP_GET_VARS["p"]))

    {

    }

    else

    {

      // variable from address

      echo "Sender:<select name=\"sender\">\n";

      for ($i=1 ; $i <= $number_of_arrays ; $i++)

      {

        $serv = "serv_" . $i;

        global $$serv;

        $prov_array = $$serv;

        echo "  <option value=\"" . $prov_array["emailaddress"] . "\">" . $prov_array["emailaddress"] . "</option>\n";

      }

      echo "</select><br />\n";

      ?>

      To: <input name="recipient" emptyok="false" value="<? if(ISSET($HTTP_GET_VARS["m"])) echo $HTTP_GET_VARS["r"]; ?>" /><br />

      Subject: <input name="subject" value="<? if(ISSET($HTTP_GET_VARS["s"])) echo "RE:" . $HTTP_GET_VARS["s"]; ?>" /><br />

      <?php

      if ($HTTP_GET_VARS["w"] == "reply")

      {

        ?>

        Include original message? <select name="incl">

                                    <option value="y">Yes</option>

                                    <option value="n">No</option>

                                  </select><br />

        <?php

      }

      ?>         

      Message: <input name="message" value="" /><br />

      <anchor>Send

        <go href="<?php echo $HTTP_SERVER_VARS['PHP_SELF']; ?>?w=send" method="post">

          <postfield name="sender" value="$(sender)"/>

          <postfield name="recipient" value="$(recipient)"/>

          <postfield name="subject" value="$(subject)"/>

          <postfield name="incl" value="$(incl)"/>

          <postfield name="message" value="$(message)"/>

        </go>

      </anchor>

      <do type="accept" label="Send">

        <go href="<?php echo $HTTP_SERVER_VARS['PHP_SELF']; ?>?w=send" method="post">

          <postfield name="sender" value="$(sender)"/>

          <postfield name="recipient" value="$(recipient)"/>

          <postfield name="subject" value="$(subject)"/>

          <postfield name="incl" value="$(incl)"/>

          <postfield name="message" value="$(message)"/>

        </go>

      </do>

      <anchor>Cancel

        <go href="<?php echo $HTTP_SERVER_VARS['PHP_SELF']; ?>" />

      </anchor>

      <do type="accept" label="Cancel">

        <go href="<?php echo $HTTP_SERVER_VARS['PHP_SELF']; ?>" />

      </do>

      <?php

    }

  }

  else /* email verzenden */

  {

    // check for a recipient

    if ($HTTP_POST_VARS["recipient"] != "")

    {

      // send message

      $rc = false;

      $rc = mail($HTTP_POST_VARS["recipient"]

               , $HTTP_POST_VARS["subject"]

               , $HTTP_POST_VARS["message"] 

               , "From: " . $HTTP_POST_VARS["sender"] . "\r\n"

               . "Reply-To: " . $HTTP_POST_VARS["sender"] . "\r\n"

               . "Return-path: " . $HTTP_POST_VARS["sender"] . "\r\n"

               .  "X-Mailer: Momail PHP/" . phpversion()

               , "-f " . $HTTP_POST_VARS["sender"]

      );

   

      // check send process and report back to user

      if ($rc=true)

      {

        echo "Message was succesfully send to " . $HTTP_POST_VARS["recipient"] . "<br />\n";

        echo "<a href=\"" . $HTTP_SERVER_VARS['PHP_SELF'] . "\">Provider</a><br />\n";

      }

      else

      {

        echo "Message couldn't be send to " . $HTTP_POST_VARS["recipient"] . "<br />\n";

        echo "<a href=\"" . $HTTP_SERVER_VARS['PHP_SELF'] . "\">Provider</a><br />\n";

      }

    }

    else

    {

      echo "Without a recipient the message couldn't be send.<br />\n";

      echo "<a href=\"" . $HTTP_SERVER_VARS['PHP_SELF'] . "\">Provider</a><br />\n";

    }

  }  

}



function build_wml()

{

  // set globals

  global $HTTP_GET_VARS;

 

  // make dynamic title

  $title = "E-mail";

  if (ISSET($HTTP_GET_VARS["p"]))

  {

    $serv = $HTTP_GET_VARS["p"];

    global $$serv;

    $prov_array = $$serv;

    $title = $prov_array["nickname"];

  }

 

  // cache-control, wml headers and encoding type

  Header("Content-type: text/vnd.wap.wml");

  Header("Cache-Control: no-store, no-cache, must-revalidate"); 

  echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?".">\n";

  ?>

  <!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.openmobilealliance.org/tech/DTD/wml_1_1.dtd">

  <wml>

  <card newcontext="true" id="main" title="<?php echo $title; ?>">

  <p align="left">

  <?php

}



/*

 * Program Flow

 * This is where the different functions are called upon

 */

if (!function_exists("imap_open"))

{

  build_wml();

  echo "Message from Momail:<br /><br />\r\n"

     . "Momail detected a PHP installation without IMAP.<br /><br />\r\n"

     . "You cannot use Momail without that.<br /><br />\r\n"

     . "Go to www.php.net/imap if you are looking for technical information about installing the IMAP extension on your server.\r\n";

  echo "</p>";

  echo "</card>";

  echo "</wml>";

  return;

}



// redirect users that have only one e-mail account directly to that inbox (don't display provider list)

if ($number_of_arrays == 1 && !ISSET($HTTP_GET_VARS["p"])) Header("Location: " . $HTTP_SERVER_VARS['PHP_SELF'] . "?p=serv_1");



// display provider list or inbox/message

if(!ISSET($HTTP_GET_VARS["p"]))

{

  // check to see whether user wants to send an e-mail

  if (ISSET($HTTP_GET_VARS["w"]))

  {

    build_wml();

    send_mail();

  }

  else

  { 

    // display provider list

    // when only one provider was specified the user is redirected

    // to the inbox of that provicer by a header above

    build_wml();

    disp_prov();

    echo "<br />\n";

    disp_sendmail("new" , 0 , 0);

  }

}

else

{

  // get connection data

  $prov = $$HTTP_GET_VARS["p"];



  // check connection data

  if ($prov["name"] == "" || $prov["password"] == "")

  {

    // start session

    $session_name = preg_replace("/\.|\s/" , "_" , $prov["nickname"]);

    session_name("$session_name");

    session_start();

  

    // check for user input

    if (count($HTTP_POST_VARS) != 0)

    {

      $prov["name"] = $HTTP_POST_VARS["name"];

      $prov["password"] = $HTTP_POST_VARS["ww"];

      $HTTP_SESSION_VARS["name"] = $prov["name"];

      $HTTP_SESSION_VARS["password"] = $prov["password"];

    }

    elseif ($HTTP_SESSION_VARS["name"] && $HTTP_SESSION_VARS["password"])

    {

      $prov["name"] = $HTTP_SESSION_VARS["name"];

      $prov["password"] = $HTTP_SESSION_VARS["password"];

    }

    else

    {

      build_wml();

      disp_login();

    }

  }



  // create special session urls (if necessary) for the hrefs inside <go /> tags

  // is a bug fix for many wap browser that don't append this standard to these urls

  $session_urls = NULL;

  if($HTTP_SESSION_VARS)

  {

    $session_urls = array();

    $session_urls["amp"] = "&amp;" . session_name() . "=" . session_id();

    $session_urls["question"] = "?" . session_name() . "=" . session_id();

  }



  // connect to server if required arguments are set

  if ($prov["name"] != "" && $prov["password"] != "")

  {

    // connect to server

    $conn = @imap_open ($prov["server"] , $prov["name"] , $prov["password"]);



    if ($conn)

    {

      // build wml page

      build_wml();



      // delete message from server if $d is set

      if(ISSET($HTTP_GET_VARS["d"]) && $HTTP_SERVER_VARS['HTTP_REFERER'] = $HTTP_SERVER_VARS['PHP_SELF'])

      {

        mes_del();

      }

      elseif (ISSET($HTTP_GET_VARS["d"]) && $HTTP_SERVER_VARS['HTTP_REFERER'] != $HTTP_SERVER_VARS['PHP_SELF'])

      {

        echo "Error: The page that you are coming from is either "

           . "not visible to Momail (due perhaps to firewall software) "

           . "or not a page generated by Momail. Because this is a "

           . "possible security thread, the message was not deleted.<br />\n";

      }



      // check inbox and get number of messages

      $check = imap_check($conn);

      if($check) $Nmsgs = $check -> Nmsgs;



      // check for user input

      if (!ISSET($HTTP_GET_VARS["m"]))

      {

        // display inbox

        disp_inbox();

      }

      else

      {

        if (!ISSET($HTTP_GET_VARS["w"]))

        {

          // display message

#          disp_sendmail("reply" , $HTTP_GET_VARS["p"] , $HTTP_GET_VARS["m"]);

          disp_mes();

#          disp_sendmail("reply" , $HTTP_GET_VARS["p"] , $HTTP_GET_VARS["m"]);

          disp_delmes();

        }

        else

        {

          // reply to message

          send_mail($action , $id);

        }

      }

    }

    else

    {

      // display connection error

      build_wml();

      echo "Connection error<br />\n";

      echo "<a href=\"" . $HTTP_SERVER_VARS['PHP_SELF'] . "\">Back</a><br />\n";

      echo "<do name=\"back\" type=\"accept\" label=\"Back\"><go href=\"" . $HTTP_SERVER_VARS['PHP_SELF'] . "\" /></do>\n";

    }

  }

}



// close pop3_stream

if ($conn) imap_close($conn);

?>

</p>

</card>

</wml>