File size: 14,486 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
363
364
365
366
367
<?php

/**
 * class.pmTrAlfresco.pmFunctions.php
 *
 * ProcessMaker Open Source Edition
 * Copyright (C) 2004 - 2008 Colosa Inc.
 * *
 */
////////////////////////////////////////////////////
// pmTrAlfresco PM Functions
//
// Copyright (C) 2007 COLOSA
//
// License: LGPL, see LICENSE
////////////////////////////////////////////////////


/**
 * Alfresco Triggers that allow ProcessMaker to perform common DM actions
 * @class pmTrAlfresco
 * @name Alfresco DM Triggers v. 0.1
 * @icon /images/triggers/alfrescoIcon.png
 * @className class.pmTrAlfresco.pmFunctions.php
 */
/**
 * @method
 *
 * Cancel Checkedout document/file
 *
 * @name cancelCheckout
 * @label Cancel Checkedout document/file
 *
 * @param string | $alfrescoServerUrl | Server name and port where Alfresco exists | http://localhost:8080/alfresco
 * @param string | $docUid | Document Uid
 * @param string | $user | Valid Admin username to connect to Alfresco server
 * @param string | $pwd | Valid Admin password to connect to Alfresco server
 *
 * @return object | $result | Response |
 *
 */


// Validation left
function cancelCheckout($alfrescoServerUrl, $docUid, $user = "", $pwd = "")
{
    //require_once(PATH_CORE. 'classes' . PATH_SEP.'triggers' . PATH_SEP . 'class.pmTrAlfresco.php');
    $alfresco_url = "$alfrescoServerUrl/s/cmis/pwc/s/workspace:SpacesStore/i/$docUid";
    $domapi_exec = RestClient::delete($alfresco_url, $user, $pwd, "application/atom+xml;type=entry");
    //$alfrescoMessage = $domapi_exec['header'];
    $domapi_res = G::json_decode($domapi_exec->getResponse());
    return $domapi_res;
}

/**
 * @method
 *
 * Checkin document/file
 *
 * @name Checkin
 * @label Checkin document/file
 *
 * @param string | $alfrescoServerUrl | Server name and port where Alfresco exists | http://localhost:8080/alfresco
 * @param string | $docUid | Document Uid
 * @param string | $comments | Comments
 * @param string | $user | Valid Admin username to connect to Alfresco server
 * @param string | $pwd | Valid Admin password to connect to Alfresco server
 *
 * @return string | $result | Response |
 *
 */

function checkIn($alfrescoServerUrl, $docUid, $comments, $user = "", $pwd = "")
{
    $alfresco_url = "$alfrescoServerUrl/s/cmis/pwc/i/$docUid?checkin=true&checkinComment=$comments";
    $xmlData = array();
    $xmlData = '<?xml version="1.0" encoding="utf-8"?><entry xmlns="http://www.w3.org/2005/Atom"/>';

    $alfresco_exec = RestClient::put($alfresco_url, $xmlData, $user, $pwd, "application/atom+xml");
    $alfrescoMessage = $alfresco_exec->getResponseMessage();
    if ($alfrescoMessage === 'OK') {
        return "The Document has been Checkedin";
    } elseif ($alfrescoMessage === 'Internal Server Error') {
        return "Please enter a Valid Document Id";
    } else {
        return $alfrescoMessage;
    }
}

/**
 * @method
 *
 * Checkout document/file
 *
 * @name Checkout
 * @label Checkout document/file
 *
 * @param string | $alfrescoServerUrl | Server name and port where Alfresco exists | http://localhost:8080/alfresco
 * @param string | $docUid | Document Uid
 * @param string | $user | Valid Admin username to connect to Alfresco server
 * @param string | $pwd | Valid Admin password to connect to Alfresco server
 *
 * @return string | $result | Response |
 *
 */
