plugins/staticpages/functions.inc
author Sami Barakat
Sun, 18 Oct 2009 02:06:26 +0100
branchHEAD
changeset 7388 dbf2c1e5cedc
parent 7354 82e167a0e3a4
child 7390 2b5aac1d58f8
permissions -rw-r--r--
Static page comments are now searchable (cf. bug #0000902)
     1 <?php
     2 
     3 /* Reminder: always indent with 4 spaces (no tabs). */
     4 // +---------------------------------------------------------------------------+
     5 // | Static Pages Plugin 1.6                                                   |
     6 // +---------------------------------------------------------------------------+
     7 // | functions.inc                                                             |
     8 // |                                                                           |
     9 // | This file does two things: 1) it implements the necessary Geeklog Plugin  |
    10 // | API method and 2) implements all the common code needed by the Static     |
    11 // | Pages' PHP files.                                                         |
    12 // +---------------------------------------------------------------------------+
    13 // | Copyright (C) 2000-2009 by the following authors:                         |
    14 // |                                                                           |
    15 // | Authors: Tony Bibbs       - tony AT tonybibbs DOT com                     |
    16 // |          Tom Willett      - twillett AT users DOT sourceforge DOT net     |
    17 // |          Blaine Lang      - blaine AT portalparts DOT com                 |
    18 // |          Dirk Haun        - dirk AT haun-online DOT de                    |
    19 // +---------------------------------------------------------------------------+
    20 // |                                                                           |
    21 // | This program is free software; you can redistribute it and/or             |
    22 // | modify it under the terms of the GNU General Public License               |
    23 // | as published by the Free Software Foundation; either version 2            |
    24 // | of the License, or (at your option) any later version.                    |
    25 // |                                                                           |
    26 // | This program is distributed in the hope that it will be useful,           |
    27 // | but WITHOUT ANY WARRANTY; without even the implied warranty of            |
    28 // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             |
    29 // | GNU General Public License for more details.                              |
    30 // |                                                                           |
    31 // | You should have received a copy of the GNU General Public License         |
    32 // | along with this program; if not, write to the Free Software Foundation,   |
    33 // | Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.           |
    34 // |                                                                           |
    35 // +---------------------------------------------------------------------------+
    36 
    37 /**
    38 * Implementation of the Plugin API for the Static Pages plugin
    39 *
    40 * @package StaticPages
    41 */
    42 
    43 if (strpos(strtolower($_SERVER['PHP_SELF']), 'functions.inc') !== false) {
    44     die('This file can not be used on its own.');
    45 }
    46 
    47 $plugin_path = $_CONF['path'] . 'plugins/staticpages/';
    48 
    49 /**
    50 * Language file include
    51 */
    52 $langfile = $plugin_path . 'language/' . $_CONF['language'] . '.php';
    53 if (file_exists($langfile)) {
    54     require_once $langfile;
    55 } else {
    56     require_once $plugin_path . 'language/english.php';
    57 }
    58 
    59 /**
    60 * Check and see if we need to load the plugin configuration
    61 */
    62 if (!isset($_SP_CONF['allow_php'])) {
    63     require_once $_CONF['path_system'] . 'classes/config.class.php';
    64 
    65     $sp_config = config::get_instance();
    66     $_SP_CONF = $sp_config->get_config('staticpages');
    67 }
    68 
    69 
    70 // +---------------------------------------------------------------------------+
    71 // | Plugin API - Services                                                     |
    72 // +---------------------------------------------------------------------------+
    73 
    74 include_once $plugin_path . 'services.inc.php';
    75 
    76 // +---------------------------------------------------------------------------+
    77 // | Geeklog Plugin API Implementations                                        |
    78 // +---------------------------------------------------------------------------+
    79 
    80 /**
    81 * Returns the items for this plugin that should appear on the main menu
    82 *
    83 * NOTE: this MUST return the url/value pairs in the following format
    84 * $<arrayname>[<label>] = <url>
    85 *
    86 */
    87 function plugin_getmenuitems_staticpages()
    88 {
    89     global $_CONF, $_TABLES, $_SP_CONF;
    90 
    91     $order = '';
    92     if (!empty ($_SP_CONF['sort_menu_by'])) {
    93         $order = ' ORDER BY ';
    94         if ($_SP_CONF['sort_menu_by'] == 'date') {
    95             $order .= 'sp_date DESC';
    96         } else if ($_SP_CONF['sort_menu_by'] == 'label') {
    97             $order .= 'sp_label';
    98         } else if ($_SP_CONF['sort_menu_by'] == 'title') {
    99             $order .= 'sp_title';
   100         } else { // default to "sort by id"
   101             $order .= 'sp_id';
   102         }
   103     }
   104 
   105     $result = DB_query('SELECT sp_id, sp_label FROM ' . $_TABLES['staticpage'] . ' WHERE sp_onmenu = 1' . COM_getPermSql('AND') . COM_getLangSql('sp_id', 'AND') . $order);
   106     $nrows = DB_numRows ($result);
   107     $menuitems = array ();
   108     for ($i = 0; $i < $nrows; $i++) {
   109         $A = DB_fetchArray ($result);
   110         $menuitems[$A['sp_label']] = COM_buildURL ($_CONF['site_url'] . '/staticpages/index.php?page=' . $A['sp_id']);
   111     }
   112 
   113     return $menuitems;
   114 }
   115 
   116 /**
   117 * Plugin should display [a] comment[s]
   118 *
   119 * @param   string  $id     Unique idenifier for item comment belongs to
   120 * @param   int     $cid    Comment id to display (possibly including sub-comments)
   121 * @param   string  $title  Page/comment title
   122 * @param   string  $order  'ASC' or 'DSC' or blank
   123 * @param   string  $format 'threaded', 'nested', or 'flat'
   124 * @param   int     $page   Page number of comments to display
   125 * @param   boolean $view   True to view comment (by cid), false to display (by $pid)
   126 * @return  mixed   results of calling the plugin_displaycomment_ function
   127 */
   128 function plugin_displaycomment_staticpages ($id, $cid, $title, $order, $format, $page, $view)
   129 {
   130     global $_TABLES, $LANG_ACCESS;
   131 
   132     $retval = '';
   133 
   134     $sql = "SELECT COUNT(*) AS count, commentcode, owner_id, group_id, perm_owner, "
   135         . "perm_group, perm_members, perm_anon "
   136         . "FROM {$_TABLES['staticpage']} "
   137         . "WHERE (sp_id = '$id')" . COM_getPermSQL('AND')
   138         . ' GROUP BY sp_id';
   139     $result = DB_query ($sql);
   140     $A = DB_fetchArray ($result);
   141     $allowed = $A['count'];
   142 
   143     if ($allowed == 1) {
   144         $delete_option = (SEC_hasRights ('staticpages.edit') &&
   145                 (SEC_hasAccess ($A['owner_id'], $A['group_id'],
   146                     $A['perm_owner'], $A['perm_group'], $A['perm_members'],
   147                     $A['perm_anon']) == 3));
   148         $retval .= CMT_userComments ($id, $title, 'staticpages', $order, $format,
   149                                      $cid, $page, $view, $delete_option,
   150                                      $A['commentcode']);
   151     } else {
   152         $retval .= COM_startBlock ($LANG_ACCESS['accessdenied'], '',
   153                         COM_getBlockTemplate ('_msg_block', 'header'))
   154                 . $LANG_ACCESS['storydenialmsg']
   155                 . COM_endBlock (COM_getBlockTemplate ('_msg_block', 'footer'));
   156     }
   157 
   158     return $retval;
   159 }
   160 
   161 /**
   162  * Static Page saves a comment
   163  *
   164  * @param   string  $title  comment title
   165  * @param   string  $comment comment text
   166  * @param   string  $id     Item id to which $cid belongs
   167  * @param   int     $pid    comment parent
   168  * @param   string  $postmode 'html' or 'text'
   169  * @return  mixed   false for failure, HTML string (redirect?) for success
   170  */
   171 function plugin_savecomment_staticpages($title, $comment, $id, $pid, $postmode)
   172 {
   173     global $_CONF, $_TABLES, $LANG03, $_USER;
   174 
   175     $retval = '';
   176 
   177     $commentcode = DB_getItem($_TABLES['staticpage'], 'commentcode',
   178                               "sp_id = '$id'");
   179     if ($commentcode != 0) {
   180         return COM_refresh($_CONF['site_url'] . '/index.php');
   181     }
   182 
   183     $ret = CMT_saveComment($title, $comment, $id, $pid, 'staticpages', $postmode);
   184     if ($ret > 0) { // failure //FIXME: some failures should not return to comment form
   185         $retval .= COM_siteHeader('menu', $LANG03[1])
   186                 . CMT_commentForm($title, $comment, $id, $pid, 'staticpages',
   187                                   $LANG03[14], $postmode)
   188                 . COM_siteFooter();
   189     } else { // success
   190         $msg = '';
   191         if (($_CONF['commentsubmission'] == 1) &&
   192                 !SEC_hasRights('comment.submit')) {
   193             $msg = '&msg=15';
   194         }
   195         $retval = COM_refresh ($_CONF['site_url']
   196                                 . "/staticpages/index.php?page=$id$msg");
   197     }
   198 
   199     return $retval;
   200 }
   201 
   202 /**
   203  * staticpages: delete a comment
   204  *
   205  * @param   int     $cid    Comment to be deleted
   206  * @param   string  $id     Item id to which $cid belongs
   207  * @return  mixed   false for failure, HTML string (redirect?) for success
   208  */
   209 function plugin_deletecomment_staticpages($cid, $id)
   210 {
   211     global $_CONF, $_TABLES, $_USER;
   212 
   213     $retval = '';
   214 
   215     $has_editPermissions = SEC_hasRights ('staticpages.edit');
   216     $result = DB_query ("SELECT owner_id,group_id,perm_owner,perm_group,perm_members,perm_anon "
   217         . "FROM {$_TABLES['staticpage']} WHERE sp_id = '{$id}'");
   218     $A = DB_fetchArray ($result);
   219 
   220     if ($has_editPermissions && SEC_hasAccess ($A['owner_id'],
   221             $A['group_id'], $A['perm_owner'], $A['perm_group'],
   222             $A['perm_members'], $A['perm_anon']) == 3) {
   223         CMT_deleteComment($cid, $id, 'staticpages');
   224         $retval .= COM_refresh ($_CONF['site_url']
   225                                  . "/staticpages/index.php?page=$id");
   226     } else {
   227         COM_errorLog ("User {$_USER['username']} (IP: {$_SERVER['REMOTE_ADDR']}) "
   228                     . "tried to illegally delete comment $cid from staticpage $id");
   229         $retval .= COM_refresh ($_CONF['site_url'] . '/index.php');
   230     }
   231 
   232     return $retval;
   233 }
   234 
   235 /**
   236 * Helper function: Count static pages visible to the current user
   237 *
   238 * @return   int     number of pages
   239 *
   240 */
   241 function SP_countVisiblePages ()
   242 {
   243     global $_TABLES;
   244 
   245     $perms = SP_getPerms ();
   246     if (!empty ($perms)) {
   247         $perms = ' WHERE ' . $perms;
   248     }
   249     $result = DB_query ("SELECT COUNT(*) AS cnt FROM {$_TABLES['staticpage']}" . $perms);
   250     $A = DB_fetchArray ($result);
   251 
   252     return $A['cnt'];
   253 }
   254 
   255 /**
   256 * Prepare static page for display.
   257 *
   258 * @param    string  $page           static page id
   259 * @param    array   $A              static page data
   260 * @param    string  $comment_order  sorting of comments
   261 * @param    string  $comment_mode   comment mode (nested, flat, etc.)
   262 * @param    int     $msg            optional message number
   263 * @return   string                  HTML for the static page
   264 *
   265 */
   266 function SP_displayPage ($page, $A, $comment_order = 'ASC', $comment_mode = 'nested', $msg = 0)
   267 {
   268     global $_CONF, $_TABLES, $_USER,
   269            $LANG01, $LANG11, $LANG_STATIC, $_IMAGE_TYPE, $_SP_CONF;
   270 
   271     $retval = '';
   272 
   273     $sp_url = COM_buildUrl($_CONF['site_url'] . '/staticpages/index.php?page='
   274                            . $page);
   275     $headercode = '<link rel="canonical" href="' . $sp_url . '"' . XHTML . '>';
   276 
   277     // Meta Tags
   278     If ($_SP_CONF['meta_tags'] > 0) {
   279         $meta_description  = $A['meta_description'];
   280         $meta_keywords  = $A['meta_keywords'];
   281         $headercode .= COM_createMetaTags($meta_description, $meta_keywords);
   282     }
   283 
   284     if ($A['sp_format'] == 'allblocks' OR $A['sp_format'] == 'leftblocks') {
   285         $retval .= COM_siteHeader('menu', $A['sp_title'], $headercode);
   286     } else {
   287         if ($A['sp_format'] <> 'blankpage') {
   288             $retval .= COM_siteHeader('none', $A['sp_title'], $headercode);
   289         }
   290     }
   291     if ($msg > 0) {
   292         $retval .= COM_showMessage($msg, 'staticpages');
   293     }
   294     if (($A['sp_inblock'] == 1) && ($A['sp_format'] != 'blankpage')) {
   295         $retval .= COM_startBlock (stripslashes ($A['sp_title']), $A['sp_help'],
   296                         COM_getBlockTemplate ('_staticpages_block', 'header'));
   297     }
   298 
   299     $spage = new Template( $_CONF['path'] . 'plugins/staticpages/templates/' );
   300     $spage->set_var('xhtml', XHTML);
   301     if (XHTML != '') {
   302         $spage->set_var('xmlns', ' xmlns="http://www.w3.org/1999/xhtml"');
   303     }
   304     $spage->set_var('site_url', $_CONF['site_url']);
   305     $spage->set_var('layout_url', $_CONF['layout_url']);
   306     $spage->set_var('site_admin_url', $_CONF['site_admin_url']);
   307     $spage -> set_file( array('page'=>'staticpage.thtml',
   308         'comments' => 'spcomments.thtml'));
   309 
   310     if ($A['sp_format'] <> 'blankpage') {
   311         if ($_CONF['hideprintericon'] == 0) {
   312             $icon_url = $_CONF['layout_url'] . '/images/print.' . $_IMAGE_TYPE;
   313             $attr = array('title' => $LANG_STATIC['printable_format']);
   314             $printicon = COM_createImage($icon_url, $LANG01[65], $attr);
   315             $print_url = COM_buildURL ($_CONF['site_url']
   316                 . '/staticpages/index.php?page=' . $page . '&amp;disp_mode=print');
   317             $icon = COM_createLink($printicon, $print_url);
   318             $spage->set_var('print_icon', $icon);
   319         }
   320         if ((SEC_hasAccess ($A['owner_id'], $A['group_id'], $A['perm_owner'],
   321                 $A['perm_group'], $A['perm_members'], $A['perm_anon']) == 3) &&
   322                 SEC_hasRights ('staticpages.edit')) {
   323             $icon_url = $_CONF['layout_url'] . '/images/edit.' . $_IMAGE_TYPE;
   324             $attr = array('title' => $LANG_STATIC['edit']);
   325             $editiconhtml = COM_createImage($icon_url, $LANG_STATIC['edit'], $attr);
   326             $attr = array('class' => 'editlink','title' => $LANG_STATIC['edit']);
   327             $url = $_CONF['site_admin_url']
   328                 . '/plugins/staticpages/index.php?mode=edit&amp;sp_id=' . $page;
   329             $icon =
   330                 '&nbsp;' . COM_createLink(
   331                 $editiconhtml, //display
   332                 $url,  //target
   333                 $attr //other attributes
   334             );
   335             $spage->set_var('edit_icon', $icon);
   336         }
   337     }
   338 
   339     if($A['commentcode'] >= 0 ) {
   340         $delete_option = (SEC_hasRights('staticpages.edit') &&
   341             SEC_hasAccess($A['owner_id'], $A['group_id'],
   342             $A['perm_owner'], $A['perm_group'], $A['perm_members'],
   343             $A['perm_anon']) == 3 ? true : false);
   344         require_once $_CONF['path_system'] . 'lib-comment.php';
   345         $spage->set_var ('commentbar', CMT_userComments($page, $A['sp_title'], 'staticpages',
   346                                     $comment_order, $comment_mode, 0, 1, false,
   347                                     $delete_option, $A['commentcode']));
   348     }
   349 
   350 
   351     $content = SP_render_content (stripslashes ($A['sp_content']), $A['sp_php']);
   352     $spage->set_var('content', $content );
   353 
   354     $spage->set_var('info_separator', 'hidden');
   355     if ($A['sp_format'] <> 'blankpage') {
   356         $curtime = COM_getUserDateTimeFormat ($A['sp_date']);
   357         if ($_SP_CONF['show_date'] == 1) {
   358             $lastupdate = $LANG_STATIC['lastupdated']. ' ' . $curtime[0];
   359             $spage->set_var('lastupdate', $lastupdate);
   360         }
   361 
   362         if ($_SP_CONF['show_hits'] == 1) {
   363             if ($_SP_CONF['show_date'] == 1) {
   364                 $spage->set_var('info_separator','visible');
   365             }
   366             $hits = COM_numberFormat ($A['sp_hits']) . ' ' . $LANG_STATIC['hits'];
   367             $spage->set_var('hits', $hits);
   368         }
   369     }
   370 
   371     $retval .= $spage->finish($spage->parse('output', 'page'));
   372     if (($A['sp_inblock'] == 1) && ($A['sp_format'] != 'blankpage')) {
   373         $retval .= COM_endBlock (COM_getBlockTemplate ('_staticpages_block',
   374                                                        'footer'));
   375     }
   376 
   377     if ($A['sp_format'] <> 'blankpage') {
   378         if (!isset($_USER['noboxes'])) {
   379             if (empty($_USER['uid']) || ($_USER['uid'] == 1)) {
   380                 $_USER['noboxes'] = 0;
   381             } else {
   382                 $_USER['noboxes'] = DB_getItem($_TABLES['userindex'],
   383                                         'noboxes', "uid = '{$_USER['uid']}'");
   384             }
   385         }
   386 
   387         if (($A['sp_format'] == 'allblocks') && ($_USER['noboxes'] != 1)) {
   388             $retval .= COM_siteFooter(true);
   389         } else if (($A['sp_format'] == 'leftblocks') || ($A['sp_format'] == 'noblocks')) {
   390             $retval .= COM_siteFooter(false);
   391         } else {
   392             $retval .= COM_siteFooter();
   393         }
   394     }
   395 
   396     return $retval;
   397 }
   398 
   399 /**
   400 * Prepare static page for print (i.e. display as "printable version").
   401 *
   402 * @param    string  $page       static page id
   403 * @param    array   $A          static page data
   404 * @return   string              HTML for the static page
   405 *
   406 */
   407 function SP_printPage($page, $A)
   408 {
   409     global $_CONF, $_TABLES, $LANG01, $LANG_DIRECTION;
   410 
   411     $template_path = staticpages_templatePath();
   412     $print = new Template($template_path);
   413     $print->set_file(array('print' => 'printable.thtml'));
   414     $print->set_var('site_url', $_CONF['site_url']);
   415     $print->set_var('site_admin_url', $_CONF['site_admin_url']);
   416     $print->set_var('layout_url', $_CONF['layout_url']);
   417     $print->set_var('site_name', $_CONF['site_name']);
   418     $print->set_var('site_slogan', $_CONF['site_slogan']);
   419 
   420     $print->set_var('direction', $LANG_DIRECTION);
   421     COM_setLangIdAndAttribute($print);
   422     $print->set_var('xhtml', XHTML);
   423     $print->set_var('page_title', $_CONF['site_name'] . ' - '
   424                                   . stripslashes($A['sp_title']));
   425     $sp_url = COM_buildUrl($_CONF['site_url']
   426                                   . '/staticpages/index.php?page=' . $page);
   427     $print->set_var('sp_url', $sp_url);
   428     $print->set_var('sp_title', stripslashes($A['sp_title']));
   429     $print->set_var('sp_content',
   430             SP_render_content(stripslashes($A['sp_content']), $A['sp_php']));
   431     $print->set_var('sp_hits', COM_numberFormat($A['sp_hits']));
   432     $printable = COM_buildURL($_CONF['site_url']
   433                . '/staticpages/index.php?page=' . $page . '&amp;mode=print');
   434     $print->set_var('printable_url', $printable);
   435     if ($A['commentcode'] >= 0) {
   436         $commentsUrl = $sp_url . '#comments';
   437         $comments = DB_count($_TABLES['comments'],
   438                              array('sid', 'type'), array($page, 'staticpages'));
   439         $numComments = COM_numberFormat($comments);
   440         $print->set_var('story_comments', $numComments);
   441         $print->set_var('comments_url', $commentsUrl);
   442         $print->set_var('comments_text', $numComments . ' ' . $LANG01[3]);
   443         $print->set_var('comments_count', $numComments);
   444         $print->set_var('lang_comments', $LANG01[3]);
   445         $comments_with_count = sprintf($LANG01[121], $numComments);
   446 
   447         if ($comments > 0) {
   448             $comments_with_count = COM_createLink($comments_with_count,
   449                                                   $commentsUrl);
   450         }
   451         $print->set_var('comments_with_count', $comments_with_count);
   452     }
   453     $print->parse('output', 'print');
   454 
   455     return $print->finish($print->get_var('output'));
   456 }
   457 
   458 /**
   459 * Prepare static page for display
   460 *
   461 * @param    string  $page           static page id
   462 * @param    string  $mode           type of display to return ('', 'print', 'autotag'
   463 * @param    string  $comment_order  sorting of comments
   464 * @param    string  $comment_mode   comment mode (nested, flat, etc.)
   465 * @param    int     $msg            optional message number
   466 * @return   string                  HTML for the static page
   467 *
   468 */
   469 function SP_returnStaticpage($page='', $mode='', $comment_order = 'ASC', $comment_mode = 'nested', $msg = 0)
   470 {
   471     global $_CONF, $_TABLES, $LANG_ACCESS, $LANG_STATIC, $LANG_LOGIN;
   472 
   473     $retval = '';
   474 
   475     $args = array(
   476                 'sp_id' => $page,
   477                 'mode'  => $mode
   478                  );
   479     $svc_msg = array();
   480 
   481     if (PLG_invokeService('staticpages', 'get', $args, $retval, $svc_msg) == PLG_RET_OK) {
   482 
   483         if ($mode == 'print') {
   484             $retval = SP_printPage($page, $retval);
   485         } else if ($mode =='autotag') {
   486             $retval = SP_render_content(stripslashes($retval['sp_content']), $retval['sp_php']);
   487         } else {
   488             $retval = SP_displayPage($page, $retval, $comment_order, $comment_mode, $msg);
   489         }
   490 
   491         // increment hit counter for page
   492         DB_query("UPDATE {$_TABLES['staticpage']} SET sp_hits = sp_hits + 1 WHERE sp_id = '$page'");
   493 
   494     }
   495 
   496     return $retval;
   497 }
   498 
   499 /**
   500 * Shows the statistics for the Static Pages plugin on stats.php.
   501 * If $showsitestats is 1 then we are to only print the overall stats in the
   502 * 'site statistics box' otherwise we show the detailed stats
   503 *
   504 * @param    int     showsitestate   Flag to let us know which stats to get
   505 */
   506 function plugin_showstats_staticpages($showsitestats)
   507 {
   508     global $_CONF, $_TABLES, $LANG_STATIC;
   509 
   510     $retval = '';
   511 
   512     $perms = SP_getPerms();
   513     if (!empty($perms)) {
   514         $perms = ' AND ' . $perms;
   515     }
   516     $result = DB_query("SELECT sp_id,sp_title,sp_hits FROM {$_TABLES['staticpage']} WHERE sp_hits > 0" . $perms . ' ORDER BY sp_hits DESC LIMIT 10');
   517     $nrows  = DB_numRows($result);
   518     if ($nrows > 0) {
   519         require_once $_CONF['path_system'] . 'lib-admin.php';
   520 
   521         $header_arr = array(
   522             array('text'         => $LANG_STATIC['stats_page_title'],
   523                   'field'        => 'sid',
   524                   'header_class' => 'stats-header-title'),
   525             array('text'         => $LANG_STATIC['stats_hits'],
   526                   'field'        => 'sp_hits',
   527                   'header_class' => 'stats-header-count',
   528                   'field_class'  => 'stats-list-count'),
   529         );
   530         $data_arr = array();
   531         $text_arr = array('has_menu' => false,
   532                           'title'    => $LANG_STATIC['stats_headline']
   533         );
   534         for ($i = 0; $i < $nrows; $i++) {
   535             $A = DB_fetchArray($result);
   536             $A['sp_title'] = stripslashes($A['sp_title']);
   537             $A['sid'] = COM_createLink($A['sp_title'],
   538                             COM_buildUrl($_CONF['site_url']
   539                                 . "/staticpages/index.php?page={$A['sp_id']}"));
   540             $A['sp_hits'] = COM_NumberFormat($A['sp_hits']);
   541             $data_arr[$i] = $A;
   542         }
   543         $retval .= ADMIN_simpleList("", $header_arr, $text_arr, $data_arr);
   544     } else {
   545         $retval .= COM_startBlock($LANG_STATIC['stats_headline']);
   546         $retval .= $LANG_STATIC['stats_no_hits'];
   547         $retval .= COM_endBlock();
   548     }
   549 
   550     return $retval;
   551 }
   552 
   553 /**
   554 * New stats plugin API function for proper integration with the site stats
   555 *
   556 * @return   array(item text, item count);
   557 *
   558 */
   559 function plugin_statssummary_staticpages()
   560 {
   561     global $LANG_STATIC;
   562 
   563     $total_pages = SP_countVisiblePages();
   564 
   565     return array($LANG_STATIC['staticpages'], COM_numberFormat($total_pages));
   566 }
   567 
   568 /**
   569 * Geeklog is asking us to provide any new items that show up in the type
   570 * drop-down on search.php.  Let's let users search static pages!
   571 *
   572 */
   573 function plugin_searchtypes_staticpages()
   574 {
   575     global $LANG_STATIC;
   576 
   577     $tmp['staticpages'] = $LANG_STATIC['staticpages'];
   578 
   579     return $tmp;
   580 }
   581 
   582 
   583 /**
   584 * this searches for static pages matching the user query and returns an array of
   585 * for the header and table rows back to search.php where it will be formated and
   586 * printed
   587 *
   588 * @param    string  $query      Keywords user is looking for
   589 * @param    date    $datestart  Start date to get results for
   590 * @param    date    $dateend    End date to get results for
   591 * @param    string  $topic      The topic they were searching in
   592 * @param    string  $type       Type of items they are searching, or 'all' (deprecated)
   593 * @param    int     $author     Get all results by this author
   594 * @param    string  $keyType    search key type: 'all', 'phrase', 'any'
   595 * @param    int     $page       page number of current search (deprecated)
   596 * @param    int     $perpage    number of results per page (deprecated)
   597 *
   598 */
   599 function plugin_dopluginsearch_staticpages($query, $datestart, $dateend, $topic, $type, $author, $keyType, $page, $perpage)
   600 {
   601     global $_TABLES, $_DB_dbms, $LANG_STATIC, $LANG09;
   602 
   603     // Make sure the query is SQL safe
   604     $query = trim(addslashes($query));
   605 
   606     $sql = "SELECT sp.sp_id AS id, sp.sp_title AS title, sp.sp_content AS description, ";
   607     $sql .= "UNIX_TIMESTAMP(sp.sp_date) AS date, sp.sp_uid AS uid, sp.sp_hits AS hits, ";
   608     $sql .= "CONCAT('/staticpages/index.php?page=', sp.sp_id) AS url ";
   609     $sql .= "FROM {$_TABLES['staticpage']} AS sp,{$_TABLES['users']} AS u ";
   610     $sql .= "WHERE (sp.sp_uid = u.uid) AND (sp_php <> 1) ";
   611     $sql .= COM_getPermSQL('AND') . COM_getLangSQL('sp_id', 'AND', 'sp') . ' ';
   612 
   613     if (!empty ($author)) {
   614         $sql .= "AND (sp_uid = '$author') ";
   615     }
   616 
   617     $search_p = new SearchCriteria('staticpages', $LANG_STATIC['staticpages']);
   618 
   619     $columns = array('title' => 'sp_title', 'sp_content');
   620     $sql .= $search_p->getDateRangeSQL('AND', 'sp_date', $datestart, $dateend);
   621     list($sql,$ftsql) = $search_p->buildSearchSQL($keyType, $query, $columns, $sql);
   622 
   623     $search_p->setSQL($sql);
   624     $search_p->setFTSQL($ftsql);
   625     $search_p->setRank(3);
   626     $search_p->setURLRewrite(true);
   627 
   628     // Search static page comments
   629     $sql = "SELECT c.cid AS id, c.title AS title, c.comment AS description, ";
   630     $sql .= "UNIX_TIMESTAMP(c.date) AS date, c.uid AS uid, ";
   631 
   632     // MSSQL has a problem when concatenating numeric values
   633     if ($_DB_dbms == 'mssql') {
   634         $sql .= "'/comment.php?mode=view&amp;cid=' + CAST(c.cid AS varchar(10)) AS url ";
   635     } else {
   636         $sql .= "CONCAT('/comment.php?mode=view&amp;cid=',c.cid) AS url ";
   637     }
   638 
   639     $sql .= "FROM {$_TABLES['users']} AS u, {$_TABLES['comments']} AS c ";
   640     $sql .= "LEFT JOIN {$_TABLES['staticpage']} AS s ON ((s.sp_id = c.sid) ";
   641     $sql .= COM_getPermSQL('AND',0,2,'s') . COM_getTopicSQL('AND',0,'s') . COM_getLangSQL('sid','AND','s') . ") ";
   642     $sql .= "WHERE (u.uid = c.uid) AND (s.commentcode >= 0) AND (s.sp_date <= NOW()) ";
   643 
   644     if (!empty($topic)) {
   645         $sql .= "AND (s.tid = '$topic') ";
   646     }
   647     if (!empty($author)) {
   648         $sql .= "AND (c.uid = '$author') ";
   649     }
   650 
   651     $search_c = new SearchCriteria('comments', array($LANG_STATIC['staticpages'],$LANG09[66]));
   652 
   653     $columns = array('title' => 'c.title', 'comment');
   654     $sql .= $search_c->getDateRangeSQL('AND', 'c_date', $datestart, $dateend);
   655     list($sql, $ftsql) = $search_c->buildSearchSQL($keyType, $query, $columns, $sql);
   656 
   657     $search_c->setSQL($sql);
   658     $search_c->setFTSQL($ftsql);
   659     $search_c->setRank(2);
   660 
   661     return array($search_p, $search_c);
   662 }
   663 
   664 
   665 /**
   666 * This will put an option for static pages in the command and control block on
   667 * moderation.php
   668 *
   669 */
   670 function plugin_cclabel_staticpages()
   671 {
   672     global $LANG_STATIC, $_CONF;
   673 
   674     if (SEC_hasRights ('staticpages.edit,staticpages.delete', 'OR')) {
   675         return array ($LANG_STATIC['staticpages'],
   676                 $_CONF['site_admin_url'] . '/plugins/staticpages/index.php',
   677                 plugin_geticon_staticpages ());
   678     }
   679 
   680     return false;
   681 }
   682 
   683 /**
   684 * returns the administrative option for this plugin
   685 *
   686 */
   687 function plugin_getadminoption_staticpages()
   688 {
   689     global $_CONF, $_TABLES, $LANG_STATIC;
   690 
   691     if (SEC_hasRights ('staticpages.edit,staticpages.delete', 'OR')) {
   692         $result = DB_query ("SELECT COUNT(*) AS cnt FROM {$_TABLES['staticpage']}" . COM_getPermSQL ('WHERE', 0, 3));
   693         $A = DB_fetchArray ($result);
   694         $total_pages = $A['cnt'];
   695         return array ($LANG_STATIC['staticpages'], $_CONF['site_admin_url'] . '/plugins/staticpages/index.php', $total_pages);
   696     }
   697 }
   698 
   699 /**
   700 * Return SQL where statement with appropriate permissions
   701 *
   702 * Takes User id and permission and returns SQL where clause which will return
   703 * the appropriate objects.
   704 * This assumes that the table has the following security structure:
   705 * owner_id        | mediumint(8)
   706 * group_id        | mediumint(8)
   707 * perm_owner      | tinyint(1) unsigned
   708 * perm_group      | tinyint(1) unsigned
   709 * perm_members    | tinyint(1) unsigned
   710 * perm_anon       | tinyint(1) unsigned
   711 * This will work with the standard GL tables
   712 *
   713 * @param    string  $table  Table name (used in joins)
   714 * @param    int     $access Access if blank read access  2 = read 3 = read/edit
   715 * @param    int     $u_id   User ID if blank current user
   716 * @return   string          Where clause of sql statement
   717 *
   718 */
   719 function SP_getPerms ($table = '', $access = '2', $u_id = '')
   720 {
   721     global $_USER, $_GROUPS;
   722 
   723     if ($table != '') { $table .= '.'; }
   724 
   725     if ($u_id == '') {
   726         if (isset ($_USER['uid'])) {
   727             $uid = $_USER['uid'];
   728         } else {
   729             $uid = 1;
   730         }
   731         $GROUPS = $_GROUPS;
   732     } else {
   733         $uid = $u_id;
   734         $GROUPS = SEC_getUserGroups ($uid);
   735     }
   736 
   737     $sql = '(';
   738 
   739     if ($uid > 1) {
   740         $sql .= "((owner_id = '{$uid}') AND (perm_owner >= $access)) OR ";
   741 
   742         $sql .= "((group_id IN (" . implode (',', $GROUPS) . ")) "
   743              . "AND (perm_group >= $access)) OR (perm_members >= $access)";
   744     } else {
   745         $sql .= "perm_anon >= $access";
   746     }
   747 
   748     $sql .= ')';
   749 
   750     return $sql;
   751 }
   752 
   753 /**
   754 * Display static pages in the center block.
   755 *
   756 * @param   where   int      where the static page will be displayed (0..3)
   757 * @param   page    int      page number
   758 * @param   topic   string   topic ID
   759 * @return          string   HTML for the static page (can be empty)
   760 */
   761 function plugin_centerblock_staticpages ($where = 1, $page = 1, $topic ='')
   762 {
   763     global $_CONF, $_TABLES, $_SP_CONF, $LANG_STATIC, $_IMAGE_TYPE, $LANG01;
   764 
   765     $retval = '';
   766 
   767     if ($page > 1) {
   768         return $retval; // we only support page 1 at the moment ...
   769     }
   770 
   771     $moresql = "(sp_where = $where) AND ";
   772     $displayFeatured = false;
   773 
   774     // If there are no featured stories, we won't be called with $where == 2.
   775     // So, if asked to display pages for the top of the page, check if we
   776     // have pages to be displayed after the featured story and if there is
   777     // no featured story, display those pages as well.
   778     if (($where == 1) && ($_CONF['showfirstasfeatured'] == 0)) {
   779         if (DB_count ($_TABLES['stories'], 'featured', 1) == 0) {
   780             // no featured story found - redefine $moresql
   781             $moresql = "(sp_where = 1 OR sp_where = 2) AND ";
   782             $displayFeatured = true;
   783         }
   784     }
   785 
   786     if (empty ($topic)) {
   787         $moresql .= "((sp_tid = 'none') OR (sp_tid = 'all'))";
   788     } else {
   789         $moresql .= "((sp_tid = '{$topic}') OR (sp_tid = 'all'))";
   790     }
   791 
   792     if ($_SP_CONF['sort_by'] == 'date') {
   793         $sort = 'sp_date DESC';
   794     } else if ($_SP_CONF['sort_by'] == 'title') {
   795         $sort = 'sp_title';
   796     } else { // default to "sort by id"
   797         $sort = 'sp_id';
   798     }
   799     if ($displayFeatured) {
   800         $sort = 'sp_where,' . $sort;
   801     }
   802 
   803     $perms = SP_getPerms ();
   804     if (!empty ($perms)) {
   805         $perms = ' AND ' . $perms;
   806     }
   807     $spsql = "SELECT sp_id,sp_title,sp_content,sp_format,sp_date,sp_hits,owner_id,group_id,perm_owner,perm_group,perm_members,perm_anon,sp_php,sp_inblock,sp_help FROM {$_TABLES['staticpage']} WHERE (sp_centerblock = 1)" . COM_getLangSql ('sp_id', 'AND') . ' AND ' . $moresql . $perms . " ORDER BY " . $sort;
   808     $result = DB_query ($spsql);
   809 
   810     $pages = DB_numRows ($result);
   811     if ($pages > 0) {
   812         for ($i = 0; $i < $pages; $i++) {
   813             $S = DB_fetchArray ($result);
   814 
   815             if ($where == 0) {
   816                 switch ($S['sp_format']) {
   817                     case 'noblocks':
   818                         $retval .= COM_siteHeader ('none');
   819                         break;
   820                     case 'allblocks':
   821                     case 'leftblocks':
   822                         $retval .= COM_siteHeader ('menu');
   823                         break;
   824                 }
   825                 if (isset($_GET['msg'])) {
   826                     $msg = COM_applyFilter($_GET['msg'], true);
   827                     if ($msg > 0) {
   828                         $retval .= COM_showMessage($msg);
   829                     }
   830                 }
   831             }
   832 
   833             if (($S['sp_inblock'] == 1) && !empty ($S['sp_title'])
   834                 && (($where != 0) || ($S['sp_format'] != 'blankpage'))) {
   835                 $retval .= COM_startBlock ($S['sp_title'], $S['sp_help'],
   836                     COM_getBlockTemplate ('_staticpages_centerblock', 'header'));
   837             }
   838 
   839             $spage = new Template( $_CONF['path'] . 'plugins/staticpages/templates/' );
   840             $spage->set_file( array('page'=>'centerblock.thtml'));
   841             $spage->set_var('xhtml', XHTML);
   842             $spage->set_var('site_url', $_CONF['site_url']);
   843             $spage->set_var('layout_url', $_CONF['layout_url']);
   844             $spage->set_var('site_admin_url', $_CONF['site_admin_url']);
   845 
   846             if ($_CONF['hideprintericon'] == 0) {
   847                 $icon_url = $_CONF['layout_url'] . '/images/print.' . $_IMAGE_TYPE;
   848                 $attr = array('title' => $LANG_STATIC['printable_format']);
   849                 $printicon = COM_createImage($icon_url, $LANG01[65], $attr);
   850                 $print_url = COM_buildURL ($_CONF['site_url']
   851                     . '/staticpages/index.php?page=' . $S['sp_id'] . '&amp;mode=print');
   852                 $icon = COM_createLink($printicon, $print_url);
   853                 $spage->set_var('print_icon', $icon);
   854             }
   855             if ((SEC_hasAccess ($S['owner_id'], $S['group_id'], $S['perm_owner'],
   856                     $S['perm_group'], $S['perm_members'], $S['perm_anon']) == 3) &&
   857                     SEC_hasRights ('staticpages.edit')) {
   858                 $icon_url = $_CONF['layout_url'] . '/images/edit.' . $_IMAGE_TYPE;
   859                 $attr = array('title' => $LANG_STATIC['edit']);
   860                 $editiconhtml = COM_createImage($icon_url, $LANG_STATIC['edit'], $attr);
   861                 $url = $_CONF['site_admin_url']
   862                     . '/plugins/staticpages/index.php?mode=edit&amp;sp_id=' . $S['sp_id'];
   863                 $attr = array('class' => 'editlink','title' => $LANG_STATIC['edit']);
   864                 $icon =
   865                     '&nbsp;' . COM_createLink(
   866                     $editiconhtml, //display
   867                     $url,  //target
   868                     $attr //other attributes
   869                 );
   870                 $spage->set_var('edit_icon', $icon);
   871             }
   872 
   873             $spage->set_var('info_separator', 'hidden');
   874             if ($_SP_CONF['show_date'] == 1) {
   875                 $curtime = COM_getUserDateTimeFormat($S['sp_date']);
   876                 $lastupdate = $LANG_STATIC['lastupdated']. ' ' . $curtime[0];
   877                 $spage->set_var('lastupdate', $lastupdate);
   878             }
   879 
   880             if ($_SP_CONF['show_hits'] == 1) {
   881                 if ($_SP_CONF['show_date'] == 1) {
   882                     $spage->set_var('info_separator', 'visible');
   883                 }
   884                 $hits = COM_numberFormat($S['sp_hits']) . ' '
   885                                          . $LANG_STATIC['hits'];
   886                 $spage->set_var('hits', $hits);
   887             }
   888 
   889             $content = SP_render_content (stripslashes ($S['sp_content']), $S['sp_php']);
   890             $spage->set_var('content', $content );
   891             $retval .= $spage->finish($spage->parse('output', 'page'));
   892             if (($S['sp_inblock'] == 1) && !empty ($S['sp_title'])
   893                 && (($where != 0) || ($S['sp_format'] != 'blankpage'))) {
   894                 $retval .= COM_endBlock (COM_getBlockTemplate ('_staticpages_centerblock', 'footer'));
   895             }
   896 
   897             if ($where == 0) {
   898                 if ($S['sp_format'] == 'allblocks') {
   899                     $retval .= COM_siteFooter (true);
   900                 } else if ($S['sp_format'] != 'blankpage') {
   901                     $retval .= COM_siteFooter ();
   902                 }
   903             }
   904 
   905             // increment hit counter for page
   906             DB_query ("UPDATE {$_TABLES['staticpage']} SET sp_hits = sp_hits + 1 WHERE sp_id = '{$S['sp_id']}'");
   907         }
   908     }
   909 
   910     return $retval;
   911 }
   912 
   913 /**
   914 * A user is about to be deleted. Update ownership of any static pages owned
   915 * by that user or delete them.
   916 *
   917 * @param   uid   int   User id of deleted user
   918 *
   919 */
   920 function plugin_user_delete_staticpages($uid)
   921 {
   922     global $_TABLES, $_SP_CONF;
   923 
   924     if (DB_count ($_TABLES['staticpage'], 'sp_uid', $uid) +
   925             DB_count ($_TABLES['staticpage'], 'owner_id', $uid) == 0) {
   926         return;
   927     }
   928 
   929     if ($_SP_CONF['delete_pages'] == 1) {
   930         // delete the pages
   931         DB_query ("DELETE FROM {$_TABLES['staticpage']} WHERE (sp_uid = $uid) OR (owner_id = $uid)");
   932     } else {
   933         // assign ownership to a user from the Root group
   934         $rootgroup = DB_getItem ($_TABLES['groups'], 'grp_id',
   935                                  "grp_name = 'Root'");
   936         $result = DB_query ("SELECT DISTINCT ug_uid FROM {$_TABLES['group_assignments']} WHERE ug_main_grp_id = $rootgroup ORDER BY ug_uid LIMIT 1");
   937         $A = DB_fetchArray ($result);
   938         $rootuser = $A['ug_uid'];
   939 
   940         DB_query ("UPDATE {$_TABLES['staticpage']} SET sp_uid = $rootuser WHERE sp_uid = $uid");
   941         DB_query ("UPDATE {$_TABLES['staticpage']} SET owner_id = $rootuser WHERE owner_id = $uid");
   942     }
   943 }
   944 
   945 
   946 /**
   947 * Return the current version of code.
   948 * Used in the Plugin Editor to show the registered version and code version
   949 */
   950 function plugin_chkVersion_staticpages()
   951 {
   952     global $_CONF;
   953 
   954     require_once $_CONF['path'] . 'plugins/staticpages/autoinstall.php';
   955 
   956     $inst_parms = plugin_autoinstall_staticpages('staticpages');
   957 
   958     return $inst_parms['info']['pi_version'];
   959 }
   960 
   961 /**
   962 * Implements the [staticpage:] autotag.
   963 *
   964 */
   965 function plugin_autotags_staticpages($op, $content = '', $autotag = '')
   966 {
   967     global $_CONF, $_TABLES;
   968 
   969     static $recursive = array();
   970 
   971     if ($op == 'tagname' ) {
   972         return array('staticpage', 'staticpage_content');
   973     } else if ($op == 'parse') {
   974         if ($autotag['tag'] == 'staticpage' ) {
   975             $sp_id = COM_applyFilter($autotag['parm1']);
   976             if (! empty($sp_id)) {
   977                 $url = COM_buildUrl($_CONF['site_url']
   978                                 . '/staticpages/index.php?page=' . $sp_id);
   979                 if (empty($autotag['parm2'])) {
   980                     $linktext = stripslashes(DB_getItem($_TABLES['staticpage'],
   981                                              'sp_title', "sp_id = '$sp_id'"));
   982                 } else {
   983                     $linktext = $autotag['parm2'];
   984                 }
   985                 $link = COM_createLink($linktext, $url);
   986                 $content = str_replace($autotag['tagstr'], $link, $content);
   987             }
   988         } else if ($autotag['tag'] == 'staticpage_content') {
   989             $sp_id = COM_applyFilter($autotag['parm1']);
   990             if (! empty($sp_id)) {
   991                 if (isset($recursive[$sp_id])) {
   992                     $content = '';
   993                 } else {
   994                     $recursive[$sp_id] = true;
   995                     $sp_content = SP_returnStaticpage($sp_id, 'autotag');
   996                     $content = str_replace($autotag['tagstr'], $sp_content,
   997                                            $content);
   998                 }
   999             }
  1000         }
  1001 
  1002         return $content;
  1003     }
  1004 }
  1005 
  1006 /**
  1007 * Returns the URL of the plugin's icon
  1008 *
  1009 * @return   string      URL of the icon
  1010 *
  1011 */
  1012 function plugin_geticon_staticpages ()
  1013 {
  1014     global $_CONF;
  1015 
  1016     return $_CONF['site_url'] . '/staticpages/images/staticpages.png';
  1017 }
  1018 
  1019 /**
  1020 * Update the Static Pages plugin
  1021 *
  1022 * @return   int     Number of message to display (true = generic success msg)
  1023 *
  1024 */
  1025 function plugin_upgrade_staticpages()
  1026 {
  1027     global $_CONF, $_TABLES, $_DB_dbms;
  1028 
  1029     $installed_version = DB_getItem($_TABLES['plugins'], 'pi_version',
  1030                                     "pi_name = 'staticpages'");
  1031     $code_version = plugin_chkVersion_staticpages();
  1032     if ($installed_version == $code_version) {
  1033         // nothing to do
  1034         return true;
  1035     }
  1036 
  1037     require_once $_CONF['path'] . 'plugins/staticpages/autoinstall.php';
  1038 
  1039     if (! plugin_compatible_with_this_version_staticpages('staticpages')) {
  1040         return 3002;
  1041     }
  1042 
  1043     $inst_parms = plugin_autoinstall_staticpages('staticpages');
  1044     $pi_gl_version = $inst_parms['info']['pi_gl_version'];
  1045 
  1046     require_once $_CONF['path'] . 'plugins/staticpages/sql/'
  1047                                 . $_DB_dbms . '_updates.php';
  1048 
  1049     $current_version = $installed_version;
  1050     $done = false;
  1051     while (! $done) {
  1052         switch ($current_version) {
  1053         case '1.5.1':
  1054             // no db changes
  1055             $current_version = '1.6.0';
  1056             break;
  1057 
  1058         case '1.6.0':
  1059             if (isset($_UPDATES[$current_version])) {
  1060                 $_SQL = $_UPDATES[$current_version];
  1061                 foreach ($_SQL as $sql) {
  1062                     DB_query($sql);
  1063                 }
  1064             }
  1065 
  1066             update_ConfValues_1_6_0();
  1067 
  1068             $current_version = '1.6.1';
  1069             break;
  1070 
  1071         default:
  1072             $done = true;
  1073         }
  1074     }
  1075 
  1076     DB_query("UPDATE {$_TABLES['plugins']} SET pi_version = '$code_version', pi_gl_version = '$pi_gl_version' WHERE pi_name = 'staticpages'");
  1077 
  1078     return true;
  1079 }
  1080 
  1081 /**
  1082 * Called during site migration - handle changed URLs or paths
  1083 *
  1084 * @param    array   $old_conf   contents of the $_CONF array on the old site
  1085 * @param    boolean             true on success, otherwise false
  1086 *
  1087 */
  1088 function plugin_migrate_staticpages($old_conf)
  1089 {
  1090     global $_CONF;
  1091 
  1092     $tables = array(
  1093         'staticpage'    => 'sp_id, sp_content'
  1094     );
  1095 
  1096     if ($old_conf['site_url'] != $_CONF['site_url']) {
  1097         INST_updateSiteUrl($old_conf['site_url'], $_CONF['site_url'], $tables);
  1098     }
  1099 
  1100     return true;
  1101 }
  1102 
  1103 /**
  1104 * Automatic uninstall function for plugins
  1105 *
  1106 * @return   array
  1107 *
  1108 * This code is automatically uninstalling the plugin.
  1109 * It passes an array to the core code function that removes
  1110 * tables, groups, features and php blocks from the tables.
  1111 * Additionally, this code can perform special actions that cannot be
  1112 * foreseen by the core code (interactions with other plugins for example)
  1113 *
  1114 */
  1115 function plugin_autouninstall_staticpages ()
  1116 {
  1117     $out = array (
  1118         /* give the name of the tables, without $_TABLES[] */
  1119         'tables' => array('staticpage'),
  1120         /* give the full name of the group, as in the db */
  1121         'groups' => array('Static Page Admin', // correct Admin group name
  1122                           'Static Pages Admin' // typo in Geeklog 1.6.0
  1123                          ),
  1124         /* give the full name of the feature, as in the db */
  1125         'features' => array('staticpages.edit', 'staticpages.delete', 'staticpages.PHP'),
  1126         /* give the full name of the block, including 'phpblock_', etc */
  1127         'php_blocks' => array('phpblock_calendar'),
  1128         /* give all vars with their name */
  1129         'vars'=> array()
  1130     );
  1131 
  1132     return $out;
  1133 }
  1134 
  1135 /**
  1136 * Get path for the template files.
  1137 *
  1138 * @param    string  $path   subdirectory within the base template path
  1139 * @return   string          full path to template directory
  1140 *
  1141 */
  1142 function staticpages_templatePath ($path = '')
  1143 {
  1144     global $_CONF;
  1145 
  1146     if (empty ($path)) {
  1147         $layout_path = $_CONF['path_layout'] . 'staticpages';
  1148     } else {
  1149         $layout_path = $_CONF['path_layout'] . 'staticpages/' . $path;
  1150     }
  1151 
  1152     if (is_dir ($layout_path)) {
  1153         $retval = $layout_path;
  1154     } else {
  1155         $retval = $_CONF['path'] . 'plugins/staticpages/templates';
  1156         if (!empty ($path)) {
  1157             $retval .= '/' . $path;
  1158         }
  1159     }
  1160 
  1161     return $retval;
  1162 }
  1163 
  1164 function plugin_getListField_staticpages($fieldname, $fieldvalue, $A, $icon_arr)
  1165 {
  1166     global $_CONF, $LANG_ADMIN, $LANG_STATIC, $_TABLES;
  1167 
  1168     switch($fieldname) {
  1169         case "edit":
  1170             $retval = COM_createLink($icon_arr['edit'],
  1171                 "{$_CONF['site_admin_url']}/plugins/staticpages/index.php?mode=edit&amp;sp_id={$A['sp_id']}");
  1172             break;
  1173         case "copy":
  1174             $retval = COM_createLink($icon_arr['copy'],
  1175                 "{$_CONF['site_admin_url']}/plugins/staticpages/index.php?mode=clone&amp;sp_id={$A['sp_id']}");
  1176             break;
  1177         case "sp_title":
  1178             $sp_title = stripslashes ($A['sp_title']);
  1179             $url = COM_buildUrl ($_CONF['site_url'] .
  1180                                  '/staticpages/index.php?page=' . $A['sp_id']);
  1181             $retval = COM_createLink($sp_title, $url, array('title'=>$LANG_STATIC['title_display']));
  1182             break;
  1183         case "sp_uid":
  1184             $retval = COM_getDisplayName ($A['sp_uid']);
  1185             break;
  1186         case "sp_centerblock":
  1187             if ($A['sp_centerblock']) {
  1188                 switch ($A['sp_where']) {
  1189                     case '1': $where = $LANG_STATIC['centerblock_top']; break;
  1190                     case '2': $where = $LANG_STATIC['centerblock_feat']; break;
  1191                     case '3': $where = $LANG_STATIC['centerblock_bottom']; break;
  1192                     default:  $where = $LANG_STATIC['centerblock_entire']; break;
  1193                 }
  1194                 $retval = $where;
  1195             } else {
  1196                 $retval = $LANG_STATIC['centerblock_no'];
  1197             }
  1198             break;
  1199         case "unixdate":
  1200             $retval = strftime ($_CONF['daytime'], $A['unixdate']);
  1201             break;
  1202         default:
  1203             $retval = $fieldvalue;
  1204             break;
  1205     }
  1206     return $retval;
  1207 }
  1208 
  1209 /**
  1210 * Render the actual content of a static page (without any surrounding blocks)
  1211 *
  1212 * @param    string  $sp_content the content (HTML or PHP source)
  1213 * @param    int     $sp_php     flag: 1 = content is PHP source, 0 = is HTML
  1214 * @return   string              rendered content (HTML)
  1215 *
  1216 */
  1217 function SP_render_content ($sp_content, $sp_php)
  1218 {
  1219     global $_SP_CONF, $LANG_STATIC;
  1220 
  1221     $retval = '';
  1222 
  1223     if ($_SP_CONF['allow_php'] == 1) {
  1224         // Check for type (ie html or php)
  1225         if ($sp_php == 1) {
  1226             $retval = eval ($sp_content);
  1227         } else if ($sp_php == 2) {
  1228             ob_start ();
  1229             eval ($sp_content);
  1230             $retval = ob_get_contents ();
  1231             ob_end_clean ();
  1232         } else {
  1233             $retval = $sp_content;
  1234         }
  1235         $retval = PLG_replacetags ($retval);
  1236     } else {
  1237         if ($sp_php != 0) {
  1238             COM_errorLog ("PHP in static pages is disabled. Can not display page '$page'.", 1);
  1239             $retval .= $LANG_STATIC['deny_msg'];
  1240         } else {
  1241             $retval .= PLG_replacetags ($sp_content);
  1242         }
  1243     }
  1244 
  1245     return $retval;
  1246 }
  1247 
  1248 /**
  1249  * Return true since this plugin supports webservices
  1250  *
  1251  * @return  bool	True, if webservices are supported
  1252  */
  1253 function plugin_wsEnabled_staticpages()
  1254 {
  1255     return true;
  1256 }
  1257 
  1258 /**
  1259 * Return information for a static page
  1260 *
  1261 * @param    string  $sp_id      static page ID or '*'
  1262 * @param    string  $what       comma-separated list of properties
  1263 * @param    int     $uid        user ID or 0 = current user
  1264 * @param    array   $options    (reserved for future extensions)
  1265 * @return   mixed               string or array of strings with the information
  1266 *
  1267 */
  1268 function plugin_getiteminfo_staticpages($sp_id, $what, $uid = 0, $options = array())
  1269 {
  1270     global $_CONF, $_TABLES;
  1271 
  1272     // parse $what to see what we need to pull from the database
  1273     $properties = explode(',', $what);
  1274     $fields = array();
  1275     foreach ($properties as $p) {
  1276         switch ($p) {
  1277         case 'date-modified':
  1278             $fields[] = 'UNIX_TIMESTAMP(sp_date) AS unixdate';
  1279             break;
  1280         case 'description':
  1281         case 'excerpt':
  1282             $fields[] = 'sp_content';
  1283             $fields[] = 'sp_php';
  1284             break;
  1285         case 'id':
  1286             $fields[] = 'sp_id';
  1287             break;
  1288         case 'title':
  1289             $fields[] = 'sp_title';
  1290             break;
  1291         case 'url':
  1292             // needed for $sp_id == '*', but also in case we're only requesting
  1293             // the URL (so that $fields isn't emtpy)
  1294             $fields[] = 'sp_id';
  1295             break;
  1296         default:
  1297             // nothing to do
  1298             break;
  1299         }
  1300     }
  1301 
  1302     $fields = array_unique($fields);
  1303 
  1304     if (count($fields) == 0) {
  1305         $retval = array();
  1306 
  1307         return $retval;
  1308     }
  1309 
  1310     // prepare SQL request
  1311     if ($sp_id == '*') {
  1312         $where = '';
  1313         $permOp = 'WHERE';
  1314     } else {
  1315         $where = " WHERE sp_id = '" . addslashes($sp_id) . "'";
  1316         $permOp = 'AND';
  1317     }
  1318     if ($uid > 0) {
  1319         $permSql = COM_getPermSql($permOp, $uid);
  1320     } else {
  1321         $permSql = COM_getPermSql($permOp);
  1322     }
  1323     $sql = "SELECT " . implode(',', $fields)
  1324             . " FROM {$_TABLES['staticpage']}" . $where . $permSql;
  1325     if ($sp_id != '*') {
  1326         $sql .= ' LIMIT 1';
  1327     }
  1328 
  1329     $result = DB_query($sql);
  1330     $numRows = DB_numRows($result);
  1331 
  1332     $retval = array();
  1333     for ($i = 0; $i < $numRows; $i++) {
  1334         $A = DB_fetchArray($result);
  1335 
  1336         $props = array();
  1337         foreach ($properties as $p) {
  1338             switch ($p) {
  1339             case 'date-modified':
  1340                 $props['date-modified'] = $A['unixdate'];
  1341                 break;
  1342             case 'description':
  1343             case 'excerpt':
  1344                 $props[$p] = SP_render_content(stripslashes($A['sp_content']),
  1345                                                             $A['sp_php']);
  1346                 break;
  1347             case 'id':
  1348                 $props['id'] = $A['sp_id'];
  1349                 break;
  1350             case 'title':
  1351                 $props['title'] = stripslashes($A['sp_title']);
  1352                 break;
  1353             case 'url':
  1354                 if (empty($A['sp_id'])) {
  1355                     $props['url'] = COM_buildUrl($_CONF['site_url']
  1356                             . '/staticpages/index.php?page=' . $sp_id);
  1357                 } else {
  1358                     $props['url'] = COM_buildUrl($_CONF['site_url']
  1359                             . '/staticpages/index.php?page=' . $A['sp_id']);
  1360                 }
  1361                 break;
  1362             default:
  1363                 // return empty string for unknown properties
  1364                 $props[$p] = '';
  1365                 break;
  1366             }
  1367         }
  1368 
  1369         $mapped = array();
  1370         foreach ($props as $key => $value) {
  1371             if ($sp_id == '*') {
  1372                 if ($value != '') {
  1373                     $mapped[$key] = $value;
  1374                 }
  1375             } else {
  1376                 $mapped[] = $value;
  1377             }
  1378         }
  1379 
  1380         if ($sp_id == '*') {
  1381             $retval[] = $mapped;
  1382         } else {
  1383             $retval = $mapped;
  1384             break;
  1385         }
  1386     }
  1387 
  1388     if (($sp_id != '*') && (count($retval) == 1)) {
  1389         $retval = $retval[0];
  1390     }
  1391 
  1392     return $retval;
  1393 }
  1394 
  1395 /**
  1396 * Provide URL of a documentation file
  1397 *
  1398 * @param    string  $file   documentation file being requested, e.g. 'config'
  1399 * @return   mixed           URL or false when not available
  1400 *
  1401 */
  1402 function plugin_getdocumentationurl_staticpages($file)
  1403 {
  1404     global $_CONF;
  1405 
  1406     static $docurl;
  1407 
  1408     switch ($file) {
  1409     case 'index':
  1410     case 'config':
  1411         if (isset($docurl)) {
  1412             $retval = $docurl;
  1413         } else {
  1414             $doclang = COM_getLanguageName();
  1415             $docs = 'docs/' . $doclang . '/staticpages.html';
  1416             if (file_exists($_CONF['path_html'] . $docs)) {
  1417                 $retval = $_CONF['site_url'] . '/' . $docs;
  1418             } else {
  1419                 $retval = $_CONF['site_url'] . '/docs/english/staticpages.html';
  1420             }
  1421             $docurl = $retval;
  1422         }
  1423         break;
  1424 
  1425     default:
  1426         $retval = false;
  1427         break;
  1428     }
  1429 
  1430     return $retval;
  1431 }
  1432 
  1433 /**
  1434 * Provide URL and ID for the link to a comment's parent
  1435 *
  1436 * NOTE: The Plugin API does not support $_CONF['url_rewrite'] here,
  1437 *       so we'll end up with a non-rewritten URL ...
  1438 *
  1439 * @return   array   array consisting of the base URL and the ID name
  1440 *
  1441 */
  1442 function plugin_getcommenturlid_staticpages()
  1443 {
  1444     global $_CONF;
  1445 
  1446     $tmp = array(
  1447         $_CONF['site_url'] . '/staticpages/index.php',
  1448         'page'
  1449     );
  1450 
  1451     return $tmp;
  1452 }
  1453 
  1454 ?>