<!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" /><style type="text/css"><!--
#msg dl { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer { 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 #fc0 solid; padding: 6px; }
#msg ul, pre { overflow: auto; }
#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>
<title>[BuddyPress] [3034] branches/1.2/bp-core/bp-core-avatars.php:
  Better avatar file handling.</title>
</head>
<body>

<div id="msg">
<dl>
<dt>Revision</dt> <dd>3034</dd>
<dt>Author</dt> <dd>johnjamesjacoby</dd>
<dt>Date</dt> <dd>2010-06-10 20:36:08 +0000 (Thu, 10 Jun 2010)</dd>
</dl>

<h3>Log Message</h3>
<pre>Better avatar file handling. Should improve performance slightly and handle situations where avatars with deprecated file names still exist on the server and cannot be overwritten.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#branches12bpcorebpcoreavatarsphp">branches/1.2/bp-core/bp-core-avatars.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="branches12bpcorebpcoreavatarsphp"></a>
<div class="modfile"><h4>Modified: branches/1.2/bp-core/bp-core-avatars.php (3033 => 3034)</h4>
<pre class="diff"><span>
<span class="info">--- branches/1.2/bp-core/bp-core-avatars.php        2010-06-10 19:43:47 UTC (rev 3033)
+++ branches/1.2/bp-core/bp-core-avatars.php        2010-06-10 20:36:08 UTC (rev 3034)
</span><span class="lines">@@ -116,21 +116,58 @@
</span><span class="cx">          * Set the file names to search for, to select the full size
</span><span class="cx">          * or thumbnail image.
</span><span class="cx">          */
</span><del>-        $avatar_name = ( 'full' == $type ) ? '-bpfull' : '-bpthumb';
</del><ins>+        $avatar_size = ( 'full' == $type ) ? '-bpfull' : '-bpthumb';
</ins><span class="cx">         $legacy_user_avatar_name = ( 'full' == $type ) ? '-avatar2' : '-avatar1';
</span><span class="cx">         $legacy_group_avatar_name = ( 'full' == $type ) ? '-groupavatar-full' : '-groupavatar-thumb';
</span><span class="cx"> 
</span><ins>+        // Check for directory
</ins><span class="cx">         if ( file_exists( $avatar_folder_dir ) ) {
</span><ins>+
+                // Open directory
</ins><span class="cx">                 if ( $av_dir = opendir( $avatar_folder_dir ) ) {
</span><ins>+
+                        // Stash files in an array once to check for one that matches
+                        $avatar_files = array();
</ins><span class="cx">                         while ( false !== ( $avatar_file = readdir($av_dir) ) ) {
</span><del>-                                if ( preg_match( &quot;/{$avatar_name}/&quot;, $avatar_file ) || preg_match( &quot;/{$legacy_user_avatar_name}/&quot;, $avatar_file ) || preg_match( &quot;/{$legacy_group_avatar_name}/&quot;, $avatar_file ) )
-                                        $avatar_url = $avatar_folder_url . '/' . $avatar_file;
</del><ins>+                                // Only add files to the array (skip directories)
+                                if ( 2 &lt; strlen( $avatar_file ) )
+                                        $avatar_files[] = $avatar_file;
</ins><span class="cx">                         }
</span><ins>+
+                        // Check for array
+                        if ( 0 &lt; count( $avatar_files ) ) {
+
+                                // Check for current avatar
+                                foreach( $avatar_files as $key =&gt; $value ) {
+                                        if ( strpos ( $value, $avatar_size )!== false )
+                                                $avatar_url = $avatar_folder_url . '/' . $avatar_files[$key];
+                                }
+
+                                // Legacy avatar check
+                                if ( !isset( $avatar_url ) ) {
+                                        foreach( $avatar_files as $key =&gt; $value ) {
+                                                if ( strpos ( $value, $legacy_user_avatar_name )!== false )
+                                                        $avatar_url = $avatar_folder_url . '/' . $avatar_files[$key];
+                                        }
+
+                                        // Legacy group avatar check
+                                        if ( !isset( $avatar_url ) ) {
+                                                foreach( $avatar_files as $key =&gt; $value ) {
+                                                        if ( strpos ( $value, $legacy_group_avatar_name )!== false )
+                                                                $avatar_url = $avatar_folder_url . '/' . $avatar_files[$key];
+                                                }
+                                        }
+                                }
+                        }
</ins><span class="cx">                 }
</span><del>-                closedir($av_dir);
</del><span class="cx"> 
</span><ins>+                // Close the avatar directory
+                closedir( $av_dir );
+
+                // If we found an avatar, return it wrapped in an img element
</ins><span class="cx">                 if ( $avatar_url )
</span><span class="cx">                         return apply_filters( 'bp_core_fetch_avatar', &quot;&lt;img src='{$avatar_url}' alt='{$alt}' class='{$class}'{$css_id}{$html_width}{$html_height} /&gt;&quot;, $params, $item_id, $avatar_dir, $css_id, $html_width, $html_height, $avatar_folder_url, $avatar_folder_dir );
</span><ins>+
</ins><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         /* If no avatars have been uploaded for this item, display a gravatar */
</span></span></pre>
</div>
</div>

</body>
</html>