| 1 |
<?php
|
| 2 |
// $Id: photos_swfu.module,v 1.6 2008/10/08 16:22:19 eastcn Exp $
|
| 3 |
// More features, please try the patch: http://drupal.org/node/324497#comment-1102758
|
| 4 |
function photos_swfu_menu() {
|
| 5 |
$items = array();
|
| 6 |
$items['photos_swfu/upload'] = array(
|
| 7 |
'page callback' => 'photos_swfu_upload',
|
| 8 |
'access callback' => TRUE,
|
| 9 |
'type' => MENU_CALLBACK,
|
| 10 |
);
|
| 11 |
$items['photos_swfu/upload_node/%node'] = array(
|
| 12 |
'page callback' => 'photos_swfu_node',
|
| 13 |
'page arguments' => array(2),
|
| 14 |
'access callback' => TRUE,
|
| 15 |
'type' => MENU_CALLBACK,
|
| 16 |
);
|
| 17 |
return $items;
|
| 18 |
}
|
| 19 |
|
| 20 |
function photos_swfu_theme() {
|
| 21 |
return array(
|
| 22 |
'photos_swfu_style' => array(
|
| 23 |
'template' => 'photos_swfu_style',
|
| 24 |
'arguments' => array('v' => NULL, 't' => NULL),
|
| 25 |
),
|
| 26 |
);
|
| 27 |
}
|
| 28 |
|
| 29 |
function photos_swfu_form_alter(&$form, $form_state, $form_id) {
|
| 30 |
if ($form_id == "photos_upload_form") {
|
| 31 |
if($_SESSION['photos_swfu_switch'] != 1){
|
| 32 |
if(variable_get('photos_num', 0)){
|
| 33 |
$a = variable_get('photos_num',5);
|
| 34 |
}else{
|
| 35 |
$a = 5;
|
| 36 |
}
|
| 37 |
unset($form['date']);
|
| 38 |
unset($form['new']['submit']);
|
| 39 |
for($i =0;$i < $a; ++$i){
|
| 40 |
unset($form['new']['images_'.$i]);
|
| 41 |
unset($form['new']['des_'.$i]);
|
| 42 |
}
|
| 43 |
}
|
| 44 |
$form['new'] += photos_swfu_form();
|
| 45 |
}
|
| 46 |
}
|
| 47 |
|
| 48 |
function photos_swfu_form(){
|
| 49 |
global $user;
|
| 50 |
drupal_add_css(drupal_get_path('module', 'photos_swfu') .'/simpledemo/default.css');
|
| 51 |
drupal_add_js(drupal_get_path('module', 'photos_swfu') .'/simpledemo/swfupload.js');
|
| 52 |
drupal_add_js(drupal_get_path('module', 'photos_swfu') .'/simpledemo/swfupload.queue.js');
|
| 53 |
drupal_add_js(drupal_get_path('module', 'photos_swfu') .'/simpledemo/fileprogress.js');
|
| 54 |
drupal_add_js(drupal_get_path('module', 'photos_swfu') .'/simpledemo/handlers.js');
|
| 55 |
$op = array();
|
| 56 |
$op['query']['uid'] = session_id();
|
| 57 |
if(is_numeric($_GET['pid'])){
|
| 58 |
$op['query']['pid'] = $_GET['pid'];
|
| 59 |
}
|
| 60 |
if(arg(0) == 'node' && is_numeric(arg(1))){
|
| 61 |
$v['url'] = url('photos_swfu/upload_node/'.arg(1), $op);
|
| 62 |
}else{
|
| 63 |
$v['url'] = url('photos_swfu/upload', $op);
|
| 64 |
}
|
| 65 |
$v['swf'] = base_path() . drupal_get_path('module', 'photos_swfu') .'/simpledemo/swfupload.swf';
|
| 66 |
$v['image'] = base_path() . drupal_get_path('module', 'photos_swfu') .'/simpledemo/XPButtonUploadText_61x22.png';
|
| 67 |
if(arg(1) == 'quote'){
|
| 68 |
$v['href']= url($_GET['q'], array('query' => array('type' => 'upload')));
|
| 69 |
}else{
|
| 70 |
$v['href'] = url($_GET['q'], array('query' => array('type' => 'tx')));
|
| 71 |
}
|
| 72 |
if ($user->uid == 1) {
|
| 73 |
$v['max_file_size'] = '100';
|
| 74 |
$v['num_uploads'] = '0';
|
| 75 |
}else {
|
| 76 |
$limits = _upload_file_limits($user);
|
| 77 |
$v['max_file_size'] = $limits['file_size'] / 1024 / 1024;
|
| 78 |
$v['num_uploads'] = variable_get('photos_swfu_num', 50);
|
| 79 |
}
|
| 80 |
if (variable_get('photos_upzip', 0)) {
|
| 81 |
$v['zip'] = ';*.zip';
|
| 82 |
}
|
| 83 |
if($_SESSION['photos_swfu_switch'] != 1){
|
| 84 |
$form['new']['swfu'] = array(
|
| 85 |
'#value' => theme('photos_swfu_style', $v, $t),
|
| 86 |
'#weight' => -1
|
| 87 |
);
|
| 88 |
$svluae = t('Click here to switch to the classic upload form');
|
| 89 |
}else{
|
| 90 |
$svluae = t('Click here to switch back to the flash upload form');
|
| 91 |
}
|
| 92 |
$form['button']['#weight'] = -10;
|
| 93 |
$form['button']['switch'] = array(
|
| 94 |
'#type' => 'submit',
|
| 95 |
'#value' => $svluae,
|
| 96 |
'#submit' => array('_photos_swfu_switch'),
|
| 97 |
'#weight' => -20,
|
| 98 |
);
|
| 99 |
return $form;
|
| 100 |
}
|
| 101 |
|
| 102 |
function _photos_swfu_switch(){
|
| 103 |
if($_SESSION['photos_swfu_switch'] != 1){
|
| 104 |
$_SESSION['photos_swfu_switch'] = 1;
|
| 105 |
$t = t('Model form');
|
| 106 |
}else{
|
| 107 |
$_SESSION['photos_swfu_switch'] = 0;
|
| 108 |
$t = t('Model flash');
|
| 109 |
}
|
| 110 |
drupal_set_message($t);
|
| 111 |
}
|
| 112 |
|
| 113 |
function photos_swfu_upload() {
|
| 114 |
global $user;
|
| 115 |
$session = $_GET['uid'];
|
| 116 |
$uid = db_result(db_query("SELECT uid FROM {sessions} WHERE sid = '%s' AND hostname = '%s'", $session, ip_address()));
|
| 117 |
if ($uid) {
|
| 118 |
if(is_uploaded_file($_FILES['Filedata']['tmp_name']) && !$_FILES['Filedata']['error']){
|
| 119 |
$user = user_load(array('uid' => $uid));
|
| 120 |
$limits = _upload_file_limits($user);
|
| 121 |
$path = _photos_check_path();
|
| 122 |
$files = new stdClass();
|
| 123 |
$ext = end(explode('.',$_FILES['Filedata']['name']));
|
| 124 |
$files->filesize = $_FILES['Filedata']['size'];
|
| 125 |
if(variable_get('photos_rname', 0)){
|
| 126 |
$name = round(rand(15770,967049700)).time().'.'.$ext;
|
| 127 |
}else{
|
| 128 |
$name = $_FILES['Filedata']['name'];
|
| 129 |
}
|
| 130 |
$files->filepath = file_destination(file_create_path($path .'/'.trim(basename($name))),FILE_EXISTS_RENAME);
|
| 131 |
$files->filename = end(explode('/',$files->filepath));
|
| 132 |
if(is_numeric($_GET['pid'])){
|
| 133 |
$node = node_load($_GET['pid']);
|
| 134 |
if(node_access('update', $node)){
|
| 135 |
$files->pid = $node->nid;
|
| 136 |
$files->title = $node->title;
|
| 137 |
}else{
|
| 138 |
return header("HTTP/1.0 403 Internal Server Error");
|
| 139 |
}
|
| 140 |
}else{
|
| 141 |
$node = db_fetch_object(db_query_range("SELECT nid, title FROM {node} WHERE uid = %d AND type = '%s'", $user->uid, 'photos', 0, 1));
|
| 142 |
if($node){
|
| 143 |
$files->pid = $node->nid;
|
| 144 |
$files->title = $node->title;
|
| 145 |
}else{
|
| 146 |
return header("HTTP/1.0 403 Internal Server Error");
|
| 147 |
}
|
| 148 |
}
|
| 149 |
if($ext == 'zip' || $ext == 'ZIP'){
|
| 150 |
if(!variable_get('photos_upzip',0)){
|
| 151 |
return header("HTTP/1.0 502 Internal Server Error");
|
| 152 |
}
|
| 153 |
if (file_move($_FILES['Filedata']['tmp_name'], $files->filepath)) {
|
| 154 |
if(!_photos_unzip($files->filepath, $path, $files)){
|
| 155 |
return header("HTTP/1.0 502 Internal Server Error");
|
| 156 |
}
|
| 157 |
}else{
|
| 158 |
return header("HTTP/1.0 502 Internal Server Error");
|
| 159 |
}
|
| 160 |
}else{
|
| 161 |
$validators = array(
|
| 162 |
'file_validate_is_image' => array(),
|
| 163 |
'file_validate_image_resolution' => array($limits['resolution']),
|
| 164 |
'file_validate_size' => array($limits['file_size'], $limits['user_size'])
|
| 165 |
);
|
| 166 |
$_FILES['files']['name']['album'] = $name;
|
| 167 |
$_FILES['files']['tmp_name']['album'] = $_FILES['Filedata']['tmp_name'];
|
| 168 |
$_FILES['files']['size']['album'] = $_FILES['Filedata']['size'];
|
| 169 |
if ($file = file_save_upload('album', $validators, $path)) {
|
| 170 |
$title = urlencode($_FILES['Filedata']['name']);
|
| 171 |
$file->title = ($title ? urldecode($title):$files->title);
|
| 172 |
$file->pid = $files->pid;
|
| 173 |
$file->nid = $files->nid;
|
| 174 |
if(!_photos_image_date($file, $path)){
|
| 175 |
return header("HTTP/1.0 502 Internal Server Error");
|
| 176 |
}
|
| 177 |
}else {
|
| 178 |
return header("HTTP/1.0 502 Internal Server Error");
|
| 179 |
}
|
| 180 |
}
|
| 181 |
}else{
|
| 182 |
return header("HTTP/1.0 403 Internal Server Error");
|
| 183 |
}
|
| 184 |
}else{
|
| 185 |
return header("HTTP/1.0 403 Internal Server Error");
|
| 186 |
}
|
| 187 |
return true;
|
| 188 |
}
|
| 189 |
|
| 190 |
function photos_swfu_node($node) {
|
| 191 |
global $user;
|
| 192 |
$session = $_GET['uid'];
|
| 193 |
$uid = db_result(db_query("SELECT uid FROM {sessions} WHERE sid = '%s' AND hostname = '%s'", $session, ip_address()));
|
| 194 |
if ($user = user_load(array('uid' => $uid))) {
|
| 195 |
if(node_access('update', $node) && is_uploaded_file($_FILES['Filedata']['tmp_name']) && !$_FILES['Filedata']['error']){
|
| 196 |
$limits = _upload_file_limits($user);
|
| 197 |
$path = _photos_check_path();
|
| 198 |
$files = new stdClass();
|
| 199 |
$ext = end(explode('.',$_FILES['Filedata']['name']));
|
| 200 |
$files->filesize = $_FILES['Filedata']['size'];
|
| 201 |
if(variable_get('photos_rname', 0)){
|
| 202 |
$name = round(rand(15770,967049700)).time().'.'.$ext;
|
| 203 |
}else{
|
| 204 |
$name = $_FILES['Filedata']['name'];
|
| 205 |
}
|
| 206 |
$files->filepath = file_destination(file_create_path($path .'/'.trim(basename($name))),FILE_EXISTS_RENAME);
|
| 207 |
$files->filename = end(explode('/',$files->filepath));
|
| 208 |
if($node->type == 'photos'){
|
| 209 |
$files->pid = $node->nid;
|
| 210 |
$files->title = $node->title;
|
| 211 |
}else{
|
| 212 |
$files->nid = $node->nid;
|
| 213 |
$files->title = $node->title;
|
| 214 |
if(is_numeric($_GET['pid'])){
|
| 215 |
$node = node_load($_GET['pid']);
|
| 216 |
if(node_access('update', $node)){
|
| 217 |
$files->pid = $node->nid;
|
| 218 |
$files->title = $node->title;
|
| 219 |
}else{
|
| 220 |
return header("HTTP/1.0 403 Internal Server Error");
|
| 221 |
}
|
| 222 |
}else{
|
| 223 |
$node = db_fetch_object(db_query_range("SELECT nid, title FROM {node} WHERE uid = %d AND type = '%s'", $user->uid, 'photos', 0, 1));
|
| 224 |
if($node){
|
| 225 |
$files->pid = $node->nid;
|
| 226 |
$files->title = $node->title;
|
| 227 |
}else{
|
| 228 |
return header("HTTP/1.0 403 Internal Server Error");
|
| 229 |
}
|
| 230 |
}
|
| 231 |
}
|
| 232 |
if($ext == 'zip' || $ext == 'ZIP'){
|
| 233 |
if(!variable_get('photos_upzip',0)){
|
| 234 |
return header("HTTP/1.0 502 Internal Server Error");
|
| 235 |
}
|
| 236 |
if (file_move($_FILES['Filedata']['tmp_name'], $files->filepath)) {
|
| 237 |
if(!_photos_unzip($files->filepath, $path, $files)){
|
| 238 |
return header("HTTP/1.0 502 Internal Server Error");
|
| 239 |
}else {
|
| 240 |
return;
|
| 241 |
}
|
| 242 |
}else{
|
| 243 |
return header("HTTP/1.0 502 Internal Server Error");
|
| 244 |
}
|
| 245 |
}else{
|
| 246 |
$validators = array(
|
| 247 |
'file_validate_is_image' => array(),
|
| 248 |
'file_validate_image_resolution' => array($limits['resolution']),
|
| 249 |
'file_validate_size' => array($limits['file_size'], $limits['user_size'])
|
| 250 |
);
|
| 251 |
$_FILES['files']['name']['album'] = $name;
|
| 252 |
$_FILES['files']['tmp_name']['album'] = $_FILES['Filedata']['tmp_name'];
|
| 253 |
$_FILES['files']['size']['album'] = $_FILES['Filedata']['size'];
|
| 254 |
if ($file = file_save_upload('album', $validators, $path)) {
|
| 255 |
$title = urlencode($_FILES['Filedata']['name']);
|
| 256 |
$file->title = ($title ? urldecode($title): $files->title);
|
| 257 |
$file->pid = $files->pid;
|
| 258 |
$file->nid = $files->nid;
|
| 259 |
if(!_photos_image_date($file, $path)){
|
| 260 |
return header("HTTP/1.0 502 Internal Server Error");
|
| 261 |
}
|
| 262 |
}else {
|
| 263 |
return header("HTTP/1.0 502 Internal Server Error");
|
| 264 |
}
|
| 265 |
}
|
| 266 |
}else{
|
| 267 |
return header("HTTP/1.0 403 Internal Server Error");
|
| 268 |
}
|
| 269 |
}else{
|
| 270 |
return header("HTTP/1.0 403 Internal Server Error");
|
| 271 |
}
|
| 272 |
}
|
| 273 |
|
| 274 |
//transliteration_init
|
| 275 |
function photos_swfu_init() {
|
| 276 |
if (module_exists('transliteration') && !empty($_FILES['Filedata'])) {
|
| 277 |
require_once(drupal_get_path('module', 'transliteration') .'/transliteration.inc');
|
| 278 |
$langcode = NULL;
|
| 279 |
if (!empty($_POST['language'])) {
|
| 280 |
$languages = language_list();
|
| 281 |
$langcode = isset($languages[$_POST['language']]) ? $_POST['language'] : NULL;
|
| 282 |
}
|
| 283 |
$_FILES['Filedata']['name'] = transliteration_clean_filename($_FILES['Filedata']['name'], $langcode);
|
| 284 |
}
|
| 285 |
}
|