The release of Tipsy (MVW PHP)

Over the last decade I have modified and released several different PHP frameworks, with each version changing as my style changes. This new framework, created almost from scratch for some of my newer, smaller sites, focuses on speed of development, with the idea of making REST based web apps super easy to build.

This new version has been named Tipsy, because, obviously, I made it…and everything else…while drinking.

Tipsy is an MVW (Model View Whatever) framework, following the examples of AngularJS.

Heres a quick example of how easy, yet how powerful and lightweight, Tipsy is:

index.php

require_once 'Tipsy.php';
use Tipsy\Tipsy;

$tipsy = new Tipsy;

$tipsy->router()
    ->when('hello', function($Scope, $View) {
        $Scope->user = 'Devin';
        $View->display('hello');
    })
    ->otherwise(function() {
        echo '404';
    });

$tipsy->start();

hello.phtml

<h1>Welcome, <?=$user?>!</h1>

Take a look at the source, examples, and documentation, over at Tipsy’s Github Repo.

Learn to build an application using Angular.js

How awesome would it be if there was a short online class on how to build an application using Angular? O wait there already is. Check it out.

http://campus.codeschool.com/courses/shaping-up-with-angular-js/intro

I've moved to tumblr!

After nearly 18 years of building and managing my own website using nothing but a text editor and scratch code, I’ve decided to build on top of an existing platform for once.

Tumblrs custom themes are ridiculously simple to modify so that each page can display each type of data nice and pretty.

Maybe now I will write posts more often than once every year…

I fucking love AngularJS. I fucking love Cana.

About a year ago I started a personal project called droplet that is basically a shared photo library social network (for people who have friends). It was my test to learn AngularJS. I fucking hated it. The documentation was absolutely awful, wrong at least half of the time. Or the docs reflect some crazy version that never even made it to main repo. It was a complete fucking mess, and that is what I expected.

The fact that it was a fuckshow actually made it MORE fun to learn. So I built this silly little app in 2 days, and decided I had enough knowledge to switch our entire company, Crunchbutton, away from my scratch built JS framework to Angular. I was wrong. But not in a bad way.

Over the course of the next couple days, i converted what was basically jQuery and a backbone (for templates ONLY) MVC framework and turned it into Angular…for the most part. I got seriously tired of all the broken shit, or people claiming this piece of code works, or whatever, and it just — not — working. So I had my developer partner, Daniel, do all the hard stuff! And hard stuff he did. He converted all of my half finished code into pretty angular directives and fixed all the junk I broke. He was my savior.

A couple weeks go by, and we are ready for live. It was awesome, but in reality at the time it was nothing more than a framework change, and a messy one at that. It took us both a while to finish cleaning things up and angularify everything. Soon after, we pushed our app to the iOS store. Yay! But thats not what this post is about.

The app has been in the store for just over a month, and since then I have COMPLETELY redone the UI and design. I spent countless drunk hours designing all the new stuff without touching any code, trying to make sure every pixel was perfect. Well…its fucking perfect.

So let me get to the point:

Without changing more than a dozen lines of code (adding directives), I have built an entire new ui ON TOP of our existing theme, that functions different due to how easy it is to bind functionality to Angular within HTML and HTML that changes.

That about sums it up. So let me explain quickly how and why things are so easy.

We use Cana. Cana is my PHP framework. Hand crafted. Aged for 12 years. This is my sipping whiskey.

Cana has the ability to configure sites, or apps, with a config array to change the include paths in a cascading manner that allows us to create sub templates for ONLY files that have changed compare to the parent. Heres what I mean:

  • active themes configuration is [“sexy”,”old”]
  • old theme has 15 files, including home.phtml
  • sexy theme only has layout.phtml and home.phtml
  • when the request is made to the server, it cascades downwards automatically to include all of the templates needed
  • this works for views, controllers, and models
  • did i mention there phtml so we can pre process them? :p


Now, when a request is made to the server for a page, it bundles all view files required for angular in the initial request by parsing the layout templates tags.

So in the end, what we have is a system that allows me to edit ONLY the html files I need to create a whole new theme, with new functionality thanks to the power of AngularJS. This is something I did not expect when we first switched.

