PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/webapp/plugins/twitterrealtime/controller/class.TwitterRealtimePluginConfigurationController.php

http://github.com/ginatrapani/ThinkUp
PHP | 215 lines | 125 code | 20 blank | 70 comment | 22 complexity | 3f08f768a6b766544f078ebbac30d74d MD5 | raw file
Possible License(s): Apache-2.0, GPL-2.0, GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. *
  4. * ThinkUp/webapp/plugins/twitterrealltime/controller/class.TwitterRealtimePluginConfigurationController.php
  5. *
  6. * Copyright (c) 2011-2012 Mark Wilkie, Amy Unruh
  7. *
  8. * LICENSE:
  9. *
  10. * This file is part of ThinkUp (http://thinkupapp.com).
  11. *
  12. * ThinkUp is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
  13. * License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any
  14. * later version.
  15. *
  16. * ThinkUp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
  17. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  18. * details.
  19. *
  20. * You should have received a copy of the GNU General Public License along with ThinkUp. If not, see
  21. * <http://www.gnu.org/licenses/>.
  22. *
  23. *
  24. * Twitter Realtime Plugin Configuration Controller
  25. *
  26. * Handles plugin configuration requests.
  27. *
  28. * @license http://www.gnu.org/licenses/gpl.html
  29. * @author Amy Unruh
  30. */
  31. class TwitterRealtimePluginConfigurationController extends PluginConfigurationController {
  32. /**
  33. *
  34. * @var Owner
  35. */
  36. var $owner;
  37. /**
  38. * @var $int php major version num required for redis
  39. */
  40. var $php_major_version_for_redis = 5;
  41. /**
  42. * @var $int php minor version num required for redis
  43. */
  44. var $php_minor_version_for_redis = 3;
  45. /**
  46. * @return str
  47. */
  48. public function authControl() {
  49. $config = Config::getInstance();
  50. Loader::definePathConstants();
  51. $this->setViewTemplate(THINKUP_WEBAPP_PATH.'plugins/twitterrealtime/view/twitterrealtime.account.index.tpl');
  52. $this->view_mgr->addHelp('twitterrealtime', 'userguide/settings/plugins/twitterrealtime');
  53. $id = DAOFactory::getDAO('InstanceDAO');
  54. $od = DAOFactory::getDAO('OwnerDAO');
  55. // get plugin option values if defined...
  56. $plugin_options = $this->getPluginOptions();
  57. // get oauth option values from twitter plugin.
  58. // @TODO -- what is the right way to do this?
  59. $plugin_option_dao = DAOFactory::GetDAO('PluginOptionDAO');
  60. $twitter_options = $plugin_option_dao->getOptionsHash('twitter', true);
  61. $oauth_consumer_key = null;
  62. if(isset($twitter_options['oauth_consumer_key'])) {
  63. $oauth_consumer_key = $twitter_options['oauth_consumer_key']->option_value;
  64. }
  65. $oauth_consumer_secret = null;
  66. if(isset($twitter_options['oauth_consumer_secret'])) {
  67. $oauth_consumer_secret = $twitter_options['oauth_consumer_secret']->option_value;
  68. }
  69. // @TODO - get any other option values as necessary
  70. // $archive_limit = $this->getPluginOption('archive_limit');
  71. //Add public user instance
  72. if (isset($_GET['twitter_username'])) { // if form was submitted
  73. $logger = Logger::getInstance();
  74. $api = new TwitterAPIAccessorOAuth('NOAUTH', 'NOAUTH', $oauth_consumer_key, $oauth_consumer_secret,
  75. $num_twitter_errors, $max_api_calls_per_crawl);
  76. $api_call = str_replace("[id]", $_GET['twitter_username'], $api->cURL_source['show_user']);
  77. list($cURL_status, $data) = $api->apiRequestFromWebapp($api_call);
  78. if ($cURL_status == 200) {
  79. $thisFeed = array();
  80. try {
  81. $xml = $api->createParserFromString(utf8_encode($data));
  82. $user = array('user_id'=>$xml->id, 'user_name'=>$xml->screen_name, 'is_protected'=>$xml->protected);
  83. } catch(Exception $e) {
  84. $this->addErrorMessage($e->getMessage());
  85. }
  86. if (isset($user) && $user["is_protected"] == 'false') {
  87. // if so, add to instances table and owners table
  88. $i = $id->getByUsernameOnNetwork($_GET['twitter_username'], 'twitter');
  89. $oid = DAOFactory::getDAO('OwnerInstanceDAO');;
  90. $msg = '';
  91. if (isset($i)) { //Instance exists
  92. $oi = $oid->get($this->owner->id, $i->id);
  93. if ($oi == null) { //Owner_instance doesn't exist
  94. $oid->insert($this->owner->id, $i->id, '', '');
  95. }
  96. } else { //Instance does not exist
  97. $id->insert($user["user_id"], $user["user_name"]);
  98. $i = $id->getByUsernameOnNetwork($user["user_name"], 'twitter');
  99. $oid->insert($this->owner->id, $i->id, '', '');
  100. }
  101. $this->addSuccessMessage($_GET['twitter_username']." has been added to ThinkUp.");
  102. $this->addSuccessMessage("Added ".$_GET['twitter_username']." to ThinkUp.");
  103. } else { // if not, return error
  104. $this->addErrorMessage($_GET['twitter_username'].
  105. " is a private Twitter account; ThinkUp cannot track it without authorization.");
  106. }
  107. } else {
  108. $this->addErrorMessage($_GET['twitter_username']." is not a valid Twitter username.");
  109. }
  110. }
  111. $auth_from_twitter = '';
  112. if (isset($oauth_consumer_key) && isset($oauth_consumer_secret)) {
  113. $to = new TwitterOAuth($oauth_consumer_key, $oauth_consumer_secret);
  114. /* Request tokens from twitter */
  115. $tok = $to->getRequestToken();
  116. if (isset($tok['oauth_token'])) {
  117. $token = $tok['oauth_token'];
  118. $_SESSION['oauth_request_token_secret'] = $tok['oauth_token_secret'];
  119. /* Build the authorization URL */
  120. $oauthorize_link = $to->getAuthorizeURL($token);
  121. // create indication that auth from twitter plugin is okay
  122. $auth_from_twitter = "Using the Twitter Consumer key and secret as set in " .
  123. "the <a href=\"./?p=twitter\">Twitter plugin</a>.";
  124. } else {
  125. //set error message here
  126. $this->addErrorMessage(
  127. "Unable to obtain OAuth token. Check your Twitter plugin consumer key and secret configuration.");
  128. $oauthorize_link = '';
  129. }
  130. } else {
  131. $this->addErrorMessage(
  132. "Missing required settings! Please configure the Twitter plugin.");
  133. $oauthorize_link = '';
  134. }
  135. $owner_instances = $id->getByOwnerAndNetwork($this->owner, 'twitter');
  136. $this->addToView('owner_instances', $owner_instances);
  137. $this->addToView('oauthorize_link', $oauthorize_link);
  138. $this->addToView('auth_from_twitter', $auth_from_twitter);
  139. // add plugin options from
  140. $this->addOptionForm();
  141. $plugin = new TwitterRealtimePlugin();
  142. $this->addToView('is_configured', $plugin->isConfigured());
  143. return $this->generateView();
  144. }
  145. /**
  146. * Set plugin option fields for admin/plugin form
  147. */
  148. private function addOptionForm() {
  149. // for now at least, require consumer auth info to be the same as the twitter plugin, since the
  150. // accounts are all auth'd against that key. So, we don't have fields for those options here.
  151. // When the Twitter Realtime plugin is started up, it will exit
  152. // without doing its stuff if the auth info is not set in the Twitter plugin.
  153. $php_path_label = 'Path to the PHP interpreter to use';
  154. $php_path = array('name' => 'php_path', 'label' => $php_path_label,
  155. // @TODO - should this have a default set?
  156. //'default_value' => '/usr/bin/php'
  157. );
  158. $this->addPluginOption(self::FORM_TEXT_ELEMENT, $php_path);
  159. $has_redis = $this->isRedisSupported();
  160. $use_redis_label = "Use Redis"; // @TODO -- more information
  161. if($has_redis) {
  162. $use_redis = array('name' => 'use_redis', 'label' => $use_redis_label,
  163. 'values' => array('True' => 'true', 'False' => 'false'), 'default_value' => 'false');
  164. $this->addPluginOption(self::FORM_RADIO_ELEMENT, $use_redis);
  165. }
  166. }
  167. /**
  168. * Do we have redis support?
  169. * @returns boolean
  170. */
  171. private function isRedisSupported() {
  172. $version = explode('.', PHP_VERSION);
  173. // check whether predis is supported. first part of this check is overkill-
  174. // if major v. is less than 5, we should not even be running
  175. if (! ($version[0] >= $this->php_major_version_for_redis &&
  176. $version[1] >= $this->php_minor_version_for_redis)) {
  177. return false;
  178. }
  179. // can i ping a redis server?
  180. $redis_status = false;
  181. $redis = null;
  182. require_once THINKUP_WEBAPP_PATH . 'plugins/twitterrealtime/extlib/predis/lib/Predis.php';
  183. eval('$redis = new Predis\Client();'); //for php less than 5.3
  184. if(! is_null($redis)) {
  185. try {
  186. $resp = $redis->ping();
  187. $redis_status = true;
  188. }
  189. catch (Exception $e) {
  190. error_log("Exception: " . $e->getMessage() . ". Check that the Redis server is running.");
  191. }
  192. }
  193. return $redis_status;
  194. }
  195. }