Matt clued me into an interesting piece of software called KavaTunes last night. He found it on the Mac Heist website as one of their free software titles when you solve puzzles in one of their yearly heists. I was not interested at first, however after watching him for a bit, I quickly wanted a copy as well. What KavaTunes is, is a piece of software that builds a php page of your iTunes library, but to take it a step further, it even looks like your iTunes library! This may be the simplest way to get access to your music on a local network, and on the campus network it is a perfect solution.
Oh course my g4 server is my test bed for playing with things and has a custom apache and php setups, so unlike Matt I had quite a bit of extra trouble with the setup. (With Matt laughing at the issues I was having the whole time ^_^) First off was that KavaTunes always builds in ~/Library/Webserver/Documents/ however I have a custom webserv folder instead, so it took a while to figure out why it was not building in the right place, but a symbolic link fixed that. Then do not call your iTunes music folder iTunes as KavaTunes adds a another separate folder under this name with its required php files. And lastly do not mess up your symbolic links, as it just causes a general mess. After about 30 minutes of bug fixing, which would have been nice if it gave you some warning about these things, I was up and running. It even streams my movies and tv shows I have on iTunes.
If you are interested grab a copy from Mac Heist
February 10, 2009
KavaTunes
April 5, 2008
PHP RC4 Encryption
One of the harder encryption problems comes when you need to encrypt something but need to also be able to decrypt it as well. I ran into such a problem recently in PHP. The regular simple methods such as sha1 or MD5 with a simple salt would not work because they are only one way, and to make matters worse the crypto modules I needed for a solution where not installed on the server.
Luckily I stumbled upon RC4Cript a simple php class that supports a simple RC4 encryption. Although RC4 is not exactly the safest encription Wikipedia, it was fine for simply securing the data I had, and I did not feel up to the task of coming up with a better solution. It took a while to get it to work because I was confused into thinking that I needed bin2hex() after encrypting it, however that is simply to give a more readable form, leave it as is when using the encrypted text in the decrypt function. Other than that, the source has an example that is pretty self explanatory.
March 9, 2008
PHP Thmbnail Function
I had whipped up a online photo album for the AMS website I am working on, however after photos where uploaded I realized that I was not using thumbnails, but instead the huge pictures, which on dial up would take years to load a large album. So I went looking for a good thumbnail function, sadly all the ones I found where way to complex for what I was trying to do. With no other options, I did some research and studied how the other functions worked an came up with my own simple version.
function makeThumb($im,$dest,$thumbwidth,$thumbheight)
{
$resizeResult = TRUE;
$imgResult = TRUE;
list($width, $height) = getimagesize($im);
$exttype = exif_imagetype($im);
$tim = ImageCreateTrueColor($thumbwidth,$thumbheight);
if($exttype == IMAGETYPE_GIF)
{
$image = imagecreatefromgif($im);
imagealphablending($tim, FALSE);
imagesavealpha($tim, TRUE);
$resizeResult = imagecopyresampled($tim, $image, 0, 0, 0, 0, $thumbwidth, $thumbheight, $width, $height);
//header('Content-type: image/gif');
$imgResult = imagegif($tim,$dest);
}
else if($exttype == IMAGETYPE_JPEG)
{
$image = imagecreatefromjpeg($im);
$resizeResult = imagecopyresampled($tim, $image, 0, 0, 0, 0, $thumbwidth, $thumbheight, $width, $height);
//header('Content-type: image/jpeg');
$imgResult = imagejpeg($tim,$dest);
}
else if($exttype == IMAGETYPE_PNG)
{
$image = imagecreatefrompng($im);
imagealphablending($tim, FALSE);
imagesavealpha($tim, TRUE);
$resizeResult = imagecopyresampled($tim, $image, 0, 0, 0, 0, $thumbwidth, $thumbheight, $width, $height);
//header('Content-type: image/png');
$imgResult = imagepng($tim,$dest);
}
if($resizeResult != TRUE || $imgResult != TRUE){ return FALSE; } else return TRUE;
}
To use just call makeThumb("Location of Image", "Place to put thumbnail", width, height); Very simple, also it will return false if something fails inside. Remember to make sure the directory it is writing to has the permission to write set correctly or it will always fail.
February 12, 2008
Simple Flat File Database
I had recently been refused access to a sql database for a project, this I started looking for other options. After a little searching I stumbled across txtSQL, which is a very powerful but easy to use flat file database.
To start off download the latest release (2.2 final at the time of writing this). Then read the read me in the doc folder it will walk you through setting up the include files you will need as well as the database folder, by default called data. One you have these placed correctly just like with mysql you will need to connect to the database.
include('includes/txtSQL.class.php');
$sql = new txtSQL('includes/data'); // Location of the database files
$sql->connect('root', ''); // This is the default name and password
$sql->selectdb('XXX'); // XXX is the database name
This will let you connect to the database you want to use, I do recommend changing the password from its default. However there is no database or tables right now so you should add these.
// txtSQL created a new database named testDatabase
$sql->createdb(array('db' => 'XXX')) or die('Error creating txtSQL DB, txtSQL said: '.$sql->get_last_error());
// Create Table in new database
$columns = array(
'id' => array('type' => 'int', 'auto_increment' => 1, 'permanent' => 1
'name' => array('type' => 'string', 'max' => 50),
'email' => array('type' => 'string', 'max' => 50) );
$sql->createtable(
'db' => 'testDb',
'table' => 'testTable',
'columns' => $columns);
Now you have tables set up, unlike sql databases, this is all commands with arrays, so it takes a little getting used to, but is not to horrible to learn. Finally, you just need to manipulate the data.
// Select
$where = 'somthing = '.$_POST['something'];
$results = $sql->execute('select', array('table' => 'XXX', 'where' => array('0' => $where), 'limit' => array(1)));
// Grab first result
$result = $results[0]; // Just want the first result
//or use them all
foreach($results as $result) // Grab each row from the result
{
//... Do something
}
// Insert
$insertvalues = array( 'name' => 'matt', 'date' => '9/8/06' );
$sql->execute('insert', array('table' => 'XXX', 'values' => $insertvalues));
The rest delete and update are also similar, and you can find example in the documentation included.
Although txtSQL is not a perfect replacement for a real sql database it does work rather well. However it is suggested that is only be used with smaller database needs, otherwise stick to a real real database management system. Still for what I needed it to do I am quite happy.
January 11, 2008
Making a code editor in a webpage
One of the big goals that I had over the winter break was to finish the upgrade of the Plymouth AMS website. Is started writing in in xml with a xsl style sheet. Although this is good for simper websites that I am going to be around to keep it up, because this is not the case I had to think of something else. After talking with a more experienced web programer in a workshop, I decided to re-write it in php as a web app. Thus after logging in you could edit the basic html on the site, yet this would leave the more advanced layout out of it. Perfect for what the AMS group will be doing. Ether way it is a huge advance from the front page created website they had before.
All that it will requite is a simple html/text editor written in php. This turned out to be a little more complex then I thought it would be. I started with a simple text area that filled the width of the page. Looks good. But after trying to edit 2 lines I got really annoyed. NO TABS, there was no way to tab, and a programer like me lives for code structure. So now I needed tabs... By default web pages use the tab key to go from one field to another not to add a tab character, luckily after some searching I found this very handy java script that would add tabs to text areas. It works very well, and now I have my beloved tabs again.
So first problem fixed, now another rears its head. This text area allows html code, which tends to have quotes here and there, the Mysql calls to the database I am storing the text in also has both double and single quotes. This does not mix well. Basically once it concatenates the strings together the quoted sections get completely screwed up by the new quotes added to my query. This must be a common occurrence so off I went searching for php functions that would take care of it.
As expected there was a function called htmlentities, basically it converts all applicable characters to HTML entities, aka the escaped form. This takes care of the quote problem, I just save this to the database and edit it when needed, as a bonus the text area field accepts these characters and shows them as they should be with out any problems. Thus there is no need to convert them back when editing the text, only when displaying them in html. See example below...
$query_string="UPDATE `***` SET `***` = '".htmlentities(stripslashes($_POST['***']),ENT_QUOTES)."' WHERE `***` = '***' LIMIT 1";
When displaying the html code from the database it is a little more complicated. First off I wanted this to be a hybrid text/html editor, this when you line return, it puts a <br /> just like a text editor would. To do this use the handy nl2br function, it converts the /n new line characters to <br />. Which looks really good, except that it does every line return, I only want non code style returns so then follow this by str_replace which replaces every "><br />" with just a ">" Thus removing all of the non needed breaks.
The last part is html_entity_decode which convert all HTML entities to their applicable characters. Thus the html is back to normal. You put this all together and it does the job very well.
$message = mysql_fetch_array($result, MYSQL_ASSOC);
$mconverted = nl2br( html_entity_decode($message['text']) ); // Convert back to html code
$removes = str_replace("><br />",">",$mconverted ); // Remove extra br due to code style
$xtpl->assign('message', $removes);
January 8, 2008
Remember to update xTemplate
I was having the hardest time yesterday getting xtemplate to work on the G4 web server. Every time I would open the php page a white screen would load. No errors, no problems, would not echo anything to the page!
I checked online and no one else seems to have this problem. So I though that maybe the Mac version PHP had something to do with it, but no luck there as well. Regular PHP would work fine, yet even pages I have done in the past and worked would not load. Finally I downloaded a fresh version of xtemplate and checked to see if the tutorial files worked. Low and behold they worked! Apparently a new version of xtemplate has come out and it works with the newer version of php I am running.
So a note for any one having problems with xtemplate running on a mac you might want to get the latest version!
[UPDATE: I had never turned on error reporting in my PHP install so that is why it never gave me any errors.]
December 27, 2007
PHP & Mysql on Mac OSX
Now that have a running G4 tower that I can use as a server, all that is left is to set up the required items on it. My main goal is PHP and MYSQL so that I can start development on the next update to the PSU AMS groups website. From what I had learned from previous setups is that PHP is already installed in Mac OS 10.4, however it is not completely active. Luckily the process was very painless and straight forward. It also helped that I was not doing anything more then a simple setup.
I made the PHP setup a lot harder than it had to be and tried to build from the latest source. Sadly I did not have all of the required dependancy files for it and that quickly became a waist. Still it was a good idea to read the Apple developer page on PHP first here. Luckily someone had already made an installer for PHP, and I just had to find it at Entropy. It installed without any problems, just make sure to read the directions.
Mysql was next, just like PHP there was a handy installer for it. Apple's developer site has a great page on installing Mysql on Mac OS X here, that I started with. It boiled down to just get Mac installer from the Mysql site and install it. It also included a control panel for Mysql that I installed, it lets you set Mysql to start up on every time the computer is turned on.
One thing that was missing was the phpMyAdmin page for Mysql. I thought that it would be included, however after trying to get to lazurus.local~efeion/mysql, which is how I get to it from school, it quickly become apparent that was not the case. It is actually a separate project here. To make it similar to what I am use to using, after extracting the directory, I named it mysql and placed it in the sites folder in my directory. This is where apache is set to host websites by default, and is fine for what I am doing. Now if I went to lazurus.local/~efeion/mysql and the phpMyAdmin page would show up, but now complained about not being configured. Easiest way to fix that was to go to the mysql/scrips/setup.php page. This handy script will generate a config file for phpMyAdmin, just grab what it outputs and save it in a file called config.inc.php located in the main mysql folder. There is a sample version in that folder for the more advanced users to set up manually. However I am still new to this and did not want to dig that deep yet.
Now I had a PHP web server with a Mysql back end all set up, which was a lot easier than I expected.