Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
soap_api_file_retrieval_service.inc
1 <?php
18 require_once SQ_PACKAGES_PATH.'/web_services/api/soap_api/soap_api.inc';
19 
31 class Soap_Api_File_Retrieval_Service extends Soap_Api
32 {
33 
34 
41  function __construct($assetid=0)
42  {
43  parent::__construct($assetid);
44 
45  }//end constructor
46 
47 
55  public function getFunctionList()
56  {
57  return Array(
58  'Download' => '1',
59  'Upload' => '1',
60  'GetFileInformation' => '1',
61  'GetImageInformation' => '1',
62  );
63 
64  }//end getFunctionList()
65 
66 
80  function Download($request)
81  {
82  $request_info = (Array) $request;
83  $file_asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($request_info['AssetID']);
84  $type_ancestors = $file_asset->getTypeAncestors(FALSE);
85  if (in_array('file', $type_ancestors) || $file_asset->type() == 'file') {
86  // Now we do the stuff
87  $file_info = $file_asset->getExistingFile();
88  $file_path = $file_info['path'];
89  $file_content = file_get_contents($file_path);
90  $file_content_base64 = base64_encode($file_content);
91 
92  $file_name = $file_info['filename'];
93  $file_type = get_file_type($file_name);
94  $file_size = $file_info['size'];
95  $file_modified = $file_info['modified'];
96 
97  return Array (
98  'FileName' => $file_name,
99  'FileType' => $file_type,
100  'FileSize' => $file_size,
101  'LastModified' => date('d-m-Y H:i:s', $file_modified),
102  'FileContentBase64' => $file_content_base64,
103  );
104  } else {
105  throw new SoapFault('Server', 'The Asset ID Provided Is Not Of A File Asset');
106  }//end else
107 
108  $GLOBALS['SQ_SYSTEM']->am->includeAsset($link_info['type_code']);
109  $object_name = str_replace(' ', '_', ucwords(str_replace('_', ' ', $link_info['type_code'])));
110  $asset = new $object_name();
111  $asset->setAttrValue('name', $link_info['name']);
112 
113  $link_info['asset'] = $parent_asset;
114  $linkid = $asset->create($link_info);
115  if ($linkid) {
116  return Array (
117  'CreateMessage' => Array (
118  'message' => 'Asset was created successfully. Link id #'.$linkid,
119  ),
120  'AssetObject' => Array (
121  'object_xml' => 'walao xml',
122  ),
123  );
124  }//end if
125 
126  }//end Download()
127 
128 
142  function GetFileInformation($request)
143  {
144  $request_info = (Array) $request;
145  $file_asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($request_info['AssetID']);
146  $type_ancestors = $file_asset->getTypeAncestors(FALSE);
147  if (in_array('file', $type_ancestors) || $file_asset->type() == 'file') {
148  $file_info = $file_asset->getExistingFile();
149 
150  require_once SQ_FUDGE_PATH.'/general/file_system.inc';
151  $file_name = $file_info['filename'];
152  $file_type = get_file_type($file_name);
153  $file_size = $file_info['size'];
154  $file_modified = $file_info['modified'];
155 
156  return Array (
157  'FileName' => $file_name,
158  'FileType' => $file_type,
159  'FileSize' => $file_size,
160  'LastModified' => date('d-m-Y H:i:s', $file_modified),
161  );
162  } else {
163  throw new SoapFault('Server', 'The Asset ID Provided Is Not Of A File Asset');
164  }//end else
165 
166  }//end GetFileInformation()
167 
168 
182  function GetImageInformation($request)
183  {
184  $request_info = (Array) $request;
185 
186  $file_asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($request_info['AssetID']);
187  if ($file_asset->type() === 'image') {
188  $fileInfo = $file_asset->getExistingFile();
189  $image_info = getimagesize($fileInfo['path']);
190  $mime_info = $image_info['mime'];
191 
192  return Array (
193  'FileName' => $file_asset->vars['name']['value'],
194  'FileTitle' => $file_asset->vars['title']['value'],
195  'FileType' => $file_asset->type(),
196  'FileSize' => $file_asset->vars['size']['value'],
197  'MimeType' => $mime_info,
198  'ImageWidth' => $file_asset->vars['width']['value'],
199  'ImageHeight' => $file_asset->vars['height']['value'],
200  'LastModified' => date('d-m-Y H:i:s', $file_modified),
201  );
202  } else {
203  throw new SoapFault('Server', 'The Asset ID provided is not of a file asset');
204  }//end else
205 
206  }//end GetImageInformation()
207 
223  function Upload($request)
224  {
225  $request_info = (Array) $request;
226  $file_asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($request_info['AssetID']);
227  $file_content = array_get_index($request_info, 'FileContentBase64', '');
228  $file_name = array_get_index($request_info, 'FileName', '');
229  $type_ancestors = $file_asset->getTypeAncestors(FALSE);
230  if (in_array('file', $type_ancestors) || $file_asset->type() == 'file') {
231  // Now we do the stuff
232  $edit_fns = $file_asset->getEditFns();
233  if (!empty($file_content)) {
234  //We are going to write to the Matrix Data tmp dir
235  $tmp_file_name = self::getRandomFilename($file_name);
236 
237  $destination_file = SQ_DATA_PATH.'/temp/'.$tmp_file_name;
238  while (file_exists($destination_file)) {
239  sleep(1);
240  $tmp_file_name = self::getRandomFilename($file_name);
241  }//end if
242  file_put_contents($destination_file, base64_decode($file_content));
243  }//end if
244 
245  // Construct the info array to pass to edit fns
246  require_once SQ_FUDGE_PATH.'/general/file_system.inc';
247  $file_type = get_file_type($file_name);
248  $info = Array (
249  'name' => $file_name,
250  'type' => $file_type,
251  'tmp_name' => $destination_file,
252  'non_uploaded_file' => TRUE,
253  'error' => 0,
254  'size' => filesize($destination_file),
255  );
256 
257  //processFileUpload
258  $prefix = $file_asset->type().'_'.$file_asset->id;
259  require_once SQ_INCLUDE_PATH.'/backend_outputter.inc';
260  $o = new Backend_Outputter();
261  $GLOBALS['SQ_SYSTEM']->am->acquireLock($file_asset->id, 'attributes');
262 
263  $result = $edit_fns->processFileUpload($file_asset, $o, $prefix, $info);
264  $GLOBALS['SQ_SYSTEM']->am->releaseLock($file_asset->id, 'attributes');
265 
266  // Remove the tmp file before we return
267  if (file_exists($destination_file)) {
268  unlink($destination_file);
269  }//end if
270  return Array (
271  'UploadResult' => (string) $result,
272  );
273  } else {
274  throw new SoapFault('Server', 'The Asset ID Provided Is Not Of A File Asset');
275  }//end else
276 
277  }//end Upload()
278 
279 
288  public static function getRandomFilename($file_name)
289  {
290  $hash_file_name = hash('md5', $file_name.time());
291  $rand_position = rand(0, strlen($hash_file_name)-6);
292  $tmp_file_name = substr($hash_file_name, $rand_position, 6);
293 
294  return $tmp_file_name;
295 
296  }//end getRandomFileName()
297 
298 
305  public static function getComplexElements($func_list=Array())
306  {
307  $obj_optional = self::getArgType('AssetObject', 0, 1);
308 
309  $complex_types = Array (
310  'Download' => Array (
311  'Download' => Array (
312  'AssetID' => self::$string_non_optional,
313  ),
314  'DownloadResponse' => Array (
315  'FileName' => self::$string_optional,
316  'FileType' => self::$string_optional,
317  'FileSize' => self::$string_optional,
318  'LastModified' => self::$string_optional,
319  'FileContentBase64' => self::$string_optional,
320  ),
321  ),
322  'GetFileInformation' => Array (
323  'GetFileInformation' => Array (
324  'AssetID' => self::$string_non_optional,
325  ),
326  'GetFileInformationResponse' => Array (
327  'FileName' => self::$string_optional,
328  'FileType' => self::$string_optional,
329  'FileSize' => self::$string_optional,
330  'LastModified' => self::$string_optional,
331  ),
332  ),
333  'GetImageInformation' => Array (
334  'GetImageInformation' => Array (
335  'AssetID' => self::$string_non_optional,
336  ),
337  'GetImageInformationResponse' => Array (
338  'FileName' => self::$string_optional,
339  'FileTitle' => self::$string_optional,
340  'FileType' => self::$string_optional,
341  'FileSize' => self::$string_optional,
342  'MimeType' => self::$string_optional,
343  'ImageWidth' => self::$string_optional,
344  'ImageHeight' => self::$string_optional,
345  'LastModified' => self::$string_optional,
346  ),
347  ),
348  'Upload' => Array (
349  'Upload' => Array (
350  'AssetID' => self::$string_non_optional,
351  'FileName' => self::$string_non_optional,
352  'FileContentBase64' => self::$string_non_optional,
353  ),
354  'UploadResponse' => Array (
355  'UploadResult' => self::$string_optional,
356  ),
357 
358 
359  ),
360  );
361 
362  $complex_types_available = parent::getComplexElements($complex_types);
363 
364  return $complex_types_available;
365 
366  }//end getComplexElements()
367 
368 
375  public static function getSimpleRestrictedTypes($func_list=Array())
376  {
377  return Array();
378 
379  }//end getSimpleRestrictedTypes()
380 
381 
382 }//end class
383 ?>