MyBB Community Forums

Full Version: Attach link
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Here is the plugin:- http://mods.mybb.com/view/attach-link

1 week before, it is working fine, when we download a file, it will be downloaded from my other server url, i.e. powerhost.com/myFile.txt
But now, it is downloading as a attachment from my URL, i.e. exmple.com/attachment.php?aid=5

I've checked my uploads folder, but there is no attachment , So do any one know what is this issue about?
Have you CHMOD that folder to 777 ?
Which folder? Uploads, (yes it is 777, but I think its not the case)
Attachments are working fine, as per this plugin, it attaches the link, so the file must be download from that url, not from my server, but in my case, when I attach the url, it works fine for some days (download from other url) but after some time, It shows me the url of file from my server,

in short,
attach link , Download file from :- powerhost.com/myfile.txt
Attachment, Download file from:- mywebsite.com/attachment.php?aid=4

but in my case

attach link , Download file from :- mywebsite.com/attachment.php?aid=4 (the link chnged after some time, but file didn't exist on my website, it is still on powerhost and downloadable, the whole mess is about the url)
Attachment, Download file from:- mywebsite.com/attachment.php?aid=4
Paste your plugin code here please, I'd have a look.
It's not mine btw :p
<?php

if(!defined("IN_MYBB"))
{
	die("Direct initialization of this file is not allowed.<br /><br />Please make sure IN_MYBB is defined.");
}
$plugins->add_hook('newreply_start', 'makeattachment_newreply');
$plugins->add_hook('newreply_do_newreply_start', 'makeattachment_newreply');
$plugins->add_hook('editpost_start', 'makeattachment_editpost');
$plugins->add_hook("newthread_start", 'makeattachment_newthread');
$plugins->add_hook("newthread_do_newthread_start", 'makeattachment_newthread');
$plugins->add_hook('attachment_end', 'downloadattachment');

function attachlink_info()
{
	return array(
		"name" => "Attach Link",
		"description" => "Attach simple links to seem just like normal files attached in your forum .",
		"website" => "http://www.mybbiran.com",
		"author" => "Ahmad Badkoubehei",
		"authorsite" => "http://www.aucs.ir",
		"version" => "1.0",
		'guid'        => 'bedd42564b3d18ae00cb4115b2a200a8'
	);
}

function attachlink_install()
{
	global $templates,$db;

	require_once MYBB_ROOT."inc/adminfunctions_templates.php";

	$templatearray = array(
		'title' => 'attachlink_attchlink',
		'template' => "<tr class=\"trow1\">
<td colspan=\"3\" style=\"padding: 0px\">
<br /><b>{\$lang->attachlink_title}</b><br /><br />
<table width=\"100%\" border=\"0\" style=\"margin: 0\">
<tr>
<td class=\"trow1\" width=\"1\"><img src=\"{\$theme[\'imgdir\']}/paperclip.gif\" alt=\"\" /></td>
<td class=\"trow1\" style=\"white-space: nowrap\" width=\"1\"><strong>{\$lang->attachlink_url}</strong></td>
<td><input type=\"text\" name=\"link_attachment_url\" size=\"30\" />{\$lang->attachlink_required}</td><td class=\"trow1\" align=\"center\">
</td>
</tr>
<tr>
<td class=\"trow1\" width=\"1\"><img src=\"{\$theme[\'imgdir\']}/paperclip.gif\" alt=\"\" /></td>
<td class=\"trow1\" style=\"white-space: nowrap\"><strong>{\$lang->attachlink_filename}<strong></td>
<td><input type=\"text\" name=\"link_attachment_name\" size=\"30\" /></td>
<td class=\"trow1\" align=\"center\">
</td>
</tr>
<tr>
<td class=\"trow1\" width=\"1\"><img src=\"{\$theme[\'imgdir\']}/paperclip.gif\" alt=\"\" /></td>
<td class=\"trow1\" style=\"white-space: nowrap\"><strong>{\$lang->attachlink_filesize}</strong></td>
<td><input type=\"text\" name=\"link_attachment_size\" size=\"30\" /></td>
<td class=\"trow1\" align=\"center\">
<input type=\"submit\" class=\"button\" name=\"newlinkattachment\" value=\"{\$lang->add_attachment}\"  tabindex=\"13\" onclick=\"NewLinkAttachment()\" />
</td>
</tr>
</table>
<script type=\"text/javascript\">
function NewLinkAttachment(){
document.input.action.value = \"newlinkattach\";
}
</script>
</td>
</tr>",
		'sid' => '-1',
		);
	$db->insert_query("templates", $templatearray);
	
	$templatearray = array(
		'title' => 'confirm_attachment_download',
		'template' => "<html>
<head>
<title>{\$title}</title>
{\$headerinclude}
</head>
<body>
<br />
<br />
<br />
<br />
<div style=\"margin: auto auto; width: {\$lang->redirect_width}\" align=\"center\">
<table border=\"0\" cellspacing=\"{\$theme[\'borderwidth\']}\" cellpadding=\"{\$theme[\'tablespace\']}\" class=\"tborder\">
<tr>
<td class=\"thead\"><strong>{\$title}</strong></td>
</tr>
<tr>
<td class=\"trow1\" align=\"center\"><p>{\$lang->attachlink_confirmdownload}<br /><a href=\"{\$url}\">{\$url}</a>
<br /><br />
<input type=\"button\" value=\"{\$lang->yes}\" onclick=\"javascript:window.location=\'{\$url}\'\" />
<input type=\"button\" value=\"{\$lang->no}\" onclick=\"javascript:window.close()\" />
</p></td>
</tr>
</table>
</div>
</body>
</html>" ,
		'sid' => '-1',
		);
	$db->insert_query("templates", $templatearray);
	
	find_replace_templatesets("post_attachments_new", '#'.preg_quote('</tr>').'#', '</tr>{$attachlink_new}');
	
	if(!$db->field_exists("atype","attachments"))
		$db->query("ALTER TABLE ".TABLE_PREFIX."attachments ADD `atype` INT NOT NULL DEFAULT 0");
}

function attachlink_uninstall()
{
	global $templates,$db;

	require_once MYBB_ROOT."inc/adminfunctions_templates.php";
	
	$db->delete_query("templates", "title='attachlink_attchlink'");
	$db->delete_query("templates", "title='confirm_attachment_download'");
	
	find_replace_templatesets("post_attachments_new", '#'.preg_quote('{$attachlink_new}').'#','');
	
	if($db->field_exists("atype","attachments"))
		$db->query("ALTER TABLE ".TABLE_PREFIX."attachments DROP `atype`");
}

function attachlink_is_installed()
{
	global $db;
	if($db->field_exists("atype","attachments"))
		return true;
	else
		return false;
}

function attachlink_activate()
{
	global $db;
	
	$set_group = array(
		"name"			=> "attachlink",
		"title"			=> "Attach Link",
		"description"	=> "Configuration of Attach Link plugin",
		"disporder"		=> "0",
		"isdefault"		=> "1"
	);
	
	$db->insert_query("settinggroups", $set_group);
	$gid = $db->insert_id();
	
	$setting[] = array(
		"name"			=> "attachlink_confirm_download",
		"title"			=> "Need user confirmation to download attachment ?",
		"description"	=> "If set ON , when user clicks on download link of a Link Attachment , a page is shown to get confirmation to redirect him . Else it will redirect immediately .",
		"optionscode"	=> "onoff",
		"value"			=> '1',
		"disporder"		=> '1',
		"gid"			=> intval($gid),
	);
	
	foreach($setting as $t)
	{
		$db->insert_query("settings", $t);
	}
	
	rebuild_settings();
}

function attachlink_deactivate()
{
	global $db;
	
	$db->delete_query("settings", "name IN ('attachlink_confirm_download')");
	$db->delete_query("settinggroups", "name='attachlink'");
	
	rebuild_settings();
}

function do_makeattachment($tpid)
{
	global $mybb,$db,$tid,$linkattached;
	if(!$linkattached){
		$posthash = $mybb->input['posthash'];
		$path_info = pathinfo($mybb->input['link_attachment_url']);
		
		if($mybb->input['link_attachment_size'])
			$fsize=intval($mybb->input['link_attachment_size']);
		else
			$fsize=remote_file_size($mybb->input['link_attachment_url']);
		
		if($mybb->input['link_attachment_name'])
			$filename=$mybb->input['link_attachment_name'];
		else{
			$filename=basename($mybb->input['link_attachment_url']);
			if(strpos($filename,"?")!==FALSE)
				$filename=substr($filename,0,strpos($filename,"?"));
		}
		
		$attacharray = array(
			"pid" => $tpid,
			"posthash" => $posthash,
			"uid" => $mybb->user['uid'],
			"filename" => $filename,
			"filetype" => $path_info['extension'],
			"filesize" => $fsize,
			"attachname" => $mybb->input['link_attachment_url'],
			"downloads" => 0,
			"visible" => 1,
			"dateuploaded" => TIME_NOW,
			"atype" => 1
		);
		
		$aid = $db->insert_query("attachments", $attacharray);
		
		require_once MYBB_ROOT."inc/functions.php";
		update_thread_counters($tid, array("attachmentcount" => "+1"));
		$linkattached=1;
		return $aid;
	}
}

function makeattachment_newreply()
{
	global $mybb,$lang,$tid,$theme,$attachlink_new,$templates;
	
	$lang->load("attachlink");
	
	eval("\$attachlink_new = \"".$templates->get("attachlink_attchlink")."\";");
	
	$tid = intval($mybb->input['tid']);
	
	if($mybb->input['link_attachment_url']!=""){
		do_makeattachment(intval($pid));
	
		if(!$mybb->input['submit'])
		{
			$mybb->input['action'] = "newreply";
			$mybb->input['newattachment'] = "newattachment";
		}
	}
}

function makeattachment_editpost()
{
	global $mybb,$lang,$tid,$db,$theme,$attachlink_new,$templates;
	
	$lang->load("attachlink");
	
	eval("\$attachlink_new = \"".$templates->get("attachlink_attchlink")."\";");
	
	$pid = intval($mybb->input['pid']);
	$query = $db->simple_select("posts", "*", "pid='$pid'");
	$post = $db->fetch_array($query);
	$tid = $post['tid'];
	
	if($mybb->input['link_attachment_url']!=""){
		do_makeattachment(intval($mybb->input['pid']));
		
		if(!$mybb->input['submit'])
		{
			$mybb->input['action'] = "editpost";
			$mybb->input['newattachment'] = "newattachment";
		}
	}
}

function makeattachment_newthread()
{
	global $mybb,$lang,$theme,$attachlink_new,$templates;
	
	$lang->load("attachlink");
	
	eval("\$attachlink_new = \"".$templates->get("attachlink_attchlink")."\";");
	
	if($mybb->input['link_attachment_url']!=""){
		do_makeattachment(intval($mybb->input['pid']));
		
		if(!$mybb->input['submit'])
		{
			$mybb->input['action'] = "newthread";
			$mybb->input['newattachment'] = "newattachment";
		}
	}
}

function downloadattachment(){
	global $mybb,$db,$templates;
	$aid = intval($mybb->input['aid']);

	$query = $db->simple_select("attachments", "*", "aid='{$aid}'");
	$attachment = $db->fetch_array($query);
	
	if($attachment['atype']==0)
		return;
	$url=$attachment['attachname'];
	$url = htmlspecialchars_decode($url);
	$url = str_replace(array("\n","\r",";"), "", $url);
	if(intval($mybb->input['acceptdownload'])==1 || intval($mybb->settings['attachlink_confirm_download'])==0){
		Header( "HTTP/1.1 301 Moved Permanently" );
		Header( "Location: ".$url );
	}
	else{
		confirmdownload($url);
	}
}

function confirmdownload($url)
{
	global $header, $footer, $mybb, $theme, $headerinclude, $templates, $lang, $plugins;

	$lang->load("attachlink");
	
	$title = $mybb->settings['bbname'];

	$url = str_replace("&amp;", "&", $url);
	$url = htmlspecialchars($url);
	eval("\$confirmdownloadtext = \"".$templates->get("confirm_attachment_download")."\";");
	output_page($confirmdownloadtext);
	exit;
}

function remote_file_size ($url){ 
	$head = ""; 
	$url_p = parse_url($url); 
	$host = $url_p["host"]; 
	if(!preg_match("/[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*/",$host)){
		// a domain name was given, not an IP
		$ip=gethostbyname($host);
		if(!preg_match("/[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*/",$ip)){
			//domain could not be resolved
			return -1;
		}
	}
	$port = intval($url_p["port"]); 
	if(!$port) $port=80;
	$path = $url_p["path"]; 
	//echo "Getting " . $host . ":" . $port . $path . " ...";

	$fp = fsockopen($host, $port, $errno, $errstr, 20); 
	if(!$fp) { 
		return false; 
		} else { 
		fputs($fp, "HEAD "  . $url  . " HTTP/1.1\r\n"); 
		fputs($fp, "HOST: " . $host . "\r\n"); 
		fputs($fp, "User-Agent: http://www.example.com/my_application\r\n");
		fputs($fp, "Connection: close\r\n\r\n"); 
		$headers = ""; 
		while (!feof($fp)) { 
			$headers .= fgets ($fp, 128); 
			} 
		} 
	fclose ($fp); 
	//echo $errno .": " . $errstr . "<br />";
	$return = -2; 
	$arr_headers = explode("\n", $headers); 
	// echo "HTTP headers for <a href='" . $url . "'>..." . substr($url,strlen($url)-20). "</a>:";
	// echo "<div class='http_headers'>";
	foreach($arr_headers as $header) { 
		// if (trim($header)) echo trim($header) . "<br />";
		$s1 = "HTTP/1.1"; 
		$s2 = "Content-Length: "; 
		$s3 = "Location: "; 
		if(substr(strtolower ($header), 0, strlen($s1)) == strtolower($s1)) $status = substr($header, strlen($s1)); 
		if(substr(strtolower ($header), 0, strlen($s2)) == strtolower($s2)) $size   = substr($header, strlen($s2));  
		if(substr(strtolower ($header), 0, strlen($s3)) == strtolower($s3)) $newurl = substr($header, strlen($s3));  
		} 
	// echo "</div>";
	if(intval($size) > 0) {
		$return=strval($size);
	} else {
		$return=$status;
	}
	// echo intval($status) .": [" . $newurl . "]<br />";
	if (intval($status)==302 && strlen($newurl) > 0) {
		// 302 redirect: get HTTP HEAD of new URL
		$return=remote_file_size($newurl);
	}
	return $return; 
} 

?>

The code looks fine to me. Have you setup correct URL? And make sure settings are fine?