Rio Bautista’s TechLog

Tech solutions worth remembering

My Move to the OSX (the dark-side)

I recently bought my 6th laptop and this time I thought I’d make this big jump to the other-side (OSX).  I wanted to find out what makes a Mac so loved by the “simple-ones”.  Well it looks like this whole “simplicity” is what gives the Mac its advantage.  It’s the same secret I suppose that puts facebook ahead of the other social networking sites.  It captures even the simplest of the simple users there is.  It takes away all the non-essentials and keeps you focused on what you need.  Putting all its power into delivering your very basic needs. I’ll keep that in mind the next time I create something. Focused Power… I like that.

Running Drupal on XAMPP/OSX

I was able to setup my XAMPP/OSX and got it working.  Following the directions on how to install XAMPP on OSX it tells you to put your site on the htdocs folder.  But I decided to setup my Drupal site on another directory outside of the XAMPP folder.  My experience tells me to simply look for the httpd.conf file and insert a virtual-host that points to my site.  So I did and I also included a vhost for my local xampp stuff.  The XAMPP site is now working but not my drupal site.  Here’s what I did with my conf:

<Directory “/Users/riobautista/Documents/workspace/skoolflyer”>
Options Indexes FollowSymLinks ExecCGI Includes
AllowOverride All
Order allow,deny
Allow from all
</Directory>

<VirtualHost *:80>
ServerAdmin rio.bautista@ragingriverict.com
DocumentRoot “/Applications/XAMPP/xamppfiles/htdocs”
ServerName localhost
</VirtualHost>

<VirtualHost *:80>
ServerAdmin rio.bautista@ragingriverict.com
DocumentRoot “/Users/riobautista/Documents/workspace/skoolflyer”
ServerName skoolflyer.local
</VirtualHost>

I restarted my Apache after modifying the conf but I’m getting an Error 403 (Permission Denied) when I tried to access the site.  I’m thinking maybe I should setup the permissions so I went into the terminal and did a chmod 755 on the skoolflyer folder but still didn’t solve the problem.

I just moved my site to the /Users/riobautista/Sites/ this fixed the problem.

This time, I got a bunch of deprecated function ereg().  Turns out XAMPP 1.7.2a is using PHP 5.30 which no longer support ereg().  Good thing I found this simple patch that fixed the problem.  The problem is in line 902 of the includes/file.inc:

