JayBachatero.com

Archive for the ‘PHP Snippets’ Category

Komodo (Edit) Abbreviations

Recently the Komodo (Edit) developers added a new feature to Komodo 5.1, that feature is abbreviations.  The abbreviations feature allow you to quickly replace certain keywords with snippets of code by just pressing CTRL + T (or your key of preference).  This can be extremely useful.  I decided to create an abbreviation library for CodeIgniter.

Let’s say I type cicontroller and press CTRL + T, that will be replaced with:

1
2
3
4
5
6
7
8
9
10
11
12
class MyController extends Controller
{
	public function __construct()
	{
		parent::Controller();
	}
 
	public function index()
	{
 
	}
}

MyController is highlighted by default for easy replacing.

Here is my current library.  I’ll update it as I add more items.

CodeIgniter Library

Simple Flat File Image Gallery

I did this for a co-worker. It’s a small script that scans a given directory for images and displays the html resized version as a thumbnail with a link to the full image. It’s not fully done though. I didn’t finish adding support for pages since it was something simple and did it while I had some free time at work. Maybe it may be of some use to someone.

(more…)

  • 1 Comment
  • Filed under: PHP Snippets
  • 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

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

    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.

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

      200812191431542008121918500420081219185202200812191852162008121918523620081219185255

    Latest Photos

    Citi Field
    Citi Field
    Citi Field
    Citi Field
    Citi Field
    Citi Field
    Citi Field
    Citi Field
    Citi Field
    Citi Field

    Calendar

    March 2010
    S M T W T F S
    « May    
     123456
    78910111213
    14151617181920
    21222324252627
    28293031  

    Google Ads