Файл: htdocs/classes/smartmail.class.php
Строк: 292
<?php
/**********
*
* SmartMail 1.0 - The WebMailClient for your SmartPhone
* copyright mac@homac.at
* V 1.0
*
**********/
class SmartMail {
var $auth = false;
var $user;
var $pass;
var $server;
var $port;
var $basefolder = "INBOX";
var $sentfolder = "INBOX.Sent";
var $per_page;
var $connection;
var $error;
var $success;
var $p = 1; //current page
var $m = 0;
var $decode = false;
function SmartMail() {
global $server, $port, $per_page;
$this->server = $server;
$this->port = $port;
$this->per_page = $per_page;
$this->auth = $_SESSION["auth"];
$this->user = $_SESSION["user"];
$this->pass = $_SESSION["pass"];
$p = intval($_REQUEST["p"]);
if ($p) $this->p = $p;
$f = trim($_REQUEST["f"]);
if ($f) $this->basefolder = $f;
}
function login() {
if (!$_REQUEST["user"] || !$_REQUEST["pass"]) {
$this->error = lang("err_nouserpass");
return false;
} else {
//check connection
$this->connect(false,$_REQUEST["user"],$_REQUEST["pass"]);
}
if (!$this->error) {
$this->auth = $_SESSION["auth"] = true;
$this->user = $_SESSION["user"] = $_REQUEST["user"];
$this->pass = $_SESSION["pass"] = $_REQUEST["pass"];
} else return false;
}
function logout() {
session_destroy();
$this->success = lang("succ_logout");
$this->auth = false;
}
function connect($folder=false,$user=false,$pass=false) {
if (!$folder) $folder = $this->basefolder;
if (!$user) $user = $this->user;
if (!$pass) $pass = $this->pass;
$this->connection = @imap_open('{'.$this->server.':'.$this->port.'/novalidate-cert}'.$folder, $user, $pass);
if (!$this->connection) $this->error = lang("err_conn") . "<hr>" . imap_last_error();
}
function messagelist() {
$this->connect();
$messages = array();
$messages["count"] = $this->messcount = imap_num_msg($this->connection);
//some pagecalculations
if (!$this->p) $this->p = 1;
if ((($this->p-1) * $this->per_page) > $this->messcount) $this->p = 1; //if page is too high the page will be set to 1
$starti = $this->messcount - ($this->per_page*($this->p-1));
$endi = $starti - ($this->per_page);
if ($endi < 0) $endi = 0;
$this->pages = ceil($this->messcount/$this->per_page);
for ($i=$starti;$i>$endi;$i--) {
$decode = false;
$cmess = imap_fetch_overview($this->connection,$i);
if ($cmess) {
preg_match_all('/^(.*)<(.*@.*)>$/',$cmess[0]->from,$from_arr);
$cmess[0]->from_name = trim($from_arr[1][0]);
$cmess[0]->from_mail = trim($from_arr[2][0]);
if ($cmess[0]->from_name) $cmess[0]->from = $cmess[0]->from_name;
elseif ($cmess[0]->from_mail) $cmess[0]->from = $cmess[0]->from_mail;
//charset
if (preg_match('/UTF-8/',$cmess[0]->subject)) $decode = "UTF8";
$cmess[0]->subject = $this->messdecode($cmess[0]->subject,$decode);
$cmess[0]->from = $this->messdecode($cmess[0]->from,$decode);
$messages["headers"][] = $cmess;
}
}
return $messages;
}
function folderlist($checkmessages=true) {
$this->connect();
$folderlist = array();
//folderlist
$folders = imap_listmailbox($this->connection, '{'.$this->server.':'.$this->port.'/novalidate-cert}', "*");
for ($i=0;$i<count($folders);$i++) {
$thisfolder = preg_replace('/(^.*}' . $this->basefolder . '.)/',"",$folders[$i]);
if (preg_match('/}/',$thisfolder)) $thisfolder = $this->basefolder;
//messagecount
$checkfolder = ($thisfolder == $this->basefolder ? false : $this->basefolder.".".$thisfolder);
if ($checkmessages) $this->connect($checkfolder);
if ($checkmessages) {
$folderlist[] = array("checkfolder" => $checkfolder,
"name" => $thisfolder,
"messages" => ($checkmessages ? imap_num_msg($this->connection) : "0"));
} else $folderlist[] = $checkfolder;
}
return $folderlist;
}
function drawPagination() {
if ($this->pages < 2) return false; //nothing to page
if ($this->p == 1) {
$output = "<img src='images/icons/pfirst_dis.png' alt='1'>
<img src='images/icons/prwd_dis.png' alt='1'>n";
} else {
$output = "<a href='?p=1&f=" . $this->basefolder . "'><img src='images/icons/pfirst.png' alt='1'></a>
<a href='?p=" . ($this->p - 1) . "&f=" . $this->basefolder . "'><img src='images/icons/prwd.png' alt='1'></a>n";
}
$output .= $this->p . "/" . $this->pages . "n";
if ($this->p == $this->pages) {
$output.= "<img src='images/icons/pfwd_dis.png' alt='" . $this->pages . "'>
<img src='images/icons/plast_dis.png' alt='" . $this->pages . "'>n";
} else {
$output .= "<a href='?p=" . ($this->p + 1) . "&f=" . $this->basefolder . "'><img src='images/icons/pfwd.png' alt='1'></a>
<a href='?p=" . $this->pages . "&f=" . $this->basefolder . "'><img src='images/icons/plast.png' alt='" . $this->pages . "'></a>";
}
return $output;
}
function showmess() {
$this->m = intval($_REQUEST["m"]);
$this->connect($this->basefolder);
//check html
$body = $this->get_part($this->m, "TEXT/HTML");
if (!$body) {
$body = $this->get_part($this->m, "TEXT/PLAIN");
$body = make_clickable($body);
$body= nl2br($body);
$type = "text";
}
//check headerinfos
$headerinfos = imap_headerinfo($this->connection,$this->m);
$header = array("date" => $headerinfos->MailDate,
"subject" => ($headerinfos->subject?$this->messdecode($headerinfos->subject):"No Subject"),
"fromname" => $this->messdecode($headerinfos->from[0]->personal),
"frommail" => $this->messdecode($headerinfos->from[0]->mailbox . "@" . $headerinfos->from[0]->host),
"toaddress" => $this->messdecode($headerinfos->toaddress),
"fromaddress" => $this->messdecode($headerinfos->fromaddress),
"deleted" => ($headerinfos->Deleted == "D" ? true : false),
);
//messcheck
$num = imap_num_msg($this->connection);
if ($this->m == 1) $prev = false;
else $prev = $this->m - 1;
if ($this->m < $num) $next = $this->m + 1;
else $next = false;
$message = array("body" => $body,
"type" => $type,
"header" =>$header,
"next" =>$next,
"prev" =>$prev);
return $message;
}
function messdecode($str,$decode=false) {
if (!$decode) $decode = $this->decode;
if ($decode == "UTF8") return utf8_decode(strDecode($str));
else return strDecode($str);
}
//delete message
function delmess() {
$m = intval($_REQUEST["m"]);
if (!$m) {
$this->error = lang("err_messnotfound");
return false;
}
$this->connect($this->basefolder);
imap_delete($this->connection,$m);
$this->success = lang("succ_delete");
}
//delete message
function undelmess() {
$m = intval($_REQUEST["m"]);
if (!$m) {
$this->error = lang("err_messnotfound");
return false;
}
$this->connect($this->basefolder);
imap_undelete($this->connection,$m,'\DELETED');
echo imap_last_error();
$this->success = lang("succ_undelete");
}
//send message
function sendmail() {
global $subject, $from, $to, $body, $mode;
//errorchecking
if (!$subject) $subject = "No Subject";
if (!$from) $errors[] = lang("err_nofrom");
if (!$to) $errors[] = lang("err_noto");
else {
//split for multiple mailadresses
$toarr = explode(",",$to);
for ($i=0;$i<count($toarr);$i++) {
if (!is_email($toarr[$i])) $errors[] = sprintf(lang("err_wrongmail"),$toarr[$i]);
}
}
if (!$errors) {
//send message
imap_mail($to, $subject, $body, "From: $fromnX-Mailer: PHP SmartMailnMime-Version: 1.0nContent-Type: text/plain; charset=iso-8859-1nContent-Transfer-Encoding: 8bitn");
//flag as replied when replied
if ($mode == "reply" && $this->m) {
//connection from showmess
imap_setflag_full($this->connection, $this->m, "\Answered");
}
//move message to sent folder
if ($this->sentfolder) {
$folders = $this->folderlist(false);
if (in_array($this->sentfolder,$folders)) {
$this->connect($this->sentfolder);
imap_append($this->connection, '{'.$this->server.'}'.$this->sentfolder, "To: $tonFrom: $fromnSubject: $subjectnDate: " . date("r") . "nX-Mailer: PHP SmartMailnMime-Version: 1.0nContent-Type: text/plain; charset=iso-8859-1nContent-Transfer-Encoding: 8bitnn$bodyn");
}
}
$this->success = lang("succ_send");
} else $this->error = "- " . implode("<br />- ",$errors);
}
/** handle message content **/
function messtype($structure) {
if (!$structure) return "TEXT/PLAIN";
else {
$prim = array("TEXT","MULTIPART","MESSAGE","APPLICATION","AUDIO","IMAGE","VIDEO","OTHER"); //http://de3.php.net/manual/de/function.imap-fetchstructure.php
if($structure->subtype) { //ALTERNATIVE, PLAIN, REPORT, HTML, ...
return $prim[intval($structure->type)] . "/" .$structure->subtype;
} else return "TEXT/PLAIN";
}
}
function get_part($m, $mime_type, $structure = false,$part = false) {
if(!$structure) {
$structure = imap_fetchstructure($this->connection, $m);
}
if($structure) {
if($mime_type == $this->messtype($structure)) {
if(!$part) $part = "1"; //key of 1st part
$text = imap_fetchbody($this->connection, $m, $part);
if($structure->encoding == 3) { //http://de3.php.net/manual/de/function.imap-fetchstructure.php
$text = imap_base64($text);
} elseif($structure->encoding == 4) { //http://de3.php.net/manual/de/function.imap-fetchstructure.php
$text = imap_qprint($text);
} else {
$text = $text;
}
//check charset
if (is_array($structure->parameters)) {
if ($structure->parameters[0]->value == "utf-8") {
$this->decode = "UTF8";
$text = utf8_decode($text);
}
}
return $text;
}
if($structure->type == 1) /* multipart */ { //http://de3.php.net/manual/de/function.imap-fetchstructure.php
while(list($key, $sub_structure) = each($structure->parts)) {
if($part) { //enlarge part
$part .= ".";
}
$data = $this->get_part($m, $mime_type, $sub_structure,$part . ($key + 1));
if($data) {
return $data;
}
}
}
}
return false;
}
}
?>