Newer
Older
Christoph Zysik
committed
<?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' => inet_pton($ip),
Christoph Zysik
committed
':auid' => $uid,
':acid' => $cid,
':auntil'=> $until,
':buntil'=> $until,
);
return $this->database->rawInsert( $sql, $sql_params );
}
public function addContentToReload( $cid, $uid, $ip, $until )
{
$sql = 'INSERT INTO `'.DB_PREFIX.'_content_reloads`
(`ip`, `uid`, `cid`, `until`) VALUES
(:aip, :auid, :acid, :auntil) ON DUPLICATE KEY UPDATE
`until` = :buntil ';
$sql_params = array(
':aip' => inet_pton($ip),
':auid' => $uid,
':acid' => $cid,
':auntil'=> $until,
':buntil'=> $until,
);
return $this->database->rawInsert( $sql, $sql_params );
}
public function isCampaignInReload( $cid, $uid, $ip, $time )
$sql = ' COUNT(`until`) AS `c`, `until` FROM `'.DB_PREFIX.'_campaign_reloads` WHERE `until` >= :until AND ( (`uid` = :uid OR `ip` = :ip) AND `cid` = :cid ) LIMIT 1';
$sql_params = array(
':until' => $time,
':uid' => $uid,
':ip' => inet_pton($ip),
':cid' => $cid,
);
$db_result = $this->database->select( $sql, $sql_params );
$in_reload = ( isset($db_result[0]) && 0 == $db_result[0]->c ) ? false : true;
$until = ( $in_reload ) ? $db_result[0]->until : 0;
return array('in_reload' => $in_reload, 'until' => $until);
public function isContentInReload( $cid, $uid, $ip, $time)
$sql = ' COUNT(`until`) AS `c`, `until` FROM `'.DB_PREFIX.'_content_reloads` WHERE `until` >= :until AND ( (`uid` = :uid OR `ip` = :ip) AND `cid` = :cid ) LIMIT 1';
$sql_params = array(
':until' => $time,
':uid' => $uid,
':ip' => inet_pton($ip),
':cid' => $cid,
);
$db_result = $this->database->select( $sql, $sql_params );
$in_reload = ( isset($db_result[0]) && 0 == $db_result[0]->c ) ? false : true;
$until = ( $in_reload ) ? $db_result[0]->until : 0;
return array('in_reload' => $in_reload, 'until' => $until);
Christoph Zysik
committed
public function invalidateAlreadyRunningAd( $uid, $ip )
{
$result = false;
Christoph Zysik
committed
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->getIsClickableById( $id, $ip, $uid, $zeit );
if( false !== $kamp )
Christoph Zysik
committed
{
$until = $zeit + $kamp->reload;
Christoph Zysik
committed
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
$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;
}
}