Archive for the 'PHP' Category

PHP, Programming, phpBB

Prosilver: Online/Offline Text Tag

If you don’t like the idea of an angled online image like the one that is default in the Prosilver theme of phpBB3, you can move to an Online/Offline text tag instead.

1. Go to your ACP
2. Click on the Styles Tab
3. Under Style Components click Templates
4. Click edit next to Prosilver
5. In the dropdown, select viewtopic_body.html
6. If you already applied my Profiles Left Mod, find this code:

<dl class="postprofile<!-- IF postrow.S_ONLINE --> online<!-- ENDIF -->" id="profile{postrow.POST_ID}"></dl>

Change it to:

<dl class="postprofile" id="profile{postrow.POST_ID}"></dl>

This just removes the online class from the postprofile dl object, which is responsible for the angled online background image.

7. Find the following code:

<dt>
				<!-- IF postrow.POSTER_AVATAR -->
					<!-- IF postrow.U_POST_AUTHOR --><a href="{postrow.U_POST_AUTHOR}">{postrow.POSTER_AVATAR}</a><!-- ELSE -->{postrow.POSTER_AVATAR}<!-- ENDIF -->
				<!-- ENDIF -->
				<!-- IF not postrow.U_POST_AUTHOR --><strong>{postrow.POST_AUTHOR_FULL}</strong><!-- ELSE -->{postrow.POST_AUTHOR_FULL}<!-- ENDIF -->
			</dt>
 
			<!-- IF postrow.RANK_TITLE or postrow.RANK_IMG -->
<dd>{postrow.RANK_TITLE}<!-- IF postrow.RANK_TITLE and postrow.RANK_IMG --><!-- ENDIF -->{postrow.RANK_IMG}</dd>
 
<!-- ENDIF -->
<dd> </dd>

Change it to:

<dt>
				<!-- IF postrow.POSTER_AVATAR -->
					<!-- IF postrow.U_POST_AUTHOR --><a href="{postrow.U_POST_AUTHOR}">{postrow.POSTER_AVATAR}</a><!-- ELSE -->{postrow.POSTER_AVATAR}<!-- ENDIF -->
				<!-- ENDIF -->
				<!-- IF not postrow.U_POST_AUTHOR --><strong>{postrow.POST_AUTHOR_FULL}</strong><!-- ELSE -->{postrow.POST_AUTHOR_FULL}<!-- ENDIF -->
			</dt>
 
			<!-- IF postrow.S_ONLINE -->
<dd style="color: #00ff00">Online</dd>
 
<!-- ELSE -->
<dd style="color: #ff0000">Offline</dd>
 
<!-- ENDIF -->
 
			<!-- IF postrow.RANK_TITLE or postrow.RANK_IMG -->
<dd>{postrow.RANK_TITLE}<!-- IF postrow.RANK_TITLE and postrow.RANK_IMG --><!-- ENDIF -->{postrow.RANK_IMG}</dd>
 
<!-- ENDIF -->
<dd> </dd>

What this does is adds the following code above the Rank Title or Rank Image (or both) in the postprofile block.

<!-- IF postrow.S_ONLINE -->
<dd style="color: #00ff00">Online</dd>
 
<!-- ELSE -->
<dd style="color: #ff0000">Offline</dd>
 
<!-- ENDIF -->

You can move that chunk around and put it below the Rank Title/Image or elsewhere. Let me know if you need more help.

PHP, Programming, phpBB

Adding Post Titles To Your Forum List in phpBB3

6 weeks after it’s release, I finally found the time to upgrade the forum over at Snackbar Games to phpBB 3. My hand was kind of forced due to some massive load that was generated on our phpBB2.x install and to appease the webhosting company, I told them I was going to upgrade to a clean install of phpBB3 with no mods or hacks installed.

The one thing I missed about my modded phpBB2.x forum was that the forumlist showed the topic or subject of the last post in that forum, something that phpBB3 lacks out of the box. Luckily there is a very easy way to add this functionality back in.

