* dont give sql error in gathering statistic data, if table does not exists (eg. Gallery is not installed)

This commit is contained in:
Ralf Becker 2011-01-05 08:35:47 +00:00
parent 9ab5949756
commit d50bfdab5a
2 changed files with 523 additions and 0 deletions

View File

@ -0,0 +1,310 @@
<?php
/**
* EGgroupware admin - submit EGroupware usage statistic
*
* @link http://www.egroupware.org
* @author Ralf Becker <RalfBecker-AT-outdoor-training.de>
* @package admin
* @copyright (c) 2009 by Ralf Becker <RalfBecker-AT-outdoor-training.de>
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
* @version $Id$
*/
/**
* Submit statistical data to egroupware.org
*/
class admin_statistics
{
const CONFIG_APP = 'admin';
const CONFIG_LAST_SUBMIT = 'last_statistics_submit';
const CONFIG_POSTPONE_SUBMIT = 'postpone_statistics_submit';
const CONFIG_SUBMIT_ID = 'statistics_submit_id';
const CONFIG_COUNTRY = 'country_submit';
const CONFIG_USAGE_TYPE = 'usage_type_submit';
const CONFIG_INSTALL_TYPE = 'install_type_submit';
const SUBMIT_URL = 'http://www.egroupware-server.org/usage-statistic';
const STATISTIC_URL = 'http://www.egroupware-server.org/usage-statistic';
const SUBMISION_RATE = 2592000; // 30 days
/**
* Which methods of this class can be called as menuation
*
* @var array
*/
public $public_functions = array(
'submit' => true,
);
/**
* Display and allow to submit statistical data
*
* @param array $content=null
*/
public function submit($content=null)
{
if (is_array($content))
{
$config = new config(self::CONFIG_APP);
if ($content['postpone'])
{
config::save_value(self::CONFIG_POSTPONE_SUBMIT,time()+$content['postpone'],self::CONFIG_APP);
$what = 'postpone';
}
elseif($content['submit'])
{
config::save_value(self::CONFIG_LAST_SUBMIT,time(),self::CONFIG_APP);
config::save_value(self::CONFIG_SUBMIT_ID,empty($content['submit_id']) ? '***none***' : $content['submit_id'],self::CONFIG_APP);
config::save_value(self::CONFIG_COUNTRY,empty($content['country']) ? '***multinational***' : $content['country'],self::CONFIG_APP);
config::save_value(self::CONFIG_USAGE_TYPE,$content['usage_type'],self::CONFIG_APP);
config::save_value(self::CONFIG_INSTALL_TYPE,$content['install_type'],self::CONFIG_APP);
config::save_value(self::CONFIG_POSTPONE_SUBMIT,null,self::CONFIG_APP); // remove evtl. postpone time
$what = 'submited';
}
egw::redirect_link('/admin/index.php','statistics='.($what ? $what : 'cancled'));
}
$sel_options['usage_type'] = array(
'commercial' => lang('Commercial: all sorts of companies'),
'governmental' => lang('Governmental: incl. state or municipal authorities or services'),
'educational' => lang('Educational: Universities, Schools, ...'),
'non-profit' => lang('Non profit: Clubs, associations, ...'),
'personal' => lang('Personal: eg. within a family'),
'other' => lang('Other'),
);
$sel_options['install_type'] = array(
'archive' => lang('Archive: zip or tar'),
'package' => lang('RPM or Debian package'),
'svn' => lang('Subversion checkout'),
'other' => lang('Other'),
);
$sel_options['postpone'] = array(
//10 => '10 secs',
3600 => lang('one hour'),
2*3600 => lang('two hours'),
24*3600 => lang('one day'),
2*24*3600 => lang('two days'),
7*24*3600 => lang('one week'),
14*24*3600 => lang('two weeks'),
30*24*3600 => lang('one month'),
60*24*3600 => lang('two months'),
);
$config = config::read(self::CONFIG_APP);
//_debug_array($config);
$content = array_merge(self::gather_data(),array(
'statistic_url' => html::a_href(self::STATISTIC_URL,self::STATISTIC_URL,'',' target="_blank"'),
'submit_host' => parse_url(self::SUBMIT_URL,PHP_URL_HOST),
'submit_url' => self::SUBMIT_URL,
'last_submitted' => $config[self::CONFIG_LAST_SUBMIT],
));
//_debug_array($content);
// show previous submit ID
if ($config['statistics_submit_id'])
{
$content['submit_id'] = $config['statistics_submit_id'] == '***none***' ? '' : $config['statistics_submit_id'];
}
// show previous country
if ($config[self::CONFIG_COUNTRY])
{
$content['country'] = $config[self::CONFIG_COUNTRY] == '***multinational***' ? '' : $config[self::CONFIG_COUNTRY];
}
// show previous usage_type
if ($config[self::CONFIG_USAGE_TYPE])
{
$content['usage_type'] = $config[self::CONFIG_USAGE_TYPE];
}
// check if we detected svn or rpm/deb packages --> readonly
if ($content['install_type'] && isset($sel_options['install_type'][$content['install_type']]))
{
$sel_options['install_type'] = array($content['install_type'] => $sel_options['install_type'][$content['install_type']]);
}
// else default to previous type
elseif($config[self::CONFIG_INSTALL_TYPE])
{
$content['install_type'] = $config[self::CONFIG_INSTALL_TYPE];
}
// check if we are due for a new submission
if (!isset($config[self::CONFIG_LAST_SUBMIT]) || $config[self::CONFIG_LAST_SUBMIT ] <= time()-self::SUBMISION_RATE)
{
// clear etemplate_exec_id and replace form.action, before submitting the form
$content['onclick'] = "
var action=this.form.action;
var exec_id=this.form['etemplate_exec_id'].value;
this.form.action='$content[submit_url]';
this.form['etemplate_exec_id'].value='';
this.form.target='_blank';
if (!confirm('".addslashes(lang('Submit displayed information?'))."')) return false;
this.form.submit();
this.form.action = action;
this.form['etemplate_exec_id'].value = exec_id;
this.form.target = '';
return true;";
}
else // we are not due --> tell it the user
{
$readonlys['submit'] = $readonlys['postpone'] = true;
$content['msg'] = lang('Your last submission was less then %1 days ago!',
ceil((time()-$config[self::CONFIG_LAST_SUBMIT])/24/3600));
}
$GLOBALS['egw_info']['flags']['app_header'] = lang('Submit statistic information');
$tmpl = new etemplate('admin.statistics');
$tmpl->exec('admin.admin_statistics.submit',$content,$sel_options,$readonlys,$preserv);
}
/**
* Gather statistical data to submit
*
* @return array key => value pairs
*/
protected static function gather_data()
{
// submit id is sha1 hash from install_id
$data['submit_id'] = sha1($GLOBALS['egw_info']['server']['install_id']);
$data['country'] = $GLOBALS['egw_info']['user']['preferences']['common']['country'];
// api version
$data['version'] = $GLOBALS['egw_info']['apps']['phpgwapi']['version'];
// append EPL version
if (isset($GLOBALS['egw_info']['apps']['stylite']))
{
$data['version'] .= ' '.$GLOBALS['egw_info']['apps']['stylite']['version'].'EPL';
}
// sessions in the last 30 days
$data['sessions'] = $GLOBALS['egw']->db->query('SELECT COUNT(*) FROM egw_access_log WHERE li > '.(time()-30*24*3600))->fetchColumn();
// total accounts from accounts table or ldap
$GLOBALS['egw']->accounts->search(array(
'type' => 'accounts',
'start' => 0,
));
$data['users'] = $GLOBALS['egw']->accounts->total;
$data['php'] = PHP_VERSION.': '.PHP_SAPI;
$data['os'] = PHP_OS;
if (file_exists($file = '/etc/SuSE-release') || file_exists($file = '/etc/redhat-release') || file_exists($file = '/etc/debian_version'))
{
$data['os'] .= ': '.str_replace(array("\n","\r"),'',implode(',',file($file)));
}
if (file_exists('.svn'))
{
$data['install_type'] = 'svn';
}
elseif(EGW_INCLUDE_ROOT == '/usr/share/egroupware' && PHP_OS == 'Linux' && is_link('/usr/share/egroupware/header.inc.php'))
{
$data['install_type'] = 'package';
}
foreach($GLOBALS['egw_info']['apps'] as $app => $app_data)
{
if (in_array($app,array(
'admin','phpgwapi','emailadmin','sambaadmin','developer_tools',
'home','preferences','etemplate','registration','manual','egw-pear',
)))
{
continue; // --> ignore to not submit too much
}
if (($users = self::gather_app_users($app))) // ignore apps noone is allowed to run
{
$data['apps'][$app] = $app.':'.round(100.0*$users/$data['users']).'%';
if (($entries = self::gather_app_entries($app)))
{
$data['apps'][$app] .= ':'.$entries;
}
}
}
ksort($data['apps']);
$data['apps'] = implode("\n",$data['apps']);
return $data;
}
/**
* Get percentage of users allowed to use an application
*
* @param string $app
* @return int number of users allowed to run application
*/
static function gather_app_users($app)
{
$users = array();
if (($access = $GLOBALS['egw']->acl->get_ids_for_location('run',1,$app)))
{
foreach($access as $uid)
{
if ($uid > 0)
{
$users[] = $uid;
}
elseif (($members = $GLOBALS['egw']->accounts->members($uid,true)))
{
$users = array_merge($users,$members);
}
}
$users = array_unique($users);
}
return count($users);
}
/**
* Get percentage of users allowed to use an application
*
* @param string $app
* @return int
*/
static function gather_app_entries($app)
{
// main table for each application
static $app2table = array(
'addressbook' => 'egw_addressbook',
'bookmarks' => 'egw_bookmarks',
'calendar' => 'egw_cal_dates',
'infolog' => 'egw_infolog',
'filemanager' => 'egw_sqlfs',
'gallery' => 'g2_Item',
'news_admin' => 'egw_news WHERE news_submittedby > 0', // exclude imported rss feeds
'polls' => 'egw_polls',
'projectmanager' => 'egw_pm_projects',
'phpbrain' => 'egw_kb_articles',
'resources' => 'egw_resources',
'sitemgr' => 'egw_sitemgr_pages',
'syncml' => 'egw_syncmlsummary',
'timesheet' => 'egw_timesheet',
'tracker' => 'egw_tracker',
'wiki' => 'egw_wiki_pages',
'mydms' => 'phpgw_mydms_Documents',
);
if (($table = $app2table[$app]))
{
try {
$entries = (int)$GLOBALS['egw']->db->query('SELECT COUNT(*) FROM '.$table)->fetchColumn();
//echo "$app ($table): $entries<br />\n";
}
catch(egw_exception_db $e) {
$entries = null;
}
}
return $entries;
}
/**
* Check if next submission is due, in which case we call submit and NOT return to the admin hook
*
*/
public static function check()
{
if (isset($_GET['statistics']))
{
return;
}
$config = config::read(self::CONFIG_APP);
if (isset($config[self::CONFIG_POSTPONE_SUBMIT]) && $config[self::CONFIG_POSTPONE_SUBMIT] > time() ||
isset($config[self::CONFIG_LAST_SUBMIT ]) && $config[self::CONFIG_LAST_SUBMIT ] > time()-self::SUBMISION_RATE)
{
return;
}
//die('Due for new statistics submission: last_submit='.$config[self::CONFIG_LAST_SUBMIT ].', postpone='.$config[self::CONFIG_POSTPONE_SUBMIT].', '.function_backtrace());
egw::redirect_link('/index.php',array('menuaction'=>'admin.admin_statistics.submit'));
}
}

