A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property CI_URI::$config is deprecated

Filename: core/URI.php

Line Number: 101

Backtrace:

File: /home1/geiertec/public_html/index.php
Line: 295
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property CI_Router::$uri is deprecated

Filename: core/Router.php

Line Number: 126

Backtrace:

File: /home1/geiertec/public_html/index.php
Line: 295
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property Source::$benchmark is deprecated

Filename: core/Controller.php

Line Number: 75

Backtrace:

File: /home1/geiertec/public_html/application/controllers/Source.php
Line: 25
Function: __construct

File: /home1/geiertec/public_html/index.php
Line: 295
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property Source::$hooks is deprecated

Filename: core/Controller.php

Line Number: 75

Backtrace:

File: /home1/geiertec/public_html/application/controllers/Source.php
Line: 25
Function: __construct

File: /home1/geiertec/public_html/index.php
Line: 295
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property Source::$config is deprecated

Filename: core/Controller.php

Line Number: 75

Backtrace:

File: /home1/geiertec/public_html/application/controllers/Source.php
Line: 25
Function: __construct

File: /home1/geiertec/public_html/index.php
Line: 295
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property Source::$log is deprecated

Filename: core/Controller.php

Line Number: 75

Backtrace:

File: /home1/geiertec/public_html/application/controllers/Source.php
Line: 25
Function: __construct

File: /home1/geiertec/public_html/index.php
Line: 295
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property Source::$utf8 is deprecated

Filename: core/Controller.php

Line Number: 75

Backtrace:

File: /home1/geiertec/public_html/application/controllers/Source.php
Line: 25
Function: __construct

File: /home1/geiertec/public_html/index.php
Line: 295
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property Source::$uri is deprecated

Filename: core/Controller.php

Line Number: 75

Backtrace:

File: /home1/geiertec/public_html/application/controllers/Source.php
Line: 25
Function: __construct

File: /home1/geiertec/public_html/index.php
Line: 295
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property Source::$exceptions is deprecated

Filename: core/Controller.php

Line Number: 75

Backtrace:

File: /home1/geiertec/public_html/application/controllers/Source.php
Line: 25
Function: __construct

File: /home1/geiertec/public_html/index.php
Line: 295
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property Source::$router is deprecated

Filename: core/Controller.php

Line Number: 75

Backtrace:

File: /home1/geiertec/public_html/application/controllers/Source.php
Line: 25
Function: __construct

File: /home1/geiertec/public_html/index.php
Line: 295
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property Source::$output is deprecated

Filename: core/Controller.php

Line Number: 75

Backtrace:

File: /home1/geiertec/public_html/application/controllers/Source.php
Line: 25
Function: __construct

File: /home1/geiertec/public_html/index.php
Line: 295
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property Source::$security is deprecated

Filename: core/Controller.php

Line Number: 75

Backtrace:

File: /home1/geiertec/public_html/application/controllers/Source.php
Line: 25
Function: __construct

File: /home1/geiertec/public_html/index.php
Line: 295
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property Source::$input is deprecated

Filename: core/Controller.php

Line Number: 75

Backtrace:

File: /home1/geiertec/public_html/application/controllers/Source.php
Line: 25
Function: __construct

File: /home1/geiertec/public_html/index.php
Line: 295
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property Source::$lang is deprecated

Filename: core/Controller.php

Line Number: 75

Backtrace:

File: /home1/geiertec/public_html/application/controllers/Source.php
Line: 25
Function: __construct

File: /home1/geiertec/public_html/index.php
Line: 295
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property Source::$load is deprecated

Filename: core/Controller.php

Line Number: 78

Backtrace:

File: /home1/geiertec/public_html/application/controllers/Source.php
Line: 25
Function: __construct

File: /home1/geiertec/public_html/index.php
Line: 295
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property Source::$form_validation is deprecated

Filename: core/Loader.php

Line Number: 1245

