403Webshell
Server IP : 138.197.176.125  /  Your IP : 216.73.217.54
Web Server : Apache/2.4.41 (Ubuntu)
System : Linux SuiteCRM-8 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64
User : root ( 0)
PHP Version : 8.3.19
Disable Function : NONE
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : ON  |  Sudo : ON  |  Pkexec : ON
Directory :  /var/www/vtiger75/modules/CTPushNotification/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ Back ]     

Current File : /var/www/vtiger75/modules/CTPushNotification/PUSHNotifier.php
<?php
/*+*******************************************************************************
 * The content of this file is subject to the CRMTiger Pro license.
 * ("License"); You may not use this file except in compliance with the License
 * The Initial Developer of the Original Code is vTiger
 * The Modified Code of the Original Code owned by https://crmtiger.com/
 * Portions created by CRMTiger.com are Copyright(C) CRMTiger.com
 * All Rights Reserved.
  ***************************************************************************** */
require_once 'vtlib/Vtiger/Net/Client.php';

class PUSHNotifier {
	/**
	 * Send SMS (Creates SMS Entity record, links it with related CRM record and triggers provider to send sms)
	 *
	 * @param String $message
	 * @param Array $tonumbers
	 * @param Integer $ownerid User id to assign the SMS record
	 * @param mixed $linktoids List of CRM record id to link SMS record
	 * @param String $linktoModule Modulename of CRM record to link with (if not provided lookup it will be calculated)
	 */
	static function sendnotification($message, $tonumbers, $ownerid = false, $linktoids = false, $linktoModule = false, $title = '', $ws_id = '',$workflowId = '') {
		$license_data = CTMobileSettings_Module_Model::getLicenseData();
        if(strtolower($license_data['Plan']) != 'free'){
			global $current_user, $adb;

			if($ownerid === false) {
				if(isset($current_user) && !empty($current_user)) {
					$ownerid = $current_user->id;
				} else {
					$ownerid = 1;
				}
			}
			$title = html_entity_decode(decode_html($title),ENT_QUOTES,'UTF-8');
			$message = html_entity_decode(decode_html($message),ENT_QUOTES,'UTF-8');
			foreach ($tonumbers as $userid){
				if($userid!=''){
					$checkpermission = self::checkNotificationPermission($workflowId,$userid);
					if($checkpermission){
						$data = self::getdevicekey($userid);
						$devicekey = $data['devicetoken'];
						$device_type = $data['device_type'];
						if($devicekey != '' && $device_type != ''){
							$record_id = self::saveNotification($title,$message,$userid,$linktoids,'Draft',$devicekey);
							if($record_id != ''){
								$result = self::sendpushnotification($message,$devicekey,$device_type, $ws_id, $linktoModule, $title);
								if($result){
									$results = json_decode($result,true);
			                        if($results['success']){
			                            $sendstatus = "Send";
			                        }else{
			                            $sendstatus = "Failed";
			                        }
									$recordModel = Vtiger_Record_Model::getInstanceById($record_id, $moduleName);
									$modelData = $recordModel->getData();
									$recordModel->set('mode', 'edit');
									$recordModel->set('pushnotification_response', $result);
									$recordModel->set('pushnotificationstatus', $sendstatus);
									$recordModel->save();
								}
							}
						}else{
							$record_id = self::saveNotification($title,$message,$userid,$linktoids,'Failed','');
						}
					}else{
						$record_id = self::saveNotification($title,$message,$userid,$linktoids,'Failed','');
					}
					
				}
			}
		}	
	}


	static function sendCustomerNotification($message, $tonumbers, $ownerid = false, $linktoids = false, $linktoModule = false, $title = '', $ws_id = '',$workflowId = '') {
		$license_data = CTMobileSettings_Module_Model::getLicenseData();
        if(strtolower($license_data['Plan']) != 'free'){
			global $current_user, $adb;

			if($ownerid === false) {
				if(isset($current_user) && !empty($current_user)) {
					$ownerid = $current_user->id;
				} else {
					$ownerid = 1;
				}
			}
			$title = html_entity_decode(decode_html($title),ENT_QUOTES,'UTF-8');
			$message = html_entity_decode(decode_html($message),ENT_QUOTES,'UTF-8');
			foreach ($tonumbers as $contactid){
				if($contactid!=''){
					$checkpermission = self::checkCustomerNotificationPermission($workflowId,$contactid);
					if($checkpermission){
						$data = self::getCustomerdevicekey($contactid);
						$devicekey = $data['devicetoken'];
						$device_type = $data['device_type'];
						if($devicekey != '' && $device_type != ''){
							//$record_id = true;
							$record_id = self::saveCustomerNotification($title,$message,$userid,$linktoids,'Draft',$devicekey,$contactid);
							if($record_id){
								$result = self::sendCustomerPushNotification($message,$devicekey,$device_type, $ws_id, $linktoModule, $title);
								if($result){
									/*$filename = 'logs/notification_log.log';
									$time_utc = new DateTime('now', new DateTimeZone('UTC'));
									$currentTimestamp = $time_utc->format('Y-m-d H:i:s');
									$user_postData = $currentTimestamp." schedule later method call, next trigger time is ";
									if (file_exists($filename)) {
									$userfile = fopen($filename, "a+");
									fwrite($userfile,"\n". print_r($result,true));
									fclose($userfile);
									} else {
									$userfile = fopen($filename, "w+");
									fwrite($userfile, print_r($result,true));
									fclose($userfile);
									}*/
									$results = json_decode($result,true);
			                        if($results['success']){
			                            $sendstatus = "Send";
			                        }else{
			                            $sendstatus = "Failed";
			                        }
									$recordModel = Vtiger_Record_Model::getInstanceById($record_id, $moduleName);
									$modelData = $recordModel->getData();
									$recordModel->set('mode', 'edit');
									$recordModel->set('pushnotification_response', $result);
									$recordModel->set('pushnotificationstatus', $sendstatus);
									$recordModel->save();
								}
							}
						}else{
							//$record_id = self::saveNotification($title,$message,$userid,$linktoids,'Failed','');
						}
					}else{
						//$record_id = self::saveNotification($title,$message,$userid,$linktoids,'Failed','');
					}
					
				}
			}
		}	
	}
	
