PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/Webmails/MailBox.php

https://bitbucket.org/yousef_fadila/vtiger
PHP | 266 lines | 215 code | 34 blank | 17 comment | 54 complexity | eab4485adfdd35337c48916d209191f7 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
  1. <?php
  2. /*+********************************************************************************
  3. * The contents of this file are subject to the vtiger CRM Public License Version 1.0
  4. * ("License"); You may not use this file except in compliance with the License
  5. * The Original Code is: vtiger CRM Open Source
  6. * The Initial Developer of the Original Code is vtiger.
  7. * Portions created by vtiger are Copyright (C) vtiger.
  8. * Portions created by FOSS Labs are Copyright (C) FOSS Labs.
  9. * All Rights Reserved.
  10. ************************************************************************************/
  11. //Modified By Krem on 30/05/2008 - Details at http://creadev.net/Webmails-vTiger504
  12. include_once('config.php');
  13. require_once('include/logging.php');
  14. require_once('include/utils/utils.php');
  15. class MailBox {
  16. var $mbox;
  17. var $db;
  18. var $boxinfo;
  19. var $readonly='false';
  20. var $enabled;
  21. var $login_username;
  22. var $secretkey;
  23. var $imapServerAddress;
  24. var $ssltype;
  25. var $sslmeth;
  26. var $box_refresh;
  27. var $mails_per_page;
  28. var $mail_protocol;
  29. var $account_name;
  30. var $display_name;
  31. var $mailbox;
  32. var $mailList;
  33. function MailBox($mailbox = '',$p='',$s='') {
  34. global $current_user;
  35. require_once('include/utils/encryption.php');
  36. $oencrypt = new Encryption();
  37. $this->db = PearDatabase::getInstance();
  38. $this->db->println("Entering MailBox($mailbox)");
  39. $this->mailbox = $mailbox;
  40. $tmp = getMailServerInfo($current_user);
  41. if($this->db->num_rows($tmp) < 1)
  42. $this->enabled = 'false';
  43. else
  44. $this->enabled = 'true';
  45. $this->boxinfo = $this->db->fetch_array($tmp);
  46. $this->login_username=trim($this->boxinfo["mail_username"]);
  47. $this->secretkey=$oencrypt->decrypt(trim($this->boxinfo["mail_password"]));
  48. $this->imapServerAddress=gethostbyname(trim($this->boxinfo["mail_servername"]));
  49. $this->mail_protocol=$this->boxinfo["mail_protocol"];
  50. $this->ssltype=$this->boxinfo["ssltype"];
  51. $this->sslmeth=$this->boxinfo["sslmeth"];
  52. $this->box_refresh=trim($this->boxinfo["box_refresh"]);
  53. $this->mails_per_page=trim($this->boxinfo["mails_per_page"]);
  54. if($this->mails_per_page < 1)
  55. $this->mails_per_page=20;
  56. $this->account_name=$this->boxinfo["account_name"];
  57. $this->display_name=$this->boxinfo["display_name"];
  58. //$this->imapServerAddress=$this->boxinfo["mail_servername"];
  59. $this->db->println("Setting Mailbox Name");
  60. if($this->mailbox != "")
  61. $this->mailbox=$mailbox;
  62. $this->db->println("Opening Mailbox");
  63. if(!$this->mbox && $this->mailbox != "")
  64. $this->getImapMbox();
  65. $this->db->println("Loading mail list");
  66. $pa=$p;
  67. $se=$s;
  68. if($this->mbox){
  69. if ($se != ""){$this->mailList = $this->searchMailList($se,$pa);}
  70. else if ($pa == ""){$this->mailList = $this->customMailList(0);}
  71. else {$this->mailList = $this->customMailList($pa);}
  72. }
  73. $this->db->println("Exiting MailBox($mailbox)");
  74. }
  75. function customMailList($page) {
  76. $info = imap_check($this->mbox);
  77. $numEmails = $info->Nmsgs;
  78. $current_mails = ceil($page*$this->mails_per_page);
  79. $current_mails = $numEmails - $current_mails;
  80. $start =$current_mails-$this->mails_per_page+1;
  81. if ($start<=0) $start=1;
  82. if ($current_mails<=0)$current_mails=0;
  83. $mailOverviews = @imap_fetch_overview($this->mbox, "$start:$current_mails", 0);
  84. $out = array("overview"=>$mailOverviews,"count"=>$numEmails);
  85. return $out;
  86. }
  87. function searchMailList($searchstring,$page) {
  88. $search="";
  89. $searchlist = Array();
  90. $searchlist = imap_search($this->mbox,$searchstring);
  91. if ($searchlist==false) return $out;
  92. $num_searches = count($searchlist);
  93. if ($num_searches < $this->mails_per_page){
  94. $current_mails = $num_searches -1;
  95. }else{
  96. $current_mails = ceil($page*$this->mails_per_page);
  97. $current_mails = $num_searches - $current_mails-1;
  98. }
  99. $start = $current_mails-$this->mails_per_page;
  100. if ($start < 0)$start=0;
  101. $j=0;
  102. for($i=$current_mails; $i >= $start ; $i--){
  103. if($i==$current_mails){
  104. $search=$searchlist[$i];
  105. }else $search=$search.",".$searchlist[$i];
  106. $j++;
  107. }
  108. if ($search!="")
  109. $result = @imap_fetch_overview($this->mbox, "$search",0);
  110. $out = array("overview"=>$result,"count"=>count($searchlist));
  111. return $out;
  112. }
  113. function fullMailList() {
  114. $mailHeaders = @imap_headers($this->mbox);
  115. $numEmails = sizeof($mailHeaders);
  116. $mailOverviews = @imap_fetch_overview($this->mbox, "1:$numEmails", 0);
  117. $out = array("headers"=>$mailHeaders,"overview"=>$mailOverviews,"count"=>$numEmails);
  118. return $out;
  119. }
  120. function isBase64($iVal){
  121. $_tmp=preg_replace("/[^A-Z0-9\+\/\=]/i","",$iVal);
  122. return (strlen($_tmp) % 4 == 0 ) ? "y" : "n";
  123. }
  124. function getImapMbox() {
  125. $this->db->println("Entering getImapMbox()");
  126. $mods = parsePHPModules();
  127. $this->db->println("Parsing PHP Modules");
  128. // first we will try a regular old IMAP connection:
  129. if($this->ssltype == "") {$this->ssltype = "notls";}
  130. if($this->sslmeth == "") {$this->sslmeth = "novalidate-cert";}
  131. if($this->mail_protocol == "pop3")
  132. $port = "110";
  133. else
  134. {
  135. if($mods["imap"]["SSL Support"] == "enabled" && ($this->ssltype == "tls" || $this->ssltype == "ssl"))
  136. $port = "993";
  137. else
  138. $port = "143";
  139. }
  140. $this->db->println("Building connection string");
  141. if(preg_match("/@/",$this->login_username))
  142. {
  143. $mailparts = split("@",$this->login_username);
  144. $user="".trim($mailparts[0])."";
  145. $domain="".trim($mailparts[1])."";
  146. // This section added to fix a bug when connecting as user@domain.com
  147. if($this->readonly == "true")
  148. {
  149. if($mods["imap"]["SSL Support"] == "enabled")
  150. $connectString = "/".$this->ssltype."/".$this->sslmeth."/user={$user}@{$domain}/readonly";
  151. else
  152. $connectString = "/notls/novalidate-cert/user={$user}@{$domain}/readonly";
  153. }
  154. else
  155. {
  156. if($mods["imap"]["SSL Support"] == "enabled")
  157. $connectString = "/".$this->ssltype."/".$this->sslmeth."/user={$user}@{$domain}";
  158. else
  159. $connectString = "/notls/novalidate-cert/user={$user}@{$domain}";
  160. }
  161. }
  162. else
  163. {
  164. if($this->readonly == "true")
  165. {
  166. if($mods["imap"]["SSL Support"] == "enabled")
  167. $connectString = "/".$this->ssltype."/".$this->sslmeth."/readonly";
  168. else
  169. $connectString = "/notls/novalidate-cert/readonly";
  170. }
  171. else
  172. {
  173. if($mods["imap"]["SSL Support"] == "enabled")
  174. $connectString = "/".$this->ssltype."/".$this->sslmeth;
  175. else
  176. $connectString = "/notls/novalidate-cert";
  177. }
  178. }
  179. //$connectString = "{".$this->imapServerAddress."/".$this->mail_protocol.":".$port.$connectString."}".$this->mailbox;
  180. $connectString = "{".$this->imapServerAddress.":".$port."/".$this->mail_protocol.$connectString."}".$this->mailbox;
  181. //Reference - http://forums.vtiger.com/viewtopic.php?p=33478#33478 - which has no tls or validate-cert
  182. $connectString1 = "{".$this->imapServerAddress."/".$this->mail_protocol.":".$port."}".$this->mailbox;
  183. $this->db->println("Done Building Connection String.. $connectString Connecting to box");
  184. //checking the imap support in php
  185. if(!function_exists('imap_open'))
  186. {
  187. echo "<strong>".$mod_strings['LBL_ENABLE_IMAP_SUPPORT']."</strong>";
  188. exit();
  189. }
  190. if(!$this->mbox = @imap_open($connectString, $this->login_username, $this->secretkey))
  191. {
  192. //try second string which has no tls or validate-cert
  193. if(!$this->mbox = @imap_open($connectString1, $this->login_username, $this->secretkey))
  194. {
  195. global $current_user,$mod_strings;
  196. $this->db->println("CONNECTION ERROR - Could not be connected to the server using imap_open function through the connection strings $connectString and $connectString1");
  197. echo "<br>&nbsp;<b>".$mod_strings['LBL_MAIL_CONNECT_ERROR']."<a href='index.php?module=Users&action=AddMailAccount&return_module=Webmails&return_action=index&record=".$current_user->id."'> ".$mod_strings['LBL_HERE']."</a>. ".$mod_strings['LBL_PLEASE']." <a href='index.php?module=Emails&action=index&parenttab=".vtlib_purify($_REQUEST['parenttab'])."'>".$mod_strings['LBL_CLICK_HERE']."</a>".$mod_strings['LBL_GOTO_EMAILS_MODULE']." </b>";
  198. exit;
  199. }
  200. }
  201. $this->db->println("Done connecting to box");
  202. }
  203. } // END CLASS
  204. function parsePHPModules() {
  205. ob_start();
  206. phpinfo(INFO_MODULES);
  207. $s = ob_get_contents();
  208. ob_end_clean();
  209. $s = strip_tags($s,'<h2><th><td>');
  210. $s = preg_replace('/<th[^>]*>([^<]+)<\/th>/',"<info>\\1</info>",$s);
  211. $s = preg_replace('/<td[^>]*>([^<]+)<\/td>/',"<info>\\1</info>",$s);
  212. $vTmp = preg_split('/(<h2>[^<]+<\/h2>)/',$s,-1,PREG_SPLIT_DELIM_CAPTURE);
  213. $vModules = array();
  214. for ($i=1;$i<count($vTmp);$i++) {
  215. if (preg_match('/<h2>([^<]+)<\/h2>/',$vTmp[$i],$vMat)) {
  216. $vName = trim($vMat[1]);
  217. $vTmp2 = explode("\n",$vTmp[$i+1]);
  218. foreach ($vTmp2 AS $vOne) {
  219. $vPat = '<info>([^<]+)<\/info>';
  220. $vPat3 = "/$vPat\s*$vPat\s*$vPat/";
  221. $vPat2 = "/$vPat\s*$vPat/";
  222. if (preg_match($vPat3,$vOne,$vMat)) { // 3cols
  223. $vModules[$vName][trim($vMat[1])] = array(trim($vMat[2]),trim($vMat[3]));
  224. } elseif (preg_match($vPat2,$vOne,$vMat)) { // 2cols
  225. $vModules[$vName][trim($vMat[1])] = trim($vMat[2]);
  226. }
  227. }
  228. }
  229. }
  230. return $vModules;
  231. }
  232. ?>