Top Poster
#1 ramon fincken 176
#2 Site admin 2
#3 Ber|Art 2
#4 Jan 0
#5 roger beckers 0
#6 Christine 0
#7 nextstep 0

The time now is Fri Jul 03, 2009 1:55 am
Ramonfincken.com Forum Index
View unanswered posts
[tutorial] Phpbb3 creating a hook that is not existing/standard
Author ramon fincken :: Posted: Sun Jun 21, 2009 5:45 pm :: Category: PHP coding


So today I needed to create a hook in my Phpbb3 install, to keep clear of the default phpbb3 coding as much as possible.

Beeing used to Wordpress coding this should be an easy task.
Manual:
http://area51.phpbb.com/docs/hook_system.html

But the darn thing wouldn't work.
Why?

Quote:
Pre-defined hookable phpBB3 functions

In phpBB3 there are four functions you are able to hook into with your custom functions:

phpbb_user_session_handler(); which is called within user::setup after the session and the user object is correctly initialized.
append_sid($url, $params = false, $is_amp = true, $session_id = false); which is called for building urls (appending the session id)
$template->display($handle, $include_once = true); which is called directly before outputting the (not-yet-compiled) template.
exit_handler(); which is called at the very end of phpBB3's execution.



Since i make use of Phpbb3's user system which is closely interconnected with my own custom coding ... I needed a function which deleted a few table entries for that user as well.
The easy and dirty way: Just add the code in the user_delete() function.


Now for a step by step way to create a NEW hook using as little phpbb3 core code changes.
This has been tested with a phpbb3 upgrade ( just merge the files ).
Alter the attached file/function to adapt to your liking for other hooks you want to create !

Step1:
Download the hook_user_delete.php file as txt: http://www.ramonfincken.com/uploads/topic_155.txt

Code:
COPY
hook_user_delete.php TO ./includes/hooks/hook_user_delete.php


(This file will be automatically included because of the hook_ prefix.)

Step2:
Code:
OPEN
./includes/functions_user.php


Code:
FIND
function user_delete


Code:
FIND
$phpEx;


Code:
AFTER, ADD

   // ---------------------------------
   // hook-my_custom_user_delete START
   global $phpbb_hook;
   if (!empty($phpbb_hook) && $phpbb_hook->call_hook(__FUNCTION__))
   {
      if ($phpbb_hook->hook_return(__FUNCTION__))
      {
         return $phpbb_hook->hook_return_result(__FUNCTION__);
      }
   }
   // hook-my_custom_user_delete END   
   // ---------------------------------
   


Step3:
Clear the cache ( ACP > index > clear cache )
OR
FTP to ./cache/ and delete the file data_hooks.php ( you can safely delete this file, it will be automatically re-created )


Now the only thing you have to do is look in the hook_user_delete.php file and adapt the function to your liking.

The core function:
Code:
/**
 * Phpbb3 addon ( hook ) to initiate actions when deleting a user from the ACP using the standard user_delete() function
 *
 * @author  Ramon Fincken, RamonFincken.com WebsiteFreelancers.nl
 */
function my_user_delete()
{
   global $cache, $config, $db, $user, $auth, $phpbb_hook; // Note: Not sure if you need phpbb_hook, probably not :)
   global $phpbb_root_path, $phpEx;
   
   // User to delete: $user_id
   $user_id = intval($_POST['u']); // Note: it would be nicer to have this as a param, but this works just fine

   if($user_id > 0)
   {   
      // Note: in my case I want to do some deletes, but feel free to
      // * call a function ( make sure phpbb has ACCESS to that function ! )
      // * do an insert in a "users_deleted" table, which can be read by your own code/CMS to perform actions later :)
      
      $sql = array();
      $sql[] = 'DELETE FROM some_table WHERE user_id = '.$user_id;
      $sql[] = 'UPDATE LOW_PRIORITY suggestions SET submitter = 0 WHERE submitter = '.$user_id;
   
      foreach($sql AS $key => $sql_temp)
      {
         $db->sql_query($sql_temp);
      }
   }
   return;
}