1. Go to your ACP and click on the Styles Tab.
2. Under Style Components on the left, click Templates
3. On the right, click the Edit button next to Prosilver. This should bring up a drop down list with all the available template files for the Prosilver theme.
4. Select ‘forumlist_body.html’ and let the lower half of the page refresh to display the current contents of that file.
5. Find the following block of code in your file:

<dd class="lastpost"><span>
						<!-- IF forumrow.LAST_POST_TIME --><dfn>{L_LAST_POST}</dfn> {L_POST_BY_AUTHOR} {forumrow.LAST_POSTER_FULL}
						<a href="{forumrow.U_LAST_POST}">{LAST_POST_IMG}</a> <br />{L_POSTED_ON_DATE} {forumrow.LAST_POST_TIME}<!-- ELSE -->{L_NO_POSTS}<!-- ENDIF --></span>
					</dd>

Change it to:

<dd class="lastpost"><span>
						<!-- IF forumrow.LAST_POST_TIME --><a href="{forumrow.U_LAST_POST}">{forumrow.LAST_POST_SUBJECT}</a> <br /><dfn>{L_LAST_POST}</dfn> {L_POST_BY_AUTHOR} {forumrow.LAST_POSTER_FULL}
						<a href="{forumrow.U_LAST_POST}">{LAST_POST_IMG}</a> <br />{L_POSTED_ON_DATE} {forumrow.LAST_POST_TIME}<!-- ELSE -->{L_NO_POSTS}<!-- ENDIF --></span>
					</dd>

All we did was add this small bit of code right inside the IF statement that checks the forumrow.LAST_POST_TIME variable. All this does, is builds a link to the last post in the thread and uses the subject line of this last post to display in the forumlist.

<a href="{forumrow.U_LAST_POST}">{forumrow.LAST_POST_SUBJECT}</a> <br />

Ideally, the topic title would be displayed instead of the subject from the last post, but that information is not available without altering the query for the forumlist and I wanted to go with a simple fix here that didn’t alter any of the core code of the system. To see this small hack in action, head on over to the SBG forum.

PHP, Programming

Programmers: Experience vs Talent

Nick Halstead recently asked himself the question:

Is experience better than talent?

It’s a perfectly valid question and probably one that more hiring managers should ponder before kicking off a recruitment run. Nick’s conclusion was really less of a conclusion and more of an affirmation that it’s too hard to give a simple answer to the question. Instead of attempting to answer the question in the same manner as Nick, which is to say that I don’t really have any valid bullet points on the topic, I am going to turn to my other passion, the NBA, and see if I can draw an analogy to help answer the question.

If we apply this question to the NBA, what we are essentially asking is “Would it be better to hire someone with unproven talent and an unproven track record or a seasoned veteran?” Taking this one step further, put yourself in the shoes of the Portland Trailblazers at the moment they discovered they had the #1 selection in the most recent draft. The question become “Do we draft a potential superstar in Greg Oden or Kevin Durant or trade that draft pick for someone with experience?” Portland chose to draft Oden #1 and he is currently sitting on the bench due to a necessary surgery.

If you stop the analogy there you might arrive at the conclusion that Portland made a mistake in going with unproven talent, but you would be wrong. Oden has yet to prove his value, something Portland is hoping he does next year after being in the league for a full year. Let’s shift the question to the Seattle Supersonics who selected Durant #2. Durant is currently averaging nearly 20 points per game, which is a great way of measuring a single players output on the court. In college, he averaged nearly 26. While lower than his collegiate numbers, you could say that the decision to draft Durant is paying off and falling in line with expectations. Unfortunately, Durant is also out with a minor ankle injury. Injuries are something that don’t affect programmers and their ability to produce quality code.

Durant and Oden were both amazing college ballplayers that entered the draft after a single collegiate season. While this is a form of experience, it is on a different level and could be compared to a programmer with 1 or more years of University under their belt. It would be expected that a college student with any formal exposure to programming has a form of experience.

