OSDN Git Service

This commit was generated by cvs2svn to compensate for changes in r4,
[nucleus-jp/nucleus-jp-ancient.git] / euc / nucleus / media.php
1 <?php
2 /**
3   * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/) 
4   * Copyright (C) 2002-2004 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   * Media popup window for Nucleus
13   *
14   * Purpose:
15   *   - can be openen from an add-item form or bookmarklet popup
16   *   - shows a list of recent files, allowing browsing, search and 
17   *     upload of new files
18   *   - close the popup by selecting a file in the list. The file gets
19   *     passed through to the add-item form (linkto, popupimg or inline img)
20   *
21   * $Id: media.php,v 1.1.1.1 2005-02-28 07:13:53 kimitake Exp $
22   */
23   
24 $CONF = array();
25
26 // defines how much media items will be shown per page. You can override this
27 // in config.php if you like. (changing it in config.php instead of here will
28 // allow your settings to be kept even after a Nucleus upgrade)
29 $CONF['MediaPerPage'] = 10;
30
31 // include all classes and config data
32 include('../config.php');
33 include($DIR_LIBS . 'MEDIA.php');       // media classes
34
35 sendContentType('application/xhtml+xml', 'media');
36
37 // user needs to be logged in to use this
38 if (!$member->isLoggedIn()) {
39         media_loginAndPassThrough();
40         exit;
41 }
42
43 // check if member is on at least one teamlist
44 $query = 'SELECT * FROM ' . sql_table('team'). ' WHERE tmember=' . $member->getID();
45 $teams = mysql_query($query);
46 if (mysql_num_rows($teams) == 0)
47         media_doError(_ERROR_DISALLOWEDUPLOAD);
48         
49 // get action
50 $action = requestVar('action');
51 if ($action == '')
52         $action = 'selectmedia';
53         
54 // check ticket
55 $aActionsNotToCheck = array('selectmedia', _MEDIA_FILTER_APPLY, _MEDIA_COLLECTION_SELECT);
56 if (!in_array($action, $aActionsNotToCheck))
57 {
58         if (!$manager->checkTicket())
59                 media_doError(_ERROR_BADTICKET);
60
61
62
63 switch($action) {
64         case 'chooseupload':
65         case _MEDIA_UPLOAD_TO:
66         case _MEDIA_UPLOAD_NEW:
67                 media_choose();
68                 break;
69         case 'uploadfile':
70                 media_upload();
71                 break;
72         case _MEDIA_FILTER_APPLY:
73         case 'selectmedia':
74         case _MEDIA_COLLECTION_SELECT:
75         default:
76                 media_select();
77                 break;
78 }
79
80 // select a file
81 function media_select() {
82         global $member, $CONF, $DIR_MEDIA, $manager;
83         
84         media_head();
85         
86         // show 10 files + navigation buttons 
87         // show msg when no files
88         // show upload form
89         // files sorted according to last modification date
90
91         // currently selected collection
92         $currentCollection = requestVar('collection');
93         if (!$currentCollection || !@is_dir($DIR_MEDIA . $currentCollection))
94                 $currentCollection = $member->getID();
95                 
96         
97         // get collection list
98         $collections = MEDIA::getCollectionList();
99
100         if (sizeof($collections) > 1) {
101         ?>
102                 <form method="post" action="media.php"><div>
103                         <label for="media_collection"><?php echo htmlspecialchars(_MEDIA_COLLECTION_LABEL)?></label>
104                         <select name="collection" id="media_collection">
105                                 <?php                                   foreach ($collections as $dirname => $description) {
106                                                 echo '<option value="',htmlspecialchars($dirname),'"';
107                                                 if ($dirname == $currentCollection) {
108                                                         echo ' selected="selected"';
109                                                 }
110                                                 echo '>',htmlspecialchars($description),'</option>';
111                                         }
112                                 ?>
113                         </select>
114                         <input type="submit" name="action" value="<?php echo htmlspecialchars(_MEDIA_COLLECTION_SELECT) ?>" title="<?php echo htmlspecialchars(_MEDIA_COLLECTION_TT)?>" />
115                         <input type="submit" name="action" value="<?php echo htmlspecialchars(_MEDIA_UPLOAD_TO) ?>" title="<?php echo htmlspecialchars(_MEDIA_UPLOADLINK) ?>" />
116                         <?php $manager->addTicketHidden() ?>
117                 </div></form>
118         <?php   } else {
119         ?>
120                 <form method="post" action="media.php" style="float:right"><div>
121                         <input type="hidden" name="collection" value="<?php echo htmlspecialchars($currentCollection)?>" />
122                         <input type="submit" name="action" value="<?php echo htmlspecialchars(_MEDIA_UPLOAD_NEW) ?>" title="<?php echo htmlspecialchars(_MEDIA_UPLOADLINK) ?>" />
123                         <?php $manager->addTicketHidden() ?>
124                 </div></form>   
125         <?php   } // if sizeof
126         
127         $filter = requestVar('filter'); 
128         $offset = intRequestVar('offset');      
129         $arr = MEDIA::getMediaListByCollection($currentCollection, $filter);
130
131         ?>
132                 <form method="post" action="media.php"><div>
133                         <label for="media_filter"><?php echo htmlspecialchars(_MEDIA_FILTER_LABEL)?></label>
134                         <input id="media_filter" type="text" name="filter" value="<?php echo htmlspecialchars($filter)?>" />
135                         <input type="submit" name="action" value="<?php echo htmlspecialchars(_MEDIA_FILTER_APPLY) ?>" />
136                         <input type="hidden" name="collection" value="<?php echo htmlspecialchars($currentCollection)?>" />                     
137                         <input type="hidden" name="offset" value="<?php echo intval($offset)?>" />                                              
138                 </div></form>   
139         
140         <?php
141         
142         ?>      
143                 <table width="100%">
144                 <caption><?php echo _MEDIA_COLLECTION_LABEL . htmlspecialchars($collections[$currentCollection])?></caption>
145                 <tr>
146                  <th><?php echo _MEDIA_MODIFIED?></th><th><?php echo _MEDIA_FILENAME?></th><th><?php echo _MEDIA_DIMENSIONS?></th>
147                 </tr>
148         
149         <?php   
150         
151         if (sizeof($arr)>0) {
152         
153                 if (($offset + $CONF['MediaPerPage']) >= sizeof($arr))
154                         $offset = sizeof($arr) - $CONF['MediaPerPage'];
155
156                 if ($offset < 0) $offset = 0;
157                 
158                 $idxStart = $offset;
159                 $idxEnd = $offset + $CONF['MediaPerPage'];
160                 $idxNext = $idxEnd;
161                 $idxPrev = $idxStart - $CONF['MediaPerPage'];
162
163                 if ($idxPrev < 0) $idxPrev = 0;
164
165                 if ($idxEnd > sizeof($arr))
166                         $idxEnd = sizeof($arr);
167
168                 for($i=$idxStart;$i<$idxEnd;$i++) {
169                         $obj = $arr[$i];
170                         $filename = $DIR_MEDIA . $currentCollection . '/' . $obj->filename;
171
172                         $old_level = error_reporting(0);
173                         $size = @GetImageSize($filename); 
174                         error_reporting($old_level);
175                         $width = $size[0];
176                         $height = $size[1];
177                         $filetype = $size[2];
178                         
179                         echo "<tr>";
180                         echo "<td>". date("Y-m-d",$obj->timestamp) ."</td>";
181
182                         if ($filetype != 0) {
183                                 // image (gif/jpg/png/swf)
184                                 echo "<td><a href='media.php' onclick='chooseImage(\"$currentCollection\",\"$obj->filename\","
185                                                            . "\"$width\",\"$height\""
186                                                            . ")' title='" . htmlspecialchars($obj->filename). "'>"
187                                                            . htmlspecialchars(shorten($obj->filename,25,'...'))
188                                                            ."</a>";
189                                 echo ' (<a href="', htmlspecialchars($CONF['MediaURL'] . $currentCollection . '/' . $obj->filename), '" onclick="window.open(this.href); return false;" title="',htmlspecialchars(_MEDIA_VIEW_TT),'">',_MEDIA_VIEW,'</a>)';
190                                 echo "</td>";
191                         } else {
192                                 // no image (e.g. mpg)
193                                 echo "<td><a href='media.php' onclick='chooseOther(\"$currentCollection\",\"$obj->filename\""
194                                                            . ")' title='" . htmlspecialchars($obj->filename). "'>"
195                                                            . htmlspecialchars(shorten($obj->filename,30,'...'))
196                                                            ."</a></td>";
197
198                         }
199                         echo '<td>' , $width , 'x' , $height , '</td>';
200                         echo '</tr>';
201                 }
202         } // if (sizeof($arr)>0)
203         ?>
204         
205                 </table>
206         <?php   
207         if ($idxStart > 0)
208                 echo "<a href='media.php?offset=$idxPrev&amp;collection=".urlencode($currentCollection)."'>". _LISTS_PREV."</a> ";
209         if ($idxEnd < sizeof($arr))
210                 echo "<a href='media.php?offset=$idxNext&amp;collection=".urlencode($currentCollection)."'>". _LISTS_NEXT."</a> ";
211         
212         ?>
213                 <input id="typeradio0" type="radio" name="typeradio" onclick="setType(0);" checked="checked" /><label for="typeradio0"><?php echo _MEDIA_INLINE?></label>
214                 <input id="typeradio1" type="radio" name="typeradio" onclick="setType(1);" /><label for="typeradio1"><?php echo _MEDIA_POPUP?></label>
215         <?php   
216         media_foot();
217      
218                 
219 }
220
221 /**
222   * Shows a screen where you can select the file to upload
223   */
224 function media_choose() {
225         global $CONF, $member, $manager;
226
227         $currentCollection = requestVar('collection');
228         
229         $collections = MEDIA::getCollectionList();
230
231         media_head();
232         ?>
233         <h1><?php echo _UPLOAD_TITLE?></h1>
234         
235         <p><?php echo _UPLOAD_MSG?></p>
236         
237         <form method="post" enctype="multipart/form-data" action="media.php">
238         <div>
239           <input type="hidden" name="action" value="uploadfile" />
240           <?php $manager->addTicketHidden() ?>
241           <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $CONF['MaxUploadSize']?>" />
242           File:
243           <br />
244           <input name="uploadfile" type="file" size="40" />
245         <?php           if (sizeof($collections) > 1) {
246         ?>
247                 <br /><br /><label for="upload_collection">Collection:</label>
248                 <br /><select name="collection" id="upload_collection">
249                         <?php                           foreach ($collections as $dirname => $description) {
250                                         echo '<option value="',htmlspecialchars($dirname),'"';
251                                         if ($dirname == $currentCollection) {
252                                                 echo ' selected="selected"';
253                                         }
254                                         echo '>',htmlspecialchars($description),'</option>';
255                                 }
256                         ?>
257                 </select>
258         <?php           } else {
259         ?>
260                 <input name="collection" type="hidden" value="<?php echo htmlspecialchars(requestVar('collection'))?>" />                       
261         <?php           } // if sizeof
262         ?>  
263           <br /><br />
264           <input type="submit" value="<?php echo _UPLOAD_BUTTON?>" />
265         </div>
266         </form>
267         
268         <?php   
269         media_foot();
270 }
271
272
273 /**
274   * accepts a file for upload
275   */
276 function media_upload() {
277         global $DIR_MEDIA, $member, $CONF;
278
279         $uploadInfo = postFileInfo('uploadfile');
280         
281         $filename = $uploadInfo['name'];
282         $filetype = $uploadInfo['type'];
283         $filesize = $uploadInfo['size'];
284         $filetempname = $uploadInfo['tmp_name'];
285         
286         if ($filesize > $CONF['MaxUploadSize'])
287                 media_doError(_ERROR_FILE_TOO_BIG);
288         
289         // check file type against allowed types
290         $ok = 0;
291         $allowedtypes = explode (',', $CONF['AllowedTypes']);
292         foreach ( $allowedtypes as $type ) 
293                 if (eregi("\." .$type. "$",$filename)) $ok = 1;    
294         if (!$ok) media_doError(_ERROR_BADFILETYPE);
295                 
296         if (!is_uploaded_file($filetempname)) 
297                 media_doError(_ERROR_BADREQUEST);
298
299         // prefix filename with current date (YYYY-MM-DD-)
300         // this to avoid nameclashes
301         if ($CONF['MediaPrefix'])
302                 $filename = strftime("%Y%m%d-", time()) . $filename;
303
304         $collection = requestVar('collection');
305         $res = MEDIA::addMediaObject($collection, $filetempname, $filename);
306
307         if ($res != '') 
308                 media_doError($res);
309         
310         // shows updated list afterwards
311         media_select();
312 }
313
314 function media_loginAndPassThrough() {
315         media_head();
316         ?>
317                 <h1><?php echo _LOGIN_PLEASE?></h1>
318         
319                 <form method="post" action="media.php">
320                 <div>
321                         <input name="action" value="login" type="hidden" />
322                         <input name="collection" value="<?php echo htmlspecialchars(requestVar('collection'))?>" type="hidden" />                       
323                         <?php echo _LOGINFORM_NAME?>: <input name="login" />
324                         <br /><?php echo _LOGINFORM_PWD?>: <input name="password" type="password" />
325                         <br /><input type="submit" value="<?php echo _LOGIN?>" />
326                 </div>
327                 </form>
328                 <p><a href="media.php" onclick="window.close();"><?php echo _POPUP_CLOSE?></a></p>
329         <?php   media_foot();
330         exit;
331 }
332
333 function media_doError($msg) {
334         media_head();
335         ?>
336         <h1><?php echo _ERROR?></h1>
337         <p><?php echo $msg?></p>
338         <p><a href="media.php" onclick="history.back()"><?php echo _BACK?></a></p>
339         <?php   media_foot();
340         exit;
341 }
342
343
344 function media_head() {
345 ?>
346         <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
347         <html xmlns="http://www.w3.org/1999/xhtml">
348         <head>
349                 <meta http-equiv="Content-Type" content="text/html; charset=EUC-JP" />
350                 <title>Nucleus Media</title>
351                 <link rel="stylesheet" type="text/css" href="styles/popups.css" />
352                 <script type="text/javascript">
353                         var type = 0;
354                         function setType(val) { type = val; }
355                         
356                         function chooseImage(collection, filename, width, height) {
357                                 window.opener.focus(); 
358                                 window.opener.includeImage(collection,
359                                                                                    filename, 
360                                                            type == 0 ? 'inline' : 'popup',
361                                                            width,
362                                                            height
363                                                            );
364                                 window.close();
365                         }
366                         
367                         function chooseOther(collection, filename) {
368                                 window.opener.focus(); 
369                                 window.opener.includeOtherMedia(collection, filename);
370                                 window.close();
371                         
372                         }
373                 </script>
374         </head>
375         <body>          
376 <?php }
377
378 function media_foot() {
379 ?>
380         </body>
381         </html> 
382 <?php } 
383
384 ?>