Changeset - r5:a570202d2793
[Not reviewed]
v0.2 default
0 3 0
Wolfgang Scherer (ws) - 13 years ago 2012-03-30 16:13:40
wolfgang.scherer@gmx.de
index.php: Umlaut and HTML-escaping fixed. Display enhancement.
lib/util.php: vacation_split/vacation_join functions to centralize the gory details.
Message `Content-Type: text/plain; charset="utf-8"'
and `Content-Transfer-Encoding: quoted-printable' added.
lib/language.php: Add administrator manual headline.
3 files changed with 99 insertions and 20 deletions:
0 comments (0 inline, 0 general)
index.php
Show inline comments
 
<?php // -*- php -*-
 
// Copyright (C) 2012, Wolfgang Scherer, <Wolfgang.Scherer at gmx.de>
 
// Sponsored by WIEDENMANN SEILE GMBH, http://www.wiedenmannseile.de
 
//
 
// This file is part of Wiedenmann Vacation.
 
//
 
// This program is free software; you can redistribute it and/or modify
 
// it under the terms of the GNU General Public License as published
 
// by the Free Software Foundation; either version 3 of the License, or
 
// (at your option) any later version.
 
//
 
// This program is distributed in the hope that it will be useful, but
 
// WITHOUT ANY WARRANTY; without even the implied warranty of
 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
// General Public License for more details.
 
//
 
// You should have received a copy of the GNU General Public License
 
// along with this program. If not, see <http://www.gnu.org/licenses/>,
 
// or write to Wolfgang Scherer, <Wolfgang.Scherer at gmx.de>
 

	
 
// $_REQUEST['_DEBUG_'] = 1;
 

	
 
// --------------------------------------------------
 
// |||:sec:||| Configuration
 
// --------------------------------------------------
 
require_once(dirname(__FILE__) . '/lib/util.php');
 
require_once(dirname(__FILE__) . '/lib/template.php');
 

	
 
// _DEBUG_ = 1 normal debugging
 
// _DEBUG_ = 2 turn off administrator rights
 
// _DEBUG_ = 3 provoke error #1
 
// _DEBUG_ = 4 provoke error #2
 
if (isset($_REQUEST['_DEBUG_'])) {
 
    $_debug = $_REQUEST['_DEBUG_'];
 
    if (empty($_debug)) {
 
        $_debug = 1;
 
    }
 
} else {
 
    $_debug = 0;
 
}
 

	
 
file_put_contents('/tmp/xxxxxxx', 'check');
 
system('touch /tmp/xxxxxx1');
 

	
 
// --------------------------------------------------
 
// |||:sec:||| Setup
 
// --------------------------------------------------
 

	
 
if (isset($_SERVER["REMOTE_USER"])) {
 
    $remote_user = $_SERVER["REMOTE_USER"];
 
} else {
 
    $remote_user = '';
 
}
 
if (isset($_REQUEST["user"])) {
 
    $request_user = $_REQUEST["user"];
 
} else {
 
    $request_user = '';
 
}
 

	
 
$is_admin = in_array($remote_user, $ADMIN_USERS);
 
if ($_debug > 1) {
 
    $is_admin = False;
 
}
 

	
 
$users = get_users();
 
$user_names = array_keys($users);
 

	
 
if (!$is_admin) {
 
    $user = $remote_user;
 
} else {
 
    $user = $request_user;
 
    if (empty($user)) {
 
        $user = $remote_user;
 
    }
 
}
 

	
 
// --------------------------------------------------
 
// |||:sec:||| HTML Header
 
// --------------------------------------------------
 

	
 
$title = get_text('title');
 
$css = '
 
span.label {
 
    width: 100px;
 
    display: inline-block;
 
    vertical-align: top;
 
}
 
hr.sep {
 
    width: 550px;
 
    text-align: left;
 
    margin-left: 0;
 
}
 
';
 

	
 
echo substitute_elements(
 
    $HEAD, Array(
 
        'title' => $title,
 
        'css' => $css,
 
        ));
 
hl($title);
 

	
 
// --------------------------------------------------
 
// |||:sec:||| Error handling (User)
 
// --------------------------------------------------
 

	
 
if ($_debug > 2) {
 
    $user = '';                 // error #1
 
}
 
if ($_debug > 3) {
 
    $user = 'unknown';          // error #2
 
}
 

	
 
if (empty($user)) {
 
    error_msg(get_text('error_no_user'));
 
    echo $FOOT;
 
    exit(0);
 
}
 

	
 
if (!$is_admin && !in_array($user, $user_names)) {
 
    error_msg(sprintf('%s (%s)!', get_text('error_no_user'), $user));
 
    echo $FOOT;
 
    exit(0);
 
}
 

	
 
