Issues (2473)

Branch: master

Security Analysis    no vulnerabilities found

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

mod/cp_notifications/start.php (55 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
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
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
154
/**
155
 *
156
 * This contains all the notifications that are required to be triggered from the original action files. Filepaths
157
 * are documented beside each case (and in the readme.md file)
158
 *
159
 * @param string $hook    The name of the plugin hook
160
 * @param string $type    The type of the plugin hook
161
 * @param mixed  $value   The current value of the plugin hook
162
 * @param mixed  $params  Data passed from the trigger
163
 */
164
function cp_overwrite_notification_hook($hook, $type, $value, $params) {
165
166
	elgg_load_library('elgg:gc_notification:functions');
167
	$cp_msg_type = trim($params['cp_msg_type']);
168
	$to_recipients = array();
169
	$email_only = false;
170
	$add_to_sent = false;
171
	$embed_image = NULL;
172
	$sender_guid = elgg_get_site_entity()->guid;
173
174
	switch ($cp_msg_type) {
175
176
		/// EMAIL NOTIFICATIONS ONLY (password reset, registration, etc)
177
		case 'cp_friend_invite': // invitefriends/actions/invite.php
178
			$message = array(
179
				'cp_msg_type' => $cp_msg_type,
180
				'cp_from_user' => $params['cp_from']->name,
181
				'cp_to_user' => $params['cp_to'],
182
				'cp_join_url' => $params['cp_join_url'],
183
				'cp_msg' => $params['cp_email_msg'],
184
				'_user_e-mail' => $params['cp_to'],
185
			);
186
			$subject = elgg_echo('cp_notify:subject:invite_new_user',array(),'en') . ' | ' . elgg_echo('cp_notify:subject:invite_new_user',array(),'fr');
187
			$template = elgg_view('cp_notifications/email_template', $message);
188
			$site_template = elgg_view('cp_notifications/site_template', $message);
189
			$user_obj = get_user_by_email($params['cp_to']);
190
			$info_notif = 'cp_friend_invite';
191
			$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());
192
			return true;
193
194
195
		case 'cp_forgot_password':	// send email notifications only - /wet4/users/action/password
196
			cp_send_new_password_request($params['cp_password_requester']);
197
			return true;
198
199
		case 'cp_group_invite_email':
200
		case 'cp_group_invite':	// group_tools/lib/functions.php (returns user's email, so return after mail is sent out)
201
202
			$group_name = $params['cp_invite_to_group']['name'];
203
			if (elgg_is_active_plugin('wet4')) {
204
				$group_name_en =  htmlspecialchars(filter_tags(htmlspecialchars_decode(gc_explode_translation($group_name, 'en'))));
205
				$group_name_fr = htmlspecialchars(filter_tags(htmlspecialchars_decode(gc_explode_translation($group_name, 'fr'))));
206
			}
207
208
			$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
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...
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...
209
			$subject = htmlspecialchars_decode($subject, ENT_QUOTES);
210
211
			$user_email = $params['cp_invitee'];
212
213
214
			$message = array(
215
				'cp_email_invited' => $params['cp_invitee'],
216
				'cp_email_invited_by' => $params['cp_inviter'],
217
				'cp_group_invite' => $params['cp_invite_to_group'],
218
				'cp_invitation_non_user_url' => $params['cp_invitation_nonuser_url'],
219
				'cp_invitation_url' => $params['cp_invitation_url'],
220
				'cp_invitation_code' => $params['cp_invitation_code'],
221
				'cp_invitation_msg' => $params['cp_invite_msg'],
222
				'cp_msg_type' => $cp_msg_type,
223
				'_user_e-mail' => $params['cp_invitee'],
224
				'group_link' => $params['group_link'],
225
				'cp_user_profile' => $params['cp_user_profile'],
226
			);
227
			$template = elgg_view('cp_notifications/email_template', $message);
228
229
			// invitation through email, user might not exist
230
			if ($cp_msg_type === 'cp_group_invite') {
231
				$site_template = elgg_view('cp_notifications/site_template', $message);
232
				if ($params['cp_invitee'] instanceof ElggUser)
233
					$send_to_user = get_user_by_email($params['cp_invitee']);
234
				else 
235
					$send_to_user = $params['cp_invitee'];
236
			} 
237
			$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
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...
238
			return true;
239
240
		case 'cp_useradd': // cp_notifications/actions/useradd.php
241
			$message = array(
242
				'cp_msg_type' => $cp_msg_type,
243
				'cp_user_name' => $params['cp_user_name'],
244
				'cp_site_name' => $params['cp_site_name'],
245
				'cp_site_url' => $params['cp_site_url'],
246
				'cp_username' => $params['cp_username'],
247
				'cp_password' => $params['cp_password'],
248
			);
249
			$to_recipients[] = $params['cp_user'];
250
			$info_notif = 'cp_useradd';
251
			
252
			$subject = elgg_echo('cp_notify:subject:add_new_user',array(),'en') . ' | ' . elgg_echo('cp_notify:subject:add_new_user',array(),'fr');
253
			$email_only = true;
254
			break;
255
256
		case 'cp_validate_user': // uservalidationbyemail/lib/functions.php
257
			$message = array(
258
				'cp_validate_user' => $params['cp_validate_user'],
259
				'cp_validate_url' => $params['cp_validate_url'],
260
				'cp_msg_type' => $cp_msg_type
261
			);
262
			$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');
263
			$to_recipients[] = get_user($params['cp_validate_user']['guid']);
264
			$info_notif = 'cp_validate_user';
265
			$email_only = true;
266
			break;
267
268
		case 'cp_site_msg_type':	// messages/actions/messages/send.php
269
			$add_to_sent = true;
270
			$sender_guid = $params['cp_from']['guid'];
271
			$to_recipients[] = get_user($params['cp_to']['guid']);
272
			$info_notif = 'cp_site_msg_type';			
273
			$subject = $params['cp_topic_title'];
274
			$message = array(
275
				'cp_msg_title' => $params['cp_topic_title'],
276
				'cp_msg_content' => $params['cp_topic_description'],
277
				'cp_sender' => $params['cp_from']['name'],
278
				'cp_msg_url' => $params['cp_topic_url'],
279
				'cp_msg_type' => 'cp_site_msg_type',
280
			);
281
			break;
282
283
		/// NORMAL NOTIFICATIONS that will send out both email and site notification
284
285
		case 'cp_wire_image':
286
287
			/// the function thewire_image_get_attachments will return an entity
288
			$embed_image = $params['wire_imagedata_loc'];
289
			$author = elgg_get_logged_in_user_entity();
290
			$message = array(
291
				'cp_msg_type' => $params['cp_msg_type'],
292
				'wire_entity' => $params['wire_entity'],
293
				'imagedata_location' => $params['wire_imagedata_loc'],
294
				'author' => $author,
295
			);
296
297
298
			$subject = elgg_echo('cp_notifications:mail_body:subtype:thewireSubj', array($author->name, 'Wire'), 'en').' | ';
299
			$subject .= elgg_echo('cp_notifications:mail_body:subtype:thewireSubj',array($author->name, 'Fil'),'fr');
300
			$content_entity = $params['wire_entity'];
301
302
			$query = "SELECT * FROM elggentity_relationships WHERE relationship = 'cp_subscribed_to_email' AND guid_two = {$author->getGUID()}";
303
			$users = get_data($query);
304
305
			foreach ($users as $user) {
306
				$to_recipients[$user->guid_one] = get_entity($user->guid_one);
307
			}
308
			$info_notif = 'cp_wire_image';			
309
			
310
			break;
311
312
		case 'cp_wire_share': // thewire_tools/actions/add.php
313
314
315
			$message = array(
316
				'cp_msg_type' => $cp_msg_type,
317
				'cp_shared_by' => $params['cp_shared_by'],
318
				'cp_content_reshare' => $params['cp_content_reshared'],
319
				'cp_content' => $params['cp_content'],
320
				'cp_recipient' => $params['cp_recipient'],
321
				'cp_wire_url' => $params['cp_wire_url'],
322
				
323
				'imagedata_location' => $params['wire_imagedata_loc'],
324
			);
325
326
			$parent_item = $params['cp_content']->getContainerEntity();
327
328
			$subject = elgg_echo('cp_notify:wireshare:subject',array($params['cp_shared_by']->name),'en').' | ';
329
			$subject .= elgg_echo('cp_notify:wireshare:subject',array($params['cp_shared_by']->name),'fr');
330
331
			$to_recipients[] = $params['cp_recipient'];
332
			$info_notif = 'cp_wire_share';						
333
			$content_entity = $params['cp_content_reshared'];
334
			$author = $params['cp_shared_by'];
335
			$content_url = $params['cp_content_reshared']->getURL();
336
			break;
337
338
339
		case 'cp_messageboard': // messageboard/actions/add.php
340
			$message = array(
341
				'cp_msg_type' => $cp_msg_type,
342
				'cp_message_content' => $params['cp_message_content'],
343
				'cp_writer_name' => $params['cp_writer']->name,
344
				'cp_owner_profile' => $params['cp_recipient']->getURL(),
345
			);
346
			$subject = elgg_echo('cp_notify:messageboard:subject',array(),'en') . ' | ' . elgg_echo('cp_notify:messageboard:subject',array(),'fr');
347
			$to_recipients[] = $params['cp_recipient'];
348
			$info_notif = 'cp_messageboard';			
349
			$content_entity = $params['cp_message_content'];
350
			$author = $params['cp_writer'];
351
			break;
352
353
		case 'cp_add_grp_operator': // group_operators/actions/group_operators/add.php (adds group operator)
354
			$message = array(
355
				'cp_msg_type' => $cp_msg_type,
356
				'cp_to_operator' => $params['cp_to_operator'],
357
				'cp_who_made_operator' => $params['cp_who_made_operator'],
358
				'cp_group_name' => $params['cp_group_name'],
359
				'cp_who_made_operator' => $params['cp_who_made_operator'],
360
				'cp_group_url' => $params['cp_group_url'],
361
			);
362
			$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');
363
			$to_recipients[] = $params['cp_to_user'];
364
			$info_notif = 'cp_add_grp_operator';						
365
			break;
366
367
368
		case 'cp_grp_admin_transfer': // group_tools/lib/functions.php (this is transfer of group owner through group edit)
369
			$message = array(
370
				'cp_msg_type' => $cp_msg_type,
371
				'cp_group_name' => $params['cp_group_name'],
372
				'cp_group_url' => $params['cp_group_url'],
373
				'cp_appointer' => $params['cp_appointer']
374
			);
375
			$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');
376
			$to_recipients[] = $params['cp_new_owner_user'];
377
			$info_notif = 'cp_grp_admin_transfer';									
378
			break;
379
380
381
		case 'cp_wire_mention': // thewire_tools/lib/events.php (TODO: share option in notifications setting)
382
383
			$message = array(
384
				'cp_mention_by' => $params['cp_mention_by'],
385
				'cp_view_mention' => $params['cp_view_your_mention'],
386
				'cp_msg_type' => $cp_msg_type,
387
				'cp_wire_mention_url' => $params['cp_wire_mention_url'],
388
			);
389
390
			$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');
391
			$to_recipients[] = $params['cp_send_to'];
392
			$info_notif = 'cp_wire_mention';									
393
			$content_entity = $params['cp_wire_entity'];
394
			$author = $content_entity->getOwnerEntity();
395
			break;
396
397
398
		case 'cp_friend_approve': // friend_request/actions/approve
399
			$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');
400
			$message = array(
401
				'cp_approver' => $params['cp_approver'],
402
				'cp_approver_profile' => $params['cp_approver_profile'],
403
				'cp_msg_type' => $cp_msg_type,
404
405
				);
406
			$to_recipients[] = get_user($params['cp_request_guid']);
407
			$info_notif = 'cp_friend_approve';									
408
			$content_entity = $params['object'];
409
			$author = $params['object'];
410
			break;
411
412
413
		case 'cp_group_add':	// group_tools/lib/functions.php OR groups/actions/groups/membership/add.php ????
414
			$to_recipients[] = $params['cp_user_added'];
415
			$info_notif = 'cp_group_add';												
416
			$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');
417
			$message = array(
418
				'cp_user_added' => $params['cp_user_added'],
419
				'cp_group' => $params['cp_group'],
420
				'cp_message' => $params['cp_added_msg'],
421
				'cp_msg_type' => $cp_msg_type
422
			);
423
			break;
424
425
426
		case 'cp_group_invite': // group_tools/lib/functions.php
427
428
			$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');
429
			$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');
430
431
			$message = array(
432
				'cp_group_invite_from' => $params['cp_invitee'], // user we're inviting
433
				'cp_group_invite_to' => $params['cp_inviter'], // user inviting others
434
				'cp_group' => $params['cp_invite_to_group'],
435
				'cp_invitation_url' => $params['cp_invitation_url'],
436
				'cp_invitation_msg' => $params['cp_invite_msg'],
437
				'cp_msg_type' => $cp_msg_type
438
			);
439
			$to_recipients[] = get_user($params['cp_invitee']['guid']);
440
			$info_notif = 'cp_group_invite';												
441
			$content_entity = $params['cp_invite_to_group'];
442
			$author = $params['cp_inviter'];
443
			break;
444
445
446
		case 'cp_group_mail': // group_tools/actions/mail.php
447
		
448
			$message = array(
449
				'cp_group' => $params['cp_group'],
450
				'cp_group_subject' => $params['cp_group_subject'],
451
				'cp_group_message' => $params['cp_group_message'],
452
				'cp_msg_type' => $cp_msg_type
453
			);
454
			$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');
455
			
456
			foreach ($params['cp_group_mail_users'] as $to_user) {
457
				$to_recipients[$to_user] = get_user($to_user);
458
			}
459
			$info_notif = 'cp_group_mail';															
460
			break;
461
462
463
		case 'cp_friend_request': // friend_request/lib/events.php
464
			$message = array(
465
				'cp_friend_request_from' => $params['cp_friend_requester'],
466
				'cp_friend_request_to' => $params['cp_friend_receiver'],
467
				'cp_friend_invitation_url' => $params['cp_friend_invite_url'],
468
				'cp_msg_type' => $cp_msg_type
469
			);
470
			$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');
471
			$to_recipients[] = get_user($params['cp_friend_receiver']['guid']);
472
			$info_notif = 'cp_friend_request';												
473
474
			$content_entity = $params['cp_relationship'];
475
			$author = $params['cp_friend_requester'];
476
477
			break;
478
479
480 View Code Duplication
		case 'cp_hjpost': // gcforums/actions/gcforums/create.php
481
			$message = array(
482
				'cp_hjpost_author' => $params['cp_topic_author'],
483
				'cp_hjpost_title' => $params['cp_topic_title'],
484
				'cp_hjpost_description' => $params['cp_topic_description'],
485
				'cp_hjpost_url' => $params['cp_topic_url'],
486
				'cp_msg_type' => $cp_msg_type
487
				);
488
			$t_user = $params['cp_subscribers'];
489
			$subject = elgg_echo('cp_notify:subject:hjpost',array($params['cp_topic_author'],$params['cp_topic_title']),'en');
490
			$subject .= ' | '.elgg_echo('cp_notify:subject:hjpost',array($params['cp_topic_author'],$params['cp_topic_title']),'fr');
491
			foreach ($t_user as $s_uer)
492
				$to_recipients[] = get_user($s_uer);
493
494
			$info_notif = 'cp_hjpost';												
495
			$content_entity = $params['cp_post'];
496
			$content_url = $params['cp_topic_url'];
497
			$author = $params['cp_post']->getOwnerEntity();
498
499
			break;
500
501
502 View Code Duplication
		case 'cp_hjtopic': // gcforums/actions/gcforums/create.php
503
			$message = array(
504
				'cp_hjtopic_author' => $params['cp_topic_author'],
505
				'cp_hjtopic_title' => $params['cp_topic_title'],
506
				'cp_hjtopic_description' => $params['cp_topic_description'],
507
				'cp_hjtopic_url' => $params['cp_topic_url'],
508
				'cp_msg_type' => $cp_msg_type
509
				);
510
			$t_user = $params['cp_subscribers'];
511
			$subject = elgg_echo('cp_notify:subject:hjtopic',array($params['cp_topic_author'],$params['cp_topic_title']),'en');
512
			$subject .= ' | '.elgg_echo('cp_notify:subject:hjtopic',array($params['cp_topic_author'],$params['cp_topic_title']),'fr');
513
			foreach ($t_user as $s_uer)
514
				$to_recipients[] = get_user($s_uer);
515
516
			$info_notif = 'cp_hjtopic';															
517
			$content_url = $params['cp_topic_url'];
518
			$content_entity = $params['cp_topic'];
519
			$author = $params['cp_topic']->getOwnerEntity();
520
			break;
521
522
		case 'cp_event_request': // event_calendar/actions/event_calendar/request_personal_calendar.php
523
			$message = array(
524
				'cp_event_request_user' => $params['cp_event_request_user'],
525
				'cp_event_request_url' => $params['cp_event_request_url'],
526
				'cp_event_object' => $params['cp_event_obj'],
527
				'type_event' => $params['type_event'], // create, request, cancel
528
				'cp_msg_type' => $cp_msg_type
529
			);
530
			$subject = elgg_echo('cp_notify:event_request:subject',array($params['cp_event_request_user'], $params['cp_event_obj']->title),'en');
531
			$subject .= ' | '.elgg_echo('cp_notify:event_request:subject',array($params['cp_event_request_user'], $params['cp_event_obj']->title),'fr');
532
			$to_recipients[] = $params['cp_event_owner'];
533
			$info_notif = 'cp_event_request';															
534
			
535
			break;
536
537
		case 'cp_event_ics': // .../mod/event_calendar/actions/event_calendar/add_ics.php
538
			$message = array(
539
				'cp_event_send_to_user' => $params['cp_event_send_to_user'],
540
				//'cp_event_invite_url' => $params['cp_event_invite_url'],
541
				'startdate' => $params['startdate'],
542
				'enddate' => $params['enddate'],
543
				'cp_event' => $params['cp_event'],
544
				'cp_msg_type' => $cp_msg_type,
545
			);
546
547
			$event = $params['cp_event'];
548
			$startdate = $params['startdate'];
549
			$enddate = $params['enddate'];
550
			
551
			// Add to my Outlook calendar | Ajoutez a mon calendrier d'Outlook
552
		    $subject = $event->title.' - '.elgg_get_logged_in_user_entity()->username; 
553
554
		   	$event = 'event';
555
			$to_recipients[] = $params['cp_event_send_to_user'];
556
			$info_notif = 'cp_event_ics';															
557
			   
558
			break;
559
560
			case 'cp_welcome_message':	// messages/actions/messages/send.php
561
			$add_to_sent = true;
562
			$sender_guid = $params['cp_from']['guid'];
563
			$to_recipients[] = get_user($params['cp_to']['guid']);
564
			$info_notif = 'cp_welcome_message';																		
565
			$subject = $params['cp_topic_title'];
566
			$message = array(
567
				'cp_msg_title' => $params['cp_topic_title'],
568
				'cp_msg_content' => $params['cp_topic_description'],
569
				'cp_msg_content_fr' => $params['cp_topic_description_fr'],
570
				'cp_msg_content_en' => $params['cp_topic_description_en'],
571
572
				'cp_sender' => $params['cp_from']['name'],
573
				'cp_msg_url' => $params['cp_topic_url'],
574
				'cp_msg_type' => 'cp_welcome_message',
575
			);
576
			break;
577
578
		default:
579
			break;
580
	}
581
582
583
	if (empty($subject))
584
		return false;
585
586
	$subject = htmlspecialchars_decode($subject,ENT_QUOTES);
587
588
	if (is_array($to_recipients)) {
589
		foreach ($to_recipients as $to_recipient) {
590
591
			// username for link in footer (both email notification and site notification
592
			$message['user_name'] = $to_recipient->username;
0 ignored issues
show
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...
593
			if ($cp_msg_type != 'cp_event_ics') {
594
				$template = elgg_view('cp_notifications/email_template', $message);
595
				$site_template = elgg_view('cp_notifications/site_template', $message);
596
			}
597
598
			$newsletter_appropriate = array('cp_wire_share','cp_wire_image','cp_messageboard','cp_wire_mention','cp_hjpost','cp_hjtopic', 'cp_friend_request', 'cp_friend_approve');
599
			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)) {
600
				$result = create_digest($author, $cp_msg_type, $content_entity, $to_recipient, $content_url);
0 ignored issues
show
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...
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...
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...
601
				continue;
602
603
			} else {
604
				
605
					$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
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...
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...
606
				}
607
608
			if (!$email_only)
609
				messages_send($subject, $site_template, $to_recipient->guid, $sender_guid, 0, true, $add_to_sent);
0 ignored issues
show
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...
610
		}
