--- acidfree.module.org	2008-12-15 14:48:04.000000000 +0000
+++ acidfree.module	2009-09-24 17:29:47.243164000 +0100
@@ -1,22 +1,22 @@
 <?php
 /* ex: set syntax=php tabstop=4 expandtab shiftwidth=4 softtabstop=4: */
 /*
-Acidfree Photo Albums for Drupal
-Copyright (C) 2005, 2006, 2007, 2008 Vernon Mauery
+    Acidfree Photo Albums for Drupal
+    Copyright (C) 2005, 2006, 2007, 2008 Vernon Mauery
 
-This program is free software; you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 2 of the License, or
-(at your option) any later version.
-
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-GNU General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */
 
 /**
@@ -385,10 +385,12 @@
             variable_get('acidfree_show_alternate_view', true) &&
             $view != $node->view;
 }
+
 function acidfree_mass_import_access($node) {
-    return node_access('update', $node) || 
-        (node_access('view', $node) && $node->share);
+  return (node_access('update', $node) || //Pachkov.Sergey
+        (node_access('view', $node) && $node->share)) && user_access('acidfree mass import');   //Pachkov.Sergey
 }
+
 function acidfree_menu() {
     $items = array();
     $items['admin/settings/acidfree'] = array(
@@ -411,8 +413,10 @@
             'page callback' => 'acidfree_page',
             'page arguments' => array('per_user_album', 1),
             'title' => 'Acidfree albums',
-            'access callback' => '_acidfree_access_per_user_album',
-            'access arguments' => array(1),
+            'access callback' => 'user_access',             //Pachkov.Sergey
+            'access arguments' => array('access content'),  //Pachkov.Sergey
+            //'access callback' => '_acidfree_access_per_user_album',   //Old CODE
+            //'access arguments' => array(1),
             'type' => MENU_SUGGESTED_ITEM,
         );
     $items['acidfree/thumbnail/%acidfree_node'] = array(
@@ -431,7 +435,7 @@
             'access arguments' => array(1),
         );
 */
-    foreach (array('list'=>'List view', 'grid'=>'Grid view') as $k=>$v) {
+    foreach (array('list' => 'List view', 'grid' => 'Grid view') as $k => $v) {
         $items["node/%acidfree_album/$k"] = array(
                 'page callback' => 'acidfree_album_view',
                 'page arguments' => array(1, "album_{$k}_view"),
@@ -442,64 +446,50 @@
             );
     }
     $items['node/%acidfree_album/import'] = array(
-            'access callback' => 'acidfree_mass_import_access',
-            'access arguments' => array(1),
             'title' => 'Mass import',
             'page callback' => 'drupal_get_form',
             'page arguments' => array('acidfree_mass_import', 1),
+            'access callback' => 'acidfree_mass_import_access',
+            'access arguments' => array(1),
             'type' => variable_get('acidfree_mass_import_local_task', false) ? 
                 MENU_LOCAL_TASK : MENU_SUGGESTED_ITEM,
         );
-    return $items;
-    /*
-    } else {
 
-        $path = explode('/', $_GET['q']);
-        if ($path[0] == 'node' && is_numeric($path[1])) {
-            $album = acidfree_node_load($path[1]);
-            if ($album && $album->type == 'acidfree') {
-                $items[] = Array('path' => "node/{$path[1]}/contents",
-                        'access' => node_access('update', $album) && user_access('use album contents mode'),
-                        'title' => t('Album contents'),
-                        'callback' => 'drupal_get_form',
-                        'callback arguments' => array('acidfree_album_contents', $album->tid),
-                        'type' => MENU_LOCAL_TASK);
-                if (variable_get('acidfree_show_alternate_view', true)) {
-                    $other_view = array('grid' => 'list', 'list' => 'grid');
-                    $view_name = array('grid' => t('Grid'), 'list' => t('List'));
-                    $view = $other_view[$album->view];
-                    $items[] = Array('path' => "node/{$path[1]}/{$view}",
-                            'access' => 1,
-                            'title' => $view_name[$view],
-                            'callback' => 'acidfree_album_view',
-                            'callback arguments' => array("album_{$view}_view", $album->tid),
-                            'type' => MENU_LOCAL_TASK,
-                            'weight' => -4,
-                        );
-                }
-                if ($album->share || node_access('update', $album) ||
-                        user_access('can upload to any album')) {
-                    $items[] = array('path' => "node/{$path[1]}/import",
-                            'access' => user_access('acidfree mass import'),
-                            'title' => t('Mass import'),
-                            'callback' => 'acidfree_mass_import_form',
-                            'callback arguments' => $album->nid,
-                            'type' => variable_get('acidfree_mass_import_local_task', false) ? 
-                                MENU_LOCAL_TASK : MENU_SUGGESTED_ITEM,
-                            'weight' => 5,
-                        );
-                }
-            }
-        }
-        if (variable_get('acidfree_per_user_albums', false)) {
-            $items[] = array('path' => 'user/acidfree', 'title' => t('My Acidfree Albums'),
-                    'access' => user_access('get a per-user album'),
-                    'callback' => 'acidfree_page',
-                    'type' => MENU_SUGGESTED_ITEM,
-                );
-        }
-    }
-    */
+    $items["node/%/contents"] = Array(
+            'title' => t('Album contents'),
+            'callback' => 'drupal_get_form',
+            'callback arguments' => array('acidfree_album_contents', $album->tid),
+            'access arguments' => array('use album contents mode'),
+            'type' => MENU_LOCAL_TASK
+    );
+    
+    $other_view = array('grid' => 'list', 'list' => 'grid');
+    $view_name = array('grid' => t('Grid'), 'list' => t('List'));
+    $view = $other_view[$album->view];
+    $items["node/%/{$view}"] = Array(
+        'title' => $view_name[$view],
+        'page callback' => 'acidfree_album_view',
+        'page arguments' => array("album_{$view}_view", $album->tid),
+        'access arguments' => array(1), // FIXME?: if (variable_get('acidfree_show_alternate_view', true)) {
+        'type' => MENU_LOCAL_TASK,
+        'weight' => -4,
+    );
+    $items["node/%/import"] = array(
+        'title' => t('Mass import'),
+        'page callback' => 'acidfree_mass_import_form',
+        'page arguments' => $album->nid,
+        'access arguments' => array('can upload to any album'), // FIXME: AND array('acidfree mass import') AND ($album->share || node_access('update', $album)
+        'type' => variable_get('acidfree_mass_import_local_task', false) ?  MENU_LOCAL_TASK : MENU_SUGGESTED_ITEM,
+        'weight' => 5,
+    );
+    $items['user/%/edit/acidfree'] = array(
+        'title' => t('My Albums'),
+        'page callback' => 'acidfree_page',
+        'access arguments' => array('get a per-user album'), // FIXME: if (variable_get('acidfree_per_user_albums', FALSE))
+        'type' => MENU_SUGGESTED_ITEM | MENU_LOCAL_TASK,
+        'file' => 'user.pages.inc',
+        'file path' => drupal_get_path('module', 'user'),
+    );
     return $items;
 }
 
