00001 <?php
00002
00003
00015 define('SAVED_NEW', 1);
00016
00020 define('SAVED_UPDATED', 2);
00021
00025 define('SAVED_DELETED', 3);
00026
00035 function drupal_set_content($region = NULL, $data = NULL) {
00036 static $content = array();
00037
00038 if (!is_null($region) && !is_null($data)) {
00039 $content[$region][] = $data;
00040 }
00041 return $content;
00042 }
00043
00053 function drupal_get_content($region = NULL, $delimiter = ' ') {
00054 $content = drupal_set_content();
00055 if (isset($region)) {
00056 if (isset($content[$region]) && is_array($content[$region])) {
00057 return implode($delimiter, $content[$region]);
00058 }
00059 }
00060 else {
00061 foreach (array_keys($content) as $region) {
00062 if (is_array($content[$region])) {
00063 $content[$region] = implode($delimiter, $content[$region]);
00064 }
00065 }
00066 return $content;
00067 }
00068 }
00069
00077 function drupal_set_breadcrumb($breadcrumb = NULL) {
00078 static $stored_breadcrumb;
00079
00080 if (!is_null($breadcrumb)) {
00081 $stored_breadcrumb = $breadcrumb;
00082 }
00083 return $stored_breadcrumb;
00084 }
00085
00089 function drupal_get_breadcrumb() {
00090 $breadcrumb = drupal_set_breadcrumb();
00091
00092 if (is_null($breadcrumb)) {
00093 $breadcrumb = menu_get_active_breadcrumb();
00094 }
00095
00096 return $breadcrumb;
00097 }
00098
00104 function drupal_set_html_head($data = NULL) {
00105 static $stored_head = '';
00106
00107 if (!is_null($data)) {
00108 $stored_head .= $data . "\n";
00109 }
00110 return $stored_head;
00111 }
00112
00116 function drupal_get_html_head() {
00117 $output = "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n";
00118 return $output . drupal_set_html_head();
00119 }
00120
00124 function drupal_clear_path_cache() {
00125 drupal_lookup_path('wipe');
00126 }
00127
00134 function drupal_set_header($header = NULL) {
00135
00136
00137
00138 static $stored_headers = array();
00139
00140 if (strlen($header)) {
00141 header($header);
00142 $stored_headers[] = $header;
00143 }
00144 return implode("\n", $stored_headers);
00145 }
00146
00150 function drupal_get_headers() {
00151 return drupal_set_header();
00152 }
00153
00162 function drupal_add_feed(a> = NULL, $title = '') {
00163 static $stored_feed_links = array();
00164
00165 if (!is_null(a>) && !isset($stored_feed_links[a>])) {
00166 $stored_feed_links[$url] = theme('feed_icon', $url, $title);
00167
00168 drupal_add_link(array('rel' => 'alternate',
00169 'type' => 'application/rss+xml',
00170 'title' => $title,
00171 'href' => $url));
00172 }
00173 return $stored_feed_links;
00174 }
00175
00182 function drupal_get_feeds($delimiter = "\n") {
00183 $feeds = drupal_add_feed();
00184 return implode($feeds, $delimiter);
00185 }
00186
00206 function drupal_query_string_encode($query, $exclude = array(), $parent = '') {
00207 $params = array();
00208
00209 foreach ($query as $key => $value) {
00210 $key = drupal_urlencode($key);
00211 if ($parent) {
00212 $key = $parent . '[' . $key . ']';
00213 }
00214
00215 if (in_array($key, $exclude)) {
00216 continue;
00217 }
00218
00219 if (is_array($value)) {
00220 $params[] = drupal_query_string_encode($value, $exclude, $key);
00221 }
00222 else {
00223 $params[] = $key . '=' . drupal_urlencode($value);
00224 }
00225 }
00226
00227 return implode('&', $params);
00228 }
00229
00240 function drupal_get_destination() {
00241 if (isset($_REQUEST['destination'])) {
00242 return 'destination=' . urlencode($_REQUEST['destination']);
00243 }
00244 else {
00245
00246 $path = isset($_GET['q']) ? $_GET['q'] : '';
00247 $query = drupal_query_string_encode($_GET, array('q'));
00248 if ($query != '') {
00249 $path .= '?' . $query;
00250 }
00251 return 'destination=' . urlencode($path);
00252 }
00253 }
00254
00296 function drupal_goto($path = '', $query = NULL, $fragment = NULL, $http_response_code = 302) {
00297
00298 if (isset($_REQUEST['destination'])) {
00299 extract(parse_url(span>])));
00300 }
00301 else if (isset($_REQUEST['edit']['destination'])) {
00302 extract(parse_url(urldecode($_REQUEST['edit']['destination'])));
00303 }
00304
00305 a> = url($path, array('query' => $query, 'fragment' => $fragment, 'absolute' => TRUE));
00306
00307 a> = str_replace(array("\n", "\r"), '', a>);
00308
00309
00310
00311 if (!defined('MAINTENANCE_MODE') || MAINTENANCE_MODE != 'update') {
00312 module_invoke_all('exit', a>);
00313 }
00314
00315
00316
00317 session_write_close();
00318
00319 header('Location: ' . a>, TRUE, $http_response_code);
00320
00321
00322
00323
00324 exit();
00325 }
00326
00330 function drupal_site_offline() {
00331 drupal_maintenance_theme();
00332 drupal_set_header('HTTP/1.1 503 Service unavailable');
00333 drupal_set_title(t('Site off-line'));
00334 print theme('maintenance_page', filter_xss_admin(variable_get('site_offline_message',
00335 t('@site is currently under maintenance. We should be back shortly. Thank you for your patience.', array('@site' => variable_get('site_name', 'Drupal'))))));
00336 }
00337
00341 function drupal_not_found() {
00342 drupal_set_header('HTTP/1.1 404 Not Found');
00343
00344 watchdog('page not found', check_plain($_GET['q']), NULL, WATCHDOG_WARNING);
00345
00346
00347 if (!isset($_REQUEST['destination'])) {
00348 $_REQUEST['destination'] = $_GET['q'];
00349 }
00350
00351 $path = drupal_get_normal_path(variable_get('site_404', ''));
00352 if ($path && $path != $_GET['q']) {
00353
00354
00355 menu_set_active_item($path);
00356 $return = menu_execute_active_handler($path);
00357 }
00358
00359 if (empty($return) || $return == MENU_NOT_FOUND || $return == MENU_ACCESS_DENIED) {
00360 drupal_set_title(t('Page not found'));
00361 $return = t('The requested page could not be found.');
00362 }
00363
00364
00365 print theme('page', $return, FALSE);
00366 }
00367
00371 function drupal_access_denied() {
00372 drupal_set_header('HTTP/1.1 403 Forbidden');
00373 watchdog('access denied', check_plain($_GET['q']), NULL, WATCHDOG_WARNING);
00374
00375
00376 if (!isset($_REQUEST['destination'])) {
00377 $_REQUEST['destination'] = $_GET['q'];
00378 }
00379
00380 $path = drupal_get_normal_path(variable_get('site_403', ''));
00381 if ($path && $path != $_GET['q']) {
00382
00383
00384 menu_set_active_item($path);
00385 $return = menu_execute_active_handler($path);
00386 }
00387
00388 if (empty($return) || $return == MENU_NOT_FOUND || $return == MENU_ACCESS_DENIED) {
00389 drupal_set_title(t('Access denied'));
00390 $return = t('You are not authorized to access this page.');
00391 }
00392 print theme('page', $return);
00393 }
00394
00416 function drupal_http_request(a>, $headers = array(), $method = 'GET', $data = NULL, $retry = 3) {
00417 static $self_test = FALSE;
00418 $result = new stdClass();
00419
00420
00421
00422
00423 if (!$self_test && variable_get('drupal_http_request_fails', FALSE)) {
00424 $self_test = TRUE;
00425 $works = module_invoke('system', 'check_http_request');
00426 $self_test = FALSE;
00427 if (!$works) {
00428
00429
00430 $result->error = t("The server can't issue HTTP requests");
00431 return $result;
00432 }
00433 }
00434
00435
00436 $uri = parse_url(a>);
00437
00438 switch ($uri['scheme']) {
00439 case 'http':
00440 $port = isset($uri['port']) ? $uri['port'] : 80;
00441 $host = $uri['host'] . ($port != 80 ? ':' . $port : '');
00442 $fp = @fsockopen($uri['host'], $port, $errno, $errstr, 15);
00443 break;
00444 case 'https':
00445
00446 $port = isset($uri['port']) ? $uri['port'] : 443;
00447 $host = $uri['host'] . ($port != 443 ? ':' . $port : '');
00448 $fp = @fsockopen('ssl://' . $uri['host'], $port, $errno, $errstr, 20);
00449 break;
00450 default:
00451 $result->error = 'invalid schema ' . $uri['scheme'];
00452 return $result;
00453 }
00454
00455
00456 if (!$fp) {
00457
00458
00459 $result->code = -$errno;
00460 $result->error = trim($errstr);
00461 return $result;
00462 }
00463
00464
00465 $path = isset($uri['path']) ? $uri['path'] : '/';
00466 if (isset($uri['query'])) {
00467 $path .= '?' . $uri['query'];
00468 }
00469
00470
00471 $defaults = array(
00472
00473
00474
00475 'Host' => "Host: $host",
00476 'User-Agent' => 'User-Agent: Drupal (+http://drupal.org/)',
00477 'Content-Length' => 'Content-Length: ' . strlen($data)
00478 );
00479
00480
00481 if (isset($uri['user'])) {
00482 $defaults['Authorization'] = 'Authorization: Basic ' . base64_encode($uri['user'] . (!empty($uri['pass']) ? ":" . $uri['pass'] : ''));
00483 }
00484
00485 foreach ($headers as $header => $value) {
00486 $defaults[$header] = $header . ': ' . $value;
00487 }
00488
00489 $request = $method . ' ' . $path . " HTTP/1.0\r\n";
00490 $request .= implode("\r\n", $defaults);
00491 $request .= "\r\n\r\n";
00492 if ($data) {
00493 $request .= $data . "\r\n";
00494 }
00495 $result->request = $request;
00496
00497 fwrite($fp, $request);
00498
00499
00500 $response = '';
00501 while (!feof($fp) && $chunk = fread($fp, 1024)) {
00502 $response .= $chunk;
00503 }
00504 fclose($fp);
00505
00506
00507 list($split, $result->data) = explode("\r\n\r\n", $response, 2);
00508 $split = preg_split("/\r\n|\n|\r/", $split);
00509
00510 list($protocol, $code, $text) = explode(' ', trim(array_shift($split)), 3);
00511 $result->headers = array();
00512
00513
00514 while ($line = trim(array_shift($split))) {
00515 list($header, $value) = explode(':', $line, 2);
00516 if (isset($result->headers[$header]) && $header == 'Set-Cookie') {
00517
00518
00519 $result->headers[$header] .= ',' . trim($value);
00520 }
00521 else {
00522 $result->headers[$header] = trim($value);
00523 }
00524 }
00525
00526 $responses = array(
00527 100 => 'Continue', 101 => 'Switching Protocols',
00528 200 => 'OK', 201 => 'Created', 202 => 'Accepted', 203 => 'Non-Authoritative Information', 204 => 'No Content', 205 => 'Reset Content', 206 => 'Partial Content',
00529 300 => 'Multiple Choices', 301 => 'Moved Permanently', 302 => 'Found', 303 => 'See Other', 304 => 'Not Modified', 305 => 'Use Proxy', 307 => 'Temporary Redirect',
00530 400 => 'Bad Request', 401 => 'Unauthorized', 402 => 'Payment Required', 403 => 'Forbidden', 404 => 'Not Found', 405 => 'Method Not Allowed', 406 => 'Not Acceptable', 407 => 'Proxy Authentication Required', 408 => 'Request Time-out', 409 => 'Conflict', 410 => 'Gone', 411 => 'Length Required', 412 => 'Precondition Failed', 413 => 'Request Entity Too Large', 414 => 'Request-URI Too Large', 415 => 'Unsupported Media Type', 416 => 'Requested range not satisfiable', 417 => 'Expectation Failed',
00531 500 => 'Internal Server Error', 501 => 'Not Implemented', 502 => 'Bad Gateway', 503 => 'Service Unavailable', 504 => 'Gateway Time-out', 505 => 'HTTP Version not supported'
00532 );
00533
00534
00535 if (!isset($responses[$code])) {
00536 $code = floor($code / 100) * 100;
00537 }
00538
00539 switch ($code) {
00540 case 200:
00541 case 304:
00542 break;
00543 case 301:
00544 case 302:
00545 case 307:
00546 $location = $result->headers['Location'];
00547
00548 if ($retry) {
00549 $result = drupal_http_request($result->headers['Location'], $headers, $method, $data, --$retry);
00550 $result->redirect_code = $result->code;
00551 }
00552 $result->redirect_url = $location;
00553
00554 break;
00555 default:
00556 $result->error = $text;
00557 }
00558
00559 $result->code = $code;
00560 return $result;
00561 }
00573 function drupal_error_handler($errno, $message, $filename, $line, $context) {
00574
00575
00576 if (error_reporting() == 0) {
00577 return;
00578 }
00579
00580 if ($errno & (E_ALL)) {
00581 $types = array(1 => 'error', 2 => 'warning', 4 => 'parse error', 8 => 'notice', 16 => 'core error', 32 => 'core warning', 64 => 'compile error', 128 => 'compile warning', 256 => 'user error', 512 => 'user warning', 1024 => 'user notice', 2048 => 'strict warning', 4096 => 'recoverable fatal error');
00582
00583
00584
00585 if (isset($context[DB_ERROR])) {
00586 $backtrace = array_reverse(debug_backtrace());
00587
00588
00589 $query_functions = array('db_query', 'pager_query', 'db_query_range', 'db_query_temporary', 'update_sql');
00590
00591
00592
00593 foreach ($backtrace as $index => $function) {
00594 if (in_array($function['function'], $query_functions)) {
00595 $line = $backtrace[$index]['line'];
00596 $filename = $backtrace[$index]['file'];
00597 break;
00598 }
00599 }
00600 }
00601
00602 $entry = $types[$errno] . ': ' . $message . ' in ' . $filename . ' on line ' . $line . '.';
00603
00604
00605 if (variable_get('error_level', 1) == 1 || strstr($_SERVER['SCRIPT_NAME'], 'update.php')) {
00606 drupal_set_message($entry, 'error');
00607 }
00608
00609 watchdog('php', '%message in %file on line %line.', array('%error' => $types[$errno], '%message' => $message, '%file' => $filename, '%line' => $line), WATCHDOG_ERROR);
00610 }
00611 }
00612
00613 function _fix_gpc_magic(&$item) {
00614 if (is_array($item)) {
00615 array_walk($item, '_fix_gpc_magic');
00616 }
00617 else {
00618 $item = stripslashes($item);
00619 }
00620 }
00621
00629 function _fix_gpc_magic_files(&$item, $key) {
00630 if ($key != 'tmp_name') {
00631 if (is_array($item)) {
00632 array_walk($item, '_fix_gpc_magic_files');
00633 }
00634 else {
00635 $item = stripslashes($item);
00636 }
00637 }
00638 }
00639
00643 function fix_gpc_magic() {
00644 static $fixed = FALSE;
00645 if (!$fixed && ini_get('magic_quotes_gpc')) {
00646 array_walk($_GET, '_fix_gpc_magic');
00647 array_walk($_POST, '_fix_gpc_magic');
00648 array_walk($_COOKIE, '_fix_gpc_magic');
00649 array_walk($_REQUEST, '_fix_gpc_magic');
00650 array_walk($_FILES, '_fix_gpc_magic_files');
00651 $fixed = TRUE;
00652 }
00653 }
00654
00753 function t($string, $args = array(), $langcode = NULL) {
00754 global $language;
00755 static $custom_strings;
00756
00757 $langcode = isset($langcode) ? $langcode : $language->language;
00758
00759
00760
00761
00762
00763 if (!isset($custom_strings[$langcode])) {
00764 $custom_strings[$langcode] = variable_get('locale_custom_strings_' . $langcode, array());
00765 }
00766
00767 if (isset($custom_strings[$langcode][$string])) {
00768 $string = $custom_strings[$langcode][$string];
00769 }
00770
00771 elseif (function_exists('locale') && $langcode != 'en') {
00772 $string = locale($string, $langcode);
00773 }
00774 if (empty($args)) {
00775 return $string;
00776 }
00777 else {
00778
00779 foreach ($args as $key => $value) {
00780 switch ($key[0]) {
00781 case '@':
00782
00783 $args[$key] = check_plain($value);
00784 break;
00785
00786 case '%':
00787 default:
00788
00789 $args[$key] = theme('placeholder', $value);
00790 break;
00791
00792 case '!':
00793
00794 }
00795 }
00796 return strtr($string, $args);
00797 }
00798 }
00799
00816 function valid_email_address($mail) {
00817 $user = '[a-zA-Z0-9_\-\.\+\^!#\$%&*+\/\=\?\`\|\{\}~\']+';
00818 $domain = '(?:(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.?)+';
00819 $ipv4 = '[0-9]{1,3}(\.[0-9]{1,3}){3}';
00820 $ipv6 = '[0-9a-fA-F]{1,4}(\:[0-9a-fA-F]{1,4}){7}';
00821
00822 return preg_match("/^$user@($domain|(\[($ipv4|$ipv6)\]))$/", $mail);
00823 }
00824
00838 function valid_url(a>, $absolute = FALSE) {
00839 $allowed_characters = '[a-z0-9\/:_\-_\.\?\$,;~=#&%\+]';
00840 if ($absolute) {
00841 return preg_match("/^(http|https|ftp):\/\/" . $allowed_characters . "+$/i", a>);
00842 }
00843 else {
00844 return preg_match("/^" . $allowed_characters . "+$/i", a>);
00845 }
00846 }
00847
00858 function flood_register_event($name) {
00859 db_query("INSERT INTO {flood} (event, hostname, timestamp) VALUES ('%s', '%s', %d)", $name, ip_address(), time());
00860 }
00861
00875 function flood_is_allowed($name, $threshold) {
00876 $number = db_result(db_query("SELECT COUNT(*) FROM {flood} WHERE event = '%s' AND hostname = '%s' AND timestamp > %d", $name, ip_address(), time() - 3600));
00877 return ($number < $threshold ? TRUE : FALSE);
00878 }
00879
00880 function check_file($filename) {
00881 return is_uploaded_file($filename);
00882 }
00883
00887 function check_url($uri) {
00888 return filter_xss_bad_protocol($uri, FALSE);
00889 }
00890
00902 function format_rss_channel($title, $link, $description, $items, $langcode = NULL, $args = array()) {
00903 global $language;
00904 $langcode = $langcode ? $langcode : $language->language;
00905
00906 $output = "<channel>\n";
00907 $output .= ' <title>' . check_plain($title) . "</title>\n";
00908 $output .= ' <link>' . check_url($link) . "</link>\n";
00909
00910
00911
00912
00913 $output .= ' <description>' . check_plain(decode_entities(strip_tags($description))) . "</description>\n";
00914 $output .= ' <language>' . check_plain($langcode) . "</language>\n";
00915 $output .= format_xml_elements($args);
00916 $output .= $items;
00917 $output .= "</channel>\n";
00918
00919 return $output;
00920 }
00921
00927 function format_rss_item($title, $link, $description, $args = array()) {
00928 $output = "<item>\n";
00929 $output .= ' <title>' . check_plain($title) . "</title>\n";
00930 $output .= ' <link>' . check_url($link) . "</link>\n";
00931 $output .= ' <description>' . check_plain($description) . "</description>\n";
00932 $output .= format_xml_elements($args);
00933 $output .= "</item>\n";
00934
00935 return $output;
00936 }
00937
00952 function format_xml_elements($array) {
00953 $output = '';
00954 foreach ($array as $key => $value) {
00955 if (is_numeric($key)) {
00956 if ($value['key']) {
00957 $output .= ' <' . $value['key'];
00958 if (isset($value['attributes']) && is_array($value['attributes'])) {
00959 $output .= drupal_attributes($value['attributes']);
00960 }
00961
00962 if ($value['value'] != '') {
00963 $output .= '>' . (is_array($value['value']) ? format_xml_elements($value['value']) : check_plain($value['value'])) . '</' . $value['key'] . ">\n";
00964 }
00965 else {
00966 $output .= " />\n";
00967 }
00968 }
00969 }
00970 else {
00971 $output .= ' <' . $key . '>' . (is_array($value) ? format_xml_elements($value) : check_plain($value)) . "</$key>\n";
00972 }
00973 }
00974 return $output;
00975 }
00976
01023 function format_plural($count, $singular, $plural, $args = array(), $langcode = NULL) {
01024 $args['@count'] = $count;
01025 if ($count == 1) {
01026 return t($singular, $args, $langcode);
01027 }
01028
01029
01030 $index = (function_exists('locale_get_plural')) ? locale_get_plural($count, $langcode) : -1;
01031
01032 if ($index < 0) {
01033 return t($plural, $args, $langcode);
01034 }
01035 else {
01036 switch ($index) {
01037 case "0":
01038 return t($singular, $args, $langcode);
01039 case "1":
01040 return t($plural, $args, $langcode);
01041 default:
01042 unset($args['@count']);
01043 $args['@count[' . $index . ']'] = $count;
01044 return t(strtr($plural, array('@count' => '@count[' . $index . ']')), $args, $langcode);
01045 }
01046 }
01047 }
01048
01058 function parse_size($size) {
01059 $suffixes = array(
01060 '' => 1,
01061 'k' => 1024,
01062 'm' => 1048576,
01063 'g' => 1073741824,
01064 );
01065 if (preg_match('/([0-9]+)\s*(k|m|g)?(b?(ytes?)?)/i', $size, $match)) {
01066 return $match[1] * $suffixes[drupal_strtolower($match[2])];
01067 }
01068 }
01069
01081 function format_size($size, $langcode = NULL) {
01082 if ($size < 1024) {
01083 return format_plural($size, '1 byte', '@count bytes', array(), $langcode);
01084 }
01085 else {
01086 $size = round($size / 1024, 2);
01087 $suffix = t('KB', array(), $langcode);
01088 if ($size >= 1024) {
01089 $size = round($size / 1024, 2);
01090 $suffix = t('MB', array(), $langcode);
01091 }
01092 return t('@size @suffix', array('@size' => $size, '@suffix' => $suffix), $langcode);
01093 }
01094 }
01095
01109 function format_interval($timestamp, $granularity = 2, $langcode = NULL) {
01110 $units = array('1 year|@count years' => 31536000, '1 week|@count weeks' => 604800, '1 day|@count days' => 86400, '1 hour|@count hours' => 3600, '1 min|@count min' => 60, '1 sec|@count sec' => 1);
01111 $output = '';
01112 foreach ($units as $key => $value) {
01113 $key = explode('|', $key);
01114 if ($timestamp >= $value) {
01115 $output .= ($output ? ' ' : '') . format_plural(floor($timestamp / $value), $key[0], $key[1], array(), $langcode);
01116 $timestamp %= $value;
01117 $granularity--;
01118 }
01119
01120 if ($granularity == 0) {
01121 break;
01122 }
01123 }
01124 return $output ? $output : t('0 sec', array(), $langcode);
01125 }
01126
01151 function format_date($timestamp, $type = 'medium', $format = '', $timezone = NULL, $langcode = NULL) {
01152 if (!isset($timezone)) {
01153 global $user;
01154 if (variable_get('configurable_timezones', 1) && $user->uid && strlen($user->timezone)) {
01155 $timezone = $user->timezone;
01156 }
01157 else {
01158 $timezone = variable_get('date_default_timezone', 0);
01159 }
01160 }
01161
01162 $timestamp += $timezone;
01163
01164 switch ($type) {
01165 case 'small':
01166 $format = variable_get('date_format_short', 'm/d/Y - H:i');
01167 break;
01168 case 'large':
01169 $format = variable_get('date_format_long', 'l, F j, Y - H:i');
01170 break;
01171 case 'custom':
01172
01173 break;
01174 case 'medium':
01175 default:
01176 $format = variable_get('date_format_medium', 'D, m/d/Y - H:i');
01177 }
01178
01179 $max = strlen($format);
01180 $date = '';
01181 for ($i = 0; $i < $max; $i++) {
01182 $c = $format[$i];
01183 if (strpos('AaDlM', $c) !== FALSE) {
01184 $date .= t(gmdate($c, $timestamp), array(), $langcode);
01185 }
01186 else if ($c == 'F') {
01187
01188
01189
01190 $date .= trim(t('!long-month-name ' . gmdate($c, $timestamp), array('!long-month-name' => ''), $langcode));
01191 }
01192 else if (strpos('BdgGhHiIjLmnsStTUwWYyz', $c) !== FALSE) {
01193 $date .= gmdate($c, $timestamp);
01194 }
01195 else if ($c == 'r') {
01196 $date .= format_date($timestamp - $timezone, 'custom', 'D, d M Y H:i:s O', $timezone, $langcode);
01197 }
01198 else if ($c == 'O') {
01199 $date .= sprintf('%s%02d%02d', ($timezone < 0 ? '-' : '+'), abs($timezone / 3600), abs($timezone % 3600) / 60);
01200 }
01201 else if ($c == 'Z') {
01202 $date .= $timezone;
01203 }
01204 else if ($c == '\\') {
01205 $date .= $format[++$i];
01206 }
01207 else {
01208 $date .= $c;
01209 }
01210 }
01211
01212 return $date;
01213 }
01214
01257 function url($path = NULL, $options = array()) {
01258
01259 $options += array(
01260 'fragment' => '',
01261 'query' => '',
01262 'absolute' => FALSE,
01263 'alias' => FALSE,
01264 'prefix' => ''
01265 );
01266 if (!isset($options['external'])) {
01267
01268
01269
01270 $colonpos = strpos($path, ':');
01271 $options['external'] = ($colonpos !== FALSE && !preg_match('![/?#]!', substr($path, 0, $colonpos)) && filter_xss_bad_protocol($path, FALSE) == check_plain($path));
01272 }
01273
01274
01275 if (function_exists('language_url_rewrite')) {
01276 language_url_rewrite($path, $options);
01277 }
01278 if ($options['fragment']) {
01279 $options['fragment'] = '#' . $options['fragment'];
01280 }
01281 if (is_array($options['query'])) {
01282 $options['query'] = drupal_query_string_encode($options['query']);
01283 }
01284
01285 if ($options['external']) {
01286
01287 if (strpos($path, '#') !== FALSE) {
01288 list($path, $old_fragment) = explode('#', $path, 2);
01289 if (isset($old_fragment) && !$options['fragment']) {
01290 $options['fragment'] = '#' . $old_fragment;
01291 }
01292 }
01293
01294 if ($options['query']) {
01295 $path .= (strpos($path, '?') !== FALSE ? '&' : '?') . $options['query'];
01296 }
01297
01298 return $path . $options['fragment'];
01299 }
01300
01301 global $base_url;
01302 static $script;
01303 static $clean_url;
01304
01305 if (!isset($script)) {
01306
01307
01308
01309 $script = (strpos($_SERVER['SERVER_SOFTWARE'], 'Apache') === FALSE) ? 'index.php' : '';
01310 }
01311
01312
01313 if (!isset($clean_url)) {
01314 $clean_url = (bool)variable_get('clean_url', '0');
01315 }
01316
01317 if (!isset($options['base_url'])) {
01318
01319 $options['base_url'] = $base_url;
01320 }
01321
01322
01323 $original_path = $path;
01324
01325
01326 if ($path == '<front>') {
01327 $path = '';
01328 }
01329 elseif (!empty($path) && !$options['alias']) {
01330 $path = drupal_get_path_alias($path, isset($options['language']) ? $options['language']->language : '');
01331 }
01332
01333 if (function_exists('custom_url_rewrite_outbound')) {
01334
01335 custom_url_rewrite_outbound($path, $options, $original_path);
01336 }
01337
01338 $base = $options['absolute'] ? $options['base_url'] . '/' : base_path();
01339 $prefix = empty($path) ? rtrim($options['prefix'], '/') : $options['prefix'];
01340 $path = drupal_urlencode($prefix . $path);
01341
01342 if ($clean_url) {
01343
01344 if ($options['query']) {
01345 return $base . $path . '?' . $options['query'] . $options['fragment'];
01346 }
01347 else {
01348 return $base . $path . $options['fragment'];
01349 }
01350 }
01351 else {
01352
01353 $variables = array();
01354 if (!empty($path)) {
01355 $variables[] = 'q=' . $path;
01356 }
01357 if (!empty($options['query'])) {
01358 $variables[] = $options['query'];
01359 }
01360 if ($query = join('&', $variables)) {
01361 return $base . $script . '?' . $query . $options['fragment'];
01362 }
01363 else {
01364 return $base . $options['fragment'];
01365 }
01366 }
01367 }
01368
01377 function drupal_attributes($attributes = array()) {
01378 if (is_array($attributes)) {
01379 $t = '';
01380 foreach ($attributes as $key => $value) {
01381 $t .= " $key=" . '"' . check_plain($value) . '"';
01382 }
01383 return $t;
01384 }
01385 }
01386
01430 function l($text, $path, $options = array()) {
01431
01432 $options += array(
01433 'attributes' => array(),
01434 'html' => FALSE,
01435 );
01436
01437
01438 if ($path == $_GET['q'] || ($path == '<front>' && drupal_is_front_page())) {
01439 if (isset($options['attributes']['class'])) {
01440 $options['attributes']['class'] .= ' active';
01441 }
01442 else {
01443 $options['attributes']['class'] = 'active';
01444 }
01445 }
01446
01447
01448
01449 if (isset($options['attributes']['title']) && strpos($options['attributes']['title'], '<') !== FALSE) {
01450 $options['attributes']['title'] = strip_tags($options['attributes']['title']);
01451 }
01452
01453 return '<a href="' . check_url(url($path, $options)) . '"' . drupal_attributes($options['attributes']) . '>' . ($options['html'] ? $text : check_plain($text)) . '</a>';
01454 }
01455
01462 function drupal_page_footer() {
01463
01464 if (variable_get('cache', CACHE_DISABLED) != CACHE_DISABLED) {
01465 page_set_cache();
01466 }
01467
01468 module_invoke_all('exit');
01469
01470 registry_cache_hook_implementations(FALSE, TRUE);
01471 registry_cache_path_files();
01472 }
01473
01490 function drupal_map_assoc($array, $function = NULL) {
01491 if (!isset($function)) {
01492 $result = array();
01493 foreach ($array as $value) {
01494 $result[$value] = $value;
01495 }
01496 return $result;
01497 }
01498 elseif (function_exists($function)) {
01499 $result = array();
01500 foreach ($array as $value) {
01501 $result[$value] = $function($value);
01502 }
01503 return $result;
01504 }
01505 }
01506
01524 function drupal_eval($code) {
01525 global $theme_path, $theme_info, $conf;
01526
01527
01528 $old_theme_path = $theme_path;
01529
01530
01531
01532
01533 if (!isset($theme_info)) {
01534 $theme_path = drupal_get_path('theme', $conf['theme_default']);
01535 }
01536 else {
01537 $theme_path = dirname($theme_info->filename);
01538 }
01539
01540 ob_start();
01541 print eval('?>' . $code);
01542 $output = ob_get_contents();
01543 ob_end_clean();
01544
01545
01546 $theme_path = $old_theme_path;
01547
01548 return $output;
01549 }
01550
01562 function drupal_get_path($type, $name) {
01563 return dirname(drupal_get_filename($type, $name));
01564 }
01565
01570 function base_path() {
01571 return $GLOBALS['base_path'];
01572 }
01573
01577 function drupal_add_link($attributes) {
01578 drupal_set_html_head('<link' . drupal_attributes($attributes) . " />\n");
01579 }
01580
01632 function drupal_add_css($path = NULL, $type = 'module', $media = 'all', $preprocess = TRUE) {
01633 static $css = array();
01634 global $language;
01635
01636
01637
01638 if (isset($path)) {
01639
01640 if (!isset($css[$media])) {
01641 $css[$media] = array('module' => array(), 'theme' => array());
01642 }
01643 $css[$media][$type][$path] = $preprocess;
01644
01645
01646 if (defined('LANGUAGE_RTL') && $language->direction == LANGUAGE_RTL) {
01647 $rtl_path = str_replace('.css', '-rtl.css', $path);
01648 if (file_exists($rtl_path)) {
01649 $css[$media][$type][$rtl_path] = $preprocess;
01650 }
01651 }
01652 }
01653
01654 return $css;
01655 }
01656
01679 function drupal_get_css($css = NULL) {
01680 $output = '';
01681 if (!isset($css)) {
01682 $css = drupal_add_css();
01683 }
01684 $no_module_preprocess = '';
01685 $no_theme_preprocess = '';
01686
01687 $preprocess_css = (variable_get('preprocess_css', FALSE) && (!defined('MAINTENANCE_MODE') || MAINTENANCE_MODE != 'update'));
01688 $directory = file_directory_path();
01689 $is_writable = is_dir($directory) && is_writable($directory) && (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PUBLIC);
01690
01691
01692
01693
01694
01695 $query_string = '?' . substr(variable_get('css_js_query_string', '0'), 0, 1);
01696
01697 foreach ($css as $media => $types) {
01698
01699
01700 foreach ($types as $type => $files) {
01701 if ($type == 'module') {
01702
01703 $theme_styles = array();
01704 foreach (array_keys($css[$media]['theme']) as $theme_style) {
01705 $theme_styles[] = basename($theme_style);
01706 }
01707 }
01708 foreach ($types[$type] as $file => $preprocess) {
01709
01710
01711 if ($type == 'module' && in_array(str_replace('-rtl.css', '.css', basename($file)), $theme_styles)) {
01712
01713 unset($types[$type][$file]);
01714 continue;
01715 }
01716 if (!$preprocess || !($is_writable && $preprocess_css)) {
01717
01718
01719 if (!$preprocess && $type == 'module') {
01720 $no_module_preprocess .= '<link type="text/css" rel="stylesheet" media="' . $media . '" href="' . base_path() . $file . $query_string . '" />' . "\n";
01721 }
01722
01723
01724 else if (!$preprocess && $type == 'theme') {
01725 $no_theme_preprocess .= '<link type="text/css" rel="stylesheet" media="' . $media . '" href="' . base_path() . $file . $query_string . '" />' . "\n";
01726 }
01727 else {
01728 $output .= '<link type="text/css" rel="stylesheet" media="' . $media . '" href="' . base_path() . $file . $query_string . '" />' . "\n";
01729 }
01730 }
01731 }
01732 }
01733
01734 if ($is_writable && $preprocess_css) {
01735 $filename = md5(serialize($types) . $query_string) . '.css';
01736 $preprocess_file = drupal_build_css_cache($types, $filename);
01737 $output .= '<link type="text/css" rel="stylesheet" media="' . $media . '" href="' . base_path() . $preprocess_file . '" />' . "\n";
01738 }
01739 }
01740
01741 return $no_module_preprocess . $output . $no_theme_preprocess;
01742 }
01743
01755 function drupal_build_css_cache($types, $filename) {
01756 $data = '';
01757
01758
01759 $csspath = file_create_path('css');
01760 file_check_directory($csspath, FILE_CREATE_DIRECTORY);
01761
01762 if (!file_exists($csspath . '/' . $filename)) {
01763
01764 foreach ($types as $type) {
01765 foreach ($type as $file => $cache) {
01766 if ($cache) {
01767 $contents = drupal_load_stylesheet($file, TRUE);
01768
01769 $base = base_path() . dirname($file) . '/';
01770 _drupal_build_css_path(NULL, $base);
01771
01772 $data .= preg_replace_callback('/url\([\'"]?(?![a-z]+:|\/+)([^\'")]+)[\'"]?\)/i', '_drupal_build_css_path', $contents);
01773 }
01774 }
01775 }
01776
01777
01778
01779 $regexp = '/@import[^;]+;/i';
01780 preg_match_all($regexp, $data, $matches);
01781 $data = preg_replace($regexp, '', $data);
01782 $data = implode('', $matches[0]) . $data;
01783
01784
01785 file_save_data($data, $csspath . '/' . $filename, FILE_EXISTS_REPLACE);
01786 }
01787 return $csspath . '/' . $filename;
01788 }
01789
01795 function _drupal_build_css_path($matches, $base = NULL) {
01796 static $_base;
01797
01798 if (isset($base)) {
01799 $_base = $base;
01800 }
01801
01802
01803 $path = $_base . $matches[1];
01804 $last = '';
01805 while ($path != $last) {
01806 $last = $path;
01807 $path = preg_replace('`(^|/)(?!../)([^/]+)/../`', '$1', $path);
01808 }
01809 return 'url(' . $path . ')';
01810 }
01811
01830 function drupal_load_stylesheet($file, $optimize = NULL) {
01831 static $_optimize;
01832
01833 if (isset($optimize)) {
01834 $_optimize = $optimize;
01835 }
01836
01837 $contents = '';
01838 if (file_exists($file)) {
01839
01840 $contents = file_get_contents($file);
01841
01842
01843 $cwd = getcwd();
01844 chdir(dirname($file));
01845
01846
01847
01848 $contents = preg_replace_callback('/@import\s*(?:url\()?[\'"]?(?![a-z]+:)([^\'"\()]+)[\'"]?\)?;/', '_drupal_load_stylesheet', $contents);
01849
01850 $contents = preg_replace('/^@charset\s+[\'"](\S*)\b[\'"];/i', '', $contents);
01851
01852 if ($_optimize) {
01853
01854 $contents = preg_replace('<
01855 \s*([@{}:;,]|\)\s|\s\()\s* | # Remove whitespace around separators, but keep space around parentheses.
01856 /\*([^*\\\\]|\*(?!/))+\*/ | # Remove comments that are not CSS hacks.
01857 [\n\r] # Remove line breaks.
01858 >x', '\1', $contents);
01859 }
01860
01861
01862 chdir($cwd);
01863 }
01864
01865 return $contents;
01866 }
01867
01874 function _drupal_load_stylesheet($matches) {
01875 $filename = $matches[1];
01876
01877 $file = drupal_load_stylesheet($filename);
01878
01879 return preg_replace('/url\(([\'"]?)(?![a-z]+:)([^\'")]+)[\'"]?\)?;/i', 'url(\1' . dirname($filename) . '/', $file);
01880 }
01881
01885 function drupal_clear_css_cache() {
01886 file_scan_directory(file_create_path('css'), '.*', array('.', '..', 'CVS'), 'file_delete', TRUE);
01887 }
01888
01945 function drupal_add_js($data = NULL, $type = 'module', $scope = 'header', $defer = FALSE, $cache = TRUE, $preprocess = TRUE) {
01946 static $javascript = array();
01947
01948 if (isset($data)) {
01949
01950
01951
01952 if (empty($javascript)) {
01953 $javascript['header'] = array(
01954 'core' => array(
01955 'misc/jquery.js' => array('cache' => TRUE, 'defer' => FALSE, 'preprocess' => TRUE),
01956 'misc/drupal.js' => array('cache' => TRUE, 'defer' => FALSE, 'preprocess' => TRUE),
01957 ),
01958 'module' => array(),
01959 'theme' => array(),
01960 'setting' => array(
01961 array('basePath' => base_path()),
01962 ),
01963 'inline' => array(),
01964 );
01965 }
01966
01967 if (isset($scope) && !isset($javascript[$scope])) {
01968 $javascript[$scope] = array('core' => array(), 'module' => array(), 'theme' => array(), 'setting' => array(), 'inline' => array());
01969 }
01970
01971 if (isset($type) && isset($scope) && !isset($javascript[$scope][$type])) {
01972 $javascript[$scope][$type] = array();
01973 }
01974
01975 switch ($type) {
01976 case 'setting':
01977 $javascript[$scope][$type][] = $data;
01978 break;
01979 case 'inline':
01980 $javascript[$scope][$type][] = array('code' => $data, 'defer' => $defer);
01981 break;
01982 default:
01983
01984 $javascript[$scope][$type][$data] = array('cache' => $cache, 'defer' => $defer, 'preprocess' => (!$cache ? FALSE : $preprocess));
01985 }
01986 }
01987
01988 if (isset($scope)) {
01989
01990 if (isset($javascript[$scope])) {
01991 return $javascript[$scope];
01992 }
01993 else {
01994 return array();
01995 }
01996 }
01997 else {
01998 return $javascript;
01999 }
02000 }
02001
02019 function drupal_get_js($scope = 'header', $javascript = NULL) {
02020 if ((!defined('MAINTENANCE_MODE') || MAINTENANCE_MODE != 'update') && function_exists('locale_update_js_files')) {
02021 locale_update_js_files();
02022 }
02023
02024 if (!isset($javascript)) {
02025 $javascript = drupal_add_js(NULL, NULL, $scope);
02026 }
02027
02028 if (empty($javascript)) {
02029 return '';
02030 }
02031
02032 $output = '';
02033 $preprocessed = '';
02034 $no_preprocess = array('core' => '', 'module' => '', 'theme' => '');
02035 $files = array();
02036 $preprocess_js = (variable_get('preprocess_js', FALSE) && (!defined('MAINTENANCE_MODE') || MAINTENANCE_MODE != 'update'));
02037 $directory = file_directory_path();
02038 $is_writable = is_dir($directory) && is_writable($directory) && (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PUBLIC);
02039
02040
02041
02042
02043
02044
02045
02046 $query_string = '?' . substr(variable_get('css_js_query_string', '0'), 0, 1);
02047
02048 foreach ($javascript as $type => $data) {
02049
02050 if (!$data) continue;
02051
02052 switch ($type) {
02053 case 'setting':
02054 $output .= '<script type="text/javascript">jQuery.extend(Drupal.settings, ' . drupal_to_js(call_user_func_array('array_merge_recursive', $data)) . ");</script>\n";
02055 break;
02056 case 'inline':
02057 foreach ($data as $info) {
02058 $output .= '<script type="text/javascript"' . ($info['defer'] ? ' defer="defer"' : '') . '>' . $info['code'] . "</script>\n";
02059 }
02060 break;
02061 default:
02062
02063
02064 foreach ($data as $path => $info) {
02065 if (!$info['preprocess'] || !$is_writable || !$preprocess_js) {
02066 $no_preprocess[$type] .= '<script type="text/javascript"' . ($info['defer'] ? ' defer="defer"' : '') . ' src="' . base_path() . $path . ($info['cache'] ? $query_string : '?' . time()) . "\"></script>\n";
02067 }
02068 else {
02069 $files[$path] = $info;
02070 }
02071 }
02072 }
02073 }
02074
02075
02076 if ($is_writable && $preprocess_js && count($files) > 0) {
02077 $filename = md5(serialize($files) . $query_string) . '.js';
02078 $preprocess_file = drupal_build_js_cache($files, $filename);
02079 $preprocessed .= '<script type="text/javascript" src="' . base_path() . $preprocess_file . '"></script>' . "\n";
02080 }
02081
02082
02083
02084 $output = $preprocessed . implode('', $no_preprocess) . $output;
02085
02086 return $output;
02087 }
02088
02196 function drupal_add_tabledrag($table_id, $action, $relationship, $group, $subgroup = NULL, $source = NULL, $hidden = TRUE, $limit = 0) {
02197 static $js_added = FALSE;
02198 if (!$js_added) {
02199 drupal_add_js('misc/tabledrag.js', 'core');
02200 $js_added = TRUE;
02201 }
02202
02203
02204 $target = isset($subgroup) ? $subgroup : $group;
02205 $source = isset($source) ? $source : $target;
02206 $settings['tableDrag'][$table_id][$group][] = array(
02207 'target' => $target,
02208 'source' => $source,
02209 'relationship' => $relationship,
02210 'action' => $action,
02211 'hidden' => $hidden,
02212 'limit' => $limit,
02213 );
02214 drupal_add_js($settings, 'setting');
02215 }
02216
02227 function drupal_build_js_cache($files, $filename) {
02228 $contents = '';
02229
02230
02231 $jspath = file_create_path('js');
02232 file_check_directory($jspath, FILE_CREATE_DIRECTORY);
02233
02234 if (!file_exists($jspath . '/' . $filename)) {
02235
02236 foreach ($files as $path => $info) {
02237 if ($info['preprocess']) {
02238
02239 $contents .= file_get_contents($path) . ';';
02240 }
02241 }
02242
02243
02244 file_save_data($contents, $jspath . '/' . $filename, FILE_EXISTS_REPLACE);
02245 }
02246
02247 return $jspath . '/' . $filename;
02248 }
02249
02253 function drupal_clear_js_cache() {
02254 file_scan_directory(file_create_path('js'), '.*', array('.', '..', 'CVS'), 'file_delete', TRUE);
02255 variable_set('javascript_parsed', array());
02256 }
02257
02263 function drupal_to_js($var) {
02264
02265 return str_replace(array("<", ">", "&"), array('\x3c', '\x3e', '\x26'), json_encode($var));
02266 }
02267
02277 function drupal_json($var = NULL) {
02278
02279 drupal_set_header('Content-Type: text/javascript; charset=utf-8');
02280
02281 if (isset($var)) {
02282 echo drupal_to_js($var);
02283 }
02284 }
02285
02305 function drupal_urlencode($text) {
02306 if (variable_get('clean_url', '0')) {
02307 return str_replace(array('%2F', '%26', '%23', '//'),
02308 array('/', '%2526', '%2523', '/%252F'),
02309 rawurlencode($text));
02310 }
02311 else {
02312 return str_replace('%2F', '/', rawurlencode($text));
02313 }
02314 }
02315
02326 function drupal_random_bytes($count) {
02327 static $random_state;
02328
02329 if (empty($random_state)) {
02330 $random_state = getmypid();
02331 }
02332 $output = '';
02333
02334
02335 if ($fh = @fopen('/dev/urandom', 'rb')) {
02336 $output = fread($fh, $count);
02337 fclose($fh);
02338 }
02339
02340
02341
02342
02343
02344
02345
02346
02347 while (strlen($output) < $count) {
02348 $random_state = md5(microtime() . mt_rand() . $random_state);
02349 $output .= md5(mt_rand() . $random_state, TRUE);
02350 }
02351 return substr($output, 0, $count);
02352 }
02353
02360 function drupal_get_private_key() {
02361 if (!($key = variable_get('drupal_private_key', 0))) {
02362 $key = md5(drupal_random_bytes(64));
02363 variable_set('drupal_private_key', $key);
02364 }
02365 return $key;
02366 }
02367
02374 function drupal_get_token($value = '') {
02375 $private_key = drupal_get_private_key();
02376 return md5(session_id() . $value . $private_key);
02377 }
02378
02392 function drupal_valid_token($token, $value = '', $skip_anonymous = FALSE) {
02393 global $user;
02394 return (($skip_anonymous && $user->uid == 0) || ($token == md5(session_id() . $value . variable_get('drupal_private_key', ''))));
02395 }
02396
02419 function xmlrpc(a>) {
02420 require_once './includes/xmlrpc.inc';
02421 $args = func_get_args();
02422 return call_user_func_array('_xmlrpc', $args);
02423 }
02424
02425 function _drupal_bootstrap_full() {
02426 static $called;
02427
02428 if ($called) {
02429 return;
02430 }
02431 $called = 1;
02432 require_once './includes/theme.inc';
02433 require_once './includes/pager.inc';
02434 require_once './includes/menu.inc';
02435 require_once './includes/tablesort.inc';
02436 require_once './includes/file.inc';
02437 require_once './includes/unicode.inc';
02438 require_once './includes/image.inc';
02439 require_once './includes/form.inc';
02440 require_once './includes/mail.inc';
02441 require_once './includes/actions.inc';
02442
02443 set_error_handler('drupal_error_handler');
02444
02445 drupal_set_header('Content-Type: text/html; charset=utf-8');
02446
02447 unicode_check();
02448
02449 fix_gpc_magic();
02450
02451 module_load_all();
02452
02453
02454 if (!defined('MAINTENANCE_MODE') || MAINTENANCE_MODE != 'update') {
02455 module_invoke_all('init');
02456 }
02457 }
02458
02472 function page_set_cache() {
02473 global $user, $base_root;
02474
02475 if (!$user->uid && $_SERVER['REQUEST_METHOD'] == 'GET' && count(drupal_get_messages(NULL, FALSE)) == 0) {
02476
02477 if ($data = ob_get_contents()) {
02478 $cache = TRUE;
02479 if (variable_get('page_compression', TRUE) && function_exists('gzencode')) {
02480
02481
02482 if (zlib_get_coding_type() == 'deflate') {
02483 $cache = FALSE;
02484 }
02485 else if (zlib_get_coding_type() == FALSE) {
02486 $data = gzencode($data, 9, FORCE_GZIP);
02487 }
02488
02489
02490 }
02491 ob_end_flush();
02492 if ($cache && $data) {
02493 cache_set($base_root . request_uri(), $data, 'cache_page', CACHE_TEMPORARY, drupal_get_headers());
02494 }
02495 }
02496 }
02497 }
02498
02504 function drupal_cron_run() {
02505
02506 @ignore_user_abort(TRUE);
02507
02508
02509 @set_time_limit(240);
02510
02511
02512 $semaphore = variable_get('cron_semaphore', FALSE);
02513
02514 if ($semaphore) {
02515 if (time() - $semaphore > 3600) {
02516
02517
02518 watchdog('cron', 'Cron has been running for more than an hour and is most likely stuck.', array(), WATCHDOG_ERROR);
02519
02520
02521 variable_del('cron_semaphore');
02522 }
02523 else {
02524
02525 watchdog('cron', 'Attempting to re-run cron while it is already running.', array(), WATCHDOG_WARNING);
02526 }
02527 }
02528 else {
02529
02530 register_shutdown_function('drupal_cron_cleanup');
02531
02532
02533 variable_set('cron_semaphore', time());
02534
02535
02536 module_invoke_all('cron');
02537
02538
02539 variable_set('cron_last', time());
02540 watchdog('cron', 'Cron run completed.', array(), WATCHDOG_NOTICE);
02541
02542
02543 variable_del('cron_semaphore');
02544
02545
02546 return TRUE;
02547 }
02548 }
02549
02553 function drupal_cron_cleanup() {
02554
02555 if (variable_get('cron_semaphore', FALSE)) {
02556 watchdog('cron', 'Cron run exceeded the time limit and was aborted.', array(), WATCHDOG_WARNING);
02557
02558
02559 variable_del('cron_semaphore');
02560 }
02561 }
02562
02590 function drupal_system_listing($mask, $directory, $key = 'name', $min_depth = 1) {
02591 global $profile;
02592 $config = conf_path();
02593
02594
02595
02596
02597
02598
02599 if (!isset($profile)) {
02600 $profile = variable_get('install_profile', 'default');
02601 }
02602 $searchdir = array($directory);
02603 $files = array();
02604
02605
02606 $searchdir[] = 'sites/all/' . $directory;
02607
02608
02609
02610
02611
02612 if (file_exists("profiles/$profile/$directory")) {
02613 $searchdir[] = "profiles/$profile/$directory";
02614 }
02615
02616 if (file_exists("$config/$directory")) {
02617 $searchdir[] = "$config/$directory";
02618 }
02619
02620
02621 foreach ($searchdir as $dir) {
02622 $files = array_merge($files, file_scan_directory($dir, $mask, array('.', '..', 'CVS'), 0, TRUE, $key, $min_depth));
02623 }
02624
02625 return $files;
02626 }
02627
02628
02643 function drupal_alter($type, &$data) {
02644
02645
02646
02647
02648
02649
02650
02651
02652 if (is_array($data) && isset($data['__drupal_alter_by_ref'])) {
02653 $by_ref_parameters = $data['__drupal_alter_by_ref'];
02654 unset($data['__drupal_alter_by_ref']);
02655 }
02656
02657
02658
02659 $args = array(&$data);
02660 if (isset($by_ref_parameters)) {
02661 $args = array_merge($args, $by_ref_parameters);
02662 }
02663
02664
02665
02666 $additional_args = func_get_args();
02667 array_shift($additional_args);
02668 array_shift($additional_args);
02669 $args = array_merge($args, $additional_args);
02670
02671 foreach (module_implements($type . '_alter') as $module) {
02672 $function = $module . '_' . $type . '_alter';
02673 call_user_func_array($function, $args);
02674 }
02675 }
02676
02677
02690 function drupal_render(&$elements) {
02691 if (!isset($elements) || (isset($elements['#access']) && !$elements['#access'])) {
02692 return NULL;
02693 }
02694
02695
02696
02697 if (!isset($elements['#defaults_loaded']) || !$elements['#defaults_loaded']) {
02698 if ((!empty($elements['#type'])) && ($info = _element_info($elements['#type']))) {
02699 $elements += $info;
02700 }
02701 }
02702
02703
02704
02705
02706 if (isset($elements['#pre_render'])) {
02707 foreach ($elements['#pre_render'] as $function) {
02708 if (drupal_function_exists($function)) {
02709 $elements = $function($elements);
02710 }
02711 }
02712 }
02713
02714 $content = '';
02715
02716
02717 if (!isset($elements['#sorted'])) {
02718 uasort($elements, "element_sort");
02719 }
02720 $elements += array('#title' => NULL, '#description' => NULL);
02721 if (!isset($elements['#children'])) {
02722 $children = element_children($elements);
02723
02724 if (isset($elements['#theme']) && empty($elements['#theme_used'])) {
02725 $elements['#theme_used'] = TRUE;
02726
02727 $previous = array();
02728 foreach (array('#value', '#type', '#prefix', '#suffix') as $key) {
02729 $previous[$key] = isset($elements[$key]) ? $elements[$key] : NULL;
02730 }
02731
02732 if (empty($children)) {
02733 $elements['#printed'] = TRUE;
02734 }
02735 else {
02736 $elements['#value'] = '';
02737 }
02738 $elements['#type'] = 'markup';
02739
02740 unset($elements['#prefix'], $elements['#suffix']);
02741 $content = theme($elements['#theme'], $elements);
02742
02743 foreach (array('#value', '#type', '#prefix', '#suffix') as $key) {
02744 $elements[$key] = isset($previous[$key]) ? $previous[$key] : NULL;
02745 }
02746 }
02747
02748 if (!isset($content) || $content === '') {
02749 foreach ($children as $key) {
02750 $content .= drupal_render($elements[$key]);
02751 }
02752 }
02753 }
02754 if (isset($content) && $content !== '') {
02755 $elements['#children'] = $content;
02756 }
02757
02758
02759 if (!isset($elements['#printed'])) {
02760 $content = theme(!empty($elements['#type']) ? $elements['#type'] : 'markup', $elements);
02761 $elements['#printed'] = TRUE;
02762 }
02763
02764 if (isset($content) && $content !== '') {
02765
02766
02767
02768 if (isset($elements['#post_render'])) {
02769 foreach ($elements['#post_render'] as $function) {
02770 if (drupal_function_exists($function)) {
02771 $content = $function($content, $elements);
02772 }
02773 }
02774 }
02775 $prefix = isset($elements['#prefix']) ? $elements['#prefix'] : '';
02776 $suffix = isset($elements['#suffix']) ? $elements['#suffix'] : '';
02777 return $prefix . $content . $suffix;
02778 }
02779 }
02780
02784 function element_sort($a, $b) {
02785 $a_weight = (is_array($a) && isset($a['#weight'])) ? $a['#weight'] : 0;
02786 $b_weight = (is_array($b) && isset($b['#weight'])) ? $b['#weight'] : 0;
02787 if ($a_weight == $b_weight) {
02788 return 0;
02789 }
02790 return ($a_weight < $b_weight) ? -1 : 1;
02791 }
02792
02796 function element_property($key) {
02797 return $key[0] == '#';
02798 }
02799
02803 function element_properties($element) {
02804 return array_filter(array_keys((array) $element), 'element_property');
02805 }
02806
02810 function element_child($key) {
02811 return !isset($key[0]) || $key[0] != '#';
02812 }
02813
02817 function element_children($element) {
02818 return array_filter(array_keys((array) $element), 'element_child');
02819 }
02820
02824 function drupal_common_theme() {
02825 return array(
02826
02827 'placeholder' => array(
02828 'arguments' => array('text' => NULL)
02829 ),
02830 'page' => array(
02831 'arguments' => array('content' => NULL, 'show_blocks' => TRUE, 'show_messages' => TRUE),
02832 'template' => 'page',
02833 ),
02834 'maintenance_page' => array(
02835 'arguments' => array('content' => NULL, 'show_blocks' => TRUE, 'show_messages' => TRUE),
02836 'template' => 'maintenance-page',
02837 ),
02838 'update_page' => array(
02839 'arguments' => array('content' => NULL, 'show_messages' => TRUE),
02840 ),
02841 'install_page' => array(
02842 'arguments' => array('content' => NULL),
02843 ),
02844 'task_list' => array(
02845 'arguments' => array('items' => NULL, 'active' => NULL),
02846 ),
02847 'status_messages' => array(
02848 'arguments' => array('display' => NULL),
02849 ),
02850 'links' => array(
02851 'arguments' => array('links' => NULL, 'attributes' => array('class' => 'links')),
02852 ),
02853 'image' => array(
02854 'arguments' => array('path' => NULL, 'alt' => '', 'title' => '', 'attributes' => NULL, 'getsize' => TRUE),
02855 ),
02856 'breadcrumb' => array(
02857 'arguments' => array('breadcrumb' => NULL),
02858 ),
02859 'help' => array(
02860 'arguments' => array(),
02861 ),
02862 'submenu' => array(
02863 'arguments' => array('links' => NULL),
02864 ),
02865 'table' => array(
02866 'arguments' => array('header' => NULL, 'rows' => NULL, 'attributes' => array(), 'caption' => NULL),
02867 ),
02868 'table_select_header_cell' => array(
02869 'arguments' => array(),
02870 ),
02871 'tablesort_indicator' => array(
02872 'arguments' => array('style' => NULL),
02873 ),
02874 'box' => array(
02875 'arguments' => array('title' => NULL, 'content' => NULL, 'region' => 'main'),
02876 'template' => 'box',
02877 ),
02878 'block' => array(
02879 'arguments' => array('block' => NULL),
02880 'template' => 'block',
02881 ),
02882 'mark' => array(
02883 'arguments' => array('type' => MARK_NEW),
02884 ),
02885 'item_list' => array(
02886 'arguments' => array('items' => array(), 'title' => NULL, 'type' => 'ul', 'attributes' => NULL),
02887 ),
02888 'more_help_link' => array(
02889 'arguments' => array('url' => NULL),
02890 ),
02891 'xml_icon' => array(
02892 'arguments' => array('url' => NULL),
02893 ),
02894 'feed_icon' => array(
02895 'arguments' => array('url' => NULL, 'title' => NULL),
02896 ),
02897 'more_link' => array(
02898 'arguments' => array('url' => NULL, 'title' => NULL)
02899 ),
02900 'closure' => array(
02901 'arguments' => array('main' => 0),
02902 ),
02903 'blocks' => array(
02904 'arguments' => array('region' => NULL),
02905 ),
02906 'username' => array(
02907 'arguments' => array('object' => NULL),
02908 ),
02909 'progress_bar' => array(
02910 'arguments' => array('percent' => NULL, 'message' => NULL),
02911 ),
02912 'indentation' => array(
02913 'arguments' => array('size' => 1),
02914 ),
02915
02916 'pager' => array(
02917 'arguments' => array('tags' => array(), 'limit' => 10, 'element' => 0, 'parameters' => array()),
02918 ),
02919 'pager_first' => array(
02920 'arguments' => array('text' => NULL, 'limit' => NULL, 'element' => 0, 'parameters' => array()),
02921 ),
02922 'pager_previous' => array(
02923 'arguments' => array('text' => NULL, 'limit' => NULL, 'element' => 0, 'interval' => 1, 'parameters' => array()),
02924 ),
02925 'pager_next' => array(
02926 'arguments' => array('text' => NULL, 'limit' => NULL, 'element' => 0, 'interval' => 1, 'parameters' => array()),
02927 ),
02928 'pager_last' => array(
02929 'arguments' => array('text' => NULL, 'limit' => NULL, 'element' => 0, 'parameters' => array()),
02930 ),
02931 'pager_link' => array(
02932 'arguments' => array('text' => NULL, 'page_new' => NULL, 'element' => NULL, 'parameters' => array(), 'attributes' => array()),
02933 ),
02934
02935 'locale_admin_manage_screen' => array(
02936 'arguments' => array('form' => NULL),
02937 ),
02938
02939 'menu_item_link' => array(
02940 'arguments' => array('item' => NULL),
02941 ),
02942 'menu_tree' => array(
02943 'arguments' => array('tree' => NULL),
02944 ),
02945 'menu_item' => array(
02946 'arguments' => array('link' => NULL, 'has_children' => NULL, 'menu' => ''),
02947 ),
02948 'menu_local_task' => array(
02949 'arguments' => array('link' => NULL, 'active' => FALSE),
02950 ),
02951 'menu_local_tasks' => array(
02952 'arguments' => array(),
02953 ),
02954
02955 'select' => array(
02956 'arguments' => array('element' => NULL),
02957 ),
02958 'fieldset' => array(
02959 'arguments' => array('element' => NULL),
02960 ),
02961 'radio' => array(
02962 'arguments' => array('element' => NULL),
02963 ),
02964 'radios' => array(
02965 'arguments' => array('element' => NULL),
02966 ),
02967 'password_confirm' => array(
02968 'arguments' => array('element' => NULL),
02969 ),
02970 'date' => array(
02971 'arguments' => array('element' => NULL),
02972 ),
02973 'item' => array(
02974 'arguments' => array('element' => NULL),
02975 ),
02976 'checkbox' => array(
02977 'arguments' => array('element' => NULL),
02978 ),
02979 'checkboxes' => array(
02980 'arguments' => array('element' => NULL),
02981 ),
02982 'submit' => array(
02983 'arguments' => array('element' => NULL),
02984 ),
02985 'button' => array(
02986 'arguments' => array('element' => NULL),
02987 ),
02988 'image_button' => array(
02989 'arguments' => array('element' => NULL),
02990 ),
02991 'hidden' => array(
02992 'arguments' => array('element' => NULL),
02993 ),
02994 'token' => array(
02995 'arguments' => array('element' => NULL),
02996 ),
02997 'textfield' => array(
02998 'arguments' => array('element' => NULL),
02999 ),
03000 'form' => array(
03001 'arguments' => array('element' => NULL),
03002 ),
03003 'textarea' => array(
03004 'arguments' => array('element' => NULL),
03005 ),
03006 'markup' => array(
03007 'arguments' => array('element' => NULL),
03008 ),
03009 'password' => array(
03010 'arguments' => array('element' => NULL),
03011 ),
03012 'file' => array(
03013 'arguments' => array('element' => NULL),
03014 ),
03015 'form_element' => array(
03016 'arguments' => array('element' => NULL, 'value' => NULL),
03017 ),
03018 );
03019 }
03020
03037 function drupal_get_schema($table = NULL, $rebuild = FALSE) {
03038 static $schema = array();
03039
03040 if (empty($schema) || $rebuild) {
03041
03042 if (!$rebuild && $cached = cache_get('schema')) {
03043 $schema = $cached->data;
03044 }
03045
03046 else {
03047 $schema = array();
03048
03049 module_load_all_includes('install');
03050
03051
03052 foreach (module_implements('schema') as $module) {
03053 $current = module_invoke($module, 'schema');
03054 _drupal_initialize_schema($module, $current);
03055 $schema = array_merge($schema, $current);
03056 }
03057
03058 drupal_alter('schema', $schema);
03059 cache_set('schema', $schema);
03060 }
03061 }
03062
03063 if (!isset($table)) {
03064 return $schema;
03065 }
03066 elseif (isset($schema[$table])) {
03067 return $schema[$table];
03068 }
03069 else {
03070 return FALSE;
03071 }
03072 }
03073
03088 function drupal_install_schema($module) {
03089 $schema = drupal_get_schema_unprocessed($module);
03090 _drupal_initialize_schema($module, $schema);
03091
03092 $ret = array();
03093 foreach ($schema as $name => $table) {
03094 db_create_table($ret, $name, $table);
03095 }
03096 return $ret;
03097 }
03098
03113 function drupal_uninstall_schema($module) {
03114 $schema = drupal_get_schema_unprocessed($module);
03115 _drupal_initialize_schema($module, $schema);
03116
03117 $ret = array();
03118 foreach ($schema as $table) {
03119 db_drop_table($ret, $table['name']);
03120 }
03121 return $ret;
03122 }
03123
03148 function drupal_get_schema_unprocessed($module, $table = NULL) {
03149
03150 module_load_install($module);
03151 $schema = module_invoke($module, 'schema');
03152
03153 if (!is_null($table) && isset($schema[$table])) {
03154 return $schema[$table];
03155 }
03156 else {
03157 return $schema;
03158 }
03159 }
03160
03170 function _drupal_initialize_schema($module, &$schema) {
03171
03172 foreach ($schema as $name => $table) {
03173 if (empty($table['module'])) {
03174 $schema[$name]['module'] = $module;
03175 }
03176 if (!isset($table['name'])) {
03177 $schema[$name]['name'] = $name;
03178 }
03179 }
03180 }
03181
03192 function drupal_schema_fields_sql($table, $prefix = NULL) {
03193 $schema = drupal_get_schema($table);
03194 $fields = array_keys($schema['fields']);
03195 if ($prefix) {
03196 $columns = array();
03197 foreach ($fields as $field) {
03198 $columns[] = "$prefix.$field";
03199 }
03200 return $columns;
03201 }
03202 else {
03203 return $fields;
03204 }
03205 }
03206
03230 function drupal_write_record($table, &$object, $update = array()) {
03231
03232 if (is_string($update)) {
03233 $update = array($update);
03234 }
03235
03236 $schema = drupal_get_schema($table);
03237 if (empty($schema)) {
03238 return FALSE;
03239 }
03240
03241
03242 if (is_array($object)) {
03243 $object = (object) $object;
03244 $array = TRUE;
03245 }
03246 else {
03247 $array = FALSE;
03248 }
03249
03250 $fields = $defs = $values = $serials = $placeholders = array();
03251
03252
03253
03254 foreach ($schema['fields'] as $field => $info) {
03255
03256 if ($info['type'] == 'serial' && count($update)) {
03257 continue;
03258 }
03259
03260
03261 if (!isset($object->$field) && !count($update) && isset($info['default'])) {
03262 $object->$field = $info['default'];
03263 }
03264
03265
03266 if ($info['type'] == 'serial') {
03267 $serials[] = $field;
03268
03269 unset($object->$field);
03270 }
03271
03272
03273 if (isset($object->$field)) {
03274 $fields[] = $field;
03275 $placeholders[] = db_type_placeholder($info['type']);
03276
03277 if (empty($info['serialize'])) {
03278 $values[] = $object->$field;
03279 }
03280 elseif (!empty($object->$field)) {
03281 $values[] = serialize($object->$field);
03282 }
03283 else {
03284 $values[] = '';
03285 }
03286 }
03287 }
03288
03289 if (empty($fields)) {
03290
03291
03292 if ($array) {
03293 $object = (array)$object;
03294 }
03295 return;
03296 }
03297
03298
03299 $query = '';
03300 if (!count($update)) {
03301 $query = "INSERT INTO {" . $table . "} (" . implode(', ', $fields) . ') VALUES (' . implode(', ', $placeholders) . ')';
03302 $return = SAVED_NEW;
03303 }
03304 else {
03305 $query = '';
03306 foreach ($fields as $id => $field) {
03307 if ($query) {
03308 $query .= ', ';
03309 }
03310 $query .= $field . ' = ' . $placeholders[$id];
03311 }
03312
03313 foreach ($update as $key){
03314 $conditions[] = "$key = " . db_type_placeholder($schema['fields'][$key]['type']);
03315 $values[] = $object->$key;
03316 }
03317
03318 $query = "UPDATE {" . $table . "} SET $query WHERE " . implode(' AND ', $conditions);
03319 $return = SAVED_UPDATED;
03320 }
03321
03322
03323 if (db_query($query, $values)) {
03324 if ($serials) {
03325
03326 foreach ($serials as $field) {
03327 $object->$field = db_last_insert_id($table, $field);
03328 }
03329 }
03330 }
03331 else {
03332 $return = FALSE;
03333 }
03334
03335
03336 if ($array) {
03337 $object = (array) $object;
03338 }
03339
03340 return $return;
03341 }
03342
03406 function drupal_parse_info_file($filename) {
03407 $info = array();
03408
03409 if (!file_exists($filename)) {
03410 return $info;
03411 }
03412
03413 $data = file_get_contents($filename);
03414 if (preg_match_all('
03415 @^\s* # Start at the beginning of a line, ignoring leading whitespace
03416 ((?:
03417 [^=;\[\]]| # Key names cannot contain equal signs, semi-colons or square brackets,
03418 \[[^\[\]]*\] # unless they are balanced and not nested
03419 )+?)
03420 \s*=\s* # Key/value pairs are separated by equal signs (ignoring white-space)
03421 (?:
03422 ("(?:[^"]|(?<=\\\\)")*")| # Double-quoted string, which may contain slash-escaped quotes/slashes
03423 (\'(?:[^\']|(?<=\\\\)\')*\')| # Single-quoted string, which may contain slash-escaped quotes/slashes
03424 ([^\r\n]*?) # Non-quoted string
03425 )\s*$ # Stop at the next end of a line, ignoring trailing whitespace
03426 @msx', $data, $matches, PREG_SET_ORDER)) {
03427 foreach ($matches as $match) {
03428
03429 $i = 0;
03430 foreach (array('key', 'value1', 'value2', 'value3') as $var) {
03431 $$var = isset($match[++$i]) ? $match[$i] : '';
03432 }
03433 $value = stripslashes(substr($value1, 1, -1)) . stripslashes(substr($value2, 1, -1)) . $value3;
03434
03435
03436 $keys = preg_split('/\]?\[/', rtrim($key, ']'));
03437 $last = array_pop($keys);
03438 $parent = &$info;
03439
03440
03441 foreach ($keys as $key) {
03442 if ($key == '') {
03443 $key = count($parent);
03444 }
03445 if (!isset($parent[$key]) || !is_array($parent[$key])) {
03446 $parent[$key] = array();
03447 }
03448 $parent = &$parent[$key];
03449 }
03450
03451
03452 if (defined($value)) {
03453 $value = constant($value);
03454 }
03455
03456
03457 if ($last == '') {
03458 $last = count($parent);
03459 }
03460 $parent[$last] = $value;
03461 }
03462 }
03463
03464 return $info;
03465 }
03466
03475 function watchdog_severity_levels() {
03476 return array(
03477 WATCHDOG_EMERG => t('emergency'),
03478 WATCHDOG_ALERT => t('alert'),
03479 WATCHDOG_CRITICAL => t('critical'),
03480 WATCHDOG_ERROR => t('error'),
03481 WATCHDOG_WARNING => t('warning'),
03482 WATCHDOG_NOTICE => t('notice'),
03483 WATCHDOG_INFO => t('info'),
03484 WATCHDOG_DEBUG => t('debug'),
03485 );
03486 }
03487
03488
03492 function drupal_explode_tags($tags) {
03493
03494
03495 $regexp = '%(?:^|,\ *)("(?>[^"]*)(?>""[^"]* )*"|(?: [^",]*))%x';
03496 preg_match_all($regexp, $tags, $matches);
03497 $typed_tags = array_unique($matches[1]);
03498
03499 $tags = array();
03500 foreach ($typed_tags as $tag) {
03501
03502
03503
03504 $tag = trim(str_replace('""', '"', preg_replace('/^"(.*)"$/', '\1', $tag)));
03505 if ($tag != "") {
03506 $tags[] = $tag;
03507 }
03508 }
03509
03510 return $tags;
03511 }
03512
03516 function drupal_implode_tags($tags) {
03517 $encoded_tags = array();
03518 foreach ($tags as $tag) {
03519
03520 if (strpos($tag, ',') !== FALSE || strpos($tag, '"') !== FALSE) {
03521 $tag = '"' . str_replace('"', '""', $tag) . '"';
03522 }
03523
03524 $encoded_tags[] = $tag;
03525 }
03526 return implode(', ', $encoded_tags);
03527 }
03528
03535 function drupal_flush_all_caches() {
03536
03537 _drupal_flush_css_js();
03538
03539 drupal_rebuild_code_registry();
03540 drupal_clear_css_cache();
03541 drupal_clear_js_cache();
03542 system_theme_data();
03543 drupal_rebuild_theme_registry();
03544 menu_rebuild();
03545 node_types_rebuild();
03546
03547
03548 $core = array('cache', 'cache_block', 'cache_filter', 'cache_registry', 'cache_page');
03549 $cache_tables = array_merge(module_invoke_all('flush_caches'), $core);
03550 foreach ($cache_tables as $table) {
03551 cache_clear_all('*', $table, TRUE);
03552 }
03553 }
03554
03564 function _drupal_flush_css_js() {
03565 $string_history = variable_get('css_js_query_string', '00000000000000000000');
03566 $new_character = $string_history[0];
03567 $characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
03568 while (strpos($string_history, $new_character) !== FALSE) {
03569 $new_character = $characters[mt_rand(0, strlen($characters) - 1)];
03570 }
03571 variable_set('css_js_query_string', $new_character . substr($string_history, 0, 19));
03572 }