changed git call from https to git readonly
[atutor.git] / mods / wiki / plugins / markup / smilies.php
1 <?php
2
3 /*
4    You can embed smilies into pages, if you load this plugin. Only the
5    location and URL to the images must be configured, the directory is
6    read in at initialization.
7    Images with a textual name then can be referenced as   :word:
8    from within any WikiPage, and ordinary smilies can be written as is
9    and get replaced by their graphical counterpart (if there is one).
10
11    A prepared "_smilies.tar.gz" package is available from
12    http://erfurtwiki.sourceforge.net/downloads/contrib-add-ons/
13    Just untar it in the ewiki main directory, due to the 'special'
14    characters this however only works on Unix filesystems.
15 */
16
17
18 define("SMILIES_DIR", "./img/smilies/");
19 define("SMILIES_BASE_HREF", "/img/smilies/");
20 $ewiki_config["smilies"] = array(
21    ":)" => "cap.gif",
22 );
23
24
25 $ewiki_plugins["format_final"][] = "smilies_format_final";
26
27 function smilies_format_final(&$html) {
28
29    global $ewiki_config;
30
31    #-- read in directories
32    static $imgs, $regex;
33    if (!isset($imgs)) {
34       $imgs = array();
35       $regex = array();
36       foreach (smilies_dir(SMILIES_DIR) as $fn) {
37          #-- check for file name extensions
38          if (($r = strrpos($fn, ".")) >= 2) {
39             $id = substr($fn, 0, $r);
40             if ($r = strrpos($fn, "/")) {
41                $id = substr($fn, $r+1);
42             }
43             $id0 = $id[0];
44
45             #-- word images
46             if (($id0>="a") && ($id0<="z") || ($id0>="0") && ($id0<="9")) {
47                $id = ":$id:";
48             }
49             else {
50                $id = htmlentities($id);
51             }
52
53             #-- decoding, encoding
54             if ((strpos($id, "%")!==false) && ($uu = urldecode($id))) {
55                $id = $uu;
56             }
57             $imgs[$id] = $fn;
58             $regex[] = preg_quote($id);
59          }
60       }
61
62       #-- append default images and aliases
63       foreach ($ewiki_config["smilies"] as $id=>$fn) {
64          $imgs[$id] = $fn;
65          $regex[] = preg_quote($id);
66       }
67
68       $regex = implode("|", $regex);
69    }
70 #print_r($imgs);
71 #print_r($regex);
72
73    #-- use regex to insert <img> tags
74    if ($imgs) {
75       $html = preg_replace(
76          '/(?!<[^>]*)('.$regex.')/e',
77          '
78             "<img src=\"" .
79             SMILIES_BASE_HREF . urlencode(stripslashes($imgs["$1"])) .
80             "\" alt=\"" . htmlentities("$1") . "\" />"
81          ',
82          $html
83       );
84    }
85 }
86
87
88 function smilies_dir($dir, $prep="") {
89    $r = array();
90    if ($dh = opendir($dir)) {
91       while ($fn = readdir($dh)) {
92          if (is_dir("$dir/$fn")) {
93             if ($fn[0] != ".") {
94                $r = array_merge($r, smilies_dir("$dir/$fn", "$fn/"));
95             }
96          }
97          else {
98             $r[] = "$prep$fn";
99          }
100       }
101       closedir($dh);
102    }
103    return($r);
104 }
105
106 ?>