@@ -1359,9 +1349,11 @@
     $argc = func_num_args();
     $argv = func_get_args();
     $path = _acidfree_get_path(true);
-    drupal_add_js(drupal_get_path('module', 'acidfree').'/acidfree.js');
-    if ($path == 'user/acidfree' && user_access('get a per-user album') &&
-            variable_get('acidfree_per_user_albums', 0)) {
+    $argv[1] = $argv[1]->uid;
+    drupal_add_js(drupal_get_path('module', 'acidfree') . '/acidfree.js');
+    if (arg(0) == 'user' && arg(2) == 'edit' && arg(3) == 'acidfree' 
+        && user_access('get a per-user album') 
+        && variable_get('acidfree_per_user_albums', 0)) {
         global $user;
         $tid = acidfree_get_root($user->uid, true);
         if (!$tid)
@@ -1383,16 +1375,16 @@
     } elseif ($argv[0] == 'per-user-albums' &&
         variable_get('acidfree_per_user_albums', 0)) {
         $output = acidfree_list_per_user_albums();
-    } else if ($argc == 2 && $argv[0] == 'user' && is_numeric($argv[1])) {
-        $tid = acidfree_get_root($argv[1]);
-        if (!$node)
-            return drupal_not_found();
-        $nid = _nid_from_tid($tid);
-        drupal_goto("node/{$nid}");
+    } else if ($argc == 2 && $argv[0] == 'per_user_album' && is_numeric($argv[1])) {
+        $result = db_query("SELECT MIN(nid) FROM {node} WHERE uid = %d AND type = '%s'", $argv[1], 'acidfree');
+        $min_nid = 0;
+        $min_nid = (array)db_fetch_object($result);
+        $min_nid = $min_nid['MIN(nid)'];
+        if ($min_nid == 0) return drupal_not_found();
+        drupal_goto("node/{$min_nid}");
     } else {
         drupal_not_found();
-        //gotto(__LINE__);
-        return;
+        exit();
     }
     return $output;
 }
@@ -1402,6 +1394,7 @@
     if ($reset) $recursion_level = 0;
     if ($recursion_level++ > 2) {
         //trace();
+        drupal_set_message(t('Recursion exceeded.'), 'error');
         exit(1);
     }
     if ($title === NULL) {
@@ -1442,7 +1435,7 @@
     return $node->tid;
 }
 
