Passed
Push — email-analytics ( 2008c3...7d1e14 )
by Ilia
08:22
created

start.php ➔ count_emails()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
elgg_register_event_handler('init','system','cp_notifications_init');
4
 
5
6
function cp_notifications_init() {
7
8
	elgg_register_library('elgg:gc_notification:functions', elgg_get_plugins_path() . 'cp_notifications/lib/functions.php');
9
	elgg_register_css('cp_notifications-css','mod/cp_notifications/css/notifications-table.css');
10
11
	// hide the irrelavent view module from the group
12
	elgg_unextend_view("groups/edit", "group_tools/forms/notifications");
13
14
	elgg_register_plugin_hook_handler('register', 'menu:entity', 'notify_entity_menu_setup', 400);
15
	// since most of the notifications are built within the action file itself, the trigger_plugin_hook was added to respected plugins
16
	elgg_register_plugin_hook_handler('cp_overwrite_notification', 'all', 'cp_overwrite_notification_hook');
17
	elgg_register_plugin_hook_handler('cron', 'daily', 'cp_digest_daily_cron_handler');
18
	elgg_register_plugin_hook_handler('cron', 'weekly', 'cp_digest_weekly_cron_handler', 100);
19
	// hooks and events: intercepts and blocks emails and notifications to be sent out
20
	elgg_register_plugin_hook_handler('email', 'system', 'cpn_email_handler_hook');
21
22
23
	$action_base = elgg_get_plugins_path() . 'cp_notifications/actions/cp_notifications';
24
	elgg_register_action('cp_notify/subscribe', "$action_base/subscribe.php");
25
	elgg_register_action('cp_notify/unsubscribe', "$action_base/unsubscribe.php");
26
	elgg_register_action('user/requestnewpassword', "$action_base/request_new_password.php", 'public');
27
	elgg_register_action('cp_notifications/set_personal_subscription', "$action_base/set_personal_subscription.php");
28
	elgg_register_action('cp_notifications/reset_personal_subscription', "$action_base/reset_personal_subscription.php");
29
    elgg_register_action('cp_notifications/subscribe_users_to_group_content',"$action_base/subscribe_users_to_group_content.php");
30
    elgg_register_action('cp_notifications/undo_subscribe_users_to_group_content',"$action_base/undo_subscribe_users_to_group_content.php");
31
    elgg_register_action('cp_notifications/usersettings/save', elgg_get_plugins_path() . 'cp_notifications/actions/usersettings/save.php');
32
	elgg_register_action('cp_notifications/user_autosubscription',"{$action_base}/user_autosubscription.php");
33
	elgg_register_action('cp_notifications/fix_inconsistent_subscription_script',"{$action_base}/fix_inconsistent_subscription_script.php");
34
  
35
  	elgg_register_action('cp_notifications/fix_forums_subscription',"$actions_base/fix_forums_subscription.php");
0 ignored issues
show
Bug introduced by
The variable $actions_base does not exist. Did you mean $action_base?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
36
  
37
	elgg_register_action('useradd',"$action_base/useradd.php",'admin'); // actions/useradd.php (core file)
38
39
	// this plugin must now be placed after "group_tools" plugin
40
	elgg_register_action('group_tools/mail', "{$action_base}/group_mail.php"); 
41
42
43
	// Ajax action files
44
	elgg_register_action('cp_notify/retrieve_group_contents', elgg_get_plugins_path().'cp_notifications/actions/ajax_usersettings/retrieve_group_contents.php'); 
45
	elgg_register_action('cp_notify/retrieve_personal_content', elgg_get_plugins_path().'cp_notifications/actions/ajax_usersettings/retrieve_personal_content.php'); 
46
	elgg_register_action('cp_notify/retrieve_pt_users', elgg_get_plugins_path().'cp_notifications/actions/ajax_usersettings/retrieve_pt_users.php'); 
47
	elgg_register_action('cp_notify/retrieve_messages', elgg_get_plugins_path().'cp_notifications/actions/ajax_usersettings/retrieve_messages.php'); 
48
	elgg_register_action('cp_notify/retrieve_user_info', elgg_get_plugins_path().'cp_notifications/actions/ajax_settings/retrieve_user_info.php'); 
49
50
	// send notifications when the action is sent out
51
	elgg_register_event_handler('create','object','cp_create_notification',900);
52
	elgg_register_event_handler('single_file_upload', 'object', 'cp_create_notification');
53
	elgg_register_event_handler('single_zip_file_upload', 'object', 'cp_create_notification');
54
	elgg_register_event_handler('multi_file_upload', 'object', 'cp_create_notification');
55
56
	elgg_register_event_handler('create','annotation','cp_create_annotation_notification');
57
	elgg_register_event_handler('create', 'membership_request', 'cp_membership_request');
58
59
	// we need to check if the mention plugin is installed and activated because it does notifications differently...
60
	if (elgg_is_active_plugin('mentions')) {
61
		elgg_unregister_event_handler('create', 'object','mentions_notification_handler');
62
		elgg_unregister_event_handler('update', 'annotation','mentions_notification_handler');
63
	}
64
65
    elgg_extend_view("js/elgg", "js/notification"); 
66
    elgg_extend_view("js/elgg", "js/popup");
67
    elgg_extend_view("js/elgg","js/wet4/language_ajax");
68
69
    // remove core notification settings portion of the main settings page
70
    elgg_unextend_view('forms/account/settings', 'core/settings/account/notifications');
71
72
73
	/// "minor save" for contents within groups (basically put the option for all the forms, then filter via URL)
74
	$group_entity = elgg_get_page_owner_entity();
75
	$current_user = elgg_get_logged_in_user_entity();
76
77
78
	if (elgg_is_active_plugin('group_operators'))
79
		elgg_load_library('elgg:group_operators');
80
81
	if ($group_entity instanceof ElggGroup) {
82
83
	// TODO: check to make sure that get_group_operators() is available
84
85
	if (elgg_is_logged_in() && (in_array($current_user, get_group_operators($group_entity)) || elgg_is_admin_user($current_user->getGUID()))) {
86
87
88
			$url = str_replace(elgg_get_site_url(),"", $_SERVER['REQUEST_URI']);
89
			if (strpos($url,'edit') == false) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing strpos($url, 'edit') of type integer to the boolean false. If you are specifically checking for 0, consider using something more explicit like === 0 instead.
Loading history...
90
91
				$plugin_list = elgg_get_plugins('active', 1);
92
				foreach ($plugin_list as $plugin_form)
93
				{
94
					$filepath = elgg_get_plugins_path().$plugin_form['title'].'/views/default/forms/'.$plugin_form['title'];
95
					if (file_exists($filepath))
96
					{
97
						$dir = scandir($filepath);
98
						foreach ($dir as $form_file)
99
						{
100
							if ((strpos($form_file,'save') !== false || strpos($form_file, 'upload') !== false) && (!strstr($form_file, '.old')))
101
							{
102
								$remove_php = explode('.',$form_file);
103
								elgg_extend_view('forms/'.$plugin_form['title'].'/'.$remove_php[0], 'forms/minor_save', 500);
104
							}
105
						}
106
					}
107
				}
108
				
109
				elgg_extend_view('forms/photos/image/save', 'forms/minor_save', 500);
110
				elgg_extend_view('forms/photos/album/save', 'forms/minor_save', 500);
111
				elgg_extend_view('forms/discussion/save', 'forms/minor_save', 500);
112
			}
113
		}
114
	}
115
116
	$subtype_array = array('blog', 'bookmarks', 'discussion');
117
	foreach ($subtype_array as $subtype) 
118
		elgg_register_plugin_hook_handler('action', $subtype.'/save', 'minor_save_hook_handler', 300);
119
	
120
	$subtype_array = array('file/upload', 'ideas/saveidea', 'photos/album/save');
121
	foreach ($subtype_array as $subtype) 
122
		elgg_register_plugin_hook_handler('action', $subtype, 'minor_save_hook_handler', 300);	
123
124
}
125
126
127
/**
128
 * catches the minor save, determines whether to cancel or process the event handlers
129
 *
130
 * @param string $hook    The name of the plugin hook
131
 * @param string $type    The type of the plugin hook
132
 * @param mixed  $value   The current value of the plugin hook
133
 * @param mixed  $params  Data passed from the trigger
134
 *
135
 * @return mixed if not null, this will be the new value of the plugin hook
136
 */
137
function minor_save_hook_handler($hook, $type, $value, $params) {
138
139
140
    if (strcmp(get_input('minor_save'), 'yes') === 0) {
141
142
	    elgg_unregister_event_handler('create','object','cp_create_notification', 900);
0 ignored issues
show
Unused Code introduced by
The call to elgg_unregister_event_handler() has too many arguments starting with 900.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
143
		elgg_unregister_event_handler('single_file_upload', 'object', 'cp_create_notification');
144
		elgg_unregister_event_handler('single_zip_file_upload', 'object', 'cp_create_notification');
145
		elgg_unregister_event_handler('multi_file_upload', 'object', 'cp_create_notification');
146
		elgg_unregister_event_handler('create','annotation','cp_create_annotation_notification');
147
148
	}
149
150
    return true;
151
}
152
153
function count_email_types( $type ){
154
	try {
155
		$m = new Memcached();
156
		$m->setOption(Memcached::OPT_BINARY_PROTOCOL, true);
157
		$m->addServer('localhost', 11211);
158
159
		$n = $m->increment("email:$type", 1, 1);
160
	} catch(Exception $err){
161
		error_log("could not increment email counter for $type");
162
	}
163
}
164
165
/**
166
 *
167
 * This contains all the notifications that are required to be triggered from the original action files. Filepaths
168
 * are documented beside each case (and in the readme.md file)
169
 *
170
 * @param string $hook    The name of the plugin hook
171
 * @param string $type    The type of the plugin hook
172
 * @param mixed  $value   The current value of the plugin hook
173
 * @param mixed  $params  Data passed from the trigger
174
 */
