| [ Index ] | [ Index ] [ Classes ] [ Functions ] [ Variables ] [ Constants ] |
PHP Cross Reference of TXP stable 4.0.6 |
||
[Summary view] [Print] [Text view]
1 <?php 2 /* 3 _______________________________________ 4 ________| Textpattern |________ 5 \ | Mod File Upload | / 6 \ | Michael Manfre (http://manfre.net) | / 7 / |_______________________________________| \ 8 /___________) (___________\ 9 10 Textpattern Copyright 2004 by Dean Allen. All rights reserved. 11 Use of this software denotes acceptance of the Textpattern license agreement 12 13 "Mod File Upload" Copyright 2004 by Michael Manfre. All rights reserved. 14 Use of this mod denotes acceptance of the Textpattern license agreement 15 16 $HeadURL: http://svn.textpattern.com/releases/4.0.6/source/textpattern/include/txp_file.php $ 17 $LastChangedRevision: 2530 $ 18 19 */ 20 21 if (!defined('txpinterface')) die('txpinterface is undefined.'); 22 23 $levels = array( 24 1 => gTxt('private'), 25 0 => gTxt('public') 26 ); 27 28 global $file_statuses; 29 $file_statuses = array( 30 2 => gTxt('hidden'), 31 3 => gTxt('pending'), 32 4 => gTxt('live'), 33 ); 34 35 if ($event == 'file') { 36 require_privs('file'); 37 38 if(!$step or !in_array($step, array('file_change_max_size','file_change_pageby','file_db_add','file_delete','file_edit','file_insert','file_list','file_replace','file_save','file_reset_count','file_create'))){ 39 file_list(); 40 } else $step(); 41 } 42 43 // ------------------------------------------------------------- 44 45 function file_list($message = '') 46 { 47 global $txpcfg, $extensions, $file_base_path, $file_statuses; 48 49 pagetop(gTxt('file'), $message); 50 51 extract($txpcfg); 52 extract(get_prefs()); 53 54 extract(gpsa(array('page', 'sort', 'dir', 'crit', 'search_method'))); 55 56 if (!is_dir($file_base_path) or !is_writeable($file_base_path)) 57 { 58 echo graf( 59 gTxt('file_dir_not_writeable', array('{filedir}' => $file_base_path)) 60 , ' id="warning"'); 61 } 62 63 else 64 { 65 $existing_files = get_filenames(); 66 67 if (count($existing_files) > 0) 68 { 69 echo form( 70 eInput('file'). 71 sInput('file_create'). 72 73 graf(gTxt('existing_file').sp.selectInput('filename', $existing_files, '', 1).sp. 74 fInput('submit', '', gTxt('Create'), 'smallerbox')) 75 76 , 'text-align: center;'); 77 } 78 79 echo file_upload_form(gTxt('upload_file'), 'upload', 'file_insert'); 80 } 81 82 $dir = ($dir == 'desc') ? 'desc' : 'asc'; 83 84 switch ($sort) 85 { 86 case 'id': 87 $sort_sql = 'id '.$dir; 88 break; 89 90 case 'description': 91 $sort_sql = 'description '.$dir.', filename desc'; 92 break; 93 94 case 'category': 95 $sort_sql = 'category '.$dir.', filename desc'; 96 break; 97 98 case 'downloads': 99 $sort_sql = 'downloads '.$dir.', filename desc'; 100 break; 101 102 default: 103 $sort = 'filename'; 104 $sort_sql = 'filename '.$dir; 105 break; 106 } 107 108 $switch_dir = ($dir == 'desc') ? 'asc' : 'desc'; 109 110 $criteria = 1; 111 112 if ($search_method and $crit) 113 { 114 $crit_escaped = doSlash($crit); 115 116 $critsql = array( 117 'id' => "id = '$crit_escaped'", 118 'filename' => "filename like '%$crit_escaped%'", 119 'description' => "description like '%$crit_escaped%'", 120 'category' => "category like '%$crit_escaped%'" 121 ); 122 123 if (array_key_exists($search_method, $critsql)) 124 { 125 $criteria = $critsql[$search_method]; 126 $limit = 500; 127 } 128 129 else 130 { 131 $search_method = ''; 132 $crit = ''; 133 } 134 } 135 136 else 137 { 138 $search_method = ''; 139 $crit = ''; 140 } 141 142 $total = safe_count('txp_file', "$criteria"); 143 144 if ($total < 1) 145 { 146 if ($criteria != 1) 147 { 148 echo n.file_search_form($crit, $search_method). 149 n.graf(gTxt('no_results_found'), ' style="text-align: center;"'); 150 } 151 152 else 153 { 154 echo n.graf(gTxt('no_files_recorded'), ' style="text-align: center;"'); 155 } 156 157 return; 158 } 159 160 $limit = max(@$file_list_pageby, 15); 161 162 list($page, $offset, $numPages) = pager($total, $limit, $page); 163 164 echo file_search_form($crit, $search_method); 165 166 $rs = safe_rows_start('*', 'txp_file', "$criteria order by $sort_sql limit $offset, $limit"); 167 168 if ($rs) 169 { 170 echo startTable('list'). 171 172 tr( 173 column_head('ID', 'id', 'file', true, $switch_dir, $crit, $search_method, ('id' == $sort) ? $dir : ''). 174 hCell(). 175 column_head('file_name', 'filename', 'file', true, $switch_dir, $crit, $search_method, ('filename' == $sort) ? $dir : ''). 176 column_head('description', 'description', 'file', true, $switch_dir, $crit, $search_method, ('description' == $sort) ? $dir : ''). 177 column_head('file_category', 'category', 'file', true, $switch_dir, $crit, $search_method, ('category' == $sort) ? $dir : ''). 178 // column_head('permissions', 'permissions', 'file', true, $switch_dir, $crit, $search_method). 179 hCell(gTxt('tags')). 180 hCell(gTxt('status')). 181 hCell(gTxt('condition')). 182 column_head('downloads', 'downloads', 'file', true, $switch_dir, $crit, $search_method, ('downloads' == $sort) ? $dir : ''). 183 hCell() 184 ); 185 186 while ($a = nextRow($rs)) 187 { 188 extract($a); 189 190 $edit_url = '?event=file'.a.'step=file_edit'.a.'id='.$id.a.'sort='.$sort. 191 a.'dir='.$dir.a.'page='.$page.a.'search_method='.$search_method.a.'crit='.$crit; 192 193 $file_exists = file_exists(build_file_path($file_base_path, $filename)); 194 195 $download_link = ($file_exists) ? '<li>'.make_download_link($id, '', $filename).'</li>' : ''; 196 197 $category = ($category) ? '<span title="'.htmlspecialchars(fetch_category_title($category, 'file')).'">'.$category.'</span>' : ''; 198 199 $tag_url = '?event=tag'.a.'tag_name=file_download_link'.a.'id='.$id.a.'description='.urlencode($description). 200 a.'filename='.urlencode($filename); 201 202 $condition = '<span class="'; 203 $condition .= ($file_exists) ? 'ok' : 'not-ok'; 204 $condition .= '">'; 205 $condition .= ($file_exists) ? gTxt('file_status_ok') : gTxt('file_status_missing'); 206 $condition .= '</span>'; 207 208 echo tr( 209 210 n.td($id). 211 212 td( 213 '<ul>'. 214 '<li>'.href(gTxt('edit'), $edit_url).'</li>'. 215 $download_link. 216 '</ul>' 217 , 65). 218 219 td( 220 href(htmlspecialchars($filename), $edit_url) 221 , 125). 222 223 td(htmlspecialchars($description), 150). 224 td($category, 90). 225 226 /* 227 td( 228 ($permissions == '1') ? gTxt('private') : gTxt('public') 229 ,80). 230 */ 231 232 td( 233 n.'<ul>'. 234 n.t.'<li><a target="_blank" href="'.$tag_url.a.'type=textile" onclick="popWin(this.href, 400, 250); return false;">Textile</a></li>'. 235 n.t.'<li><a target="_blank" href="'.$tag_url.a.'type=textpattern" onclick="popWin(this.href, 400, 250); return false;">Textpattern</a></li>'. 236 n.t.'<li><a target="_blank" href="'.$tag_url.a.'type=xhtml" onclick="popWin(this.href, 400, 250); return false;">XHTML</a></li>'. 237 n.'</ul>' 238 , 75). 239 240 td($file_statuses[$status], 45). 241 242 td($condition, 45). 243 244 td( 245 ($downloads == '0' ? gTxt('none') : $downloads) 246 , 25). 247 248 td( 249 dLink('file', 'file_delete', 'id', $id, '', '', '', false, array($page, $sort, $dir, $crit, $search_method)) 250 , 10) 251 ); 252 } 253 254 echo endTable(). 255 256 nav_form('file', $page, $numPages, $sort, $dir, $crit, $search_method). 257 258 pageby_form('file', $file_list_pageby); 259 } 260 } 261 262 // ------------------------------------------------------------- 263 264 function file_search_form($crit, $method) 265 { 266 $methods = array( 267 'id' => gTxt('ID'), 268 'filename' => gTxt('file_name'), 269 'description' => gTxt('description'), 270 'category' => gTxt('file_category') 271 ); 272 273 return search_form('file', 'file_list', $crit, $methods, $method, 'filename'); 274 } 275 276 // ------------------------------------------------------------- 277 278 function file_edit($message = '', $id = '') 279 { 280 global $txpcfg, $file_base_path, $levels, $file_statuses; 281 282 pagetop('file', $message); 283 284 extract(gpsa(array('name', 'category', 'permissions', 'description', 'sort', 'dir', 'page', 'crit', 'search_method', 'publish_now'))); 285 286 if (!$id) 287 { 288 $id = gps('id'); 289 } 290 $id = assert_int($id); 291 292 $categories = getTree('root', 'file'); 293 294 $rs = safe_row('*, unix_timestamp(created) as created, unix_timestamp(modified) as modified', 'txp_file', "id = $id"); 295 296 if ($rs) 297 { 298 extract($rs); 299 300 if ($permissions=='') $permissions='-1'; 301 302 $file_exists = file_exists(build_file_path($file_base_path,$filename)); 303 $replace = ($file_exists) ? tr(td(file_upload_form(gTxt('replace_file'),'upload','file_replace',$id))) : ''; 304 305 $existing_files = get_filenames(); 306 307 $condition = '<span class="'; 308 $condition .= ($file_exists) ? 'ok' : 'not-ok'; 309 $condition .= '">'; 310 $condition .= ($file_exists)?gTxt('file_status_ok'):gTxt('file_status_missing'); 311 $condition .= '</span>'; 312 313 $downloadlink = ($file_exists)?make_download_link($id, htmlspecialchars($filename),$filename):htmlspecialchars($filename); 314 315 $created = 316 n.graf(checkbox('publish_now', '1', $publish_now, '', 'publish_now').'<label for="publish_now">'.gTxt('set_to_now').'</label>'). 317 318 n.graf(gTxt('or_publish_at').sp.popHelp('timestamp')). 319 320 n.graf(gtxt('date').sp. 321 tsi('year', '%Y', $rs['created']).' / '. 322 tsi('month', '%m', $rs['created']).' / '. 323 tsi('day', '%d', $rs['created']) 324 ). 325 326 n.graf(gTxt('time').sp. 327 tsi('hour', '%H', $rs['created']).' : '. 328 tsi('minute', '%M', $rs['created']).' : '. 329 tsi('second', '%S', $rs['created']) 330 ); 331 332 $form = ''; 333 334 if ($file_exists) { 335 $form = tr( 336 td( 337 form( 338 graf(gTxt('file_category').br.treeSelectInput('category', 339 $categories,$category)) . 340 // graf(gTxt('permissions').br.selectInput('perms',$levels,$permissions)). 341 graf(gTxt('description').br.text_area('description','100','400',$description)) . 342 fieldset(radio_list('status', $file_statuses, $status, 4), gTxt('status'), 'file-status'). 343 fieldset($created, gTxt('timestamp'), 'file-created'). 344 graf(fInput('submit','',gTxt('save'))) . 345 346 eInput('file') . 347 sInput('file_save'). 348 349 hInput('filename', $filename). 350 hInput('id', $id) . 351 352 hInput('sort', $sort). 353 hInput('dir', $dir). 354 hInput('page', $page). 355 hInput('crit', $crit). 356 hInput('search_method', $search_method) 357 ) 358 ) 359 ); 360 } else { 361 362 $form = tr( 363 tda( 364 hed(gTxt('file_relink'),3). 365 file_upload_form(gTxt('upload_file'),'upload','file_replace',$id). 366 form( 367 graf(gTxt('existing_file').' '. 368 selectInput('filename',$existing_files,"",1). 369 fInput('submit','',gTxt('Save'),'smallerbox'). 370 371 eInput('file'). 372 sInput('file_save'). 373 374 hInput('id',$id). 375 hInput('category',$category). 376 hInput('perms',($permissions=='-1') ? '' : $permissions). 377 hInput('description',$description). 378 hInput('status',$status). 379 380 hInput('sort', $sort). 381 hInput('dir', $dir). 382 hInput('page', $page). 383 hInput('crit', $crit). 384 hInput('search_method', $search_method) 385 386 ) 387 ), 388 ' colspan="4" style="border:0"' 389 ) 390 ); 391 } 392 echo startTable('list'), 393 tr( 394 td( 395 graf(gTxt('file_status').br.$condition) . 396 graf(gTxt('file_name').br.$downloadlink) . 397 graf(gTxt('file_download_count').br.$downloads) 398 ) 399 ), 400 $form, 401 $replace, 402 endTable(); 403 } 404 } 405 406 // ------------------------------------------------------------- 407 function file_db_add($filename,$category,$permissions,$description,$size) 408 { 409 $rs = safe_insert("txp_file", 410 "filename = '$filename', 411 category = '$category', 412 permissions = '$permissions', 413 description = '$description', 414 size = '$size', 415 created = now(), 416 modified = now() 417 "); 418 419 if ($rs) { 420 $GLOBALS['ID'] = mysql_insert_id( ); 421 return $GLOBALS['ID']; 422 } 423 424 return false; 425 } 426 427 // ------------------------------------------------------------- 428 function file_create() 429 { 430 global $txpcfg,$extensions,$txp_user,$file_base_path; 431 extract($txpcfg); 432 extract(doSlash(gpsa(array('filename','category','permissions','description')))); 433 434 $size = filesize(build_file_path($file_base_path,$filename)); 435 $id = file_db_add($filename,$category,$permissions,$description, $size); 436 437 if($id === false){ 438 file_list(gTxt('file_upload_failed').' (db_add)'); 439 } else { 440 $newpath = build_file_path($file_base_path,trim($filename)); 441 442 if (is_file($newpath)) { 443 file_set_perm($newpath); 444 file_list(gTxt('linked_to_file').' '.$filename); 445 } else { 446 file_list(gTxt('file_not_found').' '.$filename); 447 } 448 } 449 } 450 451 // ------------------------------------------------------------- 452 function file_insert() 453 { 454 global $txpcfg,$extensions,$txp_user,$file_base_path,$file_max_upload_size; 455 extract($txpcfg); 456 extract(doSlash(gpsa(array('category','permissions','description')))); 457 458 $name = file_get_uploaded_name(); 459 $file = file_get_uploaded(); 460 461 if ($file === false) { 462 // could not get uploaded file 463 file_list(gTxt('file_upload_failed') ." $name - ".upload_get_errormsg($_FILES['thefile']['error'])); 464 return; 465 } 466 467 $size = filesize($file); 468 if ($file_max_upload_size < $size) { 469 unlink($file); 470 file_list(gTxt('file_upload_failed') ." $name - ".upload_get_errormsg(UPLOAD_ERR_FORM_SIZE)); 471 return; 472 } 473 474 if (!is_file(build_file_path($file_base_path,$name))) { 475 476 $id = file_db_add($name,$category,$permissions,$description,$size); 477 478 if(!$id){ 479 file_list(gTxt('file_upload_failed').' (db_add)'); 480 } else { 481 482 $id = assert_int($id); 483 $newpath = build_file_path($file_base_path,trim($name)); 484 485 if(!shift_uploaded_file($file, $newpath)) { 486 safe_delete("txp_file","id = $id"); 487 safe_alter("txp_file", "auto_increment=$id"); 488 if ( isset( $GLOBALS['ID'])) unset( $GLOBALS['ID']); 489 file_list($newpath.' '.gTxt('upload_dir_perms')); 490 // clean up file 491 } else { 492 file_set_perm($newpath); 493 494 $message = gTxt('file_uploaded', array('{name}' => htmlspecialchars($name))); 495 496 file_edit($message, $id); 497 } 498 } 499 } 500 501 else 502 { 503 $message = gTxt('file_already_exists', array('{name}' => $name)); 504 505 file_list($message); 506 } 507 } 508 509 // ------------------------------------------------------------- 510 function file_replace() 511 { 512 global $txpcfg,$extensions,$txp_user,$file_base_path; 513 extract($txpcfg); 514 $id = assert_int(gps('id')); 515 516 $rs = safe_row('filename','txp_file',"id = $id"); 517 518 if (!$rs) { 519 file_list(messenger(gTxt('invalid_id'),$id,'')); 520 return; 521 } 522 523 extract($rs); 524 525 $file = file_get_uploaded(); 526 $name = file_get_uploaded_name(); 527 528 if ($file === false) { 529 // could not get uploaded file 530 file_list(gTxt('file_upload_failed') ." $name ".upload_get_errormsg($_FILES['thefile']['error'])); 531 return; 532 } 533 534 if (!$filename) { 535 file_list(gTxt('invalid_filename')); 536 } else { 537 $newpath = build_file_path($file_base_path,$filename); 538 539 if (is_file($newpath)) { 540 rename($newpath,$newpath.'.tmp'); 541 } 542 543 if(!shift_uploaded_file($file, $newpath)) { 544 safe_delete("txp_file","id = $id"); 545 546 file_list($newpath.sp.gTxt('upload_dir_perms')); 547 // rename tmp back 548 rename($newpath.'.tmp',$newpath); 549 550 // remove tmp upload 551 unlink($file); 552 } else { 553 file_set_perm($newpath); 554 if ($size = filesize($newpath)) 555 safe_update('txp_file', 'size = '.$size.', modified = now()', 'id = '.$id); 556 557 $message = gTxt('file_uploaded', array('{name}' => htmlspecialchars($name))); 558 559 file_edit($message, $id); 560 // clean up old 561 if (is_file($newpath.'.tmp')) 562 unlink($newpath.'.tmp'); 563 } 564 } 565 } 566 567 568 // ------------------------------------------------------------- 569 function file_reset_count() 570 { 571 extract(doSlash(gpsa(array('id','filename','category','description')))); 572 573 if ($id) { 574 $id = assert_int($id); 575 if (safe_update('txp_file','downloads = 0',"id = $id")) { 576 file_edit(gTxt('reset_file_count_success'),$id); 577 } 578 } else { 579 file_list(gTxt('reset_file_count_failure')); 580 } 581 } 582 583 // ------------------------------------------------------------- 584 585 function file_save() 586 { 587 global $file_base_path; 588 589 extract(doSlash(gpsa(array('id', 'filename', 'category', 'description', 'status', 'publish_now', 'year', 'month', 'day', 'hour', 'minute', 'second')))); 590 591 $id = assert_int($id); 592 593 $permissions = gps('perms'); 594 if (is_array($permissions)) { 595 asort($permissions); 596 $permissions = implode(",",$permissions); 597 } 598 599 $perms = doSlash($permissions); 600 601 $old_filename = fetch('filename','txp_file','id',$id); 602 603 if ($old_filename != false && strcmp($old_filename, $filename) != 0) 604 { 605 $old_path = build_file_path($file_base_path,$old_filename); 606 $new_path = build_file_path($file_base_path,$filename); 607 608 if (file_exists($old_path) && shift_uploaded_file($old_path, $new_path) === false) 609 { 610 $message = gTxt('file_cannot_rename', array('{name}' => $filename)); 611 612 return file_list($message); 613 } 614 615 else 616 { 617 file_set_perm($new_path); 618 } 619 } 620 621 $created_ts = @safe_strtotime($year.'-'.$month.'-'.$day.' '.$hour.':'.$minute.':'.$second); 622 if ($publish_now) 623 $created = 'now()'; 624 elseif ($created_ts > 0) 625 $created = "from_unixtime('".$created_ts."')"; 626 else 627 $created = ''; 628 629 $size = filesize(build_file_path($file_base_path,$filename)); 630 $rs = safe_update('txp_file', " 631 filename = '$filename', 632 category = '$category', 633 permissions = '$perms', 634 description = '$description', 635 status = '$status', 636 size = '$size', 637 modified = now()" 638 .($created ? ", created = $created" : '') 639 , "id = $id"); 640 641 if (!$rs) 642 { 643 // update failed, rollback name 644 if (shift_uploaded_file($new_path, $old_path) === false) 645 { 646 $message = gTxt('file_unsynchronized', array('{name}' => $filename)); 647 648 return file_list($message); 649 } 650 651 else 652 { 653 $message = gTxt('file_not_updated', array('{name}' => $filename)); 654 655 return file_list($message); 656 } 657 } 658 659 $message = gTxt('file_updated', array('{name}' => $filename)); 660 661 file_list($message); 662 } 663 664 // ------------------------------------------------------------- 665 666 function file_delete() 667 { 668 global $txpcfg, $file_base_path; 669 670 extract($txpcfg); 671 672 $id = assert_int(ps('id')); 673 674 $rs = safe_row('*', 'txp_file', "id = $id"); 675 676 if ($rs) 677 { 678 extract($rs); 679 680 $filepath = build_file_path($file_base_path, $filename); 681 682 $rsd = safe_delete('txp_file', "id = $id"); 683 $ul = false; 684 685 if ($rsd && is_file($filepath)) 686 { 687 $ul = unlink($filepath); 688 } 689 690 if ($rsd && $ul) 691 { 692 $message = gTxt('file_deleted', array('{name}' => $filename)); 693 694 return file_list($message); 695 } 696 697 else 698 { 699 file_list(messenger(gTxt('file_delete_failed'), $filename, '')); 700 } 701 } 702 703 else 704 { 705 file_list(messenger(gTxt('file_not_found'), $filename, '')); 706 } 707 } 708 709 // ------------------------------------------------------------- 710 function file_get_uploaded_name() 711 { 712 return $_FILES['thefile']['name']; 713 } 714 715 // ------------------------------------------------------------- 716 function file_get_uploaded() 717 { 718 return get_uploaded_file($_FILES['thefile']['tmp_name']); 719 } 720 721 // ------------------------------------------------------------- 722 function file_set_perm($file) 723 { 724 return @chmod($file,0644); 725 } 726 727 // ------------------------------------------------------------- 728 function file_upload_form($label,$pophelp,$step,$id='') 729 { 730 global $file_max_upload_size; 731 732 if (!$file_max_upload_size || intval($file_max_upload_size)==0) $file_max_upload_size = 2*(1024*1024); 733 734 $max_file_size = (intval($file_max_upload_size) == 0) ? '': intval($file_max_upload_size); 735 736 return upload_form($label, $pophelp, $step, 'file', $id, $max_file_size); 737 } 738 739 // ------------------------------------------------------------- 740 function file_change_pageby() 741 { 742 event_change_pageby('file'); 743 file_list(); 744 } 745 746 // ------------------------------------------------------------- 747 function file_change_max_size() 748 { 749 // DEPRECATED function; removed old code 750 file_list(); 751 } 752 753 // ------------------------------------------------------------- 754 755 function make_download_link($id, $label = '', $filename = '') 756 { 757 $label = ($label) ? $label : gTxt('download'); 758 $url = filedownloadurl($id, $filename); 759 return '<a href="'.$url.'">'.$label.'</a>'; 760 } 761 762 // ------------------------------------------------------------- 763 function get_filenames() 764 { 765 global $file_base_path; 766 767 $dirlist = array(); 768 769 if (!is_dir($file_base_path)) 770 return $dirlist; 771 772 if (chdir($file_base_path)) { 773 if (function_exists('glob')) 774 $g_array = glob("*.*"); 775 else { 776 $dh = opendir($file_base_path); 777 $g_array = array(); 778 while (false !== ($filename = readdir($dh))) { 779 $g_array[] = $filename; 780 } 781 closedir($dh); 782 783 } 784 785 if ($g_array) { 786 foreach ($g_array as $filename) { 787 if (is_file($filename)) { 788 $dirlist[$filename] = $filename; 789 } 790 } 791 } 792 } 793 794 $files = array(); 795 $rs = safe_rows("filename", "txp_file", "1=1"); 796 797 if ($rs) { 798 foreach ($rs as $a) { 799 $files[$a['filename']] = $a['filename']; 800 } 801 } 802 803 return array_diff($dirlist,$files); 804 } 805 806 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Mon Feb 18 03:42:45 2008 | Cross-referenced by PHPXref 0.7 |