OSDN Git Service

95cf2e7c662a5bb0555b458fa14e5649ae15c007
[nucleus-jp/nucleus-jp-ancient.git] / nucleus / media.php
1 <?php
2 /*
3  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
4  * Copyright (C) 2002-2011 The Nucleus Group
5  *
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)
11  */
12 /**
13  * Media popup window for Nucleus
14  *
15  * Purpose:
16  *   - can be openen from an add-item form or bookmarklet popup
17  *   - shows a list of recent files, allowing browsing, search and
18  *     upload of new files
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)
21  *
22  * @license http://nucleuscms.org/license.txt GNU General Public License
23  * @copyright Copyright (C) 2002-2011 The Nucleus Group
24  * @version $Id$
25  * $NucleusJP: media.php,v 1.8.2.1 2007/09/07 07:36:44 kimitake Exp $
26  *
27  */
28
29 $CONF = array();
30
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;
35
36 // include all classes and config data
37 $DIR_LIBS = '';
38 require_once('../config.php');
39 //include($DIR_LIBS . 'MEDIA.php');     // media classes
40 include_libs('MEDIA.php',false,false);
41
42 sendContentType('application/xhtml+xml', 'media');
43
44 // user needs to be logged in to use this
45 if (!$member->isLoggedIn()) {
46         media_loginAndPassThrough();
47         exit;
48 }
49
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);
55
56 // get action
57 $action = requestVar('action');
58 if ($action == '')
59         $action = 'selectmedia';
60
61 // check ticket
62 $aActionsNotToCheck = array('selectmedia', _MEDIA_FILTER_APPLY, _MEDIA_COLLECTION_SELECT);
63 if (!in_array($action, $aActionsNotToCheck))
64 {
65         if (!$manager->checkTicket())
66                 media_doError(_ERROR_BADTICKET);
67 }
68
69
70 switch($action) {
71         case 'chooseupload':
72         case _MEDIA_UPLOAD_TO:
73         case _MEDIA_UPLOAD_NEW:
74                 if (!$member->isAdmin() and $CONF['AllowUpload'] != true) {
75                         media_doError(_ERROR_DISALLOWED);
76                 } else {
77                         media_choose();
78                 }
79                 break;
80         case 'uploadfile':
81                 if (!$member->isAdmin() and $CONF['AllowUpload'] != true) {
82                         media_doError(_ERROR_DISALLOWED);
83                 } else {
84                         media_upload();
85                 }
86                 break;
87         case _MEDIA_FILTER_APPLY:
88         case 'selectmedia':
89         case _MEDIA_COLLECTION_SELECT:
90         default:
91                 media_select();
92                 break;
93 }
94
95 // select a file
96 function media_select() {
97         global $member, $CONF, $DIR_MEDIA, $manager;
98
99         // show 10 files + navigation buttons
100         // show msg when no files
101         // show upload form
102         // files sorted according to last modification date
103
104         // currently selected collection
105         $currentCollection = requestVar('collection');
106         if (!$currentCollection || !@is_dir($DIR_MEDIA . $currentCollection))
107                 $currentCollection = $member->getID();
108
109         // avoid directory travarsal and accessing invalid directory
110         if (!MEDIA::isValidCollection($currentCollection)) media_doError(_ERROR_DISALLOWED);
111
112         media_head();
113
114         // get collection list
115         $collections = MEDIA::getCollectionList();
116
117         if (sizeof($collections) > 1) {
118         ?>
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"';
126                                                 }
127                                                 echo '>',htmlspecialchars($description),'</option>';
128                                         }
129                                 ?>
130                         </select>
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() ?>
134                 </div></form>
135         <?php   } else {
136         ?>
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() ?>
141                 </div></form>
142         <?php   } // if sizeof
143
144         $filter = requestVar('filter');
145         $offset = intRequestVar('offset');
146         $arr = MEDIA::getMediaListByCollection($currentCollection, $filter);
147
148         ?>
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)?>" />
155                 </div></form>
156
157         <?php
158
159         ?>
160                 <table width="100%">
161                 <caption><?php echo _MEDIA_COLLECTION_LABEL . htmlspecialchars($collections[$currentCollection])?></caption>
162                 <tr>
163                  <th><?php echo _MEDIA_MODIFIED?></th><th><?php echo _MEDIA_FILENAME?></th><th><?php echo _MEDIA_DIMENSIONS?></th>
164                 </tr>
165
166         <?php
167
168         if (sizeof($arr)>0) {
169
170                 if (($offset + $CONF['MediaPerPage']) >= sizeof($arr))
171                         $offset = sizeof($arr) - $CONF['MediaPerPage'];
172
173                 if ($offset < 0) $offset = 0;
174
175                 $idxStart = $offset;
176                 $idxEnd = $offset + $CONF['MediaPerPage'];
177                 $idxNext = $idxEnd;
178                 $idxPrev = $idxStart - $CONF['MediaPerPage'];
179
180                 if ($idxPrev < 0) $idxPrev = 0;
181
182                 if ($idxEnd > sizeof($arr))
183                         $idxEnd = sizeof($arr);
184
185                 for($i=$idxStart;$i<$idxEnd;$i++) {
186                         $obj = $arr[$i];
187                         $filename = $DIR_MEDIA . $currentCollection . '/' . $obj->filename;
188
189                         $old_level = error_reporting(0);
190                         $size = @GetImageSize($filename);
191                         error_reporting($old_level);
192                         $width = $size[0];
193                         $height = $size[1];
194                         $filetype = $size[2];
195
196                         echo "<tr>";
197                         echo "<td>". date("Y-m-d",$obj->timestamp) ."</td>";
198
199                         // strings for javascript
200                         $jsCurrentCollection = str_replace("'","\\'",$currentCollection);
201                         $jsFileName = str_replace("'","\\'",$obj->filename);
202
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,'...'))
209                                                            ."</a>";
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>)';
211                                 echo "</td>";
212                         } else {
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,'...'))
217                                                            ."</a></td>";
218
219                         }
220                         echo '<td>' , htmlspecialchars($width) , 'x' , htmlspecialchars($height) , '</td>';
221                         echo '</tr>';
222                 }
223         } // if (sizeof($arr)>0)
224         ?>
225
226                 </table>
227         <?php
228         if ($idxStart > 0)
229                 echo "<a href='media.php?offset=$idxPrev&amp;collection=".urlencode($currentCollection)."'>". _LISTS_PREV."</a> ";
230         if ($idxEnd < sizeof($arr))
231                 echo "<a href='media.php?offset=$idxNext&amp;collection=".urlencode($currentCollection)."'>". _LISTS_NEXT."</a> ";
232
233         ?>
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>
236         <?php
237         media_foot();
238
239
240 }
241
242 /**
243   * Shows a screen where you can select the file to upload
244   */
245 function media_choose() {
246         global $CONF, $member, $manager;
247
248         $currentCollection = requestVar('collection');
249
250         $collections = MEDIA::getCollectionList();
251
252         media_head();
253         ?>
254         <h1><?php echo _UPLOAD_TITLE?></h1>
255
256         <p><?php echo _UPLOAD_MSG?></p>
257
258         <form method="post" enctype="multipart/form-data" action="media.php">
259         <div>
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']?>" />
263           File:
264           <br />
265           <input name="uploadfile" type="file" size="40" />
266         <?php           if (sizeof($collections) > 1) {
267         ?>
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"';
274                                         }
275                                         echo '>',htmlspecialchars($description),'</option>';
276                                 }
277                         ?>
278                 </select>
279         <?php           } else {
280         ?>
281                 <input name="collection" type="hidden" value="<?php echo htmlspecialchars(requestVar('collection'))?>" />
282         <?php           } // if sizeof
283         ?>
284         <br /><br />
285         <?php
286         $manager->notify(
287                 'MediaUploadFormExtras',
288                 array()
289         );
290         ?>
291         <br /><br />
292         <input type="submit" value="<?php echo _UPLOAD_BUTTON?>" />
293         </div>
294         </form>
295
296         <?php
297         media_foot();
298 }
299
300
301 /**
302   * accepts a file for upload
303   */
304 function media_upload() {
305         global $DIR_MEDIA, $member, $CONF;
306         
307         $uploadInfo = postFileInfo('uploadfile');
308         
309         $filename = $uploadInfo['name'];
310         $filetype = $uploadInfo['type'];
311         $filesize = $uploadInfo['size'];
312         $filetempname = $uploadInfo['tmp_name'];
313         $fileerror = intval($uploadInfo['error']);
314         
315         // clean filename of characters that may cause trouble in a filename using cleanFileName() function from globalfunctions.php
316         $filename = cleanFileName($filename);
317         if ($filename === false) 
318                 media_doError(_ERROR_BADFILETYPE);
319         
320         switch ($fileerror)
321         {
322                 case 0: // = UPLOAD_ERR_OK
323                         break;
324                 case 1: // = UPLOAD_ERR_INI_SIZE
325                 case 2: // = UPLOAD_ERR_FORM_SIZE
326                         media_doError(_ERROR_FILE_TOO_BIG);
327                 case 3: // = UPLOAD_ERR_PARTIAL
328                 case 4: // = UPLOAD_ERR_NO_FILE
329                 case 6: // = UPLOAD_ERR_NO_TMP_DIR
330                 case 7: // = UPLOAD_ERR_CANT_WRITE
331                 default:
332                         // include error code for debugging
333                         // (see http://www.php.net/manual/en/features.file-upload.errors.php)
334                         media_doError(_ERROR_BADREQUEST . ' (' . $fileerror . ')');
335         }
336         
337         if ($filesize > $CONF['MaxUploadSize'])
338                 media_doError(_ERROR_FILE_TOO_BIG);
339         
340         // check file type against allowed types
341         $ok = 0;
342         $allowedtypes = explode (',', $CONF['AllowedTypes']);
343         foreach ( $allowedtypes as $type )
344         {
345                 //if (eregi("\." .$type. "$",$filename)) $ok = 1;
346                 if (preg_match("#\." .$type. "$#i",$filename)) $ok = 1;
347         }
348         if (!$ok) media_doError(_ERROR_BADFILETYPE);
349         
350         if (!is_uploaded_file($filetempname))
351                 media_doError(_ERROR_BADREQUEST);
352         
353         // prefix filename with current date (YYYY-MM-DD-)
354         // this to avoid nameclashes
355         if ($CONF['MediaPrefix'])
356                 $filename = strftime("%Y%m%d-", time()) . $filename;
357
358         $collection = requestVar('collection');
359         $res = MEDIA::addMediaObject($collection, $filetempname, $filename);
360
361         if ($res != '')
362                 media_doError($res);
363
364         // shows updated list afterwards
365         media_select();
366 }
367
368 function media_loginAndPassThrough() {
369         media_head();
370         ?>
371                 <h1><?php echo _LOGIN_PLEASE?></h1>
372
373                 <form method="post" action="media.php">
374                 <div>
375                         <input name="action" value="login" type="hidden" />
376                         <input name="collection" value="<?php echo htmlspecialchars(requestVar('collection'))?>" type="hidden" />
377                         <?php echo _LOGINFORM_NAME?>: <input name="login" />
378                         <br /><?php echo _LOGINFORM_PWD?>: <input name="password" type="password" />
379                         <br /><input type="submit" value="<?php echo _LOGIN?>" />
380                 </div>
381                 </form>
382                 <p><a href="media.php" onclick="window.close();"><?php echo _POPUP_CLOSE?></a></p>
383         <?php   media_foot();
384         exit;
385 }
386
387 function media_doError($msg) {
388         media_head();
389         ?>
390         <h1><?php echo _ERROR?></h1>
391         <p><?php echo $msg?></p>
392         <p><a href="media.php" onclick="history.back(); return false;"><?php echo _BACK?></a></p>
393         <?php   media_foot();
394         exit;
395 }
396
397
398 function media_head() {
399 ?>
400         <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
401         <html <?php echo _HTML_XML_NAME_SPACE_AND_LANG_CODE; ?>>
402         <head>
403                 <meta http-equiv="Content-Type" content="text/html; charset=<?php echo _CHARSET ?>" />
404                 <title>Nucleus Media</title>
405                 <link rel="stylesheet" type="text/css" href="styles/popups.css" />
406                 <script type="text/javascript">
407                         var type = 0;
408                         function setType(val) { type = val; }
409
410                         function chooseImage(collection, filename, width, height) {
411                                 window.opener.focus();
412                                 window.opener.includeImage(collection,
413                                                                                    filename,
414                                                                                    type == 0 ? 'inline' : 'popup',
415                                                                                    width,
416                                                                                    height
417                                                                                    );
418                                 window.close();
419                         }
420
421                         function chooseOther(collection, filename) {
422                                 window.opener.focus();
423                                 window.opener.includeOtherMedia(collection, filename);
424                                 window.close();
425
426                         }
427                 </script>
428         </head>
429         <body>
430 <?php }
431
432 function media_foot() {
433 ?>
434         </body>
435         </html>
436 <?php }
437
438 ?>