<?php

$plugins->add_hook('xmlhttp', 'editableposts_xmlhttp');
$plugins->add_hook('postbit', 'editableposts_postbit');
$plugins->add_hook('editpost_end', 'editableposts_editpost_end');

$plugins->add_hook('datahandler_post_update', 'editableposts_post_update');
$plugins->add_hook('datahandler_post_insert_post', 'editableposts_post_insert');
$plugins->add_hook('datahandler_post_insert_thread_post', 'editableposts_post_insert');

// our hack for editpost.php
if(strtolower(basename($_SERVER['PHP_SELF'])) == 'editpost.php')
{
	$plugins->add_hook('global_end','editableposts_global_editpost');
}

define('EDITABLEPOSTS_TEMPLATE', '<br />
<label><input type="checkbox" class="checkbox" name="postoptions[editable]" value="yes" tabindex="8" {$postoptionschecked[\'editable\']} /> <b>Post is Publically Editable</b> - Allow all users to edit this post.</label>');

function editableposts_info()
{
	return array(
		'name'			=> 'Publically Editable Posts',
		'description'	=> 'Allows posters (and mods/admins) to give the option to others to allow their posts to be edited by regular users.',
		'website'		=> 'http://mybbhacks.zingaburga.com/',
		'author'		=> 'ZiNgA BuRgA',
		'authorsite'	=> 'http://zingaburga.com/',
		'version'		=> '1.0'
	);
}

function editableposts_activate()
{
	global $db;
	$db->query('ALTER TABLE '.TABLE_PREFIX.'posts ADD (
		`editable` CHAR(3) NOT NULL DEFAULT "no"
	)');
	
	require MYBB_ROOT."/inc/adminfunctions_templates.php";
	find_replace_templatesets('editpost', '#'.preg_quote('{$disablesmilies}').'#', '{$disablesmilies}{$enableeditable}');
	find_replace_templatesets('newreply', '#'.preg_quote('{$disablesmilies}').'#', '{$disablesmilies}'.EDITABLEPOSTS_TEMPLATE);
	find_replace_templatesets('newthread', '#'.preg_quote('{$disablesmilies}').'#', '{$disablesmilies}'.EDITABLEPOSTS_TEMPLATE);
}

function editableposts_deactivate()
{
	global $db;
	$db->query('ALTER TABLE '.TABLE_PREFIX.'posts DROP `editable`');
	
	require MYBB_ROOT."/inc/adminfunctions_templates.php";
	find_replace_templatesets('editpost', '#'.preg_quote('{$enableeditable}').'#', '', 0);
	find_replace_templatesets('newreply', '#'.preg_quote(EDITABLEPOSTS_TEMPLATE).'#', '', 0);
	find_replace_templatesets('newthread', '#'.preg_quote(EDITABLEPOSTS_TEMPLATE).'#', '', 0);
}

function editableposts_editpost_start()
{
	/*
	// this function removes the mod checkboxes
	global $templates;
	// template hacking is cool :)
	$templates->cache['post_attachments_attachment_mod_unapprove'] = $templates->cache['post_attachments_attachment_mod_approve'] = '';
	*/
	global $mybb, $user_cache;
	//$user_cache[$mybb->user['uid']]['issupermod'] = 'no'; // this guy is no longer a super mod :P
	$mybb->usergroup['issupermod'] = 'no';
}

function editableposts_xmlhttp()
{
	global $mybb, $post;
	if($mybb->input['action'] != 'edit_post' || !$mybb->user['uid']) return;
	editableposts_do_mod_hack();
	if($post['editable'] == 'yes') $mybb->input['postoptions']['editable'] = 'yes'; // fake this
}

function editableposts_postbit($post)
{
	global $templates, $mybb, $thread, $lang, $theme;
	if($post['button_edit'] || $post['editable'] != 'yes') return;
	
	if($forumpermissions['caneditposts'] != 'no' && $mybb->user['uid'] && $thread['closed'] != 'yes')
	{
		eval("\$post['button_edit'] = \"".$templates->get("postbit_edit")."\";");
	}
}

function editableposts_do_mod_hack()
{
	global $db, $plugins, $mybb, $forumpermissions, $thread, $user_cache, $post;
	$pid = intval($mybb->input['pid']);
	//$query = $db->simple_select(TABLE_PREFIX."posts", "*", "pid='$pid'");
	//$post = $db->fetch_array($query);
	$post = get_post($pid);
	if($post['pid'] && $post['editable'] == 'yes')
	{
		$thread = get_thread($post['tid']);
		$forumpermissions = forum_permissions($thread['fid']);
			
		if($thread['closed'] != 'yes' && $forumpermissions['caneditposts'] != 'no' && $mybb->user['uid'] != $post['uid'] && ($mybb->settings['edittimelimit'] == 0 || $post['dateline'] >= (time()-($mybb->settings['edittimelimit']*60))) && is_moderator($thread['fid'], "caneditposts") != "yes")
		{
			// make this user appear to be a moderator
			//$GLOBALS['user_cache'][$mybb->user['uid']]['permissions']['issupermod'] = 'yes';
			$mybb->usergroup['issupermod'] = 'yes';
			// mark them as a "fake" mod, for later on
			$GLOBALS['editableposts_fakemod'] = true;

			// remove the mod options checkboxes as well
			$plugins->add_hook('editpost_start',  'editableposts_editpost_start', 5);
			$plugins->add_hook('editpost_do_editpost_start',  'editableposts_editpost_start', 5);
			// whilst we're here, let's fix up a little bug in MyBB
			if($mybb->input['attachmentact'] == 'approve' || $mybb->input['attachmentact'] == 'unapprove') $mybb->input['attachmentact'] = '';
		}
	}
}

function editableposts_global_editpost()
{
	global $mybb;
	if($mybb->user['uid'] && ($mybb->input['action'] != "deletepost" || $mybb->request_method != "post"))
	{
		editableposts_do_mod_hack();
	}
}

function editableposts_editpost_end()
{
	global $fid, $mybb, $post, $enableeditable, $templates;
	if(is_moderator($fid, "caneditposts") == "yes" || $mybb->user['uid'] == $post['uid'])
	{
		if($post['editable'] == 'yes')
			$postoptionschecked['editable'] = 'checked="checked"';
		// let's do a little dirty trick ;)
		$templates->cache['editableposts_random_template'] = EDITABLEPOSTS_TEMPLATE;
		eval('$enableeditable = "'.$templates->get('editableposts_random_template').'";');
	}
}

function editableposts_post_update(&$ph)
{
	global $mybb, $editableposts_fakemod;
	$oldpost = get_post($ph->pid);
	if((is_moderator($ph->data['fid'], "caneditposts") == 'yes' && !$editableposts_fakemod) || $mybb->user['uid'] == $oldpost['uid'])
	{
		if($mybb->input['postoptions']['editable'] == 'yes') $ph->post_update_data['editable'] = 'yes';
		else $ph->post_update_data['editable'] = 'no';
	}
	else
	{
		$ph->post_update_data['editable'] = $oldpost['editable'];
	}
}

function editableposts_post_insert(&$ph)
{
	global $mybb;
	if(is_array($ph->post_insert_data)) $n = 'post_insert_data';
	if(is_array($ph->post_update_data)) $n = 'post_update_data';
	if($mybb->input['postoptions']['editable'] == 'yes') $ph->$n['editable'] = 'yes';
	else $ph->$n['editable'] = 'no';
}

?>