Rewrite code using cUrl

apps

#1

I know very little about cUrl, and even after reading as much as I can find about it, for the life of me, I can not figure out how to rewrite this snippet of code to use cUrl instead of the fopen() statements. allow_url_fopen is disabled, and there is no option for reenabling it. Here is the section of code I need help with (it is released under the GNU/GPL, so it is open source):

//Check if folder is writable
function is__writable($path)
{

if ($path{strlen($path)-1}==’/’)

return expose_M::is__writable($path.uniqid(mt_rand()).’.tmp’);

else {

if (ereg(’.tmp’, $path))
{

if (!($f = @fopen($path, 'w ')))
return false;
fclose($f);
unlink($path);
return true;

}
else {
if (!($f = @fopen($path, ‘a’)))
return false;
fclose($f);
return true;
}//We have a path error.
}

}

Brian D. Saylor
Captain / Webmaster
Mountville Fire Company No. 1


#2

I’ve looked through the code you have provided and, while there are uses of “fopen” visible, the supporting code that defines the variables in use is not provided, so I can’t really see what the code is trying to do.

This makes it “difficult” to know how to proceed to “cURLify” it. Since you mentioned that the code is released under the GNU/GPL license, can you point us to the original source code for the application from which this snippet was taken?

From looking at the mountvillefire.com website, I see that you are using Joomla!, and am wondering if the code you are having difficulty with is a Joomla! extension (component). If so, and that code is GNU/GPL, it is quite likely that if we can’t sort it out, there may be those on the extremely active Joomla! forums that can help.

It is also possible that a different component, or “wrapping” another application within Joomla!, may afford you the same functionality without code that relies upon allow_url_fopen being enabled. :wink:

–rlparker


#3

You are correct, I am utilizing Joomla. The component that I am trying to use is the Expose 4 gallery component. Some of the functionality is not available, due to fopen being disable. I thought that rewriting the sections of code using cUrl instead of fopen could get the functionality, without fopen. The full code is below, I shall thank you in advance for looking at it to try to render some assistance, I have also posted my query on a few active Joomla boards as well.

[quote]defined(’_VALID_MOS’) or die(‘Direct access to this location is not allowed.’);

class expose_M {

// Get the path for our xml files
function rpath() {
$xmlF = realpath2 ("…/components/com_expose/expose/config")."/";
return $xmlF;
}

function rpath2() {
$xmlF = realpath2 (“components/com_expose/expose/config”)."/";
return $xmlF;
}

// Save the new config values from the form
function SaveConf() {
// Loop the config form and call our setConf function
$xmlF = expose_M::rpath();
foreach ($POST as $key => $val) {
if (!isset(${$key})) ${$key} = $val;
$fix_form = str_replace("
"," ", $key);
$setcon = expose_M::setConf($xmlF, $fix_form, $val);
}

return $setcon;

}

// Save the new formats values from the form
function SaveFormats() {
$xmlF = expose_M::rpath();
foreach ($POST as $key => $val) {
if (!isset(${$key})) ${$key} = $val;
$ids = explode(’-’,$key);
$fix_form = str_replace("
"," ", $ids[0]);
$setcon = expose_M::setFonts($xmlF, $ids[1], $val, $fix_form);
}
return $setcon;
}

// Function to save the changes made in the config form
function setFonts($path, $attr, $attrv, $pname) {
$dom = domxml_open_file($path . “formats.xml”);
$root = $dom->document_element();
$nodes = $root->child_nodes ();
for ($i = 0; $i < count ($nodes); $i++) {
$node = $nodes[$i];
if ($node->node_type () == XML_ELEMENT_NODE) {
if ($node->node_name () == “format”) {
if ($node->get_attribute (“id”) == $attr) {
changeNodeProperty ($node, $pname, $attrv);
}
}
}
}
$dom->dump_file ($path . “formats.xml”, false, true);
return “Saved”;

}

// Save config
function setConf($path, $attr, $attrv) {

$dom = domxml_open_file($path . “config.xml”);

$root = $dom->document_element();
$nodes = $root->child_nodes ();
for ($i = 0; $i < count ($nodes); $i++) {
$node = $nodes[$i];
if ($node->node_type () == XML_ELEMENT_NODE) {
if ($node->node_name () == “param”) {
if ($node->get_attribute (“name”) == $attr) {
$node->set_attribute (“value”, $attrv);
}
}
}
}
$dom->dump_file ($path . “config.xml”, false, true);
return “Saved”;

}

// Get the values from param in config.xml
function GetAttr($path,$attr) {
$settings = array ();
$dom = domxml_open_file($path . “config.xml”);
$root = $dom->document_element();
$nodes = $root->child_nodes ();
for ($i = 0; $i < count ($nodes); $i++) {
$node = $nodes[$i];
if ($node->node_type () == XML_ELEMENT_NODE) {
if ($node->node_name () == “param”) {
if($node->get_attribute (“name”) == $attr) {
return $node->get_attribute (“value”);
}
}
}
}
}

//Check if folder is writable
function is__writable($path)
{

if ($path{strlen($path)-1}==’/’)

return expose_M::is__writable($path.uniqid(mt_rand()).’.tmp’);

else {

if (ereg(’.tmp’, $path))
{

if (!($f = @fopen($path, ‘w+’)))
return false;
fclose($f);
unlink($path);
return true;

}
else {
if (!($f = @fopen($path, ‘a’)))
return false;
fclose($f);
return true;
}//We have a path error.
}

}

// Check if the system has the SYSTEM REQUIREMENTS
function syscheck() {

if (version_compare (‘5.0.0’, phpversion(), ‘<=’) == 1 || function_exists (domxml_open_file)) {
$domerror = 0;
} else {
$domerror = 1;
$domresponse = ‘DOMXML extension unavailable
’;
}

if (function_exists (‘imagecreatefromjpeg’)) {
$gderror = 0;
} else {
$gdresponse = ‘GD extension unavailable
’;
$gderror = 1;
}

if (expose_M::is__writable(dirname(FILE).’/’.’…/img/’)) {
$imgpatherror = 0;
} else {
$imgresponse = ‘expose/img/ folder is NOT writable. please set the correct permissions.
’;
$imgpatherror = 1;
}

if (expose_M::is__writable(dirname(FILE).’/’.’…/config/’)) {
$confpatherror = 0;
} else {
$confresponse = ‘expose/config/ folder is NOT writable. please set the correct permissions.
’;
$confpatherror = 1;
}

if (expose_M::is__writable(dirname(FILE).’/’.’…/xml/’)) {
$xmlpatherror = 0;
} else {
$xmlpatherror = 1;
$xmlresponse = ‘expose/xml/ folder is NOT writable. please set the correct permissions.
’;
}

if (expose_M::is__writable(dirname(FILE).’/’.’…/manager/amfphp/extra/passhash.inc.php’)) {
$passerror = 0;
} else {
$passerror = 1;
$passresponse = ‘Password file (expose/manager/amfphp/extra/passhash.inc.php) is NOT writable. please set the correct permissions.
’;
}

if ($domerror == 1 || $gderror == 1 || $imgpatherror == 1 || $xmlpatherror == 1 || $passerror == 1 || $confpatherror == 1) {

$errorMsg = "Your system dose not meet the SYSTEM REQUIREMENTS to run the Expose config section.</b />

".$domresponse.$gdresponse.$imgresponse.$xmlresponse.$passresponse.$confresponse;
return $errorMsg;
}else{
return “”;
}

}

} // End Class
?>
[/quote]
Brian D. Saylor
Captain / Webmaster
Mountville Fire Company No. 1


#4

If I am understanding this correctly, the $path variable is holding the “realpath” (which is supposed to be an absolute, or “relative from root” path). That means that fopen() should work, because you are not trying to access a URL (remote or otherwise). What is the actual error you are receiving?

si-blog
Max discount on any plan with promocode SCJESSEYTOTAL


#5

From doing a little research, and from looking at that code, I think the problem with the Expose component may be unrelated to the allow_url_fopen issue, but rather a matter of which the DOMXML library is used.

Are you using PHP5? Have you considered “testing” by switching your domain to use PHP4 for a short test?

I’d try that and see if it has any effect on the component’s operation; it’s easy enough to do and easy to change it back if it doesn’t help. Your main site should operate just fine using either PHP4 or PHP5. :wink:

–rlparker