Please login or register.

Login with username, password and session length
Advanced search  

News:

Latest sNews - sNews 1.7 - with its own forums - for discussion and user mods.

Pages: [1] 2

Author Topic: PHP code inserting  (Read 9210 times)

Pali

  • Full Member
  • ***
  • Karma: 16
  • Posts: 111
    • LE-PAVON
PHP code inserting
« on: May 05, 2007, 05:01:14 PM »

hello everybody,

is it possible to put some PHP code directly to the article? I know that i can use include tag to insert some function from external file - but this isnt working well if i want to work with function parameters... in this case I have to create many files with different parameters, but with same function...

If i need to insert searchform anywhere to the article, i currently make file search.php with this code: and in article write this code: [include]search.php[/include]

There is no better way?

I need to control function parameters from article too,  

I was playing with file_include function but without success.

Thanks

Pali
Logged
I like sNews!

philmoz

  • High flyer
  • ULTIMATE member
  • ******
  • Karma: 161
  • Posts: 1988
    • fiddle 'n fly
PHP code inserting
« Reply #1 on: May 05, 2007, 06:27:18 PM »

best way would probably do it through the editor... somehow...

make new custom button that uses a prompt to collect the function-name, and parameters, and wrap them in a cusom tag eg [func] [/func]
then process these in file_include function.

to see how to do most of this, see the insert hidden article mod for adding button, altering js editor and processing in file_include
Logged
Of all the things I have lost, it is my mind that I miss the most.

philmoz

  • High flyer
  • ULTIMATE member
  • ******
  • Karma: 161
  • Posts: 1988
    • fiddle 'n fly
PHP code inserting
« Reply #2 on: May 05, 2007, 08:59:50 PM »

here's a start...

in file_include function, make start like this... [edited]
Quote
function file_include($text, $shorten) {
       $fulltext = substr($text, 0, $shorten);

      /*auto function*/
    $ins = strpos($fulltext, '[/func]');
    if ($ins > 0) {
        $text = str_replace('[func]', '|&|', $fulltext);
        $text = str_replace('[/func]', '|&|', $text);
        $text = explode('|&|', $text);$num=count($text)-1;$i=1;
      while ($i <= $num) {
       $func = explode(':|:',$text[$i]);
       ob_start();
       $returned = call_user_func_array($func[0],explode(',',$func[1]));
       $text[$i]= ob_get_clean();
       if (empty($text[$i])){$text[$i]=$returned;}
       $i=$i+2;
      }
       $fulltext = implode($text);
    }
in form_articles(), add this line for the button
Quote
html_input('button', 'func', '', 'Function', '', 'button', 'onclick="tag(\'func\')"', '', '', '', '', '', '', '', '');
in js -- editor, add this after similar for include.
Quote
else if(tag == 'func'){
   var url = prompt("", ""); // function name
   var title = prompt("", ""); //params, seperated by comma.
   if(url != null) { start = "[func]" + url + ':|:'+title ; end = "[/func]"; } else { start = ""; end = ""; }
   }
and language vars
Quote
$l['function']='Function Name';$l['parameters']='Parameters';
and a test function, just to see output of this as it is.
Note requires 3 parameters, so when asked, enter 3 items seperated by comma.
Quote
function hello($parm,$parm2,$parm3){
         $echo ="
hello there ".$parm.
          "
hello there ".$parm2.
          "
hello there ".$parm3."
";
return $echo;
}
Logged
Of all the things I have lost, it is my mind that I miss the most.

Pali

  • Full Member
  • ***
  • Karma: 16
  • Posts: 111
    • LE-PAVON
PHP code inserting
« Reply #3 on: May 05, 2007, 09:38:35 PM »

Thanks Phil, you are GREAT!

there is just one thing - i entered comma separated values so my code looks like this:

Code: [Select]
[func]hello:|:adin,dva,tri[/func]but the output looks like this:

hello there adin,dva,tri
hello there
hello there

?

if i enter searchform there, it is working great.... (no parameters) :)

other thing - i can use just one func like this - other are showing like standard text only (without tags)

thanks
Logged
I like sNews!

codetwist

  • Hero Member
  • *****
  • Karma: 50
  • Posts: 940
PHP code inserting
« Reply #4 on: May 05, 2007, 10:19:33 PM »

Regarding parameters:

Instead of lines
Code: [Select]
       $param=$func[1];
        $text[1]= call_user_func($callback,$param);