The analogy does start to fall apart when you factor in the difference between measuring basketball stats as a form of talent versus the intangible talent that a programmer possesses. We did see from the analogy that taking a risk on unproven talent can reap great rewards in the form of a “superstar”. Often enough though, a potential superstar goes bust and fades into the crowd. What had happened if either team had gone with experience? It’s hard to put a real value on the 2 young players in their first year, but both of these players are making close to $4M this year in salary (Source: HoopsHype). One could assume that their trade value is much higher than their per year salary in pure dollars. I’d expect that either of these players on the open “market” would fetch a superstar in return if the deal was right or if there was another player thrown into the deal.

What that leaves you with is an almost 1:1 trade value. Let’s scale back the basketball economics though and get back to the real issue of experience vs. talent. Talent has risks associated with it, especially in younger players or employees. They are unproven and could fizzle out. Take Dolphins running back Ricky Williams for example. Likewise, a young talent could explode and have massive rewards far greater than one could imagine. The experienced employee is the safe choice. You have a track record that shows you what to expect and while not the most talented person you may have more an idea of what you are signing up for versus the young talent.

In the end, I think I am arriving at the same conclusion that Nick did and that is that it depends. Not all young talent is created equal and not all experience is learned from. That said, for hiring managers faced with the choice I would said that a good mix of the two is really the best formula. The young talent can learn and grow through the experience of your veterans and allow their true potential to really shine through. Your vets may even get a second wind as the new talent comes in and raises the drive of those around him/her.

PHP, Programming, Tech, phpBB

Making Prosilver Have Profiles on the Left

phpBB 3.0 was released this week and the default theme, Prosilver, has the post content on the left and the poster’s profile on the right. This is reverse from most forum software and the subsilver theme of previous phpBB releases. I’ve taken the time to tweak the CSS and code to flip the poster profile back to the left and the post content to the right. Here are the steps you need to take:

Prosilver Left Packaged Style Download Now Available
Many users were having issues with fully completing this mod without a typo and since it involves modifying the main templates for the theme, I went ahead and packaged this style into an easy to install theme with the name of “prosilver_left”. You can download this theme as a zip file here.

Simply unzip this file and upload the “prosilver_left” folder to your styles directory in your phpBB3 directory. Install it from the Styles tab of your ACP.

Help/Support Instructions - If you need assistance after attempting this modification, please make sure you accurately describe your problem (including an image if necessary) and either include a link to the live board or PM the link to me. Please post your requests in the official thread at phpBB.com

Mod Instructions

Demo: http://www.darksideofthecarton.com/phpbb3_demo/viewtopic.php?f=2&t=1

1. Go to your ACP and click on the Styles Tab.
2. Under Style Components on the left, click Themes
3. On the right, click the Edit button next to Prosilver. This should bring up an editing window with the full CSS for the Prosilver theme inside it.
4. Find the following code inside that CSS file. It should be around like 1054. We are going to change the float value for the .postbody class from left to right.

/* Post body styles
----------------------------------------*/
.postbody {
padding: 0;
line-height: 1.48em;
color: #333333;
width: 76%;
float: left;
clear: both;
}

Change it to:

/* Post body styles
----------------------------------------*/
.postbody {
padding: 0;
line-height: 1.48em;
color: #333333;
width: 76%;
float: right;
clear: both;
}

5. Find the following code inside that CSS file. It should be around like 1532. We are going to change the float value for the .postprofile class from right to left and move the white vertical border to the right side.

/* Poster profile block
----------------------------------------*/
.postprofile {
/* Also see tweaks.css */
margin: 5px 0 10px 0;
min-height: 80px;
color: #666666;
border-left: 1px solid #FFFFFF;
width: 22%;
float: right;
display: inline;
}

Change it to:

/* Poster profile block
----------------------------------------*/
.postprofile {
/* Also see tweaks.css */
margin: 5px 0 10px 0;
min-height: 80px;
color: #666666;
border-right: 1px solid #FFFFFF;
width: 22%;
float: left;
display: inline;
}

6: Next find this:

.pm .postprofile {
border-left: 1px solid #DDDDDD;
}

Change it to:

.pm .postprofile {
border-right: 1px solid #DDDDDD;
}

7: Next find this:

