Elgg  Version 1.9
elgglib.php
Go to the documentation of this file.
1 <?php
19 function elgg_register_library($name, $location) {
21 
22  if (!isset($CONFIG->libraries)) {
23  $CONFIG->libraries = array();
24  }
25 
26  $CONFIG->libraries[$name] = $location;
27 }
28 
40 
41  static $loaded_libraries = array();
42 
43  if (in_array($name, $loaded_libraries)) {
44  return;
45  }
46 
47  if (!isset($CONFIG->libraries)) {
48  $CONFIG->libraries = array();
49  }
50 
51  if (!isset($CONFIG->libraries[$name])) {
52  $error = $name . " is not a registered library";
54  }
55 
56  if (!include_once($CONFIG->libraries[$name])) {
57  $error = "Could not load the " . $name . " library from " . $CONFIG->libraries[$name];
59  }
60 
61  $loaded_libraries[] = $name;
62 }
63 
79 function forward($location = "", $reason = 'system') {
80  if (!headers_sent($file, $line)) {
81  if ($location === REFERER) {
82  $location = _elgg_services()->request->headers->get('Referer');
83  }
84 
85  $location = elgg_normalize_url($location);
86 
87  // return new forward location or false to stop the forward or empty string to exit
89  $params = array('current_url' => $current_page, 'forward_url' => $location);
90  $location = elgg_trigger_plugin_hook('forward', $reason, $params, $location);
91 
92  if ($location) {
93  header("Location: {$location}");
94  exit;
95  } else if ($location === '') {
96  exit;
97  }
98  } else {
99  throw new SecurityException("Redirect could not be issued due to headers already being sent. Halting execution for security. "
100  . "Output started in file $file at line $line. Search http://learn.elgg.org/ for more information.");
101  }
102 }
103 
128 function elgg_register_js($name, $url, $location = 'head', $priority = null) {
129  return elgg_register_external_file('js', $name, $url, $location, $priority);
130 }
131 
153 function elgg_define_js($name, $config) {
154  $src = elgg_extract('src', $config);
155 
156  if ($src) {
158  _elgg_services()->amdConfig->addPath($name, $url);
159  }
160 
161  // shimmed module
162  if (isset($config['deps']) || isset($config['exports'])) {
163  _elgg_services()->amdConfig->addShim($name, $config);
164  }
165 }
166 
176  return elgg_unregister_external_file('js', $name);
177 }
178 
190 function elgg_load_js($name) {
192 }
193 
194 
203  _elgg_services()->amdConfig->addDependency($name);
204 }
205 
206 
215 function elgg_get_loaded_js($location = 'head') {
216  return elgg_get_loaded_external_files('js', $location);
217 }
218 
229 function elgg_register_css($name, $url, $priority = null) {
230  return elgg_register_external_file('css', $name, $url, 'head', $priority);
231 }
232 
242  return elgg_unregister_external_file('css', $name);
243 }
244 
256 function elgg_load_css($name) {
258 }
259 
267  return elgg_get_loaded_external_files('css', 'head');
268 }
269 
282 function elgg_register_external_file($type, $name, $url, $location, $priority = 500) {
283  global $CONFIG;
284 
285  if (empty($name) || empty($url)) {
286  return false;
287  }
288 
291 
293 
294  $name = trim(strtolower($name));
295 
296  // normalize bogus priorities, but allow empty, null, and false to be defaults.
297  if (!is_numeric($priority)) {
298  $priority = 500;
299  }
300 
301  // no negative priorities right now.
302  $priority = max((int)$priority, 0);
303 
304  $item = elgg_extract($name, $CONFIG->externals_map[$type]);
305 
306  if ($item) {
307  // updating a registered item
308  // don't update loaded because it could already be set
309  $item->url = $url;
310  $item->location = $location;
311 
312  // if loaded before registered, that means it hasn't been added to the list yet
313  if ($CONFIG->externals[$type]->contains($item)) {
314  $priority = $CONFIG->externals[$type]->move($item, $priority);
315  } else {
316  $priority = $CONFIG->externals[$type]->add($item, $priority);
317  }
318  } else {
319  $item = new stdClass();
320  $item->loaded = false;
321  $item->url = $url;
322  $item->location = $location;
323 
324  $priority = $CONFIG->externals[$type]->add($item, $priority);
325  }
326 
327  $CONFIG->externals_map[$type][$name] = $item;
328 
329  return $priority !== false;
330 }
331 
342  global $CONFIG;
343 
345 
346  $name = trim(strtolower($name));
347  $item = elgg_extract($name, $CONFIG->externals_map[$type]);
348 
349  if ($item) {
350  unset($CONFIG->externals_map[$type][$name]);
351  return $CONFIG->externals[$type]->remove($item);
352  }
353 
354  return false;
355 }
356 
367  global $CONFIG;
368 
370 
371  $name = trim(strtolower($name));
372 
373  $item = elgg_extract($name, $CONFIG->externals_map[$type]);
374 
375  if ($item) {
376  // update a registered item
377  $item->loaded = true;
378  } else {
379  $item = new stdClass();
380  $item->loaded = true;
381  $item->url = '';
382  $item->location = '';
383 
384  $CONFIG->externals[$type]->add($item);
385  $CONFIG->externals_map[$type][$name] = $item;
386  }
387 }
388 
398 function elgg_get_loaded_external_files($type, $location) {
399  global $CONFIG;
400 
401  if (isset($CONFIG->externals) && $CONFIG->externals[$type] instanceof ElggPriorityList) {
402  $items = $CONFIG->externals[$type]->getElements();
403 
404  $callback = "return \$v->loaded == true && \$v->location == '$location';";
405  $items = array_filter($items, create_function('$v', $callback));
406  if ($items) {
407  array_walk($items, create_function('&$v,$k', '$v = $v->url;'));
408  }
409  return $items;
410  }
411  return array();
412 }
413 
421  global $CONFIG;
422 
423  if (!isset($CONFIG->externals)) {
424  $CONFIG->externals = array();
425  }
426 
427  if (!isset($CONFIG->externals[$type]) || !$CONFIG->externals[$type] instanceof ElggPriorityList) {
428  $CONFIG->externals[$type] = new ElggPriorityList();
429  }
430 
431  if (!isset($CONFIG->externals_map)) {
432  $CONFIG->externals_map = array();
433  }
434 
435  if (!isset($CONFIG->externals_map[$type])) {
436  $CONFIG->externals_map[$type] = array();
437  }
438 }
439 
452 function elgg_get_file_list($directory, $exceptions = array(), $list = array(),
453 $extensions = null) {
454 
455  $directory = sanitise_filepath($directory);
456  if ($handle = opendir($directory)) {
457  while (($file = readdir($handle)) !== false) {
458  if (!is_file($directory . $file) || in_array($file, $exceptions)) {
459  continue;
460  }
461 
462  if (is_array($extensions)) {
463  if (in_array(strrchr($file, '.'), $extensions)) {
464  $list[] = $directory . $file;
465  }
466  } else {
467  $list[] = $directory . $file;
468  }
469  }
470  closedir($handle);
471  }
472 
473  return $list;
474 }
475 
484 function sanitise_filepath($path, $append_slash = true) {
485  // Convert to correct UNIX paths
486  $path = str_replace('\\', '/', $path);
487  $path = str_replace('../', '/', $path);
488  // replace // with / except when preceeded by :
489  $path = preg_replace("/([^:])\/\//", "$1/", $path);
490 
491  // Sort trailing slash
492  $path = trim($path);
493  // rtrim defaults plus /
494  $path = rtrim($path, " \n\t\0\x0B/");
495 
496  if ($append_slash) {
497  $path = $path . '/';
498  }
499 
500  return $path;
501 }
502 
532 function system_messages($message = null, $register = "success", $count = false) {
533  $session = _elgg_services()->session;
534  $messages = $session->get('msg', array());
535  if (!isset($messages[$register]) && !empty($register)) {
536  $messages[$register] = array();
537  }
538  if (!$count) {
539  if (!empty($message) && is_array($message)) {
540  $messages[$register] = array_merge($messages[$register], $message);
541  return true;
542  } else if (!empty($message) && is_string($message)) {
543  $messages[$register][] = $message;
544  $session->set('msg', $messages);
545  return true;
546  } else if (is_null($message)) {
547  if ($register != "") {
548  $returnarray = array();
549  $returnarray[$register] = $messages[$register];
550  $messages[$register] = array();
551  } else {
552  $returnarray = $messages;
553  $messages = array();
554  }
555  $session->set('msg', $messages);
556  return $returnarray;
557  }
558  } else {
559  if (!empty($register)) {
560  return sizeof($messages[$register]);
561  } else {
562  $count = 0;
563  foreach ($messages as $submessages) {
564  $count += sizeof($submessages);
565  }
566  return $count;
567  }
568  }
569  return false;
570 }
571 
579 function count_messages($register = "") {
580  return system_messages(null, $register, true);
581 }
582 
593  return system_messages($message, "success");
594 }
595 
606  return system_messages($error, "error");
607 }
608 
669 function elgg_register_event_handler($event, $object_type, $callback, $priority = 500) {
670  return _elgg_services()->events->registerHandler($event, $object_type, $callback, $priority);
671 }
672 
683 function elgg_unregister_event_handler($event, $object_type, $callback) {
684  return _elgg_services()->events->unregisterHandler($event, $object_type, $callback);
685 }
686 
720 function elgg_trigger_event($event, $object_type, $object = null) {
721  return _elgg_services()->events->trigger($event, $object_type, $object);
722 }
723 
741 function elgg_trigger_before_event($event, $object_type, $object = null) {
742  return _elgg_services()->events->trigger("$event:before", $object_type, $object);
743 }
744 
760 function elgg_trigger_after_event($event, $object_type, $object = null) {
761  $options = array(
763  );
764  return _elgg_services()->events->trigger("$event:after", $object_type, $object, $options);
765 }
766 
780 function elgg_trigger_deprecated_event($event, $object_type, $object = null, $message, $version) {
781  $options = array(
784  );
785  return _elgg_services()->events->trigger($event, $object_type, $object, $options);
786 }
787 
853 function elgg_register_plugin_hook_handler($hook, $type, $callback, $priority = 500) {
854  return _elgg_services()->hooks->registerHandler($hook, $type, $callback, $priority);
855 }
856 
867 function elgg_unregister_plugin_hook_handler($hook, $entity_type, $callback) {
868  _elgg_services()->hooks->unregisterHandler($hook, $entity_type, $callback);
869 }
870 
925 function elgg_trigger_plugin_hook($hook, $type, $params = null, $returnvalue = null) {
926  return _elgg_services()->hooks->trigger($hook, $type, $params, $returnvalue);
927 }
928 
947  $timestamp = time();
948  error_log("Exception #$timestamp: $exception");
949 
950  // Wipe any existing output buffer
951  ob_end_clean();
952 
953  // make sure the error isn't cached
954  header("Cache-Control: no-cache, must-revalidate", true);
955  header('Expires: Fri, 05 Feb 1982 00:00:00 -0500', true);
956  // @note Do not send a 500 header because it is not a server error
957 
958  // we don't want the 'pagesetup', 'system' event to fire
959  global $CONFIG;
960  $CONFIG->pagesetupdone = true;
961 
962  try {
963  // allow custom scripts to trigger on exception
964  // $CONFIG->exception_include can be set locally in settings.php
965  // value should be a system path to a file to include
966  if (!empty($CONFIG->exception_include) && is_file($CONFIG->exception_include)) {
967  ob_start();
968  include $CONFIG->exception_include;
969  $exception_output = ob_get_clean();
970 
971  // if content is returned from the custom handler we will output
972  // that instead of our default failsafe view
973  if (!empty($exception_output)) {
974  echo $exception_output;
975  exit;
976  }
977  }
978 
979  elgg_set_viewtype('failsafe');
980 
981  if (elgg_is_admin_logged_in()) {
982  if (!elgg_view_exists("messages/exceptions/admin_exception")) {
983  elgg_set_viewtype('failsafe');
984  }
985 
986  $body = elgg_view("messages/exceptions/admin_exception", array(
987  'object' => $exception,
988  'ts' => $timestamp
989  ));
990  } else {
991  if (!elgg_view_exists("messages/exceptions/exception")) {
992  elgg_set_viewtype('failsafe');
993  }
994 
995  $body = elgg_view("messages/exceptions/exception", array(
996  'object' => $exception,
997  'ts' => $timestamp
998  ));
999  }
1000  echo elgg_view_page(elgg_echo('exception:title'), $body);
1001  } catch (Exception $e) {
1002  $timestamp = time();
1003  $message = $e->getMessage();
1004  echo "Fatal error in exception handler. Check log for Exception #$timestamp";
1005  error_log("Exception #$timestamp : fatal error in exception handler : $message");
1006  }
1007 }
1008 
1033 function _elgg_php_error_handler($errno, $errmsg, $filename, $linenum, $vars) {
1034  $error = date("Y-m-d H:i:s (T)") . ": \"$errmsg\" in file $filename (line $linenum)";
1035 
1036  switch ($errno) {
1037  case E_USER_ERROR:
1038  error_log("PHP ERROR: $error");
1039  register_error("ERROR: $error");
1040 
1041  // Since this is a fatal error, we want to stop any further execution but do so gracefully.
1042  throw new Exception($error);
1043  break;
1044 
1045  case E_WARNING :
1046  case E_USER_WARNING :
1047  case E_RECOVERABLE_ERROR: // (e.g. type hint violation)
1048 
1049  // check if the error wasn't suppressed by the error control operator (@)
1050  if (error_reporting()) {
1051  error_log("PHP WARNING: $error");
1052  }
1053  break;
1054 
1055  default:
1056  global $CONFIG;
1057  if (isset($CONFIG->debug) && $CONFIG->debug === 'NOTICE') {
1058  error_log("PHP NOTICE: $error");
1059  }
1060  }
1061 
1062  return true;
1063 }
1064 
1065 
1083 function elgg_log($message, $level = 'NOTICE') {
1084  static $levels = array(
1085  'INFO' => 200,
1086  'NOTICE' => 250,
1087  'WARNING' => 300,
1088  'DEBUG' => 300,
1089  'ERROR' => 400,
1090  );
1091 
1092  if ($level == 'DEBUG') {
1093  elgg_deprecated_notice("The 'DEBUG' level for logging has been deprecated.", 1.9);
1094  }
1095 
1096  $level = $levels[$level];
1097  return _elgg_services()->logger->log($message, $level);
1098 }
1099 
1114 function elgg_dump($value, $to_screen = true) {
1115  _elgg_services()->logger->dump($value, $to_screen);
1116 }
1117 
1126 function elgg_get_version($human_readable = false) {
1127  global $CONFIG;
1128 
1129  static $version, $release;
1130 
1131  if (isset($CONFIG->path)) {
1132  if (!isset($version) || !isset($release)) {
1133  if (!include($CONFIG->path . "version.php")) {
1134  return false;
1135  }
1136  }
1137  return $human_readable ? $release : $version;
1138  }
1139 
1140  return false;
1141 }
1142 
1171 function elgg_deprecated_notice($msg, $dep_version, $backtrace_level = 1) {
1172  // if it's a major release behind, visual and logged
1173  // if it's a 1 minor release behind, visual and logged
1174  // if it's for current minor release, logged.
1175  // bugfixes don't matter because we are not deprecating between them
1176 
1177  if (!$dep_version) {
1178  return false;
1179  }
1180 
1181  $elgg_version = elgg_get_version(true);
1182  $elgg_version_arr = explode('.', $elgg_version);
1183  $elgg_major_version = (int)$elgg_version_arr[0];
1184  $elgg_minor_version = (int)$elgg_version_arr[1];
1185 
1186  $dep_major_version = (int)$dep_version;
1187  $dep_minor_version = 10 * ($dep_version - $dep_major_version);
1188 
1189  $visual = false;
1190 
1191  if (($dep_major_version < $elgg_major_version) ||
1192  ($dep_minor_version < $elgg_minor_version)) {
1193  $visual = true;
1194  }
1195 
1196  $msg = "Deprecated in $dep_major_version.$dep_minor_version: $msg";
1197 
1198  if ($visual && elgg_is_admin_logged_in()) {
1199  register_error($msg);
1200  }
1201 
1202  // Get a file and line number for the log. Never show this in the UI.
1203  // Skip over the function that sent this notice and see who called the deprecated
1204  // function itself.
1205  $msg .= " Called from ";
1206  $stack = array();
1207  $backtrace = debug_backtrace();
1208  // never show this call.
1209  array_shift($backtrace);
1210  $i = count($backtrace);
1211 
1212  foreach ($backtrace as $trace) {
1213  $stack[] = "[#$i] {$trace['file']}:{$trace['line']}";
1214  $i--;
1215 
1216  if ($backtrace_level > 0) {
1217  if ($backtrace_level <= 1) {
1218  break;
1219  }
1220  $backtrace_level--;
1221  }
1222  }
1223 
1224  $msg .= implode("<br /> -> ", $stack);
1225 
1226  elgg_log($msg, 'WARNING');
1227 
1228  return true;
1229 }
1230 
1242 function elgg_http_build_url(array $parts, $html_encode = true) {
1243  // build only what's given to us.
1244  $scheme = isset($parts['scheme']) ? "{$parts['scheme']}://" : '';
1245  $host = isset($parts['host']) ? "{$parts['host']}" : '';
1246  $port = isset($parts['port']) ? ":{$parts['port']}" : '';
1247  $path = isset($parts['path']) ? "{$parts['path']}" : '';
1248  $query = isset($parts['query']) ? "?{$parts['query']}" : '';
1249  $fragment = isset($parts['fragment']) ? "#{$parts['fragment']}" : '';
1250 
1251  $string = $scheme . $host . $port . $path . $query . $fragment;
1252 
1253  if ($html_encode) {
1254  return elgg_format_url($string);
1255  } else {
1256  return $string;
1257  }
1258 }
1259 
1277 function elgg_add_action_tokens_to_url($url, $html_encode = false) {
1279  $components = parse_url($url);
1280 
1281  if (isset($components['query'])) {
1282  $query = elgg_parse_str($components['query']);
1283  } else {
1284  $query = array();
1285  }
1286 
1287  if (isset($query['__elgg_ts']) && isset($query['__elgg_token'])) {
1288  return $url;
1289  }
1290 
1291  // append action tokens to the existing query
1292  $query['__elgg_ts'] = time();
1293  $query['__elgg_token'] = generate_action_token($query['__elgg_ts']);
1294  $components['query'] = http_build_query($query);
1295 
1296  // rebuild the full url
1297  return elgg_http_build_url($components, $html_encode);
1298 }
1299 
1312  return elgg_http_add_url_query_elements($url, array($element => null));
1313 }
1314 
1325 function elgg_http_add_url_query_elements($url, array $elements) {
1326  $url_array = parse_url($url);
1327 
1328  if (isset($url_array['query'])) {
1329  $query = elgg_parse_str($url_array['query']);
1330  } else {
1331  $query = array();
1332  }
1333 
1334  foreach ($elements as $k => $v) {
1335  if ($v === null) {
1336  unset($query[$k]);
1337  } else {
1338  $query[$k] = $v;
1339  }
1340  }
1341 
1342  // why check path? A: if no path, this may be a relative URL like "?foo=1". In this case,
1343  // the output "" would be interpreted the current URL, so in this case we *must* set
1344  // a query to make sure elements are removed.
1345  if ($query || empty($url_array['path'])) {
1346  $url_array['query'] = http_build_query($query);
1347  } else {
1348  unset($url_array['query']);
1349  }
1350  $string = elgg_http_build_url($url_array, false);
1351 
1352  return $string;
1353 }
1354 
1369 function elgg_http_url_is_identical($url1, $url2, $ignore_params = array('offset', 'limit')) {
1370  $url1 = elgg_normalize_url($url1);
1371  $url2 = elgg_normalize_url($url2);
1372 
1373  // @todo - should probably do something with relative URLs
1374 
1375  if ($url1 == $url2) {
1376  return true;
1377  }
1378 
1379  $url1_info = parse_url($url1);
1380  $url2_info = parse_url($url2);
1381 
1382  if (isset($url1_info['path'])) {
1383  $url1_info['path'] = trim($url1_info['path'], '/');
1384  }
1385  if (isset($url2_info['path'])) {
1386  $url2_info['path'] = trim($url2_info['path'], '/');
1387  }
1388 
1389  // compare basic bits
1390  $parts = array('scheme', 'host', 'path');
1391 
1392  foreach ($parts as $part) {
1393  if ((isset($url1_info[$part]) && isset($url2_info[$part]))
1394  && $url1_info[$part] != $url2_info[$part]) {
1395  return false;
1396  } elseif (isset($url1_info[$part]) && !isset($url2_info[$part])) {
1397  return false;
1398  } elseif (!isset($url1_info[$part]) && isset($url2_info[$part])) {
1399  return false;
1400  }
1401  }
1402 
1403  // quick compare of get params
1404  if (isset($url1_info['query']) && isset($url2_info['query'])
1405  && $url1_info['query'] == $url2_info['query']) {
1406  return true;
1407  }
1408 
1409  // compare get params that might be out of order
1410  $url1_params = array();
1411  $url2_params = array();
1412 
1413  if (isset($url1_info['query'])) {
1414  if ($url1_info['query'] = html_entity_decode($url1_info['query'])) {
1415  $url1_params = elgg_parse_str($url1_info['query']);
1416  }
1417  }
1418 
1419  if (isset($url2_info['query'])) {
1420  if ($url2_info['query'] = html_entity_decode($url2_info['query'])) {
1421  $url2_params = elgg_parse_str($url2_info['query']);
1422  }
1423  }
1424 
1425  // drop ignored params
1426  foreach ($ignore_params as $param) {
1427  if (isset($url1_params[$param])) {
1428  unset($url1_params[$param]);
1429  }
1430  if (isset($url2_params[$param])) {
1431  unset($url2_params[$param]);
1432  }
1433  }
1434 
1435  // array_diff_assoc only returns the items in arr1 that aren't in arrN
1436  // but not the items that ARE in arrN but NOT in arr1
1437  // if arr1 is an empty array, this function will return 0 no matter what.
1438  // since we only care if they're different and not how different,
1439  // add the results together to get a non-zero (ie, different) result
1440  $diff_count = count(array_diff_assoc($url1_params, $url2_params));
1441  $diff_count += count(array_diff_assoc($url2_params, $url1_params));
1442  if ($diff_count > 0) {
1443  return false;
1444  }
1445 
1446  return true;
1447 }
1448 
1464 function elgg_extract($key, array $array, $default = null, $strict = true) {
1465  if (!is_array($array)) {
1466  return $default;
1467  }
1468 
1469  if ($strict) {
1470  return (isset($array[$key])) ? $array[$key] : $default;
1471  } else {
1472  return (isset($array[$key]) && !empty($array[$key])) ? $array[$key] : $default;
1473  }
1474 }
1475 
1496 function elgg_sort_3d_array_by_value(&$array, $element, $sort_order = SORT_ASC,
1497 $sort_type = SORT_LOCALE_STRING) {
1498 
1499  $sort = array();
1500 
1501  foreach ($array as $v) {
1502  if (isset($v[$element])) {
1503  $sort[] = strtolower($v[$element]);
1504  } else {
1505  $sort[] = null;
1506  }
1507  };
1508 
1509  return array_multisort($sort, $sort_order, $sort_type, $array);
1510 }
1511 
1522 function ini_get_bool($ini_get_arg) {
1523  $temp = strtolower(ini_get($ini_get_arg));
1524 
1525  if ($temp == '1' || $temp == 'on' || $temp == 'true') {
1526  return true;
1527  }
1528  return false;
1529 }
1530 
1542 function elgg_get_ini_setting_in_bytes($setting) {
1543  // retrieve INI setting
1544  $val = ini_get($setting);
1545 
1546  // convert INI setting when shorthand notation is used
1547  $last = strtolower($val[strlen($val) - 1]);
1548  switch($last) {
1549  case 'g':
1550  $val *= 1024;
1551  // fallthrough intentional
1552  case 'm':
1553  $val *= 1024;
1554  // fallthrough intentional
1555  case 'k':
1556  $val *= 1024;
1557  }
1558 
1559  // return byte value
1560  return $val;
1561 }
1562 
1574  if (($string === '') || ($string === false) || ($string === null)) {
1575  return false;
1576  }
1577 
1578  return true;
1579 }
1580 
1595  foreach ($singulars as $singular) {
1596  $plural = $singular . 's';
1597 
1598  if (array_key_exists($singular, $options)) {
1599  if ($options[$singular] === ELGG_ENTITIES_ANY_VALUE) {
1600  $options[$plural] = $options[$singular];
1601  } else {
1602  // Test for array refs #2641
1603  if (!is_array($options[$singular])) {
1604  $options[$plural] = array($options[$singular]);
1605  } else {
1606  $options[$plural] = $options[$singular];
1607  }
1608  }
1609  }
1610 
1611  unset($options[$singular]);
1612  }
1613 
1614  return $options;
1615 }
1616 
1634 
1635  try {
1636  elgg_trigger_event('shutdown', 'system');
1637 
1638  $time = (float)(microtime(true) - $START_MICROTIME);
1639  $uri = _elgg_services()->request->server->get('REQUEST_URI', 'CLI');
1640  // demoted to NOTICE from DEBUG so javascript is not corrupted
1641  elgg_log("Page {$uri} generated in $time seconds", 'INFO');
1642  } catch (Exception $e) {
1643  $message = 'Error: ' . get_class($e) . ' thrown within the shutdown handler. ';
1644  $message .= "Message: '{$e->getMessage()}' in file {$e->getFile()} (line {$e->getLine()})";
1645  error_log($message);
1646  error_log("Exception trace stack: {$e->getTraceAsString()}");
1647  }
1648 
1649  // Prevent an APC session bug: https://bugs.php.net/bug.php?id=60657
1650  session_write_close();
1651 }
1652 
1665 function _elgg_js_page_handler($page) {
1666  return _elgg_cacheable_view_page_handler($page, 'js');
1667 }
1668 
1681 function _elgg_ajax_page_handler($page) {
1682  if (is_array($page) && sizeof($page)) {
1683  // throw away 'view' and form the view name
1684  unset($page[0]);
1685  $view = implode('/', $page);
1686 
1687  $allowed_views = elgg_get_config('allowed_ajax_views');
1688  if (!array_key_exists($view, $allowed_views)) {
1689  header('HTTP/1.1 403 Forbidden');
1690  exit;
1691  }
1692 
1693  // pull out GET parameters through filter
1694  $vars = array();
1695  foreach (_elgg_services()->request->query->keys() as $name) {
1696  $vars[$name] = get_input($name);
1697  }
1698 
1699  if (isset($vars['guid'])) {
1700  $vars['entity'] = get_entity($vars['guid']);
1701  }
1702 
1703  // Try to guess the mime-type
1704  switch ($page[1]) {
1705  case "js":
1706  header("Content-Type: text/javascript");
1707  break;
1708  case "css":
1709  header("Content-Type: text/css");
1710  break;
1711  }
1712 
1714  return true;
1715  }
1716  return false;
1717 }
1718 
1730 function _elgg_css_page_handler($page) {
1731  if (!isset($page[0])) {
1732  // default css
1733  $page[0] = 'elgg';
1734  }
1735 
1736  return _elgg_cacheable_view_page_handler($page, 'css');
1737 }
1738 
1751 
1752  switch ($type) {
1753  case 'js':
1754  $content_type = 'text/javascript';
1755  break;
1756 
1757  case 'css':
1758  $content_type = 'text/css';
1759  break;
1760 
1761  default:
1762  return false;
1763  break;
1764  }
1765 
1766  if ($page) {
1767  // the view file names can have multiple dots
1768  // eg: views/default/js/calendars/jquery.fullcalendar.min.php
1769  // translates to the url /js/<ts>/calendars/jquery.fullcalendar.min.js
1770  // and the view js/calendars/jquery.fullcalendar.min
1771  // we ignore the last two dots for the ts and the ext.
1772  // Additionally, the timestamp is optional.
1773  $page = implode('/', $page);
1774  $regex = '|(.+?)\.\w+$|';
1775  if (!preg_match($regex, $page, $matches)) {
1776  return false;
1777  }
1778  $view = "$type/{$matches[1]}";
1779  if (!elgg_view_exists($view)) {
1780  return false;
1781  }
1782  $return = elgg_view($view);
1783 
1784  header("Content-type: $content_type");
1785 
1786  // @todo should js be cached when simple cache turned off
1787  //header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', strtotime("+10 days")), true);
1788  //header("Pragma: public");
1789  //header("Cache-Control: public");
1790  //header("Content-Length: " . strlen($return));
1791 
1792  echo $return;
1793  return true;
1794  }
1795  return false;
1796 }
1797 
1810  $order_by = strtolower($order_by);
1811 
1812  if (strpos($order_by, ' asc') !== false) {
1813  $return = str_replace(' asc', ' desc', $order_by);
1814  } elseif (strpos($order_by, ' desc') !== false) {
1815  $return = str_replace(' desc', ' asc', $order_by);
1816  } else {
1817  // no order specified, so default to desc since mysql defaults to asc
1818  $return = $order_by . ' desc';
1819  }
1820 
1821  return $return;
1822 }
1823 
1836  // our db functions return the number of rows affected...
1837  return $object->enable() ? true : false;
1838 }
1839 
1850  // our db functions return the number of rows affected...
1851  return $object->disable() ? true : false;
1852 }
1853 
1864  // our db functions return the number of rows affected...
1865  return $object->delete() ? true : false;
1866 }
1867 
1878  if (!$options || !is_array($options)) {
1879  return false;
1880  }
1881 
1882  // at least one of these is required.
1883  $required = array(
1884  // generic restraints
1885  'guid', 'guids'
1886  );
1887 
1888  switch ($type) {
1889  case 'metadata':
1890  $metadata_required = array(
1891  'metadata_owner_guid', 'metadata_owner_guids',
1892  'metadata_name', 'metadata_names',
1893  'metadata_value', 'metadata_values'
1894  );
1895 
1896  $required = array_merge($required, $metadata_required);
1897  break;
1898 
1899  case 'annotations':
1900  case 'annotation':
1901  $annotations_required = array(
1902  'annotation_owner_guid', 'annotation_owner_guids',
1903  'annotation_name', 'annotation_names',
1904  'annotation_value', 'annotation_values'
1905  );
1906 
1907  $required = array_merge($required, $annotations_required);
1908  break;
1909 
1910  default:
1911  return false;
1912  }
1913 
1914  foreach ($required as $key) {
1915  // check that it exists and is something.
1916  if (isset($options[$key]) && $options[$key]) {
1917  return true;
1918  }
1919  }
1920 
1921  return false;
1922 }
1923 
1931 
1932  elgg_load_css('elgg.walled_garden');
1933  elgg_load_js('elgg.walled_garden');
1934 
1935  $content = elgg_view('core/walled_garden/login');
1936 
1937  $params = array(
1938  'content' => $content,
1939  'class' => 'elgg-walledgarden-double',
1940  'id' => 'elgg-walledgarden-login',
1941  );
1942  $body = elgg_view_layout('walled_garden', $params);
1943  echo elgg_view_page('', $body, 'walled_garden');
1944 
1945  return true;
1946 }
1947 
1948 
1957  $view = $page[0];
1958  $params = array(
1959  'content' => elgg_view("core/walled_garden/$view"),
1960  'class' => 'elgg-walledgarden-single hidden',
1961  'id' => str_replace('_', '-', "elgg-walledgarden-$view"),
1962  );
1963  echo elgg_view_layout('walled_garden', $params);
1964  return true;
1965 }
1966 
1981  global $CONFIG;
1982 
1983  elgg_register_css('elgg.walled_garden', elgg_get_simplecache_url('css', 'walled_garden'));
1984  elgg_register_js('elgg.walled_garden', elgg_get_simplecache_url('js', 'walled_garden'));
1985 
1986  elgg_register_page_handler('walled_garden', '_elgg_walled_garden_ajax_handler');
1987 
1988  // check for external page view
1989  if (isset($CONFIG->site) && $CONFIG->site instanceof ElggSite) {
1990  $CONFIG->site->checkWalledGarden();
1991  }
1992 }
1993 
2004  if (isset($accesses[ACCESS_PUBLIC])) {
2005  unset($accesses[ACCESS_PUBLIC]);
2006  }
2007  return $accesses;
2008 }
2009 
2023 function _elgg_engine_boot() {
2024  // Register the error handlers
2025  set_error_handler('_elgg_php_error_handler');
2026  set_exception_handler('_elgg_php_exception_handler');
2027 
2028  _elgg_services()->db->setupConnections();
2029 
2030  _elgg_services()->db->assertInstalled();
2031 
2033 
2035 
2037 
2039 
2040  _elgg_load_cache();
2041 
2043 }
2044 
2054 function _elgg_init() {
2055  global $CONFIG;
2056 
2057  elgg_register_action('comment/save');
2058  elgg_register_action('comment/delete');
2059 
2060  elgg_register_page_handler('js', '_elgg_js_page_handler');
2061  elgg_register_page_handler('css', '_elgg_css_page_handler');
2062  elgg_register_page_handler('ajax', '_elgg_ajax_page_handler');
2063 
2064  elgg_register_js('elgg.autocomplete', 'js/lib/ui.autocomplete.js');
2065  elgg_register_js('jquery.ui.autocomplete.html', 'vendors/jquery/jquery.ui.autocomplete.html.js');
2066 
2067  elgg_register_external_view('js/elgg/UserPicker.js', true);
2068 
2069  elgg_register_js('elgg.friendspicker', 'js/lib/ui.friends_picker.js');
2070  elgg_register_js('elgg.avatar_cropper', 'js/lib/ui.avatar_cropper.js');
2071  elgg_register_js('jquery.imgareaselect', 'vendors/jquery/jquery.imgareaselect/scripts/jquery.imgareaselect.min.js');
2072  elgg_register_js('elgg.ui.river', 'js/lib/ui.river.js');
2073 
2074  elgg_register_css('jquery.imgareaselect', 'vendors/jquery/jquery.imgareaselect/css/imgareaselect-deprecated.css');
2075 
2076  // Trigger the shutdown:system event upon PHP shutdown.
2077  register_shutdown_function('_elgg_shutdown_hook');
2078 
2079  // Sets a blacklist of words in the current language.
2080  // This is a comma separated list in word:blacklist.
2081  // @todo possibly deprecate
2082  $CONFIG->wordblacklist = array();
2083  $list = explode(',', elgg_echo('word:blacklist'));
2084  if ($list) {
2085  foreach ($list as $l) {
2086  $CONFIG->wordblacklist[] = trim($l);
2087  }
2088  }
2089 }
2090 
2103 function _elgg_api_test($hook, $type, $value, $params) {
2104  global $CONFIG;
2105  $value[] = $CONFIG->path . 'engine/tests/ElggTravisInstallTest.php';
2106  $value[] = $CONFIG->path . 'engine/tests/ElggCoreHelpersTest.php';
2107  $value[] = $CONFIG->path . 'engine/tests/ElggCoreRegressionBugsTest.php';
2108  $value[] = $CONFIG->path . 'engine/tests/ElggBatchTest.php';
2109  return $value;
2110 }
2111 
2120 define('ACCESS_DEFAULT', -1);
2121 define('ACCESS_PRIVATE', 0);
2122 define('ACCESS_LOGGED_IN', 1);
2123 define('ACCESS_PUBLIC', 2);
2124 define('ACCESS_FRIENDS', -2);
2134 define('ELGG_ENTITIES_ANY_VALUE', null);
2135 
2143 define('ELGG_ENTITIES_NO_VALUE', 0);
2144 
2152 define('REFERRER', -1);
2153 
2162 define('REFERER', -1);
2163 
2164 elgg_register_event_handler('init', 'system', '_elgg_init');
2165 elgg_register_event_handler('boot', 'system', '_elgg_engine_boot', 1);
2166 elgg_register_plugin_hook_handler('unit_test', 'system', '_elgg_api_test');
2167 
2168 elgg_register_event_handler('init', 'system', '_elgg_walled_garden_init', 1000);
_elgg_load_autoload_cache()
Load cached data into the autoload system.
Definition: autoloader.php:62
$messages
Definition: admin.php:27
elgg_http_add_url_query_elements($url, array $elements)
Sets elements in a URL&#39;s query string.
Definition: elgglib.php:1325
elgg_get_config($name, $site_guid=0)
Get an Elgg configuration value.
$view
Definition: crop.php:68
elgg_get_loaded_css()
Get the loaded CSS URLs.
Definition: elgglib.php:266
if(!$owner||!($owner instanceof ElggUser)||!$owner->canEdit()) $error
Definition: upload.php:14
elgg_unregister_plugin_hook_handler($hook, $entity_type, $callback)
Unregister a callback as a plugin hook.
Definition: elgglib.php:867
get_input($variable, $default=null, $filter_result=true)
Get some input from variables passed submitted through GET or POST.
Definition: input.php:27
elgg_add_action_tokens_to_url($url, $html_encode=false)
Adds action tokens to URL.
Definition: elgglib.php:1277
_elgg_css_page_handler($page)
Serve CSS.
Definition: elgglib.php:1730
_elgg_is_valid_options_for_batch_operation($options, $type)
Checks if there are some constraints on the options array for potentially dangerous operations...
Definition: elgglib.php:1877
elgg_register_external_file($type, $name, $url, $location, $priority=500)
Core registration function for external files.
Definition: elgglib.php:282
elgg_is_admin_logged_in()
Returns whether or not the viewer is currently logged in and an admin user.
Definition: sessions.php:65
elgg_normalize_url($url)
Definition: output.php:290
$current_page
Definition: pagination.php:49
if($guid==elgg_get_logged_in_user_guid()) $name
Definition: delete.php:21
elgg_define_js($name, $config)
Defines a JS lib as an AMD module.
Definition: elgglib.php:153
current_page_url()
Returns the current page&#39;s complete URL.
Definition: input.php:106
elgg_sort_3d_array_by_value(&$array, $element, $sort_order=SORT_ASC, $sort_type=SORT_LOCALE_STRING)
Sorts a 3d array by specific element.
Definition: elgglib.php:1496
$e
Definition: metadata.php:12
$object
Definition: upgrade.php:12
$extensions
elgg_http_url_is_identical($url1, $url2, $ignore_params=array('offset', 'limit'))
Test if two URLs are functionally identical.
Definition: elgglib.php:1369
elgg_unregister_external_file($type, $name)
Unregister an external file.
Definition: elgglib.php:341
sanitise_filepath($path, $append_slash=true)
Sanitise file paths ensuring that they begin and end with slashes etc.
Definition: elgglib.php:484
if(is_callable('mb_internal_encoding')) elgg_parse_str($str)
Parses a string using mb_parse_str() if available.
Definition: mb_wrapper.php:21
$session
Definition: login.php:9
$value
Definition: longtext.php:29
elgg_register_external_view($view, $cacheable=false)
Registers a view as being available externally (i.e.
Definition: views.php:231
elgg_view_exists($view, $viewtype= '', $recurse=true)
Returns whether the specified view exists.
Definition: views.php:318
elgg_register_css($name, $url, $priority=null)
Register a CSS file for inclusion in the HTML head.
Definition: elgglib.php:229
elgg_load_library($name)
Load a php library.
Definition: elgglib.php:38
$return
Definition: opendd.php:15
$default
Definition: checkbox.php:36
$src
Definition: iframe.php:18
exit
Definition: reorder.php:12
elgg parse_url
Parse a URL into its parts.
Definition: elgglib.js:432
elgg_extract($key, array $array, $default=null, $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:1464
_elgg_load_site_config()
Loads configuration related to this site.
elgg_register_plugin_hook_handler($hook, $type, $callback, $priority=500)
Register a callback as a plugin hook handler.
Definition: elgglib.php:853
$timestamp
Definition: date.php:35
_elgg_sql_reverse_order_by_clause($order_by)
Reverses the ordering in an ORDER BY clause.
Definition: elgglib.php:1809
_elgg_ajax_page_handler($page)
Serve individual views for Ajax.
Definition: elgglib.php:1681
_elgg_load_translations()
private
Definition: languages.php:148
elgg_trigger_before_event($event, $object_type, $object=null)
Trigger a "Before event" indicating a process is about to begin.
Definition: elgglib.php:741
$url
Definition: exceptions.php:24
_elgg_shutdown_hook()
Emits a shutdown:system event upon PHP shutdown, but before database connections are dropped...
Definition: elgglib.php:1632
_elgg_php_exception_handler($exception)
Intercepts, logs, and displays uncaught exceptions.
Definition: elgglib.php:946
register_error($error)
Display an error on next page load.
Definition: elgglib.php:605
$string
elgg_get_loaded_external_files($type, $location)
Get external resource descriptors.
Definition: elgglib.php:398
$params
Definition: login.php:72
is_not_null($string)
Returns true is string is not empty, false, or null.
Definition: elgglib.php:1573
$options
Definition: index.php:14
elgg_batch_delete_callback($object)
Delete objects with a delete() method.
Definition: elgglib.php:1863
_elgg_js_page_handler($page)
Serve javascript pages.
Definition: elgglib.php:1665
$exception
_elgg_bootstrap_externals_data_structure($type)
Bootstraps the externals data structure in $CONFIG.
Definition: elgglib.php:420
elgg_unregister_css($name)
Unregister a CSS file.
Definition: elgglib.php:241
elgg ElggPriorityList
Priority lists allow you to create an indexed list that can be iterated through in a specific order...
elgg_require_js($name)
Request that Elgg load an AMD module onto the page.
Definition: elgglib.php:202
elgg_get_file_list($directory, $exceptions=array(), $list=array(), $extensions=null)
Returns a list of files in $directory.
Definition: elgglib.php:452
elgg_get_ini_setting_in_bytes($setting)
Returns a PHP INI setting in bytes.
Definition: elgglib.php:1542
const OPTION_DEPRECATION_MESSAGE
_elgg_api_test($hook, $type, $value, $params)
Adds unit tests for the general API.
Definition: elgglib.php:2103
elgg_load_css($name)
Load a CSS file for this page.
Definition: elgglib.php:256
elgg_register_js($name, $url, $location= 'head', $priority=null)
Register a JavaScript file for inclusion.
Definition: elgglib.php:128
generate_action_token($timestamp)
Generate an action token.
Definition: actions.php:156
elgg_set_viewtype($viewtype="")
Manually set the viewtype.
Definition: views.php:70
elgg_echo($message_key, $args=array(), $language="")
Given a message key, returns an appropriately translated full-text string.
Definition: languages.php:21
elgg_get_simplecache_url($type, $view)
Definition: cache.php:155
elgg_http_remove_url_query_element($url, $element)
Removes an element from a URL&#39;s query string.
Definition: elgglib.php:1311
_elgg_load_cache()
Loads the system cache during engine boot.
Definition: cache.php:301
$key
Definition: summary.php:34
elgg_trigger_deprecated_event($event, $object_type, $object=null, $message, $version)
Trigger an event normally, but send a notice about deprecated use if any handlers are registered...
Definition: elgglib.php:780
const REFERER
Definition: elgglib.php:2162
_elgg_services()
Definition: autoloader.php:14
$item
Definition: item.php:12
elgg_load_external_file($type, $name)
Load an external resource for use on this page.
Definition: elgglib.php:366
_elgg_engine_boot()
Boots the engine.
Definition: elgglib.php:2023
_elgg_walled_garden_index()
Intercepts the index page when Walled Garden mode is enabled.
Definition: elgglib.php:1930
global $CONFIG
_elgg_cacheable_view_page_handler($page, $type)
Serves a JS or CSS view with headers for caching.
Definition: elgglib.php:1750
ini_get_bool($ini_get_arg)
Return the state of a php.ini setting as a bool.
Definition: elgglib.php:1522
elgg_dump($value, $to_screen=true)
Logs or displays $value.
Definition: elgglib.php:1114
const ELGG_ENTITIES_ANY_VALUE
Definition: elgglib.php:2134
elgg echo
Translates a string.
Definition: languages.js:43
elgg_trigger_plugin_hook($hook, $type, $params=null, $returnvalue=null)
Trigger a Plugin Hook and run all handler callbacks registered to that hook:type. ...
Definition: elgglib.php:925
elgg_register_page_handler($identifier, $function)
Registers a page handler for a particular identifier.
Definition: pagehandler.php:34
_elgg_walled_garden_ajax_handler($page)
Serve walled garden sections.
Definition: elgglib.php:1956
global $START_MICROTIME
The time with microseconds when the Elgg engine was started.
Definition: start.php:24
elgg_deprecated_notice($msg, $dep_version, $backtrace_level=1)
Sends a notice about deprecated use of a function, view, etc.
Definition: elgglib.php:1171
elgg global
Pointer to the global context.
Definition: elgglib.js:12
$type
Definition: add.php:8
elgg_register_library($name, $location)
Register a php library.
Definition: elgglib.php:19
elgg_view($view, $vars=array(), $bypass=false, $ignored=false, $viewtype= '')
Return a parsed view.
Definition: views.php:354
system_messages($message=null, $register="success", $count=false)
Queues a message to be displayed.
Definition: elgglib.php:532
elgg_view_layout($layout_name, $vars=array())
Displays a layout with optional parameters.
Definition: views.php:617
$items
Definition: list.php:11
elgg_register_event_handler($event, $object_type, $callback, $priority=500)
Register a callback as an Elgg event handler.
Definition: elgglib.php:669
elgg_log($message, $level= 'NOTICE')
Display or log a message.
Definition: elgglib.php:1083
_elgg_walled_garden_remove_public_access($hook, $type, $accesses)
Remove public access for walled gardens.
Definition: elgglib.php:2003
const ACCESS_PUBLIC
Definition: elgglib.php:2123
$count
Definition: tools.php:19
forward($location="", $reason= 'system')
Forward to $location.
Definition: elgglib.php:79
elgg_get_version($human_readable=false)
Get the current Elgg version information.
Definition: elgglib.php:1126
elgg_load_js($name)
Load a JavaScript resource on this page.
Definition: elgglib.php:190
elgg_http_build_url(array $parts, $html_encode=true)
Builds a URL from the a parts array like one returned by parse_url().
Definition: elgglib.php:1242
$content
Set robots.txt action.
Definition: set_robots.php:6
$sort
Definition: plugins.php:19
elgg_batch_disable_callback($object)
Disable objects with a disable() method.
Definition: elgglib.php:1849
elgg_format_url($url)
Handles formatting of ampersands in urls.
Definition: output.php:105
elgg_trigger_after_event($event, $object_type, $object=null)
Trigger an "After event" indicating a process has finished.
Definition: elgglib.php:760
_elgg_init()
Elgg&#39;s main init.
Definition: elgglib.php:2054
$filename
Definition: crop.php:23
elgg_view_page($title, $body, $page_shell= 'default', $vars=array())
Assembles and outputs a full page.
Definition: views.php:437
clearfix elgg elgg elgg elgg page header
Definition: admin.php:127
elgg_register_action($action, $filename="", $access= 'logged_in')
Registers an action.
Definition: actions.php:85
_elgg_session_boot()
Initializes the session and checks for the remember me cookie.
Definition: sessions.php:393
_elgg_normalize_plural_options_array($options, $singulars)
Normalise the singular keys in an options array to plural keys.
Definition: elgglib.php:1594
elgg_trigger_event($event, $object_type, $object=null)
Trigger an Elgg Event and attempt to run all handler callbacks registered to that event...
Definition: elgglib.php:720
_elgg_walled_garden_init()
Checks the status of the Walled Garden and forwards to a login page if required.
Definition: elgglib.php:1980
_elgg_php_error_handler($errno, $errmsg, $filename, $linenum, $vars)
Intercepts catchable PHP errors.
Definition: elgglib.php:1033
$version
Definition: version.php:14
_elgg_load_application_config()
Loads configuration related to Elgg as an application.
const OPTION_DEPRECATION_VERSION
$path
Definition: invalid.php:17
system_message($message)
Display a system message on next page load.
Definition: elgglib.php:592
A Site entity.
Definition: ElggSite.php:28
elgg_get_loaded_js($location= 'head')
Get the JavaScript URLs that are loaded.
Definition: elgglib.php:215
elgg_batch_enable_callback($object)
Enable objects with an enable() method.
Definition: elgglib.php:1835
elgg_unregister_event_handler($event, $object_type, $callback)
Unregisters a callback for an event.
Definition: elgglib.php:683
$priority
elgg_unregister_js($name)
Unregister a JavaScript file.
Definition: elgglib.php:175
count_messages($register="")
Counts the number of messages, either globally or in a particular register.
Definition: elgglib.php:579
if($composer===null) if(!isset($composer->version)) $release
Definition: version.php:30
get_entity($guid)
Loads and returns an entity object from a guid.
Definition: entities.php:604
if(file_exists($welcome)) $vars
Definition: upgrade.php:93