611
	}
612
613
	// register the error, if either of the arrays are not populated
614
	if (!is_array($to_recipients)) {
615
		notification_logging('error: in cp_create_notification(), $to_recipients is not array'.$info_notif);
0 ignored issues
show
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...
616
	}
617
}
618
619
620
/**
621
 * returns the headers for ical
622
 *
623
 * @param string 		$type_event
0 ignored issues
show
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...
624
 * @param ElggObject 	$event
625
 * @param string 		$start_date
626
 * @param string 		$end_date
627
 */
628
function cp_ical_headers($event_type, $event, $start_date, $end_date) {
629
630
	$end_date = date("Ymd\THis", strtotime($end_date));
631
	$start_date = date("Ymd\THis", strtotime($startdate));
0 ignored issues
show
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...
632
	$current_date = date("Ymd\TGis");
633
634
	$ical = "
635
	BEGIN:VCALENDAR \r\n
636
    PRODID:-//Microsoft Corporation//Outlook 10.0 MIMEDIR//EN \r\n
637
    VERSION:2.0 \r\n
638
    METHOD: {$type_event} \r\n
0 ignored issues
show
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...
639
    BEGIN:VTIMEZONE \r\n
640
    TZID:Eastern Time \r\n
641
    BEGIN:STANDARD \r\n
642
    DTSTART:20091101T020000 \r\n
643
    RRULE:FREQ=YEARLY;INTERVAL=1;BYDAY=1SU;BYMONTH=11 \r\n
644
    TZOFFSETFROM:-0400 \r\n
645
    TZOFFSETTO:-0500 \r\n
646
    TZNAME:EST \r\n
647
    END:STANDARD \r\n
648
    BEGIN:DAYLIGHT \r\n
649
    DTSTART:20090301T020000 \r\n
650
    RRULE:FREQ=YEARLY;INTERVAL=1;BYDAY=2SU;BYMONTH=3 \r\n
651
    TZOFFSETFROM:-0500 \r\n
652
    TZOFFSETTO:-0400 \r\n
653
    TZNAME:EDST \r\n
654
    END:DAYLIGHT \r\n
655
    END:VTIMEZONE \r\n
656
    BEGIN:VEVENT \r\n
657
    LAST-MODIFIED: {$current_date} \r\n
658
    UID: {$event->guid} \r\n
659
    DTSTAMP:  \r\n
660
    DTSTART;TZID='Eastern Time': {$start_date} \r\n
661
    DTEND;TZID='Eastern Time': {$end_date} \r\n
662
    TRANSP:OPAQUE \r\n
663
    SEQUENCE:1 \r\n
664
	SUMMARY: {$event->title} \r\n
665
	LOCATION: {$event->venue} \r\n
666
    CLASS:PUBLIC \r\n
667
    PRIORITY:5 \r\n
668
    BEGIN:VALARM \r\n
669
    TRIGGER:-PT15M \r\n
670
    ACTION:DISPLAY \r\n
671
    DESCRIPTION:Reminder \r\n
672
    END:VALARM \r\n
673
    END:VEVENT \r\n
674
    END:VCALENDAR \r\n";
675
676
	return $ical;
677
}
678
679
680
681
682
/**
683
 * cp_create_annotation_notification is an event handler, invokes everytime a user likes something, edit something, etc
684
 *
685
 * This contains the likes and the comments that get posted. we also filter out the
686
 * following : blog revision, discussion replies (?), tasks, poll votes, folder creation
687
 *
688
 * @param string $event		the name of the event
689
 * @param string $type		the type of the object
690
 * @param mixed $object		the object/entity of the event
691
 */
