3 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
4 * Copyright (C) 2002-2009 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)
13 * Media popup window for Nucleus
16 * - can be openen from an add-item form or bookmarklet popup
17 * - shows a list of recent files, allowing browsing, search and
19 * - close the popup by selecting a file in the list. The file gets
20 * passed through to the add-item form (linkto, popupimg or inline img)
22 * @license http://nucleuscms.org/license.txt GNU General Public License
23 * @copyright Copyright (C) 2002-2009 The Nucleus Group
25 * $NucleusJP: media.php,v 1.8.2.1 2007/09/07 07:36:44 kimitake Exp $
31 // defines how much media items will be shown per page. You can override this
32 // in config.php if you like. (changing it in config.php instead of here will
33 // allow your settings to be kept even after a Nucleus upgrade)
34 $CONF['MediaPerPage'] = 10;
36 // include all classes and config data
37 require('../config.php');
38 include($DIR_LIBS . 'MEDIA.php'); // media classes
40 sendContentType('application/xhtml+xml', 'media');
42 // user needs to be logged in to use this
43 if (!$member->isLoggedIn()) {
44 media_loginAndPassThrough();
48 // check if member is on at least one teamlist
49 $query = 'SELECT * FROM ' . sql_table('team'). ' WHERE tmember=' . $member->getID();
50 $teams = sql_query($query);
51 if (sql_num_rows($teams) == 0)
52 media_doError(_ERROR_DISALLOWEDUPLOAD);
55 $action = requestVar('action');
57 $action = 'selectmedia';
60 $aActionsNotToCheck = array('selectmedia', _MEDIA_FILTER_APPLY, _MEDIA_COLLECTION_SELECT);
61 if (!in_array($action, $aActionsNotToCheck))
63 if (!$manager->checkTicket())
64 media_doError(_ERROR_BADTICKET);
70 case _MEDIA_UPLOAD_TO:
71 case _MEDIA_UPLOAD_NEW:
72 if (!$member->isAdmin() and $CONF['AllowUpload'] != true) {
73 media_doError(_ERROR_DISALLOWED);
79 if (!$member->isAdmin() and $CONF['AllowUpload'] != true) {
80 media_doError(_ERROR_DISALLOWED);
85 case _MEDIA_FILTER_APPLY:
87 case _MEDIA_COLLECTION_SELECT:
94 function media_select() {
95 global $member, $CONF, $DIR_MEDIA, $manager;
97 // show 10 files + navigation buttons
98 // show msg when no files
100 // files sorted according to last modification date
102 // currently selected collection
103 $currentCollection = requestVar('collection');
104 if (!$currentCollection || !@is_dir($DIR_MEDIA . $currentCollection))
105 $currentCollection = $member->getID();
107 // avoid directory travarsal and accessing invalid directory
108 if (!MEDIA::isValidCollection($currentCollection)) media_doError(_ERROR_DISALLOWED);
112 // get collection list
113 $collections = MEDIA::getCollectionList();
115 if (sizeof($collections) > 1) {
117 <form method="post" action="media.php"><div>
118 <label for="media_collection"><?php echo htmlspecialchars(_MEDIA_COLLECTION_LABEL)?></label>
119 <select name="collection" id="media_collection">
120 <?php foreach ($collections as $dirname => $description) {
121 echo '<option value="',htmlspecialchars($dirname),'"';
122 if ($dirname == $currentCollection) {
123 echo ' selected="selected"';
125 echo '>',htmlspecialchars($description),'</option>';
129 <input type="submit" name="action" value="<?php echo htmlspecialchars(_MEDIA_COLLECTION_SELECT) ?>" title="<?php echo htmlspecialchars(_MEDIA_COLLECTION_TT)?>" />
130 <input type="submit" name="action" value="<?php echo htmlspecialchars(_MEDIA_UPLOAD_TO) ?>" title="<?php echo htmlspecialchars(_MEDIA_UPLOADLINK) ?>" />
131 <?php $manager->addTicketHidden() ?>
135 <form method="post" action="media.php" style="float:right"><div>
136 <input type="hidden" name="collection" value="<?php echo htmlspecialchars($currentCollection)?>" />
137 <input type="submit" name="action" value="<?php echo htmlspecialchars(_MEDIA_UPLOAD_NEW) ?>" title="<?php echo htmlspecialchars(_MEDIA_UPLOADLINK) ?>" />
138 <?php $manager->addTicketHidden() ?>
142 $filter = requestVar('filter');
143 $offset = intRequestVar('offset');
144 $arr = MEDIA::getMediaListByCollection($currentCollection, $filter);
147 <form method="post" action="media.php"><div>
148 <label for="media_filter"><?php echo htmlspecialchars(_MEDIA_FILTER_LABEL)?></label>
149 <input id="media_filter" type="text" name="filter" value="<?php echo htmlspecialchars($filter)?>" />
150 <input type="submit" name="action" value="<?php echo htmlspecialchars(_MEDIA_FILTER_APPLY) ?>" />
151 <input type="hidden" name="collection" value="<?php echo htmlspecialchars($currentCollection)?>" />
152 <input type="hidden" name="offset" value="<?php echo intval($offset)?>" />
159 <caption><?php echo _MEDIA_COLLECTION_LABEL . htmlspecialchars($collections[$currentCollection])?></caption>
161 <th><?php echo _MEDIA_MODIFIED?></th><th><?php echo _MEDIA_FILENAME?></th><th><?php echo _MEDIA_DIMENSIONS?></th>
166 if (sizeof($arr)>0) {
168 if (($offset + $CONF['MediaPerPage']) >= sizeof($arr))
169 $offset = sizeof($arr) - $CONF['MediaPerPage'];
171 if ($offset < 0) $offset = 0;
174 $idxEnd = $offset + $CONF['MediaPerPage'];
176 $idxPrev = $idxStart - $CONF['MediaPerPage'];
178 if ($idxPrev < 0) $idxPrev = 0;
180 if ($idxEnd > sizeof($arr))
181 $idxEnd = sizeof($arr);
183 for($i=$idxStart;$i<$idxEnd;$i++) {
185 $filename = $DIR_MEDIA . $currentCollection . '/' . $obj->filename;
187 $old_level = error_reporting(0);
188 $size = @GetImageSize($filename);
189 error_reporting($old_level);
192 $filetype = $size[2];
195 echo "<td>". date("Y-m-d",$obj->timestamp) ."</td>";
197 // strings for javascript
198 $jsCurrentCollection = str_replace("'","\\'",$currentCollection);
199 $jsFileName = str_replace("'","\\'",$obj->filename);
201 if ($filetype != 0) {
202 // image (gif/jpg/png/swf)
203 echo "<td><a href=\"media.php\" onclick=\"chooseImage('", htmlspecialchars($jsCurrentCollection), "','", htmlspecialchars($jsFileName), "',"
204 . "'", htmlspecialchars($width), "','" , htmlspecialchars($height), "'"
205 . ")\" title=\"" . htmlspecialchars($obj->filename). "\">"
206 . htmlspecialchars(shorten($obj->filename,25,'...'))
208 echo ' (<a href="', htmlspecialchars($CONF['MediaURL'] . $currentCollection . '/' . $obj->filename), '" onclick="window.open(this.href); return false;" title="',htmlspecialchars(_MEDIA_VIEW_TT),'">',_MEDIA_VIEW,'</a>)';
211 // no image (e.g. mpg)
212 echo "<td><a href='media.php' onclick=\"chooseOther('" , htmlspecialchars($jsCurrentCollection), "','", htmlspecialchars($jsFileName), "'"
213 . ")\" title=\"" . htmlspecialchars($obj->filename). "\">"
214 . htmlspecialchars(shorten($obj->filename,30,'...'))
218 echo '<td>' , htmlspecialchars($width) , 'x' , htmlspecialchars($height) , '</td>';
221 } // if (sizeof($arr)>0)
227 echo "<a href='media.php?offset=$idxPrev&collection=".urlencode($currentCollection)."'>". _LISTS_PREV."</a> ";
228 if ($idxEnd < sizeof($arr))
229 echo "<a href='media.php?offset=$idxNext&collection=".urlencode($currentCollection)."'>". _LISTS_NEXT."</a> ";
232 <input id="typeradio0" type="radio" name="typeradio" onclick="setType(0);" checked="checked" /><label for="typeradio0"><?php echo _MEDIA_INLINE?></label>
233 <input id="typeradio1" type="radio" name="typeradio" onclick="setType(1);" /><label for="typeradio1"><?php echo _MEDIA_POPUP?></label>
241 * Shows a screen where you can select the file to upload
243 function media_choose() {
244 global $CONF, $member, $manager;
246 $currentCollection = requestVar('collection');
248 $collections = MEDIA::getCollectionList();
252 <h1><?php echo _UPLOAD_TITLE?></h1>
254 <p><?php echo _UPLOAD_MSG?></p>
256 <form method="post" enctype="multipart/form-data" action="media.php">
258 <input type="hidden" name="action" value="uploadfile" />
259 <?php $manager->addTicketHidden() ?>
260 <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $CONF['MaxUploadSize']?>" />
263 <input name="uploadfile" type="file" size="40" />
264 <?php if (sizeof($collections) > 1) {
266 <br /><br /><label for="upload_collection">Collection:</label>
267 <br /><select name="collection" id="upload_collection">
268 <?php foreach ($collections as $dirname => $description) {
269 echo '<option value="',htmlspecialchars($dirname),'"';
270 if ($dirname == $currentCollection) {
271 echo ' selected="selected"';
273 echo '>',htmlspecialchars($description),'</option>';
279 <input name="collection" type="hidden" value="<?php echo htmlspecialchars(requestVar('collection'))?>" />
283 <input type="submit" value="<?php echo _UPLOAD_BUTTON?>" />
293 * accepts a file for upload
295 function media_upload() {
296 global $DIR_MEDIA, $member, $CONF;
298 $uploadInfo = postFileInfo('uploadfile');
300 $filename = $uploadInfo['name'];
301 $filetype = $uploadInfo['type'];
302 $filesize = $uploadInfo['size'];
303 $filetempname = $uploadInfo['tmp_name'];
304 $fileerror = intval($uploadInfo['error']);
308 case 0: // = UPLOAD_ERR_OK
310 case 1: // = UPLOAD_ERR_INI_SIZE
311 case 2: // = UPLOAD_ERR_FORM_SIZE
312 media_doError(_ERROR_FILE_TOO_BIG);
313 case 3: // = UPLOAD_ERR_PARTIAL
314 case 4: // = UPLOAD_ERR_NO_FILE
315 case 6: // = UPLOAD_ERR_NO_TMP_DIR
316 case 7: // = UPLOAD_ERR_CANT_WRITE
318 // include error code for debugging
319 // (see http://www.php.net/manual/en/features.file-upload.errors.php)
320 media_doError(_ERROR_BADREQUEST . ' (' . $fileerror . ')');
323 if ($filesize > $CONF['MaxUploadSize'])
324 media_doError(_ERROR_FILE_TOO_BIG);
326 // check file type against allowed types
328 $allowedtypes = explode (',', $CONF['AllowedTypes']);
329 foreach ( $allowedtypes as $type )
330 if (eregi("\." .$type. "$",$filename)) $ok = 1;
331 if (!$ok) media_doError(_ERROR_BADFILETYPE);
333 if (!is_uploaded_file($filetempname))
334 media_doError(_ERROR_BADREQUEST);
336 // prefix filename with current date (YYYY-MM-DD-)
337 // this to avoid nameclashes
338 if ($CONF['MediaPrefix'])
339 $filename = strftime("%Y%m%d-", time()) . $filename;
341 $collection = requestVar('collection');
342 $res = MEDIA::addMediaObject($collection, $filetempname, $filename);
347 // shows updated list afterwards
351 function media_loginAndPassThrough() {
354 <h1><?php echo _LOGIN_PLEASE?></h1>
356 <form method="post" action="media.php">
358 <input name="action" value="login" type="hidden" />
359 <input name="collection" value="<?php echo htmlspecialchars(requestVar('collection'))?>" type="hidden" />
360 <?php echo _LOGINFORM_NAME?> <input name="login" />
361 <br /><?php echo _LOGINFORM_PWD?> <input name="password" type="password" />
362 <br /><input type="submit" value="<?php echo _LOGIN?>" />
365 <p><a href="media.php" onclick="window.close();"><?php echo _POPUP_CLOSE?></a></p>
370 function media_doError($msg) {
373 <h1><?php echo _ERROR?></h1>
374 <p><?php echo $msg?></p>
375 <p><a href="media.php" onclick="history.back()"><?php echo _BACK?></a></p>
381 function media_head() {
383 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
384 <html <?php echo _HTML_XML_NAME_SPACE_AND_LANG_CODE; ?>>
386 <meta http-equiv="Content-Type" content="text/html; charset=<?php echo _CHARSET ?>" />
387 <title>Nucleus Media</title>
388 <link rel="stylesheet" type="text/css" href="styles/popups.css" />
389 <script type="text/javascript">
391 function setType(val) { type = val; }
393 function chooseImage(collection, filename, width, height) {
394 window.opener.focus();
395 window.opener.includeImage(collection,
397 type == 0 ? 'inline' : 'popup',
404 function chooseOther(collection, filename) {
405 window.opener.focus();
406 window.opener.includeOtherMedia(collection, filename);
415 function media_foot() {