<?php
date_default_timezone_set('Asia/Jakarta');
?>
<?php
function tanggal_indo($date, $cetak_hari = false)
{
	$hari = array ( 1 =>    'Senin',
				'Selasa',
				'Rabu',
				'Kamis',
				'Jumat',
				'Sabtu',
				'Minggu'
			);
			
	$bulan = array (1 =>   'Januari',
				'Februari',
				'Maret',
				'April',
				'Mei',
				'Juni',
				'Juli',
				'Agustus',
				'September',
				'Oktober',
				'November',
				'Desember'
			);
	$split 	  = explode('-', $date);
	$tgl_indo = @$split[2] . ' ' . @$bulan[ (int)$split[1] ] . ' ' . @$split[0];
	
	if ($cetak_hari) {
		$num = date('N', strtotime($date));
		return $hari[$num] . ', ' . $tgl_indo;
	}
	return $tgl_indo;
}
?>

<?php
function url_strip($string){
    $string = str_replace(array('[\', \']'), '', $string);
    $string = preg_replace('/\[.*\]/U', '', $string);
    $string = preg_replace('/&(amp;)?#?[a-z0-9]+;/i', '-', $string);
    $string = preg_replace(array('/[^a-z0-9]/i', '/[-]+/') , '-', $string);
    return strtolower(trim($string, '-'));
}

function url_dot($stringg){
    $stringg = str_replace(array('[\', \']'), '', $stringg);
    $stringg = preg_replace('/\[.*\]/U', '', $stringg);
    $stringg = preg_replace('/&(amp;)?#?[a-z0-9]+;/i', '.', $stringg);
    $stringg = preg_replace(array('/[^a-z0-9]/i', '/[-]+/') , '.', $stringg);
    return strtolower(trim($stringg, '.'));
}
?>
<?php
function back_to_top_button(){
?>
    <script>
    $(document).ready(function(){
        $(window).scroll(function(){
            if($(this).scrollTop() > 100){ 
                $('#scroll').fadeIn();
            }else{ 
                $('#scroll').fadeOut(); 
            } 
        }); 
        $('#scroll').click(function(){ 
            $("html, body").animate({ scrollTop: 0 }, 600); 
            return false; 
        }); 
    });
    </script>
    <a href="#" id="scroll" style="display: none;"><span></span></a> 
<?php
}
?>
<?php
function limit_word($string, $word_limit) {
        $words = explode(" ", $string);
        return implode(" ", array_splice($words, 0, $word_limit));
    }
?>
<?php
function replace_links( $url ) 
{    
	$url = preg_replace("(^https?://)", "", $url );
	
	return $url;
}
?>

<?php 
    function getFirstPara($string){
        $string = substr($string,0, strpos($string, "</p>")+4);
        return $string;
    }
    
    // If you wanted to remove the paragraph tags from the HTML
    function getFirstPara2($string){
        $string = substr($string,0, strpos($string, "</p>")+4);
        $string = str_replace("<p>", "", str_replace("<p/>", "", $string));
        return $string;
    }
    
    // Then call the function, passing the string variable    
    //echo getFirstPara($HTMLString);//
?>

<?php
// get paragraph
$string = "<p>Maecenas laoreet est bibendum, dictum mi vel, cursus mi. Curabitur feugiat libero vitae lorem venenatis consequat. Donec luctus nisi cursus miet sapien blandit, quis congue massa tincidunt. Maecenas laoreet est bibendum, dictum mi vel, cursus mi. Curabitur feugiat libero vitae lorem venenatis consequat. Donec luctus nisi cursus miet sapien blandit, quis congue massa tincidunt.</p><p><br />Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit. Nullam id dolor id nibh ultricies vehicula ut id elit.lor sit amet, consectetur adipiscing elit. Duis mollis, est non com.</p>";

preg_match_all("/\<p\>(.*?)\<\/p\>/", $string, $arrParagraphs);
$paragraphs=$arrParagraphs[0];

//echo $paragraphs[0]."<br />\n";
//echo $paragraphs[1]."<br />\n";
?>

