Location: PHPKode > projects > CsWebmail > cswebmail-3.10/cswebmail-3.10/index.php
<?
if(function_exists('apd_set_pprof_trace')){
  apd_set_pprof_trace();
}
    
class timer{
  var $msec_start; var $sec_start;
  var $time;
  function timer(){
    $this->msec_start=0;$this->sec_start=0;
    $this->time=0;
  }
  function start(){
    list($this->msec_start,$this->sec_start) = split(' ',microtime());
  }
  function stop(){
    list($msec_end,$sec_end) = split(' ',microtime());
    $this->time = ($sec_end-$this->sec_start + ($msec_end-$this->msec_start)/100);
  }
}
// timers
$global_timer = new timer();

$global_timer->start();

// unregister all globals
if (ini_get('register_globals')) {
  foreach($_REQUEST as $k=>$v) { unset(${$k}); }
}


define('INCPATH',getcwd().'/include/');
if(!defined('TMPLPATH')){
  define('TMPLPATH',getcwd().'/templates/');
}

// add to include
set_include_path(get_include_path().':'.INCPATH.'Pear'.':'.INCPATH.'gpg');

include(INCPATH.'config.php');
include(INCPATH.'mail.php');
include(INCPATH.'files.php');
include(INCPATH.'functions.php');
include(INCPATH.'cache.php');
include(INCPATH.'mail_protocols.php');

// no chache
no_cache();

// need some preactions
$PRE_ACTION_HANDLERS = array(
     'read-mail'        => 'ReadMailPreaction',
     'compose'          => 'ComposePreaction',
     'compose-reply'    => 'ComposePreaction',
     'compose-reply-all'=> 'ComposePreaction',
     'compose-forward'  => 'ComposePreaction',
     'compose-continue' => 'ComposePreaction',
     'show-folder'      => 'ShowFolderPreaction');
// not use main design
$STANDALONE_ACTION_HANDLERS = array(
     'login'            => 'LoginPreaction',
     'infinite-session' => 'HelperActions',
     'show-loading'     => 'HelperActions',
     'logout'           => 'LogoutPreaction',
     'preview-mail'     => 'PreviewMailPreaction',
     'get-part'         => 'GetPartPreaction',
     'view-part'        => 'GetPartPreaction',
     'get-all-parts'    => 'GetAllPartsPreaction',
     'scan-virus-part'  => 'ScanPartForViruses', 
     'delete'           => 'DeletePreaction',
     'multi-delete'     => 'DeletePreaction',
     'spam'             => 'SpamPreaction',
     'move'             => 'MovePreaction',
     'show-mail-printable' => 'ShowMailPrintablePreaction',
     'set-label'        => 'SetLabelPreaction',
     'remove-label'     => 'SetLabelPreaction',
     'remove-labels'    => 'RemoveLabelsPreaction',
     'toggle-deleted-flag' => 'ToggleDeletedFlagPreaction',
     'settings-save'    => 'SettingsPreaction',
     'settings-default' => 'SettingsPreaction',
     'compose-send'     => 'ComposeSendPreaction',
     'compose-postpone' => 'ComposeSendPreaction',
     'compose-notify'   => 'ComposeSendPreaction',
     'compose-spell-check'   => 'SpellCheckPreaction',
     'compose-execute-spell' => 'SpellCheckPreaction',
     'addressbook-standalone' => 'AddressbookPreaction',
     'addressbook-choose' => 'AddressbookPreaction',
     'addressbook-take'   => 'AddressbookPreaction',
     'addressbook-add'    => 'AddressbookPreaction',
     'addressbook-save'   => 'AddressbookPreaction',
     'addressbook-delete' => 'AddressbookPreaction',
     'addressbook-ok'     => 'AddressbookPreaction',
     'addressbook-export' => 'AddressbookPreaction',
     'addressbook-import' => 'AddressbookPreaction',
     'addressbook-query'  => 'AddressbookPreaction',
     'newsgroups-refresh' => 'NewsgroupsPreaction',
     'newsgroups-add'     => 'NewsgroupsPreaction',
     'newsgroups-save'    => 'NewsgroupsPreaction',
     'newsgroups-delete'  => 'NewsgroupsPreaction',
     'folders-delete' => 'FoldersPreaction',
     'folders-empty'  => 'FoldersPreaction',
     'folders-view'   => 'FoldersPreaction',
     'folders-create' => 'FoldersPreaction',
     'folders-rename' => 'FoldersPreaction',
     'folders-refresh' => 'FoldersPreaction',
     'filters-block'    => 'FiltersPreaction',
     'filters-add'      => 'FiltersPreaction',
     'filters-save'     => 'FiltersPreaction',
     'filters-refresh'  => 'FiltersPreaction',
     'filters-delete'   => 'FiltersPreaction',
     'filters-move-up'  => 'FiltersPreaction',
     'filters-move-down'=> 'FiltersPreaction',
     'restore-incoming-execute'       =>  'RestorePreaction',
     'restore-incoming-range-execute' =>  'RestorePreaction',
     'restore-inbox-execute'          =>  'RestorePreaction',
     'labels-refresh' => 'LabelsPreaction',
     'labels-add'     => 'LabelsPreaction',
     'labels-save'    => 'LabelsPreaction',
     'labels-delete'  => 'LabelsPreaction',
     'gnupg-generate-key'      => 'GnupgPreaction',
     'gnupg-import'            => 'GnupgPreaction',
     'gnupg-send-public-key'   => 'GnupgPreaction',
     'gnupg-verify-passphrase' => 'GnupgPreaction',
     'gnupg-lookup-import'     => 'GnupgPreaction',);

