I am using display suite
to customize the look of my node. I created a code field
in display suite
named Download Code
and customize the downloading of file field called field_code_file
using the following code:
<?php
global $user;
if (!isset($_SESSION['downinc'])) $_SESSION['downinc'] = 1;
if ($user->uid) $_SESSION['downinc'] = 0;
?>
<form id="form1" name="form1" method="post" action="/download-code">
<input type="hidden" name="download_code" id="download_code" value="[node:field_code_file]" />
<input type="hidden" name="uri" id="uri" value="[node:url:relative]" />
<input type="submit" class="downloadButton" value="Download Code">
</form>
When my visitors download the code, he/she will be redirected to the download-code page. A normal Page
node I created with PHP Code as text format. Since I learned that it is not advisable to use PHP code in node, I have created a module (not for this one) and transfer all the code from it to my simple module. You can find the module that I have created at this link. That works great. But what I want now is also transfer the code I have above and below into a module.
Here's my code in the download-page node:
<script language="javascript">
var time_left = 5;
var cinterval;
function time_dec(){
time_left--;
document.getElementById('countdown').innerHTML = time_left;
if(time_left == 0){
clearInterval(cinterval);
}
}
cinterval = setInterval('time_dec()', 1000);
</script>
<?php
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate"); // HTTP/1.1
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache"); // HTTP/1.0
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
if (!isset($_SERVER['HTTP_REFERER']))
{
echo 'Do not call this file directly';
exit();
}
$tracker = array(
'database' => 'c1tracker',
'username' => 'c1tracker',
'password' => 'mypass',
'host' => 'localhost',
'driver' => 'mysql',
);
Database::addConnectionInfo('tracker', 'default', $tracker);
db_set_active('tracker');
$current_ip = $_SERVER['REMOTE_ADDR'];
$referrer = $_SERVER['HTTP_REFERER'];
$user_agent = $_SERVER['HTTP_USER_AGENT'];
global $user;
if (!isset($_SESSION['downinc'])) {
//set session here since it was not set from the referrer page because of caching.
$_SESSION['downinc'] = 1;
$error = "Invalid download. No Session.";
$sql = "INSERT INTO `download_log` (`id`, `error`, `ip_address`, `referrer`, `user_agent`, `date_time`) VALUES (NULL, '$error', '$current_ip', '$referrer', '$user_agent', now())";
db_query($sql);
}
if (!isset($_POST['download_code'])){
$error = "Invalid download. File not set.";
echo "<p>$error</p>";
echo "<p>Please clear your browser's cache.</p>";
echo "<h4><a href=\"http://www.mysite.com/user/register\">Sign up</a> now or <a href=\"http://www.mysite.com/user/login\">login</a> if you are an existing member.</h4>";
$sql = "INSERT INTO `download_log` (`id`, `error`, `ip_address`, `referrer`, `user_agent`, `date_time`) VALUES (NULL, '$error', '$current_ip', '$referrer', '$user_agent', now())";
db_query($sql);
}else{
$file = $_POST['download_code'];
if ( $_SESSION['downinc'] < 4 || $user->uid ) {
header("Refresh: 5; URL=$file");
$_SESSION['downinc'] = $_SESSION['downinc'] + 1;
echo "<p>Your download will start in <span id=\"countdown\">5</span> seconds...</p>";
}else{
$uri = substr($_POST['uri'],1);
echo "<p>You have downloaded code 3 times.</p>";
echo "<p>To continue downloading, please login or register.</p>";
echo "<h3><a href=\"http://www.mysite.com/user/register?destination=$uri\">Sign up</a> now or <a href=\"http://www.mysite.com/user/login?destination=$uri\">login</a> if you are an existing member.</h3>";
}
}
db_set_active();
?>
<p>Problems with the download? Please go back to previous <a href="<?php echo $referrer ?>">page.</a></p>
<p>If you have problem downloading this file, kindly <a href="http://www.mysite.com/contact">contact us</a>.</p>
So anyone can guide me on where to start? What hook do I need in order to catch the link from Download Code
created using display suite
?
BTW, I'm using drupal 7.