2 /**********************************************************************
3 Copyright (C) FrontAccounting, LLC.
4 Released under the terms of the GNU General Public License, GPL,
5 as published by the Free Software Foundation, either version 3
6 of the License, or (at your option) any later version.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 include_once($path_to_root. "/includes/archive.inc");
13 include_once($path_to_root. "/includes/remote_url.inc");
14 include_once($path_to_root. "/includes/hooks.inc");
16 define('PKG_CACHE_PATH', $path_to_root.'/modules/_cache');
17 define('PUBKEY_PATH', $path_to_root);
19 // FrontAccounting package class
21 class package extends gzip_file {
22 function package($filename, $basedir=null)
27 $basedir = PKG_CACHE_PATH.'/'.substr(basename($filename), 0, -4);
28 if (file_exists($basedir)) {
29 // flush_dir($basedir, true);
33 $this->archive($filename);
34 $this->set_options(array('basedir'=> $basedir));
35 $this->options['type'] = "pkg";
38 // Used by archive class. Use create_archive() instead.
42 return $this->create_gzip();
45 // Install package and clean temp directory.
53 $this->set_options(array('overwrite' => 1));
54 $this->extract_files(); // extract package in cache directory
55 $cachepath = $this->options['basedir'];
56 $ctrl = get_control_file("$cachepath/_init/config");
58 $targetdir = $path_to_root.'/'.$ctrl['InstallPath'];
60 if (!is_dir($targetdir))
63 $dpackage = new package("$cachepath/_data", $targetdir);
64 $dpackage->set_options(array('overwrite' => 1));
66 $flist = $dpackage->extract_files(true);
67 if (count($dpackage->error)) {
68 $this->error = array_merge($this->error, $dpackage->error);
71 copy_files($flist, $targetdir, "$cachepath/_back");
73 $dpackage->extract_files(); //install package in target directory
75 $install = hook_invoke($ctrl['Package'], 'install_extension', $dummy);
76 $success &= $install===null || $install;
77 $success &= count($dpackage->error) == 0;
78 $this->error = array_merge($this->error, $dpackage->error);
82 // Removing package related sources
90 $cachepath = $this->options['basedir'];
91 $ctrl = get_control_file("$cachepath/_init/config");
93 $targetdir = $path_to_root.'/'.$ctrl['InstallPath'];
95 $dpackage = new package("$cachepath/_data", $targetdir);
97 $flist = $dpackage->extract_files(true);
99 $success &= copy_files($flist, "$cachepath/_back", $targetdir, true);
101 if (strpos($ctrl['InstallPath'], 'modules/') === 0) { // flush module directory
102 flush_dir($targetdir, true);
106 $uninstall = hook_invoke($ctrl['Package'], 'uninstall_extension', $dummy);
107 $success &= $uninstall===null || $uninstall;
112 // Purge all package related configuration and data.
121 // Changes field value read from control file (single, or multiline) into
122 // arrays of subfields if needed.
124 function ufmt_property($key, $value)
126 // indexes used in output arrays
128 // 'MenuTabs' => array('url', 'access', 'tab_id', 'title', 'section'),
129 // 'MenuEntries' => array('url', 'access', 'tab_id', 'title'),
131 if (!isset($sub_fields[$key]))
132 return $value==='' ? null : $value;
136 if (!is_array($value))
137 $value = array($value);
138 foreach($value as $line) {
139 $indexes = $sub_fields[$key];
141 preg_match_all('/(["])(?:\\\\?+.)*?\1|[^"\s][\S]*/', $line, $match);
142 foreach($match[0] as $n => $subf) {
144 $val = strtr(substr($subf, 1, -1),
149 $ret[array_shift($indexes)] = $val;
158 //=============================================================================
160 // Retrieve control file and return as associative array
161 // $index is name of field used as key in result array, or null for numeric keys
163 function get_control_file($file, $index = false) {
165 $list = gzopen($file, 'rb');
166 if (!$list) return null;
168 $repo = $pkg = array();
169 $key = false; $value = '';
172 $line = rtrim($line);
173 if ($line && ctype_space($line[0])) { // continuation of multiline property
174 if (strlen(ltrim($line))) {
175 if ($value !== '' && !is_array($value))
176 $value = array($value);
177 $value[] = ltrim($line);
181 if ($key) { // save previous property if any
182 $pkg[$key] = ufmt_property($key, $value);
184 if (!strlen($line)) { // end of section
186 if ($index !== true) {
187 if ($index === false) break;
188 if (!isset($pkg[$index])) {
189 display_error(sprintf(_("No key field '%s' in file '%s'"), $index, $file));
192 $repo[$pkg[$index]] = $pkg;
197 $key = null; $value = '';
199 } elseif (preg_match('/([^:]*):\s*(.*)/', $line, $m)) {
200 $key = $m[1]; $value = $m[2];
202 display_error("Empty key in line $line");
206 display_error("File parse error in line $line");
210 } while ((($line = fgets($list))!==false) || $key);
213 return $index === false ? $pkg : $repo;
216 // Save configuration data to control file.
218 function save_control_file($fname, $list, $zip=false)
220 $file = $zip ? gzopen($fname, 'wb') : fopen($fname, 'wb');
221 foreach($list as $section) {
222 foreach($section as $key => $value) {
223 if (is_array($value)) { // multiline value
224 if (is_array(reset($value))) { // lines have subfields
225 foreach($value as $i => $line) {
226 // Subfields containing white spaces or double quotes are doublequoted
227 // with " escaped with backslash.
228 foreach($line as $n => $subfield)
229 if (preg_match('/[\s"]/', $subfield)) {
231 '"'.strtr($subfield, array('"'=>'\\"')).'"';
233 // Subfields are separated by white space.
234 $value[$i] = implode(' ', $value[$i]);
237 // array elements on subsequent lines starting with white space
238 $value = implode("\n ", $value);
240 $zip ? gzwrite($file, "$key: $value\n") : fwrite($file, "$key: $value\n");
242 $zip ? gzwrite($file, "\n"): fwrite($file, "\n");
244 $zip ? gzclose($file) : fclose($file);
247 // Retrieve text field in localized version or default one
248 // when the localized is not avaialable.
250 function pkg_prop($pkg, $property, $lang=false)
253 if ($lang && isset($pkg[$property.'-'.user_language()]))
254 $prop = @$pkg[$pname];
256 $prop = @$pkg[$property];
258 return is_array($prop) ? implode("\n ",$prop): $prop;
261 // Retrieve list of packages from repository and return as table ($pkgname==null),
262 // or find $pkgname package in repository and optionaly download
264 // $type is type/s of package
265 // $filter is optional field selection array in form field=>newkey
266 // or (0=>field1, 1=>field2...)
267 // $outkey - field used as a key in package list. If null 'Package' field is used.
269 function get_pkg_or_list($type = null, $pkgname = null, $filter=array(), $outkey=null, $download=true) {
271 global $path_to_root, $repo_auth;
273 $repo = (isset($repo_auth['scheme']) ? $repo_auth['scheme'] : 'http://')
274 .(isset($repo_auth['login']) ? $repo_auth['login'].':' : '')
275 .(isset($repo_auth['pass']) ? $repo_auth['pass'].'@' : '')
276 .(isset($repo_auth['host']) ? $repo_auth['host'].'/' : '')
277 .(isset($repo_auth['path']) ? $repo_auth['path'].'/' : '')
278 .$repo_auth['branch'];
280 // first download local copy of repo release file
281 // and check remote signature with local copy of public key
283 $loclist = PKG_CACHE_PATH.'/Release.gz';
284 $target_dir = $download==true ? "$path_to_root/tmp/" : $download;
286 if (isset($type) && !is_array($type)) {
287 $type = array($type);
291 if (!file_exists($loclist)) {
292 if (!url_copy($repo.'/Release.gz', $loclist))
294 display_error(_("Cannot download repo index file." ));
299 $sig = url_get_contents($repo.'/Release.sig');
300 $data = file_get_contents($loclist);
301 $cert = file_get_contents(PUBKEY_PATH.'/FA.pem');
302 if (!function_exists('openssl_verify')) {
303 display_error(_("OpenSSL have to be available on your server to use extension repository system."));
306 if (openssl_verify($data, $sig, $cert) <= 0) {
308 if (!@unlink($loclist))
310 display_error(sprintf(_("Cannot delete outdated '%s' file."), $loclist));
314 display_error(_('Release file in repository is invalid, or public key is outdated.'));
322 $Release = get_control_file($loclist, 'Filename');
323 // download and check all indexes containing given package types
324 // then complete package list or seek for pkg
326 foreach($Release as $fname => $parms) {
327 if ($type && !count(array_intersect(explode(' ', $parms['Type']), $type))) {
328 unset($Release[$fname]); continue; // no packages of selected type in this index
330 if ($Release[$fname]['Version'] != $repo_auth['branch']) {
331 display_warning(_('Repository version does not match application version.')); // ?
333 $remoteindex = $repo.'/'.$fname;
334 $locindex = PKG_CACHE_PATH.'/'.$fname;
337 if (!file_exists($locindex)) {
338 if (!url_copy($remoteindex, $locindex)) {
339 display_error(sprintf(_("Cannot download '%s' file." ), $fname));
344 if ($parms['SHA1sum'] != sha1_file($locindex)) { // check subdir index consistency
346 if (!@unlink($locindex)) {
347 display_error(sprintf(_("Cannot delete outdated '%s' file."), $locindex));
351 display_error(sprintf( _("Security alert: broken index file in repository '%s'. Please inform repository administrator about this issue."),
359 // scan subdir list and select packages of given type
360 $pkglist = get_control_file($locindex, 'Package');
361 foreach($pkglist as $name => $pkg) {
362 $pkgfullname = $repo.'/'.$parms['Path']."/".$pkg['Filename'].'.pkg';
363 if (!isset($type) || in_array($pkg['Type'], $type)) {
367 foreach($filter as $field => $key) {
368 if (is_numeric($field))
369 $p[$field] = @$pkg[$field];
371 $p[$key] = @$pkg[$field];
374 if ($pkgname == null) {
375 $Packages[$outkey ? $outkey : $name] = $p;
376 } elseif ($pkgname == $pkg['Package']) {
377 //download package to temp directory
379 $locname = $target_dir.$pkg['Filename'].'.pkg';
380 if (!url_copy($pkgfullname, $locname)) {
381 display_error(sprintf(_("Cannot download '%s' file." ), $pkgfullname));
384 // checking sha1 hash is expensive proces, so chekc the package
385 // consistency just before downloading
386 if ($pkg['SHA1sum'] != sha1_file($locname)) {
387 display_error(sprintf( _("Security alert: broken package '%s' in repository. Please inform repository administrator about this issue."),
401 function get_package($pkgname, $type = null)
403 return get_pkg_or_list($type, $pkgname);
406 Returns full name of installed package, or null if package is not installed.
408 function installed_package($package)
410 $cache = opendir(PKG_CACHE_PATH);
412 while ($file = @readdir($cache)) {
413 if (!is_dir(PKG_CACHE_PATH.'/'.$file))
415 if (strpos($file, $package.'-') === 0)
423 Remove package from system
425 function uninstall_package($name)
427 $name = installed_package($name);
428 if (!$name) return true; // not installed
429 $pkg = new package($name.'.pkg');
432 flush_dir(PKG_CACHE_PATH.'/'.$name, true);
433 rmdir(PKG_CACHE_PATH.'/'.$name);
435 return count($pkg->error)==0;
438 //---------------------------------------------------------------------------------------
440 // Return merged list of available and installed languages in inform of local
441 // configuration array supplemented with installed versions information.
443 function get_languages_list()
445 global $installed_languages;
447 $pkgs = get_pkg_or_list('language', null, array(
448 'Package' => 'package',
449 'Version' => 'available',
451 'Language' => 'code',
452 'Encoding' => 'encoding',
454 'Description' => 'Descr',
455 'InstallPath' => 'path'
458 // add/update languages already installed
460 foreach($installed_languages as $id => $l) {
461 $list = array_search_keys($l['code'], $pkgs, 'code'); // get all packages with this code
462 foreach ($list as $name) {
463 if ($l['encoding'] == $pkgs[$name]['encoding']) { // if the same encoding
464 $pkgs[$name]['version'] = @$l['version']; // set installed version
465 $pkgs[$name]['local_id'] = $id; // index in installed_languages
469 $l['local_id'] = $id;
470 if (!isset($l['package']) || $l['package'] == '' || !isset($pkgs[$l['package']]))
473 $pkgs[$l['package']] = array_merge($pkgs[$l['package']], $l);
479 //---------------------------------------------------------------------------------------
481 // Return merged list of available and installed extensions as a local
482 // configuration array supplemented with installed versions information.
484 function get_extensions_list($type = null)
486 global $path_to_root;
488 if (isset($type) || !is_array($type)) {
489 $type = array($type);
492 $pkgs = get_pkg_or_list($type, null, array(
493 'Package' => 'package',
494 'Version' => 'available',
496 'Description' => 'Descr',
498 'DefaultStatus' => 'active',
499 // 'MenuTabs' => 'tabs',
500 // 'MenuEntries' => 'entries',
501 'Encoding' => 'encoding',
502 // 'AccessExtensions' => 'acc_file',
503 'InstallPath' => 'path'
506 // lookup for local extensions
507 $path = $path_to_root.'/modules/';
509 $moddir = opendir($path);
511 while(false != ($fname = readdir($moddir)))
513 if(!in_array($fname, array('.','..','CVS','_cache')) && is_dir($path.$fname))
515 if (!isset($pkgs[$fname]))
516 $pkgs[$fname] = array(
521 'type' => 'extension',
522 'path' => 'modules/'.$fname,
528 // add/update extensions already installed
530 $installed = get_company_extensions();
531 foreach($installed as $extno => $ext) {
532 if (!in_array($ext['type'], $type)) continue;
533 $ext['local_id'] = $extno;
534 // if (!isset($pkgs[$ext['package']]) || $ext['package'] == '')
537 $pkgs[$ext['package']] = array_merge($pkgs[$ext['package']], $ext);
544 // Return merged list of available and installed extensions as a local
545 // configuration array supplemented with installed versions information.
547 function get_themes_list()
549 $pkgs = get_pkg_or_list('theme', null, array(
550 'Package' => 'package',
551 'Version' => 'available',
553 'Description' => 'Descr'
556 // add/update extensions already installed
558 $local = get_company_extensions();
560 foreach($local as $extno => $ext) {
561 if (isset($pkgs[@$ext['package']])) {
562 $ext['local_id'] = $extno;
563 $pkgs[$ext['package']] = array_merge($pkgs[$ext['package']], $ext);
566 // TODO: Add other themes from themes directory
571 //---------------------------------------------------------------------------------------
573 // Return merged list of available and installed COAs as a local
574 // configuration array supplemented with installed versions information.
576 function get_charts_list()
578 $pkgs = get_pkg_or_list('chart', null, array(
579 'Package' => 'package',
580 'Version' => 'available',
582 'Description' => 'Descr',
584 'InstallPath' => 'path',
585 'Encoding' => 'encoding',
589 // add/update default charts
591 $local = get_company_extensions();
593 foreach($local as $extno => $ext) {
594 if ($ext['type'] != 'chart') continue;
595 $ext['local_id'] = $extno;
596 if (!isset($pkgs[$ext['package']]) || $ext['package'] == '')
599 $pkgs[$ext['package']] = array_merge($pkgs[$ext['package']], $ext);
605 //---------------------------------------------------------------------------------------------
606 // Install/update package from repository
608 function install_language($pkg_name)
610 global $path_to_root, $installed_languages, $Ajax;
612 $pkg = get_pkg_or_list('language', $pkg_name);
615 $i = array_search_key($pkg['Language'], $installed_languages, 'code');
617 $i = count($installed_languages);
618 else { // remove another already installed package for this language
619 $old_pkg = @$installed_languages[$i]['package'];
620 if ($old_pkg && ($pkg['Package'] != $old_pkg))
621 uninstall_package($old_pkg);
624 $package = new package("$path_to_root/tmp/".$pkg['Filename'].'.pkg');
625 if ($package->install()) {
627 'name' => $pkg['Name'],
628 'package' => $pkg['Package'],
629 'code' => $pkg['Language'],
630 'encoding' => $pkg['Encoding'],
631 'version' => $pkg['Version'],
632 'path' => $pkg['InstallPath']
634 if ($pkg['RTLDir']=='yes')
636 $installed_languages[$i] = $lang;
637 write_lang($installed_languages);
638 unlink("$path_to_root/tmp/".$pkg['Filename'].'.pkg');
639 $Ajax->activate('lang_tbl');
641 display_error(implode('<br>', $package->error));
645 display_error(sprintf(_("Package '%s' not found."), $pkg_name));
650 //---------------------------------------------------------------------------------------------
651 // Install/update extension or theme package from repository
653 function install_extension($pkg_name)
655 global $path_to_root, $installed_extensions, $next_extension_id, $Ajax, $db_connections;
657 $pkg = get_pkg_or_list(array('extension', 'theme', 'chart'), $pkg_name);
659 $package = new package("$path_to_root/tmp/".$pkg['Filename'].'.pkg');
660 $local_exts = get_company_extensions();
661 if ($package->install()) {
662 $ext_id = array_search_key($pkg['Package'], $local_exts, 'package');
663 if ($ext_id === null)
664 $ext_id = $next_extension_id++;
665 else { // remove another already installed package for this language
666 $old_pkg = $installed_extensions[$ext_id]['package'];
668 uninstall_package($old_pkg);
671 'name' => $pkg['Name'],
672 'package' => $pkg['Package'],
673 'version' => $pkg['Version'],
674 'type' => $pkg['Type'],
675 'active' => @$pkg['DefaultStatus'] == 'active' ? true : false,
676 'path' => $pkg['InstallPath'],
678 if (isset($pkg['SqlScript']))
679 $ext['sql'] = $pkg['SqlScript'];
681 $local_exts[$ext_id] = $ext;
682 $ret = update_extensions($local_exts);
684 if (($ext['active'] == true) && file_exists($path_to_root.'/'.$ext['path'].'/hooks.php'))
686 // we need to include the new hooks file to activate extension
687 include_once($path_to_root.'/'.$ext['path'].'/hooks.php');
688 foreach($db_connections as $comp => $db)
689 activate_hooks($ext['package'], $comp);
692 unlink("$path_to_root/tmp/".$pkg['Filename'].'.pkg');
693 $Ajax->activate('ext_tbl');
696 display_error(implode('<br>', $package->error));
700 display_error(sprintf(_("Package '%s' not found."), $pkg_name));
706 Returns true if newer package version is available
708 function check_pkg_upgrade($current, $available)
710 preg_match_all('/[\d]+/', $available, $aver);
711 if (!count($aver[0]))
713 preg_match_all('/[\d]+/', $current, $cver);
714 if (!count($cver[0]))
716 foreach($aver[0] as $n => $ver)
717 if ($ver>@$cver[0][$n])
723 // Returns package info from index file
725 function get_package_info($pkg, $type=null, $filter=array(), $outkey=null, $download=true) {
726 return get_pkg_or_list($type, $pkg, $filter, null, false);
730 Check basic extension source compatibility.
732 function check_src_ext_version($ext_v)
736 $compat_levels = 2; // current policy is keeping compatibility on major version level.
737 $app = explode('.', substr($src_version, 0, strspn($src_version, "0123456789.")));
738 $pkg = explode('.', substr($ext_v, 0, strspn($ext_v, "0123456789.")));
740 for ($i=0; $i < min($compat_levels, count($app)); $i++)
741 if ($pkg[$i] < $app[$i])