Textpattern PHP Cross Reference Content Management Systems

Source: /textpattern/include/txp_image.php - 1089 lines - 28638 bytes - Summary - Text - Print

   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.5.4/source/textpattern/include/txp_image.php $
  13  $LastChangedRevision: 4089 $
  14  
  15  */
  16  
  17      if (!defined('txpinterface')) die('txpinterface is undefined.');
  18  
  19      global $extensions;
  20      $extensions = (has_privs('image.create.trusted')) ?
  21              array(0,'.gif','.jpg','.png','.swf',0,0,0,0,0,0,0,0,'.swf') :
  22              array(0,'.gif','.jpg','.png');
  23  
  24      define("IMPATH",$path_to_site.DS.$img_dir.DS);
  25      include txpath.'/lib/class.thumb.php';
  26  
  27      if ($event == 'image')
  28      {
  29          require_privs('image');
  30  
  31          global $all_image_cats, $all_image_authors;
  32          $all_image_cats = getTree('root', 'image');
  33          $all_image_authors = the_privileged('image.edit.own');
  34  
  35          $available_steps = array(
  36              'image_list'          => false,
  37              'image_edit'          => false,
  38              'image_insert'        => true,
  39              'image_replace'       => true,
  40              'image_save'          => true,
  41              'thumbnail_insert'    => true,
  42              'image_change_pageby' => true,
  43              'thumbnail_create'    => true,
  44              'thumbnail_delete'    => true,
  45              'image_multi_edit'    => true,
  46          );
  47  
  48          if ($step && bouncer($step, $available_steps)) {
  49              $step();
  50          } else {
  51              image_list();
  52          }
  53      }
  54  
  55  // -------------------------------------------------------------
  56  
  57  	function image_list($message = '')
  58      {
  59          global $txpcfg, $extensions, $img_dir, $file_max_upload_size, $image_list_pageby, $txp_user, $event;
  60  
  61          pagetop(gTxt('tab_image'), $message);
  62  
  63          extract($txpcfg);
  64  
  65          extract(gpsa(array('page', 'sort', 'dir', 'crit', 'search_method')));
  66          if ($sort === '') $sort = get_pref('image_sort_column', 'id');
  67          if ($dir === '') $dir = get_pref('image_sort_dir', 'desc');
  68          $dir = ($dir == 'asc') ? 'asc' : 'desc';
  69  
  70          echo '<h1 class="txp-heading">'.gTxt('tab_image').'</h1>';
  71          echo '<div id="'.$event.'_control" class="txp-control-panel">';
  72  
  73          if (!is_dir(IMPATH) or !is_writeable(IMPATH))
  74          {
  75              echo graf(
  76                  gTxt('img_dir_not_writeable', array('{imgdir}' => IMPATH))
  77              ,' class="alert-block warning"');
  78          }
  79  
  80          elseif (has_privs('image.edit.own'))
  81          {
  82              echo upload_form(gTxt('upload_image'), 'upload_image', 'image_insert', 'image', '', $file_max_upload_size);
  83          }
  84  
  85          switch ($sort)
  86          {
  87              case 'name':
  88                  $sort_sql = 'name '.$dir;
  89              break;
  90  
  91              case 'thumbnail':
  92                  $sort_sql = 'thumbnail '.$dir.', id asc';
  93              break;
  94  
  95              case 'category':
  96                  $sort_sql = 'category '.$dir.', id asc';
  97              break;
  98  
  99              case 'date':
 100                  $sort_sql = 'date '.$dir.', id asc';
 101              break;
 102  
 103              case 'author':
 104                  $sort_sql = 'author '.$dir.', id asc';
 105              break;
 106  
 107              default:
 108                  $sort = 'id';
 109                  $sort_sql = 'id '.$dir;
 110              break;
 111          }
 112  
 113          set_pref('image_sort_column', $sort, 'image', 2, '', 0, PREF_PRIVATE);
 114          set_pref('image_sort_dir', $dir, 'image', 2, '', 0, PREF_PRIVATE);
 115  
 116          $switch_dir = ($dir == 'desc') ? 'asc' : 'desc';
 117  
 118          $criteria = 1;
 119  
 120          if ($search_method and $crit != '')
 121          {
 122              $verbatim = preg_match('/^"(.*)"$/', $crit, $m);
 123              $crit_escaped = doSlash($verbatim ? $m[1] : str_replace(array('\\','%','_','\''), array('\\\\','\\%','\\_', '\\\''), $crit));
 124              $critsql = $verbatim ?
 125                  array(
 126                      'id'       => "ID in ('" .join("','", do_list($crit_escaped)). "')",
 127                      'name'     => "name = '$crit_escaped'",
 128                      'category' => "category = '$crit_escaped'",
 129                      'author'   => "author = '$crit_escaped'",
 130                      'alt'      => "alt = '$crit_escaped'",
 131                      'caption'  => "caption = '$crit_escaped'"
 132                  ) : array(
 133                      'id'       => "ID in ('" .join("','", do_list($crit_escaped)). "')",
 134                      'name'     => "name like '%$crit_escaped%'",
 135                      'category' => "category like '%$crit_escaped%'",
 136                      'author'   => "author like '%$crit_escaped%'",
 137                      'alt'      => "alt like '%$crit_escaped%'",
 138                      'caption'  => "caption like '%$crit_escaped%'"
 139                  );
 140  
 141              if (array_key_exists($search_method, $critsql))
 142              {
 143                  $criteria = $critsql[$search_method];
 144                  $limit = 500;
 145              }
 146  
 147              else
 148              {
 149                  $search_method = '';
 150                  $crit = '';
 151              }
 152          }
 153  
 154          else
 155          {
 156              $search_method = '';
 157              $crit = '';
 158          }
 159  
 160          $criteria .= callback_event('admin_criteria', 'image_list', 0, $criteria);
 161  
 162          $total = safe_count('txp_image', "$criteria");
 163  
 164          if ($total < 1)
 165          {
 166              if ($criteria != 1)
 167              {
 168                  echo n.image_search_form($crit, $search_method).
 169                      n.graf(gTxt('no_results_found'), ' class="indicator"').'</div>';
 170              }
 171  
 172              else
 173              {
 174                  echo n.graf(gTxt('no_images_recorded'), ' class="indicator"').'</div>';
 175              }
 176  
 177              return;
 178          }
 179  
 180          $limit = max($image_list_pageby, 15);
 181  
 182          list($page, $offset, $numPages) = pager($total, $limit, $page);
 183  
 184          echo image_search_form($crit, $search_method);
 185  
 186          $rs = safe_rows_start('*, unix_timestamp(date) as uDate', 'txp_image',
 187              "$criteria order by $sort_sql limit $offset, $limit
 188          ");
 189  
 190          echo pluggable_ui('image_ui', 'extend_controls', '', $rs);
 191          echo '</div>'; // end txp-control-panel
 192  
 193          if ($rs)
 194          {
 195              $show_authors = !has_single_author('txp_image');
 196  
 197              echo n.'<div id="'.$event.'_container" class="txp-container">';
 198              echo n.n.'<form name="longform" id="images_form" class="multi_edit_form" method="post" action="index.php">'.
 199  
 200                  n.'<div class="txp-listtables">'.
 201                  n.startTable('', '', 'txp-list').
 202                  n.'<thead>'.
 203                  n.tr(
 204                      n.hCell(fInput('checkbox', 'select_all', 0, '', '', '', '', '', 'select_all'), '', ' title="'.gTxt('toggle_all_selected').'" class="multi-edit"').
 205                      n.column_head('ID', 'id', 'image', true, $switch_dir, $crit, $search_method, (('id' == $sort) ? "$dir " : '').'id').
 206                      n.column_head('name', 'name', 'image', true, $switch_dir, $crit, $search_method, (('name' == $sort) ? "$dir " : '').'name').
 207                      n.column_head('date', 'date', 'image', true, $switch_dir, $crit, $search_method, (('date' == $sort) ? "$dir " : '').'images_detail date created').
 208                      n.column_head('thumbnail', 'thumbnail', 'image', true, $switch_dir, $crit, $search_method, (('thumbnail' == $sort) ? "$dir " : '').'thumbnail').
 209                      n.hCell(gTxt('tags'), '', ' class="images_detail tag-build"').
 210                      n.column_head('image_category', 'category', 'image', true, $switch_dir, $crit, $search_method, (('category' == $sort) ? "$dir " : '').'category').
 211                      ($show_authors ? n.column_head('author', 'author', 'image', true, $switch_dir, $crit, $search_method, (('author' == $sort) ? "$dir " : '').'author') : '')
 212                  ).
 213                  n.'</thead>';
 214  
 215              echo '<tbody>';
 216  
 217              $validator = new Validator();
 218  
 219              while ($a = nextRow($rs))
 220              {
 221                  extract($a);
 222  
 223                  $edit_url = '?event=image'.a.'step=image_edit'.a.'id='.$id.a.'sort='.$sort.
 224                      a.'dir='.$dir.a.'page='.$page.a.'search_method='.$search_method.a.'crit='.$crit;
 225  
 226                  $name = empty($name) ? gTxt('unnamed') : txpspecialchars($name);
 227  
 228                  if ($thumbnail) {
 229                      if ($ext != '.swf') {
 230                          $thumbnail = '<img class="content-image" src="'.imagesrcurl($id, $ext, true)."?$uDate".'" alt="" '.
 231                                              "title='$id$ext ($w &#215; $h)'".
 232                                              ($thumb_w ? " width='$thumb_w' height='$thumb_h'" : ''). ' />';
 233                      } else {
 234                          $thumbnail = '';
 235                      }
 236                  } else {
 237                      $thumbnail = gTxt('no');
 238                  }
 239  
 240                  if ($ext != '.swf') {
 241                      $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);
 242                      $tagbuilder = '<a target="_blank" href="'.$tag_url.a.'type=textile" onclick="popWin(this.href); return false;">Textile</a>'.sp.
 243                              '&#124;'.sp.'<a target="_blank" href="'.$tag_url.a.'type=textpattern" onclick="popWin(this.href); return false;">Textpattern</a>'.sp.
 244                              '&#124;'.sp.'<a target="_blank" href="'.$tag_url.a.'type=html" onclick="popWin(this.href); return false;">HTML</a>';
 245                  } else {
 246                      $tagbuilder = sp;
 247                  }
 248  
 249                  $validator->setConstraints(array(new CategoryConstraint($category, array('type' => 'image'))));
 250                  $vc = $validator->validate() ? '' : ' error';
 251                  $category = ($category) ? '<span title="'.txpspecialchars(fetch_category_title($category, 'image')).'">'.$category.'</span>' : '';
 252  
 253                  $can_edit = has_privs('image.edit') || ($author == $txp_user && has_privs('image.edit.own'));
 254  
 255                  echo n.n.tr(
 256                      n.td($can_edit ? fInput('checkbox', 'selected[]', $id) : '&#160;'
 257                      , '', 'multi-edit').
 258  
 259                      n.td(
 260                          ($can_edit ? href($id, $edit_url, ' title="'.gTxt('edit').'"') : $id).sp.
 261                          '<span class="images_detail">[<a href="'.imagesrcurl($id, $ext).'">'.gTxt('view').'</a>]</span>'
 262                      , '', 'id').
 263  
 264                      td(
 265                          ($can_edit ? href($name, $edit_url, ' title="'.gTxt('edit').'"') : $name)
 266                      , '', 'name').
 267  
 268                      td(
 269                          gTime($uDate)
 270                      , '', 'images_detail date created').
 271  
 272                      td(
 273                          pluggable_ui('image_ui', 'thumbnail',
 274                          ($can_edit ? href($thumbnail, $edit_url) : $thumbnail)
 275                          , $a)
 276                      , '', 'thumbnail').
 277  
 278                      td($tagbuilder, '', 'images_detail tag-build').
 279                      td($category, '', 'category'.$vc).
 280  
 281                      ($show_authors ? td(
 282                          '<span title="'.txpspecialchars(get_author_name($author)).'">'.txpspecialchars($author).'</span>'
 283                      , '', 'author') : '')
 284                  );
 285              }
 286  
 287              echo '</tbody>',
 288                  n, endTable(),
 289                  n, '</div>',
 290                  n, image_multiedit_form($page, $sort, $dir, $crit, $search_method),
 291                  n, tInput(),
 292                  n, '</form>',
 293                  n, graf(
 294                      toggle_box('images_detail'),
 295                      ' class="detail-toggle"'
 296                  ),
 297                  n, '<div id="'.$event.'_navigation" class="txp-navigation">',
 298                  n, nav_form('image', $page, $numPages, $sort, $dir, $crit, $search_method, $total, $limit),
 299                  n, pageby_form('image', $image_list_pageby),
 300                  n, '</div>',
 301                  n, '</div>';
 302          }
 303      }
 304  
 305  // -------------------------------------------------------------
 306  
 307  	function image_search_form($crit, $method)
 308      {
 309          $methods =    array(
 310              'id'       => gTxt('ID'),
 311              'name'     => gTxt('name'),
 312              'category' => gTxt('image_category'),
 313              'author'   => gTxt('author'),
 314              'alt'      => gTxt('alt_text'),
 315              'caption'  => gTxt('caption')
 316          );
 317  
 318          return search_form('image', 'image_list', $crit, $methods, $method, 'name');
 319      }
 320  
 321  // -------------------------------------------------------------
 322  
 323  	function image_multiedit_form($page, $sort, $dir, $crit, $search_method)
 324      {
 325          global $all_image_cats, $all_image_authors;
 326  
 327          $categories = $all_image_cats ? treeSelectInput('category', $all_image_cats, '') : '';
 328          $authors = $all_image_authors ? selectInput('author', $all_image_authors, '', true) : '';
 329  
 330          $methods = array(
 331              'changecategory' => array('label' => gTxt('changecategory'), 'html' => $categories),
 332              'changeauthor'   => array('label' => gTxt('changeauthor'), 'html' => $authors),
 333              'delete'         => gTxt('delete'),
 334          );
 335  
 336          if (!$categories)
 337          {
 338              unset($methods['changecategory']);
 339          }
 340  
 341          if (has_single_author('txp_image'))
 342          {
 343              unset($methods['changeauthor']);
 344          }
 345  
 346          if (!has_privs('image.delete.own') && !has_privs('image.delete'))
 347          {
 348              unset($methods['delete']);
 349          }
 350  
 351          return multi_edit($methods, 'image', 'image_multi_edit', $page, $sort, $dir, $crit, $search_method);
 352      }
 353  
 354  // -------------------------------------------------------------
 355  
 356  	function image_multi_edit()
 357      {
 358          global $txp_user, $all_image_cats, $all_image_authors;
 359  
 360          // Empty entry to permit clearing the category
 361          $categories = array('');
 362  
 363          foreach ($all_image_cats as $row) {
 364              $categories[] = $row['name'];
 365          }
 366  
 367          $selected = ps('selected');
 368  
 369          if (!$selected or !is_array($selected))
 370          {
 371              return image_list();
 372          }
 373  
 374          $selected = array_map('assert_int', $selected);
 375          $method   = ps('edit_method');
 376          $changed  = array();
 377          $key = '';
 378  
 379          switch ($method)
 380          {
 381              case 'delete':
 382                  return image_delete($selected);
 383                  break;
 384  
 385              case 'changecategory':
 386                  $val = ps('category');
 387                  if (in_array($val, $categories))
 388                  {
 389                      $key = 'category';
 390                  }
 391                  break;
 392  
 393              case 'changeauthor':
 394                  $val = ps('author');
 395                  if (in_array($val, $all_image_authors))
 396                  {
 397                      $key = 'author';
 398                  }
 399                  break;
 400  
 401              default:
 402                  $key = '';
 403                  $val = '';
 404                  break;
 405          }
 406  
 407          if (!has_privs('image.edit'))
 408          {
 409              if (has_privs('image.edit.own'))
 410              {
 411                  $selected = safe_column('id', 'txp_image', 'id IN ('.join(',', $selected).') AND author=\''.doSlash($txp_user).'\'');
 412              }
 413              else
 414              {
 415                  $selected = array();
 416              }
 417          }
 418  
 419          if ($selected and $key)
 420          {
 421              foreach ($selected as $id)
 422              {
 423                  if (safe_update('txp_image', "$key = '".doSlash($val)."'", "id = $id"))
 424                  {
 425                      $changed[] = $id;
 426                  }
 427              }
 428          }
 429  
 430          if ($changed)
 431          {
 432              update_lastmod();
 433  
 434              return image_list(gTxt('image_updated', array('{name}' => join(', ', $changed))));
 435          }
 436  
 437          return image_list();
 438      }
 439  
 440  // -------------------------------------------------------------
 441  	function image_edit($message='',$id='')
 442      {
 443          global $prefs, $file_max_upload_size, $txp_user, $event, $all_image_cats;
 444  
 445          if (!$id) $id = gps('id');
 446          $id = assert_int($id);
 447  
 448          $rs = safe_row("*, unix_timestamp(date) as uDate", "txp_image", "id = $id");
 449  
 450          if ($rs) {
 451              extract($rs);
 452  
 453              if (!has_privs('image.edit') && !($author == $txp_user && has_privs('image.edit.own')))
 454              {
 455                  image_list(gTxt('restricted_area'));
 456                  return;
 457              }
 458  
 459              pagetop(gTxt('edit_image'),$message);
 460  
 461              extract(gpsa(array('page', 'sort', 'dir', 'crit', 'search_method')));
 462  
 463              if ($ext != '.swf') {
 464                  $aspect = ($h == $w) ? ' square' : (($h > $w) ? ' portrait' : ' landscape');
 465                  $img_info = $id.$ext.' ('.$w.' &#215; '.$h.')';
 466                  $img = '<div class="fullsize-image"><img class="content-image" src="'.imagesrcurl($id, $ext)."?$uDate".'" alt="'.$img_info.'" title="'.$img_info.'" /></div>';
 467              } else {
 468                  $img = $aspect = '';
 469              }
 470  
 471              if ($thumbnail and ($ext != '.swf')) {
 472                  $thumb_info = $id.'t'.$ext.' ('.$thumb_w.' &#215; '.$thumb_h.')';
 473                  $thumb = '<img class="content-image" src="'.imagesrcurl($id, $ext, true)."?$uDate".'" alt="'.$thumb_info.'" '.
 474                              ($thumb_w ? 'width="'.$thumb_w.'" height="'.$thumb_h.'" title="'.$thumb_info.'"' : ''). ' />';
 475              } else {
 476                  $thumb = '';
 477                  if ($thumb_w == 0) {
 478                      $thumb_w = get_pref('thumb_w', 0);
 479                  }
 480                  if ($thumb_h == 0) {
 481                      $thumb_h = get_pref('thumb_h', 0);
 482                  }
 483              }
 484  
 485              echo n.'<div id="'.$event.'_container" class="txp-container">';
 486              echo
 487                  pluggable_ui(
 488                      'image_ui',
 489                      'fullsize_image',
 490                      $img,
 491                      $rs
 492                  ),
 493  
 494                  '<div class="txp-edit">',
 495                  hed(gTxt('edit_image'), 2),
 496  
 497                  pluggable_ui(
 498                      'image_ui',
 499                      'image_edit',
 500                      '<div class="summary-details replace-image">'.n.
 501                          '<h3>'.gTxt('replace_image').sp.popHelp('replace_image_form').'</h3>'.n.
 502                          '<div>'.n.
 503                              upload_form('', '', 'image_replace', 'image', $id, $file_max_upload_size, 'image_replace', 'image-replace').n.
 504                          '</div>'.n.
 505                      '</div>'.n,
 506                      $rs
 507                  ),
 508  
 509                  pluggable_ui(
 510                      'image_ui',
 511                      'thumbnail_image',
 512                      '<div class="thumbnail-edit">'.
 513                      (($thumbnail)
 514                          ? $thumb.n.dLink('image','thumbnail_delete','id',$id, '', '', '', '', array($page, $sort, $dir, $crit, $search_method))
 515                          :     '').
 516                      '</div>',
 517                      $rs
 518                  ),
 519  
 520                  pluggable_ui(
 521                      'image_ui',
 522                      'thumbnail_edit',
 523                      '<div class="summary-details thumbnail-upload">'.n.
 524                          '<h3>'.gTxt('upload_thumbnail').sp.popHelp('upload_thumbnail').'</h3>'.n.
 525                          '<div>'.n.
 526                              upload_form('', '', 'thumbnail_insert','image', $id, $file_max_upload_size, 'upload_thumbnail', 'thumbnail-upload').n.
 527                          '</div>'.n.
 528                      '</div>'.n,
 529                      $rs
 530                  ),
 531  
 532                  (check_gd($ext))
 533                  ? pluggable_ui(
 534                      'image_ui',
 535                      'thumbnail_create',
 536                      '<div class="summary-details thumbnail-alter">'.n.
 537                          '<h3>'.gTxt('create_thumbnail').sp.popHelp('create_thumbnail').'</h3>'.n.
 538                          '<div>'.n.
 539                              form(
 540                                  graf(
 541                                      '<label for="width">'.gTxt('thumb_width').'</label>'.n.
 542                                      fInput('text', 'width', @$thumb_w, 'input-xsmall', '', '', INPUT_XSMALL, '', 'width').n.
 543                                      '<label for="height">'.gTxt('thumb_height').'</label>'.n.
 544                                      fInput('text', 'height', @$thumb_h, 'input-xsmall', '', '', INPUT_XSMALL, '', 'height').n.
 545                                      '<label for="crop">'.gTxt('keep_square_pixels').'</label>'.n.
 546                                      checkbox('crop', 1, @$prefs['thumb_crop'], '', 'crop').n.
 547                                      fInput('submit', '', gTxt('Create'))
 548                                  , ' class="edit-alter-thumbnail"').n.
 549                                  n.hInput('id', $id).n.
 550                                  n.eInput('image').n.
 551                                  n.sInput('thumbnail_create').n.
 552                                  n.hInput('sort', $sort).n.
 553                                  n.hInput('dir', $dir).n.
 554                                  n.hInput('page', $page).n.
 555                                  n.hInput('search_method', $search_method).n.
 556                                  n.hInput('crit', $crit)
 557                              , '', '', 'post', 'edit-form', '', 'thumbnail_alter_form').n.
 558                          '</div>'.n.
 559                      '</div>'.n,
 560                      $rs
 561                  )
 562                  : '',
 563  
 564                  '<div class="image-detail">',
 565                      form(
 566                          inputLabel('image_name', fInput('text', 'name', $name, '', '', '', INPUT_REGULAR, '', 'image_name'), 'image_name').n.
 567                          inputLabel('image_category', treeSelectInput('category', $all_image_cats, $category, 'image_category'), 'image_category').n.
 568                          inputLabel('image_alt_text', fInput('text', 'alt', $alt, '', '', '', INPUT_REGULAR, '', 'image_alt_text'), 'alt_text').n.
 569                          inputLabel('image_caption', '<textarea id="image_caption" name="caption" rows="'.INPUT_XSMALL.'" cols="'.INPUT_LARGE.'">'.$caption.'</textarea>', 'caption', '', '', '').n.
 570                          pluggable_ui('image_ui', 'extend_detail_form', '', $rs).n.
 571                          graf(fInput('submit', '', gTxt('save'), 'publish')).
 572                          n.hInput('id', $id).
 573                          n.eInput('image').
 574                          n.sInput('image_save').
 575                          n.hInput('sort', $sort).
 576                          n.hInput('dir', $dir).
 577                          n.hInput('page', $page).
 578                          n.hInput('search_method', $search_method).
 579                          n.hInput('crit', $crit)
 580                      , '', '', 'post', 'edit-form', '', 'image_details_form'),
 581                  '</div>',
 582              '</div>'.n.'</div>';
 583          }
 584      }
 585  
 586  // -------------------------------------------------------------
 587  	function image_insert()
 588      {
 589          global $txpcfg, $extensions, $txp_user;
 590  
 591          if (!has_privs('image.edit.own'))
 592          {
 593              image_list(gTxt('restricted_area'));
 594              return;
 595          }
 596  
 597          extract($txpcfg);
 598  
 599          $meta = gpsa(array('caption', 'alt', 'category'));
 600  
 601          $img_result = image_data($_FILES['thefile'], $meta);
 602  
 603          if (is_array($img_result))
 604          {
 605              list($message, $id) = $img_result;
 606  
 607              return image_edit($message, $id);
 608          }
 609  
 610          else
 611          {
 612              return image_list(array($img_result, E_ERROR));
 613          }
 614      }
 615  
 616  // -------------------------------------------------------------
 617  	function image_replace()
 618      {
 619          global $txpcfg,$extensions,$txp_user;
 620          extract($txpcfg);
 621  
 622          $id = assert_int(gps('id'));
 623          $rs = safe_row("*", "txp_image", "id = $id");
 624  
 625          if (!has_privs('image.edit') && !($rs['author'] == $txp_user && has_privs('image.edit.own')))
 626          {
 627              image_list(gTxt('restricted_area'));
 628              return;
 629          }
 630  
 631          if ($rs) {
 632              $meta = array('category' => $rs['category'], 'caption' => $rs['caption'], 'alt' => $rs['alt']);
 633          } else {
 634              $meta = '';
 635          }
 636  
 637          $img_result = image_data($_FILES['thefile'], $meta, $id);
 638  
 639          if(is_array($img_result))
 640          {
 641              list($message, $id) = $img_result;
 642              return image_edit($message, $id);
 643          }else{
 644              return image_edit(array($img_result, E_ERROR), $id);
 645          }
 646      }
 647  
 648  // -------------------------------------------------------------
 649  	function thumbnail_insert()
 650      {
 651          global $txpcfg,$extensions,$txp_user,$img_dir,$path_to_site;
 652          extract($txpcfg);
 653          $id = assert_int(gps('id'));
 654  
 655          $author = fetch('author', 'txp_image', 'id', $id);
 656          if (!has_privs('image.edit') && !($author == $txp_user && has_privs('image.edit.own')))
 657          {
 658              image_list(gTxt('restricted_area'));
 659              return;
 660          }
 661  
 662          $file = $_FILES['thefile']['tmp_name'];
 663          $name = $_FILES['thefile']['name'];
 664  
 665          $file = get_uploaded_file($file);
 666  
 667          if (empty($file))
 668          {
 669              image_edit(array(upload_get_errormsg(UPLOAD_ERR_NO_FILE), E_ERROR), $id);
 670              return;
 671          }
 672  
 673          list($w, $h, $extension) = getimagesize($file);
 674  
 675          if (($file !== false) && @$extensions[$extension]) {
 676              $ext = $extensions[$extension];
 677              $newpath = IMPATH.$id.'t'.$ext;
 678  
 679              if (shift_uploaded_file($file, $newpath) == false) {
 680                  image_list(array($newpath.sp.gTxt('upload_dir_perms'), E_ERROR));
 681              } else {
 682                  chmod($newpath, 0644);
 683                  safe_update("txp_image", "thumbnail = 1, thumb_w = $w, thumb_h = $h, date = now()", "id = $id");
 684  
 685                  $message = gTxt('image_uploaded', array('{name}' => $name));
 686                  update_lastmod();
 687  
 688                  image_edit($message, $id);
 689              }
 690          } else {
 691              if ($file === false)
 692                  image_list(array(upload_get_errormsg($_FILES['thefile']['error']), E_ERROR));
 693              else
 694                  image_list(array(gTxt('only_graphic_files_allowed'), E_ERROR));
 695          }
 696      }
 697  
 698  
 699  // -------------------------------------------------------------
 700  	function image_save()
 701      {
 702          global $txp_user;
 703  
 704          $varray = array_map('assert_string', gpsa(array('id', 'name', 'category', 'caption', 'alt')));
 705          extract(doSlash($varray));
 706          $id = $varray['id'] = assert_int($id);
 707  
 708          $author = fetch('author', 'txp_image', 'id', $id);
 709          if (!has_privs('image.edit') && !($author == $txp_user && has_privs('image.edit.own')))
 710          {
 711              image_list(gTxt('restricted_area'));
 712              return;
 713          }
 714  
 715          $constraints = array(
 716              'category' => new CategoryConstraint(gps('category'), array('type' => 'image')),
 717          );
 718          callback_event_ref('image_ui', 'validate_save', 0, $varray, $constraints);
 719          $validator = new Validator($constraints);
 720  
 721          if ($validator->validate() && safe_update(
 722              "txp_image",
 723              "name    = '$name',
 724              category = '$category',
 725              alt      = '$alt',
 726              caption  = '$caption'",
 727              "id = $id"
 728          ))
 729          {
 730              $message = gTxt('image_updated', array('{name}' => doStrip($name)));
 731              update_lastmod();
 732          }
 733          else
 734          {
 735              $message = array(gTxt('image_save_failed'), E_ERROR);
 736          }
 737  
 738          image_list($message);
 739      }
 740  
 741  // -------------------------------------------------------------
 742  
 743  	function image_delete($ids = array())
 744      {
 745          global $txp_user, $event;
 746  
 747          $ids = $ids ? array_map('assert_int', $ids) : array(assert_int(ps('id')));
 748          $message = '';
 749  
 750          if (!has_privs('image.delete'))
 751          {
 752              if (has_privs('image.delete.own'))
 753              {
 754                  $ids = safe_column('id', 'txp_image', 'id IN ('.join(',', $ids).') AND author=\''.doSlash($txp_user).'\'' );
 755              }
 756              else
 757              {
 758                  $ids = array();
 759              }
 760          }
 761  
 762          if (!empty($ids))
 763          {
 764              $fail = array();
 765  
 766              $rs   = safe_rows_start('id, ext', 'txp_image', 'id IN ('.join(',', $ids).')');
 767  
 768              if ($rs)
 769              {
 770                  while ($a = nextRow($rs))
 771                  {
 772                      extract($a);
 773  
 774                      // notify plugins of pending deletion, pass image's $id
 775                      callback_event('image_deleted', $event, false, $id);
 776  
 777                      $rsd = safe_delete('txp_image', "id = $id");
 778  
 779                      $ul  = false;
 780  
 781                      if (is_file(IMPATH.$id.$ext))
 782                      {
 783                          $ul = unlink(IMPATH.$id.$ext);
 784                      }
 785  
 786                      if (is_file(IMPATH.$id.'t'.$ext))
 787                      {
 788                          $ult = unlink(IMPATH.$id.'t'.$ext);
 789                      }
 790  
 791                      if (!$rsd or !$ul)
 792                      {
 793                          $fail[] = $id;
 794                      }
 795                  }
 796  
 797                  if ($fail)
 798                  {
 799                      $message = array(gTxt('image_delete_failed', array('{name}' => join(', ', $fail))), E_ERROR);
 800                  }
 801                  else
 802                  {
 803                      update_lastmod();
 804                      $message = gTxt('image_deleted', array('{name}' => join(', ', $ids)));
 805                  }
 806              }
 807          }
 808          image_list($message);
 809      }
 810  
 811  // -------------------------------------------------------------
 812  	function image_change_pageby()
 813      {
 814          event_change_pageby('image');
 815          image_list();
 816      }
 817  
 818  // -------------------------------------------------------------
 819  
 820  	function thumbnail_create()
 821      {
 822          global $prefs, $txp_user;
 823  
 824          extract(doSlash(gpsa(array('id', 'width', 'height'))));
 825          $id = assert_int($id);
 826  
 827          $author = fetch('author', 'txp_image', 'id', $id);
 828          if (!has_privs('image.edit') && !($author == $txp_user && has_privs('image.edit.own')))
 829          {
 830              image_list(gTxt('restricted_area'));
 831              return;
 832          }
 833  
 834          $width = (int) $width;
 835          $height = (int) $height;
 836  
 837          if ($width == 0) $width = '';
 838          if ($height == 0) $height = '';
 839  
 840          $crop = gps('crop');
 841  
 842          $prefs['thumb_w'] = $width;
 843          $prefs['thumb_h'] = $height;
 844          $prefs['thumb_crop'] = $crop;
 845  
 846          // hidden prefs
 847          set_pref('thumb_w', $width, 'image', 2);
 848          set_pref('thumb_h', $height, 'image', 2);
 849          set_pref('thumb_crop', $crop, 'image', 2);
 850  
 851          if ($width === '' && $height === '')
 852          {
 853              image_edit(array(gTxt('invalid_width_or_height'), E_ERROR), $id);
 854              return;
 855          }
 856  
 857          $t = new txp_thumb( $id );
 858          $t->crop = ($crop == '1');
 859          $t->hint = '0';
 860  
 861          $t->width = $width;
 862          $t->height = $height;
 863  
 864          if ($t->write())
 865          {
 866              $message = gTxt('thumbnail_saved', array('{id}' => $id));
 867              update_lastmod();
 868  
 869              image_edit($message, $id);
 870          }
 871  
 872          else
 873          {
 874              $message = array(gTxt('thumbnail_not_saved', array('{id}' => $id)), E_ERROR);
 875  
 876              image_edit($message, $id);
 877          }
 878      }
 879  
 880  // -------------------------------------------------------------
 881  	function thumbnail_delete()
 882      {
 883          global $txp_user;
 884  
 885          $id = assert_int(gps('id'));
 886  
 887          $author = fetch('author', 'txp_image', 'id', $id);
 888          if (!has_privs('image.edit') && !($author == $txp_user && has_privs('image.edit.own')))
 889          {
 890              image_list(gTxt('restricted_area'));
 891              return;
 892          }
 893  
 894          $t = new txp_thumb($id);
 895          if ($t->delete()) {
 896              callback_event('thumbnail_deleted', '', false, $id);
 897              update_lastmod();
 898              image_edit(gTxt('thumbnail_deleted'),$id);
 899          } else {
 900              image_edit(array(gTxt('thumbnail_delete_failed'), E_ERROR),$id);
 901          }
 902      }
 903  
 904  // -------------------------------------------------------------
 905  // Refactoring attempt, allowing other - plugin - functions to
 906  // upload images without the need for writing duplicated code.
 907  
 908  	function image_data($file , $meta = '', $id = '', $uploaded = true)
 909      {
 910          global $txpcfg, $extensions, $txp_user, $prefs, $file_max_upload_size, $event;
 911  
 912          extract($txpcfg);
 913  
 914          $name = $file['name'];
 915          $error = $file['error'];
 916          $file = $file['tmp_name'];
 917  
 918          if ($uploaded)
 919          {
 920              $file = get_uploaded_file($file);
 921  
 922              if ($file_max_upload_size < filesize($file))
 923              {
 924                  unlink($file);
 925  
 926                  return upload_get_errormsg(UPLOAD_ERR_FORM_SIZE);
 927              }
 928          }
 929  
 930          if (empty($file))
 931          {
 932              return upload_get_errormsg(UPLOAD_ERR_NO_FILE);
 933          }
 934  
 935          list($w, $h, $extension) = getimagesize($file);
 936  
 937          if (($file !== false) && @$extensions[$extension])
 938          {
 939              $ext = $extensions[$extension];
 940  
 941              $name = substr($name, 0, strrpos($name, '.')).$ext;
 942              $safename = doSlash($name);
 943  
 944              if ($meta == false)
 945              {
 946                  $meta = array('category' => '', 'caption' => '', 'alt' => '');
 947              }
 948  
 949              extract(doSlash($meta));
 950  
 951              $q ="
 952                  name = '$safename',
 953                  ext = '$ext',
 954                  w = $w,
 955                  h = $h,
 956                  alt = '$alt',
 957                  caption = '$caption',
 958                  category = '$category',
 959                  date = now(),
 960                  author = '".doSlash($txp_user)."'
 961              ";
 962  
 963              if (empty($id))
 964              {
 965                  $rs = safe_insert('txp_image', $q);
 966                  if ($rs)
 967                  {
 968                      $id = $GLOBALS['ID'] = $rs;
 969                  }
 970              }
 971              else
 972              {
 973                  $id = assert_int($id);
 974  
 975                  $rs = safe_update('txp_image', $q, "id = $id");
 976              }
 977  
 978              if (!$rs)
 979              {
 980                  return gTxt('image_save_error');
 981              }
 982  
 983              else
 984              {
 985                  $newpath = IMPATH.$id.$ext;
 986  
 987                  if (shift_uploaded_file($file, $newpath) == false)
 988                  {
 989                      $id = assert_int($id);
 990  
 991                      safe_delete('txp_image', "id = $id");
 992  
 993                      safe_alter('txp_image', "auto_increment = $id");
 994  
 995                      if (isset($GLOBALS['ID']))
 996                      {
 997                          unset( $GLOBALS['ID']);
 998                      }
 999  
1000                      return $newpath.sp.gTxt('upload_dir_perms');
1001                  }
1002  
1003                  else
1004                  {
1005                      @chmod($newpath, 0644);
1006  
1007                      // GD is supported
1008                      if (check_gd($ext))
1009                      {
1010                          // Auto-generate a thumbnail using the last settings
1011                          if (isset($prefs['thumb_w'], $prefs['thumb_h'], $prefs['thumb_crop']))
1012                          {
1013                              $width  = intval($prefs['thumb_w']);
1014                              $height = intval($prefs['thumb_h']);
1015  
1016                              if ($width > 0 or $height > 0)
1017                              {
1018                                  $t = new txp_thumb( $id );
1019  
1020                                  $t->crop = ($prefs['thumb_crop'] == '1');
1021                                  $t->hint = '0';
1022                                  $t->width = $width;
1023                                  $t->height = $height;
1024  
1025                                  $t->write();
1026                              }
1027                          }
1028                      }
1029  
1030                      $message = gTxt('image_uploaded', array('{name}' => $name));
1031                      update_lastmod();
1032  
1033                      // call post-upload plugins with new image's $id
1034                      callback_event('image_uploaded', $event, false, $id);
1035  
1036                      return array($message, $id);
1037                  }
1038              }
1039          }
1040  
1041          else
1042          {
1043              if ($file === false)
1044              {
1045                  return upload_get_errormsg($error);
1046              }
1047  
1048              else
1049              {
1050                  return gTxt('only_graphic_files_allowed');
1051              }
1052          }
1053      }
1054  
1055  // -------------------------------------------------------------
1056  // check GD info
1057  
1058  	function check_gd($image_type) {
1059          // GD is installed
1060          if (function_exists('gd_info')) {
1061              $gd_info = gd_info();
1062  
1063              switch ($image_type) {
1064                  // check gif support
1065                  case '.gif':
1066                      return ($gd_info['GIF Create Support'] == 1) ? true : false;
1067                  break;
1068  
1069                  // check png support
1070                  case '.png':
1071                      return ($gd_info['PNG Support'] == 1) ? true : false;
1072                  break;
1073  
1074                  // check jpg support
1075                  case '.jpg':
1076                      return (!empty($gd_info['JPEG Support']) || !empty($gd_info['JPG Support'])) ? true : false;
1077                  break;
1078  
1079                  // unsupported format
1080                  default:
1081                      return false;
1082                  break;
1083              }
1084          } else { // GD isn't installed
1085              return false;
1086          }
1087      }
1088  
1089  ?>

title

Description

title

Description

title

Description

title

title

Body