Max YO před 6 roky
rodič
revize
9fe6c4178f
3 změnil soubory, kde provedl 327 přidání a 0 odebrání
  1. 101 0
      src/Lib/Email_lib.php
  2. 131 0
      src/Lib/Pdf_lib.php
  3. 95 0
      src/Lib/Sms_lib.php

+ 101 - 0
src/Lib/Email_lib.php

@@ -0,0 +1,101 @@
+<?php
+
+namespace KarmaFW\Lib;
+
+use PHPMailer\PHPMailer\PHPMailer;
+use PHPMailer\PHPMailer\Exception;
+
+
+class Email_lib
+{
+
+	public static function sendmail($to, $subject, $message, $from, $from_name=null)
+	{
+		$headers = "";
+		return mail($to, $subject, $message, $headers);
+	}
+
+
+	public static function sendmailSMTP($to, $subject, $message_html, $message_text='', $from=null, $from_name=null, $options=[]) {
+
+		if (defined('SMTP_CALLBACK') && empty($options['no_callback'])) {
+			$smtp_callback_func = SMTP_CALLBACK;
+			if (is_callable($smtp_callback_func)) {
+				return $smtp_callback_func($to, $subject, $message_html, $message_text, $from, $from_name);
+			}
+			// aucun message envoyé ni callback
+			return boolval($smtp_callback_func); // permet de mettre "1" pour un retour ok ou "0" pour un retour en erreur
+		}
+
+		if (true) {
+			$mail = [
+				'from' => $from,
+				'from_name' => $from_name,
+				'to' => $to,
+				'subject' => $subject,
+				'message_html' => $message_html,
+				'message_text' => $message_text,
+			];
+			//echo "Emails désactivés";
+			//pre($mail, 1);
+			return false;
+		}
+
+		$mail = new PHPmailer();
+
+		$mail->IsSMTP();
+		$mail->IsHTML(! empty($message_html));
+		$mail->CharSet = "UTF-8";
+
+		if (defined('SMTP_HOST') && ! empty(SMTP_HOST)) {
+			$mail->SMTPAuth = SMTP_SMTPAUTH;
+			$mail->Host = SMTP_HOST;
+			$mail->Port = SMTP_PORT;
+			$mail->Username = SMTP_USERNAME;
+			$mail->Password = SMTP_PASSWORD;
+
+		} else {
+			$mail->Host = 'localhost';
+			$mail->Port = 25;
+		}
+
+		if (empty($from)) {
+			$processUser = posix_getpwuid(posix_geteuid());
+			//$from = $processUser['name'] . "@" . gethostname();
+			$from = $processUser['name'] . "@localhost";
+		}
+		$mail->From = $from;
+
+		if (! empty($from_name)) {
+			$mail->FromName = $from_name;
+		}
+
+		$mail->AddAddress($to); 
+
+		$mail->Subject = $subject;
+
+		if (! empty($message_html)) {
+			$mail->Body = $message_html;
+
+		    if (! empty($message_text)) {
+		    	$mail->AltBody = $message_text;
+		    }
+
+		} else {
+			$mail->Body = $message_text;
+		}
+
+		if(!$mail->Send()){
+			//echo $mail->ErrorInfo;
+			$ok = false;
+		} else {
+			//echo 'Mail envoyé avec succès';
+			$ok = true;
+		}
+		$mail->SmtpClose();
+		unset($mail);
+
+		return $ok;
+	}
+
+}

+ 131 - 0
src/Lib/Pdf_lib.php

