[go: up one dir, main page]

File: SOGoFolder.m

package info (click to toggle)
sogo 1.3.16-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 21,620 kB
  • sloc: objc: 99,218; python: 4,840; sh: 1,217; perl: 861; makefile: 132; sql: 53; php: 43; ansic: 4
file content (608 lines) | stat: -rw-r--r-- 15,493 bytes parent folder | download | duplicates (3)
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
/* SOGoFolder.m - this file is part of SOGo
 *
 * Copyright (C) 2007-2011 Inverse inc.
 *
 * Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
 *
 * This file 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, or (at your option)
 * any later version.
 *
 * This file 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; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#import <Foundation/NSArray.h>
#import <Foundation/NSDictionary.h>
#import <Foundation/NSEnumerator.h>
#import <Foundation/NSString.h>
#import <Foundation/NSURL.h>
#import <Foundation/NSValue.h>

#import <NGObjWeb/WOApplication.h>
#import <NGObjWeb/WOContext.h>
#import <NGObjWeb/WORequest.h>
#import <NGObjWeb/SoWebDAVValue.h>

#import <NGExtensions/NSObject+Logs.h>

#import <DOM/DOMElement.h>

#import <SaxObjC/XMLNamespaces.h>

#import <SOGoUI/SOGoFolderAdvisory.h>

#import "DOMNode+SOGo.h"
#import "NSArray+Utilities.h"
#import "NSObject+DAV.h"
#import "NSString+DAV.h"
#import "NSString+Utilities.h"
#import "SOGoPermissions.h"
#import "SOGoUser.h"
#import "SOGoDomainDefaults.h"
#import "SOGoWebDAVAclManager.h"
#import "WORequest+SOGo.h"
#import "WOResponse+SOGo.h"

#import "SOGoFolder.h"

@interface SOGoObject (SOGoDAVHelpers)

- (void) _fillArrayWithPrincipalsOwnedBySelf: (NSMutableArray *) hrefs;

@end

@interface SOGoFolder (private)

- (NSArray *) _interpretWebDAVArrayValue: (id) value;
- (NSDictionary *) _expandPropertyResponse: (NGDOMElement *) property
                                   forHREF: (NSString *) href;

@end

@implementation SOGoFolder

+ (SOGoWebDAVAclManager *) webdavAclManager
{
  static SOGoWebDAVAclManager *aclManager = nil;

  if (!aclManager)
    {
      aclManager = [SOGoWebDAVAclManager new];
      [aclManager registerDAVPermission: davElement (@"read", XMLNS_WEBDAV)
			abstract: YES
			withEquivalent: SoPerm_WebDAVAccess
			asChildOf: davElement (@"all", XMLNS_WEBDAV)];
      [aclManager registerDAVPermission: davElement (@"read-current-user-privilege-set", XMLNS_WEBDAV)
			abstract: YES
			withEquivalent: nil
			asChildOf: davElement (@"read", XMLNS_WEBDAV)];
    }

  return aclManager;
}

- (id) init
{
  if ((self = [super init]))
    {
      displayName = nil;
      isSubscription = NO;
    }

  return self;
}

- (void) dealloc
{
  [displayName release];
  [super dealloc];
}

- (void) setDisplayName: (NSString *) newDisplayName
{
  ASSIGN (displayName, newDisplayName);
}

- (NSString *) displayName
{
  return ((id)displayName ? (id)displayName : (id)nameInContainer);
}

- (void) setIsSubscription: (BOOL) newIsSubscription
{
  isSubscription = newIsSubscription;
}

- (BOOL) isSubscription
{
  return isSubscription;
}

- (NSString *) realNameInContainer
{
  NSString *realNameInContainer, *ownerName;

  if (isSubscription)
    {
      ownerName = [[self ownerInContext: context] asCSSIdentifier];
      realNameInContainer
	= [nameInContainer substringFromIndex: [ownerName length] + 1];
    }
  else
    realNameInContainer = nameInContainer;

  return realNameInContainer;
}

- (NSString *) folderType
{
  [self subclassResponsibility: _cmd];

  return nil;
}

- (NSArray *) toOneRelationshipKeys
{
  return nil;
}

- (NSArray *) toManyRelationshipKeys
{
  return nil;
}

- (BOOL) isValidContentName: (NSString *) name
{
  return ([name length] > 0);
}

- (BOOL) isFolderish
{
  return YES;
}

- (NSString *) davURLAsString
{
  return [[container davURLAsString]
           stringByAppendingFormat: @"%@/", nameInContainer];
}

- (NSString *) httpURLForAdvisoryToUser: (NSString *) uid
{
  return [[self soURL] absoluteString];
}

- (NSString *) resourceURLForAdvisoryToUser: (NSString *) uid
{
  return [[self davURL] absoluteString];
}

/* sorting */
- (NSComparisonResult) _compareByOrigin: (SOGoFolder *) otherFolder
{
  NSComparisonResult comparison;

  if (isSubscription == [otherFolder isSubscription])
    comparison = NSOrderedSame;
  else
    {
      if (isSubscription)
	comparison = NSOrderedDescending;
      else
	comparison = NSOrderedAscending;
    }

  return comparison;
}

