Posts Tagged ‘php’

Install and Serve a Rails Application from PHP Subdirectory Using Apache, Phussion Passenger and Ruby Enterprise Edition

Posted 25 Feb 2010 — by admin
Category Ruby on Rails

Here is how to install a Rails application out of a subdirectory (rather than as a subdomain) with the Apache web server(Apache2). In this example I’m going to use my own blog which is a Wordpress installation and serve a Rails application from the subdirectory “reader”. Note, I’m not going to keep my Rails application in the document root of my Wordpress Blog, which is a PHP application and therefore anyone could browse the ruby source code :( . I’ll keep it elsewhere on the filesystem and tell Apache about the location in the VirtualHost file.

You can visit the application by going to http://seanbehan.com/reader. The application just parses a bunch of RSS feeds and displays them.
It uses the Feedzirra library, which I’ve also written about http://seanbehan.com/ruby-on-rails/installing-feedzirra-rss-parser-on-ubuntu-8/.

I’m using Phussion Passenger and Ruby Enterprise Edition from the folks at Mod Rails. Installing both Phussion Passenger and Ruby Enterprise Edition is simple and a very well documented process. However, you’ll need to download and compile them from source. If you install Ruby Enterprise Edition (REE) it comes w/ Passenger so you won’t need to download it separately. I recommend going with REE. http://www.rubyenterpriseedition.com/download.html

Installing Ruby Enterprise Edition

Here are the commands to download and install (change the X.X.X to the package you’ve actually downloaded).

wget http://rubyforge.org/frs/download.php/68719/ruby-enterprise-1.8.7-2010.01.tar.gz
tar xzvf ruby-enterprise-X.X.X.tar.gz
./ruby-enterprise-X.X.X/installer

When you run the installer you’ll be prompted for an installation location. Just hit enter to install in the default location. Follow the instructions from there and remember to copy/paste any code that they give you.

Official Instructions on installation for Passenger on its own are available here http://www.modrails.com/install.html I’ve written about setting up an entire box w/ Passenger here http://seanbehan.com/ruby-on-rails/new-ubuntu-slice-apache-mysql-php-ruby-on-rails-git-and/ If you already have Passenger installed and want to use REE just download and install REE and it’ll recompile Passenger with REE support if you follow the instructions.

*** If you install REE you’ll need to either link or reinstall all your gems. I linked the REE gem with the one in /usr/bin so that I can run gem install and REE will be aware of it.

ln -s /opt/ruby-enterprise-X.X.X/bin/gem /usr/bin/gem

The VirtualHost

If you have Passenger and REE successfully installed you’ll need to modify your VirtualHost file and add Rails application information to it.

<VirtualHost *>
  # Normal virtual host info
  ServerName seanbehan.com
  ServerAlias *.seanbehan.com
  DocumentRoot /var/www/seanbehan.com/wordpress

  # Rails info goes here
  Alias /reader /var/www/seanbehan.com/reader/public
  <Location /reader>
    PassengerAppRoot /var/www/seanbehan.com/reader
    RailsEnv production
  </Location>
</VirtualHost>

The “Location” directive tells apache to forward requests starting with /reader to the directory
/var/www/seanbehan.com/reader/public which is the location of our Rails app. However, we need to add the PassengerAppRoot assignment so that it knows where the actual application lives.

Routing in Rails with Relative Path

And finally, your Rails application will need to be aware of the relative url prefix assigned to each route. Normally, you could do this w/ the :path_prefix at the individual route level like so

map.resources :feeds, :path_prefix => "reader"

This will work but you can add a line to your RAILS_ROOT/config/environments/production.rb file which will handle all your routes for you. This way you don’t need to setup a relative path on your development environment work.

# in RAILS_ROOT/config/environments/production.rb
config.action_controller.relative_url_root = '/reader'

Final Thoughts

Remember Ruby Enterprise Edition is the new Ruby Interpreter that your Rails web applications are using. If you
have any gem or rake issues make sure that you’re using the same interpreter that REE is using. Look in the location
installation of REE “/opt/ruby-ent…” bin/gem or bin/rake and see if that helps. I just linked those to the standard /usr/bin/gem and /usr/bin/rake and everything worked fine.

Also I’ve read some people have trouble using the alias with passenger. This may be an older issue but works for me without a problem on Ubuntu (latest).

Here are some useful resources I found along the way…

http://robots.thoughtbot.com/post/159806388/phusion-passenger-with-a-prefix

http://www.modrails.com/documentation/Users%20guide.html#deploying_rails_to_sub_uri

http://www.modrails.com/documentation/Users%20guide.html#RailsBaseURI

http://stackoverflow.com/questions/848258/server-prefix-and-rails-routes

Generate MySQL Datetime Type Using PHP Date() Function

Posted 05 Feb 2010 — by admin
Category php

If you want to insert a datetime that matches the default mysql datetime type format use this

date('Y-m-d H:i:s');

PHP Paypal IPN Script

Posted 29 Jan 2010 — by admin
Category php

Simple script for posting a valid response back to Paypal service after a sale is completed using the Paypal Instant Checkout payment method.

<?php
// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-validate';

foreach ($_POST as $key => $value) {
  $value = urlencode(stripslashes($value));
  $req .= "&$key=$value";
}

// post back to PayPal system to validate
$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30);

