| 1 |
<?php
|
| 2 |
// $Id: asset.types.inc,v 1.3 2008/01/22 02:13:18 rz Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* This file is included by asset_asset_type() and includes all the file and
|
| 7 |
* directory specific functions
|
| 8 |
*/
|
| 9 |
|
| 10 |
/**
|
| 11 |
* @addtogroup asset
|
| 12 |
* @{
|
| 13 |
*/
|
| 14 |
|
| 15 |
/**
|
| 16 |
* Implementation of hook_asset_type('load') for file asset type
|
| 17 |
*/
|
| 18 |
function asset_file_load($asset){
|
| 19 |
$files = array();
|
| 20 |
$result = db_query('SELECT f.*, a.label FROM {files} f INNER JOIN {asset_files} a ON f.fid=a.fid WHERE a.aid=%d', $asset->aid);
|
| 21 |
while($row = db_fetch_array($result)){
|
| 22 |
if($row['label'] == 'default'){
|
| 23 |
$default = $row;
|
| 24 |
}
|
| 25 |
$files[$row['label']] = $row;
|
| 26 |
}
|
| 27 |
return array('file' => $default, 'files' => $files);
|
| 28 |
}
|
| 29 |
|
| 30 |
/**
|
| 31 |
* Implementation of hook_asset_type('view') for file asset type
|
| 32 |
*/
|
| 33 |
function asset_file_view($asset){
|
| 34 |
if(strpos($asset->file['filemime'], 'image/') === 0){
|
| 35 |
return theme('image', file_create_path($asset->file['filepath']), $asset->title, $asset->title);
|
| 36 |
}
|
| 37 |
else{
|
| 38 |
return l($asset->file['filename'], file_create_url($asset->file['filepath']));
|
| 39 |
}
|
| 40 |
}
|
| 41 |
|
| 42 |
/**
|
| 43 |
* Implementation of hook_asset_type('form') for file asset type
|
| 44 |
*/
|
| 45 |
function asset_file_form($asset){
|
| 46 |
if($asset->file['fid']){
|
| 47 |
$form['_upload'] = array(
|
| 48 |
'#type' => 'item',
|
| 49 |
'#value' => $asset->file['filename'],
|
| 50 |
'#title' => t('Current file'),
|
| 51 |
);
|
| 52 |
$overwrite = t('This will replace any files that have been uploaded previously.');
|
| 53 |
}
|
| 54 |
$form['upload'] = array(
|
| 55 |
'#type' => 'file',
|
| 56 |
'#title' => t('Attach new file'),
|
| 57 |
'#description' => $overwrite ? $overwrite : NULL,
|
| 58 |
'#size' => 25,
|
| 59 |
);
|
| 60 |
|
| 61 |
if($options = _asset_file_localfile_options()){
|
| 62 |
$form['localfile'] = array(
|
| 63 |
'#type' => 'select',
|
| 64 |
'#title' => t('Or select a file'),
|
| 65 |
'#options' => $options,
|
| 66 |
);
|
| 67 |
}
|
| 68 |
|
| 69 |
$form['#attributes']['enctype'] = 'multipart/form-data';
|
| 70 |
return $form;
|
| 71 |
}
|
| 72 |
|
| 73 |
function _asset_file_localfile_options($ext = FALSE){
|
| 74 |
if($dir = variable_get('asset_ftp_dir', '')){
|
| 75 |
$options = array('#' => t('-- Select a file --'));
|
| 76 |
$regex = $ext ? '.*\.'. $ext : '.*';
|
| 77 |
foreach(file_scan_directory($dir, $regex) as $file){
|
| 78 |
$path = str_replace($dir, '', $file->filename);
|
| 79 |
$options[$path] = $path;
|
| 80 |
}
|
| 81 |
asort($options);
|
| 82 |
}
|
| 83 |
else{
|
| 84 |
$options = FALSE;
|
| 85 |
}
|
| 86 |
|
| 87 |
return $options;
|
| 88 |
}
|
| 89 |
|
| 90 |
/**
|
| 91 |
* Implementation of hook_asset_type('validate') for file asset type
|
| 92 |
*/
|
| 93 |
function asset_file_validate($asset){
|
| 94 |
if(!$asset->aid && !file_check_upload('upload')){
|
| 95 |
$ftp = variable_get('asset_ftp_dir', '');
|
| 96 |
// if ftp uploads are allowed and no localfile was selected
|
| 97 |
if($ftp && $asset->localfile == '#'){
|
| 98 |
form_set_error('upload', t('You must upload a file or select one from the list.'));
|
| 99 |
}
|
| 100 |
// no ftp and no upload
|
| 101 |
elseif(!$ftp){
|
| 102 |
form_set_error('upload', t('You must upload a file.'));
|
| 103 |
}
|
| 104 |
}
|
| 105 |
}
|
| 106 |
|
| 107 |
/**
|
| 108 |
* Implementation of hook_asset_type('insert') for file asset type
|
| 109 |
*/
|
| 110 |
function asset_file_insert(&$asset){
|
| 111 |
$file = file_check_upload('upload');
|
| 112 |
$asset_path = variable_get('asset_file_directory_path', '');
|
| 113 |
$dest = $asset_path ? $asset_path .'/'. $file->filename : $file->filename;
|
| 114 |
if ($file = file_save_upload($file, $dest)) {
|
| 115 |
$file->fid = db_next_id('{files}_fid');
|
| 116 |
db_query("INSERT INTO {files} (fid, nid, filename, filepath, filemime, filesize) VALUES (%d, %d, '%s', '%s', '%s', %d)", $file->fid, 0, $file->filename, $file->filepath, $file->filemime, $file->filesize);
|
| 117 |
db_query("INSERT INTO {asset_files} (aid, fid, label) VALUES (%d, %d, '%s')", $asset->aid, $file->fid, 'default');
|
| 118 |
}
|
| 119 |
elseif($asset->localfile){
|
| 120 |
$path = variable_get('asset_ftp_dir', '') . $asset->localfile;
|
| 121 |
$asset_path = variable_get('asset_file_directory_path', '');
|
| 122 |
$dest = $asset_path ? $asset_path .'/'. basename($path) : basename($path);
|
| 123 |
|
| 124 |
// prepare the file object. based on file_check_upload
|
| 125 |
// Begin building file object.
|
| 126 |
$file = new stdClass();
|
| 127 |
$file->filename = trim(basename($path), '.');
|
| 128 |
$file->filepath = $path;
|
| 129 |
$file->filemime = mime_content_type($path);
|
| 130 |
$file->filesize = filesize($path);
|
| 131 |
|
| 132 |
// Rename potentially executable files, to help prevent exploits.
|
| 133 |
if (preg_match('/\.(php|pl|py|cgi|asp|js)$/i', $file->filename) && (substr($file->filename, -4) != '.txt')) {
|
| 134 |
$file->filemime = 'text/plain';
|
| 135 |
$file->filepath .= '.txt';
|
| 136 |
$file->filename .= '.txt';
|
| 137 |
}
|
| 138 |
|
| 139 |
if(file_move($file, $dest)){
|
| 140 |
$file->fid = db_next_id('{files}_fid');
|
| 141 |
db_query("INSERT INTO {files} (fid, nid, filename, filepath, filemime, filesize) VALUES (%d, %d, '%s', '%s', '%s', %d)", $file->fid, 0, $file->filename, $file->filepath, $file->filemime, $file->filesize);
|
| 142 |
db_query("INSERT INTO {asset_files} (aid, fid, label) VALUES (%d, %d, '%s')", $asset->aid, $file->fid, 'default');
|
| 143 |
}
|
| 144 |
}
|
| 145 |
$asset->file = db_fetch_array(db_query('SELECT f.* FROM {files} f INNER JOIN {asset_files} a ON f.fid=a.fid WHERE a.aid=%d', $asset->aid));
|
| 146 |
}
|
| 147 |
|
| 148 |
function _asset_file_insert_localfile($localfile){
|
| 149 |
$path = variable_get('asset_ftp_dir', '') . $localfile;
|
| 150 |
$asset_path = variable_get('asset_file_directory_path', '');
|
| 151 |
$dest = $asset_path ? $asset_path .'/'. basename($path) : basename($path);
|
| 152 |
|
| 153 |
// prepare the file object. based on file_check_upload
|
| 154 |
// Begin building file object.
|
| 155 |
$file = new stdClass();
|
| 156 |
$file->filename = trim(basename($path), '.');
|
| 157 |
$file->filepath = $path;
|
| 158 |
$file->filemime = mime_content_type($path);
|
| 159 |
$file->filesize = filesize($path);
|
| 160 |
|
| 161 |
// Rename potentially executable files, to help prevent exploits.
|
| 162 |
if (preg_match('/\.(php|pl|py|cgi|asp|js)$/i', $file->filename) && (substr($file->filename, -4) != '.txt')) {
|
| 163 |
$file->filemime = 'text/plain';
|
| 164 |
$file->filepath .= '.txt';
|
| 165 |
$file->filename .= '.txt';
|
| 166 |
}
|
| 167 |
|
| 168 |
if(file_move($file, $dest)){
|
| 169 |
$file->fid = db_next_id('{files}_fid');
|
| 170 |
db_query("INSERT INTO {files} (fid, nid, filename, filepath, filemime, filesize) VALUES (%d, %d, '%s', '%s', '%s', %d)", $file->fid, 0, $file->filename, $file->filepath, $file->filemime, $file->filesize);
|
| 171 |
db_query("INSERT INTO {asset_files} (aid, fid) VALUES (%d, %d)", $asset->aid, $file->fid);
|
| 172 |
return $file;
|
| 173 |
}
|
| 174 |
else{
|
| 175 |
return false;
|
| 176 |
}
|
| 177 |
}
|
| 178 |
|
| 179 |
/**
|
| 180 |
* Implementation of hook_asset_type('update') for file asset type
|
| 181 |
*/
|
| 182 |
function asset_file_update(&$asset){
|
| 183 |
$file = file_check_upload('upload');
|
| 184 |
$asset_path = variable_get('asset_file_directory_path', '');
|
| 185 |
$dest = $asset_path ? $asset_path .'/'. $file->filename : $file->filename;
|
| 186 |
if ($file = file_save_upload($file, $dest)) {
|
| 187 |
$file->fid = db_next_id('{files}_fid');
|
| 188 |
// delete the old file and remove db entry
|
| 189 |
file_delete($asset->file['filepath']);
|
| 190 |
db_query("DELETE FROM {files} WHERE fid=%d", $asset->file['fid']);
|
| 191 |
// add the new file and update the relationship
|
| 192 |
db_query("INSERT INTO {files} (fid, nid, filename, filepath, filemime, filesize) VALUES (%d, %d, '%s', '%s', '%s', %d)", $file->fid, 0, $file->filename, $file->filepath, $file->filemime, $file->filesize);
|
| 193 |
db_query("UPDATE {asset_files} SET fid=%d WHERE aid=%d", $file->fid, $asset->aid);
|
| 194 |
}
|
| 195 |
}
|
| 196 |
|
| 197 |
/**
|
| 198 |
* Implementation of hook_asset_type('delete') for file asset type
|
| 199 |
*/
|
| 200 |
function asset_file_delete($asset){
|
| 201 |
file_delete($asset->file['filepath']);
|
| 202 |
db_query("DELETE FROM {files} WHERE fid=%d", $asset->file['fid']);
|
| 203 |
db_query("DELETE FROM {asset_files} WHERE aid=%d", $asset->aid);
|
| 204 |
}
|
| 205 |
|
| 206 |
/**
|
| 207 |
* Implementation of hook_asset_type('icon') for file asset type
|
| 208 |
*/
|
| 209 |
function asset_file_icon($asset){
|
| 210 |
if(strpos($asset->file['filemime'], 'image/') === 0){
|
| 211 |
$format = variable_get('asset_file_image_icon', 'default');
|
| 212 |
if($format == 'default'){
|
| 213 |
return file_create_path($asset->file['filepath']);
|
| 214 |
}
|
| 215 |
else{
|
| 216 |
return file_directory_path() .'/imagecache/'. $format .'/'. $asset->file['filepath'];
|
| 217 |
}
|
| 218 |
}
|
| 219 |
$path = pathinfo($asset->file['filepath']);
|
| 220 |
$icon = drupal_get_path('module', 'asset') . '/icons/' . $path['extension'] . '.png';
|
| 221 |
if (file_exists($icon)) {
|
| 222 |
return $icon;
|
| 223 |
}
|
| 224 |
}
|
| 225 |
|
| 226 |
/**
|
| 227 |
* Implementation of hook_asset_type('img') for file asset type
|
| 228 |
*/
|
| 229 |
function asset_file_img($asset){
|
| 230 |
if(strpos($asset->file['filemime'], 'image/') === 0){
|
| 231 |
return file_create_path($asset->file['filepath']);
|
| 232 |
}
|
| 233 |
$path = pathinfo($asset->file['filepath']);
|
| 234 |
$icon = drupal_get_path('module', 'asset') . '/icons/' . $path['extension'] . '.png';
|
| 235 |
if (file_exists($icon)) {
|
| 236 |
return $icon;
|
| 237 |
}
|
| 238 |
}
|
| 239 |
|
| 240 |
/**
|
| 241 |
* Implementation of hook_asset_type('view') for directory asset type
|
| 242 |
*/
|
| 243 |
function asset_directory_view($asset){
|
| 244 |
$items = array();
|
| 245 |
if($asset->aid){
|
| 246 |
$parent = asset_load($asset->pid);
|
| 247 |
$parent->link_title = '..';
|
| 248 |
$items[] = $parent;
|
| 249 |
}
|
| 250 |
$result = db_query('SELECT a.*, (a.type = "directory") as directory FROM {asset} a WHERE a.pid=%d ORDER BY directory DESC, a.title', $asset->aid);
|
| 251 |
while($row = db_fetch_object($result)){
|
| 252 |
$a = asset_load($row->aid);
|
| 253 |
$items[] = $a;
|
| 254 |
}
|
| 255 |
return theme('asset_directory_browse', $items);
|
| 256 |
}
|
| 257 |
|
| 258 |
function theme_asset_directory_browse($items = array()){
|
| 259 |
$size = 64;
|
| 260 |
$links = array();
|
| 261 |
foreach($items as $asset){
|
| 262 |
$icon = theme('asset_icon', $asset, $size);
|
| 263 |
$links[] = tbl($icon, 'asset/'. $asset->aid, array(), NULL, NULL, FALSE, TRUE);
|
| 264 |
}
|
| 265 |
$output .= '<ul class="asset-directory-browse clear-block">';
|
| 266 |
foreach($links as $link){
|
| 267 |
$output .= '<li>'. $link .'</li>';
|
| 268 |
}
|
| 269 |
$output .= '</ul>';
|
| 270 |
return $output;
|
| 271 |
}
|
| 272 |
|
| 273 |
/**
|
| 274 |
* Implementation of hook_asset_type('icon') for directory asset type
|
| 275 |
*/
|
| 276 |
function asset_directory_icon($asset){
|
| 277 |
$icon = drupal_get_path('module', 'asset') . '/icons/folder.png';
|
| 278 |
if (file_exists($icon)) {
|
| 279 |
return $icon;
|
| 280 |
}
|
| 281 |
}
|
| 282 |
|
| 283 |
if(!function_exists('mime_content_type ')) {
|
| 284 |
function mime_content_type($filename) {
|
| 285 |
$ext = strtolower(array_pop(explode('.',$filename)));
|
| 286 |
$types = array(
|
| 287 |
'gif' => 'image/gif',
|
| 288 |
'png' => 'image/png',
|
| 289 |
'jpg' => 'image/jpeg',
|
| 290 |
'jpeg' => 'image/jpeg',
|
| 291 |
'mpeg' => 'video/mpeg',
|
| 292 |
'mov' => 'video/quicktime',
|
| 293 |
'avi' => 'video/x-msvideo',
|
| 294 |
'flv' => 'video/x-flv',
|
| 295 |
);
|
| 296 |
|
| 297 |
$mimetype = $types[$ext] ? $types[$ext] : 'text/plain';
|
| 298 |
return $mimetype;
|
| 299 |
}
|
| 300 |
}
|
| 301 |
|
| 302 |
/**
|
| 303 |
* @} End of "addtogroup asset".
|
| 304 |
*/
|