JayBachatero.com

Archive for the ‘PHP Snippets’ Category

Hex-to-Numeric Entities Converter

At work I get files sometimes with hexadecimal entities but the problem is that we use numeric entities. I decided to write a simple script that will search the file for hex entities and convert them to numeric one. This is intended to be used with Komodo Edit or via the command line.

(more…)

How to use the SMF user system outside of SMF

I wrote this article a while back for the doc site.

Call SSI.php before anything else.
How do I show a login/logout form?
How do I redirect users after they login/logout?
How do I restrict access to certain areas of my pages?
Extras

Hooking the SMF user system with external applications is a common issue people have. This document will help explain how to accomplish this. The first thing you will need to do is change the files that you are going to implement this on into php files. So if you have a .html or .htm extension proceding the filename of the file, rename the file with the extension at the end being .php. The reason for this is so that the php content can get parsed as php and not as html. You may ask yourself: By doing this, will it have any effects on my current page? Well the answer is no. Everything will show just the way it did before.
(more…)

How to recount posts for members in SMF.

Something that members ask from time to time is for a way to recount the total posts for their member. A while back I wrote a small script to do this. To prevent the script from overloading the server it's done in small steps. It still needs some work but here it is. Hope it comes in handy to some.

Recount User Posts File Download

PHP:
  1. <?php
  2. // Load SSI.php
  3. require_once('SSI.php');
  4.  
  5. // Turn this on.
  6.  
  7. // Are we allowed in here?
  8. isAllowedTo('admin_forum');
  9.  
  10. $_REQUEST['start'] = !isset($_REQUEST['start']) ? '0' : (int) $_REQUEST['start'];
  11.  
  12. // Header step and footer.
  13. show_header();
  14. // Check if it exists.
  15. if (!empty($_REQUEST['start']) || (empty($_REQUEST['start']) && isset($_REQUEST['recount'])))
  16.         recount();
  17. elseif (isset($_GET['start']) && $_GET['start'] != -1)
  18.         // Give a link to start
  19.         echo '
  20.                 <h2><a href="', $_SERVER['PHP_SELF'], '?recount;start=0">Start Post Recount</a></h2>';
  21. // Footeeerr.
  22. show_footer();
  23.  
  24.  
  25. function recount()
  26. {
  27.         global $db_prefix;
  28.  
  29.         while (true)
  30.         {
  31.                 // Only run this query if we don't have the total.
  32.                 if (!isset($_SESSION['totalMembers']))
  33.                 {
  34.                         $request = db_query("
  35.                                 SELECT COUNT(DISTINCT m.ID_MEMBER)
  36.                                 FROM ({$db_prefix}messages AS m, {$db_prefix}boards AS b)
  37.                                 WHERE m.ID_MEMBER != 0
  38.                                         AND b.countPosts = 0
  39.                                         AND m.ID_BOARD = b.ID_BOARD", __FILE__, __LINE__);
  40.                         list ($_SESSION['totalMembers']) = mysql_fetch_row($request);
  41.                         mysql_free_result($request);
  42.                         $_SESSION['recountedMembers'] = 0;
  43.                 }
  44.  
  45.                 // Lets get the members and their post counts.
  46.                 // Make sure that we only get boards that have posts count enabled.
  47.                 // !!! On a big board 200 might be a bit to high to count.
  48.                 $request = db_query("
  49.                         SELECT m.ID_MEMBER, COUNT(m.ID_MEMBER) AS posts
  50.                         FROM ({$db_prefix}messages AS m, {$db_prefix}boards AS b)
  51.                         WHERE m.ID_MEMBER != 0
  52.                                 AND b.countPosts = 0
  53.                                 AND m.ID_BOARD = b.ID_BOARD
  54.                         GROUP BY m.ID_MEMBER
  55.                         LIMIT $_REQUEST[start], 200", __FILE__, __LINE__);
  56.                 $total_rows = mysql_num_rows($request);
  57.                 $_SESSION['recountedMembers'] += $total_rows * 200;
  58.  
  59.                 // Get all the results and assign the correct value.
  60.                 while ($row = mysql_fetch_assoc($request))
  61.                         db_query("
  62.                                 UPDATE {$db_prefix}members
  63.                                 SET posts = $row[posts]
  64.                                 WHERE ID_MEMBER = $row[ID_MEMBER]", __FILE__, __LINE__);
  65.  
  66.                 $_GET['start'] += 200;
  67.                 if ($total_rows <200)
  68.                         $_GET['start'] = -1;
  69.                 nextStep();
  70.         }
  71. }
  72.  
  73.  
  74. function show_header()
  75. {
  76.         global $steps, $timestart;
  77.  
  78.         if (isset($_SESSION['totalMembers']) && !empty($_GET['start']))
  79.         {
  80.                 $timestart = time();
  81.                 $total_members = isset($_SESSION['totalMembers']) ? $_SESSION['totalMembers'] : 0;
  82.                 $fake_start = $_GET['start'] + 200>= $total_members ? $total_members : $_GET['start'] + 2;
  83.                 $percent = round(100 / $total_members * $fake_start, 2);
  84.         }
  85.  
  86.         echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  87. <html>
  88.         <head>
  89.                 <title>SMF Member Posts Recount</title>
  90.                 <style type="text/css"><!--
  91.                         body
  92.                         {
  93.                                 background-color: #E5E5E5;
  94.                                 margin: 0;
  95.                                 padding: 0;
  96.                                 font-size: 10pt;
  97.                         }
  98.                         #header
  99.                         {
  100.                                 font-weight: bold;
  101.                                 font-size: 200%;
  102.                         }
  103.                         #content
  104.                         {
  105.                                 background-color: #CECED7;
  106.                                 border: 1px solid #8B9AAA;
  107.                                 padding: 2px;
  108.                         }
  109.                         #footer
  110.                         {
  111.                                 font-size: 12px;
  112.                                 text-align: right;
  113.                                 font-style: italic;
  114.                         }
  115.                         #container
  116.                         {
  117.                                 width: 50%;
  118.                                 margin: 0 auto 0 auto;
  119.                         }
  120.                         h2
  121.                         {
  122.                                 margin: 0;
  123.                                 margin-top: 0.5ex;
  124.                                 margin-bottom: 0.5ex;
  125.                                 padding-bottom: 3px;
  126.                                 border-bottom: 1px dashed black;
  127.                                 font-size: 14pt;
  128.                                 font-weight: normal;
  129.                         }
  130.                         h3
  131.                         {
  132.                                 margin: 0;
  133.                                 margin-bottom: 2ex;
  134.                                 font-size: 10pt;
  135.                                 font-weight: normal;
  136.                         }
  137.                         input
  138.                         {
  139.                                 margin: 2px;
  140.                         }
  141.                         .status_bar_border
  142.                         {
  143.                                 height: 20px;
  144.                                 width: 450px;
  145.                                 background: #ffffff;
  146.                                 border: 1px solid silver;
  147.                                 margin: 0 auto 0 auto;
  148.                                 padding-right: 4px;
  149.                                 text-align: right;
  150.                         }
  151.                         .status_bar
  152.                         {
  153.                                 height: 16px;
  154.                                 margin: 2px;
  155.                                 padding: 0px;
  156.                                 text-align: center;
  157.                                 text-align: right;
  158.                         }
  159.                         .bg1
  160.                         {
  161.                                 background: #3dc0df;
  162.                         }
  163.                         .bg2
  164.                         {
  165.                                 background: #C9DDEC;
  166.                         }
  167.                         .percent
  168.                         {
  169.                                 text-align: right;
  170.                                 color: #000;
  171.                                 font-weight: bold;
  172.                                 padding-bottom: 5px;
  173.                         }
  174.                         .codebox
  175.                         {
  176.                                 background-color: #cccccc;
  177.                                 font: normal 10px "Courier New", monospace;
  178.                                 border: solid 1px black;
  179.                                 padding: 5px;
  180.                                 white-space: nowrap;
  181.                                 overflow: auto;
  182.                                 width: 95%;
  183.                         }
  184.                 --></style>
  185.         </head>
  186.         <body>
  187.                 <div id="container">
  188.                         <div id="header">
  189.                                 SMF Member Posts Recount
  190.                         </div>
  191.                         <div id="content">';
  192.  
  193.         if (!empty($total_members))
  194.                 echo '
  195.                                 <h2>Recount Status</h2>
  196.                                 <h3 style="margin: 0 0 0 40px; font-weight: bold;">Overall Process</h3>
  197.                                 <div class="status_bar_border">
  198.                                         <div class="status_bar bg1" style="width: ', $percent, '%; white-space: nowrap;">', $percent, '% (', $fake_start, ' out of ', $total_members, ' members) </div>
  199.                                 </div>';
  200.  
  201.         if (isset($percent) && $percent == 100 || isset($_GET['start']) && $_GET['start'] == -1)
  202.                 echo '
  203.                                 <h1>Recount Complete</h1>';
  204.  
  205. }
  206.  
  207. function show_footer()
  208. {
  209.         echo '
  210.                         </div>
  211.                         <div id="footer">
  212.                                 <a href="http://jaybachatero.com">JayBachatero.com</a>
  213.                         </div>
  214.                 </div>
  215.         </body>
  216. </html>';
  217. }
  218.  
  219. function nextStep($step = null)
  220. {
  221.         global $timestart;
  222.  
  223.         @set_time_limit(300);
  224.         if (function_exists('apache_reset_timeout'))
  225.                 apache_reset_timeout();
  226.  
  227.         echo '
  228.                                 <h2 style="margin-top: 2ex;">Recount Paused!</h2>
  229.                                 <h3>This recount has paused to avoid server overload.</h3>
  230.                                 <form action="', $_SERVER['PHP_SELF'], '?start=', $_GET['start'], '" method="post" name="auto_submit">
  231.                                         <div align="right" style="margin: 1ex;"><input name="timer" type="submit" value="Continue" /></div>
  232.                                 </form>
  233.                                 <script language="JavaScript" type="text/javascript"><!-- // --><![CDATA[
  234.                                         window.onload = autoSubmit;
  235.                                         var countdown = 3;
  236.                                         function autoSubmit()
  237.                                         {
  238.                                                 if (countdown == 0)
  239.                                                         document.auto_submit.submit();
  240.                                                 else if (countdown == -1)
  241.                                                         return;
  242.                                                 document.auto_submit.timer.value = "Continue (" + countdown + ")";
  243.                                                 countdown--;
  244.                                                 setTimeout("autoSubmit();", 1000);
  245.                                         }
  246.                                 // ]]></script>';
  247.  
  248.         show_footer();
  249.         exit;
  250. }
  251. ?>

I got tired of having to press tab each time when I enter my SS number in the Unemployment Insurance Benefits Online website so I made this GreaseMonkey script. I'm not so good in JS but it gets the job done. I'm pretty sure it can be written better.

JavaScript:
  1.  
  2. // ==UserScript==
  3. // @name           Unemployment Insurance Benefits Online by JayBachatero
  4. // @namespace      http://www.jaybachatero.com
  5. // @description    Go to next field when entering ssn.
  6. // @include        https://ui.labor.state.ny.us/UBC/home.do
  7. // @version        1.0
  8. // ==/UserScript==
  9.  
  10. // The name of the fields we are going to look for.
  11. var ssn_fields = new Array(
  12.         ['ILoginForm.SSN1_KEY', 3],
  13.         ['ILoginForm.SSN2_KEY', 2],
  14.         ['ILoginForm.SSN3_KEY', 4]
  15. );
  16. // Current field.
  17. var current_field = 0;
  18.  
  19. function goToNextField()
  20. {
  21.         // Stop right there.
  22.         if (current_field> 2)
  23.                 return;
  24.  
  25.         // Field properties.
  26.         var id = ssn_fields[current_field][0];
  27.         var field_properties = document.getElementById(id);
  28.         // Current value.
  29.         var current_value = field_properties.value.length;
  30.         // Check the current field.
  31.         if (current_value == ssn_fields[current_field][1])
  32.         {
  33.                 // Increase the current_field
  34.                 current_field = current_field>= 2 ? 2 : current_field + 1;
  35.                 // Move to the next field.
  36.                 document.getElementById(ssn_fields[current_field][0]).focus();
  37.         }
  38.  
  39.         // Only if the current field is not the last one.
  40.         setTimeout(goToNextField, 500);
  41. }
  42.  
  43. // Start it.
  44. setTimeout(goToNextField, 1000);
  • 0 Comments
  • Filed under: PHP Snippets
  • PhotoStream

      My 42" LCD In My RoomAnd More Rain In The AfternoonAnd More Rain In The AfternoonAnd More Rain In The AfternoonDark Monday Morning in NYCDark Monday Morning in NYC

    Calendar

    December 2008
    S M T W T F S
    « Sep    
     123456
    78910111213
    14151617181920
    21222324252627
    28293031