692
function cp_create_annotation_notification($event, $type, $object) {
693
694
	elgg_load_library('elgg:gc_notification:functions');
695
	$entity = get_entity($object->entity_guid);
696
697
	if ($entity->entity_minor_edit)	return;
698
699
	$dbprefix = elgg_get_config('dbprefix');
700
	$site = elgg_get_site_entity();
701
	$object_subtype = $object->getSubtype();
702
	$liked_content = get_entity($object->entity_guid);
703
	$type_of_like = $liked_content->getSubtype();
704
	if (!$type_of_like) $type_of_like = $liked_content->getType();
705
706
	$action_type = "content_revision";
707
	$author = $liked_by;
0 ignored issues
show
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...
708
709
	/// EDITS TO BLOGS AND PAGES, THEY ARE CONSIDERED ANNOTATION DUE TO REVISIONS AND MULTIPLE COPIES OF SAME CONTENT
710
	if (strcmp($object_subtype, 'likes') != 0) {
711
712
		$content = get_entity($object->entity_guid);
713
		$get_error_info='if object subtype likes 0';
714
		// auto save -drafts or -published blogs, we don't send out notifications
715 View Code Duplication
		if (strcmp($object_subtype,'blog_auto_save') == 0 && (strcmp($entity->status,'draft') == 0 || strcmp($entity->status, 'published') == 0)){
716
			$get_error_info='draft/autosave';
717
			return;
718
		} 
719
720
721
		// if we are publishing, or revising blogs then send out notification
722
		if (strcmp($object_subtype,'blog_revision') == 0 && strcmp($entity->status,'published') == 0) {
723
			$current_user = get_user($entity->getOwnerGUID());
724
			$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');
725
			$subject = htmlspecialchars_decode($subject,ENT_QUOTES);
726
727
			add_entity_relationship($entity->getOwnerGUID(), 'cp_subscribed_to_email', $entity->getGUID());
728
			add_entity_relationship($entity->getOwnerGUID(), 'cp_subscribed_to_site_mail', $entity->getGUID());
729
730
			$message = array(
731
				'cp_content' => $entity,
732
				'cp_user' => $current_user->username,
733
				'cp_msg_type' => 'cp_content_edit',
734
				'cp_fr_entity' => 'Ce blogue',
735
				'cp_en_entity' => 'blog',
736
			);
737
738
			if($entity->getContainerEntity() instanceof ElggGroup){
739
				$author_id = $object->getOwnerGUID();
740
				$content_id = $entity->getContainerGUID();
741
			}else{
742
				$author_id = $current_user->guid;
743
			}
744
745
			$author = $current_user;
746
			$content_entity = $entity;
747
			$get_error_info = 'blog revision or published';
748
749
			$watchers = get_subscribers($dbprefix, $author_id, $content_id);
0 ignored issues
show
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...
750 View Code Duplication
			foreach ($watchers as $watcher) {
751
				$message['user_name'] = $watcher->username;
752
753
				$template = elgg_view('cp_notifications/email_template', $message);
754
755
				$recipient_user = get_user($watcher->guid);
756
757
				if (has_access_to_entity($entity, $recipient_user) && $entity->access_id != 0) {
0 ignored issues
show
$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...
758
					if (strcmp(elgg_get_plugin_user_setting('cpn_set_digest', $watcher->guid,'cp_notifications'), 'set_digest_yes') == 0)
759
						create_digest($author, $action_type, $content_entity, get_entity($watcher->guid));
0 ignored issues
show
$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...
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...
760
					else
761
						(elgg_is_active_plugin('phpmailer')) ? phpmailer_send($watcher->email, $watcher->name, $subject, $template, NULL, true) : mail($watcher->email, $subject, $template, cp_get_headers());
762
				
763
					if (check_entity_relationship($watcher->guid, 'cp_subscribed_to_site_mail', $entity->getContainerGUID()))
764
						messages_send($subject, $template, $watcher->guid, $site->guid, 0, true, false);
765
				}
766
			}
767
			return true;
768
		}
769
770
771
		// checks for condition if the content being modified is a page or task
772
		if (strcmp($object_subtype,'page') == 0 || strcmp($object_subtype,'page_top') == 0 || strcmp($object_subtype,'task') == 0 || strcmp($object_subtype,'task_top') == 0) {
773
			$current_user = get_user($object->owner_guid);
774
			$subject = elgg_echo('cp_notify:subject:edit_content',array('The page', gc_explode_translation($entity->title,'en'), $current_user->username),'en');
775
			$subject .= ' | '.elgg_echo('cp_notify:subject:edit_content:f',array('La page', gc_explode_translation($entity->title,'fr'), $current_user->username),'fr');
776
777
			$subject = htmlspecialchars_decode($subject,ENT_QUOTES);
778
779
			$message = array(
780
				'cp_content' => $entity,
781
				'cp_user' => $current_user->username,
782
				'cp_msg_type' => 'cp_content_edit',
783
				'cp_fr_entity' => 'Cette page',
784
				'cp_en_entity' => 'page',
785
			);
786
787
			$author = $current_user;
788
			$content_entity = $entity;
789
			$get_error_info = 'page, page top';			
790
791
			$watchers = get_subscribers($dbprefix, $current_user->guid, $entity->guid);
792
793 View Code Duplication
			foreach ($watchers as $watcher) {
794
				$message['user_name'] = $watcher->username;
795
				$template = elgg_view('cp_notifications/email_template', $message);
796
				$recipient_user = get_user($watcher->guid);
797
798
				if (has_access_to_entity($entity, $recipient_user) /*&& $object->access_id != 0*/) {
0 ignored issues
show
$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...
799
800
					if (strcmp(elgg_get_plugin_user_setting('cpn_set_digest', $watcher->guid,'cp_notifications'),'set_digest_yes') == 0) {
801
802
						create_digest($author, $action_type, $content_entity, get_entity($watcher->guid));
0 ignored issues
show
$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...
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...
803
					}
804
					else {
805
806
						(elgg_is_active_plugin('phpmailer')) ? phpmailer_send( $watcher->email, $watcher->name, $subject, $template, NULL, true ) : mail($watcher->email, $subject, $template, cp_get_headers());
807
					}
808
809
810
					if (check_entity_relationship($watcher->guid, 'cp_subscribed_to_site_mail', $entity->getContainerGUID())) {
811
						$site_template = elgg_view('cp_notifications/site_template', $message);
812
						messages_send($subject, $site_template, $watcher->guid, $site->guid, 0, true, false);
813
					}
814
				}
815
			}
816
			return true;
817
		}
818
		
819
	} else {
820
821
		/// LIKES TO COMMENTS AND DISCUSSION REPLIES
822
    	$content_entity = get_entity($object->entity_guid); 			// get the comment object
823
		$comment_author = get_user($content_entity->owner_guid); 		// get the user who made the comment
824
		$content = get_entity($content_entity->getContainerGUID());		// get the location of comment
825
		$content_title = $content->title; 									// get title of content
826
		$liked_by = get_user($object->owner_guid); 							// get user who liked comment
827
828
		$action_type = "post_likes";
829
830
		$to_recipients = array();
831
		$to_recipients_site = array();
832
		$get_error_info = '';
833
834
	    switch ($type_of_like) {
835
	    	case 'group':
836
	    		$group_name_en = htmlspecialchars(filter_tags(htmlspecialchars_decode(gc_explode_translation($content_entity->name, 'en'))));
837
	    		$group_name_fr = htmlspecialchars(filter_tags(htmlspecialchars_decode(gc_explode_translation($content_entity->name, 'fr'))));
838
839
	    		$subject = elgg_echo(elgg_echo('cp_notify:subject:likes_group', array($liked_by->name, $group_name_en), 'en'));
840
	    		$subject .= elgg_echo(elgg_echo('cp_notify:subject:likes_group', array($liked_by->name, $group_name_fr), 'fr'));
841
842
	    		$message = array(
843
	    			'cp_liked_by' => $liked_by->name,
844
	    			'cp_group' => $content_entity->name,
845
	    			'cp_group_link' => $content_entity->getURL(),
846
	    			'cp_msg_type' => 'cp_like_group'
847
	    		);
848
849
	    		$group_owner = $content_entity->getOwnerEntity();
850
	    		$action_type = 'like_group';
851
852
	    		if (strcmp(elgg_get_plugin_user_setting('cpn_likes_email', $group_owner->getGUID(),'cp_notifications'),'likes_email') == 0)
853
					$to_recipients[$group_owner->getGUID()] = $group_owner;
854
					$get_error_info = 'group';
855
856
    			if (strcmp(elgg_get_plugin_user_setting('cpn_likes_site', $group_owner->getGUID(),'cp_notifications'),'likes_site') == 0)
857
					$to_recipients_site[$group_owner->getGUID()] = $group_owner;
858
					$get_error_info = 'group';
859
860
	    		break;
861
862 View Code Duplication
	    	case 'comment':
863
864
	    		$content_title_en = gc_explode_translation($content_title, 'en');
865
	    		$content_title_fr = gc_explode_translation($content_title, 'fr');
866
867
	    		$subject = elgg_echo('cp_notify:subject:likes_comment', array($liked_by->name, $content_title_en),'en');
868
	    		$subject .= ' | '.elgg_echo('cp_notify:subject:likes_comment',array($liked_by->name, $content_title_fr),'fr');
869
870
	    		$message = array(
871
	    			'cp_liked_by' => $liked_by->name,
872
	    			'cp_comment_from' => $content_title,
873
	    			'content_url' => $content->getURL(),
874
					'cp_msg_type' => 'cp_likes_comments',
875
				);
876
877
	    		$author = $liked_by;
878
	    		$action_type = "like_comment";
879
880
	    		if (strcmp(elgg_get_plugin_user_setting('cpn_likes_email', $comment_author->getGUID(),'cp_notifications'),'likes_email') == 0)
881
					$to_recipients[$comment_author->getGUID()] = $comment_author;
882
					$get_error_info = 'comment';
883
884
    			if (strcmp(elgg_get_plugin_user_setting('cpn_likes_site', $comment_author->getGUID(),'cp_notifications'),'likes_site') == 0)
885
					$to_recipients_site[$comment_author->getGUID()] = $comment_author;
886
					$get_error_info = 'comment';
887
	    		break;
888
889 View Code Duplication
	    	case 'discussion_reply':
890
891
	    		$content_title_en = gc_explode_translation($content_title, 'en');
892
	    		$content_title_fr = gc_explode_translation($content_title, 'fr');
893
894
	    		$subject = elgg_echo('cp_notify:subject:likes_discussion',array($liked_by->name, $content_title_en),'en');
895
	    		$subject .= ' | '.elgg_echo('cp_notify:subject:likes_discussion',array($liked_by->name, $content_title_fr),'fr');
896
897
				$message = array(
898
					'cp_liked_by' => $liked_by->name,
899
					'cp_comment_from' => $content_title,
900
					'content_url' => $content->getURL(),
901
					'cp_msg_type' => 'cp_likes_topic_replies',
902
				);
903
				$author = $liked_by;
904
				$action_type = "like_reply";
905
906
	    		if (strcmp(elgg_get_plugin_user_setting('cpn_likes_email', $comment_author->getGUID(),'cp_notifications'),'likes_email') == 0)
907
					$to_recipients[$comment_author->getGUID()] = $comment_author;
908
					$get_error_info = 'discussion_reply';
909
    			if (strcmp(elgg_get_plugin_user_setting('cpn_likes_site', $comment_author->getGUID(),'cp_notifications'),'likes_site') == 0)
910
					$to_recipients_site[$comment_author->getGUID()] = $comment_author;
911
					$get_error_info = 'discussion_reply';
912
	    		break;
913
914
915
	    	default:
916
	    		$type_of_like = 'user_update';
917
		    	if ($liked_content instanceof ElggUser) {
918
919
					// cyu - there doesn't seem to be any differentiation between updated avatar and colleague connection
920
		    		$liked_by = get_user($object->owner_guid); // get user who liked comment
921
922
923
		    		$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');
924
		    		$message = array(
925
						'cp_msg_type' => 'cp_likes_user_update',
926
						'cp_liked_by' => $liked_by->name
927
					);
928
					$to_recipients[$liked_content->guid] = $liked_content;
929
					$get_error_info = 'user_update_if_liked_content';
930
931
932
		    	} else {
933
934
		    		$type_of_like = 'content';
935
		    		$liked_by = get_user($object->owner_guid); // get user who liked content
936
		    		$content = get_entity($object->entity_guid);
937
938
		    		$content_entity = $content;
939
		    		$author = $liked_by;
940
941
		    		$content_title_en = gc_explode_translation($content->title, 'en');
942
		    		$content_title_fr = gc_explode_translation($content->title, 'fr');
943
944
		    		// cyu - patching issue #323 (liking wire post)
945
		    		if ($content->getSubtype() === 'thewire') {
946
		    			$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');
947
		    			$content_subtype = 'thewire';
948
949
		    		} else {
950
		    			$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');
951
		    			$content_subtype = '';
952
		    		}
953
954
		    		$message = array(
955
		    			'cp_subtype' => $content_subtype,
956
						'cp_msg_type' => 'cp_likes_type',
957
						'cp_liked_by' => $liked_by->name,
958
						'cp_comment_from' => $content->title,
959
						'cp_description' => $content->description,
960
						'cp_content_url' => $content->getURL(),
961
					);
962
963
		    		if (strcmp(elgg_get_plugin_user_setting('cpn_likes_email', $content->getOwnerGUID(),'cp_notifications'), 'likes_email') == 0)
964
						$to_recipients[$content->getOwnerGUID()] = $content->getOwnerEntity();
965
						$get_error_info = 'content';
966
967
	    			if (strcmp(elgg_get_plugin_user_setting('cpn_likes_site', $content->getOwnerGUID(),'cp_notifications'), 'likes_site') == 0)
968
						$to_recipients_site[$content->getOwnerGUID()] = $content->getOwnerEntity();
969
						$get_error_info = 'content';
970
		    	}
971
	    		break;
972
973
		} // end switch statement
974
	}
975
976
	$subject = htmlspecialchars_decode($subject,ENT_QUOTES);
0 ignored issues
show
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...
977
978
	if (is_array($to_recipients)) {
979
		// send notification out via email
980
		foreach ($to_recipients as $to_recipient_id => $to_recipient) {
0 ignored issues
show
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...
981
		
982
			$message['user_name'] = get_user($to_recipient->guid)->username;
0 ignored issues
show
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...
983
984
			$recipient_user = get_user($to_recipient->guid);
985
986
			if($recipient_user->gcdeactivate)
987
				continue;
988
989
			if ($liked_by->guid == $entity->getOwnerGUID() && $to_recipient->guid == $liked_by->guid)
0 ignored issues
show
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...
990
				continue;
991
992
			if ($object->access_id == 1 || $object->access_id == 2 || $content_entity->getType() === 'group' || $action_type === 'post_likes') {
993
994 View Code Duplication
				if (strcmp(elgg_get_plugin_user_setting('cpn_set_digest', $to_recipient->guid,'cp_notifications'),'set_digest_yes') == 0)
995
					create_digest($author, $action_type, $content_entity, $to_recipient);
0 ignored issues
show
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...
996
997
				else {
998
					$template = elgg_view('cp_notifications/email_template', $message);
999
1000
					if (elgg_is_active_plugin('phpmailer')) {
1001
1002
						phpmailer_send( $to_recipient->email, $to_recipient->name, $subject, $template, NULL, true );
1003
					}
1004
					else
1005
						mail($to_recipient->email, $subject, $template, cp_get_headers());
1006
				}
1007
			}
1008
		}
1009
	}
1010
1011
	if (is_array($to_recipients_site)) {
1012
		// send notification out via site
1013
		foreach ($to_recipients_site as $to_recipient_id => $to_recipient) {
0 ignored issues
show
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...
1014
			$site_template = elgg_view('cp_notifications/site_template', $message);
1015
			$recipient_user = get_user($to_recipient->guid);
1016
1017
			if (($object->access_id == 1 || $object->access_id == 2 || $content_entity->getType() === 'group' || $action_type === 'post_likes')  && 
1018
				(strcmp(elgg_get_plugin_user_setting('cpn_set_digest', $to_recipient->guid,'cp_notifications'),'set_digest_yes') !== 0)) {
1019
1020
				messages_send($subject, $site_template, $to_recipient->guid, $site->guid, 0, true, false);
1021
			}
1022
1023
		}
1024
	}
1025
1026
	// register the error, if either of the arrays are not populated
1027
	if (!is_array($to_recipients) || !is_array($to_recipients_site)) {
1028
		$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');
1029
		if ($comment_author) {
1030
			$error_message .= 'Comment author= '. print_r($comment_author, true);
0 ignored issues
show
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...
1031
		}
1032
		if ($group_owner) {
1033
			$error_message .= 'Group owner= ' . print_r($group_owner, true);
0 ignored issues
show
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...
1034
		}
1035
		$error_message .= 'State: '.$get_error_info; //from which state the error is from
1036
		notification_logging($error_message);
1037
	}
1038
1039
} // end of function
1040
1041
1042
1043
/**
1044
 * function cp_create_notification is an event handler, invokes everytime a new entity is created
1045
 * This contains the notifications for new content posted on GCconnex
1046
 *
1047
 * @param string $event		the name of the event
1048
 * @param string $type		the type of object (eg "user", "group", ...)
1049
 * @param mixed $object		the object/entity of the event
1050
 */