- (NSComparisonResult) _compareByNameInContainer: (SOGoFolder *) otherFolder
{
  NSString *selfName, *otherName;
  NSComparisonResult comparison;

  selfName = [self realNameInContainer];
  otherName = [otherFolder realNameInContainer];
  if ([selfName isEqualToString: @"personal"])
    {
      if ([otherName isEqualToString: @"personal"])
	comparison = NSOrderedSame;
      else
	comparison = NSOrderedAscending;
    }
  else
    {
      if ([otherName isEqualToString: @"personal"])
	comparison = NSOrderedDescending;
      else
	comparison = NSOrderedSame;
    }

  return comparison;
}

- (NSComparisonResult) compare: (id) otherFolder
{
  NSComparisonResult comparison;

  comparison = [self _compareByOrigin: otherFolder];
  if (comparison == NSOrderedSame)
    {
      comparison = [self _compareByNameInContainer: otherFolder];
      if (comparison == NSOrderedSame)
        {
          if ([self displayName] == nil)
            comparison = NSOrderedAscending;
          else if ([otherFolder displayName] == nil)
            comparison = NSOrderedDescending;
          else
            comparison
              = [[self displayName]
                  localizedCaseInsensitiveCompare: [otherFolder displayName]];
        }
    }

  return comparison;
}

/* email advisories */

- (void) sendFolderAdvisoryTemplate: (NSString *) template
{
  NSString *pageName;
  SOGoUser *user;
  SOGoFolderAdvisory *page;
  NSString *language;

  user = [SOGoUser userWithLogin: [self ownerInContext: context]];
  if ([[user domainDefaults] foldersSendEMailNotifications])
    {
      language = [[user userDefaults] language];
      pageName = [NSString stringWithFormat: @"SOGoFolder%@%@Advisory",
			   language, template];

      page = [[WOApplication application] pageWithName: pageName
					  inContext: context];
      [page setFolderObject: self];
      [page setRecipientUID: [user login]];
      [page send];
    }
}

/* WebDAV */

- (NSString *) davEntityTag
{
  return @"\"None\"";
}

- (NSString *) davContentType
{
  return @"httpd/unix-directory";
}

- (NSArray *) davResourceType
{
  NSArray *rType, *groupDavCollection;

  if ([self respondsToSelector: @selector (groupDavResourceType)])
    {
      groupDavCollection
	= [NSArray arrayWithObjects: [self groupDavResourceType],
		   XMLNS_GROUPDAV, nil];
      rType = [NSArray arrayWithObjects: @"collection", groupDavCollection,
		       nil];
    }
  else
    rType = [NSArray arrayWithObject: @"collection"];

  return rType;
}

- (BOOL) davIsCollection
{
  return YES;
}

