de

designoir

Token

Token.php

<?php

/**
 * class Token
 * Copyright (C) 2006  Dao Gottwald
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Contact information:
 *   Dao Gottwald  <dao at design-noir.de>
 *   Herltestraße 12
 *   D-01307, Germany
 *
 * @version  1.0
 */

class Token {
    protected
        $data_token = array(),
        $data_strings = array();
    
    private
        $delimiterA,
        $delimiterB;
    
    public function __construct ($a = '', $b = '') {
        $this->delimiterA = $a;
        $this->delimiterB = $b;
    }
    
    public function add ($string) {
        $hash = md5 ($string);
        if (!isset ($this->data_token[$hash])) {
            $this->data_token[$hash] = $this->delimiterA . md5 ($hash. uniqid (mt_rand(), true)) . $this->delimiterB;
            $this->data_strings[$hash] = $string;
        }
        return $this->data_token[$hash];
    }
    
    public function encode_string ($string, &$text) {
        if (is_array ($string)) {
            foreach ($string as &$s) {
                $this->encode_string ($s, $text);
            }
        } else {
            $text = str_replace ($string, $this->add ($string), $text);
        }
    }
    
    public function encode (&$text) {
        $text = str_replace ($this->data_strings, $this->data_token, $text);
    }
    
    public function decode (&$text) {
        $text = str_replace ($this->data_token, $this->data_strings, $text);
    }
}

?>

NestedToken.php

Requires: Token

<?php

/**
 * class NestedToken
 * Copyright (C) 2006  Dao Gottwald
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Contact information:
 *   Dao Gottwald  <dao at design-noir.de>
 *   Herltestraße 12
 *   D-01307, Germany
 *
 * @version  1.0
 */

require_once 'Token.php';

class NestedToken extends Token {
    public function decode (&$text) {
        $data_token = array_reverse ($this->data_token, true);
        foreach ($data_token as $hash => &$token) {
            $text = str_replace ($token, $this->data_strings[$hash], $text);
        }
    }
}

?>

Comments

HTML will not be interpreted.

Last changed on December 23, 2006 Dão G., 2005–2010
aggressiv akt andromeda bar beine blue efeu frontal fugaetu industriell komet land noir rost rot sonnenblume splash split winter wolke zeit