// --------------------------------------------------
 
// |||:sec:||| Setup HOME/vacation(1)
 
// --------------------------------------------------
 

	
 
$home = $users[$user][1];
 

	
 
if ($_debug) {
 
    echo ('<pre>'."\n");            // |:debug:|
 
    echo ('user: ' . $user . "\n");
 
    echo ('home: ' . $home . "\n");
 
    echo ('</pre>'."\n");
 
}
 

	
 
$vacation_msg_file = sprintf('%s/%s', $home, $VACATION_MSG_FILE);
 
$vacation_db_file = sprintf('%s/%s', $home, $VACATION_DB_FILE);
 
$forward_file = sprintf('%s/%s', $home, $FORWARD_FILE);
 
$forward_active = file_exists($forward_file);
 
$forward_settings = Array(
 
    '\\'.$user, '', sprintf($VACATION_FORWARD_ENTRY, $user));
 
if ($forward_active)
 
{
 
    $forward_settings = file_get_contents($forward_file);
 
    $lines = explode("\n", $forward_settings);
 
    $forward_settings = preg_split('/, */', $lines[0]);
 
    if (count($forward_settings) < 3) {
 
        $forward_settings[2] = $forward_settings[1];
 
        $forward_settings[1] = '';
 
    }
 
}
 
$vacation_forward = $forward_settings[1];
 
$vacation_forward = preg_replace('/^\\\\/', '', $vacation_forward);
 

	
 
if (!file_exists($vacation_msg_file)) {
 
    $vacation_msg = get_text('vacation');
 
    $decode = False;
 
} else {
 
    $vacation_msg = file_get_contents($vacation_msg_file);
 
    $decode = True;
 
}
 

	
 
$lines = explode("\n", $vacation_msg);
 
$vacation_subject = array_shift($lines);
 
$vacation_subject = preg_replace('/^[Ss]ubject: */', '', $vacation_subject);
 
while (True) {
 
    $line = array_shift($lines);
 
    if (empty($line)) {
 
        break;
 
    }
 
}
 
$vacation_body = implode($lines, "\n");
 
// $lines = explode("\n", $vacation_msg);
 
// $vacation_subject = array_shift($lines);
 
// $vacation_subject = preg_replace('/^[Ss]ubject: */', '', $vacation_subject);
 
// $vacation_headers = Array();
 
// // remove additional headers
 
// while (True) {
 
//     $line = array_shift($lines);
 
//     if (empty($line)) {
 
//         break;
 
//     }
 
//     $vacation_headers[] = $line;
 
// }
 
// $vacation_body = implode($lines, "\n");
 

	
 
$vacation_parts = vacation_split($vacation_msg, $decode);
 
$vacation_subject = $vacation_parts[0];
 
$vacation_headers = $vacation_parts[1];
 
$vacation_body = $vacation_parts[2];
 

	
 
// --------------------------------------------------
 
// |||:sec:||| Set vacation(1) parameters
 
// --------------------------------------------------
 

	
 
$vacation_setup = isset($_REQUEST['setzen']);
 
$vacation_clear = isset($_REQUEST['loeschen']);
 
