1
  
2
  
3
  
4
  
5
  
6
  
7
  
8
  
9
  
10
  
11
  
12
  
13
  
14
  
15
  
16
  
17
  
18
  
19
  
20
  
21
  
22
  
23
  
24
  
25
  
26
  
27
  
28
  
29
  
30
  
31
  
32
  
33
  
34
  
35
  
36
  
37
  
38
  
39
  
40
  
41
  
42
  
43
  
44
  
45
  
46
  
47
  
48
  
49
  
50
  
51
  
52
  
53
  
54
  
55
  
56
  
57
  
58
  
59
  
60
  
61
  
62
  
63
  
64
  
65
  
66
  
67
  
68
  
69
  
70
  
71
  
72
  
73
  
74
  
75
  
76
  
77
  
78
  
79
  
80
  
81
  
82
  
83
  
84
  
85
  
86
  
87
  
88
  
89
  
90
  
91
  
92
  
93
  
94
  
95
  
96
  
97
  
98
  
99
  
100
  
101
  
102
  
103
  
104
  
105
  
106
  
107
  
108
  
109
  
110
  
111
  
112
  
113
  
114
  
115
  
116
  
117
  
118
  
119
  
120
  
121
  
122
  
123
  
124
  
125
  
126
  
127
  
128
  
129
  
130
  
131
  
132
  
133
  
134
  
135
  
136
  
137
  
138
  
139
  
140
  
141
  
142
  
143
  
144
  
145
  
146
  
147
  
148
  
149
  
150
  
151
  
152
  
153
  
154
  
155
  
156
  
157
  
158
  
159
  
160
  
161
  
162
  
163
  
164
  
165
  
166
  
167
  
168
  
169
  
170
  
171
  
172
  
173
  
174
  
175
  
176
  
177
  
178
  
179
  
180
  
181
  
182
  
183
  
184
  
185
  
186
  
187
  
188
  
189
  
190
  
191
  
192
  
193
  
194
  
195
  
196
  
197
  
198
  
199
  
200
  
201
  
202
  
203
  
204
  
205
  
206
  
207
  
208
  
209
  
210
  
211
  
212
  
213
  
214
  
/* 
 * $Id: configtablist.pike,v 1.15 1999/07/25 01:23:40 nilsson Exp $ 
 * 
 * Makes a tab-list like the one in the config-interface. 
 * 
 * $Author: nilsson $ 
 */ 
 
constant cvs_version="$Id: configtablist.pike,v 1.15 1999/07/25 01:23:40 nilsson Exp $"; 
constant thread_safe=1; 
 
#define old_rxml_compat 1 
 
#define use_contents_cache 0 
#define use_gif_cache      1 
#include <module.h> 
inherit "module"; 
inherit "roxenlib"; 
 
#if use_contents_cache   
mapping(string:string) contents_cache = ([]); 
#endif 
 
#if use_gif_cache   
mapping(string:string) gif_cache = ([]); 
#endif 
 
/* 
 * Functions 
 */ 
 
array register_module() 
{ 
  return(({ MODULE_PARSER, "Config tab-list",  
              "Adds some tags for making a config-interface " 
              "look-alike tab-list.<br>\n" 
              "Usage:<br>\n" 
              "<ul><pre>&lt;tablist&gt;\n" 
              "&lt;tab href=\"/tab1/\"&gt;Some text&lt;/tab&gt;\n" 
              "&lt;tab href=\"/tab2/\"&gt;Some more text&lt;/tab&gt;\n" 
              "&lt;tab href=\"a/strange/place/\"&gt;Tab 3&lt;/tab&gt;\n" 
              "&lt;/tablist&gt;\n" 
              "</pre></ul>Attributes for the &lt;tab&gt; tag:<br>\n" 
              "<ul><table border=0>\n" 
              "<tr><td><b>selected</b></td><td>Whether the tab is selected " 
              "or not.</td></tr>\n" 
              "<tr><td><b>bgcolor</b></td><td>What color to use as background. " 
              "Defaults to white.</td></tr>\n" 
              "<tr><td><b>alt</b></td><td>Alt-text for the image (default: " 
              "\"_/\" + text + \"\\_\").</td></tr>\n" 
              "<tr><td><b>border</b></td><td>Border for the image (default: " 
              "0).</td></tr>\n" 
              "<tr><td><b>noxml</b></td><td>Images will not be terminated with a slash " 
              "if this attribute is provided</td></tr>\n" 
              "</table></ul>\n" 
              "<br>The bgcolor and noxml attribute can also be given in the tablist tag as " 
              "global attributes. Using bgcolor in a tab tag will overide the global setting.", 0, 1 })); 
} 
 
