Howto compile apache module mod_example.c on Ubuntu

26 Oct

I need to write an apache module and looked around for a quick start on how to get started developing apache modules under ubuntu. I didn’t find anything 1,2,3, so here’s what I came up with:

  1. sudo apt-get install apache2-prefork-dev
  2. mkdir ~/mod_example && cd ~/mod_example
  3. wget -O mod_example.c “http://svn.apache.org/viewvc/httpd/httpd/tags/2.2.14/modules/experimental/mod_example.c?revision=818301&view=co ( adjust as necessary for your version of httpd )
  4. sudo apxs2 -c -i mod_example.c ( this builds and installs the module. )
  5. sudo su
  6. echo “LoadModule example_module /usr/lib/apache2/modules/mod_example.so” > /etc/apache2/mods-available/example.load
  7. vi /etc/apache2/mods-available/example.conf Paste the following:
    <Location /example-info>
    SetHandler example-handler
    </Location>
    
  8. cd /etc/apache2/mods-enabled && ln -s ../mods-available/example.* .
  9. service apache2 restart
  10. To test, browse to http://localhost/example-info.

    References:

OSC is hiring!

08 Sep

We presently have two open positions for talented web developers. Both positions are in our San Jose, Costa Rica office. We are centrally located on Paseo Colon, walking distance from Parque La Sabana. OSC employees enjoy a collaborative and low-stress work environment, challenging projects, competitive salaries, and fantastic co-workers.

Resume’s should be submitted to jobs at osc.co.cr. Please include a cover letter and the job title you are most interested in.


Position 1: Lead Web Developer, CRI Team.

CRI is a not-for-profit corporation that helps students in their transition from school to the external world. They provide software to school children simulating a real workplace environment.

In this position, you will help build and extend CRI’s software offerings. Of particular note will be a web module for creating, taking, and grading tests, where some parts of the tests will receive input from flash mini-movies or html5 drag-n-drop areas. You will also help improve our current report generating system.

You will work to extend and enhance the Moodle (moodle.org) open source software package.

Additional responsibilities will be in the area of operations and security in a LAMP environment, and evaluation of moving to a cloud based service model.

Experience generating flash applications and/or interacting with them programmatically is a definite plus.

This contract position is for a period of 6 months, with a high likelihood of being retained further in either a full or part-time capacity.

Fluent written english is a requirement.


Position 2: Senior Developer, Reporting — SR team.

SR is a leader in the Search Engine Marketing industry.

In this position you will help extend and improve SR’s existing reporting capabilities and generate new types of reports for SR’s clients. Reporting is a very important part of SR’s business, so this position carries significant responsibility.

We are looking for developers with experience in some or all of the following:
PHP, mysql, excel spreadsheet generation, graphing, statistical analysis, large-scale data warehousing, ETL operations (pentaho, jasper), hadoop

Salary commensurate with experience.

Semi-fluent written english is a requirement.

Bash mass file rename script

08 Sep

Ever run into a situation where you have a bunch of files that need to be renamed to a certain format? I constantly run into this and have been writing a quick bash for loop with a sed expression to mass rename the files. I noticed I was repeating this a lot, so I decided to make a little script out of it.

Anyone who downloads files that are named something like:
Something_-_01-01.ext,  Something_-_01-02.ext, etc..

.. and wants to mass-rename them in a very flexible manner, like this (just as an example):
01 – Something – 01.ext,  01 – Something – 02.ext, etc..

.. and has way too little free time to do this one by one, then check this out.

Requirements:

  • Linux or cygwin (Bash/sh interpreter + sed, tr)
  • Knowledge of what a bash script is, and how to run it.
  • Knowledge of sed and regex expressions.

Usage:

renamer.sh (-s<string>|-t<string>) [options]
 
   Options:
     -s      : SED expression (e.g. "s/_/-/g" will replace "_" with "-" for all filenames)
     -t      : Character translation map (e.g. "abc|def" will change "apple" to "dpple" )
     -m      : Move files, will not move files otherwise
     -r      : Recursive renaming in child directories
     -a<str> : Set file match condition (current: *)
     -h|-H   : Displays this usage screen