.online {
background-image: url("{T_IMAGESET_LANG_PATH}/icon_user_online.gif");
}

Change it to this:

.online {
background-image: url("{T_IMAGESET_LANG_PATH}/icon_user_online.gif");
margin-top: -5px;
}
 
.online dt {
margin-top: 5px;
}

8: Click Submit to update your Theme/CSS file for Prosilver.
9: On the left, click Templates under Style Components
10: Click Edit next to Prosilver.
11: In the drop down menu, go to the very bottom and select viewtopic_body.html and it should open an editor with the current code inside it.
12: First we need to remove the current image from being applied to the Post’s div. Find the following line of code at or around line 117:

<div id="p{postrow.POST_ID}" class="post <!-- IF postrow.S_ROW_COUNT is odd -->bg1<!-- ELSE -->bg2<!-- ENDIF --><!-- IF postrow.S_UNREAD_POST --> unreadpost<!-- ENDIF --><!-- IF postrow.S_POST_REPORTED --> reported<!-- ENDIF --><!-- IF postrow.S_ONLINE --> online<!-- ENDIF -->"></div>

Remove this:

<!-- IF postrow.S_ONLINE --> online<!-- ENDIF -->

to create this:

<div id="p{postrow.POST_ID}" class="post <!-- IF postrow.S_ROW_COUNT is odd -->bg1<!-- ELSE -->bg2<!-- ENDIF --><!-- IF postrow.S_UNREAD_POST --> unreadpost<!-- ENDIF --><!-- IF postrow.S_POST_REPORTED --> reported<!-- ENDIF -->"></div>

13: Now we need to add this back to the postprofile dl element. Find the following line of code at or around line 173:

<dl id="profile{postrow.POST_ID}" class="postprofile"></dl>

Change it to this:

<dl id="profile{postrow.POST_ID}" class="postprofile<!-- IF postrow.S_ONLINE --> online<!-- ENDIF -->"></dl>

14: Click submit to save your template.
15: Select ucp_pm_viewmessage.html from the drop down.
16: Find this code at or around line 16:

<div id="post-{MESSAGE_ID}" class="post pm<!-- IF S_POST_UNAPPROVED or S_POST_REPORTED --> reported<!-- ENDIF --><!-- IF S_ONLINE --> online<!-- ENDIF -->"></div>

Remove this:

<!-- IF S_ONLINE --> online<!-- ENDIF -->

to create this:

<div id="post-{MESSAGE_ID}" class="post pm<!-- IF S_POST_UNAPPROVED or S_POST_REPORTED --> reported<!-- ENDIF -->"></div>

17: Find this code at or around line 65:

<dl id="profile{MESSAGE_ID}" class="postprofile"></dl>

Change it to:

<dl id="profile{MESSAGE_ID}" class="postprofile<!-- IF S_ONLINE --> online<!-- ENDIF -->"></dl>

This should correctly align the profiles on the left just like in subsilver in topics as well as Private Messages. Here is what you get if completed properly:

Viewing a PM:

Viewing a Post:

PHP, Programming

PHP Is a Bad Language for New Programmers

PHP is a very simple language to learn. It’s loose data types and plethora of examples on the web make the cost of entry to PHP development very low. It also empowers new programmers to venture out and get cracking on their own projects. I admire the efforts of these new programmers, but I also think that choosing PHP as their starting point is going to bite them in the butt in the long run.

Like having just enough rope to hang yourself, figuratively of course, PHP provides you just enough rope to develop some very bad programming habits. As a product of a University Computer Science program, I strongly believe that some level of professional or formal instruction is paramount to being a good programmer. There are exceptions to all rules, and especially this one, but by and large most people don’t see programming best practices as common sense so they must be taught. Here are a few tips that I think early programmers should be firmly familiar with:

1. Watch your datatypes - PHP supports loose variable declarations. What this means is that can change a variable’s datatype on the fly based on whatever you are assigning to it at the current time. While this is nice and prevents you from having to declare massive lists of variables, do not get into the habit of reusing variables for numerous purposes in your code. A variable named $count should be used for it’s initial purpose as a counter and not used later to store some other bit of data such as a name or other string.

2. Pick a variable naming standard - PHP allows you to name variables just about anything. There are a few exceptions to this that are explained over on php.net. Keep in mind that variable names in PHP are case sensitive. One mistake that you will want to avoid in variable naming is mixing and matching your variable naming scheme. By this I mean, choose a method for utilizing case or underscores when creating variable names. $my_variable_name and $myVariableName are both acceptable and easy to read variable names. The latter is an example of Camel Case and is the method that I use for naming all of my variables. Either method is fine, but pick one way and stick with it. When working on a group project, discuss this with your fellow programmers and set a standard for the project to avoid inconsistencies.

3. Don’t be afraid to ask for help - There are a huge number of development forums filled with some very experienced programmers that are more than happy to lend new coders a hand. Don’t be afraid to ask them for advice. They have a lot of wisdom to impart and they can be a great source of information. My personal favorite is the PHP forum over at DevShed.

4. Just because  you saw it online doesn’t mean it’s good code - There is a lot of bad PHP floating around and there are a lot of well intentioned people out there passing this bad code off as a good source for learning PHP. Don’t be sucked into that cycle. Try to keep your resources to some of the more mainstream PHP sites like PHP Builder, DevShed, PHP Freaks, and the W3 Schools website.

PHP, Programming, VB

I’m Beginning To Hate VB

I’m primarily a web developer and currently spend most of my time with PHP and JavaScript. My roots go back to Unix C programming and a whole host of other languages including Perl. Making the move to Win32 development hasn’t been an easy one and it’s not a place that I want to stay for very long. I’m writing a relatively simple application for some of my users in the sales department and I’ll be damned if I don’t have to go look up the syntax for every little thing. It’s beginning to get very irritating.

Take for example, a string comparison. One thing you may not realize is that VB and other compiled languages often times require strict declarations for variables. If I want a variable named iCount, I’d have to do this:

dim iCount as integer

PHP will let me just start using $iCount whenever I want and will let it’s datatype change as I see fit. While I can see this has some limitations with being able to trace values and maybe even do error checking, it makes it very easy to whip out some code. So here is that string compare in PHP:

if($myString == "this string") {
echo "Yes, we are a match";
} else {
echo "No, we aren't a match";
}

The same code in VB?

dim myString as string
if(string.compare(myString, "this string") == 0)
msgbox("We are matched")
else
msgbox("We are not matched")
end if

Hopefully there are no typos in that VB as I’m not so good with it that I can type it freehand like I can PHP. Before you jump all over my case with the differences between a web scripting language like PHP and a compiled desktop app (yes I know there are vb scripts, but let’s pretend there aren’t) language like VB.NET keep in mind that regardless of what a language allows you to do, it should be get out of your way and let you work. I don’t feel like VB.NET really promotes a good programming experience. It sure as hell won’t let you screw something up or mis-convert some data, but you won’t have fun writing it.

PHP, Programming, Tech

SugarCRM - Installation Issues

I recently setup the free/open source version of SugarCRM to manage some of my business contacts in the gaming industry. I went with a web based solution so I could open up portions of the data to my staff. After selecting SugarCRM as my tool of choice, I setup space for it and got to the installation portion. On step 2 of the installation, I got a nasty message

Writable Session Save Path (/tmp) Not A Valid Directory

I started doing a little research and realized it was the php configuration value for the session.save_path variable. My webshost runs phpsuexec, which is related to security and resource usage and is really out of the scope of this conversation. What that means is that you have to override php.ini configuration values through a php.ini file and not through .htaccess. Using the htaccess method results in a wonderful 500 error message.

Resolving the issue, which I figured would take 5 minutes, was actually a rather frustrating process. In theory, I should have been able to slap a php.ini file in this new directory with the following line:

session.save_path = /tmp

That didn’t seem to work. After trying variations of paths for the save_path I realized that the wonderful installer was also telling me that the version of PHP I was running, 5.x, was an untested version. I run PHP 5 for it’s OO/class support and it was apparently cascading down to this new directory. I went ahead and changed my .htaccess file to use PHP 4.x instead. To do this, add the following line to your .htaccess file:

