Plugin Directory

Changeset 442050


Ignore:
Timestamp:
09/22/2011 12:02:42 PM (15 years ago)
Author:
michael.bartel
Message:

JSON API

Location:
simpleticker/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • simpleticker/trunk/SimpleTicker.php

    r440740 r442050  
    2424 */
    2525if ($_GET['action'] == 'getTickerDetails') {
     26    /*
     27     * Return detailed information about a specific ticker (defined by it's it)
     28     */
    2629    $query = "SELECT updateInterval, messageTimeout, messageCount, tickerTimeout FROM " . $wpdb->prefix . "SimpleTicker WHERE id='" . mysql_escape_string($_GET['id']) . "'";
    2730    $queryResult = $wpdb->get_row($query);
     
    3740}
    3841if ($_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     */
    3945    $query = "SELECT message FROM " . $wpdb->prefix . "SimpleTickerMsgs WHERE " .
    4046            "id_SimpleTicker='" . mysql_escape_string($_GET['id']) . "' AND createdOn > '" . date("Y-m-d h:i:s", time() - mysql_escape_string($_GET['timeout']) * 60) . "' " .
     
    5157}
    5258
    53 
     59if ($_GET['action'] == 'JSONAPI') {
     60    handleSimpleTickerJSONAPIRequest($_GET['data']);
     61}
    5462
    5563
     
    6472
    6573    $sql = "CREATE TABLE " . $wpdb->prefix . "SimpleTicker (
    66           id int(11) NOT NULL AUTO_INCREMENT,
    67           name varchar(100) NOT NULL,
    68           updateInterval INT(11) NOT NULL,
    69           messageTimeout INT(11) NOT NULL,
    70           messageCount INT(11) NOT NULL,
    71           tickerTimeout INT(11) NOT NULL,
     74          `id` INT(11) NOT NULL AUTO_INCREMENT,
     75          `name` VARCHAR(100) NOT NULL,
     76          `updateInterval` INT(11) NOT NULL,
     77          `messageTimeout` INT(11) NOT NULL,
     78          `messageCount` INT(11) NOT NULL,
     79          `tickerTimeout` INT(11) NOT NULL,
     80          `passwd` VARCHAR(250) DEFAULT NULL,
    7281          UNIQUE KEY id (id)
    7382        );";
     
    149158    global $wpdb;
    150159
    151 
    152160    /*
    153161     * update the list of tickers
     
    158166            if ($tickerId == 'new') {
    159167                if ($ticker['name'] != '') {
    160                     $query = "INSERT INTO  " . $wpdb->prefix ."SimpleTicker (name, updateInterval, messageTimeout, messageCount, tickerTimeout) " .
     168                    if ($ticker['passwd'] == '') {
     169                        $query = "INSERT INTO  " . $wpdb->prefix ."SimpleTicker (name, updateInterval, messageTimeout, messageCount, tickerTimeout) " .
    161170                                "VALUES('" . $ticker['name'] . "', '" . $ticker['updateInterval'] . "', '" . $ticker['messageTimeout'] . "', '" .
    162171                                $ticker['messageCount'] . "', '" . $ticker['tickerTimeout'] . "')";
     172                    } else {
     173                        $query = "INSERT INTO  " . $wpdb->prefix ."SimpleTicker (name, updateInterval, messageTimeout, messageCount, tickerTimeout, passwd) " .
     174                                "VALUES('" . $ticker['name'] . "', '" . $ticker['updateInterval'] . "', '" . $ticker['messageTimeout'] . "', '" .
     175                                $ticker['messageCount'] . "', '" . $ticker['tickerTimeout'] . "', '" . md5($ticker['passwd']) . "')";
     176                    }
    163177                    $wpdb->query($query);
    164178                }
     
    166180                continue;
    167181            }
    168             $wpdb->query("UPDATE " . $wpdb->prefix ."SimpleTicker SET name='" . $ticker['name'] . "', updateInterval='" . $ticker['updateInterval'] .
     182            if ($ticker['passwd'] == '') {
     183                $wpdb->query("UPDATE " . $wpdb->prefix ."SimpleTicker SET name='" . $ticker['name'] . "', updateInterval='" . $ticker['updateInterval'] .
    169184                    "', messageTimeout='" . $ticker['messageTimeout'] . "', messageCount='" . $ticker['messageCount'] . "', tickerTimeout='" . $ticker['tickerTimeout'] .
    170185                    "' WHERE id='$tickerId'");
     186            } else {
     187                $wpdb->query("UPDATE " . $wpdb->prefix ."SimpleTicker SET name='" . $ticker['name'] . "', updateInterval='" . $ticker['updateInterval'] .
     188                    "', messageTimeout='" . $ticker['messageTimeout'] . "', messageCount='" . $ticker['messageCount'] . "', tickerTimeout='" . $ticker['tickerTimeout'] .
     189                    "', passwd='" . md5($ticker['passwd']) . "' WHERE id='$tickerId'");
     190            }
    171191
    172192            if ($ticker['delete'] == "1") {
     
    208228    $code .= "            <th>Number of fading messages</th>\n";
    209229    $code .= "            <th>Show no messages created x minutes ago</th>\n";
     230    $code .= "            <th>Password</th>";
    210231    $code .= "            <th>Delete</th>";
    211232    $code .= "        </tr>\n";
     
    224245        $code .= "            <td align='center'><input type=\"text\" name=\"ticker[" . $ticker->id . "][messageCount]\" value=\"" . $ticker->messageCount . "\" /></td>";
    225246        $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>";
    226248        $code .= "            <td align='center'><input type=\"checkbox\" name=\"ticker[" . $ticker->id . "][delete]\" value=\"1\" /></td>";
    227249        $code .= "        </tr>\n";
     
    237259    $code .= "            <td align='center'><input type=\"text\" name=\"ticker[new][messageCount]\"/></td>";
    238260    $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>";
    239262    $code .= "            <td></td>";
    240263    $code .= "        </tr>\n";
     
    291314}
    292315
     316/**
     317 * Handle JSON API requests
     318 * data contains an json string encrypted with the ticker password
     319 * @param $data
     320 */
     321function 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
     383}
     384
     385/**
     386 * Returns the decrypted string
     387 * @param <string> $str
     388 * @return <string>
     389 */
     390function decryptSimpleTickerMessage($str) {
     391    return $str; //TODO implement decryption
     392}
     393
    293394?>
  • simpleticker/trunk/readme.txt

    r440740 r442050  
    88
    99== Description ==
    10 A simple ticker plugin for wordpress. It supports multiple tickers. You can define an update interval in minutes in which the client updates it's message list from the server. This update request includes new messages, which have been posted until the last update. You can also specify the amount of messages that the client fades through and the time each message stays on the screen. Each message is stored with an creation timestamp. You can tell the ticker only to show messages not older than a defined number of minutes. If there are no messages to display, then the ticker turns itself invisible.
     10A simple ticker plugin for wordpress. It supports multiple tickers. You can define an update interval
     11in minutes in which the client updates it's message list from the server. This update request includes
     12new messages, which have been posted until the last update. You can also specify the amount of messages that
     13the client fades through and the time each message stays on the screen. Each message is stored with an
     14creation timestamp. You can tell the ticker only to show messages not older than a defined number of minutes.
     15If there are no messages to display, then the ticker turns itself invisible.
     16
     17If you want to use the SimpleTicker data from an other application such as an iPhone or Android App, you can
     18get all ticker data and messages via an JSON based API. It is also possible to add and delete messages. Your
     19application will need a password for each ticker, if it want's to add or delete messages.
    1120
    1221== Copyright ==
     
    1625
    1726== History ===
     27Version 0.3
     28 - added JSON API
    1829Version 0.2
    1930 - added auto-hide
     
    3243
    3344Example for ticker 1 (with id 1): [simpleticker id=1]
     45
     46== JSON API ==
     47Call the SimpleTicker script with the "action" GET parameter with value JSONAPI (SimpleTicker.php?action=JSONAPI&data=BASE64CODEDSTRING).
     48All other data is delivered the "data" GET parameter. This "data" parameter contains a BASE64 encoded JSON string.
     49
     50The data-JSON string contains a "command" value, which defines the function you want to call. The following functions are available:
     51
     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.
     55
     56Just have a look at the end of the SimpleTicker.php file. The code is very simple and only a few lines long.
Note: See TracChangeset for help on using the changeset viewer.