b2evolution PHP Cross Reference Blogging Systems

Source: /inc/links/model/_link.funcs.php - 384 lines - 13804 bytes - Summary - Text - Print

Description: This file implements Link handling functions. This file is part of the b2evolution/evocms project - {@link http://b2evolution.net/}. See also {@link http://sourceforge.net/projects/evocms/}.

   1  <?php
   2  /**

   3   * This file implements Link handling functions.

   4   *

   5   * This file is part of the b2evolution/evocms project - {@link http://b2evolution.net/}.

   6   * See also {@link http://sourceforge.net/projects/evocms/}.

   7   *

   8   * @copyright (c)2003-2014 by Francois Planque - {@link http://fplanque.com/}.

   9   * Parts of this file are copyright (c)2004-2005 by Daniel HAHLER - {@link http://thequod.de/contact}.

  10   *

  11   * @license http://b2evolution.net/about/license.html GNU General Public License (GPL)

  12   *

  13   * @package evocore

  14   *

  15   * @version $Id: $

  16   */
  17  if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );
  18  
  19  load_class( 'links/model/_linkowner.class.php', 'LinkOwner' );
  20  load_class( 'links/model/_linkcomment.class.php', 'LinkComment' );
  21  load_class( 'links/model/_linkitem.class.php', 'LinkItem' );
  22  load_class( 'links/model/_linkuser.class.php', 'LinkUser' );
  23  
  24  /**

  25   * Get a link owner object from link_type and object ID

  26   * 

  27   * @param string link type ( item, comment, ... )

  28   * @param integer the corresponding object ID

  29   */
  30  function get_link_owner( $link_type, $object_ID )
  31  {
  32      switch( $link_type )
  33      {
  34          case 'item':
  35              // create LinkItem object

  36              $ItemCache = & get_ItemCache();
  37              $Item = $ItemCache->get_by_ID( $object_ID, false );
  38              $LinkOwner = new LinkItem( $Item );
  39              break;
  40  
  41          case 'comment':
  42              // create LinkComment object

  43              $CommentCache = & get_CommentCache();
  44              $Comment = $CommentCache->get_by_ID( $object_ID, false );
  45              $LinkOwner = new LinkComment( $Comment );
  46              break;
  47  
  48          case 'user':
  49              // create LinkUser object

  50              $UserCache = & get_UserCache();
  51              $User = $UserCache->get_by_ID( $object_ID, false );
  52              $LinkOwner = new LinkUser( $User );
  53              break;
  54  
  55          default:
  56              $LinkOwner = NULL;
  57      }
  58      return $LinkOwner;
  59  }
  60  
  61  
  62  /**

  63   * Compose screen: display link files iframe

  64   * 

  65   * @param object Form

  66   * @param object LinkOwner object

  67   * @param string iframe name

  68   * @param boolean true if creating new owner object, false otherwise

  69   */
  70  function attachment_iframe( & $Form, & $LinkOwner, $iframe_name = NULL, $creating = false )
  71  {
  72      global $admin_url, $dispatcher;
  73      global $current_User;
  74  
  75      if( ! isset( $GLOBALS[ 'files_Module' ] ) )
  76          return;
  77  
  78      $fieldset_title = T_( 'Images &amp; Attachments' ).get_manual_link( $LinkOwner->type.'_attachments_fieldset' );
  79  
  80      if( $creating )
  81      {    // Creating new Item
  82          $fieldset_title .= ' - <a id="title_file_add" href="#" >'.get_icon( 'folder', 'imgtag' ).' '.T_('Add/Link files').'</a> <span class="note">(popup)</span>';
  83  
  84          $Form->begin_fieldset( $fieldset_title, array( 'id' => 'itemform_createlinks' ) );
  85          $Form->hidden( 'is_attachments', 'false' );
  86  
  87          echo '<table cellspacing="0" cellpadding="0"><tr><td>';
  88          $Form->submit( array( 'actionArray[create_edit]', /* TRANS: This is the value of an input submit button */ T_('Save & start attaching files'), 'SaveEditButton' ) );
  89          echo '</td></tr></table>';
  90  
  91          $Form->end_fieldset();
  92  
  93          return;
  94      }
  95  
  96      // Editing link owner

  97      $Blog = & $LinkOwner->get_Blog();
  98  
  99      if( $iframe_name == NULL )
 100      {
 101          $iframe_name = 'attach_'.generate_random_key( 16 );
 102      }
 103  
 104      $fieldset_title .= ' - <a href="'.$admin_url.'?ctrl=links&amp;action=edit_links&amp;link_type='.$LinkOwner->type.'&amp;mode=iframe&amp;iframe_name='.$iframe_name.'&amp;link_object_ID='.$LinkOwner->get_ID()
 105                  .'" target="'.$iframe_name.'">'.get_icon( 'refresh', 'imgtag' ).' '.T_('Refresh').'</a>';
 106  
 107      if( $current_User->check_perm( 'files', 'view', false, $Blog->ID )
 108          && $LinkOwner->check_perm( 'edit', false ) )
 109      {    // Check that we have permission to edit owner:
 110          $fieldset_title .= ' - <a href="'.$dispatcher.'?ctrl=links&amp;link_type='.$LinkOwner->type.'&amp;fm_mode=link_object&amp;link_object_ID='.$LinkOwner->get_ID()
 111                      .'" onclick="return pop_up_window( \''.$dispatcher.'?ctrl=files&amp;mode=upload&amp;iframe_name='
 112                      .$iframe_name.'&amp;fm_mode=link_object&amp;link_type='.$LinkOwner->type.'&amp;link_object_ID='.$LinkOwner->get_ID().'\', \'fileman_upload\', 1000 )">'
 113                      .get_icon( 'folder', 'imgtag' ).' '.T_('Add/Link files').'</a> <span class="note">(popup)</span>';
 114      }
 115  
 116      $Form->begin_fieldset( $fieldset_title, array( 'id' => 'itemform_links' ) );
 117  
 118      echo '<iframe src="'.$admin_url.'?ctrl=links&amp;link_type='.$LinkOwner->type.'&amp;action=edit_links&amp;mode=iframe&amp;iframe_name='.$iframe_name.'&amp;link_object_ID='.$LinkOwner->get_ID()
 119                  .'" name="'.$iframe_name.'" width="100%" marginwidth="0" height="160" marginheight="0" align="top" scrolling="auto" frameborder="0" id="attachmentframe"></iframe>';
 120  
 121      $Form->end_fieldset();
 122  }
 123  
 124  
 125  /**

 126   * Display a table with the attached files

 127   * 

 128   * @param object LinkOwner

 129   * @param array display params

 130   */
 131  function display_attachments( & $LinkOwner, $params = array() )
 132  {
 133      global $current_User, $samedomain_htsrv_url;
 134  
 135      $params = array_merge( array(
 136              'block_start' => '<div class="attachment_list">',
 137              'block_end' => '</div>',
 138          ), $params );
 139  
 140      $links = $LinkOwner->get_Links();
 141  
 142      if( count( $links ) < 1 )
 143      { // there are no attachments
 144          return;
 145      }
 146  
 147      echo $params[ 'block_start' ];
 148      echo '<table class="grouped" cellspacing="0" cellpadding="0">';
 149      echo '<thead>';
 150      echo '<th class="firstcol shrinkwrap"><span>'.T_('Icon/Type').'</span></th>';
 151      echo '<th class="nowrap"><span>'.T_('Path').'</span></th>';
 152      echo '<th class="lastcol shrinkwrap"><span>'.T_('Actions').'</span></th>';
 153      echo '</thead><tbody>';
 154      $row_style = '';
 155      foreach( $links as $Link )
 156      { // display each link attachment in a row
 157          if( ! ( $link_File = & $Link->get_File() ) )
 158          { // No File object
 159              global $Debuglog;
 160              $Debuglog->add( sprintf( 'Link ID#%d does not have a file object!', $Link->ID ), array( 'error', 'files' ) );
 161              continue;
 162          }
 163          $row_style = ( $row_style == 'even' ) ? 'odd' : 'even';
 164          echo '<tr class="'.$row_style.'"><td class="firstcol">';
 165          echo $link_File->get_preview_thumb( 'fulltype' );
 166          echo '</td><td class="nowrap left">';
 167          echo $link_File->get_view_link();
 168          echo '</td><td class="shrinkwrap">';
 169          if( $current_User->check_perm( 'files', 'edit' ) )
 170          { // display delete link action
 171              $redirect_to = urlencode( regenerate_url( '', '', '', '&' ) );
 172              $delete_url = $samedomain_htsrv_url.'action.php?mname=collections&amp;action=unlink&amp;link_ID='.$Link->ID.'&amp;crumb_collections_unlink='.get_crumb( 'collections_unlink' ).'&amp;redirect_to='.$redirect_to;
 173              echo action_icon( T_( 'Delete' ), 'delete', $delete_url );
 174          }
 175          echo '</td></tr>';
 176      }
 177      echo '</tbody></table>';
 178      echo $params[ 'block_end' ];
 179  }
 180  
 181  
 182  /**

 183   * Display link actions

 184   * 

 185   * @param $link_ID

 186   * @param $cur_idx

 187   * @param $total_rows

 188   */
 189  function link_actions( $link_ID, $cur_idx = 0, $total_rows = 2 )
 190  {
 191      /**

 192       * @var File

 193       */
 194      global $current_File;
 195      global $LinkOwner, $current_User;
 196      global $iframe_name, $admin_url;
 197  
 198      $r = '';
 199  
 200      // Change order.

 201      if( $LinkOwner->check_perm( 'edit' ) )
 202      {    // Check that we have permission to edit LinkOwner object:
 203          if( $cur_idx > 0 )
 204          {
 205              $r .= action_icon( T_('Move upwards'), 'move_up',
 206                  regenerate_url( 'ctrl,link_object_ID,action', 'ctrl=links&amp;link_ID='.$link_ID.'&amp;action=link_move_up&amp;'.url_crumb('link') ) );
 207          }
 208          else
 209          {
 210              $r .= get_icon( 'nomove' ).' ';
 211          }
 212  
 213          if( $cur_idx < $total_rows-1 )
 214          {
 215              $r .= action_icon( T_('Move down'), 'move_down',
 216                  regenerate_url( 'ctrl,p,itm_ID,action', 'ctrl=links&amp;link_ID='.$link_ID.'&amp;action=link_move_down&amp;'.url_crumb('link') ) );
 217          }
 218          else
 219          {
 220              $r .= get_icon( 'nomove' ).' ';
 221          }
 222      }
 223  
 224      if( isset($current_File) && $current_User->check_perm( 'files', 'view', false, $current_File->get_FileRoot() ) )
 225      {
 226          if( $current_File->is_dir() )
 227              $title = T_('Locate this directory!');
 228          else
 229              $title = T_('Locate this file!');
 230          $url = $current_File->get_linkedit_url( $LinkOwner->type, $LinkOwner->get_ID() );
 231          $r .= ' <a href="'.$url.'" onclick="return pop_up_window( \''
 232                      .url_add_param( $url, 'mode=upload&amp;iframe_name='.$iframe_name.'' ).'\', \'fileman_upload\', 1000 )" target="_parent" title="'.$title.'">'
 233                      .get_icon( 'locate', 'imgtag', array( 'title'=>$title ) ).'</a> ';
 234      }
 235  
 236      // Delete link.

 237      if( $LinkOwner->check_perm( 'edit' ) )
 238      { // Check that we have permission to edit LinkOwner object:
 239          $r .= action_icon( T_('Delete this link!'), 'unlink',
 240                            regenerate_url( 'ctrl,p,itm_ID,action', 'ctrl=links&amp;link_ID='.$link_ID.'&amp;action=unlink&amp;'.url_crumb('link') ) );
 241      }
 242  
 243      if( isset( $current_File ) && $current_File->is_image() )
 244      {    // Display icon to insert image into post inline
 245          $r .= ' '.get_icon( 'add', 'imgtag', array(
 246                  'title'   => T_('Insert image into the post'),
 247                  'onclick' => 'insert_image_link( '.$link_ID.', \''.format_to_output( addslashes( $current_File->get( 'desc' ) ), 'htmlspecialchars' ).'\' )',
 248                  'style'   => 'cursor:default;'
 249              ) );
 250      }
 251  
 252      return $r;
 253  }
 254  
 255  
 256  /**

 257   * Display link position edit action

 258   * 

 259   * @param $row

 260   */
 261  function display_link_position( & $row )
 262  {
 263      global $LinkOwner, $htsrv_url;
 264      // TODO: fp>dh: can you please implement cumbs in here? I don't clearly understand your code.

 265      // TODO: dh> only handle images

 266  
 267      $id = 'display_position_'.$row->link_ID;
 268  
 269      // NOTE: dh> using method=get so that we can use regenerate_url (for non-JS).

 270      $r = '<form action="" method="post">
 271          <select id="'.$id.'" name="link_position">'
 272          .Form::get_select_options_string( $LinkOwner->get_positions(), $row->link_position, true).'</select>'
 273          .'<script type="text/javascript">jQuery("#'.$id.'").change( { url: "'.$htsrv_url.'", crumb: "'.get_crumb( 'link' ).'" }, function( event ) {
 274              evo_display_position_onchange( this, event.data.url, event.data.crumb ) } );</script>';
 275  
 276      $r .= '<noscript>';
 277      // Add hidden fields for non-JS

 278      $url = regenerate_url( 'p,itm_ID,action', 'link_ID='.$row->link_ID.'&action=set_link_position&'.url_crumb('link'), '', '&' );
 279      $params = explode('&', substr($url, strpos($url, '?')+1));
 280  
 281      foreach($params as $param)
 282      {
 283          list($k, $v) = explode('=', $param);
 284          $r .= '<input type="hidden" name="'.htmlspecialchars($k).'" value="'.htmlspecialchars($v).'" />';
 285      }
 286      $r .= '<input class="SaveButton" type="submit" value="&raquo;" />';
 287      $r .= '</noscript>';
 288      $r .= '</form>';
 289  
 290      return $r;
 291  }
 292  
 293  
 294  /**

 295   * Get all links where file is used

 296   *

 297   * @param integer File ID

 298   * @param array Params

 299   * @return string The links to that posts, comments and users where the file is used

 300   */
 301  function get_file_links( $file_ID, $params = array() )
 302  {
 303      global $DB, $current_User, $baseurl, $admin_url;
 304  
 305      $params = array_merge( array( 
 306              'separator' => '<br />',
 307              'post_prefix' => T_('Post').' - ',
 308              'comment_prefix' => T_('Comment on').' - ',
 309              'user_prefix' => T_('Profile picture').' - ',
 310          ), $params );
 311  
 312      // Create result array

 313      $attached_to = array();
 314  
 315      // Get all links with posts and comments

 316      $links_SQL = new SQL();
 317      $links_SQL->SELECT( 'link_itm_ID, link_cmt_ID' );
 318      $links_SQL->FROM( 'T_links' );
 319      $links_SQL->WHERE( 'link_file_ID = '.$DB->quote( $file_ID ) );
 320      $links = $DB->get_results( $links_SQL->get() );
 321  
 322      if( !empty( $links ) )
 323      {    // File is linked with some posts or comments
 324          $ItemCache = & get_ItemCache();
 325          $CommentCache = & get_CommentCache();
 326          foreach( $links as $link )
 327          {
 328              if( !empty( $link->link_itm_ID ) )
 329              {    // File is linked to a post
 330                  if( $Item = & $ItemCache->get_by_ID( $link->link_itm_ID, false ) )
 331                  {
 332                      $Blog = $Item->get_Blog();
 333                      if( $current_User->check_perm( 'item_post!CURSTATUS', 'view', false, $Item ) )
 334                      {    // Current user can edit the linked post
 335                          $attached_to[] = $params['post_prefix'].'<a href="'.url_add_param( $admin_url, 'ctrl=items&amp;blog='.$Blog->ID.'&amp;p='.$link->link_itm_ID ).'">'.$Item->get( 'title' ).'</a>';
 336                      }
 337                      else
 338                      {    // No access to edit the linked post
 339                          $attached_to[] = $params['post_prefix'].$Item->get( 'title' );
 340                      }
 341                  }
 342              }
 343              if( !empty( $link->link_cmt_ID ) )
 344              {    // File is linked to a comment
 345                  if( $Comment = & $CommentCache->get_by_ID( $link->link_cmt_ID, false ) )
 346                  {
 347                      $Item = $Comment->get_Item();
 348                      if( $current_User->check_perm( 'comment!CURSTATUS', 'moderate', false, $Comment ) )
 349                      {    // Current user can edit the linked Comment
 350                          $attached_to[] = $params['comment_prefix'].'<a href="'.url_add_param( $admin_url, 'ctrl=comments&amp;action=edit&amp;comment_ID='.$link->link_cmt_ID ).'">'.$Item->get( 'title' ).'</a>';
 351                      }
 352                      else
 353                      {    // No access to edit the linked Comment
 354                          $attached_to[] = $params['comment_prefix'].$Item->get( 'title' );
 355                      }
 356                  }
 357              }
 358          }
 359      }
 360  
 361      // Get all links with profile pictures

 362      $profile_links_SQL = new SQL();
 363      $profile_links_SQL->SELECT( 'user_ID, user_login' );
 364      $profile_links_SQL->FROM( 'T_users' );
 365      $profile_links_SQL->WHERE( 'user_avatar_file_ID = '.$DB->quote( $file_ID ) );
 366      $profile_links = $DB->get_results( $profile_links_SQL->get() );
 367      if( !empty( $profile_links ) )
 368      {
 369          foreach( $profile_links as $link )
 370          {
 371              if( $current_User->ID != $link->user_ID && !$current_User->check_perm( 'users', 'view' ) )
 372              { // No permission to view other users in admin form
 373                  $attached_to[] = $params['user_prefix'].'<a href="'.url_add_param( $baseurl, 'disp=user&amp;user_ID='.$link->user_ID ).'">'.$link->user_login.'</a>';
 374              }
 375              else
 376              { // Build a link to display a user in admin form
 377                  $attached_to[] = $params['user_prefix'].'<a href="?ctrl=user&amp;user_tab=profile&amp;user_ID='.$link->user_ID.'">'.$link->user_login.'</a>';
 378              }
 379          }
 380      }
 381  
 382      return implode( $params['separator'], $attached_to );
 383  }
 384  ?>

title

Description

title

Description

title

Description

title

title

Body