Being able to develop this theme on the same code line as our primary code is absolutely amazing. It saves us time with branching/merging, and lets us test our code out in the wild where we never would.

Thanks AngularJS. Thanks Cana.

Curious on how I build the views? Take a look at crunchbutton.com (or if you are feeling adventurous dev.crunchr.co) or shoot me an email from my ME page.

Disable smart quotes in OSX Mavericks

Windows and Word users have always driven me crazy when they complain about crazy characters in their posts or pages…and the cause is almost always smart quotes or dashes. With the release of OSX Mavericks, we too now have the misfortune of smart quotes. Smart quotes are those little angled quotes that point to your content. Like:

“Smart double quotes.”          "Dumb double quotes."
‘Smart single quotes’           'Dumb single quotes'

If you are pasting things back and forth between people and someone has crazy encoding settings, or isn’t using UTF-8, those characters can get all crazy, and not show up properly. Especially when pasting them straight into a database.

So, lets disable them!

To do so in OSX Mavericks:

  1. Open System Preferences from Applications or Launchpad or wherever.
  2. Go to Keyboard
  3. Go to the Text tab
  4. Uncheck Use smart quotes and dashes.

If you are using TextEdit, you will also need to:

  1. Open TextEdit
  2. In the top menu, go to TextEdit > Preferences
  3. Uncheck both Smart quotes and Smart dashes

Enjoy!

Reading google+ API feeds into your blog with PHP

Today I get this lovely email from google telling me all about the availability of the new Google+ Platform API. So naturally I have to check it out! For a while I have been using facebook and twitter feeds as simplistic blogs for some of my sites, but for my personal site, I would love to just use Google+.

I’ve created a simple example of how to setup and read your own public Google+ feeds.

Enabling API access and getting your API Key

  1. Head over to the APIs Console.
  2. Select Services
  3. Enable the Google+ API services. Without this you will not be able to access your feeds.
  4. Select API Access
  5. Under Simple API Access, grab your API Key. Hang onto this as we will need it to access any of the API services.

Getting your Google+ ID

If you don’t already know where this is located, head on over to Google+, click on your image in the upper right, and click Profile. In the address bar, it will now have a link like

https://plus.google.com/106670447018211124292

Accessing your activity stream

Now that you have your ID and Key, you can go to the url of your stream. Your stream URL will look like this

https://www.googleapis.com/plus/v1/people/106670447018211124292/activities/public?key=[API Key]

Parsing this stuff in PHP

Thankfully all the responses are in JSON which makes it really easy to read the data. Each item is inside of items.

$id = '106670447018211124292';
$key = 'YOUR KEY HERE';
$feed = json_decode(file_get_contents('https://www.googleapis.com/plus/v1/people/'.$id.'/activities/public?key='.$key));

foreach ($feed->items as $item) {
  echo '<h2>'.$item->title.'</h2>'
    .'<i>'.date('F jS Y @ H:i:s',strtotime($item->published)).'</i>'
    .'<p>'.$item->object->content.'</p>'
    .'<br /><br />';
}

And now we can access our public junk! This feed contains some additional information other than just the title and content. Like if you entered a url, it will attach the url to it inside of object.

Terminal skipping while editing files over SSH in OSX Lion

So since I installed OSX Lion ive been having this crazy issue where editing a file in nano or vi would sometimes skip characters, or shuffle them around, particularly when I hit backspace. I would then save the file and open it and it would look nothing like what i entered. There is a solution!

  1. Go to Terminal > Preferences
  2. Go to Advanced
  3. Change the Declare terminal as: from xterm-256color to xterm-color

yay! I only has this issue while editing my CentOS boxes as that all i have anymore.

Monitoring site errors using Server Density and Python

I recently stumbled upon the site Server Density while upgrading all my servers and signed up for the service. It as a great pretty interface, a customizable dashboard, and pretty extensive alert capabilities. They also have a free trial so I suggest signing up. The one thing I did not like was the inability to scan for a specific string on the website to confirm if the website is still operational, like with host-tracker.com.

The good news is Server Density allows you to create python plugins to run at the update interval on any/all of your servers. This gives you free reign over your reports. Using this tutorial, I will explain how to manually create and install this and any other plugin. Or you can just download or install it from the below links.