1051
function cp_create_notification($event, $type, $object) {
1052
1053
	$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');
1054
1055
	// 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)
1056
	if ($object instanceof ElggObject && $event !== 'single_file_upload') {
1057
1058
		if (in_array($object->getSubtype(), $do_not_subscribe_list)) 
1059
			return true;		
1060
	} else {
1061
1062
		if ('single_zip_file_upload' !== $event && 'multi_file_upload' !== $event && 'single_file_upload' !== $event)
1063
			return true;
1064
	}
1065
1066
	elgg_load_library('elgg:gc_notification:functions');
1067
	$dbprefix = elgg_get_config('dbprefix');
1068
	$no_notification = false;
1069
	$site = elgg_get_site_entity();
1070
	$to_recipients = array();
1071
	$subject = "";
1072
1073
	$switch_case = $event;
1074
	if ($object instanceof ElggObject)
1075
		$switch_case = $object->getSubtype();	
1076
1077
	switch ($switch_case) {
1078
1079
1080
		/// invoked when zipped file upload function is used (files_tools/lib/functions.php)
1081
		case 'single_zip_file_upload':
1082
1083
			$entity = get_entity($object['forward_guid']);
1084 View Code Duplication
			if (elgg_instanceof('group', $entity)) {
0 ignored issues
show
$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...
1085
				$file_forward_url = elgg_get_site_entity()->getURL()."file/group/{$object['forward_guid']}/all";
1086
			} elseif (elgg_instanceof('user', $entity)) {
0 ignored issues
show
$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...
1087
				$file_forward_url = elgg_get_site_entity()->getURL()."file/owner/{$entity->username}";
1088
			} else {
1089
				$file_forward_url = $entity->getURL();
1090
			}
1091
1092
			$to_recipients = get_subscribers($dbprefix, elgg_get_logged_in_user_guid(), $entity->getGUID());
1093
			$to_recipients_site = get_site_subscribers($dbprefix, elgg_get_logged_in_user_guid(), $entity->getGUID());
1094
1095
			$subject = elgg_echo('cp_notify_usr:subject:new_content2', array(elgg_get_logged_in_user_entity()->username, 'file'), 'en');
1096
			$subject .= ' | '.elgg_echo('cp_notify_usr:subject:new_content2', array(elgg_get_logged_in_user_entity()->username, 'fichier', false), 'fr');
1097
1098
			$author = elgg_get_logged_in_user_entity();
1099
			$message = array(
1100
				'cp_topic' => $entity,
1101
				'cp_msg_type' => 'zipped_file',
1102
				'files_uploaded' => $object['files_uploaded'],
1103
				'cp_topic_description_discussion' => 'Please view the files here',
1104
				'cp_topic_description_discussion2' => 'SVP voir les fichiers ici',
1105
			);
1106
1107
			
1108
			$content_entity = $object['files_uploaded'];
1109
			$object = get_entity($object['files_uploaded'][0]);
1110
			$author = elgg_get_logged_in_user_entity();
1111
			break;
1112
1113
		/// invoked when multiple file upload function is used
1114
		case 'multi_file_upload':
1115
1116
			$entity = get_entity($object['forward_guid']);
1117 View Code Duplication
			if (elgg_instanceof('group', $entity)) {
0 ignored issues
show
$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...
1118
				$file_forward_url = elgg_get_site_entity()->getURL()."file/group/{$object['forward_guid']}/all";
1119
			} elseif (elgg_instanceof('user', $entity)) {
0 ignored issues
show
$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...
1120
				$file_forward_url = elgg_get_site_entity()->getURL()."file/owner/{$entity->username}";
1121
			} else {
1122
				$file_forward_url = $entity->getURL();
1123
			}
1124
1125
			$author = elgg_get_logged_in_user_entity();
1126
			$to_recipients = get_subscribers($dbprefix, elgg_get_logged_in_user_guid(), $entity->getGUID());
1127
			$to_recipients_site = get_site_subscribers($dbprefix, elgg_get_logged_in_user_guid(), $entity->getGUID());
1128
1129
			$subject = elgg_echo('cp_notify_usr:subject:new_content2', array(elgg_get_logged_in_user_entity()->username, 'file'), 'en');
1130
			$subject .= ' | '.elgg_echo('cp_notify_usr:subject:new_content2', array(elgg_get_logged_in_user_entity()->username, 'fichier', false), 'fr');
1131
	
1132
			$message = array(
1133
				'cp_topic' => $entity,
1134
				'cp_msg_type' => 'multiple_file',
1135
				'files_information' => $entity,
1136
				'files_uploaded' => $object['files_uploaded'], 
1137
				'cp_msg_type' => 'multiple_file',
1138
				'cp_topic_description_discussion' => 'Please view the files here',
1139
				'cp_topic_description_discussion2' => 'SVP voir les fichiers ici',
1140
			);
1141
1142
			
1143
			$content_entity = $object['files_uploaded'];
1144
			$object = get_entity($object['files_uploaded'][0]);
1145
			$author = elgg_get_logged_in_user_entity();
1146
			break;
1147
1148
		case 'discussion_reply':
1149
		case 'comment':
1150
1151
			// if mentions plugin is enabled... check to see if there were any mentions
1152
			$cp_mentioned_users = (elgg_is_active_plugin('mentions') && $object->getSubtype() !== 'messages') ? cp_scan_mentions($object) : "";
1153
1154
			// send mentioned users a notification
1155
			if (sizeof($cp_mentioned_users) > 0 && is_array($cp_mentioned_users)) {
1156
				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...
1157
					$cp_mentioned_user = $cp_mentioned_users[$i];
1158
					$mentioned_user = get_user_by_username(substr($cp_mentioned_user, 1));
1159
1160
					if (!$mentioned_user || $mentioned_user->gcdeactivate)
1161
						break;
1162
1163
					$subject = elgg_echo('cp_notify:subject:mention',array($object->getOwnerEntity()->name), 'en') .' | ' . elgg_echo('cp_notify:subject:mention',array($object->getOwnerEntity()->name), 'fr');
1164
					$message = array(
1165
						'cp_msg_type' => 'cp_mention_type',
1166
						'cp_author' => $object->getOwnerEntity()->name,
1167
						'cp_content_desc' => $object->description,
1168
						'cp_link' => $object->getURL(),
1169
						'cp_content' => $object,
1170
					);
1171
					$template = elgg_view('cp_notifications/email_template', $message);
1172
					$user_setting = elgg_get_plugin_user_setting('cpn_mentions_email', $mentioned_user->guid, 'cp_notifications');
1173
1174 View Code Duplication
					if (strcmp($user_setting, 'mentions_email') == 0) {
1175
						$user_setting = elgg_get_plugin_user_setting('cpn_set_digest', $mentioned_user->guid, 'cp_notifications');
1176
1177
						// send digest
1178
						if (strcmp($user_setting, "set_digest_yes") == 0) {
1179
							create_digest($object->getOwnerEntity(), "cp_mention", $object, $mentioned_user);
1180
1181
						// send email and site notification
1182
						} else {
1183
1184
							if (elgg_is_active_plugin('phpmailer'))
1185
								phpmailer_send( $mentioned_user->email, $mentioned_user->name, $subject, $template, NULL, true );
1186
							else
1187
								mail($mentioned_user->email,$subject,$template,cp_get_headers());
1188
						}
1189
					}
1190
1191
					$user_setting = elgg_get_plugin_user_setting('cpn_mentions_site', $mentioned_user->guid, 'cp_notifications');
1192
					$site_template = elgg_view('cp_notifications/site_template', $message);
1193
					if (strcmp($user_setting, 'mentions_site') == 0)
1194
						messages_send($subject, $site_template, $mentioned_user->guid, $site->guid, 0, true, false);
1195
				}
1196
			}
1197
1198
			// retrieve all necessary information for notification
1199
			$container_entity = $object->getContainerEntity();
1200
1201
			$user_comment = get_user($object->owner_guid);
1202
			$topic_container = $container_entity->getContainerEntity();
1203
1204
			// comment or reply in a group
1205
			if ($topic_container instanceof ElggGroup) {
1206
				if (strcmp($object->getSubtype(), 'discussion_reply') == 0) {
1207
					$subject = elgg_echo('cp_notify:subject:comments_discussion',array(gc_explode_translation($topic_container->name,'en')),'en');
1208
					$subject .= ' | '.elgg_echo('cp_notify:subject:comments_discussion',array(gc_explode_translation($topic_container->name,'fr')),'fr');
1209
				} else {
1210
					$subject = elgg_echo('cp_notify:subject:comments',array(gc_explode_translation($topic_container->name,'en')),'en');
1211
					$subject .= ' | '.elgg_echo('cp_notify:subject:comments',array(gc_explode_translation($topic_container->name,'fr')),'fr');
1212
				}
1213
1214
			// comment or reply in a user
1215
			} else {
1216
				$entity_residence = 'usr';
1217
				$subject = elgg_echo('cp_notify:subject:comments_user', array($topic_container->name), 'en');
1218
				$subject .= ' | '.elgg_echo('cp_notify:subject:comments_user', array($topic_container->name), 'fr');
1219
			}
1220
1221
1222
			$message = array(
1223
				'cp_container' => $entity_residence,
0 ignored issues
show
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...
1224
				'cp_user_comment' => $user_comment,
1225
				'cp_topic' => $container_entity,
1226
				'cp_topic_type' => cp_translate_subtype($container_entity->getSubtype()),
1227
				'cp_comment' => $object,
1228
				'cp_msg_type' => 'cp_reply_type',
1229
			);
1230
1231
			// digest information purposes
1232
			$content_entity = $container_entity;
1233
			$author = $user_comment;
1234
1235
			$container = get_entity($container_entity->container_guid);
1236
1237
			// the user creating the content is automatically subscribed to it
1238 View Code Duplication
			if (elgg_instanceof($container, 'group')) {
1239
	 			if($container->isMember($user_comment)){
1240
					add_entity_relationship($object->getOwnerGUID(), 'cp_subscribed_to_email', $container_entity->getGUID());
1241
					add_entity_relationship($object->getOwnerGUID(), 'cp_subscribed_to_site_mail', $container_entity->getGUID());
1242
				}
1243
			} else {
1244
				add_entity_relationship($object->getOwnerGUID(), 'cp_subscribed_to_email', $container_entity->getGUID());
1245
				add_entity_relationship($object->getOwnerGUID(), 'cp_subscribed_to_site_mail', $container_entity->getGUID());
1246
			}
1247
1248
1249
			$to_recipients = get_subscribers($dbprefix, $object->getOwnerGUID(), $object->getContainerGUID());
1250
			$to_recipients_site = get_subscribers($dbprefix, $object->getOwnerGUID(), $object->getContainerGUID());
1251
			break;
1252
1253
		// micromissions / opportunities
1254
		case 'mission':
1255
1256
			$job_type = $object->job_type;		// only need to get this once
1257
			$role_type = $object->role_type;
1258
			$job_type_ids = getMissionTypeMetastringid($job_type, $role_type);
1259
			$opt_in_id = elgg_get_metastring_id('gcconnex_profile:opt:yes');
1260
1261
			// get users who want to be notified about new opportunities by site message and have opted in to this type of oppotrunity
1262
			$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}");
