protected function set_current_user() { $user = null; $AnonymousUser = array('id' => 0, 'level' => 0, 'name' => "Anonymous", 'show_samples' => true, 'language' => '', 'secondary_languages' => '', 'pool_browse_mode' => 1, 'always_resize_images' => true, 'ip_addr' => $this->request()->remoteIp()); if (!current_user() && $this->session()->user_id) { $user = User::where(['id' => $this->session()->user_id])->first(); } else { if ($this->cookies()->login && $this->cookies()->pass_hash) { $user = User::authenticate_hash($this->cookies()->login, $this->cookies()->pass_hash); } elseif (isset($this->params()->login) && isset($this->params()->password_hash)) { $user = User::authenticate($this->params()->login, $this->params()->password_hash); } elseif (isset($this->params()->user['name']) && isset($this->params()->user['password'])) { $user = User::authenticate($this->params()->user['name'], $this->params()->user['password']); } $user && $user->updateAttribute('last_logged_in_at', date('Y-m-d H:i:s')); } if ($user) { if ($user->is_blocked() && $user->ban && $user->ban->expires_at < date('Y-m-d H:i:s')) { $user->updateAttribute('level', CONFIG()->starting_level); Ban::destroyAll("user_id = " . $user->id); } $this->session()->user_id = $user->id; } else { $user = new User(); $user->assignAttributes($AnonymousUser, ['without_protection' => true]); } User::set_current_user($user); $this->current_user = $user; # For convenient access in activerecord models $user->ip_addr = $this->request()->remoteIp(); Moebooru\Versioning\Versioning::init_history(); if (!current_user()->is_anonymous()) { current_user()->log($this->request()->remoteIp()); } }
public function execute_external_data_search() { # current_user will be needed to save post history. # Set the first admin as current user. User::set_current_user(User::where('level = ?', CONFIG()->user_levels['Admin'])->first()); if (empty($this->data->last_post_id)) { $this->data->last_post_id = 0; } $post_id = $this->data->last_post_id + 1; $config = array_merge(['servers' => [], 'interval' => 3, 'source' => true, 'merge_tags' => true, 'limit' => 100, 'set_rating' => false, 'exclude_tags' => [], 'similarity' => 90], CONFIG()->external_data_search_config); $limit = $config['limit']; $interval = $config['interval']; $search_options = ['type' => 'post', 'data_search' => true, 'services' => $config['servers'], 'threshold' => $config['similarity']]; $post_count = !$limit ? -1 : 0; while ($post_count < $limit) { if (!($post = Post::where('id >= ? AND status != "deleted"', $post_id)->order('id ASC')->first())) { break; } $search_options['source'] = $post; $new_tags = []; $source = null; $external_posts = SimilarImages::similar_images($search_options)['posts_external']; $rating_set = false; foreach ($external_posts as $ep) { if (!$rating_set && $config['set_rating'] && $ep->rating) { $post->rating = $ep->rating; $rating_set = true; } if ($config['source'] && !$source && $ep->source) { $source = $ep->source; } $new_tags = array_merge($new_tags, explode(' ', $ep->tags)); } # Exclude tags. $new_tags = array_diff($new_tags, $config['exclude_tags']); if ($config['merge_tags']) { $new_tags = array_merge($new_tags, $post->tags); } $new_tags = array_filter(array_unique($new_tags)); $post->new_tags = $new_tags; if ($source) { } $post->source = $source; $post->save(); if ($limit) { $post_count++; } $this->update_data(['last_post_id' => $post->id]); $post_id = $post->id + 1; if ($config['interval']) { sleep($config['interval']); } } }