/**
 * get_article
 * 
 * 
 * 
 * 
 * 
 * 
 */
function get_article($article_id = '')
{
    $article_id = (int) $article_id;
    $conn = reader_connect();
    $query = "SELECT\n\t\t\t\t\tarticles.id, \n\t\t\t\t \tarticles.title, \n\t\t\t\t\tarticles.url, \n\t\t\t\t\tarticles.summary, \n\t\t\t\t\tarticles.body, \n\t\t\t\t\tarticles.date_uploaded, \n\t\t\t\t\tarticles.date_amended,\n\t\t\t\t\tcategories.title AS category_title, \n\t\t\t\t\tcategories.url AS category_url,\n\t\t\t\t\tparent.title AS parent_category_title,\n\t\t\t\t\tparent.url AS parent_category_url,\n\t\t\t\t\tauthors.name AS author_name, \n\t\t\t\t\tauthors.url AS author_url,\n\t\t\t\t\tauthors.email AS author_email,\n\t\t\t\t\tauthors.contact_flag, \n\t\t\t\t\tarticles.seo_title,\n\t\t\t\t\tarticles.seo_desc, \n\t\t\t\t\tarticles.seo_keywords,\n\t\t\t\t\tarticles.comments_disable, \n\t\t\t\t\tarticles.comments_hide\n\t\t\t\tFROM articles\n\t\t\t\t\tLEFT JOIN authors ON articles.author_id = authors.id \n\t\t\t\t\tLEFT JOIN categories ON articles.category_id = categories.id\n\t\t\t\t\tLEFT JOIN categories AS parent ON categories.category_id = parent.id\n\t\t\t\tWHERE articles.status IN('P','A')\n\t\t\t\t\tAND articles.date_uploaded <= NOW()";
    $query .= !empty($article_id) ? " AND articles.id = " . $article_id : " ORDER BY articles.id DESC LIMIT 0,1";
    // echo $query;
    $result = $conn->query($query);
    $row = $result->fetch_assoc();
    $row = stripslashes_deep($row);
    $result->close();
    // fix relative urls
    $row['body'] = convert_relative_urls($row['body']);
    // check pagination
    $row['pages'] = paginate_article_body($row['body']);
    $row['total_pages'] = count($row['pages']);
    // create links
    $row['link']['blog'] = WW_REAL_WEB_ROOT . '/' . date('Y/m/d', strtotime($row['date_uploaded'])) . '/' . $row['url'] . '/';
    $row['link']['cms'] = WW_REAL_WEB_ROOT . '/' . $row['category_url'] . '/' . $row['url'] . '/';
    // get tags
    $tags = get_article_tags($article_id);
    $row['tags'] = !empty($tags) ? $tags : '';
    // get attachments
    $attachments = get_article_attachments($article_id);
    $row['attachments'] = !empty($attachments) ? $attachments : '';
    // get comments
    if (empty($row['comments_hide'])) {
        $comments = get_article_comments($article_id);
        $row['comments'] = !empty($comments) ? $comments : '';
    }
    return $row;
}
/**
 * get_article
 * 
 * 
 * 
 * 
 * 
 * 
 */
function get_article($article_id = '')
{
    $article_id = (int) $article_id;
    $conn = reader_connect();
    $query = "SELECT\n\t\t\t\t\tarticles.id, \n\t\t\t\t \tarticles.title, \n\t\t\t\t\tarticles.url, \n\t\t\t\t\tarticles.summary, \n\t\t\t\t\tarticles.body, \n\t\t\t\t\tarticles.date_uploaded, \n\t\t\t\t\tarticles.date_amended,\n\t\t\t\t\tcategories.title AS category_title, \n\t\t\t\t\tcategories.url AS category_url,\n\t\t\t\t\tparent.title AS parent_category_title,\n\t\t\t\t\tparent.url AS parent_category_url,\n\t\t\t\t\tauthors.name AS author_name, \n\t\t\t\t\tauthors.url AS author_url,\n\t\t\t\t\tauthors.email AS author_email,\n\t\t\t\t\tauthors.contact_flag, \n\t\t\t\t\tarticles.seo_title,\n\t\t\t\t\tarticles.seo_desc, \n\t\t\t\t\tarticles.seo_keywords,\n\t\t\t\t\tarticles.redirect_url,\n\t\t\t\t\tarticles.redirect_code,\n\t\t\t\t\tarticles.comments_disable, \n\t\t\t\t\tarticles.comments_hide\n\t\t\t\tFROM articles\n\t\t\t\t\tLEFT JOIN authors ON articles.author_id = authors.id \n\t\t\t\t\tLEFT JOIN categories ON articles.category_id = categories.id\n\t\t\t\t\tLEFT JOIN categories AS parent ON categories.category_id = parent.id\n\t\t\t\tWHERE articles.status IN('P','A')\n\t\t\t\t\tAND articles.date_uploaded <= UTC_TIMESTAMP()";
    $query .= !empty($article_id) ? " AND articles.id = " . $article_id : " ORDER BY articles.id DESC LIMIT 0,1";
    $result = $conn->query($query);
    $row = $result->fetch_assoc();
    // if a redirect url has been set we redirect right here
    if (!empty($row['redirect_url'])) {
        // send 301 redirect unless another valid code has been set
        $redirect_code = (int) $row['redirect_code'];
        $redirect_code = !empty($redirect_code) ? $redirect_code : 301;
        redirect_article($row['redirect_url'], $redirect_code);
    }
    $row = stripslashes_deep($row);
    $result->close();
    // adjust times to local timezone if necessary
    $ts = strtotime($row['date_uploaded']);
    $offset = date('Z');
    $row['date_ts'] = $ts + $offset;
    $row['date_uploaded'] = date('Y-m-d H:i:s', $row['date_ts']);
    // fix relative urls
    $row['body'] = convert_relative_urls($row['body']);
    // check pagination
    $row['pages'] = paginate_article_body($row['body']);
    $row['total_pages'] = count($row['pages']);
    // create links
    $row['link']['blog'] = WW_REAL_WEB_ROOT . '/' . date('Y/m/d', $row['date_ts']) . '/' . $row['url'] . '/';
    $row['link']['cms'] = WW_REAL_WEB_ROOT . '/' . $row['category_url'] . '/' . $row['url'] . '/';
    // put category url in a get param if not set (for custom css styles)
    $_GET['category_url'] = isset($_GET['category_url']) ? $_GET['category_url'] : $row['category_url'];
    // get tags
    $tags = get_article_tags($article_id);
    $row['tags'] = !empty($tags) ? $tags : '';
    // get attachments
    $attachments = get_article_attachments($article_id);
    $row['attachments'] = !empty($attachments) ? $attachments : '';
    // get comments
    if (empty($row['comments_hide'])) {
        $comments = get_article_comments($article_id);
        $row['comments'] = !empty($comments) ? $comments : '';
    }
    return $row;
}