Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
site_network.inc
1 <?php
18 require_once SQ_CORE_PACKAGE_PATH.'/folder/folder.inc';
19 
31 class Site_Network extends Folder
32 {
33 
34 
41  function Site_Network($assetid=0)
42  {
43  $this->Folder($assetid);
44 
45  }//end constructor
46 
47 
58  function _createAdditional(&$link)
59  {
60  return TRUE;
61 
62  }//end _createAdditional()
63 
64 
72  function _getAllowedLinks()
73  {
74 
75  return Array(
76  SQ_LINK_TYPE_1 => Array(
77  'site' => Array(
78  'card' => 'M',
79  'exclusive' => FALSE,
80  ),
81  ),
82  SQ_LINK_TYPE_2 => Array(
83  'site' => Array(
84  'card' => 'M',
85  'exclusive' => FALSE,
86  ),
87  ),
88  SQ_LINK_NOTICE => Array(
89  'site' => Array(
90  'card' => 'M',
91  'exclusive' => FALSE,
92  ),
93  ),
94  );
95 
96  }//end _getAllowedLinks()
97 
98 
111  function canCreateLink(&$minor, $link_type, $exclusive)
112  {
113  $result = parent::canCreateLink($minor, $link_type, $exclusive);
114  if ($result !== TRUE) return $result;
115 
116  if (($minor instanceof Site) && ($link_type != SQ_LINK_NOTICE)) {
117  // site assets can only be in a single site network
118  $network_link = $GLOBALS['SQ_SYSTEM']->am->getLinks($minor->id, SQ_SC_LINK_SIGNIFICANT, 'site_network', FALSE, 'minor');
119  if (!empty($network_link)) {
120  return translate('core_one_network_for_site');
121  }
122  }
123 
124  return TRUE;
125 
126  }//end canCreateLink()
127 
128 
142  function canMoveLink(&$minor, &$old_major, $link_type)
143  {
144  if (($minor instanceof Site) && ($old_major instanceof Site_Network) && ($link_type != SQ_LINK_NOTICE)) {
145  // if we are moving from a site network, we can tolerate one existing
146  // network link - the one being moved from
147  $network_link = $GLOBALS['SQ_SYSTEM']->am->getLinks($minor->id, SQ_SC_LINK_SIGNIFICANT, 'site_network', FALSE, 'minor');
148  if (count($network_link) > 1) {
149  return translate('core_one_network_for_site');
150  }
151  } else {
152  // if we aren't moving from a site network, the usual check applies
153  return parent::canMoveLink($minor, $old_major, $link_type);
154  }
155 
156  return TRUE;
157 
158  }//end canMoveLink()
159 
160 
167  function getPrimaryURL()
168  {
169  if (!isset($this->_tmp['primary_url'])) {
170  $site_link = $GLOBALS['SQ_SYSTEM']->am->getLink($this->id, SQ_LINK_NOTICE, 'site', FALSE, 'primary_site');
171  if (empty($site_link)) return '';
172 
173  try {
174  $bind_vars = Array('assetid'=> $site_link['minorid']);
175  $primary_url_data = MatrixDAL::executeAll('site_network', 'getPrimaryURL', $bind_vars);
176  } catch (DALException $e) {
177  throw new Exception('Unable to get primary URL due to database error: '.$e->getMessage());
178  }
179 
180  if (empty($primary_url_data)) return '';
181  $primary_url = $primary_url_data[0]['url'];
182 
183  if ($primary_url_data[0]['https'] == '1') {
184  $primary_url = 'https://'.$primary_url;
185  } else {
186  $primary_url = 'http://'.$primary_url;
187  }
188 
189  $primary_url = sq_root_url($primary_url);
190  $this->_tmp['primary_url'] = $primary_url;
191  }
192 
193  return $this->_tmp['primary_url'];
194 
195  }//end getPrimaryURL()
196 
197 
209  function unserialiseSessionFile($session_file)
210  {
211  $session_str = file_get_contents($session_file);
212  // break the session at the word boundaries and the pipes
213  $parts = preg_split('/\w+\|/', $session_str, -1, PREG_SPLIT_OFFSET_CAPTURE);
214  $session_arr = Array();
215 
216  for ($i = 0; $i < count($parts); $i++) {
217  if ($i == count($parts) - 1) continue;
218  $offset = $parts[$i][1] + strlen($parts[$i][0]);
219  $len = $parts[$i + 1][1] - 1 - $offset;
220 
221  $key = substr($session_str, $offset, $len);
222  $session_arr[$key] = unserialize($parts[$i + 1][0]);
223  }
224  return $session_arr;
225 
226  }//end unserialiseSessionFile()
227 
228 
238  function serialiseSessionFile($session_file, $session_contents)
239  {
240  require_once SQ_FUDGE_PATH.'/general/file_system.inc';
241 
242  if (!file_exists($session_file)) {
243  trigger_localised_error('CORE0072', E_USER_WARNING, $session_file);
244  return FALSE;
245  }
246 
247  if (!is_array($session_contents)) {
248  trigger_localised_error('CORE0004', E_USER_WARNING, gettype($session_contents));
249  return FALSE;
250  }
251 
252  $session_str = '';
253  foreach ($session_contents as $key => $val) {
254  $session_str .= $key.'|'.serialize($val);
255  }
256 
257  if (!string_to_file($session_str, $session_file)) {
258  trigger_localised_error('CORE0021', E_USER_WARNING, $session_file);
259  return FALSE;
260  }
261  return TRUE;
262 
263  }//end serialiseSessionFile()
264 
265 
278  function syncSessionFile($pri_sessionid)
279  {
280  $pri_sess = $this->unserialiseSessionFile(SQ_CACHE_PATH.'/sess_'.$pri_sessionid);
281 
282  if (!is_array($pri_sess)) {
283  // something is definately wrong
284  trigger_localised_error('CORE0071', E_USER_ERROR);
285  return FALSE;
286  }
287 
288  $pri_timestamp = array_get_index($pri_sess, 'SQ_SESSION_TIMESTAMP', -1);
289  $sec_timestamp = array_get_index($_SESSION, 'SQ_SESSION_TIMESTAMP', -1);
290  $pri_login_key = array_get_index($pri_sess, 'SQ_LOGIN_KEY', NULL);
291  $sec_login_key = array_get_index($_SESSION, 'SQ_LOGIN_KEY', NULL);
292 
293  // if primary is younger
294  if ($pri_timestamp > $sec_timestamp || $pri_timestamp == -1) {
295  // copy primary to secondary
296  $_SESSION = $pri_sess;
297  } else {
298  // copy secondary to primary
299  $pri_sess = $_SESSION;
300  }
301 
302  $now = time();
303  $pri_sess['SQ_SESSION_TIMESTAMP'] = $now;
304  $_SESSION['SQ_SESSION_TIMESTAMP'] = $now;
305 
306  // preserve login keys
307  if (!is_null($pri_login_key)) {
308  $pri_sess['SQ_LOGIN_KEY'] = $pri_login_key;
309  }
310  if (!is_null($sec_login_key)) {
311  $_SESSION['SQ_LOGIN_KEY'] = $sec_login_key;
312  }
313 
314  // save the sessionid of the primary so that we
315  // know that we have run this script before. We won't have to
316  // do anther browser refresh if we know this.
317  $_SESSION['PRIMARY_SESSIONID'] = $pri_sessionid;
318  $pri_sess['PRIMARY_SESSIONID'] = $pri_sessionid;
319 
320  // *** JEDI MIND TRICK *** you did not see us doing this... move along
321  if (!$this->serialiseSessionFile(SQ_CACHE_PATH.'/sess_'.$pri_sessionid, $pri_sess)) {
322  trigger_localised_error('CORE0020', E_USER_ERROR);
323  return FALSE;
324  }
325  return TRUE;
326 
327  }//end syncSessionFile()
328 
329 
330 }//end class
331 
332 ?>