| [ 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 This is Textpattern 5 6 Copyright 2005 by Dean Allen 7 www.textpattern.com 8 All rights reserved 9 10 Use of this software indicates acceptance of the Textpattern license agreement 11 12 $HeadURL: http://svn.textpattern.com/releases/4.0.6/source/textpattern/include/txp_image.php $ 13 $LastChangedRevision: 2668 $ 14 15 */ 16 17 if (!defined('txpinterface')) die('txpinterface is undefined.'); 18 19 global $extensions; 20 $extensions = array(0,'.gif','.jpg','.png','.swf',0,0,0,0,0,0,0,0,'.swf'); 21 define("IMPATH",$path_to_site.DS.$img_dir.DS); 22 include txpath.'/lib/class.thumb.php'; 23 24 if ($event == 'image') { 25 require_privs('image'); 26 27 if(!$step or !in_array($step, array('image_list','image_edit','image_insert','image_delete','image_replace','image_save','thumbnail_insert','image_change_pageby','thumbnail_create','thumbnail_delete' 28 ))){ 29 image_list(); 30 } else $step(); 31 } 32 33 // ------------------------------------------------------------- 34 35 function image_list($message = '') 36 { 37 global $txpcfg, $extensions, $img_dir, $file_max_upload_size; 38 39 pagetop(gTxt('images'), $message); 40 41 extract($txpcfg); 42 extract(get_prefs()); 43 44 extract(gpsa(array('page', 'sort', 'dir', 'crit', 'search_method'))); 45 46 if (!is_dir(IMPATH) or !is_writeable(IMPATH)) 47 { 48 echo graf( 49 gTxt('img_dir_not_writeable', array('{imgdir}' => IMPATH)) 50 ,' id="warning"'); 51 } 52 53 else 54 { 55 echo upload_form(gTxt('upload_image'), 'upload', 'image_insert', 'image', '', $file_max_upload_size); 56 } 57 58 $dir = ($dir == 'asc') ? 'asc' : 'desc'; 59 60 switch ($sort) 61 { 62 case 'name': 63 $sort_sql = 'name '.$dir; 64 break; 65 66 case 'thumbnail': 67 $sort_sql = 'thumbnail '.$dir.', id asc'; 68 break; 69 70 case 'category': 71 $sort_sql = 'category '.$dir.', id asc'; 72 break; 73 74 case 'date': 75 $sort_sql = 'date '.$dir.', id asc'; 76 break; 77 78 case 'author': 79 $sort_sql = 'author '.$dir.', id asc'; 80 break; 81 82 default: 83 $sort = 'id'; 84 $sort_sql = 'id '.$dir; 85 break; 86 } 87 88 $switch_dir = ($dir == 'desc') ? 'asc' : 'desc'; 89 90 $criteria = 1; 91 92 if ($search_method and $crit) 93 { 94 $crit_escaped = doSlash($crit); 95 96 $critsql = array( 97 'id' => "id = '$crit_escaped'", 98 'name' => "name like '%$crit_escaped%'", 99 'category' => "category like '%$crit_escaped%'", 100 'author' => "author like '%$crit_escaped%'" 101 ); 102 103 if (array_key_exists($search_method, $critsql)) 104 { 105 $criteria = $critsql[$search_method]; 106 $limit = 500; 107 } 108 109 else 110 { 111 $search_method = ''; 112 $crit = ''; 113 } 114 } 115 116 else 117 { 118 $search_method = ''; 119 $crit = ''; 120 } 121 $total = safe_count('txp_image', "$criteria"); 122 123 if ($total < 1) 124 { 125 if ($criteria != 1) 126 { 127 echo n.image_search_form($crit, $search_method). 128 n.graf(gTxt('no_results_found'), ' style="text-align: center;"'); 129 } 130 131 else 132 { 133 echo n.graf(gTxt('no_images_recorded'), ' style="text-align: center;"'); 134 } 135 136 return; 137 } 138 139 $limit = max(@$image_list_pageby, 15); 140 141 list($page, $offset, $numPages) = pager($total, $limit, $page); 142 143 echo image_search_form($crit, $search_method); 144 145 $rs = safe_rows_start('*, unix_timestamp(date) as uDate', 'txp_image', 146 "$criteria order by $sort_sql limit $offset, $limit 147 "); 148 149 if ($rs) 150 { 151 echo n.n.startTable('list'). 152 n.tr( 153 column_head('ID', 'id', 'image', true, $switch_dir, $crit, $search_method, ('id' == $sort) ? $dir : ''). 154 hCell(). 155 column_head('date', 'date', 'image', true, $switch_dir, $crit, $search_method, ('date' == $sort) ? $dir : ''). 156 column_head('name', 'name', 'image', true, $switch_dir, $crit, $search_method, ('name' == $sort) ? $dir : ''). 157 column_head('thumbnail', 'thumbnail', 'image', true, $switch_dir, $crit, $search_method, ('thumbnail' == $sort) ? $dir : ''). 158 hCell(gTxt('tags')). 159 column_head('image_category', 'category', 'image', true, $switch_dir, $crit, $search_method, ('category' == $sort) ? $dir : ''). 160 column_head('author', 'author', 'image', true, $switch_dir, $crit, $search_method, ('author' == $sort) ? $dir : ''). 161 hCell() 162 ); 163 164 while ($a = nextRow($rs)) 165 { 166 extract($a); 167 168 $edit_url = '?event=image'.a.'step=image_edit'.a.'id='.$id.a.'sort='.$sort. 169 a.'dir='.$dir.a.'page='.$page.a.'search_method='.$search_method.a.'crit='.$crit; 170 171 $name = empty($name) ? gTxt('unnamed') : htmlspecialchars($name); 172 173 if ($thumbnail) { 174 if ($ext != '.swf') { 175 $thumbnail = '<img src="'.hu.$img_dir.'/'.$id.'t'.$ext.'" alt="" />'; 176 } else { 177 $thumbnail = ''; 178 } 179 } else { 180 $thumbnail = gTxt('no'); 181 } 182 183 if ($ext != '.swf') { 184 $tag_url = '?event=tag'.a.'tag_name=image'.a.'id='.$id.a.'ext='.$ext.a.'w='.$w.a.'h='.$h.a.'alt='.urlencode($alt).a.'caption='.urlencode($caption); 185 $tagbuilder = '<ul>'. 186 '<li><a target="_blank" href="'.$tag_url.a.'type=textile" onclick="popWin(this.href); return false;">Textile</a></li>'. 187 '<li><a target="_blank" href="'.$tag_url.a.'type=textpattern" onclick="popWin(this.href); return false;">Textpattern</a></li>'. 188 '<li><a target="_blank" href="'.$tag_url.a.'type=xhtml" onclick="popWin(this.href); return false;">XHTML</a></li>'. 189 '</ul>'; 190 } else { 191 $tagbuilder = sp; 192 } 193 194 $category = ($category) ? '<span title="'.htmlspecialchars(fetch_category_title($category, 'image')).'">'.$category.'</span>' : ''; 195 196 echo n.n.tr( 197 198 n.td($id, 20). 199 200 td( 201 n.'<ul>'. 202 n.t.'<li>'.href(gTxt('edit'), $edit_url).'</li>'. 203 n.t.'<li><a href="'.hu.$img_dir.'/'.$id.$ext.'">'.gTxt('view').'</a></li>'. 204 n.'</ul>' 205 , 35). 206 207 td( 208 gTime($uDate) 209 , 75). 210 211 td( 212 href($name, $edit_url) 213 , 75). 214 215 td( 216 href($thumbnail, $edit_url) 217 , 80). 218 219 td($tagbuilder, 85). 220 td($category, 75). 221 222 td( 223 '<span title="'.htmlspecialchars(get_author_name($author)).'">'.htmlspecialchars($author).'</span>' 224 , 75). 225 226 td( 227 dLink('image', 'image_delete', 'id', $id, '', '', '', false, array($page, $sort, $dir, $crit, $search_method)) 228 , 10) 229 ); 230 } 231 232 echo endTable(). 233 234 nav_form('image', $page, $numPages, $sort, $dir, $crit, $search_method). 235 236 pageby_form('image', $image_list_pageby); 237 } 238 } 239 240 // ------------------------------------------------------------- 241 242 function image_search_form($crit, $method) 243 { 244 $methods = array( 245 'id' => gTxt('ID'), 246 'name' => gTxt('name'), 247 'category' => gTxt('image_category'), 248 'author' => gTxt('author') 249 ); 250 251 return search_form('image', 'image_list', $crit, $methods, $method, 'name'); 252 } 253 254 // ------------------------------------------------------------- 255 function image_edit($message='',$id='') 256 { 257 if (!$id) $id = gps('id'); 258 $id = assert_int($id); 259 global $txpcfg,$img_dir,$file_max_upload_size; 260 261 pagetop(gTxt('edit_image'),$message); 262 263 extract(gpsa(array('page', 'sort', 'dir', 'crit', 'search_method'))); 264 265 $categories = getTree("root", "image"); 266 267 $rs = safe_row("*", "txp_image", "id = $id"); 268 269 if ($rs) { 270 extract($rs); 271 272 if ($ext != '.swf') { 273 $img = '<img src="'.hu.$img_dir.'/'.$id.$ext.'" height="'.$h.'" width="'.$w.'" alt="" title="'.$id.$ext.' ('.$w.' × '.$h.')" />'; 274 } else { 275 $img = ''; 276 } 277 278 if ($thumbnail and ($ext != '.swf')) { 279 $thumb = '<img src="'.hu.$img_dir.'/'.$id.'t'.$ext.'" alt="" />'; 280 } else { 281 $thumb = ''; 282 } 283 284 echo startTable('list'), 285 tr( 286 td( 287 $img.br. 288 upload_form(gTxt('replace_image'), 'replace_image_form', 'image_replace', 'image', $id, $file_max_upload_size, 'image-replace', '') 289 ) 290 ), 291 tr( 292 td( 293 join('', 294 array( 295 ($thumbnail) 296 ? startTable('image-thumbnail'). 297 tr( 298 td($thumb). 299 td(dLink('image','thumbnail_delete','id',$id)) 300 ). 301 endTable().br 302 : '', 303 upload_form(gTxt('upload_thumbnail'),'upload_thumbnail', 304 'thumbnail_insert','image',$id,$file_max_upload_size, 'upload-thumbnail', '') 305 ) 306 ) 307 ) 308 ), 309 310 (check_gd($ext)) 311 ? thumb_ui( $id ) 312 : '', 313 314 tr( 315 td( 316 form( 317 graf('<label for="image-name">'.gTxt('image_name').'</label>'.br. 318 fInput('text', 'name', $name, 'edit', '', '', '', '', 'image-name')). 319 320 graf('<label for="image-category">'.gTxt('image_category').'</label>'.br. 321 treeSelectInput('category', $categories, $category, 'image-category')). 322 323 graf('<label for="alt-text">'.gTxt('alt_text').'</label>'.br. 324 fInput('text', 'alt', $alt, 'edit', '', '', 50, '', 'alt-text')). 325 326 graf('<label for="caption">'.gTxt('caption').'</label>'.br. 327 text_area('caption', '100', '400', $caption, 'caption')). 328 329 n.graf(fInput('submit', '', gTxt('save'), 'publish')). 330 n.hInput('id', $id). 331 n.eInput('image'). 332 n.sInput('image_save'). 333 334 n.hInput('sort', $sort). 335 n.hInput('dir', $dir). 336 n.hInput('page', $page). 337 n.hInput('search_method', $search_method). 338 n.hInput('crit', $crit) 339 ) 340 ) 341 ), 342 endTable(); 343 } 344 } 345 346 // ------------------------------------------------------------- 347 348 function image_insert() 349 { 350 global $txpcfg, $extensions, $txp_user; 351 352 extract($txpcfg); 353 354 $meta = gpsa(array('caption', 'alt', 'category')); 355 356 $img_result = image_data($_FILES['thefile'], $meta); 357 358 if (is_array($img_result)) 359 { 360 list($message, $id) = $img_result; 361 362 return image_edit($message, $id); 363 } 364 365 else 366 { 367 return image_list($img_result); 368 } 369 } 370 371 // ------------------------------------------------------------- 372 function image_replace() 373 { 374 global $txpcfg,$extensions,$txp_user; 375 extract($txpcfg); 376 377 $id = assert_int(gps('id')); 378 $rs = safe_row("*", "txp_image", "id = $id"); 379 380 if ($rs) { 381 $meta = array('category' => $rs['category'], 'caption' => $rs['caption'], 'alt' => $rs['alt']); 382 } else { 383 $meta = ''; 384 } 385 386 $img_result = image_data($_FILES['thefile'], $meta, $id); 387 388 if(is_array($img_result)) 389 { 390 list($message, $id) = $img_result; 391 return image_edit($message, $id); 392 }else{ 393 return image_list($img_result); 394 } 395 } 396 397 // ------------------------------------------------------------- 398 function thumbnail_insert() 399 { 400 global $txpcfg,$extensions,$txp_user,$img_dir,$path_to_site; 401 extract($txpcfg); 402 $id = assert_int(gps('id')); 403 404 $file = $_FILES['thefile']['tmp_name']; 405 $name = $_FILES['thefile']['name']; 406 407 $file = get_uploaded_file($file); 408 409 list(,,$extension) = getimagesize($file); 410 411 if (($file !== false) && @$extensions[$extension]) { 412 $ext = $extensions[$extension]; 413 414 $newpath = IMPATH.$id.'t'.$ext; 415 416 if(shift_uploaded_file($file, $newpath) == false) { 417 image_list($newpath.sp.gTxt('upload_dir_perms')); 418 } else { 419 chmod($newpath,0644); 420 safe_update("txp_image", "thumbnail = 1", "id = $id"); 421 422 $message = gTxt('image_uploaded', array('{name}' => $name)); 423 update_lastmod(); 424 425 image_edit($message, $id); 426 } 427 } else { 428 if ($file === false) 429 image_list(upload_get_errormsg($_FILES['thefile']['error'])); 430 else 431 image_list(gTxt('only_graphic_files_allowed')); 432 } 433 } 434 435 436 // ------------------------------------------------------------- 437 function image_save() 438 { 439 extract(doSlash(gpsa(array('id','name','category','caption','alt')))); 440 $id = assert_int($id); 441 442 safe_update( 443 "txp_image", 444 "name = '$name', 445 category = '$category', 446 alt = '$alt', 447 caption = '$caption'", 448 "id = $id" 449 ); 450 451 $message = gTxt('image_updated', array('{name}' => $name)); 452 update_lastmod(); 453 454 image_list($message); 455 } 456 457 // ------------------------------------------------------------- 458 459 function image_delete() { 460 global $txpcfg; 461 462 extract($txpcfg); 463 464 $id = assert_int(ps('id')); 465 466 $rs = safe_row('*', 'txp_image', "id = $id"); 467 468 if ($rs) { 469 extract($rs); 470 471 $rsd = safe_delete('txp_image', "id = $id"); 472 473 $ul = false; 474 475 if (is_file(IMPATH.$id.$ext)) { 476 $ul = unlink(IMPATH.$id.$ext); 477 } 478 479 if (is_file(IMPATH.$id.'t'.$ext)) { 480 $ult = unlink(IMPATH.$id.'t'.$ext); 481 } 482 483 if ($rsd && $ul) { 484 update_lastmod(); 485 486 image_list(gTxt('image_deleted', array('{name}' => $name))); 487 } else { 488 image_list(gTxt('image_delete_failed', array('{name}' => $name))); 489 } 490 } else { 491 image_list(); 492 } 493 } 494 495 // ------------------------------------------------------------- 496 function image_change_pageby() 497 { 498 event_change_pageby('image'); 499 image_list(); 500 } 501 502 // ------------------------------------------------------------- 503 function thumb_ui($id) 504 { 505 global $prefs, $sort, $dir, $page, $search_method, $crit; 506 extract($prefs); 507 return 508 tr( 509 td( 510 form( 511 graf(gTxt('create_thumbnail')) . 512 startTable('','left','',1) . 513 tr( 514 fLabelCell(gTxt('thumb_width'), '', 'width') . 515 fInputCell('width', @$thumb_w, 1, 4, '', 'width'). 516 517 fLabelCell(gTxt('thumb_height'), '', 'height') . 518 fInputCell('height', @$thumb_h, 1, 4, '', 'height'). 519 520 fLabelCell(gTxt('keep_square_pixels'), '', 'crop') . 521 tda(checkbox('crop', 1, @$thumb_crop, '', 'crop'), ' class="noline"'). 522 523 tda( 524 fInput('submit', '', gTxt('Create'), 'smallerbox') 525 , ' class="noline"') 526 ). 527 528 n.hInput('id', $id). 529 n.eInput('image'). 530 n.sInput('thumbnail_create'). 531 532 n.hInput('sort', $sort). 533 n.hInput('dir', $dir). 534 n.hInput('page', $page). 535 n.hInput('search_method', $search_method). 536 n.hInput('crit', $crit). 537 538 endTable() 539 ) 540 ) 541 ); 542 } 543 544 // ------------------------------------------------------------- 545 546 function thumbnail_create() 547 { 548 extract(doSlash(gpsa(array('id', 'width', 'height')))); 549 550 // better checking of thumbnail dimensions 551 // don't try and use zeros 552 553 $width = (int) $width; 554 $height = (int) $height; 555 556 if ($width == 0 && $height == 0) { 557 image_edit(messenger('invalid_width_or_height', "($width)/($height)", ''), $id); 558 return; 559 } else { 560 if ($width == 0) { 561 $width = ''; 562 } 563 564 if ($height == 0) { 565 $height = ''; 566 } 567 } 568 569 $crop = gps('crop'); 570 571 $t = new txp_thumb( $id ); 572 $t->crop = ($crop == '1'); 573 $t->hint = '0'; 574 575 $t->width = $width; 576 $t->height = $height; 577 578 if ($t->write()) 579 { 580 global $prefs; 581 582 $prefs['thumb_w'] = $width; 583 $prefs['thumb_h'] = $height; 584 $prefs['thumb_crop'] = $crop; 585 586 // hidden prefs 587 set_pref('thumb_w', $width, 'image', 2); 588 set_pref('thumb_h', $height, 'image', 2); 589 set_pref('thumb_crop', $crop, 'image', 2); 590 591 $message = gTxt('thumbnail_saved', array('{id}' => $id)); 592 update_lastmod(); 593 594 image_edit($message, $id); 595 } 596 597 else 598 { 599 $message = gTxt('thumbnail_not_saved', array('{id}' => $id)); 600 601 image_edit($message, $id); 602 } 603 } 604 605 // ------------------------------------------------------------- 606 function thumbnail_delete() 607 { 608 $id = assert_int(gps('id')); 609 $t = new txp_thumb($id); 610 if ($t->delete()) { 611 image_edit(gTxt('thumbnail_deleted'),$id); 612 } else { 613 image_edit(gTxt('thumbnail_delete_failed'),$id); 614 } 615 } 616 617 // ------------------------------------------------------------- 618 // Refactoring attempt, allowing other - plugin - functions to 619 // upload images without the need for writting duplicated code. 620 621 function image_data($file , $meta = '', $id = '', $uploaded = true) 622 { 623 global $txpcfg, $extensions, $txp_user, $prefs, $file_max_upload_size; 624 625 extract($txpcfg); 626 627 $name = $file['name']; 628 $error = $file['error']; 629 $file = $file['tmp_name']; 630 631 if ($uploaded) 632 { 633 $file = get_uploaded_file($file); 634 635 if ($file_max_upload_size < filesize($file)) 636 { 637 unlink($file); 638 639 return upload_get_errormsg(UPLOAD_ERR_FORM_SIZE); 640 } 641 } 642 643 list($w, $h, $extension) = getimagesize($file); 644 645 if (($file !== false) && @$extensions[$extension]) 646 { 647 $ext = $extensions[$extension]; 648 649 $name = doSlash(substr($name, 0, strrpos($name, '.')).$ext); 650 651 if ($meta == false) 652 { 653 $meta = array('category' => '', 'caption' => '', 'alt' => ''); 654 } 655 656 extract(doSlash($meta)); 657 658 $q =" 659 name = '$name', 660 ext = '$ext', 661 w = $w, 662 h = $h, 663 alt = '$alt', 664 caption = '$caption', 665 category = '$category', 666 date = now(), 667 author = '$txp_user' 668 "; 669 670 if (empty($id)) 671 { 672 $rs = safe_insert('txp_image', $q); 673 674 $id = $GLOBALS['ID'] = mysql_insert_id(); 675 } 676 677 else 678 { 679 $id = assert_int($id); 680 681 $rs = safe_update('txp_image', $q, "id = $id"); 682 } 683 684 if (!$rs) 685 { 686 return gTxt('image_save_error'); 687 } 688 689 else 690 { 691 $newpath = IMPATH.$id.$ext; 692 693 if (shift_uploaded_file($file, $newpath) == false) 694 { 695 $id = assert_int($id); 696 697 safe_delete('txp_image', "id = $id"); 698 699 safe_alter('txp_image', "auto_increment = $id"); 700 701 if (isset($GLOBALS['ID'])) 702 { 703 unset( $GLOBALS['ID']); 704 } 705 706 return $newpath.sp.gTxt('upload_dir_perms'); 707 } 708 709 else 710 { 711 @chmod($newpath, 0644); 712 713 // GD is supported 714 if (check_gd($ext)) 715 { 716 // Auto-generate a thumbnail using the last settings 717 if (isset($prefs['thumb_w'], $prefs['thumb_h'], $prefs['thumb_crop'])) 718 { 719 $width = intval($prefs['thumb_w']); 720 $height = intval($prefs['thumb_h']); 721 722 if ($width > 0 or $height > 0) 723 { 724 $t = new txp_thumb( $id ); 725 726 $t->crop = ($prefs['thumb_crop'] == '1'); 727 $t->hint = '0'; 728 $t->width = $width; 729 $t->height = $height; 730 731 $t->write(); 732 } 733 } 734 } 735 736 $message = gTxt('image_uploaded', array('{name}' => $name)); 737 update_lastmod(); 738 739 return array($message, $id); 740 } 741 } 742 } 743 744 else 745 { 746 if ($file === false) 747 { 748 return upload_get_errormsg($error); 749 } 750 751 else 752 { 753 return gTxt('only_graphic_files_allowed'); 754 } 755 } 756 } 757 758 // ------------------------------------------------------------- 759 // check GD info 760 761 function check_gd($image_type) { 762 // GD is installed 763 if (function_exists('gd_info')) { 764 $gd_info = gd_info(); 765 766 switch ($image_type) { 767 // check gif support 768 case '.gif': 769 return ($gd_info['GIF Create Support'] == 1) ? true : false; 770 break; 771 772 // check png support 773 case '.png': 774 return ($gd_info['PNG Support'] == 1) ? true : false; 775 break; 776 777 // check jpg support 778 case '.jpg': 779 return ($gd_info['JPG Support'] == 1) ? true : false; 780 break; 781 782 // unsupported format 783 default: 784 return false; 785 break; 786 } 787 } else { // GD isn't installed 788 return false; 789 } 790 } 791 792 ?>
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 |