예제 #1
0
void ofxTwitter::checkHelpConfigurationFile() {
    
    ofFile file(ofToDataPath("ofxTwitter_configuration.json"));
    bool configFileNeedsUpdate = false;
    if(file.exists()) {
        Poco::File &filepoco = file.getPocoFile();
        Poco::Timestamp tfile = filepoco.getLastModified();
        time_t timer;
        time(&timer);
        double seconds;
        seconds = difftime(timer,tfile.epochTime());
        if(seconds < 86400) {
            configFileNeedsUpdate = false;
        } else {
            configFileNeedsUpdate = true;
        }
    } else {
        configFileNeedsUpdate = true;
    }
    
    if(!configFileNeedsUpdate) {
        ofLogNotice("ofxTwitter::authorize") << "Config up to date.";
        parseConfigurationFile();
    } else {
        updateHelpConfiguration();
    }

}
예제 #2
0
void Context::flushSessionCache() 
{
	poco_assert (isForServerUse());

	Poco::Timestamp now;
	SSL_CTX_flush_sessions(_pSSLContext, static_cast<long>(now.epochTime()));
}
예제 #3
0
void TrafficDataObject::deleteOld()
{
    Poco::Timestamp achshav;
    Poco::Timespan days = Poco::Timespan::DAYS * Poco::Util::Application::instance().config().getUInt("ion.trafficage");
    achshav -= days;
    std::time_t age = achshav.epochTime();
    _session << "DELETE FROM traffic WHERE time<?", use(age), now;
}
예제 #4
0
std::time_t UIShader::getLastModified( ofFile& _file ){
	if( _file.exists() ){
		Poco::File& pocoFile		= _file.getPocoFile();
		Poco::Timestamp timestamp	= pocoFile.getLastModified();
		std::time_t fileChangedTime = timestamp.epochTime();
		
		return fileChangedTime;
	} else {
		return 0;
	}
}
예제 #5
0
void TrafficDataObject::checkAuthStatus()
{
    Poco::Timestamp achshav;
    Poco::Timespan days = Poco::Timespan::MINUTES * Poco::Util::Application::instance().config().getUInt("ion.traffic-interval");
    achshav -= days;
    std::time_t age = achshav.epochTime();
    unsigned count = 0;
    _session << "SELECT sum(count) AS count FROM traffic WHERE time>? AND "
            "traffic.domain NOT IN (SELECT value FROM authorized_traffic WHERE  type='domain') AND "
            "traffic.ip NOT IN (SELECT value FROM authorized_traffic WHERE  type='ip') AND "
            "traffic.port NOT IN (SELECT value FROM authorized_traffic WHERE  type='port') AND "
            "traffic.process NOT IN (SELECT value FROM authorized_traffic WHERE  type='process')", use(age), into(count), now;
    if (count > 0) {
        _logger.notice("Non authorized traffic count %u", count);
        EventNotification::notifyTraffic(count);
    }
}
void ICalendarWatcher::refresh()
{
    Poco::Timestamp now; // the current time

    if (_calendar)
    {
        ICalendar::EventInstances oldInstances = _watches;
        ICalendar::EventInstances newInstances = _calendar->getEventInstances(now);

        // sort uids lexigraphically
        std::sort(oldInstances.begin(), oldInstances.end(), compareUID);
        std::sort(newInstances.begin(), newInstances.end(), compareUID);

        // unmatchedOldInstances are those events instances that are NOT
        // seen in the current instanaces.  if an instance is NOT seen,
        // that means that an instance has ended (we can check for that)
        // OR the event was "modified" -- that is -- the equality
        // relationship didn't hold and we need to check for UID equality
        // in this case -- OR the event was deleted altogether from the
        // calendar.

        ICalendar::EventInstances unmatchedOldInstances;

        // unmatchedNewInstances are those instances that did not have an
        // equality match inside of the watchedInstances vector.  Events in
        // this array can fall into three categories: 1) a modified event
        // -- == opererator failed because of a different modified date,
        // 2) an event that has just started 3) an in-progress event that
        // was added after its start time.

        ICalendar::EventInstances unmatchedNewInstances;

        // newWatches will eventually replace _watches when the refresh is done
        ICalendar::EventInstances newWatches;

        // std::set_difference - what is in first set, but not in the second one

        // TODO: combine all three of these steps into a single loop

        // find any instances that are in the current watch list that are no longer there
        std::set_difference(oldInstances.begin(),
                            oldInstances.end(),
                            newInstances.begin(),
                            newInstances.end(),
                            std::inserter(unmatchedOldInstances,
                                          unmatchedOldInstances.end()),
                            compareUID);

        // find any new instances that are not matched in the current watchlist
        std::set_difference(newInstances.begin(),
                            newInstances.end(),
                            oldInstances.begin(),
                            oldInstances.end(),
                            std::inserter(unmatchedNewInstances,
                                          unmatchedNewInstances.end()),
                            compareUID);

        // find all instances that are still are the same in both sets.
        // these instances must be checked for modification times.
        std::set_intersection(newInstances.begin(),
                              newInstances.end(),
                              oldInstances.begin(),
                              oldInstances.end(),
                              std::inserter(newWatches,
                                            newWatches.end()),
                              compareUID);

        // process the unmatched old instances
        ICalendar::EventInstances::iterator unmatchedOldInstancesIter = unmatchedOldInstances.begin();

        while (unmatchedOldInstancesIter != unmatchedOldInstances.end())
        {
            ICalendarEventInstance& instance = *unmatchedOldInstancesIter;
            Poco::Timestamp startTime = instance.getInterval().getStart();
            Poco::Timestamp endTime = instance.getInterval().getEnd();

            if (!instance.isValidEventInstance() || endTime.epochTime() < _lastUpdate.epochTime())
            {
                ofNotifyEvent(events.onEventRemoved, instance, this);
            }
            else
            {
                ofNotifyEvent(events.onEventEnded, instance, this);
            }

            ++unmatchedOldInstancesIter;
        }

        // process the old matches and check them for updates
        // process the unmatched old instances
        ICalendar::EventInstances::iterator newWatchesIter = newWatches.begin();

        while (newWatchesIter != newWatches.end())
        {
            const ICalendarEvent& evt = (*newWatchesIter).getEvent();

            std::string uid = evt.getUID();
            Poco::Timestamp lastModified = evt.getLastModified();

            std::map<std::string, Poco::Timestamp>::iterator lastUpdatedIter = _watchesLastUpdated.find(uid);

            if (lastUpdatedIter != _watchesLastUpdated.end() &&
                lastModified.epochTime() > _watchesLastUpdated[uid].epochTime())
            {
                ofNotifyEvent(events.onEventModified, *newWatchesIter, this);
            }

            ++newWatchesIter;
        }

        // process the new instances (add them to the watch list and the updated group)
        ICalendar::EventInstances::iterator unmatchedNewInstancesIter = unmatchedNewInstances.begin();

        while (unmatchedNewInstancesIter != unmatchedNewInstances.end())
        {
            ICalendarEventInstance& instance = *unmatchedNewInstancesIter;
            std::string uid = instance.getEvent().getUID();
            Poco::Timestamp startTime = instance.getInterval().getStart();
            Poco::Timestamp endTime = instance.getInterval().getEnd();

            if (startTime.epochTime() >= _lastUpdate.epochTime())
            {
                ofNotifyEvent(events.onEventStarted, instance, this);
            }
            else
            {
                ofNotifyEvent(events.onEventAdded, instance, this);
            }

            newWatches.push_back(instance); // add to current watches

            ++unmatchedNewInstancesIter;
        }

        // make a record of the watches
        _watches = newWatches;

        // clear out old last udpated lists
        _watchesLastUpdated.clear();

        ICalendar::EventInstances::iterator iter = _watches.begin();

        while (iter != _watches.end())
        {
            const ICalendarEvent& evt = (*iter).getEvent();
            _watchesLastUpdated[evt.getUID()] = evt.getLastModified();
            ++iter;
        }
    }
    else
    {
        ofLogError("ICalendarWatcher::refresh()") << "Calendar null.";
    }

    _lastUpdate = now;
}
//--------------------------------------------------------------
void CloudsVHXAuth::threadedFunction()
{
    completeArgs.success = false;
    completeArgs.result = "";
    
    if (mode == REQUEST_TOKEN || mode == REFRESH_TOKEN) {
        _ssl.setup();
        _ssl.setOpt(CURLOPT_CAINFO, ofToDataPath(GetCloudsDataPath(true) + "vhx/cacert.pem"));
        _ssl.setURL("https://api.vhx.tv/oauth/token");
        if (mode == REQUEST_TOKEN) {
            _ssl.addFormField("client_id", _clientId);
            _ssl.addFormField("client_secret", _clientSecret);
            _ssl.addFormField("grant_type", "client_credentials");
        }
        else {
            _ssl.addFormField("refresh_token", _refreshToken);
            _ssl.addFormField("grant_type", "refresh_token");
        }
        
        _ssl.perform();
        
        string response = _ssl.getResponseBody();
        ofLogVerbose("CloudsVHXAuth::threadedFunction") << "Response:" << endl << response;
        
        completeArgs.success = false;
        
        ofxJSONElement json;
        if (json.parse(response)) {
            if (json.isMember("access_token")) {
                _accessToken = json["access_token"].asString();
                _refreshToken = json["refresh_token"].asString();
                _tokenExpiry = (ofGetSystemTime() / 1000.f) + json["expires_in"].asFloat();
                
                // Save the tokens to disk.
                CloudsCryptoSaveTokens(_accessToken, _refreshToken, _tokenExpiry, _tokensPath);
                
                completeArgs.success = true;
                completeArgs.result = _accessToken;
            }
            else {
                ofLogError("CloudsVHXAuth::threadedFunction") << "Unexpected JSON format:" << endl << response;
            }
        }
        else {
            ofLogError("CloudsVHXAuth::threadedFunction") << "Unable to parse JSON:" << endl << response;
        }
        
        _ssl.clear();
        
        bNotifyComplete = true;
    }
    else if (mode == REQUEST_CODE) {
        _ssl.setup();
        _ssl.setOpt(CURLOPT_CAINFO, ofToDataPath(GetCloudsDataPath(true) + "vhx/cacert.pem"));
        _ssl.setURL("https://api.vhx.tv/oauth/codes");
        _ssl.addFormField("client_id", _clientId);
        _ssl.addFormField("client_secret", _clientSecret);

        _ssl.perform();
        
        string response = _ssl.getResponseBody();
        ofLogVerbose("CloudsVHXAuth::threadedFunction") << "Response:" << endl << response;
        
        completeArgs.success = false;
        
        ofxJSONElement json;
        if (json.parse(response)) {
            if (json.isMember("code")) {
                _code = json["code"].asString();
                _codeExpiry = (ofGetSystemTime() / 1000.f) + json["expires_in"].asFloat();
                
                completeArgs.success = true;
                completeArgs.result = _code;
            }
            else {
                ofLogError("CloudsVHXAuth::threadedFunction") << "Unexpected JSON format:" << endl << response;
            }
        }
        else {
            ofLogError("CloudsVHXAuth::threadedFunction") << "Unable to parse JSON:" << endl << response;
        }
        
        _ssl.clear();
        
        bNotifyComplete = true;
    }
    else if (mode == LINK_CODE) {
        stringstream ss;
        ss << "http://www.vhx.tv/activate/clouds";
        ss << "?client_id=" << _clientId;
        ss << "&code=" << _code;
        ofLaunchBrowser(ss.str());
        
        ss.str("");
        ss << "https://api.vhx.tv/oauth/codes/" << _code;
        ss << "?client_id=" << _clientId;
        ss << "&client_secret=" << _clientSecret;
        
        completeArgs.success = false;

        bool bWaitForLink = true;
        while (bWaitForLink && isThreadRunning()) {
            
            _ssl.setup();
            _ssl.setOpt(CURLOPT_CAINFO, ofToDataPath(GetCloudsDataPath(true) + "vhx/cacert.pem"));
            _ssl.setURL(ss.str());
            _ssl.perform();
            
            string response = _ssl.getResponseBody();
            ofLogVerbose("CloudsVHXAuth::threadedFunction") << "Response:" << endl << response;

            ofxJSONElement json;
            if (json.parse(response)) {
                if (json.isMember("access_token")) {
                    _accessToken = json["access_token"].asString();
                    _refreshToken = json["refresh_token"].asString();
                    _tokenExpiry = (ofGetSystemTime() / 1000.f) + json["expires_in"].asFloat();
                    
                    // Save the tokens to disk.
                    CloudsCryptoSaveTokens(_accessToken, _refreshToken, _tokenExpiry, _tokensPath);
                    
                    completeArgs.success = true;
                    completeArgs.result = _accessToken;

                    bWaitForLink = false;
                }
                else {
                    ofLogVerbose("CloudsVHXAuth::threadedFunction") << "Unexpected JSON result:" << endl << response;
                }
            }
            else {
                ofLogError("CloudsVHXAuth::threadedFunction") << "Unable to parse JSON:" << endl << response;
                
                bWaitForLink = false;
            }
            
            _ssl.clear();
            
            ofSleepMillis(500);
        }
        
        bNotifyComplete = true;
    }
    else if (mode == VERIFY_PACKAGE) {
        stringstream ss;
        ss << "Authorization: Bearer " << _accessToken;
        
        _ssl.setup();
        _ssl.setOpt(CURLOPT_CAINFO, ofToDataPath(GetCloudsDataPath(true) + "vhx/cacert.pem"));
        _ssl.setURL("https://api.vhx.tv/me");
        _ssl.addHeader(ss.str());
        
        _ssl.perform();
        
        string response = _ssl.getResponseBody();
        ofLogVerbose("CloudsVHXAuth::threadedFunction") << "Response:" << endl << response;
        cout << "RESPONSE " << response << endl;
        
        completeArgs.success = false;

        ofxJSONElement json;
        if (json.parse(response)) {
            if (json.isMember("_embedded")) {
                const ofxJSONElement& embedded = json["_embedded"];
                if (embedded.isMember("packages")) {
                    const ofxJSONElement& packages = embedded["packages"];
                    if(packages.size() > 0){
                        for (int i = 0; i < packages.size(); ++i) {
                            const ofxJSONElement& element = packages[i];
                            if (element.isMember("id")) {
                                string packageId = element["id"].asString();
                                if (packageId == _packageId || packageId == "8102") { //hack for rental
                                    // Found matching package, check that purchase is valid.
                                    if (element.isMember("purchase_type")) {
                                        string purchaseType = element["purchase_type"].asString();
                                        if (purchaseType == "purchase") {
                                            state = PURCHASE;
                                            completeArgs.success = true;
                                            completeArgs.result = "purchase";
                                            break;
                                        }
                                        else if (purchaseType == "rental") {
                                            if (element.isMember("expires_at")) {
                                                if (element["expires_at"].isNull()) {
                                                    // No expiry, assume we're good.
                                                    state = RENTAL;
                                                    completeArgs.success = true;
                                                    completeArgs.result = "rental";
                                                    break;
                                                }
                                                else {
                                                    // Parse the expiry date.
                                                    Poco::DateTime dt;
                                                    int tzd;
                                                    if (Poco::DateTimeParser::tryParse(element["expires_at"].asString(), dt, tzd)) {
                                                        Poco::LocalDateTime ldt(tzd, dt);
                                                        Poco::Timestamp expiryTime = ldt.timestamp();
                                                        Poco::Timestamp nowTime;
                                                        
                                                        // Make sure the rental is valid.
                                                        if (nowTime < expiryTime) {
                                                            // Expires in the future, we're good.
                                                            _packageExpiry = expiryTime.epochTime();
                                                            
                                                            state = RENTAL;
                                                            completeArgs.success = true;
                                                            completeArgs.result = "rental";
                                                            break;
                                                        }
                                                        else {
                                                            // Expired, no good.
                                                            state = EXPIRED;
                                                            completeArgs.success = true;
                                                            completeArgs.result = "expired";
                                                            continue; //check other packages
                                                        }
                                                    }
                                                    else {
                                                        // Could not parse expiry, assume no good.
                                                        state = EXPIRED;
                                                        completeArgs.success = true;
                                                        completeArgs.result = "expired";
                                                        continue; //check the other packages
                                                    }
                                                }
                                            }
                                            else {
                                                // No expiry, assume we're good.
                                                state = RENTAL;
                                                completeArgs.success = true;
                                                completeArgs.result = "rental";
                                                break;
                                            }
                                        }
                                        else {
                                            // Not a rental or purchase, assume no good.
                                            state = INACTIVE;
                                            completeArgs.success = true;
                                            completeArgs.result = "inactive";
                                            continue; //check the other packages
                                        }
                                    }
                                    else {
                                        ofLogError("CloudsVHXAuth::threadedFunction") << "Unexpected JSON result, 'purchase_type' not found:" << endl << response;
                                    }
                                }
                                else {
                                    ofLogNotice("CloudsVHXAuth::threadedFunction") << "Skipping package " << packageId;
                                }
                            }
                            else {
                                ofLogError("CloudsVHXAuth::threadedFunction") << "Unexpected JSON result, 'id' not found:" << endl << response;
                            }
                        }
                    }
                    else {
                        state = INACTIVE;
                        completeArgs.success = true;
                        completeArgs.result = "inactive";
                    }
                }
                else {
                    ofLogError("CloudsVHXAuth::threadedFunction") << "Unexpected JSON result, 'packages' not found:" << endl << response;
                }
            }
            else {
                ofLogError("CloudsVHXAuth::threadedFunction") << "Unexpected JSON result, '_embedded' not found:" << endl << response;
            }
        }
        else {
            ofLogError("CloudsVHXAuth::threadedFunction") << "Unable to parse JSON:" << endl << response;
        }
        
        _ssl.clear();
        bNotifyComplete = true;
    }
    else {
        ofLogError("CloudsVHXAuth::threadedFunction") << "Mode " << mode << " is unrecongized!";
    }
}