summaryrefslogtreecommitdiff
path: root/plugins/sieverules/importFilters/avelsieve.php
blob: c1e3fef9a91b503a4f263073d73d1965c39681b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php

/**
 * SieveRules import filter for Avelsieve
 *
 * The class should be named 'srimport_[filename]'
 * Each import filter must have:
 *   An attribute called name
 *   A pubic function called detector
 *   A pubic function called importer
 * The importer function can return either a string to be parsed by the SieveRules parser
 * or an array, similar to the one created by the SieveRules parser
 */
class srimport_avelsieve
{
	public $name = 'Squirrelmail (Avelsieve)';

	public function detector($script)
	{
		return preg_match('/#AVELSIEVE_VERSION.*/', $script) ? True : False;
	}

	public function importer($script)
	{
		$i = 0;
		$content = '';
		$name = array();

		// tokenize rules
		if ($tokens = preg_split('/(#START_SIEVE_RULE.*END_SIEVE_RULE)\n/', $script, -1, PREG_SPLIT_DELIM_CAPTURE)) {
			foreach($tokens as $token) {
				if (preg_match('/(require\s+\[.*\];)/i', $token, $matches)) {
					$content .= $matches[1] . "\n";
				}
				elseif (!preg_match('/^#START_SIEVE_RULE.*/', $token, $matches)) {
					$rulename = "# rule:[";
					$parts = explode ('"', trim($token));

					for ($i = 1; $i < count($parts); $i+=2)
						$rulename .= $parts[$i] . " ";

					$rulename .= "]\n";
					$content .= $rulename;

					$content .= trim($token) . "\n";
				}
			}
		}

		return $content;
	}
}

?>