<?php

$plugins->add_hook('datahandler_post_update', 'edithistory_post_update');
$plugins->add_hook('newreply_end', 'edithistory_newreply');
$plugins->add_hook('showthread_start', 'edithistory_showthread');


function edithistory_info()
{
	return array(
		'name'			=> 'Edit History',
		'description'	=> 'Saves all edits done to posts.',
		'website'		=> 'http://mybbhacks.zingaburga.com/',
		'author'		=> 'ZiNgA BuRgA',
		'authorsite'	=> 'http://zingaburga.com/',
		'version'		=> '1.0'
	);
}

function edithistory_activate()
{
	global $db;
	$db->query('CREATE TABLE '.TABLE_PREFIX.'post_edithistory (
		  eid int unsigned NOT NULL auto_increment,
		  pid int(10) unsigned NOT NULL default 0,
		  subject varchar(120) NOT NULL default "",
		  icon smallint unsigned NOT NULL default 0,
		  uid int unsigned NOT NULL default 0,
		  dateline bigint(30) NOT NULL default 0,
		  message text NOT NULL,
		  ipaddress varchar(30) NOT NULL default "",
		  includesig char(3) NOT NULL default "",
		  smilieoff char(3) NOT NULL default "",
		  PRIMARY KEY (eid),
		  KEY (pid),
		  KEY (dateline)
	) TYPE=MyISAM');
	
	$db->query('ALTER TABLE '.TABLE_PREFIX.'posts ADD (
		editipaddress varchar(30) NOT NULL default ""
	)');
	
	$newtemplate = '<html>
<head>
<title>{$post[\'subject\']}</title>
{$headerinclude}
<script type="text/javascript">
		 var quickdelete_confirm = "{$lang->quickdelete_confirm}";
</script>
<script type="text/javascript" src="jscripts/thread.js?ver=1212"></script>
</head>
<body>
{$header}
{$multipage}
<table border="0" cellspacing="{$theme[\'borderwidth\']}" cellpadding="{$theme[\'tablespace\']}" class="tborder" style="clear: both;">
<tr>
<td class="thead" colspan="2">
	<div>
		<strong>{$post[\'subject\']}</strong>
	</div>
</td>
</tr>
<tr>
<td class="tcat" width="15%"><span class="smalltext"><strong>{$lang->author}</strong></span></td>
<td class="tcat" width="85%"><span class="smalltext"><strong>{$lang->message}</strong></span></td>
</tr>
{$posts}
<tr>
<td colspan="2" class="tfoot">
{$multipage}
</td>
</tr>
</table>
<br />
{$footer}
</body>
</html>';
	
	$db->insert_query(TABLE_PREFIX.'templates', array(
		'title' => 'edithistory',
		'template' => $db->escape_string($newtemplate),
		'sid' => -1,
		'version' => 120
	));
}

function edithistory_deactivate()
{
	global $db;
	$db->query('DROP TABLE '.TABLE_PREFIX.'post_edithistory');
	$db->query('ALTER TABLE '.TABLE_PREFIX.'posts DROP editipaddress');
	
	$db->delete_query(TABLE_PREFIX.'templates', 'title = "edithistory"');
}


function edithistory_post_update(&$ph)
{
	//$oldpost = $db->fetch_array($db->simple_select(MY_TABLE_PREFIX.'posts', '*', 'pid='.intval($ph->data['pid'])));
	global $eh_override;
	if($eh_override) return;
	$oldpost = get_post($ph->data['pid']);
	if(!$oldpost) return;
	
	if(!$oldpost['edit_uid']) $oldpost['edit_uid'] = $oldpost['uid'];
	if(!$oldpost['edittime']) $oldpost['edittime'] = $oldpost['dateline'];
	
	global $db, $session;
	$db->insert_query(TABLE_PREFIX.'post_edithistory', array(
		'pid' => $oldpost['pid'],
		'subject' => $db->escape_string($oldpost['subject']),
		'icon' => $oldpost['icon'],
		'uid' => $oldpost['edit_uid'],
		'dateline' => $oldpost['edittime'],
		'message' => $db->escape_string($oldpost['message']),
		'ipaddress' => $oldpost['editipaddress'],
		'includesig' => $oldpost['includesig'],
		'smilieoff' => $oldpost['smilieoff']
	));
	
	$ph->post_update_data['editipaddress'] = $session->ipaddress;
}

function edithistory_newreply()
{
	global $mybb;
	if($mybb->usergroup['cancp'] == 'yes' && ($eid = intval($mybb->input['eid'])))
	{
		global $message, $db;
		$msg = $db->fetch_field($db->simple_select(TABLE_PREFIX.'post_edithistory', 'message', 'eid='.$eid), 'message');
		if($msg)
			$message .= $msg;
	}
}

function edithistory_showthread()
{
	global $mybb, $templates;
	if($mybb->usergroup['cancp'] != 'yes') return;
	$templates->cache['postbit_editedby'] = str_replace('.</p>', '. [<a href="{$mybb->settings[\'bburl\']}/edithistory.php?pid={$post[\'pid\']}">Edit history</a>]</p>', $templates->cache['postbit_editedby']);
}

?>