Пример #1
0
<?php

/* This is the php that gets and saves your app token from facebook, enter your appid and secret and
 * run this script in you browser and your app token will get saved to fb_app_token.php . Javascript will do all the actual building and data retrivel.
 * you can pass it ?debug=true if your having trouble
 *@author Lorenzo Gangi lorenzo@ironlasso.com
 *@copyright
 *@version 1.0
 */
get_feed_data();
function get_feed_data()
{
    //$app_id = "YOUR_APP_ID";
    //$app_secret = "YOUR_APP_SECRET";
    $app_id = '520394101470972';
    $app_secret = 'b0c684e274aa3fd26c71ed1064da07c1';
    $access_token = false;
    $debug = isset($_REQUEST['debug']) ? $_REQUEST['debug'] : false;
    $app_token_url = "https://graph.facebook.com/oauth/access_token?" . "client_id=" . $app_id . "&client_secret=" . $app_secret . "&grant_type=client_credentials";
    //DEBUG - check to see if https wrapper is available for php
    if ($debug) {
        $w = stream_get_wrappers();
        echo 'https wrapper: ', in_array('https', $w) ? 'https wrapper is enabled <br />' : 'https wrapper is not enabled. Try uncommenting ;extension=php_openssl.dll in your php.ini or contact your hosting provider or system administrator. <br />';
    }
    //get the app access token
    $response = file_get_contents($app_token_url);
    $params = null;
    parse_str($response, $params);
    $access_token = $params['access_token'];
    //save the app token to a file for later use
    $ourFileName = "fb_app_token.html";
Пример #2
0
/**
* display_feed($params, $tpl_prefix = '')
* $params : array of params or string feed URL for defaults
* tpl_prefix is for using different link blocks on one page
* Use display_feed('http://www.example.com/rss/rss.xml') to use default settings.
* */
function display_feed($params, $tpl_prefix = '')
{
    global $cache, $user, $config, $template, $phpbb_root_path, $phpEx;
    if (is_string($params)) {
        $params = array('url' => $params);
    }
    $_params = array('url' => trim(str_replace('&amp;', '&', $params['url'])), 'slide' => !empty($params['slide']), 'speed' => !empty($params['speed']) ? max((int) $params['speed'], 1) : 30, 'ttl' => !empty($params['ttl']) ? max((int) $params['ttl'], 0) : 3600, 'limit' => !empty($params['limit']) ? max((int) $params['limit'], 1) : 5, 'desc' => !empty($params['desc']), 'html' => !empty($params['html']), 'striptags' => !empty($params['striptags']));
    if (empty($_params['url'])) {
        return;
    }
    $cache_file = '_gym_links_' . md5($user->data['user_lang'] . $_params['url']);
    if (($feed_data = $cache->get($cache_file)) === false) {
        if (!defined('GYM_RSS_FUNC_INC')) {
            require $phpbb_root_path . 'gym_sitemaps/includes/gym_rss_functions.' . $phpEx;
        }
        $feed_data = get_feed_data($_params);
        $cache->put($cache_file, $feed_data, $_params['ttl']);
    }
    if (!empty($feed_data['items'])) {
        $template->assign_vars(array($tpl_prefix . 'GYM_RSS_AGREGATED' => true, $tpl_prefix . 'GYM_CHAN_TITLE' => $feed_data['setup']['chantitle'], $tpl_prefix . 'GYM_CHAN_LINK' => $feed_data['setup']['chanlink'], $tpl_prefix . 'GYM_CHAN_SOURCE' => $_params['url'], $tpl_prefix . 'GYM_RSS_AUTHOR' => false, $tpl_prefix . 'GYM_RSS_DATE' => $feed_data['setup']['date'], $tpl_prefix . 'GYM_RSS_DESC' => $_params['desc'], $tpl_prefix . 'GYM_RSS_SLIDE' => $_params['slide'], $tpl_prefix . 'GYM_RSS_SLIDE_SP' => 'height:' . ($_params['desc'] ? $_params['limit'] * 45 : $_params['limit'] * 20) . 'px;', $tpl_prefix . 'GYM_RSS_SLIDE_SP_JS' => $_params['desc'] ? $_params['limit'] * 45 : $_params['limit'] * 20, $tpl_prefix . 'GYM_RSS_SLIDE_EP' => $_params['desc'] ? (int) ($_params['limit'] * $feed_data['setup']['desclen'] / (count($feed_data['items']) * 2.67)) : $_params['limit'] * 45, $tpl_prefix . 'GYM_RSS_CSSID' => $tpl_prefix . 'gnews', $tpl_prefix . 'GYM_RSS_SCRSPEED' => $_params['speed']));
        $i = 1;
        foreach ($feed_data['items'] as $item) {
            if ($i > $_params['limit']) {
                break;
            }
            $template->assign_block_vars(strtolower($tpl_prefix) . 'gym_link_list', $item);
            $i++;
        }
        unset($feed_data);
    }
}
Пример #3
0
//
// Storing data like this makes searching for datapoints really fast and easy
// as we know where in the file each data point is - no need to have any searching algorithms.
//
// This is how Mike Stirling implements data storage in timestore
//
// Reads 1000 datapoints over 5 hours of 10 second data in 85-88ms
// Reads 1000 datapoints over 200 hours of 10 second data in 93ms
// Reads 1000 datapoints over 2000 hours of 10 second data in 130ms
// Reads 1000 datapoints over 2600 hours of 10 second data in 124ms
$benchstart = microtime(true);
// Feed meta data
$feed = array('id' => 1, 'start' => 1372331129, 'interval' => 10);
$start = 1372331234;
$end = $start + 3600 * 200;
$data = get_feed_data($feed, $start, $end, 1000);
echo json_encode($data);
echo "Time: " . (microtime(true) - $benchstart) * 1000 . "ms \n";
// This function would be in the emoncms feed class
function get_feed_data($feed, $start, $end, $dp)
{
    // Open the feed data file
    $fh = fopen("feed_" . $feed['id'], 'rb');
    $query_interval = ($end - $start) / $dp;
    // Calculate number of datapoints to skip to fetch the interval size we want
    $skip_size = round($query_interval / $feed['interval']);
    // Calculate the start position in the file
    $start_position = round(($start - $feed['start']) / $feed['interval']);
    $data = array();
    for ($i = 0; $i < $dp; $i++) {
        $current_position = $start_position + $skip_size * $i;
Пример #4
0
  time from feed data stored in a simple binary file
The datapoint selection algorithm starts by finding the start
  time position in the file via a binary search.
When the feed data is created a new line is added to the end of
  the feed data file, the time field is by the nature of timeseries
  data already in ascending order and unique, this makes the time
  field a suitable index for using the binary search approach to 
  query.
One the start position in the data file is found the algorithm then
  reads a datapoint every x steps. 
*/
$benchstart = microtime(true);
// Query parameters
$start = 1372424866 - 24 * 3600 * 300;
$end = 1372424866;
$data = get_feed_data(31, $start, $end, 1000);
echo json_encode($data);
echo "Time: " . (microtime(true) - $benchstart) * 1000 . "ms \n";
function get_feed_data($feedid, $start, $end, $dp)
{
    //echo $feedid." ".$start." ".$end." ".$dp."<br>";
    $fh = fopen("feed_{$feedid}", 'rb');
    $filesize = filesize("feed_{$feedid}");
    $pos = binarysearch($fh, $start, $filesize);
    // If we want one datapoint every 60s and the data rate is one
    // every 10s then calculate the skipsize to be one 6.
    // select one datapoint out of every 6.
    $interval = ($end - $start) / $dp;
    $datainterval = 5;
    $skipsize = round($interval / $datainterval);
    if ($skipsize < 1) {
Пример #5
0
function feed_controller()
{
    require "Models/feed_model.php";
    global $session, $action, $format;
    $output['content'] = "";
    $output['message'] = "";
    //---------------------------------------------------------------------------------------------------------
    // Set feed datatype: DataType::UNDEFINED|REALTIME|DAILY|HISTOGRAM
    // http://yoursite/emoncms/feed/type?id=1&type=1
    //---------------------------------------------------------------------------------------------------------
    if ($action == "type" && $session['write']) {
        $feedid = intval($_GET["id"]);
        $type = intval($_GET["type"]);
        if (feed_belongs_user($feedid, $session['userid'])) {
            set_feed_datatype($feedid, $type);
            $output['message'] = _("Feed type changed");
        } else {
            $output['message'] = _("Feed does not exist");
        }
        if ($format == 'html') {
            header("Location: view?id={$feedid}");
            // Return to feed list page
        }
    } elseif ($action == "tag" && $session['write']) {
        $feedid = intval($_GET["id"]);
        if (feed_belongs_user($feedid, $session['userid'])) {
            $newfeedtag = preg_replace('/[^\\w\\s-.]/', '', $_GET["tag"]);
            // filter out all except for alphanumeric white space and dash and full stop
            $newfeedtag = db_real_escape_string($newfeedtag);
            set_feed_tag($feedid, $newfeedtag);
            $output['message'] = _("Feed tag changed");
        } else {
            $output['message'] = _("Feed does not exist");
        }
        if ($format == 'html') {
            header("Location: list");
            // Return to feed list page
        }
    } elseif ($action == "rename" && $session['write']) {
        $feedid = intval($_GET["id"]);
        if (feed_belongs_user($feedid, $session['userid'])) {
            $newfeedname = preg_replace('/[^\\w\\s-.]/', '', $_GET["name"]);
            // filter out all except for alphanumeric white space and dash
            $newfeedname = db_real_escape_string($newfeedname);
            set_feed_name($feedid, $newfeedname);
            $output['message'] = _("Feed renamed");
        } else {
            $output['message'] = _("Feed does not exist");
        }
        if ($format == 'html') {
            header("Location: list");
            // Return to feed list page
        }
    } elseif ($action == "delete" && $session['write']) {
        $feedid = intval($_GET["id"]);
        if (feed_belongs_user($feedid, $session['userid'])) {
            delete_feed($userid, $feedid);
            $output['message'] = _("Feed ") . get_feed_name($feedid) . _(" deleted");
        } else {
            $output['message'] = _("Feed does not exist");
        }
    }
    //---------------------------------------------------------------------------------------------------------
    // Permanent delete equivalent to empty recycle bin
    // http://yoursite/emoncms/feed/permanentlydelete
    //---------------------------------------------------------------------------------------------------------
    if ($action == "permanentlydelete" && $session['write']) {
        permanently_delete_feeds($session['userid']);
        $output['message'] = "Deleted feeds are now permanently deleted";
    }
    //---------------------------------------------------------------------------------------------------------
    // Restore feed ( if in recycle bin )
    // http://yoursite/emoncms/feed/restore?id=1
    //---------------------------------------------------------------------------------------------------------
    if ($action == "restore" && $session['write']) {
        $feedid = intval($_GET["id"]);
        if (feed_belongs_user($feedid, $session['userid'])) {
            restore_feed($userid, $feedid);
        }
        $output['message'] = "feed restored";
        if ($format == 'html') {
            header("Location: list");
        }
        // Return to feed list page
    }
    //---------------------------------------------------------------------------------------------------------
    // Feed List
    // http://yoursite/emoncms/feed/list.html
    // http://yoursite/emoncms/feed/list.json
    //---------------------------------------------------------------------------------------------------------
    if ($action == 'list' && $session['read']) {
        $del = 0;
        if (isset($_GET["del"])) {
            $del = intval($_GET["del"]);
        }
        $feeds = get_user_feeds($session['userid'], $del);
        if ($format == 'json') {
            $output['content'] = json_encode($feeds);
        }
        if ($format == 'html') {
            $output['content'] = view("feed/list_view.php", array('feeds' => $feeds, 'del' => $del));
        }
    } elseif ($action == 'view' && $session['read']) {
        $feedid = intval($_GET["id"]);
        if (feed_belongs_user($feedid, $session['userid'])) {
            $feed = get_feed($feedid);
        }
        if ($format == 'json') {
            $output['content'] = json_encode($feed);
            // Allow for AJAX from remote source
            if ($_GET["callback"]) {
                $output['content'] = $_GET["callback"] . "(" . json_encode($feed) . ");";
            }
        }
        if ($format == 'html') {
            $output['content'] = view("feed/feed_view.php", array('feed' => $feed));
        }
    } elseif ($action == 'value' && $session['read']) {
        $feedid = intval($_GET["id"]);
        if (feed_belongs_user($feedid, $session['userid'])) {
            $output['content'] = get_feed_value($feedid);
        }
    } elseif ($action == 'data' && $session['read']) {
        $feedid = intval($_GET['id']);
        // Check if feed belongs to user
        if (feed_belongs_user($feedid, $session['userid'])) {
            $start = floatval($_GET['start']);
            $end = floatval($_GET['end']);
            $dp = intval($_GET['dp']);
            $data = get_feed_data($feedid, $start, $end, $dp);
            $output['content'] = json_encode($data);
        } else {
            $output['message'] = "Permission denied";
        }
    }
    //---------------------------------------------------------------------------------------------------------
    // get histogram data: energy used at different powers in the time range given
    // http://yoursite/emoncms/feed/histogram?id=1&start=000&end=000
    //---------------------------------------------------------------------------------------------------------
    if ($action == 'histogram' && $session['read']) {
        $feedid = intval($_GET['id']);
        // Check if feed belongs to user
        if (feed_belongs_user($feedid, $session['userid'])) {
            $start = floatval($_GET['start']);
            $end = floatval($_GET['end']);
            $data = get_histogram_data($feedid, $start, $end);
            $output['content'] = json_encode($data);
        } else {
            $output['message'] = "Permission denied";
        }
    }
    //---------------------------------------------------------------------------------------------------------
    // get kwh per day at given power range
    // http://yoursite/emoncms/feed/kwhatpower?id=3&min=1000&max=10000
    //---------------------------------------------------------------------------------------------------------
    if ($action == 'kwhatpower' && $session['read']) {
        $feedid = intval($_GET['id']);
        // Check if feed belongs to user
        if (feed_belongs_user($feedid, $session['userid'])) {
            $min = floatval($_GET['min']);
            $max = floatval($_GET['max']);
            $data = get_kwhd_atpower($feedid, $min, $max);
            $output['content'] = json_encode($data);
        } else {
            $output['message'] = "This is not your feed...";
        }
    }
    //---------------------------------------------------------------------------------------------------------
    // Set a datapoint at a given time.
    // http://yoursite/emoncms/feed/edit?id=1&time=...&newvalue=...
    //---------------------------------------------------------------------------------------------------------
    if ($action == "edit" && $session['write']) {
        $feedid = intval($_GET["id"]);
        if (feed_belongs_user($feedid, $session['userid'])) {
            $time = intval($_GET["time"]);
            $value = floatval($_GET["newvalue"]);
            update_feed_data($feedid, time(), $time, $value);
            $output['message'] = "Edit";
        }
    }
    if ($action == "export" && $session['write']) {
        // Feed id and start time of feed to export
        $feedid = intval($_GET["id"]);
        $start = intval($_GET["start"]);
        if (feed_belongs_user($feedid, $session['userid'])) {
            // Open database etc here
            // Extend timeout limit from 30s to 2mins
            set_time_limit(120);
            // Regulate mysql and apache load.
            $block_size = 1000;
            $sleep = 20000;
            $feedname = "feed_" . trim($feedid) . "";
            $fileName = $feedname . '.csv';
            // There is no need for the browser to cache the output
            header("Cache-Control: no-cache, no-store, must-revalidate");
            // Tell the browser to handle output as a csv file to be downloaded
            header('Content-Description: File Transfer');
            header("Content-type: text/csv");
            header("Content-Disposition: attachment; filename={$fileName}");
            header("Expires: 0");
            header("Pragma: no-cache");
            // Write to output stream
            $fh = @fopen('php://output', 'w');
            // Load new feed blocks until there is no more data
            $moredata_available = 1;
            while ($moredata_available) {
                // 1) Load a block
                $result = db_query("SELECT * FROM {$feedname} WHERE time>{$start}  \n          ORDER BY time Asc Limit {$block_size}");
                $moredata_available = 0;
                while ($row = db_fetch_array($result)) {
                    // Write block as csv to output stream
                    fputcsv($fh, array($row['time'], $row['data']));
                    // Set new start time so that we read the next block along
                    $start = $row['time'];
                    $moredata_available = 1;
                }
                // 2) Sleep for a bit
                usleep($sleep);
            }
            fclose($fh);
            exit;
        }
    }
    return $output;
}