<?php
/**
* Truncates text.
*
* Cuts a string to the length of $length and replaces the last characters
* with the ending if the text is longer than length.
*
* ### Options:
*
* - `ending` Will be used as Ending and appended to the trimmed string
* - `exact` If false, $text will not be cut mid-word
* - `html` If true, HTML tags would be handled correctly
*
* @param string  $text String to truncate.
* @param integer $length Length of returned string, including ellipsis.
* @param array $options An array of html attributes and options.
* @return string Trimmed string.
* @access public
* @link http://book.cakephp.org/view/1469/Text#truncate-1625
*/
function truncate($text, $length = 100, $options = array()) {
    $default = array(
        'ending' => '...', 'exact' => true, 'html' => false
    );
    $options = array_merge($default, $options);
    extract($options);

    if ($html) {
        if (mb_strlen(preg_replace('/<.*?>/', '', $text)) <= $length) {
            return $text;
        }
        $totalLength = mb_strlen(strip_tags($ending));
        $openTags = array();
        $truncate = '';

        preg_match_all('/(<\/?([\w+]+)[^>]*>)?([^<>]*)/', $text, $tags, PREG_SET_ORDER);
        foreach ($tags as $tag) {
            if (!preg_match('/img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param/s', $tag[2])) {
                if (preg_match('/<[\w]+[^>]*>/s', $tag[0])) {
                    array_unshift($openTags, $tag[2]);
                } else if (preg_match('/<\/([\w]+)[^>]*>/s', $tag[0], $closeTag)) {
                    $pos = array_search($closeTag[1], $openTags);
                    if ($pos !== false) {
                        array_splice($openTags, $pos, 1);
                    }
                }
            }
            $truncate .= $tag[1];

            $contentLength = mb_strlen(preg_replace('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|&#x[0-9a-f]{1,6};/i', ' ', $tag[3]));
            if ($contentLength + $totalLength > $length) {
                $left = $length - $totalLength;
                $entitiesLength = 0;
                if (preg_match_all('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|&#x[0-9a-f]{1,6};/i', $tag[3], $entities, PREG_OFFSET_CAPTURE)) {
                    foreach ($entities[0] as $entity) {
                        if ($entity[1] + 1 - $entitiesLength <= $left) {
                            $left--;
                            $entitiesLength += mb_strlen($entity[0]);
                        } else {
                            break;
                        }
                    }
                }

                $truncate .= mb_substr($tag[3], 0 , $left + $entitiesLength);
                break;
            } else {
                $truncate .= $tag[3];
                $totalLength += $contentLength;
            }
            if ($totalLength >= $length) {
                break;
            }
        }
    } else {
        if (mb_strlen($text) <= $length) {
            return $text;
        } else {
            $truncate = mb_substr($text, 0, $length - mb_strlen($ending));
        }
    }
    if (!$exact) {
        $spacepos = mb_strrpos($truncate, ' ');
        if (isset($spacepos)) {
            if ($html) {
                $bits = mb_substr($truncate, $spacepos);
                preg_match_all('/<\/([a-z]+)>/', $bits, $droppedTags, PREG_SET_ORDER);
                if (!empty($droppedTags)) {
                    foreach ($droppedTags as $closingTag) {
                        if (!in_array($closingTag[1], $openTags)) {
                            array_unshift($openTags, $closingTag[1]);
                        }
                    }
                }
            }
            $truncate = mb_substr($truncate, 0, $spacepos);
        }
    }
    $truncate .= $ending;

    if ($html) {
        foreach ($openTags as $tag) {
            $truncate .= '</'.$tag.'>';
        }
    }

    return $truncate;
}
?>
<?php
function upload_image()
{
	$currentDirectory = getcwd();
    $uploadDirectory = "../../upload/";

    $errors = []; // Store errors here

    $fileExtensionsAllowed = ['jpeg','jpg','png']; // These will be the only file extensions allowed 
    
	Global $file_name;
	Global $file_tmp_name;
	
    $file_name = rand(0,999999).'-'.$_FILES['upl']['name'];
    $fileSize = $_FILES['upl']['size'];
    $file_tmp_name  = $_FILES['upl']['tmp_name'];
    $fileType = $_FILES['upl']['type'];
    $fileExtension = pathinfo($file_name, PATHINFO_EXTENSION);;

    $uploadPath = $currentDirectory . $uploadDirectory . basename($file_name); 

    if (!empty($file_tmp_name)) {

      if (! in_array($fileExtension,$fileExtensionsAllowed)) {
        $errors[] = "This file extension is not allowed. Please upload a JPEG or PNG file";
      }

      if ($fileSize > 4000000) {
        $errors[] = "File exceeds maximum size (4MB)";
      }

      if (empty($errors)) {
        $didUpload = move_uploaded_file($file_tmp_name, $uploadPath);

        if ($didUpload) {
          echo "The file " . basename($file_name) . " has been uploaded";
        } else {
          echo "An error occurred. Please contact the administrator.";
        }
      } else {
        foreach ($errors as $error) {
          echo $error . "These are the errors" . "\n";
        }
      }

    }
}
?>
<?php
function base_url()
{   
    global $base_url;
	return $base_url;
}
?>

<?php
function actual_link()
{
	global $actual_link;
	return $actual_link;
}
?>
<?php 
function rupiah($number){
	//$result = "" . number_format($number,2,',','.');
	$result = "<span class='rupiah text-nowrap'>Rp " . number_format($number,0,',','.')."</span>";
	return $result;
}
?>

<?php 
function number($number){
	//$result = "" . number_format($number,2,',','.');
	$result = "<span class='number text-nowrap'>" . number_format($number,0,',','.')."</span>";
	return $result;
}
?>