[solution] Inserting data, ignoring any key conflicts
Author ramon fincken :: Posted: Sun Jun 07, 2009 8:24 pm :: Category: Mysql databases


How to insert data, but you are not sure if the data exists, but dont' want to see a mysql error while inserting?
This is really handy when inserting XML / RSS feeds !

There are two solutions:

Solution1 :
Code:
$sql = 'INSERT INTO '.TABLE_NAME .' ( key1, key2 ) VALUES ( val1, val2 ) ON DUPLICATE KEY UPDATE fetched=fetched+1';

This will insert the data if there is no key conflict.
If there is a key conflict ( existing key = key you want to insert ), no error will be given and NO data will be updated except for the row called "fetched" which has a counter and will increase with the count of 1;
( fetched NULL allowed, DEFAULT = 0 )

Solution2a :
Code:
$sql = 'INSERT IGNORE INTO '. TABLE_NAME . '  ( key1, key2 ) VALUES ( val1, val2 )';

This will insert the data if there is no key conflict.
If there is a key conflict ( existing key = key you want to insert ), no error will be given and NO data will be updated.

Solution2b :
You can even do cool things like this, if you don't care if the insert is carried out right away:
Code:
$sql = 'INSERT LOW_PRIORITY IGNORE INTO '. TABLE_NAME . ' ( key1, key2 ) VALUES ( val1, val2 )';


[solution] Only allow your IP to admin panel ( and deny all others )
Author ramon fincken :: Posted: Sun Jun 07, 2009 8:15 pm :: Category: HTML and other markup&programming languages


Here's a howto, it is really easy to block ALL ips except your IP to any admin panel.

Don't worry, if you have a frontend file doing things like : include('./admin/functionfile.php'); that will still work Smile


Here we go, I assume your IP adres is 123.123.123.123 and 456.456.456.456 if you are at two locations ( home and work for instance ).
Keep in mind that a combination with username/password with this htaccess trick is better then rely on only one solution.

There is one major drawback ... if you need access to your admin panel FAST and you are not at the IP adresses / locations ... you cannot enter the panel unless you have some form of remote desktop / VNC server running....

The code is self explanatary right ?
Code:
deny from all
allow from 123.123.123.123
allow from 456.456.456.456


[solution][snipplet] Phpbb3 code to calculate user age
Author ramon fincken :: Posted: Sat May 30, 2009 11:43 am :: Category: PHP coding


Code found in memberlist.php (viewprofile) is

Code:
   $age = '';

   if ($config['allow_birthdays'] && $data['user_birthday'])
   {
      list($bday_day, $bday_month, $bday_year) = array_map('intval', explode('-', $data['user_birthday']));

      if ($bday_year)
      {
         $now = getdate(time() + $user->timezone + $user->dst - date('Z'));

         $diff = $now['mon'] - $bday_month;
         if ($diff == 0)
         {
            $diff = ($now['mday'] - $bday_day < 0) ? 1 : 0;
         }
         else
         {
            $diff = ($diff < 0) ? 1 : 0;
         }

         $age = (int) ($now['year'] - $bday_year - $diff);
      }
   }



Extracting this to a general custom function to use in your custom code Smile

Code:
/**
 * Calculate_age($birhtday), returns age from phpbb3's user table, field: $row['user_birthday']
 * Return [age] OR 'Unknown'
 */
function calculate_age($birthday)
{
   $age = '';

   if ($birthday)
   {
      list($bday_day, $bday_month, $bday_year) = array_map('intval', explode('-', $birthday));

      if ($bday_year)
      {
         $now = getdate(time() + $user->timezone + $user->dst - date('Z'));

         $diff = $now['mon'] - $bday_month;
         if ($diff == 0)
         {
            $diff = ($now['mday'] - $bday_day < 0) ? 1 : 0;
         }
         else
         {
            $diff = ($diff < 0) ? 1 : 0;
         }

         $age = (int) ($now['year'] - $bday_year - $diff);
         return $age;
      }
   }   
   return 'Unknown';
}