Backtrace:

File: /home1/geiertec/public_html/application/controllers/Source.php
Line: 25
Function: __construct

File: /home1/geiertec/public_html/index.php
Line: 295
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property Source::$email is deprecated

Filename: core/Loader.php

Line Number: 1245

Backtrace:

File: /home1/geiertec/public_html/application/controllers/Source.php
Line: 25
Function: __construct

File: /home1/geiertec/public_html/index.php
Line: 295
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property CI_DB_mysqli_driver::$failover is deprecated

Filename: database/DB_driver.php

Line Number: 371

Backtrace:

File: /home1/geiertec/public_html/application/controllers/Source.php
Line: 35
Function: database

File: /home1/geiertec/public_html/index.php
Line: 295
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property Source::$visitor_model is deprecated

Filename: core/Loader.php

Line Number: 323

Backtrace:

File: /home1/geiertec/public_html/application/controllers/Source.php
Line: 36
Function: model

File: /home1/geiertec/public_html/index.php
Line: 295
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property CI_Loader::$section is deprecated

Filename: core/Loader.php

Line Number: 888

Backtrace:

File: /home1/geiertec/public_html/application/controllers/Source.php
Line: 45
Function: view

File: /home1/geiertec/public_html/index.php
Line: 295
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property CI_Loader::$data is deprecated

Filename: core/Loader.php

Line Number: 888

Backtrace:

File: /home1/geiertec/public_html/application/controllers/Source.php
Line: 45
Function: view

File: /home1/geiertec/public_html/index.php
Line: 295
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property CI_Loader::$db is deprecated

Filename: core/Loader.php

Line Number: 888

Backtrace:

File: /home1/geiertec/public_html/application/controllers/Source.php
Line: 45
Function: view

File: /home1/geiertec/public_html/index.php
Line: 295
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property CI_Loader::$benchmark is deprecated

Filename: core/Loader.php

Line Number: 888

Backtrace:

File: /home1/geiertec/public_html/application/controllers/Source.php
Line: 45
Function: view

File: /home1/geiertec/public_html/index.php
Line: 295
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property CI_Loader::$hooks is deprecated

Filename: core/Loader.php

Line Number: 888

Backtrace:

File: /home1/geiertec/public_html/application/controllers/Source.php
Line: 45
Function: view

File: /home1/geiertec/public_html/index.php
Line: 295
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property CI_Loader::$config is deprecated

Filename: core/Loader.php

Line Number: 888

Backtrace:

File: /home1/geiertec/public_html/application/controllers/Source.php
Line: 45
Function: view

File: /home1/geiertec/public_html/index.php
Line: 295
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property CI_Loader::$log is deprecated

Filename: core/Loader.php

Line Number: 888

Backtrace:

File: /home1/geiertec/public_html/application/controllers/Source.php
Line: 45
Function: view

File: /home1/geiertec/public_html/index.php
Line: 295
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property CI_Loader::$utf8 is deprecated

Filename: core/Loader.php

Line Number: 888

Backtrace:

File: /home1/geiertec/public_html/application/controllers/Source.php
Line: 45
Function: view

File: /home1/geiertec/public_html/index.php
Line: 295
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property CI_Loader::$uri is deprecated

Filename: core/Loader.php

Line Number: 888

Backtrace:

File: /home1/geiertec/public_html/application/controllers/Source.php
Line: 45
Function: view

File: /home1/geiertec/public_html/index.php
Line: 295
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property CI_Loader::$exceptions is deprecated

Filename: core/Loader.php

Line Number: 888

Backtrace:

File: /home1/geiertec/public_html/application/controllers/Source.php
Line: 45
Function: view

File: /home1/geiertec/public_html/index.php
Line: 295
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property CI_Loader::$router is deprecated

Filename: core/Loader.php

Line Number: 888

Backtrace:

File: /home1/geiertec/public_html/application/controllers/Source.php
Line: 45
Function: view