// assign posted variables to local variables
$item_name        = $_POST['item_name'];
$item_number      = $_POST['item_number'];
$payment_status   = $_POST['payment_status'];
$payment_amount   = $_POST['mc_gross'];
$payment_currency = $_POST['mc_currency'];
$txn_id           = $_POST['txn_id'];
$receiver_email   = $_POST['receiver_email'];
$payer_email      = $_POST['payer_email'];

if (!$fp) {
// HTTP ERROR
} else {
  fputs ($fp, $header . $req);
  while (!feof($fp)) {
    $res = fgets ($fp, 1024);
    if (strcmp ($res, "VERIFIED") == 0) {
      // check the payment_status is Completed
      // check that txn_id has not been previously processed
      // check that receiver_email is your Primary PayPal email
      // check that payment_amount/payment_currency are correct
      // process payment
    } else if (strcmp ($res, "INVALID") == 0) {
      // log for manual investigation
    }
  }
fclose ($fp);
}

I grabbed this script from Paypal Docs… I never can find the info I need w/in their site so I’m posting here for reference. https://www.paypal.com/us/cgi-bin/webscr?cmd=p/pdn/ipn-codesamples-pop-outside#php

Listing Files and Directories with PHP

Posted 27 Jan 2010 — by admin
Category php

Listing all files and directories using PHP 5.

<?php
$files = array(); $dir = dir(".");
while(false!==($file=$dir->read())):
  if(($file{0}!=".") && ($file{0}!="~") && (substr($file, -3)!="LCK")
    && ($file!=basename($_SERVER["PHP_SELF"]))):
      $files[$file] = stat($file); //
    endif;
endwhile;
$dir->close(); ksort($files); ?>

<table border=1 cellpadding=5>
<tr><th>Name</th><th>Size</th><th>Date</th></tr>
<?php foreach($files as $name => $stats): ?>
  <tr>
    <td><?php print $name;?></td>
    <td><?php print $stats['size']; ?></td>
    <td><?php print date("m-d-Y h:ia", $stats['mtime']); ?></td>
  </tr>
<?php endforeach; ?></table>

Highlight String in PHP

Posted 23 Jan 2010 — by admin
Category php

Function for highlighting text/strings in PHP.

$content = file_get_contents("http://php.net/");

print highlight("PHP", $content);

function highlight($match, $string){
  return str_ireplace($match, "<span style='background:yellow'>$match</span>", $string);
}

Will output something like this

Highlighted PHP.Net

Highlighted PHP.Net