Linked at:
http://www.phpbb.com/community/viewtopi...mp;start=0


[solution][snipplet] Phpbb3 code to get user avatar
Author ramon fincken :: Posted: Fri May 15, 2009 9:13 pm :: Category: PHP coding
Tagged: , , , ,


You'll need
Code:
include($phpbb_root_path . 'includes/functions_display.' . $phpEx);


Next, do this select SQL ( or merge/add with your existing SQL )..
Code:
SELECT user_avatar, user_avatar_type, user_avatar_width, user_avatar_height


In your while loop: do this:
Code:
$row['avatar'] = get_user_avatar($row['user_avatar'], $row['user_avatar_type'], $row['user_avatar_width'], $row['user_avatar_height']);



Also interesting:
http://www.ramonfincken.com/permalink/topic149.html
http://www.ramonfincken.com/permalink/topic150.html

Source:
http://www.volasail.com/2008/04/using-p...tom-pages/


[solution][snipplet] Phpbb3 code for latest users (userlist)
Author ramon fincken :: Posted: Fri May 15, 2009 8:51 pm :: Category: PHP coding


Code:
   function latest_signups($limit = 8)
   {
      global $db;
      $sql = 'SELECT u.user_id AS id, u.username_clean AS username FROM '.USERS_TABLE.' u WHERE u.user_type IN (0, 3) ORDER BY u.user_regdate DESC LIMIT '.intval($limit);
      $result = $db->sql_query($sql);

      $data = array();
      while ($row = $db->sql_fetchrow($result))
      {
         $data[] = $row;
      }
      $db->sql_freeresult($result);   
      return $data;      
   }


[solution][snipplet] Phpbb3 force user to logout PHP functions/code
Author ramon fincken :: Posted: Fri May 15, 2009 7:59 pm :: Category: PHP coding


Here you go !

Code:
   $user->session_kill();
   $user->session_begin();



This code may also be of interest to integrate all bits and pieces:
Code:
define('IN_PHPBB', true);
$phpbb_root_path = $include_prefix .'./';  // Your path here
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);

// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup();


[solution] Block external POST to my domain
Author ramon fincken :: Posted: Thu May 14, 2009 8:02 pm :: Category: HTML and other markup&programming languages


Here we go... the solution is a simple as it gets ( using htaccess )

It is in fact a tweaked hotlinking script except we don't care if its an image, but we DO care if its a PUT or a POST Smile


Please adjust mydomain\.com to your own domain !
To test: just leave mydomain\.com and hit SUBMIT on a POST method form at your site!

Code:
RewriteEngine On
RewriteCond %{REQUEST_METHOD} ^(PUT|POST)$ [NC]
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?mydomain\.com/ [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteRule ^(.*)$ http://127.0.0.1 [L]


ps: requests are redirected to 127.0.0.1 which is localhost.
ps2: this method may not work on all webhosts !


All times are GMT
Who is Online
Who is Online Our users have posted a total of 170 articles
We have 7 registered users
The newest registered user is nextstep
In total there is 1 user online :: 0 Registered, 0 Hidden and 1 Guest   [ Administrator ]   [ Moderator ]
Most users ever online was 16 on Thu May 14, 2009 10:16 am
Registered Users: None
This data is based on users active over the past five minutes
Log in
Username:    Password:      Log me on automatically each visit    

New posts New posts    No new posts No new posts    Forum is locked Forum is locked
General forums
The world of freelance
Programming
Other projects
Poll
I love gifts

Yes goodies and presents! [2]
No but I love polls ! [0]

Related google ads