Archive for the ‘Programming’ Category

mb_strrchr in php 5.1 for compatibility

I needed php’s mb_strrchr function, which is only in php 5.2+. However, my workplace is stuck in php 5.1 and cannot upgrade to php 5.2.

I decided to write a forwards-compatible mb_strrchr() that worked in php 5.1 at least. Please test thoroughly before using. It seemed to work great for the cases I needed it for:

// php 5.1 compat
if( !function_exists('mb_strrchr') ) {
	function mb_strrchr( $haystack, $needle, $part = false, $encoding = '' ) {
		$pos = mb_strrpos( $haystack, $needle, $encoding );
		if( $pos === false ) return false;
		if( $part === true ) {
			return mb_strcut( $haystack, 0, $pos, $encoding );
		} else {
			return mb_strcut( $haystack, $pos, mb_strlen($haystack), $encoding );

SMF2 Gallery2 Integration Problem

I have a SMF2 forum site, where the gallery is implemented through Gallery2 using Oldiesmann’s SMF + G2 Integration Project.

For 2 years, I’ve had the SMF and Gallery work properly together with linked member groups (a mod option), such that member groups auto-synchronized with ones in Gallery. At some point, either with a Gallery or SMF upgrade, users reported that the Gallery part of the site threw cryptic security warnings for non-admins. I explored the issue and figured users were missing in two required groups “Everybody” and “Registered Users”. I had reported it to Oldiesmann, but he claimed he had no control over those required groups and Gallery was self-managing them.

After about a year, I delved into the code and found that in order to sync the groups that a user had in SMF with the groups that a user had in Gallery, the integration code was removing all groups and adding the shared groups. This of course meant that the user was being removed from the required groups: “Everybody” and “Registered Users”. Users with privileged groups did not see the problem.

Here’s the fix that I’ve also submitted to Oldiesmann. But his forum complains when I added the code, so I had to create this post here. It says it does not allow external links (haha).

My fix will ensure that we do not remove the user from required groups and also add the users back into required groups (if necessary). Please note that the cleanup code is necessary because anyone who has ever visited the buggy gallery will have the groups removed, so there’s a lot of cleanup to do. The code provided below should auto-correct this issue.

commit 97075cd4e20d2807011b38cd293ccc38c728db9a
Author: Inderpreet Singh <inderpreet99gmail>
Date:   Sat Jul 28 10:33:40 2012 -0500

    PJ fix for SMF Gallery2 Integration:
    Avoid g2 required groups: Everybody and Registered Users groups from getting removed
    Add user to the required groups (cleanup our mess)

diff --git a/Sources/Gallery.php b/Sources/Gallery.php
index 74652fe..c289678 100755
--- a/Sources/Gallery.php
+++ b/Sources/Gallery.php
@@ -1348,10 +1348,32 @@ function groupCheck()
+		// Avoid g2 required groups: Everybody and Registered Users groups from getting removed!
+		$groupstoignore = array('Everybody', 'Registered Users');
+		$groupstoadd = $groupstoignore;
+		foreach($galgroups as $gid => $gname)
+		{
+			if(in_array($gname, $groupstoignore))
+			{
+				unset($galgroups[$gid]);
+				$groupstoadd = array_diff($groupstoadd, array($gname));
+			}
+		}
+		// Add user to the required groups (cleanup our mess)
+		foreach($groupstoadd as $gname) {
+			list($ret, $group) = GalleryCoreApi::fetchGroupByGroupName($gname);
+			if($ret)
+			{
+				fatal_error($ret->getAsText(), 'gallery');
+			}
+			GalleryCoreApi::addUserToGroup($context['user']['g2_uid'], $group->getId());
+		}
 		// array_diff will give us an array of all the values in $galgroups that aren't in $galsmfgroups
 		// $galgroups uses the group IDs as the keys, and the group names as the values. We only want the group IDs...
 		$groupstoremove = array_diff(array_keys($galgroups), $galsmfgroups);
 		// Remove them from any group(s) they no longer belong to
 		if(count($groupstoremove) > 0)

WordPress: How to programmatically remove categories returned by get_the_category_list function?

First of all, I would recommend using the wp_list_categories function (which supports ‘exclude’ and ‘exclude_tree’ arguments) instead of get_the_category_list.

Sometimes we do not have the option of choosing which function to use. Also, it is unfortunate that the get_the_category_list function does not provide a hook to target each category in the list. So we have to use a hacky method to remove items. The following code uses regex to parse the string (list of HTML elements separated by a separator) being returned by get_the_category_list function (using ‘the_category’ filter) and removes the appropriate categories.

I would also like to note and the code below removes the “Uncategorized” category (which is the default category and the most common offender users want to remove) and also the “Feature” category (something I was using in my own code).

 * Identifies the "Uncategorized", "Feature" link
 * preg_replace_callback for bu_library_hide_uncategorized function
 * @param array $regex_parts
 * @return string 
function bu_library_hide_uncategorized_callback($regex_parts) {
	if( !$regex_parts or count((array)$regex_parts) != 2)
		return $regex_parts;
	if ( in_array($regex_parts[1], array('Uncategorized', 'Feature')) )
		return '';
	return $regex_parts[0];

 * Removes the uncategorized category from $thelist string parameter
 * Ignore wp-admin requests. Unfortunately, 'the_category' filter is used in other places with 1 argument), so we must
 * make the last 2 arguments optional (or we get PHP Warnings) and quit when the 2nd argument is not supplied
function bu_library_hide_uncategorized($thelist, $separator = '', $parents = '') {
	// short circuit for lists that do not have uncategorized category,
	// or when this function is called from wp-admin (i.e. missing separator)
	if(is_admin() or !$separator or stripos($thelist, 'Uncategorized') === false) return $thelist;
	$listitems = explode($separator, $thelist);
	$new_listitems = array();
	foreach($listitems as $item) {
		if ($new_item = preg_replace_callback('!<\s*a[^>]*>(.*?)<\s*/a[^>]*>!im', 'bu_library_hide_uncategorized_callback', $item)) {
			$new_listitems[] = $new_item;
	$thelist = implode($separator, $new_listitems);
	return $thelist;
add_filter('the_category', 'bu_library_hide_uncategorized', 10, 3);

Command line python script to get context lines on a search string

grep -B and -B flags don’t work when grep is used on the command line with readline support. So I created this little script that does work on the command line. use -h flag to learn. Here’s how I’ve used it:

ls -al | ./ -b 1 -a 1 -d test.txt

This finds the test.txt file and prints the 2 files around it.

# print context when using a python script with readline support (command line piping)
# by

import sys, re
from optparse import OptionParser

def main():
    usage = "usage: %prog [options] needle"
    parser = OptionParser(usage)
    parser.add_option("-b", "--before", type="int", dest="before", default=0,
            help='Before context lines (a la grep)')
    parser.add_option("-a", "--after", type="int", dest="after", default=0,
            help='After context lines')
    parser.add_option("-d", "--debug", action="store_true", dest="debug", default=False,
            help='Debug information.')

    #Not implemented
    #parser.add_option("-o", "--output", type="string", dest="output")
    (options, args) = parser.parse_args()
    if len(args) != 1:
        parser.error("Specify what you want to search")
    needle = args[0]
    if options.debug:
        print "\nNeedle: %s\nBefore context lines: %s\nAfter context lines: %s\n" % (needle, options.before, options.after)

    lines = sys.stdin.readlines()
    lines = [x.strip() for x in lines]
    lastline = ''
    i = 0
    for line in lines:
        if needle in line:
#        if, line):
            first = max(0, i - options.before)
            last = min(len(lines), i + options.after + 1)
            if options.debug:
                print "Found '%s' on line %d, printing line %d to %d" % (needle, i, first, last)
            for println in lines[first:last]:
                print println
            print ""
        lastline = line
        i += 1

if __name__ == "__main__":

Coldfusion Cfform bug/error: “_b is undefined”

While coding a HTML form using Coldfusion’s CFForm, I encountered a bug that threw a javascript error while submitting it.

"_b is undefined" from cfform.js

around line 3:

return false; 
return false;

One of the problems with errors while submission is that it happens very quickly and the browser redirects. So the user doesn’t see the javascript error. It also breaks execution of any (onsubmit/event) javascript code.

I went through my cfform to find any problems code. However at the end, I noticed that the cfform tag was missing the “name” attribute, and by simply adding the “name” attribute I was able to avoid the error.

This is one of the reasons I do not like ColdFusion’s helpers, because the errors that they sometimes generate are very misleading and not documented.

Django/Python: UnicodeDecodeError error printing Youtube unicoded data

I was having a problem printing Youtube’s Unicode data using my print method:

print "<p>Video: desc=%s</p>" % (vid.desc)

I’m not well versed with Unicode data, so I was just able to brute force out of this problem, and get rid of the UnicodeDecodeError “ordinal out of range”, by doing the following:

print "<p>Video: desc=%s</p>" % (unicode(vid.desc,'iso-8859-1'))

PS: My database information is in utf-8 format. So, in my understanding, this is converting that utf-8 data into iso-8859-1 to show to the users.

Coldfusion solution to Oracle’s “string literal too long” (4k chars limit)

Working on a Coldfusion app with Oracle database, I wanted to import large amounts of data into “CLOB” fields (capable of handing GBs of data). I tried using SQL Developer (by Oracle) and another user tried SQL Loader, but we were both getting the same error on the INSERT statement:

ORA-01704: string literal too long
Cause: The string literal is longer than 4000 characters.
Action: Use a string literal of at most 4000 characters. Longer values may only be entered using bind variables.

We searched online and noticed that the fix to this problem required bind variables and/or creating a procedure, etc. Really a roadblock if you’re not familiar with PL/SQL and Oracle.

But the problem could be solved using Coldfusion’s JDBC connector to Oracle. I simply wrote up the following cfml code, and noticed that the cfsqltype=”cf_sql_clob” takes care of this 4000 (4k) chars limit problem.


<cfquery datasource="dsn" username="user" password="pass">
	INSERT INTO logs_table VALUES (
		<cfqueryparam value="TEST" cfsqltype="cf_sql_varchar">,
		<cfqueryparam value="HUGE AMOUNT OF TEXT HERE (use cfsavecontent and output here)" cfsqltype="cf_sql_clob">

Chrome’s javascript sort array function is different, yet proper

Chrome’s javascript sort function behaves more like the ECMA standards than the sort function implemented in other browsers (like Firefox, IE, etc), which try to maintain some backwards compatibility with legacy javascript code.

Generally, when sorting an array, I’ve always found the following (incorrect) code:

result.sort(function(a,b) { return a > b } );

According to the ECMA standards, the right way to do it is not to return true or false, like the above function, but to return -1, 0, or 1, depending on how the two strings compare, like the following:

function sortfn (a,b) {
	var upA = a.toUpperCase();
	var upB = b.toUpperCase();
	return (upA < upB) ? -1 : (upA > upB) ? 1 : 0;

Titanium Mobile integrating Admob/Smaato with Android on Mac OSX

Titanium doesn’t have Admob/Smaato support, so it is really hard to monetize free applications without using Webview Onclick Trickery (even then you’re not utilizing the full potential of Admob’s seamless integration). Thankfully, David Ashwood has released source code for Admob and Smaato Integration on his (branch). Unfortunately, the instructions in his posts are clear but not working (some only work on Linux), however I’m using Mac OSX for development, so it doesn’t help me much. Although I had learned java jar files are platform independent, but they didn’t work in my project (but that’s topic for another day). So I figured I’ll give a broad overview of how to get it working:

Note: I’m not sure if each of these steps are necessary, but this is the way I went on doing it, so you’re welcome to ignore any step that you feel is redundant. But please do leave a comment specifying it, so in the future other developers may not face the same problems.

  1. Follow Titanium Documention to get Titanium built from source (for practice and setting up paths/classpaths that you need later)
  2. Install Android SDK and Google APIs (if you haven’t already)
  3. Clone dasher’s fork
  4. Checkout the branch
  5. If using scons, edit the android/build.xml file to fix the two paths: android.platform and google.apis. They point to the older way If using Eclipse, you can specify these two CLASSPATH variables: ANDROID_PLATFORM and GOOGLE_APIS (as the Titanium Documentation to build from Source (Step 1) suggests).
  6. Run scons or Eclipse to generate jars. I got a zip archive at the end of the process in distribution folder. (I don’t know if Eclipse does it, because I did it through scons. I would argue the regular eclipse build does it as well.)
  7. Unzip this archive in your Titanium application directory, where the following directory structure exists: mobilesdk/{platform}/{version}. Be careful if replacing files/folders. I created a new directory for the {version}, because if this build doesn’t work for whatever reason, you’ll have an option to fallback on the original Titanium version.

And amazingly, you have Admob support in (the latest version for Mac OSX).

Last tidbit: I noticed that moving these jars are very easy, just be sure to carry the other jars along with you, like the admob-sdk-android.jar and SOMAAndroidSDK.jar. I’m not sure if you need these, but I got rid of some crashes that were happening earlier.

Import error while building Eclipse Plugin in a Java Project

I was trying to build a Java project I got from somebody using the Eclipse Gallileo install. Since the workspaces were different, my Eclipse install was confused about the eclipse plugins jar files, and it threw the following error:
"The import org.eclipse cannot be resolved"
on line: import org.eclipse.core.runtime.Plugin
and other cryptic errors: "BundleContext cannot be resolved to a type"

I resolved this by adding the appropriate jar library files from my eclipse directory. Here’s how I searched which files I needed.

  1. Go to eclipse plugins: cd eclipsedir/plugins
  2. For each suspecting jar, I do jar -tvf filename.jar | grep "searchingforthisClass"
  3. In the end I found my Plugin class: jar -tvf org.eclipse.core.runtime_3.5.0.v20090525.jar | grep Plugin
  4. Right click on the project in Eclipse, choose Properties, under Libraries tab, click “Add External JARs” button, then just locate that jar that you found above.
  5. Rebuild

PS: This is more of a reference for myself than for other Java developers, because this is probably as basic as it gets.

Return top