File: template.inc

package info (click to toggle)
websvn 1.61-20
  • links: PTS
  • area: main
  • in suites: etch-m68k
  • size: 728 kB
  • ctags: 450
  • sloc: php: 3,333; pascal: 1,245; sh: 155; makefile: 49
file content (305 lines) | stat: -rw-r--r-- 7,480 bytes parent folder | download | duplicates (2)
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
<?php

// WebSVN - Subversion repository viewing via the web using PHP
// Copyright (C) 2004 Tim Armes
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// --
//
// templates.inc
//
// Templating system to allow advanced page customisation

$ignore = false;

// Stack of previous test results
$ignorestack = array();

// Number of test levels currently ignored
$ignorelevel = 0;

// parseCommand
//
// Parse a special command

function parseCommand($line, $vars, $handle)
{ 
   global $ignore, $ignorestack, $ignorelevel;
   
   // Check for test conditions      
   if (strncmp(trim($line), "[websvn-test:", 13) == 0)
   {
      if (!$ignore)
      {
         $line = trim($line);
         $var = substr($line, 13, -1);
         if (empty($vars[$var]))
         {
            array_push($ignorestack, $ignore);
            $ignore = true;
         }
      }
      else
      {
         $ignorelevel++;
      }
      
      return true;
   }
   
   if (strncmp(trim($line), "[websvn-else]", 13) == 0)
   {
      if ($ignorelevel == 0)
      {
         $ignore = !$ignore;
      }
         
      return true;
   }

   if (strncmp(trim($line), "[websvn-endtest]", 16) == 0)
   {
      if ($ignorelevel > 0)
      {
         $ignorelevel--;
      }
      else
      {
         $ignore = array_pop($ignorestack);
      }
      
      return true;
   }
   
   if (strncmp(trim($line), "[websvn-getlisting]", 19) == 0)
   {
      global $path, $rev, $svnrep;
   
      if (!$ignore)
         $svnrep->listFileContents($path, $rev);
      
      return true;
   }
   
   if (strncmp(trim($line), "[websvn-defineicons]", 19) == 0)
   {
      global $icons;
      
      if (!isset($icons))
         $icons = array();
      
      // Read all the lines until we reach the end of the definition, storing
      // each one...
      
      if (!$ignore)
      {
         while (!feof($handle))
         {
            $line = trim(fgets($handle));
             
            if (strncmp($line, "[websvn-enddefineicons]", 22) == 0)
            {
               return true; 
            }
               
            $eqsign = strpos($line, "=");
            
            $match = substr($line, 0, $eqsign);
            $def = substr($line, $eqsign + 1);
            
            $icons[$match] = $def;
         }
      }
      
      return true;
   }

   if (strncmp(trim($line), "[websvn-icon]", 13) == 0)
   {
      global $icons, $vars;
   
      if (!$ignore)
      {
         // The current filetype should be defined my $vars["filetype"]
      
         if (!empty($icons[$vars["filetype"]]))
         {
            echo parseTags($icons[$vars["filetype"]], $vars);
         }
         else if (!empty($icons["*"]))
         {
            echo parseTags($icons["*"], $vars);
         }
      }
      
      return true;
   }

   if (strncmp(trim($line), "[websvn-treenode]", 17) == 0)
   {
      global $icons, $vars;
   
      if (!$ignore)
      {
         if ((!empty($icons["i-node"])) && (!empty($icons["t-node"])) && (!empty($icons["l-node"])))
         {
            for ($n = 1; $n < $vars["level"]; $n++)
            {
               if ($vars["last_i_node"][$n])
               {
                  echo parseTags($icons["e-node"], $vars);
               }
               else
               {
                  echo parseTags($icons["i-node"], $vars);
               }
            }

            if ($vars["level"] != 0)
            {
               if ($vars["node"] == 0)
               {
                  echo parseTags($icons["t-node"], $vars);
               }
               else
               {
                  echo parseTags($icons["l-node"], $vars);
                  $vars["last_i_node"][$vars["level"]] = TRUE;
               }
            }
         }
      }

      return true;
   }
      
   return false;
}

// parseTemplate
//
// Parse the given template, replacing the variables with the values passed

function parseTemplate($template, $vars, $listing)
{
   global $ignore, $vars;
   
   if (!is_file($template))
   {
     print "No template file found ($template)";
     exit;
   }

   $handle = fopen($template, "r");
   $inListing = false;
   $ignore = false;
   $listLines = array();
   
   while (!feof($handle))
   {
      $line = fgets($handle);
         
      // Check for the end of the file list
      if ($inListing)
      {
         if (strcmp(trim($line), "[websvn-endlisting]") == 0)
         {
            $inListing = false;

            // For each item in the list
            foreach ($listing as $listvars)
            {
               // Copy the value for this list item into the $vars array
               foreach ($listvars as $id => $value)
               {
                  $vars[$id] = $value;
               }
          
               // Output the list item
               foreach ($listLines as $line)
               {
                  if (!parseCommand($line, $vars, $handle))
                  {
                     if (!$ignore)
                        print parseTags($line, $vars);
                  }
               }
            }
         }
         else
         {
            if ($ignore == false)
               $listLines[] = $line;
         }
      }
      else if (parseCommand($line, $vars, $handle))
      {
         continue;
      }
      else
      {
         // Check for the start of the file list
         if (strncmp(trim($line), "[websvn-startlisting]", 21) == 0)
         {
            $inListing = true;
         }
         else
         {
            if ($ignore == false)
               print parseTags($line, $vars);
         }
      }
   }
   
   fclose($handle);
}

// parseTags
//
// Replace all occurences of [websvn:varname] wit the give variable

function parseTags($line, $vars)
{
   global $lang;
   
   // Replace the websvn variables
   while (ereg("\[websvn:([a-zA-Z0-9_]+)\]", $line, $matches))
   {
      // Make sure that the variable exists
      if (!isset($vars[$matches[1]]))
      {
         $vars[$matches[1]] = "?${matches[1]}?";
      }
      
      $line = str_replace($matches[0], $vars[$matches[1]], $line);
   }
   
   // Replace the language strings
   while (ereg("\[lang:([a-zA-Z0-9_]+)\]", $line, $matches))
   {
      // Make sure that the variable exists
      if (!isset($lang[$matches[1]]))
      {
         $lang[$matches[1]] = "?${matches[1]}?";
      }
      
      $line = str_replace($matches[0], $lang[$matches[1]], $line);
   }

   // Return the results
   return $line;
}
?>