]> err.no Git - sope/blob - sope-appserver/NGObjWeb/Templates/WOApplication+Builders.m
improved SOPE security exceptions
[sope] / sope-appserver / NGObjWeb / Templates / WOApplication+Builders.m
1 /*
2   Copyright (C) 2000-2004 SKYRIX Software AG
3
4   This file is part of OpenGroupware.org.
5
6   OGo is free software; you can redistribute it and/or modify it under
7   the terms of the GNU Lesser General Public License as published by the
8   Free Software Foundation; either version 2, or (at your option) any
9   later version.
10
11   OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12   WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14   License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with OGo; see the file COPYING.  If not, write to the
18   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19   02111-1307, USA.
20 */
21
22 #include <NGObjWeb/WOApplication.h>
23 #include "WOxTemplateBuilder.h"
24 #include <NGObjWeb/WOxElemBuilder.h>
25 #include "common.h"
26
27 @implementation WOApplication(BuilderStack)
28
29 - (void)scanForBuilderBundlesInDirectory:(NSString *)_path {
30   NGBundleManager *bm;
31   NSFileManager *fm;
32   NSEnumerator  *pathes;
33   NSString      *lPath;
34
35   bm = [NGBundleManager defaultBundleManager];
36   
37   fm = [NSFileManager defaultManager];
38   pathes = [[fm directoryContentsAtPath:_path] objectEnumerator];
39   while ((lPath = [pathes nextObject])) {
40     NSBundle *bundle;
41     BOOL isDir;
42     
43     lPath = [_path stringByAppendingPathComponent:lPath];
44     
45     if (![fm fileExistsAtPath:lPath isDirectory:&isDir])
46       continue;
47     if (!isDir)
48       continue;
49     
50     if ((bundle = [bm bundleWithPath:lPath]) == nil) {
51       [self logWithFormat:@"WARNING: could not get bundle for path: '%@'",
52               lPath];
53       continue;
54     }
55     
56     if (![bundle load]) {
57       [self logWithFormat:@"WARNING: could not load bundle: '%@'", lPath];
58       continue;
59     }
60     
61     [self debugWithFormat:@"loaded elem builder bundle: %@",
62             [lPath lastPathComponent]];
63   }
64 }
65
66 - (void)loadBuilderBundles {
67   // TODO: DUP to SoProductRegistry.m
68   NSFileManager *fm;
69   NSProcessInfo *pi;
70   NSArray       *pathes;
71   NSString      *relPath;
72   unsigned      i;
73   
74   /* scan library pathes */
75   
76   fm = [NSFileManager defaultManager];
77   pi = [NSProcessInfo processInfo];
78   
79 #if COCOA_Foundation_LIBRARY
80   /* 
81      TODO: (like COMPILE_FOR_GNUSTEP)
82      This should actually check whether we are compiling in the
83      GNUstep environment since this modifies the location of bundles.
84   */
85   pathes = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,
86                                                NSAllDomainsMask,
87                                                YES);
88 #else
89   pathes = [[pi environment] objectForKey:@"GNUSTEP_PATHPREFIX_LIST"];
90   if (pathes == nil)
91     pathes = [[pi environment] objectForKey:@"GNUSTEP_PATHLIST"];
92   
93   pathes = [[pathes stringValue] componentsSeparatedByString:@":"];
94 #endif
95   
96   if ([pathes count] == 0) {
97     [self debugWithFormat:@"found no builder bundle pathes."];
98     return;
99   }
100   
101   [self debugWithFormat:@"scanning for builder bundles ..."];
102   
103 #if !COCOA_Foundation_LIBRARY
104   relPath = @"Library/";
105 #endif
106   relPath = [NSString stringWithFormat:@"%@WOxElemBuilders-%i.%i/", relPath,
107                         SOPE_MAJOR_VERSION, SOPE_MINOR_VERSION];
108   for (i = 0; i < [pathes count]; i++) {
109     NSString *lPath;
110     BOOL     isDir;
111     
112     lPath = [[pathes objectAtIndex:i] stringByAppendingPathComponent:relPath];
113     if (![fm fileExistsAtPath:lPath isDirectory:&isDir])
114       continue;
115     if (!isDir)
116       continue;
117     
118     [self debugWithFormat:@"  directory %@", lPath];
119     [self scanForBuilderBundlesInDirectory:lPath];
120   }
121   
122   /* look into FHS pathes */
123   
124   relPath = [NSString stringWithFormat:@"lib/sope-%i.%i/wox-builders/",
125                         SOPE_MAJOR_VERSION, SOPE_MINOR_VERSION];
126   pathes = [NSArray arrayWithObjects:
127                       [@"/usr/local/" stringByAppendingString:relPath],
128                       [@"/usr/"       stringByAppendingString:relPath],
129                     nil];
130   for (i = 0; i < [pathes count]; i++) {
131     NSString *lPath;
132     BOOL     isDir;
133     
134     lPath = [pathes objectAtIndex:i];
135     if (![fm fileExistsAtPath:lPath isDirectory:&isDir])
136       continue;
137     if (!isDir)
138       continue;
139     
140     [self debugWithFormat:@"  directory %@", lPath];
141     [self scanForBuilderBundlesInDirectory:lPath];
142   }
143   
144   /* report result */
145   
146   [self debugWithFormat:@"finished scan for builders."];
147 }
148
149 - (WOxElemBuilder *)builderForDocument:(id<DOMDocument>)_document {
150   static WOxElemBuilder *builder    = nil;
151   static NSArray        *defClasses = nil;
152   NSUserDefaults *ud;
153   NSArray *classes = nil;
154   NSArray *infos;
155   
156   if (builder != nil)
157     return builder;
158     
159   ud = [NSUserDefaults standardUserDefaults];
160   if (defClasses == nil)
161     defClasses = [[ud arrayForKey:@"WOxBuilderClasses"] copy];
162   
163   /* ensure that bundles are loaded */
164   [self loadBuilderBundles];
165   
166   infos = [[NGBundleManager defaultBundleManager]
167                             providedResourcesOfType:@"WOxElemBuilder"];
168   if ([infos count] > 0) {
169     classes = [NSMutableArray arrayWithCapacity:24];
170     [(id)classes addObjectsFromArray:[infos valueForKey:@"name"]];
171     [(id)classes addObjectsFromArray:defClasses];
172   }
173   else
174     classes = defClasses;
175   
176   if ([ud boolForKey:@"WOxLogBuilderQueue"]) {
177     NSEnumerator *e;
178     NSString *b;
179       
180     if ([classes count] > 0) {
181       [self debugWithFormat:@"builder stack:"];
182       e = [classes objectEnumerator];
183       while ((b = [e nextObject]))
184         [self logWithFormat:@"  %@", b];
185     }
186     else {
187       [self debugWithFormat:@"empty wox-element builder stack !"];
188     }
189   }
190   
191   builder = [[WOxElemBuilder createBuilderQueue:classes] retain];
192   return builder;
193 }
194
195 @end /* WOApplication(BuilderStack) */