<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[27158] trunk/src: Detect and handle symlinking of plugins in plugin_basename().</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://core.trac.wordpress.org/changeset/27158">27158</a></dd>
<dt>Author</dt> <dd>nacin</dd>
<dt>Date</dt> <dd>2014-02-10 22:59:40 +0000 (Mon, 10 Feb 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>Detect and handle symlinking of plugins in plugin_basename().

props rmccue, MikeSchinkel, jdgrimes.
see <a href="http://core.trac.wordpress.org/ticket/16953">#16953</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpadminincludespluginphp">trunk/src/wp-admin/includes/plugin.php</a></li>
<li><a href="#trunksrcwpadminpluginsphp">trunk/src/wp-admin/plugins.php</a></li>
<li><a href="#trunksrcwpadminupdatephp">trunk/src/wp-admin/update.php</a></li>
<li><a href="#trunksrcwpincludesfunctionsphp">trunk/src/wp-includes/functions.php</a></li>
<li><a href="#trunksrcwpincludespluginphp">trunk/src/wp-includes/plugin.php</a></li>
<li><a href="#trunksrcwpsettingsphp">trunk/src/wp-settings.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpadminincludespluginphp"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-admin/includes/plugin.php (27157 => 27158)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-admin/includes/plugin.php   2014-02-10 04:06:36 UTC (rev 27157)
+++ trunk/src/wp-admin/includes/plugin.php      2014-02-10 22:59:40 UTC (rev 27158)
</span><span class="lines">@@ -537,7 +537,8 @@
</span><span class="cx">          if ( !empty($redirect) )
</span><span class="cx">                  wp_redirect(add_query_arg('_error_nonce', wp_create_nonce('plugin-activation-error_' . $plugin), $redirect)); // we'll override this later if the plugin can be included without fatal error
</span><span class="cx">          ob_start();
</span><del>-               include_once(WP_PLUGIN_DIR . '/' . $plugin);
</del><ins>+                wp_register_plugin_realpath( WP_PLUGIN_DIR . '/' . $plugin );
+               include_once( WP_PLUGIN_DIR . '/' . $plugin );
</ins><span class="cx"> 
</span><span class="cx">          if ( ! $silent ) {
</span><span class="cx">                  /**
</span><span class="lines">@@ -921,6 +922,7 @@
</span><span class="cx">          unset($uninstallable_plugins);
</span><span class="cx"> 
</span><span class="cx">          define('WP_UNINSTALL_PLUGIN', $file);
</span><ins>+               wp_register_plugin_realpath( WP_PLUGIN_DIR . '/' . dirname( $file ) );
</ins><span class="cx">           include WP_PLUGIN_DIR . '/' . dirname($file) . '/uninstall.php';
</span><span class="cx"> 
</span><span class="cx">          return true;
</span><span class="lines">@@ -932,6 +934,7 @@
</span><span class="cx">          update_option('uninstall_plugins', $uninstallable_plugins);
</span><span class="cx">          unset($uninstallable_plugins);
</span><span class="cx"> 
</span><ins>+               wp_register_plugin_realpath( WP_PLUGIN_DIR . '/' . $file );
</ins><span class="cx">           include WP_PLUGIN_DIR . '/' . $file;
</span><span class="cx"> 
</span><span class="cx">          add_action( 'uninstall_' . $file, $callable );
</span></span></pre></div>
<a id="trunksrcwpadminpluginsphp"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-admin/plugins.php (27157 => 27158)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-admin/plugins.php   2014-02-10 04:06:36 UTC (rev 27157)
+++ trunk/src/wp-admin/plugins.php      2014-02-10 22:59:40 UTC (rev 27158)
</span><span class="lines">@@ -143,6 +143,7 @@
</span><span class="cx">                  @ini_set('display_errors', true); //Ensure that Fatal errors are displayed.
</span><span class="cx">                  // Go back to "sandbox" scope so we get the same errors as before
</span><span class="cx">                  function plugin_sandbox_scrape( $plugin ) {
</span><ins>+                               wp_register_plugin_realpath( WP_PLUGIN_DIR . '/' . $plugin );
</ins><span class="cx">                           include( WP_PLUGIN_DIR . '/' . $plugin );
</span><span class="cx">                  }
</span><span class="cx">                  plugin_sandbox_scrape( $plugin );
</span></span></pre></div>
<a id="trunksrcwpadminupdatephp"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-admin/update.php (27157 => 27158)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-admin/update.php    2014-02-10 04:06:36 UTC (rev 27157)
+++ trunk/src/wp-admin/update.php       2014-02-10 22:59:40 UTC (rev 27158)
</span><span class="lines">@@ -84,7 +84,8 @@
</span><span class="cx"> 
</span><span class="cx">                  error_reporting( E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_ERROR | E_WARNING | E_PARSE | E_USER_ERROR | E_USER_WARNING | E_RECOVERABLE_ERROR );
</span><span class="cx">                  @ini_set('display_errors', true); //Ensure that Fatal errors are displayed.
</span><del>-                       include(WP_PLUGIN_DIR . '/' . $plugin);
</del><ins>+                        wp_register_plugin_realpath( WP_PLUGIN_DIR . '/' . $plugin );
+                       include( WP_PLUGIN_DIR . '/' . $plugin );
</ins><span class="cx">           }
</span><span class="cx">          iframe_footer();
</span><span class="cx">  } elseif ( 'install-plugin' == $action ) {
</span></span></pre></div>
<a id="trunksrcwpincludesfunctionsphp"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-includes/functions.php (27157 => 27158)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-includes/functions.php      2014-02-10 04:06:36 UTC (rev 27157)
+++ trunk/src/wp-includes/functions.php 2014-02-10 22:59:40 UTC (rev 27158)
</span><span class="lines">@@ -1434,6 +1434,23 @@
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /**
</span><ins>+ * Normalize a filesystem path.
+ *
+ * Replaces backslashes with forward slashes for Windows systems,
+ * and ensures no duplicate slashes exist.
+ *
+ * @since 3.9.0
+ *
+ * @param string $path Path to normalize.
+ * @return string Normalized path.
+ */
+function wp_normalize_path( $path ) {
+       $path = str_replace( '\\', '/', $path );
+       $path = preg_replace( '|/+|','/', $path );
+       return $path;
+}
+
+/**
</ins><span class="cx">  * Determines a writable directory for temporary files.
</span><span class="cx">  * Function's preference is the return value of <code>sys_get_temp_dir()</code>,
</span><span class="cx">  * followed by your PHP temporary upload directory, followed by WP_CONTENT_DIR,
</span></span></pre></div>
<a id="trunksrcwpincludespluginphp"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-includes/plugin.php (27157 => 27158)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-includes/plugin.php 2014-02-10 04:06:36 UTC (rev 27157)
+++ trunk/src/wp-includes/plugin.php    2014-02-10 22:59:40 UTC (rev 27158)
</span><span class="lines">@@ -592,19 +592,43 @@
</span><span class="cx">  * @return string The name of a plugin.
</span><span class="cx">  * @uses WP_PLUGIN_DIR
</span><span class="cx">  */
</span><del>-function plugin_basename($file) {
-       $file = str_replace('\\','/',$file); // sanitize for Win32 installs
-       $file = preg_replace('|/+|','/', $file); // remove any duplicate slash
-       $plugin_dir = str_replace('\\','/',WP_PLUGIN_DIR); // sanitize for Win32 installs
-       $plugin_dir = preg_replace('|/+|','/', $plugin_dir); // remove any duplicate slash
-       $mu_plugin_dir = str_replace('\\','/',WPMU_PLUGIN_DIR); // sanitize for Win32 installs
-       $mu_plugin_dir = preg_replace('|/+|','/', $mu_plugin_dir); // remove any duplicate slash
</del><ins>+function plugin_basename( $file ) {
+       global $wp_plugin_paths;
+
+       foreach ( $wp_plugin_paths as $dir => $realdir ) {
+               if ( strpos( $file, $realdir ) === 0 ) {
+                       $file = $dir . substr( $file, strlen( $realdir ) );
+               }
+       }
+
+       $file = wp_normalize_path( $file );
+       $plugin_dir = wp_normalize_path( WP_PLUGIN_DIR );
+       $mu_plugin_dir = wp_normalize_path( WPMU_PLUGIN_DIR );
+
</ins><span class="cx">   $file = preg_replace('#^' . preg_quote($plugin_dir, '#') . '/|^' . preg_quote($mu_plugin_dir, '#') . '/#','',$file); // get relative path from plugins dir
</span><span class="cx">  $file = trim($file, '/');
</span><span class="cx">  return $file;
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /**
</span><ins>+ * Register a plugin's real path.
+ *
+ * This is used in {@see plugin_basename()} to resolve symlinked paths.
+ *
+ * @param string $file Known path to the file.
+ */
+function wp_register_plugin_realpath( $file ) {
+       global $wp_plugin_paths;
+
+       $plugin_path = wp_normalize_path( dirname( $file ) );
+       $plugin_realpath = wp_normalize_path( dirname( realpath( $file ) ) );
+
+       if ( $plugin_path !== $plugin_realpath ) {
+               $wp_plugin_paths[ $plugin_path ] = $plugin_realpath;
+       }
+}
+
+/**
</ins><span class="cx">  * Gets the filesystem directory path (with trailing slash) for the plugin __FILE__ passed in
</span><span class="cx">  * @package WordPress
</span><span class="cx">  * @subpackage Plugin
</span></span></pre></div>
<a id="trunksrcwpsettingsphp"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-settings.php (27157 => 27158)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-settings.php        2014-02-10 04:06:36 UTC (rev 27157)
+++ trunk/src/wp-settings.php   2014-02-10 22:59:40 UTC (rev 27158)
</span><span class="lines">@@ -164,8 +164,11 @@
</span><span class="cx"> // Define must-use plugin directory constants, which may be overridden in the sunrise.php drop-in.
</span><span class="cx"> wp_plugin_directory_constants();
</span><span class="cx"> 
</span><ins>+$GLOBALS['wp_plugin_paths'] = array();
+
</ins><span class="cx"> // Load must-use plugins.
</span><span class="cx"> foreach ( wp_get_mu_plugins() as $mu_plugin ) {
</span><ins>+       wp_register_plugin_realpath( $mu_plugin );
</ins><span class="cx">   include_once( $mu_plugin );
</span><span class="cx"> }
</span><span class="cx"> unset( $mu_plugin );
</span><span class="lines">@@ -173,6 +176,7 @@
</span><span class="cx"> // Load network activated plugins.
</span><span class="cx"> if ( is_multisite() ) {
</span><span class="cx">  foreach( wp_get_active_network_plugins() as $network_plugin ) {
</span><ins>+               wp_register_plugin_realpath( $network_plugin );
</ins><span class="cx">           include_once( $network_plugin );
</span><span class="cx">  }
</span><span class="cx">  unset( $network_plugin );
</span><span class="lines">@@ -206,8 +210,10 @@
</span><span class="cx"> register_theme_directory( get_theme_root() );
</span><span class="cx"> 
</span><span class="cx"> // Load active plugins.
</span><del>-foreach ( wp_get_active_and_valid_plugins() as $plugin )
</del><ins>+foreach ( wp_get_active_and_valid_plugins() as $plugin ) {
+       wp_register_plugin_realpath( $plugin );
</ins><span class="cx">   include_once( $plugin );
</span><ins>+}
</ins><span class="cx"> unset( $plugin );
</span><span class="cx"> 
</span><span class="cx"> // Load pluggable functions.
</span></span></pre>
</div>
</div>

</body>
</html>