Examples:

  • Something_-_01-01.ext » 01 – Something – 01.ext 
    renamer.sh -s"s/\(.[^_]*\)_-_\([0-9]*\)-\([0-9]*\)/\2 - \1 - \3/"
  • Something_-_01_-_BlaBlaBla_-_01.ext » Something – 01 – BlaBlaBla - 01.ext 
    renamer.sh -t"_| "
     
    or..
     
    renamer.sh -s"s/_/ /g"

Script:

Download

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/bin/sh
 
APP=$0
search=""
translate=""
move=0
aFile="*"
aRecurse=0
 
doUsage() {
  echo
  echo " $APP (-s|-t) [options]"
  echo
  echo "   Options:"
  echo "     -s      : SED expression (e.g. \"s/_/-/g\" will replace \"_\" with \"-\" for all filenames)"
  echo "     -t      : Character translation map (e.g. \"abc|def\" will change \"apple\" to \"dpple\" )"
  echo "     -m      : Move files, will not move files otherwise"
  echo "     -r      : Recursive renaming in child directories"
  echo "     -a : Set file match condition (current: $aFile)"
  echo "     -h|-H   : Displays this usage screen"
  echo
}
 
transformSed() {
 
  # perform sed op
  F=`echo "$1" | sed "$search"`
 
  # see if we have problems with sed op
  if [ $? -gt 0 ]; then
    echo "Problem detected with sed expression." >&2
    return 1
  fi
 
  # echo out the new string
  echo "$F"
  return 0
}
 
transformTranslate() {
 
  INF=$1
 
  declare -a TSTR
 
  echo "$translate" | tr "|" "\n" | while read T
  do
 
    if [ -z "$T" ]; then
      T=' '
    fi
 
    TSTR[${#TSTR[@]}]="$T"
 
    if [ ${#TSTR[@]} -eq 2 ] ; then
 
      TF=${TSTR[0]}
      TT=${TSTR[1]} 
 
      F=`echo "$INF" | tr "$TF" "$TT"`
 
      # see if we have problems with sed op
      if [ $? -gt 0 ]; then
        echo "Problem detected with translate expression." >&2
        return 1
      fi
 
      echo "$F"
      return 0
    fi
  done
}
 
procFile() {
 
  F="$1"
 
  # skip dirs
  if [ -d "$F" ]; then
    return 1;
  fi
 
  # get original file path
  cpath=`dirname "$F"`
 
  # get original file name
  cfile=`basename "$F"`
 
  if [ ! -z "$search" ]; then # do sed transformation
    cfile=`transformSed "$cfile"`
    if [ $? -gt 0 ]; then return 2; fi
  fi
 
  if [ ! -z "$translate" ]; then # do translate transformation
    cfile=`transformTranslate "$cfile"`
    if [ $? -gt 0 ]; then return 2; fi
  fi
 
  # put filepath together again
  N="$cpath/$cfile"
 
  echo -e "\n BEFORE: '$F'\n  AFTER: '$N'"
 
  # optionally move files to replaced name
  if [ $move -eq 1 ]; then
 
    # only if files have changed
    if [ "$N" != "$F" ]; then
 
      mv "$F" "$N"
 
      if [ $? -eq 0 ]; then
        echo "Moved Successfully"
      else
        echo "Problem while moving.."
      fi;
 
    else
      echo "No filename change detected.."
    fi
 
  fi
}
 
findFiles() {
  if [ $aRecurse -eq 1 ] ; then
    find . -name '* *' -o -name '*' -a -name "$aFile"
  else
    find . -maxdepth 1 -name '* *' -o -name '*' -a -name "$aFile"
  fi
}
 
# read arguments
while getopts 'a:s:t:mrhH' o; do
  case $o in
    a)
      aFile="$OPTARG"
    ;;
    s)
      search="$OPTARG"
    ;;
    t)
      translate="$OPTARG"
    ;;
    m)
      move=1
    ;;
    r)
      aRecurse=1
    ;;
    h|H)
      doUsage
      exit 0
    ;;
    :|\?)
      doUsage >&2
      exit 1
    ;;
  esac
done
 
# validate arguments
if [ -z "$search" ] && [ -z "$translate" ]; then
  echo "No search/replace (-s ) or translate (-t ) operation was provided" >&2
  doUsage >&2
  exit 1
