Commit 146783b0 authored by Marius Cramer's avatar Marius Cramer

Changed all windows line breaks to unix line breaks (coding guidelines!)

parent 33bcd006
......@@ -5,12 +5,12 @@ dnsserver=mydns
[server]
auto_network_configuration=n
ip_address=0.0.0.0
netmask=255.255.255.0
gateway=0.0.0.0
hostname=server1.domain.tld
nameservers=8.8.8.8,8.8.4.4
auto_network_configuration=n
ip_address=0.0.0.0
netmask=255.255.255.0
gateway=0.0.0.0
hostname=server1.domain.tld
nameservers=8.8.8.8,8.8.4.4
firewall=bastille
loglevel=2
admin_notify_events=1
......@@ -51,12 +51,12 @@ getmail_config_dir=/etc/getmail
[web]
server_type=apache
website_basedir=/var/www
website_path=/var/www/clients/client[client_id]/web[website_id]
website_symlinks=/var/www/[website_domain]/:/var/www/clients/client[client_id]/[website_domain]/
website_symlinks_rel=n
vhost_conf_dir=/etc/apache2/sites-available
vhost_conf_enabled_dir=/etc/apache2/sites-enabled
nginx_vhost_conf_dir=/etc/nginx/sites-available
website_path=/var/www/clients/client[client_id]/web[website_id]
website_symlinks=/var/www/[website_domain]/:/var/www/clients/client[client_id]/[website_domain]/
website_symlinks_rel=n
vhost_conf_dir=/etc/apache2/sites-available
vhost_conf_enabled_dir=/etc/apache2/sites-enabled
nginx_vhost_conf_dir=/etc/nginx/sites-available
nginx_vhost_conf_enabled_dir=/etc/nginx/sites-enabled
security_level=20
user=www-data
......
<?php
/*
Copyright (c) 2012, ISPConfig UG
Contributors: web wack creations, http://www.web-wack.at
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of ISPConfig nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// Constants describing instances
define('INSTANCE_PENDING', 0);
define('INSTANCE_INSTALL', 1);
define('INSTANCE_ERROR', 2);
define('INSTANCE_SUCCESS', 3);
define('INSTANCE_REMOVE', 4);
// Constants describing packages
define('PACKAGE_LOCKED', 1);
define('PACKAGE_ENABLED', 2);
define('PACKAGE_OUTDATED', 3);
define('PACKAGE_ERROR_NOMETA', 4);
class ApsBase
{
protected $log_prefix = '';
protected $fetch_url = '';
protected $aps_version = '';
protected $packages_dir = '';
protected $temp_pkg_dir = '';
protected $interface_pkg_dir = '';
protected $interface_mode = false; // server mode by default
/**
* Constructor
*
* @param $app the application instance (db handle + log method)
* @param $interface_mode act in interface (true) or server mode (false)
* @param $log_prefix a prefix to set before all log entries
*/
public function __construct($app, $log_prefix = 'APS: ', $interface_mode = false)
{
$this->log_prefix = $log_prefix;
$this->interface_mode = $interface_mode;
$this->fetch_url = 'apscatalog.com';
$this->aps_version = '1';
$this->packages_dir = ISPC_ROOT_PATH.'/aps_packages';
$this->interface_pkg_dir = ISPC_ROOT_PATH.'/web/sites/aps_meta_packages';
}
/**
* Converts a given value to it's native representation in 1024 units
*
* @param $value the size to convert
* @return integer and string
*/
public function convertSize($value)
{
$unit = array('Bytes', 'KB', 'MB', 'GB', 'TB');
return @round($value/pow(1024, ($i = floor(log($value, 1024)))), 2).' '.$unit[$i];
}
/**
* Determine a specific xpath from a given SimpleXMLElement handle. If the
* element is found, it's string representation is returned. If not,
* the return value will stay empty
*
* @param $xml_handle the SimpleXMLElement handle
* @param $query the XPath query
* @param $array define whether to return an array or a string
* @return $ret the return string
*/
protected function getXPathValue($xml_handle, $query, $array = false)
{
$ret = '';
$xp_result = @($xml_handle->xpath($query)) ? $xml_handle->xpath($query) : false;
if($xp_result !== false) $ret = (($array === false) ? (string)$xp_result[0] : $xp_result);
return $ret;
}
}
<?php
/*
Copyright (c) 2012, ISPConfig UG
Contributors: web wack creations, http://www.web-wack.at
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of ISPConfig nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// Constants describing instances
define('INSTANCE_PENDING', 0);
define('INSTANCE_INSTALL', 1);
define('INSTANCE_ERROR', 2);
define('INSTANCE_SUCCESS', 3);
define('INSTANCE_REMOVE', 4);
// Constants describing packages
define('PACKAGE_LOCKED', 1);
define('PACKAGE_ENABLED', 2);
define('PACKAGE_OUTDATED', 3);
define('PACKAGE_ERROR_NOMETA', 4);
class ApsBase
{
protected $log_prefix = '';
protected $fetch_url = '';
protected $aps_version = '';
protected $packages_dir = '';
protected $temp_pkg_dir = '';
protected $interface_pkg_dir = '';
protected $interface_mode = false; // server mode by default
/**
* Constructor
*
* @param $app the application instance (db handle + log method)
* @param $interface_mode act in interface (true) or server mode (false)
* @param $log_prefix a prefix to set before all log entries
*/
public function __construct($app, $log_prefix = 'APS: ', $interface_mode = false)
{
$this->log_prefix = $log_prefix;
$this->interface_mode = $interface_mode;
$this->fetch_url = 'apscatalog.com';
$this->aps_version = '1';
$this->packages_dir = ISPC_ROOT_PATH.'/aps_packages';
$this->interface_pkg_dir = ISPC_ROOT_PATH.'/web/sites/aps_meta_packages';
}
/**
* Converts a given value to it's native representation in 1024 units
*
* @param $value the size to convert
* @return integer and string
*/
public function convertSize($value)
{
$unit = array('Bytes', 'KB', 'MB', 'GB', 'TB');
return @round($value/pow(1024, ($i = floor(log($value, 1024)))), 2).' '.$unit[$i];
}
/**
* Determine a specific xpath from a given SimpleXMLElement handle. If the
* element is found, it's string representation is returned. If not,
* the return value will stay empty
*
* @param $xml_handle the SimpleXMLElement handle
* @param $query the XPath query
* @param $array define whether to return an array or a string
* @return $ret the return string
*/
protected function getXPathValue($xml_handle, $query, $array = false)
{
$ret = '';
$xp_result = @($xml_handle->xpath($query)) ? $xml_handle->xpath($query) : false;
if($xp_result !== false) $ret = (($array === false) ? (string)$xp_result[0] : $xp_result);
return $ret;
}
}
?>
\ No newline at end of file
<?php
/*
Copyright (c) 2012, ISPConfig UG
Contributors: web wack creations, http://www.web-wack.at
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of ISPConfig nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
require_once('aps_base.inc.php');
@set_time_limit(0);
@ignore_user_abort(1);
class ApsCrawler extends ApsBase
{
//public $app_download_url_list = array();
/**
* Constructor
*
* @param $app the application instance (db handle + log method)
* @param $interface_mode act in interface (true) or server mode (false)
*/
public function __construct($app, $interface_mode = false)
{
parent::__construct($app, 'APS crawler: ', $interface_mode);
}
/**
* Before the cron is executed, make sure all necessary options are set
* and all functions (i.e. cURL) are available
*/
private function checkRequirements()
{
global $app;
try
{
// Check if allow_url_fopen is enabled
if(!@ini_get('allow_url_fopen')) throw new Exception('allow_url_fopen is not enabled');
// Check if the cURL module is available
if(!function_exists('curl_version')) throw new Exception('cURL is not available');
// Check if used folders are writable
if($this->interface_mode)
{
if(!is_writable($this->interface_pkg_dir))
throw new Exception('the folder '.basename($this->interface_pkg_dir).' is not writable');
}
else
{
if(!is_writable($this->packages_dir))
throw new Exception('the folder '.basename($this->packages_dir).' is not writable');
}
return true;
}
catch(Exception $e)
{
$app->log($this->log_prefix.'Aborting execution because '.$e->getMessage(), LOGLEVEL_ERROR);
return false;
}
}
/**
* Remove a directory recursively
* In case of error be silent
*
* @param $dir the directory to remove
*/
private function removeDirectory($dir)
{
if(is_dir($dir))
{
$files = scandir($dir);
foreach($files as $file)
{
if($file != '.' && $file != '..')
if(filetype($dir.'/'.$file) == 'dir') $this->removeDirectory($dir.'/'.$file);
else @unlink($dir.'/'.$file);
}
reset($files);
@rmdir($dir);
}
}
/**
* Fetch HTML data from one or more given URLs
* If a string is given, a string is returned, if an array of URLs should
* be fetched, the responses of the parallel queries are returned as array
*
* @param $input the string or array to fetch
* @return $ret a query response string or array
*/
private function fetchPage($input)
{
$ret = array();
$url = array();
$conn = array();
// Make sure we are working with an array, further on
if(!is_array($input)) $url[] = $input;
else $url = $input;
// Build the single cURL handles and add them to a multi handle
$mh = curl_multi_init();
for($i = 0; $i < count($url); $i++)
{
$conn[$i] = curl_init('http://'.$this->fetch_url.$url[$i]);
curl_setopt($conn[$i], CURLOPT_RETURNTRANSFER, true);
curl_multi_add_handle($mh, $conn[$i]);
}
$active = 0;
do curl_multi_exec($mh, $active);
while($active > 0);
// Get the response(s)
for($i = 0; $i < count($url); $i++)
{
$ret[$i] = curl_multi_getcontent($conn[$i]);
curl_multi_remove_handle($mh, $conn[$i]);
curl_close($conn[$i]);
}
curl_multi_close($mh);
if(count($url) == 1) $ret = $ret[0];
return $ret;
}
/**
* Fetch binary data from a given array
* The data is retrieved in binary mode and
* then directly written to an output file
*
* @param $input a specially structed array
* @see $this->startUpdate()
*/
private function fetchFiles($input)
{
$fh = array();
$url = array();
$conn = array();
// Build the single cURL handles and add them to a multi handle
$mh = curl_multi_init();
// Process each app
for($i = 0; $i < count($input); $i++)
{
$conn[$i] = curl_init($input[$i]['url']);
$fh[$i] = fopen($input[$i]['localtarget'], 'wb');
curl_setopt($conn[$i], CURLOPT_BINARYTRANSFER, true);
curl_setopt($conn[$i], CURLOPT_FILE, $fh[$i]);
curl_setopt($conn[$i], CURLOPT_TIMEOUT, 0);
curl_setopt($conn[$i], CURLOPT_FAILONERROR, 1);
curl_setopt($conn[$i], CURLOPT_FOLLOWLOCATION, 1);
curl_multi_add_handle($mh, $conn[$i]);
}
$active = 0;
do curl_multi_exec($mh, $active);
while($active > 0);
// Close the handles
for($i = 0; $i < count($input); $i++)
{
fclose($fh[$i]);
curl_multi_remove_handle($mh, $conn[$i]);
curl_close($conn[$i]);
}
curl_multi_close($mh);
}
/**
* A method to build query URLs out of a list of vendors
*
*/
private function formatVendorCallback($array_item)
{
$array_item = str_replace(' ', '%20', $array_item);
$array_item = str_replace('http://', '', $array_item);
$array_item = '/'.$this->aps_version.'.atom?vendor='.$array_item.'&pageSize=100';
return($array_item);
}
/**
* The main method which performs the actual crawling
*/
public function startCrawler()
{
global $app;
try
{
// Make sure the requirements are given so that this script can execute
$req_ret = $this->checkRequirements();
if(!$req_ret) return false;
// Execute the open task and first fetch all vendors (APS catalog API 1.1, p. 12)
$app->log($this->log_prefix.'Fetching data from '.$this->fetch_url);
$vendor_page = $this->fetchPage('/all-app/'); //$vendor_page = $this->fetchPage('/'.$this->aps_version.'/');
preg_match_all("/\<a href=\"(.+)\/\" class=\"vendor\"/i", $vendor_page, $matches);
$vendors = array_map('urldecode', $matches[1]);
if(!$vendors) throw new Exception('Unable to fetch vendors. Aborting');
// Format all vendors for further processing (i.e. typo3.org -> /1.atom?vendor=typo3.org&pageSize=100
//array_walk($vendors, array($this, 'formatVendorCallback'));
if(is_array($vendors)) {
foreach($vendors as $key => $array_item) {
$vendors[$key] = $this->formatVendorCallback($array_item);
}
}
// Process all vendors in chunks of 50 entries
$vendor_chunks = array_chunk($vendors, 50);
//var_dump($vendor_chunks);
// Get all known apps from the database and the highest known version
// Note: A dirty hack is used for numerical sorting of the VARCHAR field Version: +0 -> cast
// A longer but typesafe way would be: ORDER BY CAST(REPLACE(Version, '.', '') AS UNSIGNED) DESC
$existing_apps = $app->db->queryAllRecords("SELECT * FROM (
SELECT name AS Name, CONCAT(version, '-', CAST(`release` AS CHAR)) AS CurrentVersion
FROM aps_packages ORDER BY REPLACE(version, '.', '')+0 DESC, `release` DESC
) as Versions GROUP BY name");
//var_dump($existing_apps);
// Used for statistics later
$apps_in_repo = 0;
$apps_updated = 0;
$apps_downloaded = 0;
$apps_to_dl = array();
for($i = 0; $i < count($vendor_chunks); $i++)
{
// Fetch all apps for the current chunk of vendors
$apps = $this->fetchPage($vendor_chunks[$i]);
for($j = 0; $j < count($apps); $j++)
{
// Before parsing, make sure it's worth the work by checking if at least one app exists
$apps_count = substr_count($apps[$j], '<opensearch:totalResults>0</opensearch:totalResults>');
if($apps_count == 0) // obviously this vendor provides one or more apps
{
// Rename namespaces and register them
$xml = str_replace("xmlns=", "ns=", $apps[$j]);
$sxe = new SimpleXMLElement($xml);
$namespaces = $sxe->getDocNamespaces(true);
foreach($namespaces as $ns => $url) $sxe->registerXPathNamespace($ns, $url);
// Fetching values of interest
$app_name = parent::getXPathValue($sxe, 'entry[position()=1]/a:name');
$app_version = parent::getXPathValue($sxe, 'entry[position()=1]/a:version');
$app_release = parent::getXPathValue($sxe, 'entry[position()=1]/a:release');
// Find out a (possibly) existing package version
$ex_ver = '';
/*
array_walk($existing_apps,
create_function('$v, $k, $ex_ver', 'if($v["Name"] == "'.$app_name.'") $ex_ver = $v["CurrentVersion"];'), &$ex_ver);
*/
if(is_array($existing_apps)) {
foreach($existing_apps as $k => $v) {
if($v["Name"] == $app_name) $ex_ver = $v["CurrentVersion"];
}
}
$new_ver = $app_version.'-'.$app_release;
$local_intf_folder = $this->interface_pkg_dir.'/'.$app_name.'-'.$new_ver.'.app.zip/';
// Proceed if a newer or at least equal version has been found with server mode or
// interface mode is activated and there are no valid APP-META.xml and PKG_URL existing yet
if((!$this->interface_mode && version_compare($new_ver, $ex_ver) >= 0) || ($this->interface_mode && (!file_exists($local_intf_folder.'APP-META.xml') || filesize($local_intf_folder.'APP-META.xml') == 0 || !file_exists($local_intf_folder.'PKG_URL') || filesize($local_intf_folder.'PKG_URL') == 0))){
// Check if we already have an old version of this app
if(!empty($ex_ver) && version_compare($new_ver, $ex_ver) == 1) $apps_updated++;
$app_dl = parent::getXPathValue($sxe, "entry[position()=1]/link[@a:type='aps']/@href");
$app_filesize = parent::getXPathValue($sxe, "entry[position()=1]/link[@a:type='aps']/@length");
$app_metafile = parent::getXPathValue($sxe, "entry[position()=1]/link[@a:type='meta']/@href");
//$this->app_download_url_list[$app_name.'-'.$new_ver.'.app.zip'] = $app_dl;
// Skip ASP.net packages because they can't be used at all
$asp_handler = parent::getXPathValue($sxe, '//aspnet:handler');
$asp_permissions = parent::getXPathValue($sxe, '//aspnet:permissions');
$asp_version = parent::getXPathValue($sxe, '//aspnet:version');
if(!empty($asp_handler) || !empty($asp_permissions) || !empty($asp_version)) continue;
// Interface mode (download only parts)
if($this->interface_mode)
{
// Delete an obviously out-dated version from the system and DB
if(!empty($ex_ver) && version_compare($new_ver, $ex_ver) == 1)
{
$old_folder = $this->interface_pkg_dir.'/'.$app_name.'-'.$ex_ver.'.app.zip';
if(file_exists($old_folder)) $this->removeDirectory($old_folder);
/*
$app->db->query("UPDATE aps_packages SET package_status = '".PACKAGE_OUTDATED."' WHERE name = '".
$app->db->quote($app_name)."' AND CONCAT(version, '-', CAST(`release` AS CHAR)) = '".
$app->db->quote($ex_ver)."';");
*/
$tmp = $app->db->queryOneRecord("SELECT id FROM aps_packages WHERE name = '".
$app->db->quote($app_name)."' AND CONCAT(version, '-', CAST(`release` AS CHAR)) = '".
$app->db->quote($ex_ver)."';");
$app->db->datalogUpdate('aps_packages', "package_status = ".PACKAGE_OUTDATED, 'id', $tmp['id'