<?php
/**
 * default library
 * 
 * @author     Thomas Meyer
 * @licence    http://creativecommons.org/licenses/by-nc-sa/2.5/
 * @package    yana
 * @subpackage plugins
 */

/**
 * @access     public
 * @package    yana
 * @subpackage plugins
 */
 class plugin_default_library extends plugin
{

    /**
     * Default event handler
     *
     * returns bool(true) on success and bool(false) on error
     *
     * @access  public
     * @return  bool
     * @name    plugin_spamprotect::_default()
     * @param   string  $event  name of the called event in lower-case
     * @param   array   $ARGS   array of arguments passed to the function
     */
    function _default($event, $ARGS)
    {
        /* check input data */
        assert('is_string($event);');
        assert('is_array($ARGS);');
        settype($event, "string");
        settype($ARGS, "array");
        $event = strtolower($event);
        global $YANA;

        /* BEGIN visitor-counter */
        $my_counter = new counter();
        if (empty($ARGS['id'])) {
            $id = 'default';
        } else {
            $id = $ARGS['id'];
        } 
        $YANA->setVar('BESUCHER', $my_counter->count($id));
        /* END visitor-counter */

        return true;
    }

    /* public boolean */ function graphic_counter ($ARGS)
    {
        global $YANA;
        if (!empty($ARGS['id'])) {
            $my_counter = new counter();
            $count = $my_counter->count($ARGS['id']);
        } else {
            $my_counter = new counter();
            $count = $my_counter->count('default');
        }
        
        settype($ARGS['target'], 'int');
        header("Content-type: image/jpeg");
        $skinDir = $YANA->getVar("SKINDIR")."default/counter/".$ARGS['target']."/";
        if (!is_dir($skinDir)) {
            $skinDir = $YANA->getVar("SKINDIR")."default/counter/0/";
        }
        $myImage = imagecreatefromjpeg($skinDir."hintergrund.jpg");
        $myImageValues = getimagesize($skinDir."hintergrund.jpg");
        $previousImageWidths=0;
        
        if (file_exists($skinDir."blank.png")) {
            $imageValues = getimagesize($skinDir."blank.png");
            $previousImageWidths+=$imageValues[0];
        }
        
        for ($i=0; $i<strlen("$count"); $i++) {

            $image = imagecreatefrompng($skinDir.substr("$count", strlen("$count")-$i-1, 1).".png");
            $imageValues = getimagesize($skinDir.substr("$count", strlen("$count")-$i-1, 1).".png");
            $previousImageWidths+=$imageValues[0];
            if (($myImageValues[0]-$previousImageWidths) < 0 || ($myImageValues[1]-$imageValues[1]) < 0) {
               imagedestroy($image);
               imagedestroy($myImage);
               $myImage = imagecreate(325,55);
               $black   = imageColorAllocate($myImage,0,0,0);
               $white   = imageColorAllocate($myImage,255,255,255);
               imagefill($myImage,0,0,$white);
               imagestring($myImage,2,5,0,"[ERROR] image size exceeds maximum",$black);
               imagestring($myImage,2,5,10,"or value to big to be displayed",$black);
               imagestring($myImage,2,5,20,"1) check size of counter \"".$skinDir.substr("$count", strlen("$count")-$i-1, 1).".png\"",$black);
               imagestring($myImage,2,5,30,"2) check dimensions of file \"".$skinDir."hintergrund.png\"",$black);
               imagestring($myImage,2,5,40,"   are big enough for the content.",$black);
               imagepng($myImage);
               imagedestroy($myImage);
               die;
            };
            ImageCopy($myImage, $image, ($myImageValues[0]-$previousImageWidths), floor(($myImageValues[1]-$imageValues[1])/2), 0, 0, $imageValues[0], $imageValues[1]);
            imagedestroy($image);
        
        }
        
        imagejpeg($myImage);
        imagedestroy($myImage);

        die();
    }

    /* public boolean */ function sitemap ($ARGS)
    {
        global $YANA;
        if (!empty($ARGS['id'])) {
            $my_counter = new counter();
            $YANA->setVar('BESUCHER', $my_counter->count($ARGS['id']));
        } else {
            $my_counter = new counter();
            $YANA->setVar('BESUCHER', $my_counter->count('default'));
        }
        $YANA->setVar("PROFILES", $YANA->plugins->getPluginList());
        return true;
    }