175
function cp_overwrite_notification_hook($hook, $type, $value, $params) {
176
177
	elgg_load_library('elgg:gc_notification:functions');
178
	$cp_msg_type = trim($params['cp_msg_type']);
179
	$to_recipients = array();
180
	$email_only = false;
181
	$add_to_sent = false;
182
	$embed_image = NULL;
183
	$sender_guid = elgg_get_site_entity()->guid;
184
185
	count_email_types($cp_msg_type);
186
	switch ($cp_msg_type) {
187
		/// EMAIL NOTIFICATIONS ONLY (password reset, registration, etc)
188
		case 'cp_friend_invite': // invitefriends/actions/invite.php
189
			$message = array(
190
				'cp_msg_type' => $cp_msg_type,
191
				'cp_from_user' => $params['cp_from']->name,
192
				'cp_to_user' => $params['cp_to'],
193
				'cp_join_url' => $params['cp_join_url'],
194
				'cp_msg' => $params['cp_email_msg'],
195
				'_user_e-mail' => $params['cp_to'],
196
			);
197
			$subject = elgg_echo('cp_notify:subject:invite_new_user',array(),'en') . ' | ' . elgg_echo('cp_notify:subject:invite_new_user',array(),'fr');
198
			$template = elgg_view('cp_notifications/email_template', $message);
199
			$site_template = elgg_view('cp_notifications/site_template', $message);
200
			$user_obj = get_user_by_email($params['cp_to']);
201
			$info_notif = 'cp_friend_invite';
202
			$result = (elgg_is_active_plugin('phpmailer')) ? phpmailer_send($params['cp_to'], $params['cp_to'], $subject, $template, NULL,true) : mail($params['cp_to'],$subject,$template,cp_get_headers());
203
			return true;
204
205
206
		case 'cp_forgot_password':	// send email notifications only - /wet4/users/action/password
207
			cp_send_new_password_request($params['cp_password_requester']);
208
			return true;
209
210
		case 'cp_group_invite_email':
211
		case 'cp_group_invite':	// group_tools/lib/functions.php (returns user's email, so return after mail is sent out)
212
213
			$group_name = $params['cp_invite_to_group']['name'];
214
			if (elgg_is_active_plugin('wet4')) {
215
				$group_name_en =  htmlspecialchars(filter_tags(htmlspecialchars_decode(gc_explode_translation($group_name, 'en'))));
216
				$group_name_fr = htmlspecialchars(filter_tags(htmlspecialchars_decode(gc_explode_translation($group_name, 'fr'))));
217
			}
218
219
			$subject = elgg_echo('cp_notify:subject:group_invite_email',array($params['cp_inviter']['name'], $group_name_en),'en') . ' | ' . elgg_echo('cp_notify:subject:group_invite_email',array($params['cp_inviter']['name'], $group_name_fr),'fr');
0 ignored issues
show
Bug introduced by
The variable $group_name_en does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Bug introduced by
The variable $group_name_fr does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
220
			$subject = htmlspecialchars_decode($subject, ENT_QUOTES);
221
222
			$user_email = $params['cp_invitee'];
223
224
225
			$message = array(
226
				'cp_email_invited' => $params['cp_invitee'],
227
				'cp_email_invited_by' => $params['cp_inviter'],
228
				'cp_group_invite' => $params['cp_invite_to_group'],
229
				'cp_invitation_non_user_url' => $params['cp_invitation_nonuser_url'],
230
				'cp_invitation_url' => $params['cp_invitation_url'],
231
				'cp_invitation_code' => $params['cp_invitation_code'],
232
				'cp_invitation_msg' => $params['cp_invite_msg'],
233
				'cp_msg_type' => $cp_msg_type,
234
				'_user_e-mail' => $params['cp_invitee'],
235
				'group_link' => $params['group_link'],
236
				'cp_user_profile' => $params['cp_user_profile'],
237
			);
238
			$template = elgg_view('cp_notifications/email_template', $message);
239
240
			// invitation through email, user might not exist
241
			if ($cp_msg_type === 'cp_group_invite') {
242
				$site_template = elgg_view('cp_notifications/site_template', $message);
243
				if ($params['cp_invitee'] instanceof ElggUser)
244
					$send_to_user = get_user_by_email($params['cp_invitee']);
245
				else 
246
					$send_to_user = $params['cp_invitee'];
247
			} 
248
			$result = (elgg_is_active_plugin('phpmailer')) ? phpmailer_send( $params['cp_invitee']->email, $params['cp_invitee']->name, $subject, $template, NULL, true ) : mail($send_to_user->email, $subject, $template, cp_get_headers());
0 ignored issues
show
Bug introduced by
The variable $send_to_user does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
249
			return true;
250
251
		case 'cp_useradd': // cp_notifications/actions/useradd.php
252
			$message = array(
253
				'cp_msg_type' => $cp_msg_type,
254
				'cp_user_name' => $params['cp_user_name'],
255
				'cp_site_name' => $params['cp_site_name'],
256
				'cp_site_url' => $params['cp_site_url'],
257
				'cp_username' => $params['cp_username'],
258
				'cp_password' => $params['cp_password'],
259
			);
260
			$to_recipients[] = $params['cp_user'];
261
			$info_notif = 'cp_useradd';
262
			
263
			$subject = elgg_echo('cp_notify:subject:add_new_user',array(),'en') . ' | ' . elgg_echo('cp_notify:subject:add_new_user',array(),'fr');
264
			$email_only = true;
265
			break;
266
267
		case 'cp_validate_user': // uservalidationbyemail/lib/functions.php
268
			$message = array(
269
				'cp_validate_user' => $params['cp_validate_user'],
270
				'cp_validate_url' => $params['cp_validate_url'],
271
				'cp_msg_type' => $cp_msg_type
272
			);
273
			$subject = elgg_echo('cp_notify:subject:validate_user',array($params['cp_validate_user']['email']),'en') . ' | ' . elgg_echo('cp_notify:subject:validate_user',array($params['cp_validate_user']['email']),'fr');
274
			$to_recipients[] = get_user($params['cp_validate_user']['guid']);
275
			$info_notif = 'cp_validate_user';
276
			$email_only = true;
277
			break;
278
279
		case 'cp_site_msg_type':	// messages/actions/messages/send.php
280
			$add_to_sent = true;
281
			$sender_guid = $params['cp_from']['guid'];
282
			$to_recipients[] = get_user($params['cp_to']['guid']);
283
			$info_notif = 'cp_site_msg_type';			
284
			$subject = $params['cp_topic_title'];
285
			$message = array(
286
				'cp_msg_title' => $params['cp_topic_title'],
287
				'cp_msg_content' => $params['cp_topic_description'],
288
				'cp_sender' => $params['cp_from']['name'],
289
				'cp_msg_url' => $params['cp_topic_url'],
290
				'cp_msg_type' => 'cp_site_msg_type',
291
			);
292
			break;
293
294
		/// NORMAL NOTIFICATIONS that will send out both email and site notification
295
296
		case 'cp_wire_image':
297
298
			/// the function thewire_image_get_attachments will return an entity
299
			$embed_image = $params['wire_imagedata_loc'];
300
			$author = elgg_get_logged_in_user_entity();
301
			$message = array(
302
				'cp_msg_type' => $params['cp_msg_type'],
303
				'wire_entity' => $params['wire_entity'],
304
				'imagedata_location' => $params['wire_imagedata_loc'],
305
				'author' => $author,
306
			);
307
308
309
			$subject = elgg_echo('cp_notifications:mail_body:subtype:thewireSubj', array($author->name, 'Wire'), 'en').' | ';
310
			$subject .= elgg_echo('cp_notifications:mail_body:subtype:thewireSubj',array($author->name, 'Fil'),'fr');
311
			$content_entity = $params['wire_entity'];
312
313
			$query = "SELECT * FROM elggentity_relationships WHERE relationship = 'cp_subscribed_to_email' AND guid_two = {$author->getGUID()}";
314
			$users = get_data($query);
315
316
			foreach ($users as $user) {
317
				$to_recipients[$user->guid_one] = get_entity($user->guid_one);
318
			}
319
			$info_notif = 'cp_wire_image';			
320
			
321
			break;
322
323
		case 'cp_wire_share': // thewire_tools/actions/add.php
324
325
326
			$message = array(
327
				'cp_msg_type' => $cp_msg_type,
328
				'cp_shared_by' => $params['cp_shared_by'],
329
				'cp_content_reshare' => $params['cp_content_reshared'],
330
				'cp_content' => $params['cp_content'],
331
				'cp_recipient' => $params['cp_recipient'],
332
				'cp_wire_url' => $params['cp_wire_url'],
333
				
334
				'imagedata_location' => $params['wire_imagedata_loc'],
335
			);
336
337
			$parent_item = $params['cp_content']->getContainerEntity();
338
339
			$subject = elgg_echo('cp_notify:wireshare:subject',array($params['cp_shared_by']->name),'en').' | ';
340
			$subject .= elgg_echo('cp_notify:wireshare:subject',array($params['cp_shared_by']->name),'fr');
341
342
			$to_recipients[] = $params['cp_recipient'];
343
			$info_notif = 'cp_wire_share';						
344
			$content_entity = $params['cp_content_reshared'];
345
			$author = $params['cp_shared_by'];
346
			$content_url = $params['cp_content_reshared']->getURL();
347
			break;
348
349
350
		case 'cp_messageboard': // messageboard/actions/add.php
351
			$message = array(
352
				'cp_msg_type' => $cp_msg_type,
353
				'cp_message_content' => $params['cp_message_content'],
354
				'cp_writer_name' => $params['cp_writer']->name,
355
				'cp_owner_profile' => $params['cp_recipient']->getURL(),
356
			);
357
			$subject = elgg_echo('cp_notify:messageboard:subject',array(),'en') . ' | ' . elgg_echo('cp_notify:messageboard:subject',array(),'fr');
358
			$to_recipients[] = $params['cp_recipient'];
359
			$info_notif = 'cp_messageboard';			
360
			$content_entity = $params['cp_message_content'];
361
			$author = $params['cp_writer'];
362
			break;
363
364
		case 'cp_add_grp_operator': // group_operators/actions/group_operators/add.php (adds group operator)
365
			$message = array(
366
				'cp_msg_type' => $cp_msg_type,
367
				'cp_to_operator' => $params['cp_to_operator'],
368
				'cp_who_made_operator' => $params['cp_who_made_operator'],
369
				'cp_group_name' => $params['cp_group_name'],
370
				'cp_who_made_operator' => $params['cp_who_made_operator'],
371
				'cp_group_url' => $params['cp_group_url'],
372
			);
373
			$subject = elgg_echo('cp_notify:subject:add_grp_operator',array(htmlspecialchars(filter_tags(htmlspecialchars_decode(gc_explode_translation($params['cp_group_name'], 'en'))))),'en') . ' | ' . elgg_echo('cp_notify:subject:add_grp_operator',array(htmlspecialchars(filter_tags(htmlspecialchars_decode(gc_explode_translation($params['cp_group_name'], 'fr'))))),'fr');
374
			$to_recipients[] = $params['cp_to_user'];
375
			$info_notif = 'cp_add_grp_operator';						
376
			break;
377
378
379
		case 'cp_grp_admin_transfer': // group_tools/lib/functions.php (this is transfer of group owner through group edit)
380
			$message = array(
381
				'cp_msg_type' => $cp_msg_type,
382
				'cp_group_name' => $params['cp_group_name'],
383
				'cp_group_url' => $params['cp_group_url'],
384
				'cp_appointer' => $params['cp_appointer']
385
			);
386
			$subject = elgg_echo('cp_notify:subject:group_admin_transfer',array(htmlspecialchars(filter_tags(htmlspecialchars_decode(gc_explode_translation($params['cp_group_name'],'en'))))),'en') . ' | ' . elgg_echo('cp_notify:subject:group_admin_transfer',array(htmlspecialchars(filter_tags(htmlspecialchars_decode(gc_explode_translation($params['cp_group_name'],'fr'))))),'fr');
387
			$to_recipients[] = $params['cp_new_owner_user'];
388
			$info_notif = 'cp_grp_admin_transfer';									
389
			break;
390
391
392
		case 'cp_wire_mention': // thewire_tools/lib/events.php (TODO: share option in notifications setting)
393
394
			$message = array(
395
				'cp_mention_by' => $params['cp_mention_by'],
396
				'cp_view_mention' => $params['cp_view_your_mention'],
397
				'cp_msg_type' => $cp_msg_type,
398
				'cp_wire_mention_url' => $params['cp_wire_mention_url'],
399
			);
400
401
			$subject = elgg_echo('cp_notify:subject:wire_mention',array($params['cp_mention_by']),'en') . ' | ' . elgg_echo('cp_notify:subject:wire_mention',array($params['cp_mention_by']),'fr');
402
			$to_recipients[] = $params['cp_send_to'];
403
			$info_notif = 'cp_wire_mention';									
404
			$content_entity = $params['cp_wire_entity'];
405
			$author = $content_entity->getOwnerEntity();
406
			break;
407
408
409
		case 'cp_friend_approve': // friend_request/actions/approve
410
			$subject = elgg_echo('cp_notify:subject:approve_friend',array($params['cp_approver']),'en') . ' | ' . elgg_echo('cp_notify:subject:approve_friend',array($params['cp_approver']),'fr');
411
			$message = array(
412
				'cp_approver' => $params['cp_approver'],
413
				'cp_approver_profile' => $params['cp_approver_profile'],
414
				'cp_msg_type' => $cp_msg_type,
415
416
				);
417
			$to_recipients[] = get_user($params['cp_request_guid']);
418
			$info_notif = 'cp_friend_approve';									
419
			$content_entity = $params['object'];
420
			$author = $params['object'];
421
			break;
422
423
424
		case 'cp_group_add':	// group_tools/lib/functions.php OR groups/actions/groups/membership/add.php ????
425
			$to_recipients[] = $params['cp_user_added'];
426
			$info_notif = 'cp_group_add';												
427
			$subject = elgg_echo('cp_notify:subject:group_add_user',array(gc_explode_translation($params['cp_group']['name'],'en')),'en') . ' | ' . elgg_echo('cp_notify:subject:group_add_user',array(gc_explode_translation($params['cp_group']['name'],'fr')),'fr');
428
			$message = array(
429
				'cp_user_added' => $params['cp_user_added'],
430
				'cp_group' => $params['cp_group'],
431
				'cp_message' => $params['cp_added_msg'],
432
				'cp_msg_type' => $cp_msg_type
433
			);
434
			break;
435
436
437
		case 'cp_group_invite': // group_tools/lib/functions.php
438
439
			$subject = elgg_echo('cp_notify:subject:group_invite_user',array($params['cp_inviter']['name'],gc_explode_translation($params['cp_invite_to_group']['name'],'en')),'en');
440
			$subject .= ' | '.elgg_echo('cp_notify:subject:group_invite_user',array($params['cp_inviter']['name'],gc_explode_translation($params['cp_invite_to_group']['name'],'fr')),'fr');
441
442
			$message = array(
443
				'cp_group_invite_from' => $params['cp_invitee'], // user we're inviting
444
				'cp_group_invite_to' => $params['cp_inviter'], // user inviting others
445
				'cp_group' => $params['cp_invite_to_group'],
446
				'cp_invitation_url' => $params['cp_invitation_url'],
447
				'cp_invitation_msg' => $params['cp_invite_msg'],
448
				'cp_msg_type' => $cp_msg_type
449
			);
450
			$to_recipients[] = get_user($params['cp_invitee']['guid']);
451
			$info_notif = 'cp_group_invite';												
452
			$content_entity = $params['cp_invite_to_group'];
453
			$author = $params['cp_inviter'];
454
			break;
455
456
457
		case 'cp_group_mail': // group_tools/actions/mail.php
458
		
459
			$message = array(
460
				'cp_group' => $params['cp_group'],
461
				'cp_group_subject' => $params['cp_group_subject'],
462
				'cp_group_message' => $params['cp_group_message'],
463
				'cp_msg_type' => $cp_msg_type
464
			);
465
			$subject = elgg_echo('cp_notify:subject:group_mail',array($params['cp_group_subject'],gc_explode_translation($params['cp_group']['name'],'en')),'en'). ' | ' . elgg_echo('cp_notify:subject:group_mail',array($params['cp_group_subject'],gc_explode_translation($params['cp_group']['name'],'fr')),'fr');
466
			
467
			foreach ($params['cp_group_mail_users'] as $to_user) {
468
				$to_recipients[$to_user] = get_user($to_user);
469
			}
470
			$info_notif = 'cp_group_mail';															
471
			break;
472
473
474
		case 'cp_friend_request': // friend_request/lib/events.php
475
			$message = array(
476
				'cp_friend_request_from' => $params['cp_friend_requester'],
477
				'cp_friend_request_to' => $params['cp_friend_receiver'],
478
				'cp_friend_invitation_url' => $params['cp_friend_invite_url'],
479
				'cp_msg_type' => $cp_msg_type
480
			);
481
			$subject = elgg_echo('cp_notify:subject:friend_request',array($params['cp_friend_requester']['name']),'en') . ' | ' . elgg_echo('cp_notify:subject:friend_request',array($params['cp_friend_requester']['name']),'fr');
482
			$to_recipients[] = get_user($params['cp_friend_receiver']['guid']);
483
			$info_notif = 'cp_friend_request';												
484
485
			$content_entity = $params['cp_relationship'];
486
			$author = $params['cp_friend_requester'];
487
488
			break;
489
490
491 View Code Duplication
		case 'cp_hjpost': // gcforums/actions/gcforums/create.php
492
			$message = array(
493
				'cp_hjpost_author' => $params['cp_topic_author'],
494
				'cp_hjpost_title' => $params['cp_topic_title'],
495
				'cp_hjpost_description' => $params['cp_topic_description'],
496
				'cp_hjpost_url' => $params['cp_topic_url'],
497
				'cp_msg_type' => $cp_msg_type
498
				);
499
			$t_user = $params['cp_subscribers'];
500
			$subject = elgg_echo('cp_notify:subject:hjpost',array($params['cp_topic_author'],$params['cp_topic_title']),'en');
501
			$subject .= ' | '.elgg_echo('cp_notify:subject:hjpost',array($params['cp_topic_author'],$params['cp_topic_title']),'fr');
502
			foreach ($t_user as $s_uer)
503
				$to_recipients[] = get_user($s_uer);
504
505
			$info_notif = 'cp_hjpost';												
506
			$content_entity = $params['cp_post'];
507
			$content_url = $params['cp_topic_url'];
508
			$author = $params['cp_post']->getOwnerEntity();
509
510
			break;
511
512
513 View Code Duplication
		case 'cp_hjtopic': // gcforums/actions/gcforums/create.php
514
			$message = array(
515
				'cp_hjtopic_author' => $params['cp_topic_author'],
516
				'cp_hjtopic_title' => $params['cp_topic_title'],
517
				'cp_hjtopic_description' => $params['cp_topic_description'],
518
				'cp_hjtopic_url' => $params['cp_topic_url'],
519
				'cp_msg_type' => $cp_msg_type
520
				);
521
			$t_user = $params['cp_subscribers'];
522
			$subject = elgg_echo('cp_notify:subject:hjtopic',array($params['cp_topic_author'],$params['cp_topic_title']),'en');
523
			$subject .= ' | '.elgg_echo('cp_notify:subject:hjtopic',array($params['cp_topic_author'],$params['cp_topic_title']),'fr');
524
			foreach ($t_user as $s_uer)
525
				$to_recipients[] = get_user($s_uer);
526
527
			$info_notif = 'cp_hjtopic';															
528
			$content_url = $params['cp_topic_url'];
529
			$content_entity = $params['cp_topic'];
530
			$author = $params['cp_topic']->getOwnerEntity();
531
			break;
532
533
		case 'cp_event_request': // event_calendar/actions/event_calendar/request_personal_calendar.php
534
			$message = array(
535
				'cp_event_request_user' => $params['cp_event_request_user'],
536
				'cp_event_request_url' => $params['cp_event_request_url'],
537
				'cp_event_object' => $params['cp_event_obj'],
538
				'type_event' => $params['type_event'], // create, request, cancel
539
				'cp_msg_type' => $cp_msg_type
540
			);
541
			$subject = elgg_echo('cp_notify:event_request:subject',array($params['cp_event_request_user'], $params['cp_event_obj']->title),'en');
542
			$subject .= ' | '.elgg_echo('cp_notify:event_request:subject',array($params['cp_event_request_user'], $params['cp_event_obj']->title),'fr');
543
			$to_recipients[] = $params['cp_event_owner'];
544
			$info_notif = 'cp_event_request';															
545
			
546
			break;
547
548
		case 'cp_event_ics': // .../mod/event_calendar/actions/event_calendar/add_ics.php
549
			$message = array(
550
				'cp_event_send_to_user' => $params['cp_event_send_to_user'],
551
				//'cp_event_invite_url' => $params['cp_event_invite_url'],
552
				'startdate' => $params['startdate'],
553
				'enddate' => $params['enddate'],
554
				'cp_event' => $params['cp_event'],
555
				'cp_msg_type' => $cp_msg_type,
556
			);
557
558
			$event = $params['cp_event'];
559
			$startdate = $params['startdate'];
560
			$enddate = $params['enddate'];
561
			
562
			// Add to my Outlook calendar | Ajoutez a mon calendrier d'Outlook
563
		    $subject = $event->title.' - '.elgg_get_logged_in_user_entity()->username; 
564
565
		   	$event = 'event';
566
			$to_recipients[] = $params['cp_event_send_to_user'];
567
			$info_notif = 'cp_event_ics';															
568
			   
569
			break;
570
571
			case 'cp_welcome_message':	// messages/actions/messages/send.php
572
			$add_to_sent = true;
573
			$sender_guid = $params['cp_from']['guid'];
574
			$to_recipients[] = get_user($params['cp_to']['guid']);
575
			$info_notif = 'cp_welcome_message';																		
576
			$subject = $params['cp_topic_title'];
577
			$message = array(
578
				'cp_msg_title' => $params['cp_topic_title'],
579
				'cp_msg_content' => $params['cp_topic_description'],
580
				'cp_msg_content_fr' => $params['cp_topic_description_fr'],
581
				'cp_msg_content_en' => $params['cp_topic_description_en'],
582
583
				'cp_sender' => $params['cp_from']['name'],
584
				'cp_msg_url' => $params['cp_topic_url'],
585
				'cp_msg_type' => 'cp_welcome_message',
586
			);
587
			break;
588
589
		default:
590
			break;
591
	}
592
593
594
	if (empty($subject))
595
		return false;
596
597
	$subject = htmlspecialchars_decode($subject,ENT_QUOTES);
598
599
	if (is_array($to_recipients)) {
600
		foreach ($to_recipients as $to_recipient) {
601
602
			// username for link in footer (both email notification and site notification
603
			$message['user_name'] = $to_recipient->username;
0 ignored issues
show
Bug introduced by
The variable $message does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
604
			if ($cp_msg_type != 'cp_event_ics') {
605
				$template = elgg_view('cp_notifications/email_template', $message);
606
				$site_template = elgg_view('cp_notifications/site_template', $message);
607
			}
608
609
			$newsletter_appropriate = array('cp_wire_share','cp_wire_image','cp_messageboard','cp_wire_mention','cp_hjpost','cp_hjtopic', 'cp_friend_request', 'cp_friend_approve');
610
			if (strcmp(elgg_get_plugin_user_setting('cpn_set_digest', $to_recipient->guid, 'cp_notifications'),'set_digest_yes') == 0 && in_array($cp_msg_type, $newsletter_appropriate)) {
611
				$result = create_digest($author, $cp_msg_type, $content_entity, $to_recipient, $content_url);
0 ignored issues
show
Bug introduced by
The variable $author does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Bug introduced by
The variable $content_entity does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Bug introduced by
The variable $content_url does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
612
				continue;
613
614
			} else {
615
				
616
					$result = (elgg_is_active_plugin('phpmailer')) ? phpmailer_send( $to_recipient->email, $to_recipient->name, $subject, $template, NULL, true, NULL, NULL, $embed_image ) : mail($to_recipient->email, $subject, $template, cp_get_headers($event));
0 ignored issues
show
Bug introduced by
The variable $template does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Bug introduced by
The variable $event does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
617
				}
618
619
			if (!$email_only)
620
				messages_send($subject, $site_template, $to_recipient->guid, $sender_guid, 0, true, $add_to_sent);
0 ignored issues
show
Bug introduced by
The variable $site_template does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
621
		}
622
	}
623
624
	// register the error, if either of the arrays are not populated
625
	if (!is_array($to_recipients)) {
626
		notification_logging('error: in cp_create_notification(), $to_recipients is not array'.$info_notif);
0 ignored issues
show
Bug introduced by
The variable $info_notif does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
627
	}
628
}
629
630
631
/**
632
 * returns the headers for ical
633
 *
634
 * @param string 		$type_event
0 ignored issues
show
Bug introduced by
There is no parameter named $type_event. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
635
 * @param ElggObject 	$event
636
 * @param string 		$start_date
637
 * @param string 		$end_date
638
 */
