Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
page_rest_resource.inc
1 <?php
18 require_once SQ_CORE_PACKAGE_PATH.'/page/page.inc';
19 
20 
31 class Page_REST_Resource extends Page
32 {
33  protected $_res = Array();
34 
35  // These are the headers to be sent if send_headers is true.
36  // Only those with values found in the first response will be forwarded.
37  private $_response_headers = Array(
38  'Content-Type',
39  'Content-Length',
40  'Last-Modified',
41  'Cache-Control',
42  'Expires',
43  );
44 
45  // For storing extra param replacements for use in the HTTP request attribute.
46  protected $_extra_replacements = Array();
47 
48 
55  function __construct($assetid=0)
56  {
57  $this->_ser_attrs = TRUE;
58  parent::__construct($assetid);
59 
60  }//end constructor
61 
62 
69  public function printFrontend()
70  {
71  $this->_process();
72  return parent::printFrontend();
73 
74  }//end printFrontend()
75 
76 
87  public function printBody()
88  {
89  // Run the request(s).
90  $this->_process();
91 
92  // let the user decide if they want Matrix to do keyword replacements on the response. See bug
93  // #5701 Minor Enhancement : Allow REST resources assets to stop replacing keywords in response
94  $GLOBALS['SQ_SYSTEM']->setGlobalDefine('SQ_REPLACE_MYSOURCE_LEVEL_KEYWORDS', $this->attr('allow_global_replace'));
95 
96  if (!empty($this->_res['responses'])) {
97  if ($this->attr('send_headers')) {
98  foreach ($this->_response_headers as $h) {
99  if (isset($this->_res['response']['headers'][$h])) {
100  header("$h: " . $this->_res['response']['headers'][$h]);
101  }
102  }
103  }
104 
105  // Send the response. If there are multiple responses, send them all. This might
106  // seem odd, but occasionally you might be building a document from multiple fragments.
107  foreach ($this->_res['responses'] as &$resp) {
108  if ($resp['info']['http_code'] >= 400) {
109  echo $this->attr('error_response');
110  } else {
111  echo $resp['body'];
112  }
113  }
114  }
115 
116  }//end printBody()
117 
118 
125  protected function _process()
126  {
127  // Only run the requests if we are being forced to, or if they have not yet been run.
128  if (($this->attr('allow_multiple_runs')) || (empty($this->_res['responses']))) {
129  // Run the request(s).
130  $http_attr = $this->getAttribute('http_request');
131  $http_attr->setKeywordReplacements($this->_extra_replacements);
132 
133  // all the available keywords need to be passed to the attribute to have keyword set in the attribute to be replaced
134  $available_keywords = parent::getAvailableKeywords();
135  foreach ($available_keywords as $keyword => $val) {
136  $available_keywords[$keyword] = $this->getKeywordReplacement($keyword);
137  }
138 
139  $mm = $GLOBALS['SQ_SYSTEM']->getMetadataManager();
140  $metadata_fields = $mm->getMetadata($this->id);
141  foreach($metadata_fields as $schema_fieldid => $field_data) {
142  foreach ($field_data as $item) {
143  $available_keywords['asset_metadata_'.$item['name']] = $item['value'];
144  }
145  }
146 
147  $http_attr->setKeywordReplacements($available_keywords);
148 
149  $this->_res =& $http_attr->run();
150 
151  // trigger the event with this REST asset as a triggering asset and a response received
152  $GLOBALS['SQ_SYSTEM']->broadcastTriggerEvent('trigger_event_rest_response_received', $this, $this->_res);
153  }
154 
155  }//end _process()
156 
157 
167  {
168  ob_start();
169  $this->printBody();
170 
171  return ob_get_clean();
172 
173  }//end getAssetContentsKeywordReplacement()
174 
175 
184  public function getKeywordReplacement($keyword)
185  {
186  if (substr($keyword, 0, strlen('asset_rest_contents')) == 'asset_rest_contents') {
187 
188  $params = substr($keyword, strlen('asset_rest_contents') + 1);
189  $params = explode('_', $params);
190 
191  $i = 1;
192  foreach ($params as $p) {
193  $this->_extra_replacements["rest_param_$i"] = $p;
194  }
195 
197 
198  } else if (substr($keyword, 0, strlen('rest')) == 'rest') {
199  // If the keyword starts with REST, dig in the data for a replacement.
200  return $this->getKeywordReplacementFromArray($this->_res, substr($keyword, strlen('rest_')));
201 
202  }
203 
204  return parent::getKeywordReplacement($keyword);
205 
206  }//end getKewordReplacement()
207 
208 
223  public function getKeywordReplacementFromArray($array, $keyword)
224  {
225  // If $keyword is a 1st level index of this array, then great..
226  if (isset($array[$keyword])) return $array[$keyword];
227 
228  // Otherwise, split the keyword into parts, and try the first part, recursing if there's a match.
229  $parts = explode('_', $keyword);
230  $part = array_shift($parts);
231 
232  if (isset($array[$part])) {
233  return $this->getKeywordReplacementFromArray($array[$part], join($parts, '_'));
234  }
235 
236  }//end getKeywordReplacementFromArray()
237 
238 }
239 ?>