PageRenderTime 52ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/mem_twitter/mem_twitter.php

https://bitbucket.org/Manfre/txp-plugins
PHP | 512 lines | 365 code | 97 blank | 50 comment | 64 complexity | 439b500bf65f82efb43e08c2a6fad49c MD5 | raw file
  1. <?php
  2. // This is a PLUGIN TEMPLATE.
  3. // Copy this file to a new name like abc_myplugin.php. Edit the code, then
  4. // run this file at the command line to produce a plugin for distribution:
  5. // $ php abc_myplugin.php > abc_myplugin-0.1.txt
  6. // Plugin name is optional. If unset, it will be extracted from the current
  7. // file name. Uncomment and edit this line to override:
  8. $plugin['name'] = 'mem_twitter';
  9. // 0 = Plugin help is in Textile format, no raw HTML allowed (default).
  10. // 1 = Plugin help is in raw HTML. Not recommended.
  11. # $plugin['allow_html_help'] = 1;
  12. $plugin['version'] = '0.3.4';
  13. $plugin['author'] = 'Michael Manfre';
  14. $plugin['author_uri'] = 'http://manfre.net/';
  15. $plugin['description'] = 'This plugin will post to twitter whenever an article is published.';
  16. // Plugin types:
  17. // 0 = regular plugin; loaded on the public web side only
  18. // 1 = admin plugin; loaded on both the public and admin side
  19. // 2 = library; loaded only when include_plugin() or require_plugin() is called
  20. $plugin['type'] = 1;
  21. if (!defined('txpinterface'))
  22. @include_once('../zem_tpl.php');
  23. if (0) {
  24. ?>
  25. # --- BEGIN PLUGIN HELP ---
  26. h1(title). mem_twitter plugin
  27. h2(section summary). Summary
  28. p. This plugin will ping twitter whenever you post an article. This will only notify twitter when in site is in live mode.
  29. h2(section contact). Author Contact
  30. "Michael Manfre":mailto:mmanfre@gmail.com?subject=Textpattern%20mem_twitter%20plugin
  31. "http://manfre.net":http://manfre.net
  32. h2(section license). License
  33. p. This plugin is licensed under the "GPLv2":http://www.fsf.org/licensing/licenses/info/GPLv2.html.
  34. h2(section installation). Installation
  35. p. Go to advanced preferences and enter your twitter username, password and post message.
  36. h2(section). Preferences
  37. h3. mem_twitter_user
  38. p. This is your twitter username.
  39. h3. mem_twitter_pass
  40. p. This is your twitter password.
  41. h3. mem_twitter_msg
  42. p. This is the message format that will be posted to twitter. The string will be parsed and all instances of "{url}" will be replaced with the url to the article. All instances of "{title}" will be replaced with the article title.
  43. h3. mem_twitter_exclude_sections
  44. p. A comma separate list of sections that should not post to twitter.
  45. # --- END PLUGIN HELP ---
  46. <?php
  47. }
  48. # --- BEGIN PLUGIN CODE ---
  49. global $prefs;
  50. if (!isset($prefs['mem_twitter_user']))
  51. set_pref('mem_twitter_user', '', 'mem_twitter', 1, 'text_input');
  52. if (!isset($prefs['mem_twitter_pass']))
  53. set_pref('mem_twitter_pass','', 'mem_twitter', 1, 'mem_twitter_password_input');
  54. if (!isset($prefs['mem_twitter_msg']))
  55. set_pref('mem_twitter_msg', 'Blog Post: {title} - {url}', 'mem_twitter', 1, 'text_input');
  56. if (!isset($prefs['mem_twitter_exclude_sections']))
  57. set_pref('mem_twitter_exclude_sections', '', 'mem_twitter', 1, 'text_input');
  58. if (txpinterface == 'admin')
  59. {
  60. function mem_twitter_password_input($name, $val)
  61. {
  62. return fInput('password', $name, $val, 'edit', '', '', '', '', $name);
  63. }
  64. register_callback('mem_twitter_ping','ping');
  65. function mem_twitter_ping()
  66. {
  67. global $mem_twitter_user, $mem_twitter_pass, $mem_twitter_msg, $mem_twitter_exclude_sections, $production_status;
  68. // do nothing without a provided user/pass
  69. if (empty($mem_twitter_user) || empty($mem_twitter_pass))
  70. {
  71. trigger_error('mem_twitter missing account credentials.', E_USER_WARNING);
  72. return;
  73. }
  74. $article_id = empty($GLOBALS['ID']) ? ps('ID') : $GLOBALS['ID'];
  75. if (!empty($article_id))
  76. {
  77. $exclude = explode(',', $mem_twitter_exclude_sections);
  78. if (count($exclude) > 0)
  79. {
  80. $exclude = " AND Section NOT IN ('" . join("','", array_map('trim', $exclude)) . "')";
  81. }
  82. else
  83. $exclude = '';
  84. $article = safe_row('ID, title, url_title, Section, unix_timestamp(Posted) as Posted',
  85. 'textpattern',
  86. "ID={$article_id} AND Posted <= NOW()" . $exclude);
  87. if ($article)
  88. {
  89. if (!function_exists('permlinkurl'))
  90. {
  91. require_once(txpath.'/publish/taghandlers.php');
  92. }
  93. $t = new Twitter($mem_twitter_user, $mem_twitter_pass, 'memtwitter');
  94. $msg = empty($mem_twitter_msg) ? '{title} - {url}' : $mem_twitter_msg;
  95. $msg = str_replace('{title}', $article['title'], $msg);
  96. if (strpos($msg, '{url}') !== false)
  97. {
  98. $url = permlinkurl($article);
  99. // get tinyurl for txp article
  100. if ($url)
  101. $url = mem_tinyurl( $url );
  102. if (empty($url))
  103. $url = hu;
  104. $msg = str_replace('{url}', $url, $msg);
  105. }
  106. $res = $t->updateStatus($msg);
  107. if (!$res)
  108. {
  109. $err = $t->lastCurlError();
  110. if (!empty($err))
  111. {
  112. trigger_error('mem_twitter failed while trying to contact twitter. curl error="' . $err .'"', E_USER_WARNING);
  113. }
  114. else
  115. {
  116. trigger_error('mem_twitter failed while trying to contact twitter.', E_USER_WARNING);
  117. }
  118. }
  119. else if ($production_status != 'live')
  120. {
  121. trigger_error('mem_twitter updated status successfully.', E_USER_NOTICE);
  122. }
  123. }
  124. }
  125. else
  126. {
  127. if ($production_status != 'live')
  128. {
  129. trigger_error('mem_twitter could not find article ID', E_USER_WARNING);
  130. }
  131. }
  132. }
  133. /** Return a tinyurl of the passed url */
  134. function mem_tinyurl($url)
  135. {
  136. $u = 'http://tinyurl.com/api-create.php?url=' . urlencode($url);
  137. $tinyurl = file_get_contents($u);
  138. if ($tinyurl != 'Error')
  139. return $tinyurl;
  140. if ($production_status != 'live')
  141. {
  142. trigger_error('mem_twitter failed to get a TinyURL for "' . $url . '". Using ' + hu, E_USER_WARNING);
  143. }
  144. return false;
  145. }
  146. /*
  147. * Copyright (c) <2008> Justin Poliey <jdp34@njit.edu>
  148. *
  149. * Permission is hereby granted, free of charge, to any person
  150. * obtaining a copy of this software and associated documentation
  151. * files (the "Software"), to deal in the Software without
  152. * restriction, including without limitation the rights to use,
  153. * copy, modify, merge, publish, distribute, sublicense, and/or sell
  154. * copies of the Software, and to permit persons to whom the
  155. * Software is furnished to do so, subject to the following
  156. * conditions:
  157. *
  158. * The above copyright notice and this permission notice shall be
  159. * included in all copies or substantial portions of the Software.
  160. *
  161. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  162. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  163. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  164. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  165. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  166. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  167. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  168. * OTHER DEALINGS IN THE SOFTWARE.
  169. */
  170. class Twitter {
  171. /* Username:password format string */
  172. private $credentials;
  173. /* Contains the last HTTP status code returned */
  174. private $http_status;
  175. /* Contains the last API call */
  176. private $last_api_call;
  177. /* Contains the application calling the API */
  178. private $application_source;
  179. /* Twitter class constructor */
  180. function Twitter($username, $password, $source=false) {
  181. $this->credentials = sprintf("%s:%s", $username, $password);
  182. $this->application_source = $source;
  183. }
  184. function getPublicTimeline($format, $since_id = 0) {
  185. $api_call = sprintf("http://twitter.com/statuses/public_timeline.%s", $format);
  186. if ($since_id > 0) {
  187. $api_call .= sprintf("?since_id=%d", $since_id);
  188. }
  189. return $this->APICall($api_call);
  190. }
  191. function getFriendsTimeline($format, $id = NULL, $since = NULL) {
  192. if ($id != NULL) {
  193. $api_call = sprintf("http://twitter.com/statuses/friends_timeline/%s.%s", $id, $format);
  194. }
  195. else {
  196. $api_call = sprintf("http://twitter.com/statuses/friends_timeline.%s", $format);
  197. }
  198. if ($since != NULL) {
  199. $api_call .= sprintf("?since=%s", urlencode($since));
  200. }
  201. return $this->APICall($api_call, true);
  202. }
  203. function getUserTimeline($format, $id = NULL, $count = 20, $since = NULL) {
  204. if ($id != NULL) {
  205. $api_call = sprintf("http://twitter.com/statuses/user_timeline/%s.%s", $id, $format);
  206. }
  207. else {
  208. $api_call = sprintf("http://twitter.com/statuses/user_timeline.%s", $format);
  209. }
  210. if ($count != 20) {
  211. $api_call .= sprintf("?count=%d", $count);
  212. }
  213. if ($since != NULL) {
  214. $api_call .= sprintf("%ssince=%s", (strpos($api_call, "?count=") === false) ? "?" : "&", urlencode($since));
  215. }
  216. return $this->APICall($api_call, true);
  217. }
  218. function showStatus($format, $id) {
  219. $api_call = sprintf("http://twitter.com/statuses/show/%d.%s", $id, $format);
  220. return $this->APICall($api_call);
  221. }
  222. function updateStatus($status) {
  223. $status = urlencode(stripslashes(urldecode($status)));
  224. $api_call = sprintf("http://twitter.com/statuses/update.xml?status=%s", $status);
  225. return $this->APICall($api_call, true, true);
  226. }
  227. function getReplies($format, $page = 0) {
  228. $api_call = sprintf("http://twitter.com/statuses/replies.%s", $format);
  229. if ($page) {
  230. $api_call .= sprintf("?page=%d", $page);
  231. }
  232. return $this->APICall($api_call, true);
  233. }
  234. function destroyStatus($format, $id) {
  235. $api_call = sprintf("http://twitter.com/statuses/destroy/%d.%s", $id, $format);
  236. return $this->APICall($api_call, true, true);
  237. }
  238. function getFriends($format, $id = NULL) {
  239. // take care of the id parameter
  240. if ($id != NULL) {
  241. $api_call = sprintf("http://twitter.com/statuses/friends/%s.%s", $id, $format);
  242. }
  243. else {
  244. $api_call = sprintf("http://twitter.com/statuses/friends.%s", $format);
  245. }
  246. return $this->APICall($api_call, true);
  247. }
  248. function getFollowers($format, $lite = NULL) {
  249. $api_call = sprintf("http://twitter.com/statuses/followers.%s%s", $format, ($lite) ? "?lite=true" : NULL);
  250. return $this->APICall($api_call, true);
  251. }
  252. function getFeatured($format) {
  253. $api_call = sprintf("http://twitter.com/statuses/featured.%s", $format);
  254. return $this->APICall($api_call);
  255. }
  256. function showUser($format, $id, $email = NULL) {
  257. if ($email == NULL) {
  258. $api_call = sprintf("http://twitter.com/users/show/%s.%s", $id, $format);
  259. }
  260. else {
  261. $api_call = sprintf("http://twitter.com/users/show.xml?email=%s", $email);
  262. }
  263. return $this->APICall($api_call, true);
  264. }
  265. function getMessages($format, $since = NULL, $since_id = 0, $page = 1) {
  266. $api_call = sprintf("http://twitter.com/direct_messages.%s", $format);
  267. if ($since != NULL) {
  268. $api_call .= sprintf("?since=%s", urlencode($since));
  269. }
  270. if ($since_id > 0) {
  271. $api_call .= sprintf("%ssince_id=%d", (strpos($api_call, "?since") === false) ? "?" : "&", $since_id);
  272. }
  273. if ($page > 1) {
  274. $api_call .= sprintf("%spage=%d", (strpos($api_call, "?since") === false) ? "?" : "&", $page);
  275. }
  276. return $this->APICall($api_call, true);
  277. }
  278. function getSentMessages($format, $since = NULL, $since_id = 0, $page = 1) {
  279. $api_call = sprintf("http://twitter.com/direct_messages/sent.%s", $format);
  280. if ($since != NULL) {
  281. $api_call .= sprintf("?since=%s", urlencode($since));
  282. }
  283. if ($since_id > 0) {
  284. $api_call .= sprintf("%ssince_id=%d", (strpos($api_call, "?since") === false) ? "?" : "&", $since_id);
  285. }
  286. if ($page > 1) {
  287. $api_call .= sprintf("%spage=%d", (strpos($api_call, "?since") === false) ? "?" : "&", $page);
  288. }
  289. return $this->APICall($api_call, true);
  290. }
  291. function newMessage($format, $user, $text) {
  292. $text = urlencode(stripslashes(urldecode($text)));
  293. $api_call = sprintf("http://twitter.com/direct_messages/new.%s?user=%s&text=%s", $format, $user, $text);
  294. return $this->APICall($api_call, true, true);
  295. }
  296. function destroyMessage($format, $id) {
  297. $api_call = sprintf("http://twitter.com/direct_messages/destroy/%s.%s", $id, $format);
  298. return $this->APICall($api_call, true, true);
  299. }
  300. function createFriendship($format, $id) {
  301. $api_call = sprintf("http://twitter.com/friendships/create/%s.%s", $id, $format);
  302. return $this->APICall($api_call, true, true);
  303. }
  304. function destroyFriendship($format, $id) {
  305. $api_call = sprintf("http://twitter.com/friendships/destroy/%s.%s", $id, $format);
  306. return $this->APICall($api_call, true, true);
  307. }
  308. function friendshipExists($format, $user_a, $user_b) {
  309. $api_call = sprintf("http://twitter.com/friendships/exists.%s?user_a=%s&user_b=%s", $format, $user_a, $user_b);
  310. return $this->APICall($api_call, true);
  311. }
  312. function verifyCredentials($format = NULL) {
  313. $api_call = sprintf("http://twitter.com/account/verify_credentials%s", ($format != NULL) ? sprintf(".%s", $format) : NULL);
  314. return $this->APICall($api_call, true);
  315. }
  316. function endSession() {
  317. $api_call = "http://twitter.com/account/end_session";
  318. return $this->APICall($api_call, true);
  319. }
  320. function updateLocation($format, $location) {
  321. $api_call = sprintf("http://twitter.com/account/update_location.%s?location=%s", $format, $location);
  322. return $this->APICall($api_call, true, true);
  323. }
  324. function updateDeliveryDevice($format, $device) {
  325. $api_call = sprintf("http://twitter.com/account/update_delivery_device.%s?device=%s", $format, $device);
  326. return $this->APICall($api_call, true, true);
  327. }
  328. function rateLimitStatus($format) {
  329. $api_call = sprintf("http://twitter.com/account/rate_limit_status.%s", $format);
  330. return $this->APICall($api_call, true);
  331. }
  332. function getArchive($format, $page = 1) {
  333. $api_call = sprintf("http://twitter.com/account/archive.%s", $format);
  334. if ($page > 1) {
  335. $api_call .= sprintf("?page=%d", $page);
  336. }
  337. return $this->APICall($api_call, true);
  338. }
  339. function getFavorites($format, $id = NULL, $page = 1) {
  340. if ($id == NULL) {
  341. $api_call = sprintf("http://twitter.com/favorites.%s", $format);
  342. }
  343. else {
  344. $api_call = sprintf("http://twitter.com/favorites/%s.%s", $id, $format);
  345. }
  346. if ($page > 1) {
  347. $api_call .= sprintf("?page=%d", $page);
  348. }
  349. return $this->APICall($api_call, true);
  350. }
  351. function createFavorite($format, $id) {
  352. $api_call = sprintf("http://twitter.com/favorites/create/%d.%s", $id, $format);
  353. return $this->APICall($api_call, true, true);
  354. }
  355. function destroyFavorite($format, $id) {
  356. $api_call = sprintf("http://twitter.com/favorites/destroy/%d.%s", $id, $format);
  357. return $this->APICall($api_call, true, true);
  358. }
  359. function follow($format, $id) {
  360. $api_call = sprintf("http://twitter.com/notifications/follow/%d.%s", $id, $format);
  361. return $this->APICall($api_call, true, true);
  362. }
  363. function leave($format, $id) {
  364. $api_call = sprintf("http://twitter.com/notifications/leave/%d.%s", $id, $format);
  365. return $this->APICall($api_call, true, true);
  366. }
  367. function createBlock($format, $id) {
  368. $api_call = sprintf("http://twitter.com/blocks/create/%d.%s", $id, $format);
  369. return $this->APICall($api_call, true, true);
  370. }
  371. function destroyBlock($format, $id) {
  372. $api_call = sprintf("http://twitter.com/blocks/destroy/%d.%s", $id, $format);
  373. return $this->APICall($api_call, true, true);
  374. }
  375. function test($format) {
  376. $api_call = sprintf("http://twitter.com/help/test.%s", $format);
  377. return $this->APICall($api_call, true);
  378. }
  379. function downtimeSchedule($format) {
  380. $api_call = sprintf("http://twitter.com/help/downtime_schedule.%s", $format);
  381. return $this->APICall($api_call, true);
  382. }
  383. private function APICall($api_url, $require_credentials = false, $http_post = false) {
  384. $curl_handle = curl_init();
  385. if($this->application_source){
  386. $api_url .= "&source=" . $this->application_source;
  387. }
  388. curl_setopt($curl_handle, CURLOPT_URL, $api_url);
  389. if ($require_credentials) {
  390. curl_setopt($curl_handle, CURLOPT_USERPWD, $this->credentials);
  391. }
  392. if ($http_post) {
  393. curl_setopt($curl_handle, CURLOPT_POST, true);
  394. }
  395. curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, TRUE);
  396. $twitter_data = curl_exec($curl_handle);
  397. $this->http_status = curl_getinfo($curl_handle, CURLINFO_HTTP_CODE);
  398. $this->last_api_call = $api_url;
  399. $this->last_curl_error = curl_error($curl_handle);
  400. curl_close($curl_handle);
  401. return $twitter_data;
  402. }
  403. function lastStatusCode() {
  404. return $this->http_status;
  405. }
  406. function lastAPICall() {
  407. return $this->last_api_call;
  408. }
  409. function lastCurlError() {
  410. return $this->last_curl_error;
  411. }
  412. }
  413. }
  414. # --- END PLUGIN CODE ---
  415. ?>