$action = get_var('action');

// set title according to action
foreach($GLOBALS['ACTION_TITLES'] as $a=>$t){
  if(strpos($action,$a) === 0){
    $GLOBALS['TITLE'] = VERSION.' - '.$t;
    break;
  }
}

// handlers
$pre_handler = $PRE_ACTION_HANDLERS[$action];
$alone_handler = $STANDALONE_ACTION_HANDLERS[$action];
if($pre_handler){
  check_www_authentication(false);
  try{
    $pre_handler($action);
  }catch(Exception $e){
    Error::set($e->__toString());
  } 
  charset_header($GLOBALS['DEFAULT_CHARSET']);
}
else if($alone_handler){
  if($action != 'login' && $action != 'show-loading'){
    check_www_authentication(false);
  }
  if(function_exists($alone_handler)){
    try{
      $alone_handler($action);
      exit;
    }catch(Exception $e){
      Error::set($e->__toString());
    } 
  }
  else{
    Notice::set('<div style="font-size:150%">Standalone Action Error</div>'.
                "No such handler <u>$alone_handler</u>");
  }
}
else{
  charset_header($GLOBALS['DEFAULT_CHARSET']);
}


// login
function LoginPreaction($action){
  $user_name = get_var('user_name');
  $user_password = get_var('user_password');
  if($user_name == '' || $user_password == ''){
    $title = 'Login Error';
    $message = 'authentication login required.';
    include(TMPLPATH."html_start.php");
    include(TMPLPATH.'error.php');
    include(TMPLPATH."html_end.php");
    exit;
  }
  $GLOBALS['MAIL_USER_NAME']=$user_name;
  $GLOBALS['MAIL_USER_PASSWORD']=$user_password;
  check_imap_login();
  register_session_vars($user_name,$user_password);
  if($_REQUEST['remember'] == 'true'){
    remember_user($user_name,$user_password);
  }
  location_header($_SERVER['PHP_SELF'].'?action=news');
}

// logout handle
function LogoutPreaction(){
  // clean up remembered id
  if(isset($_SESSION['auto-id']) || isset($_COOKIE['auto-id'])) {
    remove_remembered_user($_SESSION['auto-id']?$_SESSION['auto-id']:$_COOKIE['auto-id']);
    setcookie('auto-id','', time()-42000,'/');
  }
  // cleanup session
  $_SESSION = array();
  if(isset($_COOKIE[session_name()])) {
    setcookie(session_name(),'', time()-42000,'/');
  }
  session_destroy();
  location_header($_SERVER['PHP_SELF'],false);
}

// helper actions
function HelperActions($action){
  if($action == 'infinite-session'){    
    if($_SESSION['user_logged'] != 'true'){
      print 'zopa';
    }
    header('Refresh: 60;URL='.$_SERVER['PHP_SELF'].'?action=infinite-session&'.sid());
    include(TMPLPATH."html_start.php");
    $theme_name = $_SESSION['preferences']?$_SESSION['preferences']->getTheme():DEFAULT_THEME;
    if($GLOBALS['THEMES'][$theme_name]['image'])
      print '<img src="'.$GLOBALS['THEMES'][$theme_name]['image'].'" title="thanx u for using '.VERSION.'">';
    else
      print VERSION;
    include(TMPLPATH."html_end.php");
  }
  else if($action == 'show-loading'){
    include(TMPLPATH."html_start.php");
    print '<span class="red">Loading ...</span>';
    include(TMPLPATH."html_end.php");
  }
}

// some preactions
// send email first
// after only notice
function ComposeSendPreaction($action){
  include(TMPLPATH."compose.php");
}
function SettingsPreaction($action){
  include(TMPLPATH."settings.php");
}
function GnupgPreaction($action){
  include(TMPLPATH."gnupg.php");
}


// read mail preactions
function ReadMailPreaction($action){
  $mailbox = get_var('mailbox');
  $mailkey = get_var('mailkey');
  if($mailbox != '' && $mailkey != ''){
    Cache::initReadMail($mailbox,$mailkey);
    $m = $_SESSION['mboxes']->find($mailbox);
    if(is_object($m)){
        $m->refresh();
    }
  }
}
function ComposePreaction($action){
  if($action != 'compose'){
    $mailbox = get_var('mailbox');
    $mailkey = get_var('mailkey');
    if($mailbox != '' && $mailkey != ''){
      $mail = Cache::getReadMail($mailbox,$mailkey);
    }
  }
}
function ShowFolderPreaction($action){
  $m = $_SESSION['mboxes']->find(get_var('mailbox'));
  if(is_object($m)) $m->refresh();
}

// special actions that not required
// in main design

// preview mail (in small windows)
function PreviewMailPreaction($action){
  $mailbox = get_var('mailbox');
  $mailkey = get_var('mailkey');
  if($mailbox != '' && $mailkey != ''){
    Cache::initReadMail($mailbox,$mailkey);
    charset_header($GLOBALS['DEFAULT_CHARSET']);
    include(TMPLPATH."html_start.php");
    $_SESSION['last_mail']->print_preview_presentation();
    include(TMPLPATH."html_end.php");
  }
}
// getting some part of mail
function GetPartPreaction($action){
  $mailbox = get_var('mailbox');
  $mailkey = get_var('mailkey');
  $part = get_var('part');
  $view = ($action == 'view-part');
  $thumbnail = get_var('thumbnail')!='';
  if($mailbox && $mailkey && $part){
    if($_SESSION['last_mail'] &&
       $_SESSION['last_mail']->mailbox_name == $mailbox &&
       $_SESSION['last_mail']->key == $mailkey){
      $mail_part = $_SESSION['last_mail']->get_part($part);
    }
    else{
      $mbox = $_SESSION['mboxes']->find($mailbox);
      $mail_part = get_part($mbox->name,$mailkey,$part);
    }
    if(!$mail_part) return;

    $mime_type = $mail_part->mime_type;
    $content = $mail_part->body;

    if($view && ToHtmlConverter::is_supported($mail_part->mime_type)){
      $mime_type = 'text/html';
      $content = ToHtmlConverter::convert($content,$mail_part->mime_type);
    }
    else if($thumbnail && eregi('image/',$mime_type)){
      $content = $mail_part->get_thumbnail();
    }
    
    header('HTTP/1.0 200 OK');
    header('Content-Type: '.$mime_type);
    if(eregi('text/',$mime_type)){
      if(!$mail_part->_charset &&
         (eregi('<meta.*charset=([^"]*).*http-equiv="?Content-Type"?',$content,$m) ||
          eregi('<meta.*http-equiv="?Content-Type"?.*charset=([^"]*)',$content,$m))){
        charset_header($m[1],$mime_type);
      }
      charset_header($mail_part->get_charset(),$mime_type);
      if(!$view)
        header('Content-Disposition: attachment;filename="'.str_replace('"','',$mail_part->filename).'"');
    }
    else if(eregi('image/',$mime_type)){
      if(!$view)
        header('Content-Disposition: attachment;filename="'.str_replace('"','',$mail_part->filename).'"');
    }
    else if($mail_part->filename){
      header('Content-Disposition:attachment; '.
             'filename="'.str_replace('"','',$mail_part->filename).'"; '.
             'size='.strlen($mail_part->body));
      // IExplorer work around
      header("Pragma: turn-off-cache");
    }
    // finally print part content
    print $content;
  }
  else
    print 'what?';
}

