|
From: Jon O. <jon...@us...> - 2005-09-27 18:56:26
|
Update of /cvsroot/mxbb/mx_gallery2/includes In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv20707/modules/mx_gallery2/includes Added Files: gallery_constants.php gallery_functions.php Log Message: added the gallery2 module --- NEW FILE: gallery_constants.php --- <?php /** * phpbb_constants.php * ------------------- * begin : April, 2003 * copyright : (C) 2002 MX-System * email : su...@mx... * description : define constants * Author : Haplo (jon...@ho...) * credit : Roman Malarev (romutis), MarcMoris * * $Id: gallery_constants.php,v 1.1 2005/09/27 18:56:14 jonohlsson Exp $ */ // ---------------------------------------------------------------------START // This file defines specific constants for the module // ------------------------------------------------------------------------- $storeConfig['tablePrefix'] = 'g2_'; define( 'GALLERY_USER_TABLE', $storeConfig['tablePrefix'] . 'user' ); define( 'GALLERY_USER_MAP_TABLE', $storeConfig['tablePrefix'] . 'externalidmap' ); define( 'GALLERY_GROUP_TABLE', $storeConfig['tablePrefix'] . 'user' ); define( 'GALLERY_GROUP_MAP_TABLE', $storeConfig['tablePrefix'] . 'usergroupmap' ); // ********************************************************************** // Read theme definition // ********************************************************************** if ( file_exists( $module_root_path . "templates/" . $theme['template_name'] . "/images" ) ) { // ---------- $current_template_images = $module_root_path . "templates/" . $theme['template_name'] . "/images" ; // ---------- } else { // ---------- $current_template_images = $module_root_path . "templates/" . "subSilver" . "/images" ; // ---------- } // ********************************************************************** // Read language definition // ********************************************************************** if ( !file_exists( $mx_root_path . 'modules/mx_gallery2/language/lang_' . $board_config['default_lang'] . '/lang_main.' . $phpEx ) ) { include( $mx_root_path . 'modules/mx_gallery2/language/lang_english/lang_main.' . $phpEx ); $link_language = 'lang_english'; } else { include( $mx_root_path . 'modules/mx_gallery2/language/lang_' . $board_config['default_lang'] . '/lang_main.' . $phpEx ); $link_language = 'lang_' . $board_config['default_lang']; } $mxbb_footer_addup[] = 'mxBB Gallery2 Module'; // ---------- $phpbb_module_version = "1.0"; $phpbb_module_author = "MX Team"; $phpbb_module_orig_author = "Menalto"; ?> --- NEW FILE: gallery_functions.php --- <?php /*************************************************************************** * gallery_functions.php * ------------------- * begin : Sep, 2005 * copyright : (C) 2005 mxBB * email : jon...@mx... * * $Id: gallery_functions.php,v 1.1 2005/09/27 18:56:14 jonohlsson Exp $ * * ***************************************************************************/ /*************************************************************************** * * 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. * ***************************************************************************/ // // Gallery Embed Doc // - included here for practical reasons /* Next you need an entry point in the emApp for all G2 requests, (similar to main.php in G2 standalone). This may be a module or plugin if the application supports these, or a plain php file. Determine the URI/path for your access point and the G2 base directory, relative to your webserver document root. Examples: __ access point __ __ G2 base dir __ a) appdir/gallery2.php appdir/gallery2 b) appdir/plugins/gallery2.php appdir/lib/gallery2 c) appdir/index.php?module=gallery2 gallery2 d) index.php?page=gallery albums Use these to determine the following values: embedURI = Filename (and any query parameters, but NO path) for access point embedPath = URL path from document root to embedUri relativeG2Path = Path from directory for access point to G2 base directory For the above examples these settings are: __ embedURI __ __ embedPath __ __ relativeG2Path __ a) gallery2.php /appdir gallery2 b) gallery2.php /appdir lib/gallery2 c) index.php?module=gallery2 /appdir ../gallery2 d) index.php?page=gallery / albums You'll also need a URI for a login page or main page of the app; G2 will redirect here for access attempts without proper permissions. Examples: / or /appdir/login.php The code in the access point to handle a G2 request looks something like this: require_once(dirname(__FILE__) . 'relative/path/to/gallery2/embed.php'); $ret = GalleryEmbed::init(array( 'embedUri' => {value}, embedPath => {value}, 'relativeG2Path' => {value}, 'loginRedirect' => {value}, 'activeUserId' => {value})); if ($ret->isError()) { // $ret->getAsHtml() has error details.. exit; } $g2data = GalleryEmbed::handleRequest(); if ($g2data['isDone']) { exit; // G2 has already sent output (redirect or binary data) } // Use $g2data['headHtml'] and $g2data['bodyHtml'] // to display G2 content inside embedding application // if you don't want to use $g2data['headHtml'] directly, you can get the css, // javascript and page title separately by calling... if (isset($g2moddata['headHtml'])) { list($title, $css, $javascript) = GalleryEmbed::parseHead($g2moddata['headHtml']); } The 'activeUserId' value passed to init() enables session synchronization by making sure the active user in G2 matches the emApp user. The value is the active emApp user id. Pass in an empty string for anonymous/guest user (NOT the user id of the emApp's anonymous user if it has an id). To complete session sync add a hook/callback/event-handler for the logout operation in the emApp: require_once(dirname(__FILE__) . 'relative/path/to/gallery2/embed.php'); $ret = GalleryEmbed::logout(); This hook is not required, but it resets the G2 session at logout so a session for a logged-in user isn't left on the server. Next decide if you want to support cookieless browsing. Some extra steps are required if G2 can't count on cookies to track its session id. The emApp must be able to provide a key=value string for its own session key and id, and have an available API to store additional data in the session. Here are the extra steps: 1) The input array for the GalleryEmbed::init() call must also contain: 'embedSessionString' => {key=value}, 'gallerySessionId' => {value} (omit gallerySessionId when the value is not yet known) 2) After the init() call: $gallerySessionId = GallerEmbed::getSessionId(); {store value in emApp's session} Other optional tasks: 1) Prior to the handleRequest() call set GalleryCapabilities. Check modules/core/classes/GalleryCapabilities.class for available settings; also see the settings made by default in GalleryEmbed::init(). Example: GalleryCapabilities::set('showSidebar', false); 2) Check for 'sidebarHtml' in content returned from handleRequest(). If the example GalleryCapabilities setting shown above is used then handleRequest() may return 'sidebarHtml' (when a core.ShowItem page is being viewed). You can include this content in a block on the emApp sidebar. Next add hooks to keep users in sync between G2 and the emApp. The code in each case will look like this: require_once(dirname(__FILE__) . 'relative/path/to/gallery2/embed.php'); $ret = GalleryEmbed::init(); if ($ret->isError()) { // $ret->getAsHtml() has error details.. } // Call GalleryEmbed as required.. // .. $ret = GalleryEmbed::done(); if ($ret->isError()) { // $ret->getAsHtml() has error details.. } You'll need hooks to call GalleryEmbed::createUser(), ::updateUser() and ::deleteUser() for user creation, updates and deletes, respectively. There may be two create hooks if the emApp supports both new user registration and an admin function to create new users directly. If the emApp supports user groups then add hooks to keep groups in sync. Synchronizing groups is optional, even if the emApp supports groups. The process is the same as above, using GalleryEmbed::createGroup, ::updateGroup, ::deleteGroup(), ::addUserToGroup() and ::removeUserFromGroup(). Note that G2 does not support nested groups.. if the emApp does then the hooks will have to expand the groups into the list of users and update the G2 groups accordingly. */ // // Define some vars // define('MAP_USERGROUP_ID', 2); // This is the externalgroupId mapped to the gallery mxBB user group define('MAP_ADMINGROUP_ID', 3); // This is the "handle" to the gallery admin group define('MAP_MXBB_USER', 0); // Flag: update user externalId map define('MAP_MXBB_GROUP', 1); // Flag: update group externalId map define('MAP_DEBUG', false); // TRUE will output all class activity $map_debug = ''; class mx_gallery { var $isInitiated = false; // Is the Gallery Embed Class initiated var $user_map_data = array(); // current user map data var $user_gallery_user_data = array(); // duplicate gallery user data /*********************************************************/ /* Init Gallery Embed Class */ /*********************************************************/ function init() { global $db, $gallery, $embedPath, $embedUri, $relativeG2Path, $loginRedirect, $fullInit, $map_debug; // // only init once // if ($this->isInitiated) { $map_debug .= "<br>GalleryEmbed is inititated - resting"; return true; } $map_debug .= "<br>Inititating GalleryEmbed - ..."; $ret = GalleryEmbed :: init(array ( 'embedPath' => $embedPath, 'embedUri' => $embedUri, 'relativeG2Path' => $relativeG2Path, 'loginRedirect' => $loginRedirect, 'activeUserId' => '', 'fullInit' => $fullInit)); $gallery->guaranteeTimeLimit(300); if (!$ret->isSuccess()) { $map_debug .= '<br>G2 did not return a success status upon an init request. Here is the error message from G2: <br /> [#(1)]'.$ret->getAsHtml(); return false; } $this->isInitiated = true; return true; } // // Syncronize GD and phpBB Users // /** * g2addexternalMapEntry: add an externalId map entry * g2updateexternalMapEntry: update an externalId map entry * * Add an entry in the G externalId, entityId map table * * @author Andy Staudacher * @access public * @param integer the uid * @param integer the entityId from G2 * @param integer/string the roles type, 'MAP_MXBB_GROUP' for groups, 'MAP_MXBB_USER' for users, or the entityType string * @return bool true or false */ function _g2addexternalMapEntry($externalId, $entityId, $entityType ) { global $db, $failures, $fullpath, $map_debug; $is_ok = true; if (is_int($entityType)) { $entityType = $entityType == MAP_MXBB_USER ? 'GalleryUser' : 'GalleryGroup'; } require_once ($fullpath."/".'modules/core/classes/ExternalIdMap.class'); $ret = ExternalIdMap :: addMapEntry(array ('externalId' => $externalId, 'entityType' => $entityType, 'entityId' => $entityId)); if ($ret->isError()) { $failures[] = $externalId; $is_ok = false; } $map_debug .= $is_ok ? '<br>externalMapEntry inserted successfully' : '<br>ERROR: externalMapEntry not inserted'; return $is_ok; } function _g2updateexternalMapEntry($externalId, $entityId, $entityType) { global $db, $failures, $fullpath, $map_debug; $is_ok = true; if (is_int($entityType)) { $entityType = $entityType == MAP_MXBB_USER ? 'GalleryUser' : 'GalleryGroup'; } require_once ($fullpath."/".'modules/core/classes/ExternalIdMap.class'); $ret = ExternalIdMap :: removeMapEntry(array ('externalId' => $externalId)); if ($ret->isError()) { $map_debug .= 'WARNING: mapping does not exist'; $failures[] = $externalId; $is_ok = false; } $ret = ExternalIdMap :: addMapEntry(array ('externalId' => $externalId, 'entityType' => $entityType, 'entityId' => $entityId)); if ($ret->isError()) { $failures[] = $externalId; $is_ok = false; } $map_debug .= $is_ok ? '<br>externalMapEntry updated successfully' : '<br>ERROR: externalMapEntry not updated'; return $is_ok; } // // Do a standard phpbb db query, to find out if gallery is syncronized with current user // function _is_mapped() { global $db, $userdata, $map_debug; // // Try to reuse old check result // $userdata_key = 'mx_gallery_is_mapped_' . $userdata['user_id']; if( !empty($userdata[$userdata_key]) ) { $map_debug .= '<br>Reusing userdata session info about usermapping'; $is_mapped = $userdata[$userdata_key]; $map_debug .= '<br>is_mapped: ' . intval($map_debug); return $is_mapped; } $map_debug .= '<br>Qeury to see if user is mapped'; // // Validate if the mxBB is already mapped in Gallery2 // $sql = "SELECT * FROM " . GALLERY_USER_MAP_TABLE . " map, " . GALLERY_USER_TABLE . " gus WHERE map.g_externalId = '" . $userdata['user_id'] . "' AND map.g_entityId = gus.g_id"; if ( !$result = $db->sql_query( $sql ) ) { mx_message_die( GENERAL_ERROR, "Could not query Gallery count", "", __LINE__, __FILE__, $sql ); } if( $total_maps = $db->sql_numrows($result) ) { $this->user_map_data = $db->sql_fetchrow($result); } $is_mapped = $total_maps > 0; $userdata[$userdata_key] = $is_mapped; $map_debug .= '<br>is_mapped: ' . intval($is_mapped); return $is_mapped; } // // Find already registered gallery users, also registered by phpbb, and not mapped // This function will (for example) extract if the gallery admin is also the phpbb admin etc... // To avoid having already registered users in gallery being created once more (will would generate a error) // function _is_gallery_user() { global $db, $userdata, $map_debug; $map_debug .= '<br>Qeury to see if user is already a gallery user'; // // Validate if the mxBB is already mapped in Gallery2 // Since phpBB will not allow users with the same 'username' or 'email', this check is sufficient // $sql = "SELECT * FROM " . GALLERY_USER_TABLE . " gus WHERE gus.g_userName = '" . $userdata['username'] . "' OR gus.g_email = '" . $userdata['user_email'] . "'"; if ( !$result = $db->sql_query( $sql ) ) { mx_message_die( GENERAL_ERROR, "Could not query Gallery count", "", __LINE__, __FILE__, $sql ); } if( $total_gallery_users = $db->sql_numrows($result) ) { $this->user_gallery_user_data = $db->sql_fetchrow($result); } $is_gallery_user = $total_gallery_users > 0; $map_debug .= '<br>is_gallery_user: ' . intval($is_gallery_user); return $is_gallery_user; } // // Public class method // Do mapping for current phpBB user (if not already done) // function do_map() { global $userdata, $map_debug; $map_ok = true; if ( $userdata['user_id'] != ANONYMOUS ) { // // Populate the $args array, for the gallery api // $user_id = $userdata['user_id']; $args['fullname'] = $userdata['username']; $args['username'] = $userdata['username']; $args['hashedpassword'] = $userdata['user_password']; $args['hashmethod'] = 'md5'; $args['email'] = $userdata['user_email']; $args['creationtimestamp'] = $userdata['user_regdate']; if( !$this->_is_mapped() ) { $map_debug .= '<br>Not mapped - do mapping'; // // only init once // if (!$this->init()) { return false; } // // First validate the mxBB user - to avoid duplicate Gallery users // For exdample the mxBB admin may have configured the Gallery admin account WITH THE SAME EMAIL // if ($this->_is_gallery_user()) { // // There already exists a gallery user with similar userdata as current mxBB user (username or email) // Map to this gallery user and update gallery user data // Do NOT create new user // $is_ok = true; // // Map mxBB user to existing Gallery user // if ( !$this->_g2addexternalMapEntry($user_id, $this->user_gallery_user_data['g_id'], MAP_MXBB_USER) ) { $map_debug .= '<br>Gallery user exists - do mapping to this user...'; $failures[] = $user_id; $is_ok = false; } $map_debug .= $is_ok ? '<br>mapping ok' : '<br>ERROR: mapping failed'; // // Nothing else to do ;) // return $is_ok; } // // we create the user // $ret = GalleryEmbed :: createUser($user_id, $args); $map_debug .= '<br>Creating user...'; if ( !$ret->isSuccess() ) { $map_debug .= 'ERROR: Could not create user'; $failures[] = $user_id; $map_ok = false; } // // Add a mxBB user group (if not exists) // list ($ret, $group) = GalleryCoreApi::loadEntityByExternalId(MAP_USERGROUP_ID, 'GalleryGroup'); if ($ret->isError()) { $map_debug .= '<br>Adding mxBB user group...'; // // If group doesn't exists - ADD // $ret = GalleryEmbed :: createGroup(MAP_USERGROUP_ID, 'mxBB Users'); if ( !$ret->isSuccess() ) { $map_debug .= 'ERROR: Could not create mxBB user group'; $failures[] = $user_id; $map_ok = false; } } $ret = GalleryEmbed :: addUserToGroup($user_id, MAP_USERGROUP_ID); $map_debug .= '<br>Adding user to mxBB user group...'; if ( !$ret->isSuccess() ) { $map_debug .= 'ERROR: Could not add user to mxBB user group'; $failures[] = $user_id; $map_ok = false; } // // Add admin to admin group // if ($userdata['user_level'] == ADMIN) { $map_debug .= '<br>User is admin - to additional settings'; // // Map the ExternalmapId for admins to the gallery admingroup // First get Gallery Group ID // list ($ret, $adminGroupId) = GalleryCoreApi::getPluginParameter('module', 'core', 'id.adminGroup'); if ($ret->isError()) { $map_debug .= '<br>ERROR: Unable to fetch the admin group. Here is the error message from G2: <br />'.$ret->getAsHtml(); flush(); return false; } // // Is there a mapping to the gallery admin group? // list ($ret, $GroupId) = GalleryCoreApi::loadEntityByExternalId(MAP_ADMINGROUP_ID, 'GalleryGroup'); if ($ret->isError() || $adminGroupId != $GroupId) { $map_debug .= '<br>Map to the Gallery admin group...'; // // If not, do it // if ( !$this->_g2updateexternalMapEntry(MAP_ADMINGROUP_ID, $adminGroupId, MAP_MXBB_GROUP) ) { $map_debug .= 'ERROR: Could not create group mapping to gallery admin group'; $failures[] = $user_id; $map_ok = false; } } $ret = GalleryEmbed :: addUserToGroup($user_id, MAP_ADMINGROUP_ID); $map_debug .= '<br>Adding user to mxBB admin group...'; if ( !$ret->isSuccess() ) { $map_debug .= 'ERROR: Could not add user to mxBB admin group'; $failures[] = $user_id; $map_ok = false; } } $map_debug .= $map_ok ? '<br>User added/mapped successfully' : '<br>ERROR: user not added/mapped'; } // // Updated phpBB user - already mapped in gallery2 // // For some reason the gallery api will not accept updated passwords (in some circumstnaces). but since this parameter is not used when in embed mode we ignore password updates // else if ($this->user_map_data['g_userName'] != $userdata['username'] || $this->user_map_data['g_email'] != $userdata['user_email'] || $this->user_map_data['g_hashedPassword'] != $userdata['user_password']) else if ($this->user_map_data['g_userName'] != $userdata['username'] || $this->user_map_data['g_email'] != $userdata['user_email'] ) { $map_debug .= '<br>Already mapped but userdata is changed - update mapping'; // // only init once // if (!$this->init()) { return false; } // // if the map exists, just update the user data // $ret = GalleryEmbed :: updateUser($user_id, $args); $map_debug .= '<br>Updating user...'; if ( !$ret->isSuccess() ) { $map_debug .= 'ERROR: Could not update user<br>'; $failures[] = $user_id; $map_ok = false; } $map_debug .= $map_ok ? '<br>user updated successfully' : '<br>ERROR: user not updated'; } } else { $map_debug .= '<br>Guest user - no mapping needed'; } return $map_ok; } /* // // TO_DO // Deleted mxBB users Former mxBB admins */ } ?> |