Location: PHPKode > scripts > Health Tip Buddy > health-tip-buddy/functions/functions_general.php
<?php

	function magic_quote_alter($value,$quote='') {
		if ($quote = '"') {
			$value = str_replace('"','\"',$value);
		} elseif ($quote= "'") {
			$value = str_replace("'","\'",$value);
		}
		if (!get_magic_quotes_gpc()) {
			return addslashes($value);
		} else {
			return $value;
		}
	}
	
	function ternary($current,$item1,$item2) {
		if ($current == $item1) {
			return $item2;
		} else {
			return $item1;
		}
	}
	
	function getResultDiv($value,$type='error') {
		// Formats successful or error results whether they are in an array or a snippet.
		if ($type == 'success') {
			$class = 'success-div';
		} elseif ($type == 'test') {
			$class = 'test-div';
		} else {
			$class = 'error-div';
		}
		if (is_array($value)) {
			for ($i = 0; $value[$i] != ''; $i++) {
				$result_div .= '<li>' . $value[$i] . '</li>';
			}
			if ($result_div != '') {
				$result_div = '<div class="' . $class . '"><ul>' . $result_div . '</ul></div>';
			}
		} else {
			if ($value != '') {
				$result_div = '<div class="' . $class . '">' . $value . '</div>';
			}
		}
		return $result_div;
	}
	
	function showSelected($value,$to_match) {
		if ($value == $to_match) {
			return ' selected ';
		} else {
			return '';
		}
	}
		
	function listArray($array) {
		while ($var = each($array)) {
			printf ("Key <b>%s</b> has the value of: <b>%s</b><br>", $var['key'], $var['value']);
		}
	} 
		
	if(!function_exists('str_split')){
		function str_split($str,$length=1){
			$cnt = strlen($str);
			for ($i=0; $i<$cnt; $i+=$length) {
				$array[]= substr($str,$i,$length);
			}   
			return $array;
		 }
	}
	
	function checkValidChars($string,$valid_chars) {
		$string_array = str_split($string);
		$valid_chars_array = str_split($valid_chars);
		$i = 0;
		while ($string_array[$i] != '') {
			if (!in_array($string_array[$i],$valid_chars_array)) {
				return false;
			}
			$i++;
		}
		return true;
	}
	
	function checkLogin($username,$password) {
		$result = mysql_query('
			SELECT *
			FROM health_tip_buddy_users
			WHERE user_username = "' . $username . '" and
				user_password = "' . $password . '"');
		if (mysql_num_rows($result) > 0) {
			return true;
		} else {
			return false;
		}
	}
	
	function checkAdminLogin($username,$password) {
		$result = mysql_query('
			SELECT *
			FROM health_tip_buddy_administrators
			WHERE administrators_username = "' . $username . '" and
				administrators_pass = "' . $password . '"');
		if (mysql_num_rows($result) > 0) {
			return true;
		} else {
			return false;
		}
	}
	
		
	function startOptionDiv($unique_id,$title) {
		global $app_template_absolute_url;
		return '<div class="option-main-div">
			<a onclick="switchImage (\'' . $app_template_absolute_url . '/health-tip-buddy/images/white-arrow-over.gif\',\'' . $app_template_absolute_url . '/health-tip-buddy/images/white-arrow-down.gif\',\'' . $unique_id . '_image\')" href="javascript:toggleLayer(\'manage_' . $unique_id . '\');"><img src="' . $app_template_absolute_url . '/health-tip-buddy/images/white-arrow-over.gif" id="' . $unique_id . '_image" name="' . $unique_id . '_image" alt="Click to expand" title="Click to expand"><p>&nbsp; ' . $title . '</p></a> 
		</div>
		<div class="option-block-div" id="manage_' . $unique_id . '">
			<div class="highlighted-border">';
	}
	function endOptionDiv() {
		return '</div>
		</div>';
	}
	
	function updateCustomSetting($setting,$value,$is_required=0) {
		global $result_div;
		if ($is_required == 1) {
			if (strlen($value) < 1) {
				return false;
			}
		}	
		if ($update = mysql_query('
			UPDATE health_tip_buddy_custom_configuration
			SET custom_configuration_value = "' . magic_quote_alter($value) . '"
			WHERE custom_configuration_variable = "' . $setting . '"')) {
			return true;
		} else {
			$result_div .= getResultDiv(mysql_error());
		}
	}
	
	function getCustomVars() {
		$result = mysql_query('
			SELECT * 
			FROM health_tip_buddy_custom_configuration');
		while ($row = mysql_fetch_array($result)) {
				$custom[$row['custom_configuration_variable']] = $row['custom_configuration_value'];
		}
		return $custom;
	}
	
	function quickStrip($string) {
		$string = strip_tags($string);
		$string = str_replace('&nbsp;','',$string);
		$string = str_replace('*','',$string);
		$string = trim($string);
		return $string;
	}
	
	function generateSubscriptionEmail() {
		global $custom_vars;
		global $app_template_absolute_url;
		$limit = 5;
		$total = $limit;
		$max_sql = 'SELECT max(mailing_list_archive_id) 
			AS max_id
			FROM health_tip_buddy_mailing_list_archive';
		$max_row = mysql_fetch_array(mysql_query($max_sql));
		$i = 0;
		$max_id = $max_row['max_id'];
		$where_query = '';
		$or = '';
		while ($i < $total) {
			$rand_numbers[$i] .= mt_rand(1, $max_id);
			$where_query .= $or . ' mailing_list_archive_id = ' . mt_rand(1, $max_id);
			$or = ' OR ';
			$i++;
		}
		$sql = '
			SELECT *
			FROM health_tip_buddy_mailing_list_archive
			WHERE ' . $where_query . '
			LIMIT ' . $limit;
		$result = mysql_query($sql);
echo mysql_error();
		$row = mysql_fetch_array($result);
		$subscriptionEmail['content'] = $custom_vars['email_header'] . $row['mailing_list_archive_content_html'] . $custom_vars['email_footer'] . '</body></html>';
		$subscriptionEmail['subject'] = $row['mailing_list_archive_subject'];
		return $subscriptionEmail;
	}
	
	function addQueuedEmail($message,$subject,$to) {
		$headers = "Content-type: text/html; charset=iso-8859-1";
		$sql = '
			INSERT INTO health_tip_buddy_email_queue
			SET email_queue_to = "' . $to .'",
				email_queue_date = "' . date('Y-m-d H:i:s') . '",
				email_queue_subject = "' . magic_quote_alter(stripslashes($subject)) . '",
				email_queue_content = "' . magic_quote_alter(stripslashes($message)) .'",
				email_queue_headers = "' . magic_quote_alter(stripslashes($headers))  . '"';
		if ($insert_query = mysql_query($sql)) {
			return true;
		}
	}
	
	function sendQueuedMail() {
		global $custom_vars;
		$now = time();
		$custom_time = strtotime($custom_vars['email_queue_date_last_reset_custom']);
		$custom_duration = multiplyFromString($custom_vars['email_queue_custom_duration']);
		$custom_count = $custom_vars['email_queue_custom_count'];
		$day_count = $custom_vars['email_queue_24_hour_count'];
		$elapsed = $now-$custom_time;
		if ($elapsed > $custom_duration) {
			// Reset Custom Timer
			updateCustomSetting('email_queue_date_last_reset_custom',date('Y-m-d H:i:s'));
			updateCustomSetting('email_queue_custom_count',0);
			$custom_count = 0;
		}
		$day_time = strtotime($custom_vars['email_queue_date_last_reset_24_hour']);
		$day_duration = 60;
		$elapsed = $now-$day_time;
		if ($elapsed > $day_duration) {
			// Reset Day Timer
			updateCustomSetting('email_queue_date_last_reset_24_hour',date('Y-m-d H:i:s'));
			updateCustomSetting('email_queue_24_hour_count',0);
			$day_count = 0;
		}
		if (($day_count < $custom_vars['email_queue_max_per_day']) && ($custom_count < $custom_vars['email_queue_custom_duration_max'])) {
			$mail_result = mysql_query('
				SELECT *
				FROM health_tip_buddy_email_queue
				ORDER BY email_queue_id ASC
				LIMIT 10');
			
			while ($row = mysql_fetch_array($mail_result)) {
				if (($day_count < $custom_vars['email_queue_max_per_day']) && ($custom_count < $custom_vars['email_queue_custom_duration_max'])) {
					$to = $row['email_queue_to'];
					$subject = $custom_vars['subject_prefix'] . stripslashes($row['email_queue_subject']);
					$message = stripslashes($row['email_queue_content']);
					$extra_headers = stripslashes($row['email_queue_headers']);
					$is_html = $row['email_queue_is_html'];
					
					mail($to,$subject,$message,$extra_headers);
					$delete = mysql_query('
						DELETE 
						FROM health_tip_buddy_email_queue
						WHERE email_queue_id = "' . $row['email_queue_id'] . '"');
					$custom_count++;
					$day_count++;
				}
			}
			updateCustomSetting('email_queue_custom_count',$custom_count);
			updateCustomSetting('email_queue_24_hour_count',$day_count);

		}
	}
	
	function multiplyFromString($string) {
		if (strpos($string,'*')) {
			$array = explode('*',$string);
			$i = 0;
			$total = 1;
			while ($array[$i] != '') {
				$total = $total * $array[$i];
				$i++;
			}
		} else {
			$total = $string;
		}
		return $total;
	}
		
?>
Return current item: Health Tip Buddy