SlideShare ist ein Scribd-Unternehmen logo
1 von 75
Downloaden Sie, um offline zu lesen
UNDERCOVER CODE
Supporting PHP With Non-Web Tools

             Sean Coates
            (for ConFoo 2010, Montréal)
WHAT WE’LL LEARN TODAY
•Input/Output, Pipes, Redirection
•Using Cron
•Processing mail
•Workers
•Creating dæmons
•Intentionally top-heavy
UNIX PHILOSOPHY
“
This is the Unix philosophy: Write programs
that do one thing and do it well. Write
programs to work together. Write programs
to handle text streams, because that is a
universal interface.                    ”
                       –Doug McIlroy
                        Creator of the Unix pipe
UNIX PHILOSOPHY
“
Write programs that do one thing
and do it well.

                                  ”
                 –Doug McIlroy
                  Creator of the Unix pipe
UNIX PHILOSOPHY
“
Write programs to work together.

                                  ”
                 –Doug McIlroy
                  Creator of the Unix pipe
UNIX PHILOSOPHY
“Write programs to handle text
 streams, because that is a universal
 interface.
                                    ”
                   –Doug McIlroy
                    Creator of the Unix pipe
UNIX PHILOSOPHY
“Write programs to handle text
 streams, because that is a universal
 interface.

                   –Doug McIlroy
                                    ”
                                        *
                    Creator of the Unix pipe
ASIDE: TEXT IS A

•Theoretical
                      *
    UNIVERSAL INTERFACE
•From A Quarter Century of
Unix (1994) (I think)
•Read: before most people
cared about Unicode
•Unicode makes this less true
ASIDE: TEXT IS A

•Theoretical
                      *
    UNIVERSAL INTERFACE
•From A Quarter Century of
Unix (1994)
•Read: before most people
cared about Unicode
•Unicode makes this less true
•…and by that, I mean painful
ASIDE: TEXT IS A

•Theoretical
                     *
    UNIVERSAL INTERFACE
•From A Quarter Century of
Unix (1994)
•Read: before most people
cared about Unicode             Photo: http://www.flickr.com/photos/guydonges/2826698176/



•Unicode makes this less true
•…and by that, I mean painful
•…and by that, I mean torture
ASIDE: TEXT IS A
    UNIVERSAL INTERFACE
•Theoretical
                       *
•From A Quarter Century of
Unix (1994)
•Read: before most people
cared about Unicode                Photo: http://www.flickr.com/photos/guydonges/2826698176/



•Unicode makes this less true
•…and by that, I mean painful
•…and by that, I mean torture
•Rant:
   http://seancoates.com/utf-wtf
ASIDE: TEXT IS A
     UNIVERSAL INTERFACE
$ echo -n "25c" | wc -c
     3
                          *
$ echo -n "25¢" | wc -c
     4                        Photo: http://www.flickr.com/photos/guydonges/2826698176/




$ echo -n “25c” | wc -c
-bash: $: command not found
     0
TEXT IS A
            *
UNIVERSAL INTERFACE


Let’s just assume this is true.
WRITE PROGRAMS THAT DO
ONE THING AND DO IT WELL.
 •Many Unixy utilities work like this:
  •wc - word count (character and line count, too)
  •sort - sorts input by line
  •uniq - remove duplicate lines, making output unique
  •tr - character translate
  •sed - stream editor
 •Unitaskers
WRITE PROGRAMS TO WORK
       TOGETHER.
•Simple tools = large toolbox
•Unitaskers are only bad in the physical world
•Unlimited toolbox size
 •(Busybox)
WRITE PROGRAMS TO WORK
        TOGETHER.
$ cat sounds.txt
oink
moo
oink
$ cat sounds.txt | uniq   $ cat sounds.txt | sort | uniq
oink                      moo
moo                       oink
oink
WRITE PROGRAMS TO
   HANDLE TEXT STREAMS.
•Power and simplicity for free
•Great for simple data
•Harder for highly structured data
•Chaining is wonderfully powerful, and iterative
WRITE PROGRAMS TO
    HANDLE TEXT STREAMS.
$ cat /usr/share/dict/words | wc -l
  234936

$ cat /usr/share/dict/words | grep '^f' | wc -l
    6384


$ cat /usr/share/dict/words | grep '^f' |
      egrep '([aeiou])1' | wc -l
    461
