Concrete5 Load Area with Ajax - php

I am trying to pull area of a page with AJAX.
In JS I have on click I pass href to PHP;
in PHP(located in tools):
<?php defined('C5_EXECUTE') or die("Access Denied.");
$path = ($_POST['path']);
$page = Page::getByPath($path);
$a = new Area('Main');
$ret = $a->display($page);
echo json_encode($ret);
?>
If I make:
echo json_encode($page);
I receive the page so everything working, But when I try to receive an Area I get this error:
concrete\elements\block_area_header_view.php on line 5
In this File I found this
$c = Page::getCurrentPage();
$areaStyle = $c->getAreaCustomStyleRule($a);
So as I understand $c is null that why I have this error how can I fix this??

This line of code:
$ret = $a->display($page);
...does not do what you think it does. The "display" function does not return the content -- instead it outputs it to the browser. So your json_encode($ret) is just encoding and echo'ing an empty variable.
To capture the displayed content and put it into a variable, you can use php's output buffering feature, like so:
ob_start();
$a->display($page);
$ret = ob_end_clean();

Related

Using PHP to determine what HTML to write out

This block of PHP code prints out some information from a file in the directory, but I want the information printed out by echo to be used inside the HTML below it. Any help how to do this? Am I even asking this question right? Thanks.
if(array_pop($words) == "fulltrajectory.xyz") {
$DIR = explode("/",htmlspecialchars($_GET["name"]));
$truncatedDIR = array_pop($DIR);
$truncatedDIR2 = ''.implode("/",$DIR);
$conffile = fopen("/var/www/scmods/fileviewer/".$truncatedDIR2."/conf.txt",'r');
$line = trim(fgets($conffile));
while(!feof($conffile)) {
$words = preg_split('/\s+/',$line);
if(strcmp($words[0],"FROZENATOMS") == 0) {
print_r($words);
$frozen = implode(",", array_slice(preg_split('/\s+/',$line), 1));
}
$line = trim(fgets($conffile));
}
echo $frozen . "<br>";
}
?>
The above code prints out some information using an echo. The information printed out in that echo I want in the HTML code below where it has $PRINTHERE. How do I get it to do that? Thanks.
$("#btns").html(Jmol.jmolButton(jmolApplet0, "select atomno=[$PRINTHERE]; halos on;", "frozen on")
You just need to make sure that your file is a php file..
Then you can use html tags with php scripts, no need to add it using JS.
It's as simple as this:
<div>
<?php echo $PRINTHERE; ?>
</div>
Do remember that PHP is server-side and JS is client-side. But if you really want to do that, you can pass a php variable like this:
<script>
var print = <?php echo $PRINTHERE; ?>;
$("#btns").html(Jmol.jmolButton(jmolApplet0, "select atomno="+print+"; halos on;", "frozen on"));
</script>

How can I replace braces with <?php ?> in php file?

I wanna replace braces with <?php ?> in a file with php extension.
I have a class as a library and in this class I have three function like these:
function replace_left_delimeter($buffer)
{
return($this->replace_right_delimeter(str_replace("{", "<?php echo $", $buffer)));
}
function replace_right_delimeter($buffer)
{
return(str_replace("}", "; ?> ", $buffer));
}
function parser($view,$data)
{
ob_start(array($this,"replace_left_delimeter"));
include APP_DIR.DS.'view'.DS.$view.'.php';
ob_end_flush();
}
and I have a view file with php extension like this:
{tmp} tmpstr
in output I save just tmpstr and in source code in browser I get
<?php echo $tmp; ?>
tmpstr
In include file <? shown as <!--? and be comment. Why?
What you're trying to do here won't work. The replacements carried out by the output buffering callback occur after PHP code has already been parsed and executed. Introducing new PHP code tags at this stage won't cause them to be executed.
You will need to instead preprocess the PHP source file before evaluating it, e.g.
$tp = file_get_contents(APP_DIR.DS.'view'.DS.$view.'.php');
$tp = str_replace("{", "<?php echo \$", $tp);
$tp = str_replace("}", "; ?>", $tp);
eval($tp);
However, I'd strongly recommend using an existing template engine; this approach will be inefficient and limited. You might want to give Twig a shot, for instance.
do this:
function parser($view,$data)
{
$data=array("data"=>$data);
$template=file_get_contents(APP_DIR.DS.'view'.DS.$view.'.php');
$replace = array();
foreach ($data as $key => $value) {
#if $data is array...
$replace = array_merge(
$replace,array("{".$key."}"=>$value)
);
}
$template=strtr($template,$replace);
echo $template;
}
and ignore other two functions.
How does this work:
process.php:
<?php
$contents = file_get_contents('php://stdin');
$contents = preg_replace('/\{([a-zA-Z_][a-zA-Z_0-9]*)\}/', '<?php echo $\1; ?>', $contents);
echo $contents;
bash script:
process.php < my_file.php
Note that the above works by doing a one-off search and replace. You can easily modify the script if you want to do this on the fly.
Note also, that modifying PHP code from within PHP code is a bad idea. Self-modifying code can lead to hard-to-find bugs, and is often associated with malicious software. If you explain what you are trying to achieve - your purpose - you might get a better response.

Simple DOM file_get_html returns empty page

I'm having trouble with passing a complex url to file_get_html When I try this code
<?php
require_once("$_SERVER[DOCUMENT_ROOT]/dom/simple_html_dom.php");
$base = $_GET['url'];
//file_get_contents() reads remote webpage content
$html_base = file_get_html("http://www.realestateinvestar.com.au/ME2/dirmod.asp?sid=1A0FFDB3E8CD48909120C118D03F6016&nm=&type=news&mod=News&mid=9A02E3B96F2A415ABC72CB5F516B4C10&tier=3&nid=C67A9DD2C0144B9EB41DB58365C05927");
foreach($html_base->find('p') as $td) {
echo $td;
}
?>
It works
But if I try to pass the url as a variable via mysite.com/goget.php?url=http://www.realestateinvestar.com.au/ME2/dirmod.asp?sid=1A0FFDB3E8CD48909120C118D03F6016&nm=&type=news&mod=News&mid=9A02E3B96F2A415ABC72CB5F516B4C10&tier=3&nid=C67A9DD2C0144B9EB41DB58365C05927
<?php
require_once("$_SERVER[DOCUMENT_ROOT]/dom/simple_html_dom.php");
$base = $_GET['url'];
//file_get_contents() reads remote webpage content
$html_base = file_get_html($base);
foreach($html_base->find('p') as $td) {
echo $td;
}
?>
It returns a blank page.
Any help?
Use urlencode():
"mysite.com/goget.php?url="
.urlencode("http://www.realestateinvestar.com.au/ME2/dirmod.asp?sid=1A0FFDB3E8CD48909120C118D03F6016&nm=&type=news&mod=News&mid=9A02E3B96F2A415ABC72CB5F516B4C10&tier=3&nid=C67A9DD2C0144B9EB41DB58365C05927")

getting data from php variable

$content = file_get_contents('file.php');
echo $content;
nothing displays, expect when displaying the page sourcecode in browser the display is this
<? foreach(glob("folder/*.php") as $class_filename) { require_once($class_filename); } ?>
so it wont execute the script when getting the content..
file.php contains this code
<? foreach(glob("folder/*.php") as $class_filename) {
require_once($class_filename);
}
?>
and if I do next
$content = foreach(glob("folder/*.php") as $class_filename) { require_once($class_filename); } ?>
it complains about unexpected foreach...
is there a way to read the folder/.php files content to single $variable and then echo/print all folder/.php files to page where it should be?
thanks for help already.
Is that what you want to do ?
$content = '';
foreach (glob('folder/*.php') as $class){$content .= file_get_contents($class);}
echo $content;
What you're trying won't execute the contents of the "file.php", jsut display the contents of them on screen.
If you want to execute file.php, use eval ($content)
To capture the output, use something like:
ob_start(); // Don't echo anything but buffer it up
$codeToRun=file_get_contents('file.php'); // Get the contents of file.php
eval ($codeToRun); // Run the contents of file.php
$content=ob_get_flush(); // Dump anything that should have been echoed to a variable and stop buffering
echo $content; //echo the stuff that should have been echoed above

php how to scan full page for all POST variables and then echo at top of page?

I can't echo my variable above my CMS include code.. but if I echo the variable after, then it recognizes the $url variable.
Here is some code:
<?php
// here is my CMS inlcude code
$template = 'news_script';
$number = '';
$category = '';
include $cutepath.'/show_news.php';
?>
If I echo $url above the include code, it returns nothing. But below, it obviously recognizes it.
Is there a php function that scans the whole page and retrieves all the POST variables so you can use $url at the top of the php page with a header('Location:'. $url); script??
Obviously $url is being defined in your show_news.php script. PHP executes a script line-by-line, and will not magically "reach back" to set a variables value in an earlier line.
Another (ugly) way would be:
<?php
// here is my CMS inlcude code
$template = 'news_script';
$number = '';
$category = '';
ob_start();
include $cutepath.'/show_news.php';
$buffered_data=ob_get_contents();
ob_end_clean();
echo $url; // here you could place your: header('Location:'. $url);
echo $buffered_data;
?>

Categories