Plugin Directory

Changeset 443624


Ignore:
Timestamp:
09/26/2011 12:23:53 PM (15 years ago)
Author:
michael.bartel
Message:

RSS Feed

Location:
simpleticker
Files:
2 edited
4 copied

Legend:

Unmodified
Added
Removed
  • simpleticker/tags/0.5/SimpleTicker.php

    r442051 r443624  
    44Plugin URI: http://mbartel.ghulbus.eu/SimpleTicker/
    55Description: Simple news ticker with multiple input possiblities
    6 Version: 0.3
     6Version: 0.5
    77Author: Michael Bartel
    88Author URI: http://facebook.com/bartel.michael/
     
    1010*/
    1111
     12include 'Template.class.php';
    1213$simpleTickerVersion = "0.2";
    1314
     
    2122
    2223/*
    23  * handle AJAX calls
     24 * handle AJAX requests
    2425 */
    2526if ($_GET['action'] == 'getTickerDetails') {
    26     /*
    27      * Return detailed information about a specific ticker (defined by it's it)
    28      */
    29     $query = "SELECT updateInterval, messageTimeout, messageCount, tickerTimeout FROM " . $wpdb->prefix . "SimpleTicker WHERE id='" . mysql_escape_string($_GET['id']) . "'";
    30     $queryResult = $wpdb->get_row($query);
    31 
    32     $result = array();
    33     $result['updateInterval'] = $queryResult->updateInterval * 60;
    34     $result['messageTimeout'] = $queryResult->messageTimeout;
    35     $result['messageCount'] = $queryResult->messageCount;
    36     $result['tickerTimeout'] = $queryResult->tickerTimeout * 60;
    37 
    38     echo json_encode($result);
     27    echo json_encode(getAJAXSimpleTickerDetails($_GET['id']));
    3928    die();
    4029}
    4130if ($_GET['action'] == 'getTickerMessages') {
    42     /*
    43      * Return all currently 'active' messages (messages, where the timeout hasn't been exceeded) for a ticker with a specific id
    44      */
    45     $query = "SELECT message FROM " . $wpdb->prefix . "SimpleTickerMsgs WHERE " .
    46             "id_SimpleTicker='" . mysql_escape_string($_GET['id']) . "' AND createdOn > '" . date("Y-m-d h:i:s", time() - mysql_escape_string($_GET['timeout']) * 60) . "' " .
    47             "ORDER BY createdOn DESC LIMIT " . mysql_escape_string($_GET['count']);
    48     $messages = $wpdb->get_results($query);
    49 
    50     $result = array();
    51     foreach ($messages as $message) {
    52         $result[] = $message->message;
    53     }
    54 
    55     echo json_encode($result);
    56     die();
    57 }
    58 
    59 if ($_GET['action'] == 'JSONAPI') {
    60     handleSimpleTickerJSONAPIRequest($_GET['data']);
    61 }
    62 
     31    echo json_encode(getAJAXSimpleTickerMessages($_GET['id'], $_GET['count'], $_GET['timeout']));
     32    die();
     33}
     34
     35/*
     36 * If no ticker ID is given by the id GET parameter, try to get the ID with the name parameter
     37 */
     38$id_SimpelTicker = isset($_GET['id']) && $_GET['id'] != '' ? $_GET['id'] : getSimpleTickerIDByName($_GET['name']);
     39
     40/*
     41 * Handle JSON requests
     42 */
     43if ($_GET['action'] == 'jsonGetTickerList') {
     44    echo json_encode(getSimpleTickerList());
     45    die();
     46}
     47if ($_GET['action'] == 'jsonGetTickerMessages') {
     48    echo json_encode(getSimpleTickerMessages($id_SimpelTicker));
     49    die();
     50}
     51if ($_GET['action'] == 'jsonManageTicker') {
     52    manageSimpleTicker($id_SimpelTicker, $_GET['data']);
     53}
     54
     55/*
     56 * Handle XML request
     57 */
     58if ($_GET['action'] == 'xmlGetTickerList') {
     59    echo getXMLSimpleTickerList();
     60    die();
     61}
     62if ($_GET['action'] == 'xmlGetTickerMessages') {
     63    echo getXMLSimpleTickerMessages($id_SimpelTicker);
     64    die();
     65}
     66
     67/*
     68 * RSS Feed
     69 */
     70if ($_GET['action'] == "rssFeed") {
     71    header("Content-Type: application/rss+xml");
     72    echo getSimpleTickerRSSFeed($id_SimpelTicker);
     73    die();
     74}
    6375
    6476/*
     
    218230     * print the list of tickers
    219231     */
    220     $code .= "<form action=\"#\" method=\"post\">\n";
    221     $code .= "<table width='100%'>\n";
    222     $code .= "    <thead>\n";
    223     $code .= "        <tr>\n";
    224     $code .= "            <th>ID</th>\n";
    225     $code .= "            <th>Name</th>\n";
    226     $code .= "            <th>Ticker updates (every x minutes)</th>\n";
    227     $code .= "            <th>Message fades (after x secondes)</th>\n";
    228     $code .= "            <th>Number of fading messages</th>\n";
    229     $code .= "            <th>Show no messages created x minutes ago</th>\n";
    230     $code .= "            <th>Password</th>";
    231     $code .= "            <th>Delete</th>";
    232     $code .= "        </tr>\n";
    233     $code .= "    </thead>\n";
    234     $code .= "    <tbody>\n";
     232    $adminPage = new Template('adminpage.tpl');
     233    $adminPage->assign('pluginpath', plugins_url('', __FILE__));
    235234
    236235    $tickers = $wpdb->get_results("SELECT * FROM " . $wpdb->prefix . "SimpleTicker");
    237236    $tickerList = array();
     237    $tickerListArr = array();
    238238    foreach ($tickers as $ticker) {
    239239        $tickerList[$ticker->id] = $ticker->name;
    240         $code .= "        <tr>\n";
    241         $code .= "            <td align='center'>" . $ticker->id . "</td>";
    242         $code .= "            <td align='center'><input type=\"text\" name=\"ticker[" . $ticker->id . "][name]\" value=\"" . $ticker->name . "\" /></td>";
    243         $code .= "            <td align='center'><input type=\"text\" name=\"ticker[" . $ticker->id . "][updateInterval]\" value=\"" . $ticker->updateInterval . "\" /></td>";
    244         $code .= "            <td align='center'><input type=\"text\" name=\"ticker[" . $ticker->id . "][messageTimeout]\" value=\"" . $ticker->messageTimeout . "\" /></td>";
    245         $code .= "            <td align='center'><input type=\"text\" name=\"ticker[" . $ticker->id . "][messageCount]\" value=\"" . $ticker->messageCount . "\" /></td>";
    246         $code .= "            <td align='center'><input type=\"text\" name=\"ticker[" . $ticker->id . "][tickerTimeout]\" value=\"" . $ticker->tickerTimeout . "\" /></td>";
    247         $code .= "            <td align='center'><input type=\"text\" name=\"ticker[" . $ticker->id . "][passwd]\" value=\"\" /></td>";
    248         $code .= "            <td align='center'><input type=\"checkbox\" name=\"ticker[" . $ticker->id . "][delete]\" value=\"1\" /></td>";
    249         $code .= "        </tr>\n";
    250     }
    251 
    252     $code .= "    </tbody>\n";
    253     $code .= "    <tfooter>\n";
    254     $code .= "        <tr>\n";
    255     $code .= "            <td></td>";
    256     $code .= "            <td align='center'><input type=\"text\" name=\"ticker[new][name]\"/></td>";
    257     $code .= "            <td align='center'><input type=\"text\" name=\"ticker[new][updateInterval]\"/></td>";
    258     $code .= "            <td align='center'><input type=\"text\" name=\"ticker[new][messageTimeout]\"/></td>";
    259     $code .= "            <td align='center'><input type=\"text\" name=\"ticker[new][messageCount]\"/></td>";
    260     $code .= "            <td align='center'><input type=\"text\" name=\"ticker[new][tickerTimeout]\"/></td>";
    261     $code .= "            <td align='center'><input type=\"text\" name=\"ticker[new][passwd]\"/></td>";
    262     $code .= "            <td></td>";
    263     $code .= "        </tr>\n";
    264     $code .= "    </tfooter>\n";
    265     $code .= "</table>\n";
    266     $code .= "<p align=\"center\"><input type=\"submit\" value=\"Update\" /></p></form>";
    267 
    268     /*
    269      * print list of messages
    270      */
    271     $code .= "<form action=\"#\" method=\"post\">\n";
    272     $code .= "<table width='100%'>\n";
    273     $code .= "    <thead>\n";
    274     $code .= "        <tr>\n";
    275     $code .= "            <th>Ticker</th>\n";
    276     $code .= "            <th>Message</th>\n";
    277     $code .= "            <th>Created</th>\n";
    278     $code .= "            <th>Delete</th>";
    279     $code .= "        </tr>\n";
    280     $code .= "    </thead>\n";
    281     $code .= "    <tbody>\n";
     240        $tmp = array();
     241        $tmp['id'] = $ticker->id;
     242        $tmp['name'] = $ticker->name;
     243        $tmp['updateInterval'] = $ticker->updateInterval;
     244        $tmp['messageTimeout'] = $ticker->messageTimeout;
     245        $tmp['messageCount'] = $ticker->messageCount;
     246        $tmp['tickerTimeout'] = $ticker->tickerTimeout;
     247        $tickerListArr[] = $tmp;
     248    }
     249    $adminPage->assignBlock('tickerlist', $tickerListArr);
     250
    282251
    283252    $messages = $wpdb->get_results("SELECT " . $wpdb->prefix . "SimpleTickerMsgs.id AS tickerId, id_SimpleTicker, name, message, createdOn FROM " .
     
    285254                                        $wpdb->prefix . "SimpleTicker ON " . $wpdb->prefix . "SimpleTickerMsgs.id_SimpleTicker = " .
    286255                                        $wpdb->prefix . "SimpleTicker.id ORDER BY createdOn DESC LIMIT 50");
     256    $messagesArr = array();
    287257    foreach ($messages as $message) {
    288         $code .= "        <tr>\n";
    289         $code .= "            <td align='center'>" . $message->name . "</td>";
    290         $code .= "            <td align='left'>" . $message->message . "</td>";
    291         $code .= "            <td align='center'>" . $message->createdOn . "</td>";
    292         $code .= "            <td align='center'><input type=\"checkbox\" name=\"msg[" . $message->tickerId . "][delete]\" value=\"1\" /></td>";
    293         $code .= "        </tr>\n";
    294     }
    295    
    296     $code .= "    </tbody>\n";
    297     $code .= "    <tfooter>\n";
    298     $code .= "        <tr><td colspan=\"4\">&nbsp;</td></tr>\n";
    299     $code .= "        <tr>\n";
    300     $code .= "            <td align='center'><select name=\"msg[new][ticker]\">";
     258        $tmp = array();
     259        $tmp['tickerId'] = $message->tickerId;
     260        $tmp['name'] = $message->name;
     261        $tmp['message'] = $message->message;
     262        $tmp['createdOn'] = $message->createdOn;
     263        $messagesArr[] = $tmp;
     264    }
     265    $adminPage->assignBlock('messagelist', $messagesArr);
     266
     267    $code = '';
    301268    foreach ($tickerList as $key => $value) {
    302269        $code .= "                <option value=\"$key\">$value</option>";
    303270    }
    304     $code .= "            </select></td>";
    305     $code .= "            <td align='center'><input type=\"text\" name=\"msg[new][message]\" size=\"100\"/></td>";
    306     $code .= "            <td align=\"left\"><input type=\"submit\" value=\"Create new message / delete selected messages\"/></td>";
    307     $code .= "            <td></td>";
    308     $code .= "        </tr>\n";
    309     $code .= "    </tfooter>\n";
    310     $code .= "</table>\n";
    311     $code .= "</form>";
    312 
    313     echo $code;
    314 }
    315 
    316 /**
    317  * Handle JSON API requests
    318  * data contains an json string encrypted with the ticker password
    319  * @param $data
    320  */
    321 function handleSimpleTickerJSONAPIRequest($data) {
    322     global $wpdb;
    323     $request = json_decode(base64_decode($data), true);
    324 
    325     switch ($request['command']) {
    326         case 'getTickerList':
    327             $tickers = $wpdb->get_results("SELECT * FROM " . $wpdb->prefix . "SimpleTicker");
    328             $tickerList = array();
    329             foreach ($tickers as $ticker) {
    330                 $tickerList[] = array('id' => $ticker->id, 'name' => $ticker->name);
    331             }
    332             echo json_encode($tickerList);
    333             die();
    334             break;
    335 
    336         case 'getTickerMessages':
    337             $messages = $wpdb->get_results("SELECT id, message, createdOn FROM " .
    338                                                 $wpdb->prefix . "SimpleTickerMsgs WHERE id_SimpleTicker = '" . mysql_escape_string($request['tickerid']) .
    339                                                 "' ORDER BY createdOn DESC LIMIT 50");
    340             $tickerMessages = array();
    341             foreach ($messages as $message) {
    342                 $tickerMessages[] = array('id' => $message->id, 'message' => $message->message, 'createdOn' => $message->createdOn);
    343             }
    344             echo json_encode($tickerMessages);
    345             die();
    346 
    347         case 'manageTickerMessage':
    348             $id_SimpleTicker = $request['tickerid'];
    349 
    350             /*
    351              * Check if the password encrypted with the token matches the password for this ticker stored in the database.
    352              */
    353             $decryptedData = json_decode(base64_decode(decryptSimpleTickerMessage($request['data'])), true);
    354             if ($decryptedData == null) {
    355                 die("NODATACONTENT");
    356             }
    357             $tickerData = $wpdb->get_row("SELECT passwd FROM " . $wpdb->prefix . "SimpleTicker WHERE id='" . mysql_escape_string($id_SimpleTicker) . "'");
    358             if ($decryptedData['passwd'] == $tickerData->passwd) {
    359                 /*
    360                  * Add new messsage
    361                  */
    362                 if ($decryptedData['action'] == 'addMessage') {
    363                     $message = $decryptedData['message'];
    364                     $wpdb->query("INSERT INTO " . $wpdb->prefix . "SimpleTickerMsgs (id_SimpleTicker, message, createdOn) VALUES ('$id_SimpleTicker', '$message', NOW())");
    365                     die("SUCCESS");
    366                 }
    367                 /*
    368                  * Delete existing message
    369                  */
    370                 if ($decryptedData['action'] == 'removeMessage') {
    371                     $id = $decryptedData['id'];
    372                     $wpdb->query("DELETE FROM " . $wpdb->prefix . "SimpleTickerMsgs WHERE id='$id'");
    373                     die("SUCCESS");
    374                 }
    375                 die("NOACTIONDEFINED");
    376             } else {
    377                 die("WRONGPASSWORD");
    378             }
    379             break;
    380 
    381     }
    382 
     271    $adminPage->assign('tickeridlist', $code);
     272   
     273
     274    $adminPage->display();
    383275}
    384276
     
    392284}
    393285
     286/**
     287 * Returns the ticker ID by it's name
     288 * @param <type> $name
     289 */
     290function getSimpleTickerIDByName($name) {
     291    global $wpdb;
     292    $query = "SELECT id FROM " . $wpdb->prefix . "SimpleTicker WHERE name='$name'";
     293    $row = $wpdb->get_Row($query);
     294    return $row->id;
     295}
     296
     297
     298/**
     299 * Return detailed information about a specific ticker (defined by it's ID)
     300 * @global <type> $wpdb
     301 * @param <type> $id_SimpleTicker
     302 * @return <type>
     303 */
     304function getAJAXSimpleTickerDetails($id_SimpleTicker) {
     305    global $wpdb;
     306    $query = "SELECT name, updateInterval, messageTimeout, messageCount, tickerTimeout FROM " . $wpdb->prefix . "SimpleTicker WHERE id='" . mysql_escape_string($id_SimpleTicker) . "'";
     307    $queryResult = $wpdb->get_row($query);
     308
     309    $result = array();
     310    $result['updateInterval'] = $queryResult->updateInterval * 60;
     311    $result['messageTimeout'] = $queryResult->messageTimeout;
     312    $result['messageCount'] = $queryResult->messageCount;
     313    $result['tickerTimeout'] = $queryResult->tickerTimeout * 60;
     314    $result['name'] = $queryResult->name;
     315
     316    return $result;
     317}
     318
     319/**
     320 * Return all currently 'active' messages (messages, where the timeout hasn't been exceeded) for a ticker with a specific ID
     321 * @global <type> $wpdb
     322 * @param <type> $id_SimpleTicker
     323 * @param <type> $messageCount
     324 * @param <type> $messageTimeout
     325 */
     326function getAJAXSimpleTickerMessages($id_SimpleTicker, $messageCount, $messageTimeout) {
     327    global $wpdb;
     328    $query = "SELECT message FROM " . $wpdb->prefix . "SimpleTickerMsgs WHERE " .
     329            "id_SimpleTicker='" . mysql_escape_string($id_SimpleTicker) . "' AND createdOn > '" . date("Y-m-d h:i:s", time() - mysql_escape_string($messageTimeout) * 60) . "' " .
     330            "ORDER BY createdOn DESC LIMIT " . mysql_escape_string($messageCount);
     331    $messages = $wpdb->get_results($query);
     332
     333    $result = array();
     334    foreach ($messages as $message) {
     335        $result[] = $message->message;
     336    }
     337
     338    return $result;
     339}
     340
     341/**
     342 * Returns a list of all ticker (ID and name)
     343 * @global  $wpdb
     344 * @return <type>
     345 */
     346function getSimpleTickerList() {
     347    global $wpdb;
     348    $tickers = $wpdb->get_results("SELECT * FROM " . $wpdb->prefix . "SimpleTicker");
     349    $tickerList = array();
     350    foreach ($tickers as $ticker) {
     351        $tickerList[] = array('id' => $ticker->id, 'name' => $ticker->name);
     352    }
     353    return $tickerList;
     354}
     355
     356/**
     357 * Returns a list with the 50 latest messages for the ticker with the given ticker ID
     358 * @global $wpdb $wpdb
     359 * @param <type> $id_SimpleTicker
     360 * @return <type>
     361 */
     362function getSimpleTickerMessages($id_SimpleTicker) {
     363    global $wpdb;
     364    $messages = $wpdb->get_results("SELECT id, message, createdOn FROM " .
     365                                        $wpdb->prefix . "SimpleTickerMsgs WHERE id_SimpleTicker = '" . mysql_escape_string($id_SimpleTicker) .
     366                                        "' ORDER BY createdOn DESC LIMIT 50");
     367    $tickerMessages = array();
     368    foreach ($messages as $message) {
     369        $uid = substr(str_pad(str_replace(array(' ', ':', '-'), array('', '', ''), $id_SimpleTicker . $message->id . $message->createdOn. $message->message), 32, 'A'), 0, 32);
     370        $tickerMessages[] = array('id' => $message->id, 'message' => $message->message, 'createdOn' => $message->createdOn, 'uid' => $uid);
     371    }
     372
     373    return $tickerMessages;
     374}
     375
     376/**
     377 * Manage tickers with JSON request and encrypted BASE64 data
     378 * @global $wpdb;
     379 * @param <type> $id_SimpleTicker
     380 * @param <type> $data
     381 */
     382function manageSimpleTicker($id_SimpleTicker, $data) {
     383    global $wpdb;
     384    $id_SimpleTicker = mysql_escape_string($id_SimpleTicker);
     385
     386    /*
     387     * Check if the password encrypted with the token matches the password for this ticker stored in the database.
     388     */
     389    $decryptedData = json_decode(decryptSimpleTickerMessage(base64_decode($data)), true);
     390    if ($decryptedData == null) {
     391        die("NODATACONTENT");
     392    }
     393    $tickerData = $wpdb->get_row("SELECT passwd FROM " . $wpdb->prefix . "SimpleTicker WHERE id='" . mysql_escape_string($id_SimpleTicker) . "'");
     394    if ($decryptedData['passwd'] == $tickerData->passwd) {
     395        /*
     396         * Add new messsage
     397         */
     398        if ($decryptedData['action'] == 'addMessage') {
     399            $message = $decryptedData['message'];
     400            $wpdb->query("INSERT INTO " . $wpdb->prefix . "SimpleTickerMsgs (id_SimpleTicker, message, createdOn) VALUES ('$id_SimpleTicker', '$message', NOW())");
     401            die("SUCCESS");
     402        }
     403        /*
     404         * Delete existing message
     405         */
     406        if ($decryptedData['action'] == 'removeMessage') {
     407            $id = $decryptedData['id'];
     408            $wpdb->query("DELETE FROM " . $wpdb->prefix . "SimpleTickerMsgs WHERE id='$id'");
     409            die("SUCCESS");
     410        }
     411        die("NOACTIONDEFINED");
     412    } else {
     413        die("WRONGPASSWORD");
     414    }
     415}
     416
     417/**
     418 * Returns an XML string representation of getSimpleTickerList
     419 * @return string
     420 */
     421function getXMLSimpleTickerList() {
     422    $tickers = getTickerList();
     423
     424    $tickerStr = "<tickerlist>\n";
     425    foreach ($tickers as $ticker) {
     426        $tickerStr .= "\t<ticker>\n\t\t<id>" . $ticker['id'] . "</id>\n\t\t<name>" . $ticker['name'] . "</name>\n\t</ticker>\n";
     427    }
     428    $tickerStr .= "</tickerlist>";
     429
     430    return $tickerStr;
     431}
     432
     433/**
     434 * Returns an XML string representation of getSimpleTickerMessages
     435 * @param <type> $id_SimpleTicker
     436 * @return string
     437 */
     438function getXMLSimpleTickerMessages($id_SimpleTicker) {
     439    $messages = getSimpleTickerMessages($id_SimpleTicker);
     440
     441    $tickerMessages = "<tickermessages>\n";
     442    foreach ($messages as $message) {
     443        $tickerMessages .= "\t<message>\n\t\t<id>" . $message['id'] . "</id>\n\t\t<message>" . $message['message'] .
     444                            "</message>\n\t\t<createdOn>" . $message['createdOn'] . "</createdOn>\n\t</message>\n";
     445    }
     446    $tickerMessages .= "</tickermessages>";
     447
     448    return $tickerMessages;
     449}
     450
     451/**
     452 * Create string content for the RSS Feed
     453 * @param <type> $id_SimpleTicker
     454 */
     455function getSimpleTickerRSSFeed($id_SimpleTicker) {
     456    $tickerInfo = getAJAXSimpleTickerDetails($id_SimpleTicker);
     457    $messages = getSimpleTickerMessages($id_SimpleTicker);
     458
     459    $feed = new Template('rssFeed.tpl');
     460    $feed->assign('name', $tickerInfo['name']);
     461    $feed->assignBlock('messages', $messages);
     462
     463    $lastMsg = $messages[0];
     464
     465    $feed->assign('lastCreatedOn', $lastMsg['createdOn']);
     466    $feed->assign('currentTimestamp', date("Y-m-d h:i:s", time()));
     467
     468    return $feed->fetch();
     469}
     470
    394471?>
  • simpleticker/tags/0.5/readme.txt

    r442062 r443624  
    44Tags: news,ticker,newsticker,textticker,text,fader,scroller,facebook
    55Requires at least: 2.6
    6 Tested up to: 3.1
    7 Stable tag: 0.3
     6Tested up to: 3.2.1
     7Stable tag: 0.5
    88
    99== Description ==
     
    1515If there are no messages to display, then the ticker turns itself invisible.
    1616
     17Every ticker has it's own RSS Feed, which can be received by either given it's ID or name.
     18
    1719If you want to use the SimpleTicker data from an other application such as an iPhone or Android App, you can
    18 get all ticker data and messages via an JSON based API. It is also possible to add and delete messages. Your
    19 application will need a password for each ticker, if it want's to add or delete messages.
     20get all ticker data and messages via an JSON or XML based API. It is also possible to add and delete messages
     21with the JSON API. Your application will need a password for each ticker, if it want's to add or delete messages.
    2022
    2123== Copyright ==
     
    2426eMail: Michael.Bartel@gmx.net
    2527
    26 == History ===
     28== History ==
     29Version 0.5
     30 - added RSS Feed
     31
     32Version 0.4
     33 - added Template-Engine and XML API
     34
    2735Version 0.3
    2836 - added JSON API
     37
    2938Version 0.2
    3039 - added auto-hide
     40
    3141Version 0.1
    3242 - first version V3.1
     
    4454Example for ticker 1 (with id 1): [simpleticker id=1]
    4555
     56== RSS Feed ==
     57You will receive the content of the RSS Feed for a specific ticker when you call SimpleTicker.php from your plugin folder (URL) and append '?action=rssFeed&id=1'. Instead of id=1 you can use name=Tickername.
     58
     59== APIs ==
     60All APIs are handled with GET paramters. The 'action' parameter specifys which function you want to call.
     61
    4662== JSON API ==
    47 Call the SimpleTicker script with the "action" GET parameter with value JSONAPI (SimpleTicker.php?action=JSONAPI&data=BASE64CODEDSTRING).
    48 All other data is delivered the "data" GET parameter. This "data" parameter contains a BASE64 encoded JSON string.
     63Instead of the using the id parameter with the tickers ID, you can use the name parameter with it's name. The JSON API provides the following functionalities:
    4964
    50 The data-JSON string contains a "command" value, which defines the function you want to call. The following functions are available:
     65* jsonGetTickerList - Returns a full list of all available tickers containing the tickers id and name.
     66* jsonGetTickerMessages - Returns a list with the last 50 messages of a ticker. You have to specify the ticker by giving it's ID in the parameter 'id'. The list contains the message id, the message itself and the createdOn timestamp.
     67* jsonManageTicker - You need a password to call the action. All further parameters are given in a BASE64 encoded encrypted JSON string provided as GET parameter named 'data'. You have to set the 'id' parameter as above to define a ticker. The JSON string contains an action attribute which can either be 'addMessage' or 'removeMessage'. The 'addMessage' actions takes an additional 'message' attribute containing the new message and the 'removeMessage' action takes an 'id' attribute.
    5168
    52 - getTickerList: no further parameter needed
    53 - getTickerMessages: "tickerid" must contain the ID of the ticker you want to get the messages from
    54 - manageTickerMessage: if you choose this function you have to give another BASE64 JSON data parameter. This parameter must be encrypted with the password for that ticker. The ticker is defined by the tickerid parameter as above.
     69== XML API ==
     70This is not really an API. It just supports the same information as delivered by the JSON API in XML format (expect the availability to mange tickers).
    5571
    56 Just have a look at the end of the SimpleTicker.php file. The code is very simple and only a few lines long.
     72* xmlGetTickerList - Returns a full list of all available tickers containing the tickers id and name.
     73* xmlGetTickerMessages - Returns a list with the last 50 messages of a ticker. You have to specify the ticker by giving it's ID in the parameter 'id'. The list contains the message id, the message itself and the createdOn timestamp.
  • simpleticker/trunk/SimpleTicker.php

    r442976 r443624  
    44Plugin URI: http://mbartel.ghulbus.eu/SimpleTicker/
    55Description: Simple news ticker with multiple input possiblities
    6 Version: 0.4
     6Version: 0.5
    77Author: Michael Bartel
    88Author URI: http://facebook.com/bartel.michael/
     
    2525 */
    2626if ($_GET['action'] == 'getTickerDetails') {
    27     /*
    28      * Return detailed information about a specific ticker (defined by it's it)
    29      */
    30     $query = "SELECT updateInterval, messageTimeout, messageCount, tickerTimeout FROM " . $wpdb->prefix . "SimpleTicker WHERE id='" . mysql_escape_string($_GET['id']) . "'";
    31     $queryResult = $wpdb->get_row($query);
    32 
    33     $result = array();
    34     $result['updateInterval'] = $queryResult->updateInterval * 60;
    35     $result['messageTimeout'] = $queryResult->messageTimeout;
    36     $result['messageCount'] = $queryResult->messageCount;
    37     $result['tickerTimeout'] = $queryResult->tickerTimeout * 60;
    38 
    39     echo json_encode($result);
     27    echo json_encode(getAJAXSimpleTickerDetails($_GET['id']));
    4028    die();
    4129}
    4230if ($_GET['action'] == 'getTickerMessages') {
    43     /*
    44      * Return all currently 'active' messages (messages, where the timeout hasn't been exceeded) for a ticker with a specific id
    45      */
    46     $query = "SELECT message FROM " . $wpdb->prefix . "SimpleTickerMsgs WHERE " .
    47             "id_SimpleTicker='" . mysql_escape_string($_GET['id']) . "' AND createdOn > '" . date("Y-m-d h:i:s", time() - mysql_escape_string($_GET['timeout']) * 60) . "' " .
    48             "ORDER BY createdOn DESC LIMIT " . mysql_escape_string($_GET['count']);
    49     $messages = $wpdb->get_results($query);
    50 
    51     $result = array();
    52     foreach ($messages as $message) {
    53         $result[] = $message->message;
    54     }
    55 
    56     echo json_encode($result);
    57     die();
    58 }
     31    echo json_encode(getAJAXSimpleTickerMessages($_GET['id'], $_GET['count'], $_GET['timeout']));
     32    die();
     33}
     34
     35/*
     36 * If no ticker ID is given by the id GET parameter, try to get the ID with the name parameter
     37 */
     38$id_SimpelTicker = isset($_GET['id']) && $_GET['id'] != '' ? $_GET['id'] : getSimpleTickerIDByName($_GET['name']);
    5939
    6040/*
     
    6242 */
    6343if ($_GET['action'] == 'jsonGetTickerList') {
    64     $tickers = $wpdb->get_results("SELECT * FROM " . $wpdb->prefix . "SimpleTicker");
    65     $tickerList = array();
    66     foreach ($tickers as $ticker) {
    67         $tickerList[] = array('id' => $ticker->id, 'name' => $ticker->name);
    68     }
    69     echo json_encode($tickerList);
     44    echo json_encode(getSimpleTickerList());
    7045    die();
    7146}
    7247if ($_GET['action'] == 'jsonGetTickerMessages') {
    73     $messages = $wpdb->get_results("SELECT id, message, createdOn FROM " .
    74                                         $wpdb->prefix . "SimpleTickerMsgs WHERE id_SimpleTicker = '" . mysql_escape_string($_GET['tickerid']) .
    75                                         "' ORDER BY createdOn DESC LIMIT 50");
    76     $tickerMessages = array();
    77     foreach ($messages as $message) {
    78         $tickerMessages[] = array('id' => $message->id, 'message' => $message->message, 'createdOn' => $message->createdOn);
    79     }
    80     echo json_encode($tickerMessages);
     48    echo json_encode(getSimpleTickerMessages($id_SimpelTicker));
    8149    die();
    8250}
    8351if ($_GET['action'] == 'jsonManageTicker') {
    84     $id_SimpleTicker = mysql_escape_string($_GET['tickerid']);
    85 
    86     /*
    87      * Check if the password encrypted with the token matches the password for this ticker stored in the database.
    88      */
    89     $decryptedData = json_decode(decryptSimpleTickerMessage(base64_decode($_GET['data'])), true);
    90     if ($decryptedData == null) {
    91         die("NODATACONTENT");
    92     }
    93     $tickerData = $wpdb->get_row("SELECT passwd FROM " . $wpdb->prefix . "SimpleTicker WHERE id='" . mysql_escape_string($id_SimpleTicker) . "'");
    94     if ($decryptedData['passwd'] == $tickerData->passwd) {
    95         /*
    96          * Add new messsage
    97          */
    98         if ($decryptedData['action'] == 'addMessage') {
    99             $message = $decryptedData['message'];
    100             $wpdb->query("INSERT INTO " . $wpdb->prefix . "SimpleTickerMsgs (id_SimpleTicker, message, createdOn) VALUES ('$id_SimpleTicker', '$message', NOW())");
    101             die("SUCCESS");
    102         }
    103         /*
    104          * Delete existing message
    105          */
    106         if ($decryptedData['action'] == 'removeMessage') {
    107             $id = $decryptedData['id'];
    108             $wpdb->query("DELETE FROM " . $wpdb->prefix . "SimpleTickerMsgs WHERE id='$id'");
    109             die("SUCCESS");
    110         }
    111         die("NOACTIONDEFINED");
    112     } else {
    113         die("WRONGPASSWORD");
    114     }
     52    manageSimpleTicker($id_SimpelTicker, $_GET['data']);
     53}
     54
     55/*
     56 * Handle XML request
     57 */
     58if ($_GET['action'] == 'xmlGetTickerList') {
     59    echo getXMLSimpleTickerList();
     60    die();
     61}
     62if ($_GET['action'] == 'xmlGetTickerMessages') {
     63    echo getXMLSimpleTickerMessages($id_SimpelTicker);
     64    die();
     65}
     66
     67/*
     68 * RSS Feed
     69 */
     70if ($_GET['action'] == "rssFeed") {
     71    header("Content-Type: application/rss+xml");
     72    echo getSimpleTickerRSSFeed($id_SimpelTicker);
     73    die();
    11574}
    11675
     
    305264     */
    306265    $adminPage = new Template('adminpage.tpl');
     266    $adminPage->assign('pluginpath', plugins_url('', __FILE__));
    307267
    308268    $tickers = $wpdb->get_results("SELECT * FROM " . $wpdb->prefix . "SimpleTicker");
     
    357317}
    358318
     319/**
     320 * Returns the ticker ID by it's name
     321 * @param <type> $name
     322 */
     323function getSimpleTickerIDByName($name) {
     324    global $wpdb;
     325    $query = "SELECT id FROM " . $wpdb->prefix . "SimpleTicker WHERE name='$name'";
     326    $row = $wpdb->get_Row($query);
     327    return $row->id;
     328}
     329
     330
     331/**
     332 * Return detailed information about a specific ticker (defined by it's ID)
     333 * @global <type> $wpdb
     334 * @param <type> $id_SimpleTicker
     335 * @return <type>
     336 */
     337function getAJAXSimpleTickerDetails($id_SimpleTicker) {
     338    global $wpdb;
     339    $query = "SELECT name, updateInterval, messageTimeout, messageCount, tickerTimeout FROM " . $wpdb->prefix . "SimpleTicker WHERE id='" . mysql_escape_string($id_SimpleTicker) . "'";
     340    $queryResult = $wpdb->get_row($query);
     341
     342    $result = array();
     343    $result['updateInterval'] = $queryResult->updateInterval * 60;
     344    $result['messageTimeout'] = $queryResult->messageTimeout;
     345    $result['messageCount'] = $queryResult->messageCount;
     346    $result['tickerTimeout'] = $queryResult->tickerTimeout * 60;
     347    $result['name'] = $queryResult->name;
     348
     349    return $result;
     350}
     351
     352/**
     353 * Return all currently 'active' messages (messages, where the timeout hasn't been exceeded) for a ticker with a specific ID
     354 * @global <type> $wpdb
     355 * @param <type> $id_SimpleTicker
     356 * @param <type> $messageCount
     357 * @param <type> $messageTimeout
     358 */
     359function getAJAXSimpleTickerMessages($id_SimpleTicker, $messageCount, $messageTimeout) {
     360    global $wpdb;
     361    $query = "SELECT message FROM " . $wpdb->prefix . "SimpleTickerMsgs WHERE " .
     362            "id_SimpleTicker='" . mysql_escape_string($id_SimpleTicker) . "' AND createdOn > '" . date("Y-m-d h:i:s", time() - mysql_escape_string($messageTimeout) * 60) . "' " .
     363            "ORDER BY createdOn DESC LIMIT " . mysql_escape_string($messageCount);
     364    $messages = $wpdb->get_results($query);
     365
     366    $result = array();
     367    foreach ($messages as $message) {
     368        $result[] = $message->message;
     369    }
     370
     371    return $result;
     372}
     373
     374/**
     375 * Returns a list of all ticker (ID and name)
     376 * @global  $wpdb
     377 * @return <type>
     378 */
     379function getSimpleTickerList() {
     380    global $wpdb;
     381    $tickers = $wpdb->get_results("SELECT * FROM " . $wpdb->prefix . "SimpleTicker");
     382    $tickerList = array();
     383    foreach ($tickers as $ticker) {
     384        $tickerList[] = array('id' => $ticker->id, 'name' => $ticker->name);
     385    }
     386    return $tickerList;
     387}
     388
     389/**
     390 * Returns a list with the 50 latest messages for the ticker with the given ticker ID
     391 * @global $wpdb $wpdb
     392 * @param <type> $id_SimpleTicker
     393 * @return <type>
     394 */
     395function getSimpleTickerMessages($id_SimpleTicker) {
     396    global $wpdb;
     397    $messages = $wpdb->get_results("SELECT id, message, createdOn FROM " .
     398                                        $wpdb->prefix . "SimpleTickerMsgs WHERE id_SimpleTicker = '" . mysql_escape_string($id_SimpleTicker) .
     399                                        "' ORDER BY createdOn DESC LIMIT 50");
     400    $tickerMessages = array();
     401    foreach ($messages as $message) {
     402        $uid = substr(str_pad(str_replace(array(' ', ':', '-'), array('', '', ''), $id_SimpleTicker . $message->id . $message->createdOn. $message->message), 32, 'A'), 0, 32);
     403        $tickerMessages[] = array('id' => $message->id, 'message' => $message->message, 'createdOn' => $message->createdOn, 'uid' => $uid);
     404    }
     405
     406    return $tickerMessages;
     407}
     408
     409/**
     410 * Manage tickers with JSON request and encrypted BASE64 data
     411 * @global $wpdb;
     412 * @param <type> $id_SimpleTicker
     413 * @param <type> $data
     414 */
     415function manageSimpleTicker($id_SimpleTicker, $data) {
     416    global $wpdb;
     417    $id_SimpleTicker = mysql_escape_string($id_SimpleTicker);
     418
     419    /*
     420     * Check if the password encrypted with the token matches the password for this ticker stored in the database.
     421     */
     422    $decryptedData = json_decode(decryptSimpleTickerMessage(base64_decode($data)), true);
     423    if ($decryptedData == null) {
     424        die("NODATACONTENT");
     425    }
     426    $tickerData = $wpdb->get_row("SELECT passwd FROM " . $wpdb->prefix . "SimpleTicker WHERE id='" . mysql_escape_string($id_SimpleTicker) . "'");
     427    if ($decryptedData['passwd'] == $tickerData->passwd) {
     428        /*
     429         * Add new messsage
     430         */
     431        if ($decryptedData['action'] == 'addMessage') {
     432            $message = $decryptedData['message'];
     433            $wpdb->query("INSERT INTO " . $wpdb->prefix . "SimpleTickerMsgs (id_SimpleTicker, message, createdOn) VALUES ('$id_SimpleTicker', '$message', NOW())");
     434            die("SUCCESS");
     435        }
     436        /*
     437         * Delete existing message
     438         */
     439        if ($decryptedData['action'] == 'removeMessage') {
     440            $id = $decryptedData['id'];
     441            $wpdb->query("DELETE FROM " . $wpdb->prefix . "SimpleTickerMsgs WHERE id='$id'");
     442            die("SUCCESS");
     443        }
     444        die("NOACTIONDEFINED");
     445    } else {
     446        die("WRONGPASSWORD");
     447    }
     448}
     449
     450/**
     451 * Returns an XML string representation of getSimpleTickerList
     452 * @return string
     453 */
     454function getXMLSimpleTickerList() {
     455    $tickers = getTickerList();
     456
     457    $tickerStr = "<tickerlist>\n";
     458    foreach ($tickers as $ticker) {
     459        $tickerStr .= "\t<ticker>\n\t\t<id>" . $ticker['id'] . "</id>\n\t\t<name>" . $ticker['name'] . "</name>\n\t</ticker>\n";
     460    }
     461    $tickerStr .= "</tickerlist>";
     462
     463    return $tickerStr;
     464}
     465
     466/**
     467 * Returns an XML string representation of getSimpleTickerMessages
     468 * @param <type> $id_SimpleTicker
     469 * @return string
     470 */
     471function getXMLSimpleTickerMessages($id_SimpleTicker) {
     472    $messages = getSimpleTickerMessages($id_SimpleTicker);
     473
     474    $tickerMessages = "<tickermessages>\n";
     475    foreach ($messages as $message) {
     476        $tickerMessages .= "\t<message>\n\t\t<id>" . $message['id'] . "</id>\n\t\t<message>" . $message['message'] .
     477                            "</message>\n\t\t<createdOn>" . $message['createdOn'] . "</createdOn>\n\t</message>\n";
     478    }
     479    $tickerMessages .= "</tickermessages>";
     480
     481    return $tickerMessages;
     482}
     483
     484/**
     485 * Create string content for the RSS Feed
     486 * @param <type> $id_SimpleTicker
     487 */
     488function getSimpleTickerRSSFeed($id_SimpleTicker) {
     489    $tickerInfo = getAJAXSimpleTickerDetails($id_SimpleTicker);
     490    $messages = getSimpleTickerMessages($id_SimpleTicker);
     491
     492    $feed = new Template('rssFeed.tpl');
     493    $feed->assign('name', $tickerInfo['name']);
     494    $feed->assignBlock('messages', $messages);
     495
     496    $lastMsg = $messages[0];
     497
     498    $feed->assign('lastCreatedOn', $lastMsg['createdOn']);
     499    $feed->assign('currentTimestamp', date("Y-m-d h:i:s", time()));
     500
     501    return $feed->fetch();
     502}
     503
    359504?>
  • simpleticker/trunk/readme.txt

    r442976 r443624  
    55Requires at least: 2.6
    66Tested up to: 3.2.1
    7 Stable tag: 0.4
     7Stable tag: 0.5
    88
    99== Description ==
     
    1414creation timestamp. You can tell the ticker only to show messages not older than a defined number of minutes.
    1515If there are no messages to display, then the ticker turns itself invisible.
     16
     17Every ticker has it's own RSS Feed, which can be received by either given it's ID or name.
    1618
    1719If you want to use the SimpleTicker data from an other application such as an iPhone or Android App, you can
     
    2527
    2628== History ==
     29Version 0.5
     30 - added RSS Feed
     31
    2732Version 0.4
    2833 - added Template-Engine and XML API
     
    4954Example for ticker 1 (with id 1): [simpleticker id=1]
    5055
     56== RSS Feed ==
     57You will receive the content of the RSS Feed for a specific ticker when you call SimpleTicker.php from your plugin folder (URL) and append '?action=rssFeed&id=1'. Instead of id=1 you can use name=Tickername.
     58
    5159== APIs ==
    5260All APIs are handled with GET paramters. The 'action' parameter specifys which function you want to call.
    5361
    5462== JSON API ==
    55 The JSON API provides the following functionalities:
     63Instead of the using the id parameter with the tickers ID, you can use the name parameter with it's name. The JSON API provides the following functionalities:
    5664
    5765* jsonGetTickerList - Returns a full list of all available tickers containing the tickers id and name.
    58 * jsonGetTickerMessages - Returns a list with the last 50 messages of a ticker. You have to specify the ticker by giving it's ID in the parameter 'tickerid'. The list contains the message id, the message itself and the createdOn timestamp.
    59 * jsonManageTicker - You need a password to call the action. All further parameters are given in a BASE64 encoded encrypted JSON string provided as GET parameter named 'data'. You have to set the 'tickerid' parameter as above to define a ticker. The JSON string contains an action attribute which can either be 'addMessage' or 'removeMessage'. The 'addMessage' actions takes an additional 'message' attribute containing the new message and the 'removeMessage' action takes an 'id' attribute.
     66* jsonGetTickerMessages - Returns a list with the last 50 messages of a ticker. You have to specify the ticker by giving it's ID in the parameter 'id'. The list contains the message id, the message itself and the createdOn timestamp.
     67* jsonManageTicker - You need a password to call the action. All further parameters are given in a BASE64 encoded encrypted JSON string provided as GET parameter named 'data'. You have to set the 'id' parameter as above to define a ticker. The JSON string contains an action attribute which can either be 'addMessage' or 'removeMessage'. The 'addMessage' actions takes an additional 'message' attribute containing the new message and the 'removeMessage' action takes an 'id' attribute.
    6068
    6169== XML API ==
    62 This is not really an API. It just supports the same information as delivered by the JSON API in XML format (expect the avaibility to mange tickers)
     70This is not really an API. It just supports the same information as delivered by the JSON API in XML format (expect the availability to mange tickers).
    6371
    6472* xmlGetTickerList - Returns a full list of all available tickers containing the tickers id and name.
    65 * xmlGetTickerMessages - Returns a list with the last 50 messages of a ticker. You have to specify the ticker by giving it's ID in the parameter 'tickerid'. The list contains the message id, the message itself and the createdOn timestamp.
     73* xmlGetTickerMessages - Returns a list with the last 50 messages of a ticker. You have to specify the ticker by giving it's ID in the parameter 'id'. The list contains the message id, the message itself and the createdOn timestamp.
Note: See TracChangeset for help on using the changeset viewer.