Skip to content

Instantly share code, notes, and snippets.

@moos3
Created August 16, 2011 17:49
Show Gist options
  • Select an option

  • Save moos3/1149702 to your computer and use it in GitHub Desktop.

Select an option

Save moos3/1149702 to your computer and use it in GitHub Desktop.
hrm
<?php
use Twitter\Tweet;
class Controller_Pastes extends Controller_Template {
public function action_new(){
if(\Input::post('paste') !== null){
$paste = \Input::post('paste');
$type = \Input::post('language');
$title = \Input::post('title');
// Did they leave their paste empty?
if(empty($paste)){
\Session::set_flash('notice', 'Your paste cannot be empty!');
}
// All in good fun..
elseif(\Input::post('do-not-fill-this-out') != '' and \Input::post('do-not-fill-this-out') != date('Ynj')){
\Session::set_flash('notice', 'I told you not to fill it out..');
}
// Is the type they provided real?
elseif(!isset(\Pastecode::$languages[$type])){
\Session::set_flash('notice', 'Please choose a valid language type');
}
elseif(mb_strlen($paste, '8bit') > 64000){
\Session::set_flash('notice', 'Pastes must be less than or equal to 500 KiB');
}
// Good to go!
else{
if (empty($title)){
// give it a random ass title
}
$user_id = null;
// if ($this->template->logged_in){
// $user_id = Session::get('user_id');
// }
$properties = array(
'ip_address' => \Input::real_ip(),
'type' => $type,
'contents' => $paste,
'title' => $title,
'user_id' => $user_id,
'private' => (\Input::post('private') ? 1 : 0),
'hash' => \Model_Paste::generateHash(\Input::real_ip(),$type,$title,\Input::post('private')),
'views' => 0,
'downloads' => 0
);
$paste = \Model_Paste::factory($properties);
if($paste->save()){
#$permalink = \Base::encode($paste->id);
\Response::redirect($paste->hash);
}else{
\Session::set_flash('notice', 'There was an issue saving your paste, please try again!');
}
}
}
$this->template->set_global('title', 'New Paste');
$this->template->content = View::factory('pastes/form', array('type' => 'php'));
}
public function action_recent(){
$data = array('results'=>\Model_Paste::find_recent());
$this->template->set_global('title','Recent Pastes');
$this->template->content = View::factory('pastes/recent',$data);
}
public function action_list(){
if (! $this->template->logged_in){
Response::redirect('twitter/login');
}
$pastes = Model_Pastes::find_all_by_user_id(Session::get('user_id'));
$this->template->title = 'My Clips';
$this->tempalte->content = View::factory('pastes/list',array('pastes'=>$pastes));
}
/**
* Reply to Paste
*
* @param string
* @return void
*/
public function action_reply($short_id = null)
{
if(!empty($short_id))
{
if(($paste = \Model_Paste::find_paste($short_id)) !== false)
{
$_POST['paste'] = $paste->contents;
$this->template->set_global('title', 'Reply to Paste '.$short_id);
$this->template->content = View::factory('pastes/form', array('type' => $paste->type));
return;
}
}
\Response::redirect('/');
}
/**
* Raw Paste
*
* @param string
* @return void
*/
public function action_raw($short_id = null)
{
if(!empty($short_id))
{
if(($paste = \Model_Paste::find_paste($short_id)) !== false)
{
$paste->downloads = $paste->downloads + 1;
$paste->save();
$this->auto_render = false;
$this->response->set_header('Content-Type', 'text/plain');
$this->response->body = $paste->contents;
return;
}
}
$this->action_404();
}
/**
* Download Paste
*
* @param string
* @return void
*/
public function action_download($short_id = null)
{
if(!empty($short_id))
{
if(($paste = \Model_Paste::find_paste($short_id)) !== false)
{
$paste->downloads = $paste->downloads + 1;
$paste->save();
$this->auto_render = false;
$this->response->set_header('Content-Type', 'text/plain');
if (end(explode('.',$paste->title)) == $paste->type){
$this->response->set_header('Content-Disposition', 'attachment; filename="'.$paste->title.'"');
}else{
$this->response->set_header('Content-Disposition','attachment; filename="'.$paste->title.$paste->type.'"');
}
$this->response->body = $paste->contents;
return;
}
}
$this->action_404();
}
/**
* The 404 action for the application.
*
* @access public
* @return void
*/
public function action_404()
{
$uri = \Uri::detect();
$uri = array_merge(array(), array_filter(explode('/', $uri)));
if(count($uri) === 1 and ctype_alnum($uri[0]))
{
$short_id = $uri[0];
if(($paste = \Model_Paste::find_paste($short_id)) !== false)
{
\Widget::add('widgets/social');
$this->template->set_global('title', $short_id);
$this->template->set_global('page_title', '
Syntax: '.\Pastecode::$languages[$paste->type].' <span>|</span>
Size: '.Num::format_bytes(mb_strlen($paste->contents, '8bit')).' <span>|</span>
'.\Date::factory($paste->created_at)->format('us_named').'
', false);
$this->template->content = View::factory('pastes/view');
$this->template->content->type = $paste->type == 'NULL' ? 'php' : $paste->type;
$this->template->content->code = $paste->contents;
$this->template->content->short_id = $short_id;
return;
}
}
$this->response->status = 404;
$this->template->set_global('title', '404 Page Not Found');
$this->template->content = View::factory('pastes/404');
}
}
<?php echo Form::open('/pastes/new/'); ?>
<div id="masthead">
<p class="language">
<label for="language">Choose your language:</label>
<?php echo Form::select('language', (isset($type) ? $type : 'php'), Pastecode::$languages, array('id' => 'language')); ?>
</p>
<h1><?php echo (isset($title) ? $title : 'New Paste'); ?></h1>
</div>
<input type="hidden" id="do-not-fill-this-out" name="do-not-fill-this-out" value="">
<?php echo \Form::textarea('paste', \Input::post('paste'), array('class' => 'pastebox', 'rows' => 22, 'cols' => 40, 'tabindex' => 1)); ?>
<div class="actions" >
<span class="title">
<label for="title">File Name:</label>
<?php echo \Form::input('title',\Input::post('title'), array('class' => 'filename')); ?>
</span>
<span class="private">
<input type="checkbox" title="Check this box to make this paste private" name="private" id="private" value="1">
<label for="private"><?php echo Asset::img('icons/lock.png', array('title' => 'Check this box to make this paste private')); ?></label>
</span>
<?php echo \Form::button('submit','Paste'); ?>
<!-- <button type="submit">Paste</button> -->
</div>
<?php echo Form::close(); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment