Esempio n. 1
0
/**
 * This method performs nearly all of the TinyPass logic for when and how to protect content.
 * Based upon the TP configuration, the post, the tags this method will either permit access
 * to a post or it will truncate the content and show a 'purchase now' widget instead of the post content.
 * 
 * Access is checked by retreiving an encrypted cookie that is stored after a successful purchase.
 * 
 */
function tinypass_intercept_content($content)
{
    global $tpstate;
    global $post;
    $tpstate->reset();
    tinypass_include();
    $ss = tinypass_load_settings();
    //break out if Tinypass is disabled
    if ($ss->isEnabled() == false) {
        return $content;
    }
    $storage = new TPStorage();
    $postOptions = $storage->getPostSettings($post->ID);
    $tagOptions = $storage->getPaywallByTag($ss, $post->ID);
    if ($tagOptions->isEnabled() == false) {
        $tagOptions = $storage->getPaywallSubRefID($ss, $post->ID);
    }
    TinyPass::$AID = $ss->getAID();
    TinyPass::$PRIVATE_KEY = $ss->getSecretKey();
    TinyPass::$SANDBOX = $ss->isSand();
    //TinyPass::$API_ENDPOINT_DEV = 'http://tinydev.com:9000';
    $store = new TPAccessTokenStore();
    $store->loadTokensFromCookie($_COOKIE);
    //we want to dump the button on this page
    if ($tagOptions->getSubscriptionPageRef() == $post->ID) {
        $tagOffer = TPPaySettings::create_offer($tagOptions, $tagOptions->getResourceId());
        $gotolink = get_page_link($tagOptions->getSubscriptionPageSuccessRef());
        $req = new TPPurchaseRequest($tagOffer);
        $req->setCallback('tinypass_redirect');
        $button1 = $req->generateTag();
        $tpstate->add_scripts = true;
        if (preg_match('/\\[tinypass\\s+rid.*\\]/', $content)) {
            $content = preg_replace('/\\[tinypass\\srid+.*\\]/', $button1, $content);
            $button1 = '';
        } else {
            $button1 = "<div id='tinypass_subscription_holder'>{$button1}</div>";
        }
        return $content . $button1 . "<script>" . "var tp_goto = '{$gotolink}';" . "if(typeof tinypass_redirect != 'function') {\n\t\t\t\t\t\t\t\tfunction tinypass_redirect(status){\n\t\t\t\t\t\t\t\tif(status.state == 'granted'){\n\t\t\t\t\t\t\t\t\twindow.location = tp_goto;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif(typeof tpOnPrepare != 'function') {\n\t\t\t\t\t\t\tfunction tpOnPrepare(status){\n\t\t\t\t\t\t\t\tif(status.state == 'granted'){\n\t\t\t\t\t\t\t\t\t//window.location = tp_goto;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}" . "</script>";
    }
    //exit if everything is disabled
    if ($postOptions->isEnabled_20() == false && $tagOptions->isEnabled() == false) {
        return $content;
    }
    $tpstate->add_scripts = true;
    //When content is shown in list form, i.e. categories we still need to truncate content
    //At this point in the execution we know that TP is enabled so we have to protect
    if (is_singular() == false) {
        $c = get_extended_with_tpmore($content);
        if ($c['extended'] == '') {
            $content = tinypass_trim_excerpt($content);
        } else {
            $content = $c['main'];
        }
        return $content;
    }
    define('DONOTCACHEPAGE', true);
    define('DONOTCACHEDB', true);
    define('DONOTMINIFY', true);
    define('DONOTCDN', true);
    define('DONOTCACHCEOBJECT', true);
    $postOffer = null;
    $tagOffer = null;
    $postToken = null;
    $tagToken = null;
    if ($postOptions->isEnabled_20() && $ss->isPPPEnabled()) {
        $postOffer = TPPaySettings::create_offer($postOptions, "wp_post_" . strval($post->ID), $postOptions->getResourceName() == '' ? $post->post_title : $postOptions->getResourceName());
        $postToken = $store->getAccessToken($postOffer->getResource()->getRID());
    }
    $tagOfferTrialActive = FALSE;
    if ($tagOptions != null && $tagOptions->isEnabled()) {
        $tagOffer = TPPaySettings::create_offer($tagOptions, $tagOptions->getResourceId());
        $tagToken = $store->findActiveToken('/' . $tagOptions->getResourceId() . '/');
    }
    //For PPV mode
    if ($tagOptions->isMode(TPPaySettings::MODE_PPV) && $tagOptions->isEnabled()) {
        $rid = "wp_post_" . strval($post->ID);
        $tagOffer = TPPaySettings::create_offer($tagOptions, $rid, $post->post_title);
        //If a offer on the post is defined then use that one
        if ($postOffer != null) {
            $tagOffer = null;
        }
        $tagToken = $store->getAccessToken($rid);
    } else {
        if ($tagOptions->isMode(TPPaySettings::MODE_METERED) && $tagOptions->isEnabled()) {
            //Only check metered if the mode is metered and it is enabled
            $meter = null;
            if ($tagOptions->isMetered()) {
                $cookieName = "tr_" . substr(md5($tagOptions->getResourceId()), 0, 20);
                $meter = TPMeterHelper::loadMeterFromCookie($cookieName, $_COOKIE);
                $lockoutPeriod = $tagOptions->getMeterLockoutPeriodFull();
                if ($meter == null) {
                    $meter = TPMeterHelper::createViewBased($cookieName, $tagOptions->getMeterMaxAccessAttempts(), $lockoutPeriod);
                }
                $lockoutPeriodEndTime = time() + TPUtils::parseLoosePeriodInSecs($lockoutPeriod);
                $meter->increment();
                setcookie($cookieName, TPMeterHelper::__generateLocalToken($cookieName, $meter), $lockoutPeriodEndTime, '/');
                if ($meter->isTrialPeriodActive()) {
                    $tagOfferTrialActive = TRUE;
                    if ($tagOptions->isCounterEnabled() && $meter->getTrialViewCount() > $tagOptions->getCounterDelay(PHP_INT_MAX)) {
                        $tpstate->show_counter = true;
                        $onclick = 'onclick="return false"';
                        if ($tagOptions->isCounterOnClick(TPPaySettings::CT_ONCLICK_PAGE)) {
                            $gotolink = get_page_link($tagOptions->getSubscriptionPageRef());
                            $onclick = 'href="' . $gotolink . '"';
                        } else {
                            if ($tagOptions->isCounterOnClick(TPPaySettings::CT_ONCLICK_APPEAL)) {
                                $onclick = 'onclick="tinypass.showAppeal(); return false"';
                                $tpstate->embed_appeal = __tinypass_create_appeal($tagOptions);
                            }
                        }
                        $tpstate->counter = __tinypass_render_template(TINYPASS_COUNTER_TEMPLATE, array('count' => $meter->getTrialViewCount(), 'max' => $meter->getTrialViewLimit(), 'remaining' => $meter->getTrialViewLimit() - $meter->getTrialViewCount(), 'position' => 'position-' . $tagOptions->getCounterPosition(), 'onclick' => $onclick));
                    }
                    if ($tagOptions->getAppealEnabled() && $meter != null) {
                        $count = $meter->getTrialViewCount();
                        if ($count == $tagOptions->getAppealNumViews() || $count > $tagOptions->getAppealNumViews() && $count % $tagOptions->getAppealFrequency() == 0) {
                            $tpstate->show_appeal = true;
                            $tpstate->embed_appeal = __tinypass_create_appeal($tagOptions);
                        }
                    }
                    return $content;
                }
            }
        }
    }
    if ($postOffer == null && $tagOffer == null) {
        return $content;
    }
    //If they already have access to the post
    if ($postToken != null && $postToken->isAccessGranted()) {
        return $content;
    }
    //If the tag level offer does not exist or access is already granted
    if ($tagToken != null && $tagToken->isAccessGranted() || $tagOfferTrialActive) {
        return $content;
    }
    $c = get_extended_with_tpmore($post->post_content);
    if ($c['extended'] == '') {
        //means there was no <!--more--> defined
        $content = tinypass_trim_excerpt($content);
    } else {
        $content = $c['main'];
    }
    if ($ss->isDisableCommentsWhenDenied()) {
        add_filter('comments_open', 'tinypass_close_comments', 1000);
        $post->comment_status = "closed";
        add_filter('comments_template', 'tinypass_skip_comments');
    }
    /*
     * Construct the offers for both the tag level offer and the post level offer if they exist
     */
    $ticketoptions = array();
    if ($postOffer) {
        $req = new TPPurchaseRequest($postOffer, $ticketoptions);
        $tpstate->post_req = array('req' => $req, 'message1' => $postOptions->getDeniedMessage1("") != "" ? $postOptions->getDeniedMessage1() : $ss->getDeniedMessage1(), 'sub1' => $postOptions->getDeniedSub1("") != "" ? $postOptions->getDeniedSub1() : $ss->getDeniedSub1());
        $req->setCallback('tinypass_reloader');
    }
    if ($tagOffer) {
        $req2 = new TPPurchaseRequest($tagOffer, $ticketoptions);
        $tpstate->tag_req = array('req' => $req2, 'message1' => $tagOptions->getDeniedMessage1(), 'sub1' => $tagOptions->getDeniedSub1());
        $req2->setCallback('tinypass_reloader');
    }
    //Switch the offer order if selected in the settings
    if ($tagOptions->isPostFirstInOrder() == false) {
        $temp = $tpstate->post_req;
        $tpstate->post_req = $tpstate->tag_req;
        $tpstate->tag_req = $temp;
    }
    return $content;
}