try these
Code: [Select]
       $paramArr = explode( ',', $func[1]);
        $text[1]= call_user_func_array($callback,$paramArr);
Logged

philmoz

  • High flyer
  • ULTIMATE member
  • ******
  • Karma: 161
  • Posts: 1988
    • fiddle 'n fly
PHP code inserting
« Reply #5 on: May 06, 2007, 04:11:24 AM »

yep, what codetwist said...

as it stands, it will only operate for a single case of [func] [/func]
for more than one, you need to set it in a loop.


updated my original -- with looping, and buffer control.
http://www.solucija.com/forum/viewtopic.php?pid=29714#p29714

only bit altered is the addition to file_include.
Logged
Of all the things I have lost, it is my mind that I miss the most.

Pali

  • Full Member
  • ***
  • Karma: 16
  • Posts: 111
    • LE-PAVON
PHP code inserting
« Reply #6 on: May 06, 2007, 10:36:44 AM »

hello phil, your new code is working except hello function;

Code: [Select]
[func]hello:|:pali,kali,hali[/func]
[func]menu_articles:|:5,2[/func]
[func]new_comments:|:1,25[/func]
shows right output for menu_articles and newcomments...

i changed function hello to give result with echo not return and its working...
Logged
I like sNews!

philmoz

  • High flyer
  • ULTIMATE member
  • ******
  • Karma: 161
  • Posts: 1988
    • fiddle 'n fly
PHP code inserting
« Reply #7 on: May 06, 2007, 01:19:49 PM »

hmmm, having trapped echo ending functions, I've lost return ending functions...

Anyone with ideas on doing both?

.... woll play with it a bit... have an idea ;)


[edited {after ruimendespost]}....
have cracked it I think.

have added alterations to my original code post.

Functions that return $value are trapped and processed, and also functions that echo $value are trapped and processed.

Tat said, YMMV
added this
Quote
if (empty($text[$i])){$text[$i]=$returned;}
and altered the call_user_func_array line.
Logged
Of all the things I have lost, it is my mind that I miss the most.

philmoz

  • High flyer
  • ULTIMATE member
  • ******
  • Karma: 161
  • Posts: 1988
    • fiddle 'n fly
PHP code inserting
« Reply #8 on: May 06, 2007, 01:57:50 PM »

@ruimendes
This works as is, only because you have control over the custom function output... currently returning an item/value for further treatment elsewhere.

In cases where it is advantageous to echo a value from the function, the method used with your gallery script falls over. (This happens to be the cause of the mathcaptcha invalidation problem in 1.5.31, of which there are many discussions, and various fixing methods).

What I have now, (after a few mind bending hours on php.net), managed to get working (to some extent ) anyway, is a way to capture and use the output of either method... which then allows you to use the snews functions (regardless of endings) in some rather different places, which is what oniip more or less asked for. Just as an exercise, alter return $txt; to echo $txt; in your sgallery() function to see what I mean.... then change it back to make it work as you expect :D

That said, there is no need for you to alter your gallery adaption, as it works as is....
Logged
Of all the things I have lost, it is my mind that I miss the most.

Pali

  • Full Member
  • ***
  • Karma: 16
  • Posts: 111
    • LE-PAVON
PHP code inserting
« Reply #9 on: May 06, 2007, 04:45:40 PM »

phil, absolutely great investigation - like CSI: Australia  :cool:
Logged
I like sNews!

Rui Mendes

  • Development,Testing, Support
  • Hero Member
  • *****
  • Karma: 195
  • Posts: 1009
  • sNews1.7
    • Comunidade Portuguesa
PHP code inserting
« Reply #10 on: May 06, 2007, 08:11:22 PM »

Hello again.

I try what you said, it works except the gallery goes to top of the article.

There is another way to catch parameters

this is just an example
Quote
$txt = "your_function:1: 5:3";
list($function, $param1,$param2,$param3) = explode(":", $txt);

echo $function.'
';
echo $param1.'
';
echo $param2.'
';
echo $param3.'
';

Result:
your_function
1
5
3
Best regards
Rui
Logged
Need a Job on Europe. Linkdin - Facebook / Group

philmoz

  • High flyer
  • ULTIMATE member
  • ******
  • Karma: 161
  • Posts: 1988
    • fiddle 'n fly
PHP code inserting
« Reply #11 on: May 07, 2007, 09:32:53 AM »