TEXT STREAMS
•“Standard” file descriptors
 •Input (#1)
 •Output (#1)
 •Error (#2)
•fopen() returns a file descriptor
•Redirection
•Pipelining
TEXT STREAMS:
          STANDARD OUTPUT
$ echo -n "foo"


       Input        Program       Output
       (null)     echo -n "foo"    foo
                    Console
TEXT STREAMS:
                 STANDARD INPUT
                              $ php
                              <?php
                 Keyboard     echo "woofn";
                                ctrl-d
                              woof

        Input               Program            Output

<?php
                             php               woof
echo "woofn";
REDIRECT
        STANDARD OUTPUT
$ echo -n "foo" > bar.txt


     Input        Program       Output
     (null)     echo -n "foo"    foo
                   bar.txt

$ cat bar.txt
foo
REDIRECT
            STANDARD INPUT
$ cat sounds.php
<?php
echo "oinkn";
echo "moon";

$ php sounds.php
oink
moo

$ echo '<?php echo "woofn";' | php
woof
REDIRECT
                 STANDARD INPUT
$ php < sounds.php
oink
moo

         Input           Program   Output
<?php
echo "oinkn";                      oink
                          php
echo "moon";                       moo

                         Console

$ cat sounds.php | php
PIPELINING

$ echo -n "foo" | wc -c
   3
PIPELINING

$ echo -n "foo" | wc -c
   3

        Input               Program       Output
        (null)            echo -n "foo"    foo
                              Pipe
        foo                  wc -c          3
                            Console
TEXT STREAMS:
             STANDARD ERROR
$ cat sounds.txt
oink
moo
oink

$ grep moo sounds.txt
moo

       Input             Program          Output
        (null)      grep moo sounds.txt    moo
TEXT STREAMS:
             STANDARD ERROR
$ grep moo nofile.txt
grep: nofile.txt: No such file or directory

Input        Program          Output                    Error
 (null) grep moo sounds.txt    (null)   grep: nofile.txt: No such file or directory
TEXT STREAMS:
          STANDARD ERROR
$ curl example.com
<HTML>
<HEAD>
 (etc.)

$ curl example.com | grep TITLE
 <TITLE>Example Web Page</TITLE>
TEXT STREAMS:
            STANDARD ERROR
$ curl fake.example.com
curl: (6) Couldn't resolve host 'fake.example.com'

$ curl fake.example.com | grep TITLE

curl: (6) Couldn't resolve host 'fake.example.com'
TEXT STREAMS:
             STANDARD ERROR
$ curl fake.example.com | grep TITLE
curl: (6) Couldn't resolve host 'fake.example.com'

Input         Program         Output                Error
                                         curl: (6) Couldn't resolve host
 (null) curl fake.example.com (null)            'fake.example.com'
                Pipe                               Console
 (null)      grep TITLE        (null)               (null)

                               Console
TEXT STREAMS
       (MORE ADVANCED)
•tee
 •curl example.com | tee example.txt | grep TITLE
•redirect stderr
 •curl fake.example.com 2 > error.log
•combine streams
 •curl fake.example.com 2>&1 > combined.log
•(assumes bash)
WHY?
•Much better languages to do this
 •Go to a Python talk
•Reasons to use PHP:
 •existing code
 •existing talent
  •== low(er) development time, faster debugging
CRON
•Time-based job scheduler (Unixy)
•Schedule is called a crontab
•Each user can have a crontab
•System has a crontab
CRON
$ crontab -l
MAILTO=sean@seancoates.com
2 * * * * php blog-hourly.php
                Command
                Day of Week
                Month
                Day of Month
                Hour
                Minute
CRON
           (SCHEDULING)
*     *    * * *   •Every minute
2     *    * * *   •On the 2nd minute   of
                    every hour
*/5   *   * * *    •Every 5 minutes
0     */2 * * *    •Top of every 2nd
                    Hour
0     0    * * 1   •Every Monday at
                    midnight
15    20   9 2 *   •Feb 9th at 8:15PM
15,45 *    * * *   •The 15th and 45th
                    minute of every hour
CRON
   (PATHS & PERMISSIONS)
• Runs as the crontab’s owner *
  • (www-data, nobody, www, etc.)
  • Caution: web root permissions
• Paths can be tricky
  • specify an explicit PATH
  • use explicit paths in commands
CRON
                  (EDITING)
$ crontab -e
(editor opens, save, exit)
crontab: installing new crontab

 • Use the crontab -e mechanism
 • System launched $EDITOR to edit the file
CRON
                       (SYSTEM)
• Often: /etc/crontab
• Sixth schedule field: user ( m h dom m dow user cmd )
• Better for centralizing (e.g. for deployment and version
    control)
•   /etc/cron.d/* (daily, monthly, weekly, etc.)

• Caution: avoid time-slam
MAIL
•Mail = headers + body
•Body can contain many “parts” (as in MIME/multipart)
 •Multipurpose Internet Mail Extensions
•MIME = much too complicated to discuss here
•Sending mail is hard; so is receiving it
•Focus on simple mail
•Or let someone else do the hard parts
MAIL
•At its core, mail looks a bit like HTTP:
 •headers
  •key: value
 •blank line
 •body
MAIL
Return-Path: <sean@seancoates.com>
X-Original-To: sean@seancoates.com
Delivered-To: sean@caedmon.net
Received: from localhost (localhost [127.0.0.1])
    by iconoclast.caedmon.net (Postfix) with ESMTP id 2D9CC78406F
    for <sean@seancoates.com>; Mon, 8 Mar 2010 14:58:20 -0500 (EST)
X-Virus-Scanned: Debian amavisd-new at iconoclast.caedmon.net
Received: from iconoclast.caedmon.net ([127.0.0.1])
    by localhost (iconoclast.caedmon.net [127.0.0.1]) (amavisd-new, port 10024)
    with ESMTP id Hjx8HGZQ1RAY for <sean@seancoates.com>;
    Mon, 8 Mar 2010 14:58:14 -0500 (EST)
Received: from [192.168.145.200] (unknown [24.2.2.2])
    by iconoclast.caedmon.net (Postfix) with ESMTPSA id BAB3A78405F
    for <sean@seancoates.com>; Mon, 8 Mar 2010 14:58:14 -0500 (EST)
From: Sean Coates <sean@seancoates.com>
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Subject: Test Subject
Date: Mon, 8 Mar 2010 14:55:50 -0500
Message-Id: <0B3DA593-3292-49C3-B3E6-4B4A26547421@seancoates.com>
To: Sean Coates <sean@seancoates.com>
Mime-Version: 1.0 (Apple Message framework v1077)
X-Mailer: Apple Mail (2.1077)

Test Body
#!/usr/bin/env php
                                     MAIL
<?php
$mail = stream_get_contents(STDIN);
// transpose possible CRLF:
$mail = str_replace(array("rn", "r"), "n", $mail);
list($tmpheaders, $body) = explode("nn", $mail, 2);

$tmpheaders = preg_split(
    "/n(S+):s+/",
    "n" . $tmpheaders,
    -1,
    PREG_SPLIT_DELIM_CAPTURE
);

$count = count($tmpheaders);
$headers = array();
for ($i=1; $i<$count; $i+=2) {
    $k = $tmpheaders[$i];
    $v = $tmpheaders[$i+1];
    if (isset($headers[$k])) {
        $headers[$k] = (array)$headers[$k];
        $headers[$k][] = $v;
    } else {
        $headers[$k] = $v;
    }
}

var_dump($headers);
MAIL
array(14) {
  ["Return-Path"]=>
  string(21) "<sean@seancoates.com>"
  ["X-Original-To"]=>
  string(19) "sean@seancoates.com"
  ["Delivered-To"]=>
  string(16) "sean@caedmon.net"
  ["Received"]=>
  array(3) {
     [0]=>
     string(167) "from localhost (localhost [127.0.0.1])
    by iconoclast.caedmon.net (Postfix) with ESMTP id 2D9CC78406F
    for <sean@seancoates.com>; Mon, 8 Mar 2010 14:58:20 -0500 (EST)"
     [1]=>
     string(212) "from iconoclast.caedmon.net ([127.0.0.1])
    by localhost (iconoclast.caedmon.net [127.0.0.1]) (amavisd-new, port 10024)
    with ESMTP id Hjx8HGZQ1RAY for <sean@seancoates.com>;
    Mon, 8 Mar 2010 14:58:14 -0500 (EST)"
     [2]=>
     string(174) "from [192.168.145.200] (unknown [24.2.2.2])
    by iconoclast.caedmon.net (Postfix) with ESMTPSA id BAB3A78405F
    for <sean@seancoates.com>; Mon, 8 Mar 2010 14:58:14 -0500 (EST)"
  }



       (1/2 Continued…)
MAIL
      (…2/2)
    ["X-Virus-Scanned"]=>
    string(44) "Debian amavisd-new at iconoclast.caedmon.net"
    ["From"]=>
    string(33) "Sean Coates <sean@seancoates.com>"
    ["Content-Type"]=>
    string(28) "text/plain; charset=us-ascii"
    ["Content-Transfer-Encoding"]=>
    string(4) "7bit"
    ["Subject"]=>
    string(12) "Test Subject"
    ["Date"]=>
    string(30) "Mon, 8 Mar 2010 14:55:50 -0500"
    ["Message-Id"]=>
    string(53) "<0B3DA593-3292-49C3-B3E6-4B4A26547421@seancoates.com>"
    ["To"]=>
    string(33) "Sean Coates <sean@seancoates.com>"
    ["Mime-Version"]=>
    string(35) "1.0 (Apple Message framework v1077)"
    ["X-Mailer"]=>
    string(19) "Apple Mail (2.1077)"
}
MAIL
#!/usr/bin/env php
<?php
$mail = stream_get_contents(STDIN);
// transpose possible CRLF:
$mail = str_replace(array("rn", "r"), "n", $mail);
list($tmpheaders, $body) = explode("nn", $mail, 2);

$tmpheaders = preg_split(
    "/n(S+):s+/",
    "n" . $tmpheaders,
    -1,
    PREG_SPLIT_DELIM_CAPTURE
);
// continued...
MAIL
// continued...
$count = count($tmpheaders);
$headers = array();
for ($i=1; $i<$count; $i+=2) {
    $k = $tmpheaders[$i];
    $v = $tmpheaders[$i+1];
    if (isset($headers[$k])) {
        $headers[$k] = (array)$headers[$k];
        $headers[$k][] = $v;
    } else {
        $headers[$k] = $v;
    }
}
MAIL
print_r($headers[$argv[1]]);
$ cat test.mail | ./simplemail.php Subject
Test Subject

$ cat test.mail | ./simplemail.php Received
Array
(
    [0] => from localhost (localhost [127.0.0.1])
  by iconoclast.caedmon.net (Postfix) with ESMTP id 2D9CC78406F
  for <sean@seancoates.com>; Mon, 8 Mar 2010 14:58:20 -0500 (EST)
    [1] => from iconoclast.caedmon.net ([127.0.0.1])
  by localhost (iconoclast.caedmon.net [127.0.0.1]) (amavisd-new,
port 10024)
  with ESMTP id Hjx8HGZQ1RAY for <sean@seancoates.com>;
  Mon, 8 Mar 2010 14:58:14 -0500 (EST)
    [2] => from [192.168.145.200] (unknown [24.2.2.2])
  by iconoclast.caedmon.net (Postfix) with ESMTPSA id BAB3A78405F
  for <sean@seancoates.com>; Mon, 8 Mar 2010 14:58:14 -0500 (EST)
)
MAIL
•Easier to just let King Wez handle it
•Mailparse
•http://pecl.php.net/mailparse
•Also handles MIME
MAIL
#!/usr/bin/env php
<?php
$mm = mailparse_msg_create();
mailparse_msg_parse($mm, stream_get_contents(STDIN));
$msg = mailparse_msg_get_part($mm, 1);
$info = mailparse_msg_get_part_data($msg);

// print_r($info);

print_r($info['headers'][$argv[1]]);

$ cat test.mail | ./mailparse.php subject
Test Subject
ALIAS
   •How is this useful?
(habari)$ cat /etc/aliases | grep security
security: |"/var/spool/postfix/bin/security"



   •Beware:
    •chroots
    •allowed bin directories
    •newaliases
   •See your MTA’s docs on how to make this work.
GEARMAN
•Offload heavy processes from web machines
•Synchronous or Asynchronous
•Examples
 •Mail queueing
 •Image resize
•Very configurable
•(We’ll barely scratch the surface)
GEARMAN
          web         web         web
         server      server      server


                  gearmand




worker                                    worker
            worker            worker
GEARMAN
                   web
                  server


              gearmand




worker                              worker
         worker            worker
GEARMAN
 web       web       web
server    server    server


         gearmand




          worker
GEARMAN
   web
  server


 gearmand




  worker
GEARMAN
     web
    server
                  gearmand


    worker


(same hardware)
GEARMAN
 web
server
                 gearmand


worker


   (same hardware)
GEARMAN
                  WORKER
#!/usr/bin/env php
<?php
require 'complicated_app/bootstrap.php';

$worker = new GearmanWorker();
$worker->addServer('127.0.0.1');
$worker->addFunction("send_invoice_mail", "send_mail");

function send_mail($to, $params) {
    return ComplicatedApp::send_invoice_email(
        $to,
        $params['amount'],
        $params['due']
    );
}
GEARMAN
               CLIENT
// ...

$client = new GearmanClient();
$client->addServer('127.0.0.1');

$task = $client->addTaskBackground(
    'send_invoice_mail',
    $params
);
DÆMONS
•Long-running processes
•Cron is a dæmon
•Often socket-listeners
•Screen
•Supervisord
•(X)Inetd, Launchctl
DÆMONS
                 SCREEN
•Terminal multiplexer (multiple terminals from one
 console)
•Screens persist between logins (doesn’t close on
 logout)
•Useful for dæmons
•A bit hackish
DÆMONS
                       SCREEN
(sarcasmic)$ ssh adnagaporp.local
(adnagaporp)$ screen -S demo

                           (adnagaporp)$ php -r '$i=0; while(true)
                             { echo ++$i . "n"; sleep(2); }'
                           1
                           2
                           3
                           4
                           5
                            ctrl-a   d

(adnagaporp)$ exit
(sarcasmic)$
DÆMONS
                       SCREEN
(sarcasmic)$ ssh adnagaporp.local
(adnagaporp)$ screen -r demo

                           (adnagaporp)$ php -r '$i=0; while(true)
                              { echo ++$i . "n"; sleep(2); }'
                           1
                           2
                           3
                           4
                           5
                           6
                           7
                           8
                           9
                           10
                           11
                           …
DÆMONS
                  SCREEN
•A bit crude
 •have to manually log in
 •no crash protection / respawn
 •no implicit logging
•Doesn’t always play well with sudo or su
•Does allow two terminals to control one screen
•Very simple and easy to use
•(see also tmux http://tmux.sourceforge.net/ )
DÆMONS
           SUPERVISORD
•Runs dæmons within a subsystem
•Handles:
 •crashes
 •concurrency
 •logging
•Friendly control interface
DÆMONS
                  SUPERVISORD
phergie-brewbot.ini:
[program:phergie-brewbot]
command=/usr/local/bin/php Bot.php
numprocs=1
directory=/home/phergie/Phergie-brewbot
stdout_logfile=/home/phergie/Phergie-brewbot/phergie_supervisor.log
autostart=true
autorestart=true
user=phergie
DÆMONS
SUPERVISORD
DÆMONS
                INIT.D
•Debian systems (Ubuntu, too), maybe others
 •/etc/init.d/*
 •/etc/rc*.d
 •update-rc.d
•Use The Debian Way™ when on Debian
DÆMONS
             LAUNCHCTL
•Mac only
•Similar to inetd/xinetd
•Avoid writing socket code
•Extremely simple to network
DÆMONS
            LAUNCHCTL
#!/usr/bin/env php
<?php
echo date('r') . "n";
DÆMONS
                               LAUNCHCTL
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/
PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
     <key>Label</key>
     <string>localhost.demodaemon</string>
     <key>ProgramArguments</key>
     <array>
         <string>/path/to/demodaemon.php</string>
     </array>
     <key>inetdCompatibility</key>
     <dict>
          <key>Wait</key>
          <false/>
     </dict>
     <key>Sockets</key>
     <dict>
          <key>Listeners</key>
               <dict>
                    <key>SockServiceName</key>
                    <string>60001</string>
                    <key>SockNodeName</key>
                    <string>127.0.0.1</string>
               </dict>
     </dict>

                         ~/Library/LaunchAgents/demodaemon.plist
</dict>
</plist>
DÆMONS
                  LAUNCHCTL
$ launchctl load ~/Library/LaunchAgents/demodaemon.plist




$ telnet localhost 60001
Mon, 08 Mar 2010 19:50:46 -0500
$
OTHER NON-CONSOLE
      TRICKS / TOOLS
•Subversion hook to lint (syntax check) code
•IRC bot (see http://phergie.org/)
•Twitter bot / interface (see @beerscore)
QUESTIONS?
•Always available to answer questions and to entertain
strange ideas (-:
•sean@seancoates.com
•@coates
•http://seancoates.com/
•Please comment: http://joind.in/1296
•…and see my talk on Friday: Interfacing with Twitter
•Also: beer.

Weitere ähnliche Inhalte

Andere mochten auch

New Product Development (NPD)
New Product Development (NPD)New Product Development (NPD)
New Product Development (NPD)Demian Entrekin
 
Atividades de inglês (interfaces Língua Portuguesa e Matemática)
Atividades de inglês (interfaces Língua Portuguesa e Matemática)Atividades de inglês (interfaces Língua Portuguesa e Matemática)
Atividades de inglês (interfaces Língua Portuguesa e Matemática)Natan Campos
 
J. Davis Values In Todays Economy And Society Speech 09 May09
J. Davis Values In Todays Economy And Society Speech 09 May09J. Davis Values In Todays Economy And Society Speech 09 May09
J. Davis Values In Todays Economy And Society Speech 09 May09Jeff Davis
 
J. Davis Values In Todays Economy And Society Pres 09 May09
J. Davis Values In Todays Economy And Society Pres 09 May09J. Davis Values In Todays Economy And Society Pres 09 May09
J. Davis Values In Todays Economy And Society Pres 09 May09Jeff Davis
 
Lexicology Ii Wordformation
Lexicology Ii   WordformationLexicology Ii   Wordformation
Lexicology Ii Wordformationacostaena
 
Réseaux Sociaux : Quand le marketing et la DSI partagent leurs expériences.
Réseaux Sociaux : Quand le marketing et la DSI partagent leurs expériences. Réseaux Sociaux : Quand le marketing et la DSI partagent leurs expériences.
Réseaux Sociaux : Quand le marketing et la DSI partagent leurs expériences. Marie_Estager
 
Les regles des jeux concours sur Facebook
Les regles des jeux concours sur FacebookLes regles des jeux concours sur Facebook
Les regles des jeux concours sur FacebookWEBQAM
 
ENIB 2013-2014 - CAI Web #1: Côté navigateur 1/3
ENIB 2013-2014 - CAI Web #1: Côté navigateur 1/3ENIB 2013-2014 - CAI Web #1: Côté navigateur 1/3
ENIB 2013-2014 - CAI Web #1: Côté navigateur 1/3Horacio Gonzalez
 

Andere mochten auch (11)

Startup 101
Startup 101Startup 101
Startup 101
 
Social Projects
Social ProjectsSocial Projects
Social Projects
 
Helena Fotoss
Helena FotossHelena Fotoss
Helena Fotoss
 
New Product Development (NPD)
New Product Development (NPD)New Product Development (NPD)
New Product Development (NPD)
 
Atividades de inglês (interfaces Língua Portuguesa e Matemática)
Atividades de inglês (interfaces Língua Portuguesa e Matemática)Atividades de inglês (interfaces Língua Portuguesa e Matemática)
Atividades de inglês (interfaces Língua Portuguesa e Matemática)
 
J. Davis Values In Todays Economy And Society Speech 09 May09
J. Davis Values In Todays Economy And Society Speech 09 May09J. Davis Values In Todays Economy And Society Speech 09 May09
J. Davis Values In Todays Economy And Society Speech 09 May09
 
J. Davis Values In Todays Economy And Society Pres 09 May09
J. Davis Values In Todays Economy And Society Pres 09 May09J. Davis Values In Todays Economy And Society Pres 09 May09
J. Davis Values In Todays Economy And Society Pres 09 May09
 
Lexicology Ii Wordformation
Lexicology Ii   WordformationLexicology Ii   Wordformation
Lexicology Ii Wordformation
 
Réseaux Sociaux : Quand le marketing et la DSI partagent leurs expériences.
Réseaux Sociaux : Quand le marketing et la DSI partagent leurs expériences. Réseaux Sociaux : Quand le marketing et la DSI partagent leurs expériences.
Réseaux Sociaux : Quand le marketing et la DSI partagent leurs expériences.
 
Les regles des jeux concours sur Facebook
Les regles des jeux concours sur FacebookLes regles des jeux concours sur Facebook
Les regles des jeux concours sur Facebook
 
ENIB 2013-2014 - CAI Web #1: Côté navigateur 1/3
ENIB 2013-2014 - CAI Web #1: Côté navigateur 1/3ENIB 2013-2014 - CAI Web #1: Côté navigateur 1/3
ENIB 2013-2014 - CAI Web #1: Côté navigateur 1/3
 

Kürzlich hochgeladen

Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfhans926745
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesBoston Institute of Analytics
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 

Kürzlich hochgeladen (20)

Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 

Undercover PHP – Supporting PHP with non-web tools

  • 1. UNDERCOVER CODE Supporting PHP With Non-Web Tools Sean Coates (for ConFoo 2010, Montréal)
  • 2. WHAT WE’LL LEARN TODAY •Input/Output, Pipes, Redirection •Using Cron •Processing mail •Workers •Creating dæmons •Intentionally top-heavy
  • 3. UNIX PHILOSOPHY “ This is the Unix philosophy: Write programs that do one thing and do it well. Write programs to work together. Write programs to handle text streams, because that is a universal interface. ” –Doug McIlroy Creator of the Unix pipe
  • 4. UNIX PHILOSOPHY “ Write programs that do one thing and do it well. ” –Doug McIlroy Creator of the Unix pipe
  • 5. UNIX PHILOSOPHY “ Write programs to work together. ” –Doug McIlroy Creator of the Unix pipe
  • 6. UNIX PHILOSOPHY “Write programs to handle text streams, because that is a universal interface. ” –Doug McIlroy Creator of the Unix pipe
  • 7. UNIX PHILOSOPHY “Write programs to handle text streams, because that is a universal interface. –Doug McIlroy ” * Creator of the Unix pipe
  • 8. ASIDE: TEXT IS A •Theoretical * UNIVERSAL INTERFACE •From A Quarter Century of Unix (1994) (I think) •Read: before most people cared about Unicode •Unicode makes this less true
  • 9. ASIDE: TEXT IS A •Theoretical * UNIVERSAL INTERFACE •From A Quarter Century of Unix (1994) •Read: before most people cared about Unicode •Unicode makes this less true •…and by that, I mean painful
  • 10. ASIDE: TEXT IS A •Theoretical * UNIVERSAL INTERFACE •From A Quarter Century of Unix (1994) •Read: before most people cared about Unicode Photo: http://www.flickr.com/photos/guydonges/2826698176/ •Unicode makes this less true •…and by that, I mean painful •…and by that, I mean torture
  • 11. ASIDE: TEXT IS A UNIVERSAL INTERFACE •Theoretical * •From A Quarter Century of Unix (1994) •Read: before most people cared about Unicode Photo: http://www.flickr.com/photos/guydonges/2826698176/ •Unicode makes this less true •…and by that, I mean painful •…and by that, I mean torture •Rant: http://seancoates.com/utf-wtf
  • 12. ASIDE: TEXT IS A UNIVERSAL INTERFACE $ echo -n "25c" | wc -c 3 * $ echo -n "25¢" | wc -c 4 Photo: http://www.flickr.com/photos/guydonges/2826698176/ $ echo -n “25c” | wc -c -bash: $: command not found 0
  • 13. TEXT IS A * UNIVERSAL INTERFACE Let’s just assume this is true.
  • 14. WRITE PROGRAMS THAT DO ONE THING AND DO IT WELL. •Many Unixy utilities work like this: •wc - word count (character and line count, too) •sort - sorts input by line •uniq - remove duplicate lines, making output unique •tr - character translate •sed - stream editor •Unitaskers
  • 15. WRITE PROGRAMS TO WORK TOGETHER. •Simple tools = large toolbox •Unitaskers are only bad in the physical world •Unlimited toolbox size •(Busybox)
  • 16. WRITE PROGRAMS TO WORK TOGETHER. $ cat sounds.txt oink moo oink $ cat sounds.txt | uniq $ cat sounds.txt | sort | uniq oink moo moo oink oink
  • 17. WRITE PROGRAMS TO HANDLE TEXT STREAMS. •Power and simplicity for free •Great for simple data •Harder for highly structured data •Chaining is wonderfully powerful, and iterative
  • 18. WRITE PROGRAMS TO HANDLE TEXT STREAMS. $ cat /usr/share/dict/words | wc -l 234936 $ cat /usr/share/dict/words | grep '^f' | wc -l 6384 $ cat /usr/share/dict/words | grep '^f' | egrep '([aeiou])1' | wc -l 461
  • 19. TEXT STREAMS •“Standard” file descriptors •Input (#1) •Output (#1) •Error (#2) •fopen() returns a file descriptor •Redirection •Pipelining
  • 20. TEXT STREAMS: STANDARD OUTPUT $ echo -n "foo" Input Program Output (null) echo -n "foo" foo Console
  • 21. TEXT STREAMS: STANDARD INPUT $ php <?php Keyboard echo "woofn"; ctrl-d woof Input Program Output <?php php woof echo "woofn";
  • 22. REDIRECT STANDARD OUTPUT $ echo -n "foo" > bar.txt Input Program Output (null) echo -n "foo" foo bar.txt $ cat bar.txt foo
  • 23. REDIRECT STANDARD INPUT $ cat sounds.php <?php echo "oinkn"; echo "moon"; $ php sounds.php oink moo $ echo '<?php echo "woofn";' | php woof
  • 24. REDIRECT STANDARD INPUT $ php < sounds.php oink moo Input Program Output <?php echo "oinkn"; oink php echo "moon"; moo Console $ cat sounds.php | php
  • 25. PIPELINING $ echo -n "foo" | wc -c 3
  • 26. PIPELINING $ echo -n "foo" | wc -c 3 Input Program Output (null) echo -n "foo" foo Pipe foo wc -c 3 Console
  • 27. TEXT STREAMS: STANDARD ERROR $ cat sounds.txt oink moo oink $ grep moo sounds.txt moo Input Program Output (null) grep moo sounds.txt moo
  • 28. TEXT STREAMS: STANDARD ERROR $ grep moo nofile.txt grep: nofile.txt: No such file or directory Input Program Output Error (null) grep moo sounds.txt (null) grep: nofile.txt: No such file or directory
  • 29. TEXT STREAMS: STANDARD ERROR $ curl example.com <HTML> <HEAD> (etc.) $ curl example.com | grep TITLE <TITLE>Example Web Page</TITLE>
  • 30. TEXT STREAMS: STANDARD ERROR $ curl fake.example.com curl: (6) Couldn't resolve host 'fake.example.com' $ curl fake.example.com | grep TITLE curl: (6) Couldn't resolve host 'fake.example.com'
  • 31. TEXT STREAMS: STANDARD ERROR $ curl fake.example.com | grep TITLE curl: (6) Couldn't resolve host 'fake.example.com' Input Program Output Error curl: (6) Couldn't resolve host (null) curl fake.example.com (null) 'fake.example.com' Pipe Console (null) grep TITLE (null) (null) Console
  • 32. TEXT STREAMS (MORE ADVANCED) •tee •curl example.com | tee example.txt | grep TITLE •redirect stderr •curl fake.example.com 2 > error.log •combine streams •curl fake.example.com 2>&1 > combined.log •(assumes bash)
  • 33. WHY? •Much better languages to do this •Go to a Python talk •Reasons to use PHP: •existing code •existing talent •== low(er) development time, faster debugging
  • 34. CRON •Time-based job scheduler (Unixy) •Schedule is called a crontab •Each user can have a crontab •System has a crontab
  • 35. CRON $ crontab -l MAILTO=sean@seancoates.com 2 * * * * php blog-hourly.php Command Day of Week Month Day of Month Hour Minute
  • 36. CRON (SCHEDULING) * * * * * •Every minute 2 * * * * •On the 2nd minute of every hour */5 * * * * •Every 5 minutes 0 */2 * * * •Top of every 2nd Hour 0 0 * * 1 •Every Monday at midnight 15 20 9 2 * •Feb 9th at 8:15PM 15,45 * * * * •The 15th and 45th minute of every hour
  • 37. CRON (PATHS & PERMISSIONS) • Runs as the crontab’s owner * • (www-data, nobody, www, etc.) • Caution: web root permissions • Paths can be tricky • specify an explicit PATH • use explicit paths in commands
  • 38. CRON (EDITING) $ crontab -e (editor opens, save, exit) crontab: installing new crontab • Use the crontab -e mechanism • System launched $EDITOR to edit the file
  • 39. CRON (SYSTEM) • Often: /etc/crontab • Sixth schedule field: user ( m h dom m dow user cmd ) • Better for centralizing (e.g. for deployment and version control) • /etc/cron.d/* (daily, monthly, weekly, etc.) • Caution: avoid time-slam
  • 40. MAIL •Mail = headers + body •Body can contain many “parts” (as in MIME/multipart) •Multipurpose Internet Mail Extensions •MIME = much too complicated to discuss here •Sending mail is hard; so is receiving it •Focus on simple mail •Or let someone else do the hard parts
  • 41. MAIL •At its core, mail looks a bit like HTTP: •headers •key: value •blank line •body
  • 42. MAIL Return-Path: <sean@seancoates.com> X-Original-To: sean@seancoates.com Delivered-To: sean@caedmon.net Received: from localhost (localhost [127.0.0.1]) by iconoclast.caedmon.net (Postfix) with ESMTP id 2D9CC78406F for <sean@seancoates.com>; Mon, 8 Mar 2010 14:58:20 -0500 (EST) X-Virus-Scanned: Debian amavisd-new at iconoclast.caedmon.net Received: from iconoclast.caedmon.net ([127.0.0.1]) by localhost (iconoclast.caedmon.net [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id Hjx8HGZQ1RAY for <sean@seancoates.com>; Mon, 8 Mar 2010 14:58:14 -0500 (EST) Received: from [192.168.145.200] (unknown [24.2.2.2]) by iconoclast.caedmon.net (Postfix) with ESMTPSA id BAB3A78405F for <sean@seancoates.com>; Mon, 8 Mar 2010 14:58:14 -0500 (EST) From: Sean Coates <sean@seancoates.com> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Subject: Test Subject Date: Mon, 8 Mar 2010 14:55:50 -0500 Message-Id: <0B3DA593-3292-49C3-B3E6-4B4A26547421@seancoates.com> To: Sean Coates <sean@seancoates.com> Mime-Version: 1.0 (Apple Message framework v1077) X-Mailer: Apple Mail (2.1077) Test Body
  • 43. #!/usr/bin/env php MAIL <?php $mail = stream_get_contents(STDIN); // transpose possible CRLF: $mail = str_replace(array("rn", "r"), "n", $mail); list($tmpheaders, $body) = explode("nn", $mail, 2); $tmpheaders = preg_split(     "/n(S+):s+/",     "n" . $tmpheaders,     -1,     PREG_SPLIT_DELIM_CAPTURE ); $count = count($tmpheaders); $headers = array(); for ($i=1; $i<$count; $i+=2) {     $k = $tmpheaders[$i];     $v = $tmpheaders[$i+1];     if (isset($headers[$k])) {         $headers[$k] = (array)$headers[$k];         $headers[$k][] = $v;     } else {         $headers[$k] = $v;     } } var_dump($headers);
  • 44. MAIL array(14) { ["Return-Path"]=> string(21) "<sean@seancoates.com>" ["X-Original-To"]=> string(19) "sean@seancoates.com" ["Delivered-To"]=> string(16) "sean@caedmon.net" ["Received"]=> array(3) { [0]=> string(167) "from localhost (localhost [127.0.0.1]) by iconoclast.caedmon.net (Postfix) with ESMTP id 2D9CC78406F for <sean@seancoates.com>; Mon, 8 Mar 2010 14:58:20 -0500 (EST)" [1]=> string(212) "from iconoclast.caedmon.net ([127.0.0.1]) by localhost (iconoclast.caedmon.net [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id Hjx8HGZQ1RAY for <sean@seancoates.com>; Mon, 8 Mar 2010 14:58:14 -0500 (EST)" [2]=> string(174) "from [192.168.145.200] (unknown [24.2.2.2]) by iconoclast.caedmon.net (Postfix) with ESMTPSA id BAB3A78405F for <sean@seancoates.com>; Mon, 8 Mar 2010 14:58:14 -0500 (EST)" } (1/2 Continued…)
  • 45. MAIL (…2/2) ["X-Virus-Scanned"]=> string(44) "Debian amavisd-new at iconoclast.caedmon.net" ["From"]=> string(33) "Sean Coates <sean@seancoates.com>" ["Content-Type"]=> string(28) "text/plain; charset=us-ascii" ["Content-Transfer-Encoding"]=> string(4) "7bit" ["Subject"]=> string(12) "Test Subject" ["Date"]=> string(30) "Mon, 8 Mar 2010 14:55:50 -0500" ["Message-Id"]=> string(53) "<0B3DA593-3292-49C3-B3E6-4B4A26547421@seancoates.com>" ["To"]=> string(33) "Sean Coates <sean@seancoates.com>" ["Mime-Version"]=> string(35) "1.0 (Apple Message framework v1077)" ["X-Mailer"]=> string(19) "Apple Mail (2.1077)" }
  • 48. MAIL print_r($headers[$argv[1]]); $ cat test.mail | ./simplemail.php Subject Test Subject $ cat test.mail | ./simplemail.php Received Array ( [0] => from localhost (localhost [127.0.0.1]) by iconoclast.caedmon.net (Postfix) with ESMTP id 2D9CC78406F for <sean@seancoates.com>; Mon, 8 Mar 2010 14:58:20 -0500 (EST) [1] => from iconoclast.caedmon.net ([127.0.0.1]) by localhost (iconoclast.caedmon.net [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id Hjx8HGZQ1RAY for <sean@seancoates.com>; Mon, 8 Mar 2010 14:58:14 -0500 (EST) [2] => from [192.168.145.200] (unknown [24.2.2.2]) by iconoclast.caedmon.net (Postfix) with ESMTPSA id BAB3A78405F for <sean@seancoates.com>; Mon, 8 Mar 2010 14:58:14 -0500 (EST) )
  • 49. MAIL •Easier to just let King Wez handle it •Mailparse •http://pecl.php.net/mailparse •Also handles MIME
  • 51. ALIAS •How is this useful? (habari)$ cat /etc/aliases | grep security security: |"/var/spool/postfix/bin/security" •Beware: •chroots •allowed bin directories •newaliases •See your MTA’s docs on how to make this work.
  • 52. GEARMAN •Offload heavy processes from web machines •Synchronous or Asynchronous •Examples •Mail queueing •Image resize •Very configurable •(We’ll barely scratch the surface)
  • 53. GEARMAN web web web server server server gearmand worker worker worker worker
  • 54. GEARMAN web server gearmand worker worker worker worker
  • 55. GEARMAN web web web server server server gearmand worker
  • 56. GEARMAN web server gearmand worker
  • 57. GEARMAN web server gearmand worker (same hardware)
  • 58. GEARMAN web server gearmand worker (same hardware)
  • 59. GEARMAN WORKER #!/usr/bin/env php <?php require 'complicated_app/bootstrap.php'; $worker = new GearmanWorker(); $worker->addServer('127.0.0.1'); $worker->addFunction("send_invoice_mail", "send_mail"); function send_mail($to, $params) {     return ComplicatedApp::send_invoice_email(         $to,         $params['amount'],         $params['due']     ); }
  • 60. GEARMAN CLIENT // ... $client = new GearmanClient(); $client->addServer('127.0.0.1'); $task = $client->addTaskBackground(     'send_invoice_mail',     $params );
  • 61. DÆMONS •Long-running processes •Cron is a dæmon •Often socket-listeners •Screen •Supervisord •(X)Inetd, Launchctl
  • 62. DÆMONS SCREEN •Terminal multiplexer (multiple terminals from one console) •Screens persist between logins (doesn’t close on logout) •Useful for dæmons •A bit hackish
  • 63. DÆMONS SCREEN (sarcasmic)$ ssh adnagaporp.local (adnagaporp)$ screen -S demo (adnagaporp)$ php -r '$i=0; while(true) { echo ++$i . "n"; sleep(2); }' 1 2 3 4 5 ctrl-a d (adnagaporp)$ exit (sarcasmic)$
  • 64. DÆMONS SCREEN (sarcasmic)$ ssh adnagaporp.local (adnagaporp)$ screen -r demo (adnagaporp)$ php -r '$i=0; while(true) { echo ++$i . "n"; sleep(2); }' 1 2 3 4 5 6 7 8 9 10 11 …
  • 65. DÆMONS SCREEN •A bit crude •have to manually log in •no crash protection / respawn •no implicit logging •Doesn’t always play well with sudo or su •Does allow two terminals to control one screen •Very simple and easy to use •(see also tmux http://tmux.sourceforge.net/ )
  • 66. DÆMONS SUPERVISORD •Runs dæmons within a subsystem •Handles: •crashes •concurrency •logging •Friendly control interface
  • 67. DÆMONS SUPERVISORD phergie-brewbot.ini: [program:phergie-brewbot] command=/usr/local/bin/php Bot.php numprocs=1 directory=/home/phergie/Phergie-brewbot stdout_logfile=/home/phergie/Phergie-brewbot/phergie_supervisor.log autostart=true autorestart=true user=phergie
  • 69. DÆMONS INIT.D •Debian systems (Ubuntu, too), maybe others •/etc/init.d/* •/etc/rc*.d •update-rc.d •Use The Debian Way™ when on Debian
  • 70. DÆMONS LAUNCHCTL •Mac only •Similar to inetd/xinetd •Avoid writing socket code •Extremely simple to network
  • 71. DÆMONS LAUNCHCTL #!/usr/bin/env php <?php echo date('r') . "n";
  • 72. DÆMONS LAUNCHCTL <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/ PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Label</key> <string>localhost.demodaemon</string> <key>ProgramArguments</key> <array> <string>/path/to/demodaemon.php</string> </array> <key>inetdCompatibility</key> <dict> <key>Wait</key> <false/> </dict> <key>Sockets</key> <dict> <key>Listeners</key> <dict> <key>SockServiceName</key> <string>60001</string> <key>SockNodeName</key> <string>127.0.0.1</string> </dict> </dict> ~/Library/LaunchAgents/demodaemon.plist </dict> </plist>
  • 73. DÆMONS LAUNCHCTL $ launchctl load ~/Library/LaunchAgents/demodaemon.plist $ telnet localhost 60001 Mon, 08 Mar 2010 19:50:46 -0500 $
  • 74. OTHER NON-CONSOLE TRICKS / TOOLS •Subversion hook to lint (syntax check) code •IRC bot (see http://phergie.org/) •Twitter bot / interface (see @beerscore)
  • 75. QUESTIONS? •Always available to answer questions and to entertain strange ideas (-: •sean@seancoates.com •@coates •http://seancoates.com/ •Please comment: http://joind.in/1296 •…and see my talk on Friday: Interfacing with Twitter •Also: beer.