2 class NP_Medium extends NucleusPlugin
8 public $collection = '';
9 public $description = '';
14 public $media = array();
19 public function getName()
24 public function getAuthor()
26 return 'Sakamoto Takashi';
29 public function getURL()
34 public function getVersion()
39 public function getMinNucleusVersion()
44 public function getDescription()
49 public function supportsFeature($what)
51 return ( $what == 'SqlTablePrefix' );
54 public function getEventList()
56 return array('AdminPrePageHead', 'AdminTemplateExtraFields');
59 public function init()
61 // include translation file for this plugin
62 if ( file_exists($this->getDirectory() . 'locales/' . i18n::get_current_locale() . '.' . i18n::get_current_charset() . '.php') )
64 include_once($this->getDirectory() . 'locales/' . i18n::get_current_locale() . '.' . i18n::get_current_charset() . '.php');
68 include_once($this->getDirectory() . 'locales/en_Latn_US.UTF-8.php');
73 public function install()
75 $this->createOption('collectionlist_head', 'NP_MEDIUM_COLLECTIONLIST_HEAD', 'textarea',
76 "<label for=\"media_collection\">"
77 . "<%text(_MEDIA_COLLECTION_LABEL)%>"
79 . "<select name=\"collection\" id=\"media_collection\">\n");
81 $this->createOption('collectionlist_body', 'NP_MEDIUM_COLLECTIONLIST_BODY', 'textarea',
82 "<option value=\"<%name%>\" <%selected%>>"
86 $this->createOption('collectionlist_foot', 'NP_MEDIUM_COLLECTIONLIST_FOOT', 'textarea',
89 $this->createOption('medialist_head', 'NP_MEDIUM_MEDIALIST_HEAD', 'textarea',
90 "<table frame=\"box\" rules=\"frame\" sumary=\"<%text(Media List)%>\">\n"
91 . "<caption><%text(_MEDIA_COLLECTION_LABEL)%> <%description%></caption>\n"
94 . "<th><%text(_MEDIA_MODIFIED)%></th>\n"
95 . "<th><%text(_MEDIA_FILENAME)%></th>\n"
96 . "<th><%text(_MEDIA_DIMENSIONS)%></th>\n"
101 $this->createOption('medialist_body_image', 'NP_MEDIUM_MEDIALIST_BODY_IMAGE', 'textarea',
103 . "<td><%timestamp%></td>\n"
105 . "<a href=\"<%mediumurl%>\" onclick=\"medium.chooseImage('<%collection%>', '<%filename%>', <%width%>, <%height%>);\" title=\"<%filename%>\">\n"
106 . "<%shortfilename%>\n"
109 . "<a href=\"<%mediumurl%>\" onclick=\"window.open(this.href); return false;\" title=\"<%text(_MEDIA_VIEW_TT%>\">\n"
110 . "<%text(_MEDIA_VIEW)%>\n"
114 . "<td><%width%> x <%height%></td>\n"
117 $this->createOption('medialist_body_other', 'NP_MEDIUM_MEDIALIST_BODY_OTHER', 'textarea',
119 . "<td><%timestamp%></td>\n"
121 . "<a href=\"<%mediumurl%>\" onclick=\"medium.chooseOther('<%collection%>', '<%filename%>');\" title=\"<%filename%>\">\n"
122 . "<%shortfilename%>\n"
125 . "<td><%size%> KB</td>\n"
128 $this->createOption('medialist_foot', 'NP_MEDIUM_MEDIALIST_FOOT', 'textarea',
132 $this->createOption('medialist_blank', 'NP_MEDIUM_MEDIALIST_BLANK', 'textarea',
138 public function event_AdminPrePageHead($data)
142 if ( !in_array($data['action'], array('createitem', 'itemedit')) )
147 $data['extrahead'] .= "<script type=\"text/javascript\" src=\"{$CONF['PluginURL']}/medium/scripts/medium.js\"></script>\n"
148 . "<script type=\"text/javascript\">\n"
149 . " medium.url = '{$CONF['ActionURL']}?action=plugin&name=medium';\n"
155 public function doAction($type)
157 global $CONF, $DIR_MEDIA, $manager, $member;
160 * defines how much media items will be shown per page. You can override this
161 * in config.php if you like. (changing it in config.php instead of here will
162 * allow your settings to be kept even after a Nucleus upgrade)
164 $CONF['MediaPerPage'] = 10;
166 /* include all classes */
167 if ( !class_exists('Media', FALSE) )
169 include_libs('MEDIA.php', FALSE, FALSE);
172 /* include all classes */
173 if ( !class_exists('BaseActions', FALSE) )
175 include_libs('BaseActions.php', FALSE, FALSE);
178 if ( !class_exists('MediumActions', FALSE) )
180 include($this->getDirectory() . 'MediumActions.php');
183 /* get skin object */
184 $skinid = $CONF['AdminSkin'];
185 if ( !Skin::existsID($skinid) )
190 $this->skin = new Skin($skinid, 'MediumActions', 'MediumSkin');
192 /* user needs to be logged in to use this */
193 if ( !$member->isLoggedIn() )
199 /* check if member is on at least one teamlist */
200 $query = 'SELECT * FROM %s WHERE tmember=%d;';
201 $query = sprintf($query, sql_table('team'), $member->getID());
202 $teams = DB::getResult($query);
203 if ( $teams->rowCount() == 0 && !$member->isAdmin() )
205 $this->error(_ERROR_DISALLOWEDUPLOAD);
209 /* avoid directory travarsal and accessing invalid directory */
210 $this->collection = requestVar('collection');
211 $this->description = $this->collection;
212 if ( !$this->collection || $this->collection == $member->getID()
213 || !@is_dir("{$DIR_MEDIA}{$this->collection}") )
215 $this->collection = $member->getID();
216 $this->description = PRIVATE_COLLECTION;
218 else if ( !Media::isValidCollection($this->collection) )
220 $this->error(_ERROR_DISALLOWED);
225 if ( !in_array($type, array('select', 'choose', 'upload')) )
231 $needless_to_check = array('select', 'choose');
232 if ( !in_array($type, $needless_to_check) )
234 if ( !$manager->checkTicket() )
236 $this->error(_ERROR_BADTICKET);
245 if ( !$member->isAdmin() && !$CONF['AllowUpload'] )
247 $this->error(_ERROR_DISALLOWED);
253 if ( !$member->isAdmin() && !$CONF['AllowUpload'] )
255 $this->error(_ERROR_DISALLOWED);
268 private function select()
272 $this->type = 'select';
273 $this->filter = requestVar('filter');
275 $media = Media::getMediaListByCollection($this->collection, $this->filter);
277 $this->amount = count($media);
278 $this->offset = intRequestVar('offset');
280 if ( $this->amount > 0 )
282 if ( ($this->offset + $CONF['MediaPerPage']) >= $this->amount )
284 $this->offset = $this->amount - $CONF['MediaPerPage'];
287 if ( $this->offset < 0 )
292 $start = $this->offset;
293 $end = $this->offset + $CONF['MediaPerPage'];
295 $prev = $start - $CONF['MediaPerPage'];
302 if ( $end > $this->amount )
304 $end = $this->amount;
312 if ( $end < $this->amount )
317 for( $index = $start; $index < $end; $index++ )
319 $this->media[] = $media[$index];
325 $this->skin->parse('fileparse', $this->getDirectory() . 'skins/select.skn');
329 private function choose()
331 $this->type = 'choose';
332 $this->skin->parse('fileparse', $this->getDirectory() . 'skins/choose.skn');
335 private function upload()
339 $this->type = 'upload';
341 $uploadInfo = postFileInfo('uploadfile');
343 $filename = $uploadInfo['name'];
344 $filetype = $uploadInfo['type'];
345 $filesize = $uploadInfo['size'];
346 $filetempname = $uploadInfo['tmp_name'];
347 $fileerror = (integer) $uploadInfo['error'];
349 switch ( $fileerror )
351 // include error code for debugging
352 // (see http://www.php.net/manual/en/features.file-upload.errors.php)
353 case 0: // = UPLOAD_ERR_OK
355 case 1: // = UPLOAD_ERR_INI_SIZE
356 case 2: // = UPLOAD_ERR_FORM_SIZE
357 $this->error(_ERROR_FILE_TOO_BIG);
359 case 3: // = UPLOAD_ERR_PARTIAL
360 case 4: // = UPLOAD_ERR_NO_FILE
361 case 6: // = UPLOAD_ERR_NO_TMP_DIR
362 case 7: // = UPLOAD_ERR_CANT_WRITE
364 $this->error(_ERROR_BADREQUEST . ' (' . $fileerror . ')');
368 if ( $filesize > $CONF['MaxUploadSize'] )
370 $this->error(_ERROR_FILE_TOO_BIG);
374 // check file type against allowed types
376 $allowedtypes = preg_split('#,#', $CONF['AllowedTypes']);
377 foreach ( $allowedtypes as $type )
379 if ( preg_match("#.{$type}$#i", $filename) )
386 $this->error(_ERROR_BADFILETYPE);
390 if ( !is_uploaded_file($filetempname) )
392 $this->error(_ERROR_BADREQUEST);
395 // prefix filename with current date (YYYY-MM-DD-)
396 // this to avoid nameclashes
397 if ( $CONF['MediaPrefix'] )
399 $filename = i18n::formatted_datetime("%Y%m%d-", time()) . $filename;
402 $res = Media::addMediaObject($this->collection, $filetempname, $filename);
414 private function login()
416 $this->type = 'login';
417 $this->skin->parse('fileparse', $this->getDirectory() . 'skins/login.skn');
421 private function error($msg)
423 $this->type = 'error';
424 $this->message = $msg;
425 $this->skin->parse('fileparse', $this->getDirectory() . 'skins/error.skn');