Esempio n. 1
0
 static function change_provider($new_provider)
 {
     $current_provider = module::get_var("gallery", "identity_provider");
     if (!empty($current_provider)) {
         module::uninstall($current_provider);
     }
     try {
         IdentityProvider::reset();
         $provider = new IdentityProvider($new_provider);
         module::set_var("gallery", "identity_provider", $new_provider);
         if (method_exists("{$new_provider}_installer", "initialize")) {
             call_user_func("{$new_provider}_installer::initialize");
         }
         module::event("identity_provider_changed", $current_provider, $new_provider);
         auth::login($provider->admin_user());
         Session::instance()->regenerate();
     } catch (Exception $e) {
         static $restore_already_running;
         // In case of error, make an attempt to restore the old provider.  Since that's calling into
         // this function again and can fail, we should be sure not to get into an infinite recursion.
         if (!$restore_already_running) {
             $restore_already_running = true;
             // Make sure new provider is not in the database
             module::uninstall($new_provider);
             // Lets reset to the current provider so that the gallery installation is still
             // working.
             module::set_var("gallery", "identity_provider", null);
             IdentityProvider::change_provider($current_provider);
             module::activate($current_provider);
             message::error(t("Error attempting to enable \"%new_provider\" identity provider, " . "reverted to \"%old_provider\" identity provider", array("new_provider" => $new_provider, "old_provider" => $current_provider)));
             $restore_already_running = false;
         }
         throw $e;
     }
 }
 static function uninstall()
 {
     // Delete all groups so that we give other modules an opportunity to clean up
     $ldap_provider = new IdentityProvider("ldap");
     foreach ($ldap_provider->groups() as $group) {
         module::event("group_deleted", $group);
     }
 }
Esempio n. 3
0
 static function change_provider($new_provider)
 {
     if (!identity::active_user()->admin && PHP_SAPI != "cli") {
         // Below, the active user is set to the primary admin.
         access::forbidden();
     }
     $current_provider = module::get_var("gallery", "identity_provider");
     if (!empty($current_provider)) {
         module::uninstall($current_provider);
     }
     try {
         IdentityProvider::reset();
         $provider = new IdentityProvider($new_provider);
         module::set_var("gallery", "identity_provider", $new_provider);
         if (class_exists("{$new_provider}_installer") && method_exists("{$new_provider}_installer", "initialize")) {
             call_user_func("{$new_provider}_installer::initialize");
         }
         if (!$provider->admin_user()) {
             throw new Exception("IdentityProvider {$new_provider}: Couldn't find the admin user!");
         }
         module::event("identity_provider_changed", $current_provider, $new_provider);
         identity::set_active_user($provider->admin_user());
         Session::instance()->regenerate();
     } catch (Exception $e) {
         static $restore_already_running;
         // In case of error, make an attempt to restore the old provider.  Since that's calling into
         // this function again and can fail, we should be sure not to get into an infinite recursion.
         if (!$restore_already_running) {
             $restore_already_running = true;
             // Make sure new provider is not in the database
             try {
                 module::uninstall($new_provider);
             } catch (Exception $e2) {
                 Kohana_Log::add("error", "Error uninstalling failed new provider\n" . $e2->getMessage() . "\n" . $e2->getTraceAsString());
             }
             try {
                 // Lets reset to the current provider so that the gallery installation is still
                 // working.
                 module::set_var("gallery", "identity_provider", null);
                 IdentityProvider::change_provider($current_provider);
                 module::activate($current_provider);
             } catch (Exception $e2) {
                 Kohana_Log::add("error", "Error restoring original identity provider\n" . $e2->getMessage() . "\n" . $e2->getTraceAsString());
             }
             message::error(t("Error attempting to enable \"%new_provider\" identity provider, reverted to \"%old_provider\" identity provider", array("new_provider" => $new_provider, "old_provider" => $current_provider)));
             $restore_already_running = false;
         }
         throw $e;
     }
 }
 static function initialize()
 {
     module::set_version("ldap", 1);
     $root = item::root();
     foreach (IdentityProvider::instance()->groups() as $group) {
         module::event("group_created", $group);
         access::allow($group, "view", $root);
         access::allow($group, "view_full", $root);
     }
 }