- (NSArray *) _extractHREFSFromPropertyValues: (NSArray *) values
{
  NSMutableArray *hrefs;
  NSDictionary *value;
  int count, max;

  max = [values count];
  hrefs = [NSMutableArray arrayWithCapacity: max];
  for (count = 0; count < max; count++)
    {
      value = [values objectAtIndex: count];
      if ([value isKindOfClass: [NSDictionary class]])
        {
          if ([[value objectForKey: @"method"] isEqualToString: @"href"])
            [hrefs addObject: [value objectForKey: @"content"]];
          else
            [self errorWithFormat: @"value is not an href"];
        }
      else if ([value isKindOfClass: [NSString class]])
        {
          /* we extract the text between <href>XXX</href>, but in a bad way */
          [hrefs addObject: [(NSString *) value removeOutsideTags]];
        }
      else
        [self errorWithFormat: @"value class is '%@' instead of NSDictionary",
              NSStringFromClass ([value class])];
    }

  return hrefs;
}

- (NSArray *) _interpretSoWebDAVValue: (SoWebDAVValue *) value
{
  NSString *valueString;

  valueString = [value stringForTag: @"" rawName: @""
                          inContext: context prefixes: nil];

  return [NSArray arrayWithObject: [valueString removeOutsideTags]];
}

- (NSArray *) _interpretWebDAVValue: (id) value
{
  NSArray *result;

  if ([value isKindOfClass: [NSString class]])
    result = [NSArray arrayWithObject: value];
  else if ([value isKindOfClass: [SoWebDAVValue class]])
    result = [self _interpretSoWebDAVValue: value];
  else if ([value isKindOfClass: [NSArray class]])
    result = [self _interpretWebDAVArrayValue: value];
  else
    result = nil;

  return result;
}

- (NSArray *) _interpretWebDAVArrayValue: (id) value
{
  int count, max;
  NSMutableArray *results;
  id subValue, subResults;

  max = [value count];
  results = [NSMutableArray arrayWithCapacity: max];
  if (max > 0)
    {
      subValue = [value objectAtIndex: 0];
      if ([subValue isKindOfClass: [NSString class]])
        [results addObject:
                   davElementWithContent (subValue,  
                                          [value objectAtIndex: 1],
                                          [value objectAtIndex: 3])];
      else
        {
          for (count = 0; count < max; count++)
            {
              subResults
                = [self _interpretWebDAVValue: [value objectAtIndex: count]];
              [results addObjectsFromArray: subResults];
            }
        }
    }

  return results;
}

- (NSArray *) _expandedPropertyValue: (NGDOMElement *) property
                           forObject: (SOGoObject *) currentObject
{
  NSString *propertyTag;
  SEL propertySel;
  id value;

  propertyTag = [property asPropertyPropertyName];
  propertySel = [self davPropertySelectorForKey: propertyTag];
  if (propertySel)
    value = [currentObject performSelector: propertySel];
  else
    value = nil;

  return [self _interpretWebDAVValue: value];
}

- (NSArray *) _expandPropertyValue: (NGDOMElement *) property
                         forObject: (SOGoObject *) currentObject
{
  NSArray *values, *hrefs;
  NSString *href;
  NSMutableArray *expandedValues;
  int count, max;
  BOOL needsExpansion;

  needsExpansion = ([[property childElementsWithTag: @"property"] length] > 0);
  values = [self _expandedPropertyValue: property
                              forObject: currentObject];
  max = [values count];
  expandedValues = [NSMutableArray arrayWithCapacity: max];
  if (max)
    {
      if (needsExpansion)
        {
          hrefs = [self _extractHREFSFromPropertyValues: values];
          max = [hrefs count];
          for (count = 0; count < max; count++)
            {
              href = [hrefs objectAtIndex: count];
              [expandedValues addObject: [self _expandPropertyResponse: property
                                                               forHREF: href]];
            }
        }
      else
        [expandedValues setArray: values];
    }

  return expandedValues;
}