GitHub - https://github.com/arzynik/sd-webcheck

Server Density Plugin - http://plugins.serverdensity.com/webcheck

Create your plugin

First, log into your SD account and go to Plugins. Then add a new plugin and name it WebCheck.

Add your plugin to your server

Next you will need to ssh into your server and create the directory

mkdir /usr/bin/sd-agent/plugins/

In that directory, create a new file called WebCheck.py. This will be the main and only file for the plugin. Grab the code from https://github.com/arzynik/sd-webcheck/blob/master/WebCheck.py and paste it into the file.

Configure your plugin

If this is your first plugin, you will need to add your plugin directory to your plugin config file. Open up /etc/sd-agent/config.cfg and replace the plugin_directory line with

plugin_directory: /usr/bin/sd-agent/plugins

WebCheck allows you to check the status of as many sites as you want. In order to check they you will need to enter a new config entry for each site, using the below fields:

  • name - The name and key you want to use on the SD interface. Remember that all “.” will be replaced with “_” due to api limitations.
  • url -
  • find - The regex or string to search for. I typically just use an html tag but you can search for anything.
  • result - The result is the regex quantifier. Lets say you want to search from </html>, you would keep this True. Or lets say you want to search for Parse Error, you would then make this False.

Here is an example config entry

[WebCheck 1]
name: devin.la 
url: http://devin.la/
find: </html>
result: True

Restart the agent service to load the changes. Once it is restart it will begin sending data to SD

/etc/init.d/sd-agent restart

Settings up alerts

Because WebCheck only returns a boolean result, you probably wont be using it with graphs much. The alerts, however, are extremely useful. I personally recommend the iPhone push notifications, as they are uber fast.

  1. In your Server Density account, go to Alerts and click add new alert.
  2. Select your server that you installed it on, who is going to get it, and the alert type.
  3. For Check, select WebCheck.
  4. For Key, enter the name of your config entry. For me it is devin_la.
  5. For Trigger threshold, set it to Less than 1.
  6. Add it and you now know when your server is messed!

I love it!

One of my developers was working on a site and pushed some naughty changes to one of the sites I had just set up with this, and within seconds i got a push notification that it was broken. He fixed it pretty quick and it then alerted me when it was all fixed! Please share your cool plugins with me!

Importing and exporting cPanel accounts into other systems

Finally, after six years, I found an affordable host thats slightly faster than my box I built in 2005 then. goodbye cPanel — hello vm. I curently have hosting accounts with Dreamhost, Media Temple, Linode, and Rackspace Cloud, though I wont get into that right now. For now lets focus on how to get all those cPanel accounts from my other box into one of my new CentOS vms. For these I will be using no admin panel of any kind, no webmin, VHCS, WHM, or cPanel. I simply dont need it as its just as easy to ssh in and run a command rather than loging into a web interface and clicking something.

What I need to import

Im going to need a few things imported

  • Users
  • Passwords
  • MySQL databases
  • MySQL users
  • Files
  • vhosts

The reason I will not be needing email is I have switched to Google Apps for everything. Its more reliable and faster than anything I have ever set up using a single box. If you haven’t tried it yet I highly recommend it.

The import script

You can find my cPanel import script on GitHub

cPanel-import - https://github.com/arzynik/cpanel-import

Running the script

Running the script is easy and has a few options

Usage: cpanel-import.php file [options]
Ex: cpanel-import.php devin.tar.gz --ip=100.100.100.100 --forceuser
  • username - Import as a different username.
  • password - Overwrite the users password.
  • ip - The IP to bind the vhost to.
  • mysql-user - The root or super admin for mysql.
  • mysql-pass - The root or super admin password for mysql.
  • source - Where to find the file.
  • dest - Where to find put the user.
  • ignore - Comma separate list of files to ignore in homedir.
  • httpd - Apache path.
  • debug - Show commands.
  • verbose - Show additional info.
  • forceuser - If the user exists, keep going, and delete their home directory.

This will create the user, database, vhost, and pretty much set up the entire site for you, and restart apache. Easy!

Disabling Spotlight on OSX Lion

I have had so many problems with Lion, one of which is it keeps indexing spotlight. This sucks when i need something to actualy run. So … lets disable it!

sudo mdutil -a -i off