Location: PHPKode > projects > Zzap > zzap/app/plugins/twitter/twitterPlugin.php
<?php 
/**
 * zzap group messaging / microblogging
 * 
 * Copyright (C) 2008 Dirk Ollmetzer
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * @package Zzap
 * @subpackage plugins
 * @author Dirk Ollmetzer <hide@address.com>
 * @copyright 2008 Dirk Ollmetzer
 * @version $Id: twitterPlugin.php 148 2008-10-27 20:37:34Z dollmetzer $
 * @license GPLv3 http://www.gnu.org/copyleft/gpl.html
 */

require_once PATH_LIB.'Plugin.php';
require_once PATH_APP.'plugins/twitter/twitterModel.php';
require_once PATH_APP.'plugins/twitter/twitterLib.php';
require_once PATH_APP.'plugins/twitter/twitterFetch.php';

/**
 * twitter plugin
 * 
 * Extends zzap with functionallity for receiving and sending
 * messages to and from twitter.
 * 
 * @package Zzap
 * @subpackage plugins
 * @author Dirk Ollmetzer <hide@address.com>
 * @copyright 2008 Dirk Ollmetzer
 * @version $Id: twitterPlugin.php 148 2008-10-27 20:37:34Z dollmetzer $
 * @license GPLv3 http://www.gnu.org/copyleft/gpl.html
 */
class twitterPlugin extends Plugin
{
	
	/**
	 * Hook function
	 * Called, when user is visiting his startpage.
	 * Possible use: poll new messages from external sources
	 *
	 * @param array $user User data from DB
	 */
	public function userHomepage($user) {

		// fetch twittermessages
		$fetcher = new twitterFetch();
		$fetcher->refresh($user);
	
	}

	
	/**
	 * Hook function
	 * Called from pluginController to display and process the user settings
	 * for this plugin.
	 */
	public function userSettings() {
		
		//echo "twitterPlugin::userSettings()<br />\n";
		$language = $GLOBALS['application']->getLanguage();
		
		$twitterModel = new twitterModel();
		$twitter = $twitterModel->getTwitterByUser($GLOBALS['session']->get('user_id')); 
		
		// fetch and check form data
		$error = array();
		if(isset($_POST['f_submit'])) {
			$f_twittername = substr(strip_tags($_POST['f_twittername']), 0, 64);
			$f_twitterpass = substr(strip_tags($_POST['f_twitterpassword']), 0, 64);
			$f_twitterpush = strip_tags($_POST['f_push']);
			$f_twitterpull = strip_tags($_POST['f_pull']);

			if($f_twitterpush != '') {
				$f_twitterpush = 1;
			} else {
				$f_twitterpush = 0;
			}
		
			if($f_twitterpull != '') {
				$f_twitterpull = 1;
			} else {
				$f_twitterpull = 0;
			}
			
			if(sizeof($error) == 0) {
				$twitterModel->saveTwitter($GLOBALS['session']->get('user_id'), $f_twittername, $f_twitterpass, $f_twitterpush, $f_twitterpull);
			}
			
			$twitter['twittername'] = $f_twittername;
			$twitter['twitterpass'] = $f_twitterpass;
			$twitter['push'] = $f_twitterpush;
			$twitter['pull'] = $f_twitterpull;
			
		}
		
		$GLOBALS['application']->content['formError'] = $error;
		
		// set plugin view partial
		$GLOBALS['application']->content['piView']  = 'twitter/views/'.$GLOBALS['session']->get('ua_markup').'_settings.php';
		$GLOBALS['application']->content['piName']  = 'twitter';
		$GLOBALS['application']->content['piTitle'] = $language['headline'];
		$GLOBALS['application']->content['twitter'] = $twitter;
		
	} /* userSettings() */
	
	
	
	/**
	 * Hook function
	 * Called from the asynchronous messageProcess script after
	 * the publishing of a message.
	 * Sends a message to twitter, if push is demanded in the settings and name
	 * and password of the twitteraccount is set. 
	 * 
	 * @param array $user    Sender. Associative array of record set from DB table 'user'
	 * @param array $message Message. Associative array of record set from DB table 'message'
	 */
	public function messageProcess($user, $message) {

		$GLOBALS['application']->writeLog('Twitter Plugin :: messageProcess started', 'messageProcess');
		$twitterModel = new twitterModel();
		$twitterData = $twitterModel->getTwitterByUser($message['userid']);
		$GLOBALS['application']->writeLog('twitterData : '.print_r($twitterData, true), 'messageProcess');
		
		if(($twitterData['twittername'] != '') AND ($twitterData['twitterpass'] != '') AND ($twitterData['push'] == 1)) {
			$GLOBALS['application']->writeLog('Twitter User name '.$twitterData['twittername'], 'messageProcess');
			
			// check, if link to detailpage shall be appended
			$link = $GLOBALS['application']->buildLink('state/show', $message['id']);
			$append = false;
			if(file_exists(PATH_HTDOCS."img/msg/".$message['id'].'_480_'.$message['userid'].'.jpg')) {
				$append  = true;
			}
			
			if($append) {
				// append, if link fits into 140 characters
				if(strlen($message['message'].' '.$link) <= 140) {
					//$message['message'] = $message['message'].' '.$link;
					$linkmessage = $message['message'].' '.$link;
				} else {
					$linkmessage = $message['message'];
				}
			} else {
				$linkmessage = $message['message'];
			}
			
			$twitter = new twitterLib($twitterData['twittername'], $twitterData['twitterpass']);
			$twitterResponse = $twitter->updateStatus($linkmessage);
		
			$GLOBALS['application']->writeLog('sent to twitter: "'.$linkmessage.'"', 'messageProcess');
		}

	} /* messageProcess($user, $message) */


}
?>
Return current item: Zzap