Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class GmailImap
- {
- var $server = '{imap.gmail.com:993/ssl/novalidate-cert}';
- var $headers;
- var $connection;
- function __construct($email, $password)
- {
- imap_timeout(IMAP_OPENTIMEOUT, 10);
- $this->connection = imap_open($this->server, $email, $password, OP_READONLY);
- if (!$this->connection){
- //debug::dump(imap_errors());
- throw new GmailImapException("Spojení se nezdařilo! ".imap_last_error());
- }
- if( !imap_ping($this->connection) ){
- throw new GmailImapException("Spojení se přerušilo! ".imap_last_error());
- }
- if( $hdr = imap_check($this->connection) ){
- //debug::dump("Zprav na serveru: " . $hdr->Nmsgs);
- } else {
- throw new GmailImapException("'imap_check' selhal! ".imap_last_error());
- }
- if( class_exists('Environment') ){
- Environment::getApplication()->onShutdown[] = array($this, '__destruct');
- }
- }
- function getAttachment($time = Null, $saveAStime = Null)
- {
- $filtered = imap_search($this->connection, "ON ".date("d-M-Y", $time));
- if( $filtered === False ){
- return array( $time => array() );
- }
- if( $saveAStime !== Null ){
- $time = $saveAStime;
- }
- $xmls = array();
- foreach( $filtered AS $msgno ){
- // Tue, 22 Dec 2009 13:19:35 +0100
- // prectu hlavicky zprav
- $overview = imap_fetch_overview($this->connection, $msgno.':'.$msgno, 0);
- // a projdu vsechny zpravy
- foreach( $overview AS $i => $message ){
- //echo "message: "; debug::dump($message);
- $struct = imap_fetchstructure($this->connection, $message->msgno);
- // echo "struct: "; debug::dump($struct);
- //echo "parts: "; debug::dump($struct->parts);
- $subjArray = explode(" ", $message->subject);
- // echo "subjArray: "; debug::dump($subjArray);
- //debug::dump("Pocet priloh ve zprave ".$message->msgno." je: ".(count($struct->parts)-1));
- // $message = imap_fetchbody($this->connection, $message->msgno, 1);
- // switch( $struct->parts[0]->type ){
- // case 0: //7BIT
- // $message = imap_utf7_decode($message);
- // break;
- // case 1: //8BIT
- // $message = imap_8bit($message);
- // break;
- // case 2: //BINARY
- // $message = ($message);
- // break;
- // case 3: //BASE64
- // $message = base64_decode($message);
- // break;
- // case 4: //QUOTED-PRINTABLE
- // $message = quoted_printable_decode($message);
- // break;
- // case 5: //OTHER
- // }
- //Pokud obsahuje prilohu...
- if( count($struct->parts) >= 2 ){
- // nactu druhou cast zpravy (prvni priloha)
- $fileContent = imap_fetchbody($this->connection, $message->msgno, 2);
- //echo "fileContent"; debug::dump($fileContent);
- // a provedu parserovani
- $decoded = imap_base64($fileContent);
- //echo "decoded"; debug::dump($decoded);
- $time = strtotime($message->date);
- $report = Environment::expand("%appDir%/temp/reports").'/'.$saveAStime.'.'.date('Y-m-d', $saveAStime).'.'.rand(1000,9999);
- $report_zip = $report.".zip";
- $report_xml = $report.".xml";
- if( !@file_put_contents("safe://".$report_zip, $decoded) ){
- throw new IOException("Writting error for file {$report_zip}");
- }
- $zip = new PclZip("safe://".$report_zip);
- if ($zip->extract(PCLZIP_OPT_PATH, dirname($report)) == 0) {
- if( !is_file($report_zip) ){
- throw new FileNotFoundException("File {$report_zip} doesn't exists!");
- }
- throw new IOException("Error: wasn't able to open archive {$report_zip}");
- }
- $extracted = dirname($report_zip).'/report.xml';
- if( !@copy($extracted, "safe://".$report_xml) ){
- if( !is_file($extracted) ){
- throw new FileNotFoundException("File {$extracted} doesn't exists!");
- }
- throw new IOException("Error: wasn't able to copy '{$extracted}' to '{$report_xml}'");
- }
- if( !@unlink("safe://".$extracted) OR !@unlink("safe://".$report_zip) ){
- throw new IOException("Error: wasn't able to clean up");
- }
- $xmls[$saveAStime][] = $report_xml;
- }
- //echo "<hr>";
- }
- }
- return $xmls;
- }
- public function __destruct()
- {
- // ukoncim spojeni
- $ukonci = @imap_close($this->connection);
- if( !$ukonci ){
- //debug::dump(imap_errors());
- throw new GmailImapException("Spojení se nepodařilo ukončit.");
- }
- }
- /**
- * Return array of IMAP messages for pagination
- *
- * @param int $page page number to get
- * @param int $per_page number of results per page
- * @param array $sort array('subject', 'asc') etc
- *
- * @return mixed array containing imap_fetch_overview, pages, and total rows if successful, false if an error occurred
- * @author Raja K
- */
- public function listMessages($page = 1, $per_page = 25, $sort = null) {
- $limit = ($per_page * $page);
- $start = ($limit - $per_page) + 1;
- $start = ($start < 1) ? 1 : $start;
- $limit = (($limit - $start) != ($per_page-1)) ? ($start + ($per_page-1)) : $limit;
- $info = imap_check($this->_imap_stream);
- $limit = ($info->Nmsgs < $limit) ? $info->Nmsgs : $limit;
- if(true === is_array($sort)) {
- $sorting = array(
- 'direction' => array( 'asc' => 0,
- 'desc' => 1),
- 'by' => array( 'date' => SORTDATE,
- 'arrival' => SORTARRIVAL,
- 'from' => SORTFROM,
- 'subject' => SORTSUBJECT,
- 'size' => SORTSIZE));
- $by = (true === is_int($by = $sorting['by'][$sort[0]]))
- ? $by
- : $sorting['by']['date'];
- $direction = (true === is_int($direction = $sorting['direction'][$sort[1]]))
- ? $direction
- : $sorting['direction']['desc'];
- $sorted = imap_sort($this->_imap_stream, $by, $direction);
- $msgs = array_chunk($sorted, $per_page);
- $msgs = $msgs[$page-1];
- }
- else
- $msgs = range($start, $limit); //just to keep it consistent
- $result = imap_fetch_overview($this->_imap_stream, implode($msgs, ','), 0);
- if(false === is_array($result)) return false;
- //sorting!
- if(true === is_array($sorted)) {
- $tmp_result = array();
- foreach($result as $r)
- $tmp_result[$r->msgno] = $r;
- $result = array();
- foreach($msgs as $msgno) {
- $result[] = $tmp_result[$msgno];
- }
- }
- $return = array('res' => $result,
- 'start' => $start,
- 'limit' => $limit,
- 'sorting' => array('by' => $sort[0], 'direction' => $sort[1]),
- 'total' => imap_num_msg($this->_imap_stream));
- $return['pages'] = ceil($return['total'] / $per_page);
- return $return;
- }
- }
- class GmailImapException extends Exception { }
Advertisement
Add Comment
Please, Sign In to add comment