This blob has been accessed 377 times via Git panel.
- <?php
- header('Content-Type: text/html; charset=UTF-8');
- header('Cache-Control: private');
- header('X-Frame-Options: SAMEORIGIN');
- header('X-XSS-Protection: 1; mode=block');
- ini_set("session.use_trans_sid","0");
- ini_set("mbstring.language","Neutral");
- ini_set("mbstring.internal_encoding","UTF-8");
- ini_set("mbstring.encoding_translation","On");
- ini_set("mbstring.http_input","auto");
- ini_set("mbstring.http_output","UTF-8");
- ini_set("mbstring.detect_order","auto");
- ini_set("mbstring.substitute_character","none");
- ini_set("default_charset","UTF-8");
- error_reporting(E_ERROR);
- session_cache_limiter(false);
- session_start();
- if ($_SESSION['logged_lvl'] == "subscriber") {
- $fileUsr = sha1($_SESSION['logged_uid']);
- }
- if (isset($_POST['mbox_min']) and !empty($_POST['mbox_min']) and is_numeric($_POST['mbox_min'])) {
- $mbox_min = $_POST['mbox_min'];
- }
- else {
- $mbox_min = "0";
- }
- if (isset($_POST['mbox_ppg']) and !empty($_POST['mbox_ppg']) and is_numeric($_POST['mbox_ppg'])) {
- $mbox_ppg = $_POST['mbox_ppg'];
- }
- else {
- $mbox_ppg = "14";
- }
- $mbox_max = $mbox_min + $mbox_ppg;
- function rmdirr($recurse_dirname) {
- if (!file_exists($recurse_dirname)) {
- return false;
- }
- if (is_file($recurse_dirname)) {
- return unlink($recurse_dirname);
- }
- $recurse_dir = dir($recurse_dirname);
- while (false !== $recurse_entry = $recurse_dir->read()) {
- if ($recurse_entry == '.' || $recurse_entry == '..') {
- continue;
- }
- rmdirr("$recurse_dirname/$recurse_entry");
- }
- $recurse_dir->close();
- return rmdir($recurse_dirname);
- }
- function ack($ack_string) {
- echo "<table width=\"100%\"><tr></td><div class=\"ack\">$ack_string</div></td></tr></table>";
- }
- function nak($nak_string) {
- echo "<table width=\"100%\"><tr></td><div class=\"nak\">$nak_string</div></td></tr></table>";
- }
- function time_elapsed_string($ptime) {
- if ($etime < 1) {
- return '0 seconds';
- }
- $a = array( 12 * 30 * 24 * 60 * 60 => 'year',
- 30 * 24 * 60 * 60 => 'month',
- 7 * 24 * 60 * 60 => 'week',
- 24 * 60 * 60 => 'day',
- 60 * 60 => 'hour',
- 60 => 'minute',
- 1 => 'second'
- );
- foreach ($a as $secs => $str) {
- $d = $etime / $secs;
- if ($d >= 1) {
- $r = round($d);
- return $r . ' ' . $str . ($r > 1 ? 's' : '');
- }
- }
- }
- function HumanReadableFilesize($size) {
- $mod = 1024;
- $units = explode(' ','B KB MB GB TB PB');
- for ($i = 0; $size > $mod; $i++) {
- $size /= $mod;
- }
- return round($size, 2) . ' ' . $units[$i];
- }
- // cleong@organic.com
- function get_mime_type(&$struct) {
- $primary_mime_type = array("TEXT", "MULTIPART","MESSAGE", "APPLICATION", "AUDIO", "IMAGE", "VIDEO", "OTHER");
- if($struct->subtype) {
- return $primary_mime_type[(int) $struct->type] . '/' .$struct->subtype;
- }
- return "TEXT/PLAIN";
- }
- function get_part($stream, $msg_number, $mime_type, $structure = false, $part_number = false) {
- if (!$structure) {
- $structure = imap_fetchstructure($stream, $msg_number, FT_UID);
- }
- if ($structure) {
- if ($mime_type == get_mime_type($structure)) {
- if (!$part_number) {
- $part_number = "1";
- }
- $text = imap_fetchbody($stream, $msg_number, $part_number, FT_UID);
- if ($structure->encoding == 3) {
- return imap_base64($text);
- }
- else if ($structure->encoding == 4) {
- return imap_qprint($text);
- }
- else {
- return $text;
- }
- }
- if ($structure->type == 1) /* multipart */ {
- while (list($index, $sub_structure) = each($structure->parts)) {
- if ($part_number) {
- $prefix = $part_number . '.';
- }
- $data = get_part($stream, $msg_number, $mime_type, $sub_structure,$prefix . ($index + 1));
- if ($data) {
- return $data;
- }
- }
- }
- }
- return false;
- }
- function extract_emails_from($string){
- preg_match_all("/[\+\=\._a-zA-Z0-9-]+@[\._a-zA-Z0-9-]+/i", $string, $matches);
- return $matches[0];
- }
- function return_bytes($val) {
- $val = trim($val);
- switch ($last) {
- case 'k':
- case 'K':
- return(int)$val * 1024;
- break;
- case 'm':
- case 'M':
- return(int)$val * 1048576;
- break;
- default:
- return $val;
- }
- }
- $server_upload_max_filesize = return_bytes(ini_get('upload_max_filesize'));
- $server_post_max_size = return_bytes(ini_get('post_max_size'));
- $server_memory_limit = return_bytes(ini_get('memory_limit'));
- $max_file_size = $server_upload_max_filesize;
- if ($server_upload_max_filesize > $server_post_max_size) {
- $max_file_size = $server_post_max_size;
- }
- if ($server_post_max_size > $server_memory_limit) {
- $max_file_size = $server_memory_limit;
- }
- function is_image($path) {
- $a = getimagesize($path);
- $image_type = $a[2];
- if (in_array($image_type, array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG , IMAGETYPE_BMP))) {
- return true;
- }
- return false;
- }
- // chris@isavvy.co.nz
- class EmailMessage {
- protected $connection;
- protected $messageNumber;
- public $attachments;
- public $getAttachments = true;
- public function __construct($connection, $messageNumber) {
- $this->connection = $connection;
- $this->messageNumber = $messageNumber;
- }
- public function fetch() {
- $structure = @imap_fetchstructure($this->connection, $this->messageNumber);
- if (!$structure) {
- return false;
- }
- else {
- $this->recurse($structure->parts);
- return true;
- }
- }
- public function recurse($messageParts, $prefix = '', $index = 1, $fullPrefix = true) {
- foreach ($messageParts as $part) {
- $partNumber = $prefix . $index;
- if ($part->type == 2) {
- $msg = new EmailMessage($this->connection, $this->messageNumber);
- $msg->getAttachments = $this->getAttachments;
- $msg->recurse($part->parts, $partNumber.'.', 0, false);
- $this->attachments[] = array(
- 'type' => $part->type,
- 'subtype' => $part->subtype,
- 'filename' => '',
- 'data' => $msg,
- 'inline' => false,
- );
- }
- elseif (isset($part->parts)) {
- if ($fullPrefix) {
- $this->recurse($part->parts, $prefix.$index.'.');
- }
- else {
- $this->recurse($part->parts, $prefix);
- }
- }
- elseif ($part->type > 2) {
- if (isset($part->id)) {
- $id = str_replace(array('<', '>'), '', $part->id);
- $this->attachments[$id] = array(
- 'type' => $part->type,
- 'subtype' => $part->subtype,
- 'filename' => $this->getFilenameFromPart($part),
- 'data' => $this->getAttachments ? $this->getPart($partNumber, $part->encoding) : '',
- 'inline' => true,
- );
- }
- else {
- $this->attachments[] = array(
- 'type' => $part->type,
- 'subtype' => $part->subtype,
- 'filename' => $this->getFilenameFromPart($part),
- 'data' => $this->getAttachments ? $this->getPart($partNumber, $part->encoding) : '',
- 'inline' => false,
- );
- }
- }
- $index++;
- }
- }
- function getPart($partNumber, $encoding) {
- $data = imap_fetchbody($this->connection, $this->messageNumber, $partNumber);
- switch($encoding) {
- case 0: return $data; // 7BIT
- case 1: return $data; // 8BIT
- case 2: return $data; // BINARY
- case 3: return base64_decode($data); // BASE64
- case 4: return quoted_printable_decode($data); // QUOTED_PRINTABLE
- case 5: return $data; // OTHER
- }
- }
- function getFilenameFromPart($part) {
- $filename = '';
- if ($part->ifdparameters) {
- foreach ($part->dparameters as $object) {
- if (strtolower($object->attribute) == 'filename') {
- $filename = $object->value;
- }
- }
- }
- if (!$filename && $part->ifparameters) {
- foreach ($part->parameters as $object) {
- if (strtolower($object->attribute) == 'name') {
- $filename = $object->value;
- }
- }
- }
- return $filename;
- }
- }
- function kartero_sort_folders($host,$array) {
- if (is_array($array)) {
- if (in_array("{$host}INBOX",$array)) {
- $inbox = array_search("{$host}INBOX",$array);
- unset($array[$inbox]);
- unset($inbox);
- }
- if (in_array("{$host}Sent",$array)) {
- $sent = array_search("{$host}Sent",$array);
- unset($array[$sent]);
- }
- else {
- if (isset($sent)) {
- unset($sent);
- }
- }
- if (in_array("{$host}Trash",$array)) {
- $trash = array_search("{$host}Trash",$array);
- unset($array[$trash]);
- }
- else {
- if (isset($trash)) {
- unset($trash);
- }
- }
- if (in_array("{$host}Spam",$array)) {
- $spam = array_search("{$host}Spam",$array);
- unset($array[$spam]);
- }
- else {
- if (isset($spam)) {
- unset($spam);
- }
- }
- if (in_array("{$host}Drafts",$array)) {
- $drafts = array_search("{$host}Drafts",$array);
- unset($array[$drafts]);
- }
- else {
- if (isset($drafts)) {
- unset($drafts);
- }
- }
- if (in_array("{$host}Junk",$array)) {
- $junk = array_search("{$host}Junk",$array);
- unset($array[$junk]);
- }
- else {
- if (isset($junk)) {
- unset($junk);
- }
- }
- sort($array);
- if (isset($junk)) {
- array_unshift($array,"{$host}Junk");
- unset($junk);
- }
- if (isset($spam)) {
- array_unshift($array,"{$host}Spam");
- unset($spam);
- }
- if (isset($trash)) {
- array_unshift($array,"{$host}Trash");
- unset($trash);
- }
- if (isset($sent)) {
- array_unshift($array,"{$host}Sent");
- unset($sent);
- }
- if (isset($drafts)) {
- array_unshift($array,"{$host}Drafts");
- unset($drafts);
- }
- array_unshift($array,"{$host}INBOX");
- reset($array);
- return $array;
- }
- }
- function kartero_dovecot_quota($quota) {
- $prefix = "";
- $quota = "";
- }
- else {
- $quota = strip_tags($quota);
- $quota = strtoupper($quota);
- $quota = preg_replace("/[^0-9.KMGT]/","",$quota);
- if (strpos($quota,"K")) {
- $prefix = "K";
- }
- if (strpos($quota,"M")) {
- $prefix = "M";
- }
- if (strpos($quota,"G")) {
- $prefix = "G";
- }
- if (strpos($quota,"T")) {
- $prefix = "T";
- }
- $quota = preg_replace("/[^0-9.]/","",$quota);
- $quota = round($quota,0);
- }
- $quota = trim($quota);
- $prefix = trim($prefix);
- return $quota . $prefix;
- }
- function kartero_resize_image($image,$max_height=70) {
- $size = getimagesize($image);
- $width = $size[0];
- $height = $size[1];
- if ($height > $max_height) {
- $ratio = $max_height / $height;
- $new_width = $width * $ratio;
- $new_height = $max_height;
- $put_image = imagecreatetruecolor($new_width,$new_height);
- $mimetype = image_type_to_mime_type(exif_imagetype($image));
- switch($mimetype) {
- case "image/jpg":
- case "image/jpeg":
- $get_image = imagecreatefromjpeg($image);
- break;
- case "image/gif":
- $get_image = imagecreatefromgif($image);
- break;
- case "image/png":
- $get_image = imagecreatefrompng($image);
- break;
- }
- imagecopyresampled($put_image,$get_image,0,0,0,0,$new_width,$new_height,$width,$height);
- switch($mimetype) {
- case "image/jpg":
- case "image/jpeg":
- imagejpeg($put_image,$image,80);
- break;
- case "image/gif":
- imagegif($put_image,$image);
- break;
- case "image/png":
- imagepng($put_image,$image,9);
- break;
- }
- imagedestroy($put_image);
- imagedestroy($get_image);
- }
- }
- function kartero_local_storage($element,$storage,$destroy=0) {
- $storage = $element . $storage;
- $storage = md5($storage);
- if ($destroy != 0) {
- echo "\n<script>localStorage.removeItem(\"$storage\");</script>\n";
- }
- else {
- echo "\n<script>var n=document.getElementById(\"$element\");var s=function(){localStorage.setItem(\"$storage\",n.value)};if(window.localStorage){n.value=localStorage.getItem(\"$storage\")}n.onchange=s();setInterval(s,500);window.onunload=s();</script>\n";
- }
- }
- function pass1_process($mbox, $msgno, $path) {
- $emailMessage = new EmailMessage($mbox, imap_msgno($mbox,$msgno));
- $emailMessage->fetch();
- if (count($emailMessage->attachments > 0)) {
- if (!file_exists($path)) {
- mkdir($path,0700,1);
- }
- foreach ($emailMessage->attachments as $key => $attachment) {
- $fileContent = $attachment[data];
- if (($fileContent != "") and (($attachment[inline] == 1) or ($attachment[filename] == ""))) {
- $fileName = $key;
- }
- else {
- $fileName = $attachment[filename];
- }
- $fileName = str_replace(" ", "_", $fileName);
- $fileName = preg_replace("/[^a-zA-Z0-9.\-_@]/u", "", $fileName);
- if (!file_exists("$path/$fileName") and (mb_strlen($fileContent,"8bit") > 0)) {
- file_put_contents("$path/$fileName",$fileContent);
- }
- }
- }
- }
- function pass2_extract($mbox, $msgno, $p, $partno, $path) {
- $data = ($partno) ? imap_fetchbody($mbox, $msgno, $partno, FT_UID) : imap_body($mbox, $msgno, FT_UID);
- if ($p->encoding == 4)
- $data = quoted_printable_decode($data);
- else if ($p->encoding == 3)
- $data = base64_decode($data);
- $params = array();
- if ($p->parameters)
- foreach ($p->parameters as $x)
- $params[strtolower($x->attribute)] = $x->value;
- if ($p->dparameters)
- foreach ($p->dparameters as $x)
- $params[strtolower($x->attribute)] = $x->value;
- if ($params['filename'] || $params['name']) {
- $partid = htmlentities($p->id, ENT_QUOTES, "UTF-8");
- $filename = ($params['filename']) ? $params['filename'] : $params['name'];
- $attachments[$filename] = $data;
- $image[$key] = $filename;
- }
- if (count($attachments) > 0) {
- if (!file_exists($path)) {
- mkdir($path, 0700, 1);
- }
- foreach ($attachments as $key => $val) {
- $files = glob("$path/*", GLOB_NOSORT);
- if (count($files) > 0) {
- $dup = "0";
- foreach ($files as $file) {
- if (file_get_contents($file) == $val) {
- $dup++;
- }
- }
- if (($dup == 0) and (mb_strlen($val,"8bit") > 0)) {
- file_put_contents("$path/$key", $val);
- }
- }
- else {
- if (mb_strlen($val,"8bit") > 0) {
- file_put_contents("$path/$key", $val);
- }
- }
- unset($files);
- }
- }
- }
- function pass2_process($mbox, $msgno, $path) {
- $attachments = array();
- $s = imap_fetchstructure($mbox, $msgno, FT_UID);
- if (!$s->parts) {
- pass2_extract($mbox, $msgno, $s, 0, $path);
- }
- else {
- foreach ($s->parts as $partno0 => $p) {
- pass2_extract($mbox, $msgno, $p, $partno0 + 1, $path);
- }
- }
- }
- function kartero_smime_verify($e) {
- $t = tempnam(sys_get_temp_dir(), 'KSV');
- file_put_contents($t, $e);
- $r = openssl_pkcs7_verify($t, PKCS7_NOVERIFY);
- if ($r === true) {
- echo '<font color="green">TRUE</font>';
- } elseif ($r == -1) {
- echo '<font color="orange">ERROR</font>';
- } else {
- echo '<font color="red">FALSE</font>';
- }
- unlink($t);
- }
- function kartero_smime_sign($user, $name, $from, $body, $home, $days = 365) {
- preg_match('/^--.*\n?/m', $body, $matches);
- if (count($matches) > 0) {
- $body = 'Content-Type: multipart/mixed; boundary="' . trim(substr($matches[0], 2)) . "\"\n" . $body;
- }
- else {
- $body = "Content-Type: text/plain\n\n$body";
- }
- $body = str_replace("\r", '', $body);
- $name = trim($name);
- $user = trim(strtolower($user));
- $from = trim(strtolower($from));
- $home = trim(strtolower($home));
- $u = explode('@', $user);
- $n = $u[0];
- $d = $u[1];
- $h = "$home/$d/{$n[0]}/$n/smime";
- $key_file = "$h/$from.sign.key";
- $crt_file = "$h/$from.sign.crt";
- $msg_file = tempnam(sys_get_temp_dir(), 'MSG');
- file_put_contents($msg_file, $body);
- unlink($key_file);
- }
- unlink($crt_file);
- }
- if (file_exists($key_file) and file_exists($crt_file)) {
- $key = file_get_contents($key_file);
- $crt = file_get_contents($crt_file);
- } else {
- $req_key = openssl_pkey_new(array('digest_alg' => 'sha512', 'private_key_bits' => 4096, 'private_key_type' => OPENSSL_KEYTYPE_RSA));
- $req_csr = openssl_csr_new(array('countryName' => 'PH', 'stateOrProvinceName' => 'NCR', 'localityName' => 'QC', 'organizationName' => 'kartero.org', 'organizationalUnitName' => explode('@', $from)[1], 'commonName' => $name, 'emailAddress' => $from), $req_key);
- $req_crt = openssl_csr_sign($req_csr, NULL, $req_key, $days);
- openssl_pkey_export($req_key, $key);
- openssl_x509_export($req_crt, $crt);
- if (!file_exists($h)) {
- mkdir($h, 0700, 1);
- }
- file_put_contents($key_file, $key);
- file_put_contents($crt_file, $crt);
- }
- $sig_file = tempnam(sys_get_temp_dir(), 'SIG');
- if (openssl_pkcs7_sign($msg_file, $sig_file, $crt, $key, array(), PKCS7_DETACHED)) {
- if (openssl_pkcs7_verify($sig_file, PKCS7_NOVERIFY)) {
- return explode("\n\n", file_get_contents($sig_file), 2);
- }
- }
- unlink($msg_file);
- unlink($sig_file);
- }
- ?>