OSDN Git Service

FIX: PHP5/MySQL5における文法違反コードの修正
[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         $param = array();
287         $manager->notify('MediaUploadFormExtras', $param);
288         ?>
289         <br /><br />
290         <input type="submit" value="<?php echo _UPLOAD_BUTTON?>" />
291         </div>
292         </form>
293
294         <?php
295         media_foot();
296 }
297
298
299 /**
300   * accepts a file for upload
301   */
302 function media_upload() {
303         global $DIR_MEDIA, $member, $CONF;
304         
305         $uploadInfo = postFileInfo('uploadfile');
306         
307         $filename = $uploadInfo['name'];
308         $filetype = $uploadInfo['type'];
309         $filesize = $uploadInfo['size'];
310         $filetempname = $uploadInfo['tmp_name'];
311         $fileerror = intval($uploadInfo['error']);
312         
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);
317         
318         switch ($fileerror)
319         {
320                 case 0: // = UPLOAD_ERR_OK
321                         break;
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
329                 default:
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 . ')');
333         }
334         
335         if ($filesize > $CONF['MaxUploadSize'])
336                 media_doError(_ERROR_FILE_TOO_BIG);
337         
338         // check file type against allowed types
339         $ok = 0;
340         $allowedtypes = explode (',', $CONF['AllowedTypes']);
341         foreach ( $allowedtypes as $type )
342         {
343                 //if (eregi("\." .$type. "$",$filename)) $ok = 1;
344                 if (preg_match("#\." .$type. "$#i",$filename)) $ok = 1;
345         }
346         if (!$ok) media_doError(_ERROR_BADFILETYPE);
347         
348         if (!is_uploaded_file($filetempname))
349                 media_doError(_ERROR_BADREQUEST);
350         
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;
355
356         $collection = requestVar('collection');
357         $res = MEDIA::addMediaObject($collection, $filetempname, $filename);
358
359         if ($res != '')
360                 media_doError($res);
361
362         // shows updated list afterwards
363         media_select();
364 }
365
366 function media_loginAndPassThrough() {
367         media_head();
368         ?>
369                 <h1><?php echo _LOGIN_PLEASE?></h1>
370
371                 <form method="post" action="media.php">
372                 <div>
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?>" />
378                 </div>
379                 </form>
380                 <p><a href="media.php" onclick="window.close();"><?php echo _POPUP_CLOSE?></a></p>
381         <?php   media_foot();
382         exit;
383 }
384
385 function media_doError($msg) {
386         media_head();
387         ?>
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>
391         <?php   media_foot();
392         exit;
393 }
394
395
396 function media_head() {
397 ?>
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; ?>>
400         <head>
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">
405                         var type = 0;
406                         function setType(val) { type = val; }
407
408                         function chooseImage(collection, filename, width, height) {
409                                 window.opener.focus();
410                                 window.opener.includeImage(collection,
411                                                                                    filename,
412                                                                                    type == 0 ? 'inline' : 'popup',
413                                                                                    width,
414                                                                                    height
415                                                                                    );
416                                 window.close();
417                         }
418
419                         function chooseOther(collection, filename) {
420                                 window.opener.focus();
421                                 window.opener.includeOtherMedia(collection, filename);
422                                 window.close();
423
424                         }
425                 </script>
426         </head>
427         <body>
428 <?php }
429
430 function media_foot() {
431 ?>
432         </body>
433         </html>
434 <?php }
435
436 ?>