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

Revision 29, 10.4 kB (checked in by matthew, 2 years ago)

emailing when contract is due to expire is now almost working.
currently, it will mail on each cron run within 1 month of expiry date. this needs some tweaking.

  • 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',
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_general']['illuminate_notes'] = array(
24                 '#type' => 'textarea',
25                 '#title' => 'Notes',
26                 '#default_value' => variable_get('illuminate_notes','Type any notes about this contract in this box.'),
27                 '#description' => 'Any notes relating to this contract can go in here.',
28                 '#rows' => 4,
29                 '#resizable' => TRUE,
30         );
31         $form['i_diskspace'] = array(
32                 '#type' => 'fieldset',
33                 '#title' => 'Disk Space Usage Settings',
34                 '#description' => 'These settings apply to disk usage on this site.',
35         );
36         $form['i_diskspace']['disk_space_limit'] = array(
37                 '#type' => 'textfield',
38                 '#title' => t('Maximum space allowed'),
39                 '#default_value' => variable_get('disk_space_limit', 150),
40                 '#size' => 5,
41                 '#maxlength' => 4,
42                 '#field_suffix' => t('MB'),
43                 '#description' => t("Each illuminate ict website is permitted a certain amount of space. By default this is 150MB. This can be increased here."),
44                 '#required' => TRUE,
45         );
46         $form['i_hostingdate'] = array(
47                 '#type' => 'fieldset',
48                 '#title' => 'Hosting date settings.',
49                 '#description' => 'These settings apply to the length and expiry of hosting contracts.',
50         );
51         $next_year = time() + 31556926;
52         $default_expiry = variable_get('hosting_expiry_date', $next_year);
53         if (!is_array($default_expiry)) {
54                 $default_expiry = array(
55                         'month' => format_date($default_expiry, 'custom', 'n'),
56                         'day' => format_date($default_expiry, 'custom', 'j'),
57                         'year' => format_date($default_expiry, 'custom', 'Y'),
58                 );
59         }       
60         $now = time();
61         $default_start = variable_get('hosting_start_date',$now);
62         if (!is_array($default_start)) {
63                 $default_start = array(
64                         'month' => format_date($default_start, 'custom', 'n'),
65                         'day' => format_date($default_start, 'custom', 'j'),
66                         'year' => format_date($default_start, 'custom', 'Y'),
67                 );
68         }       
69         $form['i_hostingdate']['hosting_start_date'] = array(
70                 '#type' => 'date',
71                 '#title' => t('Hosting start date'),
72                 '#default_value' => $default_start,
73                 '#description' => t("Hosting for this site began on the date here. This should not be changed once set."),
74                 '#required' => TRUE,
75         );
76         $form['i_hostingdate']['hosting_expiry_date'] = array(
77                 '#type' => 'date',
78                 '#title' => t('Hosting expiry date'),
79                 '#default_value' => $default_expiry,
80                 '#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"),
81                 '#required' => TRUE,
82         );
83         $hosting_term_period = drupal_map_assoc(
84                 array(
85                         31536000,
86                         2*31536000,
87                         3*31536000,
88                         4*31536000,
89                         5*31536000,
90                         6*31536000,
91                         7*31536000,
92                         8*31536000,
93                         9*31536000,
94                         10*31536000,
95                         11*31536000,
96                         12*31536000
97                 ), 'format_interval'
98         );
99         $form['i_hostingdate']['hosting_term'] = array(
100                 '#type' => 'select',
101                 '#title' => t('Web hosting term'),
102                 '#default_value' => variable_get('hosting_term', 31536000),
103                 '#options' => $hosting_term_period,
104                 '#description' => t(""),
105                 '#required' => TRUE,
106         );
107         $form['i_hostingcost'] = array(
108                 '#type' => 'fieldset',
109                 '#title' => 'Hosting cost settings',
110                 '#description' => 'These settings apply to the cost of hosting contracts.',
111         );
112         $form['i_hostingcost']['hosting_cost'] = array(
113                 '#type' => 'textfield',
114                 '#title' => t('Yearly hosting costs'),
115                 '#default_value' => variable_get('hosting_cost', 240),
116                 '#size' => 5,
117                 '#maxlength' => 4,
118                 '#field_prefix' => t('&pound;'),
119                 '#description' => t("This is the yearly cost of hosting for this website."),
120                 '#required' => TRUE,
121         );
122        
123         return system_settings_form($form);
124 }
125
126 function iutilities_cron() {
127         $confpath = conf_path();
128        
129         //deal with space usage things
130         $space_used_string = `du -sm $confpath`;
131         $space_used_array = explode("\t", $space_used_string);
132         $space_used = (int) trim($space_used_array[0]);
133         variable_set('disk_space_used', $space_used);   
134         $disk_space_limit = (int) variable_get('disk_space_limit',150);
135         //how long since a warning email was sent??
136         $time_now = (int) time();
137         $time_last_email = (int) variable_get('disk_space_last_emailed',0);
138         $time_since_email = (int) $time_now - $time_last_email;
139         //email if space allowance has been exceeded
140         // but only email if not yet emailed
141         // or it's more than a week since an email was sent
142         if ( $space_used > $disk_space_limit && ($time_last_email==0 || $time_since_email > 604799) ) {
143                 $site_default_language = language_default();
144                 $email_warnings_role = variable_get('email_warnings_role', 'site administrator');
145                 $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'";
146                 $result = db_query(db_rewrite_sql($users_with_role_query),$email_warnings_role);
147                 while ($data = db_fetch_object($result)) {
148                         drupal_mail('iutilities', 'warning', $data->mail, $site_default_language, $params);
149                 }
150                 //set a variable which notes that an email has been sent.
151                 $time_stamp = (int) time();
152                 variable_set('disk_space_last_emailed',$time_stamp);
153         } elseif ($space_used <= $disk_space_limit) {
154                 variable_set('disk_space_last_emailed',0);
155         }
156        
157         //deal with hosting expiry things
158         //get the hosting expiry date & convert to UNIX time
159         $expiry_date = variable_get('hosting_expiry_date', time());
160         if ( is_array($expiry_date) ) {
161                 $expiry_date_timestamp = mktime(0,0,0,$expiry_date['month'],$expiry_date['day'],$expiry_date['year']);
162                 $warnings_start_date = $expiry_date_timestamp - 2592000;
163                 //warning messages will begin a month before contract expiry - 2592000s = 1 month.
164                 $now = time();
165                 if ( $warnings_start_date < $now ) {
166                         //do the sending email thing, no more than once a week.
167                         $site_default_language = language_default();
168                         $email_warnings_role = variable_get('email_warnings_role', 'site administrator');
169                         $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'";
170                         $result = db_query(db_rewrite_sql($users_with_role_query),$email_warnings_role);
171                         while ($data = db_fetch_object($result)) {
172                                 drupal_mail('iutilities', 'hosting_expiry', $data->mail, $site_default_language, $params);
173                         }
174                         //set a variable which notes that an email has been sent.
175                         variable_set('hosting_expiry_last_emailed',$now);
176                 } elseif ( $warnings_start_date >= $now ) {
177                         variable_set('hosting_expiry_last_emailed',0);
178                 }
179         }
180        
181 }
182
183 function iutilities_mail($key, &$message, $params) {
184         global $base_url;
185         $space_used = variable_get('disk_space_used',150);
186         $space_limit = variable_get('disk_space_limit',150);
187         switch($key) {
188         case 'warning':
189                 $message['subject'] = t('Space usage warning from ' . $base_url);
190                 $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.");
191                 break;
192         case 'hosting_expiry':
193                 $message['subject'] = t('Hosting expiry warning from ' . $base_url);
194                 $message['body'] = t("You are about to run out of time.");
195         }
196 }
197
198 function iutilities_menu() {
199         $items['admin/reports/illuminate'] = array(
200                 'title' => 'Illuminate Hosting Report',
201                 'description' => 'Dispay a hosting report for this website. Includes the space limit for the site and other contract information.',
202                 'access arguments' => array('access disk usage information'),
203                 'page callback' => 'display_diskusage_report',
204         );
205         $items['admin/settings/illuminate'] = array(
206                 'title' => 'Set Hosting Defaults',
207                 'description' => 'Hosting defaults can be changed here.',
208                 'access callback' => 'access_disk_usage_settings',
209                 'access arguments' => array('access disk usage settings'),
210                 'page callback' => 'drupal_get_form',
211                 'page arguments' => array(iutilities_admin),
212         );
213         return $items;
214 } // illuminate_menu()
215
216 function access_disk_usage_settings() {
217         global $user;
218         if ( $user->uid == 1 ) {
219                 return TRUE;
220         } else {
221                 return FALSE;
222         }
223 }
224
225 function display_diskusage_report() {
226         $space_used = variable_get('disk_space_used',150);
227         $space_limit = variable_get('disk_space_limit',150);
228         $space_percentage = ( $space_used / $space_limit ) * 100;
229         $output = '<p>Your hosting account with illuminate ICT allows a maximum of <strong>'. $space_limit . 'MB</strong>.</p>';
230         $output .= '<p>You are currently using <strong>' . $space_used . 'MB</strong> of your allowance.</p>';
231         $output .= '<p>This is <strong>' . $space_percentage . '&#37;</strong> of the available space.</p>';
232         return $output;
233 }
234
235
236 function iutilities_init() {
237   //include necessary css
238   global $user;
239   if ($user->uid > 0) {
240     $path = drupal_get_path('module', 'illuminate');
241     drupal_add_css($path . '/iutilities.css', 'module', 'all', FALSE);
242   }
243 }
244
245 function iutilities_footer($main=0) {
246   //output footer message
247   global $user;
248
249   $siteDir =   "sites/" . $_SERVER[SERVER_NAME];
250   $path = drupal_get_path('module', 'illuminate');
251   //footer message is available for all users
252   if ($user->uid == 0) {
253     $loginLink = '<a href="/user/login">[login]</a>';
254   } else {
255     $loginLink = '<a href="/logout">[logout]</a>';
256   }
257   $illuminateStuff =  $loginLink . ' <br/>Website developed in association with <a href="http://www.illuminateict.org.uk">Illuminate ICT</a>';
258  
259   return $illuminateStuff;
260 }
Note: See TracBrowser for help on using the browser.