This repository has been archived by the owner on Jun 15, 2022. It is now read-only.
/
sharepoint.php
1326 lines (1181 loc) · 43.7 KB
/
sharepoint.php
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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
\mainpage
@version v0.90 september 2005
@author Jesper Balle jb@kfumspejderne.dk
Forbindelse til sharepoint vha. web services.
*/
if (!defined('_SHAREPOINT_LAYER')) {
define('_SHAREPOINT_LAYER',1);
//==============================================================================================
// CONSTANT DEFINITIONS
//==============================================================================================
// Pt. anvendes NuSoap http://sourceforge.net/projects/nusoap/
// Man kan evt. overveje at kigge på PEARs soap-klienter i stedet
include_once("/home/wwwsites/generel/shared/nusoap/nusoap.php");
//==============================================================================================
// CLASS SharepointSite
//==============================================================================================
class SharepointSite {
//
// PUBLIC VARS
//
var $debug = 0; // Debug level
/* 0: intet output
1: fejl vises
3: alle funktionskald vises
4: ved fejl vises også kommunikation
5: alle funktionskald vises og ved fejl også kommunikation
' 6: alt kommunikation vises
*/
//
// PRIVATE VARS
//
var $soapUser = null; // Hvilken bruger skal forbinde til web service
var $soapPass = null; // Password tilhørende brugeren
var $sitePath = "/gruppe/gruppenavn"; // relativ sti til sharepoint site
var $siteHost = "sps.spejdernet.dk "; // sharepoint server hostname
var $siteProt = "http"; // protokol til sharepoint site
var $siteDomain = "SPEJDERNET"; // domain for the site
/**
* Konstruktør
*/
function SharepointSite($site, $user=null, $pass=null) {
$this->setSite($site);
$this->soapUser = $user;
$this->soapPass = $pass;
}
/**
* Angiv adresse for Sharepoint sitet
* @param string $site Url til sitet. Enten som en komplet url (http://sps.spejdernet.dk/gruppe/gruppenavn),
uden protokol (sps.spejdernet.dk/gruppe/gruppenavn) eller relativt (/gruppe/gruppenavn).
* @access public
*/
function setSite($site) {
$site = trim($site);
if ($pos = strpos($site, "://")) {
$this->siteProt = trim(substr($site, 0, $pos));
$site = substr($site, $pos+3);
$pos = strpos($site, "/");
$this->setHost(substr($site, 0, $pos));
$this->sitePath = trim(substr($site, $pos));
} elseif (substr($site,0,1)!="/") {
$pos = strpos($site,"/");
$this->setHost(substr($site, 0, $pos));
$this->sitePath = trim(substr($site, $pos+1));
} else {
$this->sitePath = trim($site);
}
if (substr($this->sitePath, -1)=="/")
$this->sitePath = trim(substr($this->sitePath, 0, strlen($this->sitePath)-1));
}
/**
* Sæt servernavn for Sharepoint sitet.
* @param string $host servernavn eks. sps.spejdernet.dk
* @access private
*/
function setHost($host) {
$this->siteHost = trim($host);
$array = explode(".", $host);
$this->siteDomain = strtoupper($array[sizeof($array)-2]);
}
/**
* Den fulde adresse på sitet (uden afsluttende '/').
* @return string url
* @access private
*/
function getUrl() {
return $this->siteProt."://".trim($this->siteHost).trim($this->sitePath);
}
//==============================================================================================
// LISTEINDHOLD
//==============================================================================================
/**
* Returner indholdet af en liste
* @access public
*/
function listContents($var1, $var2=null) {
$this->_debugStart("listContent({$list})");
$inputfail = "Enten gives et listQuery-objekt, eller en tekststreng med listens id som parameter (evt. suppleret med et heltal for antallet af liste-elementer der må returneres).";
// valider og parse input
switch(gettype($var1)) {
case "string": // det er en tekststreng
$query = new listQuery($var1);
$query->limitRows($var2);
break;
case "object": // det er måske en listQuery
if(get_class($var1)!="listquery")
return $this->_debugError(10, $inputfail);
else $query = $var1;
break;
default:
return $this->_debugError(10, $inputfail);
}
// generer forespørgsel
$soapPath = $this->getUrl() . "/_vti_bin/DspSts.asmx";
$soapAction = "http://schemas.microsoft.com/sharepoint/dsp/queryRequest";
$soapHeader = '<request document="content" method="query" xmlns="http://schemas.microsoft.com/sharepoint/dsp" />'
. ' <versions xmlns="http://schemas.microsoft.com/sharepoint/dsp">'
. ' <version>1.0</version>'
. ' </versions>';
$soapBody = $query->_makeBody();
// Udfør forespørgsel
$result = $this->_doCall($soapPath, $soapAction, $soapBody, $soapHeader);
if($result !== null) return $result;
// UDTRÆK OG BEARBEJD RESULTAT
$dsQueryResponse = $this->result[1]["children"][0]["children"][0];
if($dsQueryResponse["attrs"]["status"]!="success")
return $this->_debugError(5, $dsQueryResponse["attrs"]["status"]);
// Lige lidt så vi kan finde rundt i resultatet
$rows = $dsQueryResponse["children"][1]["children"];
$skema = $dsQueryResponse["children"][0];
$field = $skema["children"][0]["children"][0]["children"][0]["children"][0]["children"][0]["children"];
// Felttyper
$fields = array();
if(is_array($field))
foreach($field as $fieldtype) {
$key = $this->_normalizeKey($fieldtype["attrs"]["name"]);
$row = array();
$row["displayname"] = $this->_normalizeString($fieldtype["attrs"]["d:displayName"]);
$row["fieldtype"] = isset($fieldtype["attrs"]["type"]) ? $fieldtype["attrs"]["type"] : null;
$fields[$key] = $row;
}
// Data
$result = array();
if(is_array($rows))
foreach($rows as $row) {
$resultRow = array();
foreach($row["attrs"] as $key => $oldvalue) {
$newKey = $this->_normalizeKey($key);
switch ($fields[$newKey]["fieldtype"]) {
case "x:int": $value = $this->_normalizeInt($oldvalue); break;
case "x:float": $value = $this->_normalizeDouble($oldvalue); break;
case "x:boolean": $value = $this->_normalizeBoolean($oldvalue, true); break;
case "x:dateTime": $value = $this->_normalizeDate($oldvalue); break;
case "x:string": $value = $this->_normalizeString($oldvalue); break;
default: $value = $this->_normalize($oldvalue); break;
}
$resultRow[$newKey] = $value;
$resultRow[ $fields[$newKey]["displayname"] ] = $value;
}
$result[] = $resultRow;
}
// Returner
$this->_debugEnd();
return $result;
}
/**
* Indsæt eller rediger listeelementer
* @param string $list GUID for den pågældende liste.
* @param array $values Oplysningerne som skal indsættes. Feltnavnet som key og værdien som value.
* @param int $id hvis man redigerer et element, skrives dets ID her. Ellers null (eller undlad parametren).
* @access public
*/
function listItems($list, $values, $id=null) {
$this->_debugStart("listItems(".substr($list,0,5)."..., {$values}, {$id})");
// valider input
if(!is_array($values))
return $this->_debugError(10, "\$values skal være et array");
if(is_array($values[0]) && (!is_array($id) && $id!=null))
return $this->_debugError(10, "ved redigering af flere elementer skal disses id'er angives i et array");
if(is_array($id)) if(sizeof($values)!=sizeof($id))
return $this->_debugError(10, "ved redigering af flere elementer skal alle deres tilhørende id'er angives i et array");
// Opbyg forespørgsel
$soapPath = $this->getUrl()."/_vti_bin/Lists.asmx";
$soapAction = "http://schemas.microsoft.com/sharepoint/soap/UpdateListItems";
$br = "\n";
$soapBody = '<UpdateListItems xmlns="http://schemas.microsoft.com/sharepoint/soap/">'.$br
. ' <listName>'.$list.'</listName>'.$br
. ' <updates>'.$br
. ' <Batch OnError="Continue" >'.$br;
if(is_array($values[0])) {
// Flere elementer samtidigt
for($i=0; $i<sizeof($values); $i++) {
if(is_array($thisvalue = $values[$i])) {
if(is_array($id))
$soapBody .= ' <Method ID="'.($i+1).'" Cmd="Update">'.$br
. ' <Field Name="ID">'.$id[$i].'</Field>'.$br;
else
$soapBody .= ' <Method ID="'.($i+1).'" Cmd="New">'.$br;
foreach($thisvalue as $key => $value) {
$soapBody .= ' <Field Name="'.$key.'">';
if($value === true) $soapBody .= "1";
else if($value === false) $soapBody .= "0";
else $soapBody .= stripslashes($value);
$soapBody .= '</Field>'.$br;
}
$soapBody .= ' </Method>'.$br;
}
}
} else {
// Kun et enkelt element
if($id != null)
$soapBody .= ' <Method ID="1" Cmd="Update">'.$br
. ' <Field Name="ID">'.$id.'</Field>'.$br;
else
$soapBody .= ' <Method ID="1" Cmd="New">'.$br;
foreach($values as $key => $value) {
$soapBody .= ' <Field Name="'.$key.'">';
if($value === true) $soapBody .= "1";
else if($value === false) $soapBody .= "0";
else $soapBody .= stripslashes($value);
$soapBody .= '</Field>'.$br;
}
$soapBody .= ' </Method>'.$br;
}
$soapBody .= ' </Batch>'.$br
. ' </updates>'.$br
. '</UpdateListItems>';
// Udfør forespørgsel
$result = $this->_doCall($soapPath, $soapAction, $soapBody);
if($result !== null) return $result;
// Udtræk resultatet
$results = $this->result[0]["children"][0]["children"][0]["children"][0]["children"];
$result = array();
foreach($results as $elementResult) {
$resultRow = array();
$resultRow["errorcode"] = $elementResult["children"][0]["chardata"];
$resultRow["result"] = ($resultRow["errorcode"]=="0x00000000") ? true : false;
$row = array();
foreach($elementResult["children"][ sizeof($elementResult["children"])-1 ]["attrs"] as $key => $value)
if($key != "xmlns:z")
$row[ $this->_normalizeKey($key) ] = $this->_normalize($value);
$resultRow["row"] = $row;
$result[] = $resultRow;
}
if(sizeof($result)==1)
$result = $result[0];
// returner
$this->_debugEnd();
return $result;
}
//==============================================================================================
// VEDHÆFTEDE FILER
//==============================================================================================
/**
* Returner vedhæftede filer fra et listeelement
* @param string $list GUID for den pågældende liste
* @param int $id Det pågældende elements ID.
* @access public
*/
function listItemAttachments($list, $id) {
$this->_debugStart("getAttachments(".substr($list,0,7)."..., {$id})");
// Opbyg forespørgsel
$soapAction = "http://schemas.microsoft.com/sharepoint/soap/GetAttachmentCollection";
$soapBody = "<GetAttachmentCollection xmlns=\"http://schemas.microsoft.com/sharepoint/soap/\">\n"
. " <listName>{$list}</listName>\n"
. " <listItemID>{$id}</listItemID>\n"
. "</GetAttachmentCollection>";
$soapPath = $this->getUrl()."/_vti_bin/Lists.asmx";
// Udfør forespørgsel
$result = $this->_doCall($soapPath, $soapAction, $soapBody);
if($result != null) return $result;
// Udtræk resultatet
$result = array();
$nodes = $this->result[0]["children"][0]["children"][0]["children"][0]["children"];
if(is_array($nodes))
foreach($nodes as $row)
$result[] = $row["chardata"];
// Returner
$this->_debugEnd();
return $result;
}
//==============================================================================================
// LISTER
//==============================================================================================
/**
* Oplysninger om samtlige lister og dokumentbiblioteker på sitet
* @access public
*/
function listCollection() {
$this->_debugStart("getListCollection()");
// Opbyg forespørgsel
$soapPath = $this->getUrl()."/_vti_bin/Lists.asmx";
$soapAction = "http://schemas.microsoft.com/sharepoint/soap/GetListCollection";
$soapBody = "<GetListCollection xmlns=\"http://schemas.microsoft.com/sharepoint/soap/\" />";
// Udfør forespørgsel
$this->_doCall($soapPath, $soapAction, $soapBody);
if(!is_array($this->result)) return $this->result;
// Udtræk resultatet
$result = array();
$lists = $this->result[0]["children"][0]["children"][0]["children"][0]["children"];
foreach($lists as $row) {
$list = array();
foreach($row["attrs"] as $key => $value)
$list[ $this->_normalizeKey($key)] = $this->_normalize($value);
$result[] = $list;
}
$this->_debugEnd();
return $result;
}
/**
* Alle oplysninger om en given liste
* @param string $listName Nanvet på listen
* @access public
*/
function getList($listName) {
$this->_debugStart("getLists({$listName})");
// Opbyg forespørgsel
$soapPath = $this->getUrl()."/_vti_bin/Lists.asmx";
$soapAction = "http://schemas.microsoft.com/sharepoint/soap/GetList";
$soapBody = '<GetList xmlns="http://schemas.microsoft.com/sharepoint/soap/">'.$br
. ' <listName>'.$listName.'</listName>'.$br
. '</GetList>';
// Udfør forespørgsel
var_dump($this->_doCall($soapPath, $soapAction, $soapBody, null));
// Returner
$this->_debugEnd();
return $result;
}
function listViewCollection($listGuid) {
$this->_debugStart("viewCollection('{$listGuid}')");
// Opbyg forespørgsel
$soapAction = "http://schemas.microsoft.com/sharepoint/soap/GetViewCollection";
$soapBody = '<GetViewCollection xmlns="http://schemas.microsoft.com/sharepoint/soap/">'.$br
. ' <listName>'.$listGuid.'</listName>'.$br
. '</GetViewCollection>';
$soapPath = $this->getUrl()."/_vti_bin/Views.asmx";
// Udfør forespørgsel
$result = $this->_doCall($soapPath, $soapAction, $soapBody);
if($result != null) return $result;
// Udtræk resultatet
$result = array();
$nodes = $this->result[0]["children"][0]["children"][0]["children"][0]["children"];
foreach($nodes as $row) {
$view = array();
foreach($row["attrs"] as $key=>$value)
$view[$this->_normalizeKey($key)] = $this->_normalize($value);
$result[] = $view;
}
// Returner
$this->_debugEnd();
return $result;
}
//==============================================================================================
// BILLEDER
//==============================================================================================
function uploadImageFile($libraryName, $filename, $picturePath, $folder="", $overwrite=false) {
$this->_debugStart("uploadImageFile ('".$libraryName."', '{$filename}', '".substr($picturePath,0,25)."', '{$folder}', '{$overwrite}')");
if(!file_exists($picturePath))
return $this->_debugError(10, "Den angivne fil ({$picturePath}) findes ikke.");
$data = file_get_contents($picturePath);
if(!$data)
return $this->_debugError(10, "Der kunne ikke indlæses nogen data fra den angivne fil");
return $this->uploadImage($libraryName, $filename, base64_encode($data), $folder, $overwrite);
}
function uploadImage($libraryName, $filename, $picture_data, $folder="", $overwrite=false) {
$this->_debugStart("uploadImage ('".$libraryName."', '{$filename}', '".substr($picture_data,0,25)."', '{$folder}', '{$overwrite}')");
// opbyg forespørgsel
$soapPath = $this->getUrl()."/_vti_bin/Imaging.asmx";
$soapAction = "http://schemas.microsoft.com/sharepoint/soap/ois/Upload";
$soapBody = "<Upload xmlns=\"http://schemas.microsoft.com/sharepoint/soap/ois/\">\n"
. " <strListName>".$libraryName."</strListName>\n"
. " <strFolder>".$folder."</strFolder>\n"
. " <bytes>".$picture_data."</bytes>\n"
. " <fileName>".$filename."</fileName>\n"
. " <fOverWriteIfExist>";
$soapBody .= ($overwrite) ? "true" : "false";
$soapBody .="</fOverWriteIfExist>\n"
. "</Upload>";
// Udfør forespørgsel
$result = $this->_doCall($soapPath, $soapAction, $soapBody);
if($result !== null) return $result;
// Udtræk resultatet
$result = array();
$upload = $this->result[0]["children"][0]["children"][0]["children"][0];
foreach($upload["attrs"] as $key=>$value)
if($key!="xmlns")
$result[ $this->_normalizeKey($key) ] = $this->_normalize($value);
// returner
$this->_debugEnd();
return $result;
}
function createImgNewFolder($list, $parentfolder) {
$this->_debugStart("createImgNewFolder ('{$list}', '{$parentfolder}')");
// opbyg forespørgsel
$soapPath = $this->getUrl()."/_vti_bin/Imaging.asmx";
$soapAction = "http://schemas.microsoft.com/sharepoint/soap/ois/CreateNewFolder";
$soapBody = "<CreateNewFolder xmlns=\"http://schemas.microsoft.com/sharepoint/soap/ois/\">\n"
. " <strListName>".$list."</strListName>\n"
. " <strParentFolder>".$folder."</strParentFolder>\n"
. "</CreateNewFolder>";
// Udfør forespørgsel
$result = $this->_doCall($soapPath, $soapAction, $soapBody);
if($result !== null) return $result;
// Udtræk resultatet
$result = $this->result[0]["children"][0]["children"][0]["children"][0]["attrs"]["title"];
// returner
$this->_debugEnd();
return $result;
}
function renameImg($list, $folder, $renames) {
$this->_debugStart("renameImg('{$list}', '{$folder}', '{$renames}')");
// valider input
if(!is_array($renames)) return $this->_debugError(10, "renames skal være et array à la 'gammel navn => ny navn'");
// opbyg forespørgsel
$soapPath = $this->getUrl()."/_vti_bin/Imaging.asmx";
$soapAction = "http://schemas.microsoft.com/sharepoint/soap/ois/Rename";
$soapBody = '<Rename xmlns="http://schemas.microsoft.com/sharepoint/soap/ois/">'."\n"
. ' <strListName>'.$list.'</strListName>'."\n"
. ' <strFolder>'.$folder.'</strFolder>'."\n"
. ' <request>'."\n"
. ' <files>'."\n";
foreach($renames as $key=>$value)
$soapBody .=" <file filename=\"{$key}\" newbasename=\"{$value}\"/>\n";
$soapBody .=' </files>'."\n"
. ' </request>'."\n</Rename>";
// Udfør forespørgsel
$result = $this->_doCall($soapPath, $soapAction, $soapBody);
if($result !== null) return $result;
// Udtræk resultatet
$result = $this->result[0]["children"][0]["children"][0]["children"][0]["children"];
$retur = array();
foreach($result as $i)
$retur[] = $i["attrs"];
// returner
$this->_debugEnd();
return $retur;
}
function createImgFolder($list, $parentFolder, $folderName) {
$this->_debugStart("createImgFolder('{$list}', '{$parentFolder}', '{$folderName}')");
$tmp = $this->createImgNewFolder($list, $parentfolder);
$result = $this->renameImg($list, $parentFolder, array($tmp=>$folderName));
// returner
$this->_debugEnd();
return $result;
}
function imageFolderContents($listname, $folder) {
$this->_debugStart("renameImg('{$list}', '{$folder}', '{$renames}')");
// opbyg forespørgsel
$soapPath = $this->getUrl()."/_vti_bin/Imaging.asmx";
$soapAction = "http://schemas.microsoft.com/sharepoint/soap/ois/GetListItems";
$soapBody = '<GetListItems xmlns="http://schemas.microsoft.com/sharepoint/soap/ois/">'."\n"
. ' <strListName>'.$listname.'</strListName>'."\n"
. ' <strFolder>'.$folder.'</strFolder>'."\n"
. '</GetListItems>';
// Udfør forespørgsel
$result = $this->_doCall($soapPath, $soapAction, $soapBody);
if($result !== null) return $result;
// Udtræk resultatet
$result = $this->result[0]["children"][0]["children"][0]["children"][0]["children"];
$retur = array();
foreach($result as $i) {
$row = array();
foreach($i["attrs"] as $key=>$value)
if($key != "xmlns:z")
$row[ $this->_normalizeKey($key) ] = $this->_normalize($value);
$retur[] = $row;
}
// returner
$this->_debugEnd();
return $retur;
}
//==============================================================================================
// USER INFO
//==============================================================================================
/**
* Oplysninger om en given bruger som findes på sitet
* @param string $username Brugerens login
* @access public
*/
function userInfo($username) {
$this->_debugStart("userInfo({$username})");
// Opbyg forespørgsel
$soapPath = $this->getUrl()."/_vti_bin/UserGroup.asmx";
$soapAction = "http://schemas.microsoft.com/sharepoint/soap/directory/GetUserInfo";
$soapBody = '<GetUserInfo xmlns="http://schemas.microsoft.com/sharepoint/soap/directory/">'
. ' <userLoginName>'.$this->_domainAdd($username).'</userLoginName>'
. '</GetUserInfo>';
// Udfør forespørgsel
$result = $this->_doCall($soapPath, $soapAction, $soapBody);
if($result !== null) return $result;
// Udtræk resultatet
$result = array();
$attributes = $this->result[0]["children"][0]["children"][0]["children"][0]["children"][0]["attrs"];
foreach($attributes as $key=>$value)
$result[$this->_normalizeKey($key)] = $value;
$this->_debugEnd();
return $result;
}
/**
* En brugers profil-oplysninger
* @param string $username Brugerens login
* @access public
*/
function userProfile($username) {
$this->_debugStart("userProfile({$username})");
// Opbyg forespørgsel
$soapAction = "http://microsoft.com/webservices/SharePointPortalServer/UserProfileService/GetUserProfileByName";
$soapBody = '<GetUserProfileByName xmlns="http://microsoft.com/webservices/SharePointPortalServer/UserProfileService">'
. ' <AccountName>'.$this->_domainRemove($username).'</AccountName>'
. '</GetUserProfileByName>';
$soapPath = "http://sps.spejdernet.dk"."/_vti_bin/UserProfileService.asmx";
// Udfør forespørgsel
$result = $this->_doCall($soapPath, $soapAction, $soapBody);
if($result != null) return $result;
// Udtræk resultatet
$result = array();
$nodes = $this->result[0]["children"][0]["children"][0]["children"];
if(is_array($nodes))
foreach($nodes as $property)
$result[$property["children"][0]["chardata"]] = $this->_normalize($property["children"][1]["chardata"]);
// Returner
$this->_debugEnd();
return $result;
}
//==============================================================================================
// TVÆRGÅENDE WEBSTEDSGRUPPER
//==============================================================================================
/**
* Opret en tværgående webstedsgruppe på Sharepoint sitet
* @param string $groupName Navnet på den nye tværgående webstedsgruppe
* @param string $ownerIdent Hvem ejer webstedsgruppen - brugernavn eller navn på anden webstedgruppe
* @param string $ownerType Hvad ejer webstedsgruppen - "group" eller "user"
* @param string $defUserLogin Startmedlem - brugernavn
* @param string $description Beskrivelse til webstedsgruppen
* @access public
*/
function addGroup($groupName, $ownerIdent, $ownerType, $defUserLogin, $description="") {
$this->_debugStart("addGroup({$groupName}, {$ownerIdent}, {$ownerType}, {$defUserLogin}, {$description})");
// Opbyg forespørgsel
$soapPath = $this->getUrl() . "/_vti_bin/UserGroup.asmx";
$soapAction = "http://schemas.microsoft.com/sharepoint/soap/directory/AddGroup";
$soapBody = "<AddGroup xmlns=\"http://schemas.microsoft.com/sharepoint/soap/directory/\">\n"
. " <groupName>".$groupName."</groupName>\n"
. " <ownerIdentifier>".$ownerIdent."</ownerIdentifier>\n"
. " <ownerType>".$ownerType."</ownerType>\n"
. " <defaultUserLoginName>".$defUserLogin."</defaultUserLoginName>\n"
. " <description>".$description."</description>\n"
. "</AddGroup>";
// Udfør forespørgsel
$result = $this->_doCall($soapPath, $soapAction, $soapBody);
if(!($result === null)) return $result;
// Returner
$this->_debugEnd();
return true;
}
/**
* Hvilke tværgående webstedsgrupper en bruger er medlem af.
* @param string $username Brugerens login
* @access public
*/
function groupCollectionFromUser($username) {
$this->_debugStart("groupCollectionFromUser({$userName})");
// Opbyg forespørgsel
$soapPath = $this->getUrl() . "/_vti_bin/UserGroup.asmx";
$soapAction = "http://schemas.microsoft.com/sharepoint/soap/directory/GetGroupCollectionFromUser";
$soapBody = '<GetGroupCollectionFromUser xmlns="http://schemas.microsoft.com/sharepoint/soap/directory/">'
. ' <userLoginName>'.$this->_domainAdd($username).'</userLoginName>'
. '</GetGroupCollectionFromUser>';
// Udfør forespørgsel
$this->_doCall($soapPath, $soapAction, $soapBody);
if(!is_array($this->result)) return $this->result;
// Udtræk resultatet
$groups = $this->result[0]["children"][0]["children"][0]["children"][0]["children"][0]["children"];
$result = array();
foreach($groups as $group) {
$groupinfo = array();
foreach($group["attrs"] as $key => $value)
$groupinfo[$this->_normalizeKey($key)] = $this->_normalize($value);
$result[$groupinfo["name"]] = $groupinfo;
}
// Returner
$this->_debugEnd();
return $result;
}
/**
* Tilføj en bruger til en tværgående webstedsgruppe
* @param string $groupName Den tværgående webstedgruppes navn
* @param string $userName Brugerens navn (displayname)
* @param string $userLogin Brugerens login
* @param string $userEmail Brugerens e-mail adresse
* @param string $userNotes Noter for brugeren
* @access public
*/
function addUserToGroup($groupName, $userName, $userLogin, $userEmail="", $userNotes="") {
$this->_debugStart("addUserToGroup({$groupName}, {$userName}, {$userLogin}, {$userEmail}, {$userNotes})");
// Opbyg forespørgsel
$br = "\n";
$soapPath = $this->getUrl() . "/_vti_bin/UserGroup.asmx";
$soapAction = "http://schemas.microsoft.com/sharepoint/soap/directory/AddUserToGroup";
$soapBody = '<AddUserToGroup xmlns="http://schemas.microsoft.com/sharepoint/soap/directory/">'.$br
. ' <groupName>'.$groupName.'</groupName>'.$br
. ' <userName>'.$userName.'</userName>'.$br
. ' <userLoginName>'.$this->_domainAdd($userLogin).'</userLoginName>'.$br
. ' <userEmail>'.$userEmail.'</userEmail>'.$br
. ' <userNotes>'.$userNotes.'</userNotes>'.$br
. '</AddUserToGroup>';
// Udfør forespørgsel
$result = $this->_doCall($soapPath, $soapAction, $soapBody);
if($result !== null) return $resultt;
// Udtræk resultatet
$this->_debugEnd();
return true;
}
/**
* Oplysninger om en given tværgående webstedsgruppe
* @param string $group
* @access public
*/
function groupInfo($group) {
$this->_debugStart("groupInfo({$group})");
// Opbyg forespørgsel
$soapPath = $this->getUrl()."/_vti_bin/UserGroup.asmx";
$soapAction = "http://schemas.microsoft.com/sharepoint/soap/directory/GetGroupInfo";
$soapBody = '<GetGroupInfo xmlns="http://schemas.microsoft.com/sharepoint/soap/directory/">\n'
. ' <groupName>'.$group.'</groupName>\n'
. '</GetGroupInfo>';
// Udfør forespørgsel
$result = $this->_doCall($soapPath, $soapAction, $soapBody);
if($result !== null) return $result;
// Udtræk resultatet
$result = array();
$attributes = $this->result[0]["children"][0]["children"][0]["children"][0]["children"][0]["attrs"];
foreach($attributes as $key => $value)
$result[strtolower($key)] = $this->_normalize($value);
// Returner
$this->_debugEnd();
return $result;
}
//==============================================================================================
// HELPER FUNCTIONS
//==============================================================================================
/**
* Fjern et evt. domæne fra et brugernavn
* @param string $username brugernavn der evt. indeholder domæne
* @return string brugernavnet uden domæne
* @access private
*/
function _domainRemove($username) {
// Fjern evt. domæne
if($pos = strpos($username, "\\"))
$username = substr($username, $pos+1);
return $username;
}
/**
* Tilføj domæne til et brugernavn
* @param string $username brugernavn der evt. indeholder domæne
* @return string brugernavnet med domæne
* @access private
*/
function _domainAdd($username) {
$search = strtolower($this->siteDomain)."\\";
$pos = strpos(strtolower($username), $search);
if($pos!==false)
return $username;
else
return $this->siteDomain."\\".$username;
}
// WEB SERVICE FUNCTIONS
var $client = null; // Active soapclient
/**
* Udfør et webservice kald og sørger for at svaret parses
* @param string $soapUrl adressen til den pågældende webservice
* @param string $soapAction angiver SOAPAction til webservicen
* @param string $soapBody forespørgsel der skal sendes til webservicen
* @param string @soapHeader evt. headere der skal sendes med forespørgslen til webservicen
* @access private
*/
function _doCall($soapUrl, $soapAction, $soapBody, $soapHeader=null) {
// Kald web service
$result = $this->_makeRequest($soapUrl, $soapAction, $soapBody, $soapHeader);
if(!($result === null)) return $result;
// Parse response
$result = $this->_parseResponse();
return $result;
}
/**
* Udfør et webservice kald
* @param string $soapUrl adressen til den pågældende webservice
* @param string $soapAction angiver SOAPAction til webservicen
* @param string $soapBody forespørgsel der skal sendes til webservicen
* @param string @soapHeader evt. headere der skal sendes med forespørgslen til webservicen
* @access private
*/
function _makeRequest($soapUrl, $soapAction, $soapBody, $soapHeader=null) {
// Opret web service klient
$this->client = new soapclient($soapUrl);
if ($err = $this->client->getError()) return $this->_debugError(1, $err);
//Credentials
if($this->soapUser)
$this->client->setCredentials($this->soapUser, $this->soapPass);
// Send request
$msg = $this->client->serializeEnvelope($soapBody, $soapHeader);
$this->client->send($msg,$soapAction);
if ($err = $this->client->getError()) return $this->_debugError(2, $err);
return null;
}
/**
* Udtrækker xml fra webservice svaret
* @return string xml
* @access private
*/
function _getXmlResponse() {
return substr($this->client->response, strpos($this->client->response, "<?xml") );
}
/**
* Parser webservice svar (herunder udtrækker xml)
* @access private
*/
function _parseResponse() {
$doc = $this->_getXmlResponse();
$result = $this->_parseXml($doc);
if ($result !== null) return $result;
$result = $this->_parseError();
if($result !== null) return $result;
}
function _parseError() {
if($this->result[0]["children"][0]["name"]=="soap:Fault") {
// Der opstod en fejl
$fejl = $this->result[0]["children"][0]["children"];
$detail = $fejl[2]["children"];
$errorstring = $this->_normalizeString($fejl[1]["chardata"]);
if(strlen($detail[0]["chardata"])>0)
$errorstring .= " - "
.$this->_normalizeString($detail[0]["chardata"]) // errorstring
." (".
$this->_normalizeString($detail[1]["chardata"]) // errorcode
.")";
return $this->_debugError(5, $errorstring);
} else
return null;
}
function _parseXml($doc) {
$parser = new xml2array($doc);
$result = $parser->parse();
if(!is_array($result)) return debugError(4, $result); // XML Parser error
else {
// vi gider ikke have SOAP:ENVELOPE med...
$this->result = $result[0]["children"];
return null;
}
}
//==============================================================================================
// Normalisering af tegn og typer
// Tilsyneladende opstår en fejl med danske tegn/special-tegn i forbindelse med kommunikationen
// - dette fikses her
//==============================================================================================
var $replaceChars = array (
"â" => "\"",
"æ" => "æ",
"ø" => "ø",
"Ã¥" => "å",
"Ã" => "Æ",
"Ã" => "Ø",
"Ã
" => "Å",
"â" => "-",
"é" => "é",
"'" => "'",
"â" => "á",
"Â" => "-",
"Ã" => "É"
);
function _normalizeBack($str) {
return str_replace( array_values($this->replaceChars),
array_keys($this->replaceChars),
$str
);
}
function _normalize($str) {
// datoer
$val = $this->_normalizeDate($str);
if($val!==null) {
return $val;
}
// heltal
$val = $this->_normalizeInt($str);
if(!($val===null))
return $val;
// decimaltal
$val = $this->_normalizeDouble($str);
if(!($val===null))
return $val;
// Boolean
$val = $this->_normalizeBoolean($str);
if(!($val===null))
return $val;
// tekst
return $this->_normalizeString($str);
}
function _normalizeString($str) {
foreach($this->replaceChars as $search => $replace)
$str = str_replace($search, $replace, $str);
return $str;
}
function _normalizeDate($datestr) {
$eregStr =
'([0-9]{4})-'. // centuries & years CCYY-
'([0-9]{2})-'. // months MM-
'([0-9]{2})'. // days DD
'T'. // separator T
'([0-9]{2}):'. // hours hh:
'([0-9]{2}):'. // minutes mm:
'([0-9]{2})';//(\.[0-9]+)?'. // seconds ss.ss...
// '(Z|[+\-][0-9]{2}:?[0-9]{2})?'; // Z to indicate UTC, -/+HH:MM:SS.SS... for local tz's
if(ereg($eregStr,$datestr,$regs)){
// not utc
if($regs[8] != 'Z'){
$op = substr($regs[8],0,1);
$h = substr($regs[8],1,2);
$m = substr($regs[8],strlen($regs[8])-2,2);
if($op == '-'){
$regs[4] = $regs[4] + $h;
$regs[5] = $regs[5] + $m;
} elseif($op == '+'){
$regs[4] = $regs[4] - $h;
$regs[5] = $regs[5] - $m;
}
}
// $result = strtotime("$regs[1]-$regs[2]-$regs[3] $regs[4]:$regs[5]:$regs[6]Z");
$result = mktime($regs[4], $regs[5], $regs[6], $regs[2], $regs[3], $regs[1]);
return $result;
} else {
return null;;
}
}
function _normalizeInt($str) {
$val = intval($str);
if($val."" === $str)
return $val;
else return null;
}
function _normalizeDouble($str) {
$val = doubleval($str);
if($val."" === $str)
return $val;
else return null;
}
function _normalizeBoolean($str, $explicit=false) {
if($explicit)
if($str == "1") return true;
elseif($str == "0") return false;
if($str=="True") return true;
elseif($str=="False") return false;
else return null;
}
function _normalizeKey($key) {
$newKey = str_replace("ows_","",$key );
return ereg_replace("_x[0-9a-f]{4}_","",$newKey);
}
//==============================================================================================
// DEBUGGING AND ERROR HANDLING
//==============================================================================================
var $debugFunction = null; // den kaldte funktion
var $errorcode = null; // den sidste fejlkode
var $errorstring = null; // den sidste fejltekst
/**
* Starter en funktions debugging
* @param string $func Angivelse af hvilken funktion der er blevet kaldt
* @access private
*/
function _debugStart($func) {
$this->result = null;
$this->client = null;
$this->errorcode = null;
$this->errorstring = null;
if($this->debugFunction==null)
$this->debugFunction = htmlentities($func);
else
$this->debugFuction = "Udefineret sharepoint funktion";
// vis alle funktionskald