639
function cp_ical_headers($event_type, $event, $start_date, $end_date) {
640
641
	$end_date = date("Ymd\THis", strtotime($end_date));
642
	$start_date = date("Ymd\THis", strtotime($startdate));
0 ignored issues
show
Bug introduced by
The variable $startdate does not exist. Did you mean $start_date?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
643
	$current_date = date("Ymd\TGis");
644
645
	$ical = "
646
	BEGIN:VCALENDAR \r\n
647
    PRODID:-//Microsoft Corporation//Outlook 10.0 MIMEDIR//EN \r\n
648
    VERSION:2.0 \r\n
649
    METHOD: {$type_event} \r\n
0 ignored issues
show
Bug introduced by
The variable $type_event does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
650
    BEGIN:VTIMEZONE \r\n
651
    TZID:Eastern Time \r\n
652
    BEGIN:STANDARD \r\n
653
    DTSTART:20091101T020000 \r\n
654
    RRULE:FREQ=YEARLY;INTERVAL=1;BYDAY=1SU;BYMONTH=11 \r\n
655
    TZOFFSETFROM:-0400 \r\n
656
    TZOFFSETTO:-0500 \r\n
657
    TZNAME:EST \r\n
658
    END:STANDARD \r\n
659
    BEGIN:DAYLIGHT \r\n
660
    DTSTART:20090301T020000 \r\n
661
    RRULE:FREQ=YEARLY;INTERVAL=1;BYDAY=2SU;BYMONTH=3 \r\n
662
    TZOFFSETFROM:-0500 \r\n
663
    TZOFFSETTO:-0400 \r\n
664
    TZNAME:EDST \r\n
665
    END:DAYLIGHT \r\n
666
    END:VTIMEZONE \r\n
667
    BEGIN:VEVENT \r\n
668
    LAST-MODIFIED: {$current_date} \r\n
669
    UID: {$event->guid} \r\n
670
    DTSTAMP:  \r\n
671
    DTSTART;TZID='Eastern Time': {$start_date} \r\n
672
    DTEND;TZID='Eastern Time': {$end_date} \r\n
673
    TRANSP:OPAQUE \r\n
674
    SEQUENCE:1 \r\n
675
	SUMMARY: {$event->title} \r\n
676
	LOCATION: {$event->venue} \r\n
677
    CLASS:PUBLIC \r\n
678
    PRIORITY:5 \r\n
679
    BEGIN:VALARM \r\n
680
    TRIGGER:-PT15M \r\n
681
    ACTION:DISPLAY \r\n
682
    DESCRIPTION:Reminder \r\n
683
    END:VALARM \r\n
684
    END:VEVENT \r\n
685
    END:VCALENDAR \r\n";
686
687
	return $ical;
688
}
689
690
691
692
693
/**
694
 * cp_create_annotation_notification is an event handler, invokes everytime a user likes something, edit something, etc
695
 *
696
 * This contains the likes and the comments that get posted. we also filter out the
697
 * following : blog revision, discussion replies (?), tasks, poll votes, folder creation
698
 *
699
 * @param string $event		the name of the event
700
 * @param string $type		the type of the object
701
 * @param mixed $object		the object/entity of the event
702
 */
703
function cp_create_annotation_notification($event, $type, $object) {
704
705
	elgg_load_library('elgg:gc_notification:functions');
706
	$entity = get_entity($object->entity_guid);
707
708
	if ($entity->entity_minor_edit)	return;
709
710
	$dbprefix = elgg_get_config('dbprefix');
711
	$site = elgg_get_site_entity();
712
	$object_subtype = $object->getSubtype();
713
	$liked_content = get_entity($object->entity_guid);
714
	$type_of_like = $liked_content->getSubtype();
715
	if (!$type_of_like) $type_of_like = $liked_content->getType();
716
717
	$action_type = "content_revision";
718
	$author = $liked_by;
0 ignored issues
show
Bug introduced by
The variable $liked_by seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
719
720
	/// EDITS TO BLOGS AND PAGES, THEY ARE CONSIDERED ANNOTATION DUE TO REVISIONS AND MULTIPLE COPIES OF SAME CONTENT
721
	if (strcmp($object_subtype, 'likes') != 0) {
722
723
		$content = get_entity($object->entity_guid);
724
		$get_error_info='if object subtype likes 0';
725
		// auto save -drafts or -published blogs, we don't send out notifications
726 View Code Duplication
		if (strcmp($object_subtype,'blog_auto_save') == 0 && (strcmp($entity->status,'draft') == 0 || strcmp($entity->status, 'published') == 0)){
727
			$get_error_info='draft/autosave';
728
			return;
729
		} 
730
731
732
		// if we are publishing, or revising blogs then send out notification
733
		if (strcmp($object_subtype,'blog_revision') == 0 && strcmp($entity->status,'published') == 0) {
734
			$current_user = get_user($entity->getOwnerGUID());
735
			$subject = elgg_echo('cp_notify:subject:edit_content',array('The blog',gc_explode_translation($entity->title,'en'), $current_user->username),'en') . ' | ' . elgg_echo('cp_notify:subject:edit_content:m',array('Le blogue',gc_explode_translation($entity->title,'fr'), $current_user->username),'fr');
736
			$subject = htmlspecialchars_decode($subject,ENT_QUOTES);
737
738
			add_entity_relationship($entity->getOwnerGUID(), 'cp_subscribed_to_email', $entity->getGUID());
739
			add_entity_relationship($entity->getOwnerGUID(), 'cp_subscribed_to_site_mail', $entity->getGUID());
740
741
			$message = array(
742
				'cp_content' => $entity,
743
				'cp_user' => $current_user->username,
744
				'cp_msg_type' => 'cp_content_edit',
745
				'cp_fr_entity' => 'Ce blogue',
746
				'cp_en_entity' => 'blog',
747
			);
748
749
			if($entity->getContainerEntity() instanceof ElggGroup){
750
				$author_id = $object->getOwnerGUID();
751
				$content_id = $entity->getContainerGUID();
752
			}else{
753
				$author_id = $current_user->guid;
754
			}
755
756
			$author = $current_user;
757
			$content_entity = $entity;
758
			$get_error_info = 'blog revision or published';
759
760
			$watchers = get_subscribers($dbprefix, $author_id, $content_id);
0 ignored issues
show
Bug introduced by
The variable $content_id does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
761 View Code Duplication
			foreach ($watchers as $watcher) {
762
				$message['user_name'] = $watcher->username;
763
764
				$template = elgg_view('cp_notifications/email_template', $message);
765
766
				$recipient_user = get_user($watcher->guid);
767
768
				if (has_access_to_entity($entity, $recipient_user) && $entity->access_id != 0) {
0 ignored issues
show
Documentation introduced by
$recipient_user is of type object<ElggEntity>, but the function expects a object<ElggUser>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
769
					if (strcmp(elgg_get_plugin_user_setting('cpn_set_digest', $watcher->guid,'cp_notifications'), 'set_digest_yes') == 0)
770
						create_digest($author, $action_type, $content_entity, get_entity($watcher->guid));
0 ignored issues
show
Compatibility introduced by
$author of type object<ElggEntity> is not a sub-type of object<ElggUser>. It seems like you assume a child class of the class ElggEntity to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
Compatibility introduced by
get_entity($watcher->guid) of type object<ElggEntity> is not a sub-type of object<ElggUser>. It seems like you assume a child class of the class ElggEntity to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
771
					else{
772
						(elgg_is_active_plugin('phpmailer')) ? phpmailer_send($watcher->email, $watcher->name, $subject, $template, NULL, true) : mail($watcher->email, $subject, $template, cp_get_headers());
773
						count_email_types($message['cp_en_entity']);
774
					}
775
				
776
					if (check_entity_relationship($watcher->guid, 'cp_subscribed_to_site_mail', $entity->getContainerGUID()))
777
						messages_send($subject, $template, $watcher->guid, $site->guid, 0, true, false);
778
				}
779
			}
780
			return true;
781
		}
782
783
784
		// checks for condition if the content being modified is a page or task
785
		if (strcmp($object_subtype,'page') == 0 || strcmp($object_subtype,'page_top') == 0 || strcmp($object_subtype,'task') == 0 || strcmp($object_subtype,'task_top') == 0) {
786
			$current_user = get_user($object->owner_guid);
787
			$subject = elgg_echo('cp_notify:subject:edit_content',array('The page', gc_explode_translation($entity->title,'en'), $current_user->username),'en');
788
			$subject .= ' | '.elgg_echo('cp_notify:subject:edit_content:f',array('La page', gc_explode_translation($entity->title,'fr'), $current_user->username),'fr');
789
790
			$subject = htmlspecialchars_decode($subject,ENT_QUOTES);
791
792
			$message = array(
793
				'cp_content' => $entity,
794
				'cp_user' => $current_user->username,
795
				'cp_msg_type' => 'cp_content_edit',
796
				'cp_fr_entity' => 'Cette page',
797
				'cp_en_entity' => 'page',
798
			);
799
800
			$author = $current_user;
801
			$content_entity = $entity;
802
			$get_error_info = 'page, page top';			
803
804
			$watchers = get_subscribers($dbprefix, $current_user->guid, $entity->guid);
805
806 View Code Duplication
			foreach ($watchers as $watcher) {
807
				$message['user_name'] = $watcher->username;
808
				$template = elgg_view('cp_notifications/email_template', $message);
809
				$recipient_user = get_user($watcher->guid);
810
811
				if (has_access_to_entity($entity, $recipient_user) /*&& $object->access_id != 0*/) {
0 ignored issues
show
Documentation introduced by
$recipient_user is of type object<ElggEntity>, but the function expects a object<ElggUser>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
812
813
					if (strcmp(elgg_get_plugin_user_setting('cpn_set_digest', $watcher->guid,'cp_notifications'),'set_digest_yes') == 0) {
814
815
						create_digest($author, $action_type, $content_entity, get_entity($watcher->guid));
0 ignored issues
show
Compatibility introduced by
$author of type object<ElggEntity> is not a sub-type of object<ElggUser>. It seems like you assume a child class of the class ElggEntity to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
Compatibility introduced by
get_entity($watcher->guid) of type object<ElggEntity> is not a sub-type of object<ElggUser>. It seems like you assume a child class of the class ElggEntity to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
816
					}
817
					else {
818
						(elgg_is_active_plugin('phpmailer')) ? phpmailer_send( $watcher->email, $watcher->name, $subject, $template, NULL, true ) : mail($watcher->email, $subject, $template, cp_get_headers());
819
						count_email_types($message['cp_en_entity']);
820
					}
821
822
823
					if (check_entity_relationship($watcher->guid, 'cp_subscribed_to_site_mail', $entity->getContainerGUID())) {
824
						$site_template = elgg_view('cp_notifications/site_template', $message);
825
						messages_send($subject, $site_template, $watcher->guid, $site->guid, 0, true, false);
826
					}
827
				}
828
			}
829
			return true;
830
		}
831
		
832
	} else {
833
834
		/// LIKES TO COMMENTS AND DISCUSSION REPLIES
835
    	$content_entity = get_entity($object->entity_guid); 			// get the comment object
836
		$comment_author = get_user($content_entity->owner_guid); 		// get the user who made the comment
837
		$content = get_entity($content_entity->getContainerGUID());		// get the location of comment
838
		$content_title = $content->title; 									// get title of content
839
		$liked_by = get_user($object->owner_guid); 							// get user who liked comment
840
841
		$action_type = "post_likes";
842
843
		$to_recipients = array();
844
		$to_recipients_site = array();
845
		$get_error_info = '';
846
847
	    switch ($type_of_like) {
848
	    	case 'group':
849
	    		$group_name_en = htmlspecialchars(filter_tags(htmlspecialchars_decode(gc_explode_translation($content_entity->name, 'en'))));
850
	    		$group_name_fr = htmlspecialchars(filter_tags(htmlspecialchars_decode(gc_explode_translation($content_entity->name, 'fr'))));
851
852
	    		$subject = elgg_echo(elgg_echo('cp_notify:subject:likes_group', array($liked_by->name, $group_name_en), 'en'));
853
	    		$subject .= elgg_echo(elgg_echo('cp_notify:subject:likes_group', array($liked_by->name, $group_name_fr), 'fr'));
854
855
	    		$message = array(
856
	    			'cp_liked_by' => $liked_by->name,
857
	    			'cp_group' => $content_entity->name,
858
	    			'cp_group_link' => $content_entity->getURL(),
859
	    			'cp_msg_type' => 'cp_like_group'
860
	    		);
861
862
	    		$group_owner = $content_entity->getOwnerEntity();
863
	    		$action_type = 'like_group';
864
865
	    		if (strcmp(elgg_get_plugin_user_setting('cpn_likes_email', $group_owner->getGUID(),'cp_notifications'),'likes_email') == 0)
866
					$to_recipients[$group_owner->getGUID()] = $group_owner;
867
					$get_error_info = 'group';
868
869
    			if (strcmp(elgg_get_plugin_user_setting('cpn_likes_site', $group_owner->getGUID(),'cp_notifications'),'likes_site') == 0)
870
					$to_recipients_site[$group_owner->getGUID()] = $group_owner;
871
					$get_error_info = 'group';
872
873
	    		break;
874
875 View Code Duplication
	    	case 'comment':
876
877
	    		$content_title_en = gc_explode_translation($content_title, 'en');
878
	    		$content_title_fr = gc_explode_translation($content_title, 'fr');
879
880
	    		$subject = elgg_echo('cp_notify:subject:likes_comment', array($liked_by->name, $content_title_en),'en');
881
	    		$subject .= ' | '.elgg_echo('cp_notify:subject:likes_comment',array($liked_by->name, $content_title_fr),'fr');
882
883
	    		$message = array(
884
	    			'cp_liked_by' => $liked_by->name,
885
	    			'cp_comment_from' => $content_title,
886
	    			'content_url' => $content->getURL(),
887
					'cp_msg_type' => 'cp_likes_comments',
888
				);
889
890
	    		$author = $liked_by;
891
	    		$action_type = "like_comment";
892
893
	    		if (strcmp(elgg_get_plugin_user_setting('cpn_likes_email', $comment_author->getGUID(),'cp_notifications'),'likes_email') == 0)
894
					$to_recipients[$comment_author->getGUID()] = $comment_author;
895
					$get_error_info = 'comment';
896
897
    			if (strcmp(elgg_get_plugin_user_setting('cpn_likes_site', $comment_author->getGUID(),'cp_notifications'),'likes_site') == 0)
898
					$to_recipients_site[$comment_author->getGUID()] = $comment_author;
899
					$get_error_info = 'comment';
900
	    		break;
901
902 View Code Duplication
	    	case 'discussion_reply':
903
904
	    		$content_title_en = gc_explode_translation($content_title, 'en');
905
	    		$content_title_fr = gc_explode_translation($content_title, 'fr');
906
907
	    		$subject = elgg_echo('cp_notify:subject:likes_discussion',array($liked_by->name, $content_title_en),'en');
908
	    		$subject .= ' | '.elgg_echo('cp_notify:subject:likes_discussion',array($liked_by->name, $content_title_fr),'fr');
909
910
				$message = array(
911
					'cp_liked_by' => $liked_by->name,
912
					'cp_comment_from' => $content_title,
913
					'content_url' => $content->getURL(),
914
					'cp_msg_type' => 'cp_likes_topic_replies',
915
				);
916
				$author = $liked_by;
917
				$action_type = "like_reply";
918
919
	    		if (strcmp(elgg_get_plugin_user_setting('cpn_likes_email', $comment_author->getGUID(),'cp_notifications'),'likes_email') == 0)
920
					$to_recipients[$comment_author->getGUID()] = $comment_author;
921
					$get_error_info = 'discussion_reply';
922
    			if (strcmp(elgg_get_plugin_user_setting('cpn_likes_site', $comment_author->getGUID(),'cp_notifications'),'likes_site') == 0)
923
					$to_recipients_site[$comment_author->getGUID()] = $comment_author;
924
					$get_error_info = 'discussion_reply';
925
	    		break;
926
927
928
	    	default:
929
	    		$type_of_like = 'user_update';
930
		    	if ($liked_content instanceof ElggUser) {
931
932
					// cyu - there doesn't seem to be any differentiation between updated avatar and colleague connection
933
		    		$liked_by = get_user($object->owner_guid); // get user who liked comment
934
935
936
		    		$subject = elgg_echo('cp_notify:subject:likes_user_update',array($liked_by->name),'en') . ' | ' . elgg_echo('cp_notify:subject:likes_user_update',array($liked_by->name),'fr');
937
		    		$message = array(
938
						'cp_msg_type' => 'cp_likes_user_update',
939
						'cp_liked_by' => $liked_by->name
940
					);
941
					$to_recipients[$liked_content->guid] = $liked_content;
942
					$get_error_info = 'user_update_if_liked_content';
943
944
945
		    	} else {
946
947
		    		$type_of_like = 'content';
948
		    		$liked_by = get_user($object->owner_guid); // get user who liked content
949
		    		$content = get_entity($object->entity_guid);
950
951
		    		$content_entity = $content;
952
		    		$author = $liked_by;
953
954
		    		$content_title_en = gc_explode_translation($content->title, 'en');
955
		    		$content_title_fr = gc_explode_translation($content->title, 'fr');
956
957
		    		// cyu - patching issue #323 (liking wire post)
958
		    		if ($content->getSubtype() === 'thewire') {
959
		    			$subject = elgg_echo('cp_notify:subject:likes_wire',array($liked_by->name, $content_title_en),'en') . ' | ' . elgg_echo('cp_notify:subject:likes_wire',array($liked_by->name, $content_title_fr), 'fr');
960
		    			$content_subtype = 'thewire';
961
962
		    		} else {
963
		    			$subject = elgg_echo('cp_notify:subject:likes',array($liked_by->name, $content_title_en),'en') . ' | ' . elgg_echo('cp_notify:subject:likes',array($liked_by->name, $content_title_fr), 'fr');
964
		    			$content_subtype = '';
965
		    		}
966
967
		    		$message = array(
968
		    			'cp_subtype' => $content_subtype,
969
						'cp_msg_type' => 'cp_likes_type',
970
						'cp_liked_by' => $liked_by->name,
971
						'cp_comment_from' => $content->title,
972
						'cp_description' => $content->description,
973
						'cp_content_url' => $content->getURL(),
974
					);
975
976
		    		if (strcmp(elgg_get_plugin_user_setting('cpn_likes_email', $content->getOwnerGUID(),'cp_notifications'), 'likes_email') == 0)
977
						$to_recipients[$content->getOwnerGUID()] = $content->getOwnerEntity();
978
						$get_error_info = 'content';
979
980
	    			if (strcmp(elgg_get_plugin_user_setting('cpn_likes_site', $content->getOwnerGUID(),'cp_notifications'), 'likes_site') == 0)
981
						$to_recipients_site[$content->getOwnerGUID()] = $content->getOwnerEntity();
982
						$get_error_info = 'content';
983
		    	}
984
	    		break;
985
986
		} // end switch statement
987
	}
988
989
	$subject = htmlspecialchars_decode($subject,ENT_QUOTES);
0 ignored issues
show
Bug introduced by
The variable $subject does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
990
991
	if (is_array($to_recipients)) {
992
		// send notification out via email
993
		foreach ($to_recipients as $to_recipient_id => $to_recipient) {
0 ignored issues
show
Bug introduced by
The variable $to_recipients does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
994
		
995
			$message['user_name'] = get_user($to_recipient->guid)->username;
0 ignored issues
show
Bug introduced by
The variable $message does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
996
997
			$recipient_user = get_user($to_recipient->guid);
998
999
			if($recipient_user->gcdeactivate)
1000
				continue;
1001
1002
			if ($liked_by->guid == $entity->getOwnerGUID() && $to_recipient->guid == $liked_by->guid)
0 ignored issues
show
Bug introduced by
The variable $liked_by does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
1003
				continue;
1004
1005
			if ($object->access_id == 1 || $object->access_id == 2 || $content_entity->getType() === 'group' || $action_type === 'post_likes') {
1006
1007 View Code Duplication
				if (strcmp(elgg_get_plugin_user_setting('cpn_set_digest', $to_recipient->guid,'cp_notifications'),'set_digest_yes') == 0)
1008
					create_digest($author, $action_type, $content_entity, $to_recipient);
0 ignored issues
show
Bug introduced by
The variable $content_entity does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
1009
1010
				else {
1011
					$template = elgg_view('cp_notifications/email_template', $message);
1012
1013
					if (elgg_is_active_plugin('phpmailer')) {
1014
						phpmailer_send( $to_recipient->email, $to_recipient->name, $subject, $template, NULL, true );
1015
						count_email_types($message['cp_msg_type']);
1016
					}
1017
					else
1018
						mail($to_recipient->email, $subject, $template, cp_get_headers());
1019
				}
1020
			}
1021
		}
1022
	}
1023
1024
	if (is_array($to_recipients_site)) {
1025
		// send notification out via site
1026
		foreach ($to_recipients_site as $to_recipient_id => $to_recipient) {
0 ignored issues
show
Bug introduced by
The variable $to_recipients_site does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
1027
			$site_template = elgg_view('cp_notifications/site_template', $message);
1028
			$recipient_user = get_user($to_recipient->guid);
1029
1030
			if (($object->access_id == 1 || $object->access_id == 2 || $content_entity->getType() === 'group' || $action_type === 'post_likes')  && 
1031
				(strcmp(elgg_get_plugin_user_setting('cpn_set_digest', $to_recipient->guid,'cp_notifications'),'set_digest_yes') !== 0)) {
1032
1033
				messages_send($subject, $site_template, $to_recipient->guid, $site->guid, 0, true, false);
1034
			}
1035
1036
		}
1037
	}
1038
1039
	// register the error, if either of the arrays are not populated
1040
	if (!is_array($to_recipients) || !is_array($to_recipients_site)) {
1041
		$error_message = "error: in cp_create_notification(), \$to_recipients or \$to_recipients_site is not array"."\r\n"."Owner_entity = ".($content->getOwnerEntity()?print_r($content->getOwnerEntity(),true) : 'N/A');
1042
		if ($comment_author) {
1043
			$error_message .= 'Comment author= '. print_r($comment_author, true);
0 ignored issues
show
Bug introduced by
The variable $comment_author does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
1044
		}
1045
		if ($group_owner) {
1046
			$error_message .= 'Group owner= ' . print_r($group_owner, true);
0 ignored issues
show
Bug introduced by
The variable $group_owner does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
1047
		}
1048
		$error_message .= 'State: '.$get_error_info; //from which state the error is from
1049
		notification_logging($error_message);
1050
	}
1051
1052
} // end of function
1053
1054
1055
1056
/**
1057
 * function cp_create_notification is an event handler, invokes everytime a new entity is created
1058
 * This contains the notifications for new content posted on GCconnex
1059
 *
1060
 * @param string $event		the name of the event
1061
 * @param string $type		the type of object (eg "user", "group", ...)
1062
 * @param mixed $object		the object/entity of the event
1063
 */
1064
function cp_create_notification($event, $type, $object) {
1065
1066
	$do_not_subscribe_list = array('mission-posted', 'file', 'tidypics_batch', 'hjforum', 'hjforumcategory','hjforumtopic', 'messages', 'hjforumpost', 'site_notification', 'poll_choice','blog_revision','widget','folder','c_photo', 'cp_digest','MySkill', 'education', 'experience', 'poll_choice3');
1067
1068
	// since we implemented the multi file upload, each file uploaded will invoke this hook once to many times (we don't allow subtype file to go through, but check the event)
1069
	if ($object instanceof ElggObject && $event !== 'single_file_upload') {
1070
1071
		if (in_array($object->getSubtype(), $do_not_subscribe_list)) 
1072
			return true;		
1073
	} else {
1074
1075
		if ('single_zip_file_upload' !== $event && 'multi_file_upload' !== $event && 'single_file_upload' !== $event)
1076
			return true;
1077
	}
1078
1079
	elgg_load_library('elgg:gc_notification:functions');
1080
	$dbprefix = elgg_get_config('dbprefix');
1081
	$no_notification = false;
1082
	$site = elgg_get_site_entity();
1083
	$to_recipients = array();
1084
	$subject = "";
1085
1086
	$switch_case = $event;
1087
	if ($object instanceof ElggObject)
1088
		$switch_case = $object->getSubtype();	
1089
1090
	switch ($switch_case) {
1091
1092
1093
		/// invoked when zipped file upload function is used (files_tools/lib/functions.php)
1094
		case 'single_zip_file_upload':
1095
1096
			$entity = get_entity($object['forward_guid']);
1097 View Code Duplication
			if (elgg_instanceof('group', $entity)) {
0 ignored issues
show
Documentation introduced by
$entity is of type object<ElggEntity>, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
1098
				$file_forward_url = elgg_get_site_entity()->getURL()."file/group/{$object['forward_guid']}/all";
1099
			} elseif (elgg_instanceof('user', $entity)) {
0 ignored issues
show
Documentation introduced by
$entity is of type object<ElggEntity>, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
1100
				$file_forward_url = elgg_get_site_entity()->getURL()."file/owner/{$entity->username}";
1101
			} else {
1102
				$file_forward_url = $entity->getURL();
1103
			}
1104
1105
			$to_recipients = get_subscribers($dbprefix, elgg_get_logged_in_user_guid(), $entity->getGUID());
1106
			$to_recipients_site = get_site_subscribers($dbprefix, elgg_get_logged_in_user_guid(), $entity->getGUID());
1107
1108
			$subject = elgg_echo('cp_notify_usr:subject:new_content2', array(elgg_get_logged_in_user_entity()->username, 'file'), 'en');
1109
			$subject .= ' | '.elgg_echo('cp_notify_usr:subject:new_content2', array(elgg_get_logged_in_user_entity()->username, 'fichier', false), 'fr');
1110
1111
			$author = elgg_get_logged_in_user_entity();
1112
			$message = array(
1113
				'cp_topic' => $entity,
1114
				'cp_msg_type' => 'zipped_file',
1115
				'files_uploaded' => $object['files_uploaded'],
1116
				'cp_topic_description_discussion' => 'Please view the files here',
1117
				'cp_topic_description_discussion2' => 'SVP voir les fichiers ici',
1118
			);
1119
1120
			
1121
			$content_entity = $object['files_uploaded'];
1122
			$object = get_entity($object['files_uploaded'][0]);
1123
			$author = elgg_get_logged_in_user_entity();
1124
			break;
1125
1126
		/// invoked when multiple file upload function is used
1127
		case 'multi_file_upload':
1128
1129
			$entity = get_entity($object['forward_guid']);
1130 View Code Duplication
			if (elgg_instanceof('group', $entity)) {
0 ignored issues
show
Documentation introduced by
$entity is of type object<ElggEntity>, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
1131
				$file_forward_url = elgg_get_site_entity()->getURL()."file/group/{$object['forward_guid']}/all";
1132
			} elseif (elgg_instanceof('user', $entity)) {
0 ignored issues
show
Documentation introduced by
$entity is of type object<ElggEntity>, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
1133
				$file_forward_url = elgg_get_site_entity()->getURL()."file/owner/{$entity->username}";
1134
			} else {
1135
				$file_forward_url = $entity->getURL();
1136
			}
1137
1138
			$author = elgg_get_logged_in_user_entity();
1139
			$to_recipients = get_subscribers($dbprefix, elgg_get_logged_in_user_guid(), $entity->getGUID());
1140
			$to_recipients_site = get_site_subscribers($dbprefix, elgg_get_logged_in_user_guid(), $entity->getGUID());
1141
1142
			$subject = elgg_echo('cp_notify_usr:subject:new_content2', array(elgg_get_logged_in_user_entity()->username, 'file'), 'en');
1143
			$subject .= ' | '.elgg_echo('cp_notify_usr:subject:new_content2', array(elgg_get_logged_in_user_entity()->username, 'fichier', false), 'fr');
1144
	
1145
			$message = array(
1146
				'cp_topic' => $entity,
1147
				'cp_msg_type' => 'multiple_file',
1148
				'files_information' => $entity,
1149
				'files_uploaded' => $object['files_uploaded'], 
1150
				'cp_msg_type' => 'multiple_file',
1151
				'cp_topic_description_discussion' => 'Please view the files here',
1152
				'cp_topic_description_discussion2' => 'SVP voir les fichiers ici',
1153
			);
1154
1155
			
1156
			$content_entity = $object['files_uploaded'];
1157
			$object = get_entity($object['files_uploaded'][0]);
1158
			$author = elgg_get_logged_in_user_entity();
1159
			break;
1160
1161
		case 'discussion_reply':
1162
		case 'comment':
1163
1164
			// if mentions plugin is enabled... check to see if there were any mentions
1165
			$cp_mentioned_users = (elgg_is_active_plugin('mentions') && $object->getSubtype() !== 'messages') ? cp_scan_mentions($object) : "";
1166
1167
			// send mentioned users a notification
1168
			if (sizeof($cp_mentioned_users) > 0 && is_array($cp_mentioned_users)) {
1169
				for ($i = 0; $i < sizeof($cp_mentioned_users); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function sizeof() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
1170
					$cp_mentioned_user = $cp_mentioned_users[$i];
1171
					$mentioned_user = get_user_by_username(substr($cp_mentioned_user, 1));
1172
1173
					if (!$mentioned_user || $mentioned_user->gcdeactivate)
1174
						break;
1175
1176
					$subject = elgg_echo('cp_notify:subject:mention',array($object->getOwnerEntity()->name), 'en') .' | ' . elgg_echo('cp_notify:subject:mention',array($object->getOwnerEntity()->name), 'fr');
1177
					$message = array(
1178
						'cp_msg_type' => 'cp_mention_type',
1179
						'cp_author' => $object->getOwnerEntity()->name,
1180
						'cp_content_desc' => $object->description,
1181
						'cp_link' => $object->getURL(),
1182
						'cp_content' => $object,
1183
					);
1184
					$template = elgg_view('cp_notifications/email_template', $message);
1185
					$user_setting = elgg_get_plugin_user_setting('cpn_mentions_email', $mentioned_user->guid, 'cp_notifications');
1186
1187 View Code Duplication
					if (strcmp($user_setting, 'mentions_email') == 0) {
1188
						$user_setting = elgg_get_plugin_user_setting('cpn_set_digest', $mentioned_user->guid, 'cp_notifications');
1189
1190
						// send digest
1191
						if (strcmp($user_setting, "set_digest_yes") == 0) {
1192
							create_digest($object->getOwnerEntity(), "cp_mention", $object, $mentioned_user);
1193
1194
						// send email and site notification
1195
						} else {
1196
1197
							if (elgg_is_active_plugin('phpmailer'))
1198
								phpmailer_send( $mentioned_user->email, $mentioned_user->name, $subject, $template, NULL, true );
1199
							else
1200
								mail($mentioned_user->email,$subject,$template,cp_get_headers());
1201
						}
1202
					}
1203
1204
					$user_setting = elgg_get_plugin_user_setting('cpn_mentions_site', $mentioned_user->guid, 'cp_notifications');
1205
					$site_template = elgg_view('cp_notifications/site_template', $message);
1206
					if (strcmp($user_setting, 'mentions_site') == 0)
1207
						messages_send($subject, $site_template, $mentioned_user->guid, $site->guid, 0, true, false);
1208
				}
1209
			}
1210
1211
			// retrieve all necessary information for notification
1212
			$container_entity = $object->getContainerEntity();
1213
1214
			$user_comment = get_user($object->owner_guid);
1215
			$topic_container = $container_entity->getContainerEntity();
1216
1217
			// comment or reply in a group
1218
			if ($topic_container instanceof ElggGroup) {
1219
				if (strcmp($object->getSubtype(), 'discussion_reply') == 0) {
1220
					$subject = elgg_echo('cp_notify:subject:comments_discussion',array(gc_explode_translation($topic_container->name,'en')),'en');
1221
					$subject .= ' | '.elgg_echo('cp_notify:subject:comments_discussion',array(gc_explode_translation($topic_container->name,'fr')),'fr');
1222
				} else {
1223
					$subject = elgg_echo('cp_notify:subject:comments',array(gc_explode_translation($topic_container->name,'en')),'en');
1224
					$subject .= ' | '.elgg_echo('cp_notify:subject:comments',array(gc_explode_translation($topic_container->name,'fr')),'fr');
1225
				}
1226
1227
			// comment or reply in a user
1228
			} else {
1229
				$entity_residence = 'usr';
1230
				$subject = elgg_echo('cp_notify:subject:comments_user', array($topic_container->name), 'en');
1231
				$subject .= ' | '.elgg_echo('cp_notify:subject:comments_user', array($topic_container->name), 'fr');
1232
			}
1233
1234
1235
			$message = array(
1236
				'cp_container' => $entity_residence,
0 ignored issues
show
Bug introduced by
The variable $entity_residence does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
1237
				'cp_user_comment' => $user_comment,
1238
				'cp_topic' => $container_entity,
1239
				'cp_topic_type' => cp_translate_subtype($container_entity->getSubtype()),
1240
				'cp_comment' => $object,
1241
				'cp_msg_type' => 'cp_reply_type',
1242
			);
1243
1244
			// digest information purposes
1245
			$content_entity = $container_entity;
1246
			$author = $user_comment;
1247
1248
			$container = get_entity($container_entity->container_guid);
1249
1250
			// the user creating the content is automatically subscribed to it
1251 View Code Duplication
			if (elgg_instanceof($container, 'group')) {
1252
	 			if($container->isMember($user_comment)){
1253
					add_entity_relationship($object->getOwnerGUID(), 'cp_subscribed_to_email', $container_entity->getGUID());
1254
					add_entity_relationship($object->getOwnerGUID(), 'cp_subscribed_to_site_mail', $container_entity->getGUID());
1255
				}
1256
			} else {
1257
				add_entity_relationship($object->getOwnerGUID(), 'cp_subscribed_to_email', $container_entity->getGUID());
1258
				add_entity_relationship($object->getOwnerGUID(), 'cp_subscribed_to_site_mail', $container_entity->getGUID());
1259
			}
1260
1261
1262
			$to_recipients = get_subscribers($dbprefix, $object->getOwnerGUID(), $object->getContainerGUID());
1263
			$to_recipients_site = get_subscribers($dbprefix, $object->getOwnerGUID(), $object->getContainerGUID());
1264
			break;
1265
1266
		// micromissions / opportunities
1267
		case 'mission':
1268
1269
			$job_type = $object->job_type;		// only need to get this once
1270
			$role_type = $object->role_type;
1271
			$job_type_ids = getMissionTypeMetastringid($job_type, $role_type);
1272
			$opt_in_id = elgg_get_metastring_id('gcconnex_profile:opt:yes');
1273
1274
			// get users who want to be notified about new opportunities by site message and have opted in to this type of oppotrunity
1275
			$op_siteusers = get_data("SELECT ps.id, ps.entity_guid as entity_guid FROM {$dbprefix}private_settings ps JOIN {$dbprefix}metadata md ON ps.entity_guid = md.entity_guid WHERE ps.name = 'plugin:user_setting:cp_notifications:cpn_opportunities_site' AND ps.value = 'opportunities_site' AND md.name_id = {$job_type_ids} AND md.value_id = {$opt_in_id}");
1276
1277
			foreach ($op_siteusers as $result) {
1278
				$userid = $result->entity_guid;
1279
				$user_obj = get_user($userid);
1280
				$to_recipients_site[$userid] = $user_obj;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$to_recipients_site was never initialized. Although not strictly required by PHP, it is generally a good practice to add $to_recipients_site = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
1281
			}
1282
1283
			// get users who want to be notified about new opportunities by email and have opted in to this type of oppotrunity
1284
			$op_emailusers = get_data("SELECT ps.id, ps.entity_guid as entity_guid FROM {$dbprefix}private_settings ps JOIN {$dbprefix}metadata md ON ps.entity_guid = md.entity_guid WHERE ps.name = 'plugin:user_setting:cp_notifications:cpn_opportunities_email' AND ps.value = 'opportunities_email' AND md.name_id = {$job_type_ids} AND md.value_id = {$opt_in_id}");
1285
1286
			foreach ($op_emailusers as $result) {
1287
				$userid = $result->entity_guid;
1288
				$user_obj = get_user($userid);
1289
				$to_recipients[$userid] = $user_obj;
1290
			}
1291
1292
			$message = array(
1293
				'cp_topic' => $object,
1294
				'cp_msg_type' => 'new_mission',
1295
			);
1296
1297
			// digest information purposes
1298
			$content_entity = $object;
1299
			$author = $object->getOwnerEntity();
1300
1301
			$subject = elgg_echo('cp_new_mission:subject',array(),'en') . ' | ' . elgg_echo('cp_new_mission:subject',array(),'fr');
1302
1303
			// the user creating the content is automatically subscribed to it
1304
			add_entity_relationship(elgg_get_logged_in_user_guid(), 'cp_subscribed_to_email', $object->getGUID());
1305
			add_entity_relationship(elgg_get_logged_in_user_guid(), 'cp_subscribed_to_site_mail', $object->getGUID());
1306
			break; 
1307
1308
		case 'single_file_upload':
1309
1310
		default:
1311
1312
			// cyu - there is an issue with regards to auto-saving drafts
1313 View Code Duplication
			if (strcmp($object->getSubtype(),'blog') == 0) {
1314
			$get_error_info='draft blog';
1315
				
1316
				if (strcmp($object->status,'draft') == 0 || strcmp($object->status,'unsaved_draft') == 0) return;
1317
			}
1318
1319
			// the user creating the content is automatically subscribed to it (with exception that is not a widget, forum, etc..)
1320
			$cp_whitelist = array('blog', 'bookmarks', 'poll', 'groupforumtopic', 'image', 'idea', 'page', 'page_top', 'thewire', 'task_top', 'question', 'answer', 'cp_wire_image','event_calendar');
1321 View Code Duplication
			if (in_array($object->getSubtype(), $cp_whitelist)) {
1322
1323
				if ($object->getSubtype() == 'answer') {// subcribed to the question
1324
					add_entity_relationship($object->getOwnerGUID(), 'cp_subscribed_to_email', $object->getContainerGUID());
1325
					add_entity_relationship($object->getOwnerGUID(), 'cp_subscribed_to_site_mail', $object->getContainerGUID());
1326
				}
1327
				add_entity_relationship($object->getOwnerGUID(), 'cp_subscribed_to_email', $object->getGUID());
1328
				add_entity_relationship($object->getOwnerGUID(), 'cp_subscribed_to_site_mail', $object->getGUID());
1329
            }
1330
1331
			$user = get_user($object->owner_guid);
1332
			$group = $object->getContainerEntity();
1333
1334
			$group_name = htmlspecialchars(filter_tags(htmlspecialchars_decode(gc_explode_translation($group->name,'en'))));
1335
			$group_name2 = htmlspecialchars(filter_tags(htmlspecialchars_decode(gc_explode_translation($group->name,'fr'))));
1336
1337
			// fem and mas are different (so the subject should be reflected on which subtype is passed)
1338
			$subtypes_gender_subject = array(
1339
1340
				'blog' => elgg_echo('cp_notify:subject:new_content_mas',array("blogue",$group_name2),'fr'),
1341
				'bookmarks' => elgg_echo('cp_notify:subject:new_content_mas',array("signet",$group_name2),'fr'),
1342
				'file' => elgg_echo('cp_notify:subject:new_content_mas',array("fichier",$group_name2),'fr'),
1343
				'poll' => elgg_echo('cp_notify:subject:new_content_mas',array("sondage",$group_name2),'fr'),
1344
				'event_calendar' => elgg_echo('cp_notify:subject:new_content_mas',array("nouvel événement",$group_name2),'fr'),
1345
				'album' => elgg_echo('cp_notify:subject:new_content_mas',array("album d'image",$group_name2),'fr'),
1346
				'groupforumtopic' => elgg_echo('cp_notify:subject:new_content_fem',array("discussion",$group_name2),'fr'),
1347
				'image' => elgg_echo('cp_notify:subject:new_content_fem',array("image",$group_name2),'fr'),
1348
				'idea' => elgg_echo('cp_notify:subject:new_content_fem',array("idée",$group_name2),'fr'),
1349
				'page' => elgg_echo('cp_notify:subject:new_content_fem',array("page",$group_name2),'fr'),
1350
				'page_top' => elgg_echo('cp_notify:subject:new_content_fem',array("page",$group_name2),'fr'),
1351
				'task_top' => elgg_echo('cp_notify:subject:new_content_fem',array("task",$group_name2),'fr'),
1352
				'task' => elgg_echo('cp_notify:subject:new_content_fem',array("task",$group_name2),'fr'),
1353
        		'question' => elgg_echo('cp_notify:subject:new_content_fem',array("question",$group_name2),'fr'),
1354
        		'etherpad' => elgg_echo('cp_notify:subject:new_content_fem',array(elgg_echo('etherpad:single'),$group_name2),'fr')
1355
			);
1356
1357
			// if there is something missing, we can use a fallback string
1358
			$subj_gender = ($subtypes_gender_subject[$object->getSubtype()] == '') ? elgg_echo('cp_notify:subject:new_content_mas',array($user->name,$object->getSubtype(),$object->title),'fr') : $subtypes_gender_subject[$object->getSubtype()];
1359
1360
			// subscribed to content within a group
1361
			if ($object->getContainerEntity() instanceof ElggGroup) {
1362
				$subject = elgg_echo('cp_notify:subject:new_content',array(cp_translate_subtype($object->getSubtype()),$group_name),'en');
1363
				$subject .= ' | '.$subj_gender;
1364
1365
				$guidone = $object->getContainerGUID();
1366
				$author_id = $object->getOwnerGUID();
1367
				$content_id = $object->getContainerGUID();
1368
1369
			// subscribed to users or friends
1370
			} else {
1371
1372
				if (trim($object->title) === '') {
1373
1374
					/// check if poll_choice is found in the subtype
1375
					if (strpos($object->getSubtype(), "poll_choice") !== false)
1376
						return true;
1377
1378
					$subject = elgg_echo('cp_notify_usr:subject:new_content2',array($object->getOwnerEntity()->username,cp_translate_subtype($object->getSubtype())),'en');
1379
					$subject .= ' | '.elgg_echo('cp_notify_usr:subject:new_content2',array($object->getOwnerEntity()->username,cp_translate_subtype($object->getSubtype(), false)),'fr');
1380
1381
				} else {
1382
1383
					if (strcmp($object->getSubtype(), 'hjforumpost') != 0 || strcmp($object->getSubtype(), 'hjforumtopic') != 0) {
1384
						if ($object->getSubtype() == 'answer'){
1385
							$question_guid = $object->getContainerGUID();
1386
							$answer_entity = get_entity($question_guid);
1387
					
1388
							$subject = elgg_echo('cp_notify_usr:subject:new_content',array($object->getOwnerEntity()->username, cp_translate_subtype($object->getSubtype()), gc_explode_translation($answer_entity->title,'en')),'en');
1389
							$subject .= ' | '.elgg_echo('cp_notify_usr:subject:new_content_f',array($object->getOwnerEntity()->username, cp_translate_subtype($object->getSubtype(), false), gc_explode_translation($answer_entity->title,'fr')),'fr');
1390
						
1391
						} else if ($object->getSubtype() == 'etherpad'){
1392
							$subject = elgg_echo('cp_notify_usr:subject:new_content',array($object->getOwnerEntity()->username, elgg_echo('etherpad:single'), gc_explode_translation($object->title,'en')),'en');
1393
							$subject .= ' | '.elgg_echo('cp_notify_usr:subject:new_content_f',array($object->getOwnerEntity()->username, elgg_echo('etherpad:single'), gc_explode_translation($object->title,'fr')),'fr');
1394
						} else {
1395
							
1396
							$subject = elgg_echo('cp_notify_usr:subject:new_content',array($object->getOwnerEntity()->username, cp_translate_subtype($object->getSubtype()), gc_explode_translation($object->title,'en')),'en');
1397
							$subject .= ' | '.elgg_echo('cp_notify_usr:subject:new_content',array($object->getOwnerEntity()->username, cp_translate_subtype($object->getSubtype(), false), gc_explode_translation($object->title,'fr')),'fr');
1398
						}
1399
					}
1400
				}
1401
1402
				$guidone = $object->getOwnerGUID();
1403
				$author_id = $object->getOwnerGUID();
1404
				
1405
				// Get guid of the question
1406
				if($object->getSubtype = 'answer') {
1407
						$content_id = $object->getContainerGUID();
1408
				}
1409
			}
1410
	
1411
			// client wants the html tags stripped from the notifications
1412
			$object_description = ($object->description != strip_tags($object->description)) ? "" : $object->description;
1413
1414
			$message = array(
1415
				'cp_topic' => $object,
1416
				'cp_msg_type' => 'cp_new_type',
1417
				'cp_topic_description_discussion' => $object->description,
1418
				'cp_topic_description_discussion2' => $object->description2,
1419
			);
1420
1421
			$content_entity = $object;
1422
			$author = $object->getOwnerEntity();
1423
1424
			$to_recipients = get_subscribers($dbprefix, $author_id, $content_id);
0 ignored issues
show
Bug introduced by
The variable $content_id does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
1425
			$to_recipients_site = get_site_subscribers($dbprefix, $author_id, $content_id);
1426
			break;
1427
1428
	} // end of switch statement
1429
1430
1431
1432
	$notification_error_type = "";
1433
1434
	// check for empty subjects or empty content
1435
	if (empty($subject)) return false;
1436
	$subject = htmlspecialchars_decode($subject,ENT_QUOTES);
1437
1438
	/// send the email notification
1439
	if (is_array($to_recipients)) {
1440
1441
		foreach ($to_recipients as $to_recipient) {
1442
1443
			$recipient_user = get_user($to_recipient->guid);
1444
			//Nick - GCdeactivate - don't send notification
1445
			if ($to_recipient->guid == $author->guid || $recipient_user->gcdeactivate)
1446
				continue;
1447
1448
				$user_setting = elgg_get_plugin_user_setting('cpn_set_digest', $to_recipient->guid, 'cp_notifications');
1449
				
1450
1451
				
1452
					if (has_access_to_entity($object, $recipient_user) && $object->access_id != 0) {
0 ignored issues
show
Documentation introduced by
$recipient_user is of type object<ElggEntity>, but the function expects a object<ElggUser>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
1453
				
1454
					if (strcmp($user_setting, "set_digest_yes") == 0) {
1455
						create_digest($author, $switch_case, $content_entity, get_entity($to_recipient->guid));
0 ignored issues
show
Compatibility introduced by
get_entity($to_recipient->guid) of type object<ElggEntity> is not a sub-type of object<ElggUser>. It seems like you assume a child class of the class ElggEntity to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
1456
				
1457
					} else {
1458
				
1459
						$template = elgg_view('cp_notifications/email_template', $message);
1460
				
1461
						if (elgg_is_active_plugin('phpmailer')){
1462
							phpmailer_send( $to_recipient->email, $to_recipient->name, $subject, $template, NULL, true );
1463
							count_email_types($message['cp_msg_type']);
1464
						}
1465
						else
1466
							mail($to_recipient->email,$subject,$template,cp_get_headers());
1467
					}
1468
				}
1469
			
1470
1471
		}
1472
	}
1473
1474
	/// send site notifications
1475
	if (is_array($to_recipients_site)) {
1476
		
1477
		foreach ($to_recipients_site as $to_recipient) {
0 ignored issues
show
Bug introduced by
The variable $to_recipients_site does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
1478
			$user_setting = elgg_get_plugin_user_setting('cpn_set_digest', $to_recipient->guid, 'cp_notifications');
1479
			$recipient_user = get_user($to_recipient->guid);
1480
1481
			// check to see if the author is the recipient or recipient has the digest enabled
1482
			if ($to_recipient->guid == $author->guid || strcmp($user_setting, "set_digest_yes") == 0)
1483
				continue;
1484
1485
			if (has_access_to_entity($object, $recipient_user) && $object->access_id != 0) {
0 ignored issues
show
Documentation introduced by
$recipient_user is of type object<ElggEntity>, but the function expects a object<ElggUser>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
1486
1487
				$site_template = elgg_view('cp_notifications/site_template', $message);
1488
				messages_send($subject, $site_template, $to_recipient->guid, $site->guid, 0, true, false);
1489
			}
1490
		}
1491
	}
1492
1493
	// register the error, if either of the arrays are not populated
1494
	if (!is_array($to_recipients) || !is_array($to_recipients_site)) {
1495
		$error_message = "error: in cp_create_notification(), \$to_recipients or \$to_recipients_site is not array"."\r\n"."Author_id = ".($object->owner_guid?$object->owner_guid : 'N/A')."\r\n"."Content_id = ".($object->container_guid?$object->container_guid : 'N/A')."\r\n"."Content_id = ".$object->getContainerGUID();
1496
1497
		notification_logging($error_message);
1498
1499
	}
1500
1501
}
1502
1503
1504
function notification_logging($error_message) {
1505
	// logging mechanism
1506
	if (elgg_is_active_plugin('wet4')) {
1507
		elgg_load_library('GCconnex_logging');
1508
		$errStack = '';
1509
		$errType = 'custom';
1510
		gc_err_logging($error_message, $errStack, 'Notifications',$errType);
1511
	}
1512
}
1513
1514
/**
1515
 * get all the users that are subscribed to a specified entity (content or user id)
1516
 * and return the array of users
1517
 *
1518
 * @param string 			$dbprefix
1519
 * @param integer 			$user_guid
1520
 * @param optional integer 	$entity_guid
1521
1522
 * @param email vs site_mail
1523
 * @return Array <ElggUser>
1524
1525
 */
1526
1527 View Code Duplication
function get_subscribers($dbprefix, $user_guid, $entity_guid = '') {
1528
	$subscribed_to = ($entity_guid != '') ? $entity_guid : $user_guid;
1529
1530
	$query = "	SELECT DISTINCT u.guid, u.email, u.username, u.name
1531
				FROM {$dbprefix}entity_relationships r LEFT JOIN {$dbprefix}users_entity u ON r.guid_one = u.guid
1532
				WHERE r.guid_one <> {$user_guid} AND r.relationship = 'cp_subscribed_to_email' AND r.guid_two = {$subscribed_to}";
1533
1534
	return get_data($query);
1535
}
1536
1537 View Code Duplication
function get_site_subscribers($dbprefix, $user_guid, $entity_guid = '') {
1538
	$subscribed_to = ($entity_guid != '') ? $entity_guid : $user_guid;
1539
1540
	$query = " SELECT DISTINCT u.guid, u.email, u.username, u.name
1541
	FROM {$dbprefix}entity_relationships r LEFT JOIN {$dbprefix}users_entity u ON r.guid_one = u.guid
1542
	WHERE r.guid_one <> {$user_guid} AND r.relationship = 'cp_subscribed_to_site_mail' AND r.guid_two = {$subscribed_to}";
1543
1544
	return get_data($query);
1545
}
1546
1547
1548
/**
1549
 * setup crontab either on a daily or weekly basis
1550
 * get users who are subscribed to digest
1551
 * run crontab, retrieve users, send digest, reset timer (update timestamp)
1552
 *
1553
 * @param string $hook    The name of the plugin hook
1554
 * @param string $type    The type of the plugin hook
0 ignored issues
show
Bug introduced by
There is no parameter named $type. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
1555
 * @param mixed  $value   The current value of the plugin hook
0 ignored issues
show
Bug introduced by
There is no parameter named $value. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
1556
 * @param mixed  $params  Data passed from the trigger
1557
 */
1558
function cp_digest_weekly_cron_handler($hook, $entity_type, $return_value, $params) {
1559
	echo "<p>Starting up the cron job for the Notifications (cp_notifications plugin)</p>";
1560
	elgg_load_library('elgg:gc_notification:functions');
1561
	
1562
	initialize_queue('weekly');
1563
1564
	// TODO: write the functions above and rewrite the email handler below
1565
	cp_digest_email_handler($hook, $entity_type, $return_value, $params, 'weekly');
1566
}
1567
1568
1569
1570
/**
1571
 * setup crontab either on a daily or weekly basis
1572
 * get users who are subscribed to digest
1573
 * run crontab, retrieve users, send digest, reset timer (update timestamp)
1574
 *
1575
 * @param string $hook    The name of the plugin hook
1576
 * @param string $entity_type    The type of the plugin hook
1577
 * @param mixed  $return_value   The current value of the plugin hook
1578
 * @param mixed  $params  Data passed from the trigger
1579
 */
1580
function cp_digest_daily_cron_handler($hook, $entity_type, $return_value, $params) {
1581
	echo "<p>Starting up the cron job for the Notifications (cp_notifications plugin)</p>";
1582
	elgg_load_library('elgg:gc_notification:functions');
1583
1584
	initialize_queue('daily');
1585
1586
	cp_digest_email_handler($hook, $entity_type, $return_value, $params, 'daily');
1587
}
1588
1589
/**
1590
 * setup crontab either on a daily or weekly basis
1591
 * get users who are subscribed to digest
1592
 * run crontab, retrieve users, send digest, reset timer (update timestamp)
1593
 *
1594
 * @param string $hook    The name of the plugin hook
1595
 * @param string $entity_type    The type of the plugin hook
1596
 * @param mixed  $return_value   The current value of the plugin hook
1597
 * @param mixed  $params  Data passed from the trigger
1598
 */