View File

@ -0,0 +1,213 @@
<?php
/**
* eGgroupWare admin - Timed Asynchron Services
*
* @link http://www.egroupware.org
* @author Ralf Becker <RalfBecker-AT-outdoor-training.de>
* @package admin
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
* @version $Id$
*/
/**
* Class to admin cron-job like timed calls of eGroupWare methods
*/
class uiasyncservice
{
var $public_functions = array(
'index' => True,
);
function index()
{
if ($GLOBALS['egw']->acl->check('asyncservice_access',1,'admin'))
{
$GLOBALS['egw']->redirect_link('/index.php');
}
$GLOBALS['egw_info']['flags']['app_header'] = lang('Admin').' - '.lang('Asynchronous timed services');
$GLOBALS['egw']->js->validate_file('jscode','openwindow','admin');
$GLOBALS['egw']->common->egw_header();
echo parse_navbar();
$async = $GLOBALS['egw']->asyncservice; // use an own instance, as we might set debug=True
$async->debug = !!$_POST['debug'];
$units = array(
'year' => lang('Year'),
'month' => lang('Month'),
'day' => lang('Day'),
'dow' => lang('Day of week<br>(0-6, 0=Sun)'),
'hour' => lang('Hour<br>(0-23)'),
'min' => lang('Minute')
);
if ($_POST['send'] || $_POST['test'] || $_POST['cancel'] || $_POST['install'] || $_POST['deinstall'] || $_POST['update'] || isset($_POST['asyncservice']))
{
$times = array();
foreach($units as $u => $ulabel)
{
if ($_POST[$u] !== '')
{
$times[$u] = $_POST[$u];
}
}
if ($_POST['test'])
{
if (strpos($GLOBALS['egw_info']['user']['email'],'@') === false)
{
echo '<p><b>'.lang("You have no email address for your user set !!!")."</b></p>\n";
}
elseif (!$async->set_timer($times,'test','admin.uiasyncservice.test',$GLOBALS['egw_info']['user']['email']))
{
echo '<p><b>'.lang("Error setting timer, wrong syntax or maybe there's one already running !!!")."</b></p>\n";
}
}
if ($_POST['cancel'])
{
if (!$async->cancel_timer('test'))
{
echo '<p><b>'.lang("Error canceling timer, maybe there's none set !!!")."</b></p>\n";
}
}
if ($_POST['install'] || $_POST['deinstall'])
{
if (!($install = $async->install($_POST['install'] ? $times : False)))
{
echo '<p><b>'.lang('Error: %1 not found or other error !!!',$async->crontab)."</b></p>\n";
}
$_POST['asyncservice'] = $_POST['deinstall'] ? 'fallback' : 'crontab';
}
}
else
{
$times = array('min' => '*/5'); // set some default
}
echo '<form action="'.$GLOBALS['egw']->link('/index.php',array('menuaction'=>'admin.uiasyncservice.index')).'" method="POST">'."\n<p>";
echo '<div style="text-align: left; margin: 10px;">'."\n";
$last_run = $async->last_check_run();
$lr_date = $last_run['end'] ? $GLOBALS['egw']->common->show_date($last_run['end']) : lang('never');
echo '<p><b>'.lang('Async services last executed').'</b>: '.$lr_date.' ('.$last_run['run_by'].")</p>\n<hr>\n";
if (isset($_POST['asyncservice']) && $_POST['asyncservice'] != $GLOBALS['egw_info']['server']['asyncservice'])
{
$config =& CreateObject('phpgwapi.config','phpgwapi');
$config->read_repository();
$config->value('asyncservice',$GLOBALS['egw_info']['server']['asyncservice']=$_POST['asyncservice']);
$config->save_repository();
unset($config);
}
/*
if (!$async->only_fallback)
{
$installed = $async->installed();
if (is_array($installed) && isset($installed['cronline']))
{
*/
$async_use['cron'] = lang('crontab only (recomended)');
/*
}
}
$async_use[''] = lang('fallback (after each pageview)');
$async_use['off'] = lang('disabled (not recomended)');
*/
echo '<p><b>'.lang('Run Asynchronous services').'</b>'.
' <select name="asyncservice" onChange="this.form.submit();">';
foreach ($async_use as $key => $label)
{
$selected = $key == $GLOBALS['egw_info']['server']['asyncservice'] ? ' selected' : '';
echo "<option value=\"$key\"$selected>$label</option>\n";
}
echo "</select>\n";
if (is_array($installed) && isset($installed['cronline']))
{
echo ' &nbsp; <input type="submit" name="deinstall" value="'.lang('Deinstall crontab')."\">\n";
}
echo "</p>\n";
if ($async->only_fallback)
{
echo '<p>'.lang('Under windows you need to install the asyncservice %1manually%2 or use the fallback mode. Fallback means the jobs get only checked after each page-view !!!','<a href="http://www.egroupware.org/wiki/TimedAsyncServicesWindows" target="_blank">','</a>')."</p>\n";
}
else
{
echo '<p>'.lang('Installed crontab').": /etc/cron.d/egroupware\n";
/*
if (is_array($installed) && isset($installed['cronline']))
{
echo "$installed[cronline]</p>";
}
elseif ($installed === 0)
{
echo '<b>'.lang('%1 not found or not executable !!!',$async->crontab)."</b></p>\n";
}
else
{
echo '<b>'.lang('asyncservices not yet installed or other error (%1) !!!',$installed['error'])."</b></p>\n";
}
echo '<p><input type="submit" name="install" value="'.lang('Install crontab')."\">\n".
lang("for the times below (empty values count as '*', all empty = every minute)")."</p>\n";
*/
}
echo "<hr><table border=0><tr>\n";
foreach ($units as $u => $ulabel)
{
echo " <td>$ulabel</td><td><input name=\"$u\" value=\"$times[$u]\" size=5> &nbsp; </td>\n";
}
echo "</tr><tr>\n <td colspan=4>\n";
echo ' <input type="submit" name="send" value="'.lang('Calculate next run').'"></td>'."\n";
echo ' <td colspan="8"><input type="checkbox" name="debug" value="1"'.($_POST['debug'] ? ' checked' : '')."> \n".
lang('Enable debug-messages')."</td>\n</tr></table>\n";
if ($_POST['send'])
{
$next = $async->next_run($times,True);
echo "<p>asyncservice::next_run(";print_r($times);echo")=".($next === False ? 'False':"'$next'=".$GLOBALS['egw']->common->show_date($next))."</p>\n";
}
echo '<hr><p><input type="submit" name="cancel" value="'.lang('Cancel TestJob!')."\"> &nbsp;\n";
echo '<input type="submit" name="test" value="'.lang('Start TestJob!')."\">\n";
echo lang('for the times above')."</p>\n";
echo '<p>'.lang('The TestJob sends you a mail everytime it is called.')."</p>\n";
echo '<hr><p><b>'.lang('Jobs').":</b>\n";
if ($jobs = $async->read('%'))
{
echo "<table border=1>\n<tr>\n<th>Id</th><th>".lang('Next run').'</th><th>'.lang('Times').'</th><th>'.lang('Method').'</th><th>'.lang('Data')."</th><th>".lang('LoginID')."</th></tr>\n";
foreach($jobs as $job)
{
echo "<tr>\n<td>$job[id]</td><td>".$GLOBALS['egw']->common->show_date($job['next'])."</td><td>";
print_r($job['times']);
echo "</td><td>$job[method]</td><td>";
print_r($job['data']);
echo "</td><td align=\"center\">".$GLOBALS['egw']->accounts->id2name($job[account_id])."</td></tr>\n";
}
echo "</table>\n";
}
else
{
echo lang('No jobs in the database !!!')."</p>\n";
}
echo '<p><input type="submit" name="update" value="'.lang('Update').'"></p>'."\n";
echo "</form>\n";
}
function test($to)
{
$returncode = $GLOBALS['egw']->send->msg('email',$to,$subject='Asynchronous timed services','Greetings from cron ;-)');
if (!$returncode) // not nice, but better than failing silently
{
echo "<p>bocalendar::send_update: sending message to '$to' subject='$subject' failed !!!<br>\n";
echo $GLOBALS['egw']->send->err['desc']."</p>\n";
}
//print_r($GLOBALS['egw_info']['user']);
}
}