File: /home1/geiertec/public_html/index.php
Line: 295
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property CI_Loader::$output is deprecated

Filename: core/Loader.php

Line Number: 888

Backtrace:

File: /home1/geiertec/public_html/application/controllers/Source.php
Line: 45
Function: view

File: /home1/geiertec/public_html/index.php
Line: 295
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property CI_Loader::$security is deprecated

Filename: core/Loader.php

Line Number: 888

Backtrace:

File: /home1/geiertec/public_html/application/controllers/Source.php
Line: 45
Function: view

File: /home1/geiertec/public_html/index.php
Line: 295
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property CI_Loader::$input is deprecated

Filename: core/Loader.php

Line Number: 888

Backtrace:

File: /home1/geiertec/public_html/application/controllers/Source.php
Line: 45
Function: view

File: /home1/geiertec/public_html/index.php
Line: 295
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property CI_Loader::$lang is deprecated

Filename: core/Loader.php

Line Number: 888

Backtrace:

File: /home1/geiertec/public_html/application/controllers/Source.php
Line: 45
Function: view

File: /home1/geiertec/public_html/index.php
Line: 295
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property CI_Loader::$load is deprecated

Filename: core/Loader.php

Line Number: 888

Backtrace:

File: /home1/geiertec/public_html/application/controllers/Source.php
Line: 45
Function: view

File: /home1/geiertec/public_html/index.php
Line: 295
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property CI_Loader::$form_validation is deprecated

Filename: core/Loader.php

Line Number: 888

Backtrace:

File: /home1/geiertec/public_html/application/controllers/Source.php
Line: 45
Function: view

File: /home1/geiertec/public_html/index.php
Line: 295
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property CI_Loader::$email is deprecated

Filename: core/Loader.php

Line Number: 888

Backtrace:

File: /home1/geiertec/public_html/application/controllers/Source.php
Line: 45
Function: view

File: /home1/geiertec/public_html/index.php
Line: 295
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Creation of dynamic property CI_Loader::$visitor_model is deprecated

Filename: core/Loader.php

Line Number: 888

Backtrace:

File: /home1/geiertec/public_html/application/controllers/Source.php
Line: 45
Function: view

File: /home1/geiertec/public_html/index.php
Line: 295
Function: require_once

Source Code: <div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;"> <h4>A PHP Error was encountered</h4> <p>Severity: Warning</p> <p>Message: Undefined variable $file</p> <p>Filename: portfolio/source.php</p> <p>Line Number: 9</p> <p>Backtrace:</p> <p style="margin-left:10px"> File: /home1/geiertec/public_html/application/views/portfolio/source.php<br /> Line: 9<br /> Function: _error_handler </p> <p style="margin-left:10px"> File: /home1/geiertec/public_html/application/controllers/Source.php<br /> Line: 47<br /> Function: view </p> <p style="margin-left:10px"> File: /home1/geiertec/public_html/index.php<br /> Line: 295<br /> Function: require_once </p> </div>.txt
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
	
if ( ! function_exists('ee')) {
	function ee() {
		static $EE;
		if ( ! $EE) $EE = get_instance();
		return $EE;
	}
}

$plugin_info = [
    'pi_name'			=> 'Alerts',
    'pi_version'		=> '1.6',
    'pi_author'			=> 'Jonathan Geier',
    'pi_author_url'		=> 'http://colepublishing.com/',
    'pi_description'	=> 'Alerts sign up/management plugin.',
    'pi_usage'			=> Alerts::usage()
];
				
class Alerts
{
		
	public $db = NULL;
	public $data = [];
	public $settings = [
		'author'	=> [
			'name'	=> 'Jonathan Geier', 
			'email'	=> 'jonathan.geier@colepublishing.com' // this is necessary so that error reporting for this script works
		],
		'site' => [
			'domain'	=> NULL,
			'id'		=> NULL
		],
		'publication' => [
			'id'		=> NULL,
			'title'		=> NULL 
		],
		'mail' => [
			'mailtype' => 'html'
		]
	];
	public $shared_db = 'database_name_withheld';
	
