Skip to content
Snippets Groups Projects
ptc.php 3.63 KiB
Newer Older
<?php

/*
* TODO
* nothing checks wether user landed on blacklist...
* blacklist check is broken currently anyway
* please fix me
*/
class Ptc
{

    private $database;
    private $campaigns;

    public function __construct($database, $campaigns)
    {
        $this->database  = $database;
        $this->campaigns = $campaigns;
    }

    public function addCampaignToReload( $cid, $uid, $ip, $until )
    {
        $sql = 'INSERT INTO `'.DB_PREFIX.'_campaign_reloads`
                (`ip`, `uid`, `cid`, `until`) VALUES
                (:aip, :auid, :acid, :auntil) ON DUPLICATE KEY UPDATE
                `until` = :buntil ';
        $sql_params = array(
                            ':aip'   => $ip,
                            ':auid'  => $uid,
                            ':acid'  => $cid,
                            ':auntil'=> $until,
                            ':buntil'=> $until,
                      );
        return $this->database->rawInsert( $sql, $sql_params );
    }

    public function invalidateAlreadyRunningAd( $uid, $ip )
    {
        $result = false;

        if( isset($_SESSION['current_ad']) )
        {
            $tan = ( isset($_SESSION['current_ad']['tan']) ) ? $_SESSION['current_ad']['tan'] : false;
            $art = ( isset($_SESSION['current_ad']['art']) ) ? $_SESSION['current_ad']['art'] : '';
            $id  = ( isset($_SESSION['current_ad']['id']) )  ? $_SESSION['current_ad']['id'] : 0;

            if( 0 !== $id )
            {
                $kamp = $this->campaigns->getById( $id );
                if( 0 < $kamp['count'] )
                {
                    $until = time() + $kamp['data']->reload;
                    $this->addCampaignToReload( $id, $uid, $ip, $until );
                }
            }
            unset( $_SESSION['current_ad'] );
            $result = true;
        }
        return $result;
    }

    public function setCurrentRunningAd( $art, $cid, $tan )
    {
        $_SESSION['current_ad']['art'] = $art;
        $_SESSION['current_ad']['id']  = $cid;
        $_SESSION['current_ad']['tan'] = $tan;
        
        $_SESSION['current_ad']['time'] = time();
        $_SESSION['current_ad']['paid'] = false;
    }

    public function handleAdPayout( $cid, $adtype, $uid, $ip, $preis, $verdienst, $msg = 'Paid4Action', $billanzcat = 'Werbebereich' )
    {
        $result = true;

        // TODO adtype dependent counter table
        db_query("UPDATE `".DB_PREFIX."_kontodaten`  SET `klicks` = `klicks` + 1, `kv` = `kv` + ".$verdienst.", `fc_klicks` = `fc_klicks` + 1 WHERE `uid` = ".$uid); // Hier Zusatz für Fakeschutz  
        kontobuchung ('+', $verdienst, create_code(14), $uid, 1, $msg);
        refumsatz ($verdienst, $uid);
        // TODO add handler $adtype dependent
        rallysystem ($uid, '1', $verdienst);
        bilanz($preis, $verdienst, $billanzcat, $msg);

        return $result;
    }

    function checkAdOkAndPay( $cid, $uid, $ip )
    {
        $result = false;

        $time = time();
        $data = $this->campaigns->getIsClickableById( $cid, $ip, $uid, $time );

        if ( false !== $data  )
        {
            $timepassed = ( $time - $_SESSION['current_ad']['time'] ) +1;

            if( $timepassed >= $data->aufendhalt )
            {

                $until = $time + $data->reload;

                $this->handleAdPayout( $cid, $data->werbeart, $uid, $ip, $data->preis, $data->verdienst );
                $this->addCampaignToReload( $cid, $uid, $ip, $until );
                $this->campaigns->decreaseAvailCountById( $cid, 1 );

                $_SESSION['current_ad']['paid'] = true;
                $result = true;
            }
        }

        return $result;
    }
}