1<?php
2/**
3 * Givemeyouremail Plugin: allows visitors to give their email
4 *
5 * @license	GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author	 Anthony Dahanne <anthony.dahanne@gmail.com>
7 */
8
9if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
10if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
11require_once(DOKU_PLUGIN.'syntax.php');
12
13/**
14 * All DokuWiki plugins to extend the parser/rendering mechanism
15 * need to inherit from this class
16 */
17class syntax_plugin_givemeyouremail extends DokuWiki_Syntax_Plugin {
18	/**
19	* return some info
20	*/
21  function getInfo(){
22	return array(
23		'author' => 'Anthony Dahanne',
24		'email'  => 'anthony.dahanne@gmail.com',
25		'date'   => '2009/01/01',
26		'name'   => 'GiveMeYourEmail Plugin',
27		'desc'   => 'Allow visitors to give their email adress',
28		'url'	=> 'http://blog.dahanne.net',
29	);
30  }
31
32  function getType(){ return 'substition';}
33  function getPType(){ return 'block';}
34  function getSort(){ return 4343; }
35
36	/**
37	* Connect pattern to lexer
38	*/
39  function connectTo($mode){
40	$this->Lexer->addSpecialPattern('<givemeyouremail>', $mode, 'plugin_givemeyouremail');
41  }
42
43	/**
44	* Handle the match
45	*/
46  function handle($match, $state, $pos, &$handler){
47	$match = substr($match, 8, -9);  // strip markup
48	list($title, $options) = preg_split('/>/u', $match, 2);
49	list($leftstr, $rightstr) = split('\|', $title);
50	if ($rightstr == "") {
51		$title = $leftstr;
52		$disable = "";
53		$single = "";
54		$login = "";
55	}
56	else {
57		$title = $rightstr;
58		$disable = strpos($leftstr, "disable");
59		$single = strpos($leftstr, "single");
60		$login = strpos($leftstr, "login");
61	}
62	if (!$options){
63		$options = $title;
64		$title   = NULL;
65	}
66	$options = explode('^', $match);
67
68	$c = count($options);
69	for ($i = 0; $i < $c; $i++){
70		$options[$i] = trim($options[$i]);
71	}
72
73	return array(trim($title), $options, $disable, $single, $login);
74  }
75	/**
76	* Create output
77	*/
78  function render($mode, &$renderer, $data) {
79	if ($mode == 'xhtml'){
80		global $lang;
81
82		$options = $data[1];
83		$c = count($options)-1;
84		$title   = $renderer->_xmlEntities($data[0]);
85		$disable   = $renderer->_xmlEntities($data[2]);
86		$single   = $renderer->_xmlEntities($data[3]);
87		$login   = $renderer->_xmlEntities($data[4]);
88		$dID	 = md5($title);
89
90		// prevent caching to ensure the poll results are fresh
91		$renderer->info['cache'] = false;
92
93		// get givemeyouremail file contents
94		$dfile   = metaFN($dID, '.givemeyouremail');
95		$old_dfile   = metaFN(md5(cleanID($title)), '.givemeyouremail');
96		// rename old meta File
97		if (file_exists($old_dfile)) {
98			rename($old_dfile, $dfile);
99		}
100		$givemeyouremail  = unserialize(@file_get_contents($dfile));
101
102		// output the givemeyouremail
103		$renderer->table_open();
104		if ($title){
105			$renderer->tablerow_open();
106			$renderer->tableheader_open($c);
107			$renderer->doc .= $title;
108			$renderer->tableheader_close();
109			$renderer->tablerow_close();
110		}
111		$renderer->tablerow_open();
112		$renderer->tableheader_open();
113		$renderer->doc .= $lang['email'];
114		$renderer->tableheader_close();
115		for ($i = 1; $i < $c; $i++){
116			$renderer->tableheader_open();
117			$renderer->doc .= $renderer->_xmlEntities($options[$i]);
118			$renderer->tableheader_close();
119		}
120		$renderer->tablerow_close();
121
122
123		$email_adress_valid = true;
124		if ($submit = $_REQUEST[$dID.'-submit']){
125		// user has just entered his email address, let's add it
126			$user = trim($_REQUEST['fullname']);
127			if ($this->_isValidEmailAdress($user)){
128			      $givemeyouremail .= $user.';';
129			      //we append this email to the export list
130			      @file_put_contents(getcwd().'/lib/plugins/givemeyouremail/adresses.txt',$givemeyouremail);
131			}else{
132			  $email_adress_valid=false;
133			}
134			$fh = fopen($dfile, 'w');
135			fwrite($fh, serialize($givemeyouremail));
136			fclose($fh);
137		}
138
139		// display results
140		if(!empty($user)){
141		  $renderer->doc .= $this->_givemeyouremailResults($givemeyouremail, $options,$email_adress_valid,$user);
142		}
143		// display entry form
144		if ($disable=="") {
145			$renderer->doc .= $this->_givemeyouremailForm($c, $dID, $givemeyouremail, $options, $login, $single);
146		}
147		//if user is admin, print the addresses list
148		if (auth_quickaclcheck($ID) >= AUTH_ADMIN) {
149			$renderer->doc.= $this->_givemeyouremailReadAllAddresses($dfile);
150		}
151		$renderer->table_close();
152
153		return true;
154	  }
155
156	return false;
157	}
158
159  function _isValidEmailAdress($email_adress){
160      $pattern = '/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/';
161      if(empty($email_adress) || !preg_match($pattern,$email_adress)){
162	return false;
163      }
164      return true;
165  }
166
167
168  function _givemeyouremailResults($givemeyouremail, $options, $email_adress_valid,$last_email_adress){
169
170	$message_not_ok = 'L\'adresse email que tu as saisi :'.$last_email_adress.' n\'était pas valide';
171	$message_ok = 'Ton adresse : '.$last_email_adress.' est bien enregistrée.';
172
173	//was the previous email adress filled in ok? print the message!
174	if(!$email_adress_valid){
175	  $ret.= '<tr><td class="givemeyouremail_warning">'.$this->getLang('email_not_ok').' : '.$last_email_adress.'</td></tr><script>window.alert("'.$this->getLang('email_not_ok').'");</script>';
176	}else{
177	  $ret.='<tr><td class="givemeyouremail_ok">'.$this->getLang('email_ok').' : '.$last_email_adress.'</td></tr><script>window.alert("'.$this->getLang('email_ok').'");</script>';
178	}
179	return $ret;
180  }
181
182	function _givemeyouremailReadAllAddresses($dfile){
183		$email_addresses= unserialize(@file_get_contents($dfile));
184//getcwd().'/lib/plugins/givemeyouremail/adresses.txt');
185		$addresses_array = explode(';',$email_addresses);
186		foreach($addresses_array as $address){
187			$ret.='<tr><td>'.$address.'</td></tr>';
188		}
189		$ret.='<tr><td><textarea>'.$email_addresses.'</textarea></td></tr>';
190		return $ret;
191	}
192
193
194  function _givemeyouremailForm($n, $dID, $givemeyouremail, $options, $login, $single){
195	global $lang;
196	global $ID;
197	global $INFO;
198
199	$count = array();
200
201	$ret = '<form id="givemeyouremail__form" method="post" action="'.script().
202		'" accept-charset="'.$lang['encoding'].'"><tr>'.
203		'<input type="hidden" name="do" value="show" />'.
204		'<input type="hidden" name="id" value="'.$ID.'" />';
205	$ret .= '<td class="rightalign"><input type="text" name="fullname" /></td>';
206	$ret .= '</tr><tr><td class="centeralign">'.
207		'<input class="button" type="submit" name="'.$dID.'-submit" '.
208		'value="'.$this->getLang('btn_submit').'" />'.
209		'</td></tr></form>';
210	return $ret;
211  }
212}
213
214?>
215