/**
  * Get one record from the table 
  * 
  * @param 	int 	$id							The record's id.
  * @param 	array 	$filters[optional]			Key value pairs for filtering the result. e.g. array('price'=>100) means 'AND price=100'.
  * @return 	array								The result row as array.
  */
 public static function get_one($id, $filters = array())
 {
     $filter = array_merge($filters, array('id' => $id));
     $all = static::get_all($filter, '', '1');
     $ad = array();
     if (isset($all[0])) {
         $ad = $all[0];
         $user = User::get_one(0, array('email' => $ad['email']));
         $ad['user_id'] = isset($user['id']) ? $user['id'] : 0;
     }
     return $ad;
 }
            }
            $last = Ad::create(array('user_id' => User::get_id(), 'name' => $p_name, 'email' => $p_email, 'telephone' => $p_telephone, 'title' => $p_title, 'description' => $p_description, 'picture' => $p_picture, 'category' => $p_category, 'price' => $p_price, 'city' => $p_city, 'region' => $p_region, 'expiry' => $expiry, 'webpage' => $p_webpage, 'code' => $code, 'ipaddr' => $ipaddr, 'postedon' => $postedon, 'lastmodified' => $postedon));
            if (User::is_logged_in()) {
                Ad::activate($last);
            } else {
                if (!($user_exists = User::exists(0, array('email' => $p_email)))) {
                    $p_em = explode('@', $p_email);
                    $username = substr($p_em[0], 0, 6);
                    $active = 0;
                    $createdon = date("Y-m-d H:i:s", time());
                    $password = substr($code, 0, 6);
                    $userid = User::create(array('email' => $p_email, 'username' => $username, 'password' => $password, 'name' => $p_name, 'active' => $active, 'createdon' => $createdon, 'ipaddr' => $ipaddr, 'code' => $code));
                    $registration_message = StaticContent::get_content('user-registration-email');
                    eval("\$registration_message = \"{$registration_message}\";");
                } else {
                    $user = User::get_one(0, array('email' => $p_email));
                    $username = $user['username'];
                }
                $ad_activation_message = StaticContent::get_content('ad-activation-email');
                eval("\$ad_activation_message = \"{$ad_activation_message}\";");
                mail($p_email, "Ad activation (Id: {$last})", $ad_activation_message, "From: " . $noreply);
                debug($ad_activation_message);
                if (!$user_exists) {
                    mail($p_email, "Registration", $registration_message, "From: " . $noreply);
                    debug($registration_message);
                }
            }
        }
    }
}
$curr_page = "post-an-ad";
    exit;
}
if (isset($_POST['user_lost_password'])) {
    $p_email = trim(strip_tags($_POST['email']));
    $success = true;
    $errors = array();
    if ($p_email == '') {
        $success = false;
        array_push($errors, "Please enter your email");
    }
    if ($p_email != '' && !preg_match('/^[\\.\\+_a-z0-9-]+@([0-9a-z][0-9a-z-]*[0-9a-z]\\.)+[a-z]{2}[mtgvu]?$/i', $p_email)) {
        $success = false;
        array_push($errors, "Your email is formated incorrectly.");
    }
    if ($success) {
        $user = User::get_one(1);
        if ($user['email'] != $p_email) {
            $success = false;
            array_push($errors, "Incorrect email.");
        }
    }
    if ($success) {
        $message = StaticContent::get_content('user-lost-pasword-email');
        eval("\$message = \"{$message}\";");
        mail($p_email, "Password reminder", $message, "From: " . $noreply);
    }
}
include "page-header.php";
?>

<div id="wrapper">
<?php

/**
 * Classified-ads-script
 * 
 * @copyright  Copyright (c) Szilard Szabo
 * @license    GPL v3
 * @package    Frontend
 */
include "./admin/include/common.php";
if (!User::is_logged_in() || User::get_id() != 1) {
    header('Location: index.php');
    exit;
}
$id = isset($_GET['id']) ? (int) $_GET['id'] : 0;
$exists = User::exists(0, array('id' => $id));
if ($exists) {
    $user = User::get_one($id);
    UserBanned::create($user, $id);
    Ad::delete(0, array('user_id' => $id));
    User::delete($id);
}
include "./templates/user-ban.php";
    }
    if ($p_category != '' && !preg_match('/^[0-9]{0,10}$/', $p_category)) {
        $success = false;
        array_push($errors, "Incorrect Category.");
    }
    if ('' != $p_webpage && 0 !== strpos($p_webpage, 'http://')) {
        $p_webpage = 'http://' . $p_webpage;
    }
    if ($p_webpage != '' && !preg_match('/^((http|https):\\/\\/)?[a-z0-9]+([\\-\\.]{1}[a-z0-9]+)*\\.[a-z]{2,5}((:[0-9]{1,5})?\\/.*)?$/i', $p_webpage)) {
        $success = false;
        array_push($errors, "The format of webpage is incorrect.");
    }
    if ($success) {
        $fields = array('name' => $p_name, 'telephone' => $p_telephone, 'city' => $p_city, 'region' => $p_region, 'category' => $p_category, 'webpage' => $p_webpage, 'active' => $p_active);
        User::update($g_id, $fields);
        $user = User::get_one($g_id);
        if ($p_send_activation_email) {
            $userid = $user['id'];
            $code = $user['code'];
            $username = $user['username'];
            $password = $user['password'];
            $message = StaticContent::get_content('user-registration-email');
            eval("\$message = \"{$message}\";");
            mail($user['email'], "Registration", $message, "From: " . $noreply);
        }
    }
}
include "page-header.php";
?>

<div id="wrapper">
<?php

/**
 * Classified-users-script
 * 
 * @copyright  Copyright (c) Szilard Szabo
 * @license    GPL v3
 * @package    Frontend
 */
include "./admin/include/common.php";
$r_id = isset($_REQUEST['id']) ? (int) $_REQUEST['id'] : 0;
$exists = User::exists($r_id, array("active" => 1));
if ($exists) {
    $user = User::get_one($r_id);
    if (isset($_POST['send']) && User::is_logged_in()) {
        $success = true;
        $errors = array();
        $p_rate = isset($_POST['rate']) ? (int) $_POST['rate'] : 0;
        $p_comment = strip_tags($_POST['comment']);
        if ($p_rate < 1 || $p_rate > 5) {
            $success = false;
            array_push($errors, "Please provide a valid rate.");
        }
        if ($p_comment != '' && !preg_match('/^[\\s\\S]{0,200}$/u', $p_comment)) {
            $success = false;
            array_push($errors, "The comment can't be more than 200 character long.");
        }
        if ($success) {
            UserReview::create(array('reviewed_user' => $r_id, 'user_id' => User::get_id(), 'rate' => $p_rate, 'comment' => $p_comment));
        }
    }