Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
cmis_common.inc
1 <?php
17 require_once 'Mail/mimeDecode.php';
18 
31 {
32 
38  var $security_header = NULL;
39 
45  var $ptr = NULL;
46 
47 
52  function CMIS_Common()
53  {
54 
55  }//end constructor
56 
57 
76  function connect($wsdl, $conn_info)
77  {
78  if (empty($wsdl) || empty($conn_info['username']) || empty($conn_info['password'])) return FALSE;
79 
80  if ($conn_info['ws_security']) {
81  $created = date('Y-m-d\TH:i:s\Z', time());
82  $soap_header_data =
83  '<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" SOAP-ENV:mustUnderstand="1">
84  <wsu:Timestamp xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
85  <wsu:Created>'.$created.'</wsu:Created>
86  </wsu:Timestamp>
87  <wsse:UsernameToken>
88  <wsse:Username>'.$conn_info['username'].'</wsse:Username>
89  <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">'.$conn_info['password'].'</wsse:Password>
90  </wsse:UsernameToken>
91  </wsse:Security>';
92 
93  $soap_header_var = @new SoapVar($soap_header_data, XSD_ANYXML);
94  $namespace = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd';
95  $this->security_header = @new SoapHeader($namespace, "wsse:Security", $soap_header_var, TRUE);
96  }
97 
98  // Prep the params to be passed to the SOAP client
99  $params = Array();
100  $params['exceptions'] = TRUE;
101  $params['trace'] = TRUE;
102  $params['soap_version'] = SOAP_1_1;
103  $params['login'] = $conn_info['username'];
104  $params['password'] = $conn_info['password'];
105  //This is Sharepoint 2010 specific, using basic authentication.
106  if (!$conn_info['ws_security']) $params['location'] = substr($wsdl, 0, strpos($wsdl, '?')).'/basic';
107 
108  // Make sure the pointer is open
109  if ($this->ptr) $this->ptr=NULL;
110 
111  if ($conn_info['verify']){
112  $options = array(
113  'FOLLOWLOCATION' => 1,
114  'RETURNTRANSFER' => 1,
115  'HTTPAUTH' => CURLAUTH_BASIC,
116  'TIMEOUT' => 3,
117  'USERPWD' => $conn_info['username'].":".$conn_info['password'],
118  );
119  $details = fetch_url($wsdl, $options, array(), FALSE);
120  $contents = $details['response'];
121 
122  if (strpos($contents, '?xml') === FALSE) return FALSE;
123 
124  try {
125  $this->ptr = new SoapClient($wsdl, $params);
126  $fns = $this->ptr->__getFunctions();
127  if (empty($fns)) return FALSE;
128 
129  if ($conn_info['service_name'] == 'RepositoryService') {
130  $response = $this->ptr->__soapCall('getRepositories', Array(), Array('uri'=>''), $this->security_header);
131  } else if ($conn_info['service_name'] == 'NavigationService'){
132  $response = $this->ptr->__soapCall('getChildren', Array('paramaters' => Array ('repositoryId' => '', 'folderId' => '')), Array('uri'=>''), $this->security_header);
133  } else if ($conn_info['service_name'] == 'ObjectService'){
134  $response = $this->ptr->__soapCall('getContentStream', Array('paramaters' => Array ('repositoryId' => '', 'objectId' => '')), Array('uri'=>''), $this->security_header);
135  }
136  } catch (SoapFault $e) {
137  if (strpos($e->getMessage(), 'looks like we got no XML document') === FALSE) {
138  trigger_error("Failed to connect to CMIS ".$conn_info['service_name'].": ".$e->getMessage()."\n", E_USER_WARNING);
139  return FALSE;
140  }
141  }//end if
142 
143  } else {
144  $this->ptr = new SoapClient($wsdl, $params);
145  }
146 
147  return TRUE;
148 
149  }//end connect()
150 
151 
158  public function getRepositories()
159  {
160  $repositories = NULL;
161  if (!$this->ptr) return $repositories;
162 
163  try {
164  $response = $this->ptr->__soapCall('getRepositories', Array(), Array('uri'=>''), $this->security_header);
165  } catch (SoapFault $e) {
166  if (strpos($e->getMessage(), 'looks like we got no XML document') === FALSE) {
167  trigger_error("Failed to getRepositories: ".$e->getMessage()."\n", E_USER_WARNING);
168  return $repositories;
169  }
170  }
171 
172  $data = $this->processSoapRequest();
173 
174  if (isset($data['xml']->getRepositoriesResponse)){
175  $repositories = $data['xml']->getRepositoriesResponse;
176  }
177 
178  return $repositories;
179  }
180 
181 
190  public function getRepositoryInfo($repositoryId)
191  {
192  $repositoryInfo = NULL;
193  if (!$this->ptr) return $repositoryInfo;
194 
195  try {
196  $response = $this->ptr->__soapCall('getRepositoryInfo', Array('paramaters' => Array ('repositoryId' => $repositoryId)), Array('uri'=>''), $this->security_header);
197  } catch (SoapFault $e) {
198  if (strpos($e->getMessage(), 'looks like we got no XML document') === FALSE) {
199  trigger_error("Failed to getRepositoryInfo: ".$e->getMessage()."\n", E_USER_WARNING);
200  return $repositoryInfo;
201  }
202  }
203 
204  $data = $this->processSoapRequest();
205  if (isset($data['xml']->getRepositoryInfoResponse)) {
206  $repositoryInfo = $data['xml']->getRepositoryInfoResponse;
207  }
208 
209  return $repositoryInfo;
210  }
211 
212 
222  public function getTypeDefinition($repositoryId, $typeId)
223  {
224  $type_def = NULL;
225  if (!$this->ptr) return $type_def;
226 
227  try {
228  $response = $this->ptr->__soapCall('getTypeDefinition', Array('paramaters' => Array ('repositoryId' => $repositoryId, 'typeId' => $typeId)), Array('uri'=>''), $this->security_header);
229  } catch (SoapFault $e) {
230  if (strpos($e->getMessage(), 'looks like we got no XML document') === FALSE) {
231  trigger_error("Failed to getTypeDefinition: ".$e->getMessage()."\n", E_USER_WARNING);
232  return $type_def;
233  }
234  }
235 
236  $data = $this->processSoapRequest();
237  if (isset($data['xml']->getTypeDefinitionResponse)){
238  $type_def = $data['xml']->getTypeDefinitionResponse;
239  }
240 
241  return $type_def;
242  }
243 
244 
254  public function getChildren($repositoryId, $folderId)
255  {
256  $children = NULL;
257  if (!$this->ptr) return $children;
258 
259  try {
260  $response = $this->ptr->__soapCall('getChildren', Array('paramaters' => Array ('repositoryId' => $repositoryId, 'folderId' => $folderId, 'includePathSegment' => TRUE)), Array('uri'=>''), $this->security_header);
261  } catch (SoapFault $e) {
262  if (strpos($e->getMessage(), 'looks like we got no XML document') === FALSE) {
263  trigger_error("Failed to getChildren: ".$e->getMessage()."\n", E_USER_WARNING);
264  return $children;
265  }
266  }
267 
268  $data = $this->processSoapRequest();
269  if (isset($data['xml']->getChildrenResponse)){
270  $children = $data['xml']->getChildrenResponse;
271  }
272 
273  return $children;
274  }
275 
276 
286  public function getContentStream($repositoryId, $objectId)
287  {
288  $contentStream = '';
289  if (!$this->ptr) return $contentStream;
290 
291  try {
292  $response = $this->ptr->__soapCall('getContentStream', Array('paramaters' => Array ('repositoryId' => $repositoryId, 'objectId' => $objectId)), Array('uri'=>''), $this->security_header);
293  } catch (SoapFault $e) {
294  if (strpos($e->getMessage(), 'looks like we got no XML document') === FALSE) {
295  trigger_error("Failed to getContentStream: ".$e->getMessage()."\n", E_USER_WARNING);
296  return $contentStream;
297  }
298  }
299 
300  $data = $this->processSoapRequest();
301  if (isset($data['binary'])) {
302  $contentStream = $data['binary'];
303  } else if (isset($data['xml']->getContentStreamResponse->contentStream->stream)){
304  $contentStream = (string)$data['xml']->getContentStreamResponse->contentStream->stream;
305  $contentStream = base64_decode($contentStream);
306  }
307 
308  return $contentStream;
309  }
310 
311 
320  public function processSoapRequest()
321  {
322  $data = Array();
323  $decode_params['include_bodies'] = TRUE;
324  $decode_params['decode_bodies'] = TRUE;
325  $decode_params['decode_headers'] = TRUE;
326 
327  $soap_header = $this->ptr->__getLastResponseHeaders();
328  $soap_response = $this->ptr->__getLastResponse();
329 
330  $decoder = new Mail_mimeDecode($soap_header."\r\n".$soap_response);
331  $structure = $decoder->decode($decode_params);
332 
333  if (isset($structure->parts[1]->body)) {
334  $data['binary'] = $structure->parts[1]->body;
335  $data['binary'] = preg_replace('|\r\n$|', '', $data['binary']); //decoding seems to add extra line to end
336  }
337 
338  $soap_env = (isset($structure->parts[0]->body)) ? $structure->parts[0]->body : '';
339 
340  if (preg_match('|<([^:]+:)?Body[^>]*>(.*)</([^:]+:)?Body>|msi', $soap_env, $match)) {//extract the soap body
341  //take out the colons in the tag names which SimpleXMLElement can't process
342  $match[0] = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $match[0]);
343  try {
344  $xml = new SimpleXMLElement($match[0]);
345  $data['xml'] = $xml;
346  }catch(Exception $e){
347  trigger_error("Failed to parse XML".$e->getMessage()."\n", E_USER_WARNING);
348  }
349  }
350 
351  return $data;
352  }
353 
354 }//end class
355 
356 ?>