[ Index ] |
PHP Cross Reference of vtigercrm-6.1.0 |
[Summary view] [Print] [Text view]
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 * All Rights Reserved. 9 * 10 ********************************************************************************/ 11 require_once ('modules/Settings/MailConverter/handlers/MailScannerRule.php'); 12 13 /** 14 * Mail Scanner information manager. 15 */ 16 class Vtiger_MailScannerInfo { 17 // id of this scanner record 18 var $scannerid = false; 19 // name of this scanner 20 var $scannername=false; 21 // mail server to connect to 22 var $server = false; 23 // mail protocol to use 24 var $protocol = false; 25 // username to use 26 var $username = false; 27 // password to use 28 var $password = false; 29 // notls/tls/ssl 30 var $ssltype = false; 31 // validate-certificate or novalidate-certificate 32 var $sslmethod = false; 33 // last successful connection url to use 34 var $connecturl= false; 35 // search for type 36 var $searchfor = false; 37 // post scan mark record as 38 var $markas = false; 39 // server time_zone 40 var $time_zone = false; 41 42 // is the scannered enabled? 43 var $isvalid = false; 44 45 // Last scan on the folders. 46 var $lastscan = false; 47 48 // Need rescan on the folders? 49 var $rescan = false; 50 51 // Rules associated with this mail scanner 52 var $rules = false; 53 54 /** 55 * Constructor 56 */ 57 function __construct($scannername, $initialize=true) { 58 if($initialize && $scannername) $this->initialize($scannername); 59 } 60 61 /** 62 * Encrypt/Decrypt input. 63 * @access private 64 */ 65 function __crypt($password, $encrypt=true) { 66 require_once ('include/utils/encryption.php'); 67 $cryptobj = new Encryption(); 68 if($encrypt) return $cryptobj->encrypt(trim($password)); 69 else return $cryptobj->decrypt(trim($password)); 70 } 71 72 /** 73 * Initialize this instance. 74 */ 75 function initialize($scannername) { 76 global $adb; 77 $result = $adb->pquery("SELECT * FROM vtiger_mailscanner WHERE scannername=?", Array($scannername)); 78 79 if($adb->num_rows($result)) { 80 $this->scannerid = $adb->query_result($result, 0, 'scannerid'); 81 $this->scannername= $adb->query_result($result, 0, 'scannername'); 82 $this->server = $adb->query_result($result, 0, 'server'); 83 $this->protocol = $adb->query_result($result, 0, 'protocol'); 84 $this->username = $adb->query_result($result, 0, 'username'); 85 $this->password = $adb->query_result($result, 0, 'password'); 86 $this->password = $this->__crypt($this->password, false); 87 $this->ssltype = $adb->query_result($result, 0, 'ssltype'); 88 $this->sslmethod = $adb->query_result($result, 0, 'sslmethod'); 89 $this->connecturl = $adb->query_result($result, 0, 'connecturl'); 90 $this->searchfor = $adb->query_result($result, 0, 'searchfor'); 91 $this->markas = $adb->query_result($result, 0, 'markas'); 92 $this->isvalid = $adb->query_result($result, 0, 'isvalid'); 93 $this->time_zone = $adb->query_result($result, 0, 'time_zone'); 94 95 $this->initializeFolderInfo(); 96 $this->initializeRules(); 97 } 98 } 99 100 /** 101 * Initialize the folder details 102 */ 103 function initializeFolderInfo() { 104 global $adb; 105 if($this->scannerid) { 106 $this->lastscan = Array(); 107 $this->rescan = Array(); 108 $lastscanres = $adb->pquery("SELECT * FROM vtiger_mailscanner_folders WHERE scannerid=?",Array($this->scannerid)); 109 $lastscancount = $adb->num_rows($lastscanres); 110 if($lastscancount) { 111 for($lsindex = 0; $lsindex < $lastscancount; ++$lsindex) { 112 $folder = $adb->query_result($lastscanres, $lsindex, 'foldername'); 113 $scannedon =$adb->query_result($lastscanres, $lsindex, 'lastscan'); 114 $nextrescan =$adb->query_result($lastscanres, $lsindex, 'rescan'); 115 $this->lastscan[$folder] = $scannedon; 116 $this->rescan[$folder] = ($nextrescan == 0)? false : true; 117 } 118 } 119 } 120 } 121 122 /** 123 * Delete lastscan details with this scanner 124 */ 125 function clearLastscan() { 126 global $adb; 127 $adb->pquery("DELETE FROM vtiger_mailscanner_folders WHERE scannerid=?", Array($this->scannerid)); 128 $this->lastscan = false; 129 } 130 131 /** 132 * Update rescan flag on all folders 133 */ 134 function updateAllFolderRescan($rescanFlag=false) { 135 global $adb; 136 $useRescanFlag = $rescanFlag? 1 : 0; 137 $adb->pquery("UPDATE vtiger_mailscanner_folders set rescan=? WHERE scannerid=?", 138 Array($rescanFlag, $this->scannerid)); 139 if($this->rescan) { 140 foreach($this->rescan as $folderName=>$oldRescanFlag) { 141 $this->rescan[$folderName] = $rescanFlag; 142 } 143 } 144 } 145 146 function dateBasedOnMailServerTimezone($format='d-M-Y') { 147 $returnDate = NULL; 148 ##--Fix for trac : http://trac.vtiger.com/cgi-bin/trac.cgi/ticket/8051-## 149 if ($this->timezone && trim($this->timezone)) { 150 $currentTZ = date_default_timezone_get(); 151 list ($tzhours, $tzminutes) = explode(':', trim($this->time_zone)); 152 $returnDate = date($format, strtotime(sprintf("%s hours %s minutes", $tzhours, $tzminutes))); 153 date_default_timezone_set($currentTZ); 154 } else { 155 // Search email one-day before to overcome timezone differences. 156 $returnDate = date($format, strtotime("-1 day")); 157 } 158 return $returnDate; 159 } 160 161 /** 162 * Update lastscan information on folder (or set for rescan next) 163 */ 164 function updateLastscan($folderName, $rescanFolder=false, $enabledForScan=1) { 165 global $adb; 166 167 $scannedOn = $this->dateBasedOnMailServerTimezone('d-M-Y'); 168 169 $needRescan = $rescanFolder? 1 : 0; 170 171 $folderInfo = $adb->pquery("SELECT folderid FROM vtiger_mailscanner_folders WHERE scannerid=? AND foldername=?", 172 Array($this->scannerid, $folderName)); 173 if($adb->num_rows($folderInfo)) { 174 $folderid = $adb->query_result($folderInfo, 0, 'folderid'); 175 $adb->pquery("UPDATE vtiger_mailscanner_folders SET lastscan=?, rescan=? WHERE folderid=?", 176 Array($scannedOn, $needRescan, $folderid)); 177 } else { 178 $adb->pquery("INSERT INTO vtiger_mailscanner_folders(scannerid, foldername, lastscan, rescan, enabled) 179 VALUES(?,?,?,?,?)", Array($this->scannerid, $folderName, $scannedOn, $needRescan, $enabledForScan)); 180 } 181 if(!$this->lastscan) $this->lastscan = Array(); 182 $this->lastscan[$folderName] = $scannedOn; 183 184 if(!$this->rescan) $this->rescan = Array(); 185 $this->rescan[$folderName] = $needRescan; 186 } 187 188 /** 189 * Get lastscan of the folder. 190 */ 191 function getLastscan($folderName) { 192 if($this->lastscan) return $this->lastscan[$folderName]; 193 else return false; 194 } 195 196 /** 197 * Does the folder need message rescan? 198 */ 199 function needRescan($folderName) { 200 if($this->rescan && isset($this->rescan[$folderName])) { 201 return $this->rescan[$folderName]; 202 } 203 // TODO Pick details of rescan flag of folder from database? 204 return false; 205 } 206 207 /** 208 * Check if rescan is required atleast on a folder? 209 */ 210 function checkRescan() { 211 $rescanRequired = false; 212 if($this->rescan) { 213 foreach($this->rescan as $folderName=>$rescan) { 214 if($rescan) { 215 $rescanRequired = $folderName; 216 break; 217 } 218 } 219 } 220 return $rescanRequired; 221 } 222 223 /** 224 * Get the folder information that has been scanned 225 */ 226 function getFolderInfo() { 227 $folderinfo = false; 228 if($this->scannerid) { 229 global $adb; 230 $fldres = $adb->pquery("SELECT * FROM vtiger_mailscanner_folders WHERE scannerid=?", Array($this->scannerid)); 231 $fldcount = $adb->num_rows($fldres); 232 if($fldcount) { 233 $folderinfo = Array(); 234 for($index = 0; $index < $fldcount; ++$index) { 235 $foldername = $adb->query_result($fldres, $index, 'foldername'); 236 $folderid = $adb->query_result($fldres, $index, 'folderid'); 237 $lastscan = $adb->query_result($fldres, $index, 'lastscan'); 238 $rescan = $adb->query_result($fldres, $index, 'rescan'); 239 $enabled = $adb->query_result($fldres, $index, 'enabled'); 240 $folderinfo[$foldername] = Array ('folderid'=>$folderid, 'lastscan'=>$lastscan, 'rescan'=> $rescan, 'enabled'=>$enabled); 241 } 242 } 243 } 244 return $folderinfo; 245 } 246 247 /** 248 * Update the folder information with given folder names 249 */ 250 function updateFolderInfo($foldernames, $rescanFolder=false) { 251 if($this->scannerid && !empty($foldernames)) { 252 global $adb; 253 $qmarks = Array(); 254 foreach($foldernames as $foldername) { 255 $qmarks[] = '?'; 256 $this->updateLastscan($foldername, $rescanFolder); 257 } 258 // Delete the folder that is no longer present 259 $adb->pquery("DELETE FROM vtiger_mailscanner_folders WHERE scannerid=? AND foldername NOT IN 260 (". implode(',', $qmarks) . ")", Array($this->scannerid, $foldernames)); 261 } 262 } 263 264 /** 265 * Enable only given folders for scanning 266 */ 267 function enableFoldersForScan($folderinfo) { 268 if($this->scannerid) { 269 global $adb; 270 $adb->pquery("UPDATE vtiger_mailscanner_folders set enabled=0 WHERE scannerid=?", Array($this->scannerid)); 271 foreach($folderinfo as $foldername=>$foldervalue) { 272 $folderid = $foldervalue[folderid]; 273 $enabled = $foldervalue[enabled]; 274 $adb->pquery("UPDATE vtiger_mailscanner_folders set enabled=? WHERE folderid=? AND scannerid=?", 275 Array($enabled,$folderid,$this->scannerid)); 276 } 277 } 278 } 279 280 /** 281 * Initialize scanner rule information 282 */ 283 function initializeRules() { 284 global $adb; 285 if($this->scannerid) { 286 $this->rules = Array(); 287 $rulesres = $adb->pquery("SELECT * FROM vtiger_mailscanner_rules WHERE scannerid=? ORDER BY sequence",Array($this->scannerid)); 288 $rulescount = $adb->num_rows($rulesres); 289 if($rulescount) { 290 for($index = 0; $index < $rulescount; ++$index) { 291 $ruleid = $adb->query_result($rulesres, $index, 'ruleid'); 292 $scannerrule = new Vtiger_MailScannerRule($ruleid); 293 $scannerrule->debug = $this->debug; 294 $this->rules[] = $scannerrule; 295 } 296 } 297 } 298 } 299 300 /** 301 * Get scanner information as map 302 */ 303 function getAsMap() { 304 $infomap = Array(); 305 $keys = Array('scannerid', 'scannername', 'server', 'protocol', 'username', 'password', 'ssltype', 306 'sslmethod', 'connecturl', 'searchfor', 'markas', 'isvalid', 'time_zone', 'rules'); 307 foreach($keys as $key) { 308 $infomap[$key] = $this->$key; 309 } 310 $infomap['requireRescan'] = $this->checkRescan(); 311 return $infomap; 312 } 313 314 /** 315 * Compare this instance with give instance 316 */ 317 function compare($otherInstance) { 318 $checkkeys = Array('server', 'scannername', 'protocol', 'username', 'password', 'ssltype', 'sslmethod', 'searchfor', 'markas'); 319 foreach($checkkeys as $key) { 320 if($this->$key != $otherInstance->$key) return false; 321 } 322 return true; 323 } 324 325 /** 326 * Create/Update the scanner information in database 327 */ 328 function update($otherInstance) { 329 $mailServerChanged = false; 330 331 // Is there is change in server setup? 332 if($this->server != $otherInstance->server || $this->username != $otherInstance->username) { 333 $mailServerChanged = true; 334 $this->clearLastscan(); 335 // TODO How to handle lastscan info if server settings switches back in future? 336 } 337 338 $this->server = $otherInstance->server; 339 $this->scannername= $otherInstance->scannername; 340 $this->protocol = $otherInstance->protocol; 341 $this->username = $otherInstance->username; 342 $this->password = $otherInstance->password; 343 $this->ssltype = $otherInstance->ssltype; 344 $this->sslmethod = $otherInstance->sslmethod; 345 $this->connecturl= $otherInstance->connecturl; 346 $this->searchfor = $otherInstance->searchfor; 347 $this->markas = $otherInstance->markas; 348 $this->isvalid = $otherInstance->isvalid; 349 $this->time_zone = $otherInstance->time_zone; 350 351 $useisvalid = ($this->isvalid)? 1 : 0; 352 353 $usepassword = $this->__crypt($this->password); 354 355 global $adb; 356 if($this->scannerid == false) { 357 $adb->pquery("INSERT INTO vtiger_mailscanner(scannername,server,protocol,username,password,ssltype, 358 sslmethod,connecturl,searchfor,markas,isvalid,time_zone) VALUES(?,?,?,?,?,?,?,?,?,?,?,?)", 359 Array($this->scannername,$this->server, $this->protocol, $this->username, $usepassword, 360 $this->ssltype, $this->sslmethod, $this->connecturl, $this->searchfor, $this->markas, $useisvalid, $this->time_zone)); 361 $this->scannerid = $adb->database->Insert_ID(); 362 } else { //this record is exist in the data 363 $adb->pquery("UPDATE vtiger_mailscanner SET scannername=?,server=?,protocol=?,username=?,password=?,ssltype=?, 364 sslmethod=?,connecturl=?,searchfor=?,markas=?,isvalid=?, time_zone=? WHERE scannerid=?", 365 Array($this->scannername,$this->server,$this->protocol, $this->username, $usepassword, $this->ssltype, 366 $this->sslmethod, $this->connecturl,$this->searchfor, $this->markas,$useisvalid, $this->time_zone, $this->scannerid)); 367 } 368 369 return $mailServerChanged; 370 } 371 372 /** 373 * Delete the scanner information from database 374 */ 375 function delete() { 376 global $adb; 377 378 // Delete dependencies 379 if(!empty($this->rules)) { 380 foreach($this->rules as $rule) { 381 $rule->delete(); 382 } 383 } 384 385 if($this->scannerid) { 386 $tables = Array( 387 'vtiger_mailscanner', 388 'vtiger_mailscanner_ids', 389 'vtiger_mailscanner_folders' 390 ); 391 foreach($tables as $table) { 392 $adb->pquery("DELETE FROM $table WHERE scannerid=?", Array($this->scannerid)); 393 } 394 $adb->pquery("DELETE FROM vtiger_mailscanner_ruleactions 395 WHERE actionid in (SELECT actionid FROM vtiger_mailscanner_actions WHERE scannerid=?)", Array($this->scannerid)); 396 $adb->pquery("DELETE FROM vtiger_mailscanner_actions WHERE scannerid=?", Array($this->scannerid)); 397 } 398 } 399 400 /** 401 * List all the mail-scanners configured. 402 */ 403 static function listAll() { 404 $scanners = array(); 405 406 global $adb; 407 $result = $adb->pquery("SELECT scannername FROM vtiger_mailscanner", array()); 408 if($result && $adb->num_rows($result)) { 409 while($resultrow = $adb->fetch_array($result)) { 410 $scanners[] = new self( decode_html($resultrow['scannername'] )); 411 } 412 } 413 return $scanners; 414 } 415 } 416 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:08:37 2014 | Cross-referenced by PHPXref 0.7.1 |