void create() 
{ 
} 
 
string tag_config_tab(string t, mapping a, string contents, mapping d) 
{ 
  string dir = "u/"; 
  mapping img_attrs = ([]); 
  if(a->help) return register_module()[2]; 
  if (a->selected) { 
    dir = "s/"; 
  } 
  if(a->bgcolor) 
    dir+=replace(a->bgcolor,"#","|"); 
  else if(d->bgcolor) 
    dir+=replace(d->bgcolor,"#","|"); 
  else 
    dir+="white"; 
  dir+="/"; 
  m_delete(a, "selected"); 
 
  img_attrs->src = query_internal_location() + dir + 
    replace(http_encode_string(contents), "?", "%3f") + ".gif"; 
  if (a->alt) { 
    img_attrs->alt = a->alt; 
    m_delete(a, "alt"); 
  } else { 
    img_attrs->alt = "_/" + html_encode_string(contents) + "\\_"; 
  } 
  if (a->border) { 
    img_attrs->border = a->border; 
    m_delete(a, "border"); 
  } else { 
    img_attrs->border="0"; 
  } 
  if(!a->noxml && !d->noxml) img_attrs["/"]="/"; 
  m_delete(a, "noxml"); 
  return make_container("a", a, make_container("b", ([]), 
                                               make_tag("img", img_attrs))); 
} 
 
int my_hash(mixed o) 
{ 
  switch(sprintf("%t",o)) 
  { 
    case "string": return hash(o); 
    case "int": return o; 
    case "mapping": 
      int h = 17 + sizeof(o); 
      foreach(indices(o), mixed index) 
         h += hash(index) * my_hash(o[index]); 
      return h; 
 
   case "array": 
    return hash(sprintf("%O",o)); 
   default: 
     return hash(encode_value(o)); 
  } 
} 
 
string tag_config_tablist(string t, mapping a, string contents) 
{ 
#if use_contents_cache   
  object md5 = Crypto.md5(); 
  md5->update(contents+my_hash(a)); 
  string key=md5->digest(); 
  if(contents_cache[key]) 
    return contents_cache[key]; 
#endif 
  string res=replace(parse_html(contents, ([]), (["tab":tag_config_tab]), a), 
                 ({ "\n", "\r" }), ({ "", "" })); 
#if use_contents_cache   
  contents_cache[key]=res; 
#endif   
  return res; 
} 
 
mapping query_tag_callers() 
{ 
  return ([]); 
} 
  mapping query_container_callers() 
{ 
  return ([ "tablist":tag_config_tablist  
#if old_rxml_compat 
          ,"config_tablist":tag_config_tablist 
#endif 
  ]); 
} 
 
#if constant(thread_create) 
object interface_lock = Thread.Mutex(); 
#endif /* constant(thread_create) */ 
 
object load_interface() 
{ 
#if constant(thread_create) 
  // Only one thread at a time may call roxen->configuration_interface(). 
  // 
  // load_interface() shouldn't be called recursively, 
  // so don't protect against it. 
  mixed key = interface_lock->lock(); 
#endif /* constant(thread_create) */ 
  return(roxen->configuration_interface()); 
} 
 
mapping find_internal(string f, object id) 
{ 
  string s; 
#if use_gif_cache 
  if(s=gif_cache[f]) 
  { 
//    werror("Configtablist: "+f+" found in cache.\n"); 
    return http_string_answer(s,"image/gif"); 
  } 
#endif   
 
  array pagecolor; //=({ 122, 122, 122 }); //parse_color("lightblue"); 
  array(string) arr = f/"/"; 
  if (sizeof(arr) > 1) { 
    object interface = load_interface(); 
    object(Image.image) button; 
 
    if (arr[-1][sizeof(arr[-1])-4..] == ".gif") { 
      arr[-1] = arr[-1][..sizeof(arr[-1])-5]; 
    } 
 
    pagecolor=parse_color(replace(arr[1],"|","#",)); 
     
    switch (arr[0]) { 
    case "s":   /* Selected */ 
      button = interface->draw_selected_button(arr[2..]*"/", 
                                               interface->button_font, 
                                               pagecolor); 
      break; 
    case "u":   /* Unselected */ 
      button = interface->draw_unselected_button(arr[2..]*"/", 
                                                 interface->button_font, 
                                                 pagecolor); 
      break; 
    default: 
      return 0; 
    } 
     
    s=Image.GIF.encode(button,@pagecolor); 
#if use_gif_cache 
    if(!gif_cache[f]) 
      gif_cache[f]=s; 
#endif   
    return http_string_answer(s,"image/gif"); 
  } 
  return 0; 
}