[ Index ]
 

Code source de Plume CMS 1.2.2

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/manager/tools/link/ -> class.link.php (source)

   1  <?php
   2  /*

   3  # ***** BEGIN LICENSE BLOCK *****

   4  # This file is part of Plume CMS, a website management application.

   5  # Copyright (C) 2001-2005 Loic d'Anterroches and contributors.

   6  #

   7  # Plume CMS is free software; you can redistribute it and/or modify

   8  # it under the terms of the GNU General Public License as published by

   9  # the Free Software Foundation; either version 2 of the License, or

  10  # (at your option) any later version.

  11  #

  12  # Plume CMS is distributed in the hope that it will be useful,

  13  # but WITHOUT ANY WARRANTY; without even the implied warranty of

  14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

  15  # GNU General Public License for more details.

  16  #

  17  # The Initial Developer of the Original Code is

  18  # Olivier Meunier.

  19  # Portions created by the Initial Developer are Copyright (C) 2003

  20  # the Initial Developer. All Rights Reserved.

  21  #

  22  # Contributor(s):

  23  # - Sebastien Fievet

  24  #

  25  # You should have received a copy of the GNU General Public License

  26  # along with this program; if not, write to the Free Software

  27  # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

  28  #

  29  # ***** END LICENSE BLOCK ***** */

  30  

  31  class link

  32  {

  33      var $con;

  34      var $site_id;

  35      var $table;

  36      

  37      var $protocols = array('http', 'https', 'ftp');

  38      

  39      function link($site_id)

  40      {

  41          $this->getConnection();

  42          $this->site_id = $site_id;

  43          $this->table = $this->con->pfx.'links';

  44      }

  45      

  46      function getConnection()

  47      {

  48          if ($this->con === null) $this->con =& pxDBConnect();

  49      }

  50      

  51      function isRunning() {

  52          $sql = 'SHOW TABLES LIKE \''.$this->table.'\'';

  53          $rs = $this->con->select($sql);

  54          

  55          if($rs->isEmpty()) return false;

  56          return true;

  57      }

  58      

  59      /**

  60       * Validate an URI (RFC2396)

  61       *

  62       * @param string    $url        URI to validate

  63       * @param array     $options    Options used by the validation method.

  64       *                              key => type

  65       *                              'domain_check' => boolean

  66       *                                  Whether to check the DNS entry or not

  67       *                              'allowed_schemes' => array, list of protocols

  68       *                                  List of allowed schemes ('http',

  69       *                                  'ssh+svn', 'mms')

  70       * @author Copyrights PEAR::Validate package

  71       */
  72  	function isURI($url, $options = null)
  73      {
  74          $domain_check = false;
  75          $allowed_schemes = null;
  76          if (is_array($options)) {
  77              extract($options);
  78          }
  79          if (preg_match(
  80              '!^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?!',
  81              $url,$matches)
  82          ) {
  83              $scheme = $matches[2];
  84              $authority = $matches[4];
  85              if ( is_array($allowed_schemes) &&
  86                  !in_array($scheme,$allowed_schemes)
  87              ) {
  88                  return false;
  89              }
  90              if ($domain_check && function_exists('checkdnsrr')) {
  91                  if (!checkdnsrr($authority, 'A')) {
  92                      return false;
  93                  }
  94              }
  95              return true;
  96          }
  97          return false;
  98      }
  99      
 100  	function addLink($label,$href,$title='',$lang='',$cat='-1')
 101      {
 102          #Position maximum

 103          $strReq = 'SELECT MAX(position) '.
 104                  'FROM '.$this->table.' '.
 105                  'WHERE website_id=\''.$this->site_id.'\'';
 106              
 107          if (($rs = $this->con->select($strReq)) === false) {
 108              return false;
 109          }
 110          $max = $rs->f(0);
 111          $position = $rs->f(0)+1;
 112  
 113          # On calcule la position d'insertion

 114          $strReq = 'SELECT position '.
 115                  'FROM '.$this->table.' '.
 116                  'WHERE href=\'\' AND position>='.($cat+1).' AND website_id=\''.$this->site_id.'\' '.
 117                  'ORDER BY position ASC';
 118          $rs = $this->con->select($strReq);
 119          if (!$rs->isEmpty()) {
 120              $position = $rs->f('position');
 121          }
 122  
 123          #On met à jour la position des éléments de $position à $max

 124          $this->__updPosition($position, $max, +1);
 125  
 126          $insReq = 'INSERT INTO '.$this->table.' '.
 127                  '(website_id, label, href, title, lang, position) VALUES '.
 128                  '(\''.$this->con->escapeStr($this->site_id).'\', '.
 129                  '\''.$this->con->escapeStr($label).'\', '.
 130                  '\''.$this->con->escapeStr($href).'\', '.
 131                  '\''.$this->con->escapeStr($title).'\', '.
 132                  '\''.$this->con->escapeStr($lang).'\', '.
 133                  '\''.(integer) $position.'\')';
 134          
 135          if ($this->con->execute($insReq) === false) {
 136              return false;
 137          }
 138          
 139          @touch($_PX_config['manager_path'].'/cache/'.$this->site_id.'/MASS_UPDATE', time());
 140          return true;
 141      }
 142      
 143  	function updLink($link_id,$label,$href,$title='',$lang='', $rel='',$cat='-1', $oldCat='-1')
 144      {
 145          $updReq = '';
 146          if ($cat!=$oldCat) {
 147              #Position actuelle

 148              $strReq = 'SELECT position '.
 149                      'FROM '.$this->table.' '.
 150                      'WHERE link_id = '.$link_id.' AND website_id=\''.$this->site_id.'\'';
 151              $rs = $this->con->select($strReq);
 152              if ($rs->isEmpty()) {
 153                  return false;
 154              }
 155              $currentPosition = $rs->f('position');
 156  
 157              #Maximum

 158              $strReq = 'SELECT MAX(position) '.
 159                      'FROM '.$this->table.' '.
 160                      'WHERE website_id=\''.$this->site_id.'\'';
 161                  
 162              if (($rs = $this->con->select($strReq)) === false) {
 163                  return false;
 164              }
 165              $position = $rs->f(0);
 166  
 167              # On calcule la position d'insertion

 168              $strReq = 'SELECT position '.
 169                      'FROM '.$this->table.' '.
 170                      'WHERE href=\'\' AND position>='.($cat+1).' AND website_id=\''.$this->site_id.'\' '.
 171                      'ORDER BY position ASC';
 172              $rs = $this->con->select($strReq);
 173              if (!$rs->isEmpty()) {
 174                  $position = $rs->f('position');
 175              }
 176  
 177              #On met à jour la position des éléments de $position à $max

 178              if ($currentPosition < $position) {
 179                  $this->__updPosition($currentPosition+1, $position, -1);
 180              } else {
 181                  $this->__updPosition($position, $currentPosition-1, +1);
 182              }
 183              $updReq = 'UPDATE '.$this->table.' SET '.
 184                      'label = \''.$this->con->escapeStr($label).'\','.
 185                      'href = \''.$this->con->escapeStr($href).'\','.
 186                      'title = \''.$this->con->escapeStr($title).'\','.
 187                      'lang = \''. $this->con->escapeStr($lang).'\','.
 188                      'rel = \''. $this->con->escapeStr($rel).'\','.
 189                      'position = '.(integer)$position.' '.
 190                      'WHERE link_id = '.$link_id;
 191          } else {
 192              $updReq = 'UPDATE '.$this->table.' SET '.
 193                      'label = \''.$this->con->escapeStr($label).'\','.
 194                      'href = \''.$this->con->escapeStr($href).'\','.
 195                      'title = \''.$this->con->escapeStr($title).'\','.
 196                      'lang = \'' . $this->con->escapeStr($lang).'\','.
 197                      'rel = \'' . $this->con->escapeStr($rel).'\''.
 198                      'WHERE link_id = '.$link_id;
 199          }
 200          
 201          if ($this->con->execute($updReq) === false) {
 202              return false;
 203          }
 204          
 205          @touch($_PX_config['manager_path'].'/cache/'.$this->site_id.'/MASS_UPDATE', time());
 206          return true;
 207      }
 208  
 209      # Ordonner un lien

 210  	function ordLink($link_id,$ord)
 211      {
 212          $this->__reordEntries();
 213          
 214          $ord = ($ord == '+') ? '+' : '-';
 215          
 216          $strReq = 'SELECT position '.
 217                  'FROM '.$this->table.' '.
 218                  'WHERE link_id = '.$link_id.' AND website_id=\''.$this->site_id.'\'';
 219          $rs = $this->con->select($strReq);
 220          
 221          if ($rs->isEmpty()) {
 222              return false;
 223          }
 224          
 225          $position = $rs->f('position');
 226          
 227          $strReq = 'SELECT MAX(position) FROM '.$this->table.' WHERE website_id=\''.$this->site_id.'\'';
 228          $rs = $this->con->select($strReq);
 229          $max_ord = $rs->f(0);
 230          
 231          # Si on veut monter le plus haut, on arrête

 232          if ($position == 0 && $ord == '+') {
 233              return false;
 234          }
 235          
 236          # Idem pour le plus bas

 237          if ($position == $max_ord && $ord == '-') {
 238              return false;
 239          }
 240          
 241          $new_ord = ($ord == '+') ? $position-1 : $position+1;
 242          
 243          # On met à jour les deux entrées

 244          $updReq = 'UPDATE '.$this->table.' SET '.
 245                  'position = '.$position.' '.
 246                  'WHERE position = '.$new_ord.' AND website_id=\''.$this->site_id.'\'';
 247          
 248          if (!$this->con->execute($updReq)) {
 249              return false;
 250          }
 251          
 252          $updReq = 'UPDATE '.$this->table.' SET '.
 253                  'position = '.$new_ord.' '.
 254                  'WHERE link_id = '.$link_id;
 255          
 256          if (!$this->con->execute($updReq)) {
 257              return false;
 258          }
 259          
 260          @touch($_PX_config['manager_path'].'/cache/'.$this->site_id.'/MASS_UPDATE', time());
 261          return true;
 262      }
 263      
 264      # Création de catégorie

 265  	function addCat($title)
 266      {
 267          $strReq = 'SELECT MAX(position) '.
 268                  'FROM '.$this->table.' '.
 269                  'WHERE website_id=\''.$this->site_id.'\'';
 270              
 271          if (($rs = $this->con->select($strReq)) === false) {
 272              return false;
 273          }
 274          
 275          $position = $rs->f(0)+1;
 276  
 277          $insReq = 'INSERT INTO '.$this->table.' '.
 278                  '(website_id, title, position) VALUES '.
 279                  '(\''.$this->con->escapeStr($this->site_id).'\', '.
 280                  '\''.$this->con->escapeStr($title).'\', '.
 281                  '\''.(integer) $position.'\')';
 282          
 283          if ($this->con->execute($insReq) === false) {
 284              return false;
 285          }
 286          
 287          @touch($_PX_config['manager_path'].'/cache/'.$this->site_id.'/MASS_UPDATE', time());
 288          return true;
 289      }
 290      
 291      # Modification de catégories

 292  	function updCat($id,$title)
 293      {
 294          return $this->updLink($id,'','',$title,'');
 295      }
 296      
 297      # Ordonner une catégorie

 298  	function ordCat($link_id,$ord)
 299      {
 300          $this->__reordEntries();
 301          
 302          $ord = ($ord == '+') ? '+' : '-';
 303          
 304          $strReq = 'SELECT position '.
 305                  'FROM '.$this->table.' '.
 306                  'WHERE link_id = '.$link_id;
 307          $rs = $this->con->select($strReq);
 308          
 309          if ($rs->isEmpty()) {
 310              return false;
 311          }
 312          
 313          $position = $rs->f('position');
 314  
 315          $strReq = 'SELECT MAX(position) FROM '.$this->table.' WHERE website_id=\''.$this->site_id.'\'';
 316          $rs = $this->con->select($strReq);
 317          $max = $rs->f(0);
 318          
 319          # Position de la catégorie avec laquelle échanger

 320          if ($ord == '+') {
 321              # On calcule la position de la catégorie avec laquelle échanger

 322              $strReq = 'SELECT position '.
 323                      'FROM '.$this->table.' '.
 324                      'WHERE href=\'\' AND position<'.$position.' AND website_id=\''.$this->site_id.'\' '.
 325                      'ORDER BY position DESC';
 326              $rs = $this->con->select($strReq);
 327              $swapPosition = -1;
 328              $swapPositionEnd = $position-1;
 329              if (!$rs->isEmpty()) {
 330                  $swapPosition = $rs->f('position');
 331              }
 332  
 333              # On calcule la position du dernier élément de la catégorie

 334              $strReq = 'SELECT position '.
 335                      'FROM '.$this->table.' '.
 336                      'WHERE href=\'\' AND position>='.($position+1).' AND website_id=\''.$this->site_id.'\' '.
 337                      'ORDER BY position ASC';
 338              $rs = $this->con->select($strReq);
 339              $positionEnd = $max;
 340              if (!$rs->isEmpty()) {
 341                  $positionEnd = $rs->f('position')-1;
 342              }
 343          } else {
 344              # On calcule la position de la catégorie avec laquelle échanger

 345              $strReq = 'SELECT position '.
 346                      'FROM '.$this->table.' '.
 347                      'WHERE href=\'\' AND position>'.$position.' AND website_id=\''.$this->site_id.'\' '.
 348                      'ORDER BY position ASC';
 349              $rs = $this->con->select($strReq);
 350              $swapPosition = $max+1;
 351              $positionEnd = $max;
 352              if (!$rs->isEmpty()) {
 353                  $swapPosition = $rs->f('position');
 354                  $positionEnd = $swapPosition-1;
 355              }
 356  
 357              # On calcule la position du dernier élément de la catégorie avec laquelle échanger

 358              $strReq = 'SELECT position '.
 359                      'FROM '.$this->table.' '.
 360                      'WHERE href=\'\' AND position>='.($swapPosition+1).' AND website_id=\''.$this->site_id.'\' '.
 361                      'ORDER BY position ASC';
 362              $rs = $this->con->select($strReq);
 363              $swapPositionEnd = $max;
 364              if (!$rs->isEmpty()) {
 365                  $swapPositionEnd = $rs->f('position')-1;
 366              }
 367          }
 368          $strReq = 'SELECT link_id, position '.
 369                  'FROM '.$this->table.' '.
 370                  'WHERE position>='.$position.' AND position<='.$positionEnd.' AND website_id=\''.$this->site_id.'\' '.
 371                  'ORDER BY position';
 372          $rs = $this->con->select($strReq);
 373          
 374          $strReq = 'SELECT link_id, position '.
 375                  'FROM '.$this->table.' '.
 376                  'WHERE position>='.$swapPosition.' AND position<='.$swapPositionEnd.' AND website_id=\''.$this->site_id.'\' '.
 377                  'ORDER BY position';
 378          $rsSwap = $this->con->select($strReq);
 379  
 380          if (!$rs->isEmpty()) {
 381              if ($ord == '-') {
 382                  $delta = $swapPositionEnd-$swapPosition+1;
 383              } else {
 384                  $delta = -($swapPositionEnd-$swapPosition+1);
 385              }
 386              while(!$rs->EOF()) {
 387                  $pos = $rs->f('position')+$delta;
 388                  $updReq = 'UPDATE '.$this->table.' SET '.
 389                          'position = '.$pos.' '.
 390                          'WHERE link_id = '.$rs->f('link_id');
 391  
 392                  if (!$this->con->execute($updReq)) {
 393                      return false;
 394                  }
 395                  $rs->moveNext();
 396              }
 397          }
 398  
 399          if (!$rsSwap->isEmpty()) {
 400              if ($ord == '-') {
 401                  $delta = -($positionEnd-$position+1);
 402              } else {
 403                  $delta = $positionEnd-$position+1;
 404              }
 405              while(!$rsSwap->EOF()) {
 406                  $pos = $rsSwap->f('position')+$delta;
 407                  $updReq = 'UPDATE '.$this->table.' SET '.
 408                          'position = '.$pos.' '.
 409                          'WHERE link_id = '.$rsSwap->f('link_id');
 410  
 411                  if (!$this->con->execute($updReq)) {
 412                      return false;
 413                  }
 414                  $rsSwap->moveNext();
 415              }
 416          }
 417          
 418          @touch($_PX_config['manager_path'].'/cache/'.$this->site_id.'/MASS_UPDATE', time());
 419          return true;
 420      }
 421      
 422      # Suppression (lien ou catégorie)

 423  	function delEntry($link_id)
 424      {
 425          $delReq = 'DELETE FROM '.$this->table.' '.
 426                  'WHERE link_id = '.$link_id;
 427          
 428          if ($this->con->execute($delReq) === false) {
 429              return false;
 430          }
 431          
 432          @touch($_PX_config['manager_path'].'/cache/'.$this->site_id.'/MASS_UPDATE', time());
 433          return true;
 434      }
 435      
 436      # Ordonner les entrées

 437  	function ordEntries($ord)
 438      {
 439          if (!is_array($ord)) {
 440              return false;
 441          }
 442          
 443          foreach ($ord as $k => $v)
 444          {
 445              $updReq = 'UPDATE '.$this->table.' SET '.
 446                      'position = '.(integer) $v.' '.
 447                      'WHERE link_id = '.(integer) $k;
 448              
 449              if (!$this->con->execute($updReq)) {
 450                  return false;
 451              }
 452          }
 453          
 454          @touch($_PX_config['manager_path'].'/cache/'.$this->site_id.'/MASS_UPDATE', time());
 455          return true;
 456      }
 457      
 458      # Recuperer les entrées

 459      function &getEntries() {
 460          $sql = 'SELECT link_id, label, href, title, lang, position ';
 461          $sql.= 'FROM '.$this->table.' ';
 462          $sql.= 'WHERE website_id=\''.$this->site_id.'\' ';
 463          $sql.= 'ORDER BY position ';
 464      
 465          $rs = $this->con->select($sql);
 466          
 467          return $rs;
 468      }
 469      
 470      function &getEntry($link_id) {
 471          $strReq = 'SELECT link_id, label, href, title, lang, rel, position '.
 472              'FROM '.$this->table.' '.
 473              'WHERE link_id = '.$link_id;
 474      
 475          $rs = $this->con->select($strReq);
 476          
 477          return $rs;
 478      }
 479      
 480      # Incrémente/décrémente la position des éléments entre $min et $max

 481  	function __updPosition($min, $max, $delta)
 482      {
 483          if ($min<=$max) {
 484              $strReq = 'SELECT link_id, position '.
 485                      'FROM '.$this->table.' '.
 486                      'WHERE position>='.$min.' AND position<='.$max.' AND website_id=\''.$this->site_id.'\' '.
 487                      'ORDER BY position ';
 488              $rs = $this->con->select($strReq);
 489          
 490              while (!$rs->EOF()) {
 491                  $position = $rs->f('position')+$delta;
 492                  $updReq = 'UPDATE '.$this->table.' SET '.
 493                          'position = '.$position.' '.
 494                          'WHERE link_id = '.$rs->f('link_id');
 495  
 496                  if (!$this->con->execute($updReq)) {
 497                      return false;
 498                  }
 499                  $rs->moveNext();
 500              }
 501          }
 502          return true;
 503      }
 504  
 505      # Réordonner les entrées

 506  	function __reordEntries()
 507      {
 508          $i = 0;
 509          $strReq = 'SELECT link_id, position '.
 510                  'FROM '.$this->table.' '.
 511                  'WHERE website_id=\''.$this->site_id.'\' '.
 512                  'ORDER BY position ';
 513          $rs = $this->con->select($strReq);
 514          
 515          while (!$rs->EOF())
 516          {
 517              $updReq = 'UPDATE '.$this->table.' SET '.
 518                      'position = '.$i.' '.
 519                      'WHERE link_id = '.$rs->f('link_id');
 520              
 521              if (!$this->con->execute($updReq)) {
 522                  return false;
 523              }
 524              
 525              $i++;
 526              $rs->moveNext();
 527          }
 528          
 529          @touch($_PX_config['manager_path'].'/cache/'.$this->site_id.'/MASS_UPDATE', time());
 530          return true;
 531      }
 532      
 533      # Ajout de header

 534  	function insertHeader() {
 535          global $_px_theme;
 536          
 537          $PLUGIN_HEAD =
 538          '<style type="text/css">'."\n".
 539          '.sort {border : 1px solid #ccc; padding : 0.3em; margin : 5px 0 0 0; background : #f7f5f0; clear : both;}'."\n".
 540          '.sort p {margin : 0.5em 0 0 0;}'."\n".
 541          '.sortJS {padding : 0 0.3em 0.5em 35px; cursor : move; background : #f7f5f0 url(tools/link/themes/'.$_px_theme.'/images/updown.png) no-repeat 5px 5px;}'."\n".
 542          '.sort img.status {float: right; margin: 2px 0 0 4px; position: relative;}'."\n".
 543          '</style>'."\n".
 544          '<script type="text/javascript" src="js/drag.js"></script>'.
 545          '<script type="text/javascript" src="js/dragsort.js"></script>'.
 546          '<script type="text/javascript">'."\n".
 547          '  if (document.getElementById) { '."\n".
 548          '    window.onload = function() { '."\n".
 549          '    dragSort.dest = document.getElementById(\'dndSort\');'."\n".
 550          '    dragSort.makeElementSortable(document.getElementById(\'sortlinks\'));'."\n".
 551          '    };'."\n".
 552          '  }'."\n".
 553          '</script>';
 554          
 555          echo($PLUGIN_HEAD);
 556      }
 557  }
 558  ?>


Généré le : Mon Nov 26 11:57:01 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics