Changeset - r7:fad285782be8
[Not reviewed]
v0.3 default
0 1 0
Wolfgang Scherer (ws) - 13 years ago 2012-03-30 18:20:52
wolfgang.scherer@gmx.de
lib/util.php: poor man's quoted_printable_encode for PHP < 5.30.
1 file changed with 31 insertions and 0 deletions:
0 comments (0 inline, 0 general)
lib/util.php
Show inline comments
 
@@ -59,273 +59,304 @@ function get_users()
 
            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
 
// --------------------------------------------------
 

	
 
if (!function_exists("quoted_printable_encode")) {
 
    // only available with PHP >= 5.3.0
 
    if (function_exists("imap_8bit")) {
 
        function quoted_printable_encode($string) {
 
            return imap_8bit($string);
 
        }
 
    } else {
 
        /**
 
         * Process a string to fit the requirements of RFC2045 section 6.7. Note that
 
         * this works, but replaces more characters than the minimum set. For readability
 
         * the spaces and CRLF pairs aren't encoded though.
 
         */
 
        function quoted_printable_encode($string) {
 
            $string = str_replace(array('%20', '%0D%0A', '%'), array(' ', "\r\n", '='), rawurlencode($string));
 
            $string = preg_replace('/[^\r\n]{73}[^=\r\n]{2}/', "$0=\r\n", $string);
 
            return $string;
 
        }
 
    }
 
}
 

	
 
// there should be no need ... quoted_printable_decode is around since PHP4
 
if (!function_exists("quoted_printable_decode")) {
 
    if (function_exists("imap_qprint")) {
 
        echo "imap_qprint";
 
        function quoted_printable_decode($string) {
 
            return imap_qprint($string);
 
        }
 
    } else {
 
    }
 
}
 

	
 
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)