File size: 13,549 Bytes
07c3cdd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<?php

class PmTrSharepointClass
{

    public function __construct($server, $auth)
    {
        set_include_path(PATH_PLUGINS . 'pmTrSharepoint' . PATH_SEPARATOR . get_include_path());
        $this->server = $server;
        $this->auth = $auth;
        $this->dwsObj = new wscaller();
        $this->dwsObj->setAuthUser($this->auth);
    }

    public function createDWS($name, $users, $title, $documents)
    {
        //print "<br>- Method createDWS";
        $this->dwsObj->setwsdlurl($this->server . "/_vti_bin/Dws.asmx?WSDL");

        $this->dwsObj->loadSOAPClient();

        $paramArray = array('name' => '', 'users' => '', 'title' => $name, 'documents' => '');

        $methodName = 'CreateDws';

        $result = $this->dwsObj->callWsMethod($methodName, $paramArray);
        $xml = $result->CreateDwsResult; // in Result we get string in Xml format
        $xmlNew = simplexml_load_string($xml); // used to parse string to xml
        $xmlArray = @G::json_decode(@G::json_encode($xmlNew), 1); // used to convert Objects to array
        $dwsUrl = $xmlArray['Url'];
        return "Dws with following Url is created:$dwsUrl";

        /* $newResult = $result->CreateDwsResult;
          $needleStart='<Url>';
          $urlStartPos = strpos($newResult, $needleStart);
          $urlStart = $urlStartPos + 5;
          $needleEnd='</Url>';
          $urlEndPos = strpos($newResult, $needleEnd);
          $length = $urlEndPos - $urlStart;
          $result = substr($newResult, $urlStart, $length);
          return $result; */
    }

    public function deleteDWS($dwsname)
    {
        //print "<br>- Method createDWS";
        $url = $this->server . "/" . $dwsname . "/_vti_bin/Dws.asmx?WSDL";
        $this->dwsObj->setwsdlurl($url);

        $this->dwsObj->loadSOAPClient();
        $paramArray = null;
        $methodName = 'DeleteDws';
        $result = $this->dwsObj->callWsMethod($methodName, $paramArray = null);
        var_dump($result);
        return $result;
    }

    public function createFolderDWS($dwsname, $dwsFolderName)
    {
        //print "<br>- Method createDWS";
        $this->dwsObj->setwsdlurl($this->server . "/" . $dwsname . "/_vti_bin/Dws.asmx?WSDL");

        $this->dwsObj->loadSOAPClient();

        $url = "Shared Documents/$dwsFolderName";
        $paramArray = array('url' => $url);

        # $paramArray = array('name' => '', 'users' => '', 'title' => $name, 'documents' => '');


        $methodName = 'CreateFolder';

        $result = $this->dwsObj->callWsMethod($methodName, $paramArray);
        var_dump($result);
        return $result;
    }

    public function deleteFolderDWS($dwsname, $folderName)
    {
        //print "<br>- Method createDWS";
        $this->dwsObj->setwsdlurl($this->server . "/" . $dwsname . "/_vti_bin/Dws.asmx?WSDL");

        $this->dwsObj->loadSOAPClient();

        $url = "Shared Documents/$folderName";
        $paramArray = array('url' => $url);

        # $paramArray = array('name' => '', 'users' => '', 'title' => $name, 'documents' => '');


        $methodName = 'DeleteFolder';

        $result = $this->dwsObj->callWsMethod($methodName, $paramArray);
        var_dump($result);
        return $result;
    }

    public function findDWSdoc($dwsname, $guid)
    {
        //print "<br>- Method createDWS";
        $this->dwsObj->setwsdlurl($this->server . $dwsName . "/_vti_bin/Dws.asmx?WSDL");

        $this->dwsObj->loadSOAPClient();

        $paramArray = array('id' => '$guid');

        $methodName = 'FindDwsDoc';

        $result = $this->dwsObj->callWsMethod($methodName, $paramArray);
        var_dump($result);
    }

    public function getDWSData($newFileName, $dwsname, $lastUpdate)
    {
        //print "<br>- Method getDWSData<br />";
        $url = $this->server . "/" . $dwsname . "/_vti_bin/Dws.asmx?WSDL";
        $this->dwsObj->setwsdlurl($url);
        if ($this->dwsObj->loadSOAPClient()) {
            $doc = "Shared Documents";
            $paramArray = array('document' => '', 'lastUpdate' => '');
            $methodName = 'GetDwsData';
            $result = $this->dwsObj->callWsMethod($methodName, $paramArray);
            var_dump($result);
            $sResult = $result->GetDwsDataResult;
            /*            $xmlNew = simplexml_load_string($sResult);// used to parse string to xml
              $xmlArray = @G::json_decode(@G::json_encode($xmlNew),1);// used to convert Objects to array */
            $serializeResult = serialize($sResult); // serializing the Array for Returning.
            var_dump($serializeResult);
            return $serializeResult;
        } else {
            return "The enter the Correct Dws Name";
        }
    }

