home *** CD-ROM | disk | FTP | other *** search
/ PCGUIA 117 / PC Guia 117.iso / Software / Produtividade / Software2 / Product4 / Setup.exe / drupal-4.6.0 / includes / bootstrap.inc next >
Encoding:
Text File  |  2005-04-05  |  17.2 KB  |  651 lines

  1. <?php
  2. // $Id: bootstrap.inc,v 1.44 2005/04/05 19:00:24 dries Exp $
  3.  
  4. /**
  5.  * @file
  6.  * Functions that need to be loaded on every Drupal request.
  7.  */
  8.  
  9. define('CACHE_PERMANENT', 0);
  10. define('CACHE_TEMPORARY', -1);
  11.  
  12. define('WATCHDOG_NOTICE', 0);
  13. define('WATCHDOG_WARNING', 1);
  14. define('WATCHDOG_ERROR', 2);
  15.  
  16.  
  17. /**
  18.  * Locate the appropriate configuration file.
  19.  *
  20.  * Try finding a matching configuration directory by stripping the
  21.  * website's hostname from left to right and pathname from right to
  22.  * left.  The first configuration file found will be used, the
  23.  * remaining will ignored.  If no configuration file is found,
  24.  * return a default value '$confdir/default'.
  25.  *
  26.  * Example for a fictitious site installed at
  27.  * http://www.drupal.org/mysite/test/ the 'settings.php' is
  28.  * searched in the following directories:
  29.  *
  30.  *  1. $confdir/www.drupal.org.mysite.test
  31.  *  2. $confdir/drupal.org.mysite.test
  32.  *  3. $confdir/org.mysite.test
  33.  *
  34.  *  4. $confdir/www.drupal.org.mysite
  35.  *  5. $confdir/drupal.org.mysite
  36.  *  6. $confdir/org.mysite
  37.  *
  38.  *  7. $confdir/www.drupal.org
  39.  *  8. $confdir/drupal.org
  40.  *  9. $confdir/org
  41.  *
  42.  * 10. $confdir/default
  43.  */
  44. function conf_init() {
  45.   static $conf = '';
  46.  
  47.   if ($conf) {
  48.     return $conf;
  49.   }
  50.  
  51.   $confdir = 'sites';
  52.   $uri = explode('/', $_SERVER['PHP_SELF']);
  53.   $server = explode('.', rtrim($_SERVER['HTTP_HOST'], '.'));
  54.   for ($i = count($uri) - 1; $i > 0; $i--) {
  55.     for ($j = count($server); $j > 0; $j--) {
  56.       $dir = implode('.', array_slice($server, -$j)) . implode('.', array_slice($uri, 0, $i));
  57.       if (file_exists("$confdir/$dir/settings.php")) {
  58.         $conf = "$confdir/$dir";
  59.         return $conf;
  60.       }
  61.     }
  62.   }
  63.   $conf = "$confdir/default";
  64.   return $conf;
  65. }
  66.  
  67. /**
  68.  * Returns and optionally sets the filename for a system item (module,
  69.  * theme, etc.).  The filename, whether provided, cached, or retrieved
  70.  * from the database, is only returned if the file exists.
  71.  *
  72.  * @param $type
  73.  *   The type of the item (i.e. theme, theme_engine, module).
  74.  * @param $name
  75.  *   The name of the item for which the filename is requested.
  76.  * @param $filename
  77.  *   The filename of the item if it is to be set explicitly rather
  78.  *   than by consulting the database.
  79.  *
  80.  * @return
  81.  *   The filename of the requested item.
  82.  */
  83. function drupal_get_filename($type, $name, $filename = NULL) {
  84.   static $files = array();
  85.  
  86.   if (!$files[$type]) {
  87.     $files[$type] = array();
  88.   }
  89.  
  90.   if ($filename && file_exists($filename)) {
  91.     $files[$type][$name] = $filename;
  92.   }
  93.   elseif ($files[$type][$name]) {
  94.     // nothing
  95.   }
  96.   elseif (($file = db_result(db_query("SELECT filename FROM {system} WHERE name = '%s' AND type = '%s'", $name, $type))) && file_exists($file)) {
  97.     $files[$type][$name] = $file;
  98.   }
  99.   else {
  100.     $config = conf_init();
  101.     $dir = (($type == 'theme_engine') ? 'themes/engines' : "${type}s");
  102.     $file = (($type == 'theme_engine') ? "$name.engine" : "$name.$type");
  103.  
  104.     foreach (array("$config/$dir/$file", "$config/$dir/$name/$file", "$dir/$file", "$dir/$name/$file") as $file) {
  105.       if (file_exists($file)) {
  106.         $files[$type][$name] = $file;
  107.         break;
  108.       }
  109.     }
  110.   }
  111.  
  112.   return $files[$type][$name];
  113. }
  114.  
  115. /**
  116.  * Load the persistent variable table.
  117.  *
  118.  * The variable table is composed of values that have been saved in the table
  119.  * with variable_set() as well as those explicitly specified in the configuration
  120.  * file.
  121.  */
  122. function variable_init($conf = array()) {
  123.   // NOTE: caching the variables improves performance with 20% when serving cached pages.
  124.   if ($cached = cache_get('variables')) {
  125.     $variables = unserialize($cached->data);
  126.   }
  127.   else {
  128.     $result = db_query('SELECT * FROM {variable}');
  129.     while ($variable = db_fetch_object($result)) {
  130.       $variables[$variable->name] = unserialize($variable->value);
  131.     }
  132.     cache_set('variables', serialize($variables));
  133.   }
  134.  
  135.   foreach ($conf as $name => $value) {
  136.     $variables[$name] = $value;
  137.   }
  138.  
  139.   return $variables;
  140. }
  141.  
  142. /**
  143.  * Return a persistent variable.
  144.  *
  145.  * @param $name
  146.  *   The name of the variable to return.
  147.  * @param $default
  148.  *   The default value to use if this variable has never been set.
  149.  * @return
  150.  *   The value of the variable.
  151.  */
  152. function variable_get($name, $default) {
  153.   global $conf;
  154.  
  155.   return isset($conf[$name]) ? $conf[$name] : $default;
  156. }
  157.  
  158. /**
  159.  * Set a persistent variable.
  160.  *
  161.  * @param $name
  162.  *   The name of the variable to set.
  163.  * @param $value
  164.  *   The value to set. This can be any PHP data type; these functions take care
  165.  *   of serialization as necessary.
  166.  */
  167. function variable_set($name, $value) {
  168.   global $conf;
  169.  
  170.   db_query("DELETE FROM {variable} WHERE name = '%s'", $name);
  171.   db_query("INSERT INTO {variable} (name, value) VALUES ('%s', '%s')", $name, serialize($value));
  172.   cache_clear_all('variables');
  173.  
  174.   $conf[$name] = $value;
  175. }
  176.  
  177. /**
  178.  * Unset a persistent variable.
  179.  *
  180.  * @param $name
  181.  *   The name of the variable to undefine.
  182.  */
  183. function variable_del($name) {
  184.   global $conf;
  185.  
  186.   db_query("DELETE FROM {variable} WHERE name = '%s'", $name);
  187.   cache_clear_all('variables');
  188.  
  189.   unset($conf[$name]);
  190. }
  191.  
  192. /**
  193.  * Return data from the persistent cache.
  194.  *
  195.  * @param $key
  196.  *   The cache ID of the data to retrieve.
  197.  */
  198. function cache_get($key) {
  199.   $cache = db_fetch_object(db_query("SELECT data, created, headers FROM {cache} WHERE cid = '%s'", $key));
  200.   if (isset($cache->data)) {
  201.     $cache->data = db_decode_blob($cache->data);
  202.     return $cache;
  203.   }
  204.   return 0;
  205. }
  206.  
  207. /**
  208.  * Store data in the persistent cache.
  209.  *
  210.  * @param $cid
  211.  *   The cache ID of the data to store.
  212.  * @param $data
  213.  *   The data to store in the cache. Complex data types must be serialized first.
  214.  * @param $expire
  215.  *   One of the following values:
  216.  *   - CACHE_PERMANENT: Indicates that the item should never be removed unless
  217.  *     explicitly told to using cache_clear_all() with a cache ID.
  218.  *   - CACHE_TEMPORARY: Indicates that the item should be removed at the next
  219.  *     general cache wipe.
  220.  *   - A Unix timestamp: Indicates that the item should be kept at least until
  221.  *     the given time, after which it behaves like CACHE_TEMPORARY.
  222.  * @param $headers
  223.  *   A string containing HTTP header information for cached pages.
  224.  */
  225. function cache_set($cid, $data, $expire = CACHE_PERMANENT, $headers = NULL) {
  226.   $data = db_encode_blob($data);
  227.  
  228.   db_query("UPDATE {cache} SET data = '%s', created = %d, expire = %d, headers = '%s' WHERE cid = '%s'", $data, time(), $expire, $headers, $cid);
  229.   if (!db_affected_rows()) {
  230.     @db_query("INSERT INTO {cache} (cid, data, created, expire, headers) VALUES ('%s', '%s', %d, %d, '%s')", $cid, $data, time(), $expire, $headers);
  231.   }
  232. }
  233.  
  234. /**
  235.  * Expire data from the cache.
  236.  *
  237.  * @param $cid
  238.  *   If set, the cache ID to delete. Otherwise, all cache entries that can expire
  239.  *   are deleted.
  240.  *
  241.  * @param $wildcard
  242.  *   If set to true, the $cid is treated as a substring to match rather than a
  243.  *   complete ID.
  244.  */
  245. function cache_clear_all($cid = NULL, $wildcard = false) {
  246.   if (empty($cid)) {
  247.     db_query("DELETE FROM {cache} WHERE expire != %d AND expire < %d", CACHE_PERMANENT, time());
  248.   }
  249.   else {
  250.     if ($wildcard) {
  251.       db_query("DELETE FROM {cache} WHERE cid LIKE '%%%s%%'", $cid);
  252.     }
  253.     else {
  254.       db_query("DELETE FROM {cache} WHERE cid = '%s'", $cid);
  255.     }
  256.   }
  257. }
  258.  
  259. /**
  260.  * Store the current page in the cache.
  261.  */
  262. function page_set_cache() {
  263.   global $user, $base_url;
  264.  
  265.   if (!$user->uid && $_SERVER['REQUEST_METHOD'] == 'GET') {
  266.     // This will fail in some cases, see page_get_cache() for the explanation.
  267.     if ($data = ob_get_contents()) {
  268.       if (function_exists('gzencode')) {
  269.         if (version_compare(phpversion(), '4.2', '>=')) {
  270.           $data = gzencode($data, 9, FORCE_GZIP);
  271.         }
  272.         else {
  273.           $data = gzencode($data, FORCE_GZIP);
  274.         }
  275.       }
  276.       ob_end_flush();
  277.       cache_set($base_url . request_uri(), $data, CACHE_TEMPORARY, drupal_get_headers());
  278.     }
  279.   }
  280. }
  281.  
  282. /**
  283.  * Retrieve the current page from the cache.
  284.  *
  285.  * Note, we do not serve cached pages when status messages are waiting (from
  286.  * a redirected form submission which was completed).
  287.  * Because the output handler is not activated, the resulting page will not
  288.  * get cached either.
  289.  */
  290. function page_get_cache() {
  291.   global $user, $base_url;
  292.  
  293.   $cache = NULL;
  294.  
  295.   if (!$user->uid && $_SERVER['REQUEST_METHOD'] == 'GET' && count(drupal_set_message()) == 0) {
  296.     $cache = cache_get($base_url . request_uri());
  297.  
  298.     if (empty($cache)) {
  299.       ob_start();
  300.     }
  301.   }
  302.  
  303.   return $cache;
  304. }
  305.  
  306. /**
  307.  * Call all init or exit hooks without including all modules.
  308.  *
  309.  * @param $op
  310.  *   The name of the bootstrap hook we wish to invoke.
  311.  */
  312. function bootstrap_invoke_all($op) {
  313.   foreach (module_list(FALSE, TRUE) as $module) {
  314.     drupal_load('module', $module);
  315.     module_invoke($module, $op);
  316.  }
  317. }
  318.  
  319. /**
  320.  * Includes a file with the provided type and name.  This prevents
  321.  * including a theme, engine, module, etc., more than once.
  322.  *
  323.  * @param $type
  324.  *   The type of item to load (i.e. theme, theme_engine, module).
  325.  * @param $name
  326.  *   The name of the item to load.
  327.  *
  328.  * @return
  329.  *   TRUE if the item is loaded or has already been loaded.
  330.  */
  331. function drupal_load($type, $name) {
  332.   // print $name. '<br />';
  333.   static $files = array();
  334.  
  335.   if ($files[$type][$name]) {
  336.     return TRUE;
  337.   }
  338.  
  339.   $filename = drupal_get_filename($type, $name);
  340.  
  341.   if ($filename) {
  342.     include_once($filename);
  343.     $files[$type][$name] = TRUE;
  344.  
  345.     return TRUE;
  346.   }
  347.  
  348.   return FALSE;
  349. }
  350.  
  351. /**
  352.  * Return an array mapping path aliases to their internal Drupal paths.
  353.  */
  354. function drupal_get_path_map($action = '') {
  355.   static $map = NULL;
  356.  
  357.   if ($action == 'rebuild') {
  358.     $map = NULL;
  359.   }
  360.  
  361.   if (is_null($map)) {
  362.     $map = array();  // Make $map non-null in case no aliases are defined.
  363.     $result = db_query('SELECT * FROM {url_alias}');
  364.     while ($data = db_fetch_object($result)) {
  365.       $map[$data->dst] = $data->src;
  366.     }
  367.   }
  368.  
  369.   return $map;
  370. }
  371.  
  372. /**
  373.  * Given an internal Drupal path, return the alias set by the administrator.
  374.  */
  375. function drupal_get_path_alias($path) {
  376.   if (($map = drupal_get_path_map()) && ($newpath = array_search($path, $map))) {
  377.     return $newpath;
  378.   }
  379.   elseif (function_exists('conf_url_rewrite')) {
  380.     return conf_url_rewrite($path, 'outgoing');
  381.   }
  382.   else {
  383.     // No alias found. Return the normal path.
  384.     return $path;
  385.   }
  386. }
  387.  
  388. /**
  389.  * Get the title of the current page, for display on the page and in the title bar.
  390.  */
  391. function drupal_get_title() {
  392.   $title = drupal_set_title();
  393.  
  394.   if (!isset($title)) {
  395.     // during a bootstrap, menu.inc is not included and thus we cannot provide a title
  396.     if (function_exists('menu_get_active_title')) {
  397.       $title = check_plain(menu_get_active_title());
  398.     }
  399.   }
  400.  
  401.   return $title;
  402. }
  403.  
  404. /**
  405.  * Set the title of the current page, for display on the page and in the title bar.
  406.  */
  407. function drupal_set_title($title = NULL) {
  408.   static $stored_title;
  409.  
  410.   if (isset($title)) {
  411.     $stored_title = $title;
  412.   }
  413.   return $stored_title;
  414. }
  415.  
  416. /**
  417.  * Set HTTP headers in preparation for a page response.
  418.  */
  419. function drupal_page_header() {
  420.   if (variable_get('dev_timer', 0)) {
  421.     timer_start();
  422.   }
  423.  
  424.   if (variable_get('cache', 0)) {
  425.     if ($cache = page_get_cache()) {
  426.       bootstrap_invoke_all('init');
  427.       // Set default values:
  428.       $date = gmdate('D, d M Y H:i:s', $cache->created) .' GMT';
  429.       $etag = '"'. md5($date) .'"';
  430.  
  431.       // Check http headers:
  432.       $modified_since = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? $_SERVER['HTTP_IF_MODIFIED_SINCE'] == $date : NULL;
  433.       if (!empty($_SERVER['HTTP_IF_MODIFIED_SINCE']) && ($timestamp = strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])) != -1) {
  434.         $modified_since = $cache->created <= $timestamp;
  435.       }
  436.       else {
  437.         $modified_since = NULL;
  438.       }
  439.       $none_match = !empty($_SERVER['HTTP_IF_NONE_MATCH']) ? $_SERVER['HTTP_IF_NONE_MATCH'] == $etag : NULL;
  440.  
  441.       // The type checking here is very important, be careful when changing entries.
  442.       if (($modified_since !== NULL || $none_match !== NULL) && $modified_since !== false && $none_match !== false) {
  443.         header('HTTP/1.0 304 Not Modified');
  444.         exit();
  445.       }
  446.  
  447.       // Send appropriate response:
  448.       header("Last-Modified: $date");
  449.       header("ETag: $etag");
  450.  
  451.       // Determine if the browser accepts gzipped data.
  452.       if (@strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') === false && function_exists('gzencode')) {
  453.         // Strip the gzip header and run uncompress.
  454.         $cache->data = gzinflate(substr(substr($cache->data, 10), 0, -8));
  455.       }
  456.       elseif (function_exists('gzencode')) {
  457.         header('Content-Encoding: gzip');
  458.       }
  459.  
  460.       // Send the original request's headers.  We send them one after
  461.       // another so PHP's header() function can deal with duplicate
  462.       // headers.
  463.       $headers = explode("\n", $cache->headers);
  464.       foreach ($headers as $header) {
  465.         header($header);
  466.       }
  467.  
  468.       print $cache->data;
  469.       bootstrap_invoke_all('exit');
  470.       exit();
  471.     }
  472.     else {
  473.       header("Expires: Sun, 19 Nov 1978 05:00:00 GMT");
  474.       header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  475.       header("Cache-Control: no-store, no-cache, must-revalidate");
  476.       header("Cache-Control: post-check=0, pre-check=0", false);
  477.       header("Pragma: no-cache");
  478.     }
  479.   }
  480. }
  481.  
  482. /**
  483.  * Define the critical hooks that force modules to always be loaded.
  484.  */
  485. function bootstrap_hooks() {
  486.   return array('init', 'exit');
  487. }
  488.  
  489. /**
  490.  * Unserializes and appends elements from a serialized string.
  491.  *
  492.  * @param $obj
  493.  *   The object to which the elements are appended.
  494.  * @param $field
  495.  *   The attribute of $obj whose value should be unserialized.
  496.  */
  497. function drupal_unpack($obj, $field = 'data') {
  498.   if ($obj->$field && $data = unserialize($obj->$field)) {
  499.     foreach ($data as $key => $value) {
  500.       if (!isset($obj->$key)) {
  501.         $obj->$key = $value;
  502.       }
  503.     }
  504.   }
  505.   return $obj;
  506. }
  507.  
  508. /**
  509.  * Return the URI of the referring page.
  510.  */
  511. function referer_uri() {
  512.   if (isset($_SERVER['HTTP_REFERER'])) {
  513.     return $_SERVER['HTTP_REFERER'];
  514.   }
  515. }
  516.  
  517. /**
  518.  * Return a component of the current Drupal path.
  519.  *
  520.  * When viewing a page at the path "admin/node/configure", for example, arg(0)
  521.  * would return "admin", arg(1) would return "node", and arg(2) would return
  522.  * "configure".
  523.  *
  524.  * Avoid use of this function where possible, as resulting code is hard to read.
  525.  * Instead, attempt to use named arguments in menu callback functions. See the
  526.  * explanation in menu.inc for how to construct callbacks that take arguments.
  527.  */
  528. function arg($index) {
  529.   static $arguments, $q;
  530.  
  531.   if (empty($arguments) || $q != $_GET['q']) {
  532.     $arguments = explode('/', $_GET['q']);
  533.   }
  534.  
  535.   if (array_key_exists($index, $arguments)) {
  536.     return $arguments[$index];
  537.   }
  538. }
  539.  
  540. /**
  541.  * Prepare a URL for use in an HTML attribute.
  542.  *
  543.  * We replace ( and ) with their url-encoded equivalents to prevent XSS attacks.
  544.  */
  545. function check_url($uri) {
  546.   $uri = htmlspecialchars($uri, ENT_QUOTES);
  547.  
  548.   $uri = strtr($uri, array('(' => '%28', ')' => '%29'));
  549.  
  550.   return $uri;
  551. }
  552.  
  553. /**
  554.  * Since request_uri() is only available on Apache, we generate an
  555.  * equivalent using other environment vars.
  556.  */
  557. function request_uri() {
  558.  
  559.   if (isset($_SERVER['REQUEST_URI'])) {
  560.     $uri = $_SERVER['REQUEST_URI'];
  561.   }
  562.   else {
  563.     if (isset($_SERVER['argv'])) {
  564.       $uri = $_SERVER['PHP_SELF'] .'?'. $_SERVER['argv'][0];
  565.     }
  566.     else {
  567.       $uri = $_SERVER['PHP_SELF'] .'?'. $_SERVER['QUERY_STRING'];
  568.     }
  569.   }
  570.  
  571.   return $uri;
  572. }
  573.  
  574. /**
  575.  * Begin a global timer, for benchmarking of page execution time.
  576.  */
  577. function timer_start() {
  578.   global $timer;
  579.   list($usec, $sec) = explode(' ', microtime());
  580.   $timer = (float)$usec + (float)$sec;
  581. }
  582.  
  583. /**
  584.  * Log a system message.
  585.  *
  586.  * @param $type
  587.  *   The category to which this message belongs.
  588.  * @param $message
  589.  *   The message to store in the log.
  590.  * @param $severity
  591.  *   The severity of the message. One of the following values:
  592.  *   - WATCHDOG_NOTICE
  593.  *   - WATCHDOG_WARNING
  594.  *   - WATCHDOG_ERROR
  595.  * @param $link
  596.  *   A link to associate with the message.
  597.  */
  598. function watchdog($type, $message, $severity = WATCHDOG_NOTICE, $link = NULL) {
  599.   global $user;
  600.   db_query("INSERT INTO {watchdog} (uid, type, message, severity, link, location, hostname, timestamp) VALUES (%d, '%s', '%s', %d, '%s', '%s', '%s', %d)", $user->uid, $type, $message, $severity, $link, request_uri(), $_SERVER['REMOTE_ADDR'], time());
  601. }
  602.  
  603. /**
  604.  * Set a message for the user to see.
  605.  *
  606.  * The message is stored in the session so that it can persist through a redirect.
  607.  *
  608.  * If called with no arguments, this function returns all set messages without
  609.  * clearing them.
  610.  */
  611. function drupal_set_message($message = NULL, $type = 'status') {
  612.   if (isset($message)) {
  613.     if (!isset($_SESSION['messages'])) {
  614.       $_SESSION['messages'] = array();
  615.     }
  616.  
  617.     if (!isset($_SESSION['messages'][$type])) {
  618.       $_SESSION['messages'][$type] = array();
  619.     }
  620.  
  621.     $_SESSION['messages'][$type][] = $message;
  622.   }
  623.  
  624.   return $_SESSION['messages'];
  625. }
  626.  
  627. /**
  628.  * Return all messages that have been set.
  629.  *
  630.  * As a side effect, this function clears the message queue.
  631.  */
  632. function drupal_get_messages() {
  633.   $messages = drupal_set_message();
  634.   $_SESSION['messages'] = array();
  635.  
  636.   return $messages;
  637. }
  638.  
  639. unset($conf);
  640. $config = conf_init();
  641.  
  642. include_once "$config/settings.php";
  643. include_once 'includes/database.inc';
  644. include_once 'includes/session.inc';
  645. include_once 'includes/module.inc';
  646.  
  647. // Initialize configuration variables, using values from conf.php if available.
  648. $conf = variable_init(isset($conf) ? $conf : array());
  649.  
  650. ?>
  651.