***Notice all the styles are gone :( … would need to parse the document body to maintain stylesheet and other ‘php’ strings that might be in resources paths.

Absolutize Relative Links Using PHP and Preg_Replace_Callback

Posted 13 Jan 2010 — by admin
Category php

I was in the market for a simple php script to replace hrefs with their absolute paths from scraped web pages. I wrote one myself. I used the preg_replace_callback function so that I could pass the parsed results as a single variable.

<?php
$domain = "http://seanbehan.com";
$pattern = "/\bhref=[\"|'](.*?)[\"|']/";
$string = file_get_contents($domain);

// prepends relative links w/ $domain skips returns the match if already absolute
function replace_href($match){
  global $domain;
  if(substr($match[1], 0, 7)!=="http://" && substr($match[1],0,8)!=="https://"){
    return "href='".$domain.$match[1]."'";
  } else {
    return "href='".$match[1]."'s";
  }
}
print preg_replace_callback($pattern, "replace_href", $string);

TinyMCE Rich Text Editor: HELLO EDITOR Plugin Tutorial and Example

Posted 06 Nov 2009 — by admin
Category Wordpress

I wanted to create a button for the TinyMCE Rich Text Editor for Wordpress. It was tough to find good docs on the subject. There are a couple of useful posts out there but in general I found them lacking.

http://codex.wordpress.org/TinyMCE_Custom_Buttons#Creating_an_MCE_Editor_Plugin

The above resource has a good section on the PHP code needed to write your own callbacks but there is no mention of the JavaScript required to create a button and interact with it. Links are available for more reading but why not also post a code sample to get up and running?

Anyway, I made a simple plugin after hobbling a bunch of resources together to get a button in the text editor that when clicked triggers an alert to the window. The code isn’t fancy but it might provide a starting off point for a larger project or be useful as a resource for some of the basic ideas involved. I read through a couple of the sample plugins included with Wordpress along with the Viper Video Quicktags Plugin, which makes extensive use of the feature. However, they were too complex for a quick, basic understanding.

This plugin doesn’t do a lot. I’ll probably flesh it out a bit more when I have a clearer understanding of how it all works. For now here are some code samples and at the bottom of this post there is a link to the .zip file for the plugin which you can install on your own site.

In wp-content/plugins/hello_editor/index.php

<?php
/*
Plugin Name: Hello Editor
Plugin URI: http://seanbehan.com/wordpress/tinymce-rich-text-editor-hello-editor-plugin-tutorial-and-example/
Description: A simple plugin showing how to add a button to the rich text editor in Wordpress.
The plugin doesn't do anything except place a button and respond to onclick event, with "Hello Editor".
Most of the editor plugins available are too 'functional' to quickly read and see how to do the
basics. This plugin doesn't 'do' anything, rather it makes it simple to read what is going on.
Version: 0.1
Author: Sean Behan
Author URI: http://seanbehan.com
*/

define( "HELLO_EDITOR_PLUGIN_DIR", "hello_editor" );
define( "HELLO_EDITOR_PLUGIN_URL", "/wp-content/plugins/" . HELLO_EDITOR_PLUGIN_DIR );

// Register the external pllugin from the .js file
function hello_editor_register_external_plugin($plugin_array) {
  $plugin_array['HELLO_EDITOR'] = HELLO_EDITOR_PLUGIN_URL . '/editor_plugin.js';
  return $plugin_array;
}

// Add the button to the array ***NOTE*** The name here "HELLO_EDITOR" must
//match the name in the editor plugin file for the addButton(name_of_button) function!
function hello_editor_register_button($buttons) {
  array_push( $buttons, "|", "HELLO_EDITOR" );
  return $buttons;
}

// Filters which will call our functions
function hello_editor_init(){
  add_filter("mce_external_plugins", "hello_editor_register_external_plugin");
  add_filter('mce_buttons', 'hello_editor_register_button');
}

// When the editor is initialized
add_action('init', 'hello_editor_init');

In wp-content/plugins/hello_editor/editor_plugin.js

(function() {
  //Init the plugin
  tinymce.create('tinymce.plugins.HELLO_EDITOR', {
    init : function( ed, url ) {
    //addButton name needs to match $plugin_array['HELLO_EDITOR'] in add_filter function
    ed.addButton('HELLO_EDITOR', {
      title : 'Hello Editor',
      image : url + "/buttons/hello.png",
      onclick : function(){
        alert("HELLO EDITOR!");
      }
    });
  },
  //Info about the plugin
  getInfo : function() {
    return {
      longname : "Sean Behan's Sample Hello Editor Plugin",
      author : 'Sean Behan',
      authorurl : 'http://www.seanbehan.com/',
      infourl : 'http://seanbehan.com/wordpress/tinymce-rich-text-editor-hello-editor-plugin-tutorial-and-example/',
      version : "0.1"
    };
  }
});
tinymce.PluginManager.add('HELLO_EDITOR', tinymce.plugins.HELLO_EDITOR);
})();

You’ll also need a directory called buttons inside the hello_editor plugin directory with a hello.png file which will be displayed. Without it you’ll just get a blank button. You can rename this but remember to change the name in the editor_plugin.js file as well.

You can download the sample plugin here http://seanbehan.com/wp-content/uploads/2009/11/hello_editor.zip

Ruby on Rails vs. PHP in a Nutshell

Posted 29 Jul 2009 — by admin
Category Ruby on Rails
YouTube Preview Image