Script

Filter
2005-12-09

Validera Epost

Php
Validera Epost på serversidan med perl-regular expressions i PHP
// OBS: Uppdaterad 060926!
function validera_epost($epost) {
    if (!preg_match("/^([a-z0-9])+([a-z0-9\._-])*@([a-z0-9\._-])+[\.]([a-z]){2,4}$/i" , $epost)) {
        return false;
    }
    return true;
}

// Alternativ 2: 061229
function validateEmail($value) {
    // valid email formats: *@*.*, *@*.*.*, *.*@*.*, *.*@*.*.*)
    return (!eregi('^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$', $value)) ? 0 : 1;
} 
2005-11-14

Schemalägg uppgifter

Linux
Schemalägg uppgifter i Linux med Crontab
You can execute crontab if your name appears in the file /usr/lib/cron/cron.allow. If that file does not exist, you can use crontab if your name does not appear in the file /usr/lib/cron/cron.deny.
If only cron.deny exists and is empty, all users can use crontab. If neither file exists, only the root user can use crontab. The allow/deny files consist of one user name per line.

export EDITOR=vi ;to specify a editor to open crontab file.

crontab -e     Edit your crontab file, or create one if it doesn't already exist.
crontab -l      Display your crontab file.
crontab -r      Remove your crontab file.
crontab -v      Display the last time you edited your crontab file. (This option is only available on a few systems.) 

Vid ovanstående kommandon sparas en fil i /var/spool/cron/crontabs med ditt användarnamn. Annars kan man lägga ett skript i /etc/cron.daily mfl... eller redigera direkt i /etc/crontab:

# /etc/crontab
#
# m (0-59), 
# |     h (0-23), 
# |     |       d (1-31), 
# |     |       |       m (1-12), 
# |     |       |       |       dow (0-6 with 0=Sunday). 
# |     |       |       |       |       command(s)
# |     |       |       |       |       |
  */5   *       *       *       *       fetchmail -aKv -m "/usr/bin/procmail -d %T" >/dev/null 2>&1
2005-11-07

Mystery Permission Changes

Linux
There's a program in Mandrake called msec that does a regular security sweep of the system, repairing things it thinks are wrong.
One of the things it does at level 4 (which is what the instructions suggest you select)
is make the user home directories so the can't be ready by others (0700). Unfortunately,
you need the permissions set looser than that if you want to get personal web spaces
working (0711 to allow reading of ~/public_html).

To override this, create a file /etc/security/msec/perm.local and put this line to override the default permission settings:
/home/* current 711

The defaults are in /usr/share/msec/perm.4 for those interested.

Then, you should be able to chmod 0711 /home/* and have it stick.
2005-10-29

CGI i praktiken

Cgi
Sökformulär med databaskoppling i perl
#!/usr/bin/perl -wT

# Filens namn är /cgi-bin/script_html
# I Apaches config-fil:
# DirectoryIndex /cgi-bin/script2_html

use strict;
use DBI();
use CGI;
use CGI::Carp qw(fatalsToBrowser);

my($data, @args);

$data=<>;

if ($data) {
    &get_name($data);
} elsif ($ENV{'PATH_INFO'} eq "/whole_database") {
    $data = "xname=&sname=";
    &get_name();
} else {
    &ask_question;
}

sub print_header {
    print qq(content-type: text/html\n\n
    <html><head><title>$_[0]</title></head><body>);
}

sub print_bottom {
    print qq(</body></html>);
}

sub ask_question {
    &print_header("ask");
    print qq(<a href="/cgi-bin/script2_html/whole_database">
    Se HELA databasen...</a><br>
    <form method="post" action="/cgi-bin/script2_html/name">
    Name<br>
    <input name="xname"><br>
    Second name<br>
    <input name="sname"><br>
    <input type="submit">
    </form>);
    &print_bottom;
}

sub get_name {
    my ($t, @val, $ref, $dbm, $query, $xname, $sname, $sth, $rows);
    &print_header("get_name");
    $xname = "";
    $sname = "";
    @args = split(/&/, $data);
    foreach $t (@args) {
        @val = split(/=/, $t);
        if ($val[0] eq "xname") {
            $xname = $val[1] if ($val[1]);
        } elsif ($val[0] eq "sname") {
            $sname = $val[1] if ($val[1]);
        }
    }
    print qq(<table border="1">);
    $dbm = DBI->connect("DBI:mysql:database=people;host=localhost", 'webuser') or die "Kunde inte connecta";
    $query = qq(select xname, sname from people where xname like ? and sname like ?);
    $sth = $dbm->prepare($query) or die "Kunde inte preparera fråga: $!";
    $sth->execute("%$xname%", "%$sname%") or die "Kunde inte exekvera fråga: $dbm->errstr()<br>";
    $rows = $sth->rows;
    if (!$xname && !$sname) {
        print qq(<caption>The whole database</caption>);
    } else {
        print qq(<caption>The $rows people called $xname $sname</caption>);
    }
    print qq(<tr><th>First name</th><th>Last name</th></tr>);
    while ($ref = $sth->fetchrow_hashref) {
        print qq(<tr><td>$ref->{'xname'}</td><td>$ref->{'sname'}</td></tr>);
    }
    print qq(</table>);
    &print_bottom;
    $sth->finish;
    $dbm->disconnect;
} 
2005-10-28

Databaser med Perl

Cgi
Connecta en webbsida till en MySQL-databas i perl
#!/usr/bin/perl
use strict;
use DBI();
my ($dbm, $query, $xname, $sname, $sth, $rows, $ref);

print "content-type: text/html\n\n";

$dbm = DBI->connect("DBI:mysql:database=people;host=localhost", 'webuser') or die "Kunde inte kontakta databasen...";

# Avoiding SQL injects with placeholder...
$xname = "Anne";
$query = qq(select xname, sname from people where xname like ?);
$sth = $dbm->prepare($query) or die "Kunde inte preparera $query: $!";
$sth->execute("%$xname%");
$rows = $sth->rows;

print <<END_TOP;
<html>
<head>
<title>People</title>
</head>
<body>
<table border="1">
<caption>There are $rows people with names matching '$xname'</caption>
<tr>
<th>Surname</th>
<th>Last name</th>
</tr>
END_TOP

while ($ref = $sth->fetchrow_hashref) {
    print qq(<tr>\n<td>$ref->{'xname'}</td>\n<td>$ref->{'sname'}</td>\n</tr>\n);
}

print <<END_BOTTOM;
</table>
</body>
</html>
END_BOTTOM

$sth->finish;
$dbm->disconnect; 
🙂