-
Notifications
You must be signed in to change notification settings - Fork 0
/
MailCollector.php
209 lines (168 loc) · 5.46 KB
/
MailCollector.php
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
/*
Plugin Name: MailCollector
Plugin URI: http://digitalhymn.com
Description: Gathers e-mail addresses to be used in ML. Shortcode: [mailcollectorform destination="path/to/page"]
Author: Davide 'Folletto' Casali
Version: 0.1
Author URI: http://digitalhymn.com
******************************************************************************************
* Originally designed for Good50x70.org
* This plugins handlers mail gathering.
*
*
*
*/
$cmc_version = "0.1";
// Libs
require_once 'lib/wordpress.portal.php';
// Admin
require_once 'admin/wp-admin.php';
require_once 'admin/wp-shortcode.php';
/************************************************************************************ CLASS
******************************************************************************************
* MailCollector Class
*/
class MailCollector {
function form($action, $label = "Get it") {
/****************************************************************************************************
* Show form.
*
* @return html
*/
$out = '';
/*$out .= ' <script type="text/javascript">
function mailcollectorCheckEmail() {
var mt = document.getElementById("mailcollectorEmail");
if (mt.value.match(/^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/)) {
return true;
}
return false;
}
</script>
';*/
$out .= '<form id="mailcollector" action="' . get_bloginfo('url') . "/mailcollector/submit" . '" enctype="multipart/form-data" method="post">';
$out .= '<input type="hidden" name="destination" id="destination" value="' . $action . '" />';
$out .= '<label for="mailcollectorEmail">';
$out .= ' <input type="text" maxlength="50" id="email" name="email" />';
$out .= '</label>';
$out .= '<input type="submit" value="' . $label . '" />';
$out .= '<form>';
return $out;
}
function add_mail($email, $notes = "") {
/****************************************************************************************************
* Adds the specified mail to the database.
*
* @param e-mail string
* @return insert ID on success, false on fail
*/
global $wpdb;
$out = false;
$email = $wpdb->escape($email);
$notes = $wpdb->escape($notes);
if ($email && !$this->mail_exists($email)) {
$query = "
INSERT INTO " . $wpdb->prefix . "mailcollector
(email, notes, timestamp)
VALUES
('$email', '$notes', NOW())
";
$result = $wpdb->query($query);
if ($result > 0) {
$out = mysql_insert_id();
}
}
return $out;
}
function mail_exists($email) {
/****************************************************************************************************
* Check if a mail exists.
*
* @return boolean
*/
global $wpdb;
$out = array();
$query = 'SELECT email FROM ' . $wpdb->prefix . 'mailcollector As mc
WHERE
email = \'' . $email . '\'
';
$out = $wpdb->get_results($query);
return sizeof($out) > 0;
}
function get_list() {
/****************************************************************************************************
* Get the mail list from DB.
*
* @return array of emails
*/
global $wpdb;
$out = array();
$query = 'SELECT * FROM ' . $wpdb->prefix . 'mailcollector As mc
WHERE
flag > 0
ORDER BY
email ASC
';
$out = $wpdb->get_results($query);
return $out;
}
function get_emails() {
/****************************************************************************************************
* Get the mail list from DB.
*
* @return array of emails
*/
$out = array();
$list = $this->get_list();
foreach ($list as $item) {
$out[] = $item->email;
}
return $out;
}
}
/************************************************************************************ SETUP
******************************************************************************************
* MailCollector Setup
*/
register_activation_hook(__FILE__, 'cmc_install');
function cmc_install() {
global $wpdb;
global $cmc_version;
$out = -1;
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
$table_name = $wpdb->prefix . "mailcollector";
if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
$sql = "CREATE TABLE `$table_name` (
`mid` int(11) unsigned NOT NULL auto_increment,
`email` varchar(50) NOT NULL default '',
`notes` text,
`timestamp` datetime default NULL,
`flag` tinyint(8) NOT NULL default '1',
PRIMARY KEY (`mid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;";
echo $sql;
// *** Create table using delta diff (supports upgrades)
dbDelta($sql);
$out++;
}
// ****** Completing
if ($out > 0) {
add_option("cmc_version", $cmc_version);
}
return $out;
}
/************************************************************************************** RUN
******************************************************************************************
* ImageContest Runtime
*/
$MailCollector = new MailCollector(); // OBJECT INIT
wpp::add_virtual_page('mailcollector/api/add', array(
//get_template_directory() . "/mailcollector.php",
dirname(__FILE__) . "/virtual/api.add.php"
));
wpp::add_virtual_page('mailcollector/submit', array(
get_template_directory() . "/mailcollector.php",
dirname(__FILE__) . "/virtual/submit.php"
));
?>