if ($vacation_setup || $vacation_clear) {
 
    $forward_active = isset($_REQUEST['aktiv']);
 
    $vacation_forward = isset($_REQUEST['forward']) ? $_REQUEST['forward'] : '';
 
    if ($vacation_forward == $user) {
 
        $vacation_forward = '';
 
    }
 
    $vacation_subject = isset($_REQUEST['subject']) ? trim($_REQUEST['subject']) : '';
 
    $vacation_body = isset($_REQUEST['body']) ? trim($_REQUEST['body']) : '';
 
    $forward_settings = Array(
 
        '\\'.$user, $vacation_forward,
 
        sprintf($VACATION_FORWARD_ENTRY, $user));
 

	
 
    while (True) {
 
        if ($vacation_clear) {
 
            system(sprintf(
 
                       '%s -u %s %s %s -i',
 
                       $SUDO_CMD, $SUDO_OPT_H, $user, $VACATION_CMD));
 
                       $SUDO_CMD, $user, $SUDO_OPT_H, $VACATION_CMD));
 
            break;
 
        }
 
        if (empty ($vacation_subject)) {
 
            error_msg(get_text('error_no_subject'));
 
            break;
 
        }
 
        // Actually set it
 

	
 
        // If not active, remove "Vertretung"
 
        if (!$forward_active) {
 
            $vacation_forward = '';
 
        }
 
        $forward_settings = Array(
 
            '\\'.$user, $vacation_forward,
 
            sprintf($VACATION_FORWARD_ENTRY, $user));
 

	
 
        // Prepare vacation(1) message
 
        $vacation_msg = sprintf(
 
            "Subject: %s\n\n%s\n", $vacation_subject, $vacation_body);
 
        $vacation_msg = vacation_join($vacation_subject, $vacation_body);
 
        $vacation_msg_pipe = popen(sprintf(
 
                  '%s -u %s %s %s %s',
 
                  $SUDO_CMD, $user, $SUDO_OPT_H, $WRITE_TO_CMD, $vacation_msg_file),
 
            'w');
 
        fwrite($vacation_msg_pipe, $vacation_msg);
 
        pclose($vacation_msg_pipe);
 

	
 
        // Initialize vacation(1) if necessary.
 
        if (!file_exists($vacation_db_file)) {
 
            system(sprintf(
 
                       '%s -u %s %s %s -i',
 
                       $SUDO_CMD, $user, $SUDO_OPT_H, $VACATION_CMD));
 
        }
 

	
 
        // Write or remove .forward file
 
        if ($forward_active) {
 
            $use_fs = Array();
 
            foreach ($forward_settings as $fs) {
 
                if (empty($fs)) {
 
                    continue;
 
                }
 
                $use_fs[] = $fs;
 
            }
 
            $forward_file_pipe = popen(sprintf(
 
                      '%s -u %s %s %s %s',
 
                      $SUDO_CMD, $user, $SUDO_OPT_H, $WRITE_TO_CMD, $forward_file),
 
                'w');
 
            fwrite($forward_file_pipe, implode(", ", $use_fs)."\n");
 
            pclose($forward_file_pipe);
 
        } else {
 
            system(sprintf(
 
                       '%s -u %s %s /bin/rm -f %s',
 
                       $SUDO_CMD, $user, $SUDO_OPT_H, $forward_file));
 
        }
 
        break;
 
    }
 
}
 

	
 
// echo ('<pre>'."\n");            // |:debug:|
 
// echo ($vacation_forward);
 
// var_dump($forward_settings);
 
// echo ('</pre>'."\n");
 

	
 
// --------------------------------------------------
 
// |||:sec:||| User Interface
 
// --------------------------------------------------
 

	
 
$form = Array();
 
//$form[] = '<hr class="sep"/>';
 
$form[] = '<form action="index.php" method="post">';
 

	
 
if ($_debug) {
 
    $form[] = sprintf(
 
        '<input type="hidden" name="_DEBUG_" value="%s" />', $_debug);
 
}
 

	
 
// column names
 
$form[] = '<span class="label"></span>';
 
$form[] = '<span style="display: inline-block;"><table border="0" cellspacing="0" cellpadding="2" style="width: 410px;">';
 
$form[] = '<span class="label">';
 
$form[] = sprintf('<input type="submit" name="holen" value="%s" />', get_text('refresh'));
 
$form[] = '</span>';
 
$form[] = '<span style="display: inline-block;"><table border="0" cellspacing="0" cellpadding="2" style="width: 410px; vertical-align: top;">';
 
$trow = Array();
 
$trow[] = get_text('user');
 
$trow[] = get_text('forward_to');
 
$trow[] = get_text('active');
 
$form[] = sprintf('<tr><th style="text-align: center;">&nbsp;%s&nbsp;</th></tr>', implode($trow, '&nbsp;</th><th style="text-align: center;">&nbsp;'));
 

	
 
$trow = Array();
 

	
 
// user
 
$user_control = Array();
 
if ($is_admin) {
 
    $user_control[] = '<select name="user" />';
 
    foreach ($user_names as $sel_user) {
 
        if ($sel_user == $user) {
 
            $selected = ' selected="selected"';
 
        } else {
 
            $selected = '';
 
        }
 
        $user_control[] = sprintf('<option%s>%s</option>', $selected, $sel_user);
 
    }
 
    $user_control[] = '</select>';
 
    $user_control = implode($user_control, "\n");
 
} else {
 
    $user_control = sprintf(
 
        '<input type="hidden" name="user" value="%s" />%s', $user, $user);
 
}
 
$trow[] = $user_control;
 

	
 
// forward
 
$user_control = Array();
 
$user_control[] = '<select name="forward" />';
 
$user_control[] = sprintf('<option value="">%s</option>', get_text('none'));
 
$selected = '';
 
foreach ($user_names as $sel_user) {
 
    if (!$is_admin && $sel_user == $user) {
 
        continue;
 
    }
 
    if ($sel_user == $vacation_forward) {
 
        $selected = ' selected="selected"';
 
    } else {
 
        $selected = '';
 
    }
 
    $user_control[] = sprintf('<option%s>%s</option>', $selected, $sel_user);
 
}
 
$user_control[] = '</select>';
 
$user_control = implode($user_control, "\n");
 
$trow[] = $user_control;
 

	
 
// active checkbox
 
$user_control = Array();
 
if ($forward_active) {
 
    $active_checked = ' checked="checked"';
 
} else {
 
    $active_checked = '';
 
}
 
$user_control[] = sprintf('<input type="checkbox" name="aktiv" value="aktiv"%s><br />',
 
                          $active_checked);
 
$user_control = implode($user_control, "\n");
 
$trow[] = $user_control;
 

	
 
$form[] = sprintf('<tr><td style="text-align: center;">%s</td></tr>', implode($trow, '</td><td style="text-align: center;">'));
 
$form[] = '</table></span><br />';
 

	
 
// subject
 
$form[] = sprintf('<span class="label">%s<sup>*</sup>:</span>', get_text('subject'));
 
$form[] = (
 
    '<input type="text" name="subject" value="' . $vacation_subject
 
    '<input type="text" name="subject" value="' . htmlspecialchars($vacation_subject, ENT_COMPAT, "UTF-8")
 
    .'" size="49" /><br />');
 

	
 
// message
 
$form[] = sprintf('<span class="label">%s<sup>*</sup>:</span>', get_text('message'));
 
$form[] = (
 
    '<textarea name="body" cols="56" rows="10"'
 
    .' style="vertical-align: top;">' . $vacation_body . '</textarea><br />');
 
    .' style="vertical-align: top;">' . htmlspecialchars($vacation_body, ENT_NOQUOTES, "UTF-8") . '</textarea><br />');
 

	
 
$form[] = '<span class="label">&nbsp;</span>';
 
$form[] = (
 
    '<span style="display: inline-block; width: 410px;"><sup>*</sup>'
 
    . get_text('explain_subject')
 
    .'</span><br />');
 

	
 
// actions
 
$form[] = '<hr class="sep"/>';
 
$form[] = '<span class="label">&nbsp;</span>';
 
$form[] = sprintf('<input type="submit" name="holen" value="%s" />', get_text('refresh'));
 
$form[] = sprintf('<input type="submit" name="setzen" value="%s" /><br />', get_text('save'));
 
$form[] = '<hr class="sep"/>';
 

	
 
// --------------------------------------------------
 
// |||:sec:||| Vacation History
 
// --------------------------------------------------
 

	
 
$vacation_list_cmd = sprintf(
 
    '%s -u %s %s %s -l', $SUDO_CMD, $user, $SUDO_OPT_H, $VACATION_CMD);
 
// echo ('<pre>'."\n");
 
// echo ($vacation_list_cmd);
 
// echo ('</pre>'."\n");
 
$vacation_ausgabe = popen ($vacation_list_cmd, 'r');
 
$vacation_list = stream_get_contents($vacation_ausgabe);
 
pclose($vacation_ausgabe);
 

	
 
if (!empty ($vacation_list)) {
 
    $form[] = shl(get_text('reply_history'));
 
    $form[] = '<hr class="sep"/>';
 
    // $form[] = '<hr class="sep"/>';
 
    $form[] = '<pre>';
 
    $form[] = $vacation_list;
 
    $form[] = '</pre>';
 
    $form[] = '<hr class="sep"/>';
 
    $form[] = '<span class="label">&nbsp;</span>';
 
    $form[] = sprintf('<input type="submit" name="loeschen" value="%s" /><br />', get_text('delete'));
 
    if ($is_admin) {
 
        echo ('<hr class="sep"/>');
 
        $form[] = ('<hr class="sep"/>');
 
    }
 
} else {
 
    $form[] = shl(get_text('reply_history_empty'));
 
}
 

	
 
$form[] = '</form>';
 
echo implode($form, "\n");
 

	
 
// --------------------------------------------------
 
// |||:sec:||| Administrator Manual
 
// --------------------------------------------------
 

	
 
if ($is_admin) {
 
    echo shl(get_text('manual_headline'));
 
    echo (sprintf('<a href="%s">%s</a><br />'."\n", get_text('manual_link'), get_text('manual')));
 
}
 

	
 
// --------------------------------------------------
 
// |||:sec:||| Debug
 
// --------------------------------------------------
 

	
 
if ($_debug) {
 
    echo "<pre>\n";                 // |:debug:|
 
    echo "\$_REQUEST: \n";
 
    var_dump($_REQUEST);
 
    echo "</pre>\n";
 

	
 
    echo "\$_SERVER: \n";
 
    echo "<pre>\n";                 // |:debug:|
 
    var_dump($_SERVER);
 
    echo "</pre>\n";
 
}
 

	
 
echo $FOOT;
 

	
 
//
 
// :ide-menu: Emacs IDE Menu - Buffer @BUFFER@
 
// . M-x `eIDE-menu' ()(eIDE-menu "z")
 
// :ide: COMPILE: PHP _DEBUG_=2 _DEBUG_TEST_=2
 
// . (compile (concat "php " (file-name-nondirectory (buffer-file-name)) " _DEBUG_=2 _DEBUG_TEST_=2"))
 

	
 
// :ide: QUO: $this->
 
// . (insert "$this->" )
 

	
 
// :ide: COMPILE: PHP w/o args
 
// . (compile (concat "php " (file-name-nondirectory (buffer-file-name)) ""))
 

	
 
// :ide: COMPILE: PHP _DEBUG_=1 _DEBUG_TEST_=1
 
// . (compile (concat "php " (file-name-nondirectory (buffer-file-name)) " _DEBUG_=1 _DEBUG_TEST_=1"))
 

	
 
//
 
// Local Variables:
 
// mode: php
 
// End:
 
?>
lib/language.php
Show inline comments
 
<?php // -*- php -*-
 
// Copyright (C) 2012, Wolfgang Scherer, <Wolfgang.Scherer at gmx.de>
 
// Sponsored by WIEDENMANN SEILE GMBH, http://www.wiedenmannseile.de
 
//
 
// This file is part of Wiedenmann Vacation.
 
//
 
// This program is free software; you can redistribute it and/or modify
 
// it under the terms of the GNU General Public License as published
 
// by the Free Software Foundation; either version 3 of the License, or
 
// (at your option) any later version.
 
//
 
// This program is distributed in the hope that it will be useful, but
 
// WITHOUT ANY WARRANTY; without even the implied warranty of
 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
// General Public License for more details.
 
//
 
// You should have received a copy of the GNU General Public License
 
// along with this program. If not, see <http://www.gnu.org/licenses/>,
 
// or write to Wolfgang Scherer, <Wolfgang.Scherer at gmx.de>
 

	
 
// $_REQUEST['_DEBUG_'] = 1;
 

	
 
$LC_LANGUAGES = Array(
 
    'en' => Array(
 
        'vacation' => 'Subject: Re: $SUBJECT
 

	
 
I am currently not able to read mail.
 

	
 
Your message has been forwarded to a co-worker for processing.
 
',
 
        'error_no_subject' => 'error: subject is empty!',
 
        'error_no_user' => 'error: no user specified!',
 
        'error_unknown_user' => 'error: unkown user',
 

	
 
        'title' => 'Email Vacation Settings',
 
        'user' => 'User',
 
        'forward_to' => 'Forward To',
 
        'active' => 'Active',
 
        'none' => 'none',
 
        'subject' => 'Subject',
 
        'message' => 'Message',
 
        'explain_subject' =>
 
        ' The expression'
 
        .' <em>$SUBJECT</em> is replaced with the subject of the incoming message.',
 
        'refresh' => 'Refresh',
 
        'save' => 'Save',
 

	
 
        'reply_history_empty' => 'Autoreply History Is Empty',
 
        'reply_history' => 'Autoreply History',
 
        'delete' => 'Delete',
 

	
 
        'manual_headline' => 'Administrator Manual',
 
        'manual' => 'Manual',
 
        'manual_link' => 'doc/README.html',
 
        // |:here:|
 
        ),
 
    'de' => Array(
 
        'vacation' => 'Subject: Re: $SUBJECT
 

	
 
Ich bin leider vorübergehend nicht per Email erreichbar.
 

	
 
Ihre Mail wird zur Bearbeitung an meine Vertretung weitergeleitet.
 
',
 
        'error_no_subject' => 'Fehler: Betreff ist leer!',
 
        'error_no_user' => 'Fehler: Benutzer kann nicht ermittelt werden!',
 
        'error_unknown_user' => 'Fehler: unbekannter Benutzer',
 

	
 
        'title' => 'Email Urlaubseinstellungen',
 

	
 
        'user' => 'Benutzer',
 
        'forward_to' => 'Vertretung',
 
        'active' => 'Aktiv',
 
        'none' => 'niemand',
 
        'subject' => 'Betreff',
 
        'message' => 'Nachricht',
 
        'explain_subject' =>
 
        ' Der Ausdruck'
 
        .' <em>$SUBJECT</em> wird durch den Betreff der eingehenden Email'
 
        .' ersetzt.',
 
        'refresh' => 'Neu einlesen',
 
        'save' => 'Speichern',
 

	
 
        'reply_history_empty' => 'Keine automatisch beantworteten Emails',
 
        'reply_history' => 'Automatisch  Beantwortete Emails',
 
        'delete' => 'Löschen',
 

	
 
        'manual_headline' => 'Administratorhandbuch',
 
        'manual' => 'Anleitung',
 
        'manual_link' => 'doc/README-de.html',
 
        // |:here:|
 
        ),
 
    );
 

	
 
//
 
// :ide-menu: Emacs IDE Menu - Buffer @BUFFER@
 
// . M-x `eIDE-menu' ()(eIDE-menu "z")
 
// :ide: COMPILE: PHP _DEBUG_=2 _DEBUG_TEST_=2
 
// . (compile (concat "php " (file-name-nondirectory (buffer-file-name)) " _DEBUG_=2 _DEBUG_TEST_=2"))
 

	
 
// :ide: QUO: $this->
 
// . (insert "$this->" )
 

	
 
// :ide: COMPILE: PHP w/o args
 
// . (compile (concat "php " (file-name-nondirectory (buffer-file-name)) ""))
 

	
 
// :ide: COMPILE: PHP _DEBUG_=1 _DEBUG_TEST_=1
 
// . (compile (concat "php " (file-name-nondirectory (buffer-file-name)) " _DEBUG_=1 _DEBUG_TEST_=1"))
 

	
 
//
 
// Local Variables:
 
// mode: php
 
// End:
 
?>
lib/util.php
Show inline comments
 
<?php // -*- php -*-
 
// Copyright (C) 2012, Wolfgang Scherer, <Wolfgang.Scherer at gmx.de>
 
// Sponsored by WIEDENMANN SEILE GMBH, http://www.wiedenmannseile.de
 
//
 
// This file is part of Wiedenmann Vacation.
 
//
 
// This program is free software; you can redistribute it and/or modify
 
// it under the terms of the GNU General Public License as published
 
// by the Free Software Foundation; either version 3 of the License, or
 
// (at your option) any later version.
 
//
 
// This program is distributed in the hope that it will be useful, but
 
// WITHOUT ANY WARRANTY; without even the implied warranty of
 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
// General Public License for more details.
 
//
 
// You should have received a copy of the GNU General Public License
 
// along with this program. If not, see <http://www.gnu.org/licenses/>,
 
// or write to <Wolfgang.Scherer at gmx.de>
 

	
 
// $_REQUEST['_DEBUG_'] = 1;
 
// $_REQUEST['_DEBUG_TEST_'] = 1;
 
require_once(dirname(__FILE__) . '/../lib/config.php');
 
require_once(dirname(__FILE__) . '/../lib/language.php');
 

	
 
// --------------------------------------------------
 
// |||:sec:||| Benutzer
 
// --------------------------------------------------
 

	
 
function get_users()
 
{
 
    global $PASSWD_FILE;
 
    global $USER_ID_MIN;
 
    global $USER_ID_MAX;
 
    global $ALLOWED_USERS;
 
    global $INVALID_USERS;
 
    global $HOME_PFX;
 

	
 
    $passwd = file_get_contents($PASSWD_FILE);
 
    $lines = explode("\n", $passwd);
 
    $users = Array();
 
    //var_dump($lines);
 
    foreach ($lines as $line) {
 
        if (empty($line)) {
 
            continue;
 
        }
 
        $fields = explode(':', $line);
 
        $user = $fields[0];
 
        $uid = $fields[2];
 
        $home = $fields[5];
 

	
 
        // uncoditionally accept allowed users
 
        if (!in_array($user, $ALLOWED_USERS)) {
 
            if ($uid < $USER_ID_MIN || $uid > $USER_ID_MAX) {
 
                // skip users with a user id outside the allowed range
 
                continue;
 
            }
 
            $home_pos = strpos($home, $HOME_PFX);
 
            if ($home_pos === false || $home_pos > 0) {
 
                // skip users without a HOME directory
 
                continue;
 
            }
 
            if (in_array($user, $INVALID_USERS)) {
 
                // skip invalid users
 
                continue;
 
            }
 
        }
 
        $users[$user] = Array($user, $home);
 
    }
 
    ksort($users);
 
    return $users;
 
}
 

	
 
function make_htpasswd()
 
