gallery_base.inc 6.46 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
<?php
// $Id$

/**
 * gallery.module : gallery_base.inc
 * Base functions
 */
 
/* --------------------------------------------------------------------------
 * Gallery init and misc functions
 * --------------------------------------------------------------------------
 */
 
/**
 * fetch a galleryEmbed object
 */
function _gallery_init($full = false, $vars = null) {
  global $user;
  
  if (!$vars) {
    $embedPath = variable_get('gallery_dir', './gallery2/') . 'embed.php';
    $g2Uri =  variable_get('gallery_uri', '/gallery2/');
    $embedUri = variable_get('gallery_embed_uri', '?q=gallery'); 
    $gallery_valid = variable_get('gallery_valid', 0);
  } else {
    $embedPath = $vars['gallery_dir'] . 'embed.php';
    $g2Uri = $vars['gallery_uri'];
    $embedUri = $vars['gallery_embed_uri']; 
    $gallery_valid = $vars['gallery_valid']; 
  }

  if (!$gallery_valid) {
    // It's probably now bad practice to return a null here, given that $ret=null is a gallery2
    // success response
    return array(false, null);
  }

  include_once($embedPath);
 
  $new_user_needed = false;
  $active_id = ($user->uid>0) ? $user->uid : '';
  $params = array('embedUri' => $embedUri,
                  'g2Uri' => $g2Uri,
                  'loginRedirect' => url('user/login', null, null, true),
                  'activeUserId' => $active_id,
                  'activeLanguage' => gallery_get_language($user),
                  'fullInit' => $full);

  $ret = GalleryEmbed::init($params);
  if (!$ret) {
    // No error returned, but it is still possible that the ExternalID mapping has not been done
    $ret2 = GalleryEmbed::isExternalIdMapped($user->uid, 'GalleryUser');
    if ($ret2 && ($ret2->getErrorCode() & ERROR_MISSING_OBJECT)) {
      // Need to make a new user, but don't try to map anonymous user.
      $new_user_needed = ($user->uid>0);
    }    
  } 
  if (($new_user_needed) || ($ret && ($ret->getErrorCode() & ERROR_MISSING_OBJECT))) {
    // Our user mapping is missing.  Make a mapping, or create a new user.
    $g2_user = null;
    // Get the G2 user that matches the Drupal username
    list ($ret, $g2_user) = GalleryCoreApi::fetchUserByUsername($user->name);
    if ($ret && !($ret->getErrorCode() & ERROR_MISSING_OBJECT)) {
      return array(false, $ret);
    }
    if (!isset($g2_user)) {
      // No G2 user with a matching username.  If this is the admin user, we're going to
      // try a little harder and match it to the oldest admin in G2.
      if ($user->uid == 1) {
        list ($ret, $admin_group_id) = GalleryCoreApi::getPluginParameter('module', 'core', 'id.adminGroup');
        if ($ret) {
          return array(false, $ret);
        }
        list ($ret, $g2_users) = GalleryCoreApi::fetchUsersForGroup($admin_group_id);
        if ($ret) {
          return array(false, $ret);
        }
        $keys = array_keys($g2_users);
        $g2_user_name = $g2_users[$keys[0]];
        list ($ret, $g2_user) = GalleryCoreApi::fetchUserByUsername($g2_user_name);
        if ($ret) {
          return array(false, $ret);
        }
      }
    }
    
    if (isset($g2_user)) {
      // The G2 user was found so add to the External ID Map
      $ret = GalleryEmbed::addExternalIdMapEntry($user->uid, $g2_user->getId(), 'GalleryUser');
      if ($ret) {
        return array(false, $ret);
      }
    } else {
      // No matching G2 user found -- create one. 
95 96
      $path = drupal_get_path('module', 'gallery');
      require_once($path . '/gallery_user.inc');
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
      return gallery_modify_user($user, 'create');
    }
  }
  return array(true, null);
}

/**
 * Include head information with check made for uniqueness (see drupal_add_js)
 */
function gallery_set_html_head($info, $unique=true) {
  static $sent = array(); 
  if ($unique) {
  // Only set html head if this info has not been sent before. 
    $hash_info = md5($info);
    if (!isset($sent[$hash_info])) {
      drupal_set_html_head($info);
      $sent[$hash_info] = true;
    } 
  } else {
    drupal_set_html_head($info);
  }
}

/**
 * Get the language for the user. If unknown, make a best guess.
 */
kiz_0987's avatar
kiz_0987 committed
123
function gallery_get_language($user) {
124 125 126 127 128 129 130 131 132 133 134 135 136 137
  // Added depdev patch for language support (http://drupal.org/node/32374)
  // without i18 part (I seem to remember a Rewrite issue with it
  // Added test for no user language defined
  if (($user->uid==0 || !($user->language)) && module_exist('locale')) { 
    // This is a visitor and locale module is enabled 
    // Get drupal's default language
    $result = db_query('SELECT locale, name FROM {locales_meta} WHERE isdefault = 1'); 
    $row = db_fetch_object($result);
    return $row->locale;
  } else {
    return $user->language;
  }   
}

kiz_0987's avatar
kiz_0987 committed
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
/**
 * Split an image block result into individual images
 * (Kludge until imageblock has this option)
 */
function _gallery_split_imageblock($html) {
  /**
   * From http://uk.php.net/manual/en/function.preg-split.php
   * Split the html from image block into <...> parts
   */
  $pattern = '/(<(?:[^<>]+(?:"[^"]*"|\'[^\']*\')?)+>)/';
  $html_array = preg_split ($pattern, trim ($html), -1, 
    PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
  
  $ndx = 0;
  $images = array();
  $image_html = '';
  // Now iterate through this array and combine again, but on a per-image basis
  foreach ($html_array as $value) {
    $value = trim($value);
    $image_html .= $value; 
    if (!strcmp($value, '<div class="one-image">')) {
      // Found the opening <div> for the image
      $open_divs = 0;
    } else if (!strncmp($value, '<div', 4)) {
      // Found a <div> but not the main image one (eg a frame)
      $open_divs++;
    } else if (!strcmp($value, '</div>')) {
      // Found a </div> but check if it's for the main image or a subcomponent (eg frame)
      if ($open_divs>0) {
        $open_divs--;
      } else {
        // This must be the closing div for "one-image" so move to next image
        $images[] = $image_html;
        $image_html = '';
      }
    }
  }
  return $images;  
}


179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
/* 
 * --------------------------------------------------------------------------
 * Error Functions
 * --------------------------------------------------------------------------
 */
function gallery_error($message, $ret) {
// Changed default, just in case!
  $error_mode = variable_get('gallery_error_mode', array(1));
  if (in_array(2, $error_mode)) {
    drupal_set_message($message);
  }

  if (isset($ret)) {
    $full_message = $message . '<br/>' . $ret->getAsHtml();
  } else {
    $full_message = $message;
  }

  if (in_array(1, $error_mode)) {
    watchdog('gallery', $full_message, WATCHDOG_ERROR);
  }
}
 
 
?>