TYPO3  7.6
Grammar.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of SwiftMailer.
5  * (c) 2004-2009 Chris Corbyn
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10 
18 {
24  private static $_specials = array();
25 
31  private static $_grammar = array();
32 
36  public function __construct()
37  {
38  $this->init();
39  }
40 
41  public function __wakeup()
42  {
43  $this->init();
44  }
45 
46  protected function init()
47  {
48  if (count(self::$_specials) > 0) {
49  return;
50  }
51 
52  self::$_specials = array(
53  '(', ')', '<', '>', '[', ']',
54  ':', ';', '@', ',', '.', '"',
55  );
56 
57  /*** Refer to RFC 2822 for ABNF grammar ***/
58 
59  // All basic building blocks
60  self::$_grammar['NO-WS-CTL'] = '[\x01-\x08\x0B\x0C\x0E-\x19\x7F]';
61  self::$_grammar['WSP'] = '[ \t]';
62  self::$_grammar['CRLF'] = '(?:\r\n)';
63  self::$_grammar['FWS'] = '(?:(?:'.self::$_grammar['WSP'].'*'.
64  self::$_grammar['CRLF'].')?'.self::$_grammar['WSP'].')';
65  self::$_grammar['text'] = '[\x00-\x08\x0B\x0C\x0E-\x7F]';
66  self::$_grammar['quoted-pair'] = '(?:\\\\'.self::$_grammar['text'].')';
67  self::$_grammar['ctext'] = '(?:'.self::$_grammar['NO-WS-CTL'].
68  '|[\x21-\x27\x2A-\x5B\x5D-\x7E])';
69  // Uses recursive PCRE (?1) -- could be a weak point??
70  self::$_grammar['ccontent'] = '(?:'.self::$_grammar['ctext'].'|'.
71  self::$_grammar['quoted-pair'].'|(?1))';
72  self::$_grammar['comment'] = '(\((?:'.self::$_grammar['FWS'].'|'.
73  self::$_grammar['ccontent'].')*'.self::$_grammar['FWS'].'?\))';
74  self::$_grammar['CFWS'] = '(?:(?:'.self::$_grammar['FWS'].'?'.
75  self::$_grammar['comment'].')*(?:(?:'.self::$_grammar['FWS'].'?'.
76  self::$_grammar['comment'].')|'.self::$_grammar['FWS'].'))';
77  self::$_grammar['qtext'] = '(?:'.self::$_grammar['NO-WS-CTL'].
78  '|[\x21\x23-\x5B\x5D-\x7E])';
79  self::$_grammar['qcontent'] = '(?:'.self::$_grammar['qtext'].'|'.
80  self::$_grammar['quoted-pair'].')';
81  self::$_grammar['quoted-string'] = '(?:'.self::$_grammar['CFWS'].'?"'.
82  '('.self::$_grammar['FWS'].'?'.self::$_grammar['qcontent'].')*'.
83  self::$_grammar['FWS'].'?"'.self::$_grammar['CFWS'].'?)';
84  self::$_grammar['atext'] = '[a-zA-Z0-9!#\$%&\'\*\+\-\/=\?\^_`\{\}\|~]';
85  self::$_grammar['atom'] = '(?:'.self::$_grammar['CFWS'].'?'.
86  self::$_grammar['atext'].'+'.self::$_grammar['CFWS'].'?)';
87  self::$_grammar['dot-atom-text'] = '(?:'.self::$_grammar['atext'].'+'.
88  '(\.'.self::$_grammar['atext'].'+)*)';
89  self::$_grammar['dot-atom'] = '(?:'.self::$_grammar['CFWS'].'?'.
90  self::$_grammar['dot-atom-text'].'+'.self::$_grammar['CFWS'].'?)';
91  self::$_grammar['word'] = '(?:'.self::$_grammar['atom'].'|'.
92  self::$_grammar['quoted-string'].')';
93  self::$_grammar['phrase'] = '(?:'.self::$_grammar['word'].'+?)';
94  self::$_grammar['no-fold-quote'] = '(?:"(?:'.self::$_grammar['qtext'].
95  '|'.self::$_grammar['quoted-pair'].')*")';
96  self::$_grammar['dtext'] = '(?:'.self::$_grammar['NO-WS-CTL'].
97  '|[\x21-\x5A\x5E-\x7E])';
98  self::$_grammar['no-fold-literal'] = '(?:\[(?:'.self::$_grammar['dtext'].
99  '|'.self::$_grammar['quoted-pair'].')*\])';
100 
101  // Message IDs
102  self::$_grammar['id-left'] = '(?:'.self::$_grammar['dot-atom-text'].'|'.
103  self::$_grammar['no-fold-quote'].')';
104  self::$_grammar['id-right'] = '(?:'.self::$_grammar['dot-atom-text'].'|'.
105  self::$_grammar['no-fold-literal'].')';
106 
107  // Addresses, mailboxes and paths
108  self::$_grammar['local-part'] = '(?:'.self::$_grammar['dot-atom'].'|'.
109  self::$_grammar['quoted-string'].')';
110  self::$_grammar['dcontent'] = '(?:'.self::$_grammar['dtext'].'|'.
111  self::$_grammar['quoted-pair'].')';
112  self::$_grammar['domain-literal'] = '(?:'.self::$_grammar['CFWS'].'?\[('.
113  self::$_grammar['FWS'].'?'.self::$_grammar['dcontent'].')*?'.
114  self::$_grammar['FWS'].'?\]'.self::$_grammar['CFWS'].'?)';
115  self::$_grammar['domain'] = '(?:'.self::$_grammar['dot-atom'].'|'.
116  self::$_grammar['domain-literal'].')';
117  self::$_grammar['addr-spec'] = '(?:'.self::$_grammar['local-part'].'@'.
118  self::$_grammar['domain'].')';
119  }
120 
128  public function getDefinition($name)
129  {
130  if (array_key_exists($name, self::$_grammar)) {
131  return self::$_grammar[$name];
132  } else {
134  "No such grammar '".$name."' defined."
135  );
136  }
137  }
138 
144  public function getGrammarDefinitions()
145  {
146  return self::$_grammar;
147  }
148 
154  public function getSpecials()
155  {
156  return self::$_specials;
157  }
158 
168  public function escapeSpecials($token, $include = array(), $exclude = array())
169  {
170  foreach (array_merge(array('\\'), array_diff(self::$_specials, $exclude), $include) as $char) {
171  $token = str_replace($char, '\\'.$char, $token);
172  }
173 
174  return $token;
175  }
176 }