OSDN Git Service

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