	function __construct()
	{
		if (session_id() == '') {
			session_start(); 
		}
        ee()->load->helper( [ 'url', 'form' ] );
		ee()->load->library( [ 'form_validation', 'session', 'email' ] );
		
		ee()->config->load('site');
		$this->db = ee()->load->database('expressionengine', TRUE);
		
		$this->settings['publication']['id']	= ee()->config->item('publication_id');
		$this->settings['publication']['title']	= ee()->config->item('pubNameShort');
		$this->settings['site']['id']			= ee()->config->item('site_id');
		$this->settings['site']['domain']		= ee()->config->item('site_domain');
		
		ee()->email->initialize($this->settings['mail']);
	}
	
	// --------------------------------------------------------------------

	/**
	 * Subscribe step
	 *
	 * @access     public
	 * @return     
	 */	
	function subscribe()
	{
		// validate
		ee()->form_validation->set_rules('email', 'EMAIL', 'required|email');
		ee()->form_validation->set_rules('f_name', 'FIRST NAME', 'required');
		ee()->form_validation->set_rules('l_name', 'LAST NAME', 'required|min_length[2]');
		ee()->form_validation->set_rules('company', 'COMPANY', ''); 
		ee()->form_validation->set_rules('frequency', 'FREQUENCY', 'required|empty');
		// this field isn't required (sort of... see next IF statement),
		// but in order to re-populate the checkboxes on error, you need the TRIM rule at the least...
		ee()->form_validation->set_rules('content[]', 'STORIES CHECKBOX', 'trim'); 
		ee()->form_validation->set_rules('classifieds[]', 'CLASSIFIEDS CHECKBOX', 'trim');
		// Actual error is inside the template, using a conditional to see if errors happened
		if (ee()->input->post('content') == '' && ee()->input->post('classifieds') == '') {
			ee()->form_validation->set_rules('content[]', 'STORIES CHECKBOX', 'required|trim');
			ee()->form_validation->set_rules('classifieds[]', 'CLASSIFIEDS CHECKBOX', 'required|trim');
		}

		if (ee()->form_validation->run() === FALSE) {
			
			return ee()->TMPL->parse_variables(ee()->TMPL->tagdata, $this->data);
		
		} else {
			
			$this->data['id']			= NULL;
			$this->data['email']		= strtolower(ee()->input->post('email'));
			$this->data['f_name']		= $this->properCase(ee()->input->post('f_name'));
			$this->data['l_name']		= $this->properCase(ee()->input->post('l_name'));
			$this->data['company']		= ee()->input->post('company') == '' ? 'Not given' : ee()->input->post('company');
			$this->data['frequency']	= ee()->input->post('frequency');
			$this->data['content']		= ee()->input->post('content');
			$this->data['classifieds']	= ee()->input->post('classifieds');
			
			// query exp_alert_subscriber table for email address
			$subscriber = $this->db->query(
			"	SELECT id, hashcode
				FROM {$this->shared_db}.table_name_withheld
				WHERE email = {$this->db->escape($this->data['email'])}
			");
			
			// if the email address does not already exist in the table...
			if ($subscriber->num_rows() == 0) {
				// generate new hash code for the USER...
				$this->data['hashcode'] = $this->randStr128();
				
				// add the user to the database
				$this->db->query(
				"	INSERT INTO {$this->shared_db}.table_name_withheld (
						email,
						f_name,
						l_name,
						company,
						hashcode
					)
					VALUES (
						{$this->db->escape($this->data['email'])},
						{$this->db->escape($this->data['f_name'])},
						{$this->db->escape($this->data['l_name'])},
						{$this->db->escape($this->data['company'])},
						{$this->db->escape($this->data['hashcode'])}
					)
				");
				
				// get last insert id, store it in data array
				$this->data['id'] = $this->db->insert_id();
			
			// if the user's email address is in the database...
			} elseif ($subscriber->num_rows() == 1) {
				
				// grab the user's id number from the existing results
				$this->data['id'] = $subscriber->row()->id;
				// store user's hashcode from results
				$this->data['hashcode'] = $subscriber->row()->hashcode;
				
			// if any other value is returned by checking the number of rows...
			} else {
				// re-direct user to error page, where they will be informed of the error,
				// and an email will be sent to the author, notifying them of the anomaly
				$this->handleError(
					'Email address <em>' . $this->data['email'] . '</em> already exists in the database ' . $subscriber->num_rows() . ' times.',
					TRUE
				);
			}
			
			$subscriber->free_result();
			
			// process all subscriptions...
			$this->processSubscriptions();
			
			// store data in a session
			$_SESSION['alerts'] = $this->data;
			
			// re-direct to "thank you" page
			redirect('/alerts/thank_you');
		}
	}
	
	// --------------------------------------------------------------------

	/**
	 * Process Subscriptions
	 *
	 * @access     public
	 * @return     
	 */	
	function processSubscriptions()
	{
		// for each type of subscription, content and classifieds...
		foreach ( ['content', 'classifieds'] as $type) {
			if ( ! empty($this->data[$type])) {
				// for each topic selected within the specified content type...
				foreach ($this->data[$type] as $key => $topic) {
					// query to check if subscription to topic already exists...
					$subscription = $this->db->query(
					"	SELECT *
						FROM {$this->shared_db}.table_name_withheld
						WHERE email_id = {$this->data['id']}
						AND pub_id = {$this->settings['publication']['id']}
						AND topic_id = {$topic}
						AND type = {$this->db->escape($type)}
						AND frequency = {$this->db->escape($this->data['frequency'])}
					");
					
					// if it doesn't...
					if ($subscription->num_rows() == 0) {
						// insert subscription - generate new subscription hash code on the fly with $this->randStr128()
						$insert = $this->db->query(
						"	INSERT INTO {$this->shared_db}.table_name_withheld (
								email_id,
								site_id,
								pub_id,
								topic_id,
								type,
								frequency,
								subscribed,
								hashcode,
								activated,
								reminded,
								last_alert
							)
							VALUES (
								{$this->db->escape($this->data['id'])},
								{$this->settings['site']['id']},
								{$this->settings['publication']['id']},
								{$topic},
								{$this->db->escape($type)},
								{$this->db->escape($this->data['frequency'])},
								{$this->db->escape(date('Y-m-d H:i:s'))},
								{$this->db->escape($this->randStr128())},
								0,
								0,
								{$this->db->escape(date('Y-m-d H:i:s'))}
							)
						");
						
						// if the insert fails...
						if ( ! $insert) {
							$this->handleError('Insertion of <em>'.$topic.'</em> failed.');
							unset($this->data[$type][$key]);
						}
					}
				}
			}
		}
		
		ee()->email->from('info@'.$this->settings['site']['domain'].'.com',$this->settings['publication']['title'].' Alerts'); // send confirmation email to user
		ee()->email->reply_to('info@'.$this->settings['site']['domain'].'.com',$this->settings['publication']['title'].' Alerts');
		ee()->email->to($this->data['email'], $this->data['f_name'].' '.$this->data['l_name']);
		ee()->email->subject($this->settings['publication']['title'].' Alerts Confirmation Email');
		
		$message = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
		$message .= '<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
		$message .= "<title>{$this->settings['publication']['title']} Alerts Confirmation Email</title>";
		$message .= '</head><body>';
		$message .= "<p>Thank you for your subscription(s) to through {$this->settings['publication']['title']} Alerts.</p>";
		
		$message .= "<p>Please ";
		$message .= "<a href=\"http://www.{$this->settings['site']['domain']}.com/alerts/activate/{$this->data['hashcode']}\">click here</a> to activate all of your unactivated subscriptions.";
		$message .= "</p>";
		
		$message .= "<p>To manage your existing subscriptions, ";
		$message .= "<a href=\"http://www.{$this->settings['site']['domain']}.com/alerts/manage/{$this->data['hashcode']}\">click here</a>.";
		$message .= "</p>";
		
		$message .= '</body></html>';
		
		ee()->email->message($message);
		
		// send user a confirmation email for subscription
		if ( ! ee()->email->send()) { // if the email doesn't send...
			$this->handleError('Confirmation email to <em>'.$this->data['email'].'</em>, failed.');
		}
	}
	
	// --------------------------------------------------------------------

	/**
	 * Thank you confirmation
	 *
	 * @access     public
	 * @return     
	 */	
	function thank_you()
	{
		if ($_SESSION['alerts'] == '') {
			redirect('/alerts');
		}
		
		$this->data = $_SESSION['alerts'];
		$thanks[0]['f_name'] = $this->data['f_name'];
		$thanks[0]['email'] = $this->data['email'];
		session_destroy();
		
		return ee()->TMPL->parse_variables(ee()->TMPL->tagdata, $thanks);
	}
	
	// --------------------------------------------------------------------

	/**
	 * Activate subscriptions
	 *
	 * @access     public
	 * @return     
	 */	
	function activate()
	{
		// grab the 3rd segment of the incoming url - should be a 128 character hash
		$this->data['hashcode'] = ee()->uri->segment(3);
		
		// query database for USER hash
		$subscriber = $this->db->query(
		"	SELECT id
			FROM {$this->shared_db}.table_name_withheld
			WHERE hashcode = {$this->db->escape($this->data['hashcode'])}
		");
		
		// if the number of rows returned is 0...
		if ($subscriber->num_rows() == 1) {
			
			$this->data['id'] = $subscriber->row()->id;
			// change the value of column "activated" in exp_alert_subscription to TRUE
			$this->db->query(
			"	UPDATE {$this->shared_db}.table_name_withheld n, {$this->shared_db}.table_name_withheld r
				SET n.activated = 1
				WHERE r.id = n.email_id
				AND r.id = {$this->data['id']}
				AND r.hashcode = {$this->db->escape($this->data['hashcode'])}
			");
			
		// if the number of rows is not equal to 1 or 0...
		} else {
			$this->handleError(
				'Subscriber key, <em>' . $this->data['hashcode'] . '</em>, was found ' . $subscriber->num_rows() . ' times.',
				TRUE
			);
			exit();
		}
		
		$subscriber->free_result();
		$activate[0]['hash'] = $this->data['hashcode'];
		
		session_destroy();
		return ee()->TMPL->parse_variables(ee()->TMPL->tagdata, $activate);
	}
	
	// --------------------------------------------------------------------

	/**
	 * Unsubscribe
	 *
	 * @access     public
	 * @return     
	 */	
	function unsubscribe() 
	{
		// if the third segment of the uri does not exist, then redirect
		if (ee()->uri->segment(3) === FALSE) {
			redirect('alerts');
		}
			
		// check if length of hash is 128 characters
		if (strlen(ee()->uri->segment(3)) != 128) {
			$this->handleError('Subscription key, <em>'.$this->data['subscription_hash'].'</em> is not a valid key.', TRUE);
			exit();
		}
		
		// grab the 3rd segment of the incoming url - should be a 128-character hash
		$this->data['subscription_hash'] = ee()->uri->segment(3);
		
		// query SUBSCRIPTIONS for hash
		$subscription = $this->db->query(
		"	SELECT r.hashcode, t.group_title, n.type, n.frequency, s.site_domain 
			FROM exp_tagger_groups t, exp_sites s, {$this->shared_db}.table_name_withheld n, {$this->shared_db}.table_name_withheld r 
			WHERE n.topic_id = t.group_id 
			AND s.oscar_pub_id = n.pub_id 
			AND n.email_id = r.id 
			AND n.hashcode = {$this->db->escape($this->data['subscription_hash'])}
		");
		
		// if the number of rows returned is 1...
		if ($subscription->num_rows() == 1) {
		
			// store result hashcode
			$this->data['hashcode']			= $subscription->row()->hashcode;
			$this->data['topic']			= $subscription->row()->group_title;
			$this->data['type']				= $subscription->row()->type;
			$this->data['frequency']		= $subscription->row()->frequency;
			$this->data['site']['domain']	= $subscription->row()->site_domain;
			$subscription->free_result();
		
			// delete the subscription row that contains the hash
			$delete = $this->db->query(
			"	DELETE FROM {$this->shared_db}.table_name_withheld
				WHERE hashcode = {$this->db->escape($this->data['subscription_hash'])}
			");
			
			// if the delete is successful...
			if ($delete) {
				
				// if only one row is deleted
				if ($this->db->affected_rows() == 1){
					// create a success message for the next page
					$this->data['success'] = "<div class=\"box blue\">Your {$this->data['frequency']} {$this->data['type']} subscription to <strong>{$this->data['topic']}</strong> on {$this->data['site']['domain']}.com was successfully deleted.<br />You will no longer receive those alerts.</div>";
					// store all data in the session
					$_SESSION['alerts'] = $this->data;
					// redirect user back to "manage"
					redirect('/alerts/manage/'.$this->data['hashcode']);
					
				// if, somehow, more than one row was deleted (we should never get a report for this error, but just in case)...
				} else {
					$this->handleError(
						'Subscription key, <em>'.$this->data['subscription_hash'].'</em>, was deleted ' . $delete->num_rows() . ' times.',
						TRUE
					);
					exit();
				}
				
			// if the delete is NOT successful...
			} else {
				$this->handleError('Unable to delete subscription key <em>'.$this->data['subscription_hash'].'</em>.', TRUE);
				exit();
			}
			
		// Subscriptions not equal to 1	
		} else {
			
			$this->handleError(
				'Subscription key, <em>'.$this->data['subscription_hash'].'</em>, was found ' . $subscription->num_rows() . ' times.'
			);
			exit();
			
		}
	}
	
	// --------------------------------------------------------------------

	/**
	 * Manage subscriptions
	 *
	 * @access     public
	 * @return     
	 */	
	function manage()
	{
		// if the third segment of the uri exists...
		if ( ee()->uri->segment(3) === FALSE) {
			redirect('/alerts');
		}
		
		// if the hash is 128 characters long...
		if (strlen(ee()->uri->segment(3)) != 128) {
			$this->handleError('Subscriber key <em>'.$this->data['subscription_hash'].'</em> is not a valid key.', TRUE);
			exit();
		}
		
		// if there is a success message being stored in the session...
		if (isset($_SESSION['alerts']['success'])) {
			// store that value in a variable to be parsed to the page
			$manage[0]['success'] = $_SESSION['alerts']['success'];
		} else {
			// set the variable to be parsed to the page to and empty string
			$manage[0]['success'] = '';
		}
		
		// pull all subscriber info based on hash
		$subscriber = $this->db->query(
		"	SELECT *
			FROM {$this->shared_db}.table_name_withheld
			WHERE hashcode = {$this->db->escape(ee()->uri->segment(3))}
		");
		
		// if there is 1 result...
		if ($subscriber->num_rows() == 1){
			// store the user's name in a variable to be parsed to the page
			$manage[0]['f_name'] = $subscriber->row()->f_name;
			
			// query to find out how many subscriptions belong to the current user
			$subscription = $this->db->query(
			"	SELECT count(*) AS quantity
				FROM {$this->shared_db}.table_name_withheld
				WHERE email_id = {$subscriber->row()->id}
			");
			
			// store the number of subscriptions in a variable to be parsed to the page
			$manage[0]['quantity'] = $subscription->row()->quantity;
			// parse variables to the page
			return ee()->TMPL->parse_variables(ee()->TMPL->tagdata, $manage);

		} else {
			$this->handleError(
				'Subscriber key, <em>'.$this->data['subscription_hash'].'</em>, was found ' . $subscriber->num_rows() . ' times.',
				TRUE
			);
			exit();
		}
	}
	
	// --------------------------------------------------------------------

	/**
	 * Error handler
	 *
	 * @access     public
	 * @return     
	 */	
	function handleError($message, $redirect = FALSE)
	{
		$this->data['error'] = $message;
		
		ee()->email->from('info@' . $this->settings['site']['domain'] . '.com', $this->settings['publication']['title'] . ' Alerts');
		ee()->email->reply_to('info@' . $this->settings['site']['domain'] . '.com', $this->settings['publication']['title'] . ' Alerts');
		ee()->email->to($this->settings['author']['email'], $this->settings['author']['name']);
		ee()->email->subject($this->settings['publication']['title'] . ' Alerts User Error');
		ee()->email->message('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'.PHP_EOL.'<html>'.PHP_EOL.'<head>'.PHP_EOL.'<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />'.PHP_EOL.'<title>'.$this->settings['publication']['title'].' Alerts User Error</title>'.PHP_EOL.'</head>'.PHP_EOL.'<body>'.PHP_EOL.'<p>' . $this->data['error'] . '</p><pre>' . print_r($this->data, TRUE) . '</pre>'.PHP_EOL.'</body>'.PHP_EOL.'</html>');
		ee()->email->set_alt_message($this->data['error'] . PHP_EOL . print_r($this->data, TRUE));
		ee()->email->send();
		
		$this->db->query(
		"	INSERT INTO {$this->shared_db}.table_name_withheld (`when`, message, note, addressed)
			VALUES (
				{$this->db->escape(date('Y-m-d H:i:s'))},
				{$this->db->escape($this->data['error'])},
				{$this->db->escape(json_encode($this->data))},
				0
			)
		");
		
		if ($redirect) {
			$_SESSION['alerts'] = $this->data;
			redirect('/alerts/error');
			exit();
		}
	}
	
	// --------------------------------------------------------------------

	/**
	 * Error
	 *
	 * @access     public
	 * @return     
	 */	
	function error()
	{
		$this->data				= $_SESSION['alerts'];
		$error[0]['message']	= $this->data['error'];
		$name					= explode(' ', $this->settings['author']['name']);
		$error[0]['author']		= $name[0];
		
		session_destroy();
		return ee()->TMPL->parse_variables(ee()->TMPL->tagdata, $error);
	}
	
	// --------------------------------------------------------------------

	/**
	 * Random String Creation
	 *
	 * @access     public
	 * @return     
	 */	
	function randStr128()
	{
		$chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
		$randStr = '';
		for ($i = 0; $i < 128; $i++) {
			$randStr .= $chars[rand(0, strlen($chars) - 1)];
		}
		return $randStr;
	}
	
	// --------------------------------------------------------------------

	/**
	 * String case correction
	 *
	 * @access     public
	 * @return     
	 */	
	function properCase($string)
	{
		return ucfirst(strtolower($string));
	}
	
	// --------------------------------------------------------------------

	/**
	 * Plugin Usage
	 *
	 * @access     public
	 * @return     
	 */	
	function usage()
	{
        ob_start();
        ?>Does back-end form processing for Alerts sign-up forms.<?php
		$buffer = ob_get_contents();
		ob_end_clean(); 
		return $buffer;
	}
	
	// --------------------------------------------------------------------

	/**
	 * Destructor
	 *
	 * @access     public
	 * @return     
	 */	
	function __destruct()
	{
	}
}