/** * Controll Class Definition * * Classe met control-methods * Gebruik alleen "static methods" zodat er geen instantie gecreerd hoeft te worden! * * @package backstage3 * @version 1.0 * @author Bart van Raaij * @copyright Cybox internet & communicatie 2013 * @since 2012 */ use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; class Control { private $error; /** parsed opgevraagde url. niet te verwarren met Str::parseUrl **/ public static function parseRequestURI(){ $args = Str::getVar('args'); $urlcomponents = array_filter(explode("/",$args)); return array_replace(array_fill(0,6,false),$urlcomponents); } public static function detectMobile($overrideua=false){ if($overrideua){ if(is_array($overrideua)) $mobileua = $overrideua; else $mobileua = array(strtolower($overrideua)); } else $mobileua = array('android','webos','iphone','ipod','blackberry','iemobile','opera mini'); $clientua = (isset($_SERVER['HTTP_USER_AGENT'])) ? strtolower($_SERVER['HTTP_USER_AGENT']) : 'notset'; $test = (preg_match('/'.implode('|', $mobileua).'/', $clientua, $matches)); return ($test!==0); } /** ingebouwde cbxTwit functionaliteit **/ /** let op: user/pass is cbxTwit user/pass, geen twitter.com user/pass natuulrijk! * * @param $user * @param $key * @param $aantal * @return array */ public static function cbxTwitGetTweets($user,$key,$aantal){ $feed = PROTOCOL.'://cdn.cybox.nl/cbxtwit/gettweets.php?user='.$user.'&key='.$key.'&c='.$aantal; $json = file_get_contents($feed); $tweets = json_decode($json,true); return $tweets; } public static function loadConfigFromFile($path){ $json = file_get_contents($path); $config = json_decode($json,true); return $config; } public static function loadConfig(){ $config = array(); $config += Control::loadConfigFromFile(BACKSTAGE_LIBRARY_PATH.'../conf/global.conf'); $config += Control::loadConfigFromFile(BACKSTAGE_LIBRARY_PATH.'../conf/local.conf'); if(!isset($config['ROOT'])){ $config['ROOT'] = str_replace('backstageLib/Base','',__dir__); } //some additions $config['TMP_PATH'] = $config['ROOT'].'files/tmp/'; $config['LOG_PATH'] = $config['ROOT'].'files/log/'; $config['MEDIA_UPLOAD_PATH'] = $config['ROOT'].'files/media/'; $config['IMAGES_PATH'] = $config['ROOT'].'images/'; $config['CYBOX'] = (isset($_SERVER['REMOTE_ADDR']) && isset($config['CYBOX_IPS']) && in_array($_SERVER['REMOTE_ADDR'],$config['CYBOX_IPS'])); //alle config values definen foreach($config as $key=>$value){ if(is_array($value)) $value = serialize($value); define($key,$value); } return $config; } public static function showErrors(){ error_reporting(E_ALL); ini_set('display_errors',1); } public static function includeJs($plugin,$action,$forcereload=false){ if($action=='') $action='main'; if($action=='new') $action='edit'; $specificjs = 'js/plugin/plugin_'.$plugin.'_'.$action.'.js'; if(file_exists($specificjs)){ if($forcereload) $specificjs .= '?'.uniqid(); echo ''; } } public static function mail($to,$fromname,$fromaddr,$replytoname=false,$replytoaddr=false,$subject,$htmlmessage,$bccaddr=false){ $message = '
'.PHP_EOL; $message .= $htmlmessage.PHP_EOL; $message .= ""; $headers = "From: ".$fromname." <".$fromaddr.">\n"; if($replytoaddr && !$replytoname) $headers .= "Reply-To: ".$replytoaddr . "\n"; if($replytoname&&$replytoaddr) $headers .= "Reply-To: " . $replytoname . " " . "<" . $replytoaddr . ">\n"; if($bccaddr) $headers .= "Bcc: ". $bccaddr . "\n"; $headers .= "X-Priority: 3\n"; $headers .= "X-Mailer: PHP". phpversion() ."\n"; $headers .= "MIME-Version: 1.0\n"; $headers .= "Content-Type: text/html; charset=\"utf-8\"\n"; $headers .= "Content-Transfer-Encoding: 8bit\n"; #$headers .= $message; $additional_parameters = "-f ".$fromaddr; $s= mail($to, $subject, $message, $headers, $additional_parameters); return $s; } public static function htmlMailAttachments($to,$toname=false,$fromname,$fromaddr,$replytoname=false,$replytoaddr=false,$subject,$htmlmessage,$attachments=false,$bccaddr=false){ $messages = array(); $allowedFileTypes = ['jpeg', 'jpg', 'gif', 'png', 'pdf', 'doc', 'docx']; $allowedFileSize = 5; /* html message */ $message = ' '; $message .= $htmlmessage; $message .= ""; $mail = new PHPMailer(true); $mail->setLanguage('nl'); //Recipients $mail->setFrom($fromaddr, $fromname); $mail->addAddress($to, $toname); if($replytoaddr && $repltytoname) { $mail->addReplyTo($replytoaddr, $replytoname); } if($bccaddr) $mail->addBCC($bccaddr); //Attachments if($attachments) { foreach ($attachments as $key => $file) { if (!empty($file['tmp_name'])) { $uploadfile = tempnam(sys_get_temp_dir(), hash('sha256', $file['name'])); if (move_uploaded_file($file['tmp_name'], $uploadfile)) { if ($file['size'] > 0 && $file['size'] <= ($allowedFileSize * 1048576)) { $pathinfo = pathinfo($file['name']); if (in_array($pathinfo['extension'], $allowedFileTypes)) { $prefix = ""; $mail->addAttachment($uploadfile, 'bestand.' . $pathinfo['extension']); } else { $messages[] = ['type' => 'error', 'info' => 'Bestandstype niet toegestaan, graag ander bestandsformaat insturen.']; } } else { $messages[] = ['type' => 'error', 'info' => 'Bestand is te groot, probeer het opniew.']; } } else { $messages[] = ['type' => 'error', 'info' => 'Bestand kon niet worden verwerkt.']; } } } } //Content $mail->isHTML(true); // Set email format to HTML $mail->Subject = $subject; $mail->Body = $message; if (empty($messages)) { if ($mail->send()) { return true; } else { $messages[] = ['type' => 'error', 'info' => 'Bericht kon niet worden verzonden, error bericht: '. $mail->ErrorInfo]; } } return $messages; } public static function httpGet($url){ $curl = curl_init(); curl_setopt($curl, CURLOPT_VERBOSE, TRUE); curl_setopt ($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_TIMEOUT, 30); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl,CURLOPT_IPRESOLVE,CURL_IPRESOLVE_V4); #curl_setopt($curl,CURLOPT_FRESH_CONNECT,true); $result = curl_exec ($curl); return $result; } public function findAddress($postcode,$huisnummer=false){ #if($huisnummer==false) return false; $headers = array(); $headers[] = 'X-Api-Key: '.POSTCODE_API_KEY; // De URL naar de API call $postcode = strtoupper(str_replace(' ','',$postcode)); $url = 'https://postcode-api.apiwise.nl/v2/addresses/?postcode='.$postcode; if($huisnummer){ $huisnummer = strtoupper(str_replace(' ','',$huisnummer)); $url .= '&number='.$huisnummer.''; } $curl = curl_init($url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); // De ruwe JSON response $response = curl_exec($curl); if(!$response){ $this->error = curl_error($curl); curl_close($curl); return false; } // Gebruik json_decode() om de response naar een PHP array te converteren $data = json_decode($response,true); if(!isset($data['_embedded'])) return false; if(!isset($data['_embedded']['addresses'])) return false; if(!isset($data['_embedded']['addresses'][0])) return false; $adrdata = $data['_embedded']['addresses'][0]; $sdata = array(); $sdata['street'] = $adrdata['street']; $sdata['house_number'] = $adrdata['number']; $sdata['postcode'] = $adrdata['postcode']; $sdata['town'] = $adrdata['city']['label']; $sdata['municipality'] = $adrdata['municipality']['label']; $sdata['province'] = $adrdata['province']['label']; $sdata['latitude'] = $adrdata['geo']['center']['wgs84']['coordinates'][1]; $sdata['longitude'] = $adrdata['geo']['center']['wgs84']['coordinates'][0]; $sdata['x'] = $adrdata['geo']['center']['rd']['coordinates'][0]; $sdata['y'] = $adrdata['geo']['center']['rd']['coordinates'][1]; return $sdata; } public function getError(){ return $this->error; } /** aanmelden mailstage * * @param $email * @return bool */ public static function subscribeMailstage($email) { /* mailstage v1 $api_url = "http://mailstage.cybox.nl/API/subscribe.php"; $argumenten = "syscode=".MAILSTAGE_SYSCODE."&clientid=".MAILSTAGE_CLIENT_ID."&address=".$email."&name=".$name; if(defined('MAILSTAGE_GROUP_ID')) $argumenten .= "&mailing_group_id=".MAILSTAGE_GROUP_ID; $request_url = $api_url."?".$argumenten; $result = Control::httpGet($request_url); return ($result=='0'); */ //Deze zet voor nu gewoon de addressen in db! if($email = filter_var($email,FILTER_VALIDATE_EMAIL)){ //$s = $this->db->insertRow('nieuwsbrief',array('address'=>$email)); //Nieuwe mailstage!! $params = array('username'=>'[username]','key'=>'[key]','group_id'=>0,'address'=>$email); $request = urlencode(base64_encode(json_encode($params,JSON_UNESCAPED_UNICODE + JSON_UNESCAPED_SLASHES))); $fullurl = 'http://www.mailstage.nl/act/subscribe/'.$request; $s = file_get_contents($fullurl); return ($s!=0); } else return false; } public static function mailstageArchive() { global $settings; $api_url = "http://mailstage.cybox.nl/API/archief.php"; $argumenten = "token=".$settings->ms_token."&client=".$settings->ms_clientid; $request_url = $api_url."?".$argumenten; $result = Control::httpGet($request_url); if ($result == "0" || $result == "1") return false; return unserialize($result); } public static function HTTPAuth($users,$realm='Backstage preview'){ if (isset($_SERVER['PHP_AUTH_USER'])) { if(isset($users[$_SERVER['PHP_AUTH_USER']]) && $users[$_SERVER['PHP_AUTH_USER']] == $_SERVER['PHP_AUTH_PW']) { return true; } } header('WWW-Authenticate: Basic realm="'.$realm.'"'); header('HTTP/1.0 401 Unauthorized'); header('Content-type: text/plain'); echo 'Invalid credentials'; exit; } public static function gitInfo(){ exec('git describe --tags --abbrev=0',$tag); exec('git rev-parse --abbrev-ref HEAD',$branch); return array('tag'=>$tag[0],'branch'=>$branch[0]); } }