1cf0cc4f57250d001aab7be9c83dc0d82043e4b3
[mw_sourceml] / app / data / modules / share / sml_data_sources_xml.php
1 <?php
2
3   class sml_data_sources_xml extends mw_data
4   {
5
6     function get_source_xml_from_url($url, $IGNORE_UPDATE = false)
7     { $env = $this->env();
8       if($env->config("cache_actif")) return $this->get_source_xml_from_cache($url, $IGNORE_UPDATE);
9       else return @file_get_contents($url);
10     }
11
12     # ----------------------------------------------------------------------------------------
13     #                                                                               XML source
14     #
15
16     function set_source_xml($id, $params = array())
17     { $env = $this->env();
18       $OK = false;
19       if(strlen($id) > 0 && ($fh = @fopen($this->source_xml_path($id), "wb")) !== false){
20         if(($content = $this->source_xml_content($id, $params)) !== false){
21           if(@fwrite($fh, $content)) $OK = true;
22         }
23         @fclose($fh);
24       }
25       return $OK;
26     }
27
28     function maj_source_xml_groupe($groupe)
29     { $OK = true;
30       if($groupe !== false)
31       { $sgbd = $this->sgbd();
32         $env = $this->env();
33         if(($sources = $this->sources(array("id_groupe" => $groupe["id"]))) !== false)
34         { foreach($sources["list"] as $id_source => $source)
35           { if(!$this->set_source_xml($source["id"], array("source" => $source)))
36             { $OK = false;
37               break;
38             }
39           }
40         }
41         else $OK = false;
42       }
43       else $OK = false;
44       return $OK;
45     }
46
47     function maj_source_xml_licence($licence)
48     { $OK = true;
49       if($licence !== false)
50       { $sgbd = $this->sgbd();
51         $env = $this->env();
52         if(($sources = $this->sources(array("id_licence" => $licence["id"]))) !== false)
53         { foreach($sources["list"] as $id_source => $source)
54           { if(!$this->set_source_xml($source["id"], array("source" => $source)))
55             { $OK = false;
56               break;
57             }
58           }
59         }
60         else $OK = false;
61       }
62       else $OK = false;
63       return $OK;
64     }
65
66     function maj_source_xml_all()
67     { $OK = true;
68       $sgbd = $this->sgbd();
69       $env = $this->env();
70       if(($sources = $this->sources(array())) !== false)
71       { foreach($sources["list"] as $id_source => $source)
72         { if(!$this->set_source_xml($source["id"], array("source" => $source)))
73           { $OK = false;
74             break;
75           }
76         }
77       }
78       else $OK = false;
79       return $OK;
80     }
81
82     function get_source_xml($id_source)
83     { $source_file = $this->source_xml_path($id_source);
84       if(file_exists($source_file))
85       { return @file_get_contents($source_file);
86       }
87       return "";
88     }
89
90     function del_source_xml($id_source)
91     { $env = $this->env();
92       if(file_exists($this->source_xml_path($id_source)))
93       { return
94            $this->del_edit_derivations(array("id_source" => $id_source))
95         && $this->del_edit_reference_content($id_source)
96         && @unlink($this->source_xml_path($id_source));
97       }
98       return true;
99     }
100
101     # ----------------------------------------------------------------------------------------
102     #                                                                   edition XML derivation
103     #
104
105     function set_edit_derivation_content($id_source, $id_source_derivation, $content)
106     { $env = $this->env();
107       $derivations_dir = $this->derivations_edit_xml_dir_path();
108       if(!file_exists($derivations_dir) || !is_dir($derivations_dir)) @mkdir($derivations_dir);
109       if(file_exists($derivations_dir) && is_dir($derivations_dir))
110       { $derivations_dir = $this->derivations_edit_xml_dir_path($id_source);
111         if(!file_exists($derivations_dir) || !is_dir($derivations_dir)) @mkdir($derivations_dir);
112         if(file_exists($derivations_dir) && is_dir($derivations_dir))
113         { if($fh = @fopen($this->derivation_edit_xml_path($id_source, $id_source_derivation), "w+"))
114           { if(@fwrite($fh, $content))
115             { @fclose($fh);
116               return true;
117             }
118             @fclose($fh);
119           }
120         }
121       }
122       return false;
123     }
124     
125     function get_edit_derivation_content($id_source, $id_source_derivation)
126     { $env = $this->env();
127       $derivation_file = $this->derivation_edit_xml_path($id_source, $id_source_derivation);
128       $derivation_content = "";
129       if(file_exists($derivation_file))
130       { $derivation_content = @file_get_contents($derivation_file);
131       }
132       return $derivation_content;
133     }
134
135     function del_edit_derivation_content($id_source, $id_source_derivation)
136     { $env = $this->env();
137       $derivation_file = $this->derivation_edit_xml_path($id_source, $id_source_derivation);
138       $OK = true;
139       if(file_exists($derivation_file))
140       { $OK = @unlink($derivation_file);
141       }
142       return $OK;
143     }
144
145     function del_edit_derivations($id_source)
146     { $env = $this->env();
147       $derivations_dir = $this->derivations_edit_xml_dir_path($id_source);
148       $OK = true;
149       if(is_dir($derivations_dir))
150       { if($dh = opendir($derivations_dir))
151         { while($OK && (($file = readdir($dh)) !== false))
152           { if(preg_match("/^.+\.xml$/", $file))
153             { $OK = @unlink($derivations_dir.$file) && $OK;
154             }
155           }
156           closedir($dh);
157 //          $OK = @rmdir($derivations_dir) && $OK;
158           @rmdir($derivations_dir);
159         }
160         else $OK = false;
161       }
162       return $OK;
163     }
164
165     # ----------------------------------------------------------------------------------------
166     #                                                                    edition XML reference
167     #
168
169     function set_edit_reference_content($id_source, $content)
170     { $env = $this->env();
171       $references_dir = $this->reference_edit_xml_dir_path();
172       if(!file_exists($references_dir) || !is_dir($references_dir)) @mkdir($references_dir);
173       if(file_exists($references_dir) && is_dir($references_dir))
174       { if($fh = @fopen($this->reference_edit_xml_path($id_source), "w+"))
175         { if(@fwrite($fh, $content))
176           { @fclose($fh);
177             return true;
178           }
179           @fclose($fh);
180         }
181       }
182       return false;
183     }
184
185     function get_edit_reference_content($id_source)
186     { $env = $this->env();
187       $reference_file = $this->reference_edit_xml_path($id_source);
188       $reference_content = "";
189       if(file_exists($reference_file))
190       { $reference_content = @file_get_contents($reference_file);
191       }
192       return $reference_content;
193     }
194
195     function del_edit_reference_content($id_source)
196     { $env = $this->env();
197       $reference_file = $this->reference_edit_xml_path($id_source);
198       $OK = true;
199       if(file_exists($reference_file))
200       { $OK = @unlink($reference_file);
201       }
202       return $OK;
203     }
204
205   }
206
207 ?>