Created
May 21, 2010 21:43
-
-
Save kdambekalns/409471 to your computer and use it in GitHub Desktop.
A (useless) twitter logging backend for FLOW3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(ENCODING = 'utf-8'); | |
namespace F3\FLOW3\Log\Backend; | |
/* * | |
* This script belongs to the FLOW3 framework. * | |
* * | |
* It is free software; you can redistribute it and/or modify it under * | |
* the terms of the GNU Lesser General Public License as published by the * | |
* Free Software Foundation, either version 3 of the License, or (at your * | |
* option) any later version. * | |
* * | |
* This script is distributed in the hope that it will be useful, but * | |
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- * | |
* TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser * | |
* General Public License for more details. * | |
* * | |
* You should have received a copy of the GNU Lesser General Public * | |
* License along with the script. * | |
* If not, see http://www.gnu.org/licenses/lgpl.html * | |
* * | |
* The TYPO3 project - inspiring people to share! * | |
* */ | |
/** | |
* A backend which just twitters everything | |
* | |
* To use it make sure to configure it in Objects.yaml | |
* F3\FLOW3\Log\Backend\TwitterBackend: | |
* autowiring: off | |
* scope: prototype | |
* | |
* and Settings.yaml: | |
* log: | |
* systemLogger: | |
* backend: F3\FLOW3\Log\Backend\TwitterBackend | |
* backendOptions: | |
* username: youruser | |
* password: yourpassword | |
* | |
* @version $Id$ | |
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later | |
* @api | |
* @scope prototype | |
*/ | |
class TwitterBackend extends \F3\FLOW3\Log\Backend\AbstractBackend { | |
protected $severityLabels = array( | |
LOG_EMERG => 'EMERGENCY', | |
LOG_ALERT => 'ALERT ', | |
LOG_CRIT => 'CRITICAL ', | |
LOG_ERR => 'ERROR ', | |
LOG_WARNING => 'WARNING ', | |
LOG_NOTICE => 'NOTICE ', | |
LOG_INFO => 'INFO ', | |
LOG_DEBUG => 'DEBUG ', | |
); | |
/** | |
* @var string | |
*/ | |
protected $username; | |
/** | |
* @var string | |
*/ | |
protected $password; | |
/** | |
* @param string $username | |
* @return void | |
* @author Karsten Dambekalns <karsten@typo3.org> | |
*/ | |
public function setUsername($username) { | |
$this->username = $username; | |
} | |
/** | |
* @param string $password | |
* @return void | |
* @author Karsten Dambekalns <karsten@typo3.org> | |
*/ | |
public function setPassword($password) { | |
$this->password = $password; | |
} | |
/** | |
* Does nothing | |
* | |
* @return void | |
* @author Karsten Dambekalns <karsten@typo3.org> | |
* @api | |
*/ | |
public function open() {} | |
/** | |
* Logs to twitter | |
* | |
* @param string $message The message to log | |
* @param integer $severity One of the LOG_* constants | |
* @param mixed $additionalData (ignored) | |
* @param string $packageKey (ignored) | |
* @param string $className (ignored) | |
* @param string $methodName (ignored) | |
* @return void | |
* @author Karsten Dambekalns <karsten@typo3.org> | |
* @api | |
*/ | |
public function append($message, $severity = 1, $additionalData = NULL, $packageKey = NULL, $className = NULL, $methodName = NULL) { | |
$context = stream_context_create( | |
array( | |
'http' => array( | |
'method' => 'POST', | |
'header' => sprintf("Authorization: Basic %s\r\n", base64_encode($this->username . ':' . $this->password)). | |
"Content-type: application/x-www-form-urlencoded\r\n", | |
'content' => http_build_query(array('status' => $message . ' #' . $this->severityLabels[$severity])), | |
'timeout' => 5, | |
), | |
) | |
); | |
return file_get_contents('http://twitter.com/statuses/update.xml', false, $context) !== FALSE; | |
} | |
/** | |
* Does nothing | |
* | |
* @return void | |
* @author Karsten Dambekalns <karsten@typo3.org> | |
* @api | |
*/ | |
public function close() {} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment