Our sample code should really initialize variables properly ...
3 /* Reminder: always indent with 4 spaces (no tabs). */
4 // +---------------------------------------------------------------------------+
6 // +---------------------------------------------------------------------------+
9 // | Your very own custom Geeklog library. |
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. |
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. |
22 // +---------------------------------------------------------------------------+
23 // | Copyright (C) 2000-2009 by the following authors: |
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 // +---------------------------------------------------------------------------+
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. |
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. |
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. |
44 // +---------------------------------------------------------------------------+
46 if (strpos(strtolower($_SERVER['PHP_SELF']), 'lib-custom.php') !== false) {
47 die('This file can not be used on its own!');
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;
56 * Sample PHP Block function
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.
62 function phpblock_showrights()
64 global $_RIGHTS, $_CST_VERBOSE;
69 COM_errorLog('**** Inside phpblock_showrights in lib-custom.php ****', 1);
74 for ($i = 0; $i < count($_RIGHTS); $i++) {
75 $retval .= '<li>' . $_RIGHTS[$i] . '</li>' . LB;
79 COM_errorLog('**** Leaving phpblock_showrights in lib-custom.php ****', 1);
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']
90 function CUSTOM_runScheduledTask() {
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
102 function CUSTOM_loginErrorHandler($msg='') {
103 global $_CONF,$MESSAGE;
107 } elseif ($msg == '') {
110 $retval = COM_refresh($_CONF['site_url'] .'/index.php?mode=loginfail&msg='.$msg);
117 * Include any code in this function to add custom template variables.
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)
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.
132 * @param string $templatename name of the template, e.g. 'header'
133 * @param ref &$template reference to the template
135 * @see PLG_templateSetVars
138 function CUSTOM_templateSetVars($templatename, &$template)
140 // define a {hello_world} variable available in header.thtml and
141 // a {hello_again} variable available in the story templates
143 switch ($templatename) {
145 $template->set_var('hello_world', 'Hello, world!');
149 case 'featuredstorytext':
150 case 'archivestorytext':
151 $template->set_var('hello_again', 'Hello (again)!');
157 /* Sample Custom Member Functions to create and update Custom Membership registration and profile
159 Note1: Enable CustomRegistration Feature in the configuration
160 $_CONF['custom_registration'] = true; // Set to true if you have custom code
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.
166 Note3: Optional parm $bulkimport added so that if your using the [Batch Add] feature,
167 you can execute different logic if required.
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
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)
182 global $_CONF, $_TABLES;
184 // Ensure all data is prepared correctly before inserts, quotes may need to
185 // be escaped with addslashes()
187 if (isset ($_POST['email'])) {
188 $email = COM_applyFilter ($_POST['email']);
189 $email = addslashes ($email);
193 if (isset ($_POST['homepage'])) {
194 $homepage = COM_applyFilter ($_POST['homepage']);
195 $homepage = addslashes ($homepage);
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);
206 // Note: In this case, we can trust the $uid variable to contain the new
208 DB_query("UPDATE {$_TABLES['users']} SET email = '$email', homepage = '$homepage', fullname = '$fullname' WHERE uid = $uid");
213 // Delete any records from custom tables you may have used
214 function CUSTOM_userDelete($uid)
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
223 * Template: path_layout/users/profile/profile.thtml
225 function CUSTOM_userDisplay($uid)
227 global $_CONF, $_TABLES;
231 $var = "Value from custom table";
233 <td align="right"><b>Custom Fields:</b></td>
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 */
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. */
252 function CUSTOM_userEdit($uid)
254 global $_CONF, $_TABLES;
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>';
264 <td align="right">Remember user for:</td>
265 <td>' . $selection .'</td>
268 <td align="right"><b>Custom Fields:</b></td>
269 <td><input type="text" name="custom1" size="50" value="' . $var .'"' . XHTML . '></td>
271 $retval .= '<tr><td colspan="2"><hr' . XHTML . '></td></tr>';
276 /* Function called when saving the user profile. */
277 /* This function can now update any extra fields */
278 function CUSTOM_userSave($uid)
280 global $_CONF, $_TABLES;
283 if (isset ($_POST['cooktime'])) {
284 $cooktime = COM_applyFilter ($_POST['cooktime'], true);
289 DB_query("UPDATE {$_TABLES['users']} SET cookietimeout = $cooktime WHERE uid = $uid");
295 * Main Form used for Custom membership when member is registering
297 * Note: Requires a file custom/memberdetail.thtml in every theme that is
298 * installed on the site!
300 * @param string $msg an error message to display or the word 'new'
301 * @return string HTML for the registration form
304 function CUSTOM_userForm ($msg = '')
306 global $_CONF, $_TABLES, $LANG04;
310 if (!empty ($msg) && ($msg != 'new')) {
311 $retval .= COM_startBlock($LANG04[21]) . $msg . COM_endBlock();
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>";
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);
328 $user_templates->set_var('USERNAME', $LANG04[2]);
329 $user_templates->set_var('USERNAME_HELP', "Name to be used when accessing this site");
331 if (isset ($_POST['username'])) {
332 $username = COM_applyFilter ($_POST['username']);
334 $user_templates->set_var('username', $username);
336 $user_templates->set_var('EMAIL', $LANG04[5]);
337 $user_templates->set_var('EMAIL_HELP', $LANG04[33]);
339 if (isset ($_POST['email'])) {
340 $email = COM_applyFilter ($_POST['email']);
342 $user_templates->set_var('email', $email);
344 $user_templates->set_var('EMAIL_CONF', $LANG04[124]);
345 $user_templates->set_var('EMAIL_CONF_HELP', $LANG04[126]);
347 if (isset ($_POST['email_conf'])) {
348 $email_conf = COM_applyFilter ($_POST['email_conf']);
350 $user_templates->set_var('email_conf', $email_conf);
352 $user_templates->set_var('FULLNAME', $LANG04[3]);
353 $user_templates->set_var('FULLNAME_HELP', $LANG04[34]);
355 if (isset ($_POST['fullname'])) {
356 $fullname = strip_tags ($_POST['fullname']);
358 $user_templates->set_var('fullname', $fullname);
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'));
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.
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
382 function CUSTOM_userCheck ($username, $email='')
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;
400 * Custom function to retrieve and return a formatted list of blocks
401 * Can be used when calling COM_siteHeader or COM_siteFooter
404 * 1: Setup an array of blocks to display
405 * 2: Call COM_siteHeader or COM_siteFooter
407 * $myblocks = array( 'site_menu', 'site_news', 'poll_block' );
409 * COM_siteHeader( array( 'CUSTOM_showBlocks', $myblocks )) ;
410 * COM_siteFooter( true, array( 'CUSTOM_showBlocks', $myblocks ));
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
415 function CUSTOM_showBlocks($showblocks)
417 global $_CONF, $_USER, $_TABLES;
421 if( !isset( $_USER['noboxes'] )) {
422 if( !empty( $_USER['uid'] )) {
423 $noboxes = DB_getItem( $_TABLES['userindex'], 'noboxes', "uid = {$_USER['uid']}" );
428 $noboxes = $_USER['noboxes'];
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) {
441 $retval .= COM_formatBlock($A,$noboxes, $side);
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.
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).
459 function CUSTOM_mail($to, $subject, $message, $from = '', $html = false, $priority = 0)
464 $from = $_CONF['site_name'] . ' <' . $_CONF['site_mail'] . '>';
467 $headers = 'From: ' . $from . "\r\n"
468 . 'X-Mailer: Geeklog ' . VERSION . "\r\n";
471 $headers .= 'X-Priority: ' . $priority . "\r\n";
474 $charset = COM_getCharset ();
476 $headers .= "Content-Type: text/html; charset={$charset}\r\n"
477 . 'Content-Transfer-Encoding: 8bit';
479 $headers .= "Content-Type: text/plain; charset={$charset}";
482 return mail ($to, $subject, $message, $headers);
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).
492 function CUSTOM_menuEntries ()
494 global $_CONF, $_USER;
496 $myentries = array ();
498 // Sample link #1: Link to Gallery
499 $myentries[] = array ('url' => $_CONF['site_url'] . '/gallery/',
500 'label' => 'Gallery');
502 // Sample link #2: Link to the Personal Calendar - only visible for
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');
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.
522 function CUSTOM_handleError($errno, $errstr, $errfile, $errline, $errcontext)
525 if( is_array($_CONF) && function_exists('COM_mail'))
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");
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;
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;
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>