Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
trigger_action_call_rest_resource.inc
1 <?php
18 require_once SQ_CORE_PACKAGE_PATH.'/system/triggers/trigger_action/trigger_action.inc';
19 require_once SQ_ATTRIBUTES_PATH.'/http_request/http_request.inc';
20 
33 {
43  public static function execute($settings, &$state)
44  {
45  if (empty($state['asset'])) {
46  // grab the asset if assetid is given, but not the asset.
47  if (empty($state['assetid'])) {
48  return FALSE;
49  } else {
50  $state['asset'] = $GLOBALS['SQ_SYSTEM']->am->getAsset($state['assetid']);
51  }
52  }
53 
54  if (is_null($state['asset'])) return FALSE;
55 
56  $assetid = $state['asset']->id;
57 
58  $http_request_value = $settings['http_request'];
59 
60  // create a new http request object
61  $http_request_attr = new Asset_Attribute_HTTP_Request(0, $http_request_value);
62  if (is_null($http_request_attr)) return FALSE;
63 
64  $current_value = unserialize($http_request_attr->value);
65 
66  // return false if there is no method or url specified
67  if (empty($current_value['method']) && empty($current_value['urls'])) {
68  return FALSE;
69  }
70 
71  // Get list of replacements
72  $keywords_list = Array();
73  if (isset($current_value['urls'])) {
74  $keywords_list = array_merge(
75  $keywords_list,
76  retrieve_keywords_replacements(implode(', ', $current_value['urls']))
77  );
78  }
79 
80  if (isset($current_value['request_headers'])) {
81  $keywords_list = array_merge(
82  $keywords_list,
83  retrieve_keywords_replacements(implode(', ', $current_value['request_headers']))
84  );
85  }
86 
87  if (isset($current_value['request_body'])) {
88  $keywords_list = array_merge(
89  $keywords_list,
90  retrieve_keywords_replacements($current_value['request_body'])
91  );
92  }
93 
94  if (isset($current_value['http_auth_options'])) {
95  $keywords_list = array_merge(
96  $keywords_list,
97  retrieve_keywords_replacements(implode(', ', $current_value['http_auth_options']))
98  );
99  }
100 
101  $keywords_list = array_unique($keywords_list);
102 
103  // ...and find their replacements and set them to the http request object
104  $replacements = Array();
105  foreach ($keywords_list as $keyword) {
106  $replacements[$keyword] = $state['asset']->getKeywordReplacement($keyword);
107  }
108 
109  $http_request_attr->setKeywordReplacements($replacements);
110 
111  // send the request
112  $res =& $http_request_attr->run();
113 
114  // pass the triggering asset and REST responses received
115  $GLOBALS['SQ_SYSTEM']->broadcastTriggerEvent('trigger_event_rest_response_received', $state['asset'], $res);
116  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($state['asset']);
117 
118  return Array(
119  'assetid' => $assetid,
120  );
121 
122  }//end execute()
123 
124 
135  public static function getInterface($settings, $prefix, $write_access=FALSE)
136  {
137  ob_start();
138 
139  $new_prefix = str_replace(array('[',']'), '_', $prefix);
140 
141  $http_request_value = array_get_index($settings, 'http_request', Array());
142  $http_request_attr = new Asset_Attribute_HTTP_Request(0, $http_request_value);
143 
144  $http_request_attr->paint($new_prefix.'_http_request', !$write_access);
145 
146  hidden_field($prefix.'[new_prefix]', $new_prefix);
147 
148  return ob_get_clean();
149 
150  }//end getInterface()
151 
152 
164  public static function processInterface(&$settings, $request_data)
165  {
166  if (!isset($settings['http_request'])) {
167  $http_request_value = serialize(Array());
168  } else {
169  $http_request_value = $settings['http_request'];
170  }
171 
172  $http_request_attr = new Asset_Attribute_HTTP_Request(0, $http_request_value);
173  $new_prefix = str_replace(array('[',']'), '_', array_get_index($request_data, 'new_prefix', ''));
174 
175  $http_request_attr->process($new_prefix.'_http_request');
176  $settings['http_request'] = $http_request_attr->value;
177 
178  return FALSE;
179 
180  }//end processInterface()
181 
182 
192  public static function getLocks($settings, &$state)
193  {
194  return Array($state['assetid'] => Array('lookups'));
195 
196  }//end getLocks()
197 
198 
199 }//end class
200 
201 ?>