[ Index ]
 

Code source de Serendipity 1.2

Accédez au Source d'autres logiciels libres

title

Body

[fermer]

/include/ -> functions_smarty.inc.php (source)

   1  <?php # $Id: functions_smarty.inc.php 1860 2007-08-21 10:26:07Z garvinhicking $
   2  # Copyright (c) 2003-2005, Jannis Hermanns (on behalf the Serendipity Developer Team)
   3  # All rights reserved.  See LICENSE file for licensing details
   4  
   5  if (IN_serendipity !== true) {
   6      die ("Don't hack!");
   7  }
   8  
   9  if (defined('S9Y_FRAMEWORK_SMARTY')) {
  10      return;
  11  }
  12  @define('S9Y_FRAMEWORK_SMARTY', true);
  13  
  14  /**
  15   * Fetch a list of trackbacks for an entry
  16   *
  17   * @access public
  18   * @param   int     The ID of the entry
  19   * @param   string  How many trackbacks to show
  20   * @param   boolean If true, also non-approved trackbacks will be shown
  21   * @return
  22   */
  23  function &serendipity_fetchTrackbacks($id, $limit = null, $showAll = false) {
  24      global $serendipity;
  25  
  26      if (!$showAll) {
  27          $and = "AND status = 'approved'";
  28      }
  29  
  30      $query = "SELECT * FROM {$serendipity['dbPrefix']}comments WHERE entry_id = '". (int)$id ."' AND type = 'TRACKBACK' $and ORDER BY id";
  31      if (isset($limit)) {
  32          $limit  = serendipity_db_limit_sql($limit);
  33          $query .= " $limit";
  34      }
  35  
  36      $comments = serendipity_db_query($query);
  37      if (!is_array($comments)) {
  38          return array();
  39      }
  40  
  41      return $comments;
  42  }
  43  
  44  /**
  45   * Show trackbacks for an entry
  46   *
  47   * LONG
  48   *
  49   * @access public
  50   * @param   array       The superarray of trackbacks to display
  51   * @return
  52   */
  53  function &serendipity_printTrackbacks(&$trackbacks) {
  54      global $serendipity;
  55  
  56      $serendipity['smarty']->assign_by_ref('trackbacks', $trackbacks);
  57  
  58      return serendipity_smarty_fetch('TRACKBACKS', 'trackbacks.tpl');
  59  }
  60  
  61  /**
  62   * Smarty: Fetch a smarty block and pass it on to Serendipity Templates
  63   *
  64   * @access public
  65   * @param   string      The name of the block to parse data into ("COMMENTS" - virtual variable like {$COMMENTS})
  66   * @param   string      The name of the template file to fetch. Only filename, the path is auto-detected
  67   * @param   boolean     If true, the output of the smarty parser will be echoed instead of invisibly treated
  68   * @return  string      The parsed HTML code
  69   */
  70  function &serendipity_smarty_fetch($block, $file, $echo = false) {
  71      global $serendipity;
  72  
  73      $output =& $serendipity['smarty']->fetch('file:'. serendipity_getTemplateFile($file, 'serendipityPath'), null, null, ($echo === true && $serendipity['smarty_raw_mode']));
  74      $serendipity['smarty']->assign_by_ref($block, $output);
  75  
  76      return $output;
  77  }
  78  
  79  /**
  80   * Smarty Modifier: Check if a string is not empty and prepend a prefix in that case. Else, leave empty
  81   *
  82   * @access public
  83   * @param   string  The input string to check
  84   * @param   string  The prefix to prepend, if $string is not empty
  85   * @return  string  The return string
  86   */
  87  function serendipity_emptyPrefix($string, $prefix = ': ') {
  88      return (!empty($string) ? $prefix . htmlspecialchars($string) : '');
  89  }
  90  
  91  /**
  92   * Smarty Modifier: Return a remembered variable
  93   *
  94   * @access public
  95   * @param   string  The variable name
  96   * @param   string  The wanted value
  97   * @param   boolean Force default?
  98   * @param   string  Returned attribute
  99   * @return  string  The return string
 100   */
 101  function serendipity_ifRemember($name, $value, $isDefault = false, $att = 'checked') {
 102      global $serendipity;
 103  
 104      if (!is_array($serendipity['COOKIE']) && !$isDefault) {
 105          return false;
 106      }
 107  
 108      if ((!is_array($serendipity['COOKIE']) && $isDefault) ||
 109          (!isset($serendipity['COOKIE']['serendipity_' . $name]) && $isDefault) ||
 110          (isset($serendipity['COOKIE']['serendipity_' . $name]) && $serendipity['COOKIE']['serendipity_' . $name] == $value)) {
 111  
 112          return " $att=\"$att\" ";
 113      }
 114  }
 115  
 116  /**
 117   * Smarty Function: Fetch and print a single or multiple entries
 118   *
 119   * @access public
 120   * @param   array       Smarty parameter input array:
 121   *                      [FETCHING]
 122   *                          category:        (int)     The category ID (seperate multiple with ";") to fetch entries from
 123   *                          viewAuthor:      (int)     The author ID (seperate multiple with ";") to fetch entries from
 124   *                          page:            (int)     The number of the page for paginating entries
 125   *                          id:              (int)     The ID of an entry. If given, only a single entry will be fetched. If left empty, multiple entries are fetched.
 126   *                          range:           (mixed)   Restricts fetching entries to a specific timespan. Behaves differently depending on the type:
 127   *                                           Numeric:
 128   *                                            YYYYMMDD - Shows all entries from YYYY-MM-DD.
 129   *                                            If DD is "00", it will show all entries from that month.
 130   *                                            If DD is any other number, it will show entries of that specific day.
 131   *                                           2-Dimensional Array:
 132   *                                            Key #0   - Specifies the start timestamp (unix seconds)
 133   *                                            Key #1   - Specifies the end timestamp (unix seconds)
 134   *                                           Other (null, 3-dimensional Array, ...):
 135   *                                            Entries newer than $modified_since will be fetched
 136   *                          full             (boolean) Indicates if the full entry will be fetched (body+extended: TRUE), or only the body (FALSE).
 137   *                          limit            (string)  Holds a "Y" or "X, Y" string that tells which entries to fetch. X is the first entry offset, Y is number of entries. If not set, the global fetchLimit will be applied (15 entries by default)
 138   *                          fetchDrafts      (boolean) Indicates whether drafts should be fetched (TRUE) or not
 139   *                          modified_since   (int)     Holds a unix timestamp to be used in conjunction with $range, to fetch all entries newer than this timestamp
 140   *                          orderby          (string)  Holds the SQL "ORDER BY" statement.
 141   *                          filter_sql       (string)  Can contain any SQL code to inject into the central SQL statement for fetching the entry
 142   *                          noCache          (boolean) If set to TRUE, all entries will be fetched from scratch and any caching is ignored
 143   *                          noSticky         (boolean) If set to TRUE, all sticky entries will NOT be fetched.
 144   *
 145   *                      [PRINTING]
 146   *                          template:          (string)  Name of the template file to print entries with
 147   *                          preview:           (boolean) Indicates if this is a preview
 148   *                          block              (string   The name of the SMARTY block that this gets parsed into
 149   *                          use_hooks          (boolean  Indicates whether to apply footer/header event hooks
 150   *                          use_footer         (boolean  Indicates whether the pagination footer should be displayed
 151   *                          groupmode          (string   Indicates whether the input $entries array is already grouped in preparation for the smarty $entries output array [TRUE], or if it shall be grouped by date [FALSE]
 152   *                          skip_smarty_hooks  (boolean) If TRUE, no plugins will be executed at all
 153   *                          skip_smarty_hook   (mixed)   Can be set to an array of plugin hooks to NOT execute
 154   *                          prevent_reset      (boolean) If set to TRUE, the smarty $entries array will NOT be cleared. (to prevent possible duplicate output of entries)
 155   * @param   object      Smarty object
 156   * @return  string      The Smarty HTML response
 157   */
 158  function serendipity_smarty_fetchPrintEntries($params, &$smarty) {
 159      global $serendipity;
 160      static $entrycount = 0;
 161      static $restore_var_GET_keys = array('category', 'viewAuthor', 'page');
 162  
 163      // A counter variable to not assign template files multiple times
 164      $entrycount++;
 165  
 166      // Default values for function calls
 167      if (empty($params['template'])) {
 168          $params['template'] = 'entries.tpl';
 169      }
 170  
 171      if (empty($params['range'])) {
 172          $params['range'] = null;
 173      }
 174  
 175      if (empty($params['full'])) {
 176          $params['full'] = true;
 177      }
 178  
 179      if (empty($params['fetchDrafts'])) {
 180          $params['fetchDrafts'] = false;
 181      }
 182  
 183      if (empty($params['modified_since'])) {
 184          $params['modified_since'] = false;
 185      }
 186  
 187      if (empty($params['orderby'])) {
 188          $params['orderby'] = 'timestamp DESC';
 189      }
 190  
 191      if (empty($params['noCache'])) {
 192          $params['noCache'] = false;
 193      }
 194  
 195      if (empty($params['noSticky'])) {
 196          $params['noSticky'] = false;
 197      }
 198  
 199      if (empty($params['preview'])) {
 200          $params['preview'] = false;
 201      }
 202  
 203      if (empty($params['block'])) {
 204          $params['block'] = 'smarty_entries_' . $entrycount;
 205      }
 206  
 207      if (empty($params['use_hooks'])) {
 208          $params['use_hooks'] = false;
 209      }
 210  
 211      if (empty($params['use_footer'])) {
 212          $params['use_footer'] = false;
 213      }
 214  
 215      if (empty($params['groupmode'])) {
 216          $params['groupmode'] = 'date';
 217      }
 218  
 219      if (empty($params['skip_smarty_hooks'])) {
 220          $params['skip_smarty_hooks'] = true;
 221      }
 222  
 223      if (empty($params['skip_smarty_hook'])) {
 224          $params['skip_smarty_hook'] = array();
 225      }
 226  
 227      if (empty($params['prevent_reset'])) {
 228          $params['prevent_reset'] = false;
 229      }
 230  
 231      // Some functions deal with the $serendipity array. To modify them, we need to store
 232      // their original contents.
 233      $old_var = array();
 234      if (!empty($params['short_archives'])) {
 235          $old_var['short_archives']     = $serendipity['short_archives'];
 236          $serendipity['short_archives'] = $params['short_archives'];
 237      }
 238  
 239      $old_var['skip_smarty_hooks']     = $serendipity['skip_smarty_hooks'];
 240      $serendipity['skip_smarty_hooks'] = $params['skip_smarty_hooks'];
 241  
 242      $old_var['skip_smarty_hook']     = $serendipity['skip_smarty_hook'];
 243      $serendipity['skip_smarty_hook'] = $params['skip_smarty_hook'];
 244  
 245      foreach($restore_var_GET_keys AS $key) {
 246          if (!empty($params[$key])) {
 247              $old_var['GET'][$key]     = $serendipity['GET'][$key];
 248              $serendipity['GET'][$key] = $params[$key];
 249          }
 250      }
 251  
 252      if (!empty($params['id'])) {
 253          $entry = serendipity_fetchEntry(
 254              'id',
 255              (int)$params['id'],
 256              $params['full'],
 257              $params['fetchDrafts']);
 258      } else {
 259          $entry = serendipity_fetchEntries(
 260              $params['range'],
 261              $params['full'],
 262              $params['limit'],
 263              $params['fetchDrafts'],
 264              $params['modified_since'],
 265              $params['orderby'],
 266              $params['filter_sql'],
 267              $params['noCache'],
 268              $params['noSticky']
 269          );
 270  
 271          // Check whether the returned entries shall be grouped specifically
 272          switch ($params['groupmode']) {
 273              case 'date':
 274                  // No regrouping required, printEntries() does it for us.
 275                  break;
 276  
 277              case 'category':
 278                  // Regroup by primary category
 279  
 280                  $groupdata = array();
 281                  foreach($entry AS $k => $_entry) {
 282  
 283                      if (is_array($entry['categories'])) {
 284                          $groupkey = $entry['categories'][0];
 285                      } else {
 286                          $groupkey = 'none';
 287                      }
 288                      $groupdata[$groupkey]['entries'] =& $_entry;
 289                  }
 290                  $entry =& $groupdata;
 291                  break;
 292          }
 293      }
 294  
 295      serendipity_printEntries(
 296          $entry,                                 // Entry data
 297          (!empty($params['id']) ? true : false), // Extended data?
 298          $params['preview'],                     // Entry preview?
 299          'ENTRIES',
 300          false,                                  // Prevent Smarty parsing
 301          $params['use_hooks'],
 302          $params['use_footer'],
 303          ($params['groupmode'] == 'date' ? false : true) // Grouping of $entry
 304      );
 305  
 306      // Restore the $serendipity array after our modifications.
 307      if (isset($old_var['short_archives'])) {
 308          $serendipity['short_archives'] = $old_var['short_archives'];
 309      }
 310  
 311      if (is_array($old_var['GET'])) {
 312          foreach($old_var['GET'] AS $key => $val) {
 313              $serendipity['GET'][$key] = $val;
 314          }
 315      }
 316  
 317      $out = serendipity_smarty_fetch($params['block'], $params['template']);
 318      // Reset array list, because we might be in a nested code call.
 319      if ($params['prevent_reset'] == false) {
 320          $serendipity['smarty']->assign('entries', array());
 321      }
 322      $serendipity['skip_smarty_hook']  = $old_var['skip_smarty_hook'];
 323      $serendipity['skip_smarty_hooks'] = $old_var['skip_smarty_hooks'];
 324  
 325      return $out;
 326  }
 327  
 328  /**
 329   * Smarty Function: Shows a commentform
 330   *
 331   * @access public
 332   * @param   array       Smarty parameter input array:
 333   *                          id: An entryid to show the commentform for
 334   *                          url: an optional HTML target link for the form
 335   *                          comments: Optional array of containing comments
 336   *                          data: possible pre-submitted values to the input values
 337   *                          showToolbar: Toggle whether to show extended options of the comment form
 338   *                          moderate_comments: Toggle whether comments to this entry are allowed
 339   * @param   object  Smarty object
 340   * @return  void
 341   */
 342  function serendipity_smarty_showCommentForm($params, &$smarty) {
 343      global $serendipity;
 344  
 345      if (!isset($params['id']) || !isset($params['entry'])) {
 346          $smarty->trigger_error(__FUNCTION__ .": missing 'id' or 'entry' parameter");
 347          return;
 348      }
 349  
 350      if (empty($params['url'])) {
 351          $params['url'] = $serendipity['serendipityHTTPPath'] . $serendipity['indexFile'] . '?url=' . $params['entry']['commURL'];
 352      }
 353  
 354      if (!isset($params['comments'])) {
 355          $params['comments'] = NULL;
 356      }
 357  
 358      if (!isset($params['data'])) {
 359          $params['data'] = $serendipity['POST'];
 360      }
 361  
 362      if (!isset($params['showToolbar'])) {
 363          $params['showToolbar'] = true;
 364      }
 365  
 366      if (!isset($params['moderate_comments'])) {
 367          $params['moderate_comments'] = serendipity_db_bool($params['entry']['moderate_comments']);
 368      }
 369  
 370  
 371      $comment_add_data = array(
 372          'comments_messagestack' => (isset($serendipity['messagestack']['comments']) ? (array)$serendipity['messagestack']['comments'] : array()),
 373          'is_comment_added'      => (isset($serendipity['GET']['csuccess']) && $serendipity['GET']['csuccess'] == 'true' ? true: false),
 374          'is_comment_moderate'   => (isset($serendipity['GET']['csuccess']) && $serendipity['GET']['csuccess'] == 'moderate' ? true: false)
 375      );
 376  
 377      $smarty->assign($comment_add_data);
 378  
 379      serendipity_displayCommentForm(
 380          $params['id'],
 381          $params['url'],
 382          $params['comments'],
 383          $params['data'],
 384          $params['showToolbar'],
 385          $params['moderate_comments'],
 386          $params['entry']
 387      );
 388  
 389      return true;
 390  }
 391  
 392  /**
 393   * Smarty Function: Be able to include the output of a sidebar plugin within a smarty template
 394   *
 395   * @access public
 396   * @param   array       Smarty parameter input array:
 397   *                          class: The classname of the plugin to show
 398   *                          id: An ID of a plugin to show
 399   *                          side: The side of a plugin to show (left|right|hidden)
 400   *                          negate: Revert previous filters
 401   * @param   object  Smarty object
 402   * @return  string      The Smarty HTML response
 403   */
 404  function serendipity_smarty_showPlugin($params, &$smarty) {
 405      global $serendipity;
 406  
 407      if (!isset($params['class']) && !isset($params['id'])) {
 408          $smarty->trigger_error(__FUNCTION__ .": missing 'class' or 'id' parameter");
 409          return;
 410      }
 411  
 412      if (!isset($params['side'])) {
 413          $params['side'] = '*';
 414      }
 415  
 416      if (!isset($params['negate'])) {
 417          $params['negate'] = null;
 418      }
 419  
 420      return serendipity_plugin_api::generate_plugins($params['side'], null, $params['negate'], $params['class'], $params['id']);
 421  }
 422  
 423  /**
 424   * Smarty Function: Get total count for specific objects
 425   *
 426   * @access public
 427   * @param   array       Smarty parameter input array:
 428   *                      what: The type of count to show: "entries", "trackbacks", "comments"
 429   * @param   object      Smarty object
 430   * @return  string      The Smarty HTML response
 431   */
 432  function serendipity_smarty_getTotalCount($params, &$smarty) {
 433      if (!isset($params['what'])) {
 434          $smarty->trigger_error(__FUNCTION__ .": missing 'what' parameter");
 435          return;
 436      }
 437  
 438      return serendipity_getTotalCount($params['what']);
 439  }
 440  
 441  /**
 442   * Smarty Function: Be able to execute the hook of an event plugin and return its output
 443   *
 444   * Listens to specific serendipity global variables:
 445   *  $serendipity['skip_smarty_hooks'] - If TRUE, no plugins will be executed at all
 446   *  $serendipity['skip_smarty_hook']  - Can be set to an array of plugin hooks to NOT execute
 447   *
 448   * @access public
 449   * @param   array       Smarty parameter input array:
 450   *                          hook: The name of the event hook
 451   *                          hookAll: boolean whether unknown hooks shall be executed
 452   *                          data: The $eventData to an event plugin
 453   *                          addData: the $addData to an event plugin
 454   * @param   object  Smarty object
 455   * @return null
 456   */
 457  function serendipity_smarty_hookPlugin($params, &$smarty) {
 458      global $serendipity;
 459      static $hookable = array('frontend_header',
 460                               'entries_header',
 461                               'entries_footer',
 462                               'frontend_comment',
 463                               'frontend_footer');
 464  
 465      if (!isset($params['hook'])) {
 466          $smarty->trigger_error(__FUNCTION__ .": missing 'hook' parameter");
 467          return;
 468      }
 469  
 470      if (!in_array($params['hook'], $hookable) && $params['hookAll'] != 'true') {
 471          $smarty->trigger_error(__FUNCTION__ .": illegal hook '". $params['hook'] ."'");
 472          return;
 473      }
 474  
 475      // Smarty hooks can be bypassed via an internal variable (temporarily)
 476      if (isset($serendipity['skip_smarty_hooks']) && $serendipity['skip_smarty_hooks']) {
 477          return;
 478      }
 479  
 480      // A specific hook can also be bypassed by creating an associative array like this:
 481      // $serendipity['skip_smarty_hook'] = array('entries_header');
 482      // That would only skip the entries_header event hook, but allow all others.
 483      // Of course it cannot be used in conjunction with the all-blocking skip_smarty_hooks.
 484      if (isset($serendipity['skip_smarty_hook']) && is_array($serendipity['skip_smarty_hook']) && isset($serendipity['skip_smarty_hook'][$params['hook']])) {
 485          return;
 486      }
 487  
 488      if (!isset($params['data'])) {
 489          $params['data'] = &$serendipity;
 490      }
 491  
 492      if (!isset($params['addData'])) {
 493          $params['addData'] = null;
 494      }
 495  
 496      serendipity_plugin_api::hook_event($params['hook'], $params['data'], $params['addData']);
 497  }
 498  
 499  /**
 500   * Smarty Modifier: Be able to execute the hook of an event plugin and return its output, uses a REFERENCED variable.
 501   *
 502   * Listens to specific serendipity global variables:
 503   *  $serendipity['skip_smarty_hooks'] - If TRUE, no plugins will be executed at all
 504   *  $serendipity['skip_smarty_hook']  - Can be set to an array of plugin hooks to NOT execute
 505   *
 506   * @access public
 507   * @param   mixed       EventData (referenced)
 508   * @param   string      Event hook name
 509   * @param   mixed       Additional data
 510   * @return null
 511   */
 512  function serendipity_smarty_refhookPlugin(&$eventData, $hook, $addData = null) {
 513      global $serendipity;
 514  
 515      if (!isset($hook)) {
 516          $smarty->trigger_error(__FUNCTION__ .": missing 'hook' parameter");
 517          return;
 518      }
 519  
 520      // Smarty hooks can be bypassed via an internal variable (temporarily)
 521      if (isset($serendipity['skip_smarty_hooks']) && $serendipity['skip_smarty_hooks']) {
 522          return;
 523      }
 524  
 525      // A specific hook can also be bypassed by creating an associative array like this:
 526      // $serendipity['skip_smarty_hook'] = array('entries_header');
 527      // That would only skip the entries_header event hook, but allow all others.
 528      // Of course it cannot be used in conjunction with the all-blocking skip_smarty_hooks.
 529      if (isset($serendipity['skip_smarty_hook']) && is_array($serendipity['skip_smarty_hook']) && isset($serendipity['skip_smarty_hook'][$params['hook']])) {
 530          return;
 531      }
 532  
 533      serendipity_plugin_api::hook_event($hook, $eventData, $addData);
 534  }
 535  
 536  /**
 537   * Smarty Function: Prints a list of sidebar plugins
 538   *
 539   * @access public
 540   * @param   array       Smarty parameter input array:
 541   *                          side: The plugin side to show (left|right|hidden)
 542   * @param   object  Smarty object
 543   * @return string       The HTML code of a plugin's output
 544   */
 545  function serendipity_smarty_printSidebar($params, &$smarty) {
 546      if ( !isset($params['side']) ) {
 547          $smarty->trigger_error(__FUNCTION__ .": missing 'side' parameter");
 548          return;
 549      }
 550      return serendipity_plugin_api::generate_plugins($params['side']);
 551  }
 552  
 553  /**
 554   * Smarty Function: Get the full path to a template file
 555   *
 556   * @access public
 557   * @param   array       Smarty parameter input array:
 558   *                          file: The filename you want to include (any file within your template directry or the 'default' template if not found)
 559   * @param   object  Smarty object
 560   * @return  string      The requested filename with full path
 561   */
 562  function serendipity_smarty_getFile($params, &$smarty) {
 563      if ( !isset($params['file']) ) {
 564          $smarty->trigger_error(__FUNCTION__ .": missing 'file' parameter");
 565          return;
 566      }
 567      return serendipity_getTemplateFile($params['file']);
 568  }
 569  
 570  
 571  /**
 572   * Smarty Function: Picks a specified key from an array and returns it
 573   *
 574   * @access public
 575   * @param   array       Smarty parameter input array:
 576   *                          array: The array you want to check
 577   *                          key: The keyname
 578   *                          default: What (string) to return when array does not contain the key.
 579   * @param   object       Smarty object
 580   * @return  string      The requested filename with full path
 581   */
 582  function serendipity_smarty_pickKey($params, &$smarty) {
 583      if ( !isset($params['array']) ) {
 584          $smarty->trigger_error(__FUNCTION__ .": missing 'array' parameter");
 585          return;
 586      }
 587  
 588      if ( !isset($params['key']) ) {
 589          $smarty->trigger_error(__FUNCTION__ .": missing 'key' parameter");
 590          return;
 591      }
 592  
 593      return serendipity_pickKey($params['array'], $params['key'], $params['default']);
 594  }
 595  
 596  
 597  /**
 598   * Smarty Function: Get a permalink for an entry
 599   *
 600   * @access public
 601   * @param   array       Smarty parameter input array:
 602   *                          entry: $entry data to pull title etc. out of
 603   *                          is_comments: Whether a permalink for a comment feed should be embedded
 604   * @param   object  Smarty object
 605   * @return string       The permalink
 606   */
 607  function serendipity_smarty_rss_getguid($params, &$smarty) {
 608      if ( !isset($params['entry']) ) {
 609          $smarty->trigger_error(__FUNCTION__ .": missing 'entry' parameter");
 610          return;
 611      }
 612      if ( !isset($params['is_comments']) ) {
 613          $smarty->trigger_error(__FUNCTION__ .": missing 'is_comments' parameter");
 614          return;
 615      }
 616  
 617      return serendipity_rss_getguid($params['entry'], $params['is_comments']);
 618  }
 619  
 620  /**
 621   * Smarty Modifier: Format a timestamp
 622   *
 623   * @access public
 624   * @param   int     The timestamp to format (unix seconds)
 625   * @param   string  The strftime() format options on how to format this string
 626   * @param   boolean Shall timezone conversions be applied?
 627   * @param   boolean Try to detect a valid timestamp?
 628   * @param   boolean Use strftime or date?
 629   * @return
 630   */
 631  function serendipity_smarty_formatTime($timestamp, $format, $useOffset = true, $detectTimestamp = false, $useDate = false) {
 632      if ($detectTimestamp !== false && stristr($detectTimestamp, 'date') === false) {
 633          return $timestamp;
 634      }
 635  
 636      if (defined($format)) {
 637          return serendipity_formatTime(constant($format), $timestamp, $useOffset, $useDate);
 638      } else {
 639          return serendipity_formatTime($format, $timestamp, $useOffset, $useDate);
 640      }
 641  }
 642  
 643  /**
 644   * Smarty Function: Show comments to an entry
 645   *
 646   * @access public
 647   * @param   array       Smarty parameter input array:
 648   *                          entry: The $entry data
 649   *                          mode: The default viewmode (threaded/linear)
 650   * @param   object  Smarty object
 651   * @return  string      The HTML code with the comments
 652   */
 653  function &serendipity_smarty_printComments($params, &$smarty) {
 654      global $serendipity;
 655  
 656      if (!isset($params['entry'])) {
 657          $smarty->trigger_error(__FUNCTION__ .": missing 'entry' parameter");
 658          return;
 659      }
 660  
 661      if (!isset($params['mode'])) {
 662          $params['mode'] = VIEWMODE_THREADED;
 663      }
 664  
 665      if (isset($params['order']) && $params['order'] != 'DESC') {
 666          $params['order'] = 'ASC';
 667      }
 668  
 669      $comments = serendipity_fetchComments($params['entry'], (int)$params['limit'], $params['order']);
 670  
 671      if (!empty($serendipity['POST']['preview'])) {
 672          $comments[] =
 673              array(
 674                      'email'     => $serendipity['POST']['email'],
 675                      'author'    => $serendipity['POST']['name'],
 676                      'body'      => $serendipity['POST']['comment'],
 677                      'url'       => $serendipity['POST']['url'],
 678                      'parent_id' => $serendipity['POST']['replyTo'],
 679                      'timestamp' => time()
 680              );
 681      }
 682  
 683      $out = serendipity_printComments($comments, $params['mode']);
 684      return $out;
 685  }
 686  
 687  /**
 688   * Smarty Function: Show Trackbacks
 689   *
 690   * @access public
 691   * @param   array       Smarty parameter input array:
 692   *                          entry: The $entry data for the trackbacks
 693   * @param   object  Smarty object
 694   * @return
 695   */
 696  function &serendipity_smarty_printTrackbacks($params, &$smarty) {
 697      if ( !isset($params['entry']) ) {
 698          $smarty->trigger_error(__FUNCTION__ .": missing 'entry' parameter");
 699          return;
 700      }
 701  
 702      $tb =& serendipity_fetchTrackbacks($params['entry']);
 703      return serendipity_printTrackbacks($tb);
 704  }
 705  
 706  /**
 707   * Smarty Prefilter: Replace constants to direkt $smarty.const. access
 708   *
 709   * @access public
 710   * @param   string  Template source content
 711   * @param   object  Smarty object
 712   * @return
 713   */
 714  function &serendipity_replaceSmartyVars(&$tpl_source, &$smarty) {
 715      $tpl_source = str_replace('$CONST.', '$smarty.const.', $tpl_source);
 716      return $tpl_source;
 717  }
 718  
 719  /**
 720   * Initialize the Smarty framework for use in Serendipity
 721   *
 722   * @access public
 723   * @return null
 724   */
 725  function serendipity_smarty_init($vars = array()) {
 726      global $serendipity, $template_config;
 727  
 728      if (!isset($serendipity['smarty'])) {
 729          $template_dir = $serendipity['serendipityPath'] . $serendipity['templatePath'] . $serendipity['template'];
 730          if (!defined('IN_serendipity_admin') && file_exists($template_dir . '/template.inc.php')) {
 731              // If this file exists, a custom template engine will be loaded.
 732              // Beware: Smarty is used in the Admin backend, despite of this.
 733              include $template_dir . '/template.inc.php';
 734          } else {
 735              // Set a session variable if Smarty fails:
 736              $prev_smarty = $_SESSION['no_smarty'];
 737              $_SESSION['no_smarty'] = true;
 738  
 739              // Default Smarty Engine will be used
 740              @define('SMARTY_DIR', S9Y_PEAR_PATH . 'Smarty/libs/');
 741              if (!class_exists('Smarty')) {
 742                  include SMARTY_DIR . 'Smarty.class.php';
 743              }
 744  
 745              if (!class_exists('Smarty')) {
 746                  return false;
 747              }
 748  
 749              $serendipity['smarty'] = new Smarty;
 750              if ($serendipity['production'] === 'debug') {
 751                  $serendipity['smarty']->force_compile   = true;
 752                  $serendipity['smarty']->debugging       = true;
 753              }
 754  
 755              $serendipity['smarty']->template_dir  = array(
 756                  $template_dir,
 757                  $serendipity['serendipityPath'] . $serendipity['templatePath'] . 'default'
 758              );
 759              $serendipity['smarty']->compile_dir   = $serendipity['serendipityPath'] . PATH_SMARTY_COMPILE;
 760  
 761              if (!is_dir($serendipity['smarty']->compile_dir) || !is_writable($serendipity['smarty']->compile_dir)) {
 762                  printf(DIRECTORY_WRITE_ERROR, $serendipity['smarty']->compile_dir);
 763                  return false;
 764              }
 765  
 766              // Hooray for Smarty:
 767              $_SESSION['no_smarty'] = $prev_smarty;
 768  
 769              $serendipity['smarty']->config_dir    = $template_dir;
 770              $serendipity['smarty']->secure_dir    = array($serendipity['serendipityPath'] . $serendipity['templatePath']);
 771              $serendipity['smarty']->security_settings['MODIFIER_FUNCS']  = array('sprintf', 'sizeof', 'count', 'rand', 'print_r', 'str_repeat');
 772              $serendipity['smarty']->security_settings['ALLOW_CONSTANTS'] = true;
 773              $serendipity['smarty']->security      = true;
 774              $serendipity['smarty']->use_sub_dirs  = false;
 775              $serendipity['smarty']->compile_check = true;
 776              $serendipity['smarty']->compile_id    = &$serendipity['template'];
 777  
 778              $serendipity['smarty']->register_modifier('makeFilename', 'serendipity_makeFilename');
 779              $serendipity['smarty']->register_modifier('xhtml_target', 'serendipity_xhtml_target');
 780              $serendipity['smarty']->register_modifier('emptyPrefix', 'serendipity_emptyPrefix');
 781              $serendipity['smarty']->register_modifier('formatTime', 'serendipity_smarty_formatTime');
 782              $serendipity['smarty']->register_modifier('serendipity_utf8_encode', 'serendipity_utf8_encode');
 783              $serendipity['smarty']->register_modifier('ifRemember', 'serendipity_ifRemember');
 784              $serendipity['smarty']->register_modifier('checkPermission', 'serendipity_checkPermission');
 785              $serendipity['smarty']->register_modifier('serendipity_refhookPlugin', 'serendipity_smarty_refhookPlugin');
 786  
 787              $serendipity['smarty']->register_function('serendipity_printSidebar', 'serendipity_smarty_printSidebar');
 788              $serendipity['smarty']->register_function('serendipity_hookPlugin', 'serendipity_smarty_hookPlugin');
 789              $serendipity['smarty']->register_function('serendipity_showPlugin', 'serendipity_smarty_showPlugin');
 790              $serendipity['smarty']->register_function('serendipity_getFile', 'serendipity_smarty_getFile');
 791              $serendipity['smarty']->register_function('serendipity_printComments', 'serendipity_smarty_printComments');
 792              $serendipity['smarty']->register_function('serendipity_printTrackbacks', 'serendipity_smarty_printTrackbacks');
 793              $serendipity['smarty']->register_function('serendipity_rss_getguid', 'serendipity_smarty_rss_getguid');
 794              $serendipity['smarty']->register_function('serendipity_fetchPrintEntries', 'serendipity_smarty_fetchPrintEntries');
 795              $serendipity['smarty']->register_function('serendipity_getTotalCount', 'serendipity_smarty_getTotalCount');
 796              $serendipity['smarty']->register_function('pickKey', 'serendipity_smarty_pickKey');
 797              $serendipity['smarty']->register_function('serendipity_showCommentForm', 'serendipity_smarty_showCommentForm');
 798              $serendipity['smarty']->register_prefilter('serendipity_replaceSmartyVars');
 799          }
 800  
 801          if (!isset($serendipity['smarty_raw_mode'])) {
 802              if (file_exists($serendipity['smarty']->config_dir . '/layout.php') && $serendipity['template'] != 'default') {
 803                  $serendipity['smarty_raw_mode'] = true;
 804              } else {
 805                  $serendipity['smarty_raw_mode'] = false;
 806              }
 807          }
 808  
 809          if (!isset($serendipity['smarty_file'])) {
 810              $serendipity['smarty_file'] = 'index.tpl';
 811          }
 812  
 813          $category      = false;
 814          $category_info = array();
 815          if (isset($serendipity['GET']['category'])) {
 816              $category = (int)$serendipity['GET']['category'];
 817              if (isset($GLOBALS['cInfo'])) {
 818                  $category_info = $GLOBALS['cInfo'];
 819              } else {
 820                  $category_info = serendipity_fetchCategoryInfo($category);
 821              }
 822          }
 823  
 824          if (!isset($serendipity['smarty_vars']['head_link_stylesheet'])) {
 825              $serendipity['smarty_vars']['head_link_stylesheet'] = serendipity_rewriteURL('serendipity.css');
 826          }
 827  
 828          $serendipity['smarty']->assign(
 829              array(
 830                  'head_charset'              => LANG_CHARSET,
 831                  'head_version'              => $serendipity['version'],
 832                  'head_title'                => $serendipity['head_title'],
 833                  'head_subtitle'             => $serendipity['head_subtitle'],
 834                  'head_link_stylesheet'      => $serendipity['smarty_vars']['head_link_stylesheet'],
 835  
 836                  'is_xhtml'                  => true,
 837                  'use_popups'                => $serendipity['enablePopup'],
 838                  'is_embedded'               => (!$serendipity['embed'] || $serendipity['embed'] === 'false' || $serendipity['embed'] === false) ? false : true,
 839                  'is_raw_mode'               => $serendipity['smarty_raw_mode'],
 840                  'is_logged_in'              => serendipity_userLoggedIn(),
 841  
 842                  'entry_id'                  => (isset($serendipity['GET']['id']) && is_numeric($serendipity['GET']['id'])) ? $serendipity['GET']['id'] : false,
 843                  'is_single_entry'           => (isset($serendipity['GET']['id']) && is_numeric($serendipity['GET']['id'])),
 844  
 845                  'blogTitle'                 => htmlspecialchars($serendipity['blogTitle']),
 846                  'blogSubTitle'              => (!empty($serendipity['blogSubTitle']) ? htmlspecialchars($serendipity['blogSubTitle']) : ''),
 847                  'blogDescription'           => htmlspecialchars($serendipity['blogDescription']),
 848  
 849                  'serendipityHTTPPath'       => $serendipity['serendipityHTTPPath'],
 850                  'serendipityBaseURL'        => $serendipity['baseURL'],
 851                  'serendipityRewritePrefix'  => $serendipity['rewrite'] == 'none' ? $serendipity['indexFile'] . '?/' : '',
 852                  'serendipityIndexFile'      => $serendipity['indexFile'],
 853                  'serendipityVersion'        => $serendipity['version'],
 854  
 855                  'lang'                      => $serendipity['lang'],
 856                  'category'                  => $category,
 857                  'category_info'             => $category_info,
 858                  'template'                  => $serendipity['template'],
 859  
 860                  'dateRange'                 => (!empty($serendipity['range']) ? $serendipity['range'] : array())
 861              )
 862          );
 863  
 864          if (count($vars) > 0) {
 865              $serendipity['smarty']->assign($vars);
 866          }
 867  
 868          // For advanced usage, we allow template authors to create a file 'config.inc.php' where they can
 869          // setup custom smarty variables, modifiers etc. to use in their templates.
 870          @include_once $serendipity['smarty']->config_dir . '/config.inc.php';
 871  
 872          if (is_array($template_loaded_config)) {
 873              $template_vars =& $template_loaded_config;
 874              $serendipity['smarty']->assign_by_ref('template_option', $template_vars);
 875          } elseif (is_array($template_config)) {
 876              $template_vars =& serendipity_loadThemeOptions($template_config, $serendipity['smarty_vars']['template_option']);
 877              $serendipity['smarty']->assign_by_ref('template_option', $template_vars);
 878          }
 879      }
 880  
 881      return true;
 882  }
 883  
 884  /**
 885   * Purge compiled Smarty Templates
 886   *
 887   * @access public
 888   * @return null
 889   */
 890  function serendipity_smarty_purge() {
 891      global $serendipity;
 892  
 893      /* Attempt to init Smarty, brrr */
 894      serendipity_smarty_init();
 895  
 896      $files = serendipity_traversePath($serendipity['smarty']->compile_dir, '', false, '/.+\.tpl\.php$/');
 897  
 898      if ( !is_array($files) ) {
 899          return false;
 900      }
 901  
 902      foreach ( $files as $file ) {
 903          @unlink($serendipity['smarty']->compile_dir . DIRECTORY_SEPARATOR . $file['name']);
 904      }
 905  }
 906  
 907  /**
 908   * Shut down the Smarty Framework, output all parsed data
 909   *
 910   * This function is meant to be used in embedded installations, like in Gallery.
 911   * Function can be called from foreign applications. ob_start() needs to
 912   * have been called before, and will be parsed into Smarty here
 913   *
 914   * @access public
 915   * @param   string  The path to Serendipity
 916   * @return null
 917   */
 918  function serendipity_smarty_shutdown($serendipity_directory = '') {
 919  global $serendipity;
 920  
 921      #$cwd = getcwd();
 922      chdir($serendipity_directory);
 923      $raw_data = ob_get_contents();
 924      ob_end_clean();
 925      $serendipity['smarty']->assign_by_ref('content_message', $raw_data);
 926  
 927      serendipity_smarty_fetch('CONTENT', 'content.tpl');
 928      $serendipity['smarty']->assign('ENTRIES', '');
 929      if (empty($serendipity['smarty_file'])) {
 930          $serendipity['smarty_file'] = '404.tpl';
 931      }
 932      $serendipity['smarty']->display(serendipity_getTemplateFile($serendipity['smarty_file'], 'serendipityPath'));
 933  }


Généré le : Sat Nov 24 09:00:37 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics