home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / admin / compose2.php < prev    next >
Encoding:
PHP Script  |  2004-01-21  |  43.4 KB  |  1,322 lines

  1. <?php
  2. /////////////////////////////////////////////////////////
  3. //    
  4. //    source/compose2.php
  5. //
  6. //    (C)Copyright 2000-2002 Ryo Chijiiwa <Ryo@IlohaMail.org>
  7. //
  8. //        This file is part of IlohaMail.
  9. //        IlohaMail is free software released under the GPL 
  10. //        license.  See enclosed file COPYING for details,
  11. //        or see http://www.fsf.org/copyleft/gpl.html
  12. //
  13. /////////////////////////////////////////////////////////
  14.  
  15. /********************************************************
  16.  
  17.     AUTHOR: Ryo Chijiiwa <ryo@ilohamail.org>
  18.     FILE:  source/compose.php
  19.     PURPOSE:
  20.         1.  Provide interface for creating messages
  21.         2.  Provide interface for uploading attachments
  22.         3.  Form MIME format (RFC822) compliant messages
  23.         4.  Send message
  24.         5.  Save to "sent items" folder if so specified
  25.     PRE-CONDITIONS:
  26.         $user - Session ID for session validation and user preference retreaval
  27.     POST-CONDITIONS:
  28.         Displays standard message composition interface by default
  29.         If "upload" button pressed, displays all inputted text and attachment info
  30.         If "send" button pressed, sends, files, and displays status
  31.     COMMENTS:
  32.     
  33. ********************************************************/
  34.  
  35.  
  36. include("../include/super2global.inc");
  37. include("../include/header_main.inc");
  38. include("../lang/".$my_prefs["lang"]."compose.inc");
  39. include("../lang/".$my_prefs["lang"]."dates.inc");
  40. include("../include/icl.inc");
  41. include("../include/version.inc");
  42. include("../conf/defaults.inc");
  43. if ($GPG_ENABLE){
  44.     include_once("../include/gpg.inc");
  45. }
  46.  
  47. function RemoveDoubleAddresses($to) {
  48.     $to_adr = iil_ExplodeQuotedString(",", $to);
  49.     $adresses = array();
  50.     $contacts = array();
  51.     foreach($to_adr as $addr) {
  52.         $addr = trim($addr);
  53.         if (preg_match("/(.*<)?.*?([^\s\"\']+@[^\s>\"\']+)/", $addr, $email)) {
  54.             $email = strtolower($email[2]);
  55.             if (!in_array($email, $adresses)) {                        //New adres
  56.                 array_push($adresses, $email);
  57.                 $contacts[$email] = $addr;
  58.             } elseif (strlen($contacts[$email])<strlen($addr)) {                //Adres already in list and name is longer
  59.                 $contacts[$email] = trim($addr);
  60.             }
  61.         }
  62.     }
  63.     return implode(", ",$contacts);
  64. }
  65.  
  66. function ResolveContactsGroup($str){
  67.     global $contacts;
  68.     
  69.     $tokens = explode(" ", $str);
  70.     if (!is_array($tokens)) return $str;
  71.     
  72.     while ( list($k,$token)=each($tokens) ){
  73.         if (ereg("@contacts.group", $token)){
  74.             if (ereg("^<", $token)) $token = substr($token, 1);
  75.             list($group, $junk) = explode("@contacts.", $token);
  76.             $group = base64_decode($group);
  77.             $newstr = "";
  78.             reset($contacts);
  79.             while ( list($blah, $contact)=each($contacts) ){
  80.                 if ($contact["grp"]==$group && !empty($contact["email"])){
  81.                     $newstr.= (!empty($newstr)?", ":"");
  82.                     $newstr.= "\"".$contact["name"]."\" <".$contact["email"].">";
  83.                 }
  84.             }
  85.             if (ereg(",$", $token)) $newstr.= ",";
  86.             $tokens[$k] = $newstr;
  87.             if (ereg(str_replace(" ", "_", $group), $tokens[$k-1])) $tokens[$k-1] = "";
  88.         }
  89.     }
  90.     
  91.     return implode(" ", $tokens);
  92. }
  93.  
  94.  
  95. if (ini_get('file_uploads')!=1){
  96.     echo "Error:  Make sure the 'file_uploads' directive is enabled (set to 'On' or '1') in your php.ini file";
  97. }
  98.  
  99.  
  100.  
  101. /******* Init values *******/
  102. if (!isset($attachments)) $attachments=0;
  103. if (isset($change_contacts)) $show_contacts = $new_show_contacts;
  104. if (isset($change_show_cc)) $show_cc = $new_show_cc;
  105.  
  106. //read alternate identities
  107. include_once("../include/data_manager.inc");
  108. $ident_dm = new DataManager_obj;
  109. if ($ident_dm->initialize($loginID, $host, $DB_IDENTITIES_TABLE, $DB_TYPE)){
  110.     $alt_identities = $ident_dm->read();
  111. }
  112.  
  113. //Handle ddresses submitted from contacts list 
  114. //(in contacts window)
  115. if (is_array($contact_to)) $to .= (empty($to)?"":", ").urldecode(implode(", ", $contact_to));
  116. if (is_array($contact_cc)) $cc .= (empty($cc)?"":", ").urldecode(implode(", ", $contact_cc));
  117. if (is_array($contact_bcc)) $bcc .= (empty($bcc)?"":", ").urldecode(implode(", ", $contact_bcc));
  118. //(in compose window)
  119. if ((isset($to_a)) && (is_array($to_a))){
  120.     reset($to_a);
  121.     while ( list($key, $val) = each($to_a)) $$to_a_field .= ($$to_a_field!=""?", ":"").stripslashes($val);
  122. }
  123.  
  124. //generate authenticated email address
  125. if (empty($init_from_address)){
  126.     $sender_addr = $loginID.( strpos($loginID, "@")>0 ? "":"@".$host );
  127. }else{
  128.     $sender_addr = str_replace("%u", $loginID, str_replace("%h", $host, $init_from_address));
  129. }
  130.  
  131. //generate user's name
  132. $from_name = $my_prefs["user_name"];
  133. $from_name = LangEncodeSubject($from_name, $my_charset);
  134. if ((!empty($from_name)) && (count(explode(" ", $from_name)) > 1)) $from_name = "\"".$from_name."\"";
  135.  
  136. if ($TRUST_USER_ADDRESS){
  137.     //Honor User Address
  138.     //If email address is specified in prefs, use that in the "From"
  139.     //field, and set the Sender field to an authenticated address
  140.     $from_addr = (empty($my_prefs["email_address"]) ? $sender_addr : $my_prefs["email_address"] );
  141.     $from = $from_name." <".$from_addr.">";
  142.     $reply_to = "";
  143. }else{
  144.     //Default
  145.     //Set "From" to authenticated user address
  146.     //Set "Reply-To" to user specified address (if any)
  147.     $from_addr = $sender_addr;
  148.     $from = $from_name." <".$sender_addr.">";
  149.     if (!empty($my_prefs["email_address"])) $reply_to = $from_name."<".$my_prefs["email_address"].">";
  150.     else $reply_to = "";
  151. }
  152. $original_from = $from;
  153.  
  154. echo "\n<!-- FROM: $original_from //-->\n";
  155.  
  156.  
  157. //resolve groups added from contacts selector
  158. $to_has_group = $cc_has_group = $bcc_has_group = false;
  159. if (!empty($to)) $to_has_group = ereg("@contacts.group", $to);
  160. if (!empty($cc)) $cc_has_group = ereg("@contacts.group", $cc);
  161. if (!empty($bcc)) $bcc_has_group = ereg("@contacts.group", $bcc);
  162. if ($to_has_group || $cc_has_group || $bcc_has_group){
  163.     $dm = new DataManager_obj;
  164.     if ($dm->initialize($loginID, $host, $DB_CONTACTS_TABLE, $DB_TYPE)){
  165.         if (empty($sort_field)) $sort_field = "grp,name";
  166.         if (empty($sort_order)) $sort_order = "ASC";
  167.         $contacts = $dm->sort($sort_field, $sort_order);
  168.         
  169.         if ($to_has_group) $to = ResolveContactsGroup($to);
  170.         if ($cc_has_group) $cc = ResolveContactsGroup($cc);
  171.         if ($bcc_has_group) $bcc = ResolveContactsGroup($bcc);
  172.     }
  173. }
  174.  
  175.  
  176.  
  177. /***
  178.     CHECK UPLOADS DIR
  179. ***/
  180. $uploadDir = $UPLOAD_DIR.ereg_replace("[\\/]", "", $loginID.".".$host);
  181. if (!file_exists(realpath($uploadDir))) $error .= "Uploads dir not found: \"$uploadDir\"<br>";
  182.  
  183.  
  184. /****
  185.     SEND
  186. ****/
  187. function cmp_send(){}
  188. if (isset($send)){
  189.     $conn = iil_Connect($host, $loginID, $password, $AUTH_MODE);
  190.     if (!$conn)
  191.         echo "failed";
  192.     else{
  193.         //echo "Composing...<br>\n"; flush();
  194.         
  195.         $error = "";
  196.         
  197.         /**** Check for subject ***/
  198.         $no_subject = false;
  199.         if ((strlen($subject)==0)&&(!$confirm_no_subject)){
  200.             $error .= $composeErrors[0]."<br>\n";
  201.             $no_subject = true;
  202.         }
  203.         
  204.         /**** alternate identity? ****/
  205.         if ($sender_identity_id > 0){
  206.             //format sender name
  207.             $from_name = $alt_identities[$sender_identity_id]["name"];
  208.             $from_name = LangEncodeSubject($from_name, $my_charset);
  209.             if ((!empty($from_name)) && (count(explode(" ", $from_name)) > 1)) $from_name = "\"".$from_name."\"";
  210.             
  211.             //format "From:" header
  212.             $from_addr = $alt_identities[$sender_identity_id]["email"];
  213.             $from = $from_name." <".$from_addr.">";
  214.             
  215.             //format "Reply-To:" header
  216.             if (!empty($alt_identities[$sender_identity_id]["replyto"])) 
  217.                 $reply_to = $from_name." <".$alt_identities[$sender_identity_id]["replyto"].">";
  218.             else
  219.                 $reply_to = "";
  220.         }
  221.         
  222.         /**** Check "from" ***/
  223.         if (strlen($from)<7) $error .= $composeErrors[1]."<br>\n";
  224.         
  225.         /**** Check for recepient ***/
  226.         $to = stripslashes($to);
  227.         $to = str_replace(";",",",$to);
  228.         if ((strcasecmp($to, "self")==0) || (strcasecmp($to, "me")==0)) $to=$my_prefs["email_address"];
  229.         if ((strlen($to) < 7) || (strpos($to, "@")===false))
  230.             $error .= $composeErrors[2]."<br>\n";
  231.             
  232.         /**** Anti-Spam *****/
  233.         $as_ok = true;
  234.         //echo "lastSend: $lastSend <br> numSent: $numSent <br>\n";
  235.         //echo "$max_rcpt_message $max_rcpt_session $min_send_interval <br>";
  236.         if ((isset($max_rcpt_message)) && ((isset($max_rcpt_session))) && (isset($min_send_interval))){
  237.             $num_recepients = substr_count($to.$cc.$bcc, "@");
  238.             if ($num_recepients > $max_rcpt_message) $as_ok = false;
  239.             if (($num_recepients + $numSent) > $max_rcpt_session) $as_ok = false;
  240.             if ((time() - $lastSend) < $min_send_interval) $as_ok = false;
  241.         }else{
  242.             echo "Bypassing anti-spam<br>\n";
  243.         }
  244.         if (!$as_ok){
  245.             $as_error = $composeErrors[5];
  246.             $as_error = str_replace("%1", $max_rcpt_message, $as_error);
  247.             $as_error = str_replace("%2", $max_rcpt_session, $as_error);
  248.             $as_error = str_replace("%3", $min_send_interval, $as_error);
  249.             $error .= $as_error;
  250.         }
  251.         /**********************/
  252.  
  253.         if ($error){
  254.             //echo "<font color=\"red\">".$error."</font><br><br>\n";
  255.         }else{
  256.             //echo "<p>Sending....";
  257.             //flush();
  258.             
  259.             $num_parts=0;
  260.     
  261.             /*** Initialize header ***/
  262.             $headerx = "Date: ".TZDate($my_prefs["timezone"])."\r\n";
  263.             $headerx.= "X-Mailer: IlohaMail/".$version." (On: ".$_SERVER["SERVER_NAME"].")\r\n";
  264.             if (!empty($replyto_messageID)) $headerx.= "In-Reply-To: <".$replyto_messageID.">\r\n";
  265.         
  266.  
  267.             /****  Attach Sig ****/
  268.             if ($attach_sig==1){
  269.                 if ($sender_identity_id > 0) $message.="\n\n".$alt_identities[$sender_identity_id]["sig"];
  270.                 else $message.= "\n\n".$my_prefs["signature1"];
  271.             }    
  272.  
  273.             /****  Attach Tag-line ***/
  274.             
  275.             if ($userLevel < $TAG_LEVEL){
  276.                 $message .= "\n\n".$TAG_LINE;
  277.             }
  278.  
  279.             /******* GPG stuff *********/
  280.             if(isset($keytouse) && $GPG_ENABLE){
  281.                 $gpg_encrypted = gpg_encrypt($loginID, $host, $keytouse, $message);
  282.             }
  283.             
  284.             /****  smart wrap ****/
  285.             $message = LangSmartWrap($message, 74);
  286.  
  287.             /****  Encode  ****/
  288.             $subject=stripslashes($subject);
  289.             $subject=LangEncodeSubject($subject, $my_charset);
  290.             
  291.             if (!$gpg_encrypted){
  292.                 $message=stripslashes($message);
  293.                 $part[0]=LangEncodeMessage($message, $my_charset);
  294.             }else{
  295.                 $part[0]["data"] = $message;
  296.             }
  297.             /***********************/
  298.                 
  299.             /****  Pre-process addresses */
  300.             $from = stripslashes($from);
  301.             $to = stripslashes($to);
  302.             
  303.             $to = RemoveDoubleAddresses($to);
  304.             
  305.             echo "To: ".htmlspecialchars($to)." <br>\n";
  306.                 
  307.             $to = LangEncodeAddressList($to, $my_charset);
  308.             $from = LangEncodeAddressList($from, $my_charset);
  309.                     
  310.             if (!empty($cc)){
  311.                 $cc = stripslashes($cc);
  312.                 $cc = str_replace(";",",",$cc);
  313.                 $cc = RemoveDoubleAddresses($cc);
  314.                 $cc = LangEncodeAddressList($cc, $my_charset);
  315.                 
  316.             }
  317.             if (!empty($bcc)){
  318.                 $bcc = stripslashes($bcc);
  319.                 $bcc = str_replace(";",",",$bcc);
  320.                 $bcc = RemoveDoubleAddresses($bcc);
  321.                 $bcc = LangEncodeAddressList($bcc, $my_charset);
  322.             }
  323.             /***********************/
  324.  
  325.                     
  326.             /****  Add Recipients *********/
  327.             //$headerx.="Return-Path: ".$sender_addr."\n";
  328.             $headerx.="From: ".$from."\r\n";
  329.             //$headerx.="Sender: ".$sender_addr."\n";
  330.             $headerx.="Bounce-To: ".$from."\r\n";
  331.             $headerx.="Errors-To: ".$from."\r\n";
  332.             if (!empty($reply_to)) $headerx.="Reply-To: ".stripslashes($reply_to)."\r\n";
  333.             if ($cc){
  334.                 $headerx.="CC: ". stripslashes($cc)."\r\n";
  335.             }
  336.             if ($bcc && !$SMTP_SERVER){
  337.                 //add bcc to header only if sending through PHP's mail() => i.e. no SMTP_SERVER specified
  338.                 $headerx.="BCC: ".stripslashes($bcc)."\r\n";
  339.             }
  340.             /************************/
  341.                 
  342.             /****  Prepare attachments *****/
  343.             echo "Attachments: $attachments <br>\n";
  344.             if (file_exists(realpath($uploadDir))){
  345.                 if (is_array($attach)){
  346.                     while ( list($file, $v) = each($attach) ){
  347.                         if ($v==1){
  348.                             //split up file name
  349.                             $file_parts = explode(".", $file);
  350.                             
  351.                             //put together full path
  352.                             $a_path = $uploadDir."/".$file;
  353.  
  354.                             //get name and type
  355.                             $a_name=base64_decode($file_parts[1]);
  356.                             $a_type=strtolower(base64_decode($file_parts[2]));
  357.                             if ($a_type=="") $a_type="application/octet-stream";                                
  358.  
  359.                             //if data is good...
  360.                             if (($file_parts[0]==$user) && (file_exists(realpath($a_path)))){
  361.                                 echo "Attachment $i is good <br>\n";
  362.                                 $num_parts++;            
  363.                                 
  364.                                 //stick it in conent array
  365.                                 $part[$num_parts]["type"]="Content-Type: ".$a_type."; name=\"".$a_name."\"\r\n";
  366.                                 $part[$num_parts]["disposition"]="Content-Disposition: attachment; filename=\"".$a_name."\"\r\n";
  367.                                 $part[$num_parts]["encoding"]="Content-Transfer-Encoding: base64\r\n";
  368.                                 $part[$num_parts]["size"] = filesize($a_path);
  369.                                 $attachment_size += $part[$num_parts]["size"];
  370.                                 $part[$num_parts]["path"] = $a_path;
  371.                             }else if (strpos($file_parts[0], "fwd-")!==false){
  372.                             //forward an attachment
  373.                                 //extract specs of attachment
  374.                                 $fwd_specs = explode("-", $file_parts[0]);
  375.                                 $fwd_folder = base64_decode($fwd_specs[1]);
  376.                                 $fwd_id = $fwd_specs[2];
  377.                                 $fwd_part = base64_decode($fwd_specs[3]);
  378.                                 
  379.                                 //get attachment content
  380.                                 $fwd_content = iil_C_FetchPartBody($conn, $fwd_folder, $fwd_id, $fwd_part);
  381.  
  382.                                 //get attachment header
  383.                                 $fwd_header = iil_C_FetchPartHeader($conn, $fwd_folder, $fwd_id, $fwd_part);
  384.                                 
  385.                                 //extract "content-transfer-encoding field
  386.                                 $head_a = explode("\n", $fwd_header);
  387.                                 if (is_array($head_a)){
  388.                                     while ( list($k,$head_line)=each($head_a) ){
  389.                                         $head_line = chop($head_line);
  390.                                         if (strlen($head_line)>15){
  391.                                             list($head_field,$head_val)=explode(":", $head_line);
  392.                                             if (strcasecmp($head_field, "content-transfer-encoding")==0){
  393.                                                 $fwd_encoding = trim($head_val);
  394.                                                 echo $head_field.": ".$head_val."<br>\n";
  395.                                             }
  396.                                         }
  397.                                     }
  398.                                 }
  399.                                     
  400.                                 //create file in uploads dir
  401.                                 $file = $user.".".$file_parts[1].".".$file_parts[2].".".$file_parts[3];
  402.                                 $a_path = $uploadDir."/".$file;
  403.                                 $fp = fopen($a_path, "w");
  404.                                 if ($fp){
  405.                                     fputs($fp, $fwd_content);
  406.                                     fclose($fp);
  407.                                 }else{
  408.                                     echo "Error when saving fwd att to $a_path <br>\n";
  409.                                 }
  410.                                 $fwd_content = "";
  411.                                     
  412.                                 echo "Attachment $i is a forward <br>\n";
  413.                                 $num_parts++;
  414.  
  415.                                 //stick it in conent array
  416.                                 $part[$num_parts]["type"]="Content-Type: ".$a_type."; name=\"".$a_name."\"\r\n";
  417.                                 $part[$num_parts]["disposition"]="Content-Disposition: attachment; filename=\"".$a_name."\"\r\n";
  418.                                 if (!empty($fwd_encoding)) $part[$num_parts]["encoding"] = "Content-Transfer-Encoding: $fwd_encoding\r\n";
  419.                                 $part[$num_parts]["size"] = filesize($a_path);
  420.                                 $attachment_size += $part[$num_parts]["size"];
  421.                                 $part[$num_parts]["path"] = $a_path;
  422.                                 $part[$num_parts]["encoded"] = true;
  423.                                 
  424.                             }
  425.                         }
  426.                     }
  427.                 }
  428.             }
  429.  
  430.             
  431.             /**** Put together MIME message *****/
  432.             echo "Num parts: $num_parts <br>\n";
  433.             
  434.             $received_header = "Received: from ".$_SERVER["REMOTE_ADDR"]." (auth. user $loginID@$host)\r\n";
  435.             $received_header.= "          by ".$_SERVER["SERVER_NAME"]." with HTTP; ".TZDate($my_prefs["timezone"])."\r\n";
  436.             $headerx = $received_header."To: ".$to."\r\n".(!empty($subject)?"Subject: ".$subject."\r\n":"").$headerx;
  437.             
  438.             if ($gpg_encrypted){
  439.                 //OpenPGP Compliance.  See RFC2015
  440.                 //create boundary
  441.                 $tempID = ereg_replace("[/]","",$loginID).time();
  442.                 $boundary="RWP_PART_".$tempID;
  443.  
  444.                 //message header...
  445.                 $headerx.="Mime-Version: 1.0\r\n";
  446.                 $headerx.="Content-Type: multipart/encrypted; boundary=$boundary;\r\n";
  447.                 $headerx.="        protocol=\"application/pgp-encrypted\"\r\n";
  448.  
  449.                 $body = "--".$boundary."\r\n";
  450.                 $body.= "Content-Type: application/pgp-encrypted\r\n\r\n";
  451.                 $body.= "Version: 1\r\n\r\n";
  452.                 
  453.                 $body.= "--".$boundary."\r\n";
  454.                 $body.= "Content-Type: application/octet-stream\r\n\r\n";
  455.                 $body.= $part[0]["data"];
  456.                 $body.= "\r\n";
  457.                 
  458.                 $body.= "--".$boundary."--\r\n";
  459.                 
  460.                 $message = $headerx."\r\n".$body;
  461.                 $is_file = false;
  462.             }else if ($num_parts==0){
  463.                 //simple message, just store as string
  464.                 $headerx.="MIME-Version: 1.0 \r\n";
  465.                 $headerx.=$part[0]["type"];
  466.                 if (!empty($part[0]["encoding"])) $headerx.=$part[0]["encoding"];
  467.                 $body=$part[0]["data"];
  468.                 
  469.                 $message = $headerx."\r\n".$body;
  470.                 $is_file = false;
  471.             }else{
  472.                 //for multipart message, we'll assemble it and dump it into a file
  473.                 
  474.                 echo "Uploads directory: $uploadDir <br>\n";
  475.                 if (file_exists(realpath($uploadDir))){
  476.                     $tempID = ereg_replace("[/]","",$loginID).time();
  477.                     $boundary="RWP_PART_".$tempID;
  478.                     
  479.  
  480.                     $temp_file = $uploadDir."/".$tempID;
  481.                     echo "Temp file: $temp_file <br>\n";
  482.                     $temp_fp = fopen($temp_file, "w");
  483.                     if ($temp_fp){
  484.                         //setup header
  485.                         $headerx.="MIME-Version: 1.0 \r\n";
  486.                         $headerx.="Content-Type: multipart/mixed; boundary=\"$boundary\"\r\n"; 
  487.  
  488.                         //write header to temp file
  489.                         fputs($temp_fp, $headerx."\r\n");
  490.                     
  491.                         //write main body
  492.                         fputs($temp_fp, "This message is in MIME format.\n");
  493.             
  494.                         //loop through attachments
  495.                         for ($i=0;$i<=$num_parts;$i++){
  496.                             //write boundary
  497.                             fputs($temp_fp, "\n--".$boundary."\n");
  498.                             
  499.                             //form part header
  500.                             $part_header = "";
  501.                             if ($part[$i]["type"]!="") $part_header .= $part[$i]["type"];
  502.                             if ($part[$i]["encoding"]!="") $part_header .= $part[$i]["encoding"];
  503.                             if ($part[$i]["disposition"]!="") $part_header .= $part[$i]["disposition"];
  504.                             
  505.                             //write part header
  506.                             fputs($temp_fp, $part_header."\n");
  507.                                 
  508.                             //open uploaded attachment
  509.                             $ul_fp = false;
  510.                             if ((!empty($part[$i]["path"])) && (file_exists(realpath($part[$i]["path"])))){
  511.                                 $ul_fp = fopen($part[$i]["path"], "rb");
  512.                             }
  513.                             if ($ul_fp){
  514.                                 //transfer data in uploaded file to MIME message
  515.                                 
  516.                                 if ($part[$i]["encoded"]){
  517.                                     //straight transfer if already encoded
  518.                                     while(!feof($ul_fp)){
  519.                                         $line = fgets($ul_fp, 1024);
  520.                                         fputs($temp_fp, $line);
  521.                                     }
  522.                                 }else{
  523.                                     //otherwisee, base64 encode
  524.                                     while(!feof($ul_fp)){
  525.                                         //read 57 bytes at a time
  526.                                         $buffer = fread($ul_fp, 57);
  527.                                         //base 64 encode and write (line len becomes 76 bytes)
  528.                                         fputs($temp_fp, base64_encode($buffer)."\n");
  529.                                     }
  530.                                 }
  531.                                 fclose($ul_fp);
  532.                                 unlink($part[$i]["path"]);
  533.                             }else if (!empty($part[$i]["data"])){
  534.                                 //write message (part is not an attachment)
  535.                                 fputs($temp_fp, $part[$i]["data"]."\n");
  536.                             }
  537.                         }
  538.                         
  539.                         //write closing boundary
  540.                         fputs($temp_fp, "\n--".$boundary."--");
  541.                         
  542.                         //close temp file
  543.                         fclose($temp_fp);
  544.                         
  545.                         $message = $temp_file;
  546.                         $is_file = true;
  547.                     }else{
  548.                         $error .= "Temp file could not be opened: $temp_file <br>\n";
  549.                     }
  550.                 }else{
  551.                     $error .= "Invlalid uploads directory<br>\n";
  552.                 }
  553.             }
  554.             
  555.             /*** Clean up uploads directory ***/
  556.             if (file_exists(realpath($uploadDir))){
  557.                 //open directory
  558.                 if ($handle = opendir($uploadDir)) {
  559.                     //loop through files
  560.                     while (false !== ($file = readdir($handle))) {
  561.                         if ($file != "." && $file != "..") {
  562.                             //split up file name
  563.                             $file_parts = explode(".", $file);
  564.                 
  565.                             if ((count($file_parts)==4) && (strpos($file_parts[0], "fwd-")!==false)){
  566.                                 $path = $uploadDir."/".$file;
  567.                                 unlink($path);
  568.                             }
  569.                         } 
  570.                     }
  571.                     closedir($handle); 
  572.                 }
  573.             }    
  574.             
  575.  
  576.  
  577.             /**** Send message *****/
  578.             if (!empty($error)){
  579.                 echo $error;
  580.                 echo "</body></html>";
  581.                 exit;
  582.             }
  583.             
  584.                     
  585.             echo "Sending...<br>";
  586.  
  587.             $sent = false;
  588.             if (!empty($SMTP_SERVER)){
  589.             //send thru SMTP server using cusotm SMTP library
  590.                 include("../include/smtp.inc");
  591.                 
  592.                 //connect to SMTP server
  593.                 $smtp_conn = smtp_connect($SMTP_SERVER, "25", $loginID, $password);
  594.                 
  595.                 if ($smtp_conn){
  596.                     //generate list of recipients
  597.                     $recipients = $to.", ".$cc.", ".$bcc;
  598.                     $recipient_list = smtp_expand($recipients);
  599.                     echo "Sending to: ".htmlspecialchars(implode(",", $recipient_list))."<br>\n";
  600.                 
  601.                     //send message
  602.                     $sent = smtp_mail($smtp_conn, $from_addr, $recipient_list, $message, $is_file);
  603.                 }else{
  604.                     echo "SMTP connection failed: $smtp_error \n";
  605.                 }
  606.             }else{
  607.             //send using PHP's mail() function
  608.                 include_once("../include/smtp.inc");
  609.                 $to = implode(",", smtp_expand($to));
  610.                 $to = ereg_replace("[<>]", "", $to);
  611.                 echo "Adjusted to: ".htmlspecialchars($to)."<br>";
  612.                 
  613.                 
  614.                 if ($is_file){
  615.                     //open file
  616.                     $fp = fopen($message, "r");
  617.                     
  618.                     //if file opened...
  619.                     if ($fp){
  620.                         //read header
  621.                         $header = "";
  622.                         do{
  623.                             $line = chop(iil_ReadLine($fp, 1024));
  624.  
  625.                             if ((!empty($line))
  626.                                 and (!iil_StartsWith($line, "Subject:"))
  627.                                 and (!iil_StartsWith($line, "To:"))
  628.                                 )
  629.                             {
  630.                                 $header .= $line."\n";
  631.                             }                            
  632.                         }while((!feof($fp)) && (!empty($line)));
  633.                         
  634.                         echo nl2br($header);
  635.                         
  636.                         //read body
  637.                         $body = "";
  638.                         while(!feof($fp)){
  639.                             $body .= chop(fgets($fp, 8192))."\n";
  640.                         }
  641.                         fclose($fp);
  642.                         
  643.                         echo "<br>From: $from_addr <br>\n";
  644.                         
  645.                         //send
  646.                         if (ini_get("safe_mode")=="1")
  647.                             $sent = mail($to,$subject,$body,$header);
  648.                         else
  649.                             $sent = mail($to, $subject, $body, $header, "-f $from_addr");
  650.                     }else{
  651.                         $error .= "Couldn't open temp file $message for reading<br>\n";
  652.                     }
  653.                 }else{
  654.                     //take out unnecessary header fields
  655.                     $header_a = explode("\n", $headerx);
  656.                     $header_a[2] = "X-IlohaMail-Blah: ".$sender_addr;
  657.                     $header_a[3] = "X-IlohaMail-Method: mail() [mem]";
  658.                     $header_a[4] = "X-IlohaMail-Dummy: moo";
  659.  
  660.                     reset($header_a);
  661.                     while ( list($k,$line) = each($header_a) ) $header_a[$k] = chop($line);
  662.  
  663.                     $headerx = implode("\n", $header_a);
  664.                     $body = str_replace("\r", "", $body);
  665.                     
  666.                     echo "<br>From: $from_addr <br>\n";
  667.  
  668.                     //send
  669.                     if (ini_get("safe_mode")=="1")
  670.                         $sent = mail($to,$subject,$body,$headerx);
  671.                     else
  672.                         $sent = mail($to, $subject, $body, $headerx, "-f $from_addr");
  673.                 }
  674.             }
  675.             
  676.             //send!!
  677.             if ($sent){
  678.                 echo "Sent!<br>"; flush();
  679.                 $error = "";
  680.                 
  681.                 //save in send folder
  682.                 flush();
  683.                 if ($my_prefs["save_sent"]==1){
  684.                     echo "Moving to send folder...";
  685.                     if ($is_file) $saved = iil_C_AppendFromFile($conn, $my_prefs["sent_box_name"], $message);
  686.                     else $saved = iil_C_Append($conn, $my_prefs["sent_box_name"], $message);
  687.                     if (!$saved) $error .= "Couldn't save:".$conn->error."<br>\n";
  688.                     else echo "done.<br>\n";
  689.                 }
  690.                 
  691.                 //delete temp file, if necessary
  692.                 if ($is_file) unlink($message);
  693.                 
  694.                 //if replying, flag original message
  695.                 if (isset($in_reply_to)) $reply_id = $in_reply_to;
  696.                 else if (isset($forward_of)) $reply_id = $forward_of;
  697.                 if (($ICL_CAPABILITY["flags"]) && (isset($reply_id))){
  698.                     $pos = strrpos($reply_id, ":");
  699.                     $reply_uid = substr($reply_id, $pos+1);
  700.                     $reply_folder = substr($reply_id, 0, $pos);
  701.                     $reply_num = iil_C_UID2ID($conn, $reply_folder, $reply_uid);
  702.                     
  703.                     if ($reply_num !== false){
  704.                         if (iil_C_Flag($conn, $reply_folder, $reply_num, "ANSWERED") < 1){
  705.                             echo "Flagging failed:".$conn->error." ()<br>\n";
  706.                         }
  707.                     }else{
  708.                         echo "UID -> ID conversion failed.<br>\n";
  709.                     }
  710.                 }
  711.                 
  712.                 //update spam-prevention related records
  713.                 include("../include/as_update.inc");
  714.  
  715.                 if ((empty($error))&&($my_prefs["closeAfterSend"]==1)){
  716.                     //clean up uploads dir
  717.                     $uploadDir = $UPLOAD_DIR.ereg_replace("[\\/]", "", $loginID.".".$host);
  718.  
  719.                     if (file_exists(realpath($uploadDir))){
  720.                         if ($handle = opendir($uploadDir)) {
  721.                             while (false !== ($file = readdir($handle))) { 
  722.                                 if ($file != "." && $file != "..") { 
  723.                                     $file_path = $uploadDir."/".$file;
  724.                                     unlink($file_path);
  725.                                 } 
  726.                             }
  727.                             closedir($handle); 
  728.                         }
  729.                     }    
  730.                     
  731.                     //close window
  732.                     echo "<p>Message successfully sent.";
  733.                     echo "\n<script type=\"text/javascript\">\n";
  734.                     echo "   DoCloseWindow(\"main.php?user=$user&folder=".(empty($folder)?"INBOX":urlencode($folder))."\");\n";
  735.                     echo "</script>\n";
  736.                     echo "<br><br>"; flush();
  737.                 }else{
  738.                     echo $error;
  739.                 }
  740.             }else{
  741.                 echo "<p><font color=\"red\">Send FAILED</font><br>$smtp_errornum : ".nl2br($smtp_error);
  742.             }
  743.  
  744.             iil_Close($conn); 
  745.             exit;
  746.         }
  747.     iil_Close($conn);
  748.     }
  749. }
  750.  
  751.  
  752. /****
  753.     HANDLE UPLOADED FILE
  754. ****/
  755. function upload(){}
  756. if (isset($upload)){
  757.     if (($userfile)&&($userfile!="none")){
  758.         $i=$attachments;
  759.         $newfile = $user.".".base64_encode($userfile_name).".".base64_encode($userfile_type).".".base64_encode($userfile_size);
  760.         $newpath=$uploadDir."/".$newfile;
  761.         if (move_uploaded_file($userfile, $newpath)){
  762.             $attach[$newfile] = 1;
  763.         }else{
  764.             echo $userfile_name." : ".$composeErrors[3];
  765.         }
  766.     }else{
  767.         echo $composeErrors[4];
  768.     }
  769. }
  770.  
  771.  
  772. /****
  773.     FETCH LIST OF UPLOADED FILES
  774. ****/
  775. function fetchUploads(){}
  776. if (file_exists(realpath($uploadDir))){
  777.     //open directory
  778.     if ($handle = opendir($uploadDir)) {
  779.         //loop through files
  780.         while (false !== ($file = readdir($handle))) {
  781.             if ($file != "." && $file != "..") {
  782.                 //split up file name
  783.                 $file_parts = explode(".", $file);
  784.                 
  785.                 //make sure first part is session ID, and add to list
  786.                 if ((strcmp($file_parts[0], $user)==0)||(strpos($file_parts[0], "fwd-")!==false))
  787.                     $uploaded_files[] = $file;
  788.             } 
  789.         }
  790.         closedir($handle); 
  791.     }
  792. }
  793. if (is_array($fwd_att_list)){
  794.     reset($fwd_att_list);
  795.     while ( list($file, $v) = each($fwd_att_list) ){
  796.         $uploaded_files[] = $file;
  797.     }
  798. }
  799.  
  800.  
  801. /****
  802.     REPLYING OR FORWARDING
  803. ****/
  804. function replyOrForward(){}
  805. if ((isset($replyto)) || (isset($forward))){
  806.     // if REPLY, or FORWARD
  807.     if ((isset($folder))&&(isset($id))){        
  808.         include_once("../include/mime.inc");
  809.         
  810.         //connect
  811.         $conn = iil_Connect($host, $loginID, $password, $AUTH_MODE);
  812.  
  813.         //get message
  814.         $header=iil_C_FetchHeader($conn, $folder, $id);
  815.  
  816.         //check IMAP UID, if set
  817.         if (($uid > 0) && ($header->uid!=$uid)){
  818.             $temp_id = iil_C_UID2ID($conn, $folder, $uid);
  819.             if ($temp_id) $header=iil_C_FetchHeader($conn, $folder, $temp_id);
  820.             else{
  821.                 "UID - MID mismatch:  UID $uid not found.  Original message no longer exists in $folder <br>\n";
  822.                 exit;
  823.             }
  824.         }else{
  825.             //echo "UID matched:  $uid <br>\n";
  826.         }
  827.  
  828.         $structure_str=iil_C_FetchStructureString($conn, $folder, $id);
  829.         $structure=iml_GetRawStructureArray($structure_str);
  830.         
  831.         $subject=LangDecodeSubject($header->subject, $my_prefs["charset"]);
  832.         $lookfor=(isset($replyto)?"Re:":"Fwd:");
  833.         $pos = strpos ($subject, $lookfor);
  834.         if ($pos===false) {
  835.             $pos = strpos ($subject, strtoupper($lookfor));
  836.             if ($pos===false) {
  837.                 $subject=$lookfor." ".$subject;
  838.             }
  839.         }
  840.         
  841.         //get messageID
  842.         $replyto_messageID = $header->messageID;
  843.         
  844.         //get "from";
  845.         $from = $header->from;
  846.         //replace to "reply-to" if specified
  847.         if ($replyto){
  848.             $to = $from;
  849.             if (!empty($header->replyto)) $to = $header->replyto;
  850.         }
  851.         if ($replyto_all){
  852.             if (!empty($header->to)) $to .= (empty($to)?"":", ").$header->to;
  853.             if (!empty($header->cc)) $cc .= (empty($cc)?"":", ").$header->cc;
  854.         }
  855.         
  856.         //mime decode "to," "cc," and "from" fields
  857.         if (isset($to)){
  858.             $to_a = LangParseAddressList($to);
  859.             $to = "";
  860.             while ( list($k, $v) = each($to_a) ){
  861.                 //remove user's own address from "to" list
  862.                 if ((stristr($to_a[$k]["address"], $from_addr) === false) and
  863.                      (stristr($to_a[$k]["address"], $loginID."@".$host) === false) and
  864.                     ($my_prefs["email_address"] != $to_a[$k]["address"])){
  865.                     $to .= (empty($to)?"":", ")."\"".LangDecodeSubject($to_a[$k]["name"], $my_prefs["charset"])."\" <".$to_a[$k]["address"].">";
  866.                 }
  867.             }
  868.             $to = RemoveDoubleAddresses($to);
  869.         }
  870.         if (isset($cc)){
  871.             echo "<!-- $cc //-->\n";
  872.  
  873.             $cc_a = LangParseAddressList($cc);
  874.             $cc = "";
  875.             while ( list($k, $v) = each($cc_a) ){
  876.                 echo "<!-- CC: ".$cc_a[$k]["address"]." //-->\n";
  877.                 //remove user's own address from "cc" list
  878.                 if ((stristr($cc_a[$k]["address"], $from_addr) === false) and
  879.                      (stristr($cc_a[$k]["address"], $loginID."@".$host) === false) and
  880.                     ($my_prefs["email_address"] != $cc_a[$k]["address"])){
  881.                     echo "<!-- adding: ".$cc_a[$k]["address"]." //-->\n";
  882.                     $cc .= (empty($cc)?"":", ")."\"".LangDecodeSubject($cc_a[$k]["name"], $my_prefs["charset"])."\" <".$cc_a[$k]["address"].">";
  883.                 }
  884.             }
  885.         }
  886.         
  887.         $from_a = LangParseAddressList($from);
  888.         $from = "\"".LangDecodeSubject($from_a[0]["name"], $my_prefs["charset"])."\" <".$from_a[0]["address"].">";
  889.         
  890.         //format headers for reply/forward
  891.         if (isset($replyto)){
  892.             $message_head = $composeStrings[9];
  893.             $message_head = str_replace("%d", LangFormatDate($header->timestamp, $lang_datetime["prevyears"]), $message_head);
  894.             $message_head = str_replace("%s", $from, $message_head);
  895.         }else if (isset($forward)){
  896.             if ($show_header){
  897.                 $message_head = iil_C_FetchPartHeader($conn, $folder, $id, 0);
  898.             }else{
  899.                 $message_head = $composeStrings[10];
  900.                 $message_head .= $composeHStrings[5].": ".ShowDate2($header->date,"","short")."\n";
  901.                 $message_head .= $composeHStrings[1].": ". LangDecodeSubject($from, $my_prefs["charset"])."\n";
  902.                 $message_head .= $composeHStrings[0].": ".LangDecodeSubject($header->subject, $my_prefs["charset"])."\n\n";
  903.             }
  904.         }
  905.         if (!empty($message_head)) $message_head = "\n".$message_head."\n";
  906.         
  907.         //get message attachments
  908.         if ($forward){
  909.             $att_list = iml_GetPartList($structure, "");
  910.             while ( list($i,$v) = each($att_list) ){
  911.                 if ((strcasecmp($att_list[$i]["disposition"], "inline")==0)
  912.                     or (strcasecmp($att_list[$i]["disposition"], "attachment")==0)
  913.                     or (!empty($att_list[$i]["name"]))){
  914.                     $file = "fwd-".base64_encode($folder)."-$id-".base64_encode($i);
  915.                     $file .= ".".base64_encode($att_list[$i]["name"]);
  916.                     $file .= ".".base64_encode($att_list[$i]["typestring"]);
  917.                     $file .= ".".base64_encode($att_list[$i]["size"]);
  918.                     if (!$fwd_att_list[$file]){
  919.                         $uploaded_files[] = $file;
  920.                         $fwd_att_list[$file] = 1;
  921.                         $attach[$file] = 1;
  922.                     }
  923.                 }
  924.             }
  925.         }
  926.  
  927.         //get message
  928.         if (!empty($part)) $part.=".1";
  929.         else{
  930.             $part = iml_GetFirstTextPart($structure, "");
  931.         }
  932.                 
  933.         $message=iil_C_FetchPartBody($conn, $folder, $id, $part);
  934.                 
  935.         //decode message if necessary
  936.         $encoding=iml_GetPartEncodingCode($structure, $part);        
  937.         if ($encoding==3) $message = base64_decode($message);
  938.         else if ($encoding==4){
  939.             //if ($encoding == 3 ) $message = base64_decode($message);
  940.             //else if ($encoding == 4) $message = quoted_printable_decode($message);
  941.             //$message = quoted_printable_decode($message);
  942.             $message = str_replace("=\n", "", $message);
  943.             $message = quoted_printable_decode(str_replace("=\r\n", "", $message));
  944.         }
  945.         
  946.         //add quote marks
  947.         $message = str_replace("\r", "", $message);
  948.         $charset=iml_GetPartCharset($structure, $part);
  949.  
  950.  
  951.         $message=LangConvert($message, $my_prefs["charset"], $charset);
  952.         if (isset($replyto)) $message=">".str_replace("\n","\n>",$message);
  953.         $message = "\n".LangConvert($message_head, $my_prefs["charset"], $charset).$message;
  954.         
  955.         iil_Close($conn);            
  956.     }
  957. }else{
  958.     $message = stripslashes($message);
  959. }
  960.  
  961. function showForm(){}
  962.  
  963. if (($show_contacts) || ($my_prefs["showContacts"])) {
  964. ?>
  965. <?php
  966. }
  967. ?>
  968.  
  969. <FORM NAME="messageform" ENCTYPE="multipart/form-data" ACTION="compose2.php" METHOD="POST" onSubmit='DeselectAdresses(); close_popup(); return true;'>
  970.     <input type="hidden" name="user" value="<?php echo $user?>">
  971.     <input type="hidden" name="show_contacts" value="<?php echo $show_contacts?>">
  972.     <input type="hidden" name="show_cc" value="<?php echo $show_cc?>">
  973.     <?php
  974.         if ($no_subject) echo '<input type="hidden" name="confirm_no_subject" value="1">';
  975.     
  976.         if (($replyto) || ($in_reply_to)){
  977.             if (empty($in_reply_to)) $in_reply_to = $folder.":".$uid;
  978.             echo "<input type=\"hidden\" name=\"in_reply_to\" value=\"$in_reply_to\">\n";
  979.             echo "<input type=\"hidden\" name=\"replyto_messageID\" value=\"$replyto_messageID\">\n";
  980.         }else if (($forward) || ($forward_of)){
  981.             if (empty($forward_of)) $forward_of = $folder.":".$uid;
  982.             echo "<input type=\"hidden\" name=\"forward_of\" value=\"$forward_of\">\n";
  983.         }
  984.         
  985.         if (is_array($fwd_att_list)){
  986.             reset($fwd_att_list);
  987.             while ( list($file,$v) = each($fwd_att_list)){
  988.                 echo "<input type=\"hidden\" name=\"fwd_att_list[".$file."]\" value=\"1\">\n";
  989.             }
  990.         }
  991.         if (!empty($folder)){
  992.             echo '<input type="hidden" name="folder" value="'.$folder.'">';
  993.         }
  994.     ?>
  995.     
  996.     <table border="0" width="100%" bgcolor="<?php echo $my_colors["main_head_bg"]?>">
  997.     <tr>
  998.         <td valign="bottom" align="left">
  999.             <span class="bigTitle"><?php echo $composeStrings[0]; ?></span>
  1000.                
  1001.             <span class="mainHeadingSmall">
  1002.             <?php
  1003.             if (!$my_prefs["compose_inside"]){
  1004.                 $jsclose="[<a href=\"javascript:window.close();\" class=\"mainHeadingSmall\">".$composeStrings[11]."</a>]";
  1005.                 echo "<SCRIPT type=\"text/javascript\" language=\"JavaScript1.2\">\n";
  1006.                 echo "document.write('$jsclose');\n</SCRIPT>";
  1007.             }
  1008.             ?>
  1009.             </span>
  1010.         </td>
  1011.         <td valign="bottom" align="right">
  1012.                         
  1013.         </td>
  1014.     </tr>
  1015.     </table>
  1016.     <?php
  1017.     if (!empty($error)) echo '<br><font color="red">'.$error.'</font>';
  1018.         $to = encodeUTFSafeHTML($to);
  1019.         $cc = encodeUTFSafeHTML($cc);
  1020.         $bcc = encodeUTFSafeHTML($bcc);
  1021.         
  1022.         // format sender's email address (i.e. "from" string)
  1023.             $email_address = htmlspecialchars($original_from);
  1024.  
  1025.         // External table
  1026.         echo "<table border=0><tr valign=\"top\"><td>\n";
  1027.         
  1028.         echo "<table border=0>";
  1029.         //echo "<table width=\"100%\" border=\"0\" cellspacing=\"1\" cellpadding=\"1\" bgcolor=\"".$my_colors["main_bg"]."\">\n";
  1030.         echo "<tr>";
  1031.         echo "<td align=right class=\"mainLight\">".$composeHStrings[0].":</td><td class=\"mainLight\">";
  1032.         echo '<input type=text name="subject" value="'.encodeUTFSafeHTML(stripslashes($subject)).'" size="60" onKeyUp="fixtitle(\''.$composeStrings["title"].'\');"></td>';
  1033.         echo "</td></tr>\n";
  1034.         echo "<tr>";
  1035.         echo "<td align=right class=\"mainLight\">".$composeHStrings[1].":</td><td class=\"mainLight\">";
  1036.         if (($alt_identities) && (count($alt_identities)>0)){
  1037.             echo "<select name=\"sender_identity_id\">\n";
  1038.                 echo "<option value=\"-1\">".LangDecodeSubject($email_address, $my_prefs["charset"])."\n";
  1039.             while ( list($key,$ident_a) = each($alt_identities) ){
  1040.                 if ($ident_a["name"]!=$my_prefs["user_name"] || $ident_a["email"]!=$my_prefs["email_address"]){
  1041.                     echo "<option value=\"$key\" ".($key==$sender_identity_id?"SELECTED":"").">";
  1042.                     echo "\"".$ident_a["name"]."\"  <".$ident_a["email"].">\n";
  1043.                 }
  1044.             }
  1045.             echo "</select>\n";
  1046.         }else{
  1047.             echo LangDecodeSubject($email_address, $my_prefs["charset"]);
  1048.         }
  1049.         echo "</td></tr>\n";
  1050.   
  1051.         //if (($show_contacts) || ($my_prefs["showContacts"])){
  1052.         if ($show_contacts){
  1053.             echo "<tr>\n<td align=right valign=top>";
  1054.             echo "<select name=\"to_a_field\">\n";
  1055.             echo "<option value=\"to\">".$composeHStrings[2].":\n";
  1056.             echo "<option value=\"cc\">".$composeHStrings[3].":\n";
  1057.             echo "<option value=\"bcc\">".$composeHStrings[4].":\n";
  1058.             echo "</select>\n";
  1059.             echo"</td><td>";
  1060.         
  1061.             // display "select" box with contacts
  1062.             include_once("../include/data_manager.inc");
  1063.             $source_name = $DB_CONTACTS_TABLE;
  1064.             if (empty($source_name)) $source_name = "contacts";
  1065.             $dm = new DataManager_obj;
  1066.             if ($dm->initialize($loginID, $host, $source_name, $DB_TYPE)){
  1067.                 if (empty($sort_field)) $sort_field = "grp,name";
  1068.                 if (empty($sort_order)) $sort_order = "ASC";
  1069.                 $contacts = $dm->sort($sort_field, $sort_order);
  1070.             }else{
  1071.                 echo "Data Manager initialization failed:<br>\n";
  1072.                 $dm->showError();
  1073.             }
  1074.  
  1075.             if ((is_array($contacts)) && (count($contacts) > 0)){
  1076.                 echo "<select name=\"to_a[]\" MULTIPLE SIZE=7 onDblClick='CopyAdresses(); return true;'>\n";
  1077.                 while ( list($key, $foobar) = each($contacts) ){
  1078.                     $contact = $contacts[$key];
  1079.                     if (!empty($contact["email"])){
  1080.                         $line = "\"".$contact["name"]."\" <".$contact["email"].">";
  1081.                         echo "<option>".htmlspecialchars($line)."\n";
  1082.                     }
  1083.                     if (!empty($contact["email2"])){
  1084.                         $line = "\"".$contact["name"]."\" <".$contact["email2"].">";
  1085.                         echo "<option>".htmlspecialchars($line)."\n";
  1086.                     }
  1087.                 }
  1088.                 echo "</select>"; 
  1089.                 
  1090.                 echo "<script type=\"text/javascript\" language=\"JavaScript1.2\">";
  1091.                 echo "document.write('<input type=\"button\" name=\"add_contacts\" value=\"".$composeStrings[8]."\" onClick=\"CopyAdresses()\">');\n";
  1092.                 echo "</script>\n";
  1093.                 echo "<noscript><input type=\"submit\" name=\"add_contacts\" value=\"".$composeStrings[8]."\"><br></noscript>\n";
  1094.                 if ($my_prefs["showContacts"]!=1){
  1095.                     echo "<input type=\"hidden\" name=\"new_show_contacts\" value=0>\n";
  1096.                     echo "<input type=\"submit\" name=\"change_contacts\" value=\"".$composeStrings[6]."\">\n";
  1097.                 }
  1098.             }
  1099.             echo "</td></tr>\n";
  1100.             $contacts_shown = true;
  1101.         }else{
  1102.             $contacts_shown = false;
  1103.         }
  1104.         
  1105.         // display to field
  1106.         echo "<tr>\n<td align=right class=\"mainLight\">".$composeHStrings[2].":</td><td>";
  1107.         if (strlen($to) < 60)
  1108.             echo "<input type=text name=\"to\" value=\"".stripslashes($to)."\" size=60>";
  1109.         else
  1110.             echo "<textarea name=\"to\" cols=\"60\" rows=\"3\">".stripslashes($to)."</textarea>";
  1111.         if (!$contacts_shown){
  1112.             //"show contacts" button
  1113.             echo "<input type=\"hidden\" name=\"new_show_contacts\" value=1>\n";
  1114.             $popup_url = "contacts_popup.php?user=$user";
  1115.             $showcon_link = "<a href=\"javascript:open_popup('$popup_url')\" class=\"mainLight\">";
  1116.             $showcon_link.= "<img src=\"themes/".$my_prefs["theme"]."/images/addc.gif\">".$composeStrings[5]."</a>";
  1117.             $showcon_link = addslashes($showcon_link);
  1118.             echo "<script type=\"text/javascript\" language=\"JavaScript1.2\">\n";
  1119.             echo "document.write('$showcon_link');\n";
  1120.             echo "</script>\n";
  1121.             echo "<noscript>\n<input type=\"submit\" name=\"change_contacts\" value=\"".$composeStrings[5]."\">\n</noscript>\n";
  1122.             //echo "<input type=\"submit\" name=\"change_contacts\" value=\"".$composeStrings[5]."\">\n";
  1123.         }
  1124.         echo "</td></tr>\n";
  1125.         
  1126.         if ((!empty($cc)) || ($my_prefs["showCC"]==1) || ($show_cc)){
  1127.             // display cc box
  1128.             echo "<tr>\n<td align=right class=\"mainLight\">".$composeHStrings[3].":</td><td>";
  1129.             if (strlen($cc) < 60)
  1130.                 echo "<input type=text name=\"cc\" value=\"".stripslashes($cc)."\" size=60>";
  1131.             else
  1132.                 echo "<textarea name=\"cc\" cols=\"60\" rows=\"3\">".stripslashes($cc)."</textarea>";
  1133.             echo "</td></tr>\n";
  1134.             
  1135.             $cc_field_shown = true;
  1136.         }else{
  1137.             $cc_field_shown = false;
  1138.         }
  1139.         
  1140.         if ((!empty($bcc)) || ($my_prefs["showCC"]==1) || ($show_cc)){
  1141.             // display bcc box
  1142.             echo "<tr>\n<td align=right class=\"mainLight\">".$composeHStrings[4].":</td><td>";
  1143.             if (strlen($bcc) < 60)
  1144.                 echo "<input type=text name=\"bcc\" value=\"".stripslashes($bcc)."\" size=60>";
  1145.             else
  1146.                 echo "<textarea name=\"bcc\" cols=\"60\" rows=\"3\">".stripslashes($bcc)."</textarea>\n";
  1147.             echo "</td></tr>\n";
  1148.             $bcc_field_shown = true;
  1149.         }else{
  1150.             $bcc_field_shown = false;
  1151.         }
  1152.  
  1153.         //show attachments
  1154.         echo "<tr>";
  1155.         echo "<td align=\"right\" valign=\"top\" class=\"mainLight\">".$composeStrings[4].":</td>";
  1156.         echo "<td valign=\"top\">";
  1157.         if ((is_array($uploaded_files)) && (count($uploaded_files)>0)){
  1158.             //echo "<table>";
  1159.             echo "<table border=\"0\" cellspacing=\"1\" cellpadding=\"1\" bgcolor=\"".$my_colors["main_head_bg"]."\">\n";
  1160.             reset($uploaded_files);
  1161.             while ( list($k,$file) = each($uploaded_files) ){
  1162.                 $file_parts = explode(".", $file);
  1163.                 echo "<tr bgcolor=\"".$my_colors["main_bg"]."\">";
  1164.                 echo "<td valign=\"bottom\"><input type=\"checkbox\" name=\"attach[$file]\" value=\"1\" ".($attach[$file]==1?"CHECKED":"")."></td>";
  1165.                 echo "<td valign=\"bottom\">".base64_decode($file_parts[1])." </td>";
  1166.                 echo "<td valign=\"bottom\" class=\"small\">".base64_decode($file_parts[3])."bytes </td>";
  1167.                 echo "<td valign=\"bottom\" class=\"small\">(".base64_decode($file_parts[2]).")</td>";
  1168.                 echo "</td></tr>\n";
  1169.             }
  1170.             echo "</table>";
  1171.         }
  1172.  
  1173.         if ($MAX_UPLOAD_SIZE) $max_file_size = $MAX_UPLOAD_SIZE;
  1174.         else $max_file_size = ini_get('upload_max_filesize');
  1175.         if (eregi("M$", $max_file_size)) $max_file_size = (int)$max_file_size * 1000000;
  1176.         else if (eregi("K$", $max_file_size)) $max_file_size = (int)$max_file_size * 1000;
  1177.         echo '<input type="hidden" name="MAX_FILE_SIZE" value="'.$max_file_size.'">';
  1178.         echo "<INPUT NAME=\"userfile\" TYPE=\"file\">";
  1179.         echo '<INPUT TYPE="submit" NAME="upload" VALUE="'.$composeStrings[2].'">';
  1180.         echo "</td></tr>\n";
  1181.         
  1182.         echo "<tr>\n<td></td><td>";
  1183.         
  1184.         if ((!$cc_field_shown) || (!$bcc_field_shown)){
  1185.             //"show cc/bcc field" button
  1186.             include("../lang/".$my_prefs["lang"]."prefs.inc");
  1187.             echo '<input type="hidden" name="new_show_cc" value="1">';
  1188.             echo '<input type="submit" name="change_show_cc" value="'.$prefsStrings["6.2"].'">';
  1189.         }else{
  1190.             echo '<input type="hidden" name="new_show_cc" value="'.$show_cc.'">';
  1191.         }
  1192.         echo "</td></tr>\n";
  1193.  
  1194.         echo "</table>";
  1195.  
  1196.     // External table
  1197.     echo "</td><td>\n";
  1198.     echo '<input type=submit name=send value="'.$composeStrings[1].'">';
  1199.     echo "</td></tr><td colspan=2>\n";
  1200.     
  1201.     /***
  1202.         SPELL CHECK
  1203.     ****/
  1204.     if ($check_spelling){
  1205.         include("../include/spellcheck.inc");
  1206.  
  1207.         //run spell check
  1208.         $result = splchk_check($message, $spell_dict_lang);
  1209.         
  1210.         //handle results
  1211.         if ($result){
  1212.             echo "<table><tr bgcolor=\"".$my_colors["main_bg"]."\"><td>\n";
  1213.             $words = $result["words"];
  1214.             $positions = $result["pos"];
  1215.             if (count($positions)>0){
  1216.                 //show errors and possible corrections
  1217.                 echo "<b>".$composeStrings[15]."</b><br>\n";
  1218.                 echo str_replace("%s", $DICTIONARIES[$spell_dict_lang], $composeErrors[8]);
  1219.  
  1220.                 $splstr["ignore"] = $composeStrings[17];
  1221.                 $splstr["delete"] = $composeStrings[18];
  1222.                 $splstr["correct"] = $composeStrings[13];
  1223.                 $splstr["nochange"] = $composeStrings[14];
  1224.                 $splstr["formname"] = "messageform";
  1225.             
  1226.                 splchk_showform($positions, $words, $splstr);
  1227.             }else{
  1228.                 //show "no changes needed"
  1229.                 echo $composeErrors[6].str_replace("%s", $DICTIONARIES[$spell_dict_lang], $composeErrors[8]);
  1230.             }
  1231.             echo "</td></tr></table>\n";
  1232.             
  1233.         }else{
  1234.             echo $composeErrors[7];
  1235.         }
  1236.     }else if ($correct_spelling){
  1237.         //correct spelling
  1238.         include("../include/spellcheck.inc");
  1239.  
  1240.         //do some shifting here...
  1241.         while (list($num,$word)=each($words)){
  1242.             $correct_var = "correct".$num;
  1243.             $correct[$num] = $$correct_var;
  1244.         }
  1245.         
  1246.         echo "<table><tr bgcolor=\"".$my_colors["main_bg"]."\"><td>\n";
  1247.         echo "<b>".$composeStrings[16]."</b><br>\n";
  1248.  
  1249.         //do the actual corrections
  1250.         $message = splchk_correct($message, $words, $offsets, $suggestions, $correct);
  1251.  
  1252.         echo "</td></tr></table>\n";
  1253.     }
  1254.     
  1255.     /***
  1256.         SHOW TEXT BOX
  1257.     ***/
  1258.     ?>
  1259.     <br><span class="mainLight"><?php echo $composeStrings[7]?></span><br>
  1260.     <TEXTAREA NAME=message ROWS=20 COLS=77 WRAP=virtual><?php echo "\n".encodeUTFSafeHTML($message); ?></TEXTAREA>
  1261.     
  1262.     <?php
  1263.         //spell check controls
  1264.         if (is_array($DICTIONARIES) && count($DICTIONARIES)>0){
  1265.             echo "<br><select name='spell_dict_lang'>\n";
  1266.             reset($DICTIONARIES);
  1267.             while ( list($l,$n)=each($DICTIONARIES) ) echo "<option value='$l'>$n\n";
  1268.             echo "</select>\n";
  1269.             echo '<input type="submit" name="check_spelling" value="'.$composeStrings[12].'">';
  1270.         }
  1271.     ?>
  1272.  
  1273.     <!-- External talbe -->
  1274.     </td><tr><td>
  1275.     
  1276.     
  1277.     <table border=0 width="100%">
  1278.     <?php
  1279.     //GPG stuff
  1280.     if ($GPG_ENABLE){
  1281.     ?>
  1282.         <tr>
  1283.             <span class="mainLight">Whose public key to use? (this feature is still experimental)<br>
  1284.             <?php
  1285.                 $keys = gpg_list_keys();
  1286.                 $options = "";
  1287.                 if (is_array($keys) && count($keys)>0){
  1288.                     while (list($k,$str)=each($keys)){
  1289.                         $options.= "<option value=\"$k\">$str\n";
  1290.                     }
  1291.                 }
  1292.             ?>
  1293.             <select name="keytouse">
  1294.             <option value = "noencode">None</option>
  1295.             <?php echo $options ?>
  1296.             </select>
  1297.             </span>
  1298.         </tr>
  1299.     <?php
  1300.     } //end if $GPG_ENABLE
  1301.     ?>
  1302.     <tr>
  1303.         <td>
  1304.         <input type=checkbox name="attach_sig" value=1 <?php  echo ($my_prefs["show_sig1"]==1?"CHECKED":""); ?> > 
  1305.         <span class="mainLight"><?php  echo $composeStrings[3]; ?></span>
  1306.         </td>
  1307.     </tr>
  1308.     </table>
  1309.  
  1310.     </td><td>
  1311.         <input type=submit name=send value="<?php  echo  $composeStrings[1]; ?>">
  1312.     </td></tr></table><!-- End External table -->
  1313.  
  1314. </form>
  1315.     <script type=text/javascript>
  1316.         var _p = this.parent;
  1317.         if (_p==this){
  1318.             _p.document.title = "<?php echo $composeStrings["title"] ?>";
  1319.         }
  1320.     </script>
  1321. </BODY></HTML>
  1322.