/** * Initialize the embedded Gallery2 instance. Call this before any other Gallery2 calls. */ static function init_embed($embed_path) { if (!is_file($embed_path)) { return false; } // Gallery2 defines a class called Gallery. So does Gallery 3. They don't get along. So do // a total hack here and copy over a few critical files (embed.php, main.php, bootstrap.inc // and Gallery.class) and munge them so that we can rename the Gallery class to be // G2_Gallery. Is this retarded? Why yes it is. // // Store the munged files in a directory that's the md5 hash of the embed path so that // multiple import sources don't interfere with each other. $mod_path = VARPATH . "modules/g2_import/" . md5($embed_path); if (!file_exists($mod_path) || !file_exists("{$mod_path}/embed.php")) { @dir::unlink($mod_path); mkdir($mod_path); $base_dir = dirname($embed_path); file_put_contents("{$mod_path}/embed.php", str_replace(array("require_once(dirname(__FILE__) . '/modules/core/classes/GalleryDataCache.class');", "require(dirname(__FILE__) . '/modules/core/classes/GalleryEmbed.class');"), array("require_once('{$base_dir}/modules/core/classes/GalleryDataCache.class');", "require('{$base_dir}/modules/core/classes/GalleryEmbed.class');"), array_merge(array("<?php defined(\"SYSPATH\") or die(\"No direct script access.\") ?>\n"), file("{$base_dir}/embed.php")))); file_put_contents("{$mod_path}/main.php", str_replace(array("include(dirname(__FILE__) . '/bootstrap.inc');", "require_once(dirname(__FILE__) . '/init.inc');"), array("include(dirname(__FILE__) . '/bootstrap.inc');", "require_once('{$base_dir}/init.inc');"), array_merge(array("<?php defined(\"SYSPATH\") or die(\"No direct script access.\") ?>\n"), file("{$base_dir}/main.php")))); file_put_contents("{$mod_path}/bootstrap.inc", str_replace(array("require_once(dirname(__FILE__) . '/modules/core/classes/Gallery.class');", "require_once(dirname(__FILE__) . '/modules/core/classes/GalleryDataCache.class');", "define('GALLERY_CONFIG_DIR', dirname(__FILE__));", "\$gallery =& new Gallery();"), array("require_once(dirname(__FILE__) . '/Gallery.class');", "require_once('{$base_dir}/modules/core/classes/GalleryDataCache.class');", "define('GALLERY_CONFIG_DIR', '{$base_dir}');", "\$gallery =& new G2_Gallery();"), array_merge(array("<?php defined(\"SYSPATH\") or die(\"No direct script access.\") ?>\n"), file("{$base_dir}/bootstrap.inc")))); file_put_contents("{$mod_path}/Gallery.class", str_replace(array("class Gallery", "function Gallery"), array("class G2_Gallery", "function G2_Gallery"), array_merge(array("<?php defined(\"SYSPATH\") or die(\"No direct script access.\") ?>\n"), file("{$base_dir}/modules/core/classes/Gallery.class")))); } require "{$mod_path}/embed.php"; if (!class_exists("GalleryEmbed")) { return false; } $ret = GalleryEmbed::init(); if ($ret) { return false; } $admin_group_id = g2(GalleryCoreApi::getPluginParameter("module", "core", "id.adminGroup")); $admins = g2(GalleryCoreApi::fetchUsersForGroup($admin_group_id, 1)); $admin_id = current(array_flip($admins)); $admin = g2(GalleryCoreApi::loadEntitiesById($admin_id)); $GLOBALS["gallery"]->setActiveUser($admin); return true; }
static function import($task) { $start = microtime(true); g2_import::init(); $stats = $task->get("stats"); $done = $task->get("done"); $total = $task->get("total"); $completed = $task->get("completed"); $mode = $task->get("mode"); $queue = $task->get("queue"); if (!isset($mode)) { $stats = g2_import::stats(); $stats["items"] = $stats["photos"] + $stats["movies"]; unset($stats["photos"]); unset($stats["movies"]); $stats["highlights"] = $stats["albums"]; $task->set("stats", $stats); $task->set("total", $total = array_sum(array_values($stats))); $completed = 0; $mode = 0; $done = array(); foreach (array_keys($stats) as $key) { $done[$key] = 0; } $task->set("done", $done); $root_g2_id = g2(GalleryCoreApi::getDefaultAlbumId()); $root = ORM::factory("g2_map")->where("g2_id", "=", $root_g2_id)->find(); if (!$root->loaded()) { $root->g2_id = $root_g2_id; $root->g3_id = 1; $root->save(); } } $modes = array("groups", "users", "albums", "items", "comments", "tags", "highlights", "done"); while (!$task->done && microtime(true) - $start < 1.5) { if ($done[$modes[$mode]] == $stats[$modes[$mode]]) { // Nothing left to do for this mode. Advance. $mode++; $task->set("last_id", 0); $queue = array(); // Start the loop from the beginning again. This way if we get to a mode that requires no // actions (eg, if the G2 comments module isn't installed) we won't try to do any comments // queries.. in the next iteration we'll just skip over that mode. if ($modes[$mode] != "done") { continue; } } switch ($modes[$mode]) { case "groups": if (empty($queue)) { $task->set("queue", $queue = array_keys(g2(GalleryCoreApi::fetchGroupNames()))); } $log_message = g2_import::import_group($queue); if ($log_message) { $task->log($log_message); } $task->status = t("Importing groups (%count of %total)", array("count" => $done["groups"] + 1, "total" => $stats["groups"])); break; case "users": if (empty($queue)) { $task->set("queue", $queue = array_keys(g2(GalleryCoreApi::fetchUsersForGroup(GROUP_EVERYBODY)))); } $log_message = g2_import::import_user($queue); if ($log_message) { $task->log($log_message); } $task->status = t("Importing users (%count of %total)", array("count" => $done["users"] + 1, "total" => $stats["users"])); break; case "albums": if (empty($queue)) { $task->set("queue", $queue = g2(GalleryCoreApi::fetchAlbumTree())); } $log_message = g2_import::import_album($queue); if ($log_message) { $task->log($log_message); } $task->status = t("Importing albums (%count of %total)", array("count" => $done["albums"] + 1, "total" => $stats["albums"])); break; case "items": if (empty($queue)) { $task->set("queue", $queue = g2_import::get_item_ids($task->get("last_id", 0))); $task->set("last_id", end($queue)); } $log_message = g2_import::import_item($queue); if ($log_message) { $task->log($log_message); } $task->status = t("Importing photos (%count of %total)", array("count" => $done["items"] + 1, "total" => $stats["items"])); break; case "comments": if (empty($queue)) { $task->set("queue", $queue = g2_import::get_comment_ids($task->get("last_id", 0))); $task->set("last_id", end($queue)); } $log_message = g2_import::import_comment($queue); if ($log_message) { $task->log($log_message); } $task->status = t("Importing comments (%count of %total)", array("count" => $done["comments"] + 1, "total" => $stats["comments"])); break; case "tags": if (empty($queue)) { $task->set("queue", $queue = g2_import::get_tag_item_ids($task->get("last_id", 0))); $task->set("last_id", end($queue)); } $log_message = g2_import::import_tags_for_item($queue); if ($log_message) { $task->log($log_message); } $task->status = t("Importing tags (%count of %total)", array("count" => $done["tags"] + 1, "total" => $stats["tags"])); break; case "highlights": if (empty($queue)) { $task->set("queue", $queue = g2(GalleryCoreApi::fetchAlbumTree())); } $log_message = g2_import::set_album_highlight($queue); if ($log_message) { $task->log($log_message); } $task->status = t("Album highlights (%count of %total)", array("count" => $done["tags"] + 1, "total" => $stats["albums"])); break; case "done": $task->status = t("Import complete"); $task->done = true; $task->state = "success"; break; } if (!$task->done) { $done[$modes[$mode]]++; $completed++; } } $task->percent_complete = 100 * ($completed / $total); $task->set("completed", $completed); $task->set("mode", $mode); $task->set("queue", $queue); $task->set("done", $done); }
/** * Initialize the embedded Gallery 2 instance. Call this before any other Gallery 2 calls. */ static function init_embed($embed_path) { if (!is_file($embed_path)) { return false; } // Gallery 2 defines a class called Gallery. So does Gallery 3. They don't get along. So do // a total hack here and copy over a few critical files (embed.php, main.php, bootstrap.inc // and Gallery.class) and munge them so that we can rename the Gallery class to be // G2_Gallery. Is this retarded? Why yes it is. // // Store the munged files in a directory that's the md5 hash of the embed path so that // multiple import sources don't interfere with each other. $mod_path = VARPATH . "modules/g2_import/" . md5($embed_path); if (!file_exists($mod_path) || !file_exists("{$mod_path}/embed.php")) { @dir::unlink($mod_path); mkdir($mod_path); $config_dir = dirname($embed_path); if (filesize($embed_path) > 200) { // Regular install $base_dir = $config_dir; } else { // Multisite install. Line 2 of embed.php will be something like: // require('/usr/home/bharat/public_html/gallery2/embed.php'); $lines = file($embed_path); preg_match("#require\\('(.*)/embed.php'\\);#", $lines[2], $matches); $base_dir = $matches[1]; } file_put_contents("{$mod_path}/embed.php", str_replace(array("require_once(dirname(__FILE__) . '/modules/core/classes/GalleryDataCache.class');", "require(dirname(__FILE__) . '/modules/core/classes/GalleryEmbed.class');"), array("require_once('{$base_dir}/modules/core/classes/GalleryDataCache.class');", "require('{$base_dir}/modules/core/classes/GalleryEmbed.class');"), array_merge(array("<?php defined(\"SYSPATH\") or die(\"No direct script access.\") ?>\n"), file("{$base_dir}/embed.php")))); file_put_contents("{$mod_path}/main.php", str_replace(array("include(dirname(__FILE__) . '/bootstrap.inc');", "require_once(dirname(__FILE__) . '/init.inc');"), array("include(dirname(__FILE__) . '/bootstrap.inc');", "require_once('{$base_dir}/init.inc');"), array_merge(array("<?php defined(\"SYSPATH\") or die(\"No direct script access.\") ?>\n"), file("{$base_dir}/main.php")))); file_put_contents("{$mod_path}/bootstrap.inc", str_replace(array("require_once(dirname(__FILE__) . '/modules/core/classes/Gallery.class');", "require_once(dirname(__FILE__) . '/modules/core/classes/GalleryDataCache.class');", "define('GALLERY_CONFIG_DIR', dirname(__FILE__));", "\$gallery =& new Gallery();", "\$GLOBALS['gallery'] =& new Gallery();", "\$gallery = new Gallery();"), array("require_once(dirname(__FILE__) . '/Gallery.class');", "require_once('{$base_dir}/modules/core/classes/GalleryDataCache.class');", "define('GALLERY_CONFIG_DIR', '{$config_dir}');", "\$gallery =& new G2_Gallery();", "\$GLOBALS['gallery'] =& new G2_Gallery();", "\$gallery = new G2_Gallery();"), array_merge(array("<?php defined(\"SYSPATH\") or die(\"No direct script access.\") ?>\n"), file("{$base_dir}/bootstrap.inc")))); file_put_contents("{$mod_path}/Gallery.class", str_replace(array("class Gallery", "function Gallery"), array("class G2_Gallery", "function G2_Gallery"), array_merge(array("<?php defined(\"SYSPATH\") or die(\"No direct script access.\") ?>\n"), file("{$base_dir}/modules/core/classes/Gallery.class")))); } require "{$mod_path}/embed.php"; if (!class_exists("GalleryEmbed")) { return false; } $ret = GalleryEmbed::init(); if ($ret) { return false; } $admin_group_id = g2(GalleryCoreApi::getPluginParameter("module", "core", "id.adminGroup")); $admins = g2(GalleryCoreApi::fetchUsersForGroup($admin_group_id, 1)); $admin_id = current(array_flip($admins)); $admin = g2(GalleryCoreApi::loadEntitiesById($admin_id)); $GLOBALS["gallery"]->setActiveUser($admin); // Make sure we have an embed location so that embedded url generation comes out ok. Without // this, the Gallery2 ModRewrite code won't try to do url generation. $g2_embed_location = g2(GalleryCoreApi::getPluginParameter("module", "rewrite", "modrewrite.embeddedLocation")); if (empty($g2_embed_location)) { $g2_embed_location = g2(GalleryCoreApi::getPluginParameter("module", "rewrite", "modrewrite.galleryLocation")); g2(GalleryCoreApi::setPluginParameter("module", "rewrite", "modrewrite.embeddedLocation", $g2_embed_location)); g2($gallery->getStorage()->checkPoint()); } self::$g2_base_url = $g2_embed_location; return true; }
/** * Initialize the embedded Gallery 2 instance. Call this before any other Gallery 2 calls. * * Return values: * "ok" - the Gallery 2 install is fine * "missing" - the embed path does not exist * "invalid" - the install path is not a valid Gallery 2 code base * "broken" - the embed path is correct, but the Gallery 2 install is broken */ static function init_embed($embed_path) { if (!is_file($embed_path)) { return "missing"; } try { // Gallery 2 defines a class called Gallery. So does Gallery 3. They don't get along. So do // a total hack here and copy over a few critical files (embed.php, main.php, bootstrap.inc // and Gallery.class) and munge them so that we can rename the Gallery class to be // G2_Gallery. Is this retarded? Why yes it is. // // Store the munged files in a directory that's the md5 hash of the embed path so that // multiple import sources don't interfere with each other. $mod_path = VARPATH . "modules/g2_import/" . md5($embed_path); if (!file_exists($mod_path) || !file_exists("{$mod_path}/embed.php")) { @dir::unlink($mod_path); mkdir($mod_path); $config_dir = dirname($embed_path); if (filesize($embed_path) > 200) { // Regular install $base_dir = $config_dir; } else { // Multisite install. Line 2 of embed.php will be something like: // require('/usr/home/bharat/public_html/gallery2/embed.php'); $lines = file($embed_path); preg_match("#require\\('(.*)/embed.php'\\);#", $lines[2], $matches); $base_dir = $matches[1]; } file_put_contents("{$mod_path}/embed.php", str_replace(array("require_once(dirname(__FILE__) . '/modules/core/classes/GalleryDataCache.class');", "require(dirname(__FILE__) . '/modules/core/classes/GalleryEmbed.class');"), array("require_once('{$base_dir}/modules/core/classes/GalleryDataCache.class');", "require('{$base_dir}/modules/core/classes/GalleryEmbed.class');"), array_merge(array("<?php defined(\"SYSPATH\") or die(\"No direct script access.\") ?>\n"), file("{$base_dir}/embed.php")))); file_put_contents("{$mod_path}/main.php", str_replace(array("include(dirname(__FILE__) . '/bootstrap.inc');", "require_once(dirname(__FILE__) . '/init.inc');"), array("include(dirname(__FILE__) . '/bootstrap.inc');", "require_once('{$base_dir}/init.inc');"), array_merge(array("<?php defined(\"SYSPATH\") or die(\"No direct script access.\") ?>\n"), file("{$base_dir}/main.php")))); file_put_contents("{$mod_path}/bootstrap.inc", str_replace(array("require_once(dirname(__FILE__) . '/modules/core/classes/Gallery.class');", "require_once(dirname(__FILE__) . '/modules/core/classes/GalleryDataCache.class');", "define('GALLERY_CONFIG_DIR', dirname(__FILE__));", "\$gallery =& new Gallery();", "\$GLOBALS['gallery'] =& new Gallery();", "\$gallery = new Gallery();"), array("require_once(dirname(__FILE__) . '/Gallery.class');", "require_once('{$base_dir}/modules/core/classes/GalleryDataCache.class');", "define('GALLERY_CONFIG_DIR', '{$config_dir}');", "\$gallery =& new G2_Gallery();", "\$GLOBALS['gallery'] =& new G2_Gallery();", "\$gallery = new G2_Gallery();"), array_merge(array("<?php defined(\"SYSPATH\") or die(\"No direct script access.\") ?>\n"), file("{$base_dir}/bootstrap.inc")))); file_put_contents("{$mod_path}/Gallery.class", str_replace(array("class Gallery", "function Gallery"), array("class G2_Gallery", "function G2_Gallery"), array_merge(array("<?php defined(\"SYSPATH\") or die(\"No direct script access.\") ?>\n"), file("{$base_dir}/modules/core/classes/Gallery.class")))); } else { // Ok, this is a good one. If you're running a bytecode accelerator and you move your // Gallery install, these files sometimes get cached with the wrong path and then fail to // load properly. // Documented in https://sourceforge.net/apps/trac/gallery/ticket/1253 touch("{$mod_path}/embed.php"); touch("{$mod_path}/main.php"); touch("{$mod_path}/bootstrap.inc"); touch("{$mod_path}/Gallery.class.inc"); } require "{$mod_path}/embed.php"; if (!class_exists("GalleryEmbed")) { return "invalid"; } $ret = GalleryEmbed::init(); if ($ret) { Kohana_Log::add("error", "Gallery 2 call failed with: " . $ret->getAsText()); return "broken"; } $admin_group_id = g2(GalleryCoreApi::getPluginParameter("module", "core", "id.adminGroup")); $admins = g2(GalleryCoreApi::fetchUsersForGroup($admin_group_id, 1)); $admin_id = current(array_flip($admins)); $admin = g2(GalleryCoreApi::loadEntitiesById($admin_id)); $GLOBALS["gallery"]->setActiveUser($admin); // Make sure we have an embed location so that embedded url generation comes out ok. Without // this, the Gallery2 ModRewrite code won't try to do url generation. $g2_embed_location = g2(GalleryCoreApi::getPluginParameter("module", "rewrite", "modrewrite.embeddedLocation")); if (empty($g2_embed_location)) { $g2_embed_location = g2(GalleryCoreApi::getPluginParameter("module", "rewrite", "modrewrite.galleryLocation")); g2(GalleryCoreApi::setPluginParameter("module", "rewrite", "modrewrite.embeddedLocation", $g2_embed_location)); g2($gallery->getStorage()->checkPoint()); } if ($g2_embed_location) { self::$g2_base_url = $g2_embed_location; } else { self::$g2_base_url = $GLOBALS["gallery"]->getUrlGenerator()->generateUrl(array(), array("forceSessionId" => false, "htmlEntities" => false, "urlEncode" => false, "useAuthToken" => false)); } } catch (ErrorException $e) { Kohana_Log::add("error", $e->getMessage() . "\n" . $e->getTraceAsString()); return "broken"; } return "ok"; }
/** * Find admin user and set as active user * @param bool $fallback (optional) whether we should try to fall back if the * API to load the admin user object fails * @return GalleryStatus a status code */ function selectAdminUser($fallback = false) { global $gallery; list($ret, $siteAdminGroupId) = GalleryCoreApi::getPluginParameter('module', 'core', 'id.adminGroup'); if ($ret) { return $ret; } list($ret, $adminUserInfo) = GalleryCoreApi::fetchUsersForGroup($siteAdminGroupId, 1); if ($ret) { return $ret; } if (empty($adminUserInfo)) { return GalleryCoreApi::error(ERROR_MISSING_VALUE); } /* Fetch the first admin from list */ list($userId, $userName) = each($adminUserInfo); list($ret, $adminUser) = GalleryCoreApi::loadEntitiesById($userId, 'GalleryUser'); if ($ret) { if ($fallback) { /* Initialize a GalleryUser with the id of a real admin */ $gallery->debug('Unable to load admin user. Using in-memory user object as fallback'); GalleryCoreApi::requireOnce('modules/core/classes/GalleryUser.class'); $adminUser = new GalleryUser(); $adminUser->setId((int) $userId); $adminUser->setUserName($userName); } else { return $ret; } } $gallery->setActiveUser($adminUser); $session =& $gallery->getSession(); $session->put('isUpgrade', true); return null; }