Icy Phoenix API - Demo »  Show posts from    to     

Icy Phoenix


Documentation And How To - Icy Phoenix API - Demo



Mighty Gorgon [ Mon 17 Feb, 2014 22:20 ]
Post subject: Icy Phoenix API - Demo
Here is a Demo of an API you can use to fetch content within Icy Phoenix.

This demo is just to fetch the content of a topic:

Code: [Hide] [Select]
<?php
/**
*
* @package Icy Phoenix
* @version $Id$
* @copyright (c) 2008 Icy Phoenix
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

define('IN_ICYPHOENIX', true);
if (!defined('IP_ROOT_PATH')) define('IP_ROOT_PATH', './../');
if (!defined('PHP_EXT')) define('PHP_EXT', substr(strrchr(__FILE__, '.'), 1));
include(IP_ROOT_PATH . 'common.' . PHP_EXT);

// Define constant to keep page_header.php from sending headers
define('AJAX_HEADERS', true);

// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup();
// End session management

// Usage examples
/*

Display topic content
mode => topic
http://localhost/ip/api/api.php?mode=topic&topic_id=2

topic_id = topic id

*/

// CLASS API - BEGIN
/**
* Class to provide data to other features/app
*/
class class_api
{

/**
* Class init
*/
function class_api()
{

}

/**
* Get Forums
*/
function get_forums($forums_array)
{
global $db, $cache, $config, $template, $user, $lang;

$sql = "SELECT f.* FROM " . FORUMS_TABLE . " f WHERE " . $db->sql_in_set('f.forum_id', $forums_array);
$result = $db->sql_query($sql, 86400, 'forums_list_', FORUMS_CACHE_FOLDER);
$forums_data = array();
while($row = $db->sql_fetchrow($result))
{
$forums_data[$row['forum_id']] = $row;
}
$db->sql_freeresult($result);

return $forums_data;
}

/**
* Get topic
*/
function get_topic($topic_id)
{
global $db, $cache, $config, $template, $user, $lang;

$sql = "SELECT t.*, p.*, u.*
FROM " . TOPICS_TABLE . " t, " . POSTS_TABLE . " p, " . USERS_TABLE . " u
WHERE t.topic_id = " . $topic_id . "
AND p.post_id = t.topic_first_post_id
AND u.user_id = t.topic_poster
LIMIT 1";
$result = $db->sql_query($sql);
$topic_data = array();
if (!empty($result))
{
$topic_data = $db->sql_fetchrow($result);
}
$db->sql_freeresult($result);

return $topic_data;
}

}

$class_api = new class_api();
// CLASS API - END

// CONFIG - BEGIN
$full_server_url = create_server_url();
$table_fields = array(
'tag_count' => array('lang_key' => 'TAG_COUNT', 'view_level' => AUTH_ALL),
'tag_text' => array('lang_key' => 'TAG_TEXT', 'view_level' => AUTH_ALL),
);
// CONFIG - END

// VARS - BEGIN
$topic_id = request_var('topic_id', 0);

$mode_types = array('main', 'cats', 'cat', 'tags', 'tags_all', 'authors', 'authors_topics', 'topic');
$mode = request_var('mode', $mode_types[0]);
$mode = check_var_value($mode, $mode_types);

$start = request_var('start', 0);
$start = ($start < 0) ? 0 : $start;

$per_page = request_var('per_page', 0);
$per_page = (empty($per_page) || ($per_page < 10) || ($per_page > 200)) ? $config['topics_per_page'] : $per_page;

$s_hidden_fields = '';

// SORT ORDER - BEGIN
$sort_order_array = array();
switch ($mode)
{
case 'authors':
$sort_order_array = array('alpha', 'number');
break;

default:
$sort_order_array = array();
break;
}
$sort_order = request_var('sort_order', $sort_order_array[0]);
$sort_order = (in_array($sort_order, $sort_order_array) ? $sort_order : $sort_order_array[0]);
// SORT ORDER - END

// SORT DIR - BEGIN
$sort_dir = request_var('sort_dir', 'DESC');
$sort_dir = (strtoupper($sort_dir) == 'ASC') ? 'ASC' : 'DESC';
// SORT DIR - END
// VARS - END

// ARRAY BASICS - BEGIN
$result_array = array();

