Mercurial > defr > drupal > core
comparison includes/common.inc @ 19:3edae6ecd6c6 6.9
Drupal 6.9
| author | Franck Deroche <franck@defr.org> |
|---|---|
| date | Thu, 15 Jan 2009 10:15:56 +0100 |
| parents | 8e6257f3ae39 |
| children |
comparison
equal
deleted
inserted
replaced
| 18:f5131a9cd9e5 | 19:3edae6ecd6c6 |
|---|---|
| 1 <?php | 1 <?php |
| 2 // $Id: common.inc,v 1.756.2.37 2008/12/11 17:39:42 goba Exp $ | 2 // $Id: common.inc,v 1.756.2.42 2009/01/14 23:34:07 goba Exp $ |
| 3 | 3 |
| 4 /** | 4 /** |
| 5 * @file | 5 * @file |
| 6 * Common functions that many Drupal modules will need to reference. | 6 * Common functions that many Drupal modules will need to reference. |
| 7 * | 7 * |
| 412 * @return | 412 * @return |
| 413 * An object containing the HTTP request headers, response code, headers, | 413 * An object containing the HTTP request headers, response code, headers, |
| 414 * data and redirect status. | 414 * data and redirect status. |
| 415 */ | 415 */ |
| 416 function drupal_http_request($url, $headers = array(), $method = 'GET', $data = NULL, $retry = 3) { | 416 function drupal_http_request($url, $headers = array(), $method = 'GET', $data = NULL, $retry = 3) { |
| 417 static $self_test = FALSE; | |
| 418 $result = new stdClass(); | 417 $result = new stdClass(); |
| 419 // Try to clear the drupal_http_request_fails variable if it's set. We | |
| 420 // can't tie this call to any error because there is no surefire way to | |
| 421 // tell whether a request has failed, so we add the check to places where | |
| 422 // some parsing has failed. | |
| 423 if (!$self_test && variable_get('drupal_http_request_fails', FALSE)) { | |
| 424 $self_test = TRUE; | |
| 425 $works = module_invoke('system', 'check_http_request'); | |
| 426 $self_test = FALSE; | |
| 427 if (!$works) { | |
| 428 // Do not bother with further operations if we already know that we | |
| 429 // have no chance. | |
| 430 $result->error = t("The server can't issue HTTP requests"); | |
| 431 return $result; | |
| 432 } | |
| 433 } | |
| 434 | 418 |
| 435 // Parse the URL and make sure we can handle the schema. | 419 // Parse the URL and make sure we can handle the schema. |
| 436 $uri = parse_url($url); | 420 $uri = parse_url($url); |
| 437 | 421 |
| 438 if ($uri == FALSE) { | 422 if ($uri == FALSE) { |
| 466 if (!$fp) { | 450 if (!$fp) { |
| 467 // When a network error occurs, we use a negative number so it does not | 451 // When a network error occurs, we use a negative number so it does not |
| 468 // clash with the HTTP status codes. | 452 // clash with the HTTP status codes. |
| 469 $result->code = -$errno; | 453 $result->code = -$errno; |
| 470 $result->error = trim($errstr); | 454 $result->error = trim($errstr); |
| 455 | |
| 456 // Mark that this request failed. This will trigger a check of the web | |
| 457 // server's ability to make outgoing HTTP requests the next time that | |
| 458 // requirements checking is performed. | |
| 459 // @see system_requirements() | |
| 460 variable_set('drupal_http_request_fails', TRUE); | |
| 461 | |
| 471 return $result; | 462 return $result; |
| 472 } | 463 } |
| 473 | 464 |
| 474 // Construct the path to act on. | 465 // Construct the path to act on. |
| 475 $path = isset($uri['path']) ? $uri['path'] : '/'; | 466 $path = isset($uri['path']) ? $uri['path'] : '/'; |
| 682 * Any text within t() can be extracted by translators and changed into | 673 * Any text within t() can be extracted by translators and changed into |
| 683 * the equivalent text in their native language. | 674 * the equivalent text in their native language. |
| 684 * | 675 * |
| 685 * Special variables called "placeholders" are used to signal dynamic | 676 * Special variables called "placeholders" are used to signal dynamic |
| 686 * information in a string which should not be translated. Placeholders | 677 * information in a string which should not be translated. Placeholders |
| 687 * can also be used for text that may change from time to time | 678 * can also be used for text that may change from time to time (such as |
| 688 * (such as link paths) to be changed without requiring updates to translations. | 679 * link paths) to be changed without requiring updates to translations. |
| 689 * | 680 * |
| 690 * For example: | 681 * For example: |
| 691 * @code | 682 * @code |
| 692 * $output = t('There are currently %members and %visitors online.', array( | 683 * $output = t('There are currently %members and %visitors online.', array( |
| 693 * '%members' => format_plural($total_users, '1 user', '@count users'), | 684 * '%members' => format_plural($total_users, '1 user', '@count users'), |
| 699 * useful for inserting variables into things like e-mail. | 690 * useful for inserting variables into things like e-mail. |
| 700 * @code | 691 * @code |
| 701 * $message[] = t("If you don't want to receive such e-mails, you can change your settings at !url.", array('!url' => url("user/$account->uid", array('absolute' => TRUE)))); | 692 * $message[] = t("If you don't want to receive such e-mails, you can change your settings at !url.", array('!url' => url("user/$account->uid", array('absolute' => TRUE)))); |
| 702 * @endcode | 693 * @endcode |
| 703 * | 694 * |
| 704 * - @variable, which indicates that the text should be run through check_plain, | 695 * - @variable, which indicates that the text should be run through |
| 705 * to escape HTML characters. Use this for any output that's displayed within | 696 * check_plain, to escape HTML characters. Use this for any output that's |
| 706 * a Drupal page. | 697 * displayed within a Drupal page. |
| 707 * @code | 698 * @code |
| 708 * drupal_set_title($title = t("@name's blog", array('@name' => $account->name))); | 699 * drupal_set_title($title = t("@name's blog", array('@name' => $account->name))); |
| 709 * @endcode | 700 * @endcode |
| 710 * | 701 * |
| 711 * - %variable, which indicates that the string should be HTML escaped and | 702 * - %variable, which indicates that the string should be HTML escaped and |
| 714 * @code | 705 * @code |
| 715 * $message = t('%name-from sent %name-to an e-mail.', array('%name-from' => $user->name, '%name-to' => $account->name)); | 706 * $message = t('%name-from sent %name-to an e-mail.', array('%name-from' => $user->name, '%name-to' => $account->name)); |
| 716 * @endcode | 707 * @endcode |
| 717 * | 708 * |
| 718 * When using t(), try to put entire sentences and strings in one t() call. | 709 * When using t(), try to put entire sentences and strings in one t() call. |
| 719 * This makes it easier for translators, as it provides context as to what each | 710 * This makes it easier for translators, as it provides context as to what |
| 720 * word refers to. HTML markup within translation strings is allowed, but should | 711 * each word refers to. HTML markup within translation strings is allowed, but |
| 721 * be avoided if possible. The exception are embedded links; link titles add a | 712 * should be avoided if possible. The exception are embedded links; link |
| 722 * context for translators, so should be kept in the main string. | 713 * titles add a context for translators, so should be kept in the main string. |
| 723 * | 714 * |
| 724 * Here is an example of incorrect usage of t(): | 715 * Here is an example of incorrect usage of t(): |
| 725 * @code | 716 * @code |
| 726 * $output .= t('<p>Go to the @contact-page.</p>', array('@contact-page' => l(t('contact page'), 'contact'))); | 717 * $output .= t('<p>Go to the @contact-page.</p>', array('@contact-page' => l(t('contact page'), 'contact'))); |
| 727 * @endcode | 718 * @endcode |
| 840 * | 831 * |
| 841 * @param $string | 832 * @param $string |
| 842 * A string containing the English string to translate. | 833 * A string containing the English string to translate. |
| 843 * @param $args | 834 * @param $args |
| 844 * An associative array of replacements to make after translation. Incidences | 835 * An associative array of replacements to make after translation. Incidences |
| 845 * of any key in this array are replaced with the corresponding value. | 836 * of any key in this array are replaced with the corresponding value. Based |
| 846 * Based on the first character of the key, the value is escaped and/or themed: | 837 * on the first character of the key, the value is escaped and/or themed: |
| 847 * - !variable: inserted as is | 838 * - !variable: inserted as is |
| 848 * - @variable: escape plain text to HTML (check_plain) | 839 * - @variable: escape plain text to HTML (check_plain) |
| 849 * - %variable: escape text and theme as a placeholder for user-submitted | 840 * - %variable: escape text and theme as a placeholder for user-submitted |
| 850 * content (check_plain + theme_placeholder) | 841 * content (check_plain + theme_placeholder) |
| 851 * @param $langcode | 842 * @param $langcode |
| 929 /** | 920 /** |
| 930 * Verify the syntax of the given URL. | 921 * Verify the syntax of the given URL. |
| 931 * | 922 * |
| 932 * This function should only be used on actual URLs. It should not be used for | 923 * This function should only be used on actual URLs. It should not be used for |
| 933 * Drupal menu paths, which can contain arbitrary characters. | 924 * Drupal menu paths, which can contain arbitrary characters. |
| 925 * Valid values per RFC 3986. | |
| 934 * | 926 * |
| 935 * @param $url | 927 * @param $url |
| 936 * The URL to verify. | 928 * The URL to verify. |
| 937 * @param $absolute | 929 * @param $absolute |
| 938 * Whether the URL is absolute (beginning with a scheme such as "http:"). | 930 * Whether the URL is absolute (beginning with a scheme such as "http:"). |
| 939 * @return | 931 * @return |
| 940 * TRUE if the URL is in a valid format. | 932 * TRUE if the URL is in a valid format. |
| 941 */ | 933 */ |
| 942 function valid_url($url, $absolute = FALSE) { | 934 function valid_url($url, $absolute = FALSE) { |
| 943 $allowed_characters = '[a-z0-9\/:_\-_\.\?\$,;~=#&%\+]'; | |
| 944 if ($absolute) { | 935 if ($absolute) { |
| 945 return preg_match("/^(http|https|ftp):\/\/". $allowed_characters ."+$/i", $url); | 936 return (bool)preg_match(" |
| 937 /^ # Start at the beginning of the text | |
| 938 (?:ftp|https?):\/\/ # Look for ftp, http, or https schemes | |
| 939 (?: # Userinfo (optional) which is typically | |
| 940 (?:(?:[\w\.\-\+!$&'\(\)*\+,;=]|%[0-9a-f]{2})+:)* # a username or a username and password | |
| 941 (?:[\w\.\-\+%!$&'\(\)*\+,;=]|%[0-9a-f]{2})+@ # combination | |
| 942 )? | |
| 943 (?: | |
| 944 (?:[a-z0-9\-\.]|%[0-9a-f]{2})+ # A domain name or a IPv4 address | |
| 945 |(?:\[(?:[0-9a-f]{0,4}:)*(?:[0-9a-f]{0,4})\]) # or a well formed IPv6 address | |
| 946 ) | |
| 947 (?::[0-9]+)? # Server port number (optional) | |
| 948 (?:[\/|\?] | |
| 949 (?:[\w#!:\.\?\+=&@$'~*,;\/\(\)\[\]\-]|%[0-9a-f]{2}) # The path and query (optional) | |
| 950 *)? | |
| 951 $/xi", $url); | |
| 946 } | 952 } |
| 947 else { | 953 else { |
| 948 return preg_match("/^". $allowed_characters ."+$/i", $url); | 954 return (bool)preg_match("/^(?:[\w#!:\.\?\+=&@$'~*,;\/\(\)\[\]\-]|%[0-9a-f]{2})+$/i", $url); |
| 949 } | 955 } |
| 950 } | 956 } |
| 957 | |
| 951 | 958 |
| 952 /** | 959 /** |
| 953 * @} End of "defgroup validation". | 960 * @} End of "defgroup validation". |
| 954 */ | 961 */ |
| 955 | 962 |
