3 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
4 * Copyright (C) 2002-2011 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-2011 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
38 require_once('../config.php');
39 //include($DIR_LIBS . 'MEDIA.php'); // media classes
40 include_libs('MEDIA.php',false,false);
42 sendContentType('application/xhtml+xml', 'media');
44 // user needs to be logged in to use this
45 if (!$member->isLoggedIn()) {
46 media_loginAndPassThrough();
50 // check if member is on at least one teamlist
51 $query = 'SELECT * FROM ' . sql_table('team'). ' WHERE tmember=' . $member->getID();
52 $teams = sql_query($query);
53 if (sql_num_rows($teams) == 0 && !$member->isAdmin())
54 media_doError(_ERROR_DISALLOWEDUPLOAD);
57 $action = requestVar('action');
59 $action = 'selectmedia';
62 $aActionsNotToCheck = array('selectmedia', _MEDIA_FILTER_APPLY, _MEDIA_COLLECTION_SELECT);
63 if (!in_array($action, $aActionsNotToCheck))
65 if (!$manager->checkTicket())
66 media_doError(_ERROR_BADTICKET);
72 case _MEDIA_UPLOAD_TO:
73 case _MEDIA_UPLOAD_NEW:
74 if (!$member->isAdmin() and $CONF['AllowUpload'] != true) {
75 media_doError(_ERROR_DISALLOWED);
81 if (!$member->isAdmin() and $CONF['AllowUpload'] != true) {
82 media_doError(_ERROR_DISALLOWED);
87 case _MEDIA_FILTER_APPLY:
89 case _MEDIA_COLLECTION_SELECT:
96 function media_select() {
97 global $member, $CONF, $DIR_MEDIA, $manager;
99 // show 10 files + navigation buttons
100 // show msg when no files
102 // files sorted according to last modification date
104 // currently selected collection
105 $currentCollection = requestVar('collection');
106 if (!$currentCollection || !@is_dir($DIR_MEDIA . $currentCollection))
107 $currentCollection = $member->getID();
109 // avoid directory travarsal and accessing invalid directory
110 if (!MEDIA::isValidCollection($currentCollection)) media_doError(_ERROR_DISALLOWED);
114 // get collection list
115 $collections = MEDIA::getCollectionList();
117 if (sizeof($collections) > 1) {
119 <form method="post" action="media.php"><div>
120 <label for="media_collection"><?php echo htmlspecialchars(_MEDIA_COLLECTION_LABEL)?></label>
121 <select name="collection" id="media_collection">
122 <?php foreach ($collections as $dirname => $description) {
123 echo '<option value="',htmlspecialchars($dirname),'"';
124 if ($dirname == $currentCollection) {
125 echo ' selected="selected"';
127 echo '>',htmlspecialchars($description),'</option>';
131 <input type="submit" name="action" value="<?php echo htmlspecialchars(_MEDIA_COLLECTION_SELECT) ?>" title="<?php echo htmlspecialchars(_MEDIA_COLLECTION_TT)?>" />
132 <input type="submit" name="action" value="<?php echo htmlspecialchars(_MEDIA_UPLOAD_TO) ?>" title="<?php echo htmlspecialchars(_MEDIA_UPLOADLINK) ?>" />
133 <?php $manager->addTicketHidden() ?>
137 <form method="post" action="media.php" style="float:right"><div>
138 <input type="hidden" name="collection" value="<?php echo htmlspecialchars($currentCollection)?>" />
139 <input type="submit" name="action" value="<?php echo htmlspecialchars(_MEDIA_UPLOAD_NEW) ?>" title="<?php echo htmlspecialchars(_MEDIA_UPLOADLINK) ?>" />
140 <?php $manager->addTicketHidden() ?>
144 $filter = requestVar('filter');
145 $offset = intRequestVar('offset');
146 $arr = MEDIA::getMediaListByCollection($currentCollection, $filter);
149 <form method="post" action="media.php"><div>
150 <label for="media_filter"><?php echo htmlspecialchars(_MEDIA_FILTER_LABEL)?></label>
151 <input id="media_filter" type="text" name="filter" value="<?php echo htmlspecialchars($filter)?>" />
152 <input type="submit" name="action" value="<?php echo htmlspecialchars(_MEDIA_FILTER_APPLY) ?>" />
153 <input type="hidden" name="collection" value="<?php echo htmlspecialchars($currentCollection)?>" />
154 <input type="hidden" name="offset" value="<?php echo intval($offset)?>" />
161 <caption><?php echo _MEDIA_COLLECTION_LABEL . htmlspecialchars($collections[$currentCollection])?></caption>
163 <th><?php echo _MEDIA_MODIFIED?></th><th><?php echo _MEDIA_FILENAME?></th><th><?php echo _MEDIA_DIMENSIONS?></th>
168 if (sizeof($arr)>0) {
170 if (($offset + $CONF['MediaPerPage']) >= sizeof($arr))
171 $offset = sizeof($arr) - $CONF['MediaPerPage'];
173 if ($offset < 0) $offset = 0;
176 $idxEnd = $offset + $CONF['MediaPerPage'];
178 $idxPrev = $idxStart - $CONF['MediaPerPage'];
180 if ($idxPrev < 0) $idxPrev = 0;
182 if ($idxEnd > sizeof($arr))
183 $idxEnd = sizeof($arr);
185 for($i=$idxStart;$i<$idxEnd;$i++) {
187 $filename = $DIR_MEDIA . $currentCollection . '/' . $obj->filename;
189 $old_level = error_reporting(0);
190 $size = @GetImageSize($filename);
191 error_reporting($old_level);
194 $filetype = $size[2];
197 echo "<td>". date("Y-m-d",$obj->timestamp) ."</td>";
199 // strings for javascript
200 $jsCurrentCollection = str_replace("'","\\'",$currentCollection);
201 $jsFileName = str_replace("'","\\'",$obj->filename);
203 if ($filetype != 0) {
204 // image (gif/jpg/png/swf)
205 echo "<td><a href=\"media.php\" onclick=\"chooseImage('", htmlspecialchars($jsCurrentCollection), "','", htmlspecialchars($jsFileName), "',"
206 . "'", htmlspecialchars($width), "','" , htmlspecialchars($height), "'"
207 . ")\" title=\"" . htmlspecialchars($obj->filename). "\">"
208 . htmlspecialchars(shorten($obj->filename,25,'...'))
210 echo ' (<a href="', htmlspecialchars($CONF['MediaURL'] . $currentCollection . '/' . $obj->filename), '" onclick="window.open(this.href); return false;" title="',htmlspecialchars(_MEDIA_VIEW_TT),'">',_MEDIA_VIEW,'</a>)';
213 // no image (e.g. mpg)
214 echo "<td><a href='media.php' onclick=\"chooseOther('" , htmlspecialchars($jsCurrentCollection), "','", htmlspecialchars($jsFileName), "'"
215 . ")\" title=\"" . htmlspecialchars($obj->filename). "\">"
216 . htmlspecialchars(shorten($obj->filename,30,'...'))
220 echo '<td>' , htmlspecialchars($width) , 'x' , htmlspecialchars($height) , '</td>';
223 } // if (sizeof($arr)>0)
229 echo "<a href='media.php?offset=$idxPrev&collection=".urlencode($currentCollection)."'>". _LISTS_PREV."</a> ";
230 if ($idxEnd < sizeof($arr))
231 echo "<a href='media.php?offset=$idxNext&collection=".urlencode($currentCollection)."'>". _LISTS_NEXT."</a> ";
234 <input id="typeradio0" type="radio" name="typeradio" onclick="setType(0);" checked="checked" /><label for="typeradio0"><?php echo _MEDIA_INLINE?></label>
235 <input id="typeradio1" type="radio" name="typeradio" onclick="setType(1);" /><label for="typeradio1"><?php echo _MEDIA_POPUP?></label>
243 * Shows a screen where you can select the file to upload
245 function media_choose() {
246 global $CONF, $member, $manager;
248 $currentCollection = requestVar('collection');
250 $collections = MEDIA::getCollectionList();
254 <h1><?php echo _UPLOAD_TITLE?></h1>
256 <p><?php echo _UPLOAD_MSG?></p>
258 <form method="post" enctype="multipart/form-data" action="media.php">
260 <input type="hidden" name="action" value="uploadfile" />
261 <?php $manager->addTicketHidden() ?>
262 <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $CONF['MaxUploadSize']?>" />
265 <input name="uploadfile" type="file" size="40" />
266 <?php if (sizeof($collections) > 1) {
268 <br /><br /><label for="upload_collection">Collection:</label>
269 <br /><select name="collection" id="upload_collection">
270 <?php foreach ($collections as $dirname => $description) {
271 echo '<option value="',htmlspecialchars($dirname),'"';
272 if ($dirname == $currentCollection) {
273 echo ' selected="selected"';
275 echo '>',htmlspecialchars($description),'</option>';
281 <input name="collection" type="hidden" value="<?php echo htmlspecialchars(requestVar('collection'))?>" />
287 $manager->notify('MediaUploadFormExtras', $param);
290 <input type="submit" value="<?php echo _UPLOAD_BUTTON?>" />
300 * accepts a file for upload
302 function media_upload() {
303 global $DIR_MEDIA, $member, $CONF;
305 $uploadInfo = postFileInfo('uploadfile');
307 $filename = $uploadInfo['name'];
308 $filetype = $uploadInfo['type'];
309 $filesize = $uploadInfo['size'];
310 $filetempname = $uploadInfo['tmp_name'];
311 $fileerror = intval($uploadInfo['error']);
313 // clean filename of characters that may cause trouble in a filename using cleanFileName() function from globalfunctions.php
314 $filename = cleanFileName($filename);
315 if ($filename === false)
316 media_doError(_ERROR_BADFILETYPE);
320 case 0: // = UPLOAD_ERR_OK
322 case 1: // = UPLOAD_ERR_INI_SIZE
323 case 2: // = UPLOAD_ERR_FORM_SIZE
324 media_doError(_ERROR_FILE_TOO_BIG);
325 case 3: // = UPLOAD_ERR_PARTIAL
326 case 4: // = UPLOAD_ERR_NO_FILE
327 case 6: // = UPLOAD_ERR_NO_TMP_DIR
328 case 7: // = UPLOAD_ERR_CANT_WRITE
330 // include error code for debugging
331 // (see http://www.php.net/manual/en/features.file-upload.errors.php)
332 media_doError(_ERROR_BADREQUEST . ' (' . $fileerror . ')');
335 if ($filesize > $CONF['MaxUploadSize'])
336 media_doError(_ERROR_FILE_TOO_BIG);
338 // check file type against allowed types
340 $allowedtypes = explode (',', $CONF['AllowedTypes']);
341 foreach ( $allowedtypes as $type )
343 //if (eregi("\." .$type. "$",$filename)) $ok = 1;
344 if (preg_match("#\." .$type. "$#i",$filename)) $ok = 1;
346 if (!$ok) media_doError(_ERROR_BADFILETYPE);
348 if (!is_uploaded_file($filetempname))
349 media_doError(_ERROR_BADREQUEST);
351 // prefix filename with current date (YYYY-MM-DD-)
352 // this to avoid nameclashes
353 if ($CONF['MediaPrefix'])
354 $filename = strftime("%Y%m%d-", time()) . $filename;
356 $collection = requestVar('collection');
357 $res = MEDIA::addMediaObject($collection, $filetempname, $filename);
362 // shows updated list afterwards
366 function media_loginAndPassThrough() {
369 <h1><?php echo _LOGIN_PLEASE?></h1>
371 <form method="post" action="media.php">
373 <input name="action" value="login" type="hidden" />
374 <input name="collection" value="<?php echo htmlspecialchars(requestVar('collection'))?>" type="hidden" />
375 <?php echo _LOGINFORM_NAME?>: <input name="login" />
376 <br /><?php echo _LOGINFORM_PWD?>: <input name="password" type="password" />
377 <br /><input type="submit" value="<?php echo _LOGIN?>" />
380 <p><a href="media.php" onclick="window.close();"><?php echo _POPUP_CLOSE?></a></p>
385 function media_doError($msg) {
388 <h1><?php echo _ERROR?></h1>
389 <p><?php echo $msg?></p>
390 <p><a href="media.php" onclick="history.back(); return false;"><?php echo _BACK?></a></p>
396 function media_head() {
398 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
399 <html <?php echo _HTML_XML_NAME_SPACE_AND_LANG_CODE; ?>>
401 <meta http-equiv="Content-Type" content="text/html; charset=<?php echo _CHARSET ?>" />
402 <title>Nucleus Media</title>
403 <link rel="stylesheet" type="text/css" href="styles/popups.css" />
404 <script type="text/javascript">
406 function setType(val) { type = val; }
408 function chooseImage(collection, filename, width, height) {
409 window.opener.focus();
410 window.opener.includeImage(collection,
412 type == 0 ? 'inline' : 'popup',
419 function chooseOther(collection, filename) {
420 window.opener.focus();
421 window.opener.includeOtherMedia(collection, filename);
430 function media_foot() {