source: trunk/include/functions_xml.inc.php @ 2297

Last change on this file since 2297 was 2297, checked in by plg, 16 years ago

Modification: new header on PHP files, PhpWebGallery renamed Piwigo.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 6.4 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008      Piwigo Team                  http://piwigo.org |
6// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
7// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
8// +-----------------------------------------------------------------------+
9// | This program is free software; you can redistribute it and/or modify  |
10// | it under the terms of the GNU General Public License as published by  |
11// | the Free Software Foundation                                          |
12// |                                                                       |
13// | This program is distributed in the hope that it will be useful, but   |
14// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
15// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
16// | General Public License for more details.                              |
17// |                                                                       |
18// | You should have received a copy of the GNU General Public License     |
19// | along with this program; if not, write to the Free Software           |
20// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
21// | USA.                                                                  |
22// +-----------------------------------------------------------------------+
23// +-----------------------------------------------------------------------+
24// | PhpWebGallery - a PHP based picture gallery                           |
25// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
26// | Copyright (C) 2003-2008 PhpWebGallery Team - http://phpwebgallery.net |
27// +-----------------------------------------------------------------------+
28// | branch        : BSF (Best So Far)
29// | file          : $Id: functions_xml.inc.php 2297 2008-04-04 22:57:23Z plg $
30// | last update   : $Date: 2008-04-04 22:57:23 +0000 (Fri, 04 Apr 2008) $
31// | last modifier : $Author: plg $
32// | revision      : $Revision: 2297 $
33// +-----------------------------------------------------------------------+
34// | This program is free software; you can redistribute it and/or modify  |
35// | it under the terms of the GNU General Public License as published by  |
36// | the Free Software Foundation                                          |
37// |                                                                       |
38// | This program is distributed in the hope that it will be useful, but   |
39// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
40// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
41// | General Public License for more details.                              |
42// |                                                                       |
43// | You should have received a copy of the GNU General Public License     |
44// | along with this program; if not, write to the Free Software           |
45// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
46// | USA.                                                                  |
47// +-----------------------------------------------------------------------+
48define( 'ATT_REG', '\w+' );
49define( 'VAL_REG', '[^"]*' );
50
51//------------------------------------------------------------------- functions
52// getContent returns the content of a tag
53//
54// example : getContent( "<name>Joe</name>" ) returns "Joe"
55//
56// It also works with strings containing themself sub-tags :
57// <perso><name>Jean</name><firstname>Billie</fisrtname></perso> ->
58// <name>Jean</name><firstname>Billie</firstname>
59function getContent( $element )
60{
61  // deleting start of the tag
62  $content = preg_replace( '/^<[^>]+>/', '', $element );
63  // deleting end of the tag
64  $content = preg_replace( '/<\/[^>]+>$/', '', $content );
65  // replacing multiple instance of space character
66  $content = preg_replace( '/\s+/', ' ', $content );
67
68  return $content;
69}
70
71// The function get Attribute returns the value corresponding to the
72// attribute $attribute for the tag $element.
73function getAttribute( $element, $attribute )
74{
75//  echo htmlentities($element).'<br /><br />';
76  $regex = '/^<\w+[^>]*\b'.$attribute.'\s*=\s*"('.VAL_REG.')"/i';
77  if ( preg_match( $regex, $element, $out ) ) 
78  {
79    return html_entity_decode($out[1], ENT_QUOTES);
80  }
81  else return '';
82}
83
84// The function encode Attribute returns the xml attribute $attribute="$value"
85function encodeAttribute( $attribute, $value )
86{
87  return $attribute.'="'.htmlspecialchars($value, ENT_QUOTES).'" ';
88}
89
90// The function getChild returns the first child
91// exemple : getChild( "<table><tr>XXX</tr><tr>YYY</tr></table>", "tr" )
92//           returns "<tr>XXX</tr>"
93function getChild( $document, $node )
94{
95  $regex = '/<'.$node.'(\s+'.ATT_REG.'="'.VAL_REG.'")*';
96  $regex.= '(\s*\/>|>.*<\/'.$node.'>)/U';
97
98  if
99    (
100      preg_match( $regex, $document, $out )
101      or
102      preg_last_error() == PREG_NO_ERROR
103    )
104  {
105    return $out[0];
106  }
107  else
108  {
109    die('getChild: error ['.preg_last_error().'] with preg_match function');
110  }
111}
112
113// getChildren returns a list of the children identified by the $node
114// example :
115//     getChild( "<table><tr>XXX</tr><tr>YYY</tr></table>", "tr" )
116//     returns an array with :
117//          $array[0] equals "<tr>XXX</tr>"
118//          $array[1] equals "<tr>YYY</tr>"
119function getChildren( $document, $node )
120{
121  $regex = '/<'.$node.'(\s+'.ATT_REG.'="'.VAL_REG.'")*';
122  $regex.= '(\s*\/>|>.*<\/'.$node.'>)/U';
123
124  if
125    (
126      preg_match_all( $regex, $document, $out )
127      or
128      preg_last_error() == PREG_NO_ERROR
129    )
130  {
131    return $out[0];
132  }
133  else
134  {
135    die('getChild: error ['.preg_last_error().'] with preg_match_all function');
136  }
137}
138
139// get_CodeXML places the content of a text file in a PHP variable and
140// return it. If the file can't be opened, returns false.
141function getXmlCode( $filename )
142{
143  if (function_exists('ini_set'))
144  {
145    // limit must be growed with php5 and "big" listing file
146    ini_set("pcre.backtrack_limit", pow(2, 32));
147  }
148
149  $file = fopen( $filename, 'r' );
150  if ( !$file )
151  {
152    return false;
153  }
154
155  $xml_content = '';
156  while ( !feof( $file ) )
157  {
158    $xml_content .= fgets( $file, 1024 );
159  }
160  fclose( $file );
161  $xml_content = str_replace( "\n", '', $xml_content );
162  $xml_content = str_replace( "\t", '', $xml_content );
163
164  return $xml_content;
165}
166?>
Note: See TracBrowser for help on using the repository browser.