forked from weierophinney/pastebin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Paste.php
211 lines (190 loc) · 5.34 KB
/
Paste.php
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
/**
* Pastebin model
*
* @package Paste
* @license New BSD {@link http://framework.zend.com/license/new-bsd}
* @version $Id: $
*/
class Paste
{
/**
* Table fields
* @var array
*/
protected $_fields;
/**
* Form representation/input filter of model data
*
* @var Form_Paste
*/
protected $_form;
/**
* @var Zend_Db_Table_Abstract
*/
protected $_table;
/**
* Add a paste
*
* @param array $data
* @return string
*/
public function add(array $data)
{
$form = $this->getForm();
$belongTo = $form->getElementsBelongTo();
if (!empty($belongTo) && array_key_exists($belongTo, $data)) {
$data = $data[$belongTo];
}
if (!$form->isValid($data)) {
return false;
}
$values = $form->getValues();
if (!empty($belongTo)) {
$values = $values[$belongTo];
}
return $this->getTable()->insert($values);
}
/**
* Fetch a paste by id
*
* Returns boolean false on failure to find the paste; otherwise, a struct
* is returned.
*
* @param string $id
* @return struct|false
*/
public function get($id)
{
$table = $this->getTable();
$select = $table->select();
$select->where('id = ?', $id)
->where('expires IS NULL OR expires = "" OR expires > ?', date('Y-m-d H:i:s'));
$row = $table->fetchRow($select);
if (null == $row) {
return false;
}
$data = $row->toArray();
if (!empty($data['parent'])) {
$parent = $this->get($data['parent']);
if (!$parent) {
$data['parent'] = null;
}
}
$data['children'] = $this->_getChildren($id);
return $data;
}
/**
* Get list of active pastes, ordered by creation date (desc)
*
* @return array
*/
public function fetchActive(array $criteria = null)
{
$table = $this->getTable();
$adapter = $table->getAdapter();
$select = $adapter->select();
$select->from('paste', array('id', 'type', 'summary', 'user', 'created', 'expires'))
->where('expires IS NULL OR expires = "" OR expires > ?', date('Y-m-d H:i:s'));
if (null !== $criteria) {
$this->_refineSelection($select, $criteria);
} else {
$select->order('created DESC');
}
return $adapter->fetchAll($select);
}
/**
* Fetch count of active pastes
*
* @return int
*/
public function fetchActiveCount()
{
$table = $this->getTable();
$adapter = $table->getAdapter();
$select = $adapter->select();
$select->from('paste', array('count' => 'COUNT(*)'))
->where('expires IS NULL OR expires = "" OR expires > ?', date('Y-m-d H:i:s'))
->order('created DESC');
return $adapter->fetchOne($select);
}
/**
* Retrieve form/input filter
*
* @return Form_Paste
*/
public function getForm()
{
if (null === $this->_form) {
require_once dirname(__FILE__) . '/Form/Paste.php';
$this->_form = new Form_Paste();
}
return $this->_form;
}
/**
* Retrieve data provider
*
* @return Paste_Table
*/
public function getTable()
{
if (null === $this->_table) {
require_once dirname(__FILE__) . '/DbTable/Paste.php';
$this->_table = new DbTable_Paste();
}
return $this->_table;
}
/**
* Retrieve paste children
*
* @param string $id
* @return false|array
*/
protected function _getChildren($id)
{
$adapter = $this->getTable()->getAdapter();
$select = $adapter->select();
$select->from('paste', array('id'))
->where('parent = ?', $id)
->where('expires IS NULL OR expires = "" OR expires > ?', date('Y-m-d H:i:s'));
return $adapter->fetchCol($select);
}
/**
* Refine the active pastes selection based on criteria provided
*
* Allows setting a limit to the number of records returend
*
* @param Zend_Db_Select $select
* @param array $criteria
* @return void
*/
protected function _refineSelection(Zend_Db_Select $select, array $criteria)
{
if (array_key_exists('start', $criteria)
&& ($criteria['start'] == intval($criteria['start']))
) {
if (array_key_exists('count', $criteria)
&& ($criteria['count'] == intval($criteria['count']))
) {
$select->limit($criteria['count'], $criteria['start']);
}
}
$sorted = false;
if (array_key_exists('sort', $criteria)) {
$sort = $criteria['sort'];
$dir = 'ASC';
if ('-' == substr($sort, 0, 1)) {
$sort = substr($sort, 1);
$dir = 'DESC';
}
$fields = $this->getTable()->info('cols');
if (in_array($sort, $fields)) {
$select->order("$sort $dir");
$sorted = true;
}
}
if (!$sorted) {
$select->order('created DESC');
}
}
}