// get all mail parts as zip
function GetAllPartsPreaction($action){
  $mailbox = get_var('mailbox');
  $mailkey = get_var('mailkey');
  if($mailbox && $mailkey){
    Cache::initReadMail($mailbox,$mailkey);
    if(!$_SESSION['last_mail']) return;
    
    $zip = new ZipFile();
    $names = array();
    foreach($_SESSION['last_mail']->parts as $index=>$part){
      if($part->if_attachment()){
        $name = ($part->filename?$part->filename:'untitled.txt');
        if($names[$name])
          $name = $index.'_'.$name;
        $names[$name] = true;
        $zip->addFile($part->body,$name);
      }
    } 
        
    header('HTTP/1.0 200 OK');
    header('Content-Type: application/zip');
    header('Content-Disposition:attachment; filename="'.$mailbox.'_'.$mailkey.'.zip"');
    // IExplorer work around
    header("Pragma: turn-off-cache");
    print $zip->file();
  }
  else
    print 'what ?';
}
// scan mail part for viruses
function ScanPartForViruses($action){
  $mailbox = get_var('mailbox');
  $mailkey = get_var('mailkey');
  $part = get_var('part');
  if($mailbox && $mailkey && $part){
    
    include(TMPLPATH."html_start.php");
    print '<i id="wait-label" class="blink">Please wait, virus scanning is in proccess ...<br/></i>';
    flush();
    
    if($_SESSION['last_mail'] &&
       $_SESSION['last_mail']->mailbox_name == $mailbox &&
       $_SESSION['last_mail']->key == $mailkey){
      $mail_part = $_SESSION['last_mail']->get_part($part);
    }
    else{
      $mbox = $_SESSION['mboxes']->find($mailbox);
      $mail_part = get_part($mbox->name,$mailkey,$part);
    }
    if(!$mail_part) return;
    
    $ret = VirusScanner::scan($mail_part->body,$mail_part->filename);
    print '<script type="text/javascript">document.getElementById("wait-label").style.visibility="hidden";</script>';
    print '<div class="notice">'.nl2br($ret).'</div>';
    print '<div class="center"><input type="button" value="close" onclick="window.close();"/></div>';
    
    include(TMPLPATH."html_end.php");
  }
}
// delete mails (allow simultaneous delete from many mboxes)
function DeletePreaction($action){
  if($action == 'delete'){
    $mailbox = get_var('mailbox');
    $key = get_var("key");
    if($mailbox != ''){
      $rest = '';
      if(get_var('sort_order')) $rest .= '&sort_order='.get_var('sort_order');
      if(get_var('start_page')) $rest .= '&start_page='.get_var('start_page');
      if(get_var('view')) $rest .= '&view='.get_var('view');
      
      
      $mbox = $_SESSION['mboxes']->find($mailbox);
      $res = $mbox->delete($key);
      // remove last headers
      if($_SESSION['last_headers'] && $_SESSION['last_headers']->name == $mailbox)
        unset($_SESSION['last_headers']);
        
      $mailkey = get_var('mailkey');
      if(get_var('show-next') && $mailkey){
        Notice::set($res?$res:'No message was deleted');
        location_header($_SERVER['PHP_SELF'].'?action=read-mail&mailbox='.$mailbox.'&mailkey='.$mailkey.$rest);
      }
      else{
        Notice::set($res?$res:'No message was deleted');
        location_header($_SERVER['PHP_SELF'].'?action=show-folder&mailbox='.$mailbox.$rest);
      }
    }
    else{
      Notice::set('What ?');
      location_header($_SERVER['PHP_SELF'].'?action=error');
    }
  }
  else if($action == 'multi-delete'){
    $key = get_var("key");
    foreach($key as $mailbox=>$keys){
      $mbox = $_SESSION['mboxes']->find($mailbox);
      $res .= $mbox->delete($keys);
    }
    Notice::set($res?$res:'No message was deleted');
    location_header($_SERVER['PHP_SELF'].'?action=search');
  }
}
// spam
function SpamPreaction($action){
  $mailbox = get_var('mailbox');
  $key = get_var("key");
  if($mailbox != ''){
    $rest = '';
    if(get_var('sort_order')) $rest .= '&sort_order='.get_var('sort_order');
    if(get_var('start_page')) $rest .= '&start_page='.get_var('start_page');
    if(get_var('view')) $rest .= '&view='.get_var('view');
    
    $headers = $_SESSION['last_headers'];
    $res = '';
    foreach($key as $k){
      $h = $headers->get_header($k);
      if(is_object($h)){
        $_SESSION['procmail']->block_email($h->from_mail_address);
        $res .= $h->from_mail_address.', ';
      }
    }
    if($res){
      $_SESSION['procmail']->store();
      $mbox = $_SESSION['mboxes']->find($mailbox);
      $res2 .= $mbox->delete($key);
      Notice::set('<b>blocked</b> : '.wordwrap($res,50)."\n".$res2);
    }
    else
      Notice::set('No message for spam');
    location_header($_SERVER['PHP_SELF'].'?action=show-folder&mailbox='.$mailbox.$rest);
  }
  else{
    Notice::set('What ?');
    location_header($_SERVER['PHP_SELF'].'?action=error');
  } 
}
// moving mail from one mbox to another
function MovePreaction($action){
  $mailbox = get_var('mailbox');
  $newmailbox = get_var('newmailbox');
  $key = get_var("key");
  if($mailbox != '' && $newmailbox != ''){
    $rest = '';
    if(get_var('sort_order')) $rest .= '&sort_order='.get_var('sort_order');
    if(get_var('start_page')) $rest .= '&start_page='.get_var('start_page');
    if(get_var('view')) $rest .= '&view='.get_var('view');
    $copy = (get_var('copy') == 'ok');
    
    $mbox = $_SESSION['mboxes']->find($mailbox);
    if($copy){
      $res = $mbox->copy($key,$newmailbox);
    }
    else{
      $mbox->setLabel(DELETED_FLAG,$key,false);
      $res = $mbox->move($key,$newmailbox);
    }
    $_SESSION['mboxes']->refresh();
    $mailkey = get_var('mailkey');
    if(get_var('show-next') && $mailkey){
      location_header($_SERVER['PHP_SELF'].'?action=read-mail&mailbox='.$mailbox.'&mailkey='.$mailkey.$rest);
    }
    else{
      Notice::set($res?$res:'No message was moved');
      location_header($_SERVER['PHP_SELF'].'?action=show-folder&mailbox='.$mailbox.$rest);
    }
  }
  else{
    Notice::set('What ?');
    location_header($_SERVER['PHP_SELF'].'?action=error');
  }
}
// setting label
function SetLabelPreaction($action){
  $mailbox = get_var('mailbox');
  $mailkey = get_var('mailkey');
  $key = get_var('key');
  $label = get_var('label');
  if($mailbox != ''){
    $rest = '';
    if(get_var('sort_order')) $rest .= '&sort_order='.get_var('sort_order');
    if(get_var('start_page')) $rest .= '&start_page='.get_var('start_page');
    if(get_var('view')) $rest .= '&view='.get_var('view');
    
    $mbox = $_SESSION['mboxes']->find($mailbox);
    $res = $mbox->setLabel($label,$key,($action=='set-label'));
    // remove deleted flag
    $mbox->setLabel(DELETED_FLAG,$key,false);
    if($mailkey){
      Notice::set($res);
      location_header($_SERVER['PHP_SELF'].'?action=read-mail&mailbox='.$mailbox.'&mailkey='.$mailkey.$rest);
    }
    else{
      Notice::set($res?$res:'No message was labeled');
      location_header($_SERVER['PHP_SELF'].'?action=show-folder&mailbox='.$mailbox.$rest);
    }
  }
  else{
    Notice::set('What ?');
    location_header($_SERVER['PHP_SELF'].'?action=error');
  }
}
// removing labels
function RemoveLabelsPreaction($action){
  $label = get_var('label');
  $key = get_var("key");
  if(is_array($key)){
    $rest = '';
    if(get_var('sort_order')) $rest .= '&sort_order='.get_var('sort_order');
    if(get_var('start_page')) $rest .= '&start_page='.get_var('start_page');
    if(get_var('view')) $rest .= '&view='.get_var('view');
          
    foreach($key as $mailbox=>$keys){
      $mbox = $_SESSION['mboxes']->find($mailbox);
      $res .= $mbox->setLabel($label,$keys,false);
    }
    if($label){
      Notice::set($res?$res:'No message was labeled');
      location_header($_SERVER['PHP_SELF'].'?action=show-folder&label='.$label.$rest);
    }
  }
  else{
    Notice::set('What ?');
    location_header($_SERVER['PHP_SELF'].'?action=error');
  }
}
// set deleted flag (for AJAX call)
function ToggleDeletedFlagPreaction($action){
  $mailbox = get_var('mailbox');
  $mailkey = get_var('mailkey');
  $set = get_var('set') != 'false';
  if($mailbox != ''){
    $mbox = $_SESSION['mboxes']->find($mailbox);
    $res = $mbox->setLabel(DELETED_FLAG,$mailkey,$set);
    $header =& $_SESSION['last_headers']->get_header($mailkey);
    $header->is_deleted = $set;
    print 'ok';
  }
  else{
    print 'what ?';
  }
}

// show email in printable form
function ShowMailPrintablePreaction($action){
  $mail = Cache::getReadMail(get_var('mailbox'),get_var('mailkey'));
  charset_header($GLOBALS['DEFAULT_CHARSET']);
  
  include(TMPLPATH."html_start.php");
  $mail->print_printable_presentation();
  include(TMPLPATH."html_end.php");
}

function SpellCheckPreaction($action){
  include(TMPLPATH."compose.php");
}

function AddressbookPreaction($action){
  include(TMPLPATH."addressbook.php");
}

function NewsgroupsPreaction($action){
  include(TMPLPATH."newsgroups.php");
}

function FoldersPreaction($action){
  include(TMPLPATH."edit_folders.php");
}

function FiltersPreaction($action){
  include(TMPLPATH."filters.php");
}

function RestorePreaction($action){
  include(TMPLPATH."restore.php");
}

function LabelsPreaction($action){
  include(TMPLPATH."labels.php");
}

try{
  include(TMPLPATH."main.php");
}catch(Exception $e){
  print '<pre class="error">'.nl2br($e->__toString()).'</pre>';
} 



?>
Return current item: CsWebmail