system/lib-custom.php.dist
author Dirk Haun <dirk@haun-online.de>
Sat, 17 Oct 2009 20:02:27 +0200
branchHEAD
changeset 7385 12aa30c5bdb4
parent 6994 b788cc04edbe
child 7607 32c0ef2a686a
permissions -rw-r--r--
Our sample code should really initialize variables properly ...
     1 <?php
     2 
     3 /* Reminder: always indent with 4 spaces (no tabs). */
     4 // +---------------------------------------------------------------------------+
     5 // | Geeklog 1.6                                                               |
     6 // +---------------------------------------------------------------------------+
     7 // | lib-custom.php                                                            |
     8 // |                                                                           |
     9 // | Your very own custom Geeklog library.                                     |
    10 // |                                                                           |
    11 // | This is the file where you should put all of your custom code.  When      |
    12 // | possible you should not alter lib-common.php but, instead, put code here. |
    13 // | This will make upgrading to future versions of Geeklog easier for you     |
    14 // | because you will always be guaranteed that the Geeklog developers will    |
    15 // | NOT add required code to this file.                                       |
    16 // |                                                                           |
    17 // | NOTE: we have already gone through the trouble of making sure that we     |
    18 // | always include this file when lib-common.php is included some place so    |
    19 // | you will have access to lib-common.php.  It follows then that you should  |
    20 // | not include lib-common.php in this file.                                  |
    21 // |                                                                           |
    22 // +---------------------------------------------------------------------------+
    23 // | Copyright (C) 2000-2009 by the following authors:                         |
    24 // |                                                                           |
    25 // | Authors: Tony Bibbs       - tony AT tonybibbs DOT com                     |
    26 // |          Blaine Lang      - blaine AT portalparts DOT com                 |
    27 // |          Dirk Haun        - dirk AT haun-online DOT de                    |
    28 // +---------------------------------------------------------------------------+
    29 // |                                                                           |
    30 // | This program is free software; you can redistribute it and/or             |
    31 // | modify it under the terms of the GNU General Public License               |
    32 // | as published by the Free Software Foundation; either version 2            |
    33 // | of the License, or (at your option) any later version.                    |
    34 // |                                                                           |
    35 // | This program is distributed in the hope that it will be useful,           |
    36 // | but WITHOUT ANY WARRANTY; without even the implied warranty of            |
    37 // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             |
    38 // | GNU General Public License for more details.                              |
    39 // |                                                                           |
    40 // | You should have received a copy of the GNU General Public License         |
    41 // | along with this program; if not, write to the Free Software Foundation,   |
    42 // | Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.           |
    43 // |                                                                           |
    44 // +---------------------------------------------------------------------------+
    45 
    46 if (strpos(strtolower($_SERVER['PHP_SELF']), 'lib-custom.php') !== false) {
    47     die('This file can not be used on its own!');
    48 }
    49 
    50 // You can use this global variable to print useful messages to the errorlog
    51 // using COM_errorLog().  To see an example of how to do this, look in
    52 // lib-common.php and see how $_COM_VERBOSE was used throughout the code
    53 $_CST_VERBOSE = false;
    54 
    55 /**
    56 * Sample PHP Block function
    57 *
    58 * this is a sample function used by a PHP block.  This will show the rights that
    59 * a user has in the "What you have access to" block.
    60 *
    61 */
    62 function phpblock_showrights()
    63 {
    64     global $_RIGHTS, $_CST_VERBOSE;
    65 
    66     $retval = '';
    67 
    68     if ($_CST_VERBOSE) {
    69         COM_errorLog('**** Inside phpblock_showrights in lib-custom.php ****', 1);
    70     }
    71 
    72     $retval .= '&nbsp;';
    73 
    74     for ($i = 0; $i < count($_RIGHTS); $i++) {
    75         $retval .=  '<li>' . $_RIGHTS[$i] . '</li>' . LB;
    76     }
    77 
    78     if ($_CST_VERBOSE) {
    79         COM_errorLog('**** Leaving phpblock_showrights in lib-custom.php ****', 1);
    80     }
    81 
    82     return $retval;
    83 }
    84 
    85 
    86 /**
    87 * Include any code in this function that will be called by the internal CRON API
    88 * The interval between runs is determined by $_CONF['cron_schedule_interval']
    89 */
    90 function CUSTOM_runScheduledTask() {
    91 
    92 }
    93 
    94 
    95 /**
    96 * Example of custom function that can be used to handle a login error.
    97 * Only active with custom registration mode enabled
    98 * Used if you have a custom front page and need to trap and reformat any error messages
    99 * This example redirects to the front page with a extra passed variable plus the message
   100 * Note: Message could be a string but in this case maps to $MESSAGE[81] as a default - edit in language file
   101 */
   102 function CUSTOM_loginErrorHandler($msg='') {
   103     global $_CONF,$MESSAGE;
   104 
   105     if ($msg > 0) {
   106         $msg = $msg;
   107     } elseif ($msg == '') {
   108         $msg = 81;
   109     }
   110     $retval = COM_refresh($_CONF['site_url'] .'/index.php?mode=loginfail&amp;msg='.$msg);
   111     echo $retval;
   112     exit;
   113 }
   114 
   115 
   116 /**
   117 * Include any code in this function to add custom template variables.
   118 *
   119 * Called from within Geeklog for:
   120 * - 'header' (site header)
   121 * - 'footer' (site footer)
   122 * - 'storytext', 'featuredstorytext', 'archivestorytext' (story templates)
   123 * - 'story' (story submission)
   124 * - 'comment' (comment submission form)
   125 * - 'registration' (user registration form)
   126 * - 'contact' (email user form)
   127 * - 'emailstory' (email story to a friend)
   128 *
   129 * This function is called whenever PLG_templateSetVars is called, i.e. in
   130 * addition to the templates listed here, it may also be called from plugins.
   131 *
   132 * @param    string  $templatename   name of the template, e.g. 'header'
   133 * @param    ref    &$template       reference to the template
   134 * @return   void
   135 * @see      PLG_templateSetVars
   136 *
   137 */
   138 function CUSTOM_templateSetVars($templatename, &$template)
   139 {
   140     // define a {hello_world} variable available in header.thtml and
   141     // a {hello_again} variable available in the story templates
   142 
   143     switch ($templatename) {
   144     case 'header':
   145         $template->set_var('hello_world', 'Hello, world!');
   146         break;
   147 
   148     case 'storytext':
   149     case 'featuredstorytext':
   150     case 'archivestorytext':
   151         $template->set_var('hello_again', 'Hello (again)!');
   152         break;
   153     }
   154 }
   155 
   156 
   157 /*  Sample Custom Member Functions to create and update Custom Membership registration and profile
   158 
   159     Note1: Enable CustomRegistration Feature in the configuration
   160     $_CONF['custom_registration'] = true;  // Set to true if you have custom code
   161 
   162     Note2: This example requires a template file called memberdetail.thtml to be
   163     located under the theme_dir/custom directory.
   164     Sample is provided under /system with the distribution.
   165 
   166     Note3: Optional parm $bulkimport added so that if your using the [Batch Add] feature,
   167     you can execute different logic if required.
   168 
   169     Functions have been provided that are called from the Core Geeklog user and admin functions
   170     - This works with User Moderation as well
   171     - Admin will see the new registration info when checking a member's profile only
   172     - All other users will see the standard User profile with the optional extended custom information
   173     - Customization requires changes to a few of the core template files to add {customfields} variables
   174     - See notes below in the custom function about the template changes
   175 */
   176 
   177 /* Create any new records in additional tables you may have added  */
   178 /* Update any fields in the core GL tables for this user as needed */
   179 /* Called when user is first created */
   180 function CUSTOM_userCreate ($uid,$bulkimport=false)
   181 {
   182     global $_CONF, $_TABLES;
   183 
   184     // Ensure all data is prepared correctly before inserts, quotes may need to
   185     // be escaped with addslashes()
   186     $email = '';
   187     if (isset ($_POST['email'])) {
   188         $email = COM_applyFilter ($_POST['email']);
   189         $email = addslashes ($email);
   190     }
   191 
   192     $homepage = '';
   193     if (isset ($_POST['homepage'])) {
   194         $homepage = COM_applyFilter ($_POST['homepage']);
   195         $homepage = addslashes ($homepage);
   196     }
   197 
   198     $fullname = '';
   199     if (isset ($_POST['fullname'])) {
   200         // COM_applyFilter would strip special characters, e.g. quotes, so
   201         // we only strip HTML
   202         $fullname = strip_tags ($_POST['fullname']);
   203         $fullname = addslashes ($fullname);
   204     }
   205 
   206     // Note: In this case, we can trust the $uid variable to contain the new
   207     // account's uid.
   208     DB_query("UPDATE {$_TABLES['users']} SET email = '$email', homepage = '$homepage', fullname = '$fullname' WHERE uid = $uid");
   209 
   210     return true;
   211 }
   212 
   213 // Delete any records from custom tables you may have used
   214 function CUSTOM_userDelete($uid)
   215 {
   216     return true;
   217 }
   218 
   219 /* Called from users.php - when user is displaying a member profile.
   220  * This function can now return any extra fields that need to be shown.
   221  * Output is then replaced in {customfields} -- This variable needs to be added
   222  * to your templates
   223  * Template: path_layout/users/profile/profile.thtml
   224  */
   225 function CUSTOM_userDisplay($uid)
   226 {
   227     global $_CONF, $_TABLES;
   228 
   229     $retval = '';
   230 
   231     $var = "Value from custom table";
   232     $retval .= '<tr>
   233         <td align="right"><b>Custom Fields:</b></td>
   234         <td>' . $var .'</td>
   235      </tr>';
   236 
   237     return $retval;
   238 }
   239 
   240 
   241 /* Function called when editing user profile. */
   242 /* Called from usersettings.php - when user is eding their own profile  */
   243 /* and from admin/user.php when admin is editing a member profile  */
   244 /* This function can now return any extra fields that need to be shown for editing */
   245 /* Output is then replaced in {customfields} -- This variable needs to be added to your templates */
   246 /* User: path_layout/preferences/profile.thtml and Admin: path_layout/admin/user/edituser.thtml */
   247 
   248 /* This example shows adding the Cookie Timeout setting and extra text field */
   249 /* As noted: You need to add the {customfields} template variable. */
   250 /* For the edituser.thtml - maybe it would be added about the {group_edit} variable. */
   251 
   252 function CUSTOM_userEdit($uid)
   253 {
   254     global $_CONF, $_TABLES;
   255 
   256     $retval = '';
   257 
   258     $var = "Value from custom table";
   259     $cookietimeout = DB_getitem($_TABLES['users'], 'cookietimeout', $uid);
   260     $selection = '<select name="cooktime">' . LB;
   261     $selection .= COM_optionList ($_TABLES['cookiecodes'], 'cc_value,cc_descr', $cookietimeout, 0);
   262     $selection .= '</select>';
   263     $retval .= '<tr>
   264         <td align="right">Remember user for:</td>
   265         <td>' . $selection .'</td>
   266      </tr>';
   267     $retval .= '<tr>
   268         <td align="right"><b>Custom Fields:</b></td>
   269         <td><input type="text" name="custom1" size="50" value="' . $var .'"' . XHTML . '></td>
   270      </tr>';
   271     $retval .= '<tr><td colspan="2"><hr' . XHTML . '></td></tr>';
   272 
   273     return $retval;
   274 }
   275 
   276 /* Function called when saving the user profile. */
   277 /* This function can now update any extra fields  */
   278 function CUSTOM_userSave($uid)
   279 {
   280     global $_CONF, $_TABLES;
   281 
   282     $cooktime = 0;
   283     if (isset ($_POST['cooktime'])) {
   284         $cooktime = COM_applyFilter ($_POST['cooktime'], true);
   285         if ($cooktime < 0) {
   286             $cooktime = 0;
   287         }
   288 
   289         DB_query("UPDATE {$_TABLES['users']} SET cookietimeout = $cooktime WHERE uid = $uid");
   290     }
   291 }
   292 
   293 
   294 /**
   295 * Main Form used for Custom membership when member is registering
   296 *
   297 * Note: Requires a file custom/memberdetail.thtml in every theme that is
   298 *       installed on the site!
   299 *
   300 * @param    string  $msg    an error message to display or the word 'new'
   301 * @return   string          HTML for the registration form
   302 *
   303 */
   304 function CUSTOM_userForm ($msg = '')
   305 {
   306     global $_CONF, $_TABLES, $LANG04;
   307 
   308     $retval = '';
   309 
   310     if (!empty ($msg) && ($msg != 'new')) {
   311         $retval .= COM_startBlock($LANG04[21]) . $msg . COM_endBlock();
   312     }
   313 
   314     $post_url = $_CONF['site_url'] . '/users.php';
   315     $postmode = 'create';
   316     $submitbutton = '<input type="submit" value="Register Now!"' . XHTML . '>';
   317     $message = "<blockquote style=\"padding-top:10px;\"><b>Please complete the application below. Once you have completed the application, click the Register Now! button and the application will be processed immediately.</b></blockquote>";
   318 
   319     $user_templates = new Template ($_CONF['path_layout'] . 'custom');
   320     $user_templates->set_file('memberdetail', 'memberdetail.thtml');
   321     $user_templates->set_var( 'xhtml', XHTML );
   322     $user_templates->set_var('site_url', $_CONF['site_url']);
   323     $user_templates->set_var('layout_url', $_CONF['layout_url']);
   324     $user_templates->set_var('post_url', $post_url);
   325     $user_templates->set_var('startblock', COM_startBlock("Custom Registration Example"));
   326     $user_templates->set_var('message', $message);
   327 
   328     $user_templates->set_var('USERNAME', $LANG04[2]);
   329     $user_templates->set_var('USERNAME_HELP', "Name to be used when accessing this site");
   330     $username = '';
   331     if (isset ($_POST['username'])) {
   332         $username = COM_applyFilter ($_POST['username']);
   333     }
   334     $user_templates->set_var('username', $username);
   335 
   336     $user_templates->set_var('EMAIL', $LANG04[5]);
   337     $user_templates->set_var('EMAIL_HELP', $LANG04[33]);
   338     $email = '';
   339     if (isset ($_POST['email'])) {
   340         $email = COM_applyFilter ($_POST['email']);
   341     }
   342     $user_templates->set_var('email', $email);
   343 
   344     $user_templates->set_var('EMAIL_CONF', $LANG04[124]);
   345     $user_templates->set_var('EMAIL_CONF_HELP', $LANG04[126]);
   346     $email_conf = '';
   347     if (isset ($_POST['email_conf'])) {
   348         $email_conf = COM_applyFilter ($_POST['email_conf']);
   349     }
   350     $user_templates->set_var('email_conf', $email_conf);
   351 
   352     $user_templates->set_var('FULLNAME', $LANG04[3]);
   353     $user_templates->set_var('FULLNAME_HELP', $LANG04[34]);
   354     $fullname = '';
   355     if (isset ($_POST['fullname'])) {
   356         $fullname = strip_tags ($_POST['fullname']);
   357     }
   358     $user_templates->set_var('fullname', $fullname);
   359 
   360     $user_templates->set_var('user_id', $user);
   361     $user_templates->set_var('postmode', $postmode);
   362     $user_templates->set_var('submitbutton', $submitbutton);
   363     $user_templates->set_var('endblock', COM_endBlock());
   364     $user_templates->parse('output', 'memberdetail');
   365     $retval .= $user_templates->finish($user_templates->get_var('output'));
   366 
   367     return $retval;
   368 }
   369 
   370 /**
   371 * Geeklog is about to create a new user or edit an existing user.
   372 * This is the custom code's last chance to do any form validation,
   373 * e.g. to check if all required data has been entered.
   374 *
   375 * @param    string  $username   username that Geeklog would use for the new user* @param    string  $email      email address of that user
   376 * @return   mixed               Returns an empty string if no issues found validating user account form
   377 *                               If a validation test fails, return both a message and code
   378 *                                  > usercreate needs a string and usersettings saveuser() needs a message number
   379 *                               The message number will map to the GLOBALS $MESSAGE define in the site language files
   380 *                               By default $MESSAGE[400] will appear if a non-numeric is returned to usersettings.php - saveuser function
   381 */
   382 function CUSTOM_userCheck ($username, $email='')
   383 {
   384     global $MESSAGE;
   385 
   386     $retval = '';
   387 
   388     // Example, check that the full name has been entered
   389     // and complain if it's missing
   390     if (empty($_POST['fullname'])) {
   391         $retval['string'] = $MESSAGE['401'];
   392         $retval['number'] = 401;
   393     }
   394 
   395     return $retval;
   396 }
   397 
   398 
   399 /**
   400 * Custom function to retrieve and return a formatted list of blocks
   401 * Can be used when calling COM_siteHeader or COM_siteFooter
   402 *
   403 * Example:
   404 * 1: Setup an array of blocks to display
   405 * 2: Call COM_siteHeader or COM_siteFooter
   406 *
   407 *  $myblocks = array( 'site_menu', 'site_news', 'poll_block' );
   408 *
   409 * COM_siteHeader( array( 'CUSTOM_showBlocks', $myblocks )) ;
   410 * COM_siteFooter( true, array( 'CUSTOM_showBlocks', $myblocks ));
   411 *
   412 * @param   array   $showblocks    An array of block names to retrieve and format
   413 * @return  string                 Formated HTML containing site footer and optionally right blocks
   414 */
   415 function CUSTOM_showBlocks($showblocks)
   416 {
   417     global $_CONF, $_USER, $_TABLES;
   418 
   419     $retval = '';
   420 
   421     if( !isset( $_USER['noboxes'] )) {
   422         if( !empty( $_USER['uid'] )) {
   423             $noboxes = DB_getItem( $_TABLES['userindex'], 'noboxes', "uid = {$_USER['uid']}" );
   424         } else {
   425             $noboxes = 0;
   426         }
   427     } else {
   428         $noboxes = $_USER['noboxes'];
   429     }
   430 
   431     foreach($showblocks as $block) {
   432         $sql = "SELECT bid, name,type,title,content,rdfurl,phpblockfn,help,allow_autotags,onleft FROM {$_TABLES['blocks']} WHERE name='$block'";
   433         $result = DB_query($sql);
   434         if (DB_numRows($result) == 1) {
   435             $A = DB_fetchArray($result);
   436             if ($A['onleft'] == 1) {
   437                 $side = 'left';
   438             } else {
   439                 $side = 'right';
   440             }
   441             $retval .= COM_formatBlock($A,$noboxes, $side);
   442         }
   443     }
   444 
   445     return $retval;
   446 }
   447 
   448 
   449 /**
   450 * This is an example of a custom email function. When this function is NOT
   451 * commented out, Geeklog would send all emails through this function
   452 * instead of sending them through COM_mail in lib-common.php.
   453 *
   454 * This is basically a re-implementation of the way emails were sent
   455 * prior to Geeklog 1.3.9 (Geeklog uses PEAR::Mail as of version 1.3.9).
   456 *
   457 */
   458 /*
   459 function CUSTOM_mail($to, $subject, $message, $from = '', $html = false, $priority = 0)
   460 {
   461     global $_CONF;
   462 
   463     if (empty ($from)) {
   464         $from = $_CONF['site_name'] . ' <' . $_CONF['site_mail'] . '>';
   465     }
   466 
   467     $headers  = 'From: ' . $from . "\r\n"
   468               . 'X-Mailer: Geeklog ' . VERSION . "\r\n";
   469 
   470     if ($priority > 0) {
   471         $headers .= 'X-Priority: ' . $priority . "\r\n";
   472     }
   473 
   474     $charset = COM_getCharset ();
   475     if ($html) {
   476         $headers .= "Content-Type: text/html; charset={$charset}\r\n"
   477                  .  'Content-Transfer-Encoding: 8bit';
   478     } else {
   479         $headers .= "Content-Type: text/plain; charset={$charset}";
   480     }
   481 
   482     return mail ($to, $subject, $message, $headers);
   483 }
   484 */
   485 
   486 /**
   487 * This is an example of a function that returns menu entries to be used for
   488 * the 'custom' entry in $_CONF['menu_elements'] (see configuration).
   489 *
   490 */
   491 /*
   492 function CUSTOM_menuEntries ()
   493 {
   494     global $_CONF, $_USER;
   495 
   496     $myentries = array ();
   497 
   498     // Sample link #1: Link to Gallery
   499     $myentries[] = array ('url'   => $_CONF['site_url'] . '/gallery/',
   500                           'label' => 'Gallery');
   501 
   502     // Sample link #2: Link to the Personal Calendar - only visible for
   503     // logged-in users
   504     if (!empty ($_USER['uid']) && ($_USER['uid'] > 1)) {
   505         $myentries[] = array ('url'   => $_CONF['site_url']
   506                                          . '/calendar/index.php?mode=personal',
   507                               'label' => 'My Calendar');
   508     }
   509 
   510     return $myentries;
   511 }
   512 */
   513 
   514 /**
   515   * This is an example of an error handler override. This will be used in
   516   * place of COM_handleError if the user is not in the Root group. Really,
   517   * this should only be used to display some nice pretty "site error" html.
   518   * Though you could try and notify the sysadmin, and log the error, as this
   519   * example will show. The function is commented out for saftey.
   520   */
   521 /*
   522 function CUSTOM_handleError($errno, $errstr, $errfile, $errline, $errcontext)
   523 {
   524     global $_CONF;
   525     if( is_array($_CONF) && function_exists('COM_mail'))
   526     {
   527         COM_mail($_CONF['site_mail'], $_CONF['site_name'].' Error Handler',
   528                 "An error has occurred: $errno $errstr @ $errline of $errfile");
   529         COM_errorLog("Error Handler: $errno $errstr @ $errline of $errfile");
   530     }
   531     echo("
   532         <html>
   533             <head>
   534                 <title>{$_CONF['site_name']} - An error occurred.</title>
   535                 <style type=\"text/css\">
   536                     body,html {height: 100%; width: 100%;}
   537                     body{ border: 0px; padding: 0px;
   538                         background-color: white;
   539                         color: black;
   540                         }
   541                    div { margin-left: auto; margin-right: auto;
   542                             margin-top: auto; margin-bottom: auto;
   543                             border: solid thin blue; width: 400px;
   544                             padding: 5px; text-align: center;
   545                             }
   546                    h1 { color: blue;}
   547                </style>
   548             </head>
   549             <body>
   550                 <div>
   551                     <h1>An Error Has Occurred.</h1>
   552                     <p>Unfortunatley, the action you performed has caused an
   553                     error. The site administrator has been informed. If you
   554                     try again later, the issue may have been fixed.</p>
   555                 </div>
   556             </body>
   557         </html>
   558         ");
   559     exit;
   560 }
   561 */
   562 ?>