1599
function cp_digest_email_handler($hook, $entity_type, $return_value, $params, $cron_freq) {
1600
	$dbprefix = elgg_get_config('dbprefix');
1601
1602
	while( $user_guid = dequeue() ) {
1603
		$user = get_entity($user_guid);
1604
		if($user->gcdeactivate)
1605
			continue;
1606
		$frequency = elgg_get_plugin_user_setting('cpn_set_digest_frequency', $user->guid, 'cp_notifications');
1607
1608
		if ($user instanceof ElggUser && strcmp($frequency,'set_digest_' . $cron_freq) == 0 ) {
1609
			$digest_array = array();
1610
1611
			$query = "SELECT * FROM notification_digest WHERE user_guid = {$user->guid}";
1612
			$digest_items = get_data($query);
1613
1614
			$language_preference = (strcmp(elgg_get_plugin_user_setting('cpn_set_digest_language', $user->guid, 'cp_notifications'),'set_digest_en') == 0) ? 'en' : 'fr';
1615
1616
			foreach ($digest_items as $digest_item) {
1617
1618
				// check to make sure that the string is encoded with base 64 or not (legacy)
1619
				if (isJson($digest_item->notification_entry))
1620
					$notification_entry = $digest_item->notification_entry;
1621
				else
1622
					$notification_entry = base64_decode($digest_item->notification_entry);
1623
1624
1625
				if ($digest_item->entry_type === 'group') 
1626
					$digest_array[$digest_item->entry_type][base64_decode($digest_item->group_name)][$digest_item->action_type][$digest_item->entity_guid] = $notification_entry;
1627
				else
1628
					$digest_array[$digest_item->entry_type][$digest_item->action_type][$digest_item->entity_guid] = $notification_entry;
1629
1630
			}
1631
1632
			$subject = elgg_echo('cp_newsletter:subject:' . $cron_freq,$language_preference);
0 ignored issues
show
Documentation introduced by
$language_preference is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
1633
1634
			// if the array is empty, send the empty template
1635
			if (sizeof($digest_array) > 0 || !empty($digest_array))
1636
				$template = elgg_view('cp_notifications/newsletter_template', array('to' => $user, 'newsletter_content' => $digest_array));
1637
			else
1638
				$template = elgg_view('cp_notifications/newsletter_template_empty', array('to' => $user));
1639
1640
1641
			/// e-mail providers can potentially break html codes because it exceeds a limit (set by the inbox)
1642
			/// REFERENCE: https://stackoverflow.com/questions/12216228/html-email-annoying-line-breaking
1643
			$template = str_replace("<", "\r\n<", $template);
1644
1645
			echo $template . "<br/><br/>";
1646
1647
			if (elgg_is_active_plugin('phpmailer'))
1648
				phpmailer_send($user->email, $user->name, $subject, $template, NULL, true );
1649
			else
1650
				mail($user->email, $subject, $template, cp_get_headers());
1651
1652
1653
			// delete and clean up the notification, already sent so we don't need to keep it anymore
1654
			$query = "DELETE FROM notification_digest WHERE user_guid = {$user->getGUID()}";
1655
			$result = delete_data($query);
1656
1657
			count_email_types("digest_$cron_freq");
1658
		}
1659
	}
1660
}
1661
1662
1663
/**
1664
 * check if user has access then prepare the notification and send (if applicable)
1665
 *
1666
 * @param ElggObject					$entity			entity that has been created
1667
 * @param array(guid, email, username)	$to_user		recipient
1668
 * @param array(various)				$message		message that will be in the notification
0 ignored issues
show
Documentation introduced by
The doc-type array(various) could not be parsed: Expected "|" or "end of type", but got "(" at position 5. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
1669
 * @param int							$guid_two		author or group id (check if subscribe to friend or group)
1670
 */
1671
function cp_notification_preparation_send($entity, $to_user, $message, $guid_two, $subject) {
1672
1673
	$template = elgg_view('cp_notifications/email_template', $message);
1674
1675
	// TODO: fix up mission
1676
	if (strcmp($entity->getSubtype(),'mission') == 0) {
1677
		// send out emails
1678
		if ($to_user->getGUID() != elgg_get_logged_in_user_guid()) { // prevents notification to be sent to the sender
1679
			if ($to_user instanceof ElggUser) {
1680
				if (elgg_is_active_plugin('phpmailer'))
1681
					phpmailer_send( $to_user->email, $to_user->name, $subject, $template, NULL, true );
1682
				else
1683
					mail($to_user->email,$subject,$template,cp_get_headers());
1684
			}
1685
1686
		} else {
1687
			// check if user has access to the content (DO NOT send if user has no access to this object)
1688
1689
			if (has_access_to_entity($entity, $recipient_user) && $object->access_id != 0) {
0 ignored issues
show
Bug introduced by
The variable $recipient_user does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $object does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
1690
				//  GCCON-175: assemble the email content with correct username (for notification page)
1691
				$message['user_name'] = $to_user->username;
1692
1693
				// check if user subscribed to receiving notifications
1694
				if (check_entity_relationship($to_user->guid, 'cp_subscribed_to_email', $guid_two))
1695
				{
1696
					if (elgg_is_active_plugin('phpmailer'))
1697
						phpmailer_send( $to_user->email, $to_user->name, $subject, $template, NULL, true );
1698
					else
1699
						mail($to_user->email,$subject,$template,cp_get_headers());
1700
				}
1701
1702
				// send a site notification
1703
				if (check_entity_relationship($to_user->guid, 'cp_subscribed_to_site_mail', $guid_two)) {
1704
					messages_send($subject, $template, $to_recipient->guid, $site->guid, 0, true, false);
0 ignored issues
show
Bug introduced by
The variable $to_recipient does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $site does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
1705
				}
1706
			} // end if (check for access)
1707
1708
		}
1709
	}
1710
}
1711
1712
1713
/**
1714
 * cp_send_new_password_request
1715
 *
1716
 * In order to modify core code, this action had to be overwritten.
1717
 * This is a mirror image of the core function: /engine/classes/Elgg/PasswordService.php
1718
 *
1719
 * Only need to send an e-mail notification out, no need to send a site-mail
1720
 *
1721
 * @param ElggUser 	$user 	user entity
1722
 */
1723
function cp_send_new_password_request($user) {
1724
	if (!$user instanceof ElggUser)
1725
		return false;
1726
1727
	// generate code
1728
	$code = generate_random_cleartext_password();
1729
	$user->setPrivateSetting('passwd_conf_code', $code);
1730
	$user->setPrivateSetting('passwd_conf_time', time());
1731
1732
	$link = elgg_get_site_url()."changepassword?u={$user->guid}&c={$code}";
1733
	$ip_address = _elgg_services()->request->getClientIp();
1734
1735
	// we don't need to check if the plugin (cp_notifications) is enabled here
1736
	$message = array(
1737
		'cp_password_request_user' => $user->username,
1738
		'cp_password_request_ip' => $ip_address,
1739
		'cp_password_request_url' => $link,
1740
		'cp_msg_type' => 'cp_forgot_password',
1741
	);
1742
1743
	$subject = elgg_echo('cp_notify:subject:forgot_password', array(), "en");
1744
	$subject .= ' | '.elgg_echo('cp_notify:subject:forgot_password', array(), "fr");
1745
	$template = elgg_view('cp_notifications/email_template', $message);
1746
1747
	if (elgg_is_active_plugin('phpmailer'))
1748
		phpmailer_send( $user->email, $user->name, $subject, $template );
1749
	else
1750
		mail($user->email,$subject,$template,cp_get_headers());
1751
}
1752
1753
1754
/*
1755
 * cp_membership_request
1756
 *
1757
 * replaced the event (see init()) so we can send out notifications through this plugin instead
1758
 *
1759
 */
1760
function cp_membership_request($event, $type, $object) { 	// MUST always be sending notification
1761
	$request_user = get_user($object->guid_one); 			// user who sends request to join
1762
	$group_request = get_entity($object->guid_two);			// group that is being requested
1763
1764
	$message = array(
1765
		'cp_group_req_user' => $request_user,
1766
		'cp_group_req_group' => $group_request,
1767
		'cp_msg_type' => 'cp_closed_grp_req_type',
1768
	);
1769
	$template = elgg_view('cp_notifications/email_template', $message);
1770
	$subject = elgg_echo('cp_notify:subject:group_request',array($request_user->name, gc_explode_translation($group_request->name,'en')),'en');
1771
	$subject .= ' | '.elgg_echo('cp_notify:subject:group_request',array($request_user->name, gc_explode_translation($group_request->name,'fr')),'fr');
1772
1773
	$to_user = get_user($group_request->owner_guid);
1774
	if (elgg_is_active_plugin('phpmailer')) {
1775
		phpmailer_send( $to_user->email, $to_user->name, $subject, $template, NULL, true );
1776
	} else {
1777
		mail($to_user->email,$subject,$template,cp_get_headers());
1778
	}
1779
	messages_send($subject, $template, $to_user->guid, elgg_get_site_entity()->guid, 0, true, false);
1780
}
1781
1782
1783
1784
1785
/**
1786
 * intercepts all email and stops emails from sending
1787
 *
1788
 * @param string $hook    The name of the plugin hook
1789
 * @param string $type    The type of the plugin hook
1790
 * @param mixed  $value   The current value of the plugin hook
0 ignored issues
show
Bug introduced by
There is no parameter named $value. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
1791
 * @param mixed  $params  Data passed from the trigger
1792
 */
1793
function cpn_email_handler_hook($hook, $type, $notification, $params) {
1794
	return false;
1795
}
1796
1797
1798
/**
1799
 * implements the icons (likes, in this case) within the context of the entity
1800
 * TODO: make the likes button act as AJAX
1801
 *
1802
 * @param string $hook    The name of the plugin hook
1803
 * @param string $type    The type of the plugin hook
1804
 * @param mixed  $value   The current value of the plugin hook
0 ignored issues
show
Bug introduced by
There is no parameter named $value. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
1805
 * @param mixed  $params  Data passed from the trigger
1806
 *
1807
 * @return mixed if not null, this will be the new value of the plugin hook
1808
 */
1809
function notify_entity_menu_setup($hook, $type, $return, $params) {
1810
	$entity = $params['entity'];
1811
	$do_not_subscribe_list = array('comment','discussion_reply','widget');
1812
	if (elgg_in_context('widgets') || in_array($entity->getSubtype(), $do_not_subscribe_list))  return $return;
1813
1814
	// cyu - check for everything to put the bell thingy (xor)
1815
	$allow_subscription = false;
1816
	if ( $entity->getContainerEntity() instanceof ElggGroup ) {
1817
		$allow_subscription = ($entity->getContainerEntity()->isMember(elgg_get_logged_in_user_entity()) == 1 ) ? true : false;
1818
1819
	} else if ($entity instanceof ElggGroup) {
1820
		$allow_subscription =  ($entity->isMember(elgg_get_logged_in_user_entity()) == 1 ) ? true : false;
1821
1822
	} else if ( $entity->getContainerEntity() instanceof ElggUser )
1823
		$allow_subscription = true;
1824
1825
	if ($entity instanceof ElggGroup) {
1826
		$entType = 'group';
1827
		if($entity->title3){
1828
				$entName = gc_explode_translation($entity->title3, get_current_language());
1829
		}else{
1830
				$entName = $entity->name;
1831
		}
1832
	} else {
1833
		if(!in_array($entity->getSubtype(), array('comment', 'discussion_reply', 'thewire'))){
1834
			if($entity->title3){
1835
					$entName = gc_explode_translation($entity->title3, get_current_language());
1836
			}else{
1837
					$entName = $entity->title;
1838
			}
1839
		} else {
1840
			$entName = $entity->getOwnerEntity()->name;
1841
		}
1842
		$entType = $entity->getSubtype();
1843
	}
1844
1845
	if ($allow_subscription && elgg_is_logged_in()) {
1846
	    if ( check_entity_relationship(elgg_get_logged_in_user_guid(), 'cp_subscribed_to_email', $entity->getGUID()) || check_entity_relationship(elgg_get_logged_in_user_guid(), 'cp_subscribed_to_site_mail', $entity->getGUID()) ) {
1847
1848
1849
			$bell_status = (elgg_is_active_plugin('wet4')) ? '<i class="icon-unsel fa fa-lg fa-bell"><span class="wb-inv">'.elgg_echo('entity:unsubscribe:link:'.$entType, array($entName)).'</span></i>' : elgg_echo('cp_notify:stop_subscribe');
1850
1851
1852
		    $return[] = ElggMenuItem::factory(array(
1853
			    'name' => 'unset_notify',
1854
			    'href' => elgg_add_action_tokens_to_url("/action/cp_notify/unsubscribe?guid={$entity->guid}"),
1855
			    'text' => $bell_status,
1856
			    'title' => elgg_echo('cp_notify:unsubBell'),
1857
			    'priority' => 1000,
1858
			    'class' => 'bell-subbed',
1859
			    'item_class' => ''
1860
		    ));
1861
1862
	    } else {
1863
1864
		    $bell_status = (elgg_is_active_plugin('wet4')) ? '<i class="icon-unsel fa fa-lg fa-bell-slash-o"><span class="wb-inv">'.elgg_echo('entity:subscribe:link:'.$entType, array($entName)).'</span></i>' : elgg_echo('cp_notify:start_subscribe');
1865
1866
1867
		    $return[] = ElggMenuItem::factory(array(
1868
			    'name' => 'set_notify',
1869
			    'href' => elgg_add_action_tokens_to_url("/action/cp_notify/subscribe?guid={$entity->guid}"),
1870
			    'text' => $bell_status,
1871
			    'title' => elgg_echo('cp_notify:subBell'),
1872
			    'priority' => 1000,
1873
			    'class' => '',
1874
			    'item_class' => ''
1875
		    ));
1876
		}
1877
	}
1878
	return $return;
1879
}
1880
1881