[ Index ]

PHP Cross Reference of Textpattern 4.0.8

title

Body

[close]

/textpattern/include/ -> txp_image.php (source)

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


Generated: Thu May 21 23:03:01 2009 Cross-referenced by PHPXref 0.7