OSDN Git Service

新規インストール時の複数スキンインストール対応(install.php)
[nucleus-jp/nucleus-jp-ancient.git] / utf8 / nucleus / media.php
1 <?php
2 /*
3  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
4  * Copyright (C) 2002-2009 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-2009 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 require('../config.php');
38 include($DIR_LIBS . 'MEDIA.php');       // media classes
39
40 sendContentType('application/xhtml+xml', 'media');
41
42 // user needs to be logged in to use this
43 if (!$member->isLoggedIn()) {
44         media_loginAndPassThrough();
45         exit;
46 }
47
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);
53
54 // get action
55 $action = requestVar('action');
56 if ($action == '')
57         $action = 'selectmedia';
58
59 // check ticket
60 $aActionsNotToCheck = array('selectmedia', _MEDIA_FILTER_APPLY, _MEDIA_COLLECTION_SELECT);
61 if (!in_array($action, $aActionsNotToCheck))
62 {
63         if (!$manager->checkTicket())
64                 media_doError(_ERROR_BADTICKET);
65 }
66
67
68 switch($action) {
69         case 'chooseupload':
70         case _MEDIA_UPLOAD_TO:
71         case _MEDIA_UPLOAD_NEW:
72                 if (!$member->isAdmin() and $CONF['AllowUpload'] != true) {
73                         media_doError(_ERROR_DISALLOWED);
74                 } else {
75                         media_choose();
76                 }
77                 break;
78         case 'uploadfile':
79                 if (!$member->isAdmin() and $CONF['AllowUpload'] != true) {
80                         media_doError(_ERROR_DISALLOWED);
81                 } else {
82                         media_upload();
83                 }
84                 break;
85         case _MEDIA_FILTER_APPLY:
86         case 'selectmedia':
87         case _MEDIA_COLLECTION_SELECT:
88         default:
89                 media_select();
90                 break;
91 }
92
93 // select a file
94 function media_select() {
95         global $member, $CONF, $DIR_MEDIA, $manager;
96
97         // show 10 files + navigation buttons
98         // show msg when no files
99         // show upload form
100         // files sorted according to last modification date
101
102         // currently selected collection
103         $currentCollection = requestVar('collection');
104         if (!$currentCollection || !@is_dir($DIR_MEDIA . $currentCollection))
105                 $currentCollection = $member->getID();
106
107         // avoid directory travarsal and accessing invalid directory
108         if (!MEDIA::isValidCollection($currentCollection)) media_doError(_ERROR_DISALLOWED);
109
110         media_head();
111
112         // get collection list
113         $collections = MEDIA::getCollectionList();
114
115         if (sizeof($collections) > 1) {
116         ?>
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"';
124                                                 }
125                                                 echo '>',htmlspecialchars($description),'</option>';
126                                         }
127                                 ?>
128                         </select>
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() ?>
132                 </div></form>
133         <?php   } else {
134         ?>
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() ?>
139                 </div></form>
140         <?php   } // if sizeof
141
142         $filter = requestVar('filter');
143         $offset = intRequestVar('offset');
144         $arr = MEDIA::getMediaListByCollection($currentCollection, $filter);
145
146         ?>
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)?>" />
153                 </div></form>
154
155         <?php
156
157         ?>
158                 <table width="100%">
159                 <caption><?php echo _MEDIA_COLLECTION_LABEL . htmlspecialchars($collections[$currentCollection])?></caption>
160                 <tr>
161                  <th><?php echo _MEDIA_MODIFIED?></th><th><?php echo _MEDIA_FILENAME?></th><th><?php echo _MEDIA_DIMENSIONS?></th>
162                 </tr>
163
164         <?php
165
166         if (sizeof($arr)>0) {
167
168                 if (($offset + $CONF['MediaPerPage']) >= sizeof($arr))
169                         $offset = sizeof($arr) - $CONF['MediaPerPage'];
170
171                 if ($offset < 0) $offset = 0;
172
173                 $idxStart = $offset;
174                 $idxEnd = $offset + $CONF['MediaPerPage'];
175                 $idxNext = $idxEnd;
176                 $idxPrev = $idxStart - $CONF['MediaPerPage'];
177
178                 if ($idxPrev < 0) $idxPrev = 0;
179
180                 if ($idxEnd > sizeof($arr))
181                         $idxEnd = sizeof($arr);
182
183                 for($i=$idxStart;$i<$idxEnd;$i++) {
184                         $obj = $arr[$i];
185                         $filename = $DIR_MEDIA . $currentCollection . '/' . $obj->filename;
186
187                         $old_level = error_reporting(0);
188                         $size = @GetImageSize($filename);
189                         error_reporting($old_level);
190                         $width = $size[0];
191                         $height = $size[1];
192                         $filetype = $size[2];
193
194                         echo "<tr>";
195                         echo "<td>". date("Y-m-d",$obj->timestamp) ."</td>";
196
197                         // strings for javascript
198                         $jsCurrentCollection = str_replace("'","\\'",$currentCollection);
199                         $jsFileName = str_replace("'","\\'",$obj->filename);
200
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,'...'))
207                                                            ."</a>";
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>)';
209                                 echo "</td>";
210                         } else {
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,'...'))
215                                                            ."</a></td>";
216
217                         }
218                         echo '<td>' , htmlspecialchars($width) , 'x' , htmlspecialchars($height) , '</td>';
219                         echo '</tr>';
220                 }
221         } // if (sizeof($arr)>0)
222         ?>
223
224                 </table>
225         <?php
226         if ($idxStart > 0)
227                 echo "<a href='media.php?offset=$idxPrev&amp;collection=".urlencode($currentCollection)."'>". _LISTS_PREV."</a> ";
228         if ($idxEnd < sizeof($arr))
229                 echo "<a href='media.php?offset=$idxNext&amp;collection=".urlencode($currentCollection)."'>". _LISTS_NEXT."</a> ";
230
231         ?>
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>
234         <?php
235         media_foot();
236
237
238 }
239
240 /**
241   * Shows a screen where you can select the file to upload
242   */
243 function media_choose() {
244         global $CONF, $member, $manager;
245
246         $currentCollection = requestVar('collection');
247
248         $collections = MEDIA::getCollectionList();
249
250         media_head();
251         ?>
252         <h1><?php echo _UPLOAD_TITLE?></h1>
253
254         <p><?php echo _UPLOAD_MSG?></p>
255
256         <form method="post" enctype="multipart/form-data" action="media.php">
257         <div>
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']?>" />
261           File:
262           <br />
263           <input name="uploadfile" type="file" size="40" />
264         <?php           if (sizeof($collections) > 1) {
265         ?>
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"';
272                                         }
273                                         echo '>',htmlspecialchars($description),'</option>';
274                                 }
275                         ?>
276                 </select>
277         <?php           } else {
278         ?>
279                 <input name="collection" type="hidden" value="<?php echo htmlspecialchars(requestVar('collection'))?>" />
280         <?php           } // if sizeof
281         ?>
282           <br /><br />
283           <input type="submit" value="<?php echo _UPLOAD_BUTTON?>" />
284         </div>
285         </form>
286
287         <?php
288         media_foot();
289 }
290
291
292 /**
293   * accepts a file for upload
294   */
295 function media_upload() {
296         global $DIR_MEDIA, $member, $CONF;
297
298         $uploadInfo = postFileInfo('uploadfile');
299
300         $filename = $uploadInfo['name'];
301         $filetype = $uploadInfo['type'];
302         $filesize = $uploadInfo['size'];
303         $filetempname = $uploadInfo['tmp_name'];
304         $fileerror = intval($uploadInfo['error']);
305
306         switch ($fileerror)
307         {
308                 case 0: // = UPLOAD_ERR_OK
309                         break;
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
317                 default:
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 . ')');
321         }
322
323         if ($filesize > $CONF['MaxUploadSize'])
324                 media_doError(_ERROR_FILE_TOO_BIG);
325
326         // check file type against allowed types
327         $ok = 0;
328         $allowedtypes = explode (',', $CONF['AllowedTypes']);
329         foreach ( $allowedtypes as $type )
330                 if (eregi("\." .$type. "$",$filename)) $ok = 1;
331         if (!$ok) media_doError(_ERROR_BADFILETYPE);
332
333         if (!is_uploaded_file($filetempname))
334                 media_doError(_ERROR_BADREQUEST);
335
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;
340
341         $collection = requestVar('collection');
342         $res = MEDIA::addMediaObject($collection, $filetempname, $filename);
343
344         if ($res != '')
345                 media_doError($res);
346
347         // shows updated list afterwards
348         media_select();
349 }
350
351 function media_loginAndPassThrough() {
352         media_head();
353         ?>
354                 <h1><?php echo _LOGIN_PLEASE?></h1>
355
356                 <form method="post" action="media.php">
357                 <div>
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?>" />
363                 </div>
364                 </form>
365                 <p><a href="media.php" onclick="window.close();"><?php echo _POPUP_CLOSE?></a></p>
366         <?php   media_foot();
367         exit;
368 }
369
370 function media_doError($msg) {
371         media_head();
372         ?>
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>
376         <?php   media_foot();
377         exit;
378 }
379
380
381 function media_head() {
382 ?>
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; ?>>
385         <head>
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">
390                         var type = 0;
391                         function setType(val) { type = val; }
392
393                         function chooseImage(collection, filename, width, height) {
394                                 window.opener.focus();
395                                 window.opener.includeImage(collection,
396                                                                                    filename,
397                                                                                    type == 0 ? 'inline' : 'popup',
398                                                                                    width,
399                                                                                    height
400                                                                                    );
401                                 window.close();
402                         }
403
404                         function chooseOther(collection, filename) {
405                                 window.opener.focus();
406                                 window.opener.includeOtherMedia(collection, filename);
407                                 window.close();
408
409                         }
410                 </script>
411         </head>
412         <body>
413 <?php }
414
415 function media_foot() {
416 ?>
417         </body>
418         </html>
419 <?php }
420
421 ?>