| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
// Hook Implementations
|
| 5 |
|
| 6 |
/**
|
| 7 |
* Display help and module information
|
| 8 |
* @param section which section of the site we're displaying help for
|
| 9 |
* @return help text for the section
|
| 10 |
**/
|
| 11 |
function whatcounts_help($section='') {
|
| 12 |
$output = '';
|
| 13 |
|
| 14 |
switch ($section) {
|
| 15 |
case 'admin/help#whatcounts':
|
| 16 |
$output = '<p>'. t('Provides un/subscribe blocks for WhatCounts lists.') .'</p>';
|
| 17 |
$output .= filter_filter('process', 2, null, file_get_contents(dirname(__FILE__) . '/README.txt') );
|
| 18 |
break;
|
| 19 |
}
|
| 20 |
return $output;
|
| 21 |
}
|
| 22 |
|
| 23 |
/**
|
| 24 |
* Valid permissions for this module
|
| 25 |
* @return array An array of valid permissions for the whatcounts module
|
| 26 |
**/
|
| 27 |
function whatcounts_perm() {
|
| 28 |
return array('administer whatcounts');
|
| 29 |
}
|
| 30 |
|
| 31 |
/**
|
| 32 |
* hook_menu implementation
|
| 33 |
* @param may_cache whether the menu items might be cached
|
| 34 |
* @return array list of menu items
|
| 35 |
**/
|
| 36 |
function whatcounts_menu($may_cache) {
|
| 37 |
global $user;
|
| 38 |
$items = array();
|
| 39 |
|
| 40 |
if (!$may_cache) {
|
| 41 |
$items[] = array(
|
| 42 |
'path' => 'admin/user/whatcounts',
|
| 43 |
'title' => t('What Counts Subscriptions Admin'),
|
| 44 |
'callback' => 'drupal_get_form',
|
| 45 |
'callback arguments' => 'whatcounts_manage_form',
|
| 46 |
'access' => user_access('administer whatcounts'),
|
| 47 |
'type' => MENU_NORMAL_ITEM,
|
| 48 |
);
|
| 49 |
$items[] = array(
|
| 50 |
'path' => 'admin/settings/whatcounts',
|
| 51 |
'title' => t('What Counts'),
|
| 52 |
'description' => t('Configure WhatCounts module settings.'),
|
| 53 |
'callback' => 'drupal_get_form',
|
| 54 |
'callback arguments' => 'whatcounts_admin',
|
| 55 |
'access' => user_access('administer whatcounts'),
|
| 56 |
'type' => MENU_NORMAL_ITEM,
|
| 57 |
);
|
| 58 |
}
|
| 59 |
|
| 60 |
return $items;
|
| 61 |
}
|
| 62 |
|
| 63 |
/**
|
| 64 |
* Generate HTML for the whatcounts block
|
| 65 |
* @param op the operation from the URL
|
| 66 |
* @param delta offset
|
| 67 |
* @returns block HTML
|
| 68 |
**/
|
| 69 |
function whatcounts_block($op = 'list', $delta = 0, $edit = array()) {
|
| 70 |
if ($op == 'list') {
|
| 71 |
$block[0]['info'] = t('What Counts: Subscribe Block');
|
| 72 |
$block[1]['info'] = t('What Counts: Short Subscribe Block');
|
| 73 |
return $block;
|
| 74 |
}
|
| 75 |
else if ($op == 'view') {
|
| 76 |
$block['subject'] = t('WhatCounts');
|
| 77 |
$block['content'] = whatcounts_block_content($delta);
|
| 78 |
return $block;
|
| 79 |
}
|
| 80 |
else if ($op == 'configure') {
|
| 81 |
|
| 82 |
$form['preamble']['text'] = array(
|
| 83 |
'#type' => 'textarea',
|
| 84 |
'#title' => t('Preamble text'),
|
| 85 |
'#description' => t('Enter some content to precede the subscription form.'),
|
| 86 |
'#default_value' => variable_get('whatcounts_'.$delta.'_preamble',''),
|
| 87 |
'#rows' => 5,
|
| 88 |
);
|
| 89 |
$form['preamble']['format'] = filter_form(variable_get('whatcounts_'.$delta.'_preamble',FILTER_FORMAT_DEFAULT));
|
| 90 |
|
| 91 |
$form['subscribed'] = array(
|
| 92 |
'#type' => 'textfield',
|
| 93 |
'#title' => t('Already subscribed text'),
|
| 94 |
'#description' => t('Provide some text to display in case the user is already subscribed. Leave blank if you want the block to disappear altogether.'),
|
| 95 |
'#default_value' => variable_get('whatcounts_'.$delta.'_subscribed',''),
|
| 96 |
);
|
| 97 |
|
| 98 |
return $form;
|
| 99 |
|
| 100 |
# TODO
|
| 101 |
# on a per-block basis:
|
| 102 |
# allow admin to configure the command (sub/unsub), the list of fields,
|
| 103 |
# whether the plain-text/html option should be
|
| 104 |
# shown (plus a default override), whether a RSS checkbox is shown, and a goto url (override default)
|
| 105 |
|
| 106 |
}
|
| 107 |
else if ($op == 'save') {
|
| 108 |
variable_set('whatcounts_'.$delta.'_preamble',$edit['text']);
|
| 109 |
variable_set('whatcounts_'.$delta.'_preamble_format',$edit['format']);
|
| 110 |
variable_set('whatcounts_'.$delta.'_subscribed_text',$edit['subscribed']);
|
| 111 |
}
|
| 112 |
}
|
| 113 |
|
| 114 |
// Callback functions
|
| 115 |
|
| 116 |
/**
|
| 117 |
* Admin settings callback (from menu hook)
|
| 118 |
* The admin/settings/whatcounts page provides several configuration options:
|
| 119 |
**/
|
| 120 |
function whatcounts_admin() {
|
| 121 |
$form['whatcounts_realm'] = array(
|
| 122 |
'#type' => 'textfield',
|
| 123 |
'#title' => t('Realm'),
|
| 124 |
'#default_value' => variable_get('whatcounts_realm',''),
|
| 125 |
);
|
| 126 |
$form['whatcounts_pwd'] = array(
|
| 127 |
'#type' => 'textfield',
|
| 128 |
'#title' => t('Password'),
|
| 129 |
'#description' => t('As set through the api_setup interface'),
|
| 130 |
'#default_value' => variable_get('whatcounts_pwd',''),
|
| 131 |
);
|
| 132 |
$form['whatcounts_listid'] = array(
|
| 133 |
'#type' => 'textfield',
|
| 134 |
'#title' => t('List ID'),
|
| 135 |
'#default_value' => variable_get('whatcounts_listid',''),
|
| 136 |
);
|
| 137 |
$form['whatcounts_slid'] = array( # the account/list ID
|
| 138 |
'#type' => 'textfield',
|
| 139 |
'#title' => t('SLID'),
|
| 140 |
'#default_value' => variable_get('whatcounts_slid',''),
|
| 141 |
'#size' => 64,
|
| 142 |
'#maxlength' => 128,
|
| 143 |
'#description' => 'The SLID from your WhatCounts account',
|
| 144 |
);
|
| 145 |
$form['whatcounts_url'] = array( # the main url for the service
|
| 146 |
'#type' => 'textfield',
|
| 147 |
'#title' => t('Service URL'),
|
| 148 |
'#default_value' => variable_get('whatcounts_url',''),
|
| 149 |
'#size' => 128,
|
| 150 |
'#maxlength' => 256,
|
| 151 |
'#description' => 'The main URL for the WhatCounts service (eg. http://goto.rabble.ca/bin/listctrl)',
|
| 152 |
);
|
| 153 |
$form['whatcounts_goto'] = array(
|
| 154 |
'#type' => 'textfield',
|
| 155 |
'#title' => t('Goto URL'),
|
| 156 |
'#default_value' => variable_get('whatcounts_goto',''),
|
| 157 |
'#size' => 128,
|
| 158 |
'#maxlength' => 256,
|
| 159 |
);
|
| 160 |
$form['whatcounts_default_format'] = array(
|
| 161 |
'#type' => 'radios',
|
| 162 |
'#title' => t('Default Format'),
|
| 163 |
'#options' => array('plain' => t('Plain'), 'html' => t('HTML')),
|
| 164 |
'#default_value' => variable_get('whatcounts_default_format','plain'),
|
| 165 |
); # plain-text or html
|
| 166 |
#$form['whatcounts_default_rss'] = array(); # provide RSS option or not (default)
|
| 167 |
#$form['whatcounts_numblocks'] = array(); # number of blocks to provide
|
| 168 |
|
| 169 |
return system_settings_form($form);
|
| 170 |
}
|
| 171 |
|
| 172 |
/**
|
| 173 |
* Block content callback (from block hook) The block content hook takes an
|
| 174 |
* offset, and provides the content for a block to subscribe/unsubscribe
|
| 175 |
* to/from a WhatCounts list, as configured by the admin.
|
| 176 |
**/
|
| 177 |
function whatcounts_block_content($delta = 0) {
|
| 178 |
global $user;
|
| 179 |
|
| 180 |
$p = variable_get('whatcounts_'.$delta.'_preamble', '');
|
| 181 |
$f = variable_get('whatcounts_'.$delta.'_preamble_format', FILTER_FORMAT_DEFAULT);
|
| 182 |
$pre = '<div class="preamble">'. check_markup($p, $f, FALSE) .'</div>';
|
| 183 |
|
| 184 |
if ($user->uid) {
|
| 185 |
if (_whatcounts_is_subscribed($user->mail)) { # is subscribed to the whatcounts listid in question
|
| 186 |
return variable_get('whatcounts_'.$delta.'_subscribed_text','');
|
| 187 |
} else { # they are not subscribed
|
| 188 |
return $pre . drupal_get_form('whatcounts_block_form_'.$delta,$user->mail);
|
| 189 |
}
|
| 190 |
} else {
|
| 191 |
return $pre . drupal_get_form('whatcounts_block_form_'.$delta);
|
| 192 |
}
|
| 193 |
}
|
| 194 |
|
| 195 |
/**
|
| 196 |
* Implementation of hook_forms to map the block_content delta-based formIDs
|
| 197 |
* onto the build_form function
|
| 198 |
**/
|
| 199 |
function whatcounts_forms() {
|
| 200 |
# provide one of these for each of the admin-configured $numblocks
|
| 201 |
$forms['whatcounts_block_form_0'] = array(
|
| 202 |
'callback' => 'whatcounts_build_form',
|
| 203 |
);
|
| 204 |
$forms['whatcounts_block_form_1'] = array(
|
| 205 |
'callback' => 'whatcounts_build_form',
|
| 206 |
);
|
| 207 |
return $forms;
|
| 208 |
}
|
| 209 |
|
| 210 |
function whatcounts_build_form($mail = '') {
|
| 211 |
drupal_add_js(drupal_get_path('module', 'whatcounts') . '/whatcounts.js');
|
| 212 |
drupal_add_css(drupal_get_path('module', 'whatcounts') . '/whatcounts.css');
|
| 213 |
|
| 214 |
$form = array();
|
| 215 |
|
| 216 |
$form['slid'] = array(
|
| 217 |
'#type' => 'value',
|
| 218 |
'#value' => variable_get('whatcounts_slid',''),
|
| 219 |
);
|
| 220 |
$form['cmd'] = array(
|
| 221 |
'#type' => 'value',
|
| 222 |
'#value' => 'subscribe',
|
| 223 |
);
|
| 224 |
$form['goto'] = array(
|
| 225 |
'#type' => 'value',
|
| 226 |
'#value' => variable_get('whatcounts_goto',''),
|
| 227 |
);
|
| 228 |
|
| 229 |
$form['email'] = array(
|
| 230 |
'#type' => 'textfield',
|
| 231 |
'#title' => t('Your e-mail address'),
|
| 232 |
'#size' => 20,
|
| 233 |
'#maxlength' => 64,
|
| 234 |
'#default_value' => $mail,
|
| 235 |
);
|
| 236 |
$form['first'] = array(
|
| 237 |
'#type' => 'textfield',
|
| 238 |
'#title' => t('First Name'),
|
| 239 |
'#size' => 20,
|
| 240 |
'#maxlength' => 64,
|
| 241 |
);
|
| 242 |
$form['last'] = array(
|
| 243 |
'#type' => 'textfield',
|
| 244 |
'#title' => t('Last Name'),
|
| 245 |
'#size' => 20,
|
| 246 |
'#maxlength' => 64,
|
| 247 |
);
|
| 248 |
$form['format'] = array(
|
| 249 |
'#type' => 'radios',
|
| 250 |
'#name' => 'format',
|
| 251 |
'#options' => array('plain' => t('Plain Text'), 'html' => t('HTML')),
|
| 252 |
'#default_value' => variable_get('whatcounts_default_format','plain'),
|
| 253 |
'#title' => t('Format'),
|
| 254 |
'#description' => t('Select a format in which you would like to receive emails.'),
|
| 255 |
);
|
| 256 |
|
| 257 |
$form['submit'] = array('#type' => 'submit', '#value' => t('Go!'));
|
| 258 |
$form['#submit']['whatcounts_form_submit'] = array(); # Same submit handler for all form IDs
|
| 259 |
return $form;
|
| 260 |
}
|
| 261 |
|
| 262 |
# This should call the appropriate function depending on what cmd the form requested..
|
| 263 |
function whatcounts_form_submit($form_id, $form_values) {
|
| 264 |
_whatcounts_subscribe($form_values['email'],$form_values['first'],$form_values['last']);
|
| 265 |
return $form_values['goto'];
|
| 266 |
}
|
| 267 |
|
| 268 |
# This should modify the form at $delta depending on the configuration as set by the admin (in the block's settings)
|
| 269 |
function whatcounts_form_alter($form_id,&$form) {
|
| 270 |
if ($form_id == 'whatcounts_block_form_0') {
|
| 271 |
$form['email']['#size'] = 10;
|
| 272 |
$form['first']['#size'] = 10;
|
| 273 |
$form['last']['#size'] = 10;
|
| 274 |
}
|
| 275 |
|
| 276 |
if ($form_id == 'whatcounts_block_form_1') {
|
| 277 |
$form['first'] = array('#type' => 'value', '#value' => '');
|
| 278 |
$form['last'] = array('#type' => 'value', '#value' => '');
|
| 279 |
$form['format'] = array('#type' => 'value', '#value' => variable_get('whatcounts_default_format','plain'));
|
| 280 |
}
|
| 281 |
}
|
| 282 |
|
| 283 |
function whatcounts_manage_form() {
|
| 284 |
$form['email'] = array(
|
| 285 |
'#type' => 'textfield',
|
| 286 |
'#title' => t('E-Mail'),
|
| 287 |
'#size' => 50,
|
| 288 |
'#maxlength' => 100,
|
| 289 |
'#description' => t('Please enter an email address.'),
|
| 290 |
);
|
| 291 |
$form['test'] = array('#type' => 'submit', '#value' => t('Is Email Subscribed?'));
|
| 292 |
$form['sub'] = array('#type' => 'submit', '#value' => t('Subscribe Email'));
|
| 293 |
$form['unsub'] = array('#type' => 'submit', '#value' => t('Unsubscribe Email'));
|
| 294 |
$form['delete'] = array('#type' => 'submit', '#value' => t('Delete Email'));
|
| 295 |
$form['help'] = array('#value' => '<div class="help">'.t('Use this form to manage emails on your WhatCounts list. Test if an email is subscribed using the first submit button. Subscribe an email using the Subscribe Email button. Opt-out an email using the Unsubscribe Email, and remove an email entirely using the Delete Email button.').'</div>');
|
| 296 |
return $form;
|
| 297 |
}
|
| 298 |
|
| 299 |
function whatcounts_manage_form_submit($form_id, $form_values) {
|
| 300 |
$op = $_POST['op'];
|
| 301 |
|
| 302 |
switch ($op) {
|
| 303 |
case t('Is Email Subscribed?'):
|
| 304 |
$is_subbed = _whatcounts_is_subscribed($form_values['email']);
|
| 305 |
$msg = sprintf("The email address '%s' is: %s",
|
| 306 |
$form_values['email'],$is_subbed?"Subscribed":"NOT Subscribed");
|
| 307 |
break;
|
| 308 |
case t('Subscribe Email'):
|
| 309 |
$res = _whatcounts_subscribe($form_values['email']);
|
| 310 |
$msg = sprintf("WhatCounts subscription for email %s is: %s", $form_values['email'], $res?"SUCCESSFUL":"FAILED");
|
| 311 |
break;
|
| 312 |
case t('Unsubscribe Email'):
|
| 313 |
$res = _whatcounts_unsubscribe($form_values['email']);
|
| 314 |
$msg = sprintf("WhatCounts unsubscription for email %s is: %s", $form_values['email'], $res?"SUCCESSFUL":"FAILED");
|
| 315 |
break;
|
| 316 |
case t('Delete Email'):
|
| 317 |
$res = _whatcounts_delete($form_values['email']);
|
| 318 |
$msg = sprintf("WhatCounts deletion for email %s is: %s", $form_values['email'], $res?"SUCCESSFUL":"FAILED");
|
| 319 |
}
|
| 320 |
drupal_set_message($msg);
|
| 321 |
}
|
| 322 |
|
| 323 |
// Utility Functions
|
| 324 |
|
| 325 |
function _whatcounts_is_subscribed($email,$debug=0) {
|
| 326 |
$data = sprintf('cmd=findinlist&r=%s&pwd=%s&list_id=%s&email=%s',
|
| 327 |
variable_get('whatcounts_realm','whatcounts'),
|
| 328 |
variable_get('whatcounts_pwd','resesymm1'),
|
| 329 |
variable_get('whatcounts_listid','42'),
|
| 330 |
$email
|
| 331 |
);
|
| 332 |
|
| 333 |
$str = _whatcounts_curl($data, $debug);
|
| 334 |
if (preg_match("/$email/",$str)) { return TRUE; }
|
| 335 |
else { return FALSE; }
|
| 336 |
if ($str) { return TRUE; }
|
| 337 |
else { return FALSE; }
|
| 338 |
}
|
| 339 |
|
| 340 |
function _whatcounts_subscribe($email,$first='',$last='') {
|
| 341 |
|
| 342 |
$data = sprintf('cmd=sub&r=%s&pwd=%s&list_id=%s&force_sub=1&data=',
|
| 343 |
variable_get('whatcounts_realm','whatcounts'),
|
| 344 |
variable_get('whatcounts_pwd','resesymm1'),
|
| 345 |
variable_get('whatcounts_listid','42'), $email);
|
| 346 |
|
| 347 |
if ($first && $last) {
|
| 348 |
$data .= sprintf('email,first,last^%s,%s,%s',$email,$first,$last);
|
| 349 |
} elseif ($first) { # just first
|
| 350 |
$data .= sprintf('email,first^%s,%s',$email,$first);
|
| 351 |
} elseif ($last) { # just last
|
| 352 |
$data .= sprintf('email,last^%s,%s',$email,$last);
|
| 353 |
} else { # neither
|
| 354 |
$data .= sprintf('email^%s',$email);
|
| 355 |
}
|
| 356 |
|
| 357 |
$res = _whatcounts_curl($data);
|
| 358 |
if (preg_match('/SUCCESS/',$res)) { return 1; }
|
| 359 |
else { return 0; }
|
| 360 |
}
|
| 361 |
|
| 362 |
function _whatcounts_unsubscribe($email) {
|
| 363 |
|
| 364 |
$data = sprintf('cmd=unsub&r=%s&pwd=%s&list_id=%s&data=%s',
|
| 365 |
variable_get('whatcounts_realm','whatcounts'),
|
| 366 |
variable_get('whatcounts_pwd','resesymm1'),
|
| 367 |
variable_get('whatcounts_listid','42'),
|
| 368 |
$email
|
| 369 |
);
|
| 370 |
|
| 371 |
$res = _whatcounts_curl($data);
|
| 372 |
if (preg_match('/SUCCESS/',$res)) { return 1; }
|
| 373 |
else { return 0; }
|
| 374 |
}
|
| 375 |
|
| 376 |
function _whatcounts_delete($email) {
|
| 377 |
|
| 378 |
$data = sprintf('cmd=delete&r=%s&pwd=%s&list_id=%s&data=email^%s',
|
| 379 |
variable_get('whatcounts_realm','whatcounts'),
|
| 380 |
variable_get('whatcounts_pwd','resesymm1'),
|
| 381 |
variable_get('whatcounts_listid','42'),
|
| 382 |
$email
|
| 383 |
);
|
| 384 |
|
| 385 |
$res = _whatcounts_curl($data);
|
| 386 |
if (preg_match('/SUCCESS/',$res)) { return 1; }
|
| 387 |
else { return 0; }
|
| 388 |
}
|
| 389 |
|
| 390 |
function _whatcounts_curl($data,$debug=0) {
|
| 391 |
# DEBUG
|
| 392 |
if ($debug) {
|
| 393 |
_whatcounts_dsm("curl data",$data);
|
| 394 |
}
|
| 395 |
|
| 396 |
# POST the data to WhatCounts
|
| 397 |
$url = variable_get('whatcounts_url','http://whatcounts.com/bin/api_web');
|
| 398 |
$url .= sprintf('?%s',$data);
|
| 399 |
|
| 400 |
$data_array = split("&",$data);
|
| 401 |
foreach ($data_array as $entry) {
|
| 402 |
$datum = split("=",$entry);
|
| 403 |
$d[$datum[0]] = $datum[1];
|
| 404 |
}
|
| 405 |
|
| 406 |
$curl = curl_init();
|
| 407 |
curl_setopt($curl, CURLOPT_HEADER, false);
|
| 408 |
curl_setopt($curl, CURLOPT_URL, $url );
|
| 409 |
|
| 410 |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
|
| 411 |
curl_setopt($curl, CURLOPT_BINARYTRANSFER, true);
|
| 412 |
|
| 413 |
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
|
| 414 |
curl_setopt($curl, CURLOPT_DNS_CACHE_TIMEOUT, 10);
|
| 415 |
|
| 416 |
// execute and return string (this should be an empty string '')
|
| 417 |
$str = curl_exec($curl);
|
| 418 |
if ($debug) {
|
| 419 |
_dsm("curl debug",curl_getinfo($curl));
|
| 420 |
_dsm("return value",$str);
|
| 421 |
}
|
| 422 |
return $str;
|
| 423 |
}
|
| 424 |
|
| 425 |
// Theme Functions
|
| 426 |
|
| 427 |
function theme_whatcounts_form($form) {
|
| 428 |
return '<div class="whatcounts">' . drupal_render($form) . '</div>';
|
| 429 |
}
|
| 430 |
|
| 431 |
function theme_whatcounts_block_form($form) {
|
| 432 |
return '<div class="whatcounts">' . drupal_render($form) . '</div>';
|
| 433 |
}
|
| 434 |
|
| 435 |
// Test Functions
|
| 436 |
|
| 437 |
function _whatcounts_dsm($msg='',&$v) {
|
| 438 |
ob_start();
|
| 439 |
print_r($v);
|
| 440 |
drupal_set_message($msg .": ". ob_get_contents());
|
| 441 |
ob_end_clean();
|
| 442 |
}
|