902:   elseif ($depth >= $min_depth && ereg($mask, $file)) {

just replace the ereg function with mb_ereg so it becomes:

902:   elseif ($depth >= $min_depth && mb_ereg($mask, $file)) {

That worked perfectly for me so on to my work.

Hibernate on Mac

I’m now trying to find out how to put the MacBook on hibernate.  It’s a very useful feature found on Windows.  As I move around very often, this helps me completely turn off my laptop while preserving everything so that when I turn the power back on I’m back exactly at where I left off.

MacBook seems to have something else they call Safe Sleep.  It’s supposed to do exactly what hibernate does, but it is not readily accessible to the user.  That gives me the feeling that Apple is not recommending this for regular use.  I’ll try to read more on this.

I found these 2 terminal commands that lets you enable and disable SafeSleep.

To enable SafeSleep, use the Terminal program and enter the following command:

sudo pmset -a hibernatemode 3

To disable SafeSleep:

sudo pmset -a hibernatemode 1

I tried enabling SafeSleep and will now observe how well this works.  For almost a day now, SafeSleep seems to work well.  When I put my MacBook to sleep it hibernates just like windows.  It does take a bit longer to sleep and wake than the usual sleep mode but I’m fine with that.

My First Crush (Crash)

I downloaded drivers and other software for my Line6 PODxt.   Of course I got the latest ones from Line6.  Installed Line6 Monkey without a problem.  But after finishing the GearBox installation I got this message about the Line6Audio.kext:

Read more about this error here: http://line6.com/community/docs/DOC-1583

Well Line6 says this is a known issue and should not be a problem.  Anyway, after completing the installation I was asked to restart but when I did I noticed it was taking much longer than usual to boot up.  After a while I received this message telling me to shutdown by pressing the power button until the MacBook shuts down and turn it back on again.  I did but I kept getting the same message.  So got my other laptop and looked around for what I can possibly do.   I tried the single-user and verbose approach (which you do be pressing command+s or command+v during start up).  I was given the bsd terminal where I started exploring ways to uninstall the Line6 driver using the terminal.  It’s not very Apple-like, so I decided to look for an alternative.  Apparently the Apple-way is to login in SafeMode and uninstall my drivers from the Finder.  It’s too simple really.  You simply press the command+shift buttons while booting and you’ll get into SafeMode (pretty much like Windows).  From there I was able to locate the Line6 Driver Uninstaller and that fixed my problem.


October 31, 2009 Posted by | Uncategorized | Leave a comment

Skype & Apache conflict

I was setting up XAMPP on my Windows XP and for some reason Apache wouldn’t start using the XAMPP Control Panel.

So I tried starting Apache using the xampp/apache_start.bat.  That’s were I saw the problem.  Apache server is giving me this error:

“(OS 10048)Only one usage of each socket address (protocol/network address/port)
is normally permitted.  : make_sock: could not bind to address 0.0.0.0:80
no listening sockets available, shutting down
Unable to open logs”

I tried running netstat -b, to check what program is currently using port 80, but I couldn’t find it exactly.  Anyway, it only indicated Yahoo and Skype having outside connections.

So I decided to isolate the problem by turning off the programs one at a time.  On first try I closed Skype and tried running apache again and it worked.

This only shows that skype is using port 80 for some reason to connect to the outside world knowing most likely that this port is open for most firewalls.

So to confirm the theory, I started skype again and tried to start apache and as expected, it failed to start.

So there, there must be some way for them to work together, but as for me, I’d rather close skype until I need to use it, I don’t like some program acting like a webserver and doesn’t ask for my permission to do so.

October 23, 2009 Posted by | Apache, Skype, xampp | , , , | 1 Comment

Simple yet effective way to replicate/mirror a database (any database at that)

Here’s what I did:

  • First, I centralized all my data calls into a single object or function which receives an sql statement as the parameter.
  • I check if the SQL statement is performing an INSERT, UPDATE or DELETE. To do this I just check if the first word is “SELECT” ( you may have to improve that if you’re using stored Procedures ).
  • If the statement modifies data on the database then I put the complete sql statement in an “sql_transfer.sql” file which is a simple text that is accessible through the web.  This server now acts as the publisher.
  • I also have an option to perform real-time replication if the other server is within the same network. If this option is on, then I immediately execute the same sql statement on the mirror server.
  • Normally, I’d have a cron running every so number of minutes that pulls the sql_transfer.sql from my publisher server.  If the download was successful then the file is deleted from the server using a script on the publisher server. The downloaded sql_transfer.sql is then executed on statement a time deleting each one that was executed. A flag is raised telling the process not to download while the sql_transfer.sql is not yet empty.

Advantages:

  • Works even across domains and ISPs.
  • Doesn’t require complex replication tools.
  • Easy to recover in case of network problems. can be manually executed.
  • Works on practically any database platform.

April 17, 2009 Posted by | Uncategorized | , , , | 1 Comment

MySQL .frm recovery

I had problems with my hard driver that caused my MySQL tables to be inaccessible.  I was getting “#1033 – Incorrect information in file” error.  I checked the files if there where obvious anomalies on the file caused by cross-linked file pointers but the files looked ok.

I had just reinstalled my MySQL due to the corruption and didn’t realize that this database was using InnoDB.  I thought it was enough to backup and restore the database directory as that would be enough for MyISAM databases.  Later I tried copying the InnoDB files from the data directory of the old installation.  It didn’t work at first until I re-started my computer.  The database was recovered after that (structure and data).

April 17, 2009 Posted by | Uncategorized | Leave a comment

cakePHP calls crashing Apache

While doing some experimenting on cakePHP, I kept getting win32 exception errors from apache.exe causing it to stop.  It doesn’t happen on my other PHP programs.  So I thought maybe it had something to do with how cakePHP is using PHP and apache.

To figure out what was happening, I allowed Visual Studio to debug the apache.exe at the point of the error.  While in debug mode, I checked the call-stack to see any information that might give me a clue and there it was.  The last call was ZendOptimization().

Now since I’m not really using Zend extensions on my programs.  I thought maybe I should try disabling this just to isolate the problem.  To do that commented out the Zend options in my php.ini.  After a few tries, it still wasn’t working, then realized there were 2 copies of the php.ini one was under the xampp\php and another was under xampp\apache\bin.  Well you guessed it, I should have been editing the one under xampp\apache\bin for this to take effect.  And of course don’t forget to restart your apache.

February 1, 2008 Posted by | CakePHP, php, xampp | Leave a comment

Reformatting the default CakePHP layout (default.ctp)

So after working on my authentication and application security I thought it’s time to mess with the looks of my site.  To start off, I wanted to obviously change the title of my site and do some minor changes on the main layout.

When you want to change something a programmers first instinct is to look for the text you want to change by maybe doing a grep or a file-search.   And I did, however, the title appeared in a file called default.ctp that was outside my app folder.  And we wouldn’t want to mess with anything outside the app folder specially if it’s inside the cake folder.

After a few more reading, it turns out we need to create our own default.ctp inside our app folder e.g. app/views/layouts/default.ctp.  So what I did was to copy the default.ctp from the cake/libs/views/layout and customized it to my preference.  I realized, the application title, is kinda redundant (as how it was implemented) and I was hoping there’d be some configuration item that I could simply change to globally specify my application title but no.  I’d probably have to do that myself as part of the configuration class.

I’m currently looking for a summary of all the available variables I could put in the default.ctp (of course with some description and explanation as to how to effectively use them)  like I see there’s $title_for_layout, $scripts_for_layout, $content_for_layout,  $cakeDebug.  If I don’t find one, then I’d probably write one if I later find to be important.

January 15, 2008 Posted by | CakePHP, php | 14 Comments

Syntax highlighting CakePHP using Eclipse

I’m still waiting for an Eclipse plugin for CakePHP so for now I’m settling with the available PHP syntax highlighting on eclipse. So far I had to define two file-types e.g. *.thtml (for 1.1.x.x) and *.ctp (for 1.2.x.x). Do the following steps for each file-type you want to be associated with the PHP-Editor:

  1. Open your Eclipse workspace;
  2. Click on Window | Preferences;
  3. On the Preference Dialog expand the General node on the tree;
  4. Click on Content-Types under General;
  5. On the right panel under Content-Types, expand the Text node;
  6. Select PHP Source under the Text node;
  7. You’ll see the list of files associated on the list below e.g. File-Associations;
  8. Click on the Add button and type the fie extension e.g. *.ctp under file-type;
  9. Click on Ok to add the file-type;
  10. Click on Ok to close the Preference Window;

If there’s a currently opened file (with the extension you added), it will not reflect syntax highlighting until you close and reopen the file. You’re all set!

January 15, 2008 Posted by | CakePHP, php | 11 Comments

Intersecting Entities on CakePHP Models

I’m creating a User Model as shown on the data model to the right. In this model, I’d like to be able to assign several Roles to each User. To do this, I’d need to add an “intersecting” entity (UserRole). However, I couldn’t figure out how to implement an intersecting entity in CakePHP. But after further reading, it turns out that there is a particular type of association in CakePHP specifically for handling intersections and that is the hasAndBelongsToMany (HABTM) association.

It turns out the “UserRole” does not become a class of it’s own, but instead will only be used as a joining table. In effect, we’re still associating User with Role using UserRole as the joining table.  Though as Abba pointed out, it’s possible to make UserRole as another class using the “with” parameter.

So to do this, following the CakePHP convention, I created the following tables:


— Table structure for table `roles`

CREATE TABLE `roles` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(50) NOT NULL,
`description` varchar(255) NOT NULL,
`initControllerAction` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;


— Dumping data for table `roles`

INSERT INTO `roles` VALUES (1, ‘Admin’, ‘System Administrator’, ‘test’);
INSERT INTO `roles` VALUES (2, ‘User’, ‘Regular User’, ”);
INSERT INTO `roles` VALUES (3, ‘Visitor’, ‘Visitor’, ‘test’);
INSERT INTO `roles` VALUES (4, ‘Operator’, ‘System Operator’, ‘test’);

— ——————————————————–


— Table structure for table `users`

CREATE TABLE `users` (
`id` int(11) NOT NULL auto_increment,
`username` varchar(50) NOT NULL,
`first_name` varchar(50) NOT NULL,
`last_name` varchar(50) NOT NULL,
`password` varchar(32) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;


— Dumping data for table `users`

INSERT INTO `users` VALUES (1, ‘admin’, ‘Test’, ‘Administrator’, ‘cdd80313584fc62e1da825e72be7d27d’);

— ——————————————————–


— Table structure for table `users_roles`

CREATE TABLE `users_roles` (
`user_id` int(11) NOT NULL,
`role_id` int(11) NOT NULL,
PRIMARY KEY (`user_id`,`role_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;


— Dumping data for table `users_roles`

INSERT INTO `users_roles` VALUES (1, 1);
INSERT INTO `users_roles` VALUES (1, 2);

After creating and populating the tables, we’ll simply need to update the User model (app/models/user.php) to add the following lines:

class User extends AppModel {
var $name = 'User';
// this is how to implement an intersecting entity in CakePHP
var $hasAndBelongsToMany = array(
'Role' => array (
'className' => 'Role',
'joinTable' => 'users_roles',
'foreignKey' => 'user_id',
'associationForeignKey'=> 'role_id',
'conditions' => '',
'order' => '',
'limit' => '',
'unique' => true,
'finderQuery' => '',
'deleteQuery' => ''
)
);
}

I tried doing a simple pr($user) from my authentication program and it worked like magic🙂.

January 14, 2008 Posted by | CakePHP, php | , | 3 Comments

Integrating Flickr into my PHP web-app

I’m now to grab some photos from Flickr. Photos tagged with a given string and link to those images from my site.

I tried checking the Flickr API documentation but all it shows there are the available methods and their documentation. But with some poking around I finally found this page (http://www.flickr.com/services/api/auth.howto.web.html) which gives me the “how to’s” to get me started.

We’ll now need to download an API from the list. I initially picked-out the PHP5 API but after going through the doc, it turns out we need to make sure the CURL and SimpleXML extensions should be enabled. I’m not sure if that’s available on the target server so I’m going for the phpFlickr library for PHP4. It comes with some PEAR files but doesn’t really require you to have PEAR running on your server.

Below is a simple program to search for pictures having a particular tag (as I’m supposed to do with my site) :


<?
require_once(“phpFlickr.php”);
$f = new phpFlickr(“yourAPIkey”);
$result = $f->photos_search(array(‘tags’=>’rio’));
$photo = $result[‘photo’][0];
$info = $f->photos_getInfo($photo[‘id’]);
print_r($info);
?>

What I’ll try to do now is to display the picture on my site. This is not on the standard documentation and from searching the internet I came across this solution. You should be able to access the picture from the Flickr site using a URL like this:


http://farm{farm_id}.static.flickr.com/{server-id}/{id}_{secret}.jpg
or

http://farm{farm_id}.static.flickr.com/{server-id}/{id}_{secret}_[mstb].jpg

You’ll be able to get the farm_id, server-id, id and secret by executing the photos_getInfo which returns an array containing all info about about given picture_id. Using the code above we can add this line to display an image from Flickr:

echo “<img src=’http://farm{$info[‘farm’]}.static.flickr.com/{$info[‘server’]}/{$photo[‘id’]}_{$info[‘secret’]}_m.jpg’>”;

You should now be able to display a picture from Flickr. Have fun…

January 10, 2008 Posted by | Flickr API, php | Leave a comment

Installing CakePHP

I’m now setting up CakePHP for my PharmaSIS website. I haven’t tried this before so I’m logging my every move:

  1. Using Eclipse I checked out a copy of CakePHP from https://svn.cakephp.org/repo/trunk/cake/ and selected 1.2.x.x
  2. I’m now trying to get the initial page running:
    1. Added a virtual host to my Apache httpd.conf file:

      <virtualhost 80="">
      ServerName demo.PharmaSIS
      DocumentRoot "c:/projects/cake/CakePHP12/app/webroot"
      DirectoryIndex index.php
      <directory "c:/projects/cake/CakePHP12/app/webroot">
      AllowOverride All
      Allow from All
      </directory>
      </virtualhost>

    2. Added an entry in my /etc/hosts:
      • 127.0.0.1 demo.PharmaSIS
  3. Restarted Apache
  4. Accessed http://demo.PharmaSIS and got to the default CakePHP page
    1. There’s this notice which I’ll try to figure out that says:
      “Notice (1024): Please change the value of ‘Security.salt’ in app/config/core.php to a salt value specific to your application [CORE\cake\libs\debugger.php, line 535]”
    2. Changed my security salt to some random string by edting app/config/core.php
  5. That removed the Security.salt notice but it’s now telling me I don’t have a database configuration file
    1. Renamed the database.php.default to database.php and edited the settings to:

      class DATABASE_CONFIG {
      var $default = array(
      'driver' => 'mssql',
      'persistent' => false,
      'host' => 'mssql12.mssqlserver',
      'port' => '',
      'login' => 'myusername',
      'password' => 'mypassword',
      'database' => 'thedatabasename',
      'schema' => '',
      'prefix' => '',
      'encoding' => ''
      );
      }

    2. I’m not able connect to the database.
    3. Apparently, there’s nothing like a default port so I had to specify the port (using the MSSQL default 1433)
  6. The page is showing all green and seem to be correct.

That ends the installation portion of this session. Next thing I’ll do is probably to experiment on authentication.

January 8, 2008 Posted by | CakePHP, php | Leave a comment