Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
squiz_suite_system_suite.inc
1 <?php
18 require_once SQ_PACKAGES_PATH.'/squiz_suite/systems/squiz_suite_system/squiz_suite_system.inc';
19 
32 {
33 
34 
41  function __construct($assetid=0)
42  {
43  $this->_ser_attrs = TRUE;
44  parent::__construct($assetid);
45 
46  }//end constructor
47 
48 
55  public function getProductSummary()
56  {
57  $version = '';
58  $numberOfAssets = 0;
59  $numberOfRootUrls = 0;
60 
61  // Version
62  preg_match('/v(([0-9].?)\.([0-9].?)\.[0-9])/', SQ_SYSTEM_LONG_NAME, $matches);
63  if (isset($matches[1])) {
64  $version = $matches[1];
65  }
66 
67  // Number of Assets
68  $sql = 'SELECT COUNT(*) FROM sq_ast';
69  $result = MatrixDAL::executeSqlOne($sql);
70  if (!empty($result)) {
71  $numberOfAssets = $result;
72  }//end if
73 
74  // Number of Root URLs
75  $sql = 'SELECT COUNT(*) FROM sq_ast_url';
76  $result = MatrixDAL::executeSqlOne($sql);
77  if (!empty($result)) {
78  $numberOfRootUrls = $result;
79  }//end if
80 
81  $summary = Array(
82  Array('label' => 'Version', 'value' => $version),
83  Array('label' => 'Assets', 'value' => $numberOfAssets),
84  Array('label' => 'Root URLs', 'value' => $numberOfRootUrls),
85  );
86 
87  return $summary;
88 
89  }//end getProductSummary()
90 
91 
97  public function suitePing()
98  {
99  $suite_manager = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('suite_manager');
100  try {
101  // Parse the posted XML file. Exception will be thrown in case error occurs.
102  $required = array('system_id', 'system_url', 'system_name');
103  $xml = array_get_index($_POST, 'xml', NULL);
104  $xml = html_entity_decode($xml);
105  $message = $suite_manager->parseSuiteXML($xml, $required);
106 
107  $systemid = $message['system_id'];
108  $systemUrl = $message['system_url'];
109  $systemName = $message['system_name'];
110 
111  // All the required fields exist?
112  foreach ($required as $field) {
113  if ($message[$field] === NULL) {
114  $errMsg = $field.' has not been specified.';
115  throw new Exception($errMsg);
116  }
117  }
118 
119  // If the sender is a sync pending product, make it live.
120  $product = $suite_manager->getProductBySystemid($systemid);
121  if (empty($product) === FALSE && $product['status'] === 'sync pending') {
122  $suite_manager->updateProduct($product['suiteid'], 'status', 'live');
123  }
124 
125  $suite_manager->updateProduct($product['suiteid'], 'last_contacted', date('c'));
126 
127  $response = 'ok';
128  return $response;
129  } catch (Exception $e) {
130  $errMsg = "suitePing failed\n";
131  $errMsg .= $e->getMessage();
132  throw new Exception($errMsg);
133  }//end try
134 
135  }//end suitePing()
136 
137 
149  public function suiteConnect()
150  {
151  $suite_manager = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('suite_manager');
152  try {
153  $current_product = $suite_manager->getCurrentProduct();
154  if (empty($current_product) || !isset($current_product['url'])) {
155  $errMsg = 'The system is not ready to accept any connection request.';
156  throw new Exception($errMsg);
157  }
158 
159  // Parse the posted XML file. Exception will be thrown in case error occurs.
160  $required = array('system_id', 'system_url', 'system_name', 'system_type', 'cert');
161  $xml = array_get_index($_POST, 'xml', NULL);
162  $xml = html_entity_decode($xml);
163  $message = $suite_manager->parseSuiteXML($xml, $required);
164 
165  // All the required fields exist?
166  foreach ($required as $field) {
167  if ($message[$field] === NULL) {
168  $errMsg = $field.' has not been specified.';
169  throw new Exception($errMsg);
170  }
171  }
172 
173  // TODO: Maximum daily request exceeded?
174  $systemid = $message['system_id'];
175  $systemUrl = $message['system_url'];
176  $systemName = $message['system_name'];
177  $systemType = $message['system_type'];
178  $certificate = $message['cert'];
179 
180  $suite_manager->logReceivedMessage($systemid, 'suiteConnect');
181 
182  // The systemid already exists?
183  $product = $suite_manager->getProductBySystemidURL($systemid, $systemUrl);
184  if (empty($product) === FALSE) {
185  $suiteid = $product['suiteid'];
186 
187  // Is the status Live already?
188  if ($product['status'] === 'live' && $product['url'] === $systemUrl && $product['connection']['cert'] === $certificate && $product['connection']['name'] === $systemName) {
189  // Okay, the system exists, and its live.
190  // Why does the SuiteConnect come in again?
191  // Ping the sender.
192  $pingRes = $suite_maanger->sendMessage($suiteid, 'suitePing');
193  if (strpos($pingRes['result'], '<?xml') !== 0) {
194  // No sender, something wrong.
195  $errMsg = 'The requested system does not exist.';
196  throw new Exception($errMsg);
197  } else {
198  if ($pingRes['curlInfo']['http_code'] === 200) {
199  if ($product['sync_status'] === 'D') {
200  //The product has been removed but has not reached all servers yet.
201  $errMsg = 'The requested system has been removed.';
202  throw new Exception($errMsg);
203  } else {
204  // Ping worked but something wrong with server, do not accept this message.
205  $errMsg = 'The requested system is already registered.';
206  throw new Exception($errMsg);
207  }//end if
208  }//end if
209  }//end if
210  } else {
211  // The systemid exists and it's not live yet. But make sure
212  // the URL and certificate do not exist first.
213  if (array_key_exists('url', $product) === FALSE && array_key_exists('cert', $product['connection']) === FALSE) {
214  $product['connection']['cert'] = $certificate;
215  $suite_manager->updateProduct($suiteid, 'url', $systemUrl);
216  $suite_manager->updateProduct($suiteid, 'connection', $product['connection']);
217  } else {
218  $errMsg = 'The requested system can not be accepted';
219  throw new Exception($errMsg);
220  }
221  }
222  } else {
223  $connection = array();
224  $connection['cert'] = $certificate;
225  $connection['name'] = $systemName;
226 
227  $suiteid = $suite_manager->registerProduct($systemid, $systemType, $systemUrl, $connection);
228 
229  $token = $suite_manager->createConnectionToken();
230  $suite_manager->updateProduct($suiteid, 'token', $token);
231  }//end if
232 
233  // Now we are all good. Register the system and set the status
234  // as pending. Then send acknowlegement message back.
235  $suite_manager->updateProduct($suiteid, 'status', 'pending approval');
236  $suite_manager->updateProduct($suiteid, 'last_contacted', date('c'));
237 
238  $response = 'ok';
239  return $response;
240  } catch (Exception $e) {
241  $errMsg = 'suiteConnect failed'."\n";
242  $errMsg .= $e->getMessage()."\n";
243  $suite_manager->logErrorMessage($errMsg);
244 
245  // Re-throw exception here so that Suite Manager can send
246  // the proper error HTTP header.
247  throw new Exception($errMsg);
248  }//end try
249 
250  }//end suiteConnect()
251 
252 
264  public function suiteConnectAck()
265  {
266  $suite_manager = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('suite_manager');
267  try {
268  // Parse the posted XML file. Exception will be thrown in case error occurs.
269  $required = Array(
270  'system_id',
271  'system_url',
272  'system_name',
273  'system_type',
274  'conn_token',
275  'cert',
276  );
277  $enc = array_get_index($_POST, '_enc', Array());
278  $xml = array_get_index($enc, 'xml', NULL);
279  $xml = html_entity_decode($xml);
280  $message = $suite_manager->parseSuiteXML($xml, $required);
281 
282  // All the required fields exist?
283  foreach ($required as $field) {
284  if ($field !== 'conn_token' && $message[$field] === NULL) {
285  $errMsg = $field.' has not been specified.';
286  throw new Exception($errMsg);
287  }
288  }
289 
290  $systemid = $message['system_id'];
291  $systemUrl = rtrim($message['system_url'], '/');
292  $systemName = $message['system_name'];
293  $systemType = $message['system_type'];
294  $certificate = $message['cert'];
295  $token = $message['conn_token'];
296 
297  $duplicated = $suite_manager->getProductBySystemidURL($systemid, $systemUrl);
298  if (empty($duplicated) === FALSE) {
299  $errMsg = 'The requested system already exists.';
300  throw new Exception($errMsg);
301  }
302 
303  $allProducts = $suite_manager->getProducts();
304  $productInfo = array();
305  foreach ($allProducts as $product) {
306  if ($product['status'] === 'pending' && rtrim($product['url'], '/') === $systemUrl) {
307  $productInfo = $product;
308  break;
309  }
310  }
311 
312  if (empty($productInfo) === TRUE) {
313  $errMsg = 'Unknown system has sent suiteConnectAck message.';
314  throw new Exception($errMsg);
315  }
316 
317  $suite_manager->logReceivedMessage($systemid, 'suiteConnectAck');
318 
319  $productInfo['connection']['cert'] = $certificate;
320  $productInfo['connection']['name'] = $systemName;
321 
322  $suite_manager->updateProduct($productInfo['suiteid'], 'systemid', $systemid);
323  $suite_manager->updateProduct($productInfo['suiteid'], 'connection', $productInfo['connection']);
324  $suite_manager->updateProduct($productInfo['suiteid'], 'type', $systemType);
325  $suite_manager->updateProduct($productInfo['suiteid'], 'status', 'live');
326  $suite_manager->updateProduct($productInfo['suiteid'], 'sync_status', 'A');
327  $suite_manager->updateProduct($productInfo['suiteid'], 'last_contacted', date('c'));
328  $suite_manager->updateProduct($productInfo['suiteid'], 'token', $token);
329  $response = $suite_manager->sendMessage($productInfo['suiteid'], 'suiteConnectAckAck');
330  $response = $response['result'];
331  if (strpos($response, '<?xml') !== 0) {
332  $errMsg = 'suiteConnectAckAck message did not reach to '.$systemUrl;
333  throw new Exception($errMsg);
334  } else {
335  $dom = new DomDocument();
336  $dom->loadXML($response);
337 
338  $responseDom = $dom->getElementsByTagName('result')->item(0);
339  $response = Suite_Manager::getResponseFromXMLDom($responseDom);
340  if ($response !== 'ok') {
341  $errMsg = 'suiteConnectAckAck message did not reach to '.$systemUrl;
342  throw new Exception($errMsg);
343  }
344  }
345  } catch (Exception $e) {
346  $errMsg = 'suiteConnectAck failed.'."\n";
347  $errMsg .= $e->getMessage()."\n";
348  $suite_manager->logErrorMessage($errMsg);
349 
350  // Re-throw exception here so that API system can send
351  // the proper error HTTP header.
352  throw new Exception($errMsg);
353  }//end try
354 
355  }//end suiteConnectAck()
356 
357 
366  public function suiteConnectAckAck()
367  {
368  $suite_manager = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('suite_manager');
369  try {
370  // Parse the posted XML file. Exception will be thrown in case error occurs.
371  $required = Array(
372  'system_id',
373  'system_url',
374  'conn_token',
375  );
376  $enc = array_get_index($_POST, '_enc', Array());
377  $xml = array_get_index($enc, 'xml', NULL);
378  $xml = html_entity_decode($xml);
379  $message = $suite_manager->parseSuiteXML($xml, $required);
380 
381  // All the required fields exist?
382  foreach ($required as $field) {
383  if ($field !== 'conn_token' && $message[$field] === NULL) {
384  $errMsg = $field.' has not been specified.';
385  throw new Exception($errMsg);
386  }
387  }
388 
389  $systemid = $message['system_id'];
390  $systemUrl = $message['system_url'];
391  $token = $message['conn_token'];
392 
393  // The final acknowledgment has been arrived.
394  // Now two systems are connected.
395  $productInfo = array();
396  if ($token !== NULL) {
397  $productInfo = $suite_manager->getProductByToken($token);
398  } else {
399  $productInfo = $suite_manager->getProductBySystemid($systemid);
400  if (isset($productInfo[0]) === TRUE) {
401  $productInfo = $productInfo[0];
402  }
403  }
404 
405  if (empty($productInfo) === TRUE) {
406  $errMsg = 'Unknown system has been requested.';
407  throw new Exception($errMsg);
408  }
409 
410  $suite_manager->logReceivedMessage($systemid, 'suiteConnectAckAck');
411 
412  $suiteid = $productInfo['suiteid'];
413  if ($productInfo['status'] === 'pending approval') {
414  $suite_manager->updateProduct($suiteid, 'status', 'live');
415  $suite_manager->updateProduct($suiteid, 'sync_status', 'A');
416  }
417 
418  $suite_manager->updateProduct($suiteid, 'last_contacted', date('c'));
419 
420  $response = 'ok';
421  return $response;
422  } catch (Exception $e) {
423  $errMsg = 'suiteConnectAckAck failed.'."\n";
424  $errMsg .= $e->getMessage()."\n";
425  $suite_manager->logErrorMessage($errMsg);
426  }//end try
427 
428  }//end suiteConnectAckAck()
429 
430 
437  public function suiteSyncProduct()
438  {
439  $suite_manager = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('suite_manager');
440  try {
441  // Parse the posted XML file. Exception will be thrown in case error occurs.
442  $required = array(
443  'system_id',
444  'system_url',
445  'system_name',
446  'system_type',
447  'options',
448  );
449  $enc = array_get_index($_POST, '_enc', Array());
450  $xml = array_get_index($enc, 'xml', NULL);
451  $xml = html_entity_decode($xml);
452  $message = $suite_manager->parseSuiteXML($xml, $required);
453 
454  // All the required fields exist?
455  foreach ($required as $field) {
456  if (isset($message[$field]) === FALSE || $message[$field] === NULL) {
457  $errMsg = $field.' has not been specified.';
458  throw new Exception($errMsg);
459  }
460  }
461 
462  $current_product = $suite_manager->getCurrentProduct();
463  $new_request_info = $message['options'];
464  $product_info = $suite_manager->getProductByToken($new_request_info['token']);
465  if (empty($product_info) === TRUE) {
466  throw new Exception('suiteSyncProduct is not allowed');
467  }
468 
469  // System ID has been updated.
470  if ($current_product['type'] !== 'Squiz Update' && $product_info['systemid'] !== $message['system_id']) {
471  $suite_manager->updateProduct($product_info['suiteid'], 'systemid', $message['system_id']);
472  $product_info['systemid'] = $message['system_id'];
473  }
474 
475  $markedDeleted = array();
476  $markedAdded = array();
477  $suite_manager->syncProductDetails($product_info, $new_request_info);
478 
479  $result = array(
480  'systemid' => $current_product['systemid'],
481  'type' => $current_product['type'],
482  'apiurl' => $current_product['url'],
483  'name' => $current_product['connection']['name'],
484  'cert' => $current_product['connection']['cert'],
485  'added' => array(),
486  'deleted' => array(),
487  'connected' => array(),
488  );
489 
490  if ($current_product['type'] !== 'Squiz Update' && $product_info['type'] !== 'Squiz Update') {
491  $suite_manager->syncDeletedProducts($new_request_info['deleted'], $markedDeleted, $markedAdded);
492  $result = $suite_manager->getSuiteProductInfo($current_product);
493  $suite_manager->syncConnectedProducts($product_info['suiteid'], $new_request_info['connected'], $markedDeleted, $markedAdded);
494  }
495 
496  $suite_manager->updateProduct($product_info['suiteid'], 'last_contacted', date('c'));
497  return $result;
498 
499  } catch (Exception $e) {
500  $errMsg = 'suiteSyncProduct failed.'."\n";
501  $errMsg .= $e->getMessage()."\n";
502  $suite_manager->logErrorMessage($errMsg);
503  }//end try
504 
505  }//end suiteSyncProduct()
506 
507 
513  public function suiteSetToken()
514  {
515  $suite_manager = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('suite_manager');
516  try {
517  // Parse the posted XML file. Exception will be thrown in case error occurs.
518  $required = array(
519  'system_id',
520  'system_url',
521  'system_name',
522  'system_type',
523  );
524  $enc = array_get_index($_POST, '_enc', Array());
525  $xml = array_get_index($enc, 'xml', NULL);
526  $xml = html_entity_decode($xml);
527  $message = $suite_manager->parseSuiteXML($xml, $required);
528 
529  // All the required fields exist?
530  foreach ($required as $field) {
531  if (isset($message[$field]) === FALSE || $message[$field] === NULL) {
532  $errMsg = $field.' has not been specified.';
533  throw new Exception($errMsg);
534  }
535  }
536 
537  $product = $suite_manager->getProductBySystemidURL($message['system_id'], $message['system_url']);
538  if (empty($product) === TRUE) {
539  throw new Exception('Operation prohibited');
540  }
541 
542  if (empty($product['token']) === FALSE) {
543  throw new Exception('Operation prohibited');
544  }
545 
546  if ($product['status'] === 'live' && $product['sync_status'] !== 'D') {
547  $newToken = $suite_manager->createConnectionToken();
548  $result = array('token' => $newToken);
549  $suite_manager->updateProduct($product['suiteid'], 'token', $newToken);
550 
551  return $result;
552  } else {
553  throw new Exception('Operation prohibited');
554  }
555  } catch (Exception $e) {
556  $errMsg = 'suiteSetToken failed.'."\n";
557  $errMsg .= $e->getMessage()."\n";
558  $suite_manager->logErrorMessage($errMsg);
559  }//end try
560 
561  }//end suiteSetToken()
562 
563 
564 }//end class
565 
566 ?>