Fixed problem in package repository communication.
[fa-stable.git] / includes / packages.inc
1 <?php
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");
15
16 define('PKG_CACHE_PATH', $path_to_root.'/modules/_cache');
17 define('PUBKEY_PATH', $path_to_root);
18 define('REPO_URL', 'http://'.$repo_auth['login'].':'.$repo_auth['pass'].'@'.$repo_auth['host'].'/'.$repo_auth['branch']);
19 //
20 // FrontAccounting package class
21 //
22 class package extends gzip_file {
23         function package($filename, $basedir=null)
24         {
25                 global $path_to_root;
26
27                 if (!$basedir) {
28                         $basedir = PKG_CACHE_PATH.'/'.substr(basename($filename), 0, -4);
29                         if (file_exists($basedir)) {
30 //                              flush_dir($basedir, true); 
31                         } else
32                         mkdir($basedir);
33                 }
34                 $this->archive($filename);
35                 $this->set_options(array('basedir'=> $basedir));
36                 $this->options['type'] = "pkg";
37         }
38         //
39         //      Used by archive class. Use create_archive() instead.
40         //      
41         function create_pkg() 
42         {
43                 return $this->create_gzip();
44         }
45         //
46         //      Install package and clean temp directory.
47         //
48         function install()
49         {
50                 global $path_to_root;
51                 
52                 $success = true;
53
54                 $this->set_options(array('overwrite' => 1));
55                 $this->extract_files(); // extract package in cache directory
56                 $cachepath = $this->options['basedir'];
57                 $ctrl = get_control_file("$cachepath/_init/config");
58
59                 $targetdir = $path_to_root.'/'.$ctrl['InstallPath'];
60
61                 if (!is_dir($targetdir))
62                         mkdir($targetdir);
63
64                 $dpackage = new package("$cachepath/_data", $targetdir);
65                 $dpackage->set_options(array('overwrite' => 1));
66
67                 $flist = $dpackage->extract_files(true);
68                 if (count($dpackage->error)) {
69                         $this->error = array_merge($this->error, $dpackage->error);
70                         return false;
71                 }
72                 copy_files($flist, $targetdir, "$cachepath/_back");
73         
74                 $dpackage->extract_files(); //install package in target directory
75
76                 $install = hook_invoke($ctrl['Package'], 'install_extension', $dummy);
77                 $success &= $install===null || $install;
78                 $success &= count($dpackage->error) == 0;
79                 $this->error = array_merge($this->error, $dpackage->error);
80                 return $success;
81         }
82         //
83         //      Removing package related sources
84         //
85         function uninstall()
86         {
87                 global $path_to_root;
88
89                 $success = true;
90
91                 $cachepath = $this->options['basedir'];
92                 $ctrl = get_control_file("$cachepath/_init/config");
93
94                 $targetdir = $path_to_root.'/'.$ctrl['InstallPath'];
95
96                 $dpackage = new package("$cachepath/_data", $targetdir);
97
98                 $flist = $dpackage->extract_files(true);
99
100                 $success &= copy_files($flist, "$cachepath/_back", $targetdir, true);
101
102                 if (strpos($ctrl['InstallPath'], 'modules/') === 0) { // flush module directory
103                         flush_dir($targetdir, true);
104                         rmdir($targetdir);
105                 }
106
107                 $uninstall = hook_invoke($ctrl['Package'], 'uninstall_extension', $dummy);
108                 $success &= $uninstall===null || $uninstall;
109
110                 return $success;
111         }
112         //
113         //      Purge all package related configuration and data.
114         //
115         function purge()
116         {
117                 return true;
118         }
119
120 }
121 //
122 // Changes field value read from control file (single, or multiline) into 
123 // arrays of subfields if needed.
124 //
125 function ufmt_property($key, $value)
126 {
127         // indexes used in output arrays
128         $sub_fields = array(
129 //              'MenuTabs' => array('url', 'access', 'tab_id', 'title', 'section'),
130 //              'MenuEntries' => array('url', 'access', 'tab_id', 'title'),
131         );
132         if (!isset($sub_fields[$key]))
133                 return $value==='' ? null : $value;
134
135         $prop = array();
136
137         if (!is_array($value))
138                 $value = array($value);
139         foreach($value as $line) {
140                 $indexes = $sub_fields[$key];
141                 $ret = array();
142                 preg_match_all('/(["])(?:\\\\?+.)*?\1|[^"\s][\S]*/', $line, $match);
143                 foreach($match[0] as $n => $subf) {
144                         if ($match[1][$n])
145                                 $val = strtr(substr($subf, 1, -1),
146                                         array('\\"'=>'"'));
147                 else
148                                 $val = $subf;
149                         if (count($indexes))
150                                 $ret[array_shift($indexes)] = $val;
151                         else
152                                 $ret[] = $val;
153                 }
154                 if (count($ret))
155                         $prop[] = $ret;
156         }
157         return $prop;
158 }
159 //=============================================================================
160 //
161 // Retrieve control file and return as associative array
162 //      $index is name of field used as key in result array, or null for numeric keys
163 //
164 function get_control_file($file, $index = false) {
165
166         $list = gzopen($file, 'rb');
167         if (!$list) return null;
168
169         $repo = $pkg = array();
170         $key = false; $value = '';
171         $line = '';
172         do {
173                 $line = rtrim($line);
174                 if ($line && ctype_space($line[0])) { // continuation of multiline property
175                         if (strlen(ltrim($line))) {
176                                 if ($value !== '' && !is_array($value))
177                                         $value = array($value);
178                                 $value[] = ltrim($line);
179                                 continue;
180                         }
181                 }
182                 if ($key) { // save previous property if any
183                         $pkg[$key] = ufmt_property($key, $value);
184                 }
185                 if (!strlen($line)) { // end of section
186                         if (count($pkg)) {
187                                 if ($index !== true) {
188                                         if ($index === false) break;
189                                         if (!isset($pkg[$index])) {
190                                                 display_error(sprintf(_("No key field '%s' in file '%s'"), $index, $file));
191                                                 return null;
192                                         }
193                                         $repo[$pkg[$index]] = $pkg;
194                                 } else
195                                         $repo[] = $pkg;
196                         }
197                         $pkg = array(); 
198                         $key = null; $value = '';
199                         continue;
200                 } elseif (preg_match('/([^:]*):\s*(.*)/', $line, $m)) {
201                         $key = $m[1]; $value = $m[2];
202                         if (!strlen($key)) {
203                                 display_error("Empty key in line $line");
204                                 return null;
205                         }
206                 } else {
207                         display_error("File parse error in line $line");
208                         return null;
209                 }
210                 
211         } while ((($line = fgets($list))!==false) || $key);
212         fclose($list);
213
214         return $index === false ? $pkg : $repo;
215 }
216 //
217 //      Save configuration data to control file.
218 //
219 function save_control_file($fname, $list, $zip=false) 
220 {
221         $file = $zip ?  gzopen($fname, 'wb') : fopen($fname, 'wb');
222         foreach($list as $section) {
223                 foreach($section as $key => $value) {
224                         if (is_array($value)) { // multiline value
225                                 if (is_array(reset($value))) { // lines have subfields
226                                         foreach($value as $i => $line) {
227                 // Subfields containing white spaces or double quotes are doublequoted 
228                 // with " escaped with backslash.
229                                                 foreach($line as $n => $subfield)
230                                                         if (preg_match('/[\s"]/', $subfield)) {
231                                                                 $value[$i][$n] = 
232                                                                         '"'.strtr($subfield, array('"'=>'\\"')).'"';
233                                                         }
234                                                 // Subfields are separated by white space.
235                                                 $value[$i] = implode(' ', $value[$i]);
236                                         }
237                                 }
238                                 // array elements on subsequent lines starting with white space
239                                 $value = implode("\n ", $value);
240                         }
241                         $zip ? gzwrite($file, "$key: $value\n") : fwrite($file, "$key: $value\n");
242                 }
243                 $zip ? gzwrite($file, "\n"): fwrite($file, "\n");
244         }
245         $zip ? gzclose($file) : fclose($file);
246 }
247 //
248 //      Retrieve text field in localized version or default one 
249 //      when the localized is not avaialable.
250 //
251 function pkg_prop($pkg, $property, $lang=false) 
252 {
253         
254         if ($lang && isset($pkg[$property.'-'.user_language()]))
255                 $prop = @$pkg[$pname];
256         else
257                 $prop = @$pkg[$property];
258
259         return is_array($prop) ? implode("\n ",$prop): $prop;
260 }
261 //
262 //      Retrieve list of packages from repository and return as table ($pkgname==null),
263 //      or find $pkgname package in repository and optionaly download
264 //
265 //      $type is type/s of package
266 //  $filter is optional field selection array in form field=>newkey
267 //              or (0=>field1, 1=>field2...)
268 //  $outkey - field used as a key in package list. If null 'Package' field is used.
269 //
270 function get_pkg_or_list($type = null, $pkgname = null, $filter=array(), $outkey=null, $download=true) {
271
272         global $path_to_root, $repo_auth;
273
274         // first download local copy of repo release file
275         // and check remote signature with local copy of public key
276         //
277         $loclist = PKG_CACHE_PATH.'/Release.gz';
278         
279         if (isset($type) && !is_array($type)) {
280                 $type = array($type);
281         }
282         $refresh = true;
283         do{
284                 if (!file_exists($loclist)) {
285                         url_copy(REPO_URL.'/Release.gz', $loclist);
286                         $refresh = false;
287                 }
288                 $sig = url_get_contents(REPO_URL.'/Release.sig');
289                 $data = file_get_contents($loclist);
290                 $cert = file_get_contents(PUBKEY_PATH.'/FA.pem');
291                 if (!openssl_verify($data, $sig, $cert)) {
292                         if ($refresh) {
293                                 if (!@unlink($loclist))
294                                 {
295                                         display_error(sprintf(_("Cannot delete outdated '%s' file."), $loclist));
296                                         return null;
297                                 }
298                         } else {
299                                 display_error(_('Release file in repository is invalid, or public key is outdated.'));
300                                 return null;
301                         }
302                 } else
303                         $refresh = false;
304
305         } while($refresh);
306
307         $Release = get_control_file($loclist, 'Filename');
308         // download and check all indexes containing given package types
309         // then complete package list or seek for pkg
310         $Packages = array();
311         foreach($Release as $fname => $parms) {
312                 if ($type && !count(array_intersect(explode(' ', $parms['Type']), $type))) {
313                         unset($Release[$fname]); continue; // no packages of selected type in this index
314                 }
315                 if ($Release[$fname]['Version'] != $repo_auth['branch']) {
316                         display_warning(_('Repository version does not match application version.')); // ?
317                 }
318                 $remoteindex = REPO_URL.'/'.$fname;
319                 $locindex = PKG_CACHE_PATH.'/'.$fname;
320                 $refresh = true;
321                 do{
322                         if (!file_exists($locindex)) { 
323                                 url_copy($remoteindex, $locindex);
324                                 $refresh = false;
325                         }
326                         if ($parms['SHA1sum'] != sha1_file($locindex)) {        // check subdir index consistency
327                                 if ($refresh) {
328                                         if (!@unlink($locindex)) {
329                                                 display_error(sprintf(_("Cannot delete outdated '%s' file."), $locindex));
330                                                 return null;
331                                         }
332                                 } else {
333                                         display_error(sprintf( _("Security alert: broken index file in repository '%s'. Please inform repository administrator about this issue."),
334                                                 $fname));
335                                         return null;
336                                 }
337                         } else
338                                 $refresh = false;
339                 } while($refresh);
340                 
341                  // scan subdir list and select packages of given type
342                 $pkglist = get_control_file($locindex, 'Package');
343                 foreach($pkglist as $name => $pkg) {
344                         $pkgfullname = REPO_URL.'/'.$parms['Path']."/".$pkg['Filename'].'.pkg';
345                         if (!isset($type) || in_array($pkg['Type'], $type)) {
346                                 if (empty($filter))
347                                         $p = $pkg;
348                                 else {
349                                         foreach($filter as $field => $key) {
350                                                 if (is_numeric($field))
351                                                         $p[$field] = @$pkg[$field];
352                                                 else
353                                                         $p[$key] = @$pkg[$field];
354                                         }
355                                 }
356                                 if ($pkgname == null) {
357                                         $Packages[$outkey ? $outkey : $name] = $p;
358                                 } elseif ($pkgname == $pkg['Package']) {
359                                         //download package to temp directory
360                                         if ($download) {
361                                                 $locname = "$path_to_root/tmp/".$pkg['Filename'].'.pkg';
362                                                 url_copy($pkgfullname, $locname);
363                                                  // checking sha1 hash is expensive proces, so chekc the package
364                                                  // consistency just before downloading
365                                                 if ($pkg['SHA1sum'] != sha1_file($locname)) {
366                                                         display_error(sprintf( _("Security alert: broken package '%s' in repository. Please inform repository administrator about this issue."),
367                                                                 $pkgfullname));
368                                                         return null;
369                                                 }
370                                         }
371                                         return $p;
372                                 }
373                         }
374                 }
375         }
376
377         return $Packages;
378 }
379
380 function get_package($pkgname, $type = null)
381 {
382         return get_pkg_or_list($type, $pkgname);
383 }
384 /*
385         Returns full name of installed package, or null if package is not installed.
386 */
387 function installed_package($package)
388 {
389         $cache = opendir(PKG_CACHE_PATH);
390
391         while ($file = @readdir($cache)) {
392                 if (!is_dir(PKG_CACHE_PATH.'/'.$file))
393                         continue;
394                 if (strpos($file, $package.'-') === 0)
395                         return $file;
396         }
397         @closedir($cache);
398
399         return null;
400 }
401 /*
402         Remove package from system
403 */
404 function uninstall_package($name)
405 {
406         $name = installed_package($name);
407         if (!$name) return true; // not installed
408         $pkg = new package($name.'.pkg');
409         $pkg->uninstall();
410         if($name) {
411                 flush_dir(PKG_CACHE_PATH.'/'.$name, true);
412                 rmdir(PKG_CACHE_PATH.'/'.$name);
413         }
414         return count($pkg->error)==0;
415 }
416
417 //---------------------------------------------------------------------------------------
418 //
419 //      Return merged list of available and installed languages in inform of local 
420 // configuration array supplemented with installed versions information.
421 //
422 function get_languages_list()
423 {
424         global $installed_languages;
425         
426         $pkgs = get_pkg_or_list('language', null, array(
427                                 'Package' => 'package',
428                                 'Version' => 'available',
429                                 'Name' => 'name',
430                                 'Language' => 'code',
431                                 'Encoding' => 'encoding',
432                                 'RTLDir' => 'rtl',
433                                 'Description' => 'Descr',
434                                 'InstallPath' => 'path'
435                         ));
436
437         // add/update languages already installed
438         // 
439         foreach($installed_languages as $id => $l) {
440                 $list = array_search_keys($l['code'], $pkgs, 'code');   // get all packages with this code
441                 foreach ($list as $name) {
442                         if ($l['encoding'] == $pkgs[$name]['encoding']) {       // if the same encoding
443                                 $pkgs[$name]['version'] = @$l['version'];               // set installed version
444                                 $pkgs[$name]['local_id'] = $id;         // index in installed_languages
445                                 continue 2;
446                         }
447                 }
448                 $l['local_id'] = $id;
449                 if (!isset($l['package']) || $l['package'] == '' || !isset($pkgs[$l['package']]))
450                         $pkgs[] = $l;
451                 else
452                         $pkgs[$l['package']] = array_merge($pkgs[$l['package']], $l);
453         }
454         ksort($pkgs);
455         return $pkgs;
456 }
457 //---------------------------------------------------------------------------------------
458 //
459 //      Return merged list of available and installed extensions as a local 
460 // configuration array supplemented with installed versions information.
461 //
462 function get_extensions_list($type = null)
463 {
464         global $path_to_root;
465
466         if (isset($type) || !is_array($type)) {
467                 $type = array($type);
468         }
469
470         $pkgs = get_pkg_or_list($type, null, array(
471                                 'Package' => 'package',
472                                 'Version' => 'available',
473                                 'Name' => 'name',
474                                 'Description' => 'Descr',
475                                 'Type' => 'type',
476                                 'DefaultStatus'=> 'active',
477 //                              'MenuTabs' => 'tabs',
478 //                              'MenuEntries' => 'entries',
479                                 'Encoding' => 'encoding',
480 //                              'AccessExtensions' => 'acc_file',
481                                 'InstallPath' => 'path'
482                         ));
483
484         // lookup for local extensions
485         $path = $path_to_root.'/modules/';
486         $loc = array();
487         $moddir = opendir($path);
488
489         while(false != ($fname = readdir($moddir)))
490         {
491                 if(!in_array($fname, array('.','..','CVS','_cache')) && is_dir($path.$fname))
492                 {
493                         if (!isset($pkgs[$fname]))
494                                 $pkgs[$fname] = array(
495                                         'package' => $fname,
496                                         'name' => $fname,
497                                         'version' => '',
498                                         'available' => '',
499                                         'type' => 'extension',
500                                         'path' => 'modules/'.$fname,
501                                         'active' => false
502                                         );
503                 }
504         }
505
506         // add/update extensions already installed
507         // 
508         $installed = get_company_extensions();
509         foreach($installed as $extno => $ext) {
510                 if (!in_array($ext['type'], $type)) continue;
511                 $ext['local_id'] = $extno;
512 //              if (!isset($pkgs[$ext['package']]) || $ext['package'] == '')
513 //                      $pkgs[] = $ext;
514 //              else
515                         $pkgs[$ext['package']] = array_merge($pkgs[$ext['package']], $ext);
516         }
517         ksort($pkgs);
518         return $pkgs;
519 }
520 //
521 // Return merged list of available and installed extensions as a local
522 // configuration array supplemented with installed versions information.
523 //
524 function get_themes_list()
525 {
526         $pkgs = get_pkg_or_list('theme', null, array(
527                                 'Package' => 'package',
528                                 'Version' => 'available',
529                                 'Name' => 'name',
530                                 'Description' => 'Descr'
531                         ));
532
533         // add/update extensions already installed
534         // 
535         $local = get_company_extensions();
536         
537         foreach($local as $extno => $ext) {
538                 if (isset($pkgs[@$ext['package']])) {
539                         $ext['local_id'] = $extno;
540                         $pkgs[$ext['package']] = array_merge($pkgs[$ext['package']], $ext);
541                 }
542         }
543         // TODO: Add other themes from themes directory
544         
545         ksort($pkgs);
546         return $pkgs;
547 }
548 //---------------------------------------------------------------------------------------
549 //
550 //      Return merged list of available and installed COAs as a local 
551 // configuration array supplemented with installed versions information.
552 //
553 function get_charts_list()
554 {
555         $pkgs = get_pkg_or_list('chart', null, array(
556                                 'Package' => 'package',
557                                 'Version' => 'available',
558                                 'Name' => 'name',
559                                 'Description' => 'Descr',
560                                 'Type' => 'type',
561                                 'InstallPath' => 'path',
562                                 'Encoding' => 'encoding',
563                                 'SqlScript' => 'sql'
564                         ));
565
566         // add/update default charts
567         // 
568         $local = get_company_extensions();
569
570         foreach($local as $extno => $ext) {
571                 if ($ext['type'] != 'chart') continue;
572                 $ext['local_id'] = $extno;
573                 if (!isset($pkgs[$ext['package']]) || $ext['package'] == '')
574                         $pkgs[] = $ext;
575                 else
576                         $pkgs[$ext['package']] = array_merge($pkgs[$ext['package']], $ext);
577         }
578         ksort($pkgs);
579         return $pkgs;
580 }
581 //---------------------------------------------------------------------------------------------
582 //      Install/update package from repository
583 //
584 function install_language($pkg_name)
585 {
586         global $path_to_root, $installed_languages, $Ajax;
587         
588         $pkg = get_pkg_or_list('language', $pkg_name);
589
590         if ($pkg) {
591                 $i = array_search_key($pkg['Language'], $installed_languages, 'code');
592                 if ($i === null)
593                         $i = count($installed_languages);
594                 else {  // remove another already installed package for this language 
595                         $old_pkg = @$installed_languages[$i]['package'];
596                         if ($old_pkg && ($pkg['Package'] != $old_pkg))
597                                 uninstall_package($old_pkg);
598                 }
599
600                 $package = new package("$path_to_root/tmp/".$pkg['Filename'].'.pkg');
601                 if ($package->install()) {
602                         $lang = array(
603                                 'name' => $pkg['Name'],
604                                 'package' => $pkg['Package'],
605                                 'code' => $pkg['Language'],
606                                 'encoding' => $pkg['Encoding'],
607                                 'version' => $pkg['Version'],
608                                 'path' => $pkg['InstallPath']
609                         );
610                         if ($pkg['RTLDir']=='yes')
611                                 $lang['rtl'] = true;
612                         $installed_languages[$i] = $lang;
613                         write_lang($installed_languages);
614                         unlink("$path_to_root/tmp/".$pkg['Filename'].'.pkg');
615                         $Ajax->activate('lang_tbl');
616                 } else {
617                         display_error(implode('<br>', $package->error));
618                         return false;
619                 }
620         } else {
621                 display_error(sprintf(_("Package '%s' not found."), $pkg_name));
622                 return false;
623         }
624         return true;
625 }
626 //---------------------------------------------------------------------------------------------
627 //      Install/update extension or theme package from repository
628 //
629 function install_extension($pkg_name)
630 {
631         global $path_to_root, $installed_extensions, $next_extension_id, $Ajax;
632         
633         $pkg = get_pkg_or_list(array('extension', 'theme', 'chart'), $pkg_name);
634         if ($pkg) {
635                 $package = new package("$path_to_root/tmp/".$pkg['Filename'].'.pkg');
636                 $local_exts = get_company_extensions();
637                 if ($package->install()) {
638                         $ext_id = array_search_key($pkg['Package'], $local_exts, 'package');
639                         if ($ext_id === null)
640                                 $ext_id = $next_extension_id++;
641                         else {  // remove another already installed package for this language 
642                                 $old_pkg = $installed_extensions[$ext_id]['package'];
643                                 if ($old_pkg)
644                                         uninstall_package($old_pkg);
645                         }
646                         $ext = array(
647                                 'name' => $pkg['Name'],
648                                 'package' => $pkg['Package'],
649                                 'version' => $pkg['Version'],
650                                 'type' => $pkg['Type'],
651                                 'active' => true,
652                                 'path' => $pkg['InstallPath'],
653                         );
654 //                      if (isset($pkg['MenuTabs']))
655 //                              $ext['tabs'] = $pkg['MenuTabs'];
656 //                      if (isset($pkg['MenuEntries']))
657 //                              $ext['entries'] = $pkg['MenuEntries'];
658 //                      if (isset($pkg['AccessExtensions']))
659 //                              $ext['acc_file'] = $pkg['AccessExtensions'];
660                         if (isset($pkg['SqlScript']))
661                                 $ext['sql'] = $pkg['SqlScript'];
662                         $local_exts[$ext_id] = $ext;
663                         $ret = update_extensions($local_exts);
664                         unlink("$path_to_root/tmp/".$pkg['Filename'].'.pkg');
665                         $Ajax->activate('ext_tbl');
666                         return $ret;
667                 } else {
668                         display_error(implode('<br>', $package->error));
669                         return false;
670                 }
671         } else {
672                 display_error(sprintf(_("Package '%s' not found."), $pkg_name));
673                 return false;
674         }
675         return true;
676 }
677 /*
678         Returns true if newer package version is available
679 */
680 function check_pkg_upgrade($current, $available)
681 {
682         preg_match_all('/[\d]+/', $available, $aver);
683         if (!count($aver[0]))
684                 return false;
685         preg_match_all('/[\d]+/', $current, $cver);
686         if (!count($cver[0]))
687                 return true;
688         foreach($aver[0] as $n => $ver)
689                 if ($ver>@$cver[0][$n]) 
690                         return true;
691         return false;
692 }
693
694 //
695 //      Returns package info from index file
696 //
697 function get_package_info($pkg, $type=null, $filter=array(), $outkey=null, $download=true) {
698         return get_pkg_or_list($type, $pkg, $filter, null, false);
699 }
700
701 ?>