1263
1264
			foreach ($op_siteusers as $result) {
1265
				$userid = $result->entity_guid;
1266
				$user_obj = get_user($userid);
1267
				$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...
1268
			}
1269
1270
			// get users who want to be notified about new opportunities by email and have opted in to this type of oppotrunity
1271
			$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}");
1272
1273
			foreach ($op_emailusers as $result) {
1274
				$userid = $result->entity_guid;
1275
				$user_obj = get_user($userid);
1276
				$to_recipients[$userid] = $user_obj;
1277
			}
1278
1279
			$message = array(
1280
				'cp_topic' => $object,
1281
				'cp_msg_type' => 'new_mission',
1282
			);
1283
1284
			// digest information purposes
1285
			$content_entity = $object;
1286
			$author = $object->getOwnerEntity();
1287
1288
			$subject = elgg_echo('cp_new_mission:subject',array(),'en') . ' | ' . elgg_echo('cp_new_mission:subject',array(),'fr');
1289
1290
			// the user creating the content is automatically subscribed to it
1291
			add_entity_relationship(elgg_get_logged_in_user_guid(), 'cp_subscribed_to_email', $object->getGUID());
1292
			add_entity_relationship(elgg_get_logged_in_user_guid(), 'cp_subscribed_to_site_mail', $object->getGUID());
