modules/webapps/include/webapp-installer.php
6a9c38d8
 <?php
c208bd90
 /*
 This file belongs to the Webinterface of schokokeks.org Hosting
 
cf54502a
 Written 2008-2018 by schokokeks.org Hosting, namely
c208bd90
   Bernd Wurst <bernd@schokokeks.org>
   Hanno Böck <hanno@schokokeks.org>
 
 To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty.
 
2626dd47
 You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see
c208bd90
 http://creativecommons.org/publicdomain/zero/1.0/
 
 Nevertheless, in case you use a significant part of this code, we ask (but not require, see the license) that you keep the authors' names in place and return your changes to the public. We would be especially happy if you tell us what you're going to do with this code.
 */
6a9c38d8
 
 require_once('inc/base.php');
 
 function create_new_webapp($appname, $directory, $url, $data)
 {
2626dd47
     if (directory_in_use($directory)) {
         system_failure('Sie haben erst kürzlich eine Anwendung in diesem Verzeichnis installieren lassen. Aus Sicherheitsgründen können Sie in diesem Verzeichnis am selben Tag nicht schon wieder eine Anwendung installieren.');
     }
     $args = array(":username" => $_SESSION['userinfo']['username'],
8132c40e
                 ":appname" => $appname,
                 ":dir" => $directory,
                 ":url" => $url,
                 ":data" => $data);
2626dd47
     db_query("INSERT INTO vhosts.webapp_installer (appname, directory, url, state, username, data) VALUES (:appname, :dir, :url, 'new', :username, :data)", $args);
6a9c38d8
 }
 
 
d5f2f3f4
 function request_update($appname, $directory, $url)
 {
2626dd47
     if (directory_in_use($directory)) {
         system_failure('Sie haben erst kürzlich eine Anwendung in diesem Verzeichnis installieren lassen oder ein Update in diesem Verzeichnis angefordert. Bitte warten Sie bis diese Aktion durchgeführt wurde.');
     }
     $args = array(":username" => $_SESSION['userinfo']['username'],
8132c40e
                 ":appname" => $appname,
                 ":dir" => $directory,
                 ":url" => $url);
2626dd47
     db_query("INSERT INTO vhosts.webapp_installer (appname, directory, url, state, username) VALUES (:appname, :dir, :url, 'old', :username)", $args);
d5f2f3f4
 }
 
3be4b0f7
 function directory_in_use($directory)
 {
2626dd47
     $result = db_query("SELECT id FROM vhosts.webapp_installer WHERE (state IN ('new','old') OR DATE(lastchange)=CURDATE()) AND directory=?", array($directory));
     if ($result->rowCount() > 0) {
         return true;
     }
     return false;
3be4b0f7
 }
d5f2f3f4
 
 function upgradeable($appname, $version)
 {
2626dd47
     DEBUG("Is {$appname}-{$version} upgradeable?");
     if ($appname == 'Drupal7') {
         DEBUG("found Drupal-7.*!");
         return 'drupal7';
2c065cab
     }
2626dd47
     if ($appname == 'Drupal') {
         DEBUG("found Drupal!");
         if (substr($version, 0, 2) == '7.') {
             DEBUG("found Drupal-7.*!");
             return 'drupal7';
         }
         DEBUG("Version: ".substr($version, 0, 2));
     } elseif ($appname == 'MediaWiki') {
         DEBUG("found MediaWiki");
         return 'mediawiki';
     }
     /*elseif ($appname == 'owncloud')
     {
       DEBUG('found OwnCloud');
       return 'owncloud';
     }*/
     DEBUG("found no upgradeable webapp!");
     return null;
d5f2f3f4
 }
 
 
 function get_url_for_dir($docroot, $cutoff = '')
 {
2626dd47
     if (substr($docroot, -1) == '/') {
         $docroot = substr($docroot, 0, -1);
     }
     $result = db_query("SELECT `ssl`, IF(FIND_IN_SET('aliaswww', options), CONCAT('www.',fqdn), fqdn) AS fqdn FROM vhosts.v_vhost WHERE docroot IN (?, ?)", array($docroot, $docroot.'/'));
     if ($result->rowCount() < 1) {
         if (!strstr($docroot, '/')) {
             return null;
         }
         return get_url_for_dir(substr($docroot, 0, strrpos($docroot, '/')), substr($docroot, strrpos($docroot, '/')).$cutoff);
     }
     $tmp = $result->fetch();
     $prefix = 'http://';
     if ($tmp['ssl'] == 'forward' || $tmp['ssl'] == 'https') {
         $prefix = 'https://';
     }
     return $prefix.$tmp['fqdn'].$cutoff;
d5f2f3f4
 }
 
 
652f1c82
 function create_webapp_mysqldb($application, $sitename)
6a9c38d8
 {
2626dd47
     // dependet auf das mysql-modul
     require_once('modules/mysql/include/mysql.php');
9086c9ad
 
2626dd47
     $username = $_SESSION['userinfo']['username'];
     $description = "Automatisch erzeugte Datenbank für {$application} ({$sitename})";
9086c9ad
 
2626dd47
     // zuerst versuchen wir username_webappname. Wenn das nicht klappt, dann wird hochgezählt
     $handle = $username.'_'.$application;
9086c9ad
 
2626dd47
     if (validate_mysql_username($handle) && validate_mysql_dbname($handle) && ! (has_mysql_user($handle) || has_mysql_database($handle))) {
         logger(LOG_INFO, "webapps/include/webapp-installer", "create", "creating db and user »{$handle}«");
         create_mysql_database($handle, $description);
         create_mysql_account($handle, $description);
         set_mysql_access($handle, $handle, true);
         $password = random_string(10);
         set_mysql_password($handle, $password);
         return array('dbuser' => $handle, 'dbname' => $handle, 'dbpass' => $password);
     }
6a9c38d8
 
2626dd47
     for ($i = 0; $i < 100 ; $i++) {
         $handle = $username.'_'.$i;
         if (validate_mysql_username($handle) && validate_mysql_dbname($handle) && ! (has_mysql_user($handle) || has_mysql_database($handle))) {
             logger(LOG_INFO, "webapps/include/webapp-installer", "create", "creating db and user »{$handle}«");
             create_mysql_database($handle, $description);
             create_mysql_account($handle, $description);
             set_mysql_access($handle, $handle, true);
             $password = random_string(10);
             set_mysql_password($handle, $password);
             return array('dbuser' => $handle, 'dbname' => $handle, 'dbpass' => $password);
         }
652f1c82
     }
2626dd47
     system_failure('Konnte keine Datenbank erzeugen. Bitte melden Sie diesen Umstand den Administratoren!');
6a9c38d8
 }