{
 
    global $SHADOW_FILE;
 
    global $HTPASSWD_FILE;
 
    $users = get_users();
 
    $user_names = array_keys($users);
 
    $shadow = file_get_contents($SHADOW_FILE);
 
    $lines = explode("\n", $shadow);
 
    $pw_ent = Array();
 
    //var_dump($lines);
 
    foreach ($lines as $line) {
 
        if (empty($line)) {
 
            continue;
 
        }
 
        $fields = explode(':', $line);
 
        $user = $fields[0];
 
        $pass = $fields[1];
 
        if (in_array($user, $user_names)) {
 
            $pw_ent[] = sprintf('%s:%s', $user, $pass);
 
        }
 
    }
 
    $htpwd = fopen($HTPASSWD_FILE, 'w');
 
    fwrite($htpwd, implode($pw_ent, "\n"));
 
    fclose($htpwd);
 
}
 

	
 
// --------------------------------------------------
 
// |||:sec:||| substitute_elements
 
// --------------------------------------------------
 

	
 
// |:check:| syntax error on PHP 5.2.x (mecki)
 
// class substitute_elements_check
 
// {
 
//     const _doc_ = <<<'DOC'
 
// Markierungen in String ersetzen.
 

	
 
// function substitute_elements ($string, $substitutions=array(), $keep_unknown=NULL, $pfx='@', $sfx=NULL)
 

	
 
// Falls eine Markierung gefunden wurde, die nicht in $substitutions
 
// enthalten ist, wird sie durch einen leeren String ersetzt, falls
 
// $keep_unknown == false, andernfalls wird die Markierung
 
// unverändert beibehalten.
 

	
 
// Ist $keep_unknown == NULL (Standardwert), dann wird es auf
 
// isset($_REQUEST['_DEBUG_']) gesetzt. Im Debug-Modus werden die
 
// Markierungen also beibehalten im Normal-Modus nicht.
 
// DOC;
 
//     const _check_ = <<<'CHECK'
 
// global $BRNL;
 

	
 
// $string = 'Eine @Ersetzung@ an @dieser@ Stelle.' . $BRNL;
 

	
 
// $substitutions = array(
 
//     'Ersetzung' => 'neue Sache',
 
//     );
 

	
 
// echo substitute_elements($string, $substitutions, True);
 
// echo substitute_elements($string, $substitutions, False);
 
// CHECK;
 
//     static $_check_output_;
 
// }
 

	
 
function substitute_elements (
 
    $string, $substitutions=array(), $keep_unknown=NULL, $pfx='@', $sfx=NULL)
 
{
 
    if ( !isset($keep_unknown) )
 
    {
 
        $keep_unknown = isset($_REQUEST['_DEBUG_']);
 
    }
 

	
 
    if ( !isset($sfx) )
 
    {
 
        $sfx = $pfx;
 
    }
 

	
 
    $subst_rx = $pfx . '([^' . substr($sfx, 0, 1) . ']+)' .$sfx;
 

	
 
    $parts = preg_split('/' . $subst_rx . '/', $string);
 
    //hline();
 
    //$d->c()->kva($parts)->p();
 

	
 
    $subst_keys = Array();
 
    preg_match_all('/' . $subst_rx . '/', $string,
 
                   $subst_keys, PREG_PATTERN_ORDER);
 
    $subst_keys = $subst_keys[1];
 
    //hline();
 
    //$d->c()->kva($subst_keys)->p();
 

	
 
    $result = Array();
 
    $indx = 0;
 
    foreach ($parts as $part)
 
    {
 
        // pdkv('// $part '."$indx", $part);
 
        $result[] = $part;
 
        if ( array_key_exists($indx, $subst_keys))
 
        {
 
            $subst_key = $subst_keys[$indx];
 
            if (array_key_exists($subst_key, $substitutions))
 
            {
 
                $result[] = $substitutions[$subst_key];
 
            }
 
            else if ( $keep_unknown )
 
            {
 
                $result[] = $pfx . $subst_key . $sfx;
 
            }
 
            // pdkv('// $subst_key', $subst_key);
 
        }
 
        $indx += 1;
 
    }
 
    $result = implode('', $result);
 
    return $result;
 
}
 

	
 
// --------------------------------------------------
 
// |||:sec:||| Language Support
 
// --------------------------------------------------
 

	
 
if (!array_key_exists($LANGUAGE, $LC_LANGUAGES)) {
 
    $LANGUAGE = 'en';
 
}
 
$LC_MESSAGES_EN = $LC_LANGUAGES['en'];
 
$LC_MESSAGES = $LC_LANGUAGES[$LANGUAGE];
 

	
 
if ( isset($_SERVER['HTTP_ACCEPT_LANGUAGE']))
 
