| c15e9178 |
1 | <?php |
| 2 | // $Id$ |
| 3 | |
| 4 | /** |
| 5 | * gallery.module : gallery_base.inc |
| 6 | * Base functions |
| 7 | */ |
| 8 | |
| 9 | /* -------------------------------------------------------------------------- |
| 10 | * Gallery init and misc functions |
| 11 | * -------------------------------------------------------------------------- |
| 12 | */ |
| 13 | |
| 14 | /** |
| 15 | * fetch a galleryEmbed object |
| 16 | */ |
| 17 | function _gallery_init($full = false, $vars = null) { |
| 18 | global $user; |
| 19 | |
| 20 | if (!$vars) { |
| 21 | $embedPath = variable_get('gallery_dir', './gallery2/') . 'embed.php'; |
| 22 | $g2Uri = variable_get('gallery_uri', '/gallery2/'); |
| 23 | $embedUri = variable_get('gallery_embed_uri', '?q=gallery'); |
| 24 | $gallery_valid = variable_get('gallery_valid', 0); |
| 25 | } else { |
| 26 | $embedPath = $vars['gallery_dir'] . 'embed.php'; |
| 27 | $g2Uri = $vars['gallery_uri']; |
| 28 | $embedUri = $vars['gallery_embed_uri']; |
| 29 | $gallery_valid = $vars['gallery_valid']; |
| 30 | } |
| 31 | |
| 18807c9c |
32 | if ((!$gallery_valid) || (!is_readable($embedPath))) { |
| c15e9178 |
33 | // It's probably now bad practice to return a null here, given that $ret=null is a gallery2 |
| 34 | // success response |
| 35 | return array(false, null); |
| 36 | } |
| 37 | |
| 38 | include_once($embedPath); |
| 39 | |
| 40 | $new_user_needed = false; |
| 41 | $active_id = ($user->uid>0) ? $user->uid : ''; |
| 42 | $params = array('embedUri' => $embedUri, |
| 43 | 'g2Uri' => $g2Uri, |
| 44 | 'loginRedirect' => url('user/login', null, null, true), |
| 45 | 'activeUserId' => $active_id, |
| 46 | 'activeLanguage' => gallery_get_language($user), |
| 47 | 'fullInit' => $full); |
| 48 | |
| 49 | $ret = GalleryEmbed::init($params); |
| 50 | if (!$ret) { |
| 51 | // No error returned, but it is still possible that the ExternalID mapping has not been done |
| 52 | $ret2 = GalleryEmbed::isExternalIdMapped($user->uid, 'GalleryUser'); |
| 53 | if ($ret2 && ($ret2->getErrorCode() & ERROR_MISSING_OBJECT)) { |
| 54 | // Need to make a new user, but don't try to map anonymous user. |
| 55 | $new_user_needed = ($user->uid>0); |
| 56 | } |
| 57 | } |
| 58 | if (($new_user_needed) || ($ret && ($ret->getErrorCode() & ERROR_MISSING_OBJECT))) { |
| 59 | // Our user mapping is missing. Make a mapping, or create a new user. |
| 60 | $g2_user = null; |
| 61 | // Get the G2 user that matches the Drupal username |
| 62 | list ($ret, $g2_user) = GalleryCoreApi::fetchUserByUsername($user->name); |
| 63 | if ($ret && !($ret->getErrorCode() & ERROR_MISSING_OBJECT)) { |
| 64 | return array(false, $ret); |
| 65 | } |
| 66 | if (!isset($g2_user)) { |
| 67 | // No G2 user with a matching username. If this is the admin user, we're going to |
| 68 | // try a little harder and match it to the oldest admin in G2. |
| 69 | if ($user->uid == 1) { |
| 70 | list ($ret, $admin_group_id) = GalleryCoreApi::getPluginParameter('module', 'core', 'id.adminGroup'); |
| 71 | if ($ret) { |
| 72 | return array(false, $ret); |
| 73 | } |
| 74 | list ($ret, $g2_users) = GalleryCoreApi::fetchUsersForGroup($admin_group_id); |
| 75 | if ($ret) { |
| 76 | return array(false, $ret); |
| 77 | } |
| 78 | $keys = array_keys($g2_users); |
| 79 | $g2_user_name = $g2_users[$keys[0]]; |
| 80 | list ($ret, $g2_user) = GalleryCoreApi::fetchUserByUsername($g2_user_name); |
| 81 | if ($ret) { |
| 82 | return array(false, $ret); |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | if (isset($g2_user)) { |
| 88 | // The G2 user was found so add to the External ID Map |
| 89 | $ret = GalleryEmbed::addExternalIdMapEntry($user->uid, $g2_user->getId(), 'GalleryUser'); |
| 90 | if ($ret) { |
| 91 | return array(false, $ret); |
| 92 | } |
| 93 | } else { |
| 94 | // No matching G2 user found -- create one. |
| 7eda3ef1 |
95 | $path = drupal_get_path('module', 'gallery'); |
| 96 | require_once($path . '/gallery_user.inc'); |
| c15e9178 |
97 | return gallery_modify_user($user, 'create'); |
| 98 | } |
| 99 | } |
| 100 | return array(true, null); |
| 101 | } |
| 102 | |
| 103 | /** |
| c15e9178 |
104 | * Include head information with check made for uniqueness (see drupal_add_js) |
| 105 | */ |
| 106 | function gallery_set_html_head($info, $unique=true) { |
| 107 | static $sent = array(); |
| 108 | if ($unique) { |
| 109 | // Only set html head if this info has not been sent before. |
| 110 | $hash_info = md5($info); |
| 111 | if (!isset($sent[$hash_info])) { |
| 112 | drupal_set_html_head($info); |
| 113 | $sent[$hash_info] = true; |
| 114 | } |
| 115 | } else { |
| 116 | drupal_set_html_head($info); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Get the language for the user. If unknown, make a best guess. |
| 122 | */ |
| 55260521 |
123 | function gallery_get_language($user) { |
| c15e9178 |
124 | // Added depdev patch for language support (http://drupal.org/node/32374) |
| 125 | // without i18 part (I seem to remember a Rewrite issue with it |
| 126 | // Added test for no user language defined |
| 04b696f5 |
127 | if (($user->uid==0 || !($user->language)) && module_exists('locale')) { |
| c15e9178 |
128 | // This is a visitor and locale module is enabled |
| 129 | // Get drupal's default language |
| 130 | $result = db_query('SELECT locale, name FROM {locales_meta} WHERE isdefault = 1'); |
| 131 | $row = db_fetch_object($result); |
| 132 | return $row->locale; |
| 133 | } else { |
| 134 | return $user->language; |
| 135 | } |
| 136 | } |
| 137 | |
| 55260521 |
138 | /** |
| 139 | * Split an image block result into individual images |
| 140 | * (Kludge until imageblock has this option) |
| 141 | */ |
| 142 | function _gallery_split_imageblock($html) { |
| 143 | /** |
| 144 | * From http://uk.php.net/manual/en/function.preg-split.php |
| 145 | * Split the html from image block into <...> parts |
| 146 | */ |
| 147 | $pattern = '/(<(?:[^<>]+(?:"[^"]*"|\'[^\']*\')?)+>)/'; |
| 148 | $html_array = preg_split ($pattern, trim ($html), -1, |
| 149 | PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); |
| 150 | |
| 151 | $ndx = 0; |
| 152 | $images = array(); |
| 153 | $image_html = ''; |
| 154 | // Now iterate through this array and combine again, but on a per-image basis |
| 155 | foreach ($html_array as $value) { |
| 156 | $value = trim($value); |
| 157 | $image_html .= $value; |
| 158 | if (!strcmp($value, '<div class="one-image">')) { |
| 159 | // Found the opening <div> for the image |
| 160 | $open_divs = 0; |
| 161 | } else if (!strncmp($value, '<div', 4)) { |
| 162 | // Found a <div> but not the main image one (eg a frame) |
| 163 | $open_divs++; |
| 164 | } else if (!strcmp($value, '</div>')) { |
| 165 | // Found a </div> but check if it's for the main image or a subcomponent (eg frame) |
| 166 | if ($open_divs>0) { |
| 167 | $open_divs--; |
| 168 | } else { |
| 169 | // This must be the closing div for "one-image" so move to next image |
| 170 | $images[] = $image_html; |
| 171 | $image_html = ''; |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | return $images; |
| 176 | } |
| 177 | |
| 178 | |
| c15e9178 |
179 | /* |
| 180 | * -------------------------------------------------------------------------- |
| 181 | * Error Functions |
| 182 | * -------------------------------------------------------------------------- |
| 183 | */ |
| 184 | function gallery_error($message, $ret) { |
| 185 | // Changed default, just in case! |
| 186 | $error_mode = variable_get('gallery_error_mode', array(1)); |
| 187 | if (in_array(2, $error_mode)) { |
| 188 | drupal_set_message($message); |
| 189 | } |
| 190 | |
| 191 | if (isset($ret)) { |
| 192 | $full_message = $message . '<br/>' . $ret->getAsHtml(); |
| 18807c9c |
193 | } else { |
| c15e9178 |
194 | $full_message = $message; |
| 195 | } |
| 196 | |
| 197 | if (in_array(1, $error_mode)) { |
| 198 | watchdog('gallery', $full_message, WATCHDOG_ERROR); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | |
| 203 | ?> |