1: <?php
  2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12: 
 13: class Wicked_Page_AttachedFiles extends Wicked_Page {
 14: 
 15:      16:  17:  18:  19: 
 20:     public $supportedModes = array(
 21:         Wicked::MODE_CONTENT => true,
 22:         Wicked::MODE_EDIT => true,
 23:         Wicked::MODE_REMOVE => true,
 24:         Wicked::MODE_DISPLAY => true);
 25: 
 26:      27:  28:  29:  30: 
 31:     protected $_referrer = null;
 32: 
 33:      34:  35: 
 36:     public function __construct($referrer)
 37:     {
 38:         $this->_referrer = $referrer;
 39:     }
 40: 
 41:      42:  43:  44:  45: 
 46:     public function getPermissions()
 47:     {
 48:         return parent::getPermissions($this->referrer());
 49:     }
 50: 
 51:      52:  53:  54:  55: 
 56:     public function content()
 57:     {
 58:         global $wicked, $notification;
 59: 
 60:         if (!$wicked->pageExists($this->referrer())) {
 61:             throw new Wicked_Exception(sprintf(_("Referrer \"%s\" does not exist."),
 62:                                                $this->referrer()));
 63:         }
 64: 
 65:         $referrer_id = $wicked->getPageId($this->referrer());
 66:         $attachments = $wicked->getAttachedFiles($referrer_id, true);
 67: 
 68:         foreach ($attachments as $idx => $attach) {
 69:             $attachments[$idx]['date'] = date('M j, Y g:ia',
 70:                                               $attach['attachment_created']);
 71: 
 72:             $attachments[$idx]['url'] = Horde::downloadUrl(
 73:                 $attach['attachment_name'],
 74:                 array('page' => $referrer_id,
 75:                       'file' => $attach['attachment_name'],
 76:                       'version' => $attach['attachment_version']));
 77: 
 78:             $attachments[$idx]['delete_form'] = $this->allows(Wicked::MODE_REMOVE);
 79: 
 80:             $this->_page['change_author'] = $attachments[$idx]['change_author'];
 81:             $attachments[$idx]['change_author'] = $this->author();
 82:         }
 83: 
 84:         return $attachments;
 85:     }
 86: 
 87:      88:  89:  90:  91: 
 92:     public function display()
 93:     {
 94:         global $registry, $wicked, $notification, $conf;
 95: 
 96:         try {
 97:             $attachments = $this->content();
 98:         } catch (Wicked_Exception $e) {
 99:             $notification->push(sprintf(_("Error retrieving attachments: %s"),
100:                                         $e->getMessage()),
101:                                 'horde.error');
102:             throw $e;
103:         }
104: 
105:         $template = $GLOBALS['injector']->createInstance('Horde_Template');
106: 
107:         $template->setOption('gettext', true);
108:         $template->set('pageName', $this->pageName());
109:         $template->set('formAction', Wicked::url('AttachedFiles'));
110:         $template->set('deleteButton', Horde_Themes::img('delete.png'));
111:         $template->set('referrerLink', Wicked::url($this->referrer()));
112: 
113:         $refreshIcon = Horde::link($this->pageUrl())
114:             . Horde::img('reload.png',
115:                          sprintf(_("Reload \"%s\""), $this->pageTitle()))
116:             . '</a>';
117:         $template->set('refreshIcon', $refreshIcon);
118:         $template->set('attachments', $attachments, true);
119: 
120:         
121:         $files = array();
122:         foreach ($attachments as $attachment) {
123:             $files[$attachment['attachment_name']] = true;
124:         }
125:         $files = array_keys($files);
126:         sort($files);
127:         $template->set('files', $files);
128:         $template->set('canUpdate',
129:                        $this->allows(Wicked::MODE_EDIT) && count($files),
130:                        true);
131:         $template->set('canAttach', $this->allows(Wicked::MODE_EDIT), true);
132:         if ($conf['wicked']['require_change_log']) {
133:             $template->set('requireChangelog', true, true);
134:         } else {
135:             $template->set('requireChangelog', false, true);
136:         }
137: 
138:         $requiredMarker = Horde::img('required.png', '*');
139:         $template->set('requiredMarker', $requiredMarker);
140:         $template->set('referrer', $this->referrer());
141:         $template->set('formInput', Horde_Util::formInput());
142: 
143:         Horde::addScriptFile('stripe.js', 'horde', true);
144:         echo $template->fetch(WICKED_TEMPLATES . '/display/AttachedFiles.html');
145:     }
146: 
147:     public function pageName()
148:     {
149:         return 'AttachedFiles';
150:     }
151: 
152:     public function pageTitle()
153:     {
154:         return sprintf(_("Attached Files: %s"), $this->referrer());
155:     }
156: 
157:     public function referrer()
158:     {
159:         return $this->_referrer;
160:     }
161: 
162:     163: 164: 
165:     public function handleAction()
166:     {
167:         global $notification, $wicked, $registry, $conf;
168: 
169:         
170:         $cmd = Horde_Util::getPost('cmd');
171:         $version = Horde_Util::getFormData('version');
172:         $is_update = (bool)Horde_Util::getFormData('is_update');
173:         $filename = Horde_Util::getFormData('filename');
174:         $change_log = Horde_Util::getFormData('change_log');
175: 
176:         
177:         if ($cmd == 'delete' && $filename && $version) {
178:             if (!$this->allows(Wicked::MODE_REMOVE)) {
179:                 $notification->push(_("You do not have permission to delete attachments from this page."), 'horde.error');
180:                 return;
181:             }
182: 
183:             try {
184:                 $wicked->removeAttachment($wicked->getPageId($this->referrer()),
185:                                           $filename, $version);
186:                 $notification->push(
187:                     sprintf(_("Successfully deleted version %s of \"%s\" from \"%s\""),
188:                             $version, $filename, $this->referrer()),
189:                     'horde.success');
190:             } catch (Wicked_Exception $e) {
191:                 $notification->push($result->getMessage(), 'horde.error');
192:             }
193:             return;
194:         }
195: 
196:         if (empty($filename)) {
197:             $filename = Horde_Util::dispelMagicQuotes($_FILES['attachment_file']['name']);
198:         }
199: 
200:         try {
201:             $GLOBALS['browser']->wasFileUploaded('attachment_file', _("attachment"));
202:         } catch (Horde_Browser_Exception $e) {
203:             $notification->push($e, 'horde.error');
204:             return;
205:         }
206: 
207:         if (strpos($filename, ' ') !== false) {
208:             $notification->push(
209:                 _("Attachments with spaces can't be embedded into a page."),
210:                 'horde.warning');
211:         }
212: 
213:         $data = file_get_contents($_FILES['attachment_file']['tmp_name']);
214:         if ($data === false) {
215:             $notification->push(_("Can't read uploaded file."), 'horde.error');
216:             return;
217:         }
218: 
219:         if (!$this->allows(Wicked::MODE_EDIT)) {
220:             $notification->push(
221:                 sprintf(_("You do not have permission to edit \"%s\""),
222:                         $this->referrer()),
223:                 'horde.error');
224:             return;
225:         }
226: 
227:         if ($conf['wicked']['require_change_log'] && empty($change_log)) {
228:             $notification->push(
229:                 _("You must enter a change description to attach this file."),
230:                 'horde.error');
231:             return;
232:         }
233: 
234:         $referrer_id = $wicked->getPageId($this->referrer());
235:         try {
236:             $attachments = $wicked->getAttachedFiles($referrer_id);
237:         } catch (Wicked_Exception $e) {
238:             $notification->push(sprintf(_("Error retrieving attachments: %s"),
239:                                         $e->getMessage()),
240:                                 'horde.error');
241:             return;
242:         }
243: 
244:         $found = false;
245:         foreach ($attachments as $attach) {
246:             if ($filename == $attach['attachment_name']) {
247:                 $found = true;
248:                 break;
249:             }
250:         }
251: 
252:         if ($is_update) {
253:             if (!$found) {
254:                 $notification->push(
255:                     sprintf(_("Can't update \"%s\": no such attachment."),
256:                             $filename),
257:                     'horde.error');
258:                 return;
259:             }
260:         } else {
261:             if ($found) {
262:                 $notification->push(
263:                     sprintf(_("There is already an attachment named \"%s\"."),
264:                             $filename),
265:                     'horde.error');
266:                 return;
267:             }
268:         }
269: 
270:         $file = array('page_id'         => $referrer_id,
271:                       'attachment_name' => $filename,
272:                       'change_log'      => $change_log);
273: 
274:         try {
275:             $wicked->attachFile($file, $data);
276:         } catch (Wicked_Exception $e) {
277:             $notification->push($e);
278:             Horde::logMessage($e);
279:             throw $e;
280:         }
281: 
282:         if ($is_update) {
283:             $message = sprintf(_("Updated attachment \"%s\" on page \"%s\"."),
284:                                $filename, $this->referrer());
285:         } else {
286:             $message = sprintf(_("New attachment \"%s\" to page \"%s\"."),
287:                                $filename, $this->referrer());
288:         }
289:         $notification->push($message, 'horde.success');
290: 
291:         $url = Wicked::url($this->referrer(), true, -1);
292:         Wicked::mail($message . ' ' . _("View page: ") . $url . "\n",
293:                      array('Subject' => '[' . $registry->get('name')
294:                            . '] attachment: ' . $this->referrer() . ', '
295:                            . $filename));
296:     }
297: 
298: }
299: