How to add postback support for DmN CMS

How to add postback support for DmN CMS

No comments | Add new comment

Published: 12th of May 2021 | Category: Tutorials | Tags: dmncms, postback

In this article we will help you add postback support for DmN CMS, so you can reward users who vote for your game.  As most of you already know, this cms has been discontinued and the author will not update it anymore.

For the purpose of this tutorial we will be using as example the latest version which is 1.2.1. You need to edit your files manually or replace them with the ones we provide (replacement considering you are using version 1.2.1).

The files you need to edit are as follows:

assets/admincp/js/admincp.js at line 99 add:

if ($(this).val() == 10)
{
$('#supremetop100').show();
}
else
{
$('#supremetop100').hide();
}

application/views/admincp/vote_manager/view.links_editor.php at line 269 add:

application/models/model.account.php at line 878 add:

public function check_supremetop_vote()
{
$stmt = $this->website->db('web')->prepare('SELECT id FROM DmN_Votereward_SupremeTop100_Log WHERE memb_guid = :memb_guid AND validated = 0');
$stmt->execute([':memb_guid' => $this->session->userdata(['user' => 'id'])]);
return $stmt->fetch_all();
}

application/models/model.account.php at line 956 add:

public function add_supremetop_vote($memb_guid, $ip)
{
if(is_numeric($memb_guid)){
$stmt = $this->website->db('web')->prepare('INSERT INTO DmN_Votereward_SupremeTop100_Log (memb_guid, ip, time) VALUES (:memb_guid, :ip, :time)');
return $stmt->execute([':memb_guid' => $memb_guid, ':ip' => $ip, ':time' => time()]);
} else{
writelog('Invalid user id ' . htmlspecialchars($memb_guid), 'vote-api');
}
}

application/models/model.account.php at line 1009 add:

public function set_valid_vote_supremetop($id)
{
$stmt = $this->website->db('web')->prepare('UPDATE DmN_Votereward_SupremeTop100_Log SET validated = 1 WHERE id = :id');
return $stmt->execute([':id' => $id]);
}

application/controllers/controller.account_panel.php at line 555 add:

else if($links['api'] == 10)
{
$links['votelink'] = $links['votelink'] . '&username=' . $this->session->userdata(['user' => 'id']);
$countdown = $this->vars['votereward_config']['count_down'];
}

application/controllers/controller.vote_api.php at line 142 add:

public function supremetop($key = '')
{
if($this->checkApiKey($key, 'SupremeTop100'))
{
// Ensure that it is a POST request.
if(strcasecmp($_SERVER['REQUEST_METHOD'], 'POST') != 0)
{
writelog('SupremeTop100 failed REQUEST_METHOD check', 'vote_api');
}
else
{
writelog('SupremeTop100 passed REQUEST_METHOD check', 'vote_api');
// Ensure that this is SupremeTop100 sending the request
if(strcasecmp($_SERVER['HTTP_USER_AGENT'], 'SupremeTop100/1.0 (Linux; x64) Postback Agent') != 0)
{
writelog('SupremeTop100 error - unable to log vote (invalid useragent): ' . $_SERVER['HTTP_USER_AGENT'], 'vote_api');
}
else
{
writelog('SupremeTop100 passed HTTP_USER_AGENT check', 'vote_api');
// Ensure that the content type of the POST request has been set to application/json
$contentType = isset($_SERVER["CONTENT_TYPE"]) ? trim($_SERVER["CONTENT_TYPE"]) : '';
// For RESTful response
// For "normal" response use 'application/x-www-form-urlencoded'
if(strcasecmp($contentType, 'application/json') != 0)
{
writelog('SupremeTop100 error - unable to log vote (invalid content type): ' . $contentType, 'vote_api');
}
else
{
// Retrieve the RAW POST data.
$content = trim(file_get_contents("php://input"));
// Attempt to decode the incoming RAW POST data from JSON.
$decoded = json_decode($content, true);
// If json_decode failed, the JSON is invalid.
if(!is_array($decoded))
{
// Do not give too many details, because the attacker is smart.
writelog('SupremeTop100 error - unable to vote: ' . print_r($content), 'vote_api');
}
else
{
// Process the JSON parsed as array.
if($decoded['supremetop100_vote_info']['status'] == 1)
{
// Rewards your user by username or character name
if($this->Maccount->add_supremetop_vote($decoded['supremetop100_user_info']['username'], $decoded['supremetop100_user_info']['ip_address']))
{
echo 'VOTE LOGGED';
}
else
{
writelog('SupremeTop100 error - unable to log vote: ' . print_r($decoded), 'vote_api');
}
}
else
{
writelog('SupremeTop100 error - unable to vote (bad status): ' . $decoded['supremetop100_vote_info']['status'], 'vote_api');
}
}
}
}
}
}
}

application/controllers/controller.ajax.php at line 572 add:

else if($check_link['api'] == 10)
{
if($valid_votes = $this->Maccount->check_supremetop_vote())
{
if(!empty($valid_votes))
{
$count = count($valid_votes);
$i = 0;
foreach($valid_votes AS $valid)
{
$i++;
$this->Maccount->set_valid_vote_supremetop($valid['id']);
$this->Maccount->reward_voter($check_link['reward'], $check_link['reward_type'], $this->session->userdata(['user' => 'server']));
$this->Maccount->check_vote_rankings($this->session->userdata(['user' => 'username']), $this->session->userdata(['user' => 'server']));
if($i == $count)
{
if($this->Maccount->log_vote($_POST['vote']))
{
json(['success' => vsprintf(__('Vote was successful. You have received %d %s'), [$check_link['reward'], $this->website->translate_credits($check_link['reward_type'], $this->session->userdata(['user' => 'server']))]), 'next_vote' => $this->Maccount->calculate_next_vote((time() - 60), $check_link['hours']), 'reward' => $check_link['reward']]);
}
else
{
json(['error' => __('Unable to log your SupremeTop100 vote. Please try again later.')]);
}
}
}
}
else
{
json(['error' => __('Unable to validate your SupremeTop100 vote. Please try again later.')]);
}
}
else
{
json(['error' => __('Unable to validate your SupremeTop100 vote. Please try again after few minutes.')]);
}
}

Run SQL query to create required table:

USE [dmncms]
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING OFF
GO
CREATE TABLE [dbo].[DmN_Votereward_SupremeTop100_Log](
[id] [int] IDENTITY(1,1) NOT NULL,
[memb_guid] [int] NULL,
[ip] [varchar](50) NULL,
[time] [int] NULL,
[validated] [int] NOT NULL,
PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[DmN_Votereward_SupremeTop100_Log] ADD  DEFAULT ((0)) FOR [validated]
GO

If you are unable to edit manually, you can always get the full code edited here:

Remmember the above will only work for 1.2.1, for older versions you might need to edit the files individually.

Until next tutorial!

No comments


Leave Reply

Comments are submitted for moderation and approval. Do not try to spam because we will ban you.

Please complete the verification.