1293
			break; 
1294
1295
		case 'single_file_upload':
1296
1297
		default:
1298
1299
			// cyu - there is an issue with regards to auto-saving drafts
1300 View Code Duplication
			if (strcmp($object->getSubtype(),'blog') == 0) {
1301
			$get_error_info='draft blog';
1302
				
1303
				if (strcmp($object->status,'draft') == 0 || strcmp($object->status,'unsaved_draft') == 0) return;
1304
			}
1305
1306
			// the user creating the content is automatically subscribed to it (with exception that is not a widget, forum, etc..)
1307
			$cp_whitelist = array('blog', 'bookmarks', 'poll', 'groupforumtopic', 'image', 'idea', 'page', 'page_top', 'thewire', 'task_top', 'question', 'answer', 'cp_wire_image','event_calendar');
1308 View Code Duplication
			if (in_array($object->getSubtype(), $cp_whitelist)) {
1309
1310
				if ($object->getSubtype() == 'answer') {// subcribed to the question
1311
					add_entity_relationship($object->getOwnerGUID(), 'cp_subscribed_to_email', $object->getContainerGUID());
1312
					add_entity_relationship($object->getOwnerGUID(), 'cp_subscribed_to_site_mail', $object->getContainerGUID());
1313
				}
1314
				add_entity_relationship($object->getOwnerGUID(), 'cp_subscribed_to_email', $object->getGUID());
1315
				add_entity_relationship($object->getOwnerGUID(), 'cp_subscribed_to_site_mail', $object->getGUID());
1316
            }
1317
1318
			$user = get_user($object->owner_guid);
1319
			$group = $object->getContainerEntity();
1320
1321
			$group_name = htmlspecialchars(filter_tags(htmlspecialchars_decode(gc_explode_translation($group->name,'en'))));
1322
			$group_name2 = htmlspecialchars(filter_tags(htmlspecialchars_decode(gc_explode_translation($group->name,'fr'))));
1323
1324
			// fem and mas are different (so the subject should be reflected on which subtype is passed)
1325
			$subtypes_gender_subject = array(
1326
1327
				'blog' => elgg_echo('cp_notify:subject:new_content_mas',array("blogue",$group_name2),'fr'),
1328
				'bookmarks' => elgg_echo('cp_notify:subject:new_content_mas',array("signet",$group_name2),'fr'),
1329
				'file' => elgg_echo('cp_notify:subject:new_content_mas',array("fichier",$group_name2),'fr'),
1330
				'poll' => elgg_echo('cp_notify:subject:new_content_mas',array("sondage",$group_name2),'fr'),
1331
				'event_calendar' => elgg_echo('cp_notify:subject:new_content_mas',array("nouvel événement",$group_name2),'fr'),
1332
				'album' => elgg_echo('cp_notify:subject:new_content_mas',array("album d'image",$group_name2),'fr'),
1333
				'groupforumtopic' => elgg_echo('cp_notify:subject:new_content_fem',array("discussion",$group_name2),'fr'),
1334
				'image' => elgg_echo('cp_notify:subject:new_content_fem',array("image",$group_name2),'fr'),
1335
				'idea' => elgg_echo('cp_notify:subject:new_content_fem',array("idée",$group_name2),'fr'),
1336
				'page' => elgg_echo('cp_notify:subject:new_content_fem',array("page",$group_name2),'fr'),
1337
				'page_top' => elgg_echo('cp_notify:subject:new_content_fem',array("page",$group_name2),'fr'),
1338
				'task_top' => elgg_echo('cp_notify:subject:new_content_fem',array("task",$group_name2),'fr'),
1339
				'task' => elgg_echo('cp_notify:subject:new_content_fem',array("task",$group_name2),'fr'),
1340
        		'question' => elgg_echo('cp_notify:subject:new_content_fem',array("question",$group_name2),'fr'),
1341
        		'etherpad' => elgg_echo('cp_notify:subject:new_content_fem',array(elgg_echo('etherpad:single'),$group_name2),'fr')
1342
			);
1343
1344
			// if there is something missing, we can use a fallback string
1345
			$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()];
1346
1347
			// subscribed to content within a group
1348
			if ($object->getContainerEntity() instanceof ElggGroup) {
1349
				$subject = elgg_echo('cp_notify:subject:new_content',array(cp_translate_subtype($object->getSubtype()),$group_name),'en');
1350
				$subject .= ' | '.$subj_gender;
1351
1352
				$guidone = $object->getContainerGUID();
1353
				$author_id = $object->getOwnerGUID();
1354
				$content_id = $object->getContainerGUID();
1355
1356
			// subscribed to users or friends
1357
			} else {
1358
1359
				if (trim($object->title) === '') {
1360
1361
					/// check if poll_choice is found in the subtype
1362
					if (strpos($object->getSubtype(), "poll_choice") !== false)
1363
						return true;
1364
1365
					$subject = elgg_echo('cp_notify_usr:subject:new_content2',array($object->getOwnerEntity()->username,cp_translate_subtype($object->getSubtype())),'en');
1366
					$subject .= ' | '.elgg_echo('cp_notify_usr:subject:new_content2',array($object->getOwnerEntity()->username,cp_translate_subtype($object->getSubtype(), false)),'fr');
1367
1368
				} else {
1369
1370
					if (strcmp($object->getSubtype(), 'hjforumpost') != 0 || strcmp($object->getSubtype(), 'hjforumtopic') != 0) {
1371
						if ($object->getSubtype() == 'answer'){
1372
							$question_guid = $object->getContainerGUID();
1373
							$answer_entity = get_entity($question_guid);
1374
					
1375
							$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');
1376
							$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');
1377
						
1378
						} else if ($object->getSubtype() == 'etherpad'){
1379
							$subject = elgg_echo('cp_notify_usr:subject:new_content',array($object->getOwnerEntity()->username, elgg_echo('etherpad:single'), gc_explode_translation($object->title,'en')),'en');
1380
							$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');
1381
						} else {
1382
							
1383
							$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');
1384
							$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');
1385
						}
1386
					}
1387
				}
1388
1389
				$guidone = $object->getOwnerGUID();
1390
				$author_id = $object->getOwnerGUID();
1391
				
1392
				// Get guid of the question
1393
				if($object->getSubtype = 'answer') {
1394
						$content_id = $object->getContainerGUID();
1395
				}
1396
			}
1397
	
1398
			// client wants the html tags stripped from the notifications
1399
			$object_description = ($object->description != strip_tags($object->description)) ? "" : $object->description;
1400
1401
			$message = array(
1402
				'cp_topic' => $object,
1403
				'cp_msg_type' => 'cp_new_type',
1404
				'cp_topic_description_discussion' => $object->description,
1405
				'cp_topic_description_discussion2' => $object->description2,
1406
			);
1407
1408
			$content_entity = $object;
1409
			$author = $object->getOwnerEntity();
1410
1411
			$to_recipients = get_subscribers($dbprefix, $author_id, $content_id);
