Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2006-05-02 18:27:19

Jaro
Member
From: S/F
Registered: 2004-11-18
Posts: 89

Can't include php code into the form

Hi guys,

being a php illiterate I’m having problems with simple including a php code into the form.

I created a new form (misc) and this is what I’m trying to input there:

<code>
<txp:php>

function tla_ads() {

// Number of seconds before connection to XML times out // (This can be left the way it is) $CONNECTION_TIMEOUT = 10;

// Local file to store XML // This file MUST be writable by web server // You should create a blank file and CHMOD it to 666 $LOCAL_XML_FILENAME = “local_222334.xml”;

if( !file_exists($LOCAL_XML_FILENAME) ) die(“Text Link Ads script error: $LOCAL_XML_FILENAME does not exist. Please create a blank file named $LOCAL_XML_FILENAME.”); if( !is_writable($LOCAL_XML_FILENAME) ) die(“Text Link Ads script error: $LOCAL_XML_FILENAME is not writable. Please set write permissions on $LOCAL_XML_FILENAME.”);

if( filemtime($LOCAL_XML_FILENAME) < (time() – 3600) || filesize($LOCAL_XML_FILENAME) < 20) { tla_updateLocalXML(“http://www.text-link-ads.com/xml.php?inventory_key=016×5FPHK4XXXXXXX&referer=” . urlencode($_SERVER[‘REQUEST_URI’]) . “&user_agent=” . urlencode($_SERVER[‘HTTP_USER_AGENT’]), $LOCAL_XML_FILENAME, $CONNECTION_TIMEOUT); }

$xml = tla_getLocalXML($LOCAL_XML_FILENAME);

$arr_xml = tla_decodeXML($xml);

if ( is_array($arr_xml) ) { echo “<table style=‘border: 1px solid #000000; border-spacing: 0px; background-color: #F0F0F0;’ cellpadding=‘5’ cellspacing=‘0’><tbody>”;

$columns = 1; for ($i = 0; $i < count($arr_xml[‘URL’]);) { echo “<tr>”; for ($j = 0; $j < $columns; $j++) { if ($arr_xml[‘URL’][$i]) { echo “<td><span style=‘font-size:12px; color:#000000;’>”.$arr_xml[‘BeforeText’][$i].” <a href=’”.$arr_xml[‘URL’][$i].”’ style=‘font-size:12px; color:#000000’>”.$arr_xml[‘Text’][$i].”</a> “.$arr_xml[‘AfterText’][$i].”</span></td>”; } else { echo “<td> </td>”; } $i++; } echo “</tr>”; }

echo “</tbody></table>”; }

}

function tla_updateLocalXML($url, $file, $time_out)
{ $xml = file_get_contents_tla($url, $time_out); $xml = substr($xml, strpos($xml,’<?’));

if ($handle = fopen($file, “w”)) { fwrite($handle, $xml); fclose($handle); }

}

function tla_getLocalXML($file)
{ if($handle = fopen($file, “r”)){ $contents = fread($handle, filesize($file)+1); fclose($handle); } return $contents;
}

function file_get_contents_tla($url, $time_out)
{ $url = parse_url($url);

if ($handle = @fsockopen ($url[“host”], 80)) { if(function_exists(socket_set_timeout)) { socket_set_timeout($handle,$time_out,0); } else if(function_exists(stream_set_timeout)) { stream_set_timeout($handle,$time_out,0); }

fwrite ($handle, “GET $url[path]?$url[query] HTTP/1.0\r\nHost: $url[host]\r\nConnection: Close\r\n\r\n”); while (!feof($handle)) { $string .= @fread($handle, 40960); } fclose($handle); }

return $string;
}

function tla_decodeXML($xmlstg)
{

if( !function_exists(‘html_entity_decode’) ){ function html_entity_decode($string) { // replace numeric entities $string = preg_replace(‘~&#x([0-9a-f]+);~ei’, ‘chr(hexdec(“\1”))’, $string); $string = preg_replace(‘~&#([0-9]+);~e’, ‘chr(\1)’, $string); // replace literal entities $trans_tbl = get_html_translation_table(HTML_ENTITIES); $trans_tbl = array_flip($trans_tbl); return strtr($string, $trans_tbl); } }

$out = “”; $retarr = “”;

preg_match_all (“/<(.?)>(.?)</”, $xmlstg, $out, PREG_SET_ORDER); $search_ar = array(‘<’, ‘>’, ‘”’); $replace_ar = array(‘<’, ‘>’, ‘”’); $n = 0; while (isset($out[$n])) { $retarr[$out[$n]1][] = str_replace($search_ar, $replace_ar,html_entity_decode(strip_tags($out[$n]0))); $n++; } return $retarr;
}

tla_ads();

</txp:php>
</code>

When I try to save the form I get 503 error (Service Temporarily Unavailable). I have enabled using php on txp pages and in articles under admin – settings – advanced settings. I also checked the faq but didn’t really figure out what I’m doing wrong. Can anyone help me out here?

Last edited by Jaro (2006-05-02 18:30:05)

Offline

#2 2006-05-02 22:45:06

zem
Developer Emeritus
From: Melbourne, Australia
Registered: 2004-04-08
Posts: 2,579

Re: Can't include php code into the form

When I try to save the form I get 503 error (Service Temporarily Unavailable)

Though the error message is different, it’s probably caused by this issue.

For a large chunk of code like that, you’d be better off making a plugin. Or you could store the code in a file and include it – see the FAQ.

Last edited by zem (2006-05-02 22:45:51)


Alex

Offline

#3 2006-05-03 06:07:49

Jaro
Member
From: S/F
Registered: 2004-11-18
Posts: 89

Re: Can't include php code into the form

Thanks for the reply, Alex. I’ll try including the file.

Offline

#4 2006-05-03 07:44:20

Jaro
Member
From: S/F
Registered: 2004-11-18
Posts: 89

Re: Can't include php code into the form

Including file works perfect. Thanks again.

Offline

Board footer

Powered by FluxBB