1 #!/usr/bin/php
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 list Lists installed plugins
40 update-gosa rescan-i18n Rebuilds the translations
42 update-gosa rescan-classes Rebuilds the class list
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);
119 }
122 function rescan_classes()
123 {
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);
148 }
151 function rescan_i18n()
152 {
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 --use-first ".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 }
192 echo "! Warning: you may need to reload your webservice!\n";
193 }
196 function rescan_guide()
197 {
198 $master_guide= "doc/guide.xml";
199 echo "Updating Online Help Index...\n";
200 $master_guide_content="<?xml version=\"1.0\"?>\n".
201 "<!--\n".
202 "\tWARNING:\n".
203 "\tThis file is automatically generated by update-online-help.\n".
204 "\tIf you want to add entries, use doc/core/guide.xml or doc/plugins/\"Appropriate Plugin Directory\"/guide.xml.\n".
205 "\tThen execute update-online-help to merge them into this file.\n".
206 "-->\n\n".
207 "<!--\n".
208 "\tThis xml file specifies which class is documented in which help file.\n".
209 "\tIf isset ( \$_SESSION['current_class_for_help'] ) then open the helpfile which is\n".
210 "\tspecified for this class below.\n".
211 "-->\n\n".
212 "<!--\n".
213 "\t<ENTRY NAME='class name' VALUE='displayed text' PATH='path to helpfiles' FILE='path to htmlfile' />\n".
214 "\tLeave blank to display message \"There is no helpfile specified for this class.\"\n".
215 "-->\n".
216 "<ENTRIES>\n";
218 $guide= 'doc/core/guide.xml';
219 if(file_exists($guide) && is_readable($guide)) {
220 $master_guide_content.= file_get_contents($guide);
221 }
223 if(file_exists('doc/plugins')) {
224 $plugins= scandir('doc/plugins');
225 foreach($plugins as $key => $plugin) {
226 if($plugin != '.' && $plugin != '..') {
227 if(is_dir('doc/plugins/'.$plugin)) {
228 $guide= 'doc/plugins/'.$plugin.'/guide.xml';
229 if(file_exists($guide) && is_readable($guide)) {
230 $master_guide_content.= file_get_contents($guide);
231 }
232 }
233 }
234 }
235 }
237 $master_guide_content.= "</ENTRIES>";
239 $master_guide_content= preg_replace("/[ \t][ \t]*/", " ", $master_guide_content);
241 if((file_exists($master_guide) && is_writable($master_guide)) || is_writable('doc')) {
242 file_put_contents($master_guide, $master_guide_content);
243 }
245 }
248 function parse_ini($file)
249 {
250 global $description, $provides, $depends, $versions, $conflicts;
252 $res= "";
253 if (file_exists($file)){
254 $tmp= parse_ini_file($file, TRUE);
256 if (isset($tmp['gosa-plugin'])){
257 $plugin= &$tmp['gosa-plugin'];
258 if (isset($plugin['name'])&& isset($plugin['description'])){
259 $res= $plugin['name'];
260 $description[$res]= $plugin['description'];
261 $versions[$res]= $plugin['version'];
262 $provides[$res]= $res;
263 if (isset($plugin['depends'])){
264 $depends[$res]= explode(',', preg_replace('/\s+/', '', $plugin['depends']));
265 }
266 if (isset($plugin['conflicts'])){
267 $conflicts[$res]= explode(',', preg_replace('/\s+/', '', $plugin['conflicts']));
268 }
269 }
270 }
271 }
273 return $res;
274 }
277 function dependency_check()
278 {
279 global $description, $provides, $depends;
281 foreach ($depends as $name => $pl_depends){
282 foreach ($pl_depends as $pl){
283 if (!in_array($pl, $provides)){
284 echo "! Error: plugin '$name' depends on '$pl' which is not provided by any plugin\n\n";
285 exit (1);
286 }
287 }
288 }
289 }
292 function load_plugins()
293 {
294 if (!is_dir(PLUGSTATE_DIR)){
295 if (!mkdir (PLUGSTATE_DIR, 0755, TRUE)){
296 echo "Cannot create plugstate dir '".PLUGSTATE_DIR."' - aborted\n";
297 exit (2);
298 }
299 }
300 $dir= new DirectoryIterator(PLUGSTATE_DIR);
301 foreach ($dir as $entry){
302 if ($dir->isDir() && !preg_match('/^\./', $dir->__toString())){
303 $file= $dir->getPathName()."/plugin.dsc";
304 if (parse_ini($file) == ""){
305 echo "! Warning: plugin ".$dir->getPathName()." is missing declarations\n";
306 }
307 }
308 }
309 }
312 function list_plugins()
313 {
314 global $description, $versions;
315 $count= 0;
317 /* Load plugin list */
318 load_plugins();
320 /* Show plugins */
321 foreach ($description as $name => $dsc){
322 if ($count == 0){
323 echo "Plugin\t\t|Version |Description\n";
324 echo "----------------------------------------------------------------------------\n";
325 }
326 $ver= $versions[$name];
327 echo "$name\t\t|$ver\t |$dsc\n";
328 $count++;
329 }
331 /* Yell about non existing plugins... */
332 if ($count == 0){
333 echo "No plugins found...\n\n";
334 } else {
335 # Check for dependencies
336 dependency_check();
337 echo "\n";
338 }
339 }
342 function install_plugin($file)
343 {
344 global $description, $provides, $depends, $conflicts;
346 /* Load plugin list */
347 load_plugins();
349 /* Load .dsc file */
350 if (file_exists($file)){
351 $tmp= parse_ini_file($file, TRUE);
353 if (isset($tmp['gosa-plugin'])){
354 $plugin= &$tmp['gosa-plugin'];
355 if (isset($plugin['name'])&& isset($plugin['description'])){
356 $name= $plugin['name'];
357 $description= $plugin['description'];
358 $depends= array();
359 if (isset($plugin['depends'])){
360 $depends= explode(',', preg_replace('/\s+/', '', $plugin['depends']));
361 }
363 /* Already installed? */
364 if (isset($provides[$name])){
365 echo "! Error: plugin already installed\n\n";
366 exit (3);
367 }
369 /* Check if dependencies are fullfilled */
370 foreach ($depends as $dep){
371 $found= false;
372 foreach ($provides as $provide => $dummy){
373 if ($dep == $provide){
374 $found= true;
375 break;
376 }
377 }
378 if (!$found){
379 echo "! Error: plugin depends on '$dep', but this is not installed\n\n";
380 exit (3);
381 }
382 }
384 /* Check for conflicts */
385 foreach ($conflicts as $conf){
386 if (!in_array($conf, $provides)){
387 echo "! Warning: plugin conflicts with '$conf'\n\n";
388 }
389 }
391 /* Create plugstate directory and touch plugin.lst */
392 if (!mkdir (PLUGSTATE_DIR."/$name", 0755, TRUE)){
393 echo "Failed to create '".PLUGSTATE_DIR."/$name - aborted";
394 exit (3);
395 }
396 if (!$handle= fopen(PLUGSTATE_DIR."/$name/plugin.lst", 'w')) {
397 echo "Cannot open file '$filename' - aborted\n";
398 exit (1);
399 }
401 echo "Installing plugin '$name'...\n";
403 /* Copy and fill plugin.lst */
404 $path= dirname($file);
405 $dir= new RecursiveDirectoryIterator($path);
406 $all= new RecursiveIteratorIterator($dir);
407 foreach ( $all as $entry ){
408 $source= $path."/".substr($entry->getPathName(), strlen($path) + 1);
410 /* Skip description - it belongs to the state dir */
411 if (preg_match('/\/plugin.dsc$/', $source)){
412 copy ($source, PLUGSTATE_DIR."/$name/plugin.dsc");
413 continue;
414 }
416 /* Skip well known directories */
417 if (preg_match('/^\.+$/', $source) || preg_match('/\/\.svn\//', $source)) {
418 continue;
419 }
421 /* Calculate destination */
422 if (preg_match("%^.*locale/%", $source)){
423 $dest= GOSA_HOME."/locale/plugins/$name/".preg_replace("%^.*locale/%", "", $source);
424 } elseif (preg_match("%^.*help/%", $source)) {
425 $dest= GOSA_HOME."/doc/plugins/$name/".preg_replace("%^.*help/%", "", $source);
426 } elseif (preg_match("%^.*html/%", $source)) {
427 $dest= GOSA_HOME."/html/plugins/$name/".preg_replace("%^.*html/%", "", $source);
428 } else {
429 $dest= GOSA_HOME."/plugins/".substr($entry->getPathName(), strlen($path) + 1);
430 }
432 /* Destination exists in case of directories? */
433 if ($entry->isDir()){
434 if (!is_dir($dest)){
435 mkdir($dest, 0755, TRUE);
436 fwrite ($handle, "$dest\n");
437 }
438 } else {
439 if (!is_dir(dirname($dest))){
440 mkdir(dirname($dest), 0755, TRUE);
441 fwrite ($handle, dirname($dest)."\n");
442 }
443 }
445 /* Copy files */
446 if ($entry->isFile()){
447 copy ($source, $dest);
448 }
450 /* Note what we did... */
451 fwrite ($handle, "$dest\n");
452 }
454 fclose($handle);
455 }
456 }
457 }
459 /* Update caches */
460 rescan_classes();
461 rescan_i18n();
462 rescan_guide();
463 }
466 function remove_plugin($name)
467 {
468 global $description, $depends;
470 /* Load plugin list */
471 load_plugins();
473 /* Present? */
474 if (!isset($description[$name])){
475 echo "! Error: cannot find a plugin named '$name'\n\n";
476 exit (1);
477 }
479 /* Depends? */
480 foreach ($depends as $sname => $pl_depends){
481 if (in_array($name, $pl_depends)){
482 echo "! Error: plugin '$sname' depends on '$name' - cannot remove it\n\n";
483 exit (1);
484 }
485 }
487 /* Load information */
488 if (!file_exists(PLUGSTATE_DIR."/$name/plugin.lst")){
489 echo "! Error: cannot remove plugin '$name' - no install history found\n\n";
490 exit (1);
491 }
493 echo "Removing plugin '$name'...\n";
494 $contents= file(PLUGSTATE_DIR."/$name/plugin.lst");
495 $cnv= array();
496 foreach($contents as $line){
497 $entry= chop($line);
498 $cnv[strlen($entry).":$entry"]= $entry;
499 }
500 krsort($cnv);
502 /* Remove files first */
503 clearstatcache();
504 foreach ($cnv as $entry){
505 if (is_dir($entry)){
506 rmdir($entry);
507 continue;
508 }
509 if (file_exists($entry)){
510 unlink($entry);
511 }
512 }
514 /* Remove state directory for plugin */
515 rmdirRecursive(PLUGSTATE_DIR."/$name");
517 /* Update caches */
518 rescan_classes();
519 rescan_i18n();
520 rescan_guide();
521 }
525 /* Fill global values */
526 $description= $provides= $depends= $versions= $conflicts= array();
528 /* Action specified? */
529 if ($argc < 2){
530 rescan_classes();
531 rescan_i18n();
532 rescan_guide();
533 exit (0);
534 }
536 switch ($argv[1]){
537 case 'install':
538 if (isset($argv[2])){
539 install_plugin($argv[2]);
540 } else {
541 echo "Usage: update-gosa install dsc-file\n\n";
542 exit (1);
543 }
544 break;
545 case 'list':
546 list_plugins();
547 break;
548 case 'remove':
549 if (isset($argv[2])){
550 remove_plugin($argv[2]);
551 } else {
552 echo "Usage: update-gosa remove plugin-name\n\n";
553 exit (1);
554 }
555 break;
556 case 'rescan-i18n':
557 rescan_i18n();
558 break;
559 case 'rescan-classes':
560 rescan_classes();
561 break;
562 default:
563 echo "Error: Supplied command not known\n\n";
564 print_usage();
565 break;
566 }
569 ?>