noalyss Version-9
package_repository.class.php
Go to the documentation of this file.
1<?php
2
3/*
4 * This file is part of NOALYSS.
5 *
6 * PhpCompta is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * NOALYSS is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with PhpCompta; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20// Copyright (2018) Author Dany De Bontridder <dany@alchimerys.be>
21
22/**
23 * @file
24 * @brief contains the class Package_Repository
25 */
26
27/**
28 * @brief connect to NOALYSS_PACKAGE and fetch the file web.xml , it displays
29 * content of this file , build the appropriate object for installing
30 */
32{
33
34 protected $content;
35 static $time_cache_second = 3600; // cache time in second
36
37 /**
38 * @see package_repository.test.php
39 */
40 function __construct()
41 {
42 // Check we can resolve the name
43 $host=parse_url(NOALYSS_PACKAGE_REPOSITORY,PHP_URL_HOST);
44 $file=$_ENV['TMP']."/web.xml";
45
46 if ( file_exists($file) ) {
47 $date_time=new \DateTime();
48 $file_tmstamp=filemtime($file);
49
50 $delta=$date_time->getTimestamp() - $file_tmstamp;
51
52 // if file too old , refresh it
53 if ( $delta > self::$time_cache_second ) {
54 $web_repo = file_get_contents(NOALYSS_PACKAGE_REPOSITORY."/web.xml");
55 $f_file= fopen($file,"w+");
56 fwrite($f_file,$web_repo);
57 fclose($f_file);
58 $this->setContent($web_repo);
59 } else {
60 $this->setContent(file_get_contents($file));
61 }
62 } elseif( gethostbyname($host) != $host) {
63 $content=file_get_contents(NOALYSS_PACKAGE_REPOSITORY."/web.xml");
64 $this->content=simplexml_load_string($content);
65 $file=$_ENV['TMP']."/web.xml";
66 $f_file= fopen($file,"w+");
67 fwrite($f_file,$this->content->saveXML());
68 fclose($f_file);
69 } else {
70 $this->content=NULL;
71 }
72
73
74
75 }
76 /**
77 * @return int
78 */
79 public static function getTimeCacheSecond(): int
80 {
82 }
83
84 /**
85 * @param int $time_cache_second
86 */
87 public static function setTimeCacheSecond(int $time_cache_second):void
88 {
89 self::$time_cache_second = $time_cache_second;
90
91 }
92
93
94 public function getContent()
95 {
96 return $this->content;
97 }
98
99 /**
100 * @brief set content
101 * @param $content xml file to be parsed
102 * @return Package_Repository
103 */
104 public function setContent($content)
105 {
106 $this->content=simplexml_load_string($content);
107 if ( $this->content==false ) throw new Exception ("XML INVALIDE [$content]",EXC_INVALID);
108 return $this;
109 }
110 /**
111 * @brief check that NOALYSS_HOME exists and is writable
112 */
113 function can_download()
114 {
115 $download_dir=NOALYSS_HOME."/tmp";
116 if (is_dir($download_dir)&&is_writable($download_dir))
117 {
118 return TRUE;
119 }
120 return FALSE;
121 }
122
123 /**
124 * Get info for Noalyss code : version #, announce, path to the last
125 * version
126 */
128 {
129 global $g_user;
130 if ( $this->content == NULL ) {
131 throw new Exception(_("Problème réseau"),10);
132 }
133 switch ($g_user->lang)
134 {
135 case 'fr_FR.utf8':
136 echo "<pre>";
137 echo $this->content->core->description;
138 echo "</pre>";
139
140 break;
141 case 'en_US.utf8':
142 break;
143 case 'nl_NL.utf8':
144 break;
145 }
146 }
147
148 /**
149 * return a SimpleXMLElement of the plugin thanks its code, it returns NULL if no plugin is found
150 * @param string $p_code code of the plugin
151 * @return SimpleXMLElement or NULL if not found
152 */
153 function find_plugin($p_code)
154 {
155 if ( $this->content == NULL ) {
156 throw new Exception(_("Problème réseau"),10);
157 }
158 $a_plugin=$this->content->xpath('//plugins/plugin');
159 $nb_plugin=count($a_plugin);
160 for ($i=0; $i<$nb_plugin; $i++)
161 {
162 if (trim($a_plugin[$i]->code)==$p_code)
163 {
164 return $a_plugin[$i];
165 }
166 }
167 return NULL;
168 }
169
170
171 /**
172 * return a SimpleXMLElement of the db template thanks its code, it returns NULL if no template is found
173 * @param string $p_code code of the template
174 * @return SimpleXMLElement or NULL if not found
175 */
176 function find_template($p_code)
177 {
178 if ( $this->content == NULL ) {
179 throw new Exception(_("Problème réseau"),10);
180 }
181 $a_template=$this->content->xpath('//database_template/dbtemplate');
183 for ($i=0; $i<$nb_template; $i++)
184 {
185 if (trim($a_template[$i]->code)==$p_code)
186 {
187 return $a_template[$i];
188 }
189 }
190 return NULL;
191 }
192
194 {
195 if ( $this->content == NULL ) {
196 throw new Exception(_("Problème réseau"),10);
197 }
198 switch ($p_type)
199 {
200 case "core":
201 // Create an object to download & install the core
202 $obj=new Package_Core("Noalyss", "Core", $this->content->core->path);
203 return $obj;
204 break;
205 case 'template':
206 // create an object to download & install the template
207 $db=$this->find_template($p_id);
208 if ($db == NULL ) {
209 throw new Exception(_("Modèle non trouvé"),1002);
210 }
211 $obj=new Package_Template($db->name,$db->description,$db->path);
212 return $obj;
213 case 'plugin':
214 // create an object to download & install a plugin
215 $plugin = $this->find_plugin($p_id);
216 if ($plugin==NULL)
217 {
218 throw new Exception(_("Extension non trouvée"), 1001);
219 }
220 $obj=new Package_Plugin($plugin->name,$plugin->description,$plugin->path);
221 return $obj;
222 break;
223 case 'contrib':
224 //create an object to download & install a contrib
225 default:
226 break;
227 }
228 }
229 /**
230 * Read xml file from the package
231 * @param string $p_file
232 * @return SimpleXMLElement
233 */
234 public function read_package_xml($p_file)
235 {
236 $dom=new DomDocument('1.0');
237 $dom->load($p_file);
238 $xml=simplexml_import_dom($dom);
239 return $xml;
240 }
241
242}
global $g_user
if no group available , then stop
switch($op2) $xml
Definition: ajax_card.php:806
else $card content[$j]['j_montant']
$a_plugin
for($e=0;$e< $nb_dirscan;$e++) $nb_plugin
Class Package Core to install the core , if possible.
Manage the installation of plug.
connect to NOALYSS_PACKAGE and fetch the file web.xml , it displays content of this file ,...
static setTimeCacheSecond(int $time_cache_second)
find_template($p_code)
return a SimpleXMLElement of the db template thanks its code, it returns NULL if no template is found
can_download()
check that NOALYSS_HOME exists and is writable
setContent($content)
set content
display_noalyss_info()
Get info for Noalyss code : version #, announce, path to the last version.
read_package_xml($p_file)
Read xml file from the package.
find_plugin($p_code)
return a SimpleXMLElement of the plugin thanks its code, it returns NULL if no plugin is found
Show , download and install template database for accountancy from the Package repository.
const EXC_INVALID
Definition: constant.php:346
if( $delta< 0) elseif( $delta==0)
if( $xml==NULL) $a_template
$nb_template
$dom
Definition: xml.php:15