if ($config['allow_classifieds_feeds']) {
    if (!empty($cat)) {
        $feeds_mode = $cat;
    } else {
        $feeds_mode = 'active';
    }
}
// Random ads block
if ($config['display_rand_miniblock'] && $config['rand_miniblock_num_ads'] != 0) {
    display_random_ads($config['rand_miniblock_num_ads']);
}
// Hot ads block
if ($config['display_hot_ads'] && $config['hot_ads_num'] != 0) {
    display_hot_ads($config['hot_ads_num']);
}
load_rules(1);
load_prefixes();
load_locations();
user_total_ads($user->data['user_id'], 'left_bar');
$template->assign_block_vars('navlinks', array('FORUM_NAME' => $user->lang['CL_CLASSIFIEDS'], 'U_VIEW_FORUM' => append_sid("{$phpbb_root_path}" . CL_DIRECTORY . "/index.{$phpEx}")));
$template->assign_block_vars('navlinks', array('FORUM_NAME' => $classifieds_title, 'U_VIEW_FORUM' => $pagination_url));
$template->assign_vars(array('S_IN_CLASSIFIEDS_INDEX' => true, 'S_WATCHING_CAT' => $watching_cat, 'S_RANDOM_LEFT' => $config['rand_miniblock_place'], 'S_DISPLAY_RANDOM_BLOCK' => $config['display_rand_miniblock'], 'S_HOT_LEFT' => $config['hot_block_place'], 'S_DISPLAY_HOT_BLOCK' => $config['display_hot_ads'], 'S_CLOSED_COLOR' => $config['closed_color'], 'S_SOLD_COLOR' => $config['sold_color'], 'RANDOM_BLOCK_WIDTH' => $config['rand_miniblock_num_ads'] != 0 ? floor(100 / $config['rand_miniblock_num_ads']) : '', 'HOT_BLOCK_WIDTH' => $config['hot_ads_num'] != 0 ? floor(100 / $config['hot_ads_num']) : '', 'CATEGORIES' => build_categories(), 'CLASSIFIEDS_TITLE' => $classifieds_title, 'L_CL_CLASSIFIEDS_RSS' => $user->lang('CL_CLASSIFIEDS_RSS', !empty($cat) ? $user->lang['CL_FOR_CAT'] . $classifieds_title : $user->lang['CL_FOR_ALL_ACTIVE_ADS']), 'U_VIEW_REPORTED_LINK' => $auth->acl_get('a_') || $auth->acl_get('m_report_classifieds') ? append_sid("{$phpbb_root_path}" . CL_DIRECTORY . "/index.{$phpEx}", 'mode=reported') : '', 'U_NEW_AD' => $auth->acl_get('u_post_classifieds') ? append_sid("{$phpbb_root_path}" . CL_DIRECTORY . "/manage_ad.{$phpEx}", 'mode=create_ad') : '', 'U_SEARCH_ADS' => append_sid("{$phpbb_root_path}" . CL_DIRECTORY . "/index.{$phpEx}", "mode=search"), 'U_WATCH_CAT' => $can_watch_cat ? append_sid("{$phpbb_root_path}" . CL_DIRECTORY . "/index.{$phpEx}", 'id=' . $cat . '&subscribe=1') : '', 'U_CLASSIFIEDS_RSS' => $config['allow_classifieds_feeds'] ? append_sid("{$phpbb_root_path}" . CL_DIRECTORY . "/feeds.{$phpEx}", "mode={$feeds_mode}") : '', 'REPORTED_IMG' => $user->img('icon_topic_reported', 'TOPIC_REPORTED')));
if ($now > $config['last_check_expired'] + 3600) {
    // Select all Active but Expired ads and sent emails to their owners
    $sql = 'SELECT a.ad_expire, a.ad_id, a.ad_title, a.ad_date, a.ad_price, a.ad_price_text, u.user_id, u.username, u.user_lang, u.user_email, u.user_jabber, u.user_notify_type
  					FROM  ' . USERS_TABLE . ' u, ' . CLASSIFIEDS_TABLE . ' a
  					WHERE u.user_id = a.ad_poster_id
  						AND ' . $now . ' > a.ad_expire
  						AND (a.expire_email = 0 OR a.expire_email = "")
  						AND a.ad_status = ' . ACTIVE;
    $result = $db->sql_query($sql);
    }
}
// Display Advertiser's ads block
if ($config['display_advertisers_ads'] && $config['advertisers_ads_num'] != 0 && $row['ad_poster_id'] != '') {
    display_advertisers_ads($config['advertisers_ads_num'], $row['user_id'], $ad_id);
    $template->assign_vars(array('AD_POSTER_USERNAME' => $row['username'], 'ADVERTISERS_BLOCK_WIDTH' => $config['advertisers_ads_num'] != 0 ? floor(100 / $config['advertisers_ads_num']) : ''));
}
// User can edit or only move an ad
if ($auth->acl_get('u_edit_own_classifieds') && $user->data['user_id'] == $row['ad_poster_id'] || $auth->acl_get('a_') || $auth->acl_get('m_edit_classifieds')) {
    $edit_link = "{$phpbb_root_path}" . CL_DIRECTORY . "/manage_ad.{$phpEx}?mode=edit_ad&ad_id=" . $ad_id;
} elseif ($auth->acl_get('m_move_classifieds')) {
    $edit_link = "{$phpbb_root_path}" . CL_DIRECTORY . "/manage_ad.{$phpEx}?mode=move_ad&ad_id=" . $ad_id;
} else {
    $edit_link = false;
}
load_rules(2);
load_prefixes();
load_locations();
user_total_ads($row['user_id'], 'single_ad');
user_total_ads($user->data['user_id'], 'left_bar');
$template->assign_block_vars('navlinks', array('FORUM_NAME' => $user->lang['CL_CLASSIFIEDS'], 'U_VIEW_FORUM' => append_sid("{$phpbb_root_path}" . CL_DIRECTORY . "/index.{$phpEx}")));
$template->assign_block_vars('navlinks', array('FORUM_NAME' => censor_text($row['name']), 'U_VIEW_FORUM' => append_sid("{$phpbb_root_path}" . CL_DIRECTORY . "/index.{$phpEx}", "mode=cat&id=" . $row['cat_id'])));
$template->assign_block_vars('navlinks', array('FORUM_NAME' => censor_text($row['ad_title']), 'U_VIEW_FORUM' => append_sid($phpbb_root_path . CL_DIRECTORY . '/single_ad.' . $phpEx, 'ad_id=' . $ad_id)));
$template->assign_vars(array('S_IN_CLASSIFIEDS_SINGLE_AD' => true, 'S_BBCODE_ALLOWED' => true, 'S_ALLOWED_ADDTHIS_BUTTON' => $config['allow_addthis_button'], 'S_COMMENTS_ENABLED' => $config['allow_comments'], 'S_CUSTOM_FIELDS' => isset($cp_row['row']) && sizeof($cp_row['row']) ? true : false, 'S_ADVERTISERS_LEFT' => $config['advertisers_block_place'], 'S_DISPLAY_ADVERTISERS_BLOCK' => $config['display_advertisers_ads'], 'S_CAN_QUICK_AD' => $user->data['user_id'] == $row['ad_poster_id'] || $auth->acl_get('a_') ? true : false, 'CATEGORIES' => build_categories(), 'EXPIRATION_DATE' => $user->format_date($row['ad_expire']), 'AD_DESCRIPTION' => censor_text($description), 'LOCATION' => $row['location_name'] ? $row['location_name'] : $row['user_from'], 'EXPIRE_DATE' => $row['ad_expire'], 'NUM_POSTS' => $row['user_posts'], 'JOINED' => $user->format_date($row['user_regdate']), 'AD_TITLE' => censor_text($row['ad_title']), 'AD_PRICE' => $row['ad_price_text'] == 0 ? str_replace('.00', '', $row['ad_price']) : $user->lang['CL_BY_AGREEMENT'], 'PAYPAL_PRICE' => $row['ad_price_text'] == 0 ? str_replace('.00', '', $row['ad_price']) : '', 'CAT_NAME' => $row['name'], 'AD_DATE' => $user->format_date($row['ad_date']), 'AD_POSTER' => get_username_string('full', $row['user_id'], $row['username'], $row['user_colour']), 'AD_STATUS' => $row['ad_status'], 'AD_STATUS_TEXT' => $ad_status, 'USER_AIM' => $row['user_aim'], 'USER_MSN' => $row['user_msnm'], 'USER_YIM' => $row['user_yim'], 'USER_JABBER' => $row['user_jabber'], 'PHONE' => $row['phone'], 'PAYPAL' => $row['paypal'], 'AD_CURRENCY' => $row['paypal_currency'], 'AD_PREFIX' => $row['prefix_short'], 'AD_PREFIX_COLOR' => $row['prefix_color'], 'LAST_EDIT' => $row['edit_time'] ? sprintf($user->lang['CL_AD_LAST_EDIT'], $user->format_date($row['edit_time']), $row['last_edit_by']) : '', 'S_SHOW_SMILEY_LINK' => true, 'U_MORE_SMILIES' => append_sid("{$phpbb_root_path}posting.{$phpEx}", 'mode=smilies'), 'S_SMILIES_ALLOWED' => true, 'S_BBCODE_ALLOWED' => true, 'S_BBCODE_IMG' => true, 'S_LINKS_ALLOWED' => true, 'S_BBCODE_QUOTE' => true, 'U_VIEW_REPORTED_LINK' => $auth->acl_get('a_') || $auth->acl_get('m_report_classifieds') ? append_sid("{$phpbb_root_path}" . CL_DIRECTORY . "/index.{$phpEx}", 'mode=reported') : '', 'U_SEARCH_ADS' => append_sid("{$phpbb_root_path}" . CL_DIRECTORY . "/index.{$phpEx}", 'mode=search'), 'U_AD_ACTION' => append_sid("{$phpbb_root_path}" . CL_DIRECTORY . "/single_ad.{$phpEx}", "ad_id={$ad_id}"), 'U_COMMENT' => append_sid("{$phpbb_root_path}" . CL_DIRECTORY . "/includes/class_comment.{$phpEx}", "mode=new_comment&amp;ad_id=" . $ad_id), 'U_CLOSE_REPORT' => $row['reported'] && ($auth->acl_get('a_') || $auth->acl_get('m_report_classifieds')) ? append_sid("{$phpbb_root_path}" . CL_DIRECTORY . "/manage_ad.{$phpEx}", 'mode=close_report&amp;ad_id=' . $ad_id) : '', 'U_DELETE_LINK' => $auth->acl_get('u_can_delete_classifieds') && $user->data['user_id'] == $row['ad_poster_id'] || $auth->acl_get('a_') || $auth->acl_get('m_delete_classifieds') ? append_sid("{$phpbb_root_path}" . CL_DIRECTORY . "/manage_ad.{$phpEx}", 'mode=delete_ad&amp;ad_id=' . $ad_id) : '', 'U_EXTEND_LINK' => ($auth->acl_get('m_extend_classifieds') || $auth->acl_get('u_can_extend_classifieds') && $user->data['user_id'] == $row['ad_poster_id'] || $auth->acl_get('a_')) && $row['ad_expire'] < time() ? append_sid($phpbb_root_path . CL_DIRECTORY . '/manage_ad.' . $phpEx, 'mode=extend_ad&amp;ad_id=' . intval($ad_id)) : '', 'U_EDIT_LINK' => $edit_link ? append_sid($edit_link) : '', 'U_REPORT_LINK' => $auth->acl_get('u_can_report_classifieds') ? append_sid("{$phpbb_root_path}" . CL_DIRECTORY . "/manage_ad.{$phpEx}", 'mode=report_ad&amp;ad_id=' . $ad_id) : '', 'U_PM' => $config['allow_privmsg'] && $auth->acl_get('u_sendpm') && ($row['user_allow_pm'] || $auth->acl_gets('a_', 'm_') || $auth->acl_getf_global('m_')) ? append_sid("{$phpbb_root_path}ucp.{$phpEx}", 'i=pm&amp;mode=compose&amp;u=' . $row['user_id']) : '', 'U_AIM' => $row['user_aim'] && $auth->acl_get('u_sendim') ? append_sid("{$phpbb_root_path}memberlist.{$phpEx}", 'mode=contact&amp;action=aim&amp;u=' . $row['user_id']) : '', 'U_EMAIL' => !empty($row['user_allow_viewemail']) && $auth->acl_get('u_sendemail') || $auth->acl_get('a_user') ? $config['board_email_form'] && $config['email_enable'] ? append_sid("{$phpbb_root_path}memberlist.{$phpEx}", 'mode=email&amp;u=' . $row['user_id']) : ($config['board_hide_emails'] && !$auth->acl_get('a_user') ? '' : 'mailto:' . $row['user_email']) : '', 'U_MSN' => $row['user_msnm'] && $auth->acl_get('u_sendim') ? append_sid("{$phpbb_root_path}memberlist.{$phpEx}", 'mode=contact&amp;action=msnm&amp;u=' . $row['user_id']) : '', 'U_YIM' => $row['user_yim'] ? 'http://edit.yahoo.com/config/send_webmesg?.target=' . urlencode($row['user_yim']) . '&amp;.src=pg' : '', 'U_JABBER' => $row['user_jabber'] && $auth->acl_get('u_sendim') ? append_sid("{$phpbb_root_path}memberlist.{$phpEx}", 'mode=contact&amp;action=jabber&amp;u=' . $row['user_id']) : ''));
// Select images
$sql = 'SELECT image_name
					FROM  ' . CLASSIFIEDS_IMAGES_TABLE . '
					WHERE ad_id = ' . $ad_id;