    public function uploadDocumentDWS($dwsname, $folderName, $sourceUrl, $filename)
    {
        //print "<br>- Method createDWS";
        $url = $this->server . "/" . $dwsname . "/_vti_bin/Copy.asmx?WSDL";
        $this->dwsObj->setwsdlurl($url);
        $this->dwsObj->loadSOAPClient();

        $destUrlObj = new DestinationUrlCollection();
        if ($folderName != '') {
            $destUrl = $this->server . "/$dwsname/Shared%20Documents/$folderName/$filename";
        } else {
            $destUrl = $this->server . "/$dwsname/Shared%20Documents/$filename";
        }
        $destUrlObj->string = $destUrl;

        $fieldInfoObj = new FieldInformation();

        $fieldInfoCollObj = new FieldInformationCollection();
        $fieldInfoCollObj->FieldInformation = $fieldInfoObj;

        $imgfile = $sourceUrl . "/" . $filename;
        $filep = fopen($imgfile, "r");
        $fileLength = filesize($imgfile);
        $content = fread($filep, $fileLength);
        //$content = base64_encode($content);


        $paramArray = array('SourceUrl' => $imgfile, 'DestinationUrls' => $destUrlObj, 'Fields' => $fieldInfoCollObj, 'Stream' => $content
        );
        $methodName = 'CopyIntoItems';
        $result = $this->dwsObj->callWsMethod($methodName, $paramArray);
        var_dump($result);
        $newResult = $result->Results->CopyResult->ErrorCode;
        if ($newResult == 'Success') {
            return "The document has been uploaded Successfully";
        } else {
            return "Could not Upload the Document due to some Error";
        }
    }

    public function getDWSMetaData($newFileName, $dwsname, $id)
    {
        //print "<br>- Method createDWS";
        $url = $this->server . "/" . $dwsname . "/_vti_bin/Dws.asmx?WSDL";
        $this->dwsObj->setwsdlurl($url);

        $this->dwsObj->loadSOAPClient();

        $doc = "Shared Documents/$newFileName";
        $paramArray = array('document' => $doc, 'id' => '', 'minimal' => false);

        $methodName = 'GetDwsMetaData';

        $result = $this->dwsObj->callWsMethod($methodName, $paramArray);
        $sResult = $result->GetDwsMetaDataResult;
        $errorReturn = strpos($sResult, "Error");
        if (isset($sResult) && !$errorReturn) {
            $serializeResult = serialize($sResult); // serializing the Array for Returning.
            var_dump($serializeResult);
            return $serializeResult;
        } else {
            return $sResult;
        }
    }

    public function getDWSDocumentVersions($newFileName, $dwsname)
    {
        //print "<br>- Method createDWS";
        $this->dwsObj->setwsdlurl($this->server . "/" . $dwsname . "/_vti_bin/Versions.asmx?WSDL");

        $this->dwsObj->loadSOAPClient();

        $doc = "Shared Documents/$newFileName";
        $paramArray = array('fileName' => $doc);

        $methodName = 'GetVersions';

        $result = $this->dwsObj->callWsMethod($methodName, $paramArray);
        var_dump($result);
        return $result;
    }

    public function deleteDWSDocVersion($newFileName, $dwsname, $versionNum)
    {
        //print "<br>- Method createDWS";
        $this->dwsObj->setwsdlurl($this->server . "/" . $dwsname . "/_vti_bin/Versions.asmx?WSDL");

        $this->dwsObj->loadSOAPClient();

        $doc = "Shared Documents/$newFileName";
        $paramArray = array('fileName' => $doc, 'fileVersion' => $versionNum);

        $methodName = 'DeleteVersion';

        $result = $this->dwsObj->callWsMethod($methodName, $paramArray);
        if ($result) {
            $sResult = $result->DeleteVersionResult->any;
            $xmlNew = simplexml_load_string($sResult); // used to parse string to xml
            $xmlArray = @G::json_decode(@G::json_encode($xmlNew), 1); // used to convert Objects to array
            $versionCount = count($xmlArray['result']);

            if ($versionCount > 1) {
                for ($i = 0; $i < $versionCount; $i++) {
                    $version[] = $xmlArray['result'][$i]['@attributes']['version'];
                }
            } else {
                $version[] = $xmlArray['result']['@attributes']['version'];
            }

            $serializeResult = serialize($version); // serializing the Array for Returning.
            var_dump($serializeResult);
            return $serializeResult;
        } else {
            return "The given Version could not be deleted.";
        }
    }