// Validation done
function checkOut($alfrescoServerUrl, $docUid, $user = "", $pwd = "")
{
    $alfresco_url = "$alfrescoServerUrl/s/cmis/checkedout";
    $xmlData = array();
    $xmlData = '<?xml version="1.0" encoding="utf-8"?>' . '<entry xmlns="http://www.w3.org/2005/Atom" xmlns:cmisra="http://docs.oasis-open.org/ns/cmis/restatom/200908/" xmlns:cmis="http://docs.oasis-open.org/ns/cmis/core/200908/">' . '<cmisra:object>' . '<cmis:properties>' . '<cmis:propertyId propertyDefinitionId="cmis:objectId">' . '<cmis:value>workspace://SpacesStore/' . $docUid . '</cmis:value>' . '</cmis:propertyId>' . '</cmis:properties>' . '</cmisra:object>' . '</entry>';

    $alfresco_exec = RestClient::post($alfresco_url, $xmlData, $user, $pwd, "application/atom+xml;type=entry");
    $alfrescoMessage = $alfresco_exec->getResponseMessage();
    if ($alfrescoMessage === 'Created') {
        return "The Document has been Checkedout";
    } elseif ($alfrescoMessage === 'Conflict') {
        return "The Document you are trying to checkout has already been Checkedout";
    } else {
        return $alfrescoMessage;
    }
}

/**
 * @method
 *
 * Create a folder in Alfresco Repository
 *
 * @name createFolder
 * @label Create a folder in Alfresco Repository
 *
 * @param string | $alfrescoServerUrl | Server name and port where Alfresco exists | http://localhost:8080/alfresco
 * @param string | $parentFolder | Parent Folder Name
 * @param string | $folderName | Name of the Folder to be created
 * @param string | $user | Valid Admin username to connect to Alfresco server
 * @param string | $pwd | Valid Admin password to connect to Alfresco server
 *
 * @return string | $result | Response |
 *
 */
function createFolder($alfrescoServerUrl, $parentFolder, $folderName, $user, $pwd)
{
    $name = explode("/", $folderName);
    $init = substr($parentFolder, 0, 1);
    $parentFolder = ($init == "/") ? substr($parentFolder, 1) . "/" : $parentFolder . "/";
    $alfresco_url = "$alfrescoServerUrl/s/cmis/p/" . $parentFolder . "children";
    $xmlData = array();
    $xmlData = '<?xml version="1.0" encoding="utf-8"?>' . '<entry xmlns="http://www.w3.org/2005/Atom" xmlns:cmisra="http://docs.oasis-open.org/ns/cmis/restatom/200908/" xmlns:cmis="http://docs.oasis-open.org/ns/cmis/core/200908/">' . '<title>' . $name[0] . '</title>' . '<cmisra:object>' . '<cmis:properties>' . '<cmis:propertyId propertyDefinitionId="cmis:objectTypeId"><cmis:value>cmis:folder</cmis:value></cmis:propertyId>' . '</cmis:properties>' . '</cmisra:object>' . '</entry>';
    $alfresco_exec = RestClient::post($alfresco_url, $xmlData, $user, $pwd, "application/atom+xml");
    $alfrescoMessage = $alfresco_exec->getResponseMessage();
    $folderName = substr(strstr($folderName, "/"), 1);
    $parentFolder = $parentFolder . "" . $name[0];

    if ($folderName != null) {
        createFolder($alfrescoServerUrl, $parentFolder, $folderName, $user, $pwd);
    }
    if ($alfrescoMessage === 'Created') {
        return "The Folder has been Created";
    } else {
        return $alfrescoMessage;
    }
}

/**
 * @method
 *
 * Delete an object(Folder/File) from Alfresco Repository
 *
 * @name deleteObject
 * @label Delete an object(Folder/File) from Alfresco Repository
 *
 * @param string | $alfrescoServerUrl | Server name and port where Alfresco exists | http://localhost:8080/alfresco
 * @param string | $objetcId | Id of the Object(Folder/File) to be deleted
 * @param string | $user | Valid Admin username to connect to Alfresco server
 * @param string | $pwd | Valid Admin password to connect to Alfresco server
 *
 * @return object | $result | Response |
 *
 */
