Code

Updated template
[gosa.git] / gosa-core / update-gosa
1 #!/usr/bin/php5
2 <?php
3 /*
4  * This code is part of GOsa (http://www.gosa-project.org)
5  * Copyright (C) 2003-2008 GONICUS GmbH
6  *
7  * ID: $$Id: main.php 9254 2008-03-03 15:57:49Z cajus $$
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
24 define ("GOSA_HOME", dirname(__FILE__));
25 define ("LOCALE_DIR", GOSA_HOME."/locale");
26 define ("PLUGSTATE_DIR", GOSA_HOME."/state");
28 function print_usage()
29 {
30         ?>
31 update-gosa - class cache updated and plugin manager for GOsa
32 Usage: update-gosa install dsc     Install the plugin using the dsc information
33                                    placed in the plugin source directory.
35        update-gosa remove plugin   Remove the plugin named "plugin" from
36                                    the current configuration.
38        update-gosa lists           Lists installed plugins
40        update-gosa rescan-i18n     Rebuilds the translations
42        update-gosa rescan-classes  Rebuilds the class list
43        
44        update-gosa                 Shortcut for rescan-classes and rescan-i18n
45 <?php
46         exit (1);
47 }
50 function rmdirRecursive($path, $followLinks=false) {
51   $dir= opendir($path);
52   while($entry= readdir($dir)) {
53     if(is_file($path."/".$entry) || ((!$followLinks) && is_link($path."/".$entry))) {
54       unlink($path."/".$entry);
55     } elseif (is_dir($path."/".$entry) && $entry!='.' && $entry!='..') {
56       rmdirRecursive($path."/".$entry);
57     }
58   }
59   closedir($dir);
60   return rmdir($path);
61 }
64 /* Function to include all class_ files starting at a given directory base */
65 function get_classes($folder= ".")
66 {
67   static $base_dir= "";
68   static $result= array();
70   if ($base_dir == ""){
71     if ($folder == "."){
72       $base_dir= getcwd();
73     } else {
74       $base_dir= $folder;
75     }
76   }
78   $currdir=getcwd();
79   if ($folder){
80     chdir("$folder");
81   }
83   $dh = opendir(".");
84   while(false !== ($file = readdir($dh))){
86     if (preg_match("/.*\.svn.*/", $file) ||
87         preg_match("/.*smarty.*/i",$file) ||
88         preg_match("/.*\.tpl.*/",$file) ||
89         ($file==".") ||($file =="..")){
90       continue;
91     }
93     /* Recurse through all "common" directories */
94     if (is_dir($file)){
95       get_classes($file);
96       continue;
97     }
99     /* Only take care about .inc and .php files... */
100     if (!(preg_match('/\.php$/', $file) || preg_match('/\.inc$/', $file))){
101       continue;
102     }
104     /* Include existing class_ files */
105     $contents= file($file);
106     foreach($contents as $line){
107       $line= chop($line);
108       if (preg_match('/^\s*class\s*\w.*$/', $line)){
109         $class= preg_replace('/^\s*class\s*(\w+).*$/', '\1', $line);
110         $result[$class]= preg_replace("%$base_dir/%", "", "$currdir/$folder/$file");
111       }
112     }
113   }
115   closedir($dh);
116   chdir($currdir);
118   return ($result);
122 function rescan_classes()
124         echo "Updating class cache...\n";
125         $class_mapping= get_classes();
126         $filename= GOSA_HOME."/include/class_location.inc";
128         /* Sanity checks */
129         if (!file_exists($filename) || is_writable($filename)) {
131             if (!$handle= fopen($filename, 'w')) {
132                  echo "Cannot open file \"$filename\" - aborted\n";
133                  exit (1);
134             }
136         } else {
137             echo "File \"$filename\" is not writable - aborted\n";
138             exit (2);
139         }
141         fwrite ($handle, "<?php\n\$class_mapping= array(\n");
142         foreach ($class_mapping as $key => $value){
143           fwrite ($handle, "                \"$key\" => \"$value\",\n");
144         }
145         fwrite ($handle, " );\n");
147         fclose($handle);
151 function rescan_i18n()
153         echo "Updating internationalization...\n";
154         $languages= array();
155         $size= strlen(LOCALE_DIR);
157         /* Get all available messages.po files, sort them for languages */
158         $dir= new RecursiveDirectoryIterator(LOCALE_DIR);
159         $all= new RecursiveIteratorIterator($dir);
160         foreach ( $all as $element ){
161                 if ($element->isFile() && preg_match('/\/LC_MESSAGES\/messages.po$/', $element->getPathname())){
162                         $lang= preg_replace('/^.*\/([^\/]+)\/LC_MESSAGES\/.*$/', '\1', $element);
163                         if (!isset($languages[$lang])){
164                                 $languages[$lang]= array();
165                         }
166                         $languages[$lang][]= substr($element->getPathName(), $size+1);
167                 }
168         }
170         /* For each language, merge the target .mo to the compiled directory. */
171         foreach ($languages as $language => $po_files){
172                 if (!is_dir(LOCALE_DIR."/compiled/${language}/LC_MESSAGES")){
173                         if (!mkdir (LOCALE_DIR."/compiled/${language}/LC_MESSAGES", 0755, TRUE)){
174                                 echo "Failed to create '".LOCALE_DIR."/compiled/${language}/LC_MESSAGES'- aborted";
175                                 exit (3);
176                         }
177                 }
179                 /* Cat all these po files into one single file */
180                 system ("(cd ".LOCALE_DIR." && msgcat ".implode(" ", $po_files)." > compiled/${language}/LC_MESSAGES/messages.po)", $val);
181                 if ($val != 0){
182                         echo "Merging of message files failed - aborted";
183                         exit (4);
184                 }
185                 system ("(cd ".LOCALE_DIR."/compiled/${language}/LC_MESSAGES && msgfmt -o messages.mo messages.po && rm messages.po)", $val);
186                 if ($val != 0){
187                         echo "Compiling of message files failed - aborted";
188                         exit (5);
189                 }
190         }
194 function parse_ini($file)
196         global $description, $provides, $depends, $versions, $conflicts;
198         $res= "";
199         if (file_exists($file)){
200                 $tmp= parse_ini_file($file, TRUE);
202                 if (isset($tmp['gosa-plugin'])){
203                         $plugin= &$tmp['gosa-plugin'];
204                         if (isset($plugin['name'])&& isset($plugin['description'])){
205                                 $res= $plugin['name'];
206                                 $provides[$res]= $plugin[$res];
207                                 $description[$res]= $plugin['description'];
208                                 $versions[$res]= $plugin['version'];
209                                 if (isset($plugin['depends'])){
210                                         $depends[$res]= explode(',', preg_replace('/\s+/', '', $plugin['depends']));
211                                 }
212                                 if (isset($plugin['conflicts'])){
213                                         $conflicts[$res]= explode(',', preg_replace('/\s+/', '', $plugin['conflicts']));
214                                 }
215                         }
216                 }
217         }
219         return $res;
223 function dependency_check()
225         global $description, $provides, $depends;
227         foreach ($depends as $name => $pl_depends){
228                 foreach ($pl_depends as $pl){
229                         if (!in_array($pl, $provides)){
230                                 echo "! Error: plugin '$name' depends on '$pl' which is not provided by any plugin\n\n";
231                                 exit (1);
232                         }
233                 }
234         }
238 function load_plugins()
240         if (!is_dir(PLUGSTATE_DIR)){
241                 if (!mkdir (PLUGSTATE_DIR, 0755, TRUE)){
242                         echo "Cannot create plugstate dir '".PLUGSTATE_DIR."' - aborted\n";
243                         exit (2);
244                 }
245         }
246         $dir= new DirectoryIterator(PLUGSTATE_DIR);
247         foreach ($dir as $entry){
248                 if ($dir->isDir() && !preg_match('/^\./', $dir->__toString())){
249                         $file= $dir->getPathName()."/plugin.dsc";
250                         if (parse_ini($file) == ""){
251                                 echo "! Warning: plugin ".$dir->getPathName()." is missing declarations\n";
252                         }
253                 }
254         }
258 function list_plugins()
260         global $description, $versions;
261         $count= 0;
263         /* Load plugin list */
264         load_plugins();
266         /* Show plugins */
267         foreach ($description as $name => $dsc){
268                 if ($count == 0){
269                         echo "Plugin\t\t|Version |Description\n";
270                         echo "----------------------------------------------------------------------------\n";
271                 }
272                 $ver= $versions[$name];
273                 echo "$name\t\t|$ver\t |$dsc\n";
274                 $count++;
275         }
277         /* Yell about non existing plugins... */
278         if ($count == 0){
279                 echo "No plugins found...\n\n";
280         } else {
281                 # Check for dependencies
282                 dependency_check();
283                 echo "\n";
284         }
288 function install_plugin($file)
290         global $description, $provides, $depends, $conflicts;
292         /* Load plugin list */
293         load_plugins();
295         /* Load .dsc file */
296         if (file_exists($file)){
297                 $tmp= parse_ini_file($file, TRUE);
299                 if (isset($tmp['gosa-plugin'])){
300                         $plugin= &$tmp['gosa-plugin'];
301                         if (isset($plugin['name'])&& isset($plugin['description'])){
302                                 $name= $plugin['name'];
303                                 $description= $plugin['description'];
304                                 $depends= array();
305                                 if (isset($plugin['depends'])){
306                                         $depends= explode(',', preg_replace('/\s+/', '', $plugin['depends']));
307                                 }
309                                 /* Already installed? */
310                                 if (isset($provides[$name])){
311                                         echo "! Error: plugin already installed\n\n";
312                                         exit (3);
313                                 }
315                                 /* Check if dependencies are fullfilled */
316                                 foreach ($depends as $dep){
317                                         $found= false;
318                                         foreach ($provides as $provide => $dummy){
319                                                 if ($dep == $provide){
320                                                         $found= true;
321                                                         break;
322                                                 }
323                                         }
324                                         if (!$found){
325                                                 echo "! Error: plugin depends on '$dep', but this is not installed\n\n";
326                                                 exit (3);
327                                         }
328                                 }
330                                 /* Check for conflicts */
331                                 foreach ($conflicts as $conf){
332                                         if (!in_array($conf, $provides)){
333                                                 echo "! Warning: plugin conflicts with '$conf'\n\n";
334                                         }
335                                 }
337                                 /* Create plugstate directory and touch plugin.lst */
338                                 if (!mkdir (PLUGSTATE_DIR."/$name", 0755, TRUE)){
339                                         echo "Failed to create '".PLUGSTATE_DIR."/$name - aborted";
340                                         exit (3);
341                                 }
342                                 if (!$handle= fopen(PLUGSTATE_DIR."/$name/plugin.lst", 'w')) {
343                                         echo "Cannot open file '$filename' - aborted\n";
344                                         exit (1);
345                                 }
347                                 echo "Installing plugin '$name'...\n";
349                                 /* Copy and fill plugin.lst */
350                                 $path= dirname($file);
351                                 $dir= new RecursiveDirectoryIterator($path);
352                                 $all= new RecursiveIteratorIterator($dir);
353                                 foreach ( $all as $entry ){
354                                         $source= $path."/".substr($entry->getPathName(), strlen($path) + 1);
356                                         /* Skip description - it belongs to the state dir */
357                                         if (preg_match('/\/plugin.dsc$/', $source)){
358                                                 copy ($source, PLUGSTATE_DIR."/$name/plugin.dsc");
359                                                 continue;
360                                         }
362                                         /* Skip well known directories */
363                                         if (preg_match('/^\.+$/', $source) || preg_match('/\/\.svn\//', $source)) {
364                                                 continue;
365                                         }
367                                         /* Calculate destination */
368                                         if (preg_match("%^.*locale/%", $source)){
369                                                 $dest= GOSA_HOME."/locale/plugin/$name/".preg_replace("%^.*locale/%", "", $source);
370                                         } else {
371                                                 $dest= GOSA_HOME."/plugins/".substr($entry->getPathName(), strlen($path) + 1);
372                                         }
374                                         /* Destination exists in case of directories? */
375                                         if ($entry->isDir()){
376                                                 if (!is_dir($dest)){
377                                                         mkdir($dest, 0755, TRUE);
378                                                         fwrite ($handle, "$dest\n");
379                                                 }
380                                         } else {
381                                                 if (!is_dir(dirname($dest))){
382                                                         mkdir(dirname($dest), 0755, TRUE);
383                                                         fwrite ($handle, dirname($dest)."\n");
384                                                 }
385                                         }
387                                         /* Copy files */
388                                         if ($entry->isFile()){
389                                                 copy ($source, $dest);
390                                         }
392                                         /* Note what we did... */
393                                         fwrite ($handle, "$dest\n");
394                                 }
396                                 fclose($handle);
397                         }
398                 }
399         }
400         
401         /* Update caches */
402         rescan_classes();
403         rescan_i18n();
407 function remove_plugin($name)
409         global $description, $depends;
411         /* Load plugin list */
412         load_plugins();
414         /* Present? */
415         if (!isset($description[$name])){
416                 echo "! Error: cannot find a plugin named '$name'\n\n";
417                 exit (1);
418         }
420         /* Depends? */
421         foreach ($depends as $sname => $pl_depends){
422                 if (in_array($name, $pl_depends)){
423                         echo "! Error: plugin '$sname' depends on '$name' - cannot remove it\n\n";
424                         exit (1);
425                 }
426         }
428         /* Load information */
429         if (!file_exists(PLUGSTATE_DIR."/$name/plugin.lst")){
430                 echo "! Error: cannot remove plugin '$name' - no install history found\n\n";
431                 exit (1);
432         }
434         echo "Removing plugin '$name'...\n";
435         $contents= file(PLUGSTATE_DIR."/$name/plugin.lst");
436         $cnv= array();
437         foreach($contents as $line){
438                 $entry= chop($line);
439                 $cnv[strlen($entry).":$entry"]= $entry;
440         }
441         krsort($cnv);
443         /* Remove files first */
444         clearstatcache();
445         foreach ($cnv as $entry){
446                 if (is_dir($entry)){
447                         rmdir($entry);
448                         continue;
449                 }
450                 if (file_exists($entry)){
451                         unlink($entry);
452                 }
453         }
455         /* Remove state directory for plugin */
456         rmdirRecursive(PLUGSTATE_DIR."/$name");
458         /* Update caches */
459         rescan_classes();
460         rescan_i18n();
465 /* Fill global values */
466 $description= $provides= $depends= $versions= $conflicts= array();
468 /* Action specified? */
469 if ($argc < 2){
470         rescan_classes();
471         rescan_i18n();
472         exit (0);
475 switch ($argv[1]){
476         case 'install':
477                 if (isset($argv[2])){
478                         install_plugin($argv[2]);
479                 } else {
480                         echo "Usage: update-gosa install dsc-file\n\n";
481                         exit (1);
482                 }
483                 break;
484         case 'list':
485                 list_plugins();
486                 break;
487         case 'remove':
488                 if (isset($argv[2])){
489                         remove_plugin($argv[2]);
490                 } else {
491                         echo "Usage: update-gosa remove plugin-name\n\n";
492                         exit (1);
493                 }
494                 break;
495         case 'rescan-i18n':
496                 rescan_i18n();
497                 break;
498         case 'rescan-classes':
499                 rescan_classes();
500                 break;
501         default:
502                 echo "Error: Supplied command not known\n\n";
503                 print_usage();
504                 break;
508 ?>