    public function deleteAllDWSDocVersion($newFileName, $dwsname)
    {
        //print "<br>- Method createDWS";
        $this->dwsObj->setwsdlurl($this->server . "/" . $dwsname . "/_vti_bin/Versions.asmx?WSDL");

        $this->dwsObj->loadSOAPClient();

        $doc = "Shared Documents/$newFileName";
        $paramArray = array('fileName' => $doc);

        $methodName = 'DeleteAllVersions';

        $result = $this->dwsObj->callWsMethod($methodName, $paramArray);
        if ($result) {
            $xml = $result->DeleteAllVersionsResult->any; // in Result we get string in Xml format
            $xmlNew = simplexml_load_string($xml); // used to parse string to xml
            $xmlArray = @G::json_decode(@G::json_encode($xmlNew), 1); // used to convert Objects to array
            $latestVersion = $xmlArray['result']['@attributes']['version'];
            return "All Versions are Deleted, except the latest i.e $latestVersion";
        } else {
            return "The Version/ File name/ Dws Name  is incorrect";
        }
    }

    public function getDWSFolderItems($dwsname, $strFolderUrl)
    {
        $pmTrSharepointClassObj = new pmTrSharepointClass();
        //print "<br>- Method getDWSFolderItems";
        $url = $this->server . "/" . $dwsname . "/_vti_bin/SiteData.asmx?WSDL";
        $this->dwsObj->setwsdlurl($this->server . "/" . $dwsname . "/_vti_bin/SiteData.asmx?WSDL");

        $this->dwsObj->loadSOAPClient();

        #$doc = "Shared Documents/$newFileName";
        $paramArray = array('strFolderUrl' => $strFolderUrl);

        $methodName = 'EnumerateFolder';

        $result = $this->dwsObj->callWsMethod($methodName, $paramArray);
        //$newResult = $result->vUrls->_sFPUrl->Url;
        if (isset($result->vUrls->_sFPUrl->Url)) {
            $returnContent = $pmTrSharepointClassObj->getFolderUrlContent($result->vUrls->_sFPUrl->Url);
            $serializeResult = serialize($returnContent);
            return $serializeResult;
        } elseif (isset($result->vUrls->_sFPUrl)) {
            $itemCount = count($result->vUrls->_sFPUrl);
            for ($i = 0; $i < $itemCount; $i++) {
                $aObjects = $result->vUrls->_sFPUrl[$i]->IsFolder;
                //$booleanStatus = $aObjects[$i]->IsFolder;
                if ($aObjects) {
                    $listArr = $result->vUrls->_sFPUrl[$i]->Url;
                    $returnContent[] = $pmTrSharepointClassObj->getFolderUrlContent($listArr) . "(Is a Folder)";
                } else {
                    $listArr = $result->vUrls->_sFPUrl[$i]->Url;
                    $returnContent[] = $pmTrSharepointClassObj->getFolderUrlContent($listArr) . "(Is a File)";
                }
            }
            $serializeResult = serialize($returnContent);
            return $serializeResult;
        }
        return "There is some error";
    }

    public function downloadDocumentDWS($dwsname, $fileName, $fileLocation)
    {
        //print "<br>- Method createDWS";
        $url = $this->server . "/" . $dwsname . "/_vti_bin/Copy.asmx?WSDL";
        $this->dwsObj->setwsdlurl($url);

        $this->dwsObj->loadSOAPClient();

        $CompleteUrl = $this->server . "/" . $dwsname . "/Shared Documents/" . $fileName;
        $paramArray = array('Url' => $CompleteUrl);

        $methodName = 'GetItem';

        $result = $this->dwsObj->callWsMethod($methodName, $paramArray);
        $newResult = $result->Stream;

        //$latestResult = base64_decode($newResult);

        /**
         * In the Below line of code, we are coping the files at our local Directory using the php file methods.
         */
        $imgfile = $fileLocation . "/" . $fileName;
        $filep = fopen($imgfile, 'w');
        //$content = fwrite($filep, $latestResult);
        $content = fwrite($filep, $newResult);
        return $content;
    }

    public function getFolderUrlContent($newResult)
    {
        $needleStart = '/';
        $needleCount = substr_count($newResult, $needleStart);

        $urlStartPos = strpos($newResult, $needleStart);
        $urlStartPos++;

        if ($needleCount == '2') {
            $newResultPos = strpos($newResult, $needleStart, $urlStartPos);
            $newResultPos++;
            $actualResult = substr($newResult, $newResultPos);
            return $actualResult;
        } else {
            $actualResult = substr($newResult, $urlStartPos);
            return $actualResult;
        }
    }
}