Index: sources/reference.inc
===================================================================
--- sources/reference.inc	(revision 176)
+++ sources/reference.inc	(working copy)
@@ -102,12 +102,16 @@
     '#weight' => 100.5,
     '#access' => empty($element['fid']['#value']),
     '#filefield_sources_hint_text' => FILEFIELD_SOURCE_REFERENCE_HINT_TEXT,
+    /**This is necessary to set the file description automatically when pressing the "Select" button*/
+    '#suffix'=>'<input type="hidden" name="'.$element['#field_name']."[{$element['#delta']}][data][description]".'" id="'.$element['#field_name']."_{$element['#delta']}_data_description".'" />',
   );
 
   $element['filefield_reference']['autocomplete'] = array(
     '#type' => 'textfield',
     '#autocomplete_path' => 'filefield/reference/' . $element['#type_name'] . '/' . $element['#field_name'],
     '#description' => filefield_sources_element_validation_help($element['#upload_validators']),
+    /**This is necessary to set the file description automatically when pressing the "Select" button*/
+    '#attributes'=>array('onchange'=>"$('#".$element['#field_name']."_{$element['#delta']}_data_description').val(this.value.indexOf('/')>-1 ? this.value.substr(0,this.value.indexOf('/')) : this.value.substr(0,this.value.indexOf('[')));"),
   );
 
   $element['filefield_reference']['select'] = array(
@@ -193,26 +197,36 @@
  *   Optional. A CCK field array for which to filter returned files.
  */
 function filefield_source_reference_get_files($filename, $field = NULL) {
-  if (!isset($field)) {
-    foreach (content_fields() as $field) {
-      if ($field['type'] == 'filefield') {
-        $fields[] = $field;
-      }
+  /**Get the list of allowed extenstions for the given field*/
+  $allowedExtensions = isset($field['widget']['file_extensions']) ? explode(' ', strtolower($field['widget']['file_extensions'])) : array();
+
+  foreach (content_fields() as $f) {
+    if ($f['type'] == 'filefield') {
+      $fields[] = $f;
     }
   }
-  else {
-    $fields = array($field);
-  }
 
   $files = array();
-  foreach ($fields as $field) {
-    $db_info = content_database_info($field);
+  foreach ($fields as $f) {
+    $db_info = content_database_info($f);
 
     // 1 == contains, 0 == starts with.
     $like = empty($field['widget']['filefield_source_autocomplete']) ? '%s%%' : '%%%s%%';
-    $result = db_query_range("SELECT f.* FROM {" . $db_info['table'] . "} c INNER JOIN {files} f ON c." . $db_info['columns']['fid']['column'] . " = f.fid WHERE f.filename LIKE '" . $like . "' AND f.status = 1 ORDER BY f.timestamp DESC", $filename, 0, 30);
+    $description_like = empty($field['widget']['filefield_source_autocomplete']) ? '%%"%s%%' : '%%%s%%';
+    $result = db_query_range("SELECT f.*,c.* FROM {" . $db_info['table'] . "} c INNER JOIN {files} f ON c." . $db_info['columns']['fid']['column'] . " = f.fid WHERE (f.filename LIKE '" . $like . "' OR " . $db_info['columns']['data']['column'] . " LIKE '" . $description_like . "') AND f.status = 1 ORDER BY f.timestamp DESC", $filename, $filename, 0, 30);
 
     while ($file = db_fetch_object($result)) {
+      
+      /**Skip the not allowed file extensions */
+      $pi = pathinfo($file->filename);
+      if(empty($allowedExtensions) || !in_array(strtolower($pi['extension']), $allowedExtensions)) continue; //skip not-allowed extensions
+      
+      /**Populate the description field if exists and is not empty*/
+      $data = unserialize($file->{$db_info['columns']['data']['column']});
+      if(isset($data['description']) && trim($data['description'])) {
+        $file->filename = $data['description']  . ' / ' . $file->filename;
+      }
+      
       $files[$file->fid] = $file;
     }
   }
