root/drupal/modules/iutilities/trunk/iutilities.module

Revision 26, 7.7 kB (checked in by matthew, 2 years ago)

New functionality for tracking hosting contracts is under development

  • Property svn:mergeinfo set to
Line 
1 <?php
2 // $Id$
3
4 function iutilities_perm() {
5         return array('access disk usage information', 'access disk usage settings');
6 } // function illuminate_perm()
7
8 function iutilities_admin() {
9         $form['i_general'] = array(
10                 '#type' => 'fieldset',
11                 '#title' => 'General Settings',
12                 '#description' => 'The settings apply to all the utilities below.',
13         );
14         $form['i_general']['email_warnings_role'] = array(
15                 '#type' => 'textfield',
16                 '#title' => t('Role to receive warning emails'),
17                 '#default_value' => variable_get('email_warnings_role', 'site administrator'),
18                 '#size' => 20,
19                 '#maxlength' => 25,
20                 '#description' => t("The role entered here here will receive warning messages by email."),
21                 '#required' => TRUE,
22         );
23         $form['i_diskspace'] = array(
24                 '#type' => 'fieldset',
25                 '#title' => 'Disk Space Usage Settings',
26                 '#description' => 'These settings apply to disk usage on this site.',
27         );
28         $form['i_diskspace']['disk_space_limit'] = array(
29                 '#type' => 'textfield',
30                 '#title' => t('Maximum space allowed'),
31                 '#default_value' => variable_get('disk_space_limit', 150),
32                 '#size' => 5,
33                 '#maxlength' => 4,
34                 '#field_suffix' => t('MB'),
35                 '#description' => t("Each illuminate ict website is permitted a certain amount of space. By default this is 150MB. This can be increased here."),
36                 '#required' => TRUE,
37         );
38         $form['i_hostingdate'] = array(
39                 '#type' => 'fieldset',
40                 '#title' => 'Hosting date settings.',
41                 '#description' => 'These settings apply to the length and expiry of hosting contracts.',
42         );
43         $next_year = time() + 31556926;
44         $now = time();
45         $expiry_date = variable_get('hosting_expiry_date', $next_year);
46         $start_date = variable_get('hosting_start_date',$now);
47         $form['i_hostingdate']['hosting_start_date'] = array(
48                 '#type' => 'date',
49                 '#title' => t('Hosting start date.'),
50                 '#default_value' => array(
51                         'month' => format_date($start_date, 'custom', 'n'),
52                         'day' => format_date($start_date, 'custom', 'j'),
53                         'year' => format_date($start_date, 'custom', 'Y'),
54                 ),
55                 '#description' => t("Hosting for this site began on the date here. This should not be changed once set."),
56                 '#required' => TRUE,
57         );
58         $form['i_hostingdate']['hosting_expiry_date'] = array(
59                 '#type' => 'date',
60                 '#title' => t('Hosting expiry date.'),
61                 '#default_value' => array(
62                         'month' => format_date($expiry_date, 'custom', 'n'),
63                         'day' => format_date($expiry_date, 'custom', 'j'),
64                         'year' => format_date($expiry_date, 'custom', 'Y'),
65                 ),
66                 '#description' => t("Hosting for this site expires on the date here. Users with the role defined above will recieve reminder emails when the renewal date approaches"),
67                 '#required' => TRUE,
68         );
69         $hosting_term_period = drupal_map_assoc(
70                 array(
71                         31536000,
72                         2*31536000,
73                         3*31536000,
74                         4*31536000,
75                         5*31536000,
76                         6*31536000,
77                         7*31536000,
78                         8*31536000,
79                         9*31536000,
80                         10*31536000,
81                         11*31536000,
82                         12*31536000
83                 ), 'format_interval'
84         );
85         $form['i_hostingdate']['hosting_term'] = array(
86                 '#type' => 'select',
87                 '#title' => t('Web hosting term'),
88                 '#default_value' => variable_get('hosting_term', 31536000),
89                 '#options' => $hosting_term_period,
90                 '#description' => t(""),
91                 '#required' => TRUE,
92         );
93        
94         return system_settings_form($form);
95 }
96
97 function iutilities_cron() {
98         $confpath = conf_path();
99         $space_used_string = `du -sm $confpath`;
100         $space_used_array = explode("\t", $space_used_string);
101         $space_used = (int) trim($space_used_array[0]);
102         variable_set('disk_space_used', $space_used);   
103         $disk_space_limit = (int) variable_get('disk_space_limit',150);
104         //how long since a warning email was sent??
105         $time_now = (int) time();
106         $time_last_email = (int) variable_get('disk_space_last_emailed',0);
107         $time_since_email = (int) $time_now - $time_last_email;
108         //email if space allowance has been exceeded
109         // but only email if not yet emailed
110         // or it's more than a week since an email was sent
111         if ( $space_used > $disk_space_limit && ($time_last_email==0 || $time_since_email > 604799) ) {
112                 $site_default_language = language_default();
113                 $email_warnings_role = variable_get('email_warnings_role', 'site administrator');
114                 $users_with_role_query = "select u.mail from {users} u inner join {users_roles} ur on u.uid=ur.uid inner join {role} r on r.rid=ur.rid where r.name='%s'";
115                 $result = db_query(db_rewrite_sql($users_with_role_query),$email_warnings_role);
116                 while ($data = db_fetch_object($result)) {
117                         drupal_mail('iutilities', 'warning', $data->mail, $site_default_language, $params);
118                 }
119                 //set a variable which notes that an email has been sent.
120                 $time_stamp = (int) time();
121                 variable_set('disk_space_last_emailed',$time_stamp);
122         } elseif ($space_used <= $disk_space_limit) {
123                 variable_set('disk_space_last_emailed',0);
124         }
125 }
126
127 function iutilities_mail($key, &$message, $params) {
128         global $base_url;
129         $space_used = variable_get('disk_space_used',150);
130         $space_limit = variable_get('disk_space_limit',150);
131         switch($key) {
132         case 'warning':
133                 $message['subject'] = t('Space usage warning from ' . $base_url);
134                 $message['body'] = t("An information message from your website host, ILLUMINATE ICT\n------------------------------------------------\n\nYou are listed as being a site administrator for $base_url.\n\nThis email is to inform you that you are currently exceeding your space allocation on our webserver. You have been allocated " . $space_limit . "MB and you are using " . $space_used . "MB.\n\nWe are not about to discontinue your service, but we would appreciate it if you would get in touch to discuss how we could help you. Contact us at http://www.illuminateict.org.uk.\n\nYou will receive this email weekly until we can help you to reduce the amount of space you are using, or increase your space allowance on the server.");
135                 break;
136         }
137 }
138
139 function iutilities_menu() {
140         $items['admin/reports/diskusage'] = array(
141                 'title' => 'Disk Usage Report',
142                 'description' => 'Dispay a disk usage report for this website. Includes the space limit for the site.',
143                 'access arguments' => array('access disk usage information'),
144                 'page callback' => 'display_diskusage_report',
145         );
146         $items['admin/settings/illuminate'] = array(
147                 'title' => 'Set Hosting Defaults',
148                 'description' => 'Hosting defaults can be changed here.',
149                 'access callback' => 'access_disk_usage_settings',
150                 'access arguments' => array('access disk usage settings'),
151                 'page callback' => 'drupal_get_form',
152                 'page arguments' => array(iutilities_admin),
153         );
154         return $items;
155 } // illuminate_menu()
156
157 function access_disk_usage_settings() {
158         global $user;
159         if ( $user->uid == 1 ) {
160                 return TRUE;
161         } else {
162                 return FALSE;
163         }
164 }
165
166 function display_diskusage_report() {
167         $space_used = variable_get('disk_space_used',150);
168         $space_limit = variable_get('disk_space_limit',150);
169         $space_percentage = ( $space_used / $space_limit ) * 100;
170         $output = '<p>Your hosting account with illuminate ICT allows a maximum of <strong>'. $space_limit . 'MB</strong>.</p>';
171         $output .= '<p>You are currently using <strong>' . $space_used . 'MB</strong> of your allowance.</p>';
172         $output .= '<p>This is <strong>' . $space_percentage . '&#37;</strong> of the available space.</p>';
173         return $output;
174 }
175
176
177 function iutilities_init() {
178   //include necessary css
179   global $user;
180   if ($user->uid > 0) {
181     $path = drupal_get_path('module', 'illuminate');
182     drupal_add_css($path . '/iutilities.css', 'module', 'all', FALSE);
183   }
184 }
185
186 function iutilities_footer($main=0) {
187   //output footer message
188   global $user;
189
190   $siteDir =   "sites/" . $_SERVER[SERVER_NAME];
191   $path = drupal_get_path('module', 'illuminate');
192   //footer message is available for all users
193   if ($user->uid == 0) {
194     $loginLink = '<a href="/user/login">[login]</a>';
195   } else {
196     $loginLink = '<a href="/logout">[logout]</a>';
197   }
198   $illuminateStuff =  $loginLink . ' <br/>Website developed in association with <a href="http://www.illuminateict.org.uk">Illuminate ICT</a>';
199  
200   return $illuminateStuff;
201 }
Note: See TracBrowser for help on using the browser.