Advertisement
Guest User

CongressLookup.php

a guest
Jun 29th, 2013
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 16.00 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: CongressLookup
  4. Plugin URI: http://CongressLookup.com/
  5. Description: CongressLookup is powered by data APIs from the Sunlight Foundation's <a href="http://services.sunlightlabs.com/accounts/register/" targer="_blank" >Sunlight Labs</a> and <a href="http://www.google.com/intl/en-US_US/help/terms_maps.html" target="_blank">Google Maps</a>. To configure plugin, go to: Settings > CongressLookup.   <a href="constructivegrowth.net/wordpress-plugins/congresslookup/congresslookup-faqs/" target="_blank">Quick Start Guide and FAQs</a>.  Suggestion for improvements are always welcome.
  6. Version: 1.0.0
  7. Author: Constructive Growth LLC
  8. Author URI: http://constructivegrowth.net/
  9. License: GNU General Public License v2 or later
  10. */
  11.  
  12. if ( !defined('ABSPATH') ) exit();
  13.  
  14. define('LEGISLATORS_PATH', plugin_dir_url(__FILE__));
  15. define('LEGISLATORS_PATH_BASE', plugin_dir_path(__FILE__));
  16.  
  17. register_activation_hook( __FILE__, 'CongressLookup_install' );
  18.  
  19. function CongressLookup_install()
  20. {
  21.     //update_option('congress_key', '' );
  22.  
  23.     update_option('congress_cache', 1);
  24.     update_option('congress_cache_time', 30);
  25.     update_option('congress_themes', 'modern');
  26.     update_option('congress_photos_last_modified', '1307992245');
  27.     update_option('congress_options', array(0=>'fax', 1=> 'phone', 2=> 'webform', 3=> 'picture'));
  28. }
  29.  
  30.  
  31. add_action('wp_head', 'legislators_head');
  32. add_shortcode('CongressLookup', 'legislators_start');
  33. // fix
  34. if (!is_admin()) add_filter('widget_text', 'do_shortcode', 11);
  35.  
  36.  
  37. add_action('admin_notices', 'showAdminMessages');    
  38.  
  39.  
  40. if ( is_admin() ){ // admin actions
  41.     add_action('admin_menu', 'qkCongressLookupMenu');
  42.     add_action( 'admin_init', 'qkCongressLookup_registerSettings' );
  43. }
  44.  
  45. function qkCongressLookupMenu(){ add_options_page('CongressLookup', 'CongressLookup', 'administrator', 'mt-cglu', 'qkCongressLookupSettings'); }
  46.  
  47. function legislators_start()
  48. {
  49.     $html = '';
  50.  
  51.  
  52.     if(get_option('congress_key')):
  53.  
  54.         $html .='<form action="#" class="legislators" onsubmit="return getCongressFromAddress(this);">
  55.        
  56.             <p class="le_head">Locate your Senators and Representative</p>
  57.                    
  58.             <fieldset id="user-details">   
  59.                
  60.                 <label for="congress_address">Address:</label>
  61.                 <input type="text" name="congress_address" id="congress_address" value="" />
  62.                 <input type="submit" value="Find" name="submit" class="submit" />
  63.                 <img src="'.LEGISLATORS_PATH.'loader.gif" id="jloader" alt="loading" title="Loading" />
  64.                 <p class="congress_example"><i>ex: 1600 Pennsylvania Ave, Washington, DC 20500 </i></p>
  65.             </fieldset>
  66.            
  67.             <div id="map_canvas" style="width:80%; height:190px;margin:0 auto;border:1px solid #EDEDED"></div>
  68.            
  69.             <div id="congress_holder"></div>
  70.             </form>
  71.             ';
  72.        
  73.     else:
  74.          $html .='<form action="#" class="legislators">
  75.             <p class="le_head">API Key missing, please update it</p>
  76.             </form>';
  77.     endif;
  78.    
  79.    
  80.     return $html;
  81. }
  82.  
  83. function legislators_head()
  84. {
  85. ?>
  86.     <link href='http://fonts.googleapis.com/css?family=Yanone+Kaffeesatz' rel='stylesheet' type='text/css' />
  87.     <link href='<?php  echo LEGISLATORS_PATH; ?>style.css' rel='stylesheet' type='text/css' />
  88.     <?php if(get_option('congress_themes') && get_option('congress_themes') == 'modern'): ?><link href='<?php  echo LEGISLATORS_PATH; ?>light.css' rel='stylesheet' type='text/css' /> <?php endif; ?>
  89.     <?php if(get_option('congress_themes') && get_option('congress_themes') == 'custom'): ?>
  90.         <?php if(get_option('congress_themes_css')): ?>
  91.             <style type="text/css">
  92.                 <!--
  93.                 <?php echo get_option('congress_themes_css'); ?>
  94.                 -->
  95.             </style>
  96.         <?php endif; ?>
  97.     <?php endif; ?>
  98.     <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
  99.     <script type="text/javascript">
  100.         var geo_json = "<?php  echo LEGISLATORS_PATH; ?>getData.php";
  101.     </script>
  102.     <script type="text/javascript" src="<?php  echo LEGISLATORS_PATH; ?>geocode.js"></script>
  103. <?php
  104. }
  105.  
  106.  
  107. function qkCongressCheckOptions($t)
  108. {
  109.     $opt = get_option('congress_options');
  110.  
  111.     if(is_array($opt))
  112.     {
  113.         foreach($opt AS $key=>$value){
  114.             if($value == $t) return 'checked="checked"';
  115.         }
  116.     }
  117.    
  118.     return('');
  119. }
  120.  
  121. function GetRemoteLastModified( $uri )
  122. {
  123.     // default
  124.     $unixtime = 0;
  125.    
  126.     $fp = fopen( $uri, "r" );
  127.     if( !$fp ) {return;}
  128.    
  129.     $MetaData = stream_get_meta_data( $fp );
  130.        
  131.     if(isset($MetaData['wrapper_data']) && @is_array($MetaData['wrapper_data']) && count($MetaData['wrapper_data']) > 0)
  132.     {
  133.         foreach( $MetaData['wrapper_data'] as $response )
  134.         {
  135.             // case: redirection
  136.             if( substr( strtolower($response), 0, 10 ) == 'location: ' )
  137.             {
  138.                 $newUri = substr( $response, 10 );
  139.                 fclose( $fp );
  140.                 return GetRemoteLastModified( $newUri );
  141.             }
  142.             // case: last-modified
  143.             elseif( substr( strtolower($response), 0, 15 ) == 'last-modified: ' )
  144.             {
  145.                 $unixtime = strtotime( substr($response, 15) );
  146.                 break;
  147.             }
  148.         }
  149.     }
  150.    
  151.  
  152.  
  153.     fclose( $fp );
  154.     return $unixtime;
  155. }
  156.  
  157.  
  158. function congress_move_folder($source, $destination)
  159. {
  160.     // Get array of all source files
  161.     $files = scandir($source);
  162.     // Cycle through all source files
  163.     foreach ($files as $file) {
  164.       if (in_array($file, array(".",".."))) continue;
  165.       // If we copied this successfully, mark it for deletion
  166.       if (copy($source.$file, $destination.$file)) {
  167.         $delete[] = $source.$file;
  168.       }
  169.     }
  170.     // Delete all successfully-copied files
  171.     foreach ($delete as $file) {
  172.       unlink($file);
  173.     }
  174. }
  175.  
  176. function congress_updatePhotos()
  177. {
  178.     // Download zip file
  179.     $zip = @file_get_contents("http://assets.sunlightfoundation.com/moc/40x50.zip");
  180.    
  181.     if($zip)
  182.     {
  183.         $pics_file = LEGISLATORS_PATH_BASE."pics.zip";
  184.        
  185.         file_put_contents($pics_file, $zip);
  186.        
  187.         $zip = new ZipArchive;
  188.         $res = $zip->open($pics_file);
  189.         if ($res === TRUE)
  190.         {
  191.             $zip->extractTo(LEGISLATORS_PATH_BASE.'downloads/');
  192.             $zip->close();
  193.            
  194.             // move new images
  195.             congress_move_folder(LEGISLATORS_PATH_BASE.'downloads/40x50/', LEGISLATORS_PATH_BASE.'pics/');
  196.            
  197.             update_option('congress_photos_last_modified', GetRemoteLastModified('http://assets.sunlightfoundation.com/moc/40x50.zip'));
  198.            
  199.             echo '<script>window.location = "options-general.php?page=mt-cglu&photos-updated=true&r=1"; </script>';
  200.             exit;
  201.         }
  202.         else{
  203.             echo '<script>window.location = "options-general.php?page=mt-cglu&photos-updated=true&r=0"; </script>';
  204.             exit;
  205.         }
  206.     }
  207.    
  208.     return false;
  209. }
  210.  
  211.  
  212. function qkCongressLookup_registerSettings() { // whitelist options
  213.     register_setting( 'qkCongressLookup-group', 'congress_key' );
  214.     register_setting( 'qkCongressLookup-group', 'congress_cache' );
  215.     register_setting( 'qkCongressLookup-group', 'congress_cache_time' );
  216.     register_setting( 'qkCongressLookup-group', 'congress_options' );
  217.     register_setting( 'qkCongressLookup-group', 'congress_themes' );
  218.     register_setting( 'qkCongressLookup-group', 'congress_themes_css' );
  219. }
  220.  
  221. function showAdminMessages()
  222. {
  223.     $res = GetRemoteLastModified('http://assets.sunlightfoundation.com/moc/40x50.zip');
  224.     $op = get_option('congress_photos_last_modified');
  225.    
  226.     if(($op && $res) && ($op != $res))
  227.             echo '<div id="message" class="error fade"><p><strong>You need to update your congress photos. <a href="options-general.php?page=mt-cglu&photos-updated=true">Click Here to update it</a></strong></p></div>';
  228. }
  229.  
  230. function qkCongressLookupSettings() {
  231.     if( get_option('congress_themes') == 'custom' && !get_option('congress_themes_css'))
  232.     {
  233.         update_option('congress_themes_css', @file_get_contents(LEGISLATORS_PATH_BASE.'custom.css'));
  234.     }
  235. ?>
  236.     <div class="wrap">
  237.     <div class="icon32" id="icon-options-general"><br></div>
  238.     <h2>CongressLookup  Settings</h2>
  239.    
  240.     <?php if ($_REQUEST['updated']=='true') { ?>
  241.     <div id="message" class="updated fade"><p><strong>Settings Updated</strong></p></div>
  242.     <?php  } ?>
  243.    
  244.     <?php if ($_REQUEST['photos-updated']=='true') {
  245.         if(!isset( $_REQUEST['r'] )) congress_updatePhotos();
  246.         if(isset( $_REQUEST['r'] ) && $_REQUEST['r'] == 1) echo '<div id="message" class="updated fade"><p><strong>Photos Updated</strong></p></div>';
  247.         else echo '<div id="message" class="error fade"><p><strong>An error occured while updating the Photos</strong></p></div>';
  248.         }
  249.     ?>
  250.  
  251.     <div id="qk_settings-form" style="position:relative">
  252.     <form name="addnew" method="post" action="options.php">
  253.     <?php settings_fields('qkCongressLookup-group');?>
  254.         <table class="form-table">
  255.             <tbody>
  256.                 <tr valign="top">
  257.                     <th scope="row"><label for="congress_key">Sunlight API Key:</label></th>
  258.                     <td>
  259.                         <input name="congress_key" type="text" size="45" value="<?php echo get_option('congress_key'); ?>" >
  260.                         <p>Get your API Key at the <a href="http://services.sunlightlabs.com/accounts/register/" target="_blank">Sunlight Foundation</a></p>
  261.                     </td>
  262.                 </tr>
  263.                 <tr valign="top">
  264.                     <th scope="row"><label>What to display?:</label></th>
  265.                     <td>
  266.                         <table>
  267.                             <tbody>
  268.                                 <tr>
  269.                                     <td>
  270.                                         <input name="congress_options[]" type="checkbox" id="district" value="district" <?php echo qkCongressCheckOptions("district"); ?>> <label for="district">District</label> <br />
  271.                                         <input name="congress_options[]" type="checkbox" id="state" value="state" <?php echo qkCongressCheckOptions("state"); ?>> <label for="state">State</label> <br />
  272.                                         <input name="congress_options[]" type="checkbox" id="email" value="email" <?php echo qkCongressCheckOptions("email"); ?>> <label for="email">Email</label> <br />
  273.                                         <input name="congress_options[]" type="checkbox" id="qktitle" value="title" <?php echo qkCongressCheckOptions("title"); ?>> <label for="qktitle">Title</label>
  274.                                     </td>
  275.                                     <td>&nbsp;&nbsp;</td>
  276.                                     <td>
  277.                                         <input name="congress_options[]" type="checkbox" id="website" value="website" <?php echo qkCongressCheckOptions("website"); ?>> <label for="website">Website</label> <br />
  278.                                         <input name="congress_options[]" type="checkbox" id="fax" value="fax" <?php echo qkCongressCheckOptions("fax"); ?>> <label for="fax">Fax</label> <br />
  279.                                         <input name="congress_options[]" type="checkbox" id="phone" value="phone" <?php echo qkCongressCheckOptions("phone"); ?>> <label for="phone">Phone</label> <br />
  280.                                         <input name="congress_options[]" type="checkbox" id="webform" value="webform" <?php echo qkCongressCheckOptions("webform"); ?>> <label for="webform">Webform</label>
  281.                                     </td>
  282.                                     <td>&nbsp;&nbsp;</td>
  283.                                     <td>
  284.                                         <input name="congress_options[]" type="checkbox" id="gender" value="gender" <?php echo qkCongressCheckOptions("gender"); ?>> <label for="gender">Gender</label> <br />
  285.                                          <input name="congress_options[]" type="checkbox" id="birthdate" value="birthdate" <?php echo qkCongressCheckOptions("birthdate"); ?>> <label for="birthdate">Birthdate</label><br />
  286.                                         <input name="congress_options[]" type="checkbox" id="congresspedia_url" value="congresspedia_url" <?php echo qkCongressCheckOptions("congresspedia_url"); ?>> <label for="congresspedia_url">Congresspedia URL</label> <br />
  287.                                         <input name="congress_options[]" type="checkbox" id="picture" value="picture" <?php echo qkCongressCheckOptions("picture"); ?>> <label for="picture">Picture</label>
  288.                                     </td>
  289.                                 </tr>
  290.                             </tbody>
  291.                         </table>
  292.                     </td>
  293.                 </tr>
  294.                 <tr valign="top">
  295.                     <th scope="row"><label>Cache:</label></th>
  296.                     <td>
  297.                         <p>Enable this to cache the data returned by the API, to reduce the number of requests, and for fast loading. Select for how many minutes you would like the data to be cached &amp; saved</p>
  298.                         <input name="congress_cache" id="congress_cache" type="checkbox" value="1" <?php echo checked(get_option('congress_cache'),1); ?>> <label for="congress_cache">Enable cache?</label>
  299.                         <p>
  300.                             <label for="congress_cache_time">Cache time:</label>
  301.                             <input name="congress_cache_time" id="congress_cache_time" type="text" size="5" style="width:40px" value="<?php echo get_option('congress_cache_time'); ?>"> <small><i>minutes</i></small>
  302.                         </p>
  303.                        
  304.                         <?php
  305.                             $cache_folder = LEGISLATORS_PATH_BASE.'cache/';
  306.                             if(!is_writable($cache_folder))
  307.                             {
  308.                         ?>
  309.                         <p style="color:red">You need to give 777 permission to: <?php echo $cache_folder; ?></p>
  310.                         <?php
  311.                             }
  312.                         ?>
  313.                     </td>
  314.                 </tr>
  315.                 <tr valign="top">
  316.                     <th scope="row"><label for="congress_themes">Theme:</label></th>
  317.                     <td>
  318.                         <select name="congress_themes" id="congress_themes">
  319.                             <option value="" <?php echo selected(get_option('congress_themes'),''); ?>>No Theme</option>
  320.                             <option value="modern" <?php echo selected(get_option('congress_themes'),'modern'); ?>>Modern</option>
  321.                             <option value="custom" <?php echo selected(get_option('congress_themes'),'custom'); ?>>Custom</option>
  322.                         </select>
  323.  
  324.                         <div <?php if(get_option('congress_themes') != 'custom'): echo ' style="display:none" '; endif; ?>id="custom_css_div">
  325.                         <p><label for="congress_themes_css">Custom Css Code:</label></p>
  326.                         <p>
  327.                             <textarea name="congress_themes_css" id="congress_themes_css" rows="1" cols="1" style="width:80%;height:150px;">
  328. <?php if(get_option('congress_themes_css')):  echo get_option('congress_themes_css'); else: echo @file_get_contents(LEGISLATORS_PATH_BASE.'custom.css'); endif; ?>
  329.                             </textarea>
  330.                         </p>
  331.                         <p><b>Demo :</b></p>
  332.                         <p>
  333.                             <iframe src="<?php echo LEGISLATORS_PATH; ?>iframe.html?<?php echo time(); ?>" id="form_demo" style="width:85%;height:200px;border:0"></iframe>
  334.                         </p>
  335.                         </div>
  336.                     </td>
  337.                 </tr>
  338.                 <tr valign="top">
  339.                     <th scope="row"><label>Photos of Members of Congress</label></th>
  340.                     <td>
  341.                         <a class="button" href="options-general.php?page=mt-cglu&photos-updated=true" onclick="if(confirm('Are you sure?')){ return true; } else{ return false; }">Update The Photos</a><br /> &nbsp;We suggest you update when you first install CongressLookup to make sure your photos are up-to-date. &nbsp;<i>Note: May take some time to download from Sunlight Labs.  Please be patient.</i>
  342.                         <?php
  343.                             $downloads_folder = LEGISLATORS_PATH_BASE.'downloads/';
  344.                             if(!is_writable($downloads_folder))
  345.                             {
  346.                         ?>
  347.                         <p style="color:red">You need to give 777 permission to: <?php echo $downloads_folder; ?></p>
  348.                         <?php
  349.                             }
  350.                         ?>
  351.                     </td>
  352.                 </tr>
  353.                 <tr valign="top">
  354.                     <th scope="row"><label>Short Code:</label></th>
  355.                     <td>
  356.                         <p>[CongressLookup]</p>
  357.                     </td>
  358.                 </tr>
  359.             </tbody>
  360.         </table>
  361.         <?php submit_button(); ?>
  362.    
  363.        
  364.     </form>
  365.         <div style="position:absolute;top:10px;right:20px;width:20%;padding:10px;background-color:#FFFFE0;border:1px solid #E8E7AE">
  366.             <p>CongressLookup is free to use.  Please consider donating to help support the continued development of the project.</p>
  367.             <form action="https://www.paypal.com/cgi-bin/webscr" method="post" style="text-align:center;">
  368.                 <input type="hidden" name="cmd" value="_s-xclick">
  369.                 <input type="hidden" name="hosted_button_id" value="3ZSYXP8PLH6AJ">
  370.                 <input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_donateCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
  371.                 <img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1">
  372.             </form>
  373.         </div>
  374.     </div>
  375.     </div>
  376.  
  377. <script type="text/javascript">
  378.  
  379.  
  380. function iframeLoaded() {
  381.     var $frame = jQuery("#form_demo");
  382.     var contents = $frame.contents(),
  383.     styleTag = jQuery('<style></style>').appendTo(contents.find('head'));
  384.    
  385.     styleTag.text(jQuery('#congress_themes_css').val());
  386.    
  387.     jQuery('#congress_themes_css').keyup(function() {
  388.         styleTag.text(jQuery(this).val());
  389.     });
  390.            
  391. }
  392.  
  393. jQuery(document).ready(function($){
  394.  
  395.     if($('#congress_themes_css').length > 0)
  396.     {
  397.    
  398.         $("#congress_themes").change(function(){
  399.             if($(this).val() == "custom")
  400.                 $('#custom_css_div').show();
  401.             else
  402.                 $('#custom_css_div').hide();
  403.         });
  404.        
  405.     }
  406. });
  407. </script>  
  408.    
  409.    
  410. <?php }
  411.  
  412.  
  413. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement