filters.php 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  1. <?php
  2. /**
  3. * Message and Spam Filter Plugin - Filtering Functions
  4. *
  5. * @copyright &copy; 1999-2006 The SquirrelMail Project Team
  6. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  7. * @version $Id$
  8. * @package plugins
  9. * @subpackage filters
  10. */
  11. /**
  12. * do not allow to call this file directly
  13. */
  14. if ((isset($_SERVER['SCRIPT_FILENAME']) && $_SERVER['SCRIPT_FILENAME'] == __FILE__) ||
  15. (isset($HTTP_SERVER_SERVER['SCRIPT_FILENAME']) && $HTTP_SERVER_SERVER['SCRIPT_FILENAME'] == __FILE__) ) {
  16. header("Location: ../../src/login.php");
  17. die();
  18. }
  19. /** load globals */
  20. global $UseSeparateImapConnection,
  21. $AllowSpamFilters, $SpamFilters_YourHop, $SpamFilters_ShowCommercial,
  22. $SpamFilters_DNScache, $SpamFilters_BulkQuery, $SpamFilters_SharedCache,
  23. $SpamFilters_CacheTTL;
  24. /**
  25. * load required functions. Plugin depends on IMAP functions and they are not
  26. * loaded in src/webmail.php
  27. */
  28. include_once (SM_PATH . 'functions/imap.php');
  29. /** load default config */
  30. if (file_exists(SM_PATH . 'plugins/filters/config_default.php')) {
  31. include_once (SM_PATH . 'plugins/filters/config_default.php');
  32. } else {
  33. // default config was removed.
  34. $UseSeparateImapConnection = false;
  35. $AllowSpamFilters = true;
  36. $SpamFilters_YourHop = ' ';
  37. $SpamFilters_ShowCommercial = false;
  38. $SpamFilters_DNScache = array();
  39. $SpamFilters_BulkQuery = '';
  40. $SpamFilters_SharedCache = true;
  41. $SpamFilters_CacheTTL = 7200;
  42. }
  43. if (file_exists(SM_PATH . 'config/filters_config.php')) {
  44. include_once (SM_PATH . 'config/filters_config.php');
  45. } elseif (file_exists(SM_PATH . 'plugins/filters/config.php')) {
  46. include_once (SM_PATH . 'plugins/filters/config.php');
  47. }
  48. /**
  49. * Register option blocks
  50. * @access private
  51. */
  52. function filters_optpage_register_block() {
  53. global $optpage_blocks, $AllowSpamFilters;
  54. $optpage_blocks[] = array(
  55. 'name' => _("Message Filters"),
  56. 'url' => SM_PATH . 'plugins/filters/options.php',
  57. 'desc' => _("Filtering enables messages with different criteria to be automatically filtered into different folders for easier organization."),
  58. 'js' => false
  59. );
  60. if ($AllowSpamFilters) {
  61. $optpage_blocks[] = array(
  62. 'name' => _("SPAM Filters"),
  63. 'url' => SM_PATH . 'plugins/filters/spamoptions.php',
  64. 'desc' => _("SPAM filters allow you to select from various DNS based blacklists to detect junk email in your INBOX and move it to another folder (like Trash)."),
  65. 'js' => false
  66. );
  67. }
  68. }
  69. /* Receive the status of the folder and do something with it */
  70. function filters_folder_status($statusarr) {
  71. global $filter_inbox_count;
  72. if (empty($filter_inbox_count)) $filter_inbox_count=0;
  73. if ($statusarr['MAILBOX'] == 'INBOX')
  74. {
  75. if (!empty($statusarr['MESSAGES'])) $filter_inbox_count=$statusarr['MESSAGES'];
  76. }
  77. }
  78. /**
  79. * Saves the DNS Cache to disk
  80. * @access private
  81. */
  82. function filters_SaveCache () {
  83. global $data_dir, $SpamFilters_DNScache;
  84. if (file_exists($data_dir . '/dnscache')) {
  85. $fp = fopen($data_dir . '/dnscache', 'r');
  86. } else {
  87. $fp = false;
  88. }
  89. if ($fp) {
  90. flock($fp,LOCK_EX);
  91. } else {
  92. $fp = fopen($data_dir . '/dnscache', 'w+');
  93. fclose($fp);
  94. $fp = fopen($data_dir . '/dnscache', 'r');
  95. flock($fp,LOCK_EX);
  96. }
  97. $fp1 = fopen($data_dir . '/dnscache', 'w+');
  98. foreach ($SpamFilters_DNScache as $Key=> $Value) {
  99. $tstr = $Key . ',' . $Value['L'] . ',' . $Value['T'] . "\n";
  100. fputs ($fp1, $tstr);
  101. }
  102. fclose($fp1);
  103. flock($fp,LOCK_UN);
  104. fclose($fp);
  105. }
  106. /**
  107. * Loads the DNS Cache from disk
  108. * @access private
  109. */
  110. function filters_LoadCache () {
  111. global $data_dir, $SpamFilters_DNScache;
  112. if (file_exists($data_dir . '/dnscache')) {
  113. $SpamFilters_DNScache = array();
  114. if ($fp = fopen ($data_dir . '/dnscache', 'r')) {
  115. flock($fp,LOCK_SH);
  116. while ($data = fgetcsv($fp,1024)) {
  117. if ($data[2] > time()) {
  118. $SpamFilters_DNScache[$data[0]]['L'] = $data[1];
  119. $SpamFilters_DNScache[$data[0]]['T'] = $data[2];
  120. }
  121. }
  122. flock($fp,LOCK_UN);
  123. }
  124. }
  125. }
  126. /**
  127. * Uses the BulkQuery executable to query all the RBLs at once
  128. * @param array $filters Array of SPAM Fitlers
  129. * @param array $IPs Array of IP Addresses
  130. * @access private
  131. */
  132. function filters_bulkquery($filters, $IPs) {
  133. global $attachment_dir, $username,
  134. $SpamFilters_DNScache, $SpamFilters_BulkQuery,
  135. $SpamFilters_CacheTTL;
  136. if (count($IPs) > 0) {
  137. $rbls = array();
  138. foreach ($filters as $key => $value) {
  139. if ($filters[$key]['enabled']) {
  140. if ($filters[$key]['dns']) {
  141. $rbls[$filters[$key]['dns']] = true;
  142. }
  143. }
  144. }
  145. $bqfil = $attachment_dir . $username . '-bq.in';
  146. $fp = fopen($bqfil, 'w');
  147. fputs ($fp, $SpamFilters_CacheTTL . "\n");
  148. foreach ($rbls as $key => $value) {
  149. fputs ($fp, '.' . $key . "\n");
  150. }
  151. fputs ($fp, "----------\n");
  152. foreach ($IPs as $key => $value) {
  153. fputs ($fp, $key . "\n");
  154. }
  155. fclose ($fp);
  156. $bqout = array();
  157. exec ($SpamFilters_BulkQuery . ' < ' . $bqfil, $bqout);
  158. foreach ($bqout as $value) {
  159. $Chunks = explode(',', $value);
  160. $SpamFilters_DNScache[$Chunks[0]]['L'] = $Chunks[1];
  161. $SpamFilters_DNScache[$Chunks[0]]['T'] = $Chunks[2] + time();
  162. }
  163. unlink($bqfil);
  164. }
  165. }
  166. /**
  167. * Starts the filtering process
  168. * @param array $hook_args (since 1.5.2) do hook arguments. Is used to check
  169. * hook name, array key = 0.
  170. * @access private
  171. */
  172. function start_filters($hook_args) {
  173. global $imapServerAddress, $imapPort, $imap_stream, $imapConnection,
  174. $UseSeparateImapConnection, $AllowSpamFilters, $filter_inbox_count,
  175. $username;
  176. /**
  177. * check hook that calls filtering. If filters are called by right_main_after_header,
  178. * do filtering only when we are in INBOX folder.
  179. */
  180. if ($hook_args[0]=='right_main_after_header' &&
  181. (sqgetGlobalVar('mailbox',$mailbox,SQ_FORM) && $mailbox!='INBOX')) {
  182. return;
  183. }
  184. $filters = load_filters();
  185. // No point running spam filters if there aren't any to run //
  186. if ($AllowSpamFilters) {
  187. $spamfilters = load_spam_filters();
  188. $AllowSpamFilters = false;
  189. foreach($spamfilters as $filterskey=>$value) {
  190. if ($value['enabled'] == 'yes') {
  191. $AllowSpamFilters = true;
  192. break;
  193. }
  194. }
  195. }
  196. if (!$AllowSpamFilters && empty($filters)) {
  197. return;
  198. }
  199. // Detect if we have already connected to IMAP or not.
  200. // Also check if we are forced to use a separate IMAP connection
  201. if ((!isset($imap_stream) && !isset($imapConnection)) ||
  202. $UseSeparateImapConnection ) {
  203. $stream = sqimap_login($username, false, $imapServerAddress,
  204. $imapPort, 10);
  205. $previously_connected = false;
  206. } else if (isset($imapConnection)) {
  207. $stream = $imapConnection;
  208. $previously_connected = true;
  209. } else {
  210. $previously_connected = true;
  211. $stream = $imap_stream;
  212. }
  213. if (!isset($filter_inbox_count)) {
  214. $aStatus = sqimap_status_messages ($stream, 'INBOX', array('MESSAGES'));
  215. if (!empty($aStatus['MESSAGES'])) {
  216. $filter_inbox_count=$aStatus['MESSAGES'];
  217. } else {
  218. $filter_inbox_count=0;
  219. }
  220. }
  221. if ($filter_inbox_count > 0) {
  222. sqimap_mailbox_select($stream, 'INBOX');
  223. // Filter spam from inbox before we sort them into folders
  224. if ($AllowSpamFilters) {
  225. spam_filters($stream);
  226. }
  227. // Sort into folders
  228. user_filters($stream);
  229. }
  230. if (!$previously_connected) {
  231. sqimap_logout($stream);
  232. }
  233. }
  234. /**
  235. * Does the loop through each filter
  236. * @param stream imap_stream the stream to read from
  237. * @access private
  238. */
  239. function user_filters($imap_stream) {
  240. global $data_dir, $username;
  241. $filters = load_filters();
  242. if (! $filters) return;
  243. $filters_user_scan = getPref($data_dir, $username, 'filters_user_scan');
  244. $expunge = false;
  245. // For every rule
  246. for ($i=0, $num = count($filters); $i < $num; $i++) {
  247. // If it is the "combo" rule
  248. if ($filters[$i]['where'] == 'To or Cc') {
  249. /*
  250. * If it's "TO OR CC", we have to do two searches, one for TO
  251. * and the other for CC.
  252. */
  253. $expunge = filter_search_and_delete($imap_stream, 'TO',
  254. $filters[$i]['what'], $filters[$i]['folder'], $filters_user_scan, $expunge);
  255. $expunge = filter_search_and_delete($imap_stream, 'CC',
  256. $filters[$i]['what'], $filters[$i]['folder'], $filters_user_scan, $expunge);
  257. } else if ($filters[$i]['where'] == 'Header and Body') {
  258. $expunge = filter_search_and_delete($imap_stream, 'TEXT',
  259. $filters[$i]['what'], $filters[$i]['folder'], $filters_user_scan, $expunge);
  260. } else if ($filters[$i]['where'] == 'Message Body') {
  261. $expunge = filter_search_and_delete($imap_stream, 'BODY',
  262. $filters[$i]['what'], $filters[$i]['folder'], $filters_user_scan, $expunge);
  263. } else {
  264. /*
  265. * If it's a normal TO, CC, SUBJECT, or FROM, then handle it
  266. * normally.
  267. */
  268. $expunge = filter_search_and_delete($imap_stream, $filters[$i]['where'],
  269. $filters[$i]['what'], $filters[$i]['folder'], $filters_user_scan, $expunge);
  270. }
  271. }
  272. // Clean out the mailbox whether or not auto_expunge is on
  273. // That way it looks like it was redirected properly
  274. if ($expunge) {
  275. sqimap_mailbox_expunge($imap_stream, 'INBOX');
  276. }
  277. }
  278. /**
  279. * Creates and runs the IMAP command to filter messages
  280. * @param string $where Which part of the message to search (TO, CC, SUBJECT, etc...)
  281. * @param string $what String to search for
  282. * @param string $where_to Folder it will move to
  283. * @param string $user_scan Whether to search all or just unseen
  284. * @param string $should_expunge
  285. * @param boolean $where Which part of location to search
  286. * @access private
  287. */
  288. function filter_search_and_delete($imap_stream, $where, $what, $where_to, $user_scan,
  289. $should_expunge) {
  290. global $languages, $squirrelmail_language, $allow_charset_search, $imap_server_type;
  291. //TODO: make use of new mailbox cache. See mailbox_display.phpinfo
  292. if (strtolower($where_to) == 'inbox') {
  293. return array();
  294. }
  295. if ($user_scan == 'new') {
  296. $category = 'UNSEEN';
  297. } else {
  298. $category = 'ALL';
  299. }
  300. $category .= ' UNDELETED';
  301. if ($allow_charset_search &&
  302. isset($languages[$squirrelmail_language]['CHARSET']) &&
  303. $languages[$squirrelmail_language]['CHARSET']) {
  304. $search_str = 'SEARCH CHARSET '
  305. . strtoupper($languages[$squirrelmail_language]['CHARSET'])
  306. . ' ' . $category;
  307. } else {
  308. $search_str = 'SEARCH CHARSET US-ASCII ' . $category;
  309. }
  310. if ($where == 'Header') {
  311. $what = explode(':', $what);
  312. $where = strtoupper($where);
  313. $where = trim($where . ' ' . $what[0]);
  314. $what = addslashes(trim($what[1]));
  315. }
  316. // see comments in squirrelmail sqimap_search function
  317. if ($imap_server_type == 'macosx' || $imap_server_type == 'hmailserver') {
  318. $search_str .= ' ' . $where . ' ' . $what;
  319. /* read data back from IMAP */
  320. $read = sqimap_run_command($imap_stream, $search_str, true, $response, $message, TRUE);
  321. } else {
  322. $search_str .= ' ' . $where . ' {' . strlen($what) . "}";
  323. $sid = sqimap_session_id(true);
  324. fputs ($imap_stream, $sid . ' ' . $search_str . "\r\n");
  325. $read2 = sqimap_fgets($imap_stream);
  326. # server should respond with Ready for argument, then we will send search text
  327. #echo "RR2 $read2<br>";
  328. fputs ($imap_stream, "$what\r\n");
  329. #echo "SS $what<br>";
  330. $read2 = sqimap_fgets($imap_stream);
  331. #echo "RR2 $read2<br>";
  332. $read[]=$read2;
  333. $read3 = sqimap_fgets($imap_stream);
  334. #echo "RR3 $read3<br>";
  335. list($rtag,$response,$message)=explode(' ',$read3,3);
  336. ## $read2 = sqimap_retrieve_imap_response($imap_stream, $sid, true,
  337. ## $response, $message, $search_str, false, true, false);
  338. #echo "RR2 $read2 / RESPONSE $response<br>";
  339. }
  340. if (isset($read[0])) {
  341. $ids = array();
  342. for ($i = 0, $iCnt = count($read); $i < $iCnt; ++$i) {
  343. if (preg_match("/^\* SEARCH (.+)$/", $read[$i], $regs)) {
  344. $ids += preg_split("/ /", trim($regs[1]));
  345. }
  346. }
  347. if ($response == 'OK' && count($ids)) {
  348. if (sqimap_mailbox_exists($imap_stream, $where_to)) {
  349. $should_expunge = true;
  350. sqimap_msgs_list_move ($imap_stream, $ids, $where_to, false);
  351. }
  352. } elseif ($response != 'OK') {
  353. $query = $search_str . "\r\n".$what ."\r\n";
  354. if ($response == 'NO') {
  355. if (strpos($message,'BADCHARSET') !== false ||
  356. strpos($message,'character') !== false) {
  357. sqm_trigger_imap_error('SQM_IMAP_BADCHARSET',$query, $response, $message);
  358. } else {
  359. sqm_trigger_imap_error('SQM_IMAP_ERROR',$query, $response, $message);
  360. }
  361. } else {
  362. sqm_trigger_imap_error('SQM_IMAP_ERROR',$query, $response, $message);
  363. }
  364. }
  365. }
  366. return $should_expunge;
  367. }
  368. /**
  369. * Loops through all the Received Headers to find IP Addresses
  370. * @param stream imap_stream the stream to read from
  371. * @access private
  372. */
  373. function spam_filters($imap_stream) {
  374. global $data_dir, $username;
  375. global $SpamFilters_YourHop;
  376. global $SpamFilters_DNScache;
  377. global $SpamFilters_SharedCache;
  378. global $SpamFilters_BulkQuery;
  379. global $SpamFilters_CacheTTL;
  380. $filters_spam_scan = getPref($data_dir, $username, 'filters_spam_scan');
  381. $filters_spam_folder = getPref($data_dir, $username, 'filters_spam_folder');
  382. $filters = load_spam_filters();
  383. if ($SpamFilters_SharedCache) {
  384. filters_LoadCache();
  385. }
  386. $run = false;
  387. foreach ($filters as $Key => $Value) {
  388. if ($Value['enabled']) {
  389. $run = true;
  390. break;
  391. }
  392. }
  393. // short-circuit
  394. if (!$run) {
  395. return;
  396. }
  397. // Ask for a big list of all "Received" headers in the inbox with
  398. // flags for each message. Kinda big.
  399. if ($filters_spam_scan == 'new') {
  400. $search_array = array();
  401. $read = sqimap_run_command($imap_stream, 'SEARCH UNSEEN', true, $response, $message, TRUE);
  402. if (isset($read[0])) {
  403. for ($i = 0, $iCnt = count($read); $i < $iCnt; ++$i) {
  404. if (preg_match("/^\* SEARCH (.+)$/", $read[$i], $regs)) {
  405. $search_array = preg_split("/ /", trim($regs[1]));
  406. break;
  407. }
  408. }
  409. }
  410. }
  411. if ($filters_spam_scan == 'new' && count($search_array)) {
  412. $headers = sqimap_get_small_header_list ($imap_stream, $search_array, array('Received'),array());
  413. } else if ($filters_spam_scan != 'new') {
  414. $headers = sqimap_get_small_header_list ($imap_stream, null , array('Received'),array());
  415. } else {
  416. return;
  417. }
  418. if (!count($headers)) {
  419. return;
  420. }
  421. $bulkquery = (strlen($SpamFilters_BulkQuery) > 0 ? true : false);
  422. $IPs = array();
  423. $aSpamIds = array();
  424. foreach ($headers as $id => $aValue) {
  425. if (isset($aValue['UID'])) {
  426. $MsgNum = $aValue['UID'];
  427. } else {
  428. $MsgNum = $id;
  429. }
  430. // Look through all of the Received headers for IP addresses
  431. if (isset($aValue['RECEIVED'])) {
  432. foreach ($aValue['RECEIVED'] as $received) {
  433. // Check to see if this line is the right "Received from" line
  434. // to check
  435. // $aValue['Received'] is an array with all the received lines.
  436. // We should check them from bottom to top and only check the first 2.
  437. // Currently we check only the header where $SpamFilters_YourHop in occures
  438. if (is_int(strpos($received, $SpamFilters_YourHop))) {
  439. if (preg_match('/([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})/',$received,$aMatch)) {
  440. $isspam = false;
  441. if (filters_spam_check_site($aMatch[1],$aMatch[2],$aMatch[3],$aMatch[4],$filters)) {
  442. $aSpamIds[] = $MsgNum;
  443. $isspam = true;
  444. }
  445. if ($bulkquery) {
  446. array_shift($aMatch);
  447. $IP = explode('.',$aMatch);
  448. foreach ($filters as $key => $value) {
  449. if ($filters[$key]['enabled'] && $filters[$key]['dns']) {
  450. if (strlen($SpamFilters_DNScache[$IP.'.'.$filters[$key]['dns']]) == 0) {
  451. $IPs[$IP] = true;
  452. break;
  453. }
  454. }
  455. }
  456. }
  457. // If we've checked one IP and YourHop is
  458. // just a space
  459. if ($SpamFilters_YourHop == ' ' || $isspam) {
  460. break; // don't check any more
  461. }
  462. }
  463. }
  464. }
  465. }
  466. }
  467. // Lookie! It's spam! Yum!
  468. if (count($aSpamIds) && sqimap_mailbox_exists($imap_stream, $filters_spam_folder)) {
  469. sqimap_msgs_list_move ($imap_stream, $aSpamIds, $filters_spam_folder);
  470. sqimap_mailbox_expunge($imap_stream, 'INBOX');
  471. }
  472. if ($bulkquery && count($IPs)) {
  473. filters_bulkquery($filters, $IPs);
  474. }
  475. if ($SpamFilters_SharedCache) {
  476. filters_SaveCache();
  477. } else {
  478. sqsession_register($SpamFilters_DNScache, 'SpamFilters_DNScache');
  479. }
  480. }
  481. /**
  482. * Does the loop through each enabled filter for the specified IP address.
  483. * IP format: $a.$b.$c.$d
  484. * @param int $a First subset of IP
  485. * @param int $b Second subset of IP
  486. * @param int $c Third subset of IP
  487. * @param int $d Forth subset of IP
  488. * @param array $filters The Spam Filters
  489. * @return boolean Whether the IP is Spam
  490. * @access private
  491. */
  492. function filters_spam_check_site($a, $b, $c, $d, &$filters) {
  493. global $SpamFilters_DNScache, $SpamFilters_CacheTTL;
  494. foreach ($filters as $key => $value) {
  495. if ($filters[$key]['enabled']) {
  496. if ($filters[$key]['dns']) {
  497. $filter_revip = $d . '.' . $c . '.' . $b . '.' . $a . '.' .
  498. $filters[$key]['dns'];
  499. if(!isset($SpamFilters_DNScache[$filter_revip]['L']))
  500. $SpamFilters_DNScache[$filter_revip]['L'] = '';
  501. if(!isset($SpamFilters_DNScache[$filter_revip]['T']))
  502. $SpamFilters_DNScache[$filter_revip]['T'] = '';
  503. if (strlen($SpamFilters_DNScache[$filter_revip]['L']) == 0) {
  504. $SpamFilters_DNScache[$filter_revip]['L'] =
  505. gethostbyname($filter_revip);
  506. $SpamFilters_DNScache[$filter_revip]['T'] =
  507. time() + $SpamFilters_CacheTTL;
  508. }
  509. if ($SpamFilters_DNScache[$filter_revip]['L'] ==
  510. $filters[$key]['result']) {
  511. return 1;
  512. }
  513. }
  514. }
  515. }
  516. return 0;
  517. }
  518. /**
  519. * Loads the filters from the user preferences
  520. * @return array All the user filters
  521. * @access private
  522. */
  523. function load_filters() {
  524. global $data_dir, $username;
  525. $filters = array();
  526. for ($i = 0; $fltr = getPref($data_dir, $username, 'filter' . $i); $i++) {
  527. $ary = explode(',', $fltr);
  528. $filters[$i]['where'] = $ary[0];
  529. $filters[$i]['what'] = $ary[1];
  530. $filters[$i]['folder'] = $ary[2];
  531. }
  532. return $filters;
  533. }
  534. /**
  535. * Loads the Spam Filters and checks the preferences for the enabled status
  536. * @return array All the spam filters
  537. * @access private
  538. */
  539. function load_spam_filters() {
  540. global $data_dir, $username, $SpamFilters_ShowCommercial;
  541. if ($SpamFilters_ShowCommercial) {
  542. $filters['MAPS RBL']['prefname'] = 'filters_spam_maps_rbl';
  543. $filters['MAPS RBL']['name'] = 'MAPS Realtime Blackhole List';
  544. $filters['MAPS RBL']['link'] = 'http://www.mail-abuse.org/rbl/';
  545. $filters['MAPS RBL']['dns'] = 'blackholes.mail-abuse.org';
  546. $filters['MAPS RBL']['result'] = '127.0.0.2';
  547. $filters['MAPS RBL']['comment'] =
  548. _("COMMERCIAL - This list contains servers that are verified spam senders. It is a pretty reliable list to scan spam from.");
  549. $filters['MAPS RSS']['prefname'] = 'filters_spam_maps_rss';
  550. $filters['MAPS RSS']['name'] = 'MAPS Relay Spam Stopper';
  551. $filters['MAPS RSS']['link'] = 'http://www.mail-abuse.org/rss/';
  552. $filters['MAPS RSS']['dns'] = 'relays.mail-abuse.org';
  553. $filters['MAPS RSS']['result'] = '127.0.0.2';
  554. $filters['MAPS RSS']['comment'] =
  555. _("COMMERCIAL - Servers that are configured (or misconfigured) to allow spam to be relayed through their system will be banned with this. Another good one to use.");
  556. $filters['MAPS DUL']['prefname'] = 'filters_spam_maps_dul';
  557. $filters['MAPS DUL']['name'] = 'MAPS Dial-Up List';
  558. $filters['MAPS DUL']['link'] = 'http://www.mail-abuse.org/dul/';
  559. $filters['MAPS DUL']['dns'] = 'dialups.mail-abuse.org';
  560. $filters['MAPS DUL']['result'] = '127.0.0.3';
  561. $filters['MAPS DUL']['comment'] =
  562. _("COMMERCIAL - Dial-up users are often filtered out since they should use their ISP's mail servers to send mail. Spammers typically get a dial-up account and send spam directly from there.");
  563. $filters['MAPS RBLplus-RBL']['prefname'] = 'filters_spam_maps_rblplus_rbl';
  564. $filters['MAPS RBLplus-RBL']['name'] = 'MAPS RBL+ RBL List';
  565. $filters['MAPS RBLplus-RBL']['link'] = 'http://www.mail-abuse.org/';
  566. $filters['MAPS RBLplus-RBL']['dns'] = 'rbl-plus.mail-abuse.org';
  567. $filters['MAPS RBLplus-RBL']['result'] = '127.0.0.2';
  568. $filters['MAPS RBLplus-RBL']['comment'] =
  569. _("COMMERCIAL - RBL+ Blackhole entries.");
  570. $filters['MAPS RBLplus-RSS']['prefname'] = 'filters_spam_maps_rblplus_rss';
  571. $filters['MAPS RBLplus-RSS']['name'] = 'MAPS RBL+ List RSS entries';
  572. $filters['MAPS RBLplus-RSS']['link'] = 'http://www.mail-abuse.org/';
  573. $filters['MAPS RBLplus-RSS']['dns'] = 'rbl-plus.mail-abuse.org';
  574. $filters['MAPS RBLplus-RSS']['result'] = '127.0.0.2';
  575. $filters['MAPS RBLplus-RSS']['comment'] =
  576. _("COMMERCIAL - RBL+ OpenRelay entries.");
  577. $filters['MAPS RBLplus-DUL']['prefname'] = 'filters_spam_maps_rblplus_dul';
  578. $filters['MAPS RBLplus-DUL']['name'] = 'MAPS RBL+ List DUL entries';
  579. $filters['MAPS RBLplus-DUL']['link'] = 'http://www.mail-abuse.org/';
  580. $filters['MAPS RBLplus-DUL']['dns'] = 'rbl-plus.mail-abuse.org';
  581. $filters['MAPS RBLplus-DUL']['result'] = '127.0.0.3';
  582. $filters['MAPS RBLplus-DUL']['comment'] =
  583. _("COMMERCIAL - RBL+ Dial-up entries.");
  584. }
  585. $filters['ORDB']['prefname'] = 'filters_spam_ordb';
  586. $filters['ORDB']['name'] = 'Open Relay Database List';
  587. $filters['ORDB']['link'] = 'http://www.ordb.org/';
  588. $filters['ORDB']['dns'] = 'relays.ordb.org';
  589. $filters['ORDB']['result'] = '127.0.0.2';
  590. $filters['ORDB']['comment'] =
  591. _("FREE - ORDB was born when ORBS went off the air. It seems to have fewer false positives than ORBS did though.");
  592. $filters['FiveTen Direct']['prefname'] = 'filters_spam_fiveten_src';
  593. $filters['FiveTen Direct']['name'] = 'Five-Ten-sg.com Direct SPAM Sources';
  594. $filters['FiveTen Direct']['link'] = 'http://www.five-ten-sg.com/blackhole.php';
  595. $filters['FiveTen Direct']['dns'] = 'blackholes.five-ten-sg.com';
  596. $filters['FiveTen Direct']['result'] = '127.0.0.2';
  597. $filters['FiveTen Direct']['comment'] =
  598. _("FREE - Five-Ten-sg.com - Direct SPAM sources.");
  599. $filters['FiveTen DUL']['prefname'] = 'filters_spam_fiveten_dul';
  600. $filters['FiveTen DUL']['name'] = 'Five-Ten-sg.com DUL Lists';
  601. $filters['FiveTen DUL']['link'] = 'http://www.five-ten-sg.com/blackhole.php';
  602. $filters['FiveTen DUL']['dns'] = 'blackholes.five-ten-sg.com';
  603. $filters['FiveTen DUL']['result'] = '127.0.0.3';
  604. $filters['FiveTen DUL']['comment'] =
  605. _("FREE - Five-Ten-sg.com - Dial-up lists - includes some DSL IPs.");
  606. $filters['FiveTen Unc. OptIn']['prefname'] = 'filters_spam_fiveten_oi';
  607. $filters['FiveTen Unc. OptIn']['name'] = 'Five-Ten-sg.com Unconfirmed OptIn Lists';
  608. $filters['FiveTen Unc. OptIn']['link'] = 'http://www.five-ten-sg.com/blackhole.php';
  609. $filters['FiveTen Unc. OptIn']['dns'] = 'blackholes.five-ten-sg.com';
  610. $filters['FiveTen Unc. OptIn']['result'] = '127.0.0.4';
  611. $filters['FiveTen Unc. OptIn']['comment'] =
  612. _("FREE - Five-Ten-sg.com - Bulk mailers that do not use confirmed opt-in.");
  613. $filters['FiveTen Others']['prefname'] = 'filters_spam_fiveten_oth';
  614. $filters['FiveTen Others']['name'] = 'Five-Ten-sg.com Other Misc. Servers';
  615. $filters['FiveTen Others']['link'] = 'http://www.five-ten-sg.com/blackhole.php';
  616. $filters['FiveTen Others']['dns'] = 'blackholes.five-ten-sg.com';
  617. $filters['FiveTen Others']['result'] = '127.0.0.5';
  618. $filters['FiveTen Others']['comment'] =
  619. _("FREE - Five-Ten-sg.com - Other misc. servers.");
  620. $filters['FiveTen Single Stage']['prefname'] = 'filters_spam_fiveten_ss';
  621. $filters['FiveTen Single Stage']['name'] = 'Five-Ten-sg.com Single Stage Servers';
  622. $filters['FiveTen Single Stage']['link'] = 'http://www.five-ten-sg.com/blackhole.php';
  623. $filters['FiveTen Single Stage']['dns'] = 'blackholes.five-ten-sg.com';
  624. $filters['FiveTen Single Stage']['result'] = '127.0.0.6';
  625. $filters['FiveTen Single Stage']['comment'] =
  626. _("FREE - Five-Ten-sg.com - Single Stage servers.");
  627. $filters['FiveTen SPAM Support']['prefname'] = 'filters_spam_fiveten_supp';
  628. $filters['FiveTen SPAM Support']['name'] = 'Five-Ten-sg.com SPAM Support Servers';
  629. $filters['FiveTen SPAM Support']['link'] = 'http://www.five-ten-sg.com/blackhole.php';
  630. $filters['FiveTen SPAM Support']['dns'] = 'blackholes.five-ten-sg.com';
  631. $filters['FiveTen SPAM Support']['result'] = '127.0.0.7';
  632. $filters['FiveTen SPAM Support']['comment'] =
  633. _("FREE - Five-Ten-sg.com - SPAM Support servers.");
  634. $filters['FiveTen Web forms']['prefname'] = 'filters_spam_fiveten_wf';
  635. $filters['FiveTen Web forms']['name'] = 'Five-Ten-sg.com Web Form IPs';
  636. $filters['FiveTen Web forms']['link'] = 'http://www.five-ten-sg.com/blackhole.php';
  637. $filters['FiveTen Web forms']['dns'] = 'blackholes.five-ten-sg.com';
  638. $filters['FiveTen Web forms']['result'] = '127.0.0.8';
  639. $filters['FiveTen Web forms']['comment'] =
  640. _("FREE - Five-Ten-sg.com - Web Form IPs.");
  641. $filters['Dorkslayers']['prefname'] = 'filters_spam_dorks';
  642. $filters['Dorkslayers']['name'] = 'Dorkslayers Lists';
  643. $filters['Dorkslayers']['link'] = 'http://www.dorkslayers.com';
  644. $filters['Dorkslayers']['dns'] = 'orbs.dorkslayers.com';
  645. $filters['Dorkslayers']['result'] = '127.0.0.2';
  646. $filters['Dorkslayers']['comment'] =
  647. _("FREE - Dorkslayers appears to include only really bad open relays outside the US to avoid being sued. Interestingly enough, their website recommends you NOT use their service.");
  648. $filters['SPAMhaus']['prefname'] = 'filters_spam_spamhaus';
  649. $filters['SPAMhaus']['name'] = 'SPAMhaus Lists';
  650. $filters['SPAMhaus']['link'] = 'http://www.spamhaus.org';
  651. $filters['SPAMhaus']['dns'] = 'sbl.spamhaus.org';
  652. $filters['SPAMhaus']['result'] = '127.0.0.6';
  653. $filters['SPAMhaus']['comment'] =
  654. _("FREE - SPAMhaus - A list of well-known SPAM sources.");
  655. $filters['SPAMcop']['prefname'] = 'filters_spam_spamcop';
  656. $filters['SPAMcop']['name'] = 'SPAM Cop Lists';
  657. $filters['SPAMcop']['link'] = 'http://spamcop.net/bl.shtml';
  658. $filters['SPAMcop']['dns'] = 'bl.spamcop.net';
  659. $filters['SPAMcop']['result'] = '127.0.0.2';
  660. $filters['SPAMcop']['comment'] =
  661. _("FREE, for now - SpamCop - An interesting solution that lists servers that have a very high spam to legit email ratio (85 percent or more).");
  662. $filters['dev.null.dk']['prefname'] = 'filters_spam_devnull';
  663. $filters['dev.null.dk']['name'] = 'dev.null.dk Lists';
  664. $filters['dev.null.dk']['link'] = 'http://dev.null.dk/';
  665. $filters['dev.null.dk']['dns'] = 'dev.null.dk';
  666. $filters['dev.null.dk']['result'] = '127.0.0.2';
  667. $filters['dev.null.dk']['comment'] =
  668. _("FREE - dev.null.dk - I don't have any detailed info on this list.");
  669. $filters['visi.com']['prefname'] = 'filters_spam_visi';
  670. $filters['visi.com']['name'] = 'visi.com Relay Stop List';
  671. $filters['visi.com']['link'] = 'http://relays.visi.com';
  672. $filters['visi.com']['dns'] = 'relays.visi.com';
  673. $filters['visi.com']['result'] = '127.0.0.2';
  674. $filters['visi.com']['comment'] =
  675. _("FREE - visi.com - Relay Stop List. Very conservative OpenRelay List.");
  676. $filters['ahbl.org Open Relays']['prefname'] = 'filters_spam_2mb_or';
  677. $filters['ahbl.org Open Relays']['name'] = 'ahbl.org Open Relays List';
  678. $filters['ahbl.org Open Relays']['link'] = 'http://www.ahbl.org/';
  679. $filters['ahbl.org Open Relays']['dns'] = 'dnsbl.ahbl.org';
  680. $filters['ahbl.org Open Relays']['result'] = '127.0.0.2';
  681. $filters['ahbl.org Open Relays']['comment'] =
  682. _("FREE - ahbl.org Open Relays - Another list of Open Relays.");
  683. $filters['ahbl.org SPAM Source']['prefname'] = 'filters_spam_2mb_ss';
  684. $filters['ahbl.org SPAM Source']['name'] = 'ahbl.org SPAM Source List';
  685. $filters['ahbl.org SPAM Source']['link'] = 'http://www.ahbl.org/';
  686. $filters['ahbl.org SPAM Source']['dns'] = 'dnsbl.ahbl.org';
  687. $filters['ahbl.org SPAM Source']['result'] = '127.0.0.4';
  688. $filters['ahbl.org SPAM Source']['comment'] =
  689. _("FREE - ahbl.org SPAM Source - List of Direct SPAM Sources.");
  690. $filters['ahbl.org SPAM ISPs']['prefname'] = 'filters_spam_2mb_isp';
  691. $filters['ahbl.org SPAM ISPs']['name'] = 'ahbl.org SPAM-friendly ISP List';
  692. $filters['ahbl.org SPAM ISPs']['link'] = 'http://www.ahbl.org/';
  693. $filters['ahbl.org SPAM ISPs']['dns'] = 'dnsbl.ahbl.org';
  694. $filters['ahbl.org SPAM ISPs']['result'] = '127.0.0.7';
  695. $filters['ahbl.org SPAM ISPs']['comment'] =
  696. _("FREE - ahbl.org SPAM ISPs - List of SPAM-friendly ISPs.");
  697. $filters['Leadmon DUL']['prefname'] = 'filters_spam_lm_dul';
  698. $filters['Leadmon DUL']['name'] = 'Leadmon.net DUL List';
  699. $filters['Leadmon DUL']['link'] = 'http://www.leadmon.net/spamguard/';
  700. $filters['Leadmon DUL']['dns'] = 'spamguard.leadmon.net';
  701. $filters['Leadmon DUL']['result'] = '127.0.0.2';
  702. $filters['Leadmon DUL']['comment'] =
  703. _("FREE - Leadmon DUL - Another list of Dial-up or otherwise dynamically assigned IPs.");
  704. $filters['Leadmon SPAM Source']['prefname'] = 'filters_spam_lm_ss';
  705. $filters['Leadmon SPAM Source']['name'] = 'Leadmon.net SPAM Source List';
  706. $filters['Leadmon SPAM Source']['link'] = 'http://www.leadmon.net/spamguard/';
  707. $filters['Leadmon SPAM Source']['dns'] = 'spamguard.leadmon.net';
  708. $filters['Leadmon SPAM Source']['result'] = '127.0.0.3';
  709. $filters['Leadmon SPAM Source']['comment'] =
  710. _("FREE - Leadmon SPAM Source - List of IPs Leadmon.net has received SPAM directly from.");
  711. $filters['Leadmon Bulk Mailers']['prefname'] = 'filters_spam_lm_bm';
  712. $filters['Leadmon Bulk Mailers']['name'] = 'Leadmon.net Bulk Mailers List';
  713. $filters['Leadmon Bulk Mailers']['link'] = 'http://www.leadmon.net/spamguard/';
  714. $filters['Leadmon Bulk Mailers']['dns'] = 'spamguard.leadmon.net';
  715. $filters['Leadmon Bulk Mailers']['result'] = '127.0.0.4';
  716. $filters['Leadmon Bulk Mailers']['comment'] =
  717. _("FREE - Leadmon Bulk Mailers - Bulk mailers that do not require confirmed opt-in or that have allowed known spammers to become clients and abuse their services.");
  718. $filters['Leadmon Open Relays']['prefname'] = 'filters_spam_lm_or';
  719. $filters['Leadmon Open Relays']['name'] = 'Leadmon.net Open Relays List';
  720. $filters['Leadmon Open Relays']['link'] = 'http://www.leadmon.net/spamguard/';
  721. $filters['Leadmon Open Relays']['dns'] = 'spamguard.leadmon.net';
  722. $filters['Leadmon Open Relays']['result'] = '127.0.0.5';
  723. $filters['Leadmon Open Relays']['comment'] =
  724. _("FREE - Leadmon Open Relays - Single Stage Open Relays that are not listed on other active RBLs.");
  725. $filters['Leadmon Multi-stage']['prefname'] = 'filters_spam_lm_ms';
  726. $filters['Leadmon Multi-stage']['name'] = 'Leadmon.net Multi-Stage Relay List';
  727. $filters['Leadmon Multi-stage']['link'] = 'http://www.leadmon.net/spamguard/';
  728. $filters['Leadmon Multi-stage']['dns'] = 'spamguard.leadmon.net';
  729. $filters['Leadmon Multi-stage']['result'] = '127.0.0.6';
  730. $filters['Leadmon Multi-stage']['comment'] =
  731. _("FREE - Leadmon Multi-stage - Multi-Stage Open Relays that are not listed on other active RBLs and that have sent SPAM to Leadmon.net.");
  732. $filters['Leadmon SpamBlock']['prefname'] = 'filters_spam_lm_sb';
  733. $filters['Leadmon SpamBlock']['name'] = 'Leadmon.net SpamBlock Sites List';
  734. $filters['Leadmon SpamBlock']['link'] = 'http://www.leadmon.net/spamguard/';
  735. $filters['Leadmon SpamBlock']['dns'] = 'spamguard.leadmon.net';
  736. $filters['Leadmon SpamBlock']['result'] = '127.0.0.7';
  737. $filters['Leadmon SpamBlock']['comment'] =
  738. _("FREE - Leadmon SpamBlock - Sites on this listing have sent Leadmon.net direct SPAM from IPs in netblocks where the entire block has no DNS mappings. It's a list of BLOCKS of IPs being used by people who have SPAMmed Leadmon.net.");
  739. $filters['NJABL Open Relays']['prefname'] = 'filters_spam_njabl_or';
  740. $filters['NJABL Open Relays']['name'] = 'NJABL Open Relay/Direct Spam Source List';
  741. $filters['NJABL Open Relays']['link'] = 'http://www.njabl.org/';
  742. $filters['NJABL Open Relays']['dns'] = 'dnsbl.njabl.org';
  743. $filters['NJABL Open Relays']['result'] = '127.0.0.2';
  744. $filters['NJABL Open Relays']['comment'] =
  745. _("FREE, for now - Not Just Another Blacklist - Both Open Relays and Direct SPAM Sources.");
  746. $filters['NJABL DUL']['prefname'] = 'filters_spam_njabl_dul';
  747. $filters['NJABL DUL']['name'] = 'NJABL Dial-ups List';
  748. $filters['NJABL DUL']['link'] = 'http://www.njabl.org/';
  749. $filters['NJABL DUL']['dns'] = 'dnsbl.njabl.org';
  750. $filters['NJABL DUL']['result'] = '127.0.0.3';
  751. $filters['NJABL DUL']['comment'] =
  752. _("FREE, for now - Not Just Another Blacklist - Dial-up IPs.");
  753. $filters['Conf DSBL.ORG Relay']['prefname'] = 'filters_spam_dsbl_conf_ss';
  754. $filters['Conf DSBL.ORG Relay']['name'] = 'DSBL.org Confirmed Relay List';
  755. $filters['Conf DSBL.ORG Relay']['link'] = 'http://www.dsbl.org/';
  756. $filters['Conf DSBL.ORG Relay']['dns'] = 'list.dsbl.org';
  757. $filters['Conf DSBL.ORG Relay']['result'] = '127.0.0.2';
  758. $filters['Conf DSBL.ORG Relay']['comment'] =
  759. _("FREE - Distributed Sender Boycott List - Confirmed Relays");
  760. $filters['Conf DSBL.ORG Multi-Stage']['prefname'] = 'filters_spam_dsbl_conf_ms';
  761. $filters['Conf DSBL.ORG Multi-Stage']['name'] = 'DSBL.org Confirmed Multi-Stage Relay List';
  762. $filters['Conf DSBL.ORG Multi-Stage']['link'] = 'http://www.dsbl.org/';
  763. $filters['Conf DSBL.ORG Multi-Stage']['dns'] = 'multihop.dsbl.org';
  764. $filters['Conf DSBL.ORG Multi-Stage']['result'] = '127.0.0.2';
  765. $filters['Conf DSBL.ORG Multi-Stage']['comment'] =
  766. _("FREE - Distributed Sender Boycott List - Confirmed Multi-stage Relays");
  767. $filters['UN-Conf DSBL.ORG']['prefname'] = 'filters_spam_dsbl_unc';
  768. $filters['UN-Conf DSBL.ORG']['name'] = 'DSBL.org UN-Confirmed Relay List';
  769. $filters['UN-Conf DSBL.ORG']['link'] = 'http://www.dsbl.org/';
  770. $filters['UN-Conf DSBL.ORG']['dns'] = 'unconfirmed.dsbl.org';
  771. $filters['UN-Conf DSBL.ORG']['result'] = '127.0.0.2';
  772. $filters['UN-Conf DSBL.ORG']['comment'] =
  773. _("FREE - Distributed Sender Boycott List - UN-Confirmed Relays");
  774. foreach ($filters as $Key => $Value) {
  775. $filters[$Key]['enabled'] = getPref($data_dir, $username, $filters[$Key]['prefname']);
  776. }
  777. return $filters;
  778. }
  779. /**
  780. * Removes a User filter
  781. * @param int $id ID of the filter to remove
  782. * @access private
  783. */
  784. function remove_filter ($id) {
  785. global $data_dir, $username;
  786. while ($nextFilter = getPref($data_dir, $username, 'filter' . ($id + 1))) {
  787. setPref($data_dir, $username, 'filter' . $id, $nextFilter);
  788. $id ++;
  789. }
  790. removePref($data_dir, $username, 'filter' . $id);
  791. }
  792. /**
  793. * Swaps two filters
  794. * @param int $id1 ID of first filter to swap
  795. * @param int $id2 ID of second filter to swap
  796. * @access private
  797. */
  798. function filter_swap($id1, $id2) {
  799. global $data_dir, $username;
  800. $FirstFilter = getPref($data_dir, $username, 'filter' . $id1);
  801. $SecondFilter = getPref($data_dir, $username, 'filter' . $id2);
  802. if ($FirstFilter && $SecondFilter) {
  803. setPref($data_dir, $username, 'filter' . $id2, $FirstFilter);
  804. setPref($data_dir, $username, 'filter' . $id1, $SecondFilter);
  805. }
  806. }
  807. /**
  808. * This updates the filter rules when renaming or deleting folders
  809. * @param array $args
  810. * @access private
  811. */
  812. function update_for_folder ($args) {
  813. $old_folder = $args[0];
  814. $new_folder = $args[2];
  815. $action = $args[1];
  816. global $data_dir, $username;
  817. $filters = array();
  818. $filters = load_filters();
  819. $filter_count = count($filters);
  820. $p = 0;
  821. for ($i = 0; $i < $filter_count; $i++) {
  822. if (!empty($filters)) {
  823. if ($old_folder == $filters[$i]['folder']) {
  824. if ($action == 'rename') {
  825. $filters[$i]['folder'] = $new_folder;
  826. setPref($data_dir, $username, 'filter'.$i,
  827. $filters[$i]['where'].','.$filters[$i]['what'].','.$new_folder);
  828. }
  829. elseif ($action == 'delete') {
  830. remove_filter($p);
  831. $p = $p-1;
  832. }
  833. }
  834. $p++;
  835. }
  836. }
  837. }
  838. /**
  839. * Display formated error message
  840. * @param string $string text message
  841. * @return string html formated text message
  842. * @access private
  843. */
  844. function do_error($string) {
  845. global $color;
  846. echo "<p align=\"center\"><font color=\"$color[2]\">";
  847. echo $string;
  848. echo "</font></p>\n";
  849. }