Quote from: ruimendes
Hello again.

I try what you said, it works except the gallery goes to top of the article.
as expected, because you are calling a function that outputs via echo, hence, as soon as you call it, it will put it out to browser-- you are not able to assign it to a var unless you capture the buffer, and redirect it.

Quote from: ruimendes
There is another way to catch parameters
this is just an example
Quote
$txt = "your_function:1: 5:3";
list($function, $param1,$param2,$param3) = explode(":", $txt);
To a certain extent that is what my code above does. However, it then has to take the $function value, and treat it as a working function. The method I used makes use of inbuilt php functions just for that task.

The problem I faced, wasn't collection the parameters, but capturing the resultant functions output, so as to ensure that it's results happened, and were displayed in the correct place.

--- now that this is all clear in my mind Rui, thx for keeping the discussion going. A little reinforcement does wonders for the understanding :D :D
Logged
Of all the things I have lost, it is my mind that I miss the most.

Rui Mendes

  • Development,Testing, Support
  • Hero Member
  • *****
  • Karma: 195
  • Posts: 1009
  • sNews1.7
    • Comunidade Portuguesa
PHP code inserting
« Reply #12 on: May 13, 2007, 02:08:18 PM »

Sorry I remove one post by mistake.

I fix my function file_include

Code: [Select]
function file_include($text, $shorten) {
    $fulltext = substr($text, 0, $shorten);
    $ginc = strpos($fulltext, '[/gallery]');
    $inc = strpos($fulltext, '[/include]');
    if ($ginc > 0) {
        $num = substr_count($fulltext,'[gallery]');
        $dtext = str_replace('[gallery]', ' [gallery]', $fulltext);
        $dtext = str_replace('[/gallery]', '[/gallery] ', $dtext);
        $dtext = explode(' ',$dtext);
        $num1 = count($dtext);
        for ($i = 0; $i<=$num1; $i++) {
            $stext = $dtext[$i];
            $num2 = strpos($stext,'[/gallery]');
            if ($num2>0) {
                $ttext = str_replace('[gallery]','', $stext);
                $ttext = str_replace('[/gallery]','', $ttext);
                $dtext[$i]= sgallery($ttext);}}
            $fulltext = implode(' ',$dtext);}
...
My question, supose you have

$variable ="function hello() {
  echo 'Hello Worldi;
}
hello();
?>"

can we execute $variable ?

Best regards
Rui
Logged
Need a Job on Europe. Linkdin - Facebook / Group

philmoz

  • High flyer
  • ULTIMATE member
  • ******
  • Karma: 161
  • Posts: 1988
    • fiddle 'n fly
PHP code inserting
« Reply #13 on: May 13, 2007, 02:58:19 PM »

Quote from: ruimendes
My question, supose you have

$variable ="function hello() {
  echo 'Hello Worldi;
}
hello();
?>"

can we execute $variable ?
2 endings for function hello()

1) with return $var;
Quote
function hello() {
  $echo= 'Hello Worldi';
return $echo;
}
2) with echo either as last statement, or within...
Quote
function hello() {
  $echo= 'Hello Worldi';
echo $echo;
}
OR
function hello() {
  echo 'Hello Worldi';
}
now, if we say
$variable=hello();
In case 1), the result of hello() WILL be stored in $variable, which we can then echo whenever we choose.

In case 2), $variable is not filled, because as the function hello() executes, it echos it's result straight to screen.

That's why I had to trap the buffer, before it could output anything to screen, but that 'failed' when the function was as in case 1), and it wasn't going to screen anyway--- hence the 'if(empty' line.
Quote
ob_start();
        $returned = call_user_func_array($func[0],explode(',',$func[1]));
        $text[$i]= ob_get_clean();
        if (empty($text[$i])){$text[$i]=$returned;}
Logged
Of all the things I have lost, it is my mind that I miss the most.

codetwist

  • Hero Member
  • *****
  • Karma: 50
  • Posts: 940
PHP code inserting
« Reply #14 on: May 13, 2007, 03:16:02 PM »

@rui :

You can do it a little different:
Code: [Select]
function hello() {
  echo 'Hello World';
}
$variable ='hello';
?>"
And after inclusion of such code block just execute $variable:
Code: [Select]
$variable();And of course there is eval(). It might work with Your code just like this - eval($variable); ( should  be removed from $variable value)
Logged
Pages: [1] 2