Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
trigger_action_set_cookie.inc
1 <?php
18 require_once SQ_SYSTEM_ROOT.'/core/attributes/parameter_map/parameter_map.inc';
19 
42 {
43 
44 
69  public static function execute($settings, &$state)
70  {
71  // check required settings
72  $name = array_get_index($settings, 'name', '');
73  if ($name == '') return FALSE;
74 
75  //replace keywords for $value
76  if (empty($state['asset'])) {
77  // grab the asset if assetid is given, but not the asset.
78  if (empty($state['assetid'])) {
79  return FALSE;
80  } else {
81  $state['asset'] = $GLOBALS['SQ_SYSTEM']->am->getAsset($state['assetid']);
82  }
83  }
84 
85  if (is_null($state['asset'])) return FALSE;
86 
87  $old_current_asset = $GLOBALS['SQ_SYSTEM']->getGlobalDefine('CURRENT_ASSET', NULL);
88  $GLOBALS['SQ_SYSTEM']->setGlobalDefine('CURRENT_ASSET', $state['asset']);
89 
90  $parameter_map_value = array_get_index($settings, 'value_map', serialize(Array()));
91  $atr_parameter_map = new Asset_Attribute_Parameter_Map(0, $parameter_map_value);
92  $param_map_value = $atr_parameter_map->getParameterValue('value');
93 
94  // value in parameter map has higher priority than the value in the input textbox
95  if (!empty($param_map_value)) {
96  $value = $param_map_value;
97  } else {
98  $value = $settings['value'];
99  }
100 
101  if (is_null($old_current_asset)) {
102  $GLOBALS['SQ_SYSTEM']->unsetGlobalDefine('CURRENT_ASSET');
103  } else {
104  $GLOBALS['SQ_SYSTEM']->setGlobalDefine('CURRENT_ASSET', $old_current_asset);
105  }
106 
107  //replace keywords for $value
108  if (empty($state['asset'])) {
109  // grab the asset if assetid is given, but not the asset.
110  if (empty($state['assetid'])) {
111  return FALSE;
112  } else {
113  $state['asset'] = $GLOBALS['SQ_SYSTEM']->am->getAsset($state['assetid']);
114  }
115  }
116 
117  if (is_null($state['asset'])) return FALSE;
118 
119  $value = self::replaceKeywords($value, $state['asset']);
120 
121  $expire_time = array_get_index($settings, 'expire_time', 0);
122  $path = array_get_index($settings, 'path', '');
123  $domain = array_get_index($settings, 'domain', '');
124  $secure = array_get_index($settings, 'secure', FALSE);
125  $http_only = array_get_index($settings, 'http_only', FALSE);
126 
127  if ($expire_time != 0) {
128  $expire_time = time() + $expire_time;
129  }
130 
131  if (empty($path) || empty($domain)) {
132  // Calculate the domain and path like the session cookie (look at session_handler_default.inc)
133  $temp_domain = strip_url(sq_web_path('root_url'), TRUE);
134  $pos = strpos($temp_domain, '/');
135  $temp_path = '/';
136  if ($pos !== FALSE) {
137  $temp_path = substr($temp_domain, $pos);
138  $temp_domain = substr($temp_domain, 0, $pos);
139  }
140 
141  // Remove the port number from the host part, if any.
142  // Session cookies cannot have a port in the domain.
143  $temp_domain = preg_replace('|:\d+$|', '', $temp_domain);
144 
145  if (empty($path)) {
146  $path = $temp_path;
147  }
148 
149  if (empty($domain)) {
150  $domain = $temp_domain;
151  }
152  }
153 
154  if(headers_sent()) return FALSE;
155 
156  header('Set-Cookie: ' . rawurlencode($name) . '=' . rawurlencode($value)
157  . (empty($expire_time) ? '' : '; expires=' . gmdate('D, d-M-Y H:i:s', $expire_time) . ' GMT')
158  . (empty($path) ? '' : '; path=' . $path)
159  . (empty($domain) ? '' : '; domain=' . $domain)
160  . (!$secure ? '' : '; secure')
161  . (!$http_only ? '' : '; HttpOnly'), false);
162 
163  return Array(
164  'name' => $name,
165  'value' => $value,
166  'expire_time' => $expire_time,
167  'path' => $path,
168  'domain' => $domain,
169  'secure' => $secure,
170  'http_only' => $http_only
171  );
172 
173  }//end execute()
174 
175 
186  public static function getInterface($settings, $prefix, $write_access=FALSE)
187  {
188  //name textbox
189  ob_start();
190  $name = array_get_index($settings, 'name');
191  if ($write_access) {
192  text_box($prefix.'[name]', $name);
193  } else {
194  echo '<b>'.$name.'</b>';
195  }
196  $name_input = ob_get_clean();
197 
198  //value textbox
199  ob_start();
200  $value = array_get_index($settings, 'value');
201  if ($write_access) {
202  text_box($prefix.'[value]', $value);
203  } else {
204  echo '<b>'.$value.'</b>';
205  }
206  $value_input = ob_get_clean();
207 
208  //expire time textbox
209  ob_start();
210  $expire_time = array_get_index($settings, 'expire_time', 0);
211  if ($write_access) {
212  text_box($prefix.'[expire_time]', $expire_time, 10);
213  } else {
214  echo '<b>'.$expire_time.'</b>';
215  }
216  $expire_time_input = ob_get_clean();
217 
218  //path textbox
219  ob_start();
220  $path = array_get_index($settings, 'path');
221  if ($write_access) {
222  text_box($prefix.'[path]', $path, 40);
223  } else {
224  echo '<b>'.$path.'</b>';
225  }
226  $path_input = ob_get_clean();
227 
228  //domain textbox
229  ob_start();
230  $domain = array_get_index($settings, 'domain');
231  if ($write_access) {
232  text_box($prefix.'[domain]', $domain, 40);
233  } else {
234  echo '<b>'.$domain.'</b>';
235  }
236  $domain_input = ob_get_clean();
237 
238  //secure combobox
239  ob_start();
240  $secure = array_get_index($settings, 'secure', FALSE);
241  if ($write_access) {
242  combo_box($prefix.'[secure]', Array('0' => translate('no'), '1' => translate('yes')), FALSE , ($secure ? '1' : '0'));
243  } else {
244  echo '<b>'.($secure ? translate('yes') : translate('no')).'</b>';
245  }
246  $secure_input = ob_get_clean();
247 
248  //http only combobox
249  ob_start();
250  $http_only = array_get_index($settings, 'http_only', FALSE);
251  if ($write_access) {
252  combo_box($prefix.'[http_only]', Array('0' => translate('no'), '1' => translate('yes')), FALSE , ($http_only ? '1' : '0'));
253  } else {
254  echo '<b>'.($http_only ? translate('yes') : translate('no')).'</b>';
255  }
256  $http_only_input = ob_get_clean();
257 
258  //value parameter map
259  ob_start();
260  echo '<div>';
261  $new_prefix = str_replace(array('[',']'), '_', $prefix);
262  hidden_field($prefix.'[new_prefix]', $new_prefix);
263 
264  $parameter_map_value = array_get_index($settings, 'value_map', serialize(Array()));
265  $atr_parameter_map = new Asset_Attribute_Parameter_Map(0, $parameter_map_value);
266 
267  $atr_parameter_map->setParameter('value', 'Value');
268  echo $atr_parameter_map->paint($new_prefix.'_parameter_map', !$write_access);
269  echo '</div>';
270  $parameter_map_input = ob_get_clean();
271 
272  $output = translate('trigger_action_set_cookie_set_name_value', $name_input, $value_input);
273  $output .= '<br />'.translate('trigger_action_set_cookie_set_expire_time', $expire_time_input).'<br /><i>'. translate('trigger_action_set_cookie_expire_time_note').'</i>';
274  $output .= '<br />'.translate('trigger_action_set_cookie_set_path', $path_input).'<br /><i>'.translate('trigger_action_set_cookie_path_note').'</i>';
275  $output .= '<br />'.translate('trigger_action_set_cookie_set_domain', $domain_input).'<br /><i>'.translate('trigger_action_set_cookie_domain_note').'</i>';
276  $output .= '<br />'.translate('trigger_action_set_cookie_set_secure', $secure_input, translate('yes'));
277  $output .= '<br />'.translate('trigger_action_set_cookie_set_http_only', $http_only_input, translate('yes')).'<br /><br />';
278 
279  $output .= $parameter_map_input;
280 
281  return $output;
282 
283  }//end getInterface()
284 
285 
297  public static function processInterface(&$settings, $request_data)
298  {
299 
300  $settings['name'] = array_get_index($request_data, 'name');
301  $settings['value'] = array_get_index($request_data, 'value');
302  $settings['expire_time'] = array_get_index($request_data, 'expire_time', 0);
303  $settings['path'] = array_get_index($request_data, 'path');
304  $settings['domain'] = array_get_index($request_data, 'domain');
305  $secure = array_get_index($request_data, 'secure', '0');
306  $settings['secure'] = $secure == '1' ? TRUE : FALSE;
307  $http_only = array_get_index($request_data, 'http_only', '0');
308  $settings['http_only'] = $http_only == '1' ? TRUE : FALSE;
309 
310  $settings['value_map'] = '';
311 
312  $new_prefix = str_replace(array('[',']'), '_', array_get_index($request_data, 'new_prefix', ''));
313  if ($new_prefix) {
314  $atr_parameter_map = new Asset_Attribute_Parameter_Map();
315  $atr_parameter_map->process($new_prefix.'_parameter_map');
316  $settings['value_map'] = $atr_parameter_map->value;
317  }
318 
319  return FALSE;
320 
321  }//end processInterface()
322 
323 
336  public static function replaceKeywords($value, Asset $event_asset)
337  {
338  // replace global keywords
339  replace_global_keywords($value);
340 
341  // replace event asset keywords
342  $keywords = retrieve_keywords_replacements($value);
343  $replacements = Array();
344  foreach ($keywords as $keyword) {
345  $replacements[$keyword] = $event_asset->getKeywordReplacement($keyword);
346  }
347  replace_keywords($value, $replacements);
348 
349  return $value;
350 
351  }//end replaceKeywordsInWebPath()
352 
353 
354 }//end class
355 
356 ?>