Esempio n. 5
0
 /**
  * @see IdentityProvider_Driver::remove_user_to_group.
  */
 static function remove_user_from_group($user, $group_id)
 {
     return IdentityProvider::instance()->remove_user_from_group($user, $group_id);
 }
Esempio n. 6
0
 static function install()
 {
     IdentityProvider::change_provider("user");
 }
Esempio n. 7
0
 static function activate()
 {
     IdentityProvider::change_provider("user");
     // Set the latest version in initialize() below
 }
Esempio n. 8
0
 static function cron()
 {
     $owner_id = 2;
     $debug = !empty($_SERVER['argv']) && isset($_SERVER['argv'][1]) && $_SERVER['argv'][1] == "debug";
     // Login as Admin
     $debug and print "Starting user session\n";
     $session = Session::instance();
     $session->delete("user");
     auth::login(IdentityProvider::instance()->admin_user());
     // check if some folders are still unprocessed from previous run
     $entry = ORM::factory("folder_sync_entry")->where("is_directory", "=", 1)->where("checked", "=", 0)->order_by("id", "ASC")->find();
     if (!$entry->loaded()) {
         $debug and print "Adding default folders\n";
         $paths = unserialize(module::get_var("folder_sync", "authorized_paths"));
         foreach (array_keys($paths) as $path) {
             if (folder_sync::is_valid_path($path)) {
                 $path = rtrim($path, "/");
                 $debug and print " * {$path}\n";
                 $entry = ORM::factory("folder_sync_entry")->where("is_directory", "=", 1)->where("path", "=", $path)->find();
                 if ($entry && $entry->loaded()) {
                     $entry->checked = 0;
                     $entry->save();
                 } else {
                     $entry = ORM::factory("folder_sync_entry");
                     $entry->path = $path;
                     $entry->is_directory = 1;
                     $entry->parent_id = null;
                     $entry->item_id = module::get_var("folder_sync", "destination_album_id", 1);
                     $entry->md5 = '';
                     $entry->save();
                 }
             }
         }
     }
     // Scan and add files
     $debug and print "Starting the loop\n";
     $done = false;
     $limit = 500;
     while (!$done && $limit > 0) {
         $debug and print "Loop started: Limit = {$limit}\n";
         $entry = ORM::factory("folder_sync_entry")->where("is_directory", "=", 1)->where("checked", "=", 0)->order_by("id", "ASC")->find();
         if ($entry->loaded()) {
             // get the parrent
             $parent = ORM::factory("item", $entry->item_id);
             if (!$parent->loaded()) {
                 $debug and print "Deleting entry #{$entry->id} pointing to missing item #{$entry->item_id}\n";
                 //$entry->delete();
                 //continue;
             }
             $debug and print "Scanning folder: {$entry->path}\n";
             $child_paths = glob(preg_quote($entry->path) . "/*");
             if (!$child_paths) {
                 $child_paths = glob("{$entry->path}/*");
             }
             foreach ($child_paths as $child_path) {
                 $name = basename($child_path);
                 $title = item::convert_filename_to_title($name);
                 $debug and print "Found {$child_path}...";
                 if (is_dir($child_path)) {
                     $debug and print "folder\n";
                     $entry_exists = ORM::factory("folder_sync_entry")->where("is_directory", "=", 1)->where("path", "=", $child_path)->find();
                     if ($entry_exists && $entry_exists->loaded()) {
                         $debug and print "Folder is already imported, marked to re-sync.\n";
                         $entry_exists->checked = 0;
                         $entry_exists->save();
                     } else {
                         $debug and print "Adding new folder.\n";
                         $album = ORM::factory("item");
                         $album->type = "album";
                         $album->parent_id = $parent->id;
                         $album->name = $name;
                         $album->title = $title;
                         $album->owner_id = $owner_id;
                         $album->sort_order = $parent->sort_order;
                         $album->sort_column = $parent->sort_column;
                         $album->save();
                         $child_entry = ORM::factory("folder_sync_entry");
                         $child_entry->path = $child_path;
                         $child_entry->parent_id = $entry->id;
                         $child_entry->item_id = $album->id;
                         $child_entry->is_directory = 1;
                         $child_entry->md5 = "";
                         $child_entry->save();
                     }
                 } else {
                     $debug and print "file\n";
                     $ext = strtolower(pathinfo($child_path, PATHINFO_EXTENSION));
                     if (!in_array($ext, legal_file::get_extensions()) || !filesize($child_path)) {
                         // Not importable, skip it.
                         $debug and print "File is incompatible. Skipping.\n";
                         continue;
                     }
                     // check if file was already imported
                     $entry_exists = ORM::factory("folder_sync_entry")->where("is_directory", "=", 0)->where("path", "=", $child_path)->find();
                     if ($entry_exists && $entry_exists->loaded()) {
                         $debug and print "Image is already imported...";
                         if (empty($entry_exists->added) || empty($entry_exists->md5) || $entry_exists->added != filemtime($child_path) || $entry_exists->md5 != md5_file($child_path)) {
                             $item = ORM::factory("item", $entry_exists->item_id);
                             if ($item->loaded()) {
                                 $item->set_data_file($child_path);
                                 $debug and print "updating.\n";
                                 try {
                                     $item->save();
                                 } catch (ORM_Validation_Exception $e) {
                                     print "Error saving the image (ID = {$item->id}) with the new data file.\n";
                                     exit;
                                 }
                             } else {
                                 $debug and print "deleting.\n";
                                 $entry_exists->delete();
                             }
                         } else {
                             $debug and print "skipping.\n";
                         }
                         // since it's an update, don't count too much towards the limit
                         $limit -= 0.25;
                     } else {
                         if (in_array($ext, legal_file::get_photo_extensions())) {
                             $debug and print "Adding new photo.\n";
                             $item = ORM::factory("item");
                             $item->type = "photo";
                             $item->parent_id = $parent->id;
                             $item->set_data_file($child_path);
                             $item->name = $name;
                             $item->title = $title;
                             $item->owner_id = $owner_id;
                             $item->save();
                         } else {
                             if (in_array($ext, legal_file::get_movie_extensions())) {
                                 $debug and print "Adding new video.\n";
                                 $item = ORM::factory("item");
                                 $item->type = "movie";
                                 $item->parent_id = $parent->id;
                                 $item->set_data_file($child_path);
                                 $item->name = $name;
                                 $item->title = $title;
                                 $item->owner_id = $owner_id;
                                 $item->save();
                             }
                         }
                         $entry_exists = ORM::factory("folder_sync_entry");
                         $entry_exists->path = $child_path;
                         $entry_exists->parent_id = $entry->id;
                         // null if the parent was a staging dir
                         $entry_exists->is_directory = 0;
                         $entry_exists->md5 = md5_file($child_path);
                         $entry_exists->added = filemtime($child_path);
                         $entry_exists->item_id = $item->id;
                         $entry_exists->save();
                         $limit--;
                     }
                 }
                 // Did we hit the limit?
                 if ($limit <= 0) {
                     $debug and print "Reached the limit. Exiting.\n";
                     exit;
                 }
             }
             // We've processed this entry unless we reached a limit.
             if ($limit > 0) {
                 $entry->checked = 1;
                 $entry->save();
             }
         } else {
             $done = true;
             $debug and print "All folders are processed. Exiting.\n";
         }
     }
     // process deletes
     if (module::get_var("folder_sync", "process_deletes", false)) {
         $entries = ORM::factory("folder_sync_entry")->order_by("id", "ASC")->find_all();
         foreach ($entries as $entry) {
             if (!file_exists($entry->path) && $entry->item_id > 1) {
                 $item = ORM::factory("item", $entry->item_id);
                 if ($item->loaded()) {
                     $item->delete();
                 }
             }
         }
     }
     exit;
 }