<?php
/* setup includes */
require_once('includes/master.inc.php');

/* make sure we have permission to view the api */
if (SITE_CONFIG_ENABLE_API == 'no')
{
    redirect('http://' . _CONFIG_SITE_FULL_URL);
}

/* if this page only for logged in users */
if((SITE_CONFIG_ENABLE_API_AUTH == 'yes') && ($Auth->loggedIn() == false))
{
    redirect('http://' . _CONFIG_SITE_FULL_URL);
}

/* load user for api key */
$apiUrl = '';
if($Auth->loggedIn())
{
    $user = UserPeer::loadUserById($Auth->id);
    if (!$user)
    {
        redirect(WEB_ROOT);
    }

    $apiKey = trim($user->apikey);
    if((strlen($apiKey) == 0) || (isset($_REQUEST['rapi'])))
    {
        // create api key
        $apiKey = UserPeer::createApiKey();

        // save in db
        $db->query('UPDATE users SET apikey = :apikey WHERE id = :id', array('apikey' => $apiKey, 'id' => $Auth->id));
    }
    
    $apiUrl = '&key='.$apiKey;
}

/* setup page */
define("PAGE_NAME", t("api_page_name", "API"));
define("PAGE_DESCRIPTION", t("api_meta_description", "Our Free API"));
define("PAGE_KEYWORDS", t("api_meta_keywords", "api, application, programming, interface, short, url, site"));

require_once('_header.php');
?>

<div class="contentPageWrapper">

    <!-- main section -->
    <div class="pageSectionMain ui-corner-all">
        <div class="pageSectionMainInternal">
            <div id="pageHeader">
                <h2><?php echo t("api"); ?></h2>
            </div>
            <div>
                <p class="introText">
                    <?php
                    $apiContent = t("api_popup_content");
                    $apiContent = str_replace("%%SITE_URL%%", "http://" . _CONFIG_SITE_FULL_URL, $apiContent);
                    $apiContent = str_replace("%%API_URL%%", $apiUrl, $apiContent);
                    echo $apiContent;
                    ?>
                </p>
                <div class="clear"></div>
            </div>
        </div>
    </div>
    <?php include_once("_bannerRightContent.inc.php"); ?>
    <div class="clear"><!-- --></div>
</div>

<?php
require_once('_footer.php');
?>