Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
squiz_suite_system_search.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 
58  public function getSitemapXml($urls, $excludes)
59  {
60  $xml = Array();
61  $fb = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('funnelback_manager');
62  $suite = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('suite_manager');
63  $info = $suite->getCurrentProduct();
64  $systemid = array_get_index($info, 'systemid', NULL);
65  if ($systemid === NULL) {
66  return '';
67  }//end if
68 
69  // Remove duplicate
70  $urls = array_unique($urls);
71  $excludes = array_unique($excludes);
72 
73  foreach ($urls as $single_url) {
74  $protocol = parse_url($single_url, PHP_URL_SCHEME);
75  $real_url = str_replace($protocol.'://', '', $single_url);
76 
77  $asset = $GLOBALS['SQ_SYSTEM']->am->getAssetFromURL($protocol, $real_url, TRUE, TRUE);
78 
79  // Asset not found, skip over
80  if (is_null($asset)) continue;
81 
82  // Pass into Funnelback
83  $collected_xmls = $fb->getXMLCache(Array($single_url), $asset->id, FALSE, $excludes);
84 
85  foreach ($collected_xmls as $collect_xml) {
86  $content = array_get_index($collect_xml, 'xml', '');
87  if (empty($content)) continue;
88 
89  $xml[] = $content;
90  }//end foreach
91  }//end foreach
92 
93  // Remove duplicates
94  $xml = array_unique($xml);
95 
96  // Add in the Product Key for SquizSuite
97  foreach ($xml as $index => $piece) {
98  if ($systemid !== NULL) {
99  // If the requesterid is set, then we must add the requesterid on the fly.
100  preg_match('/<fbmeta name="access_locks" content="(.*?)" \/>/i', $piece, $matches);
101 
102  if (isset($matches[1])) {
103  preg_match_all('/\'(.*?)(\'\,|\'$)/i', $matches[1], $userid_matches);
104  if (isset($userid_matches[1])) {
105  $userids = $userid_matches[1];
106  $real_userids = array();
107  foreach ($userids as $userid) {
108  if ($userid == 'public') {
109  $real_userids[] = "'public'";
110  } else {
111  $real_userids[] = "'".$systemid.'://'.$userid."'";
112  }//end if
113  }//end foreach
114 
115  $real_userids_string = implode(',', $real_userids);
116 
117  // Finally, replace the corrected string.
118  $pattern = '/<fbmeta name="access_locks" content="(.*?)" \/>/i';
119  $replace = '<fbmeta name="access_locks" content="'.$real_userids_string.'" />';
120  $piece = preg_replace($pattern, $replace, $piece);
121  }
122  }
123  }
124 
125  $xml[$index] = preg_replace('/<url>(.*?)<\/url>/i', '<url>\\1<fbmeta name="SQUIZPRODUCTID" content="'.$systemid.'" /></url>', $piece);
126  }
127 
128  if (!empty($xml)) {
129  return $xml;
130  }//end if
131 
132  throw new Exception('Error: No urls found');
133 
134  }//end getSitemapXml()
135 
136 
145  public function getPageContent($url)
146  {
147  $contents = '';
148  $protocol = parse_url($url, PHP_URL_SCHEME);
149  $base_url = str_replace($protocol.'://', '', $url);
150  $suite = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('suite_manager');
151  $asset = $GLOBALS['SQ_SYSTEM']->am->getAssetFromUrl($protocol, $base_url);
152  $root_url = $GLOBALS['SQ_SYSTEM']->am->getRootURL($base_url);
153 
154  // flag to see if the context is changed to anything but default
155  $context_changed = FALSE;
156  if (isset($root_url['base_contextid']) && $root_url['base_contextid'] != '0') {
157  $GLOBALS['SQ_SYSTEM']->changeContext($root_url['base_contextid']);
158  $context_changed = TRUE;
159  }
160 
161  // Fudge Server Var so designs etc. work
162  $old_server_host = $_SERVER['HTTP_HOST'];
163  $old_server_self = $_SERVER['PHP_SELF'];
164  $_SERVER['HTTP_HOST'] = parse_url($url, PHP_URL_HOST);
165  $_SERVER['PHP_SELF'] = parse_url($url, PHP_URL_PATH);
166 
167  if ($asset->id !== $suite->id) {
168  if ($asset instanceof File) {
169  ob_start();
170  $existing = $asset->getExistingFile();
171  $mime = 'text/plain';
172  $filename = $asset->attr('title');
173  if (!empty($existing)) {
174  require_once SQ_FUDGE_PATH.'/standards_lists/mime_types.inc';
175  require_once SQ_FUDGE_PATH.'/general/file_system.inc';
176 
177  $ext = get_file_type($existing['filename']);
178  if (($asset->status & SQ_SC_STATUS_SAFE_EDITING) && ($GLOBALS['SQ_SYSTEM']->user instanceof Public_User)) {
179  // strip file versioning suffix from the file extension, e.g. 'pdf,ffv5' becomes 'pdf'
180  $ext = preg_replace('/,ffv\d+$/', '', $ext);
181  $existing['filename'] = preg_replace('/,ffv\d+$/', '', $existing['filename']);
182  }
183  $mime = (empty($standards_lists_mime_types[$ext])) ? 'text/plain' : $standards_lists_mime_types[$ext];
184  $filename = $existing['filename'];
185  readfile($existing['path']);
186  }
187  $contents = ob_get_contents();
188  ob_end_clean();
189 
190  if (!empty($contents)) {
191  $response = Array(
192  '_base64' => chunk_split(base64_encode($contents)),
193  'mimetype' => $mime,
194  'filename' => $filename,
195  );
196  } else {
197  $response = $contents;
198  }
199  } else {
200  ob_start();
201  $asset->printFrontend();
202  $response = ob_get_contents();
203  ob_end_clean();
204  }//end if
205  }//end if
206 
207  if ($context_changed) $GLOBALS['SQ_SYSTEM']->restoreContext();
208 
209  $_SERVER['HTTP_HOST'] = $old_server_host;
210  $_SERVER['PHP_SELF'] = $old_server_self;
211 
212  return $response;
213 
214  throw new Exception('Error: No urls found');
215 
216  }//end getPageContent()
217 
218 
226  public static function getAssetInfo(array $urls)
227  {
228  $info = array();
229 
230  foreach ($urls as $real_url) {
231  $am = $GLOBALS['SQ_SYSTEM']->am;
232 
233  $url_parts = parse_url($real_url);
234  $protocol = array_get_index($url_parts, 'scheme', '');
235  if (empty($protocol) === FALSE) {
236  $url = str_replace($protocol.'://', '', $real_url);
237  }
238  $asset = $am->getAssetFromUrl($protocol, $url, TRUE, TRUE);
239  if ($asset !== NULL) {
240  $type = $asset->type();
241  $iconPath = $am->getAssetIconURL($type);
242 
243  $info[$real_url] = array(
244  'assetid' => $asset->id,
245  'name' => $asset->attr('name'),
246  'icon' => $iconPath,
247  );
248  $am->forgetAsset($asset, TRUE);
249  unset($asset);
250  } else {
251  $info[$real_url] = NULL;
252  }
253  }//end foreach
254 
255  return $info;
256 
257  }//end getAssetInfo()
258 
259 
270  public static function getFileIndexingComponents($urls)
271  {
272  $info = Array();
273  if (!is_array($urls)) $urls = Array($urls);
274 
275  $am = $GLOBALS['SQ_SYSTEM']->am;
276  $mm = $GLOBALS['SQ_SYSTEM']->getMetadataManager();
277  $fm = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('funnelback_manager', TRUE);
278 
279  foreach ($urls as $real_url) {
280 
281  $url_parts = parse_url($real_url);
282  $protocol = array_get_index($url_parts, 'scheme', '');
283  if (empty($protocol) === FALSE) {
284  $url = str_replace($protocol.'://', '', $real_url);
285  }
286 
287  $asset = $am->getAssetFromUrl($protocol, $url, TRUE, TRUE);
288  if ($asset !== NULL && strpos($url, '__data') !== FALSE) {
289  // found the asset?
290  // get all we want
291  $metadata = $mm->getMetadata($asset->id);
292  foreach ($metadata as $meta) {
293  $name = $meta[0]['name'];
294 
295  $value = $meta[0]['value'];
296  require_once SQ_FUDGE_PATH.'/general/text.inc';
297  $keywords = retrieve_keywords_replacements($value);
298  $replacements = Array();
299  foreach ($keywords as $keyword) {
300  $replacements[$keyword] = $asset->getKeywordReplacement($keyword);
301  }//end foreach
302  replace_keywords($value, $replacements);
303  $name = $fm->encodeText(htmlspecialchars($name, ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET));
304  $value = $fm->encodeText(htmlspecialchars($value, ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET));
305  $info[$real_url]['metadata'][$name] = $value;
306  }//end foreach
307 
308  // get the security keys
309  $info[$real_url]['security_locks'] = $fm->getAccessLocks($asset);
310 
311  $info[$real_url]['SQUIZASSETYPE'] = $asset->type();
312  $info[$real_url]['last_modified'] = date('Y-m-d', $asset->updated).'T'.date('H:i:s', $asset->updated);
313 
314  // get author, title, file name
315  $info[$real_url]['file_title'] = $asset->attr('title');
316  $info[$real_url]['file_name'] = $asset->name;
317  if ($asset->created_userid != 0) {
318  $created_by = $GLOBALS['SQ_SYSTEM']->am->getAsset($asset->created_userid);
319  $info[$real_url]['author_name'] = $created_by->name;
320  $info[$real_url]['author_id'] = $created_by->id;
321  }
322  $info[$real_url]['created'] = date('Y-m-d', $asset->created).'T'.date('H:i:s', $asset->created);
323 
324 
325  $am->forgetAsset($asset, TRUE);
326  unset($asset);
327  } else {
328  $info[$real_url] = NULL;
329  }
330  }//end foreach
331 
332  return json_encode($info);
333 
334  }//end getFileIndexingComponents()
335 
336 
346  public static function changeStatusFunnelbackUser($userid, $set_to='activate')
347  {
348  $am = $GLOBALS['SQ_SYSTEM']->am;
349 
350  $status = SQ_STATUS_UNDER_CONSTRUCTION;
351  if ($set_to != 'activate' && $set_to != 'deactivate') {
352  return Array('success' => FALSE);
353  } else {
354  if ($set_to == 'activate') $status = SQ_STATUS_LIVE;
355  }
356  // if the userid isnt provided
357  // or it doesn't exist on system
358  // return FALSE but no errors
359  if (empty($userid)) return Array('success' => FALSE);
360  if (!$am->assetExists($userid)) return Array('success' => FALSE);
361 
362  // passed userid doesnt match
363  // the user configured return false
364  $fm = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('funnelback_manager', TRUE);
365  if ($fm->attr('user') != $userid) return Array('success' => FALSE);
366 
367  // if the user is already live, less work for us
368  $fb_user = $am->getAsset($userid);
369  if ($fb_user->status === $status) return Array('success' => TRUE);
370 
371  // all checks done and now we sure we need to
372  // change the status of the user asset passed
373  require_once SQ_SYSTEM_ROOT.'/core/hipo/jobs/hipo_job_edit_status.inc';
374  $init_hipo = new HIPO_Job_Edit_Status();
375  $running_vars = Array(
376  'assetid' => $fb_user->id,
377  'new_status' => $status,
378  'dependants_only' => 0,
379  );
380 
381  $init_hipo->setRunningVars($running_vars);
382  $init_hipo->_steps = $init_hipo->getInitialStepData();
383  if (!$init_hipo->prepare()) {
384  error_log('Could not initialise HIPO job');
385  return Array('success' => FALSE);
386  }
387  $init_hipo->freestyle();
388 
389  $errors = $init_hipo->getErrors();
390  if(!empty($errors)) {
391  error_log($errors);
392  return Array('success' => FALSE);
393  }
394 
395  return Array('success' => TRUE);
396 
397  }//end activateFunnelbackUser()
398 
399 
411  public static function validateActiveUser($username, $password='', $return_keys=FALSE)
412  {
413  if (empty($username)) return FALSE;
414  $res = FALSE;
415 
416  $user = NULL;
417  // Get a list of all the installed authentication systems
418  $auth_folder = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('authentication_folder');
419  if (is_null($auth_folder)) {
420  trigger_localised_error('SYS0249', E_USER_ERROR);
421  return;
422  }
423 
424  $auth_systems = $auth_folder->getAuthSystems();
425  foreach ($auth_systems as $systemid) {
426  $system = $GLOBALS['SQ_SYSTEM']->am->getAsset($systemid);
427  if (is_null($system)) continue;
428 
429  if (empty($password)) {
430  $user = $system->locateUser($username);
431  } else {
432  // okie seems like we are passed a password too
433  $user = $system->authenticateUser($username, $password);
434  }
435 
436  if (!is_null($user)) {
437  $res = TRUE;
438  break;
439  }
440  }//end forach
441 
442  // okie so we have found the user and we want to sent back
443  // the user id and id of all the groups it belongs to
444  if ($return_keys && $res) {
445  $res = array_merge(Array($user->id), $user->getUserGroups());
446  }
447 
448  return $res;
449 
450  }//end activateFunnelbackUser()
451 
452 
453 }//end class
454 
455 ?>