Bin will be removed on June 1, 2014 in favor of http://gist.github.com. Please save all your pastes before then. So long, and thanks for all the fish!×
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
/* SVN FILE: $Id: pastes_controller.php 4 2007-08-22 18:52:29Z jonathan $ */
/**
 * PHP versions 4 and 5
 *
 * CakeBin -- a paste application built on CakePHP framework.
 * Copyright (c) 2005 Garrett J. Woodworth : gwoo@rd11.com
 * rd11,inc : http://rd11.com
 *
 * @version         $Revision: 4 $
 * @modifiedby      $LastChangedBy: jonathan $
 * @lastmodified    $Date: 2007-08-22 14:52:29 -0400 (Wed, 22 Aug 2007) $
 * @license         OPPL
 */
uses('sanitize');
class PastesController extends AppController
{
    var $name = 'Pastes';
    var $helpers = array('Html','Ajax','Javascript','Time','Pagination');
    var $uses = array('Paste');
 
    var $sanitize;
 
    var $show;
    var $sortBy;
    var $direction;
    var $page;
    var $order;
           
    function __construct()
    {
        $this->sanitize = new Sanitize;
        $this->show = empty($_GET['show'])? '10': $this->sanitize->paranoid($_GET['show']);
        $this->sortBy = empty($_GET['sort'])? 'created': $this->sanitize->paranoid($_GET['sort']);
        $this->direction = empty($_GET['direction'])? 'desc': $this->sanitize->paranoid($_GET['direction']);
        $this->page = empty($_GET['page'])? '1': $this->sanitize->paranoid($_GET['page']);
        $this->order = $this->sortBy.' '.strtoupper($this->direction);
        parent::__construct();
    }
 
    function index()
    {
        $this->redirect('/pastes/add');
    }
   
    function add()
    {
        if (empty($this->params['data']))
        {
            $this->set('languages',$this->_languages());//set geshi languages
            $this->render();
           
            $this->_purge();//get rid of unsaved pastees
        }
        else
        {
            $this->params['data']['Paste']['id'] = md5(uniqid(rand(), true));
            $this->params['data']['Paste']['created'] = date('Y-m-d H:i:s');
            if ($this->Paste->save($this->params['data']))
            {
                $this->redirect('/pastes/show/'. $this->params['data']['Paste']['id']);
            }
            else
            {
                $this->set('languages',$this->_languages());//set geshi languages
                $this->set('data', $this->params['data']);
                $this->validateErrors($this->Paste);
                $this->render();
            }
        }
    }
 
    function edit($id=null)
    {
        if (empty($this->params['data']))
        {
            $this->Paste->id = $id;
            $this->params['data'] = $this->Paste->read();
            $this->set('languages',$this->_languages());//set geshi languages
            $this->set('data', $this->params['data']);
            $this->render();
        }
        else
        {
            if ($this->Paste->save($this->params['data']))
            {
                $this->redirect('/pastes/show/'. $this->params['data']['Paste']['id']);
            }
            else
            {
                $this->set('languages',$this->_languages());//set geshi languages
                $this->set('data', $this->params['data']);
                $this->validateErrors($this->Paste);
                $this->render();
            }
        }
    }
 
    function saved()
    {
        $data = $this->Paste->findAll($criteria='save=1', $fields=null, $this->order, $this->show, $this->page);
 
        $paging['style'] = 'ajax';
        $paging['link'] = '/bare/pastes/saved/?show='.$this->show.'&sort='.$this->sortBy.'&direction='.$this->direction.'&page=';
       
        $paging['count'] = $this->Paste->findCount($criteria='save=1','1000');
        $paging['show'] = array('10','25','50');
        $paging['page'] = $this->page;
        $paging['limit'] = $this->show;
        $paging['direction'] = $this->direction;
       
        $this->set('paging',$paging);
        $this->set('data',$data);
 
        $this->_purge();//get rid of unsaved pastees
    }
     
    function show($id)
    {
     
        $this->Paste->id = $id;
        $data = $this->Paste->read();
        vendor('geshi');
        $geshi = new GeSHi($data['Paste']['paste'], $data['Paste']['lang'],ROOT.'/vendors/geshi');
       
        $this->params['data'] = $data;  // so we can populate update form
        $this->set('geshi', $geshi );
        $this->set('languages',$this->_languages());//set geshi languages
        $this->set('paste', $data['Paste'] );
    }
 
    function delete($id)
    {
        if ($this->Paste->del($id))
        {
            $this->action = 'grid';
            $this->set('message','paste '.$id.' deleted');
            $this->saved();
            $this->render('saved','ajax');
        }
    }
   
    function _purge()
    {
        $data = $this->Paste->findAll($criteria="save!='1' AND created < CURDATE()");
   
        foreach($data as $paste)
        {
            $this->Paste->del($paste['Paste']['id']);
        }
    }
 
    function _languages()
    {
        $dir = opendir(ROOT.'/vendors/geshi');
        $languages = array();
        while ( $file = readdir($dir) )
        {
            if ( $file == '..' || $file == '.' || !stristr($file, '.') || $file == 'css-gen.cfg' ) continue;
            $lang = substr($file, 0,  strpos($file, '.'));
           
            $languages[$lang] = $lang;
        }
        closedir($dir);
        asort($languages);
       
        return $languages;
    }
 
}
 
?>