    /* public boolean */ function clear_server_cache ($ARGS) 
    {
        smartTemplate::clearCache();
        return true;
    }

    /* public boolean */ function preview ($ARGS)
    {
        global $YANA;
        if (!isset($ARGS['eintraege'])) {
            die('<h1>ERROR 500:</h1><p>Required argument &quot;eintraege&quot; not available.</p>');
        }
        $filename = $YANA->skin->getFile('blank');
        assert('!empty($filename);');
        $blankDoc = new smartTemplate($filename);
        $blankDoc->insert('*',$YANA->getVar("*"));
        assert('is_string($ARGS["eintraege"]);');
        $ARGS['eintraege'] = untaintInput($ARGS['eintraege'], "text", 0, YANA_ESCAPE_USERTEXT);
        $ARGS['eintraege'] = DisplayUtility::smilies($ARGS['eintraege']);
        $ARGS['eintraege'] = DisplayUtility::embeddedTags($ARGS['eintraege']);
        $blankDoc->insert('*','<div style="height: 100%; overflow: auto;">'.$ARGS['eintraege'].'</div>');
        die($blankDoc->get());
    }

    /* public boolean */ function security_get_image ($ARGS) 
    {
        global $YANA;
        header("Content-type: image/png");

        $imagesrc = $YANA->getVar("CONFIGDIR")."security_image/security_image".rand(0,9).".png";
        $file     = new datfile($YANA->getVar("CONFIGDIR")."security_image/security_image.dat");

        if (!$file->exists()) {
            $file->create();
        }
        $file->read();
        if (!$file->isEmpty()) {
            $contents = $file->get(0);
        } else {
            $contents = array();
        }

        if (!isset($contents['TIME']) || $contents['TIME'] < time() || $contents['MAX_TIME'] < time()) {
            $contents = array();
            $contents['MAX_TIME'] = time() + 10000;
            for ($i=1;$i<10;$i++)
            {
                $contents["_$i"] = "";
                for ($j=0;$j<5;$j++)
                {
                    $letter = "";
                    while (empty($letter) || strtolower($letter) == 'o' || $letter == '0' || $letter == 'l' || $letter == 'i')
                    {
                        switch (rand(0,2))
                        {
                            case 0:
                                $letter = chr(rand(65,90));
                            break;
                            case 1:
                                $letter = chr(rand(48,57));
                            break;
                            case 2:
                                $letter = chr(rand(97,122));
                            break;
                        }
                    }
                    $contents["_$i"] .= $letter;
                }
            }
        }
        $contents['TIME'] = time() + 1200;
        $file->remove(0);
        $file->insert($contents);
        $file->write();

        if (!isset($ARGS['security_image_index']) || $ARGS['security_image_index'] < 1 || $ARGS['security_image_index'] > 9) {
            $text =& $contents['_1'];
        } else {
            $text =& $contents['_'.$ARGS['security_image_index']];
        }

        $myImage = imagecreatefrompng($imagesrc);
        $white   = imageColorAllocate($myImage,255,255,255);
        for ($i=0; $i<5; $i++)
        {
            imagestring($myImage,5,4+($i*9)+rand(0,1),1+rand(-1,1),$text[$i],imageColorAllocate($myImage,40+rand(-30,60),40+rand(-30,60),40+rand(-30,60)));
        }
        imagepng($myImage);
        imagedestroy($myImage);
        die();
    }

    /* public boolean */ function security_check_image ($ARGS)
    {
        global $YANA;
        $file = new datfile($YANA->getVar("CONFIGDIR")."security_image/security_image.dat");
        $file->read();
        if (!$file->isEmpty()) {
            $contents = $file->get(0);
        } else {
            $contents = array();
        }

        if ($contents['MAX_TIME'] < time()) {
            return false;
        }

        if (!isset($ARGS['security_image_index']) || $ARGS['security_image_index'] < 1 || $ARGS['security_image_index'] > 9) {
            $text =& $contents['_1'];
        } else {
            $text =& $contents['_'.$ARGS['security_image_index']];
        }
        return !empty($text) && ($ARGS['security_image'] == $text);
    }

}

// © 2006, Thomas Meyer www.All-Community.de
// Copyright Hinweis nicht entfernen!
?>