";
$message.=__("Please disable spam control on your AWPCP settings because you do not have Akismet properly configured (missing API key)","AWPCP");
$message.="
";
}
} else {
global $message;
$message="
";
$message.=__("Please disable spam control on your AWPCP settings because you do not have Akismet installed","AWPCP");
$message.="
";
}
}
//Function to detect spammy posts. Requires Akismet to be installed.
function awpcp_check_spam($name, $website, $email, $details) {
$content = array();
//Construct an Akismet-like query:
$content['comment_type'] = 'comment';
//$content['comment_author'] = $name; // don't send this, it reduces accuracy
$content['comment_author_email'] = $email;
//$content['comment_author_url'] = $website; // don't send this, it reduces accuracy
$content['comment_content'] = $details;
// innocent until proven guilty
$isSpam = FALSE;
if (function_exists('akismet_init')) {
$wpcom_api_key = get_option('wordpress_api_key');
if (!empty($wpcom_api_key)) {
global $akismet_api_host, $akismet_api_port;
// set remaining required values for akismet api
$content['user_ip'] = preg_replace( '/[^0-9., ]/', '', awpcp_getip() );
$content['user_agent'] = $_SERVER['HTTP_USER_AGENT'];
$content['referrer'] = get_option('home'); // use site home page instead of $_SERVER['HTTP_REFERER']; seems to work better
$content['blog'] = get_option('home');
//if (empty($content['referrer'])) {
// $content['referrer'] = get_permalink();
//}
$queryString = '';
foreach ($content as $key => $data) {
if (!empty($data)) {
$queryString .= $key . '=' . urlencode(stripslashes($data)) . '&';
}
}
$response = akismet_http_post($queryString, $akismet_api_host, '/1.1/comment-check', $akismet_api_port);
_log("Spam check, Akismet said: ");
foreach ($response as $key => $value) {
_log($key." - ".$value."");
}
if ($response[1] == 'true') {
//update_option('akismet_spam_count', get_option('akismet_spam_count') + 1);
$isSpam = TRUE;
}
} else {
global $message;
$message="
";
$message.=__("Please disable spam control on your AWPCP settings because you do not have Akismet properly configured (missing API key)","AWPCP");
$message.="
";
}
} else {
global $message;
$message="
";
$message.=__("Please disable spam control on your AWPCP settings because you do not have Akismet installed","AWPCP");
$message.="
";
}
// Akismet says it's not spam or Akismet disabled? Check using the blacklisted words configured in WP, if any:
if ( !$isSpam)
$isSpam = wp_blacklist_check($name, $email, $website, $details, preg_replace( '/[^0-9., ]/', '', awpcp_getip() ), $_SERVER['HTTP_USER_AGENT']);
_log("Ad spam check final answer: " . $isSpam);
return $isSpam;
}
function awpcp_blacklist_check($author, $email, $url, $comment, $user_ip, $user_agent) {
// hook similar the related WP hook, lets people develop their own ad scanning filters
do_action('awpcp_blacklist_check', $author, $email, $url, $comment, $user_ip, $user_agent);
$mod_keys = trim( get_option('blacklist_keys') );
if ( '' == $mod_keys )
return false; // If no blacklist words are set then there's nothing to do here
$words = explode("\n", $mod_keys );
foreach ( (array) $words as $word ) {
$word = trim($word);
// Skip empty lines
if ( empty($word) ) { continue; }
// Do some escaping magic so that '#' chars in the spam words don't break things:
$word = preg_quote($word, '#');
$pattern = "#$word#i";
if (
preg_match($pattern, $author)
|| preg_match($pattern, $email)
|| preg_match($pattern, $url)
|| preg_match($pattern, $comment)
|| preg_match($pattern, $user_ip)
|| preg_match($pattern, $user_agent)
)
return true;
}
return false;
}
/**
* Checks if $name is equal to $setting and then tries to find a POST
* parameter with that name. If it exists, and $value was specified,
* the function checks if that parameters' values is equal to $value.
*
* @param $setting string
* @param $value
*
* @return boolean
*
function awpcp_setting_was_set($name, $setting, $value=NULL, $collection=$_POST) {
if (strcmp($name, $setting) !== 0)
return false;
if (!isset($collection[$setting]))
return false;
if (!is_null($value) && $_POST[$setting] != $value)
return false;
return true;
}
*/
// START FUNCTION: retrieve individual options from settings table
function get_awpcp_setting($column, $option) {
global $wpdb;
$tbl_ad_settings = $wpdb->prefix . "awpcp_adsettings";
$myreturn=0;
$tableexists=checkfortable($tbl_ad_settings);
if($tableexists)
{
$query="SELECT ".$column." FROM ".$tbl_ad_settings." WHERE config_option='$option'";
$res = $wpdb->get_var($query);
$myreturn = strip_slashes_recursive($res);
}
return $myreturn;
}
function get_awpcp_option($option, $default='') {
global $awpcp;
debug(is_object($awpcp), is_object($awpcp->settings), $option, $default, $awpcp->settings->get_option($option));
if ($awpcp && $awpcp->settings) {
return $awpcp->settings->get_option($option);
}
return $default;
// return get_awpcp_setting('config_value', $option);
}
function get_awpcp_option_group_id($option) {
return get_awpcp_setting('config_group_id', $option);
}
function get_awpcp_option_type($option) {
return get_awpcp_setting('option_type', $option);
}
function get_awpcp_option_config_diz($option) {
return get_awpcp_setting('config_diz', $option);
}
// END FUNCTION
function awpcp_is_classifieds()
{
global $post,$table_prefix;
$awpcppageid=$post->ID;
$classifiedspagecontent="[AWPCPCLASSIFIEDSUI]";
$query="SELECT post_content FROM {$table_prefix}posts WHERE ID='$awpcppageid' AND post_type='page' AND post_status='publish'";
$res = awpcp_query($query, __LINE__);
while ($rsrow=mysql_fetch_row($res))
{
list($thepostcontentvalue)=$rsrow;
}
$istheclassifiedspage= (strcasecmp($thepostcontentvalue, $classifiedspagecontent) == 0);
return $istheclassifiedspage;
}
// START FUNCTION: Check if the user is an admin
function checkifisadmin() {
return awpcp_current_user_is_admin() ? 1 : 0;
}
// END FUNCTION
function awpcpistableempty($table){
global $wpdb;
$myreturn=true;
$query="SELECT count(*) FROM ".$table."";
$res = awpcp_query($query, __LINE__);
if (mysql_num_rows($res) && mysql_result($res,0,0)) {
$myreturn=false;
}
return $myreturn;
}
function awpcpisqueryempty($table, $where){
global $wpdb;
$myreturn=true;
$query="SELECT count(*) FROM ".$table." ".$where;
$res = awpcp_query($query, __LINE__);
if (mysql_num_rows($res) && mysql_result($res,0,0)) {
$myreturn=false;
}
return $myreturn;
}
// START FUNCTION: Check if the admin has setup any listing fee options
function adtermsset(){
global $wpdb;
$tbl_ad_fees = $wpdb->prefix . "awpcp_adfees";
$myreturn=!awpcpistableempty($tbl_ad_fees);
return $myreturn;
}
// END FUNCTION
/**
* Get the product ID for 2 Checkout.. or something like that.
*/
function get_2co_prodid($adterm_id) {
global $wpdb;
$tbl_ad_fees = $wpdb->prefix . "awpcp_adfees";
$twoco_pid='';
$query="SELECT twoco_pid from ".$tbl_ad_fees." WHERE adterm_id='$adterm_id'";
$res = awpcp_query($query, __LINE__);
while ($rsrow=mysql_fetch_row($res))
{
list($twoco_pid)=$rsrow;
}
return $twoco_pid;
}
// START FUNCTION: Check if the admin has setup some categories
function categoriesexist(){
global $wpdb;
$tbl_categories = $wpdb->prefix . "awpcp_categories";
$myreturn=!awpcpistableempty($tbl_categories);
return $myreturn;
}
// END FUNCTION
function adtermidinuse($adterm_id)
{
global $wpdb;
$tbl_ads = $wpdb->prefix . "awpcp_ads";
$myreturn=!awpcpisqueryempty($tbl_ads, " WHERE adterm_id='$adterm_id'");
return $myreturn;
}
// START FUNCTION: Count the total number of active ads in the system
function countlistings($is_active) {
global $wpdb;
$tbl_ads = $wpdb->prefix . "awpcp_ads";
$totallistings='';
$query="SELECT count(*) FROM ".$tbl_ads." WHERE disabled='". !$is_active ."'";
$res = awpcp_query($query, __LINE__);
while ($rsrow=mysql_fetch_row($res)) {
list($totallistings)=$rsrow;
}
return $totallistings;
}
// END FUNCTION
// START FUNCTION: Count the total number of categories
function countcategories(){
global $wpdb;
$tbl_categories = $wpdb->prefix . "awpcp_categories";
$totalcategories='';
$query="SELECT count(*) FROM ".$tbl_categories."";
$res = awpcp_query($query, __LINE__);
while ($rsrow=mysql_fetch_row($res)) {
list($totalcategories)=$rsrow;
}
return $totalcategories;
}
// END FUNCTION
// START FUNCTION: Count parent categories
function countcategoriesparents(){
global $wpdb;
$tbl_categories = $wpdb->prefix . "awpcp_categories";
$totalparentcategories='';
$query="SELECT count(*) FROM ".$tbl_categories." WHERE category_parent_id='0'";
$res = awpcp_query($query, __LINE__);
while ($rsrow=mysql_fetch_row($res)) {
list($totalparentcategories)=$rsrow;
}
return $totalparentcategories;
}
// END FUNCTION
// START FUNCTION: Count children categories
function countcategorieschildren(){
global $wpdb;
$tbl_categories = $wpdb->prefix . "awpcp_categories";
$totalchildrencategories='';
$query="SELECT count(*) FROM ".$tbl_categories." WHERE category_parent_id!='0'";
$res = awpcp_query($query, __LINE__);
while ($rsrow=mysql_fetch_row($res)) {
list($totalchildrencategories)=$rsrow;
}
return $totalchildrencategories;
}
// END FUNCTION
// START FUNCTION: get number of images allowed per ad term id
function get_numimgsallowed($adtermid){
global $wpdb;
$tbl_ad_fees = $wpdb->prefix . "awpcp_adfees";
$imagesallowed='';
$query="SELECT imagesallowed FROM ".$tbl_ad_fees." WHERE adterm_id='$adtermid'";
$res = awpcp_query($query, __LINE__);
while ($rsrow=mysql_fetch_row($res)) {
list($imagesallowed)=$rsrow;
}
return $imagesallowed;
}
// END FUNCTION
// START FUNCTION check if ad has entry in adterm ID field in the event admin switched back to free mode after previously running in paid mode
// this way user continues to be allowed number of images allowed per the ad term ID
function ad_term_id_set($adid)
{
return (get_adfield_by_pk('adterm_id', $adid) != 0);
}
// END FUNCTION check if user has paid for ad in event admin switched back to free mode after previously running in paid mode
// this way if user paid for ad user continues to be allowed number of images paid for
// START FUNCTION: Check to see how many images an ad is currently using
function get_total_imagesuploaded($ad_id) {
global $wpdb;
$tbl_ad_photos = $wpdb->prefix . "awpcp_adphotos";
$totalimagesuploaded='';
$query="SELECT count(*) FROM ".$tbl_ad_photos." WHERE ad_id='$ad_id'";
$res = awpcp_query($query, __LINE__);
while ($rsrow=mysql_fetch_row($res)) {
list($totalimagesuploaded)=$rsrow;
}
return $totalimagesuploaded;
}
// END FUNCTION
// START FUNCTION: Get the total number of days an ad term last based on term ID value
function get_num_days_in_term($adtermid) {
$numdaysinterm='';
global $wpdb;
$tbl_ad_fees = $wpdb->prefix . "awpcp_adfees";
$query="SELECT rec_period from ".$tbl_ad_fees." WHERE adterm_id='$adtermid'";
$res = awpcp_query($query, __LINE__);
while ($rsrow=mysql_fetch_row($res)) {
list($numdaysinterm)=$rsrow;
}
if ('' == $numdaysinterm || 0 == $numdaysinterm) {
$numdaysinterm = 3650; // 100 years, equivalent of never expires
}
return $numdaysinterm;
}
// END FUNCTION
// START FUNCTION: Get the id for the ad term based on having the ad ID
function get_adterm_id($adid) {
return get_adfield_by_pk('adterm_id', $adid);
}
// END FUNCTION
// START FUNCTION: Get the ad term name for the ad term based on having the ad term ID
function get_adterm_name($adterm_id) {
global $wpdb;
$tbl_ad_fees = $wpdb->prefix . "awpcp_adfees";
$adterm_name='';
$query="SELECT adterm_name from ".$tbl_ad_fees." WHERE adterm_id='$adterm_id'";
$res = awpcp_query($query, __LINE__);
while ($rsrow=mysql_fetch_row($res)) {
list($adterm_name)=$rsrow;
}
return $adterm_name;
}
// END FUNCTION
// START FUNCTION: Get the ad recperiod based on having the ad term ID
function get_fee_recperiod($adterm_id) {
global $wpdb;
$tbl_ad_fees = $wpdb->prefix . "awpcp_adfees";
$recperiod='';
$query="SELECT rec_period from ".$tbl_ad_fees." WHERE adterm_id='$adterm_id'";
$res = awpcp_query($query, __LINE__);
while ($rsrow=mysql_fetch_row($res)) {
list($recperiod)=$rsrow;
}
return $recperiod;
}
// END FUNCTION
// START FUNCTION: Get the ad posters name based on having the ad ID
function get_adpostername($adid) {
return get_adfield_by_pk('ad_contact_name', $adid);
}
// END FUNCTION
// START FUNCTION: Get the ad posters access key based on given ID
function get_adkey($adid) {
return get_adfield_by_pk('ad_key', $adid);
}
// END FUNCTION
// START FUNCTION: Get the ad title based on having the ad email
function get_adcontactbyem($email) {
$adcontact='';
global $wpdb;
$tbl_ads = $wpdb->prefix . "awpcp_ads";
$query="SELECT ad_contact_name from ".$tbl_ads." WHERE ad_contact_email='$email'";
$res = awpcp_query($query, __LINE__);
while ($rsrow=mysql_fetch_row($res)) {
list($adcontact)=$rsrow;
}
return $adcontact;
}
// END FUNCTION
// START FUNCTION: Get the ad posters name based on having the ad email
function get_adtitlebyem($email) {
$adtitle='';
global $wpdb;
$tbl_ads = $wpdb->prefix . "awpcp_ads";
$query="SELECT ad_title from ".$tbl_ads." WHERE ad_contact_email='$email'";
$res = awpcp_query($query, __LINE__);
while ($rsrow=mysql_fetch_row($res)) {
list($adtitle)=$rsrow;
}
return strip_slashes_recursive($adtitle);
}
// END FUNCTION
// START FUNCTION: Get the ad posters email based on having the ad ID
function get_adposteremail($adid) {
return get_adfield_by_pk('ad_contact_email', $adid);
}
function get_adstartdate($adid) {
return get_adfield_by_pk('ad_startdate', $adid);
}
// END FUNCTION
// START FUNCTION: Get the number of times an ad has been viewed
function get_numtimesadviewd($adid)
{
return get_adfield_by_pk('ad_views', $adid);
}
// END FUNCTION: Get the number of times an ad has been viewed
// START FUNCTION: Get the ad title based on having the ad ID
function get_adtitle($adid) {
return strip_slashes_recursive(get_adfield_by_pk('ad_title', $adid));
}
// END FUNCTION
// START FUNCTION: Get the ad term fee amount for the ad term based on having the ad term ID
function get_adfee_amount($adterm_id) {
global $wpdb;
$tbl_ad_fees = $wpdb->prefix . "awpcp_adfees";
$adterm_amount='';
$query="SELECT amount from ".$tbl_ad_fees." WHERE adterm_id='$adterm_id'";
$res = awpcp_query($query, __LINE__);
while ($rsrow=mysql_fetch_row($res)) {
list($adterm_amount)=$rsrow;
}
return $adterm_amount;
}
// END FUNCTION: get ad term fee amount based on ad term ID
// START FUNCTION: Create list of top level categories for admin category management
function get_categorynameid($cat_id = 0,$cat_parent_id= 0,$exclude)
{
global $wpdb;
$optionitem='';
$tbl_categories = $wpdb->prefix . "awpcp_categories";
if(isset($exclude) && !empty($exclude))
{
$excludequery="AND category_id !='$exclude'";
}else{$excludequery='';}
$catnid=$wpdb->get_results("select category_id as cat_ID, category_parent_id as cat_parent_ID, category_name as cat_name from ".$tbl_categories." WHERE category_parent_id='0' AND category_name <> '' $excludequery");
foreach($catnid as $categories)
{
if($categories->cat_ID == $cat_parent_id)
{
$optionitem .= "";
}
else
{
$optionitem .= "";
}
}
return $optionitem;
}
// END FUNCTION: create list of top level categories for admin category management
// START FUNCTION: Create the list with both parent and child categories selection for ad post form
function get_categorynameidall($cat_id = 0)
{
global $wpdb;
$tbl_categories = $wpdb->prefix . "awpcp_categories";
$optionitem='';
// Start with the main categories
$query="SELECT category_id,category_name FROM ".$tbl_categories." WHERE category_parent_id='0' and category_name <> '' ORDER BY category_order, category_name ASC";
$res = awpcp_query($query, __LINE__);
while ($rsrow=mysql_fetch_row($res)) {
$cat_ID=$rsrow[0];
$cat_name=$rsrow[1];
$opstyle="class=\"dropdownparentcategory\"";
if($cat_ID == $cat_id)
{
$maincatoptionitem = "";
}
else {
$maincatoptionitem = "";
}
$optionitem.="$maincatoptionitem";
// While still looping through main categories get any sub categories of the main category
$maincatid=$cat_ID;
$query="SELECT category_id,category_name FROM ".$tbl_categories." WHERE category_parent_id='$maincatid' ORDER BY category_order, category_name ASC";
$res2 = awpcp_query($query, __LINE__);
while ($rsrow2=mysql_fetch_row($res2)) {
$subcat_ID=$rsrow2[0];
$subcat_name=$rsrow2[1];
if($subcat_ID == $cat_id)
{
$subcatoptionitem = "";
}
else {
$subcatoptionitem = "";
}
$optionitem.="$subcatoptionitem";
}
}
return $optionitem;
}
function get_categorycheckboxes( $cats = array(), $adterm_id ) {
global $wpdb;
$tbl_categories = $wpdb->prefix . "awpcp_categories";
$tbl_fees = $wpdb->prefix . "awpcp_adfees";
$optionitem='';
$catswithfees = array();
if (!empty($adterm_id)) {
$sql = 'select categories from '.$tbl_fees.' where adterm_id = '.$adterm_id;
$catswithfees = $wpdb->get_var($sql);
if ('' != $catswithfees) {
$catswithfees = explode(',', $catswithfees);
}
}
// Start with the main categories
$query="SELECT category_id,category_name FROM ".$tbl_categories." WHERE category_parent_id='0' and category_name <> '' ORDER BY category_order, category_name ASC";
$res = awpcp_query($query, __LINE__);
while ($rsrow=mysql_fetch_row($res)) {
$cat_ID=$rsrow[0];
$cat_name=$rsrow[1];
$opstyle="class=\"checkboxparentcategory\"";
if( in_array($cat_ID, $catswithfees) )
{
$maincatoptionitem = " $cat_name ";
}
else {
$maincatoptionitem = " $cat_name ";
}
$optionitem.="$maincatoptionitem";
// While still looping through main categories get any sub categories of the main category
$maincatid=$cat_ID;
$query="SELECT category_id,category_name FROM ".$tbl_categories." WHERE category_parent_id='$maincatid' ORDER BY category_order, category_name ASC";
$res2 = awpcp_query($query, __LINE__);
while ($rsrow2=mysql_fetch_row($res2)) {
$subcat_ID=$rsrow2[0];
$subcat_name=$rsrow2[1];
if( in_array($subcat_ID, $catswithfees) )
{
$subcatoptionitem = " - $subcat_name ";
}
else {
$subcatoptionitem = " - $subcat_name ";
}
$optionitem.="$subcatoptionitem";
}
}
return $optionitem;
}
// END FUNCTION: create drop down list of categories for ad post form
// START FUNCTION: Retrieve the category name
function get_adcatname($cat_ID){
global $wpdb;
$cname='';
$tbl_categories = $wpdb->prefix . "awpcp_categories";
if(isset($cat_ID) && (!empty($cat_ID))){
$query="SELECT category_name from ".$tbl_categories." WHERE category_id='$cat_ID'";
$cname = $wpdb->get_results($query, ARRAY_A);
foreach($cname as $cn) {
$cname = $cn['category_name'];
}
}
return strip_slashes_recursive($cname);
}
function get_adcatorder($cat_ID){
global $wpdb;
$corder='';
$tbl_categories = $wpdb->prefix . "awpcp_categories";
if(isset($cat_ID) && (!empty($cat_ID))){
$query="SELECT category_order from ".$tbl_categories." WHERE category_id='$cat_ID'";
$corder = $wpdb->get_var($query);
}
return $corder;
}
//Function to retrieve ad location data:
function get_adfield_by_pk($field, $adid) {
global $wpdb;
$tbl_ads = $wpdb->prefix . "awpcp_ads";
$thevalue='';
if(isset($adid) && (!empty($adid))){
$query="SELECT ".$field." from ".$tbl_ads." WHERE ad_id='$adid'";
$thevalue = $wpdb->get_var($query);
}
return $thevalue;
}
function get_adcountryvalue($adid){
return get_adfield_by_pk('ad_country', $adid);
}
function get_adstatevalue($adid){
return get_adfield_by_pk('ad_city', $adid);
}
function get_adcityvalue($adid){
return get_adfield_by_pk('ad_state', $adid);
}
function get_adcountyvillagevalue($adid){
return get_adfield_by_pk('ad_county_village', $adid);
}
function get_adcategory($adid){
return get_adfield_by_pk('ad_category_id', $adid);
}
function get_adparentcatname($cat_ID){
global $wpdb;
$tbl_categories = $wpdb->prefix . "awpcp_categories";
$cname='';
if($cat_ID == '0')
{
$cname="Top Level Category";
}
else
{
if(isset($cat_ID) && (!empty($cat_ID)))
{
$query="SELECT category_name from ".$tbl_categories." WHERE category_id='$cat_ID'";
$res = awpcp_query($query, __LINE__);
while ($rsrow=mysql_fetch_row($res))
{
list($cname)=$rsrow;
}
}
}
return $cname;
}
// END FUNCTION: get the name of the category parent
// START FUNCTION: Retrieve the parent category ID
function get_cat_parent_ID($cat_ID){
global $wpdb;
$cpID='';
$tbl_categories = $wpdb->prefix . "awpcp_categories";
if(isset($cat_ID) && (!empty($cat_ID))){
$query="SELECT category_parent_id from ".$tbl_categories." WHERE category_id='$cat_ID'";
$res = awpcp_query($query, __LINE__);
while ($rsrow=mysql_fetch_row($res)) {
list($cpID)=$rsrow;
}
}
return $cpID;
}
// END FUNCTION: get the ID or the category parent
// START FUNCTION: Check if the transaction ID coming back from paypal or 2checkout is a duplicate
function isdupetransid($transid){
global $wpdb;
$tbl_ads = $wpdb->prefix . "awpcp_ads";
$myreturn=!awpcpisqueryempty($tbl_ads, " WHERE ad_transaction_id='$transid'");
return $myreturn;
}
// END FUNCTION: check if a transaction ID from paypal or 2checkout is already in the system
// START FUNCTION: Check if there are any ads in the system
function ads_exist() {
global $wpdb;
$tbl_ads = $wpdb->prefix . "awpcp_ads";
$myreturn=!awpcpistableempty($tbl_ads);
return $myreturn;
}
// END FUNCTION: check if any ads exist in the system
// START FUNCTION: Check if there are any ads in a specified category
function ads_exist_cat($catid) {
global $wpdb;
$tbl_ads = $wpdb->prefix . "awpcp_ads";
$myreturn=!awpcpisqueryempty($tbl_ads, " WHERE ad_category_id='$catid' OR ad_category_parent_id='$catid'");
return $myreturn;
}
// END FUNCTION: check if a category has ads
// START FUNCTION: Check if the category has children
function category_has_children($catid) {
global $wpdb;
$tbl_categories = $wpdb->prefix . "awpcp_categories";
$myreturn=!awpcpisqueryempty($tbl_categories, " WHERE category_parent_id='$catid'");
return $myreturn;
}
// END FUNCTION: check if a category has children
// START FUNCTION: Check if the category is a child
function category_is_child($catid) {
global $wpdb;
$tbl_categories = $wpdb->prefix . "awpcp_categories";
$myreturn=false;
$query="SELECT category_parent_id FROM ".$tbl_categories." WHERE category_id='$catid'";
$res = awpcp_query($query, __LINE__);
while ($rsrow=mysql_fetch_row($res)) {
list($cparentid)=$rsrow;
if( $cparentid != '0' )
{
$myreturn=true;
}
}
return $myreturn;
}
// END FUNCTION: check if a category is a child
// START FUNCTION: Check how many ads a category contains
function total_ads_in_cat($catid) {
global $wpdb,$hasregionsmodule;
$tbl_ads = $wpdb->prefix . "awpcp_ads";
$totaladsincat='';
$filter='';
// the name of the disablependingads setting gives the wrong meaning,
// it actually means "Enable Paid Ads that are Pendings payment", so when
// the setting has a value of 1, pending Ads should NOT be excluded.
// I'll change the next condition considering the above
if((get_awpcp_option('disablependingads') == 0) && (get_awpcp_option('freepay') == 1)){
$filter=" AND (payment_status != 'Pending' AND payment_status != 'Unpaid')";
}/* else {
// never allow Unpaid Ads
$filter=" AND payment_status != 'Unpaid' ";
}*/
if($hasregionsmodule == 1) {
if( isset($_SESSION['theactiveregionid']) ) {
$theactiveregionid=$_SESSION['theactiveregionid'];
$theactiveregionname=addslashes(get_theawpcpregionname($theactiveregionid));
$filter.="AND (ad_city='$theactiveregionname' OR ad_state='$theactiveregionname' OR ad_country='$theactiveregionname' OR ad_county_village='$theactiveregionname')";
}
}
$query = "SELECT count(*) FROM " . AWPCP_TABLE_ADS . " ";
$query.= "WHERE (ad_category_id='$catid' OR ad_category_parent_id='$catid') ";
// $query.= "AND disabled = '0' AND (flagged IS NULL OR flagged =0) $filter";
$query.= "AND disabled = '0' $filter";
$res = awpcp_query($query, __LINE__);
while ($rsrow=mysql_fetch_row($res)) {
list($totaladsincat)=$rsrow;
}
return $totaladsincat;
}
// END FUNCTION: check how many ads are in a category
// START FUNCTION: Check if there are any ads in the system
function images_exist() {
global $wpdb;
$tbl_ad_photos = $wpdb->prefix . "awpcp_ads";
$myreturn=!awpcpistableempty($tbl_ad_photos);
return $myreturn;
}
// END FUNCTION: Check if images exist in system
// START FUNCTION: Eemove unwanted characters from string and setup for use with search engine friendly urls
function cleanstring($text)
{
$code_entities_match = array(' ','--','"','!','@','#','$','%','^','&','*','(',')','+','{','}','|',':','"','<','>','?','[',']','\\',';',"'",',','.','/','*','+','~','`','=');
$code_entities_replace = array('_','_','','','','','','','','','','','','','','','','','','','','','','','');
$text = str_replace($code_entities_match, $code_entities_replace, $text);
if (version_compare(PHP_VERSION, '5.2.0', '>=')) {
$text="".(filter_var($text, FILTER_SANITIZE_URL))."";
}
return $text;
}
// END FUNCTION: remove unwanted characters from string to be used in URL for search engine friendliness
// START FUNCTION: replace underscores with dashes for search engine friendly urls
function add_dashes($text) {
$text=str_replace("_","-",$text);
$text=str_replace(" ","-",$text);
return $text;
}
//Function to replace addslashes_mq, which is causing major grief. Stripping of undesireable characters now done
// through above strip_slashes_recursive_gpc.
function clean_field($foo) {
//debug();
return add_slashes_recursive($foo);
}
// END FUNCTION: replace underscores with dashes for search engine friendly urls
// START FUNCTION: get the page ID when the page name is known
// Get the id of a page by its name
function awpcp_get_page_id($name) {
global $wpdb;
if (!empty($name)) {
$sql = "SELECT ID FROM $wpdb->posts WHERE post_name = '$name'";
$id = $wpdb->get_var($sql);
return $id;
}
return 0;
}
/**
* Returns the ID of WP Page associated to a page-name setting.
*
* @param $refname the name of the setting that holds the name of the page
*/
function awpcp_get_page_id_by_ref($refname) {
global $wpdb;
$query = 'SELECT page, id FROM ' . AWPCP_TABLE_PAGES . ' WHERE page = %s';
$page = $wpdb->get_results($wpdb->prepare($query, $refname));
if (!empty($page)) {
return array_shift($page)->id;
} else {
return false;
}
}
// END FUNCTION: Get the ID from wordpress posts table where the post_name is known
// START FUNCTION: Get the page guid
function awpcp_get_guid($awpcpshowadspageid){
global $wpdb;
$awpcppageguid = $wpdb->get_var("SELECT guid FROM $wpdb->posts WHERE ID ='$awpcpshowadspageid'");
return $awpcppageguid;
}
// END FUNCTION: Get the page guid
// START FUNCTION: Get the order by setting for ad listings
function get_group_orderby()
{
$getgrouporderby=get_awpcp_option('groupbrowseadsby');
if(!isset($getgrouporderby) || empty($getgrouporderby))
{
$grouporderby='';
}
else
{
if(isset($getgrouporderby) && !empty($getgrouporderby))
{
if($getgrouporderby == 1)
{
$grouporderby="ORDER BY ad_id DESC";
}
elseif($getgrouporderby == 2)
{
$grouporderby="ORDER BY ad_title ASC, ad_id DESC";
}
elseif($getgrouporderby == 3)
{
$grouporderby="ORDER BY ad_is_paid DESC, ad_startdate DESC, ad_title ASC, ad_id DESC";
}
elseif($getgrouporderby == 4)
{
$grouporderby="ORDER BY ad_is_paid DESC, ad_title ASC, ad_id DESC";
}
elseif($getgrouporderby == 5)
{
$grouporderby="ORDER BY ad_views DESC, ad_title ASC, ad_id DESC";
}
elseif($getgrouporderby == 6)
{
$grouporderby="ORDER BY ad_views DESC, ad_id DESC";
}
}
}
return $grouporderby;
}
// END FUNCTION: Get the orderby setting for ad listings
// START FUNCTION:
/**
* Setup the structure of the URLs based on if permalinks are on and SEO urls
* are turned on.
*
* Actually it doesn't take into account if SEO urls are on. It also takes an
* argument that is expected to have the same value ALWAYS.
*
* Is easier to get the URL for a given page using:
* get_permalink(awpcp_get_page_id(sanitize-title($human-readable-pagename)));
* or
* get_permalink(awpcp_get_page_id_by_ref())
*/
function setup_url_structure($awpcpthepagename) {
$quers = '';
$theblogurl = get_bloginfo('url');
$permastruc = get_option('permalink_structure');
if(strstr($permastruc,'index.php')) {
$theblogurl.="/index.php";
}
if(isset($permastruc) && !empty($permastruc)) {
$quers="$theblogurl/$awpcpthepagename";
} else {
$quers="$theblogurl";
}
return $quers;
}
// END FUNCTION: setup structure of URLs based on if permalinks are on and SEO urls are turned on
function url_showad($ad_id) {
$url_showad='';
// $awpcppage=get_currentpagename();
// $awpcppagename = sanitize_title($awpcppage, $post_ID='');
// $quers=setup_url_structure($awpcppagename);
$permastruc=get_option('permalink_structure');
// $showadspagename=sanitize_title(get_awpcp_option('show-ads-page-name'));
$awpcp_showad_pageid = awpcp_get_page_id_by_ref('show-ads-page-name');
$awpcpadcity=get_adcityvalue($ad_id);
$awpcpadstate=get_adstatevalue($ad_id);
$awpcpadcountry=get_adcountryvalue($ad_id);
$awpcpadcountyvillage=get_adcountyvillagevalue($ad_id);
$awpcpadtitle=get_adtitle($ad_id);
$modtitle=cleanstring($awpcpadtitle);
$modtitle=add_dashes($modtitle);
$seoFriendlyUrls = get_awpcp_option('seofriendlyurls');
$params = array('id' => $ad_id);
$base_url = get_permalink($awpcp_showad_pageid);
if( $seoFriendlyUrls ) {
if(isset($permastruc) && !empty($permastruc)) {
// $url_showad="$quers/$showadspagename/$ad_id/$modtitle";
$url_showad = sprintf('%s/%s/%s', trim($base_url, '/'), $ad_id, $modtitle);
} else {
// $url_showad="$quers/?page_id=$awpcp_showad_pageid&id=$ad_id";
$url_showad = add_query_arg($params, $base_url);
}
} else {
$url_showad = add_query_arg($params, $base_url);
}
if( $seoFriendlyUrls ) {
if(isset($permastruc) && !empty($permastruc)) {
//_log("City as found: '".$awpcpadcity."'");
if( get_awpcp_option('showcityinpagetitle') && !empty($awpcpadcity) ) {
$url_showad.="/";
$url_showad.=cleanstring(add_dashes($awpcpadcity));
}
if( get_awpcp_option('showstateinpagetitle') && !empty($awpcpadstate) ) {
$url_showad.="/";
$url_showad.=cleanstring(add_dashes($awpcpadstate));
}
if( get_awpcp_option('showcountryinpagetitle') && !empty($awpcpadcountry) ) {
$url_showad.="/";
$url_showad.=cleanstring(add_dashes($awpcpadcountry));
}
if( get_awpcp_option('showcountyvillageinpagetitle') && !empty($awpcpadcountyvillage) ) {
$url_showad.="/";
$url_showad.=cleanstring(add_dashes($awpcpadcountyvillage));
}
if( get_awpcp_option('showcategoryinpagetitle') ) {
$awpcp_ad_category_id=get_adcategory($ad_id);
$awpcp_ad_category_name=cleanstring(add_dashes(get_adcatname($awpcp_ad_category_id)));
$url_showad.="/";
$url_showad.=$awpcp_ad_category_name;
}
//Always append a slash (RSS module issue)
$url_showad.="/";
}
}
//_log("Returning URL: ".$url_showad);
return $url_showad;
}
function url_browsecategory($cat_id) {
// $awpcppage=get_currentpagename();
// $awpcppagename = sanitize_title($awpcppage, $post_ID='');
// $quers=setup_url_structure($awpcppagename);
$permastruc=get_option('permalink_structure');
// $browsecatspagename=sanitize_title(get_awpcp_option('browse-categories-page-name'));
$awpcp_browsecats_pageid=awpcp_get_page_id_by_ref('browse-categories-page-name');
$awpcpcatname=get_adcatname($cat_id);
$modcatname=cleanstring($awpcpcatname);
$modcatname=add_dashes($modcatname);
$base_url = get_permalink($awpcp_browsecats_pageid);
if (get_awpcp_option('seofriendlyurls')) {
if (isset($permastruc) && !empty($permastruc)) {
// $url_browsecats="$quers/$browsecatspagename/$cat_id/$modcatname";
$url_browsecats = sprintf('%s/%s/%s', trim($base_url, '/'), $cat_id, $modcatname);
} else {
$params = array('a' => 'browsecat', 'category_id' => $cat_id);
// $url_browsecats="$quers/?page_id=$awpcp_browsecats_pageid&a=browsecat&category_id=$cat_id";
$url_browsecats = add_query_arg($params, $base_url);
}
} else {
if (isset($permastruc) && !empty($permastruc)) {
$params = array('category_id' => "$cat_id/$modcatname");
// $url_browsecats="$quers/$browsecatspagename?category_id=$cat_id/$modcatname";
} else {
$params = array('a' => 'browsecat', 'category_id' => $cat_id);
// $url_browsecats="$quers/?page_id=$awpcp_browsecats_pageid&a=browsecat&category_id=$cat_id";
}
$url_browsecats = add_query_arg($params, $base_url);
}
//_log("Returning cat URL: ".$url_browsecats);
return $url_browsecats;
}
function url_placead() {
// $awpcppage=get_currentpagename();
// $awpcppagename = sanitize_title($awpcppage);
// $quers=setup_url_structure($awpcppagename);
// $permastruc=get_option('permalink_structure');
// $placeadpagename=sanitize_title(get_awpcp_option('place-ad-page-name'));
$awpcp_placead_pageid=awpcp_get_page_id_by_ref('place-ad-page-name');
$url_placead = get_permalink($awpcp_placead_pageid);
// if( get_awpcp_option('seofriendlyurls') ) {
// if(isset($permastruc) && !empty($permastruc)) {
// $url_placead="$quers/$placeadpagename/";
// } else {
// $url_placead="$quers/?page_id=$awpcp_placead_pageid";
// }
// } else {
// if(isset($permastruc) && !empty($permastruc)) {
// $url_placead="$quers/$placeadpagename/";
// } else {
// $url_placead="$quers/?page_id=$awpcp_placead_pageid";
// }
// }
return $url_placead;
}
function url_classifiedspage() {
// $awpcppage=get_currentpagename();
// $awpcppagename = sanitize_title($awpcppage, $post_ID='');
// $quers=setup_url_structure($awpcppagename);
// $permastruc=get_option('permalink_structure');
$awpcp_pageid=awpcp_get_page_id_by_ref('main-page-name');
$url_classifiedspage = get_permalink($awpcp_pageid);
// if( get_awpcp_option('seofriendlyurls') ) {
// if(isset($permastruc) && !empty($permastruc)) {
// $url_classifiedspage="$quers/";
// } else {
// $url_classifiedspage="$quers/?page_id=$awpcp_pageid";
// }
// } else {
// if(isset($permastruc) && !empty($permastruc)) {
// $url_classifiedspage="$quers/";
// } else {
// $url_classifiedspage="$quers/?page_id=$awpcp_pageid";
// }
// }
return $url_classifiedspage;
}
function url_searchads() {
// $awpcppage=get_currentpagename();
// $awpcppagename = sanitize_title($awpcppage);
// $quers=setup_url_structure($awpcppagename);
// $permastruc=get_option('permalink_structure');
// $searchadspagename=sanitize_title(get_awpcp_option('search-ads-page-name'));
$awpcp_searchads_pageid=awpcp_get_page_id_by_ref('search-ads-page-name');
$url_searchads = get_permalink($awpcp_searchads_pageid);
// if( get_awpcp_option('seofriendlyurls') ) {
// if(isset($permastruc) && !empty($permastruc)) {
// $url_searchads="$quers/$searchadspagename/";
// } else {
// $url_searchads="$quers/?page_id=$awpcp_searchads_pageid";
// }
// } else {
// if(isset($permastruc) && !empty($permastruc)) {
// $url_searchads="$quers/$searchadspagename/";
// } else {
// $url_searchads="$quers/?page_id=$awpcp_searchads_pageid";
// }
// }
return $url_searchads;
}
function url_editad() {
// $awpcppage=get_currentpagename();
// $awpcppagename = sanitize_title($awpcppage);
// $quers=setup_url_structure($awpcppagename);
// $permastruc=get_option('permalink_structure');
$editadpagename=sanitize_title(get_awpcp_option('edit-ad-page-name'));
$awpcp_editad_pageid=awpcp_get_page_id_by_ref('edit-ad-page-name');
$url_editad = get_permalink($awpcp_editad_pageid);
// if( get_awpcp_option('seofriendlyurls') ) {
// if(isset($permastruc) && !empty($permastruc)) {
// $url_editad="$quers/$editadpagename/";
// } else {
// $url_editad="$quers/?page_id=$awpcp_editad_pageid";
// }
// } else {
// if(isset($permastruc) && !empty($permastruc)) {
// $url_editad="$quers/$editpagename/";
// } else {
// $url_editad="$quers/?page_id=$awpcp_editad_pageid";
// }
// }
return $url_editad;
}
// START FUNCTION: get the parent_id of the post
//XXX: never used?
function get_page_parent_id($awpcpwppostpageid){
global $wpdb;
$awpcppageparentid = $wpdb->get_var("SELECT post_parent FROM $wpdb->posts WHERE ID = '$awpcpwppostpageid'");
return $awpcppageparentid;
}
// END FUNCTION: get the parent id of a wordpress post where the post ID is known
// START FUNCTION: get the name of a wordpress entry from table posts where the parent id is present
//XXX: never used?
function get_awpcp_parent_page_name($awpcppageparentid) {
global $wpdb;
$awpcpparentpagename = $wpdb->get_var("SELECT post_name FROM $wpdb->posts WHERE ID = '$awpcppageparentid'");
return $awpcpparentpagename;
}
// END FUNCTION: get the name of a wordpress wp_post entry where the ID of the post parent is present
// START FUNCTION: check if a specific database table exists
function checkfortable($table) {
global $wpdb;
$tableexists = $wpdb->get_var("show tables like '$table'") == $table;
return $tableexists;
}
// END FUNCTION: check if a specific database table exists
// START FUNCTION: add field config_group_id to table adsettings v 1.0.5.6 update specific
function add_config_group_id($cvalue,$coption)
{
global $wpdb;
$tbl_ad_settings = $wpdb->prefix . "awpcp_adsettings";
//Escape quotes:
$cvalue = add_slashes_recursive($cvalue);
$query="UPDATE ".$tbl_ad_settings." SET config_group_id='$cvalue' WHERE config_option='$coption'";
$res = awpcp_query($query, __LINE__);
}
// END FUNCTION: add field config_group_id to table adsettings v 1.0.5.6 update specific
// START FUNCTION: check if a specific ad id already exists
function adidexists($adid) {
global $wpdb;
$tbl_ads = $wpdb->prefix . "awpcp_ads";
$adidexists=false;
$query="SELECT count(*) FROM ".$tbl_ads." WHERE ad_id='$adid'";
if (($res=mysql_query($query))) {
$adidexists=true;
}
return $adidexists;
}
function categoryidexists($adcategoryid) {
global $wpdb;
$tbl_categories = $wpdb->prefix . "awpcp_adcategories";
$categoryidexists=false;
$query="SELECT count(*) FROM ".$tbl_categories." WHERE categoryid='$adcategoryid'";
if (($res=mysql_query($query))) {
$categoryidexists=true;
}
return $categoryidexists;
}
// END FUNCTION: check if a specific ad id already exists
// START FUNCTION: get the current name of the classfieds page
function display_setup_text()
{
$awpcpsetuptext="
";
$awpcpsetuptext.=__("It looks like you have not yet told the system how you want your classifieds to operate.","AWPCP");
$awpcpsetuptext.="
";
$awpcpsetuptext.="
";
$awpcpsetuptext.=__("Please begin by setting up the options for your site. The system needs to know a number of things about how you want to run your classifieds.","AWPCP");
$awpcpsetuptext.='
';
return $awpcpsetuptext;
}
/**
* Returns the current name of the AWPCP main page.
*/
function get_currentpagename() {
return get_awpcp_option('main-page-name');
}
// END FUNCTION: query awpcp_pagename for the name being used for the classifieds site
// START FUNCTION: delete the classfied page name from database as needed
function deleteuserpageentry() {
global $wpdb;
$tbl_pagename = $wpdb->prefix . "awpcp_pagename";
$query="TRUNCATE ".$tbl_pagename."";
$res = awpcp_query($query, __LINE__);
mysql_query($query);
}
// END FUNCTION: delete the user page entry from awpcp_pagename table
// START FUNCTION: check if the classifieds page exists in the wp posts table
function findpagebyname($pagename) {
global $wpdb,$table_prefix;
$myreturn=false;
$query="SELECT post_title FROM {$table_prefix}posts WHERE post_title='$pagename'";
$res = awpcp_query($query, __LINE__);
if (mysql_num_rows($res) && mysql_result($res,0,0)) {
$myreturn=true;
}
return $myreturn;
}
function findpage($pagename,$shortcode) {
global $wpdb,$table_prefix;
$myreturn=false;
$query="SELECT post_title FROM {$table_prefix}posts WHERE post_title='$pagename' AND post_content LIKE '%$shortcode%'";
$res = awpcp_query($query, __LINE__);
if (mysql_num_rows($res) && mysql_result($res,0,0)) {
$myreturn=true;
}
return $myreturn;
}
// START FUNCTION: check ad_settings to see if a particular function exists to prevent duplicate entery when updating plugin
function field_exists($field) {
global $wpdb;
$tbl_ad_settings = $wpdb->prefix . "awpcp_adsettings";
$tableexists=checkfortable($tbl_ad_settings);
if($tableexists) {
$query="SELECT config_value FROM ".$tbl_ad_settings." WHERE config_option='$field'";
$res = awpcp_query($query, __LINE__);
if (mysql_num_rows($res)) {
$myreturn=true;
} else {
$myreturn=false;
}
return $myreturn;
}
return false;
}
// END FUNCTION: check if ad_settings field exists
// START FUNCTION: a general functin to shorten text to summary or excerpt
function awpcpLimitText($Text,$Min,$Max,$MinAddChar) {
if (strlen($Text) < $Min) {
$Limit = $Min-strlen($Text);
$Text .= $MinAddChar;
}
elseif (strlen($Text) >= $Max) {
$words = explode(" ", $Text);
$check=1;
while (strlen($Text) >= $Max) {
$c=count($words)-$check;
$Text=substr($Text,0,(strlen($words[$c])+1)*(-1));
$check++;
}
}
return $Text;
}
// END FUNCTION: limit text
function isValidURL($url)
{
return preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $url);
}
//function isValidEmailAddress($email) {
// if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
// return false;
// }
//
// $email_array = explode("@", $email);
// $local_array = explode(".", $email_array[0]);
// for ($i = 0; $i < sizeof($local_array); $i++) {
// if
// (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&
//?'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$",
// $local_array[$i])) {
// return false;
// }
// }
//
// if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) {
// $domain_array = explode(".", $email_array[1]);
// if (sizeof($domain_array) < 2) {
// return false; // Not enough parts to domain
// }
// for ($i = 0; $i < sizeof($domain_array); $i++) {
// if
// (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|
//?([A-Za-z0-9]+))$",
// $domain_array[$i])) {
// return false;
// }
// }
// }
// return true;
//}
function isValidEmailAddress($email) {
// First, we check that there's one @ symbol,
// and that the lengths are right.
if (!preg_match("/^[^@]{1,64}@[^@]{1,255}$/", $email)) {
// Email invalid because wrong number of characters
// in one section or wrong number of @ symbols.
return false;
}
// Split it into sections to make life easier
$email_array = explode("@", $email);
$local_array = explode(".", $email_array[0]);
for ($i = 0; $i < sizeof($local_array); $i++) {
//Special handling for 1-character domains: (e.g. q.com):
if ($i == 1 && strlen($local_array[$i]) == 1 &&
preg_match("/[A-Za-z0-9]/", $local_array[$i])) {
//single character domain is valid
continue;
}
if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$",
$local_array[$i])) {
return false;
}
}
// Check if domain is IP. If not,
// it should be valid domain name
if (!preg_match("/^\[?[0-9\.]+\]?$/", $email_array[1])) {
$domain_array = explode(".", $email_array[1]);
if (sizeof($domain_array) < 2) {
return false; // Not enough parts to domain
}
for ($i = 0; $i < sizeof($domain_array); $i++) {
if
(!preg_match("/^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$/",
$domain_array[$i])) {
return false;
}
}
}
return true;
}
// START FUNCTION: function to handle automatic ad expirations
/**
* Unused function
*/
function renewsubscription($adid) {
global $wpdb;
$query = "SELECT payment_status FROM " . AWPCP_TABLE_ADS . " WHERE ad_id='$adid'";
$res = awpcp_query($query, __LINE__);
while ($rsrow=mysql_fetch_row($res)) {
list($paymentstatus)=$rsrow;
}
if($paymentstatus != 'Cancelled') {
return true;
}
return false;
}
// END FUNCTION: process auto ad expiration
// START FUNCTION: Function to check for the existence of a default category with a category ID of 1 (used with mass category deletion)
function defaultcatexists($defid) {
global $wpdb;
$tbl_categories = $wpdb->prefix . "awpcp_categories";
$myreturn=false;
$query="SELECT * FROM ".$tbl_categories." WHERE category_id='$defid'";
$res = awpcp_query($query, __LINE__);
if (mysql_num_rows($res)) {
$myreturn=true;
}
return $myreturn;
}
// END FUNCTION: check if default category exists
// START FUNCTION: function to create a default category with an ID of 1 in the event a default category with ID 1 does not exist
function createdefaultcategory($idtomake,$titletocallit) {
global $wpdb;
$tbl_categories = $wpdb->prefix . "awpcp_categories";
$query="INSERT INTO ".$tbl_categories." SET category_name='$titletocallit',category_parent_id='0'";
$res = awpcp_query($query, __LINE__);
$newdefid=mysql_insert_id();
$query="UPDATE ".$tbl_categories." SET category_id='1' WHERE category_id='$newdefid'";
$res = awpcp_query($query, __LINE__);
}
// END FUNCTION: create default category
//////////////////////
// START FUNCTION: function to delete multiple ads at once used when admin deletes a category that contains ads but does not move the ads to a new category
//////////////////////
function massdeleteadsfromcategory($catid){
global $wpdb,$nameofsite,$siteurl,$thisadminemail;
$tbl_ads = $wpdb->prefix . "awpcp_ads";
$tbl_ad_photos = $wpdb->prefix . "awpcp_adphotos";
// Get the IDs of the ads to be deleted
$query="SELECT ad_id FROM ".$tbl_ads." WHERE ad_category_id='$catid'";
$res = awpcp_query($query, __LINE__);
$fordeletionid=array();
if (mysql_num_rows($res)) {
while ($rsrow=mysql_fetch_row($res)) {
$fordeletionid[]=$rsrow[0];
}
$totalusers=count($fordeletionid);
}
$adstodelete=join("','",$fordeletionid);
// Delete the ad images
$query="SELECT image_name FROM ".$tbl_ad_photos." WHERE ad_id IN ('$adstodelete')";
$res = awpcp_query($query, __LINE__);
for ($i=0;$i= 1 )
{
//debug($allpageswithawpcppagename);
foreach ( $allpageswithawpcppagename as $thispagewithawpcpname )
{
//Delete the pages
//wp_delete_post( $thispagewithawpcpname, $force_delete = true );
}
deleteuserpageentry($awpcppage);
}
}
else
{
return 0;
}
}
// END FUNCTION: make sure there's not more than one page with the name of the classifieds page
// START FUNCTION: create a drop down list containing names of ad posters
function create_ad_postedby_list()
{
$output = '';
global $wpdb;
$tbl_ads = $wpdb->prefix . "awpcp_ads";
$query="SELECT DISTINCT ad_contact_name FROM ".$tbl_ads." WHERE disabled='0'";
$res = awpcp_query($query, __LINE__);
while ($rsrow=mysql_fetch_row($res))
{
$output .= "";
}
return $output;
}
// END FUNCTION: create a drop down list containing names of ad posters
// START FUNCTION: create a drop down list containing price option
function create_price_dropdownlist_min($searchpricemin)
{
$output = '';
$pricerangevalues=array('0','25','50','100','500','1000','2500','5000','7500','10000','25000','50000','100000','250000','500000','1000000');
if( isset($searchpricemin) && !empty($searchpricemin) )
{
$theawpcplowvalue=$searchpricemin;
}
else
{
$theawpcplowvalue='';
}
foreach ($pricerangevalues as $pricerangevalue)
{
$output .= "";
}
return $output;
}
// END FUNCTION: create a drop down list containing cities options from saved cities in database
// START FUNCTION: create a drop down list containing state options from saved states in database
function create_dropdown_from_current_states()
{
$output = '';
global $wpdb;
$tbl_ads = $wpdb->prefix . "awpcp_ads";
$listofsavedstates=array();
$query="SELECT DISTINCT ad_state FROM ".$tbl_ads." WHERE ad_state <> '' AND disabled = '0' ORDER by ad_state ASC";
$res = awpcp_query($query, __LINE__);
while ($rsrow=mysql_fetch_row($res))
{
$listofsavedstates[]=$rsrow[0];
$savedstateslist=array_unique($listofsavedstates);
}
foreach ($savedstateslist as $savedstate)
{
$output .= "";
}
return $output;
}
// END FUNCTION: create a drop down list containing states options from saved states in database
// START FUNCTION: create a drop down list containing county/village options from saved states in database
function create_dropdown_from_current_counties()
{
$output = '';
global $wpdb;
$tbl_ads = $wpdb->prefix . "awpcp_ads";
$listofsavedcounties=array();
$query="SELECT DISTINCT ad_county_village FROM ".$tbl_ads." WHERE ad_county_village <> '' AND disabled = '0' ORDER by ad_county_village ASC";
$res = awpcp_query($query, __LINE__);
while ($rsrow=mysql_fetch_row($res))
{
$listofsavedcounties[]=$rsrow[0];
$savedcountieslist=array_unique($listofsavedcounties);
}
foreach ($savedcountieslist as $savedcounty)
{
$output .= "";
}
return $output;
}
// END FUNCTION: create a drop down list containing county/village options from saved states in database
// START FUNCTION: create a drop down list containing country options from saved countries in database
function create_dropdown_from_current_countries() {
global $wpdb;
$output = '';
$tbl_ads = $wpdb->prefix . "awpcp_ads";
$query="SELECT DISTINCT ad_country FROM ".$tbl_ads." WHERE ad_country <> '' AND disabled = '0' ORDER by ad_country ASC";
$res = awpcp_query($query, __LINE__);
$listofsavedcountries = array();
while ($rsrow=mysql_fetch_row($res)) {
$listofsavedcountries[] = $rsrow[0];
}
$savedcountrieslist = $listofsavedcountries;
foreach ($savedcountrieslist as $savedcountry) {
$output .= "";
}
return $output;
}
// END FUNCTION: create a drop down list containing country options from saved states in database
// START FUNCTION: Check if ads table contains city data
function adstablehascities()
{
$myreturn=false;
global $wpdb;
$tbl_ads = $wpdb->prefix . "awpcp_ads";
$query="SELECT ad_city FROM ".$tbl_ads." WHERE ad_city <> ''";
$res = awpcp_query($query, __LINE__);
if (mysql_num_rows($res))
{
$myreturn=true;
}
return $myreturn;
}
// END FUNCTION: Check if ads table contains city data
// START FUNCTION: Check if ads table contains state data
function adstablehasstates()
{
$myreturn=false;
global $wpdb;
$tbl_ads = $wpdb->prefix . "awpcp_ads";
$query="SELECT ad_state FROM ".$tbl_ads." WHERE ad_state <> ''";
$res = awpcp_query($query, __LINE__);
if (mysql_num_rows($res))
{
$myreturn=true;
}
return $myreturn;
}
// END FUNCTION: Check if ads table contains state data
// START FUNCTION: Check if ads table contains country data
function adstablehascountries()
{
$myreturn=false;
global $wpdb;
$tbl_ads = $wpdb->prefix . "awpcp_ads";
$query="SELECT ad_country FROM ".$tbl_ads." WHERE ad_country <> ''";
$res = awpcp_query($query, __LINE__);
if (mysql_num_rows($res))
{
$myreturn=true;
}
return $myreturn;
}
// END FUNCTION: Check if ads table contains country data
// START FUNCTION: Check if ads table contains county data
function adstablehascounties()
{
$myreturn=false;
global $wpdb;
$tbl_ads = $wpdb->prefix . "awpcp_ads";
$query="SELECT ad_county_village FROM ".$tbl_ads." WHERE ad_county_village <> ''";
$res = awpcp_query($query, __LINE__);
if (mysql_num_rows($res))
{
$myreturn=true;
}
return $myreturn;
}
// END FUNCTION: Check if ads table contains county data
// START FUNCTION: check if there are any values entered into the price field for any ad
function price_field_has_values()
{
$myreturn=false;
global $wpdb;
$tbl_ads = $wpdb->prefix . "awpcp_ads";
$query="SELECT ad_item_price FROM ".$tbl_ads." WHERE ad_item_price > '0'";
$res = awpcp_query($query, __LINE__);
if (mysql_num_rows($res))
{
$myreturn=true;
}
return $myreturn;
}
// END FUNCTION: check if there are any values entered into the price field for any ad
// START FUNCTION: get an image name associated with a specified ad ID
function get_a_random_image($ad_id)
{
global $wpdb;
$tbl_ad_photos = $wpdb->prefix . "awpcp_adphotos";
$awpcp_image_name='';
$query="SELECT image_name FROM ".$tbl_ad_photos." WHERE ad_id='$ad_id' AND disabled='0' LIMIT 1";
$res = awpcp_query($query, __LINE__);
if (mysql_num_rows($res))
{
list($awpcp_image_name)=mysql_fetch_row($res);
}
return $awpcp_image_name;
}
// END FUNCTION: get an image name associated with a specified ad ID
// START FUNCTION: check a specific ad to see if it is disabled or enabled
function check_if_ad_is_disabled($adid) {
global $wpdb;
$tbl_ads = $wpdb->prefix . "awpcp_ads";
$myreturn=false;
$query="SELECT disabled FROM ".$tbl_ads." WHERE ad_id='$adid'";
$res = awpcp_query($query, __LINE__);
while ($rsrow=mysql_fetch_row($res))
{
list($adstatusdisabled)=$rsrow;
}
if ($adstatusdisabled == 1)
{
$myreturn=true;
}
return $myreturn;
}
// END FUNCTION: check a specific ad to see if it is disabled or enabled
function check_ad_fee_paid($adid) {
global $wpdb;
$tbl_ads = $wpdb->prefix . "awpcp_ads";
$adfeeispaid=false;
$query="SELECT ad_fee_paid FROM ".$tbl_ads." WHERE ad_id='$adid'";
while ($rsrow=mysql_fetch_row($res))
{
list($ad_fee_paid)=$rsrow;
}
if($ad_fee_paid > 0){$adfeeispaid=true;}
return $adfeeispaid;
}
// START FUNCTION: get the currency code for price fields
function awpcp_get_currency_code()
{
$amtcurrencycode=get_awpcp_option('displaycurrencycode');
if(
($amtcurrencycode == 'CAD') ||
($amtcurrencycode == 'AUD') ||
($amtcurrencycode == 'NZD') ||
($amtcurrencycode == 'SGD') ||
($amtcurrencycode == 'HKD') ||
($amtcurrencycode == 'USD') )
{
$thecurrencysymbol="$";
}
if( ($amtcurrencycode == 'JPY') )
{
$thecurrencysymbol="¥";
}
if( ($amtcurrencycode == 'EUR') )
{
$thecurrencysymbol="€";
}
if( ($amtcurrencycode == 'GBP') )
{
$thecurrencysymbol="£";
}
if(empty($thecurrencysymbol)) {
$thecurrencysymbol="$amtcurrencycode";
}
return $thecurrencysymbol;
}
// END FUNCTION: get the currency code for price fields
// START FUNCTION: Clear HTML tags
function strip_html_tags( $text )
{
// Remove invisible content
$text = preg_replace(
array(
'@]*?>.*?@siu',
'@@siu',
'@@siu',
'@@siu',
'@@siu',
'@@siu',
'@]*?.*?@siu',
'@@siu',
'@]*?.*?@siu',
// Add line breaks before and after blocks
'@?((address)|(blockquote)|(center)|(del))@iu',
'@?((div)|(h[1-9])|(ins)|(isindex)|(p)|(pre))@iu',
'@?((dir)|(dl)|(dt)|(dd)|(li)|(menu)|(ol)|(ul))@iu',
'@?((table)|(th)|(td)|(caption))@iu',
'@?((form)|(button)|(fieldset)|(legend)|(input))@iu',
'@?((label)|(select)|(optgroup)|(option)|(textarea))@iu',
'@?((frameset)|(frame)|(iframe))@iu',
),
array(
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
"\n\$0", "\n\$0", "\n\$0", "\n\$0", "\n\$0", "\n\$0",
"\n\$0", "\n\$0",
),
$text );
return strip_tags( $text );
}
// END FUNCTION
// Override the SMTP settings built into WP if the admin has enabled that feature
// add_action('phpmailer_init','awpcp_phpmailer_init_smtp');
function awpcp_phpmailer_init_smtp( $phpmailer ) {
// smtp not enabled?
$enabled = get_awpcp_option('usesmtp');
if ( !$enabled || '0' == $enabled ) return;
$host = get_awpcp_option('smtphost');
$port = get_awpcp_option('smtpport');
$username = get_awpcp_option('smtpusername');
$password = get_awpcp_option('smtppassword');
// host and port not set? gotta have both.
if ( '' == trim( $hostname ) || '' == trim( $port ) )
return;
// still got defaults set? can't use those.
if ( 'mail.example.com' == trim( $hostname ) ) return;
if ( 'smtp_username' == trim( $username ) ) return;
$phpmailer->Mailer = 'smtp';
$phpmailer->Host = $host;
$phpmailer->Port = $port;
// If there's a username and password then assume SMTP Auth is necessary and set the vars:
if ( '' != trim( $username ) && '' != trim( $password ) ) {
$phpmailer->SMTPAuth = true;
$phpmailer->Username = $username;
$phpmailer->Password = $password;
}
// that's it!
}
function awpcp_process_mail($awpcpsenderemail='', $awpcpreceiveremail='',
$awpcpemailsubject='', $awpcpemailbody='', $awpcpsendername='', $awpcpreplytoemail='', $html=false) {
$headers = "MIME-Version: 1.0\n" .
"From: $awpcpsendername <$awpcpsenderemail>\n" .
"Reply-To: $awpcpreplytoemail\n";
if ($html) {
$headers .= "Content-Type: text/html; charset=\"" . get_option('blog_charset') . "\"\n";
} else {
$headers .= "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
}
$subject = $awpcpemailsubject;
$time = date_i18n( __('l F j, Y \a\t g:i a', "AWPCP"), current_time( 'timestamp' ) );
$message = "$awpcpemailbody\n\n";
$message.= __('Email sent on:', 'AWPCP')." $time\n\n";
_log("Processing email");
if(wp_mail( $awpcpreceiveremail, $subject, $message, $headers ))
{
_log("Sent via WP");
return 1;
}
elseif( awpcp_send_email($awpcpsenderemail, $awpcpreceiveremail, $awpcpemailsubject, $awpcpemailbody,true) )
{
_log("Sent via send_email");
return 1;
}
elseif( @mail($awpcpreceiveremail, $awpcpemailsubject, $awpcpemailbody, $headers) )
{
_log("Sent via mail");
return 1;
}
else
{
_log("SMTP not configured properly, all attempts failed");
return 0;
}
}
// make sure the IP isn't a reserved IP address
function awpcp_validip($ip) {
if (!empty($ip) && ip2long($ip)!=-1) {
$reserved_ips = array (
array('0.0.0.0','2.255.255.255'),
array('10.0.0.0','10.255.255.255'),
array('127.0.0.0','127.255.255.255'),
array('169.254.0.0','169.254.255.255'),
array('172.16.0.0','172.31.255.255'),
array('192.0.2.0','192.0.2.255'),
array('192.168.0.0','192.168.255.255'),
array('255.255.255.0','255.255.255.255')
);
foreach ($reserved_ips as $r) {
$min = ip2long($r[0]);
$max = ip2long($r[1]);
if ((ip2long($ip) >= $min) && (ip2long($ip) <= $max))
return false;
}
return true;
} else {
return false;
}
}
// retrieve the ad poster's IP if possible
function awpcp_getip() {
if ( awpcp_validip($_SERVER["HTTP_CLIENT_IP"]) ) {
return $_SERVER["HTTP_CLIENT_IP"];
}
foreach ( explode(",",$_SERVER["HTTP_X_FORWARDED_FOR"]) as $ip ) {
if ( awpcp_validip(trim($ip) ) ) {
return $ip;
}
}
if (awpcp_validip($_SERVER["HTTP_X_FORWARDED"])) {
return $_SERVER["HTTP_X_FORWARDED"];
} elseif (awpcp_validip($_SERVER["HTTP_FORWARDED_FOR"])) {
return $_SERVER["HTTP_FORWARDED_FOR"];
} elseif (awpcp_validip($_SERVER["HTTP_FORWARDED"])) {
return $_SERVER["HTTP_FORWARDED"];
} else {
return $_SERVER["REMOTE_ADDR"];
}
}
function is_at_least_awpcp_version($version)
{
global $awpcp_plugin_data;
if (version_compare($awpcp_plugin_data['Version'], $version, ">=")) {
// you're on a later or equal version, this is good
$ok = true;
} else {
// earlier version, this is bad
$ok = false;
}
return $ok;
}
// add_filter('awpcp_single_ad_layout', 'awpcp_insert_tweet_button', 1, 3);
function awpcp_insert_tweet_button($layout, $adid, $title) {
$adurl = url_showad($adid);
$button = '