class.uebimiau_mail.php

来自「通达OA2007SE源代码 非常好的」· PHP 代码 · 共 1,097 行 · 第 1/3 页

PHP
1,097
字号
<?
  class uebimiau
  {
    var $_current_folder = '';
    var $_system_folders = array (0 => 'inbox', 1 => 'trash', 2 => 'sent');
    function uebimiau ()
    {
      $this->_sid = uniqid ('');
    }
    function mail_connected ()
    {
      if (!empty ($this->mail_connection))
      {
        $sock_status = socket_get_status ($this->mail_connection);
        if ($sock_status['eof'])
        {
          fclose ($this->mail_connection);
          return 0;
        }
        return 1;
      }
      return 0;
    }
    function mail_get_line ()
    {
      $buffer = chop (fgets ($this->mail_connection, 1024));
      if ($this->debug)
      {
        $sendtodebug = true;
        if (((eregi ('^(\\* )', $buffer) OR eregi ('^([A-Za-z0-9]+ (OK|NO|BAD))', $buffer)) OR eregi ('^(\\+OK|\\-ERR)', $buffer)))
        {
          $output = (('<- <b>'.htmlspecialchars ($buffer)).'</b>');
        }
        else
        {
          $sendtodebug = ((1 < $this->debug) ? false : true);
          $output = htmlspecialchars ($buffer);
        }
        if ($sendtodebug)
        {
          echo (((''.'<font style="font-size:12px; font-family: Courier New; background-color: white; color: black;"> ').$output).'</font><br>
');
        }
        flush ();
      }
      return $buffer;
    }
    function mail_send_command ($cmd)
    {
      if ($this->mail_connected ())
      {
        $output = (eregi ('^(PASS|LOGIN)', $cmd, $regs) ? ($regs[1].' ****') : $cmd);
        if (($this->mail_protocol == 'imap'))
        {
          $cmd = (($this->_sid.' ').$cmd);
          $output = (($this->_sid.' ').$output);
        }
        fwrite ($this->mail_connection, ((''.$cmd).'
'));
        if ($this->debug)
        {
          echo (('<font style="font-size:12px; font-family: Courier New; background-color: white; color: black;">-&gt; <em><b>'.htmlspecialchars ($output)).'</b></em></font><br>
');
          flush ();
        }
        return 1;
      }
      return 0;
    }
    function mail_connect ()
    {
      if ($this->debug)
      {
        for ($i = 0; ($i < 20); ++$i)
        {
          echo '<!-- buffer sux -->
';
        }
      }
      if (!$this->mail_connected ())
      {
        $this->mail_connection = fsockopen ($this->mail_server, $this->mail_port, $errno, $errstr, 15);
        if ($this->mail_connection)
        {
          $buffer = $this->mail_get_line ();
          if (($this->mail_protocol == 'imap'))
          {
            $regexp = '^([ ]?\\*[ ]?OK)';
          }
          else
          {
            $regexp = '^(\\+OK)';
          }
          if (ereg ($regexp, $buffer))
          {
            return 1;
          }
          else
          {
            return 0;
          }
        }
        return 0;
      }
      else
      {
        return 1;
      }
    }
    function mail_auth ($checkfolders = false)
    {
      if ($this->mail_connected ())
      {
        if (($this->mail_protocol == 'imap'))
        {
          $this->mail_send_command (((('LOGIN '.$this->mail_user).' ').$this->mail_pass));
          $buffer = $this->mail_get_line ();
          if (ereg ((('^('.$this->_sid).' OK)'), $buffer))
          {
            if ($checkfolders)
            {
              $this->_check_folders ();
            }
            return 1;
          }
          else
          {
            $this->mail_error_msg = $buffer;
            return 0;
          }
        }
        else
        {
          $this->mail_send_command (('USER '.$this->mail_user));
          $buffer = $this->mail_get_line ();
          if (ereg ('^(\\+OK)', $buffer))
          {
            $this->mail_send_command (('PASS '.$this->mail_pass));
            $buffer = $this->mail_get_line ();
            if (ereg ('^(\\+OK)', $buffer))
            {
              if ($checkfolders)
              {
                $this->_check_folders ();
              }
              return 1;
            }
            else
            {
              $this->mail_error_msg = $buffer;
              return 0;
            }
          }
          else
          {
            return 0;
          }
        }
      }
      return 0;
    }
    function _check_folders ()
    {
      $userfolder = $this->user_folder;
      $temporary_directory = $this->temp_folder;
      $idle_timeout = $this->timeout;
      if (!file_exists ($this->user_folder))
      {
        if (!mkdir ($this->user_folder, 511))
        {
          exit ((((''.'<h1><br><br><br><center>').$error_permiss).'</center></h1>'));
        }
      }
      $boxes = $this->mail_list_boxes ();
      if (($this->mail_protocol == 'imap'))
      {
        $tmp = $this->_system_folders;
        for ($i = 0; ($i < count ($boxes)); ++$i)
        {
          $current_folder = $boxes[$i]['name'];
          if (in_array (strtolower ($current_folder), $this->_system_folders))
          {
            $current_folder = strtolower ($current_folder);
          }
          while (list ($index, $value) = each ($tmp))
          {
            if ((strtolower ($current_folder) == strtolower ($value)))
            {
              unset ($$tmp);
              continue;
            }
          }
          reset ($tmp);
        }
        while (list ($index, $value) = each ($tmp))
        {
          $this->mail_create_box ($value);
        }
        for ($i = 0; ($i < count ($boxes)); ++$i)
        {
          $current_folder = $boxes[$i]['name'];
          if (!in_array (strtolower ($current_folder), $this->_system_folders))
          {
            if (!file_exists (($this->user_folder.$current_folder)))
            {
              mkdir (($this->user_folder.$current_folder), 511);
              continue;
            }
            continue;
          }
        }
      }
      $system_folders = array_merge ($this->_system_folders, array ('_attachments', '_infos'));
      while (list ($index, $value) = each ($system_folders))
      {
        if (!file_exists (($this->user_folder.$value)))
        {
          if (in_array (strtolower ($value), $this->_system_folders))
          {
            $value = strtolower ($value);
          }
          mkdir (($this->user_folder.$value), 511);
          continue;
        }
      }
      $sessiondir = ($temporary_directory.'_sessions/');
      $all = opendir ($sessiondir);
      while ($file = readdir ($all))
      {
        $thisfile = ($sessiondir.$file);
        if (is_file ($thisfile))
        {
          $idle = intval (((time () - filemtime ($thisfile)) / 60));
          if ((($idle_timeout + 10) < $idle))
          {
            unlink ($thisfile);
            continue;
          }
          continue;
        }
      }
      closedir ($all);
      unset ($all);
    }
    function mail_retr_msg ($msg, $check = 1)
    {
      global $mail_use_top;
      global $appname;
      global $appversion;
      global $error_retrieving;
      $msgheader = $msg['header'];
      if (($this->mail_protocol == 'imap'))
      {
        if ($check)
        {
          if ((strtolower ($this->_current_folder) != strtolower ($msg['folder'])))
          {
            $boxinfo = $this->mail_select_box ($msg['folder']);
          }
          $this->mail_send_command ((((('FETCH '.$msg['id']).':').$msg['id']).' BODY.PEEK[HEADER.FIELDS (Message-Id)]'));
          $buffer = $this->mail_get_line ();
          if (eregi ((('^('.$this->_sid).' (NO|BAD))'), $buffer))
          {
            $this->mail_error_msg = $buffer;
            return 0;
          }
          while (!eregi ((('^('.$this->_sid).' OK)'), $buffer))
          {
            if (eregi ('message-id: (.*)', $buffer, $regs))
            {
              $current_id = ereg_replace ('<(.*)>', '\\1', $regs[1]);
            }
            $buffer = $this->mail_get_line ();
          }
          if ((base64_encode ($current_id) != base64_encode ($msg['message-id'])))
          {
            $this->mail_error_msg = $error_retrieving;
            return 0;
          }
        }
        if (file_exists ($msg['localname']))
        {
          $msgcontent = $this->_read_file ($msg['localname']);
        }
        else
        {
          $this->mail_send_command ((((('FETCH '.$msg['id']).':').$msg['id']).' BODY[TEXT]'));
          $buffer = $this->mail_get_line ();
          if (eregi ((('^('.$this->_sid).' (NO|BAD))'), $buffer))
          {
            $this->mail_error_msg = $buffer;
            return 0;
          }
          if (ereg ('\\{(.*)\\}', $buffer, $regs))
          {
            $bytes = $regs[1];
          }
          $buffer = $this->mail_get_line ();
          while (!eregi ((('^('.$this->_sid).' OK)'), $buffer))
          {
            if (!eregi ('[ ]?\\*[ ]?[0-9]+[ ]?FETCH', $buffer))
            {
              ($msgbody .= ((''.$buffer).'
'));
            }
            $buffer = $this->mail_get_line ();
          }
          $pos = strrpos ($msgbody, ')');
          if (!($pos === false))
          {
            $msgbody = substr ($msgbody, 0, $pos);
          }
          $msgcontent = (((''.$msgheader).'
').$msgbody);
          $this->_save_file ($msg['localname'], $msgcontent);
        }
      }
      else
      {
        if (($check AND (strtolower ($msg['folder']) == 'inbox')))
        {
          $this->mail_send_command ((('TOP '.$msg['id']).' 0'));
          $buffer = $this->mail_get_line ();
          if (!ereg ('^(\\+OK)', $buffer))
          {
            $this->mail_error_msg = $buffer;
            return 0;
          }
          unset ($header);
          while (!feof ($this->mail_connection))
          {
            $buffer = $this->mail_get_line ();
            if ((trim ($buffer) == '.'))
            {
              break;
            }
            ($header .= ((''.$buffer).'
'));
          }
          $mail_info = $this->get_mail_info ($header);
          if ((base64_encode ($mail_info['message-id']) != base64_encode ($msg['message-id'])))
          {
            $this->mail_error_msg = $error_retrieving;
            return 0;
          }
        }
        if (file_exists ($msg['localname']))
        {
          $msgcontent = $this->_read_file ($msg['localname']);
        }
        else
        {
          if ((strtolower ($msg['folder']) == 'inbox'))
          {
            $command = ($mail_use_top ? ((('TOP '.$msg['id']).' ').$msg['size']) : ('RETR '.$msg['id']));
            $this->mail_send_command ($command);
            $buffer = $this->mail_get_line ();
            if (!ereg ('^(\\+OK)', $buffer))
            {
              $this->mail_error_msg = $buffer;
              return 0;
            }
            $last_buffer = 0;
            while (!feof ($this->mail_connection))
            {
              $buffer = ereg_replace ('(

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?