0 ignored issues
show
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...
1412
			$to_recipients_site = get_site_subscribers($dbprefix, $author_id, $content_id);
1413
			break;
1414
1415
	} // end of switch statement
1416
1417
1418
1419
	$notification_error_type = "";
1420
1421
	// check for empty subjects or empty content
1422
	if (empty($subject)) return false;
1423
	$subject = htmlspecialchars_decode($subject,ENT_QUOTES);
1424
1425
	/// send the email notification
1426
	if (is_array($to_recipients)) {
1427
1428
		foreach ($to_recipients as $to_recipient) {
1429
1430
			$recipient_user = get_user($to_recipient->guid);
1431
			//Nick - GCdeactivate - don't send notification
1432
			if ($to_recipient->guid == $author->guid || $recipient_user->gcdeactivate)
1433
				continue;
1434
1435
				$user_setting = elgg_get_plugin_user_setting('cpn_set_digest', $to_recipient->guid, 'cp_notifications');
1436
				
1437
1438
				
1439
					if (has_access_to_entity($object, $recipient_user) && $object->access_id != 0) {
0 ignored issues
show
$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...
1440
				
1441 View Code Duplication
					if (strcmp($user_setting, "set_digest_yes") == 0) {
1442
						create_digest($author, $switch_case, $content_entity, get_entity($to_recipient->guid));
0 ignored issues
show
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...
1443
				
1444
					} else {
1445
				
1446
						$template = elgg_view('cp_notifications/email_template', $message);
1447
				
1448
						if (elgg_is_active_plugin('phpmailer'))
1449
							phpmailer_send( $to_recipient->email, $to_recipient->name, $subject, $template, NULL, true );
1450
						else
1451
							mail($to_recipient->email,$subject,$template,cp_get_headers());
1452
					}
1453
				}
1454
			
1455
1456
		}
1457
	}
1458
1459
	/// send site notifications
1460
	if (is_array($to_recipients_site)) {
1461
		
1462
		foreach ($to_recipients_site as $to_recipient) {
0 ignored issues
show
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...
1463
			$user_setting = elgg_get_plugin_user_setting('cpn_set_digest', $to_recipient->guid, 'cp_notifications');
1464
			$recipient_user = get_user($to_recipient->guid);
1465
1466
			// check to see if the author is the recipient or recipient has the digest enabled
1467
			if ($to_recipient->guid == $author->guid || strcmp($user_setting, "set_digest_yes") == 0)
1468
				continue;
1469
1470
			if (has_access_to_entity($object, $recipient_user) && $object->access_id != 0) {
0 ignored issues
show
$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...
1471
1472
				$site_template = elgg_view('cp_notifications/site_template', $message);
1473
				messages_send($subject, $site_template, $to_recipient->guid, $site->guid, 0, true, false);
1474
			}
1475
		}
1476
	}
1477
1478
	// register the error, if either of the arrays are not populated
1479
	if (!is_array($to_recipients) || !is_array($to_recipients_site)) {
1480
		$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();
1481
1482
		notification_logging($error_message);
1483
1484
	}
1485
1486
}
1487
1488
1489
function notification_logging($error_message) {
1490
	// logging mechanism
1491
	if (elgg_is_active_plugin('wet4')) {
1492
		elgg_load_library('GCconnex_logging');
1493
		$errStack = '';
1494
		$errType = 'custom';
1495
		gc_err_logging($error_message, $errStack, 'Notifications',$errType);
1496
	}
1497
}
1498
1499
/**
1500
 * get all the users that are subscribed to a specified entity (content or user id)
1501
 * and return the array of users
1502
 *
1503
 * @param string 			$dbprefix
1504
 * @param integer 			$user_guid
1505
 * @param optional integer 	$entity_guid
1506
1507
 * @param email vs site_mail
1508
 * @return Array <ElggUser>
1509
1510
 */
1511
1512 View Code Duplication
function get_subscribers($dbprefix, $user_guid, $entity_guid = '') {
1513
	$subscribed_to = ($entity_guid != '') ? $entity_guid : $user_guid;
1514
1515
	$query = "	SELECT DISTINCT u.guid, u.email, u.username, u.name
1516
				FROM {$dbprefix}entity_relationships r LEFT JOIN {$dbprefix}users_entity u ON r.guid_one = u.guid
1517
				WHERE r.guid_one <> {$user_guid} AND r.relationship = 'cp_subscribed_to_email' AND r.guid_two = {$subscribed_to}";
1518
1519
	return get_data($query);
1520
}
1521
1522 View Code Duplication
function get_site_subscribers($dbprefix, $user_guid, $entity_guid = '') {
1523
	$subscribed_to = ($entity_guid != '') ? $entity_guid : $user_guid;
1524
1525
	$query = " SELECT DISTINCT u.guid, u.email, u.username, u.name
1526
	FROM {$dbprefix}entity_relationships r LEFT JOIN {$dbprefix}users_entity u ON r.guid_one = u.guid
1527
	WHERE r.guid_one <> {$user_guid} AND r.relationship = 'cp_subscribed_to_site_mail' AND r.guid_two = {$subscribed_to}";
1528
1529
	return get_data($query);
1530
}
1531
1532
1533
/**
1534
 * setup crontab either on a daily or weekly basis
1535
 * get users who are subscribed to digest
1536
 * run crontab, retrieve users, send digest, reset timer (update timestamp)
1537
 *
1538
 * @param string $hook    The name of the plugin hook
1539
 * @param string $type    The type of the plugin hook
0 ignored issues
show
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...
1540
 * @param mixed  $value   The current value of the plugin hook
0 ignored issues
show
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...
1541
 * @param mixed  $params  Data passed from the trigger
1542
 */
1543
function cp_digest_weekly_cron_handler($hook, $entity_type, $return_value, $params) {
1544
	echo "<p>Starting up the cron job for the Notifications (cp_notifications plugin)</p>";
1545
	elgg_load_library('elgg:gc_notification:functions');
1546
	
1547
	initialize_queue('weekly');
1548
1549
	// TODO: write the functions above and rewrite the email handler below
1550
	cp_digest_email_handler($hook, $entity_type, $return_value, $params, 'weekly');
1551
}
1552
1553
1554
1555
/**
1556
 * setup crontab either on a daily or weekly basis
1557
 * get users who are subscribed to digest
1558
 * run crontab, retrieve users, send digest, reset timer (update timestamp)
1559
 *
1560
 * @param string $hook    The name of the plugin hook
1561
 * @param string $entity_type    The type of the plugin hook
1562
 * @param mixed  $return_value   The current value of the plugin hook
1563
 * @param mixed  $params  Data passed from the trigger
1564
 */
1565
function cp_digest_daily_cron_handler($hook, $entity_type, $return_value, $params) {
1566
	echo "<p>Starting up the cron job for the Notifications (cp_notifications plugin)</p>";
1567
	elgg_load_library('elgg:gc_notification:functions');
1568
1569
	initialize_queue('daily');
1570
1571
	cp_digest_email_handler($hook, $entity_type, $return_value, $params, 'daily');
1572
}
1573
1574
/**
1575
 * setup crontab either on a daily or weekly basis
1576
 * get users who are subscribed to digest
1577
 * run crontab, retrieve users, send digest, reset timer (update timestamp)
1578
 *
1579
 * @param string $hook    The name of the plugin hook
1580
 * @param string $entity_type    The type of the plugin hook
1581
 * @param mixed  $return_value   The current value of the plugin hook
1582
 * @param mixed  $params  Data passed from the trigger
1583
 */
1584
function cp_digest_email_handler($hook, $entity_type, $return_value, $params, $cron_freq) {
1585
	$dbprefix = elgg_get_config('dbprefix');
1586
1587
	while( $user_guid = dequeue() ) {
1588
		$user = get_entity($user_guid);
1589
		if($user->gcdeactivate)
1590
			continue;
1591
		$frequency = elgg_get_plugin_user_setting('cpn_set_digest_frequency', $user->guid, 'cp_notifications');
1592
1593
		if ($user instanceof ElggUser && strcmp($frequency,'set_digest_' . $cron_freq) == 0 ) {
1594
			$digest_array = array();
1595
1596
			$query = "SELECT * FROM notification_digest WHERE user_guid = {$user->guid}";
1597
			$digest_items = get_data($query);
1598
1599
			$language_preference = (strcmp(elgg_get_plugin_user_setting('cpn_set_digest_language', $user->guid, 'cp_notifications'),'set_digest_en') == 0) ? 'en' : 'fr';
1600
1601
			foreach ($digest_items as $digest_item) {
1602
1603
				// check to make sure that the string is encoded with base 64 or not (legacy)
1604
				if (isJson($digest_item->notification_entry))
1605
					$notification_entry = $digest_item->notification_entry;
1606
				else
1607
					$notification_entry = base64_decode($digest_item->notification_entry);
1608
1609
1610
				if ($digest_item->entry_type === 'group') 
1611
					$digest_array[$digest_item->entry_type][base64_decode($digest_item->group_name)][$digest_item->action_type][$digest_item->entity_guid] = $notification_entry;
1612
				else
1613
					$digest_array[$digest_item->entry_type][$digest_item->action_type][$digest_item->entity_guid] = $notification_entry;
1614
1615
			}
1616
1617
			$subject = elgg_echo('cp_newsletter:subject:' . $cron_freq,$language_preference);
0 ignored issues
show
$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...
1618
1619
			// if the array is empty, send the empty template
1620
			if (sizeof($digest_array) > 0 || !empty($digest_array))
1621
				$template = elgg_view('cp_notifications/newsletter_template', array('to' => $user, 'newsletter_content' => $digest_array));
1622
			else
1623
				$template = elgg_view('cp_notifications/newsletter_template_empty', array('to' => $user));
1624
1625
1626
			/// e-mail providers can potentially break html codes because it exceeds a limit (set by the inbox)
1627
			/// REFERENCE: https://stackoverflow.com/questions/12216228/html-email-annoying-line-breaking
1628
			$template = str_replace("<", "\r\n<", $template);
1629
1630
			echo $template . "<br/><br/>";
1631
1632
			if (elgg_is_active_plugin('phpmailer'))
1633
				phpmailer_send($user->email, $user->name, $subject, $template, NULL, true );
1634
			else
1635
				mail($user->email, $subject, $template, cp_get_headers());
1636
1637
1638
			// delete and clean up the notification, already sent so we don't need to keep it anymore
1639
			$query = "DELETE FROM notification_digest WHERE user_guid = {$user->getGUID()}";
1640
			$result = delete_data($query);
1641
1642
		}
1643
	}
1644
}
1645
1646
1647
/**
1648
 * check if user has access then prepare the notification and send (if applicable)
1649
 *
1650
 * @param ElggObject					$entity			entity that has been created
1651
 * @param array(guid, email, username)	$to_user		recipient
1652
 * @param array(various)				$message		message that will be in the notification
0 ignored issues
show
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...
1653
 * @param int							$guid_two		author or group id (check if subscribe to friend or group)
1654
 */