AddHandler application/x-httpd-php .php

instead of the following, which utilizes PHP 5 on my webspace:

AddHandler application/x-httpd-php5 .php

Once I did that, I started to make a little progress, but it wasn’t until I happened upon a posting about the session.save_path value that I got things working. Apparently using absolute paths isn’t necessary and simply changing the value in your php.ini to the following resolved my problems and allowed me to get past the second step of my installation:

session.save_path = tmp

So there you have it, if your install is throwing the above error, just update your files to include these values and get to using SugarCRM.

.htaccess
AddHandler application/x-httpd-php .php
php.ini
session.save_path = tmp

PHP, Programming

A Pair of Handy Functions - IP to Hex Conversion

If you’ve done any custom IP work in the phpBB world, then you know that phpBB stores IP addresses in a hex format. This is simple, but you’ll definitely need to convert back to plain IP format (in octets) if you want to display that information. I am sure phpBB probably has some functions for converting between the 2, but it was easier and faster to just write my own. Here they are for your convenient use:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function ip_to_hex($ip)
// Purpose: Takes a user's IP and converts it to the hex value needed by phpBB
// Arguements: $ip - The remote user's IP address
// Returns: $hex - The user's IP in hex
{
$pieces = explode(".", $ip);
$hex0 = dechex($pieces[0]);
$hex1 = dechex($pieces[1]);
$hex2 = dechex($pieces[2]);
$hex3 = dechex($pieces[3]);
if(strlen($hex0) &lt; 2) $hex0 = "0".$hex0;
if(strlen($hex1) &lt; 2) $hex1 = "0".$hex1;
if(strlen($hex2) &lt; 2) $hex2 = "0".$hex2;
if(strlen($hex3) &lt; 2) $hex3 = "0".$hex3;
$hex = $hex0 . $hex1 . $hex2 . $hex3;
return $hex;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function hex_to_ip($hex)
// Purpose: Takes an IP in hex form and converts it to the plain IP form
// Arguements: $hex - The user's IP in hex
// Returns: $ip - The remote user's IP address
{
$pieces[0] = substr($hex, 0, 2);
$pieces[1] = substr($hex, 2, 2);
$pieces[2] = substr($hex, 4, 2);
$pieces[3] = substr($hex, 6, 2);
$ip0 = hexdec($pieces[0]);
$ip1 = hexdec($pieces[1]);
$ip2 = hexdec($pieces[2]);
$ip3 = hexdec($pieces[3]);
$ip = $ip0.".".$ip1.".".$ip2.".".$ip3;
return $ip;
}

Database, PHP, Programming

Is Database Abstraction Really Worth The Hassle?

I’ve been building web apps for a long time now, I guess about 5 years. In the early days, database abstraction didn’t “exist”. Everyone just used the functions provided in the standard PHP library such as mysql_connect, mysql_query, etc. At some point, we as developers decided this wasn’t good enough. We needed an easier and a cleaner way to execute db calls.

Along comes PEAR and brings with it the DB package. DB promised to be an abstraction layer that provided an object oriented style API for querying most of the popular RDBMS. DB is now replaced by the MDB2 package, but the premise remains the same. I’ve never gotten on the PEAR bandwagon so I’ve always gone with writing my own implementations of some sort of simple abstraction layer.

I’ve gone through writing a set of simple functions all the way to implementing a full PHP5 MySQL class that I use for various web apps and I continue to change and modify the heck out of my abstraction layer. Where I am at now is a place of complete frustration. With all the time I’ve sunk into working and reworking my own abstraction layer, couldn’t I have just saved myself the trouble and hard-coded MySQL functions into my applications?

I mean, I can partially understand an Abs Layer for open source apps like SMF or phpBB since they aren’t in control of the RDBMS that the end user will be running, but for custom designed applications does it even make sense to go the route of abstraction?

At this point, I am beginning to think the answer to that is no. I’m curious to know what other developers think about this topic. I may ping some of the greater PHP minds out there and get some feedback.