Author Archives: Michael

Git-ftp: sync repository with production server

My server does not support git remote commands, but has ftp. But I want to be able to update production with my git changes. After learning about git log, I realized it was possible to find recent changes to make ftp easier, then realized someone must have already made it really easy. Thus I discovered git-ftp:

https://github.com/git-ftp/git-ftp

 

Git: Production and Development servers

I want me and any other developer to develop on our local machine, and push to GitHub. But I want to be able to have our production server update from the master branch of our repository on GitHub.

This is the solution I found:
http://mikeeverhart.net/git/using-git-to-deploy-code/

This solution asks I create a repo on the production server, and setup a post-recieve hook to perform a checkout to the web root.

However, with this solution the production server does not know about my GitHub at all.

How to sign a jar

Create a keystore, I started with a p12.

Find the alias for the imported p12, if needed.

Sign the jar with jarsigner:

> jarsigner -keystore keystore.jks myjartosign.jar “my alias in the keystore” -tsa “www.example-tsa.com”

You will get an warning if you don’t use a url to a tsa. I googled my certificate authority and looked up their tsa on their knowledge base.

In my case I used Comodo, and their tsa is “http://timestamp.comodoca.com/rfc3161“.

Finding an imported certificate’s alias in a java keystore

Click here if you need to create a keystore. I started with a p12.

When you need your certificate out of a keystore you will need to know the alias of the certificate.
First list the keystore’s contents:
> keytool -list -keystore keystore.jks

You should expect output like this:
My websites’s comodo ca limited id, Jan 1, 2014, PrivateKeyEntry,
Certificate fingerprint (SHA1): 45:32:13:3D:F2:1D:F7:DA:84:6A:43:DF:1E:86:B3:64CB:4B:3D

The alias is everything before the first comma:
My websites’s comodo ca limited id

Verify the alias:
> keytool -list -keystore keystore.jks -alias “My websites’s comodo ca limited id

If it worked it should list the above certificate info, and now you have the alias.

Create a Java keystore from a p12 file

You will need a keystore to sign jars. In my case I was given a p12 file from my Certificate Authority. It took a while but I finally found how to make a keystore from my p12.

You don’t need a keystore to exist to import a p12:

> keytool -v -importkeystore -srckeystore certificate.p12 -srcstoretype PKCS12 -destkeystore keystore.jks -deststoretype JKS

Now the keystore will have the contents of the p12, which is the certificate and the key.

Sources:
http://www.webfarmr.eu/2010/04/import-pkcs12-private-keys-into-jks-keystores-using-java-keytool/
http://blog.jgc.org/2011/06/importing-existing-ssl-keycertificate.html

Java 7.51 – Applets must be signed

With the new Java update 7.51 all applets must follow new guidelines:

  1. All code for Applets and Web Start applications must be signed
  2. Permissions attribute in the manifest must be set, no default

Read more here

What this means is any applet that currently exists that is not signed, has an expired certificate, or doesn’t have the permissions attribute will no longer work. As a side job I make sure photodepository.com works, and it was affected by this so here’s how to fix it.

Quick Answer:

  1. Buy a code signing certificate
  2. Resign your applet
  3. Done

Here’s how:

  1. Create a txt file for the manifest additions. Here are the contents:
    Permissions: all-permissions
    Application-Name: MyAppletName
  2. Extract you jar:
    > mkdir jarname
    > mv jarname.jar jarname
    > cd jarname
    > jar xf jarname.jar
  3. Remove the META from the extract
    > rm -r META-INF/
  4. Rejar the applet and include the required manifest additions
    > jar cfm jarname.jar ../manifestAdditions *
  5. Sign the jar. Here’s my posts to do the signing:
    > Create a Java keystore from a p12 file
    > Finding an imported certificate’s alias in a java keystore
    > How to sign a jar

 

Why I don’t like the frame property

CGRect is the type for the frame property in UIViews.
Which is a c struct defined in CGGeometry.h as the following:

struct CGRect {
  CGPoint origin;
  CGSize size;
};
typedef struct CGRect CGRect

What’s wrong with frame and CGRect? I mean they were written by Apple!

I position and resize views all the time, but I very rarely do it at the same time. What I mean is I might move the view to the position (100, 50), but maintain the size. This is how:

// method 1: use the original frame
// I have 3 lines of code for 1 task
CGRect frame = view.frame;
frame.origin = CGPointMake(100, 50);
view.frame = frame;

// method 2: recreate the frame rect
// I must pass the width and height
view.frame = CGRectMake(100, 50, view.frame.size.width, view.frame.size.height);

// method 3: not legal, but how I want to do it
view.frame.origin = CGPointMake(100, 50);

It might seem I’m complaining about writing a couple extra lines of code. But currently I’m updating the whiteboard feature in my app, which is over 2500 lines of code. The extra lines are not a hassle, they are just adding more mess to scroll past, along with affecting readability.

So if CGRect was a class, it would be able to comply with KVO. As far as I know, this is the reason the frame property not assignable. This means I could change the values of the CGRect and the view can be notified to redraw. Note: the setFrame method of UIView doesn’t just set a value, but instead lets the view know to redraw with a new size.

I’m not going to end my post with just that. Here’s a solution:

// include this function in a class, or in the prefix file
CGRect CGRectMoveOrigin(CGRect rect, CGFloat x, CGFloat y)
{
  CGRect rect;
  rect.origin.x = x;
  rect.origin.y = y;
  return rect;
}

// example
view.frame = CGRectMoveOrigin(view.frame, 100, 50);

// if CGRect were KVO compliant
view.frame.origin.x = 100;
view.frame.origin.y = 50;

