| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
/**
|
| 5 |
* Standard content page.
|
| 6 |
*/
|
| 7 |
function mmedia_content_page() {
|
| 8 |
$result = pager_query(db_rewrite_sql('SELECT m.mid, m.title, m.summary FROM {media} m WHERE m.public <> 0 ORDER BY m.created DESC'), variable_get('mmedia_default_main_page', 10));
|
| 9 |
|
| 10 |
$output = '';
|
| 11 |
$num_rows = FALSE;
|
| 12 |
while ($media = db_fetch_object($result)) {
|
| 13 |
$media = media_load($media->mid);
|
| 14 |
$filename = media_filename($media);
|
| 15 |
$profile = variable_get('mmedia_view_profile', null);
|
| 16 |
$output .= theme('media_view', mapi_display($filename, array('options' => $profile)), $filename, $media);
|
| 17 |
$num_rows = TRUE;
|
| 18 |
}
|
| 19 |
|
| 20 |
if ($num_rows) {
|
| 21 |
$feed_url = url(MEDIA_PATH . '/rss.xml', array('absolute' => TRUE));
|
| 22 |
drupal_add_feed($feed_url, variable_get('site_name', 'Drupal') .' '. t('RSS'));
|
| 23 |
$output .= theme('pager', NULL, variable_get('mmedia_default_main_page', 10));
|
| 24 |
}
|
| 25 |
else {
|
| 26 |
$default_message = t('<h1 class="title">Welcome to the Media display page!</h1><p>Please follow these steps to set up to start uploading and viewing media:</p>');
|
| 27 |
$default_message .= '<ol>';
|
| 28 |
|
| 29 |
$default_message .= '<li>'. t('<strong>Configure <a href="@presenters">presenters</a> and <a href="@profiles">profiles</a> to achieve consistant presentation of media across your website.', array('@presenters' => url('admin/mapi/presenters'), '@profiles' => url('admin/mapi/profiles'))) .'</li>';
|
| 30 |
$default_message .= '</ol>';
|
| 31 |
|
| 32 |
$output = '<div id="first-time">'. $default_message .'</div>';
|
| 33 |
}
|
| 34 |
drupal_set_title('');
|
| 35 |
|
| 36 |
return $output;
|
| 37 |
}
|
| 38 |
|
| 39 |
/**
|
| 40 |
* Generic Media Feed
|
| 41 |
*/
|
| 42 |
function mmedia_feed($mids = FALSE, $channel = array()) {
|
| 43 |
global $base_url, $language;
|
| 44 |
|
| 45 |
if ($mids === FALSE) {
|
| 46 |
$mids = array();
|
| 47 |
$result = db_query_range(db_rewrite_sql('SELECT m.mid FROM {media} m WHERE m.public <> 0 ORDER BY m.created DESC'), 0, variable_get('feed_default_items', 10));
|
| 48 |
while ($row = db_fetch_object($result)) {
|
| 49 |
$mids[] = $row->mid;
|
| 50 |
}
|
| 51 |
}
|
| 52 |
|
| 53 |
$item_length = variable_get('feed_item_length', 'teaser');
|
| 54 |
$namespaces = array('xmlns:dc' => 'http://purl.org/dc/elements/1.1/');
|
| 55 |
|
| 56 |
$items = '';
|
| 57 |
foreach ($mids as $mid) {
|
| 58 |
// Load the specified node:
|
| 59 |
$item = media_load($mid);
|
| 60 |
$item->link = url(MEDIA_PATH ."/$mid", array('absolute' => TRUE));
|
| 61 |
|
| 62 |
// Allow modules to add additional item fields and/or modify $item
|
| 63 |
$extra = media_invoke_item('rss item', $item);
|
| 64 |
$extra = array_merge($extra, array(array('key' => 'pubDate', 'value' => format_date($item->created, 'custom', 'r')), array('key' => 'dc:creator', 'value' => $item->name), array('key' => 'guid', 'value' => $item->nid .' at '. $base_url, 'attributes' => array('isPermaLink' => 'false'))));
|
| 65 |
foreach ($extra as $element) {
|
| 66 |
if (isset($element['namespace'])) {
|
| 67 |
$namespaces = array_merge($namespaces, $element['namespace']);
|
| 68 |
}
|
| 69 |
}
|
| 70 |
|
| 71 |
// Prepare the item description
|
| 72 |
switch ($item_length) {
|
| 73 |
case 'fulltext':
|
| 74 |
$item_text = $item->metadata['description'];
|
| 75 |
break;
|
| 76 |
case 'teaser':
|
| 77 |
$item_text = $item->summary;
|
| 78 |
if (!empty($item->readmore)) {
|
| 79 |
$item_text .= '<p>'. l(t('read more'), MEDIA_PATH .'/'. $item->mid, array('absolute' => TRUE, 'attributes' => array('target' => '_blank'))) .'</p>';
|
| 80 |
}
|
| 81 |
break;
|
| 82 |
case 'title':
|
| 83 |
$item_text = '';
|
| 84 |
break;
|
| 85 |
}
|
| 86 |
|
| 87 |
// TODO: Allow different derivatives to be added to the RSS item.
|
| 88 |
// add the original media file, as the primary enclosure for a RSS item.
|
| 89 |
$filename = media_filename($item);
|
| 90 |
if (mapi_file_exists($filename)) {
|
| 91 |
$extra = array_merge($extra, array(array('key' => 'enclosure', 'value' => null, 'attributes' => array(
|
| 92 |
'url' => url($filename, array('absolute' => true)),
|
| 93 |
'length' => filesize($filename),
|
| 94 |
'type' => $item->type,
|
| 95 |
))));
|
| 96 |
}
|
| 97 |
|
| 98 |
|
| 99 |
$items .= format_rss_item($item->title, $item->link, $item_text, $extra);
|
| 100 |
}
|
| 101 |
|
| 102 |
$channel_defaults = array(
|
| 103 |
'version' => '2.0',
|
| 104 |
'title' => variable_get('site_name', 'Drupal'),
|
| 105 |
'link' => $base_url,
|
| 106 |
'description' => variable_get('site_mission', ''),
|
| 107 |
'language' => $language->language
|
| 108 |
);
|
| 109 |
$channel = array_merge($channel_defaults, $channel);
|
| 110 |
|
| 111 |
$output = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
|
| 112 |
$output .= "<rss version=\"". $channel["version"] ."\" xml:base=\"". $base_url ."\" ". drupal_attributes($namespaces) .">\n";
|
| 113 |
$output .= format_rss_channel($channel['title'], $channel['link'], $channel['description'], $items, $channel['language']);
|
| 114 |
$output .= "</rss>\n";
|
| 115 |
|
| 116 |
drupal_set_header('Content-Type: application/rss+xml; charset=utf-8');
|
| 117 |
print $output;
|
| 118 |
}
|
| 119 |
|
| 120 |
/**
|
| 121 |
* Handle media autocomplete.
|
| 122 |
*/
|
| 123 |
function mmedia_autocomplete($string) {
|
| 124 |
if ($string != '') {
|
| 125 |
if (is_numeric($string)) {
|
| 126 |
$result = db_query_range('SELECT mid, name FROM {media} WHERE mid LIKE ("%d%%")', (int)$string, 0, 10);
|
| 127 |
}
|
| 128 |
else {
|
| 129 |
$result = db_query_range('SELECT mid, name FROM {media} WHERE LOWER(name) LIKE LOWER("%%%s%%")', drupal_strtolower($string), 0, 10);
|
| 130 |
}
|
| 131 |
|
| 132 |
$matches = array();
|
| 133 |
while ($object = db_fetch_object($result)) {
|
| 134 |
$t = $object->name;
|
| 135 |
// Commas and quotes in terms are special cases, so encode 'em.
|
| 136 |
if (preg_match('/,/', $t) || preg_match('/"/', $t)) {
|
| 137 |
$t = '"'. preg_replace('/"/', '""', $t) .'"';
|
| 138 |
}
|
| 139 |
$matches[$object->mid] = '<span class="autocomplete_title">'. check_plain($t) .'</span>';
|
| 140 |
}
|
| 141 |
print drupal_to_js($matches);
|
| 142 |
exit();
|
| 143 |
}
|
| 144 |
}
|
| 145 |
|
| 146 |
/**
|
| 147 |
* Transfer the media file, including derivative.
|
| 148 |
*/
|
| 149 |
function mmedia_transfer($media, $derivative = '') {
|
| 150 |
// get actual filename
|
| 151 |
$filename = media_filename($media);
|
| 152 |
|
| 153 |
// handle the derivative of a media if installed
|
| 154 |
if (module_exists('mapi_derivatives') && in_array($derivative, mapi_derivative_list())) {
|
| 155 |
$filename = mapi_derive($filename, $derivative);
|
| 156 |
}
|
| 157 |
|
| 158 |
ob_end_clean();
|
| 159 |
|
| 160 |
// handle local file
|
| 161 |
if (is_file($filename)) {
|
| 162 |
if ($fd = fopen($filename, 'rb')) {
|
| 163 |
$ext = mapi_file_ext($filename);
|
| 164 |
|
| 165 |
// set the appropriate headers
|
| 166 |
$headers = array(
|
| 167 |
'Content-Type: '. ($ext ? mapi_extension_mime($ext) : 'application/octet-stream'),
|
| 168 |
'Content-Length: '. filesize($filename),
|
| 169 |
'Content-Disposition: attachment; filename='. mapi_file_name($filename) .'.'. $ext,
|
| 170 |
);
|
| 171 |
foreach ($headers as $header) {
|
| 172 |
drupal_set_header($header);
|
| 173 |
}
|
| 174 |
|
| 175 |
// transfer the file in 1024 byte blocks to save on memory usage
|
| 176 |
while (!feof($fd)) {
|
| 177 |
print fread($fd, 1024);
|
| 178 |
}
|
| 179 |
fclose($fd);
|
| 180 |
}
|
| 181 |
// if not openable on this system.
|
| 182 |
else {
|
| 183 |
drupal_not_found();
|
| 184 |
}
|
| 185 |
}
|
| 186 |
// handle external file
|
| 187 |
else {
|
| 188 |
// Temporary redirect since we want people to still come to this site for the details,
|
| 189 |
// but allow the other site to host the media.
|
| 190 |
header("HTTP/1.0 307 Temporary redirect");
|
| 191 |
header("Location: ". $filename );
|
| 192 |
}
|
| 193 |
exit();
|
| 194 |
}
|
| 195 |
|
| 196 |
/**
|
| 197 |
* Folder autocomplete
|
| 198 |
*/
|
| 199 |
function mmedia_folder_autocomplete($string) {
|
| 200 |
if ($string != '') {
|
| 201 |
if (is_numeric($string)) {
|
| 202 |
$result = db_query_range('SELECT fid, name, path FROM {media_folders} WHERE mid LIKE ("%d%%")', (int)$string, 0, 10);
|
| 203 |
}
|
| 204 |
else {
|
| 205 |
$result = db_query_range('SELECT fid, name, path FROM {media_folders} WHERE LOWER(name) LIKE LOWER("%%%s%%")', drupal_strtolower($string), 0, 10);
|
| 206 |
}
|
| 207 |
|
| 208 |
$matches = array();
|
| 209 |
while ($folder = db_fetch_object($result)) {
|
| 210 |
$t = $folder->name;
|
| 211 |
// Commas and quotes in terms are special cases, so encode 'em.
|
| 212 |
if (preg_match('/,/', $folder->name) || preg_match('/"/', $folder->name)) {
|
| 213 |
$t = '"'. preg_replace('/"/', '""', $folder->name) .'"';
|
| 214 |
}
|
| 215 |
$matches[$folder->fid] = '<span class="autocomplete_title">'. check_plain($t) .'</span> <span class="autocomplete_path">['. FOLDER_PATH .'/'. $folder->path .']</span>';
|
| 216 |
}
|
| 217 |
print drupal_to_js($matches);
|
| 218 |
exit();
|
| 219 |
}
|
| 220 |
}
|
| 221 |
|