function deleteObject($alfrescoServerUrl, $objetcId, $user, $pwd)
{
    $getResponse = true;
    $alfresco_url  = "$alfrescoServerUrl/s/cmis/s/workspace:SpacesStore/i/$objetcId";
    $alfresco_exec = RestClient::delete($alfresco_url, $user, $pwd, "application/atom+xml", $getResponse);
    if($alfresco_exec->getResponseCode() === 204 && trim($alfresco_exec->getResponse()) === '') {
        $alfresco_res = true;
    } else {
        $alfresco_res = false;
    }
    return $getResponse ? $alfresco_res : '';
}

/**
 * @method
 *
 * Download Document/File from Alfresco Repository
 *
 * @name downloadDoc
 * @label Download Document/File from Alfresco Repository
 *
 * @param string | $alfrescoServerUrl | Server name and port where Alfresco exists | http://localhost:8080/alfresco
 * @param string | $pathFile | File Source
 * @param string | $pathFolder | Folder Name
 * @param string | $user | Valid Admin username to connect to Alfresco server
 * @param string | $pwd | Valid Admin password to connect to Alfresco server
 * @param string | $mainFolder | The main folder in alfreco to save the files
 *
 * @return string | $result | Response |
 *
 */
function downloadDoc($alfrescoServerUrl, $pathFile, $pathFolder, $user, $pwd, $mainFolder = 'Sites')
{
    if (!(G::verifyPath($pathFolder))) {
        $result = new stdclass();
        $result->error = G::Loadtranslation('ID_FILE_PLUGIN_NOT_EXISTS', SYS_LANG, array('pluginFile' => $pathFolder));
        return $result;
    }

    $dataPathFile = pathinfo($pathFile);
    $nameFile = $dataPathFile['basename'];

    $alfresco_url = "$alfrescoServerUrl" . PATH_SEP . "s" . PATH_SEP . "cmis" . PATH_SEP . "p" . PATH_SEP . $mainFolder . PATH_SEP . "$pathFile";
    $alfresco_exec = RestClient::get($alfresco_url, $user, $pwd, 'application/atom+xml');
    $sXmlArray = $alfresco_exec->getResponse(); 
    $sXmlArray = preg_replace("[\n|\r|\n\r]", '', $sXmlArray);
    $xmlObject = simplexml_load_string((string) $sXmlArray);

    if (!isset($xmlObject->content)) {
        $result = new stdclass();
        $result->error = G::Loadtranslation('ID_FILE_PLUGIN_NOT_EXISTS', SYS_LANG, array('pluginFile' => $nameFile));
        return $result;
    }

    $linkContent = (string) $xmlObject->content->attributes()->src;
    $alfresco_exec = RestClient::get($linkContent, $user, $pwd, 'application/atom+xml');
    $sXmlArray = $alfresco_exec->getResponse();
    $content = preg_replace("[^\x0A|^\x0D|\x0Ax0D|\x0Dx0A]", '', $sXmlArray);

    if ('/' != substr($pathFolder, -1)) {
        $pathFolder .= '/';
    }

    $fp = fopen($pathFolder . $nameFile, "w+");
    fwrite($fp, $content);
    fclose($fp);
    return true;
}

/**
 * @method
 *
 * Get a list of Checkedout Document/File from Alfresco Repository
 *
 * @name getCheckedoutFiles
 * @label Get a list of Checkedout Document/File from Alfresco Repository
 *
 * @param string | $alfrescoServerUrl | Server name and port where Alfresco exists | http://localhost:8080/alfresco
 * @param string | $user | Valid Admin username to connect to Alfresco server
 * @param string | $pwd | Valid Admin password to connect to Alfresco server
 *
 * @return object | $result | Response |
 *
 */
function getCheckedoutFiles($alfrescoServerUrl, $user, $pwd)
{
    $getChildrenUrl = "$alfrescoServerUrl/s/cmis/checkedout";

    $domapi_exec = RestClient::get($getChildrenUrl, $user, $pwd, 'application/atom+xml');
    $sXmlArray = G::json_decode($domapi_exec->getResponse());
    $sXmlArray = trim($sXmlArray);
    $xXmlArray = simplexml_load_string($sXmlArray);
    $aXmlArray = @G::json_decode(@G::json_encode($xXmlArray), 1);

    return $alfresco_res;
}

