root/trunk/includes/wiki.inc.php

Revision 1, 3.1 KB (checked in by anonymous, 7 years ago)

initial import

Line 
1<?php
2
3$Wiki=0;
4class Wikini
5{
6    function IncludeBuffered($filename, $notfoundText = "", $vars = "", $path = "")
7    {
8        if ($path) $dirs = explode(":", $path);
9        else $dirs = array("");
10
11        foreach($dirs as $dir)
12        {
13            if ($dir) $dir .= "/";
14            $fullfilename = $dir.$filename;
15            if (file_exists($fullfilename))
16            {
17                if (is_array($vars)) extract($vars);
18
19                ob_start();
20                include($fullfilename);
21                $output = ob_get_contents();
22                ob_end_clean();
23                return $output;
24            }
25        }
26        if ($notfoundText) return $notfoundText;
27        else return false;
28    }
29
30
31    function Wikise($text, $formatter = "wakka")
32    {
33        global $token;
34        $token = array();
35        return $this->IncludeBuffered(INCLUDESPATH."/formatters/".$formatter.".php", "<i>Impossible de trouver le formateur \"$formatter\"</i>", compact("text"));
36    }
37
38    function WikiLink($tag, $method = "", $text = "", $track = 1)
39    {
40        $tag=htmlspecialchars($tag);//avoid xss
41        $text=htmlspecialchars($text);//paranoiac again
42        if (!$text)
43        {
44            if(strlen($tag)>29)
45                $text = substr($tag, 0, 32).'[...]';
46            else
47                $text = $tag;
48        }
49
50    // is this a full link? ie, does it contain alpha-numeric characters?
51        if (preg_match("/[^[:alnum:]]/", $tag))
52        {
53            // check for email addresses
54            if (preg_match("/^.+\@.+$/", $tag))
55                $tag = "mailto:".$tag;
56            else if (preg_match("/^\/.+$/", $tag))
57                $tag = $tag;
58            // check for protocol-less URLs
59            else if (!preg_match("/:\/\//", $tag))
60                $tag = "http://".$tag;   //Very important for xss (avoid javascript:() hacking)
61            // is this an inline image (text!=tag and url ends png,gif,jpeg)
62            if($text!=$tag and preg_match("/\.(gif|jpeg|png|jpg)$/i",$tag))
63             return "<img src=\"$tag\" alt=\"$text\" />";
64            elseif($text == $tag)
65            {
66            if(strlen($tag)>29)
67                $text = substr($tag, 0, 32).'[...]';
68            return "<a href=\"$tag\">$text</a>";
69            }
70            else
71             return "<a href=\"$tag\">$text</a>";
72        }
73        else
74            return $text;
75
76    }
77
78    function Action($action, $forceLinkTracking = 0)
79    {
80        $action = trim($action); $vars=array();
81        // stupid attributes check
82        if ((stristr($action, "=\"")) || (stristr($action, "/")))
83        {
84            // extract $action and $vars_temp ("raw" attributes)
85            preg_match("/^([A-Za-z0-9]*)\/?(.*)$/", $action, $matches);
86            list(, $action, $vars_temp) = $matches;
87            // match all attributes (key and value)
88            $parameter[$vars_temp]=$vars_temp;
89            preg_match_all("/([A-Za-z0-9]*)=\"(.*)\"/U", $vars_temp, $matches);
90
91            // prepare an array for extract() to work with (in $this->IncludeBuffered())
92            if (is_array($matches))
93            {
94                for ($a = 0; $a < count($matches); $a++)
95                {
96                    $vars[$matches[1][$a]] = $matches[2][$a];
97                    $parameter[$matches[1][$a]]=$matches[2][$a];
98                }
99            }
100        }
101    //    if (!$forceLinkTracking) $this->StopLinkTracking();
102        $result = $this->IncludeBuffered(strtolower($action).".php", "<i>Action inconnue \"$action\"</i>", $vars, INCLUDESPATH."/actions/");
103    //    $this->StartLinkTracking();
104        if (isset($parameter)) unset($parameter[$parameter]);
105        unset($parameter);
106        return $result;
107    }
108}
109
110function Wikise($text, $formatter = "wakka")
111{
112    global $Wiki;
113    $Wiki = new Wikini();
114    return $Wiki->Wikise($text, $formatter);
115}
116
117?>
Note: See TracBrowser for help on using the browser.