Advertisement
Guest User

Reddit Flair Bot

a guest
Jul 28th, 2011
1,056
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.66 KB | None | 0 0
  1. <?php
  2.     //Definitions
  3.     //Your Sub-reddit! Nothing more than what comes after the /r/ (lower case please)
  4.     $sub_reddit = '';
  5.  
  6.     //A moderator account must be the one to set users flair, info must be plaintext
  7.     $mod_account = '';
  8.     $mod_password = '';
  9.     //for now: go to http://reddit . com/api/me . json while logged into the defined account, and copy the listed modhash
  10.     $mod_hash = '';
  11.  
  12.     //This is the temporary file where the login cookie will be stored to process the form, it must be created before use
  13.     $cookiejar = 'cookies.txt';
  14.    
  15.     //  Define the Encryption key for the PM
  16.     $key = ''; 
  17.    
  18.     //Sprite Image as listed on the sub-reddit example: sprite-icons
  19.     $sprite_image = '';
  20.     //The URL of the sprite for the CSS
  21.     $sprite_url = '';
  22.     //Define the height of your sprite in pixels
  23.     $sprite_height = ;
  24.     //Define if the sprite should be :before or :after in the CSS
  25.     $sprite_position = 'before';
  26.    
  27.     //Define the subject of the sent PM
  28.     $subject = 'Flair Confirmation -- Automatic Message do not Respond';
  29.  
  30.     //Enter the names of your Flair here as an array, this is text that will be displayed on the selector
  31.     $flair = array (
  32.     ); 
  33.    
  34.     function encode ( $string, $key ) {
  35.         $key = sha1 ( $key );
  36.         $strLen = strlen ( $string );
  37.         $keyLen = strlen ( $key );
  38.        
  39.         for ( $i = 0; $i < $strLen; $i++ ) {
  40.             $ordStr = ord ( substr ( $string, $i, 1) );
  41.             if ( $j == $keyLen ) $j = 0;
  42.             $ordKey = ord ( substr ( $key, $j, 1) );
  43.             $j++;
  44.             $hash .= strrev ( base_convert ( dechex ( $ordStr + $ordKey ), 16, 36) );
  45.         }
  46.        
  47.         return $hash;
  48.     }
  49.  
  50.     function decode ( $string, $key ) {
  51.         $key = sha1 ( $key );
  52.         $strLen = strlen ( $string );
  53.         $keyLen = strlen ( $key );
  54.        
  55.         for ( $i = 0; $i < $strLen; $i+=2 ) {
  56.             $ordStr = hexdec ( base_convert ( strrev ( substr ( $string, $i, 2 ) ), 36, 16 ) );
  57.             if ( $j == $keyLen ) $j = 0;
  58.             $ordKey = ord ( substr ( $key, $j, 1 ) );
  59.             $j++;
  60.             $hash .= chr ( $ordStr - $ordKey );
  61.         }  
  62.        
  63.         return $hash;
  64.     }
  65.  
  66.     //Login as the moderator account
  67.     $ch = curl_init();
  68.  
  69.     //Define the data to send as reddit's login
  70.     $post_data = 'user=' . $mod_account . '&passwd=' . $mod_password . '&api_type=json';
  71.    
  72.     $options = array (
  73.         CURLOPT_URL     =>  'http://www.reddit.com/api/login/',
  74.         CURLOPT_RETURNTRANSFER      =>  1,
  75.         CURLOPT_POST        =>  1,
  76.         CURLOPT_POSTFIELDS      =>  $post_data,
  77.         CURLOPT_COOKIEJAR       =>  $cookiejar
  78.     );
  79.    
  80.     curl_setopt_array ($ch, $options);
  81.    
  82.     $output = curl_exec($ch);
  83.     curl_close($ch);
  84.    
  85.     if (!empty($_POST)) {
  86.         //Create the key= to encode
  87.         $key2encode = 'r=' . $sub_reddit . '&name=' . $_POST['name'];
  88.         if (isset($_POST['text'])) $key2encode  .= '&text=' . $_POST['text'];
  89.         if (isset($_POST['css_class'])) $key2encode  .= '&css_class=' . $_POST['css_class'];
  90.        
  91.         $key2encode = encode($key2encode, $key);
  92.        
  93.         //The Default Message example: http://reddithon . com/flair . php
  94.         $message = 'http://'  .  $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'] . '?key='  .  $key2encode;
  95.        
  96.         $ch = curl_init();
  97.        
  98.         $post_data = 'to='  .  $_POST['name']  .  '&subject='  .  $subject  .  '&text='  .  $message  .  '&uh='  .  $mod_hash;
  99.        
  100.         $options = array (
  101.             CURLOPT_URL     =>  'http://www.reddit.com/api/compose',
  102.             CURLOPT_RETURNTRANSFER      =>  1,
  103.             CURLOPT_POST        =>  1,
  104.             CURLOPT_POSTFIELDS      =>  $post_data,
  105.             CURLOPT_COOKIEFILE      =>  $cookiejar
  106.         );
  107.        
  108.         curl_setopt_array ($ch, $options);
  109.        
  110.         $comment = curl_exec($ch);     
  111.         curl_close($ch);
  112.        
  113.         ?>
  114.             <!-- Redirect to your sub-reddit -->
  115.             <script type='text/javascript'>
  116.                 window.location = 'http://reddit.com/r/<?php echo $sub_reddit; ?>'
  117.             </script>
  118.         <?php
  119.     }
  120.    
  121.     if (isset($_GET['key'])) {
  122.         //Decode the sent key
  123.         $post_data = decode($_GET['key'], $key ) . '&uh=' . $mod_hash;     
  124.        
  125.         $ch = curl_init();
  126.        
  127.         $options = array (
  128.             CURLOPT_URL     =>  'http://www.reddit.com/api/flair',
  129.             CURLOPT_RETURNTRANSFER      =>  1,
  130.             CURLOPT_POST        =>  1,
  131.             CURLOPT_POSTFIELDS      =>  $post_data,
  132.             CURLOPT_COOKIEFILE      =>  $cookiejar
  133.         );
  134.        
  135.         curl_setopt_array ($ch, $options);
  136.        
  137.         $output = curl_exec($ch);      
  138.         curl_close($ch);
  139.        
  140.         ?>
  141.             <!-- Redirect to your sub-reddit -->
  142.             <script type='text/javascript'>
  143.                 window.location = 'http://reddit.com/r/<?php echo $sub_reddit; ?>'
  144.             </script>
  145.         <?php
  146.     }
  147. ?>
  148. <head>
  149.     <title>Your Sub-Reddit's Flair Bot!</title>
  150.     <style>
  151.         .sprite-icon {
  152.             /* MUST SET BACKGROUND IMAGE */
  153.             background-image: url(<?php echo $sprite_url ?>);
  154.             width: 28px;
  155.             height: 28px;
  156.             display: inline-block;
  157.             margin: 1px;
  158.             margin-left: 0px;
  159.         }
  160.        
  161.         #flair {
  162.             width: 225px;
  163.             margin: 0;
  164.             float: left;
  165.         }
  166.     </style>
  167. </head>
  168. <body>
  169. <?php $minimum_pieces_of_flair = count($flair); ?>
  170.     <!-- Created by m4rx -->
  171.     <div id='container'>
  172.         <div id='reddit-select'>                   
  173.             <form action='' method='POST'>
  174.                
  175.                 <div class='flair-text'>
  176.                     <label>Flair Text:</label>
  177.                     <input type='text' size='20' name='text'/>
  178.                 </div>
  179.                
  180.                 <div id='username' class='reddit-account'>
  181.                     <label>Reddit Account Name:</label>
  182.                     <input style='' size='20' type='text' name='name'/>*Case Sensitive     
  183.                 </div>         
  184.        
  185.                 <div class='sprite-select'>
  186.                     <h3>Select Flair</h3>
  187.                     <ul id='sprite-list'>
  188.                         <?php
  189.                             $temp = 0;
  190.                             $height = 0;
  191.                            
  192.                             if (isset($_GET['css'])) {
  193.                                     echo ' . flair:before { background: url(%%'  .  $sprite_image  .  '%%) no-repeat scroll -9999px -9999px transparent; display: inline-block; height: ' . $sprite_height . 'px; width: ' . $sprite_height . 'px; margin-right: 3px; vertical-align: middle;}<br />';
  194.                             }
  195.                        
  196.                             while ( $temp < $minimum_pieces_of_flair ) {
  197.                                 if ( isset ( $_GET['css'] ) ) {
  198.                                     echo '.flair-'  .  strtolower ( str_replace ( ' ', '-', $flair[$temp] ) )  .  ':before {content: ""; background-position: 0 ' . $height . 'px;}<br />';
  199.                                 }
  200.                                 else {
  201.                                 ?>
  202.                                     <li id='flair'>
  203.                                         <input name='css_class' type='radio' id='<?php echo $flair[$temp] ?>' style='display: inline; float: left; margin-top: 7px;' value='<?php echo strtolower ( str_replace ( ' ', '-', $flair[$temp] ) ); ?>' />
  204.                                         <label for='<?php echo $flair[$temp] ?>'>
  205.                                         <div class='sprite-icon' style='margin-right: 2px; float: left; background-position: 0px <?php echo $height; ?>px;'></div>
  206.                                         <div class='flair-name'><?php echo $flair[$temp]; ?></div>
  207.                                         </label>
  208.                                     </li>
  209.                                 <?php
  210.                                 }
  211.                                 $height = ($height - $sprite_height);
  212.                                 $temp = ($temp + 1);
  213.                             }
  214.                         ?>
  215.                     </ul>
  216.                 </div>
  217.                 <div class='save' style='float:right; padding: 10px;'>
  218.                     <input type = 'submit' value = 'Save'/>
  219.                 </div>                         
  220.             </form>
  221.         </div>
  222.     </div><!-- #container -->
  223. </body>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement