-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpmtabounce.php
More file actions
258 lines (234 loc) · 7.7 KB
/
pmtabounce.php
File metadata and controls
258 lines (234 loc) · 7.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<?php
/**
* This file contains the 'pmtabounce' addon.
*
* @package Interspire_Addons
* @subpackage Addons_pmtabounce
*/
/**
* Make sure the base Interspire_Addons class is defined.
*/
if (!class_exists('Interspire_Addons', false)) {
require_once(dirname(dirname(__FILE__)) . '/interspire_addons.php');
}
/**
* Load language definitions in global context.
*/
require_once (dirname(__FILE__) . '/language/language.php');
/**
* This class implements the addon callbacks.
*
* @uses Interspire_Addons
* @uses Interspire_Addons_Exception
*/
class Addons_pmtabounce extends Interspire_Addons
{
protected $default_settings = array(
'directory' => '/var/log/pmta/bounces',
'delete_after' => false);
/**
* Install
* This is called when the addon is installed in the main application.
* In this case, it simply sets the default settings and then calls the parent install method to add itself to the database.
*
* @uses default_settings
* @uses Interspire_Addons::Install
* @uses Interspire_Addons_Exception
*
* @throws Throws an Interspire_Addons_Exception if something goes wrong with the install process.
* @return True Returns true if all goes ok with the install.
*/
public function Install()
{
/* check if language file is loaded */
if (!defined('LNG_Addon_' . $this->GetId() . '_IsLoaded')) {
throw new Exception('Language file for ' . $this->GetId() . ' not loaded');
}
/* remove old event from persistent storage
InterspireEvent::listenerUnregister('IEM_CRON_RUNADDONS',
array ('Addons_pmtabounce', 'Cron_GetJobs'),
'{%IEM_ADDONS_PATH%}/pmtabounce/pmtabounce.php');
*/
$this->enabled = true;
$this->configured = true;
$this->settings = $this->default_settings;
/* store default settings in database if not done already
$settings = array_merge($this->default_settings, self::GetSettings()); */
/* self::SetSettings($this->default_settings); */
try {
$status = parent::Install();
}
catch (Interspire_Addons_Exception $e) {
throw new Exception('Unable to install addon ' . $this->GetId() . ': ' . $e->getMessage());
}
/* show message in error log in application */
trigger_error('Installed ' . $this->GetId() . ' addon', E_USER_NOTICE);
trigger_error('Settings: ' . implode(', ', array_keys($this->settings)), E_USER_NOTICE);
return true;
}
/**
* GetEventListeners
* The addon uses a few events to place itself in the app and allow it to work.
*
* IEM_SETTINGSAPI_LOADSETTINGS
* Adds new options to the settings for cron
*
* IEM_CRON_RUNADDONS
* Adds itself to the list of addons that can have cron jobs
*
* Events are persisted in admin/com/storage/iem_stash_storage.php and must be removed with
* InterspireEvent::listenerUnregister() to deactivate the event.
*
* @return Array Returns an array containing the events to listen to.
*/
public function GetEventListeners()
{
return
array (
array (
'eventname' => 'IEM_SETTINGSAPI_LOADSETTINGS',
'trigger_details' => array (
'Addons_pmtabounce',
'LoadSettings'
),
'trigger_file' => '{%IEM_ADDONS_PATH%}/pmtabounce/pmtabounce.php'
),
array (
'eventname' => 'IEM_CRON_RUNADDONS',
'trigger_details' => 'Pmtabounce_Cron_GetJobs',
'trigger_file' => '{%IEM_ADDONS_PATH%}/pmtabounce/cron.php'
)
);
}
/**
* LoadSettings
* Adds new options to the "cron settings" page and settings database table.
* Sets the "last run time" for the job to -1 which means "hasn't run".
*
* Adds a new settings entry called "CRON_PMTABOUNCE" to the settings table.
* Also adds the following times to the "run job every" dropdown box:
* - 1 minute
* - 2, 5, 10, 15, 20, 30 minutes
*
* @param EventData_IEM_SETTINGSAPI_LOADSETTINGS $data The current settings data which is passed in by reference (is an object).
*
* @uses EventData_IEM_SETTINGSAPI_LOADSETTINGS
*/
public static function LoadSettings(EventData_IEM_SETTINGSAPI_LOADSETTINGS $data)
{
$data->data->Schedule['pmtabounce'] = array (
'lastrun' => -1,
);
$data->data->pmtabounce_options = array (
'0' => 'disabled',
'1' => '1_minute',
'5' => '5_minutes',
'15' => '15_minutes',
'60' => '1_hour',
'240' => '4_hours',
'720' => '12_hours',
'1440' => '1_day',
);
$data->data->Areas[] = 'CRON_PMTABOUNCE';
}
/**
* Configure
* This method is called when the addon needs to be configured.
* It uses the templates/settings.tpl file to show its current settings and display the settings form.
*
* @uses settings
* @uses template_system
* @uses InterspireTemplate::Assign
* @uses InterspireTemplate::ParseTemplate
*
* @return String Returns the settings form with the current settings pre-filled.
*/
public function Configure()
{
/* load settings from database
$settings = array_merge($this->default_settings, self::GetSettings());
*/
foreach ($this->settings as $k => $v) {
$this->template_system->Assign($k, $v);
}
$this->template_system->Assign('SettingsUrl', $this->settings_url, false);
$this->template_system->Assign('ApplicationUrl', $this->application_url, false);
return $this->template_system->ParseTemplate('settings', true);
}
/**
* SaveSettings
* This is called when the settings form is submitted.
* It checks if any values were posted.
* It then checks against the settings it should find (from default_settings) to make sure you're not trying to sneak any extra settings in there
*
* If no form was posted or if you post invalid options, this will return false (which then displays an error message).
*
* @see Configure
* @uses default_settings
* @uses db
*
* @return Boolean Returns false if an invalid settings form is posted or if
*/
public function SaveSettings()
{
/* reject unexpected parameters */
$settings = array_intersect_key($_POST, $this->default_settings);
if (empty($settings)) {
return false;
}
/* set to false if checkbox unset */
if (!isset($settings['delete_after'])) {
$settings['delete_after'] = false;
}
return self::SetSettings($settings);
}
/**
* GetSettings
* Retrieves the saved settings from the database.
*
* @see Configure
* @uses db
*
* @return Array The saved settings.
*/
public static function GetSettings()
{
$db = IEM::getDatabase();
if (!$db) {
trigger_error("Could not get database handle", E_USER_ERROR);
return array();
}
$id = str_replace('Addons_', '', __CLASS__);
$data = $db->FetchOne("SELECT settings FROM [|PREFIX|]addons WHERE addon_id='{$id}'");
if (!$data) {
trigger_error("Could not fetch settings from database", E_USER_ERROR);
return array();
}
$settings = unserialize($data);
return $settings;
}
/**
* SetSettings
* Saves the settings to the database.
*
* @uses db
*
* @param Array $settings The settings to save for this addon.
*
* @return Boolean True if saved successfully, otherwise false.
*/
public static function SetSettings($settings)
{
$db = IEM::getDatabase();
if (!$db) {
trigger_error("Could not get database handle", E_USER_ERROR);
return false;
}
$id = str_replace('Addons_', '', __CLASS__);
$result = $db->Query("UPDATE [|PREFIX|]addons SET configured=1, settings='" . $db->Quote(serialize($settings)) . "' WHERE addon_id='{$id}'");
if (!$result) {
trigger_error("Could not update settings to database", E_USER_ERROR);
}
return (bool)$result;
}
}