- (NSDictionary *) _expandPropertyResponse: (NGDOMElement *) property
                                 forObject: (SOGoObject *) currentObject
{
  id <DOMNodeList> properties;
  NSArray *childValue;
  NGDOMElement *childProperty;
  NSDictionary *response;
  NSMutableArray *properties200, *properties404;
  int count, max;
  NSString *tagName, *tagNS;

  properties = [property childElementsWithTag: @"property"];
  max = [properties length];
  properties200 = [NSMutableArray arrayWithCapacity: max];
  properties404 = [NSMutableArray arrayWithCapacity: max];
  for (count = 0; count < max; count++)
    {
      childProperty = [properties objectAtIndex: count];
      childValue = [self _expandPropertyValue: childProperty
                                    forObject: currentObject];
      tagNS = [childProperty attribute: @"namespace"];
      if (!tagNS)
        tagNS = XMLNS_WEBDAV;
      tagName = [childProperty attribute: @"name"];
      if (childValue)
        [properties200 addObject: davElementWithContent (tagName,
                                                         tagNS,
                                                         childValue)];
      else
        [properties404 addObject: davElement (tagName, tagNS)];
    }
  response = [self responseForURL: [currentObject davURLAsString]
                withProperties200: properties200
                 andProperties404: properties404];

  return response;
}

- (NSDictionary *) _expandPropertyResponse: (NGDOMElement *) property
                                   forHREF: (NSString *) href
{
  SOGoObject *lookupObject;
  NSDictionary *response;

  lookupObject = [self lookupObjectAtDAVUrl: href];
  if (lookupObject)
    response = [self _expandPropertyResponse: property
                                   forObject: lookupObject];
  else
    response = nil;

  return response;
}

- (WOResponse *) davExpandProperty: (WOContext *) localContext
{
  WOResponse *r;
  id <DOMDocument> document;
  NSDictionary *response, *multistatus;
  NGDOMElement *documentElement;

  r = [localContext response];
  [r prepareDAVResponse];

  document = [[context request] contentAsDOMDocument];
  documentElement = (NGDOMElement *) [document documentElement];
  response = [self _expandPropertyResponse: documentElement forObject: self];
  multistatus = davElementWithContent (@"multistatus", XMLNS_WEBDAV,
                                       response);
  [r appendContentString: [multistatus asWebDavStringWithNamespaces: nil]];

  return r;
}

/* web dav acl helper */
- (void) _fillArrayWithPrincipalsOwnedBySelf: (NSMutableArray *) hrefs
{
  NSEnumerator *children;
  NSString *currentKey;

  [super _fillArrayWithPrincipalsOwnedBySelf: hrefs];
  children = [[self toOneRelationshipKeys] objectEnumerator];
  while ((currentKey = [children nextObject]))
    [[self lookupName: currentKey inContext: context
	   acquire: NO] _fillArrayWithPrincipalsOwnedBySelf: hrefs];

  children = [[self toManyRelationshipKeys] objectEnumerator];
  while ((currentKey = [children nextObject]))
    [[self lookupName: currentKey inContext: context
	   acquire: NO] _fillArrayWithPrincipalsOwnedBySelf: hrefs];
}


- (NSArray *) davGroupMemberSet
{
  return [NSArray array];
}

- (NSArray *) davGroupMembership
{
  return [NSArray array];
}

/* folder type */

- (BOOL) isEqual: (id) otherFolder
{
  return ([otherFolder class] == [self class]
	  && [container isEqual: [otherFolder container]]
	  && [nameInContainer
	       isEqualToString: [otherFolder nameInContainer]]);
}

/* acls */

- (NSString *) defaultUserID
{
  return nil;
}

- (NSArray *) subscriptionRoles
{
  return [NSArray arrayWithObjects: SoRole_Owner, SOGoRole_ObjectViewer,
		  SOGoRole_ObjectEditor, SOGoRole_ObjectCreator,
		  SOGoRole_ObjectEraser, nil];
}

- (NSArray *) aclsForUser: (NSString *) uid
{
  return nil;
}

- (NSArray *) aclUsers
{
  return nil;
}

- (NSArray *) davPrincipalURL
{
  NSArray *principalURL;
  NSString *classes;

  if ([[context request] isICal4])
    {
      classes = [[self davComplianceClassesInContext: context]
                  componentsJoinedByString: @", "];
      [[context response] setHeader: classes forKey: @"DAV"];
    }

  principalURL = [NSArray arrayWithObjects: @"href", @"DAV:", @"D",
                          [self davURLAsString], nil];

  return [NSArray arrayWithObject: principalURL];
}

@end