<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="http://www.fustrate.com/dev/sources/Other/modification.xsl"?>
<!DOCTYPE modification SYSTEM "http://www.simplemachines.org/xml/modification">
<modification xmlns="http://www.simplemachines.org/xml/modification" xmlns:smf="http://www.simplemachines.org/">
	<id>fustrate:twitly</id>
	<version>1.0.2</version>

	<file name="$sourcedir/Load.php">
		<operation>
			<search position="before"><![CDATA[
				b.ID_THEME, b.override_theme, b.permission_mode, b.countPosts]]></search>
			<add><![CDATA[, b.allow_twitter]]></add>
		</operation>

		<operation>
			<search position="before"><![CDATA[
				'override_theme' => !empty($row['override_theme']),
				'use_local_permissions' => !empty($modSettings['permission_enable_by_board']) && $row['permission_mode'] == 1,
				'permission_mode' => empty($modSettings['permission_enable_by_board']) ? (empty($row['permission_mode']) ? 'normal' : ($row['permission_mode'] == 2 ? 'no_polls' : ($row['permission_mode'] == 3 ? 'reply_only' : 'read_only'))) : 'normal',
				'posts_count' => empty($row['countPosts']),]]></search>
			<add><![CDATA[
				'allow_twitter' => !empty($row['allow_twitter']),]]></add>
		</operation>
	</file>

	<file name="$sourcedir/ManageBoards.php">
		<operation>
			<search position="before"><![CDATA[
			'no_children' => true,
			'permission_mode' => 'normal',]]></search>
			<add><![CDATA[
			'allow_twitter' => 0,]]></add>
		</operation>

		<operation>
			<search position="before"><![CDATA[
		// Checkboxes....
		$boardOptions['posts_count'] = isset($_POST['count']);]]></search>
			<add><![CDATA[
		$boardOptions['allow_twitter'] = isset($_POST['allow_twitter']);]]></add>
		</operation>
	</file>

	<file name="$sourcedir/ManagePermissions.php">
		<operation>
			<search position="before"><![CDATA[
			'post' => array(
				'delete' => true,
				'modify' => true,
				'report_any' => false,]]></search>
			<add><![CDATA[
				'post_twitter' => false,]]></add>
		</operation>

		<operation>
			<search position="before"><![CDATA[
		'manage_membergroups',
		'manage_permissions',
		'manage_bans',
		'send_mail',]]></search>
			<add><![CDATA[
		'post_twitter',]]></add>
		</operation>
	</file>

	<file name="$sourcedir/ModSettings.php">
		<operation>
			<search position="before"><![CDATA[
			// Reporting of personal messages?
			array('check', 'enableReportPM'),]]></search>
			<add><![CDATA[
			array('text', 'twitter_login'),
			array('text', 'twitter_pass'),
			array('text', 'bitly_login'),
			array('text', 'bitly_key'),]]></add>
		</operation>
	</file>

	<file name="$sourcedir/Post.php">
		<operation>
			<search position="before"><![CDATA[
	$context['is_first_post'] = $context['is_new_topic'] || (isset($_REQUEST['msg']) && $_REQUEST['msg'] == $ID_FIRST_MSG);]]></search>
			<add><![CDATA[

	$context['allow_twitter'] = allowedTo('post_twitter') && $board_info['allow_twitter'];]]></add>
		</operation>

		<operation>
			<search position="before"><![CDATA[
	elseif (empty($_REQUEST['msg']))
		sendNotifications($topic, 'reply');]]></search>
			<add><![CDATA[

	// Are we posting this to Twitter?
	if (allowedTo('post_twitter') && $board_info['allow_twitter'] && isset($_POST['post_twitter']) && isset($modSettings['twitter_login']) && isset($modSettings['twitter_pass'])){
		$tweet = $_POST['subject'];

		// Can we use bit.ly? Add it to the beginning of the tweet.
		if (isset($modSettings['bitly_login']) && isset($modSettings['bitly_key'])){
			// Just in case it wasn't global before, cuz we'll need it
			global $scripturl;

			// Save it for the check further on.
			$can_bitly = true;

			$message_id = !empty($_REQUEST['msg']) ? $_REQUEST['msg'] : $msgOptions['id'];

			$tweet = create_bitly($scripturl . '?topic=' . $topic . '.msg' . $message_id . '%23msg' . $message_id) . ' ' . $tweet;
		}

		if (!isset($_POST['ignore_twitter'])){
			// Let's take a look at the first lines of the topic - is it twitterish?
			$lines = explode("<br />", $msgOptions['body']);

			if (isset($can_bitly) && file_get_contents(($lines[0]))){
				$tweet .= ' ' . create_bitly(trim($lines[0]));

				// Remove the first line for the next part.
				$lines = array_slice($lines, 1);
			}

			// Add the new first line, for any #stuff or @things (I don't use twitter, so excuse my noobishness)
			if (in_array(substr($lines[0], 0, 1), array('#', '@')))
				$tweet .= ' ' . $lines[0];
		}

		update_twitter($tweet);
	}]]></add>
		</operation>

		<operation>
			<search position="end" />
			<add><![CDATA[

function update_twitter($status)
{
	global $modSettings;

	$curl_handle = curl_init();

	curl_setopt($curl_handle, CURLOPT_URL, 'http://twitter.com/statuses/update.xml');
	curl_setopt($curl_handle, CURLOPT_USERPWD, $modSettings['twitter_login'] . ':' . $modSettings['twitter_pass']);
	curl_setopt($curl_handle, CURLOPT_POSTFIELDS, "status=" . $status);
	curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
	curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($curl_handle, CURLOPT_POST, 1);
	curl_setopt($curl_handle, CURLOPT_HTTPHEADER, array('Expect:'));

	$twitter_data = curl_exec($curl_handle);

	curl_close($curl_handle);
}

function create_bitly($url)
{
	global $modSettings;

	$file = file_get_contents('http://api.bit.ly/shorten?version=2.0.1&longUrl=' . $url . '&login=' . $modSettings['bitly_login'] . '&apiKey=' . $modSettings['bitly_key'] . '&format=xml');

	preg_match('~<shortUrl>(.*)<\/shortUrl>~iU', $file, $matches);

	return $matches[1];
}]]></add>
		</operation>
	</file>

	<file name="$sourcedir/Subs-Boards.php">
		<operation>
			<search position="before"><![CDATA[
	// This setting is a little twisted in the database...
	if (isset($boardOptions['posts_count']))
		$boardUpdates[] = 'countPosts = ' . ($boardOptions['posts_count'] ? '0' : '1');]]></search>
			<add><![CDATA[

	// Tweet!
	if (isset($boardOptions['allow_twitter']))
		$boardUpdates[] = 'allow_twitter = ' . ($boardOptions['allow_twitter'] ? '1' : '0');]]></add>
		</operation>

		<operation>
			<search position="before"><![CDATA[
		'moderators' => '',
		'inherit_permissions' => true,]]></search>
			<add><![CDATA[
		'allow_twitter' => false,]]></add>
		</operation>

		<operation>
			<search position="before"><![CDATA[
			b.boardOrder, b.countPosts, b.memberGroups, b.ID_THEME, b.override_theme,]]></search>
			<add><![CDATA[ b.allow_twitter,]]></add>
		</operation>

		<operation>
			<search position="before"><![CDATA[
				'prev_board' => $prevBoard]]></search>
			<add><![CDATA[,
				'allow_twitter' => $row['allow_twitter']]]></add>
		</operation>
	</file>

	<file name="$themedir/ManageBoards.template.php">
		<operation>
			<search position="before"><![CDATA[
								<td valign="top" align="right">
									<input type="checkbox" name="count" ', $context['board']['count_posts'] ? ' checked="checked"' : '', ' class="check" />
								</td>]]></search>
			<add><![CDATA[
							</tr><tr>
								<td>
									<b>', $txt['mboards_allow_twitter'], '</b><br />
									', $txt['mboards_allow_twitter_desc'], '<br /><br />
								</td>
								<td valign="top" align="right">
									<input type="checkbox" name="allow_twitter" ', $context['board']['allow_twitter'] ? ' checked="checked"' : '', ' class="check" />
								</td>]]></add>
		</operation>
	</file>

	<file name="$themedir/Post.template.php">
		<operation>
			<search position="before"><![CDATA[
														<td class="smalltext"><label for="check_announce"><input type="checkbox" name="announce_topic" id="check_announce" value="1" class="check" /> ' . $txt['announce_topic'] . '</label></td>
														<td class="smalltext"></td>]]></search>
			<add><![CDATA[
													</tr>' : '', $context['allow_twitter'] ? '
													<tr>
														<td class="smalltext"><label for="post_twitter"><input type="checkbox" name="post_twitter" id="post_twitter" class="check" /> ' . $txt['post_to_twitter'] . '</label></td>
														<td class="smalltext"><label for="ignore_twitter"><input type="checkbox" name="ignore_twitter" id="ignore_twitter" class="check" /> ' . $txt['ignore_twitter'] . '</label></td>]]></add>
		</operation>
	</file>

	<file name="$languagedir/Modifications.english.php">
		<operation>
			<search position="end" />
			<add><![CDATA[
$txt['twitter_login'] = 'Twitter Login';
$txt['twitter_pass'] = 'Twitter Password';
$txt['bitly_login'] = 'Bit.ly Login';
$txt['bitly_key'] = 'Bit.ly API Key';
$txt['mboards_allow_twitter'] = 'Allowed to Twitter';
$txt['mboards_allow_twitter_desc'] = 'Authorized users can post their topic titles and urls to your Twitter account.';
$txt['post_to_twitter'] = 'Post to Twitter';
$txt['ignore_twitter'] = 'Minimal Tweet';
$txt['permissionname_post_twitter'] = 'Allowed to Twitter Posts';
$txt['permissionhelp_post_twitter'] = 'With this permission, users in select groups can tweet their posts in boards that allow Twittering.';]]></add>
		</operation>
	</file>

	<file name="$languagedir/Modifications.english-utf8.php" error="skip">
		<operation>
			<search position="end" />
			<add><![CDATA[
$txt['twitter_login'] = 'Twitter Login';
$txt['twitter_pass'] = 'Twitter Password';
$txt['bitly_login'] = 'Bit.ly Login';
$txt['bitly_key'] = 'Bit.ly API Key';
$txt['mboards_allow_twitter'] = 'Allowed to Twitter';
$txt['mboards_allow_twitter_desc'] = 'Authorized users can post their topic titles and urls to your Twitter account.';
$txt['post_to_twitter'] = 'Post to Twitter';
$txt['ignore_twitter'] = 'Minimal Tweet';
$txt['permissionname_post_twitter'] = 'Allowed to Twitter Posts';
$txt['permissionhelp_post_twitter'] = 'With this permission, users in select groups can tweet their posts in boards that allow Twittering.';]]></add>
		</operation>
	</file>
</modification>