{
 
    $accept_language = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
 
    $accept_language = preg_replace('/;[^,;]*/', '', $accept_language);
 
    $preferred_languages = explode(',', $accept_language);
 
    // if (isset($_REQUEST['_DEBUG_'])) {
 
    //     var_dump($preferred_languages);
 
    // }
 
    foreach ($preferred_languages as $plang) {
 
        if (array_key_exists($plang, $LC_LANGUAGES)) {
 
            $LC_MESSAGES = $LC_LANGUAGES[$plang];
 
            break;
 
        }
 
    }
 
}
 

	
 
function get_text($text)
 
{
 
    global $LC_MESSAGES_EN;
 
    global $LC_MESSAGES;
 
    if ( !array_key_exists($text, $LC_MESSAGES))
 
    {
 
        if ( array_key_exists($text, $LC_MESSAGES_EN) )
 
        {
 
            return $LC_MESSAGES_EN[$text];
 
        }
 
        return $text;
 
    }
 
    return $LC_MESSAGES[$text];
 
}
 

	
 
// --------------------------------------------------
 
// |||:sec:||| Überschrift/Fehlermeldung
 
// --------------------------------------------------
 

	
 
function shl($message)
 
{
 
    return sprintf('<h3>%s</h3>', $message);
 
}
 

	
 
function hl($message)
 
{
 
    echo shl($message)."\n";
 
}
 

	
 
function error_msg($message)
 
{
 
    echo sprintf('<span class="error"><strong>%s</strong></span>'."\n", $message);
 
}
 

	
 
// --------------------------------------------------
 
// |||:sec:||| vaccation(1) message
 
// --------------------------------------------------
 

	
 
function vacation_split($message, $decode)
 
{
 
    $lines = explode("\n", $message);
 
    $subject = array_shift($lines);
 
    $subject = preg_replace('/^[Ss]ubject: */', '', $subject);
 
    $headers = Array();
 
    // remove additional headers
 
    while (True) {
 
        $line = array_shift($lines);
 
        if (empty($line)) {
 
            break;
 
        }
 
        $headers[] = $line;
 
    }
 
    $headers = implode($headers, "\n");
 
    $body = implode($lines, "\n");
 
    if ( $decode )
 
    {
 
        if ( preg_match("/^=[?]/", $subject) )
 
        {
 
            $subject = preg_replace('/^=[?]utf-8[?]Q[?]/', '', $subject);
 
            $subject = preg_replace('/[?]=$/', '', $subject);
 
            $subject = quoted_printable_decode($subject);
 
        }
 
        $body = quoted_printable_decode($body);
 
    }
 
    return Array($subject, $headers, $body);
 
}
 

	
 
function vacation_join($subject, $body, $headers=Null)
 
{
 
    $subject = trim($subject);
 
    $subjectq = quoted_printable_encode($subject);
 
    if ( $subjectq != $subject )
 
    {
 
        $subject = sprintf('=?utf-8?Q?%s?=', $subjectq);
 
    }
 
    $subject = 'Subject: ' . $subject;
 
    $headera = Array(
 
        $subject,
 
        'Content-Type: text/plain;'."\n"
 
        .'  charset="utf-8"'."\n"
 
        .'Content-Transfer-Encoding: quoted-printable');
 
    if (!empty($headers))
 
    {
 
        $headers = trim($headers);
 
        if (!empty($headers))
 
        {
 
            $headers = preg_replace("/\n\n+/", "\n", $headers);
 
            $headera[] = $headers;
 
        }
 
    }
 
    $headers = implode($headera, "\n");
 
    if (empty($body))
 
    {
 
        $body = '';
 
    }
 
    return sprintf("%s\n\n%s\n",
 
                   $headers, quoted_printable_encode($body));
 
}
 

	
 
//
 
// :ide-menu: Emacs IDE Menu - Buffer @BUFFER@
 
// . M-x `eIDE-menu' ()(eIDE-menu "z")
 
// :ide: COMPILE: PHP _DEBUG_=2 _DEBUG_TEST_=2
 
// . (compile (concat "php " (file-name-nondirectory (buffer-file-name)) " _DEBUG_=2 _DEBUG_TEST_=2"))
 

	
 
// :ide: QUO: $this->
 
// . (insert "$this->" )
 

	
 
// :ide: COMPILE: PHP w/o args
 
// . (compile (concat "php " (file-name-nondirectory (buffer-file-name)) ""))
 

	
 
// :ide: COMPILE: PHP _DEBUG_=1 _DEBUG_TEST_=1
 
// . (compile (concat "php " (file-name-nondirectory (buffer-file-name)) " _DEBUG_=1 _DEBUG_TEST_=1"))
 

	
 
//
 
// Local Variables:
 
// mode: php
 
// End:
 
?>
0 comments (0 inline, 0 general)