-function acidfree_get_root($uid=-1, $create=false, $reset=true) {
+function acidfree_get_root($uid = -1, $create = FALSE, $reset = TRUE) {
     static $recursion_level = 0;
     static $vocab_id = -1;
     static $root_term_id = -1;
@@ -1669,7 +1662,6 @@
         $file->source = 'acidfree_local_filesystem';
         $file->filepath = $fullpath;
         $file->filename = $file->basename;
-
         // echo "importing file $filename<br>\n";
         if ($recursive && is_dir($fullpath)) {
             $dirs[] = $file;
@@ -1770,7 +1762,7 @@
 function _acidfree_valid_file_extensions() {
     static $valid = NULL;
     if ($valid == NULL) {
-        $ext = array('gz', 'bz2', 'zip');
+        $ext = array('zip', 'bz2', 'gz');
         $others = module_invoke_all('af_valid_extensions');
         $valid = array_merge($ext, $others);
     }
@@ -1794,7 +1786,7 @@
         }
     }
     $parent = array_values($form['parent']);
-    $parent = $parent[0];
+    $parent = $parent[0]['#default_value']; //Pachkov.Sergey
     $p = _album_from_tid($parent);
     if ($op == t('Import')) {
         $tmpdir = $form['serverpath'];
@@ -1806,10 +1798,11 @@
             _acidfree_import_dir($p, $tmpdir, $recursive, $node_items);
         }
     } elseif ($op == t('Upload')) {
-        $validators = array('file_validate_extensions' => _acidfree_valid_file_extensions());
+        //Pachkov.Sergey        //$validators = array('file_validate_extensions' => _acidfree_valid_file_extensions()); 
         for ($i=0; $i<5; $i++) {
-            if ($form["acidfreeupload{$i}name"]) {
-                if ($file = file_save_upload("acidfreeupload$i", $validators)) {
+            if ($form['#post']["acidfreeupload{$i}name"]) { 
+                //if ($file = file_save_upload("acidfreeupload$i", $validators)) {    //Original
+                if ($file = file_save_upload("acidfreeupload$i")) {   //Pachkov.Sergey
                     drupal_set_message(t('Importing file %filename',
                         array('%filename' => $file->filename)));
                     _acidfree_handle_uploaded_file($file, $p, $node_items);
@@ -1884,10 +1877,11 @@
             $types[] = $type->class;
         }
     }
-    if (!$types) {
-        $form['nothing'] = array('#value' => t('No node types available'));
-        return $form;
-    }
+    //Pachkov.Sergey
+    //if (!$types) { 
+        //$form['nothing'] = array('#value' => t('No node types available'));
+        //return $form;
+    //}
     $vocabs = array();
     $node = new stdClass();
     $form['#id'] = 'node-form';
@@ -1993,7 +1987,7 @@
 function _acidfree_filter_tag($matches) {
     $node = acidfree_node_load($matches[1]);
     if (!$node) {
-        return "<span style='color: #f00;'>{$matches[0]}</span>";
+        return "<span style='color: #f00;'>{$matches[0]}</span>"; // FIXME: convert to theme function
     } else {
         $args = preg_replace("/[ \\r\\n\\t]+/", ' ', $matches[2]);
         $match_count = preg_match_all("/([-\w]+)=(([^'\" ]+)|'([^']*)'|\"([^\"]*)\")/i", $args, $pairs);
@@ -2030,7 +2024,7 @@
             $pnid = _nid_from_tid($pterm->tid);
             $settings['link'] = 'node/'.$pnid;
         }
-        $ret = "<span class='filter-thumb' style='float: {$settings['align']};'>";
+        $ret = "<span class='filter-thumb' style='float: {$settings['align']};'>"; // FIXME: convert to theme function
         if ($settings['type'] != 'slide') {
             $size = explode('x', $settings['size']);
             $width = $size[0]; $height = ($size[1] ? $size[1] : $size[0]);
@@ -2076,7 +2070,7 @@
             $ret .= theme("acidfree_print_thumb_{$node->type}", $node);
         }
         if ($settings['title']) {
-            $ret .= "<div class='filter-thumb-title'>{$settings['title']}</div>";
+            $ret .= "<div class='filter-thumb-title'>{$settings['title']}</div>"; // FIXME: convert to theme function
         }
         $ret .= "</span>";
         return $ret;
@@ -2159,7 +2153,7 @@
     } else {
         $parent = $node;
     }
-    $tid = 1; // $parent->tid
+    $tid = $parent->tid;   //$tid = 1; // $parent->tid
     $parents = array_reverse(taxonomy_get_parents_all($tid));
     return $parents;
 }
@@ -2183,7 +2177,6 @@
             //gotto(__LINE__);
             exit(1);
         }
-drupal_set_message("REDIRECT: album");
         $p = "pid={$node->nid}";
         $pager = _acidfree_make_pager_query();
         $nid = $child->nid;
@@ -2213,7 +2206,7 @@
        */
     foreach ($order[0] as $k => $ltgt) { break; }
     $ltgt = $ltgt == 'ASC' ? '<' : '>';
-    $query = "SELECT COUNT(n.nid) FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid WHERE n.type <> '%s' AND tn.tid = %d AND (n.moderate = 0 AND n.status = 1) AND " . _acidfree_filter_clause($order, 'n') . " $ltgt '" . _acidfree_filter_clause($order, $node) . "' and n.nid <> %d";
+    $query = "SELECT COUNT(n.nid) FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid WHERE n.type <> '%s' AND tn.tid = %d AND (n.moderate = 0 AND n.status = 1) AND " . _acidfree_filter_clause($order, 'n') . " $ltgt '" . _acidfree_filter_clause($order, $node) . "' and n.nid <> %d"; // FIXME: convert variables into query arguments
     $args = array('acidfree', $parent, $node->nid);
 
     $query = db_rewrite_sql($query);
@@ -2262,18 +2255,12 @@
  * @return
  *    a pager string in the form from=a,b,c,d,e
  */
-function _acidfree_make_pager_query($values=array()) {
+function _acidfree_make_pager_query($values = array()) {
     return PAGER_STRING.'='._acidfree_make_pager_string($values);
 }
 
-function _acidfree_get_path($string=false) {
-
-    $menu = menu_get_active_trail();
-    $item = array_pop($menu);
-    $path = $item['href'];
-    if ($string)
-        return $path;
-    return explode('/', $path);
+function _acidfree_get_path($string = FALSE) {
+    return arg(); // TODO: this function should be removed
 }
 
 function _acidfree_get_parent_from_path() {
@@ -2287,8 +2274,7 @@
     return false;
 }
 
-
-function mkdir_rec($dir, $mode=0770) {
+function mkdir_rec($dir, $mode = 0770) {
     if (version_compare(PHP_VERSION, "5.0.0", ">=")) {
         return mkdir($dir, $mode, true);
     } else {
@@ -2300,6 +2286,7 @@
     }
     return true;
 }
+
 function rmdir_rec($path) {
     $dir = opendir($path);
     while ($file = readdir($dir)) {
@@ -2334,12 +2321,14 @@
  * that acidfree knows about.
  */
 function _acidfree_file_mime($f) {
-    if (ini_get('safe_mode') || !function_exists('shell_exec'))
+    if (ini_get('safe_mode') || !function_exists('shell_exec')) {
         return 'safe_mode';
+    }
     $f = escapeshellarg($f);
     $ret = explode(' ', trim( `file -biN $f` ));
     return $ret[count($ret)-1];
 }
+
 function _acidfree_finfo_file($fname) {
     if (!function_exists('finfo_open'))
         return 'no_finfo';
@@ -2465,20 +2454,27 @@
  * Implementation of hook_user
  */
 function acidfree_user($op, &$edit, &$account, $category = NULL) {
-    if ($op == 'view' && variable_get('acidfree_per_user_albums', 0)
-            && user_access('get a per-user album', $account)) {
-        $album = _album_from_tid($account->acidfree_album);
-        if (!$album) {
-            user_save($account, array('acidfree_album' => null));
-            return null;
-        }
-        $items['acidfree-albums'] = array(
-                'title' => t('Personal album'),
-                'value' => l($album->title, "node/{$album->nid}"),
-                'class' => 'acidfree-albums',
-            );
-        return array(t('Acidfree Albums') => $items);
+    if ($op == 'view') {
+    //Pachkov.Sergey.START
+      $result = db_query("SELECT nid, title FROM node WHERE nid IN (SELECT MIN(nid) FROM node WHERE uid=%d AND type='acidfree')", $account->uid);
+      $min_nid = 0;
+      $min_nid = (array)db_fetch_object($result);
+      $title_node = $min_nid['title'];
+      $min_nid = $min_nid['nid'];
+      if ($min_nid > 0) {
+        $account->content['acidfree-albums'] = array(
+          '#type' => 'user_profile_category',
+          '#title' => t('Foto album'),
+        );
+        $account->content['acidfree-albums']['album'] = array(
+                '#title' => t('Personal album'),
+                '#value' => l($title_node, "node/{$min_nid}"),
+                '#class' => 'acidfree-albums',
+                '#type' => 'user_profile_item',
+        );
+      }
     }
+    //Pachkov.Sergey.END
 }
 
 function acidfree_list_per_user_albums() {
