aboutsummaryrefslogtreecommitdiff
path: root/mod/twitterservice/vendors/twitter/twitter.class.php
blob: 457bede4c1bb9b5fb0bf64c9e1bb210532ca692f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php

/**
 * Twitter for PHP - library for sending messages to Twitter and receiving status updates.
 *
 * @author     David Grudl (+ Bugfix by Curverider)
 * @copyright  Copyright (c) 2008 David Grudl
 * @license    New BSD License
 * @link       http://phpfashion.com/
 * @version    1.0_MP
 */
class Twitter
{
	/** @var int */
	public static $cacheExpire = 1800; // 30 min

	/** @var string */
	public static $cacheDir;

	/** @var  user name */
	private $user;

	/** @var  password */
	private $pass;



	/**
	 * Creates object using your credentials.
	 * @param  string  user name
	 * @param  string  password
	 * @throws Exception
	 */
	public function __construct($user, $pass)
	{
		if (!extension_loaded('curl')) {
			throw new Exception('PHP extension CURL is not loaded.');
		}

		$this->user = $user;
		$this->pass = $pass;
	}



	/**
	 * Sends message to the Twitter.
	 * @param string   message encoded in UTF-8
	 * @return boolean TRUE on success or FALSE on failure
	 */
	public function send($message)
	{
		$result = $this->httpRequest(
			'https://twitter.com/statuses/update.xml',
			array('status' => $message)
		);
		return strpos($result, '<created_at>') !== FALSE;
	}



	/**
	 * Returns the 20 most recent statuses posted from you and your friends (optionally).
	 * @param  bool  with friends?
	 * @return SimpleXMLElement
	 * @throws Exception
	 */
	public function load($withFriends, $since = '')
	{
		$line = $withFriends ? 'friends_timeline' : 'user_timeline';
		$url = "http://twitter.com/statuses/$line/$this->user.xml";
		//if (!empty($since))
		//	$url .= "?since=" . urlencode($since);
		$feed = $this->httpRequest($url);
		if ($feed === FALSE) {
			throw new Exception('Cannot load channel.');
		}

		$xml = new SimpleXMLElement($feed);
		if (!$xml || !$xml->status) {
			throw new Exception('Invalid channel.');
		}

		return $xml;
	}



	/**
	 * Process HTTP request.
	 * @param string  URL
	 * @param array   post data
	 * @return string|FALSE
	 */
	private function httpRequest($url, $post = NULL)
	{
		/*
		if (!$post && self::$cacheDir) {
			$cacheFile = self::$cacheDir . '/twitter.' . md5($url) . '.xml';
			if (@filemtime($cacheFile) + self::$cacheExpire > time()) {
				return file_get_contents($cacheFile);
			}
		}
		*/

		$curl = curl_init();
		curl_setopt($curl, CURLOPT_URL, $url);
		// curl_setopt($curl, CURLOPT_USERPWD, "$this->user:$this->pass");
		curl_setopt($curl, CURLOPT_HEADER, FALSE);
		curl_setopt($curl, CURLOPT_TIMEOUT, 20);
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
		curl_setopt($curl, CURLOPT_FRESH_CONNECT, 1);
		curl_setopt($curl, CURLOPT_HTTPHEADER, array('Expect:'));
		if ($post) {
			curl_setopt($curl, CURLOPT_USERPWD, "$this->user:$this->pass");
			curl_setopt($curl, CURLOPT_POST, TRUE);
			curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
		}
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); // no echo, just return result
		$result = curl_exec($curl);
		$ok = curl_errno($curl) === 0 && curl_getinfo($curl, CURLINFO_HTTP_CODE) === 200;

		if (!$ok) {
			if (isset($cacheFile)) {
				$result = @file_get_contents($cacheFile);
				if (is_string($result)) {
					return $result;
				}
			}
			return FALSE;
		}

		/*
		if (isset($cacheFile)) {
			file_put_contents($cacheFile, $result);
		}
		*/

		return $result;
	}

}