summaryrefslogtreecommitdiff
path: root/_test/lib/rss_writer_class.php
blob: 684acfcfac7bcf2277d7b3c94baeeb616337e515 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
<?php
if(!defined("METAL_LIBRARY_XML_RSS_WRITER_CLASS"))
{
    define("METAL_LIBRARY_XML_RSS_WRITER_CLASS",1);

/*
 *
 * Copyright � (C) Manuel Lemos 2002
 *
 * @(#) $Id: rss_writer_class.php,v 1.5 2005/08/20 09:46:06 pachanga Exp $
 *
 */

class rss_writer_class extends xml_writer_class
{
    /*
     * Protected variables
     *
     */
    var $root="";
    var $channel="";
    var $image="";
    var $textinput="";
    var $items=0;
    var $itemsequence="";

    /*
     * Public variables
     *
     */
    var $specification="1.0";
    var $about="";
    var $rssnamespaces=array();
    var $allownoitems=0;
    var $generatedcomment="Generated by: http://www.phpclasses.org/rsswriter";


    /*
     * Protected functions
     *
     */
    Function addrssproperties(&$properties,$parent,&$required,&$optional,$scope)
    {
        $noattributes=array();
        $required_properties=0;
        Reset($properties);
        $end=(GetType($property=Key($properties))!="string");
        for(;!$end;)
        {
            if(IsSet($required[$property]))
            {
                if($required[$property])
                {
                    $this->error=("required ".$scope." property \"".$property."\" is already set");
                    return 0;
                }
                $required[$property]=1;
                $required_properties++;
            }
            else
            {
                if(IsSet($optional[$property]))
                {
                    if($optional[$property])
                    {
                        $this->error=("optional ".$scope." property \"".$property."\" is already set");
                        return 0;
                    }
                    $optional[$property]=1;
                }
                else
                {
                    if(GetType($colon=strpos($property,":",0))=="integer")
                    {
                        $namespace=substr($property,0,$colon);
                        if(!(!strcmp($namespace,"rdf") || IsSet($this->rssnamespaces[$namespace])))
                            $this->error=("the name space of property \"".$property."\" was not declared");
                    }
                    else
                        $this->error=("\"".$property."\" is not a supported ".$scope." property");
                }
            }
            if(!($this->adddatatag($property,$noattributes,$properties[$property],$parent,$path)))
                return 0;
            Next($properties);
            $end=(GetType($property=Key($properties))!="string");
        }
        if($required_properties<count($required))
        {
            Reset($required);
            $end=(GetType($property=Key($required))!="string");
            for(;!$end;)
            {
                if(!($required[$property]))
                {
                    $this->error=("it was not specified the required ".$scope." property \"".$property."\"");
                    return 0;
                }
                Next($required);
                $end=(GetType($property=Key($required))!="string");
            }
        }
        return 1;
    }

    /*
     * Public functions
     *
     */
    Function addchannel(&$properties)
    {
        if(strcmp($this->error,""))
            return 0;
        if(strcmp($this->channel,""))
        {
            $this->error="a channel was already added";
            return 0;
        }
        $channel_attributes=array();
        switch($this->specification)
        {
            case "0.9":
                $root="rdf:RDF";
                $attributes=array("xmlns:rdf"=>"http://www.w3.org/1999/02/22-rdf-syntax-ns#","xmlns"=>"http://my.netscape.com/rdf/simple/0.9/");
                $required=array("description"=>0,"link"=>0,"title"=>0);
                $optional=array();
                break;
            case "0.91":
                $root="rss";
                $attributes=array("version"=>$this->specification);
                $required=array("description"=>0,"language"=>0,"link"=>0,"title"=>0);
                $optional=array("copyright"=>0,"docs"=>0,"lastBuildDate"=>0,"managingEditor"=>0,"pubDate"=>0,"rating"=>0,"webMaster"=>0);
                break;
            case "1.0":
                if(!strcmp($this->about,""))
                {
                    $this->error="it was not specified the about URL attribute";
                    return 0;
                }
                $root="rdf:RDF";
                $attributes=array("xmlns:rdf"=>"http://www.w3.org/1999/02/22-rdf-syntax-ns#","xmlns"=>"http://purl.org/rss/1.0/");
                Reset($this->rssnamespaces);
                $end=(GetType($namespace=Key($this->rssnamespaces))!="string");
                for(;!$end;)
                {
                    if(!strcmp($namespace,"rdf"))
                    {
                        $this->error="the rdf namespace is being redeclared";
                        return 0;
                    }
                    $attributes[("xmlns:".$namespace)]=$this->rssnamespaces[$namespace];
                    Next($this->rssnamespaces);
                    $end=(GetType($namespace=Key($this->rssnamespaces))!="string");
                }
                $channel_attributes=array("rdf:about"=>$this->about);
                $required=array("description"=>0,"link"=>0,"title"=>0);
                $optional=array();
                break;
            default:
                $this->error="it was not specified a supported RSS specification version";
                return 0;
        }
        $this->addtag($root,$attributes,"",$path,1);
        $this->root=$path;
        if(!($this->addtag("channel",$channel_attributes,$this->root,$path,1)))
            return 0;
        if(!($this->addrssproperties($properties,$path,$required,$optional,"channel")))
            return 0;
        $this->channel=$path;
        return 1;
    }

    Function additem(&$properties)
    {
        if(strcmp($this->error,""))
            return 0;
        if(!strcmp($this->channel,""))
        {
            $this->error="the channel was not yet added";
            return 0;
        }
        if(strcmp($this->textinput,""))
        {
            $this->error="items can not be added to the channel after defining the textinput";
            return 0;
        }
        $attributes=array();
        switch($this->specification)
        {
            case "0.9":
                $parent=$this->root;
                break;
            case "0.91":
                $parent=$this->channel;
                break;
            case "1.0":
                if(IsSet($properties["link"]))
                    $attributes["rdf:about"]=$properties["link"];
                $parent=$this->root;
                break;
            default:
                $this->error="it was not specified a supported RSS specification version";
                return 0;
        }
        if(!($this->addtag("item",$attributes,$parent,$path,1)))
            return 0;
        $required=array("link"=>0,"title"=>0);
        $optional=array("description"=>0);
        if(!($this->addrssproperties($properties,$path,$required,$optional,"item")))
            return 0;
        if(!strcmp($this->specification,"1.0"))
        {
            if(!strcmp($this->itemsequence,""))
            {
                $attributes=array();
                if(!($this->addtag("items",$attributes,$this->channel,$path,1) && $this->addtag("rdf:Seq",$attributes,$path,$path,1)))
                    return 0;
                $this->itemsequence=$path;
            }
            $attributes=array("rdf:resource"=>$properties["link"]);
            if(!($this->addtag("rdf:li",$attributes,$this->itemsequence,$path,0)))
                return 0;
        }
        $this->items++;
        return 1;
    }

    Function addimage(&$properties)
    {
        if(strcmp($this->error,""))
            return 0;
        if(!strcmp($this->channel,""))
        {
            $this->error="the channel was not yet added";
            return 0;
        }
        if(strcmp($this->image,""))
        {
            $this->error="the channel image was already associated";
            return 0;
        }
        if($this->items!=0)
        {
            $this->error="the image can only be defined before adding the channel items";
            return 0;
        }
        $attributes=array();
        switch($this->specification)
        {
            case "0.9":
                $parent=$this->root;
                break;
            case "0.91":
                $parent=$this->channel;
                break;
            case "1.0":
                if(IsSet($properties["url"]))
                    $attributes["rdf:about"]=$properties["url"];
                $parent=$this->root;
                break;
            default:
                $this->error="it was not specified a supported RSS specification version";
                return 0;
        }
        if(!($this->addtag("image",$attributes,$parent,$path,1)))
            return 0;
        $this->image=$path;
        $required=array("link"=>0,"title"=>0,"url"=>0);
        $optional=array("description"=>0,"width"=>0,"height"=>0);
        if(!($this->addrssproperties($properties,$this->image,$required,$optional,"image")))
            return 0;
        if(!strcmp($this->specification,"1.0"))
        {
            $attributes=array("rdf:resource"=>$properties["url"]);
            return $this->addtag("image",$attributes,$this->channel,$path,0);
        }
        return 1;
    }

    Function addtextinput(&$properties)
    {
        if(strcmp($this->error,""))
            return 0;
        if(!strcmp($this->channel,""))
        {
            $this->error="the channel was not yet added";
            return 0;
        }
        if(strcmp($this->textinput,""))
        {
            $this->error="the channel text input was already associated";
            return 0;
        }
        if($this->items==0 && !$this->allownoitems)
        {
            $this->error="it were not specified any items before defining the channel text input";
            return 0;
        }
        $attributes=array();
        switch($this->specification)
        {
            case "0.9":
                $parent=$this->root;
                break;
            case "0.91":
                $parent=$this->channel;
                break;
            case "1.0":
                if(IsSet($properties["link"]))
                    $attributes["rdf:about"]=$properties["link"];
                $parent=$this->root;
                break;
            default:
                $this->error="it was not specified a supported RSS specification version";
                return 0;
        }
        if(!($this->addtag("textinput",$attributes,$parent,$path,1)))
            return 0;
        $this->textinput=$path;
        $required=array("description"=>0,"link"=>0,"name"=>0,"title"=>0);
        $optional=array();
        if(!($this->addrssproperties($properties,$this->textinput,$required,$optional,"textinput")))
            return 0;
        if(!strcmp($this->specification,"1.0"))
        {
            $attributes=array("rdf:resource"=>$properties["link"]);
            return $this->addtag("textinput",$attributes,$this->channel,$path,0);
        }
        return 1;
    }

    Function writerss(&$output)
    {
        if(strcmp($this->error,""))
            return 0;
        if(!strcmp($this->channel,""))
        {
            $this->error="it was not defined the RSS channel";
            return 0;
        }
        if($this->items==0 && !$this->allownoitems)
        {
            $this->error="it were not defined any RSS channel items";
            return 0;
        }
        switch($this->specification)
        {
            case "0.9":
                $this->dtdtype="PUBLIC";
                $this->dtddefinition="-//Netscape Communications//DTD RSS 0.9//EN";
                $this->dtdurl="http://my.netscape.com/publish/formats/rss-0.9.dtd";
                break;
            case "0.91":
                $this->dtdtype="PUBLIC";
                $this->dtddefinition="-//Netscape Communications//DTD RSS 0.91//EN";
                $this->dtdurl="http://my.netscape.com/publish/formats/rss-0.91.dtd";
                break;
            case "1.0":
                $this->dtdtype="";
                break;
            default:
                $this->error="it was not specified a supported RSS specification version";
                return 0;
        }
        return $this->write($output);
    }
};

}