$post_url_empty = $full_server_url . CMS_PAGE_VIEWTOPIC . '?' . POST_POST_URL . '={POST_ID}';
$print_url_empty = $full_server_url . 'printview.' . PHP_EXT . '?' . POST_POST_URL . '={POST_ID}';
$single_post_url_empty = $full_server_url . 'show_post.' . PHP_EXT . '?mob=1&light_view=1&' . POST_POST_URL . '={POST_ID}';
$result_array['url_post'] = $post_url_empty;
$result_array['url_print'] = $print_url_empty;
$result_array['url_single_post'] = $single_post_url_empty;
// ARRAY BASICS - END

if ($mode == 'topic')
{
if (!empty($topic_id))
{
include_once(IP_ROOT_PATH . 'includes/bbcode.' . PHP_EXT);

$topic_data = $class_api->get_topic($topic_id);

$forum_id = $topic_data['forum_id'];
$topic_id = $topic_data['topic_id'];
$post_id = $topic_data['topic_first_post_id'];
$forum_id_append = (!empty($forum_id) ? (POST_FORUM_URL . '=' . $forum_id) : '');
$topic_id_append = (!empty($topic_id) ? (POST_TOPIC_URL . '=' . $topic_id) : '');
$post_id_append = (!empty($post_id) ? (POST_POST_URL . '=' . $post_id) : '');
$views = $topic_data['topic_views'];
$replies = $topic_data['topic_replies'];

$topic_title = $topic_data['topic_title'];
// Convert and clean special chars!
$topic_title = htmlspecialchars_clean($topic_title);

$first_time = create_date_ip($lang['DATE_FORMAT_VF'], $topic_data['topic_time'], $config['board_timezone'], true);
$last_time = create_date_ip($config['default_dateformat'], $topic_data['topic_last_post_time'], $config['board_timezone']);

$forum_url = $full_server_url . CMS_PAGE_VIEWFORUM . '?' . $forum_id_append;
$topic_url = $full_server_url . CMS_PAGE_VIEWTOPIC . '?' . $forum_id_append . '&amp;' . $topic_id_append;
$post_url = $full_server_url . CMS_PAGE_VIEWTOPIC . '?' . $post_id_append;
$single_post_url = $single_post_url_empty . $post_id;

$cat_id = $cis_reverse_cats[$forum_id];
$is_gf = in_array($forum_id, $cats_all_gf) ? true : false;
$topic_img = $full_server_url . '_api/' . $cat_id . ($is_gf ? '_gf' : '') . '.png';
$forum_title = ucwords(str_replace('_', ' ', $cat_id));

$message = $topic_data['post_text'];
$message_compiled = (empty($topic_data['post_text_compiled']) || !empty($config['posts_precompiled'])) ? false : $topic_data['post_text_compiled'];
$message = censor_text($message);

$bbcode->allow_html = (($config['allow_html'] && $topic_data['enable_html']) ? true : false);
$bbcode->allow_bbcode = (($config['allow_bbcode'] && $topic_data['enable_bbcode']) ? true : false);
$bbcode->allow_smilies = (($config['allow_smilies'] && $topic_data['enable_smilies']) ? true : false);

if($message_compiled === false)
{
$bbcode->code_post_id = $topic_data['post_id'];
$message = $bbcode->parse($message);
$bbcode->code_post_id = 0;
if (empty($bbcode->allow_bbcode))
{
$message = str_replace("\n", "<br />", preg_replace("/\r\n/", "\n", $message));
}
}
else
{
$message = $message_compiled;
}

/*
if ($topic_data['enable_autolinks_acronyms'])
{
$message = $bbcode->acronym_pass($message);
$message = $bbcode->autolink_text($message, $forum_id);
}
*/

if (!empty($strip_tags))
{
$allowed_tags = '<p><b><i><u><a><ul><ol><li><h1><h2><h3><h4><h5><h6>';
$message = strip_tags($message, $allowed_tags);
}

$result_array['items'][] = array(
'forum_id' => $forum_id,
'topic_id' => $topic_id,
'post_id' => $post_id,
'gf' => ($is_gf ? 1 : 0),
'category' => $forum_title,
'title' => $topic_title,
'url' => $topic_url,
'single_post_url' => $single_post_url,
'message' => $message,
'thumb' => $topic_img,
'replies' => $replies,
'views' => $views,
'tags' => $topic_data['topic_tags'],
'first_time' => $first_time,
'first_author' => $topic_data['topic_first_poster_name'],
'last_time' => $last_time,
'last_author' => $topic_data['topic_last_poster_name'],
);

$result_array['email_text'] = $api_lang['email_text'];
$result_array['count'] = sizeof($result_array['items']);
}
}

AJAX_headers(true);
echo json_encode($result_array) . "\n\n";
flush();
garbage_collection();
exit_handler();
exit;

?>




Powered by Icy Phoenix