fi
 
# begin processing
findFiles | while read F
do
 
  # process file
  procFile "$F"
 
  # see if we have problems, if so exit early
  if [ $? -gt 1 ]; then
    exit 1
  fi
 
done
 
exit 0

cURL o Wget?

15 Aug

¿Cómo saber cuál de estas dos herramientas de línea de comandos es mejor?. Dado que cumplen funciones similares, me parece que lo mejor es enumerar algunas de sus características más importantes.

Características comunes
La principal función de ambas es descargar contenido vía protocolos FTP, HTTP y HTTPS utilizando sintaxis de URL.
Ambos están desarrollados con codigo abierto y software libre.
Continue reading

Hyper-V – Recovering VHD’s and AVHD’s

05 Aug

Anybody who has had to deal with a catastrophic recovery process knows the pains involved with retrieving data. In particular cases like mine, I needed to recover data which was locked away in virtual hard disks and in snapshots. I was unable to boot the original o/s for that disk since it was corrupted, only option was mounting each disk in a separate virtual machine and extracting what I could.

I’m not all to familiar with Hyper-V (I use Virtualbox mostly), so it was a “fun” learning experience for me. I found that Hyper-V lets you mount individual snapshots like disks, but you need to have an intact snapshot chain to do so. Also, in the latest version of Hyper-V, your vhd’s and avhd’s can not be in a compressed filesystem.. which sadly, I had to decompress (took many, many hours).

So long story short, If you ever need to switch back to a snapshot, it’s just a matter of switching the mounted hard drive image to the snapshot file. I found that this was very convenient until I dealt with a snapshot that seemed to be corrupted, thus corrupting the whole chain. I could not find any working/free tools out there for avhd recovery. Bunch of data recovery services, but they sound way too expensive. If anyone knows of any reliable tools, please comment.

Html Emails with Zend_Mail and Zend_View

30 Jul

When write web applications, eventually you will need to send emails, Zend Framework have a good implementation that can be used stand alone or as a full framework called Zend_Mail, his usage is very straight forward:

// assuming that you included your Zend_Mail library
 
// instance the class
$mail = new Zend_Mail("utf-8");
 
// add a recipient
$mail->addTo("your_recipient@somemail.com");
 
// mail content as html
$mail->setBodyHtml("<h1>Hello world</h1>");
 
// from email
$mail->setFrom("yourself@somemail.com");
 
// your subject
$mail->setSubject("Say Hello");
 
// send the email
$mail->send();

With the last example You can send mails but will be difficult to provide variables or even use common layouts in the mails, to solve that, you can use html templates like Smarty, or Zend_View, we are going to use the last one.

To use Zend_View as a HTML template, we need to create a instance (and require the library depending of your setup), define your view templates path, assign your variables and your extra configuration (partials, layouts, includes) and finally render the template to be the body of Zend_Mail.

// remember to include your Zend_View Library
 
// instance the class
$html = new Zend_View();
 
// path of your view templates path
$html->setScriptPath('/some/view/path/');
 
// clear all defined variables before, you can avoid this
$html->clearVars();
 
// add a variable
$html->someName = "Edder";
 
// render the template that you already create it
$bodyHtml = $html->render("your-template.phtml");
 
// assuming that you already have the $mail
$html->setBodyHtml($bodyHtml);
 
// send the email
$html->send();

The template used on the last example to be our email body is the following:

<h1>Hello: <?php echo $this->someName ?></h1>

For more information about using Zend_Mail and/or Zend_View refer to the official documentation.

Is it JSON or just a string?

26 Jul

I came up with this issue last week. I had an ajax call that returned a string and I needed to determine if it was ajax or not.

After searching for a couple of hours I only found references to eval, JSON.parse or jQuery.parseJSON to try to parse the string, but every time a regular string was passed to any of those functions a JavaScript error was thrown. All I needed was to identify if the string was JSON or not.

So, I ended up doing this:

1
2
3
4
5
6
7
try {
    result = eval(myVariable);
    //JSON
}
catch (e) {
    //regular string
}ï»

If you know of a better way to handle such case, please let me know :)