[5136] | 1 | <?php |
---|
| 2 | |
---|
| 3 | include_once "../header.session.inc.php"; |
---|
| 4 | |
---|
| 5 | include_once "../expressoMail1_2/inc/class.imap_functions.inc.php"; |
---|
| 6 | |
---|
| 7 | class ImapServiceAdapter extends imap_functions/* implements Service*/ |
---|
| 8 | { |
---|
| 9 | public function open( $config ) |
---|
| 10 | { |
---|
| 11 | $this->init(); |
---|
| 12 | } |
---|
| 13 | |
---|
| 14 | public function connect( $config ) |
---|
| 15 | { |
---|
| 16 | $this->init(); |
---|
| 17 | } |
---|
| 18 | |
---|
| 19 | public function find( $URI, $context, $justthese = false, $criteria = false ) |
---|
| 20 | { |
---|
[5188] | 21 | |
---|
[5136] | 22 | if( is_array($URI) ) |
---|
| 23 | { |
---|
| 24 | $criteria = $justthese; |
---|
| 25 | $justthese = $context; |
---|
| 26 | $context = $URI['context']; |
---|
| 27 | $URI = $URI['concept']; |
---|
| 28 | } |
---|
| 29 | |
---|
| 30 | switch( $URI ) |
---|
| 31 | { |
---|
| 32 | case 'folder': |
---|
| 33 | { |
---|
[5204] | 34 | $result = $this->to_utf8($this->get_folders_list()); |
---|
[5136] | 35 | |
---|
| 36 | foreach ($result as $res) { |
---|
| 37 | |
---|
| 38 | $response[] = array( |
---|
| 39 | 'id' => $res['folder_id'], |
---|
| 40 | 'commonName' => $res['folder_name'], |
---|
| 41 | 'parentFolder' => $res['folder_parent'], |
---|
| 42 | 'messageCount' => array('unseen' => isset($res['folder_unseen']) ? $res['folder_unseen'] : null, 'total' => null) |
---|
| 43 | ); |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | break; |
---|
| 47 | } |
---|
| 48 | case 'message': |
---|
| 49 | { |
---|
| 50 | //begin: for grid |
---|
| 51 | $page = $criteria['page']; //{1} get the requested page |
---|
| 52 | $limit = $criteria['rows']; //{10} get how many rows we want to have into the grid |
---|
| 53 | $sidx = $criteria['sidx']; //{id} get index row - i.e. user click to sort |
---|
| 54 | $sord = $criteria['sord']; //{desc} get the direction |
---|
| 55 | |
---|
| 56 | if( !$sidx ) $sidx = 1; |
---|
| 57 | |
---|
| 58 | $folder_name = str_replace( '.', $this->imap_delimiter, $context['folder'] ); |
---|
| 59 | |
---|
| 60 | $folder = $this->get_range_msgs2(array('folder'=>$folder_name)); |
---|
| 61 | |
---|
| 62 | $count = $folder['num_msgs']; |
---|
| 63 | |
---|
| 64 | $total_pages = $count > 0 ? ceil( $count/$limit ) : 0; |
---|
| 65 | |
---|
| 66 | if( $page > $total_pages ) |
---|
| 67 | $page = $total_pages; |
---|
| 68 | |
---|
| 69 | $start = $limit * $page - $limit; // do not put $limit*($page - 1) |
---|
| 70 | //end: for grid |
---|
| 71 | |
---|
| 72 | $service_params = array( 'folder' => $folder_name, //INBOX |
---|
| 73 | 'msg_range_begin' => $start + 1, //?? |
---|
| 74 | 'msg_range_end' => $start + $limit, //$limit = $_GET['rows']; // get how many rows we want to have into the grid |
---|
| 75 | 'sort_box_type' => 'SORTARRIVAL', |
---|
| 76 | 'search_box_type' => 'ALL', |
---|
| 77 | 'sort_box_reverse' => 1 ); |
---|
| 78 | |
---|
| 79 | $result = $this->to_utf8($this->get_range_msgs2($service_params)); |
---|
| 80 | //return var_export($result); |
---|
| 81 | |
---|
| 82 | $response = array( "page" => $page, "total" => $total_pages, "records" => $result['num_msgs'] ); |
---|
| 83 | |
---|
| 84 | for ($i=0; $i<count($result); $i++) |
---|
| 85 | { |
---|
| 86 | $flags_enum = array('Recent', 'Unseen', 'Answered', 'Draft', 'Deleted', 'Flagged'); |
---|
| 87 | |
---|
| 88 | foreach ($flags_enum as $key => $flag) |
---|
| 89 | { |
---|
| 90 | if ( !isset($result[$i][$flag]) || !trim($result[$i][$flag]) || trim($result[$i][$flag]) == '') |
---|
| 91 | unset($flags_enum[$key]); |
---|
| 92 | unset($result[$i][$flag]); |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | if (array_key_exists($i, $result)) |
---|
| 96 | { |
---|
[5188] | 97 | $response["rows"][$i] = $result[$i]; |
---|
| 98 | $response["rows"][$i]['body'] = $this->to_utf8($result[$i]['body']); |
---|
| 99 | $response["rows"][$i]['timestamp'] = ( ( $result[$i]['udate']) * 1000 ); |
---|
| 100 | //USA OS TIME ZONES |
---|
| 101 | //$response["rows"][$i]['timestamp'] = ( ( $result[$i]['udate'] + $this->functions->CalculateDateOffset()) * 1000 ); |
---|
| 102 | $response["rows"][$i]['flags'] = implode(',', $flags_enum); |
---|
| 103 | $response["rows"][$i]['size'] = $response["rows"][$i]['Size']; |
---|
| 104 | $response["rows"][$i]['udate'] = ( $result[$i]['udate'] + $this->functions->CalculateDateOffset() * 1000 ); |
---|
| 105 | unset($response["rows"][$i]['Size']); |
---|
[5136] | 106 | } |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | break; |
---|
| 110 | } |
---|
| 111 | } |
---|
| 112 | return $response; |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | public function retrieve( $concept, $id, $parents, $justthese = false, $criteria = false ) |
---|
| 116 | { |
---|
| 117 | return $this->read( array( 'id' => $id, |
---|
| 118 | 'concept' => $concept, |
---|
| 119 | 'context' => $parents ), $justthese ); |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | public function read( $URI, $justthese = false ) |
---|
| 123 | { |
---|
| 124 | $folder_name = str_replace( '.', $this->imap_delimiter, $URI['context']['folder'] ); |
---|
| 125 | |
---|
| 126 | $response = $this->get_info_msg( array('msg_number'=>$URI['id'],'msg_folder'=>$folder_name) ); |
---|
| 127 | |
---|
| 128 | return $this->to_utf8( $response ); |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | public function create( $URI, $data ) |
---|
| 132 | {} |
---|
| 133 | |
---|
| 134 | public function delete( $URI, $justthese = false, $criteria = false ) |
---|
| 135 | {} |
---|
| 136 | |
---|
| 137 | public function deleteAll( $URI, $justthese = false, $criteria = false ) // avaliar |
---|
| 138 | {} |
---|
| 139 | |
---|
| 140 | public function update( $URI, $data, $criteria = false ) |
---|
| 141 | {} |
---|
| 142 | |
---|
| 143 | public function replace( $URI, $data, $criteria = false ) |
---|
| 144 | {} |
---|
| 145 | |
---|
| 146 | public function close() |
---|
| 147 | {} |
---|
| 148 | |
---|
| 149 | public function setup() |
---|
| 150 | {} |
---|
| 151 | |
---|
| 152 | public function teardown() |
---|
| 153 | {} |
---|
| 154 | |
---|
| 155 | function to_utf8($in) |
---|
| 156 | { |
---|
| 157 | if (is_array($in)) { |
---|
| 158 | foreach ($in as $key => $value) { |
---|
| 159 | $out[$this->to_utf8($key)] = $this->to_utf8($value); |
---|
| 160 | } |
---|
| 161 | } elseif(is_string($in)) { |
---|
| 162 | //if(mb_detect_encoding($in) != "UTF-8") |
---|
[5204] | 163 | return mb_convert_encoding( $in , 'UTF-8' , 'UTF-8 , ISO-8859-1' ); |
---|
[5136] | 164 | //else |
---|
| 165 | // return $in; |
---|
| 166 | } else { |
---|
| 167 | return $in; |
---|
| 168 | } |
---|
| 169 | return $out; |
---|
[5204] | 170 | } |
---|
| 171 | |
---|
[5136] | 172 | } |
---|