	static function getCustomerdevicekey($contactid) {
		global $log, $adb;
		
		$perm_qry = "SELECT devicetoken,device_type FROM ctcustomerportal_devicetoken  WHERE contact_id = ? ";
		$perm_result = $adb->pquery($perm_qry, array($contactid));
		$perm_rows = $adb->num_rows($perm_result);
		if($perm_rows > 0){
			$devicetoken = $adb->query_result($perm_result, 0, "devicetoken");
			$device_type = $adb->query_result($perm_result,$i,'device_type');
			$data = array('devicetoken'=>$devicetoken,'device_type'=>$device_type);
			return $data;
		}
		return '';
	}

	static function checkCustomerNotificationPermission($workflowId,$contactid){
		return true;
	}

	static function getdevicekey($userid) {
		global $log, $adb;
		
		$perm_qry = "SELECT devicetoken,device_type FROM ctmobile_userdevicetoken  WHERE userid = ? ;";
		$perm_result = $adb->pquery($perm_qry, array($userid));
		$perm_rows = $adb->num_rows($perm_result);
		if($perm_rows > 0){
			$devicetoken = $adb->query_result($perm_result, 0, "devicetoken");
			$device_type = $adb->query_result($perm_result,$i,'device_type');
			$data = array('devicetoken'=>$devicetoken,'device_type'=>$device_type);
			return $data;
		}
		return '';
	}


	static function sendCustomerPushNotification($message,$devicekey, $device_type, $ws_id, $linktoModule, $title) {
		
		 define( 'API_ACCESS_KEY', 'AAAAt8Pwu4Q:APA91bEDERSiXTD5osQMNzvAtEsZxYH5yQEGEv2s0cOAa5hATJXvxaZ7b9EJpMe9mCkSL_Sw_5n1TAdc9-dA0aVVSlVc6PKIKID-5VFoLdnRCrbwTlCsu--swRCs9Kk2wPw84QY7lIzV' );
       
		
        $fcmMsg = array(
			 'content_available'=> 'true',
        );
        if($device_type == 'ios'){
			//define( 'API_ACCESS_KEY', 'AIzaSyC9q9_LoSE5_faOalJx_6wl9Q7aeOq584I' );
			$notification = array('title' =>$title , 'text' => $message, 'sound' => 'default');
			$dataPayload = array('type'=>'record','recordId' => $ws_id , 'moduleName' => $linktoModule);
			$fcmFields = array(
				'to' => $devicekey ,
				'priority' => 'high',
				'notification' => $notification,
				'data' => $dataPayload
			);
			
		}else{
			$dataPayload = array('type'=>'record','message' => $message, 'title' => $title, 'recordId' => $ws_id , 'moduleName' => $linktoModule);
			
			$fcmFields = array(
				'to' => $devicekey ,
				'priority' => 'high',
				'notification' => $fcmMsg,
				'data' => $dataPayload

			);
		}
		
        $headers = array();
        $headers['Authorization'] = 'Bearer ' .API_ACCESS_KEY;
        $headers['Content-Type'] = 'application/json';
         
        $serviceURL = 'https://fcm.googleapis.com/fcm/send';
		$httpClient = new Vtiger_Net_Client($serviceURL);
		$httpClient->setHeaders($headers);
	    $result = $httpClient->doPost(json_encode($fcmFields));
	    $result = trim($result);
       
		return $result;
	}
	
	static function sendpushnotification($message,$devicekey, $device_type, $ws_id, $linktoModule, $title) {
		
		 define( 'API_ACCESS_KEY', 'AAAA_kGRtQ8:APA91bEWdbKg2fAycMdQGfhh6wWgdorH8D4J7lmcKq6tLE8RTKFg6_BKOQLNa_-agDsJugMCM3BrhFIPbvNq6EqW2PKO5E6SN-KwFs4RWRNcfl7TWrbNCkFhuaLtVg9F_FTrHal1tn7t' );
       
		
        $fcmMsg = array(
			 'content_available'=> 'true',
        );
        if($device_type == 'ios'){
			//define( 'API_ACCESS_KEY', 'AIzaSyC9q9_LoSE5_faOalJx_6wl9Q7aeOq584I' );
			$notification = array('title' =>$title , 'text' => $message, 'sound' => 'default');
			$dataPayload = array('type'=>'record','recordId' => $ws_id , 'moduleName' => $linktoModule);
			$fcmFields = array(
				'to' => $devicekey ,
				'priority' => 'high',
				'notification' => $notification,
				'data' => $dataPayload
			);
			
		}else{
			$dataPayload = array('type'=>'record','message' => $message, 'title' => $title, 'recordId' => $ws_id , 'moduleName' => $linktoModule);
			
			$fcmFields = array(
				'to' => $devicekey ,
				'priority' => 'high',
				'notification' => $fcmMsg,
				'data' => $dataPayload

			);
		}
		
        $headers = array();
        $headers['Authorization'] = 'Bearer ' .API_ACCESS_KEY;
        $headers['Content-Type'] = 'application/json';
         
        $serviceURL = 'https://fcm.googleapis.com/fcm/send';
		$httpClient = new Vtiger_Net_Client($serviceURL);
		$httpClient->setHeaders($headers);
	    $result = $httpClient->doPost(json_encode($fcmFields));
	    $result = trim($result);
       
		return $result;
	}

	static function checkNotificationPermission($workflowId,$userid){
		if($userid == ''){
			return false;
		}
		$adb = PearDatabase::getInstance();
		$userPrivModel = Users_Privileges_Model::getInstanceById($userid);
		$moduleModel = Vtiger_Module_Model::getInstance('CTPushNotification');
		$presence = array('0', '2');
		if(($userPrivModel->isAdminUser() ||
								$userPrivModel->hasGlobalReadPermission() ||
								$userPrivModel->hasModulePermission($moduleModel->get('id'))) &&in_array($moduleModel->get('presence'), $presence)){
			$main_perm_query = "SELECT ctmobile_notification_settings.notification_id,ctmobile_notification_settings.notification_enabled FROM ctmobile_notification_settings INNER JOIN ctmobile_notification_module_settings ON ctmobile_notification_settings.notification_id = ctmobile_notification_module_settings.notification_id WHERE ctmobile_notification_module_settings.workflow_id = ?";
			$main_perm_result = $adb->pquery($main_perm_query,array($workflowId));
			if($adb->num_rows($main_perm_result)){
				$notification_id = $adb->query_result($main_perm_result,0,'notification_id');
				$notification_enabled = $adb->query_result($main_perm_result,0,'notification_enabled');
				if($notification_enabled == '1'){
					$sub_perm_query = "SELECT user_id FROM ctmobile_notification_restriction WHERE user_id = ? AND notification_id = ?";
					$sub_perm_result = $adb->pquery($sub_perm_query,array($userid,$notification_id));
					if($adb->num_rows($sub_perm_result) == 0){
						return true;
					}else{
						return false;
					}
				}else{
					return false;
				}
			}else{
				return true;
			}
		}else{
			return false;
		}
	}

	static function saveNotification($title,$message,$assigned_user_id,$pn_related='',$pushnotificationstatus='Draft',$devicetoken=''){
		$module_Name = 'CTPushNotification';
		$focus = CRMEntity::getInstance($module_Name);
		$focus->column_fields['description'] = $message;
		$focus->column_fields['assigned_user_id'] = $assigned_user_id;
		$focus->column_fields['pn_related'] = $pn_related;
		$focus->column_fields['pushnotificationstatus'] = $pushnotificationstatus;
		$focus->column_fields['devicekey'] = $devicetoken;
		$focus->column_fields['pn_title'] =  $title;
		$focus->column_fields['read_status'] = 'Unread';
		$focus->save($module_Name);
		$record_id = $focus->id;
		return $record_id;
	}

	static function saveCustomerNotification($title,$message,$assigned_user_id,$pn_related='',$pushnotificationstatus='Draft',$devicetoken='',$contactid){
		$module_Name = 'CTPushNotification';
		$focus = CRMEntity::getInstance($module_Name);
		$focus->column_fields['description'] = $message;
		$focus->column_fields['assigned_user_id'] = $assigned_user_id;
		$focus->column_fields['pn_related'] = $pn_related;
		$focus->column_fields['pushnotificationstatus'] = $pushnotificationstatus;
		$focus->column_fields['devicekey'] = $devicetoken;
		$focus->column_fields['pn_title'] =  $title;
		$focus->column_fields['read_status'] = 'Unread';
		$focus->column_fields['related_record_id'] = $contactid;
		$focus->column_fields['source'] = 'CustomerPortal';
		$focus->save($module_Name);
		$record_id = $focus->id;
		return $record_id;
	}

	
}
?>

Youez - 2016 - github.com/yon3zu
LinuXploit