@@ -0,0 +1,131 @@
+<?php
+
+namespace KarmaFW\Lib;
+
+use \Mpdf\Mpdf;
+
+
+class Pdf_lib
+{
+
+	protected $tocPreHTML = '<div class="container"><h1>Table des matières</h1><br /><br />';
+	protected $tocPostHTML = '';
+	protected $tocFont = 'Roboto';
+	protected $subject = 'Document';
+	protected $author = '';
+	protected $creator = '';
+
+
+	protected static function getFooter()
+	{
+		return 'KarmaCRM by <a href="http://www.karma-solutions.fr/" class="text-muted">Karma Solutions</a> © 2019 &nbsp; - &nbsp; page {PAGENO}/{nb} &nbsp;';
+	}
+
+
+
+	public static function buildPDF($pages_html, $pdf_options=array(), $output_inline_filename=true, $output_file=null)
+	{
+		// Buid PDF using MPDF
+
+		$mpdf = new Mpdf([
+			//'orientation' => 'L', 
+			//'format' => [210, 297], 
+			'format' => ! empty($pdf_options['format']) ? $pdf_options['format'] : 'A4-L', 
+			'tempDir' => ! empty($pdf_options['tempDir']) ? $pdf_options['tempDir'] : '/tmp/mpdf',
+			'margin_left' => 0,
+			'margin_right' => 0,
+			'margin_top' => 0,
+			'margin_bottom' => 0,
+			'margin_header' => 0,
+			'margin_footer' => 0,
+		]);
+
+		//$mpdf->showImageErrors = true;
+
+		if (! empty($pdf_options['base_href'])) {
+			$mpdf->setBasePath($pdf_options['base_href']);
+		}
+
+		$mpdf->SetDisplayMode(! empty($pdf_options['display_mode']) ? $pdf_options['display_mode'] : 'fullpage');
+		$mpdf->SetFontSize(! empty($pdf_options['font_size']) ? $pdf_options['font_size'] : 6);
+		//$mpdf->useOnlyCoreFonts = true;
+		//$mpdf->shrink_tables_to_fit = 1;
+
+		//$mpdf->SetHTMLHeader("Page HTML Head");
+		//$mpdf->SetHTMLFooter("Page HTML Foot");
+
+		//$mpdf->SetHeader("Page Head");
+
+		
+		/*
+		$mpdf->h2toc = array(
+		    'H1' => 0, 
+		    'H2' => 1, 
+		    'H3' => 2
+		);
+		*/
+		
+
+
+		$page_idx = 0;
+		foreach ($pages_html as $page_name => $page) {
+			$page_idx++;
+
+			$toc_level = isset($page['toc_level']) ? $page['toc_level'] : 0;
+			$toc_title = isset($page['toc_title']) ? $page['toc_title'] : $page_name;
+
+			$mpdf->AddPage();
+
+			if ($page_idx == 2) {
+				// on met le footer uniquement à partir de la 2eme page
+				$mpdf->SetFooter( self::getFooter() );
+			}
+
+
+			if ($page_idx > 1) {
+				$mpdf->TOC_Entry($toc_title, $toc_level);
+			}
+
+
+			if ($page_idx == 2) {
+				// on insere la table des matieres juste apres la 1ere page
+				$mpdf->TOCpagebreakByArray( [
+					'tocfont' => static::$tocFont,
+					'toc_mgl' => 8,
+					'toc_mgr' => 8,
+					'toc-preHTML' => static::$tocPreHTML, 
+					'toc-postHTML' => static::$tocPostHTML, 
+					'links' => true,
+				] );
+			}
+
+			if (! empty($page['content'])) {
+				@$mpdf->WriteHTML($page['content']);
+			}
+
+			if (! empty($page['footer'])) {
+				@$mpdf->setHTMLFooter($page['footer']);
+			}
+		}
+
+		//$mpdf->SetTitle("Rapport de crawl Karma Crawler"); // utiliser strcode2utf() si besoin
+		$mpdf->SetSubject(static::$subject);
+		$mpdf->SetAuthor(static::$author);
+		$mpdf->SetCreator(static::$creator);
+		
+
+
+		// Save file
+		if ($output_file) {
+			$mpdf->Output($output_file, 'F');
+		}
+
+		// output file inline
+		if ($output_inline_filename) {
+			$pdf_filename = basename($output_inline_filename);
+			$mpdf->Output($pdf_filename, 'I');
+		}
+
+	}
+
+}

+ 95 - 0
src/Lib/Sms_lib.php

@@ -0,0 +1,95 @@
+<?php
+
+namespace KarmaFW\Lib;
+
+
+class Sms_lib
+{
+
+
+	public static function sendSmsFreeMobile($message)
+	{
+		if (! defined('FREEMOBILE_API_KEY') || empty(FREEMOBILE_API_KEY)) {
+			return false;
+		}
+		if (! defined('FREEMOBILE_API_SECRET') || empty(FREEMOBILE_API_SECRET)) {
+			return false;
+		}
+		$API_KEY = FREEMOBILE_API_KEY;
+		$API_SECRET = FREEMOBILE_API_SECRET;
+		$url = "https://smsapi.free-mobile.fr/sendmsg?user=" . $API_KEY . "&pass=" . $API_SECRET . "&msg=" . urlencode($message);
+		$result = @file_get_contents($url);
+		if ($result === false) {
+			return false;
+		}
+		if ($result === '') {
+			return true;
+		}
+		return false;
+	}
+
+
+	public static function sendSmsSmsEnvoi($numero, $message, $sender=null)
+	{
+		// https://www.smsenvoi.com/api-sms/
+		// https://www.smsenvoi.com/api-sms/librairie-php/tutoriel-comment-envoyer-des-sms-en-php/ (7 à 10 centime par sms selon forfait)
+		// https://www.smsenvoi.com/site/webroot/API/API_SMSENVOI_HTTP_V2.pdf
+
+
+		if (! defined('SMSENVOI_API_KEY') || empty(SMSENVOI_API_KEY)) {
+			return false;
+		}
+		if (! defined('SMSENVOI_SECRET_KEY') || empty(SMSENVOI_SECRET_KEY)) {
+			return false;
+		}
+		
+		$url = "https://www.smsenvoi.com/httpapi/sendsms/";
+
+		$params = array(
+			"email" => SMSENVOI_API_KEY,
+			'apikey' => SMSENVOI_SECRET_KEY,
+			"message[type]" => 'sms',
+			"message[subtype]" => 'STANDARD',
+			//"message[senderlabel]" => 'PHP App',
+			"message[content]" => $message,
+			"message[recipients]" => $numero,
+		);
+		if (! empty($sender)) {
+			$params['message[senderlabel]'] = $sender;
+		}
+		$postdata = http_build_query($params);
+
+		$opts = array(
+			'http' => array(
+		        'method'  => 'POST',
+		        'header'  => "Content-type: application/x-www-form-urlencoded",
+		        'content' => $postdata
+	    	)
+	        , 'ssl' => array(
+		        "verify_peer" => false,
+		        "verify_peer_name" => false,
+	        )
+		);
+		$context = stream_context_create($opts);
+
+		//echo $url; exit;
+		$result_json = file_get_contents($url, false, $context);
+		$result = json_decode($result_json);
+
+		return ! empty($result->success);
+	}
+
+
+
+}
+
+
+//Sms_lib::sendSmsFreeMobile('test ' . date('Y-m-d H:i:s'));
+
+
+if (realpath($argv[0]) == realpath(__FILE__) && ! empty($argv[1]) ) {
+	Sms_lib::sendSmsFreeMobile($argv[1]);
+	echo "SMS sent !" . PHP_EOL;
+}
+
+// php Sms_lib.php "mon message envoyé par l'api Free"