<commit_before><?php
/**
 * This file is part of modcasts.
 *
 * (c) 2010 <NAME>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

namespace Modcasts\Controller;

use Modcasts\Controller,
	Modcasts\Environment;

use Symfony\Components\RequestHandler\Request,
	Symfony\Components\RequestHandler\Response;

class BackendController extends Controller {
	private $session;
	
	public function __construct(Request $request, Environment $env) {
		parent::__construct($request, $env);
		$this->session = $this->env->getSession();
	}
	
	public function indexAction() {
		$this->loginCheckAndRedirect();
		
		return $this->render('backend/index.html');
	}
	
	public function listEpisodesAction() {
		$this->loginCheckAndRedirect();
		
		$repository = $this->env->em->getRepository('Modcasts\Entities\Episode');
		$episodes = $repository->findAllDesc();
		
		return $this->render('backend/listEpisodes.html', array(
			'episodes'	=> $episodes,
		));
	}
	
	public function editEpisodeAction($id) {
		$this->loginCheckAndRedirect();
		
		$episode = $this->env->em->find('Modcasts\Entities\Episode', $id);
		
		if ( ! $episode) {
			throw new \Modcasts\FileNotFoundException;
		}
		
		$artists = $this->env->em->getRepository('Modcasts\Entities\Artist')
			->findAll();
		$licenses = $this->env->em->getRepository('Modcasts\Entities\License')
			->findAll();
		
		$errors = array();
		
		if ($this->request->get('submit')) {
			$errors = $this->processCheckAndPersistEpisode($episode);
		}
		
		return $this->render('backend/editEpisode.html', array(
			'formAction'		=> $this->request->getBasePath() . '/backend/editEpisode/' . $episode->id,
			'episode'		=> $episode,
			'artists'		=> $artists,
			'licenses'		=> $licenses,
			'errors'		=> $errors,
		));
	}
	
	public function newEpisodeAction() {
		$this->loginCheckAndRedirect();
		
		$episode = new \Modcasts\Entities\Episode;
		$episode->id = $this->env->em->getRepository('Modcasts\Entities\Episode')
			->getNextId();
		
		$artists = $this->env->em->getRepository('Modcasts\Entities\Artist')
			->findAll();
		$licenses = $this->env->em->getRepository('Modcasts\Entities\License')
			->findAll();
		
		$errors = array();
		
		if ($this->request->get('submit')) {
			$errors = $this->processCheckAndPersistEpisode($episode);
		}
		
		return $this->render('backend/newEpisode.html', array(
			'formAction'		=> $this->request->getBasePath() . '/backend/newEpisode',
			'displayIdField'	=> true,
			'episode'		=> $episode,
			'artists'		=> $artists,
			'licenses'		=> $licenses,
			'errors'		=> $errors,
		));
	}
	
	public function processCheckAndPersistEpisode($episode) {
		$episode->title = (string) $this->request->get('title');
		$episode->show_notes = (string) $this->request->get('show_notes');
		$episode->theme_artist = $this->findArtist((int) $this->request->get('theme_artist'));
		$episode->theme_license = $this->findLicense((int) $this->request->get('theme_license'));
		
		if ($file_bytes = (int) $this->request->get('file_bytes')) {
			$episode->file_bytes = $file_bytes;
		}
		
		$errors = $episode->validate();
		
		if ( ! sizeof($errors)) {
			$this->env->em->persist($episode);
			$this->env->em->flush();
			throw new \Modcasts\RedirectException('backend/listEpisodes');
		}
		
		return $errors;
	}
	
	public function deleteEpisodeAction($id) {
		$this->loginCheckAndRedirect();
		
		$episode = $this->env->em->find('Modcasts\Entities\Episode', $id);
		
		if ( ! $episode) {
			throw new \Modcasts\FileNotFoundException;
		}
		
		$this->env->em->remove($episode);
		$this->env->em->flush();
		throw new \Modcasts\RedirectException('backend/listEpisodes');
	}
	
	public function loginAction() {
		if ($this->isLoggedIn()) {
			throw new \Exception("You are already logged in.");
		}
		
		$username = '';
		$error = false;
		
		if ($this->request->get('submit')) {
			$username = (string) $this->request->get('username');
			$password = (string) $this->request->get('password');

			if ($account = $this->authenticate($username, $password)) {
				$this->session->write('account_id', $account->id);
				throw new \Modcasts\RedirectException('backend');
			}

			$error = true;
		}
		
		return $this->render('backend/login.html', array(
			'username'	=> $username,
			'error'		=> $error,
		));
	}
	
	public function authenticate($username, $password) {
		$repository = $this->env->em->getRepository('Modcasts\Entities\Account');
		$account = $repository->findOneBy(array(
			1	=> $username,
			2	=> hash('sha256', $password),
		));
		return $account;
	}
	
	public function loginCheckAndRedirect() {
		if ( ! $this->isLoggedIn()) {
			throw new \Modcasts\RedirectException('backend/login');
		}
	}
	
	public function isLoggedIn() {
		return $this->session->read('account_id');
	}
	
	public function findArtist($id) {
		$artist = $this->env->em->find('Modcasts\Entities\Artist', $id);
		return $artist;
	}
	
	public function findLicense($id) {
		$license = $this->env->em->find('Modcasts\Entities\License', $id);
		return $license;
	}
}<commit_msg>fix authentication<commit_after><?php
/**
 * This file is part of modcasts.
 *
 * (c) 2010 <NAME>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

namespace Modcasts\Controller;

use Modcasts\Controller,
	Modcasts\Environment;

use Symfony\Components\RequestHandler\Request,
	Symfony\Components\RequestHandler\Response;

class BackendController extends Controller {
	private $session;
	
	public function __construct(Request $request, Environment $env) {
		parent::__construct($request, $env);
		$this->session = $this->env->getSession();
	}
	
	public function indexAction() {
		$this->loginCheckAndRedirect();
		
		return $this->render('backend/index.html');
	}
	
	public function listEpisodesAction() {
		$this->loginCheckAndRedirect();
		
		$repository = $this->env->em->getRepository('Modcasts\Entities\Episode');
		$episodes = $repository->findAllDesc();
		
		return $this->render('backend/listEpisodes.html', array(
			'episodes'	=> $episodes,
		));
	}
	
	public function editEpisodeAction($id) {
		$this->loginCheckAndRedirect();
		
		$episode = $this->env->em->find('Modcasts\Entities\Episode', $id);
		
		if ( ! $episode) {
			throw new \Modcasts\FileNotFoundException;
		}
		
		$artists = $this->env->em->getRepository('Modcasts\Entities\Artist')
			->findAll();
		$licenses = $this->env->em->getRepository('Modcasts\Entities\License')
			->findAll();
		
		$errors = array();
		
		if ($this->request->get('submit')) {
			$errors = $this->processCheckAndPersistEpisode($episode);
		}
		
		return $this->render('backend/editEpisode.html', array(
			'formAction'		=> $this->request->getBasePath() . '/backend/editEpisode/' . $episode->id,
			'episode'		=> $episode,
			'artists'		=> $artists,
			'licenses'		=> $licenses,
			'errors'		=> $errors,
		));
	}
	
	public function newEpisodeAction() {
		$this->loginCheckAndRedirect();
		
		$episode = new \Modcasts\Entities\Episode;
		$episode->id = $this->env->em->getRepository('Modcasts\Entities\Episode')
			->getNextId();
		
		$artists = $this->env->em->getRepository('Modcasts\Entities\Artist')
			->findAll();
		$licenses = $this->env->em->getRepository('Modcasts\Entities\License')
			->findAll();
		
		$errors = array();
		
		if ($this->request->get('submit')) {
			$errors = $this->processCheckAndPersistEpisode($episode);
		}
		
		return $this->render('backend/newEpisode.html', array(
			'formAction'		=> $this->request->getBasePath() . '/backend/newEpisode',
			'displayIdField'	=> true,
			'episode'		=> $episode,
			'artists'		=> $artists,
			'licenses'		=> $licenses,
			'errors'		=> $errors,
		));
	}
	
	public function processCheckAndPersistEpisode($episode) {
		$episode->title = (string) $this->request->get('title');
		$episode->show_notes = (string) $this->request->get('show_notes');
		$episode->theme_artist = $this->findArtist((int) $this->request->get('theme_artist'));
		$episode->theme_license = $this->findLicense((int) $this->request->get('theme_license'));
		
		if ($file_bytes = (int) $this->request->get('file_bytes')) {
			$episode->file_bytes = $file_bytes;
		}
		
		$errors = $episode->validate();
		
		if ( ! sizeof($errors)) {
			$this->env->em->persist($episode);
			$this->env->em->flush();
			throw new \Modcasts\RedirectException('backend/listEpisodes');
		}
		
		return $errors;
	}
	
	public function deleteEpisodeAction($id) {
		$this->loginCheckAndRedirect();
		
		$episode = $this->env->em->find('Modcasts\Entities\Episode', $id);
		
		if ( ! $episode) {
			throw new \Modcasts\FileNotFoundException;
		}
		
		$this->env->em->remove($episode);
		$this->env->em->flush();
		throw new \Modcasts\RedirectException('backend/listEpisodes');
	}
	
	public function loginAction() {
		if ($this->isLoggedIn()) {
			throw new \Exception("You are already logged in.");
		}
		
		$username = '';
		$error = false;
		
		if ($this->request->get('submit')) {
			$username = (string) $this->request->get('username');
			$password = (string) $this->request->get('password');

			if ($account = $this->authenticate($username, $password)) {
				$this->session->write('account_id', $account->id);
				throw new \Modcasts\RedirectException('backend');
			}

			$error = true;
		}
		
		return $this->render('backend/login.html', array(
			'username'	=> $username,
			'error'		=> $error,
		));
	}
	
	public function authenticate($username, $password) {
		$repository = $this->env->em->getRepository('Modcasts\Entities\Account');
		$account = $repository->findOneBy(array(
			'username'	=> $username,
			'password'	=> hash('<PASSWORD>', $password),
		));
		return $account;
	}
	
	public function loginCheckAndRedirect() {
		if ( ! $this->isLoggedIn()) {
			throw new \Modcasts\RedirectException('backend/login');
		}
	}
	
	public function isLoggedIn() {
		return $this->session->read('account_id');
	}
	
	public function findArtist($id) {
		$artist = $this->env->em->find('Modcasts\Entities\Artist', $id);
		return $artist;
	}
	
	public function findLicense($id) {
		$license = $this->env->em->find('Modcasts\Entities\License', $id);
		return $license;
	}
}
{'fraction_non_alphanumeric': 0.18735869458370197, 'fraction_numerical': 0.0012778924604344834, 'mean_word_length': 4.159229208924949, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 256, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 38, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1670239', 'n_tokens_mistral': 3924, 'n_tokens_neox': 3359, 'n_words': 569}