I am aware of CGRectOffset, but what if I don’t want to do inline math to change the position? 🙂

In the end, I’m not actually going to use my method, now. I might in future projects. There are a lot of times where I’m only changing one of the 4 values in the CGRect, which would imply I want a ton more CGRectXXX(CGRect frame, ….) functions, and I don’t want that.

But direct access to the frame’s struct would be nice.

Using MagicalRecord to wrap CoreData

My first time using CoreData, I used apple docs and CoreData methods. It was difficult at first and a lot to learn to just have a database in iOS. So with my new app I went looking for a wrapper. What I found was MagicalRecord, which seems to have a lot of following. MagicalRecord makes CoreData act like ActiveRecord, by adding the needed methods with categories. If you have used web development frameworks before you know most of them connect with the database with an active record pattern.

Although it’s not entirely active record.

Get MagicalRecord here:
https://github.com/magicalpanda/MagicalRecord

Start with the installation docs here:
https://github.com/magicalpanda/MagicalRecord/blob/develop/Docs/Installation.md

And read the rest of the docs, up a level, here:
https://github.com/magicalpanda/MagicalRecord/tree/develop/Docs

Here’s a good the starter guide by Alexander Blunck here:
http://ablfx.com/blog/article/2
But you will need to read the actual docs.

Asynchronous URL requests with php and cURL

I’m scraping a website for data. However, the content I need appears at random, so each page load will show a different sentence. I need all the unique sentences that website will display.

How to do this? Like I’ve done many times before: curl_multi_exec. Except I never remember how to do it and end up back at the manuals.

Here’s my code:

<?php

$data = array();
$url = "http://www.example.com/";
$mh = curl_multi_init();

// I loop forever and watch the output
// put your condition to stop looping here
while(true){
	// create all cURL resources
	$chs = array();
	// number of consecutive requests sent out
	// from experience I find 20-40 to be the fastest
	// but you should experiment and find out yourself
	for($i = 0; $i < 10; $i++){
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_multi_add_handle($mh, $ch);
		$chs[] = $ch;
	}

	$active = null;
	//execute the handles
	// this loop looks weird because php 5.3.18 broke curl_multi_select
	do {
		do {
		    $mrc = curl_multi_exec($mh, $active);
		} while ($mrc == CURLM_CALL_MULTI_PERFORM);
		// this fixes the multi select from returning -1 forever
		usleep(10000);
	} while(curl_multi_select($mh) === -1);

	while ($active && $mrc == CURLM_OK) {
	    if (curl_multi_select($mh) != -1) {
	        do {
	            $mrc = curl_multi_exec($mh, $active);
	        } while ($mrc == CURLM_CALL_MULTI_PERFORM);
	    }
	}

	//close the handles
	foreach($chs as $ch){
		$html = curl_multi_getcontent($ch);
		curl_multi_remove_handle($mh, $ch);

		// parse here...
		if(!preg_match_all('/pattern/', $html, $matches)){
			echo "Error!";
		}
		
		// store matches as keys so I can find the unique ones
		// bonus: increment the value to count how many times you find that match
		foreach($matches[1] as $match){
			if(!isset($data[$match])){
				$data[$match] = 1;
				echo "+: ".$match."\n";
			} else {
				$data[$match]++;
				echo " : ".$match."\n";
			}
		}
	}
}

curl_multi_close($mh);

?>

I used the php manual, start here. And special thanks to an ‘Alex Palmer’ for his comment on how to fix the curl_multi_select issue, and ‘bfanger at gmail dot com’ for his quick solution.

Originally curl_multi_select will block until something happens, like a url returns, but now its returning -1 doesn’t matter what. Alex posted a bug report, where bfanger mentions to add a pause before checking multi select again.

Setting up my new website

I made this website to keep track of the things I had to struggle with when programming. All the time I have problems that sometimes I can’t even find a solution for on Google. So this website is me kinda giving back to the world, for when I do solve my problem.

Well, setting up this site is not so point and click and everyone says. I’ll go from the beginning, since I just bought this domain name. Here’s my format:
What I want to do.
How I did it.

Register my domain name.
I use GoDaddy. I’m not supporting them, I just use them. So login to GoDaddy. Discover the domain names price went up, of course, to $12.99. Which is a special deal since it’s usually $14.99. Then go to google and search godaddy, click on their ad to buy a domain for $0.99 and note somewhere to not promote GoDaddy. I’m a repeat customer, it’s a curse.

Tell GoDaddy to use my hosting providers name servers.
This really depends on your host. It’s usually ns1.something.com. Mine are:
DNS1.PRONETHOSTING.NET
DNS2.PRONETHOSTING.NET

Install wordpress.
I have cPanel so I decided to do a 1-click install for wordpress: click install wordpress, done.

Change my theme.
Dashboard -> Appearance -> Themes -> Twenty Twelve (Super old style).
I’ll probably change this later.

Make my permalinks (what the url looks like) not the default: ?p=123.
Dashboard -> Settings -> Permalinks -> Month and Name -> Save Changes
I prefer ‘Month and Name’ since I don’t plan on posting too many times, and for pages you can change the full name of the url.

Add a subdirectory to my root folder so I can have no wordpress content, like my hosted sites for my protfolio.
Edit the .htaccess file in your websites root directory, which will usually be the same folder that wordpress is installed is.
After ‘Rewrite Base’ add this line:
RewriteCond %{REQUEST_URI} !^/name.of.custom.folder/
Save .htaccess and test. I’m assuming you already made the folder named ‘name.of.custom.folder’ or something.

That’s it, now I’m adding my work to my portfolio.