Textpattern PHP Cross Reference Content Management Systems

Source: /textpattern/include/txp_link.php - 568 lines - 15523 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_link.php $
  13  $LastChangedRevision: 4089 $
  14  
  15  */
  16  
  17      if (!defined('txpinterface')) die('txpinterface is undefined.');
  18  
  19      if ($event == 'link')
  20      {
  21          require_privs('link');
  22  
  23          global $vars;
  24          $vars = array('category', 'url', 'linkname', 'linksort', 'description', 'id');
  25  
  26          global $all_link_cats, $all_link_authors;
  27          $all_link_cats = getTree('root', 'link');
  28          $all_link_authors = the_privileged('link.edit.own');
  29  
  30          $available_steps = array(
  31              'link_list'          => false,
  32              'link_edit'          => false,
  33              'link_save'          => true,
  34              'link_change_pageby' => true,
  35              'link_multi_edit'    => true
  36          );
  37  
  38          if ($step && bouncer($step, $available_steps)) {
  39              $step();
  40          } else {
  41              link_list();
  42          }
  43      }
  44  
  45  // -------------------------------------------------------------
  46  
  47  	function link_list($message = '')
  48      {
  49          global $event,$step, $link_list_pageby, $txp_user;
  50  
  51          pagetop(gTxt('tab_link'), $message);
  52  
  53          extract(gpsa(array('page', 'sort', 'dir', 'crit', 'search_method')));
  54          if ($sort === '') $sort = get_pref('link_sort_column', 'name');
  55          if ($dir === '') $dir = get_pref('link_sort_dir', 'asc');
  56          $dir = ($dir == 'desc') ? 'desc' : 'asc';
  57  
  58          switch ($sort)
  59          {
  60              case 'id':
  61                  $sort_sql = 'id '.$dir;
  62              break;
  63  
  64              case 'description':
  65                  $sort_sql = 'description '.$dir.', id asc';
  66              break;
  67  
  68              case 'url':
  69                  $sort_sql = 'url '.$dir.', id asc';
  70              break;
  71  
  72              case 'category':
  73                  $sort_sql = 'category '.$dir.', id asc';
  74              break;
  75  
  76              case 'date':
  77                  $sort_sql = 'date '.$dir.', id asc';
  78              break;
  79  
  80              case 'author':
  81                  $sort_sql = 'author '.$dir.', id asc';
  82              break;
  83  
  84              default:
  85                  $sort = 'name';
  86                  $sort_sql = 'linksort '.$dir.', id asc';
  87              break;
  88          }
  89  
  90          set_pref('link_sort_column', $sort, 'link', 2, '', 0, PREF_PRIVATE);
  91          set_pref('link_sort_dir', $dir, 'link', 2, '', 0, PREF_PRIVATE);
  92  
  93          $switch_dir = ($dir == 'desc') ? 'asc' : 'desc';
  94  
  95          $criteria = 1;
  96  
  97          if ($search_method and $crit != '')
  98          {
  99              $verbatim = preg_match('/^"(.*)"$/', $crit, $m);
 100              $crit_escaped = doSlash($verbatim ? $m[1] : str_replace(array('\\','%','_','\''), array('\\\\','\\%','\\_', '\\\''), $crit));
 101              $critsql = $verbatim ?
 102                  array(
 103                      'id'          => "ID in ('" .join("','", do_list($crit_escaped)). "')",
 104                      'name'        => "linkname = '$crit_escaped'",
 105                      'description' => "description = '$crit_escaped'",
 106                      'url'         => "url = '$crit_escaped'",
 107                      'category'    => "category = '$crit_escaped'",
 108                      'author'      => "author = '$crit_escaped'"
 109                  ) : array(
 110                      'id'          => "ID in ('" .join("','", do_list($crit_escaped)). "')",
 111                      'name'        => "linkname like '%$crit_escaped%'",
 112                      'description' => "description like '%$crit_escaped%'",
 113                      'url'         => "url like '%$crit_escaped%'",
 114                      'category'    => "category like '%$crit_escaped%'",
 115                      'author'      => "author like '%$crit_escaped%'"
 116                  );
 117  
 118              if (array_key_exists($search_method, $critsql))
 119              {
 120                  $criteria = $critsql[$search_method];
 121              }
 122  
 123              else
 124              {
 125                  $search_method = '';
 126                  $crit = '';
 127              }
 128          }
 129  
 130          else
 131          {
 132              $search_method = '';
 133              $crit = '';
 134          }
 135  
 136          $criteria .= callback_event('admin_criteria', 'link_list', 0, $criteria);
 137  
 138          $total = getCount('txp_link', $criteria);
 139  
 140          echo '<h1 class="txp-heading">'.gTxt('tab_link').'</h1>';
 141          echo '<div id="'.$event.'_control" class="txp-control-panel">';
 142          if (has_privs('link.edit'))
 143          {
 144              echo graf(
 145                  sLink('link', 'link_edit', gTxt('add_new_link'))
 146                  , ' class="txp-buttons"');
 147          }
 148  
 149          if ($total < 1)
 150          {
 151              if ($criteria != 1)
 152              {
 153                  echo n.link_search_form($crit, $search_method).
 154                      n.graf(gTxt('no_results_found'), ' class="indicator"').'</div>';
 155              }
 156  
 157              else
 158              {
 159                  echo n.graf(gTxt('no_links_recorded'), ' class="indicator"').'</div>';
 160              }
 161  
 162              return;
 163          }
 164  
 165          $limit = max($link_list_pageby, 15);
 166  
 167          list($page, $offset, $numPages) = pager($total, $limit, $page);
 168  
 169          echo link_search_form($crit, $search_method).'</div>';
 170  
 171          $rs = safe_rows_start('*, unix_timestamp(date) as uDate', 'txp_link', "$criteria order by $sort_sql limit $offset, $limit");
 172  
 173          if ($rs)
 174          {
 175              $show_authors = !has_single_author('txp_link');
 176  
 177              echo n.'<div id="'.$event.'_container" class="txp-container">';
 178              echo n.n.'<form action="index.php" id="links_form" class="multi_edit_form" method="post" name="longform">',
 179  
 180                  n.'<div class="txp-listtables">'.
 181                  n.startTable('', '', 'txp-list').
 182                  n.'<thead>'.
 183                  n.tr(
 184                      n.hCell(fInput('checkbox', 'select_all', 0, '', '', '', '', '', 'select_all'), '', ' title="'.gTxt('toggle_all_selected').'" class="multi-edit"').
 185                      n.column_head('ID', 'id', 'link', true, $switch_dir, $crit, $search_method, (('id' == $sort) ? "$dir " : '').'id').
 186                      n.column_head('link_name', 'name', 'link', true, $switch_dir, $crit, $search_method, (('name' == $sort) ? "$dir " : '').'name').
 187                      n.column_head('description', 'description', 'link', true, $switch_dir, $crit, $search_method, (('description' == $sort) ? "$dir " : '').'links_detail description').
 188                      n.column_head('link_category', 'category', 'link', true, $switch_dir, $crit, $search_method, (('category' == $sort) ? "$dir " : '').'category').
 189                      n.column_head('url', 'url', 'link', true, $switch_dir, $crit, $search_method, (('url' == $sort) ? "$dir " : '').'url').
 190                      n.column_head('date', 'date', 'link', true, $switch_dir, $crit, $search_method, (('date' == $sort) ? "$dir " : '').'links_detail date created').
 191                      ($show_authors ? n.column_head('author', 'author', 'link', true, $switch_dir, $crit, $search_method, (('author' == $sort) ? "$dir " : '').'author') : '')
 192                  ).
 193                  n.'</thead>';
 194  
 195              echo '<tbody>';
 196  
 197              $validator = new Validator();
 198  
 199              while ($a = nextRow($rs))
 200              {
 201                  extract($a, EXTR_PREFIX_ALL, 'link');
 202  
 203                  $edit_url = '?event=link'.a.'step=link_edit'.a.'id='.$link_id.a.'sort='.$sort.
 204                      a.'dir='.$dir.a.'page='.$page.a.'search_method='.$search_method.a.'crit='.$crit;
 205  
 206                  $validator->setConstraints(array(new CategoryConstraint($link_category, array('type' => 'link'))));
 207                  $vc = $validator->validate() ? '' : ' error';
 208  
 209                  $can_edit = has_privs('link.edit') || ($link_author == $txp_user && has_privs('link.edit.own'));
 210                  $view_url = txpspecialchars($link_url);
 211  
 212                  echo tr(
 213                      n.td(
 214                          fInput('checkbox', 'selected[]', $link_id)
 215                      , '', 'multi-edit').
 216  
 217                      n.td(
 218                          ($can_edit ? href($link_id, $edit_url, ' title="'.gTxt('edit').'"') : $link_id)
 219                      , '', 'id').
 220  
 221                      td(
 222                          ($can_edit ? href(txpspecialchars($link_linkname), $edit_url, ' title="'.gTxt('edit').'"') : txpspecialchars($link_linkname))
 223                      , '', 'name').
 224  
 225                      td(
 226                          txpspecialchars($link_description)
 227                      , '', 'links_detail description').
 228  
 229                      td(
 230                          '<span title="'.txpspecialchars(fetch_category_title($link_category, 'link')).'">'.$link_category.'</span>'
 231                      , '', 'category'.$vc).
 232  
 233                      td(
 234                          '<a rel="external" target="_blank" href="'.$view_url.'">'.$view_url.'</a>'
 235                      , '', 'url').
 236  
 237                      td(
 238                          gTime($link_uDate)
 239                      , '', 'links_detail date created').
 240  
 241                      ($show_authors ? td(
 242                          '<span title="'.txpspecialchars(get_author_name($link_author)).'">'.txpspecialchars($link_author).'</span>'
 243                      , '', 'author') : '')
 244                  );
 245              }
 246  
 247              echo '</tbody>',
 248                  n, endTable(),
 249                  n, '</div>',
 250                  n, link_multiedit_form($page, $sort, $dir, $crit, $search_method),
 251                  n, tInput(),
 252                  n, '</form>',
 253                  n, graf(
 254                      toggle_box('links_detail'),
 255                      ' class="detail-toggle"'
 256                  ),
 257                  n, '<div id="'.$event.'_navigation" class="txp-navigation">',
 258                  n, nav_form('link', $page, $numPages, $sort, $dir, $crit, $search_method, $total, $limit),
 259                  n, pageby_form('link', $link_list_pageby),
 260                  n, '</div>',
 261                  n, '</div>';
 262          }
 263      }
 264  
 265  // -------------------------------------------------------------
 266  
 267  	function link_search_form($crit, $method)
 268      {
 269          $methods =    array(
 270              'id'          => gTxt('ID'),
 271              'name'        => gTxt('link_name'),
 272              'description' => gTxt('description'),
 273              'url'         => gTxt('url'),
 274              'category'    => gTxt('link_category'),
 275              'author'      => gTxt('author')
 276          );
 277  
 278          return search_form('link', 'link_list', $crit, $methods, $method, 'name');
 279      }
 280  
 281  // -------------------------------------------------------------
 282  
 283  	function link_edit($message = '')
 284      {
 285          global $vars, $event, $step, $txp_user;
 286  
 287          pagetop(gTxt('tab_link'), $message);
 288  
 289          echo '<div id="'.$event.'_container" class="txp-container">';
 290  
 291          extract(array_map('assert_string', gpsa($vars)));
 292  
 293          $is_edit = ($id && $step == 'link_edit');
 294  
 295          $rs = array();
 296          if ($is_edit)
 297          {
 298              $id = assert_int($id);
 299              $rs = safe_row('*', 'txp_link', "id = $id");
 300              if ($rs)
 301              {
 302                  extract($rs);
 303                  if (!has_privs('link.edit') && !($author == $txp_user && has_privs('link.edit.own')))
 304                  {
 305                      link_list(gTxt('restricted_area'));
 306                      return;
 307                  }
 308              }
 309          }
 310  
 311          if (has_privs('link.edit') || has_privs('link.edit.own'))
 312          {
 313              $caption = gTxt(($is_edit) ? 'edit_link' : 'add_new_link');
 314  
 315              echo form(
 316                  '<div class="txp-edit">'.n.
 317                  hed($caption, 2).n.
 318                  inputLabel('linkname', fInput('text', 'linkname', $linkname, '', '', '', INPUT_REGULAR, '', 'linkname'), 'title').n.
 319                  inputLabel('linksort', fInput('text', 'linksort', $linksort, '', '', '', INPUT_REGULAR, '', 'linksort'), 'sort_value', 'link_sort').n.
 320                  inputLabel('url', fInput('text', 'url', $url, '', '', '', INPUT_REGULAR, '', 'url'), 'url', 'link_url', 'edit-link-url').n. /* TODO: type = 'url' once browsers are less strict and we use HTML5 doctype */
 321                  inputLabel('link_category', linkcategory_popup($category).' ['.eLink('category', 'list', '', '', gTxt('edit')).']', 'link_category', 'link_category').n.
 322                  inputLabel('link_description', '<textarea id="link_description" name="description" cols="'.INPUT_LARGE.'" rows="'.INPUT_SMALL.'">'.txpspecialchars($description).'</textarea>', 'description', 'link_description', '', '').n.
 323                  pluggable_ui('link_ui', 'extend_detail_form', '', $rs).n.
 324                  graf(fInput('submit', '', gTxt('save'), 'publish')).
 325                  eInput('link').
 326                  sInput('link_save').
 327                  hInput('id', $id).
 328                  hInput('search_method', gps('search_method')).
 329                  hInput('crit', gps('crit')).
 330                  '</div>'
 331              , '', '', 'post', 'edit-form', '', 'link_details');
 332          };
 333  
 334          echo '</div>';
 335      }
 336  
 337  //--------------------------------------------------------------
 338  
 339  	function linkcategory_popup($cat = '')
 340      {
 341          return event_category_popup('link', $cat, 'link_category');
 342      }
 343  
 344  // -------------------------------------------------------------
 345  	function link_save()
 346      {
 347          global $vars, $txp_user;
 348  
 349          $varray = array_map('assert_string', gpsa($vars));
 350          extract(doSlash($varray));
 351  
 352          if ($id)
 353          {
 354              $id = $varray['id'] = assert_int($id);
 355          }
 356  
 357          if ($linkname === '' && $url === '' && $description === '')
 358          {
 359              link_list(array(gTxt('link_empty'), E_ERROR));
 360              return;
 361          }
 362  
 363          $author = fetch('author', 'txp_link', 'id', $id);
 364          if (!has_privs('link.edit') && !($author == $txp_user && has_privs('link.edit.own')))
 365          {
 366              link_list(gTxt('restricted_area'));
 367              return;
 368          }
 369  
 370          if (!$linksort) $linksort = $linkname;
 371  
 372          $constraints = array(
 373              'category' => new CategoryConstraint($varray['category'], array('type' => 'link'))
 374          );
 375  
 376          callback_event_ref('link_ui', 'validate_save', 0, $varray, $constraints);
 377          $validator = new Validator($constraints);
 378  
 379          if ($validator->validate()) {
 380              if ($id)
 381              {
 382                  $ok = safe_update('txp_link',
 383                      "category   = '$category',
 384                      url         = '".trim($url)."',
 385                      linkname    = '$linkname',
 386                      linksort    = '$linksort',
 387                      description = '$description',
 388                      author      = '".doSlash($txp_user)."'",
 389                      "id = $id"
 390                  );
 391              }
 392              else
 393              {
 394                  $ok = safe_insert('txp_link',
 395                      "category   = '$category',
 396                      date        = now(),
 397                      url         = '".trim($url)."',
 398                      linkname    = '$linkname',
 399                      linksort    = '$linksort',
 400                      description = '$description',
 401                      author      = '".doSlash($txp_user)."'"
 402                  );
 403                  if ($ok) {
 404                      $GLOBALS['ID'] = $_POST['id'] = $ok;
 405                  }
 406              }
 407  
 408              if ($ok) {
 409                  // update lastmod due to link feeds
 410                  update_lastmod();
 411                  $message = gTxt(($id ? 'link_updated' : 'link_created'), array('{name}' => doStrip($linkname)));
 412              }
 413              else
 414              {
 415                  $message = array(gTxt('link_save_failed'), E_ERROR);
 416              }
 417          }
 418          else
 419          {
 420              $message = array(gTxt('link_save_failed'), E_ERROR);
 421          }
 422  
 423          link_list($message);
 424      }
 425  
 426  // -------------------------------------------------------------
 427  	function link_change_pageby()
 428      {
 429          event_change_pageby('link');
 430          link_list();
 431      }
 432  
 433  // -------------------------------------------------------------
 434  
 435  	function link_multiedit_form($page, $sort, $dir, $crit, $search_method)
 436      {
 437          global $all_link_cats, $all_link_authors;
 438  
 439          $categories = $all_link_cats ? treeSelectInput('category', $all_link_cats, '') : '';
 440          $authors = $all_link_authors ? selectInput('author', $all_link_authors, '', true) : '';
 441  
 442          $methods = array(
 443              'changecategory' => array('label' => gTxt('changecategory'), 'html' => $categories),
 444              'changeauthor'   => array('label' => gTxt('changeauthor'), 'html' => $authors),
 445              'delete'         => gTxt('delete'),
 446          );
 447  
 448          if (!$categories)
 449          {
 450              unset($methods['changecategory']);
 451          }
 452  
 453          if (has_single_author('txp_link'))
 454          {
 455              unset($methods['changeauthor']);
 456          }
 457  
 458          if (!has_privs('link.delete.own') && !has_privs('link.delete'))
 459          {
 460              unset($methods['delete']);
 461          }
 462  
 463          return multi_edit($methods, 'link', 'link_multi_edit', $page, $sort, $dir, $crit, $search_method);
 464      }
 465  
 466  // -------------------------------------------------------------
 467  
 468  	function link_multi_edit()
 469      {
 470          global $txp_user, $all_link_cats, $all_link_authors;
 471  
 472          // Empty entry to permit clearing the category
 473          $categories = array('');
 474  
 475          foreach ($all_link_cats as $row) {
 476              $categories[] = $row['name'];
 477          }
 478  
 479          $selected = ps('selected');
 480  
 481          if (!$selected or !is_array($selected))
 482          {
 483              link_list();
 484              return;
 485          }
 486  
 487          $selected = array_map('assert_int', $selected);
 488          $method   = ps('edit_method');
 489          $changed  = array();
 490          $key = '';
 491  
 492          switch ($method)
 493          {
 494              case 'delete':
 495                  if (!has_privs('link.delete'))
 496                  {
 497                      if (has_privs('link.delete.own'))
 498                      {
 499                          $selected = safe_column('id', 'txp_link', 'id IN ('.join(',', $selected).') AND author=\''.doSlash($txp_user).'\'' );
 500                      }
 501                      else
 502                      {
 503                          $selected = array();
 504                      }
 505                  }
 506                  foreach ($selected as $id)
 507                  {
 508                      if (safe_delete('txp_link', 'id = '.$id))
 509                      {
 510                          $changed[] = $id;
 511                      }
 512                  }
 513  
 514                  if ($changed)
 515                  {
 516                      callback_event('links_deleted', '', 0, $changed);
 517                  }
 518  
 519                  $key = '';
 520                  break;
 521  
 522              case 'changecategory':
 523                  $val = ps('category');
 524                  if (in_array($val, $categories))
 525                  {
 526                      $key = 'category';
 527                  }
 528                  break;
 529  
 530              case 'changeauthor':
 531                  $val = ps('author');
 532                  if (in_array($val, $all_link_authors))
 533                  {
 534                      $key = 'author';
 535                  }
 536                  break;
 537  
 538              default:
 539                  $key = '';
 540                  $val = '';
 541                  break;
 542          }
 543  
 544          if ($selected and $key)
 545          {
 546              foreach ($selected as $id)
 547              {
 548                  if (safe_update('txp_link', "$key = '".doSlash($val)."'", "id = $id"))
 549                  {
 550                      $changed[] = $id;
 551                  }
 552              }
 553          }
 554  
 555          if ($changed)
 556          {
 557              update_lastmod();
 558  
 559              link_list(gTxt(
 560                  ($method == 'delete' ? 'links_deleted' : 'link_updated'),
 561                  array(($method == 'delete' ? '{list}' : '{name}') => join(', ', $changed))));
 562              return;
 563          }
 564  
 565          link_list();
 566      }
 567  
 568  ?>

title

Description

title

Description

title

Description

title

title

Body