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