/**
 * @method
 *
 * Get Children of the given folder
 *
 * @name getFolderChildren
 * @label Get Children of the given folder
 *
 * @param string | $alfrescoServerUrl | Server name and port where Alfresco exists | http://localhost:8080/alfresco
 * @param string | $folderId | FolderId of the Folder whose children is to be listed
 * @param string | $user | Valid Admin username to connect to Alfresco server
 * @param string | $pwd | Valid Admin password to connect to Alfresco server
 *
 * @return object | $result | Response |
 *
 */
function getFolderChildren($alfrescoServerUrl, $folderId, $user, $pwd)
{
    $getChildrenUrl = "$alfrescoServerUrl/service/api/node/workspace/SpacesStore/$folderId/children";
    $alfresco_exec = RestClient::get($getChildrenUrl, $user, $pwd);
    $sXmlArray = $alfresco_exec->getResponse();
    $sXmlArray = trim($sXmlArray);
    $xXmlArray = simplexml_load_string($sXmlArray);
    $aXmlArray = @G::json_decode(@G::json_encode($xXmlArray), 1);

    return $aXmlArray;
}

/**
 * @method
 *
 * Upload file/document in Alfresco Repository
 *
 * @name uploadDoc
 * @label Upload file/document in Alfresco Repository
 *
 * @param string | $alfrescoServerUrl | Server name and port where Alfresco exists | http://localhost:8080/alfresco
 * @param string | $fileSource | File Source
 * @param string | $title | File Title
 * @param string | $description | Description about File
 * @param string | $docType | Type of document to be Uploaded
 * @param string | $user | Valid Admin username to connect to Alfresco server
 * @param string | $pwd | Valid Admin password to connect to Alfresco server
 * @param string | $path | Path of document to be Uploaded
 * @param string | $mainFolder | The main folder in alfreco to save the files
 *
 * @return object | $result | Response |
 *
 */
function uploadDoc($alfrescoServerUrl, $fileSource, $title, $description, $docType, $user, $pwd, $path = '', $mainFolder= 'Sites')
{
    if (!(file_exists($fileSource))) {
        $result = new stdclass();
        $result->error = G::Loadtranslation('ID_FILE_PLUGIN_NOT_EXISTS', SYS_LANG, array('pluginFile' => $fileSource));
        return $result;
    }
    $filep       = fopen($fileSource, "r");
    $fileLength  = filesize($fileSource);
    $fileContent = fread($filep, $fileLength);
    $fileContent = base64_encode($fileContent);

    if ($path != '') {
        createFolder($alfrescoServerUrl, $mainFolder, $path, $user, $pwd);
        $path = $path . PATH_SEP;
    }

    $alfresco_url = "$alfrescoServerUrl/s/cmis/p/$mainFolder/" . $path . "children";
    $xmlData = array();
    $xmlData = '<?xml version="1.0" encoding="utf-8"?><entry xmlns="http://www.w3.org/2005/Atom" xmlns:cmisra="http://docs.oasis-open.org/ns/cmis/restatom/200908/" xmlns:cmis="http://docs.oasis-open.org/ns/cmis/core/200908/"><title>' . $title . '</title><summary>' . $description . '</summary><content type="application/' . $docType . '">' . $fileContent . '</content><cmisra:object><cmis:properties><cmis:propertyId propertyDefinitionId="cmis:objectTypeId"><cmis:value>cmis:document</cmis:value></cmis:propertyId></cmis:properties></cmisra:object></entry>';

    $alfresco_exec = RestClient::post($alfresco_url, $xmlData, $user, $pwd, "application/atom+xml");
    $response = $alfresco_exec->getHeaders();
    switch ($response['code']) {
        case '201':
            //Created
            $sXmlArray     = $alfresco_exec->getResponse();
            $sXmlArray     = trim($sXmlArray);
            $xXmlArray     = simplexml_load_string($sXmlArray);
            $aXmlArray     = @G::json_decode(@G::json_encode($xXmlArray), 1);
            break;
        case '409':
            //file exists
            $aXmlArray = 'There is already a file with the same name:   ' . $title;
            break;
        default:
            $aXmlArray = $response['message'];
            break;
    }
    return $aXmlArray;
}