3 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
4 * Copyright (C) 2002-2012 The Nucleus Group
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 * (see nucleus/documentation/index.html#license for more info)
12 * Media popup window for Nucleus
15 * - can be openen from an add-item form or bookmarklet popup
16 * - shows a list of recent files, allowing browsing, search and
18 * - close the popup by selecting a file in the list. The file gets
19 * passed through to the add-item form (linkto, popupimg or inline img)
24 // defines how much media items will be shown per page. You can override this
25 // in config.php if you like. (changing it in config.php instead of here will
26 // allow your settings to be kept even after a Nucleus upgrade)
27 $CONF['MediaPerPage'] = 10;
29 // include all classes and config data
31 require_once('../config.php');
32 //include($DIR_LIBS . 'MEDIA.php'); // media classes
33 include_libs('MEDIA.php',false,false);
35 sendContentType('application/xhtml+xml', 'media');
37 // user needs to be logged in to use this
38 if (!$member->isLoggedIn()) {
39 media_loginAndPassThrough();
43 // check if member is on at least one teamlist
44 $query = 'SELECT * FROM ' . sql_table('team'). ' WHERE tmember=' . $member->getID();
45 $teams = sql_query($query);
46 if (sql_num_rows($teams) == 0 && !$member->isAdmin())
47 media_doError(_ERROR_DISALLOWEDUPLOAD);
50 $action = requestVar('action');
52 $action = 'selectmedia';
55 $aActionsNotToCheck = array('selectmedia', _MEDIA_FILTER_APPLY, _MEDIA_COLLECTION_SELECT);
56 if (!in_array($action, $aActionsNotToCheck))
58 if (!$manager->checkTicket())
59 media_doError(_ERROR_BADTICKET);
65 case _MEDIA_UPLOAD_TO:
66 case _MEDIA_UPLOAD_NEW:
67 if (!$member->isAdmin() and $CONF['AllowUpload'] != true) {
68 media_doError(_ERROR_DISALLOWED);
74 if (!$member->isAdmin() and $CONF['AllowUpload'] != true) {
75 media_doError(_ERROR_DISALLOWED);
80 case _MEDIA_FILTER_APPLY:
82 case _MEDIA_COLLECTION_SELECT:
89 function media_select() {
90 global $member, $CONF, $DIR_MEDIA, $manager;
92 // show 10 files + navigation buttons
93 // show msg when no files
95 // files sorted according to last modification date
97 // currently selected collection
98 $currentCollection = requestVar('collection');
99 if (!$currentCollection || !@is_dir($DIR_MEDIA . $currentCollection))
100 $currentCollection = $member->getID();
102 // avoid directory travarsal and accessing invalid directory
103 if (!MEDIA::isValidCollection($currentCollection)) media_doError(_ERROR_DISALLOWED);
107 // get collection list
108 $collections = MEDIA::getCollectionList();
110 if (sizeof($collections) > 1) {
112 <form method="post" action="media.php"><div>
113 <label for="media_collection"><?php echo htmlspecialchars(_MEDIA_COLLECTION_LABEL)?></label>
114 <select name="collection" id="media_collection">
115 <?php foreach ($collections as $dirname => $description) {
116 echo '<option value="',htmlspecialchars($dirname),'"';
117 if ($dirname == $currentCollection) {
118 echo ' selected="selected"';
120 echo '>',htmlspecialchars($description),'</option>';
124 <input type="submit" name="action" value="<?php echo htmlspecialchars(_MEDIA_COLLECTION_SELECT) ?>" title="<?php echo htmlspecialchars(_MEDIA_COLLECTION_TT)?>" />
125 <input type="submit" name="action" value="<?php echo htmlspecialchars(_MEDIA_UPLOAD_TO) ?>" title="<?php echo htmlspecialchars(_MEDIA_UPLOADLINK) ?>" />
126 <?php $manager->addTicketHidden() ?>
130 <form method="post" action="media.php" style="float:right"><div>
131 <input type="hidden" name="collection" value="<?php echo htmlspecialchars($currentCollection)?>" />
132 <input type="submit" name="action" value="<?php echo htmlspecialchars(_MEDIA_UPLOAD_NEW) ?>" title="<?php echo htmlspecialchars(_MEDIA_UPLOADLINK) ?>" />
133 <?php $manager->addTicketHidden() ?>
137 $filter = requestVar('filter');
138 $offset = intRequestVar('offset');
139 $arr = MEDIA::getMediaListByCollection($currentCollection, $filter);
142 <form method="post" action="media.php"><div>
143 <label for="media_filter"><?php echo htmlspecialchars(_MEDIA_FILTER_LABEL)?></label>
144 <input id="media_filter" type="text" name="filter" value="<?php echo htmlspecialchars($filter)?>" />
145 <input type="submit" name="action" value="<?php echo htmlspecialchars(_MEDIA_FILTER_APPLY) ?>" />
146 <input type="hidden" name="collection" value="<?php echo htmlspecialchars($currentCollection)?>" />
147 <input type="hidden" name="offset" value="<?php echo intval($offset)?>" />
154 <caption><?php echo _MEDIA_COLLECTION_LABEL . htmlspecialchars($collections[$currentCollection])?></caption>
156 <th><?php echo _MEDIA_MODIFIED?></th><th><?php echo _MEDIA_FILENAME?></th><th><?php echo _MEDIA_DIMENSIONS?></th>
164 if (sizeof($arr)>0) {
166 if (($offset + $CONF['MediaPerPage']) >= sizeof($arr))
167 $offset = sizeof($arr) - $CONF['MediaPerPage'];
169 if ($offset < 0) $offset = 0;
172 $idxEnd = $offset + $CONF['MediaPerPage'];
174 $idxPrev = $idxStart - $CONF['MediaPerPage'];
176 if ($idxPrev < 0) $idxPrev = 0;
178 if ($idxEnd > sizeof($arr))
179 $idxEnd = sizeof($arr);
181 for($i=$idxStart;$i<$idxEnd;$i++) {
183 $filename = $DIR_MEDIA . $currentCollection . '/' . $obj->filename;
185 $old_level = error_reporting(0);
186 $size = @GetImageSize($filename);
187 error_reporting($old_level);
190 $filetype = $size[2];
193 echo "<td>". date("Y-m-d",$obj->timestamp) ."</td>";
195 // strings for javascript
196 $jsCurrentCollection = str_replace("'","\\'",$currentCollection);
197 $jsFileName = str_replace("'","\\'",$obj->filename);
199 if ($filetype != 0) {
200 // image (gif/jpg/png/swf)
201 echo "<td><a href=\"media.php\" onclick=\"chooseImage('", htmlspecialchars($jsCurrentCollection), "','", htmlspecialchars($jsFileName), "',"
202 . "'", htmlspecialchars($width), "','" , htmlspecialchars($height), "'"
203 . ")\" title=\"" . htmlspecialchars($obj->filename). "\">"
204 . htmlspecialchars(shorten($obj->filename,25,'...'))
206 echo ' (<a href="', htmlspecialchars($CONF['MediaURL'] . $currentCollection . '/' . $obj->filename), '" onclick="window.open(this.href); return false;" title="',htmlspecialchars(_MEDIA_VIEW_TT),'">',_MEDIA_VIEW,'</a>)';
209 // no image (e.g. mpg)
210 echo "<td><a href='media.php' onclick=\"chooseOther('" , htmlspecialchars($jsCurrentCollection), "','", htmlspecialchars($jsFileName), "'"
211 . ")\" title=\"" . htmlspecialchars($obj->filename). "\">"
212 . htmlspecialchars(shorten($obj->filename,30,'...'))
216 echo '<td>' , htmlspecialchars($width) , 'x' , htmlspecialchars($height) , '</td>';
219 } // if (sizeof($arr)>0)
225 echo "<a href='media.php?offset=$idxPrev&collection=".urlencode($currentCollection)."'>". _LISTS_PREV."</a> ";
226 if ($idxEnd < sizeof($arr))
227 echo "<a href='media.php?offset=$idxNext&collection=".urlencode($currentCollection)."'>". _LISTS_NEXT."</a> ";
230 <input id="typeradio0" type="radio" name="typeradio" onclick="setType(0);" checked="checked" /><label for="typeradio0"><?php echo _MEDIA_INLINE?></label>
231 <input id="typeradio1" type="radio" name="typeradio" onclick="setType(1);" /><label for="typeradio1"><?php echo _MEDIA_POPUP?></label>
239 * Shows a screen where you can select the file to upload
241 function media_choose() {
242 global $CONF, $member, $manager;
244 $currentCollection = requestVar('collection');
246 $collections = MEDIA::getCollectionList();
250 <h1><?php echo _UPLOAD_TITLE?></h1>
252 <p><?php echo _UPLOAD_MSG?></p>
254 <form method="post" enctype="multipart/form-data" action="media.php">
256 <input type="hidden" name="action" value="uploadfile" />
257 <?php $manager->addTicketHidden() ?>
258 <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $CONF['MaxUploadSize']?>" />
261 <input name="uploadfile" type="file" size="40" />
262 <?php if (sizeof($collections) > 1) {
264 <br /><br /><label for="upload_collection">Collection:</label>
265 <br /><select name="collection" id="upload_collection">
266 <?php foreach ($collections as $dirname => $description) {
267 echo '<option value="',htmlspecialchars($dirname),'"';
268 if ($dirname == $currentCollection) {
269 echo ' selected="selected"';
271 echo '>',htmlspecialchars($description),'</option>';
277 <input name="collection" type="hidden" value="<?php echo htmlspecialchars(requestVar('collection'))?>" />
283 $manager->notify('MediaUploadFormExtras', $param);
286 <input type="submit" value="<?php echo _UPLOAD_BUTTON?>" />
296 * accepts a file for upload
298 function media_upload() {
299 global $DIR_MEDIA, $member, $CONF;
301 $uploadInfo = postFileInfo('uploadfile');
303 $filename = $uploadInfo['name'];
304 $filetype = $uploadInfo['type'];
305 $filesize = $uploadInfo['size'];
306 $filetempname = $uploadInfo['tmp_name'];
307 $fileerror = intval($uploadInfo['error']);
309 // clean filename of characters that may cause trouble in a filename using cleanFileName() function from globalfunctions.php
310 $filename = cleanFileName($filename);
311 if ($filename === false)
312 media_doError(_ERROR_BADFILETYPE);
316 case 0: // = UPLOAD_ERR_OK
318 case 1: // = UPLOAD_ERR_INI_SIZE
319 case 2: // = UPLOAD_ERR_FORM_SIZE
320 media_doError(_ERROR_FILE_TOO_BIG);
321 case 3: // = UPLOAD_ERR_PARTIAL
322 case 4: // = UPLOAD_ERR_NO_FILE
323 case 6: // = UPLOAD_ERR_NO_TMP_DIR
324 case 7: // = UPLOAD_ERR_CANT_WRITE
326 // include error code for debugging
327 // (see http://www.php.net/manual/en/features.file-upload.errors.php)
328 media_doError(_ERROR_BADREQUEST . ' (' . $fileerror . ')');
331 if ($filesize > $CONF['MaxUploadSize'])
332 media_doError(_ERROR_FILE_TOO_BIG);
334 // check file type against allowed types
336 $allowedtypes = explode (',', $CONF['AllowedTypes']);
337 foreach ( $allowedtypes as $type )
339 //if (eregi("\." .$type. "$",$filename)) $ok = 1;
340 if (preg_match("#\." .$type. "$#i",$filename)) $ok = 1;
342 if (!$ok) media_doError(_ERROR_BADFILETYPE);
344 if (!is_uploaded_file($filetempname))
345 media_doError(_ERROR_BADREQUEST);
347 // prefix filename with current date (YYYY-MM-DD-)
348 // this to avoid nameclashes
349 if ($CONF['MediaPrefix'])
350 $filename = strftime("%Y%m%d-", time()) . $filename;
352 $collection = requestVar('collection');
353 $res = MEDIA::addMediaObject($collection, $filetempname, $filename);
358 // shows updated list afterwards
362 function media_loginAndPassThrough() {
365 <h1><?php echo _LOGIN_PLEASE?></h1>
367 <form method="post" action="media.php">
369 <input name="action" value="login" type="hidden" />
370 <input name="collection" value="<?php echo htmlspecialchars(requestVar('collection'))?>" type="hidden" />
371 <?php echo _LOGINFORM_NAME?>: <input name="login" />
372 <br /><?php echo _LOGINFORM_PWD?>: <input name="password" type="password" />
373 <br /><input type="submit" value="<?php echo _LOGIN?>" />
376 <p><a href="media.php" onclick="window.close();"><?php echo _POPUP_CLOSE?></a></p>
381 function media_doError($msg) {
384 <h1><?php echo _ERROR?></h1>
385 <p><?php echo $msg?></p>
386 <p><a href="media.php" onclick="history.back(); return false;"><?php echo _BACK?></a></p>
392 function media_head() {
394 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
395 <html <?php echo _HTML_XML_NAME_SPACE_AND_LANG_CODE; ?>>
397 <meta http-equiv="Content-Type" content="text/html; charset=<?php echo _CHARSET ?>" />
398 <title>Nucleus Media</title>
399 <link rel="stylesheet" type="text/css" href="styles/popups.css" />
400 <script type="text/javascript">
402 function setType(val) { type = val; }
404 function chooseImage(collection, filename, width, height) {
405 window.opener.focus();
406 window.opener.includeImage(collection,
408 type == 0 ? 'inline' : 'popup',
415 function chooseOther(collection, filename) {
416 window.opener.focus();
417 window.opener.includeOtherMedia(collection, filename);
426 function media_foot() {