Hi all,
I’m not really into PHP but I’ve been working on a flash app that reads an XML thats created by an PHP aplication.
Here is the thing, when I press return on my keyboard the XML files that is generated by the PHP comes with an extra space between the lines just like a paragraph, anyway I need to take the paragraph of here is some parts of the code…
I would really apreciate any help.
here is my php files
xml.php
<?
function xmlInit($file = '', $rootName = 'root', $catRoot = '', $gallDirName = '')
{
if (version_compare( phpversion(), '5', '<' )){
require_once('xml4.php');
return new XML4($file, $rootName, $catRoot, $gallDirName);
}else{
require_once('xml5.php');
return new XML5($file, $rootName, $catRoot, $gallDirName);
}
}
?>
xml4.php
<?
class XML4
{
var $file = '';
var $doc = null;
var $root = null;
var $rootName = 'root';
var $catRoot = '';
var $gallDirName = '';
function XML4($iFile = '', $rootName = 'root', $catRoot = '', $gallDirName = '')
{
$this->rootName = $rootName;
$this->catRoot = $catRoot;
$this->gallDirName = $gallDirName;
$this->file = $iFile;
if (file_exists($iFile)) {
$this->doc = domxml_open_file($iFile);
$this->root = $this->doc->document_element();
}else{
$this->doc = domxml_new_doc('1.0');
$root = $this->doc->create_element($rootName);
$this->doc->append_child($root);
$this->root = $root;
$el = $this->doc->create_element('id');
$el->set_attribute('type', 'gall');
$el->set_attribute('value', '0');
$root->append_child($el);
$this->save();
}
}
function getCatId()
{
$el = $this->doc->get_elements_by_tagname('id');
if (count($el) > 0) {
foreach ($el as $v) {
if ($v->get_attribute('type') == 'gall'){
$id = $v->get_attribute('value');
$id++;
$v->set_attribute('value', $id);
$this->save();
return $id;
}
}
return 1;
}
}
function addCat($cat = '')
{
if ($cat == '')
return false;
$id = $this->getCatId();
$imgId = $this->doc->create_element('id');
$imgId->set_attribute('type', 'img');
$imgId->set_attribute('value', '0');
$el = $this->doc->create_element('gallery');
$el->set_attribute('id', $id);
$el->set_attribute('name', $cat);
@mkdir($this->catRoot.$this->gallDirName.'/'.$id.'/');
$el->set_attribute('path', $this->gallDirName.'/'.$id.'/');
$el->append_child($imgId);
$this->root->append_child($el);
$this->save();
return $id;
}
function updateCat($id, $name)
{
$cat = $this->getCat($id);
$cat->set_attribute('name', $name);
$this->save();
/*$cat = $this->getCat($id);
$cat->setAttribute('name', $name);
$this->save();*/
}
function delCat($id = '')
{
if ($id == '')
return false;
$list = $this->root->get_elements_by_tagname('gallery');
if (count($list) > 0) {
foreach ($list as $v) {
if ($v->get_attribute('id') == $id) {
//delete all files from dir
$path = $this->catRoot.$v->get_attribute('path');
if ($dh = @opendir($path)) {
while (($file = readdir($dh)) !== false) {
@unlink($path.$file);
}
closedir($dh);
}
//delete dir
@rmdir($path);
//delete cat
$this->root->remove_child($v);
$this->save();
}
}
}
}
function getCategories()
{
$list = $this->root->get_elements_by_tagname('gallery');
$res = array();
if (count($list) > 0) {
foreach ($list as $v) {
$res[] = array(
'id' => $v->get_attribute('id'),
'name' => $v->get_attribute('name'),
'path' => $v->get_attribute('path')
);
}
}
return $res;
}
function getCat($id)
{
if ($id == '')
return array();
$list = $this->root->get_elements_by_tagname('gallery');
if (count($list) > 0) {
foreach ($list as $v) {
if ($v->get_attribute('id') == $id){
return $v;
}
}
}
return null;
}
function getCatValue($id)
{
if ($id == '')
return array();
$list = $this->root->get_elements_by_tagname('gallery');
if (count($list) > 0) {
foreach ($list as $v) {
if ($v->get_attribute('id') == $id){
return array(
'id' => $v->get_attribute('id'),
'name' => $v->get_attribute('name'),
'path' => $v->get_attribute('path')
);
}
}
}
return array();
}
function addImage($data)
{
$cat = $this->getCat($data['cat']);
$images = $cat->get_elements_by_tagname('image');
$el = $this->doc->create_element('image');
$el->set_attribute('id', $data['id']);
$el->set_attribute('thumbnail', $data['thumbnail']);
$el->set_attribute('pic', $data['pic']);
$el->set_attribute('name', $data['name']);
$el->set_attribute('description', $data['desc']);
$el->set_attribute('def', ((count($images) == 0)?'1':'0'));
$cat->append_child($el);
$this->save();
return true;
}
function updateImage($cat, $id, $data)
{
if ($img = $this->getImage($cat, $id)){
$img->set_attribute('name', $data[0]);
$img->set_attribute('description', $data[1]);
$this->save();
return true;
}else{
return false;
}
}
function getImageId($cat)
{
if ($cat = $this->getCat($cat)){
$el = $cat->get_elements_by_tagname('id');
if (count($el) > 0) {
foreach ($el as $v) {
if ($v->get_attribute('type') == 'img'){
$id = $v->get_attribute('value');
$id++;
$v->set_attribute('value', $id);
$this->save();
return $id;
}
}
return 1;
}
}else{
return null;
}
}
function getImage($cat, $id)
{
if ($cat == '' || $id == '')
return null;
$cat = $this->getCat($cat);
$images = $cat->get_elements_by_tagname('image');
if (count($images) > 0) {
foreach ($images as $v) {
if ($v->get_attribute('id') == $id){
return $v;
}
}
}
return null;
}
function getImageValue($cat, $id)
{
if ($cat == '' || $id == '')
return array();
$cat = $this->getCat($cat);
$path = $cat->get_attribute('id');
$images = $cat->get_elements_by_tagname('image');
if (count($images) > 0) {
foreach ($images as $v) {
if ($v->get_attribute('id') == $id){
return array(
'id' => $v->get_attribute('id'),
'thumb' => $path.'/'.$v->get_attribute('thumbnail'),
'pic' => $path.'/'.$v->get_attribute('pic'),
'name' => $v->get_attribute('name'),
'desc' => $v->get_attribute('description')
);
}
}
}
return array();
}
function getImages($catId)
{
$cat = $this->getCat($catId);
$res = array();
$images = $cat->get_elements_by_tagname('image');
if (count($images) > 0) {
foreach ($images as $v) {
$id = $v->get_attribute('id');
$res[] = array(
'id' => $id,
'thumbnail' => $v->get_attribute('thumbnail'),
'pic' => $v->get_attribute('pic'),
'name' => $v->get_attribute('name'),
'description' => $v->get_attribute('description'),
'default' => $v->get_attribute('def')
);
}
}
return $res;
}
function setDefault($cat, $id)
{
if ($cat = $this->getCat($cat)){
$images = $cat->get_elements_by_tagname('image');
if (count($images) > 0) {
foreach ($images as $v) {
if ($v->get_attribute('id') == $id)
$v->set_attribute('def', 1);
else
$v->set_attribute('def', 0);
}
}
$this->save();
}else{
return false;
}
}
function deleteImage($cat, $img)
{
$cat = $this->getCat($cat);
$images = $cat->get_elements_by_tagname('image');
if (count($images) > 0) {
foreach ($images as $v) {
if ($v->get_attribute('id') == $img){
//delete images
$path = $this->catRoot.$cat->get_attribute('path');
@unlink($path.$v->get_attribute('thumbnail'));
@unlink($path.$v->get_attribute('pic'));
//
$cat->remove_child($v);
$this->save();
break;
}
}
}
}
function save($file = '')
{
if ($file == '')
$this->doc->dump_file($this->file);
else
$this->doc->dump_file($file);
}
function getXML()
{
return $this->doc->dump_mem();
}
}
?>
xml5.php
<?
class XML5
{
private $file = '';
private $doc = null;
private $root = null;
private $rootName = 'root';
private $catRoot = '';
private $gallDirName = '';
function XML5($iFile = '', $rootName = 'root', $catRoot = '', $gallDirName = '')
{
$this->rootName = $rootName;
$this->catRoot = $catRoot;
$this->gallDirName = $gallDirName;
$this->file = $iFile;
$this->doc = new DOMDocument('1.0', 'utf-8');
$file = (file_exists($iFile))?file_get_contents($this->file):'';
if ($file != '')
{
$this->doc->loadXML($file);
$this->root = $this->doc->documentElement;
}else{
$root = $this->doc->createElement($rootName);
$this->doc->appendChild($root);
$this->root = $root;
$el = $this->doc->createElement('id');
$el->setAttribute('type', 'gall');
$el->setAttribute('value', '0');
$root->appendChild($el);
$this->save();
}
}
function getCatId()
{
$el = $this->doc->getElementsByTagName('id');
if ($el->length > 0) {
for ($i = 0; $i < $el->length; $i++){
if ($el->item($i)->getAttribute('type') == 'gall'){
$id = $el->item($i)->getAttribute('value');
$id++;
$el->item($i)->setAttribute('value', $id);
$this->save();
return $id;
}
}
return 1;
}
}
function addCat($cat = '')
{
if ($cat == '')
return false;
$id = $this->getCatId();
$imgId = $this->doc->createElement('id');
$imgId->setAttribute('type', 'img');
$imgId->setAttribute('value', '0');
$el = $this->doc->createElement('gallery');
$el->setAttribute('id', $id);
$el->setAttribute('name', $cat);
@mkdir($this->catRoot.$this->gallDirName.'/'.$id.'/');
$el->setAttribute('path', $this->gallDirName.'/'.$id.'/');
$el->appendChild($imgId);
$this->root->appendChild($el);
$this->save();
return $id;
}
function updateCat($id, $name)
{
$cat = $this->getCat($id);
$cat->setAttribute('name', $name);
$this->save();
}
function delCat($id = '')
{
if ($id == '')
return false;
$list = $this->root->getElementsByTagName('gallery');
if ($list->length > 0) {
for ($i = 0; $i < $list->length; $i++){
if ($list->item($i)->getAttribute('id') == $id) {
//delete all files from dir
$path = $this->catRoot.$list->item($i)->getAttribute('path');
if ($dh = @opendir($path)) {
while (($file = readdir($dh)) !== false) {
@unlink($path.$file);
}
closedir($dh);
}
//delete dir
@rmdir($path);
//delete cat
$this->root->removeChild($list->item($i));
$this->save();
}
}
}
}
function getCategories()
{
$list = $this->root->getElementsByTagName('gallery');
$res = array();
if ($list->length > 0) {
for ($i = 0; $i < $list->length; $i++){
$res[] = array(
'id' => $list->item($i)->getAttribute('id'),
'name' => $list->item($i)->getAttribute('name'),
'path' => $list->item($i)->getAttribute('path')
);
}
}
return $res;
}
function getCat($id)
{
if ($id == '')
return array();
$list = $this->root->getElementsByTagName('gallery');
if ($list->length > 0) {
for ($i = 0; $i < $list->length; $i++){
if ($list->item($i)->getAttribute('id') == $id){
return $list->item($i);
}
}
}
return null;
}
function getCatValue($id)
{
if ($id == '')
return array();
$list = $this->root->getElementsByTagName('gallery');
if ($list->length > 0) {
for ($i = 0; $i < $list->length; $i++){
if ($list->item($i)->getAttribute('id') == $id){
return array(
'id' => $list->item($i)->getAttribute('id'),
'name' => $list->item($i)->getAttribute('name'),
'path' => $list->item($i)->getAttribute('path')
);
}
}
}
return array();
}
function addImage($data)
{
$cat = $this->getCat($data['cat']);
$images = $cat->getElementsByTagName('image');
$el = $this->doc->createElement('image');
$el->setAttribute('id', $data['id']);
$el->setAttribute('thumbnail', $data['thumbnail']);
$el->setAttribute('pic', $data['pic']);
$el->setAttribute('name', $data['name']);
$el->setAttribute('description', $data['desc']);
$el->setAttribute('def', (($images->length == 0)?'1':'0'));
$cat->appendChild($el);
$this->save();
return true;
}
function updateImage($cat, $id, $data)
{
if ($img = $this->getImage($cat, $id)){
$img->setAttribute('name', $data[0]);
$img->setAttribute('description', $data[1]);
$this->save();
return true;
}else{
return false;
}
}
function getImageId($cat)
{
if ($cat = $this->getCat($cat)){
$el = $cat->getElementsByTagName('id');
if ($el->length > 0) {
for ($i = 0; $i < $el->length; $i++){
if ($el->item($i)->getAttribute('type') == 'img'){
$id = $el->item($i)->getAttribute('value');
$id++;
$el->item($i)->setAttribute('value', $id);
$this->save();
return $id;
}
}
return 1;
}
}else{
return null;
}
}
function getImage($cat, $id)
{
if ($cat == '' || $id == '')
return null;
$cat = $this->getCat($cat);
$images = $cat->getElementsByTagName('image');
if ($images->length > 0) {
for ($i = 0; $i < $images->length; $i++){
if ($images->item($i)->getAttribute('id') == $id){
return $images->item($i);
}
}
}
return null;
}
function getImageValue($cat, $id)
{
if ($cat == '' || $id == '')
return array();
$cat = $this->getCat($cat);
$path = $cat->getAttribute('id');
$images = $cat->getElementsByTagName('image');
if ($images->length > 0) {
for ($i = 0; $i < $images->length; $i++){
if ($images->item($i)->getAttribute('id') == $id){
return array(
'id' => $images->item($i)->getAttribute('id'),
'thumb' => $path.'/'.$images->item($i)->getAttribute('thumbnail'),
'pic' => $path.'/'.$images->item($i)->getAttribute('pic'),
'name' => $images->item($i)->getAttribute('name'),
'desc' => $images->item($i)->getAttribute('description')
);
}
}
}
return array();
}
function getImages($catId)
{
$cat = $this->getCat($catId);
$res = array();
$images = $cat->getElementsByTagName('image');
if ($images->length > 0) {
for ($i = 0; $i < $images->length; $i++){
$id = $images->item($i)->getAttribute('id');
$res[] = array(
'id' => $id,
'thumbnail' => $images->item($i)->getAttribute('thumbnail'),
'pic' => $images->item($i)->getAttribute('pic'),
'name' => $images->item($i)->getAttribute('name'),
'description' => $images->item($i)->getAttribute('description'),
'default' => $images->item($i)->getAttribute('def')
);
}
}
return $res;
}
function setDefault($cat, $id)
{
if ($cat = $this->getCat($cat)){
$images = $cat->getElementsByTagName('image');
if ($images->length > 0) {
for ($i = 0; $i < $images->length; $i++){
if ($images->item($i)->getAttribute('id') == $id)
$images->item($i)->setAttribute('def', 1);
else
$images->item($i)->setAttribute('def', 0);
}
}
$this->save();
}else{
return false;
}
}
function deleteImage($cat, $img)
{
$cat = $this->getCat($cat);
$images = $cat->getElementsByTagName('image');
if ($images->length > 0) {
for ($i = 0; $i < $images->length; $i++){
if ($images->item($i)->getAttribute('id') == $img){
//delete images
$path = $this->catRoot.$cat->getAttribute('path');
@unlink($path.$images->item($i)->getAttribute('thumbnail'));
@unlink($path.$images->item($i)->getAttribute('pic'));
//
$cat->removeChild($images->item($i));
$this->save();
break;
}
}
}
}
function save($file = '')
{
if ($file == '')
$this->doc->save($this->file);
else
$this->doc->save($file);
}
function getXML()
{
return $this->doc->saveXML();
}
}
?>
XML file comes with this every time i hit return
(one)3;(1)0; //extra line
Thanks for the atention!!!