PHP’s chunk_split() in python…

The other day I posted some code with PHP method for compressing XFDL files. I was unfamiliar with PHP’s chunk_split function, so I googled it and came up with this reference.  I then wrote the appropriate code in Python which is a quick script like this:

#!/usr/bin/env python

#string chunk_split ( string body [, int $chunklen = 76 [, string $end = "\r\n" ]] )
def chunk_split(body,chunklen=76,end="\r\n"):
	data = ""
	for i in range(0,len(body),chunklen):
		data += body[i:min(i+chunklen,len(body))] + end
	return data

You can see this code in action through this output:

zds@CF55 ~/Desktop
$ python
Python 2.5.2 (r252:60911, Dec  2 2008, 09:26:14)
[GCC 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from chunk import *
>>> test = """Hello, how are you today?  This is a test of my python interpreta
tion of the PHP function, chunk_split().  It should take this one line of writi
ng and split it by 76 characters and add a DOS compatible line end (e.g. \r\n)
by default.  I've done my best to have code as close as possible to the PHP fun
ction.  This was done in an attempt to work out a method of compression XFDL fi
les.  I've found this function to work but still need to figure out a way to ru
n a compatible gzip method to PHP's gzencode from Python."""
>>> chunk_split(test)
"Hello, how are you today?  This is a test of my python interpretation of the\r\
n PHP function, chunk_split().  It should take this one line of writing and s\r\
nplit it by 76 characters and add a DOS compatible line end (e.g. \r\n) by defa\
r\nult.  I've done my best to have code as close as possible to the PHP functio\
r\nn.  This was done in an attempt to work out a method of compression XFDL fil\
r\nes.  I've found this function to work but still need to figure out a way to \
r\nrun a compatible gzip method to PHP's gzencode from Python.\r\n"
>>>

It appears to work!  This still doesn’t fix my compression problems with the XFDL files though.  It would appear that gzencode does the correct compression, now I just need to find a way to do the same compression in Python.  The GZipFile methods do not output the same as gzenecode() in PHP.

XFDL Viewer Update…

As I dig through miles of XML, I realize I have not posted lately. I’ve been a bit busy meeting the May 15th deadline for my project. But here’s a quick update. First, don’t forget to view the update viewer. It’s not pretty, but it works to the point I’ve set it. I’ve changed from PHP to using Python CGI (a language I’m more comfortable with) and I’ve done what took two weeks in two days. Too bad this wasn’t thought of before!! Anyhow, I figured I’d pass along this bit of code from Neil Funk that shows a better method for decompression of an XFDL in Python:

#!/usr/bin/python
from base64 import *
import zlib

# OPEN FILES
inp = open('example.xfdl','rb').read()
out = open('example.xml,'wb')

# TAKE OUT THE MIME DATA
magic = inp.splitlines()[0]
data   = inp.split(magic)

# THE TRICKY PART -- Look to the python manual for explanation.
wbits = 15+32
newData = zlib.decompress(b64decode(data),wbits)

out.write(newData)
out.close()

And drumroll please, for Chris Hutton contributed this PHP code for recompressing an XFDL from XML.  I’m working to translate this to Python, but for the time being, an XML with an XFDL extension works.

// Export XML, gzip, and base64 encode. Append XFDL header information and output to user.
// XML is the raw data to be saved.
$newxml = $xml->asXML();
// chunk_split is new to me!
$output = chunk_split(base64_encode(gzencode($newxml)));
// Add the header!
$output = "application/vnd.xfdl;content-encoding=\"base64-gzip\"\n". $output;

header("Content-disposition: attachment; filename=\"{$filename}\"");
header("Content-type: application/vnd.xfdl");

echo $output;

So thank you all for your contributions!  I’ll try to keep this blog more up-to-date.

XFDL Viewer – Good and Bad.

I used to think of myself as a decent coder, but this XFDL project is a monster.  Particularly with the parsing schemes.  Let alone the May 15th suspense on the project!!  Oh well, if it doesn’t get done, I’m still working on it…  So I broke apart the XML and just to give a taste of the parsing for this, here’s my notes:

Mind you, I’m skipping the globals but here what needs to be parsed from there:

  • Title
  • Version
  • Fontinfo
  • Bgcolor
  • Print Settings
  • Bindings

This first block is the “field” form.  This is the main part to parse because this is where all values except checks and signatures will go.

<field sid="TO"> --> sid == div id
	<itemlocation>
		<ae>
			<ae>absolute</ae> -- style='position:absolute'
			<ae>y</ae>
			<ae>x</ae>
		</ae>
		<ae>
			<ae>extent</ae> -- dimensions
			<ae>l</ae>
			<ae>w</ae>
		</ae>
		<value>THIS IS WHAT NEEDS EDITTING!</value> - input, no border
		<broderwidth>0</broderwidth> --> style
		<fontinfo> --> style
			<ae>type</ae>
			<ae>size</ae>
			<ae>attribute</ae>
		</fontinfo>
		<justify>center</justify> --> style
		<scrollhoriz>wordwrap</scrollhoriz>
		<scrollvert>fixed</scrollvert>
		<next>TO</next> -> form taborder
		<previous>DATE_A</previous> -> form taborder
		<acclabel>lorum ipsum</acclabel>
		<format>
			<ae>string</ae> --> set form input type
			<ae>optional</ae> --> validation check
			<length>
				<ae>0</ae> vert
				<ae>18</ae> horiz
			</length>
		</format>
	</itemlocation>
</field>

Next part, check boxes.  Nothing too scary here.  Much the same as above.

<check sid=""> same
	<itemlocation></itemlocation> same
	<value>on|off</value>
	<fontinfo></fontinfo> same
	<next></next> same
	<previous></previous> same
	<acclabel></acclabel> same
</check>

Here we get complicated.  Nothing bad about the labels, except there form uses labels for definitions at the end and you’ll see it later where it is reused for a new purpose.  This could pose a good challenge!

<label sid=""> same
	<itemlocation></itemlocation> same
	<value>TEXT</value>
	<linespacing>1</linespacing> OPITONAL
	<fontinfo></fontinfo> same
	<fontcolor>black</fontcolor>
	<format></format> same
</label>

Lines, not too bad… will just be a simple parsing the xml to putting them on the page.

<line sid=""> same
	<itemlocation></itemlocation>same
</line>

Buttons!  These would be easy but they will associate with signatures.

<button sid=""> same
	<itemlocation></itemlocation> same
	<value compute="">FROM CERT</value>
	<type>signature|</type>
	<vfd_signmode>custom</vfd_signmode>
	<printvisible compute="">on|off</printvisible>
	<signformat>???WTF???</signformat>
	<signature>FOR BINDINGS</signature>
	<signer>FROM CERT</signer>
	<custom:onClick>function</custom:onClick>
	<signoptions>
		<ae>omit</ae>
		<ae>triggeritem</ae>
		<ae>coordinates</ae>
		<ae>ufv_settings</ae>
	</signoptions>
	<vfd_group>??</vfd_group>
	<format></format> same
	<previous></previous> same
	<next></next> same
	<image>FROM CERT?</image>
	<signatureimage>FROM CERT?</signatureimage>
	<signitemrefs>
		<ae>LOCK BINDS</ae> -- USE FOR VALIDATION
	</signitemrefs>
</button>

Not even touching signatures until I get the parsing scheme in place!

<signature>
	WILL DO LATER
</signature>

Data will be easy enough, just needs to be decoded and displayed.  Whew… may need to save the files to the server to display but I think HTML can handle an image from data.

<data sid=""> relates to signatures & images
	<filename></filename> -- image (optional)
	<mimedata encoding="base64-gzip"></mimedata>
</data>

Do I really need the toolbar if I’m not using it’s functions?

<toolbar sid="TOOLBAR">
	<bgcolor>
		<ae>gray60</ae>
	</bgcolor>
</toolbar>

Is IBM so dense they can’t find a new name for the toolbar tags instead of reusing label?

<label sid="TOP">
	<itemlocation>
		<ae>
			<ae>within</ae>
			<ae>TOOLBAR</ae>
		</ae>
		<ae>
			<ae>absolute</ae>
			<ae>0</ae>
			<ae>0</ae>
		</ae>
	</itemlocation>
	<image>PAGE1.ArmyLogoTop</image>
	<imagemode>clip</imagemode> -- style
	<active>off</active>
</label>

I won’t complain about this one!  Seems pretty simple, just like lines…

<spacer sid=""> same
	<itemlocation></itemlocation> same
</spacer>

So keep watching the blog… I’ll post more when I figure out how to handle all this data.  And this all needs to be cycled per page to output to the browser… yuck.

XFDL Viewer

The scope of the “Apps 4 the Army” competition that I am making my XFDL Viewer for is to use web apps.  So I did a quick change to PHP and read up and got a framework setup.  The idea is to be able to import the XFDL file and then parse the resulting XML to input values, either on a single form or by batch through an uploaded database.

The framework is complete and I’m pretty happy with the results as I’ve done much more application programming than web development, but PHP isn’t too bad.  After getting the frame work together, I started to work on parsing.  I mistakenly uploaded a XFDL file rather than the decompressed XML and I found something remarkable.  PHP’s SimpleXML apparently can decompress the XFDL file!! This takes out a very complicated step of my project!!!  I’m still working to figure out why the recompression does not produce the same output but that may not be a problem with the web interface.  Stay tuned for more updates.

Ubuntu 10.04 – Fixing Title Bar Buttons (Place on the right)

After downloading Ubuntu 10.04 Lucid Lynx, I noticed that the biggest user interface change is the title bar buttons.  Unlike Windows or Mac, the buttons are located on the left side.  This is not too bad if you can get used to it, but I found several complaints online.  So, for those who are not used to gconf-editor, here are instructions to fixing the title bar.

  1. Open a terminal or use Alt+F2 and type ‘gconf-editor’.
  2. In the tree menu on the left side go to: apps -> metacity -> general
  3. On the options menu (right side of the editor) find the option midway down for ‘button_layout’.
  4. Editing these options can let you customize the menu, here’s how:
    • The colon ‘ : ‘ sets the middle, so anything on the left of the colon is on the left of the title bar.
    • Settings are comma separated values of menu, minimize, maximize, close, separator
      • Duplicates and unknown values are silently ignored.
    • Example #1: menu:minimize,maximize,close
    • Example #2: close,minimize,maximize:
    • Example #3: minimize,maximize,close:

See, one of the great things with Linux is that these configurations are available.  If you take a moment to look around your gconf-editor, you’ll see how powerful this tool is to your user interface customization.  Feel free to contact me or comment here with questions regarding this process.

Ubuntu 10.04 Alpha 2 – Review

So I decided to update my laptop to Ubuntu 10.04.  This process got off to a rough start because a pesky dog ran into my laptop, shutting the lid and causing a sleep halfway through the upgrade.  The problem came in that the computer was not set to be able to resume and so I only had half the updates done.  So when I rebooted, there was no graphical display, and for awhile, not even the ability to use a terminal.  Thankful, grub had been updated and I was able to boot into the recovery counsel, compile a wireless driver, get an internet connection and download and install the rest of the updates manually rather than through ‘sudo upgrade-manager -d’.  Once that was done, I was amazed at the new Ubuntu.

Improvements that I noticed:

  1. Better Graphical Display (Finally away from the orange/brown theme!)
  2. Applets, as per the past four releases, are better in bothe appearance and use.
  3. Ubuntu is keeping with its integration of web based applications and social networking through the Desktop.

There are many more improvements but these are the ones that I’ve noticed first.  I’ve also seen through Launchpad that bugs are being addressed, which in this pre-review from last year, it would appear the the integration of fixes for the bugs in Linux has improved quite aways in the past year, and this release appears to be continuing the quality product of Ubuntu Linux!

XFDL in Linux :: Update

So, after talking with the Army’s G6 (essential the IT Department), the competition I’m entering (which because I’m not an active duty Reservist, I cannot receive prize money) is focused on web applications, not desktop applications.  The changes you can expect to see in my coding is that there will be quite a bit of php, perl and python that I’ll be using rather than the application side.  At the same time though, I’ll take what I do in this web application and use it for developing a desktop application and a Microsoft Office plugin that can be used to batch load files.  Just figured I’d acknowledge the reason for changes.  On the plus side of all of this, perl has a specific Mime module and php has a great and simple interface to xml parsing, so this may end up easier as a web application.

Python Curses – Custom Menu

Continuing on the same project as the previous post, I came to wanting to make a custom menu with curses in Python.  Realizing that there are functions to create menus in curses already, I wanted to build this fro m the bottom up.  The concept was to produce a menu that would highlight the selection change on the arrow keys or direct input, and then on a press of the Enter key the menu would return that selection.

Now, while writing this, you’ll see in my code that I took probable the least efficient way of building this menu, but it helps in making itself explanatory for the person learning curses.  As for the context of this coding, I built it as a function in my XFDL viewer, so there are 5 options in the menu.  I used win.keypad(0) to enable the use of the arrow keys but for some reason, the curses.KEY_UP was not being detected so the arrow key up and arrow key down are 259 and 258, respectively.  This does work though, I also have the menu catch numbers 1-5 and set the highlighted line accordingly.

def menu():
    curses.init_pair(1,curses.COLOR_RED, curses.COLOR_WHITE)
    screen.keypad(1)
    pos = 1
    x = None
    # I'm going to be lazy and save some typing here.
    h = curses.color_pair(1)
    n = curses.A_NORMAL
    while x != ord('\n'):
        # Gotta reset the screen from the root or lose the border, window, etc.
        screen.clear()
        screen.border(0)
        screen.addstr(2,2, "XFDL VIEWER", curses.A_STANDOUT)
        screen.addstr(4,2, "Please select an option...", curses.A_BOLD)
        # Detect what is highlighted by the 'pos' variable.
        if pos == 1:
            screen.addstr(5,4, "1 - XFDL -> XML",h)
        else:
            screen.addstr(5,4, "1 - XFDL -> XML",n)
        if pos == 2:
            screen.addstr(6,4, "2 - XML  -> XFDL",h)
        else:
            screen.addstr(6,4, "2 - XML  -> XFDL",n)
        if pos == 3:
            screen.addstr(7,4, "3 - Show XML",h)
        else:
            screen.addstr(7,4, "3 - Show XML",n)
        if pos == 4:
            screen.addstr(8,4, "4 - Exit",h)
        else:
            screen.addstr(8,4, "4 - Exit",n)
        if pos == 5:
            screen.addstr(9,4, "5 - DEBUG", h)
        else:
            screen.addstr(9,4, "5 - DEBUG", n)
        screen.refresh()
        x = screen.getch()
        # Is 'x' 1-5 or arrow up, arrow down?
        if x == ord('1'):
            pos = 1
        elif x == ord('2'):
            pos = 2
        elif x == ord('3'):
            pos = 3
        elif x == ord('4'):
            pos = 4
        elif x == ord('5'):
            pos = 5
        # It was a pain in the ass trying to get the arrows working.
        elif x == 258:
            if pos < 5:
                pos += 1
            else:
                pos = 1
        # Since the curses.KEY_* did not work, I used the raw return value.
        elif x == 259:
            if pos > 1:
                pos += -1
            else:
                pos = 5
        elif x != ord('\n'):
            curses.flash()
            # show_error() is my custom function for displaying a message:
            # show_error(str:message, int:line#, int:seconds_to_display)
            show_error('Invalid Key',11,1)

    return ord(str(pos))

I’ve highlighted the lines pertaining to my work around for the key pad.  This function will return the menu option and then that is processed for a reaction.  Reminder: the ‘screen’ object for my curses window is a global variable. I’m quite thrilled at the simplicity of this and the curses library, although I am disappointed in the lack of tutorials on the web deeper than typical ‘Hello World’ tutorials, but I hope these posts go to help others exploring this library!

Here’s a nice picture of the library in action:

Customize menu in action...
Be sure to comment on these tutorials and let me know if there is more detail needed or if they are helpful!!

Python Curses – Custom Progress Bar

Custom Progress BarIn making a visual editor for my XFDL project, I’ve decided to do the basic start in curses.  This is a module of python/library of C that I have not been able to utilize much at this point despite interest.  Unfortunately, there seems to be limiteexid documentation beyond the standard ‘Hello World’ type of program.  I’ll write this short tutorial with the assumption you can get through those tutorials and utilize this object.

The concept here is to create a progress bar in curses.  This example does not have any data but a basic understanding of python would allow you to create the refreshes on percentage of completion rather than time based as I have here.

def show_progress():
    #Create a window object.
    win = curses.newwin(3,32,14,10)
    # Add the Border
    win.border(0)
    # Current text: Progress
    win.addstr(1,1,"Progress ")
    # This is to move the progress bar per iteration.
    pos = 10
    # Random number I chose for demonstration.
    for i in range(15):
        # Add '.' for each iteration.
        win.addstr(1,pos,".")
        # Refresh or we'll never see it.
        win.refresh()
        # Here is where you can customize for data/percentage.
        time.sleep(0.05)
        # Need to move up or we'll just redraw the same cell!
        pos += 1
    # Current text: Progress ............... Done!
    win.addstr(1,26,"Done!")
    # Gotta show our changes.
    win.refresh()
    # Without this the bar fades too quickly for this example.
    time.sleep(0.5)

See, nothing to it.  The first line creates a new window object for and the next line adds its border.  I mapped out the relative positions for text, and you’ll want to customize this to what you need to use in your program, but for the sake of demonstration, it will show a period for each iteration.  As previously stated, you can customize this to be data or percentage driven in your program.  Don’t forget to call win.refresh() for each iteration you want to show on the screen!

XFDL in Linux :: Part 2

More for the ongoing series of producing an XFDL viewer in Linux.  In the previous tutorial, we decompressed an XFDL file, although I have had trouble recompressing the file.  It turns out that I need to do some experimentation and find the exact compression method used in gzip to be able to make the form readable.  That will be for the next update though.  I thought I would give a short preview of what’s next on this.

An XFDL file is an XML (xform) by IBM meant to run through their interpreter. IBM has some great documentation on this format.  PureEdge works much like a browser does to decompress the file by Mime-type and to then parse and read the file, including embedded binaries (for pictures, files, etc) and embedded coding (custom functions).  My interpreter will have a long ways to go so I’ll be happy to just be able to place my values in the correct fields.  I’m re-reading XML parsing within Python to make this an easy function, so be patient on that part.  But for those eager to see what I’m talking about, I’ve pasted a small section of XML from a decompressed XFDL.

      <field sid="NAME">
         <itemlocation>
            <ae>
               <ae>absolute</ae>
               <ae>9</ae>
               <ae>448</ae>
            </ae>
            <ae>
               <ae>extent</ae>
               <ae>461</ae>
               <ae>24</ae>
            </ae>
         </itemlocation>
         <value></value>
         <borderwidth>0</borderwidth>
         <scrollhoriz>wordwrap</scrollhoriz>
         <scrollvert>fixed</scrollvert>
         <fontinfo>
            <ae>Times New Roman</ae>
            <ae>10</ae>
            <ae>plain</ae>
         </fontinfo>
         <format>
            <ae>string</ae>
            <ae>optional</ae>
         </format>
         <previous>NAME</previous>
         <next>GRADE</next>
         <acclabel>d ay form 46 44-r, december 19 82.
ay p d. p e version 1.00.
edition of 1 august 19 77 is obsolete.
army reserve reenlistment data.
for use of this form, see ay r 1 40-1 11, the proponent agency is r c p ay c.
item 1. enter name using last name comma first name comma middle initial format.</acclabel>
      </field>
As you can see, there is a <value> tag for these nodes.  For my next post, I’ll write some python code to break this xml to an object that can print the label and insert a value into the xml.  There is a lot of work to interpret the embedded items, code and other tags, but this will be a start!

XFDL in Linux :: Part 1

Earlier, I wrote about using PureEdge Viewer, which is Windows software from IBM on Linux through Wine. This got me thinking, do we need to use Windows software. A quick look at the file and through Google and it’s easy to see that an *.xfdl file is a gzipped, base64 encoded xml file. So this is part one of what I hope to be a tutorial into designing software on Linux using python to open, read and write xfdl files in the same way as PureEdge Viewer. It gets annoying, to say the least, to need to open Windows in VirtualBox, or an actual install to read and edit *.xfdl files. The barriers I see at this preliminary point is to convert the xml to a readable image and then to make that editable where possible.

So, in this first part, we will do the very basic converting an *.xfdl file to an *.xml file. The code should be self explanatory but if there are questions, post them through comments and I’ll do my best at getting an answer to you.

#!/usr/bin/python
""" IMPORTS """
from base64 import *
import gzip, os, sys

""" DEATH!!! """
# Standard way to die...
def die(msg=None):
    if msg == None:
        msg = "Unknown error."
    print " [*] ERROR - %s" % msg
    sys.exit(1)

""" CHECK FOR FILE """
# No file name, then we have nothing to do!
if len(sys.argv) < 2:
    die("Did not specifiy a file name.")

""" GET FILE """
# In a more advanced version, this will check the magic value of the file as well to
# ensure it is an *.xfdl file.
filename = sys.argv[1]
print "Using %s" % filename

""" OPEN FILE AND SPLIT """
# Nothing tough, grab the magic number (1st line) and then store the rest as a variable.
f = open(filename,'r').read()
magic = f.splitlines()[0]
print "magic: %s" % magic
data = f.split(magic)[1]
print "Got Data."
del f

""" BASE64 DECODE """
# First we decode the base64.
f = open('temp.gz','wb')
f.write(b64decode(data))
f.close()
print "Base64 Decoded."

""" GUNZIP DATA """
# Yes, I know this writing to a file and then deleting it is ugly but I have not found
# a way to gunzip from a data stream.
f = gzip.open('temp.gz','rb')
gunzip_data = f.read()
f.close()
os.remove('temp.gz')
print "Gunzipped Data."

""" SAVE XML FILE """
# As this gets more advanced, it should be able to stay as a data stream for editing.
filename = filename.split(".")[0] + ".xml"
f = open(filename,'wb')
f.write(gunzip_data)
f.close()
print "Saved to temporary file '%s'" % filename

Nothing too involved here, simply open the file, strip out the first line and the decode the rest from base64 and gunzip that data to get the xml inside.  In the next tutorial, we’ll look at the structure of that xml, once I actually understand it or find decent documentation on it!

Custom Brush for SyntaxHighlighter Evolved

Okay, before I get started let me just say that I realize there are several already set-up plugins for the WordPress Plugin, SyntaxHighlighter Evolved to include a brush for the *.batch language.  Also, there is a great article by the Plugin’s author that will guide you through developing a brush.  The reason I am posting mine is to pass a long a few things that went unsaid for the less experienced WordPress Plugin developer, but let me just say that this was much easier both in the plugin development and brush development than I thought it would be.  The reason I made my own brush, it’s just more fun to make my own!

Now, that being said, here we go!   My post for “Violating IT Policy” included a batch script for Cygwin to be portable.  since there was no brush in SyntaxHighlighter Evolved To develop a brush, you will need to set-up a new plugin for word press.  The plugin will be adopted by SyntaxHighlighter Evolved as you will see in a moment.  The structure for this plugin will be:

<root folder>/wp-content/plugins/
                        /plugins/<javascript brush>.js
                        /plugins/<php plugin>.php

For the javascript brush, this does have some tricky elements. but I’ve commented what I’m doing below.

/** shBrushBatch.js **/
/* Declare a new brush with SyntaxHighlighter.brushes.<your name for the brush> */
SyntaxHighlighter.brushes.Batch = function()
{
	/* Declare words that you need highlighted */
	var variable = 'clear cls goto set';
	var constants = 'if or';

	/* This is the heart of your brush, you can use the *.
	/* SyntaxHighlighter.regexLib.* brushes when applicapble. */
	this.regexList = [
		/* You may notice that my comments are looking for a css that does not match. */
		/* I'll explain below the script. */
		// comments
		{ regex: /(^::|rem).*$/gmi,                             css: 'string'},
		// stings
		{ regex: SyntaxHighlighter.regexLib.doubleQuotedString, css: 'comments'},
		{ regex: SyntaxHighlighter.regexLib.singleQuotedString, css: 'comments'},
		{ regex: /echo.*$/gmi,                                  css: 'keyword'},
		// variables
		{ regex: /%w+\1/gmi,                                    css: 'keyword'},
		{ regex: /%\*|%%?~?[fdpnxsatz]*[0-9a-z]\b/gmi,          css: 'keyword'},
		// clear cls keywords
		{ regex: new RegExp(this.getKeywords(variable), 'gmi'), css: 'variable'},
		// if and key words
		{ regex: new RegExp(this.getKeywords(constants), 'gmi'),css: 'constants'},
		// labels
		{ regex: /^:.+$/gmi,                                    css: 'script'}
	];
};

/* Create the highlighter object. */
SyntaxHighlighter.brushes.Batch.prototype = new SyntaxHighlighter.Highlighter();
/* Set the Aliases */
SyntaxHighlighter.brushes.Batch.aliases = ['bat','batch'];

So the reason that my css does not match the actual of what it is would be because the theme does not match my preference.  I perfer comments to be green, so since the string for the RDark format is green, I had to assing my comments to the string.  This was pretty confusing but the way I figured out my color scheme was simple enough.  In <root>/wp-content/plugins/syntaxhighlighter/styles/ you will find all of the css files to coordinate with the theme.  Half way down, you will see “Actual Syntax Highlighter Colors” and it is at this point that you will want to see what all of your options are.  Rather than converting #5CE63B to green, I went and made a sample script once my plugin was complete, I then went and set up a color tester like this, in place of my normal brush file:

/** shBrushBatch.js  - color test version **/
SyntaxHighlighter.brushes.Batch = function()
{
	/* 	set your wordpress to:
			1
			string
			comments
			... continue until the last keyword ...
			color3
			1 I dropped the correct closing in the example for formatting. */
	var string = 'string';
	var comments = 'comments';
	// ... continue to the last keyword ...
	var keyword = 'color3';

	this.regexList = [
		{ regex: new RegExp(this.getKeywords(string), 'gmi'), 	 css: 'string'},
		{ regex: new RegExp(this.getKeywords(comments), 'gmi'),  css: 'comments'},
		// ... continue to the last keyword ...
		{ regex: new RegExp(this.getKeywords(color3), 'gmi'), 	 css: 'color3'}
	];
};

SyntaxHighlighter.brushes.Batch.prototype = new SyntaxHighlighter.Highlighter();
SyntaxHighlighter.brushes.Batch.aliases = ['bat','batch'];

After doing this and seeing the colors, I could then associate which color I wanted for which sets of syntax.

Now, onto making the plugin work.  Obviously, you’ll need both of these files to have any results.  What this does is when you activate this plugin (yes, it does show in your plugins) then SyntaxHighlighter adopts it.  This script is pretty standard from what I saw online and is almost exactly out of the author’s blog post.  The comments at the front are what will appear in your WordPress under Plugins.  I’ve kept the comments mostly the same as the author, as he is describing it better than I could.

<?php
/*
Plugin Name: Batch Brush - SyntaxHighlighter Evolved
Descriptionn:  Adds support for the Batch language to SyntaxHighlighter Evolved.
Author: Zachary D. Skelton
Version: 1.0.2
Author URI: http://www.skeltonnetworks.com/
*/

// SyntaxHighlighter doesn't do anything until early in the "init" hook.
add_action('init','syntaxhighlighter_batch_regscript');

// Tell SyntaxHighlighter about this new brush.
add_filter('syntaxhighlighter_brushes','syntaxhighlighter_batch_addlang');

// Register the brush with WordPress.
function syntaxhighlighter_batch_regscript() {
	wp_register_script('syntaxhighlighter-brush-batch',
		plugins_url('shBrushBatch.js',__FILE__),
		array('syntaxhighlighter-core'),'1.0.2');
	}
// Add alternative names for your brush.
function syntaxhighlighter_batch_addlang($brushes) {
	$brushes['batch'] = 'batch';
	$brushes['bat'] = 'batch';

	return $brushes;
	}
?>

At this point you should be able to load your page.  If it cannot find the brush (you’ll get a dialog to let you know), you could replace plugins_url() in line 19 with a string of a direct path to the file, but there should be no need for it.  The most common reason for this is an error in your javascript brush, so make sure to check that before going to insane.  After this is all done, you may have problems seeing the results.  It took me 5 loads of the correct format before I saw the change, in that case, deactivate the plugin, clear the cache, change the plugin version number in line 6 and re-activate the plugin.  Should not need all of that but some browsers are fussy, and that takes care of any problem with the cache. As I said before, this is well documented and relatively easy to make.  You can see that I made my plugin only apply to my script but it would be easy to add more things to it.  I used a lot of my regex directly from this plugin because that is a skill I’m still working on.  But this was surprisingly simple, and you can find lots of good information on making plugins of your own.  It is surprisingly not too tough!

Complaint

“I’m a PC and Windows 7 was my idea.”

This has been annoying me for the past several months, and since I have a blog, I’m going to complain.  First of all, Windows is proprietary and if it was your idea and they didn’t pay you for it, oh well, that’s business.  Secondly, the crap in Windows 7 isn’t that great; I bought my wife a Dell Mini-10 for her birthday and it came with Windows 7.  I immediately installed Ubuntu 9.10 Netbook Remix and the comparison without even numbers, just general usage, screen real estate and speed, Ubuntu is far superior.  And finally, Ubuntu and Linux itself are open source which means that I can truly say, as a contributer (bug triage):

“”I’m a PC and Ubuntu truly is partly my idea.”

64bit Flash for Linux

This has been a pain for a long time.  I went though and installed flash for Linux in this method but it only half worked for my 64bit distro in Ubuntu.  Too many problems coordinating with 32bit libraries and I would have flash applications that loaded fine but would not accept clicks, for example a video that I cannot pause or fast forward.  I set out to find a solution, as I realized this was a problem not with flash but with coordinating that flash with a 64bit distro.  For those interested, there is a 64 bit, Linux release and it’s installation is far more simple.  It is located on Adobe’s website, but hidden deep in some of the small text, definitely not a feature that stands out until you read through the page.  Here’s the solution:

#!/bin/bash
## installFlash64.sh

function die {
	if [ "$1" == "" ]; then
		echo "[*] ERROR -- Not Specified."
	else
		echo "[*] ERROR -- $1"
	fi
	exit 1
}

function check_root {
	if [ "`whoami`" != "root" ]; then
		die "You need root to install the 64bit Flash Player."
	fi
}

## Set root user
check_root
echo "You are root, the install will begin now."

## Download the package
echo
echo "Downloading..."
wget http://download.macromedia.com/pub/labs/flashplayer10/libflashplayer-10.0.45.2.linux-x86_64.so.tar.gz

## Untar the package
echo
echo "Unpacking..."
tar xzvf libflashplayer-10.0.45.2.linux-x86_64.so.tar.gz

## Move the file
echo
echo "Installing..."
cp -v libflashplayer.so /usr/lib/mozilla/plugins

## Clean Up
echo
echo "Cleaning..."
rm -rf libflashplayer.so libflashplayer-10.0.45.2.linux-x86_64.so.tar.gz

## Done!
echo "Done!"
exit 0


Violating IT Policy!

So, I work as a social worker and we are not expected to do much more than document with our computers. For job security reasons, I use my hobby programming in work to make the workplace more efficient and ensure that I have a job tomorrow! There are some problems with this, most notably, we are not setup to do programming. So I’m going to list my favorite tricks to get setup at work.

First of all, although we work through a company ethernet connection; we do have some flexibility. For one, I read the IT Policy and it specifically states I cannot install an executable onto the harddrive. Well, problem easily solved, I have a flash drive, so in comes a website for portable applications.  Here’s the applications I use the most.

*        GOOGLE CHROME

Google Chrome is a great browser.  It’s ability to interpret javascript leaves IE# in the dust!  As for it’s portability, just like most of these, just run the executable and install it to your flash drive (or if you want a little more speed and less portability, your harddrive).  Now, Google Chrome works great right out of the box, except one thing.  It’s flash needs to be updated.  Fortunately, this is easy to solve and there are only a few steps.

  1. Get FlashPlayer XPI for Windows.
  2. Rename the *.xpi to *.zip and copy flashplayer.xpt and NPSWF32.dll
  3. Paste those two files into <install directory>/App/Chrome-bin/<version>/ directory.  Replace install directory with where you installed the application and version with the folder that is your version number.

**            NOTEPAD++

This stuff is simple, but I thought I’d post a link for the plugins.  These are easy to install, plugins go in the \Plugins\ folder, configuration files go in the \Plugins\Config\ and documentation goes in \Plugins\Doc\. Here are my favorite plugins:

***   BOA CONSTRUCTOR

For this to work, you’ll need Portable Python which installs similar to most things but it altered file associations and so I just extracted it and then it runs fine.  You’ll also want to install wxPython which is a module for python located on the Portable Python Page.  For Boa Constructor, download the file and install it on a computer of your own.  From there it copies onto the flash drive just fine, just make sure to update the settings for your main python exectuable since it won’t be in the computer’s path.

****       CODEBLOCKS

CodeBlocks is another program that will copy straight from the computer to your flash drive to make it portable.  The real trick is getting paths set and configuring WX if you’re interested in rapid development of GUI’s in C.  So here’s the quick run through.  For paths, I struggled with this on my work computer but it will not read paths that have a space.  It sees that space as the end of your path, so when setting a path for a library, for example WX, make sure you store that in a location that does not have a space in the path.  As for configuring WX, here’s what I did.  I went to the WX Pack site and got the package, which is great.  No need to compile or anything, does it all on it’s own with a directory to link your CodeBlocks library to, just be aware, this will take a lot of space on your flash drive, but with all of these things, you can always store them locally if you are daring that much against your local IT Department!

*****          CYGWIN

The biggest pain and the best program to have for avid Linux users.  This site makes a great tutorial on how to do this.  And rather than reiterating this, I’ll add my own changes.  The biggest thing was I changed the startup script and then linked that script on my desktop.  The first thing to do is to install Cygwin on your flash drive, be aware it may not install all the programs you want.  I did it three times before it all worked.  Then you have to mount the drive as you cannot do this or it will write to the registry.  As for my script, here’s what I wrote:

;rem creating a startup script
@echo off
:: clear
cls

:: locate directory
echo Locating directory...
for /F %%A in ('cd') do set WD=%%A
:: create path
echo Setting Path...
set path=%WD%\bin;%path%
set SHELL=/bin/bash
set CYGWIN=codepage:437
set HOME=/home/zbert
:: mount fs
echo Mounting File System...
bin\mount -bfu %WD%/ /
bin\mount -bfu %WD%\bin /usr/bin
bin\mount -bfu %WD%\lib /usr/lib
:: login
echo Logging in...
echo.
echo.

if "%1" == "" GOTO noopt
	if "%1" == "c" goto cmd
	if "%1" == "-c" goto cmd
	if "%1" == "--cmd" goto cmd
	if "%1" == "h" goto help
	if "%1" == "-h" goto help
	if "%1" == "--help" goto help
	echo  [*] ERROR: Unrecognized option.
	goto usage

:usage
echo usage: cygwin [c^|h^|-c^|-h^|--cmd^|--help]
goto help

:help
echo.
echo          Portable Cygwin Loader v1.0
echo.
echo     Commands:            *Run normal with no options.
echo        c ^| -c ^| --cmd     Run in Command Prompt.
echo        h ^| -h ^| --help    Display Help Message
echo.
echo        Copyright Skelton Computers © 2009
echo.
goto end

:cmd
echo cls > bin\bash -l -i
GOTO end

:noopt
bin\bash -l -i
GOTO end

:end
set path=%WD%\bin;%path%
@echo on

Gnome Applets.

Being an avid Ubuntu user, I wanted to tinker in building for the Gnome interface.  An applet was the perfect idea but a tutorial on the internet was hard to find.  I found a few like these that are in C, which I’m fond of C but for rapid development and tinkering, Python works much better.  Besides, that is my primary programming language and kindly, Ubuntu came with all the required modules to build in this way.  Most tutorials for Python were out of date.  One of the best I found in Python was this one and my personal favorite which shows a good use of classes.

Onto the tutorial!

The first thing you need to do is build a server file.  This will go to /usr/lib/bonobo/servers with root access.  The reason for this file is for Gnome to be able find your file and set it as an applet.   The lines to pay attention to are:

Line 4:          This is the full path to your source code file.
Lines 9-10:  Name and describe your applet.
Line 20-23: These are the options you can set for the applet browser.

<oaf_info>
    <oaf_server iid="OAFIID:SampleApplet_Factory"
        type="exe"
        location="/home/zbert/Desktop/testing/py/applet.py">
        <oaf_attribute name="repo_ids" type="stringv">
            <item value="IDL:Bonobo/GenericFactory:1.0"/>
            <item value="IDL:Bonobo/Unknown:1.0"/>
        </oaf_attribute>
        <oaf_attribute name="name" type="string" value="Sample Applet Factory"/>
        <oaf_attribute name="description" type="string" value="Sample Applet's Factory that launches the applet"/>
    </oaf_server>
    <oaf_server iid="OAFIID:SampleApplet"
        type="factory"
        location="OAFIID:SampleApplet_Factory">
        <oaf_attribute name="repo_ids" type="stringv">
            <item value="IDL:GNOME/Vertigo/PanelAppletShell:1.0"/>
            <item value="IDL:Bonobo/Control:1.0"/>
            <item value="IDL:Bonobo/Unknown:1.0"/>
        </oaf_attribute>
        <oaf_attribute name="name" type="string" value="Sample Applet"/>
        <oaf_attribute name="description" type="string" value="Sample applet's description."/>
        <oaf_attribute name="panel:category" type="string" value="Utility"/>
        <oaf_attribute name="panel:icon" type="string" value="gnome-laptop.png"/>
    </oaf_server>
</oaf_info>

Now with the server file in place, it’s time to build the applet. I used a picture to show my applet as a small 16×16 PNG smiley face.   As for the coding, I’ve commented what needs to be done here.

#!/usr/bin/env python
### NORMAL IMPORTS
import sys
import gtk
import pygtk
import gnomeapplet

pygtk.require('2.0')

### CREATE A MENU WITH XML
def create_menu(applet):
    xml = """
    <popup name="button3">
        <menuitem name="Item 1" verb="Networks" label="_Networks...."
            pixtype = "stock" pixname="gtk-properties"/>
        <menuitem name="Item 2" verb="Help" label="_Help"
            pixtype = "stock" pixname="gtk-help"/>
        <separator/>
        <menuitem name="Item 3" verb="About" label="_About..."
            pixtype = "stock" pixname="gnome-stock-about"/>
    </popup>
    """
    verbs = [('Networks',show_networks), ('Help',show_help), ('About',show_about)]
    applet.setup_menu(xml, verbs, None)

### WHAT HAPPENS WHEN MENU ITEM IS CLICKED
def show_about(*arguments):
    print(arguments)

### THE ASTERIK ALLOWS MULTIPLE ARGUMENTS TO BE PASSED
def show_networks(*arguments):
    print(arguments)

### ALL OF THESE SHOW IN THE DEBUG (-d) OPTION
def show_help(*arguments):
    print(arguments)

### WHERE IT ALL HAPPENS
def applet_factory(applet, iid):
    # CREATE THE XML MENU
    create_menu(applet)
    # CREATE AN IMAGE OBJECT
    im = gtk.Image()
    # SET THE FILE FOR THE IMAGE
    im.set_from_file("/home/zbert/Desktop/testing/py/face2.png")
    # ENABLE TRANSPARENCY
    applet.set_background_widget(applet)
    # CREATE AN EVENT FOR CLICKING THE IMAGE
    applet.connect('button-press-event',button_press)
    # ADD IT ALL TO THE APPLET
    applet.add(im)
    # SHOW IT!
    applet.show_all()
    # DEBUG EVENT
    print('Factory started')
    return True

### CREATE A DIALOG TO POP UP WHEN APPLET IS CLICKED
def test(*arguments):
    print(arguments)
    dia = gtk.Dialog("Message",None,gtk.DIALOG_MODAL)
    lbl = gtk.Label("this is a message")
    dia.vbox.pack_start(lbl)
    lbl.show()
    dia.run()
    dia.connect("destroy",dia.destroy)

### HANDLE THE IMAGE CLICK
def button_press(button, event):
    # LEFT BUTTON ACTIVATES THE CUSTOM MENU
    if event.button == 1:
        print "button 1"
        # CREATE A CUSTOM MENU
        m = gtk.Menu()
        i = gtk.MenuItem("Hello")
        i.show()
        # CONNECT THIS MENU ITEM TO THE DIALOG
        i.connect("activate",test,"Hello")
        m.append(i)
        # FINISH THE POPUP MENU
        m.popup(None, None, None, event.button, event.time, None)
    # RIGHT BUTTON ACTIVATES THE STANDARD MENU
    elif event.button == 2:
        # DEBUG
        print "button 2"

### STANDARD ENTRY
if __name__ == '__main__':
    # DEBUG
    print('Starting factory')
    # RUN THE APPLET AS A GTK WINDOW IN DEBUG (-d) MODE
    if len(sys.argv) > 1 and sys.argv[1] == '-d': #
        mainWindow = gtk.Window()
        mainWindow.set_title('Applet window')
        mainWindow.connect('destroy',gtk.main_quit)
        applet = gnomeapplet.Applet()
        applet_factory(applet, None)
        applet.reparent(mainWindow)
        mainWindow.show_all()
        gtk.main()
        sys.exit()
    # LET GNOME TAKE CARE OF IT AS AN APPLET
    else:
        gnomeapplet.bonobo_factory('OAFIID:SampleApplet_Factory',
            gnomeapplet.Applet.__gtype__,
            'Sample Applet', '0.1',
            applet_factory)

You can see in these pictures how the applet sits like any other in the corner.  A left click will bring up our custom coded menu with one selection, ‘Hello’ which when clicked brings up a GTK Dialog that is made in the button_press() event.  If you right click, you get the standard menu, plus our XML menu which, when in Debug (-d) mode, you will see the arguments sent by a click outputted in your terminal.  As I mentitoned, there are a handful of great resources for this that detail current Python modules with information on setting this up as I did or in a class for bigger projects.  I hope this tutorial has been helpful, and let me know what you think of these lessons with the comments interface!

VirtualBox – USB Devices

As a military member, I am issued a CAC card, which is a smart card carrying a PKI certificate for logging onto web interfaces and signing documents.  As  Linux user, I’ve been frustrated by the Windows specifics for utilizing CAC cards and have been happy to find alternatives through libcoolkey and pcsc_lite.  These work well with Firefox and I can log onto web interfaces just fine, but have not had a viable, Linux solution to signing documents.  In the military, we use an IBM product, PureEdge to utilize our forms which are *.xfdl documents.

The solution I have come to, although not purely Linux, allows me to sign documents without rebooting to another OS.  For testing purposes, I have had Windows XP running in VirtualBox so I decided to utilize that.  The problem came in that I was using virtualbox-ose and needed to not use the open source version for access to the USB devices.   The process then is relatively and well documented with simple Google searches:

  1. Set vboxuser in the user’s group.
  2. Configure your USB devices (in this case the CAC card reader) to be detected by the guest OS.
  3. Run the guest OS and install ActivClient 6.0 and Silinas ApproveIt to be able to sign *.xfdl documents.

See, simple… hmm, that sounds too close to Windows 7 add but I promise it is not!  I may be a PC but Windows 7 was NOT my idea!!

Update.

So for anyone reading this blog, I am still here, just have not added anything this week.  I’m working and have not been able to update the blog but I will soon have links and information to my linux Cribbage game.  This is near completion and I have written it using pygtk.  It is using card and game modules that I had designed for a text based run of the program and figuring no one would want to use the text based, I’ve switched to a GTK interface, which is obviously, much better.

Moodling!

Moodle Logo
Moodle

Last year, I built a framework for a training site for my company in an effort to save some money.  This framework was highly restrictive because I was keeping it on the company network.  Still, I was able to build a training site on the local intranet that embedded flash classes, wrote to a database and was something that could be adapted with time all with html, javascript and using Microsoft Access as a database.

I found a much better and more practical solution, with the thought I will not be there to write the test in html and maintain the database forever.  Along comes moodle, an open source course management system.  This is much better than anything I’d dreamt of doing with html and javascript and it’s really amazing in it’s ability to adapt to several situations.  I’m sure with this application, there will not be any problem handing off the on-line training for the company.

Linux bash script for wifi.

When starting with Backtrack4, I compiled and installed the Broadcom wl driver for my laptop’s wifi. The unfortunate thing was that I got very frustrated because the default connection manager, wicd was getting confused when I would go to a network other than the one I initially connected to after boot-up. Shortly after that, Ubuntu 9.10 was experiencing the same difficulty after upgrading the kernel. So to work around using a network manager, I created a quick script to use.

In this script, you can see that you can pass a variable for your network name and key in the same format as you would normally. This was a fun script as it could easily be adapted as it’s own network manager, even with a little more work, you could have it in the tray, it’s own gui, etc. It’s nothing more than the commands typically used to connect on wifi but it just automated the process. I then went and created a child script for my most used networks, and after placing these in my bin, I actually have started to prefer this method.

Here’s the script:

#!/bin/bash

# die with honor
function usage() {
	echo "usage: ./connectWifi \"<essid>\" [key]"
	echo "       *key in ascii prefaces s:[key]"
	echo
	exit 1
}

# killed in disgrace
function die() {
	echo
	echo "   [*] ERROR: $1..."
	echo
	exit 1
}

# check for essid
if [[ $1 ]]; then
	# setup
	echo "Starting..."
	ifconfig eth1 down
	dhclient -r eth1 -q
	echo " -Previous connection dropped."
	ifconfig eth1 up
	iwconfig eth1 essid "$1"
	echo " -ESSID Set to $1."
	# check for key
	if [[ $2 ]]; then
		iwconfig eth1 key $2
		echo " -Key set to $2."
	fi
	# connect
	iwconfig eth1 mode Managed
	echo " -Connecting..."
	echo
	dhclient eth1
	# announce success
	if [[ $? -eq 0 ]]; then
		echo
		echo "Successfully connected to $1!"
		echo
		exit 0
	fi

else
	# no variables
	usage
fi

# declare failure
die "Failed to connect to $1."

This can serve as a good example of both the commands in connecting to wifi and an introduction to bash scripting. Please comment on the code and offer any suggestions you may have!

A quick Python MySQLdb fix…

Okay, so I enjoy writing code in python a lot more than I ever would in php.  So when I came across the MySQLdb module, I was thrilled.  Now I could do anything that php could do and more, in my opinion.  Well, except php’s python comes with all sort of proctections such as mysql_real_escape()… hmm, what to do now.  Well, better come to a solution.  Here’s that solution in all of it’s glory:

#!/usr/bin/env python
## FILENAME: sql.py

def clean(unclean):
	cleaned = ""
	badchrs = "\\\/\"\'"
	temp    = ""
	for ch in unclean:
		temp = ch
		if temp in badchrs:
			temp = "\\" + temp
		cleaned += temp
	return cleaned

As for usage, just place this with your script (most likely in cgi-bin) and import it:

import sql
name    = sql.clean(form.getvalue('name'))

Obviously, there can be more escapes added but for the initial framework, this is the start of a simple fix! for discussion, what vulnerabilities are in this code? What are other ways to extend the MySQLdb module? Please comment…

Just a starter…

So, to start off, hello and thanks for visiting!  This blog has a purpose to demonstrate code examples that refers primarily to the languages of python, c/c++ and others.  Just a quick introduction, I’m a hobby programmer from Ames, Iowa.  Recently, I’ve attempted to start offering computer services to the local community and have created this site: http://www.skeltonnetworks.com/ which is in it’s intial stages but is functional.  My interests in programming relate mostly to computer security and linux.  For any comments, please feel free to write through either this blog or the contact form on my home page.