/** * List all the available settings * * @return void */ public function action_index() { $this->template->header->title = __('Application Settings'); $this->settings_content = View::factory('pages/settings/main')->bind('action_url', $action_url); $this->active = 'main'; $action_url = URL::site('settings/main/manage'); // Setting items $settings = array('site_name' => '', 'site_locale' => '', 'public_registration_enabled' => '', 'anonymous_access_enabled' => '', 'river_active_duration' => '', 'river_expiry_notice_period' => ''); if ($this->request->post()) { // Setup validation for the application settings $validation = Validation::factory($this->request->post())->rule('site_name', 'not_empty')->rule('site_locale', 'not_empty')->rule('river_active_duration', 'not_empty')->rule('river_active_duration', 'digit')->rule('river_expiry_notice_period', 'not_empty')->rule('river_expiry_notice_period', 'digit')->rule('form_auth_token', array('CSRF', 'valid')); if ($validation->check()) { // Set the setting key values $settings = array('site_name' => $this->request->post('site_name'), 'site_locale' => $this->request->post('site_locale'), 'public_registration_enabled' => $this->request->post('public_registration_enabled') == 1, 'anonymous_access_enabled' => $this->request->post('anonymous_access_enabled') == 1, 'river_active_duration' => $this->request->post('river_active_duration'), 'river_expiry_notice_period' => $this->request->post('river_expiry_notice_period')); // Update the settings Model_Setting::update_settings($settings); $this->settings_content->set('messages', array(__('The site settings have been updated.'))); } else { $this->settings_content->set('errors', $validation->errors('user')); } } $this->settings_content->settings = Model_Setting::get_settings(array_keys($settings)); }
/** * Perform the maintenance */ public function action_run() { if (php_sapi_name() !== 'cli') { Kohana::$log->add(Log::ERROR, __("Maintenance must be run in CLI mode")); exit; } Kohana::$log->add(Log::INFO, __("Running river maintenance schedule")); // Get settings $settings = Model_Setting::get_settings(array('river_active_duration', 'river_expiry_notice_period', 'site_url')); $notice_period = $settings['river_expiry_notice_period']; $site_url = $settings['site_url']; // Templates for the notifications $warning_template = View::factory('emails/expiry_warning'); $notice_template = View::factory('emails/expiry_notice'); // Fix the current date to the time when the maintenance // is being run $current_date_timestamp = time(); $current_date = date("Y-m-d H:i:s", $current_date_timestamp); // Compute the filter date $filter_date_timestamp = strtotime(sprintf("+%s day", $notice_period), $current_date_timestamp); $filter_date = date("Y-m-d H:i:s", $filter_date_timestamp); // Get the rivers that have expired or are about to expire $candidates = ORM::factory('river')->where('river_expired', '=', 0)->where('river_date_expiry', '<=', $filter_date)->find_all(); $to_be_expired = array(); $to_be_flagged = array(); $rivers = array(); foreach ($candidates as $river) { $days_to_expiry = $river->get_days_to_expiry($current_date); $river_url = $site_url . $river->get_base_url(); // Generate extension token and modify the URL if ($days_to_expiry === 0) { $token = hash("sha256", Text::random('alnum', 32)); $river_url .= '/extend?token=' . $token; $to_be_expired[$river->id] = $token; } else { // Is the river to be flagged for expiry if ($days_to_expiry > 0 and $river->expiry_candidate == 0) { $to_be_flagged[] = $river->id; } else { continue; } } $rivers[$river->id] = array('river_name' => $river->river_name, 'river_url' => $river_url, 'days_to_expiry' => $days_to_expiry); } // If no rivers found, terminate if (count($rivers) == 0) { Kohana::$log->add(Log::INFO, __("No rivers found. Exiting...")); return; } // Get the owners for each of the rivers $river_owners = $this->_get_river_owners(array_keys($rivers)); // Expire rivers if (count($to_be_expired) > 0) { $this->_expire_rivers($to_be_expired); } // Switch on the expiry flag if (count($to_be_flagged) > 0) { DB::update('rivers')->set(array('expiry_candidate' => 1))->where('id', 'IN', $to_be_flagged)->execute(); } // Send out notifications Kohana::$log->add(Log::INFO, __("Sending out notifications")); foreach ($river_owners as $river_id => $owners) { $data = $rivers[$river_id]; // Mail subject $subject = __("Your :river_name river will shutdown in :days_to_expiry day(s)!", array(":river_name" => $data['river_name'], ":days_to_expiry" => $data['days_to_expiry'])); // Mail body - expiry warning is the default $mail_body = $warning_template->set(array('river_name' => $data['river_name'], 'days_to_expiry' => $data['days_to_expiry'], 'active_duration' => $settings['river_active_duration'], 'river_url' => $data['river_url'])); if ($data['days_to_expiry'] === 0) { $subject = __("Your :river_name has shutdown!", array(":river_name" => $data['river_name'])); // Expiry notice message $mail_body = $notice_template->set(array('river_name' => $data['river_name'], 'active_duration' => $settings['river_active_duration'], 'activation_url' => $data['river_url'])); } // Construct the mail body foreach ($owners as $owner) { $mail_body->recipient_name = $owner['name']; Swiftriver_Mail::send($owner['email'], $subject, $mail_body); } } Kohana::$log->add(Log::INFO, "Completed maintenance schedule"); }