1655
function cp_notification_preparation_send($entity, $to_user, $message, $guid_two, $subject) {
1656
1657
	$template = elgg_view('cp_notifications/email_template', $message);
1658
1659
	// TODO: fix up mission
1660
	if (strcmp($entity->getSubtype(),'mission') == 0) {
1661
		// send out emails
1662
		if ($to_user->getGUID() != elgg_get_logged_in_user_guid()) { // prevents notification to be sent to the sender
1663
			if ($to_user instanceof ElggUser) {
1664
				if (elgg_is_active_plugin('phpmailer'))
1665
					phpmailer_send( $to_user->email, $to_user->name, $subject, $template, NULL, true );
1666
				else
1667
					mail($to_user->email,$subject,$template,cp_get_headers());
1668
			}
1669
1670
		} else {
1671
			// check if user has access to the content (DO NOT send if user has no access to this object)
1672
1673
			if (has_access_to_entity($entity, $recipient_user) && $object->access_id != 0) {
0 ignored issues
show
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...
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...
1674
				//  GCCON-175: assemble the email content with correct username (for notification page)
1675
				$message['user_name'] = $to_user->username;
1676
1677
				// check if user subscribed to receiving notifications
1678
				if (check_entity_relationship($to_user->guid, 'cp_subscribed_to_email', $guid_two))
1679
				{
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
				// send a site notification
1687
				if (check_entity_relationship($to_user->guid, 'cp_subscribed_to_site_mail', $guid_two)) {
1688
					messages_send($subject, $template, $to_recipient->guid, $site->guid, 0, true, false);
0 ignored issues
show
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...
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...
1689
				}
1690
			} // end if (check for access)
1691
1692
		}
1693
	}
1694
}
1695
1696
1697
/**
1698
 * cp_send_new_password_request
1699
 *
1700
 * In order to modify core code, this action had to be overwritten.
1701
 * This is a mirror image of the core function: /engine/classes/Elgg/PasswordService.php
1702
 *
1703
 * Only need to send an e-mail notification out, no need to send a site-mail
1704
 *
1705
 * @param ElggUser 	$user 	user entity
1706
 */
1707
function cp_send_new_password_request($user) {
1708
	if (!$user instanceof ElggUser)
1709
		return false;
1710
1711
	// generate code
1712
	$code = generate_random_cleartext_password();
1713
	$user->setPrivateSetting('passwd_conf_code', $code);
1714
	$user->setPrivateSetting('passwd_conf_time', time());
1715
1716
	$link = elgg_get_site_url()."changepassword?u={$user->guid}&c={$code}";
1717
	$ip_address = _elgg_services()->request->getClientIp();
1718
1719
	// we don't need to check if the plugin (cp_notifications) is enabled here
1720
	$message = array(
1721
		'cp_password_request_user' => $user->username,
1722
		'cp_password_request_ip' => $ip_address,
1723
		'cp_password_request_url' => $link,
1724
		'cp_msg_type' => 'cp_forgot_password',
1725
	);
1726
1727
	$subject = elgg_echo('cp_notify:subject:forgot_password', array(), "en");
1728
	$subject .= ' | '.elgg_echo('cp_notify:subject:forgot_password', array(), "fr");
1729
	$template = elgg_view('cp_notifications/email_template', $message);
1730
1731
	if (elgg_is_active_plugin('phpmailer'))
1732
		phpmailer_send( $user->email, $user->name, $subject, $template );
1733
	else
1734
		mail($user->email,$subject,$template,cp_get_headers());
1735
}
1736
1737
1738
/*
1739
 * cp_membership_request
1740
 *
1741
 * replaced the event (see init()) so we can send out notifications through this plugin instead
1742
 *
1743
 */
1744
function cp_membership_request($event, $type, $object) { 	// MUST always be sending notification
1745
	$request_user = get_user($object->guid_one); 			// user who sends request to join
1746
	$group_request = get_entity($object->guid_two);			// group that is being requested
1747
1748
	$message = array(
1749
		'cp_group_req_user' => $request_user,
1750
		'cp_group_req_group' => $group_request,
1751
		'cp_msg_type' => 'cp_closed_grp_req_type',
1752
	);
1753
	$template = elgg_view('cp_notifications/email_template', $message);
1754
	$subject = elgg_echo('cp_notify:subject:group_request',array($request_user->name, gc_explode_translation($group_request->name,'en')),'en');
1755
	$subject .= ' | '.elgg_echo('cp_notify:subject:group_request',array($request_user->name, gc_explode_translation($group_request->name,'fr')),'fr');
1756
1757
	$to_user = get_user($group_request->owner_guid);
1758
	if (elgg_is_active_plugin('phpmailer')) {
1759
		phpmailer_send( $to_user->email, $to_user->name, $subject, $template, NULL, true );
1760
	} else {
1761
		mail($to_user->email,$subject,$template,cp_get_headers());
1762
	}
1763
	messages_send($subject, $template, $to_user->guid, elgg_get_site_entity()->guid, 0, true, false);
1764
}
1765
1766
1767
1768
1769
/**
1770
 * intercepts all email and stops emails from sending
1771
 *
1772
 * @param string $hook    The name of the plugin hook
1773
 * @param string $type    The type of the plugin hook
1774
 * @param mixed  $value   The current value of the plugin hook
0 ignored issues
show
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...
1775
 * @param mixed  $params  Data passed from the trigger
1776
 */
1777
function cpn_email_handler_hook($hook, $type, $notification, $params) {
1778
	return false;
1779
}
1780
1781
1782
/**
1783
 * implements the icons (likes, in this case) within the context of the entity
1784
 * TODO: make the likes button act as AJAX
1785
 *
1786
 * @param string $hook    The name of the plugin hook
1787
 * @param string $type    The type of the plugin hook
1788
 * @param mixed  $value   The current value of the plugin hook
0 ignored issues
show
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...
1789
 * @param mixed  $params  Data passed from the trigger
1790
 *
1791
 * @return mixed if not null, this will be the new value of the plugin hook
1792
 */
1793
function notify_entity_menu_setup($hook, $type, $return, $params) {
1794
	$entity = $params['entity'];
1795
	$do_not_subscribe_list = array('comment','discussion_reply','widget');
1796
	if (elgg_in_context('widgets') || in_array($entity->getSubtype(), $do_not_subscribe_list))  return $return;
1797
1798
	// cyu - check for everything to put the bell thingy (xor)
1799
	$allow_subscription = false;
1800
	if ( $entity->getContainerEntity() instanceof ElggGroup ) {
1801
		$allow_subscription = ($entity->getContainerEntity()->isMember(elgg_get_logged_in_user_entity()) == 1 ) ? true : false;
1802
1803
	} else if ($entity instanceof ElggGroup) {
1804
		$allow_subscription =  ($entity->isMember(elgg_get_logged_in_user_entity()) == 1 ) ? true : false;
1805
1806
	} else if ( $entity->getContainerEntity() instanceof ElggUser )
1807
		$allow_subscription = true;
1808
1809
	if ($entity instanceof ElggGroup) {
1810
		$entType = 'group';
1811
		if($entity->title3){
1812
				$entName = gc_explode_translation($entity->title3, get_current_language());
1813
		}else{
1814
				$entName = $entity->name;
1815
		}
1816
	} else {
1817
		if(!in_array($entity->getSubtype(), array('comment', 'discussion_reply', 'thewire'))){
1818
			if($entity->title3){
1819
					$entName = gc_explode_translation($entity->title3, get_current_language());
1820
			}else{
1821
					$entName = $entity->title;
1822
			}
1823
		} else {
1824
			$entName = $entity->getOwnerEntity()->name;
1825
		}
1826
		$entType = $entity->getSubtype();
1827
	}
1828
1829
	if ($allow_subscription && elgg_is_logged_in()) {
1830
	    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()) ) {
1831
1832
1833
			$bell_status = (elgg_is_active_plugin('wet4')) ? '<span class="icon-unsel fa fa-lg fa-bell"><span class="wb-inv">'.elgg_echo('entity:unsubscribe:link:'.$entType, array($entName)).'</span></span>' : elgg_echo('cp_notify:stop_subscribe');
1834
1835
1836
		    $return[] = ElggMenuItem::factory(array(
1837
			    'name' => 'unset_notify',
1838
			    'href' => elgg_add_action_tokens_to_url("/action/cp_notify/unsubscribe?guid={$entity->guid}"),
1839
			    'text' => $bell_status,
1840
			    'title' => elgg_echo('cp_notify:unsubBell'),
1841
			    'priority' => 1000,
1842
			    'class' => 'bell-subbed',
1843
			    'item_class' => ''
1844
		    ));
1845
1846
	    } else {
1847
1848
		    $bell_status = (elgg_is_active_plugin('wet4')) ? '<span class="icon-unsel fa fa-lg fa-bell-slash-o"><span class="wb-inv">'.elgg_echo('entity:subscribe:link:'.$entType, array($entName)).'</span></span>' : elgg_echo('cp_notify:start_subscribe');
1849
1850
1851
		    $return[] = ElggMenuItem::factory(array(
1852
			    'name' => 'set_notify',
1853
			    'href' => elgg_add_action_tokens_to_url("/action/cp_notify/subscribe?guid={$entity->guid}"),
1854
			    'text' => $bell_status,
1855
			    'title' => elgg_echo('cp_notify:subBell'),
1856
			    'priority' => 1000,
1857
			    'class' => '',
1858
			    'item_class' => ''
1859
		    ));
1860
		}
1861
	}
1862
	return $return;
1863
}
1864
1865