$result = $db->sql_query($sql);
while ($row = $db->sql_fetchrow($result)) {
    $template->assign_block_vars('images', array('SOURCE' => generate_board_url() . '/' . CL_DIRECTORY . '/images/' . $row['image_name'], 'THUMB_SOURCE' => generate_board_url() . '/' . CL_DIRECTORY . '/images/thumb/' . $row['image_name']));
}
// if is bot
if ($user->data['is_bot']) {
    redirect(append_sid("{$phpbb_root_path}index.{$phpEx}"));
}
// if is not logged in
if ($user->data['user_id'] == ANONYMOUS) {
    login_box();
}
$mode = request_var('mode', '');
// check the mode
if (!$mode || $mode != 'create_ad' && $mode != 'edit_ad' && $mode != 'move_ad' && $mode != 'delete_ad' && $mode != 'report_ad' && $mode != 'close_report' && $mode != 'extend_ad') {
    trigger_error('NO_MODE');
}
// load rules
$rules_must_agree = load_rules(3);
/* show custom bbcode, smilies, categories, prefixes, locations, curency
*  request and generate temporary advertisement id for images
*  set some template variables
*/
if ($mode == 'create_ad' || $mode == 'edit_ad') {
    if (!function_exists('display_custom_bbcodes')) {
        include $phpbb_root_path . 'includes/functions_display.' . $phpEx;
    }
    display_custom_bbcodes();
    generate_smilies('inline', 0);
    list_categories();
    load_currency();
    $prefix_exist = load_prefixes();
    $location_exist = load_locations();
    $template->assign_vars(array('S_BBCODE_ALLOWED' => true, 'S_BBCODE_IMG' => true, 'S_LINKS_ALLOWED' => true, 'S_SMILIES_ALLOWED' => true, 'S_BBCODE_QUOTE' => true, 'S_SHOW_SMILEY_LINK' => true, 'S_MAX_IMG_SIZE' => $config['max_img_size'] / 1024, 'S_COMMENTS_ENABLED' => $config['allow_comments'], 'S_MANDATORY_PHONE' => $config['mandatory_phone'], 'S_MANDATORY_AD_PREFIX' => $config['mandatory_ad_prefix'], 'S_MANDATORY_AD_LOCATION' => $config['mandatory_ad_location'], 'S_DEFAULT_CURRENCY' => $config['default_currency'], 'S_ALLOW_UPLOAD' => $config['allow_upload'], 'U_MORE_SMILIES' => append_sid("{$phpbb_root_path}posting.{$phpEx}", 'mode=smilies'), 'T_CL_IMAGES_PATH' => generate_board_url() . '/' . CL_DIRECTORY . '/images/', 'T_THUMB_PATH' => generate_board_url() . '/' . CL_DIRECTORY . '/images/thumb/'));