FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » FUDforum » How To » FudAPI Examples
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
FudAPI Examples [message #19431] Tue, 20 July 2004 07:19 Go to next message
zushiba is currently offline  zushiba   United States
Messages: 20
Registered: May 2003
Karma: 0
Junior Member
I was wondering if I could get some examples of the FudAPI at work such as posting a topic or displaying online users?
Re: FudAPI Examples [message #19434 is a reply to message #19431] Tue, 20 July 2004 13:05 Go to previous messageGo to next message
Ilia is currently offline  Ilia   Canada
Messages: 13241
Registered: January 2002
Karma: 0
Senior Member
Administrator
Core Developer
Look at the comments inside the fudapi file.

FUDforum Core Developer
Re: FudAPI Examples [message #19452 is a reply to message #19431] Wed, 21 July 2004 09:57 Go to previous messageGo to next message
zushiba is currently offline  zushiba   United States
Messages: 20
Registered: May 2003
Karma: 0
Junior Member
Ah I got it.
Can anyone tell me why


/* {{{ proto: void fud_delete_topic(mixed arg) }}}
 * This function deletes topic(s) specified by arg.
 */
function fud_delete_topic($arg)
{
	fud_delete_msg(_fud_simple_fetch_query($arg, "SELECT root_msg_id FROM ".$GLOBALS['DBHOST_TBL_PREFIX']."thread WHERE id IN({ARG})"));
}

Creates a copy of the topic I tell it to delete?
Re: FudAPI Examples [message #19456 is a reply to message #19452] Wed, 21 July 2004 13:18 Go to previous messageGo to next message
Ilia is currently offline  Ilia   Canada
Messages: 13241
Registered: January 2002
Karma: 0
Senior Member
Administrator
Core Developer
Deletion code had a bug in it, it has since been fixed.

FUDforum Core Developer
Re: FudAPI Examples [message #19563 is a reply to message #19456] Thu, 05 August 2004 10:07 Go to previous messageGo to next message
mocara is currently offline  mocara   United Kingdom
Messages: 157
Registered: January 2004
Karma: 0
Senior Member
Will the fudapi be extended to include interfaces to the private messages functions? Or am I just missing that? Smile

mocara
Re: FudAPI Examples [message #19568 is a reply to message #19563] Thu, 05 August 2004 12:53 Go to previous messageGo to next message
Ilia is currently offline  Ilia   Canada
Messages: 13241
Registered: January 2002
Karma: 0
Senior Member
Administrator
Core Developer
At the moment now, but if you can make a good case for why they should be added, I can be persuaded to do so Smile

FUDforum Core Developer
Re: FudAPI Examples [message #34200 is a reply to message #19431] Fri, 13 October 2006 05:43 Go to previous messageGo to next message
DaveQB is currently offline  DaveQB   Australia
Messages: 109
Registered: January 2006
Location: Sydney
Karma: 0
Senior Member

So one includes the fudapi.inc.php file

include("fudapi.inc.php");


Then you can use the functions/methods found inside of it ?
Does one need instantiate it first ? How does one do this if so ? And then how does one call the methods inside it ?

Thanks guys.
Re: FudAPI Examples [message #34204 is a reply to message #34200] Fri, 13 October 2006 13:32 Go to previous messageGo to next message
Ilia is currently offline  Ilia   Canada
Messages: 13241
Registered: January 2002
Karma: 0
Senior Member
Administrator
Core Developer
Yes you include it, you may need to modify the top of the fudapi.inc.php script to reference the correct location of GLOBALS.php. Once you've included the file you can just use the functions found within.

FUDforum Core Developer
Re: FudAPI Examples [message #34214 is a reply to message #19431] Fri, 13 October 2006 23:28 Go to previous messageGo to next message
DaveQB is currently offline  DaveQB   Australia
Messages: 109
Registered: January 2006
Location: Sydney
Karma: 0
Senior Member

Thanks mate Cool
That did the trick.

So I am learning with the fud_fetch_online_users function

After uncommenting the GLOBALS.php line in fudapi.inc.php and including said file I run this bit of code


$ret = fud_fetch_online_users();
echo "$ret";



If there is someone of the forum I get
"Object#1"
Or if there is no one on the forum I get
"Array"

So I guess I need to learn about PHP Array handling.
PS I tried the Python way using
echo "$ret[0]";


But that failed.


All I am ultimately trying to do it get all the users in my DB out with their details so I can put them into the Gallery2 DB so I can continue my integration of Gallery2 into FudForum. GPL of course so we can all use it Cool

Re: FudAPI Examples [message #34222 is a reply to message #34214] Sat, 14 October 2006 15:03 Go to previous messageGo to next message
Ilia is currently offline  Ilia   Canada
Messages: 13241
Registered: January 2002
Karma: 0
Senior Member
Administrator
Core Developer
The function works like this:

If there is only 1 user you get an object representing that user's information, however if there is >1 user you get an array objects, where each object == 1 user.


FUDforum Core Developer
Re: FudAPI Examples [message #34229 is a reply to message #19431] Sun, 15 October 2006 07:16 Go to previous messageGo to next message
DaveQB is currently offline  DaveQB   Australia
Messages: 109
Registered: January 2006
Location: Sydney
Karma: 0
Senior Member

Thanks again Ilia

I'll have to look up how Objects and Arrays work in php as my next step.

Cheers.
Re: FudAPI Examples [message #34246 is a reply to message #19431] Mon, 16 October 2006 04:15 Go to previous messageGo to next message
DaveQB is currently offline  DaveQB   Australia
Messages: 109
Registered: January 2006
Location: Sydney
Karma: 0
Senior Member

Got something and posting what I found for any other newbies, like me, that might be following this thread.

I created this script.

<?php

include("/full/path/to/this/file/fudapi.inc.php");

$ret = fud_fetch_online_users();
print_r($ret);
echo "\n\n";

print "$ret->alias\n";

?>



The print_r($ret);

returns:

stdClass Object
(   
    [id] => 2
    [alias] => DaveQB
    [time_sec] => 1160962637
    [private] => 0
)                               



while the print "$ret->alias";
returns the username

DaveQB


Thats with someone logged on.
With no one online you simply get


Array                                                                                                              
(                                                                                                                  
)                                                                                                                  
                                                                                                                   
                                                                                                                   
PHP Notice:  Trying to get property of non-object in /home/wspirates/domains/forums.wspirates.com/public_html/users
.php on line 9                                                                                                     


And line 9 contains the printing of the alias var in the object $ret. So one needs to check if the object is created [that there is someone online] before you start using values from the object created.

Hope that helps someone.
Re: FudAPI Examples [message #34252 is a reply to message #19431] Mon, 16 October 2006 12:26 Go to previous messageGo to next message
Ernesto is currently offline  Ernesto   Sweden
Messages: 413
Registered: August 2005
Karma: 0
Senior Member
This is awesome and helps me alot.

However, which globals.php should I set the path too? (I have several, but I guess some are just "mirrors"?)


Re: FudAPI Examples [message #34260 is a reply to message #34252] Mon, 16 October 2006 13:39 Go to previous messageGo to next message
Ilia is currently offline  Ilia   Canada
Messages: 13241
Registered: January 2002
Karma: 0
Senior Member
Administrator
Core Developer
It really shouldn't matter since there is only one "real" one inside the include/ directory, the rest are symlinks to it.

FUDforum Core Developer
Re: FudAPI Examples [message #34263 is a reply to message #34252] Mon, 16 October 2006 14:12 Go to previous messageGo to next message
DaveQB is currently offline  DaveQB   Australia
Messages: 109
Registered: January 2006
Location: Sydney
Karma: 0
Senior Member

Ernesto wrote on Mon, 16 October 2006 22:26

This is awesome and helps me alot.

However, which globals.php should I set the path too? (I have several, but I guess some are just "mirrors"?)



Thanks.

It was just the kinda thing I need to start me off. Hope it does the same for you ...
Re: FudAPI Examples [message #34265 is a reply to message #19431] Mon, 16 October 2006 14:29 Go to previous messageGo to next message
Ernesto is currently offline  Ernesto   Sweden
Messages: 413
Registered: August 2005
Karma: 0
Senior Member
Using your above code example, for me it outputs:

Array ( [0] => stdClass Object ( [id] => 1 [alias] => Blah [time_sec] => 1161007717 [private] => 0 ))

But when you try to grab just the alias in the array string thingy below, it returns nothing.

So well, in conclusion, I don't understand really what (or rather how) your code works, so a bunch of more examples would be absolutely magnificent!


Re: FudAPI Examples [message #34266 is a reply to message #19431] Mon, 16 October 2006 14:45 Go to previous messageGo to next message
DaveQB is currently offline  DaveQB   Australia
Messages: 109
Registered: January 2006
Location: Sydney
Karma: 0
Senior Member

It seems your getting an Array of objects which is what Ilia said you'd get with more then 1 member on the board when you run this.

I am not sure how to print data from an Array of objects. Your up to where I am. Perhaps someone else knows how to extract data from an Array of Objects.

Re: FudAPI Examples [message #34268 is a reply to message #19431] Mon, 16 October 2006 16:35 Go to previous messageGo to next message
Ernesto is currently offline  Ernesto   Sweden
Messages: 413
Registered: August 2005
Karma: 0
Senior Member
I tried this, but didnt work:
<?php
include ("fudapi.inc.php");
$ret fud_fetch_online_users();
print_r($ret);

foreach (
$ret as $user) {
echo 
$user['alias'];
}
?>


Re: FudAPI Examples [message #34271 is a reply to message #19431] Mon, 16 October 2006 21:05 Go to previous messageGo to next message
Ernesto is currently offline  Ernesto   Sweden
Messages: 413
Registered: August 2005
Karma: 0
Senior Member
This works:
<?php
include ("fudapi.inc.php");
$ret fud_fetch_online_users();
foreach (
$ret as $item) {
  echo 
$item->alias;
}
?>


Re: FudAPI Examples [message #34274 is a reply to message #19431] Tue, 17 October 2006 05:16 Go to previous messageGo to next message
DaveQB is currently offline  DaveQB   Australia
Messages: 109
Registered: January 2006
Location: Sydney
Karma: 0
Senior Member

Ahhh thanks for that Ernesto.

Makes a whole lotta sense; just use a for loop to iterate through all the users found to be online.

I am having a tough time getting used to the PHP way of doing things. Oh well.

Re: FudAPI Examples [message #35773 is a reply to message #34200] Thu, 08 February 2007 01:28 Go to previous messageGo to next message
chickenout is currently offline  chickenout   United States
Messages: 55
Registered: February 2005
Location: Philadelphia
Karma: 0
Member
DaveQB wrote on Fri, 13 October 2006 01:43

So one includes the fudapi.inc.php file

include("fudapi.inc.php");


Then you can use the functions/methods found inside of it ?
Does one need instantiate it first ? How does one do this if so ? And then how does one call the methods inside it ?

Thanks guys.



Include it where?
Re: FudAPI Examples [message #35780 is a reply to message #35773] Fri, 09 February 2007 00:38 Go to previous messageGo to next message
Ilia is currently offline  Ilia   Canada
Messages: 13241
Registered: January 2002
Karma: 0
Senior Member
Administrator
Core Developer
You need to include the file inside the PHP script from which you intend to use the functions it provides.

FUDforum Core Developer
Re: FudAPI Examples [message #35800 is a reply to message #35780] Fri, 09 February 2007 20:15 Go to previous messageGo to next message
chickenout is currently offline  chickenout   United States
Messages: 55
Registered: February 2005
Location: Philadelphia
Karma: 0
Member
Ilia wrote on Thu, 08 February 2007 19:38

You need to include the file inside the PHP script from which you intend to use the functions it provides.


Ok, so I would have to include it in the forum_login.php if I was trying to accept a login from a 3rd party app?
Re: FudAPI Examples [message #35836 is a reply to message #35800] Wed, 14 February 2007 23:31 Go to previous messageGo to next message
Ilia is currently offline  Ilia   Canada
Messages: 13241
Registered: January 2002
Karma: 0
Senior Member
Administrator
Core Developer
That is correct.

FUDforum Core Developer
Re: FudAPI Examples [message #35934 is a reply to message #19431] Wed, 21 February 2007 00:40 Go to previous messageGo to next message
dardhal is currently offline  dardhal   Spain
Messages: 9
Registered: November 2006
Location: Spain
Karma: 0
Junior Member
Sorry if the following is too naive, or has been somewhat been replied before elsewhere, but didn't find an answer.

The fact is I am doing some kind of thread and message automation: a cron job downloads a document (PDF), checks some data inside it, and then creates a new reply under an already existing thread with a simple body text and the ZIPped PDF document attached.

The horrible but simple PHP code I have created is the following. It is run from the command line through a PHP 5.2.0 Debian binary:

#!/usr/bin/php -q
<?php

require_once("/srv/www/www.site.es/fudforum-2.7.6-nobrowse/include/GLOBALS.php");
require_once("/srv/www/www.site.es/fudforum-2.7.6-nobrowse/scripts/fudapi.inc.php");

$subject = "Message subject";
$body = "Simple, concise, message body";
$attachments = array("/tmp/compressed_pdf.zip");
fud_new_reply($subject, $body, 0, "username", 1080, null, $attachments, null);

?>


Except for a couple of warnings I consider irrelevant to the problem, it seems to run OK, and in fact a new message is created under the message with ID 1080. Even the attachment information gets added, the linkable icon and that. Size reported for the attached file is OK, however, it downloads a zero-length file.

Checking the FUDforum logs shows the problem. The PHP script runs, but leaves the following in the logs:

[Error] unable to move uploaded file
[Message Sent to User]
From: srv/www/www.site.es/fudforum-2.7.6-nobrowse/tmp/fuda_eqxSg9
To: /srv/www/www.site.es/fudforum-2.7.6-nobrowse/files/190.atch
[User IP] 0.0.0.0
[Requested URL] http://


And the same problem happens running the PHP script (remember, from the command line) as user root or as the webserver user (www-data). Both users have enough permissions on both directories, and the original file "/tmp/compressed_pdf.zip" is also accessible and with proper permissions.

Any clue about this? Maybe I am oversimplifying the script, but my PHP skills are null, and fudAPI knowledge is still quite low.
Re: FudAPI Examples [message #35939 is a reply to message #35934] Thu, 22 February 2007 00:33 Go to previous messageGo to next message
Ilia is currently offline  Ilia   Canada
Messages: 13241
Registered: January 2002
Karma: 0
Senior Member
Administrator
Core Developer
I presume you are running this code on the command line? You need to make sure that the files/ directory where the attachments are placed is writable by the user the script run as. In most cases this means you need to chmod the directory 777.

FUDforum Core Developer
Re: FudAPI Examples [message #35940 is a reply to message #35939] Thu, 22 February 2007 01:07 Go to previous messageGo to next message
dardhal is currently offline  dardhal   Spain
Messages: 9
Registered: November 2006
Location: Spain
Karma: 0
Junior Member
Ilia wrote on Thu, 22 February 2007 01:33

I presume you are running this code on the command line? You need to make sure that the files/ directory where the attachments are placed is writable by the user the script run as. In most cases this means you need to chmod the directory 777.


Yes, from the command line, and using FUDforum 2.7.6 and PHP 5.2.0. Tried with both root and www-data, and both files/ and tmp/ directories are owned by www-data and 755, but the problem is the same in both cases.

I stat'ed tmp/ and files/ and while it seems something happens under tmp/ (last modification time is updated), nothing seems to be done under files/
Re: FudAPI Examples [message #35953 is a reply to message #35940] Fri, 23 February 2007 01:42 Go to previous messageGo to next message
Ilia is currently offline  Ilia   Canada
Messages: 13241
Registered: January 2002
Karma: 0
Senior Member
Administrator
Core Developer
You need permissions to be 777 not 755

FUDforum Core Developer
Re: FudAPI Examples [message #36091 is a reply to message #35953] Sat, 03 March 2007 20:18 Go to previous messageGo to next message
dardhal is currently offline  dardhal   Spain
Messages: 9
Registered: November 2006
Location: Spain
Karma: 0
Junior Member
Ilia wrote on Fri, 23 February 2007 02:42

You need permissions to be 777 not 755


Sorry for the delay. Even after chmod'ing both tmp/ and files/ directories to 777, the script doesn't work with the same error "unable to move uploaded file". From the command line and running the PHP script as root.

I have had some free time to investigate, and I think I have found the place where it fails. function _fud_message_post calls attach_add, and inside this function safe_attachment_copy gets called, both are implemented in include/theme/default/attach.inc

Inside function safe_attachment_copy there is this little piece of code:

<?php
if (!$ext && !move_uploaded_file($source$loc)) {
                
error_dialog('unable to move uploaded file''From: '.$source.' To: '.$loc'ATCH');
        } else if (
$ext && !copy($source$loc)) {
                
error_dialog('unable to handle file attachment''From: '.$source.' To: '.$loc'ATCH');
        }
?>


And as the error message is an "unable to move uploaded file", and $ext contains zero from the caller, it seems PHP's move_uploaded_file is failing. From the PHP documentation:

Quote:

This function checks to ensure that the file designated by filename is a valid upload file (meaning that it was uploaded via PHP's HTTP POST upload mechanism). If the file is valid, it will be moved to the filename given by destination

See also is_uploaded_file(), and the section Handling file uploads for a simple usage example.



So I have inserted a test just before the move_uploaded_file call, just to see if from PHP point of view the attachment was uploaded or not:
<?php
if ( is_uploaded_file($source) ) {
                
error_dialog('The file WAS uploaded''OK');
        }
        else {
                
error_dialog('The file was NOT uploaded','KO');
        }
?>


And I get, as expected, the following error:
index.php?t=getfile&id=2815&private=0

So it seems the fix is to make PHP believe the attachment was uploaded, even if it was not. Don't know if this is possible, and if it requires modification of my crappy PHP script, some kind of FUDapi initialization, etc.
  • Attachment: error.jpg
    (Size: 9.82KB, Downloaded 1681 times)
Re: FudAPI Examples [message #36100 is a reply to message #36091] Sun, 04 March 2007 17:58 Go to previous messageGo to next message
Ilia is currently offline  Ilia   Canada
Messages: 13241
Registered: January 2002
Karma: 0
Senior Member
Administrator
Core Developer
This is a bug in fudapi, here is the URL to the patch that fixes the bug

http://cvs.prohost.org/c/index.cgi/FUDforum/chngview?cn=11749


FUDforum Core Developer
Re: FudAPI Examples [message #36103 is a reply to message #36100] Sun, 04 March 2007 18:50 Go to previous messageGo to next message
dardhal is currently offline  dardhal   Spain
Messages: 9
Registered: November 2006
Location: Spain
Karma: 0
Junior Member
Ilia wrote on Sun, 04 March 2007 18:58

This is a bug in fudapi, here is the URL to the patch that fixes the bug

http://cvs.prohost.org/c/index.cgi/FUDforum/chngview?cn=11749


I think the above patch doesn't fully fix the bug. If I use the fix above, there is no complain message, but the attachment is not even created, in fact it is not shown in the message.

I think this is a problem with function attach_add, which is defined as:
function attach_add($at, $owner, $attach_opt=0, $ext=0)


The patch above makes FUDapi call the function with attach_opt set to 1, and while I don't know the meaning of this parameter, I think what the patch tried to set is ext, not attach_opt.

In fact, I remade the patch to the following:
-			$val = attach_add($at, $msg->poster_id);
+			$val = attach_add($at, $msg->poster_id, 0, 1);


Tried again, and now it works (but when the script is run as root, the browser is unable to download the attachment, due to permissions, the script run as www-data is ok). So I think my theory is right, or at least points in the right direction.

Hope it helps.
Re: FudAPI Examples [message #36122 is a reply to message #36103] Tue, 06 March 2007 00:08 Go to previous message
Ilia is currently offline  Ilia   Canada
Messages: 13241
Registered: January 2002
Karma: 0
Senior Member
Administrator
Core Developer
Thanks for poiting out the problem it is now fixed in CVS. As far as permissions go, you may want to unlock the forum files. In this case new files are created with a world-read permission, allowing you to run the script as root.

FUDforum Core Developer
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Deleting Moderation Queue
Next Topic: integration with a cms (the user database) possible?
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Tue Nov 26 18:12:34 GMT 2024

Total time taken to generate the page: 0.03037 seconds