Advertisement
manchumahara

Twitter connect element for j1.5

Nov 13th, 2012
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 14.41 KB | None | 0 0
  1. <?php
  2. /*------------------------------------------------------------------------
  3. # twitterconnect.php - Content - Embedding Tweet With Shortcode
  4. # ------------------------------------------------------------------------
  5. # author    Sabuj Kundu of Codeboxr Team
  6. # copyright Copyright (C) 2010-2012 codeboxr.com. All Rights Reserved.
  7. # @license - http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
  8. # Websites: http://codeboxr.com
  9. # Technical Support:  Forum - http://codeboxr.com/product/tweet-embed-using-shortcode-in-joomla
  10. # Plugin type: content
  11. -------------------------------------------------------------------------*/
  12.  
  13. // Check to ensure this file is included in Joomla!
  14. defined('_JEXEC') or die( 'Restricted access' );
  15.  
  16. require_once( JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_cbsocialappsoauth' . DS . 'tables' . DS . 'cbsocialappsoauthadmin.php' );
  17. //require_once( JPATH_ROOT .DS.'components' .DS.'com_community' .DS.'tables' .DS.'oauth.php');
  18. require_once( JPATH_ROOT . DS . 'components' . DS . 'com_mysocialtimeline' . DS . 'includes'.DS.'twitteroauth'.DS.'twitteroauth.php'); // must have
  19.  
  20.  
  21. //jimport('joomla.form.formfield');
  22.  
  23. /**
  24.  * Form Field class for the Joomla Framework.
  25.  *
  26.  */
  27. //class JFormFieldTwitterconnect extends JFormField
  28. class JElementTwitterconnect extends JElement
  29. {
  30.     /**
  31.      * Color picker form field type compatible with Joomla 1.6. Displays an Adobe type color picker panel, and returns a six-digit hex value, eg #cc99ff
  32.      */
  33.     //protected $type = 'twitterconnect';
  34.         var $_name = 'twitterconnect';  
  35.  
  36.     /**
  37.      */
  38.     //protected function getInput()
  39.         function fetchElement( $name, $value, &$node, $control_name )
  40.     {
  41.        
  42.             $baseurl = JURI::base();
  43.         $baseurl = str_replace('administrator/','',$baseurl);  
  44.        
  45.         /*
  46.         // Initialize some field attributes.
  47.         $size       = $this->element['size'] ? ' size="'.(int) $this->element['size'].'"' : '';
  48.         $maxLength  = $this->element['maxlength'] ? ' maxlength="'.(int) $this->element['maxlength'].'"' : '';
  49.         $class      = $this->element['class'] ? ' class="'.(string) $this->element['class'].'"' : '';
  50.         $readonly   = ((string) $this->element['readonly'] == 'true') ? ' readonly="readonly"' : '';
  51.         $disabled   = ((string) $this->element['disabled'] == 'true') ? ' disabled="disabled"' : '';   
  52.    
  53.         $scriptname = $baseurl.'plugins/content/contentscrollpane/contentscrollpane/jscolor/color-picker.js';
  54.         $doc =& JFactory::getDocument();
  55.         $doc->addScript($scriptname);
  56.        
  57.         $options = array();
  58.         if( $this->element['cellwidth']){ $options[] = "cellWidth:". (int) $this->element['cellwidth'];}
  59.         if( $this->element['cellheight']){ $options[] = "cellHeight:".(int) $this->element['cellheight'];}
  60.         if( $this->element['top']){ $options[] = "top:". (int) $this->element['top'];}
  61.         if( $this->element['left']){ $options[] = "left:". (int) $this->element['left'];}
  62.                                                                              
  63.                 $optionString = implode(',',$options);
  64.  
  65.         $js = 'window.addEvent(\'domready\', function(){
  66.                             var colorInput = $(\''.$this->id.'\');
  67.                             var cpicker = new ColorPicker(colorInput,{'.$optionString.'});
  68.                         });
  69.                     ';
  70.                     $doc->addScriptDeclaration($js);
  71.        
  72.        
  73.  
  74.         // Initialize JavaScript field attributes.
  75.         $onchange   = $this->element['onchange'] ? ' onchange="'.(string) $this->element['onchange'].'"' : '';
  76.  
  77.         return '<input type="text" name="'.$this->name.'" id="'.$this->id.'"' .
  78.                 ' value="'.htmlspecialchars($this->value, ENT_COMPAT, 'UTF-8').'"' .
  79.                 $class.$size.$disabled.$readonly.$onchange.$maxLength.'/>';
  80.                  *
  81.                  */
  82.                
  83.                 //$extid                     = (int) JRequest::getInt('cid');
  84.                 $extid                     = JRequest::getVar('cid',  0, '', 'array');
  85.                 $extid                     = (int)$extid[0];
  86.                 //var_dump($extid);
  87.                 $oauth                     = & JTable::getInstance( 'Oauth' , 'CBSocialappsadmin' );
  88.                 $productionmode            = true;     // true = production version  false = development version(for local testing)
  89.                 if($productionmode){
  90.                     $callback                  = JURI::root() .'administrator/index.php?option=com_menus&menutype=mainmenu&task=edit&cid[]='.$extid;
  91.                 }
  92.                 else{
  93.                     $callback                  = 'http://127.0.0.1/jtest5/administrator/index.php?option=com_menus&menutype=mainmenu&task=edit&cid[]='.$extid;
  94.                 }
  95.                 //$callback                  = JURI::root() .'administrator/index.php?option=com_modules&view=module&layout=edit&id='.$extid;
  96.                
  97.                 $callbackurl               = $callback.'&tokenmode=1';
  98.                 //var_dump($callbackurl);
  99.                  // Get Plugin info
  100.                 /*
  101.                 $db =& JFactory::getDbo();     
  102.                 $query  = $db->getQuery(true);
  103.                 //$query->select('element, client_id');
  104.                 $query->from('#__extensions');
  105.                 $query->where('extension_id = '.$extid);
  106.                 $query->where('type = '.$db->quote('module'));
  107.                 $db->setQuery($query);
  108.                 */
  109.                
  110.                
  111.                
  112.                 //var_dump($moduletbale->params);
  113.                 //$plugin                    =& JPluginHelper::getPlugin('content', 'mysocialtimelinecom');    
  114.                
  115.                 $plugin  = JTable::getInstance('menu', 'JTable', array());
  116.                 $plugin->id = $extid;
  117.                 $plugin->load();
  118.                
  119.                 //var_dump($plugin);
  120.                 // Get the plugin parameters
  121.                
  122.                 //$extid                     = JRequest::getInt('extension_id','');
  123.                 $removeapp                 = JRequest::getInt('removeapp',0);
  124.                
  125.                 $oauth                     =& JTable::getInstance( 'Oauth' , 'CBSocialappsadmin' );
  126.                 $oauth->extid              = $extid;
  127.                 $oauth->app                = 'mysocialtimelinecom';
  128.                
  129.                 if($removeapp){
  130.                     //var_dump($extid);
  131.                    
  132.                     //var_dump($oauth);
  133.                     $oauth->delete();
  134.                     //exit(1);
  135.                 }
  136.                 //var_dump($extid);
  137.                
  138.                
  139.                 //define('CONSUMER_KEY', $consumerKey);
  140.                 //define('CONSUMER_SECRET', $consumerSecret);
  141.                 //define('OAUTH_CALLBACK', $callbackurl);
  142.                 /* Build TwitterOAuth object with client credentials. */
  143.                 ob_start();
  144.                 $consumerKey                = '';
  145.                 $consumerSecret             = '';
  146.                 $plgdisabled                = true;
  147.                 if(is_object($plugin)){
  148.                    //$pluginParams              = new JRegistry( $plugin->params );
  149.                    $pluginParams              = new JParameter( $plugin->params );
  150.                    //var_dump($pluginParams);
  151.                    $consumerKey               = $pluginParams->get('consumerKey','3ca7EalPxMh5uJFFC9dgdw');
  152.                    $consumerSecret            = $pluginParams->get('consumerSecret','hKLPw1nOpzJXOn8fVYju1dAeGSOdhoIuRngFyAkU9Ts');
  153.                    $plgdisabled               =  false;
  154.                 }
  155.                
  156.                
  157.                 if(empty($consumerKey) || empty($consumerSecret)):
  158.                    if($plgdisabled){
  159.                        echo '<span style="float:left; display:inline;">Please enable the plugin.</span>';
  160.                    }
  161.                    else{
  162.                        echo '<span style="float:left; display:inline;">Please add Consumer Key and Consumer Secret</span>';
  163.                    }                  
  164.                 else:                                                                      
  165.                    
  166.                     $oauth->extid              = $extid;
  167.                     $oauth->app                = 'mysocialtimelinecom';
  168.                     $tokenmode                 = JRequest::getInt('tokenmode',0);
  169.                     if($oauth->load( $extid , 'mysocialtimelinecom') && $tokenmode)
  170.                     {
  171.                         $oauth_token                 = JRequest::getString('oauth_token','');
  172.                         $oauth_verifier              = JRequest::getString('oauth_verifier','');
  173.                         //
  174.                         $requesttoken = unserialize($oauth->requesttoken); // we again convert to array
  175.                         if(!empty($oauth_token ) && !empty ($oauth_verifier) && ($oauth_token == $requesttoken['oauth_token']) && !empty($_SESSION['mysocialtimelinecom_oauth_token_secret'])){
  176.                             /* Create TwitteroAuth object with app key/secret and token key/secret from default phase */
  177.                             $consumer = new TwitterOAuth($consumerKey, $consumerSecret, $oauth_token, $_SESSION['mysocialtimelinecom_oauth_token_secret']);
  178.  
  179.                             /* Request access tokens from twitter */
  180.                             $access_token           = $consumer->getAccessToken($oauth_verifier);
  181.                             //var_dump($access_token);
  182.                             $oauth->app             = 'mysocialtimelinecom';
  183.                             $oauth->extid           = $extid;
  184.                             $oauth->accesstoken     = serialize($access_token);
  185.                            
  186.                             //$oauth->requesttoken    = $oauth_token;
  187.                             if( !empty($oauth->accesstoken) )
  188.                             {
  189.                                     if (200 == $consumer->http_code) {
  190.                                       $storeval = $oauth->store();
  191.                                       echo '<a style="float:left; display:inline;" href="'.$callback.'&removeapp=1"><img src="'.$baseurl.'components/com_mysocialtimeline/images/twitterconnect/twitter_revoke.png" alt="Remove Twitter/Unauthorize Twitter" title="Remove Twitter/Unauthorize Twitter" /></a>';
  192.                                     } else {
  193.                                       /* Save HTTP status for error dialog on connnect page.*/
  194.                                       echo '<a style="float:left; display:inline;" href="'.$callback.'">Something wrong, please click here to refresh1!</a>';
  195.                                     }
  196.                                    
  197.                                     //var_dump($storeval);
  198.                                     //User is already authenticated and we have the proper tokens to fetch data.
  199.                                     //$url    = $callback;
  200.                                      //$removeappurl   = JURI::root() .'index.php?option=com_community'.rawurlencode('&view=profile&task=callback&app=advanceinstagramforjom');
  201.                                    
  202.                             }
  203.                             else{
  204.                                  echo '<a style="float:left; display:inline;" href="'.$callback.'">Something wrong, please click here to refresh1!</a>';
  205.                             }
  206.                         }
  207.                         else{
  208.                             echo '<a style="float:left; display:inline;" href="'.$callback.'">Something wrong, please click here to refresh2!</a>';
  209.                         }
  210.                     }
  211.                     else{
  212.                         $consumer = new TwitterOAuth($consumerKey, $consumerSecret);
  213.                         if( !$oauth->load( $extid , 'mysocialtimelinecom') || empty($oauth->accesstoken) ){                        
  214.                             $request_token              = $consumer->getRequestToken($callbackurl); //original was array
  215.                             //var_dump($request_token);
  216.                            
  217.  
  218.                             /* If last connection failed don't display authorization link. */
  219.                             switch ($consumer->http_code) {
  220.                               case 200:  
  221.                                   // 200 means ok
  222.                                 //$oauth->extid               = $extid;
  223.                                 //$oauth->accesstoken         = $extid;
  224.                                 //$request_token              = $consumer->getRequestToken($callbackurl); //original was array
  225.                                  $_SESSION['mysocialtimelinecom_oauth_token']        = $token = $request_token['oauth_token'];
  226.                                 $_SESSION['mysocialtimelinecom_oauth_token_secret'] = $request_token['oauth_token_secret'];
  227.                                 $oauth->requesttoken        = serialize($request_token);
  228.                                 $storeval                   = $oauth->store();
  229.                                 $url                        = $consumer->getAuthorizeURL($token);
  230.                                 //header('Location: ' . $url);
  231.                                 echo '<a style="float:left; display:inline;" href="'.$url.'"><img src="'.$baseurl.'components/com_mysocialtimeline/images/twitterconnect/darker.png" alt="Sign in with Twitter"/></a>';
  232.                                 break;
  233.                               default:
  234.                                 /* Show notification if something went wrong. */
  235.                                 echo '<a style="float:left; display:inline;" href="'.$callback.'">Something wrong, please click here to refresh3!</a>';
  236.                             }                                                
  237.                         }
  238.                         else{
  239.                             echo '<a style="float:left; display:inline;" href="'.$callback.'&removeapp=1"><img src="'.$baseurl.'components/com_mysocialtimeline/images/twitterconnect/twitter_revoke.png" alt="Remove Twitter/Unauthorize Twitter" title="Remove Twitter/Unauthorize Twitter" /></a>';
  240.                         }
  241.                     }//end no token mode
  242.                    
  243.                
  244.                 endif;
  245.                
  246.         $html   = ob_get_contents();
  247.         ob_end_clean();
  248.        
  249.         return $html;
  250.                
  251.                 //$content = '<a href="#"><img src="'.$baseurl.'plugins/content/mysocialtimelinecom/mysocialtimelinecom/images/darker.png" alt="Sign in with Twitter"/></a>';
  252.                 //return $content;
  253.                
  254.                
  255.                
  256.     }
  257. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement