Example #1
1
function Bytes_Decode($String, $Separator = '')
{
    /****************************************************************************/
    $__args_types = array('string');
    #-----------------------------------------------------------------------------
    $__args__ = Func_Get_Args();
    eval(FUNCTION_INIT);
    /****************************************************************************/
    $Result = '';
    #-----------------------------------------------------------------------------
    for ($i = 0; $i < StrLen($String); $i++) {
        $Result .= SPrintF('%02x', Ord($String[$i])) . $Separator;
    }
    #-----------------------------------------------------------------------------
    return $Result;
}
Example #2
0
 function escapedata($data)
 {
     $position = 0;
     $length = strlen($data);
     $escapeddata = "";
     for (; $position < $length;) {
         $character = substr($data, $position, 1);
         $code = Ord($character);
         switch ($code) {
             case 34:
                 $character = "&quot;";
                 break;
             case 38:
                 $character = "&amp;";
                 break;
             case 39:
                 $character = "&apos;";
                 break;
             case 60:
                 $character = "&lt;";
                 break;
             case 62:
                 $character = "&gt;";
                 break;
             default:
                 if ($code < 32) {
                     $character = "&#" . strval($code) . ";";
                 }
                 break;
         }
         $escapeddata .= $character;
         $position++;
     }
     return $escapeddata;
 }
Example #3
0
function trim_text($str, $len, $tail = "..")
{
    if (strlen($str) < $len) {
        return $str;
        //자를길이보다 문자열이 작으면 그냥 리턴
    } else {
        $result_str = '';
        for ($i = 0; $i < $len; $i++) {
            if (Ord($str[$i]) <= 127 && Ord($str[$i]) >= 0) {
                $result_str .= $str[$i];
            } else {
                if (Ord($str[$i]) <= 223 && Ord($str[$i]) >= 194) {
                    $result_str .= $str[$i] . $str[$i + 1];
                    $i + 1;
                } else {
                    if (Ord($str[$i]) <= 239 && Ord($str[$i]) >= 224) {
                        $result_str .= $str[$i] . $str[$i + 1] . $str[$i + 2];
                        $i + 2;
                    } else {
                        if (Ord($str[$i]) <= 244 && Ord($str[$i]) >= 240) {
                            $result_str .= $str[$i] . $str[$i + 1] . $str[$i + 2] . $str[$i + 3];
                            $i + 3;
                        }
                    }
                }
            }
        }
        return $result_str . $tail;
    }
}
 public function execute(&$value, &$error)
 {
     $cuit = $value;
     $coeficiente = array(5, 4, 3, 2, 7, 6, 5, 4, 3, 2);
     $cuit_rearmado = "";
     //separo cualquier caracter que no tenga que ver con numeros
     for ($i = 0; $i < strlen($cuit); $i = $i + 1) {
         if (Ord(substr($cuit, $i, 1)) >= 48 && Ord(substr($cuit, $i, 1)) <= 57) {
             $cuit_rearmado = $cuit_rearmado . substr($cuit, $i, 1);
         }
     }
     // si no estan todos los digitos
     if (strlen($cuit_rearmado) != 11) {
         $error = $this->getParameterHolder()->get('cuit_error');
         return false;
     } else {
         $sumador = 0;
         $verificador = substr($cuit_rearmado, 10, 1);
         //tomo el digito verificador
         for ($i = 0; $i <= 9; $i = $i + 1) {
             $sumador = $sumador + substr($cuit_rearmado, $i, 1) * $coeficiente[$i];
         }
         //separo cada digito y lo multiplico por el coeficiente
         $resultado = $sumador % 11;
         $resultado = 11 - $resultado;
         //saco el digito verificador
         if (intval($verificador) != $resultado) {
             $error = $this->getParameterHolder()->get('cuit_error');
             return false;
         } else {
             //$cuit_rearmado = substr($cuit_rearmado, 0, 2) . "-" . substr($cuit_rearmado, 2, 8) . "-" . substr($cuit_rearmado, 10, 1);
             return true;
         }
     }
 }
Example #5
0
function VireZeroTerminaux($input)
{
    for ($i = 0; $i < strlen($input); $i++) {
        if (Ord($input[$i]) == 0) {
            $input = substr($input, 0, $i);
            return $input;
            break;
        }
    }
}
Example #6
0
 function adler32($adler, $str)
 {
     $BASE = 65521;
     $s1 = $adler & 0xffff;
     $s2 = $adler >> 16 & 0xffff;
     for ($i = 0; $i < strlen($str); $i++) {
         $s1 = ($s1 + Ord($str[$i])) % $BASE;
         $s2 = ($s2 + $s1) % $BASE;
     }
     return $this->leftshift($s2, 16) + $s1;
 }
Example #7
0
function adler32($adler, $str)
{
    $BASE = 65521;
    $s1 = $adler & 0xffff;
    $s2 = $adler >> 16 & 0xffff;
    for ($i = 0; $i < strlen($str); $i++) {
        $s1 = ($s1 + Ord($str[$i])) % $BASE;
        $s2 = ($s2 + $s1) % $BASE;
        //echo "s1 : $s1 <BR> s2 : $s2 <BR>";
    }
    return leftshift($s2, 16) + $s1;
}
Example #8
0
 /**
  * 截取字符串(1个汉字长度计为1;1个字母长度计为0.5)
  */
 public static function cutString($sourcestr, $cutlength, $addpoint = 1)
 {
     $returnstr = '';
     $i = 0;
     $n = 0;
     $str_length = strlen($sourcestr);
     //字符串的字节数
     while ($n < $cutlength and $i <= $str_length) {
         $temp_str = substr($sourcestr, $i, 1);
         $ascnum = Ord($temp_str);
         //得到字符串中第$i位字符的ascii码
         if ($ascnum >= 224) {
             //如果ASCII位高与224,
             $returnstr = $returnstr . substr($sourcestr, $i, 3);
             //根据UTF-8编码规范,将3个连续的字符计为单个字符
             $i = $i + 3;
             //实际Byte计为3
             $n++;
             //字串长度计1
         } elseif ($ascnum >= 192) {
             //如果ASCII位高与192,
             $returnstr = $returnstr . substr($sourcestr, $i, 2);
             //根据UTF-8编码规范,将2个连续的字符计为单个字符
             $i = $i + 2;
             //实际Byte计为2
             $n++;
             //字串长度计1
         } elseif ($ascnum >= 65 && $ascnum <= 90) {
             //如果是大写字母,
             $returnstr = $returnstr . substr($sourcestr, $i, 1);
             $i = $i + 1;
             //实际的Byte数仍计1个
             $n++;
             //但考虑整体美观,大写字母计成一个高位字符
         } else {
             //其他情况下,包括小写字母和半角标点符号,
             $returnstr = $returnstr . substr($sourcestr, $i, 1);
             $i = $i + 1;
             //实际的Byte数计1个
             $n = $n + 0.5;
             //小写字母和半角标点等与半个高位字符宽...
         }
     }
     if ($str_length > $i && $addpoint) {
         $returnstr = $returnstr . "...";
         //超过长度时在尾处加上省略号
     }
     return $returnstr;
 }
Example #9
0
function cn_substr_utf8($sourcestr, $cutlength, $cutstart = 0, $isadd = false)
{
    $returnstr = '';
    $i = 0;
    $n = 0;
    $str_length = strlen($sourcestr);
    //字符串的字节数
    while ($n < $cutlength and $i <= $str_length) {
        $temp_str = substr($sourcestr, $i, 1);
        $ascnum = Ord($temp_str);
        //得到字符串中第$i位字符的ascii码
        if ($ascnum >= 224) {
            $returnstr = $returnstr . substr($sourcestr, $i, 3);
            //根据UTF-8编码规范,将3个连续的字符计为单个字符
            $i = $i + 3;
            //实际Byte计为3
            $n++;
            //字串长度计1
        } elseif ($ascnum >= 192) {
            $returnstr = $returnstr . substr($sourcestr, $i, 2);
            //根据UTF-8编码规范,将2个连续的字符计为单个字符
            $i = $i + 2;
            //实际Byte计为2
            $n++;
            //字串长度计1
        } elseif ($ascnum >= 65 && $ascnum <= 90) {
            $returnstr = $returnstr . substr($sourcestr, $i, 1);
            $i = $i + 1;
            //实际的Byte数仍计1个
            $n++;
            //但考虑整体美观,大写字母计成一个高位字符
        } else {
            $returnstr = $returnstr . substr($sourcestr, $i, 1);
            $i = $i + 1;
            //实际的Byte数计1个
            //$n = $n + 0.5; //小写字母和半角标点等与半个高位字符宽...
            $n = $n + 1;
        }
    }
    if ($isadd) {
        if ($str_length > $i) {
            $returnstr = $returnstr . "...";
            //超过长度时在尾处加上省略号
        }
    }
    return $returnstr;
}
Example #10
0
 function first()
 {
     $sourcestr = $this->s;
     $returnstr = '';
     $i = 0;
     $n = 0;
     $str_length = strlen($sourcestr);
     //字符串的字节数
     while ($i <= $str_length) {
         $temp_str = substr($sourcestr, $i, 1);
         $ascnum = Ord($temp_str);
         //得到字符串中第$i位字符的ascii码
         if ($ascnum >= 224) {
             //如果ASCII位高与224,
             $returnstr = $returnstr . $this->getHanziInitial(substr($sourcestr, $i, 3));
             //根据UTF-8编码规范,将3个连续的字符计为单个字符
             $i = $i + 3;
             //实际Byte计为3
         } else {
             if ($ascnum >= 192) {
                 //如果ASCII位高与192,
                 $returnstr = $returnstr . $this->getHanziInitial(substr($sourcestr, $i, 2));
                 //根据UTF-8编码规范,将2个连续的字符计为单个字符
                 $i = $i + 2;
                 //实际Byte计为2
             } else {
                 if ($ascnum >= 65 && $ascnum <= 90) {
                     //如果是大写字母,
                     $returnstr = $returnstr . substr($sourcestr, $i, 1);
                     $i = $i + 1;
                     //实际的Byte数仍计1个
                 } else {
                     //其他情况下,包括小写字母和半角标点符号,
                     $returnstr = $returnstr . strtoupper(substr($sourcestr, $i, 1));
                     //小写字母转换为大写
                     $i = $i + 1;
                     //实际的Byte数计1个
                 }
             }
         }
     }
     return strtolower($returnstr);
 }
Example #11
0
 protected function validateAttribute($object, $attribute)
 {
     // Cuit que proviene como parametro
     $cuit = $object->{$attribute};
     $esCuit = false;
     $cuit_rearmado = "";
     //separo cualquier caracter que no tenga que ver con numeros
     for ($i = 0; $i < strlen($cuit); $i++) {
         if (Ord(substr($cuit, $i, 1)) >= 48 && Ord(substr($cuit, $i, 1)) <= 57) {
             $cuit_rearmado = $cuit_rearmado . substr($cuit, $i, 1);
         }
     }
     $cuit = $cuit_rearmado;
     if (strlen($cuit_rearmado) != 11) {
         // si to estan todos los digitos
         $esCuit = false;
     } else {
         $x = $i = $dv = 0;
         // Multiplico los dígitos.
         $vec[0] = substr($cuit, 0, 1) * 5;
         $vec[1] = substr($cuit, 1, 1) * 4;
         $vec[2] = substr($cuit, 2, 1) * 3;
         $vec[3] = substr($cuit, 3, 1) * 2;
         $vec[4] = substr($cuit, 4, 1) * 7;
         $vec[5] = substr($cuit, 5, 1) * 6;
         $vec[6] = substr($cuit, 6, 1) * 5;
         $vec[7] = substr($cuit, 7, 1) * 4;
         $vec[8] = substr($cuit, 8, 1) * 3;
         $vec[9] = substr($cuit, 9, 1) * 2;
         // Suma cada uno de los resultado.
         for ($i = 0; $i <= 9; $i++) {
             $x += $vec[$i];
         }
         $dv = (11 - $x % 11) % 11;
         if ($dv == substr($cuit, 10, 1)) {
             $esCuit = true;
         }
     }
     // Error
     if (!$esCuit) {
         $this->addError($object, $attribute, Yii::t('app', 'CUIT inválida'));
     }
 }
Example #12
0
 /**
  * 切割字符串
  */
 function cut_str($sourcestr, $cutlength)
 {
     //对函数参数进行安全保护
     /*
     $sourcestr参数无需要保护,只要是字符串都可以,
     $cutlength参数要求为整数
     */
     if (!ctype_digit($cutlength) || empty($cutlength)) {
         $cutlength = intval($cutlength);
     }
     //强制让第二个参数为数字
     if (!$cutlength) {
         return $sourcestr;
     }
     //如果长度为0或者空,直接返回整个字符串
     $returnstr = '';
     $i = 0;
     $n = 0;
     $str_length = strlen($sourcestr);
     while ($n < $cutlength and $i <= $str_length) {
         $temp_str = substr($sourcestr, $i, 1);
         $ascnum = Ord($temp_str);
         if ($ascnum >= 224) {
             $returnstr = $returnstr . substr($sourcestr, $i, 3);
             $i = $i + 3;
             $n++;
         } elseif ($ascnum >= 192) {
             $returnstr = $returnstr . substr($sourcestr, $i, 2);
             $i = $i + 2;
             $n++;
         } elseif ($ascnum >= 65 && $ascnum <= 90) {
             $returnstr = $returnstr . substr($sourcestr, $i, 1);
             $i = $i + 1;
             $n++;
         } else {
             $returnstr = $returnstr . substr($sourcestr, $i, 1);
             $i = $i + 1;
             $n = $n + 0.5;
         }
     }
     return $returnstr;
 }
Example #13
0
function titlen($title, $titlelen)
{
    $len = strlen($title);
    if ($len <= $titlelen) {
        $title1 = $title;
    } else {
        $title1 = substr($title, 0, $titlelen);
        $parity = 0;
        for ($i = 0; $i < $titlelen; $i++) {
            $temp_str = substr($title, $i, 1);
            if (Ord($temp_str) > 127) {
                $parity += 1;
            }
        }
        if ($parity % 2 == 1) {
            $title1 = substr($title, 0, $titlelen - 1) . "...";
        } else {
            $title1 = substr($title, 0, $titlelen) . "...";
        }
    }
    return $title1;
}
Example #14
0
 public static function cutStr($sourceStr, $cutLength = 10, $extStr = '...')
 {
     $returnStr = '';
     $i = 0;
     $n = 0;
     $strLength = strlen($sourceStr);
     if ($strLength > $cutLength) {
         while ($n < $cutLength and $i <= $strLength) {
             $tempStr = substr($sourceStr, $i, 1);
             $ascnum = Ord($tempStr);
             if ($ascnum >= 224) {
                 $returnStr = $returnStr . substr($sourceStr, $i, 3);
                 $i = $i + 3;
                 $n++;
             } elseif ($ascnum >= 192) {
                 $returnStr = $returnStr . substr($sourceStr, $i, 2);
                 $i = $i + 2;
                 $n++;
             } elseif ($ascnum >= 65 && $ascnum <= 90) {
                 $returnStr = $returnStr . substr($sourceStr, $i, 1);
                 $i = $i + 1;
                 $n++;
             } else {
                 $returnStr = $returnStr . substr($sourceStr, $i, 1);
                 $i = $i + 1;
                 $n = $n + 0.5;
             }
         }
         if ($strLength > $i) {
             $returnStr = $returnStr . $extStr;
         }
         return $returnStr;
     } else {
         return $sourceStr;
     }
 }
Example #15
0
 private function ReadCentralDir($zip, $zipfile)
 {
     $size = filesize($zipfile);
     $max_size = $size < 277 ? $size : 277;
     @fseek($zip, $size - $max_size);
     $pos = ftell($zip);
     $bytes = 0x0;
     while ($pos < $size) {
         $byte = @fread($zip, 1);
         $bytes = $bytes << 8 | Ord($byte);
         $pos++;
         //                if($bytes == 0x504b0506){ break; }
         if (substr(dechex($bytes), -8, 8) == '504b0506') {
             break;
         }
     }
     $data = unpack('vdisk/vdisk_start/vdisk_entries/ventries/Vsize/Voffset/vcomment_size', fread($zip, 18));
     $centd['comment'] = $data['comment_size'] != 0 ? fread($zip, $data['comment_size']) : '';
     // 注释
     $centd['entries'] = $data['entries'];
     $centd['disk_entries'] = $data['disk_entries'];
     $centd['offset'] = $data['offset'];
     $centd['disk_start'] = $data['disk_start'];
     $centd['size'] = $data['size'];
     $centd['disk'] = $data['disk'];
     return $centd;
 }
Example #16
0
 function Connect($host_name, $host_port, $ssl, $server_type = 'HTTP')
 {
     $domain = $host_name;
     $port = $host_port;
     if (strlen($error = $this->Resolve($domain, $ip, $server_type))) {
         return $error;
     }
     if (strlen($this->socks_host_name)) {
         switch ($this->socks_version) {
             case '4':
                 $version = 4;
                 break;
             case '5':
                 $version = 5;
                 break;
             default:
                 return 'it was not specified a supported SOCKS protocol version';
                 break;
         }
         $host_ip = $ip;
         $port = $this->socks_host_port;
         $host_server_type = $server_type;
         $server_type = 'SOCKS';
         if (strlen($error = $this->Resolve($this->socks_host_name, $ip, $server_type))) {
             return $error;
         }
     }
     if ($this->debug) {
         $this->OutputDebug('Connecting to ' . $server_type . ' server IP ' . $ip . ' port ' . $port . '...');
     }
     if ($ssl) {
         $ip = "ssl://" . $ip;
     }
     if (($this->connection = $this->timeout ? @fsockopen($ip, $port, $errno, $error, $this->timeout) : @fsockopen($ip, $port, $errno)) == 0) {
         switch ($errno) {
             case -3:
                 return $this->SetError("-3 socket could not be created");
             case -4:
                 return $this->SetError("-4 dns lookup on hostname \"" . $host_name . "\" failed");
             case -5:
                 return $this->SetError("-5 connection refused or timed out");
             case -6:
                 return $this->SetError("-6 fdopen() call failed");
             case -7:
                 return $this->SetError("-7 setvbuf() call failed");
             default:
                 return $this->SetPHPError($errno . " could not connect to the host \"" . $host_name . "\"", $php_errormsg);
         }
     } else {
         if ($this->data_timeout && function_exists("socket_set_timeout")) {
             socket_set_timeout($this->connection, $this->data_timeout, 0);
         }
         if (strlen($this->socks_host_name)) {
             if ($this->debug) {
                 $this->OutputDebug('Connected to the SOCKS server ' . $this->socks_host_name);
             }
             $send_error = 'it was not possible to send data to the SOCKS server';
             $receive_error = 'it was not possible to receive data from the SOCKS server';
             switch ($version) {
                 case 4:
                     $command = 1;
                     if (!fputs($this->connection, chr($version) . chr($command) . pack('nN', $host_port, ip2long($host_ip)) . $this->user . Chr(0))) {
                         $error = $this->SetDataAccessError($send_error);
                     } else {
                         $response = fgets($this->connection, 9);
                         if (strlen($response) != 8) {
                             $error = $this->SetDataAccessError($receive_error);
                         } else {
                             $socks_errors = array("Z" => '', "[" => 'request rejected', "\\" => 'request failed because client is not running identd (or not reachable from the server)', "]" => 'request failed because client\'s identd could not confirm the user ID string in the request');
                             $error_code = $response[1];
                             $error = isset($socks_errors[$error_code]) ? $socks_errors[$error_code] : 'unknown';
                             if (strlen($error)) {
                                 $error = 'SOCKS error: ' . $error;
                             }
                         }
                     }
                     break;
                 case 5:
                     if ($this->debug) {
                         $this->OutputDebug('Negotiating the authentication method ...');
                     }
                     $methods = 1;
                     $method = 0;
                     if (!fputs($this->connection, chr($version) . chr($methods) . chr($method))) {
                         $error = $this->SetDataAccessError($send_error);
                     } else {
                         $response = fgets($this->connection, 3);
                         if (strlen($response) != 2) {
                             $error = $this->SetDataAccessError($receive_error);
                         } elseif (Ord($response[1]) != $method) {
                             $error = 'the SOCKS server requires an authentication method that is not yet supported';
                         } else {
                             if ($this->debug) {
                                 $this->OutputDebug('Connecting to ' . $host_server_type . ' server IP ' . $host_ip . ' port ' . $host_port . '...');
                             }
                             $command = 1;
                             $address_type = 1;
                             if (!fputs($this->connection, chr($version) . chr($command) . "" . chr($address_type) . pack('Nn', ip2long($host_ip), $host_port))) {
                                 $error = $this->SetDataAccessError($send_error);
                             } else {
                                 $response = fgets($this->connection, 11);
                                 if (strlen($response) != 10) {
                                     $error = $this->SetDataAccessError($receive_error);
                                 } else {
                                     $socks_errors = array("" => '', "" => 'general SOCKS server failure', "" => 'connection not allowed by ruleset', "" => 'Network unreachable', "" => 'Host unreachable', "" => 'Connection refused', "" => 'TTL expired', "" => 'Command not supported', "" => 'Address type not supported');
                                     $error_code = $response[1];
                                     $error = isset($socks_errors[$error_code]) ? $socks_errors[$error_code] : 'unknown';
                                     if (strlen($error)) {
                                         $error = 'SOCKS error: ' . $error;
                                     }
                                 }
                             }
                         }
                     }
                     break;
                 default:
                     $error = 'support for SOCKS protocol version ' . $this->socks_version . ' is not yet implemented';
                     break;
             }
             if (strlen($error)) {
                 fclose($this->connection);
                 return $error;
             }
         }
         if ($this->debug) {
             $this->OutputDebug("Connected to {$host_name}");
         }
         if (strlen($this->proxy_host_name) && !strcmp(strtolower($this->protocol), 'https')) {
             if (function_exists('stream_socket_enable_crypto') && in_array('ssl', stream_get_transports())) {
                 $this->state = "ConnectedToProxy";
             } else {
                 $this->OutputDebug("It is not possible to start SSL after connecting to the proxy server. If the proxy refuses to forward the SSL request, you may need to upgrade to PHP 5.1 or later with OpenSSL support enabled.");
                 $this->state = "Connected";
             }
         } else {
             $this->state = "Connected";
         }
         return "";
     }
 }
Example #17
0
 function EscapeSpecialCharacters($string)
 {
     if (GetType($string) != "string") {
         $string = strval($string);
     }
     for ($escaped = "", $character = 0; $character < strlen($string); $character++) {
         switch ($string[$character]) {
             case "\"":
             case ">":
             case "<":
                 $code = Ord($string[$character]);
                 break;
             default:
                 $code = Ord($string[$character]);
                 if ($code < 32 || $code > 127) {
                     break;
                 }
                 $escaped .= $string[$character];
                 continue 2;
         }
         $escaped .= "&#{$code};";
     }
     return $escaped;
 }
Example #18
0
 function rc_dir($zip, $zip_name)
 {
     $size = filesize($zip_name);
     $maximum_size = $size < 277 ? $size : 277;
     @fseek($zip, $size - $maximum_size);
     $pos = ftell($zip);
     $bytes = 0x0;
     while ($pos < $size) {
         $byte = @fread($zip, 1);
         $bytes = $bytes << 8 | Ord($byte);
         $pos++;
         if ($bytes == 0x504b0506) {
             break;
         }
     }
     $data = unpack('vdisk/vdisk_start/vdisk_entries/ventries/Vsize/Voffset/vcomment_size', fread($zip, 18));
     $centd['comment'] = $data['comment_size'] != 0 ? fread($zip, $data['comment_size']) : '';
     $centd['entries'] = $data['entries'];
     $centd['disk_entries'] = $data['disk_entries'];
     $centd['offset'] = $data['offset'];
     $centd['disk_start'] = $data['disk_start'];
     $centd['size'] = $data['size'];
     $centd['disk'] = $data['disk'];
     return $centd;
 }
Example #19
0
function CollapsePhoneNumber($sPhoneNumber, $sPhoneCountry)
{
    switch ($sPhoneCountry) {
        case "United States":
            $sCollapsedPhoneNumber = "";
            $bHasExtension = false;
            // Loop through the input string
            for ($iCount = 0; $iCount <= strlen($sPhoneNumber); $iCount++) {
                // Take one character...
                $sThisCharacter = substr($sPhoneNumber, $iCount, 1);
                // Is it a number?
                if (Ord($sThisCharacter) >= 48 && Ord($sThisCharacter) <= 57) {
                    // Yes, add it to the returned value.
                    $sCollapsedPhoneNumber .= $sThisCharacter;
                } else {
                    if (!$bHasExtension && ($sThisCharacter == "e" || $sThisCharacter == "E")) {
                        // Yes, add the extension identifier 'e' to the stored string.
                        $sCollapsedPhoneNumber .= "e";
                        // From now on, ignore other non-digits and process normally
                        $bHasExtension = true;
                    }
                }
            }
            break;
        default:
            $sCollapsedPhoneNumber = $sPhoneNumber;
            break;
    }
    return $sCollapsedPhoneNumber;
}
 function ReadCentralDir($zip, $zip_name)
 {
     $size = filesize($zip_name);
     if ($size < 277) {
         $maximum_size = $size;
     } else {
         $maximum_size = 277;
     }
     @fseek($zip, $size - $maximum_size);
     $pos = ftell($zip);
     $bytes = 0x0;
     while ($pos < $size) {
         $byte = @fread($zip, 1);
         $bytes = $bytes << 8 & 4294967295.0 | Ord($byte);
         if ($bytes == 0x504b0506) {
             $pos++;
             break;
         }
         $pos++;
     }
     $data = unpack('vdisk/vdisk_start/vdisk_entries/ventries/Vsize/Voffset/vcomment_size', fread($zip, 18));
     if ($data['comment_size'] != 0) {
         $centd['comment'] = fread($zip, $data['comment_size']);
     } else {
         $centd['comment'] = '';
     }
     $centd['entries'] = $data['entries'];
     $centd['disk_entries'] = $data['disk_entries'];
     $centd['offset'] = $data['offset'];
     $centd['disk_start'] = $data['disk_start'];
     $centd['size'] = $data['size'];
     $centd['disk'] = $data['disk'];
     return $centd;
 }
Example #21
0
 function privReadEndCentralDir(&$p_central_dir)
 {
     $v_result = 1;
     $v_size = filesize($this->zipname);
     @fseek($this->zip_fd, $v_size);
     if (@ftell($this->zip_fd) != $v_size) {
         PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Unable to go to the end of the archive \'' . $this->zipname . '\'');
         return PclZip::errorCode();
     }
     $v_found = 0;
     if ($v_size > 26) {
         @fseek($this->zip_fd, $v_size - 22);
         if (($v_pos = @ftell($this->zip_fd)) != $v_size - 22) {
             PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Unable to seek back to the middle of the archive \'' . $this->zipname . '\'');
             return PclZip::errorCode();
         }
         $v_binary_data = @fread($this->zip_fd, 4);
         $v_data = @unpack('Vid', $v_binary_data);
         if ($v_data['id'] == 0x6054b50) {
             $v_found = 1;
         }
         $v_pos = ftell($this->zip_fd);
     }
     if (!$v_found) {
         $v_maximum_size = 65557;
         if ($v_maximum_size > $v_size) {
             $v_maximum_size = $v_size;
         }
         @fseek($this->zip_fd, $v_size - $v_maximum_size);
         if (@ftell($this->zip_fd) != $v_size - $v_maximum_size) {
             PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Unable to seek back to the middle of the archive \'' . $this->zipname . '\'');
             return PclZip::errorCode();
         }
         $v_pos = ftell($this->zip_fd);
         $v_bytes = 0x0;
         while ($v_pos < $v_size) {
             $v_byte = @fread($this->zip_fd, 1);
             $v_bytes = $v_bytes << 8 | Ord($v_byte);
             if ($v_bytes == 0x504b0506) {
                 $v_pos++;
                 break;
             }
             $v_pos++;
         }
         if ($v_pos == $v_size) {
             PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Unable to find End of Central Dir Record signature");
             return PclZip::errorCode();
         }
     }
     $v_binary_data = fread($this->zip_fd, 18);
     if (strlen($v_binary_data) != 18) {
         PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Invalid End of Central Dir Record size : " . strlen($v_binary_data));
         return PclZip::errorCode();
     }
     $v_data = unpack('vdisk/vdisk_start/vdisk_entries/ventries/Vsize/Voffset/vcomment_size', $v_binary_data);
     if ($v_pos + $v_data['comment_size'] + 18 != $v_size) {
         if (0) {
             PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'The central dir is not at the end of the archive.' . ' Some trailing bytes exists after the archive.');
             return PclZip::errorCode();
         }
     }
     if ($v_data['comment_size'] != 0) {
         $p_central_dir['comment'] = fread($this->zip_fd, $v_data['comment_size']);
     } else {
         $p_central_dir['comment'] = '';
     }
     $p_central_dir['entries'] = $v_data['entries'];
     $p_central_dir['disk_entries'] = $v_data['disk_entries'];
     $p_central_dir['offset'] = $v_data['offset'];
     $p_central_dir['size'] = $v_data['size'];
     $p_central_dir['disk'] = $v_data['disk'];
     $p_central_dir['disk_start'] = $v_data['disk_start'];
     return $v_result;
 }
Example #22
0
/**
 * 支持utf8中文字符截取
 * @author    肖飞
 * @param    string $text        待处理字符串
 * @param    int $start            从第几位截断
 * @param    int $sublen            截断几个字符
 * @param    string $code        字符串编码
 * @param    string $ellipsis    附加省略字符
 * @return    string
 */
function csubstr($string, $start = 0, $sublen = 12, $ellipsis = '', $code = 'UTF-8')
{
    if ($code == 'UTF-8') {
        $tmpstr = '';
        $i = $start;
        $n = 0;
        $str_length = strlen($string);
        //字符串的字节数
        while ($n + 0.5 < $sublen and $i < $str_length) {
            $temp_str = substr($string, $i, 1);
            $ascnum = Ord($temp_str);
            //得到字符串中第$i位字符的ascii码
            if ($ascnum >= 224) {
                //如果ASCII位高与224,
                $tmpstr .= substr($string, $i, 3);
                //根据UTF-8编码规范,将3个连续的字符计为单个字符
                $i = $i + 3;
                //实际Byte计为3
                $n++;
                //字串长度计1
            } elseif ($ascnum >= 192) {
                //如果ASCII位高与192,
                $tmpstr .= substr($string, $i, 3);
                //根据UTF-8编码规范,将2个连续的字符计为单个字符
                $i = $i + 3;
                //实际Byte计为2
                $n++;
                //字串长度计1
            } else {
                //其他情况下,包括小写字母和半角标点符号,
                $tmpstr .= substr($string, $i, 1);
                $i = $i + 1;
                //实际的Byte数计1个
                $n = $n + 0.5;
                //小写字母和半角标点等与半个高位字符宽...
            }
        }
        if (strlen($tmpstr) < $str_length) {
            $tmpstr .= $ellipsis;
            //超过长度时在尾处加上省略号
        }
        return $tmpstr;
    } else {
        $strlen = strlen($string);
        if ($sublen != 0) {
            $sublen = $sublen * 2;
        } else {
            $sublen = $strlen;
        }
        $tmpstr = '';
        for ($i = 0; $i < $strlen; $i++) {
            if ($i >= $start && $i < $start + $sublen) {
                if (ord(substr($string, $i, 1)) > 129) {
                    $tmpstr .= substr($string, $i, 2);
                } else {
                    $tmpstr .= substr($string, $i, 1);
                }
            }
            if (ord(substr($string, $i, 1)) > 129) {
                $i++;
            }
        }
        if (strlen($tmpstr) < $strlen) {
            $tmpstr .= $ellipsis;
        }
        return $tmpstr;
    }
}
Example #23
0
 function privReadEndCentralDir(&$p_central_dir)
 {
     $v_result = 1;
     // ----- Go to the end of the zip file
     $v_size = filesize($this->zipname);
     @fseek($this->zip_fd, $v_size);
     if (@ftell($this->zip_fd) != $v_size) {
         // ----- Error log
         PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Unable to go to the end of the archive \'' . $this->zipname . '\'');
         // ----- Return
         return PclZip::errorCode();
     }
     // ----- First try : look if this is an archive with no commentaries (most of the time)
     // in this case the end of central dir is at 22 bytes of the file end
     $v_found = 0;
     if ($v_size > 26) {
         @fseek($this->zip_fd, $v_size - 22);
         if (($v_pos = @ftell($this->zip_fd)) != $v_size - 22) {
             // ----- Error log
             PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Unable to seek back to the middle of the archive \'' . $this->zipname . '\'');
             // ----- Return
             return PclZip::errorCode();
         }
         // ----- Read for bytes
         $v_binary_data = @fread($this->zip_fd, 4);
         $v_data = @unpack('Vid', $v_binary_data);
         // ----- Check signature
         if ($v_data['id'] == 0x6054b50) {
             $v_found = 1;
         }
         $v_pos = ftell($this->zip_fd);
     }
     // ----- Go back to the maximum possible size of the Central Dir End Record
     if (!$v_found) {
         $v_maximum_size = 65557;
         // 0xFFFF + 22;
         if ($v_maximum_size > $v_size) {
             $v_maximum_size = $v_size;
         }
         @fseek($this->zip_fd, $v_size - $v_maximum_size);
         if (@ftell($this->zip_fd) != $v_size - $v_maximum_size) {
             // ----- Error log
             PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Unable to seek back to the middle of the archive \'' . $this->zipname . '\'');
             // ----- Return
             return PclZip::errorCode();
         }
         // ----- Read byte per byte in order to find the signature
         $v_pos = ftell($this->zip_fd);
         $v_bytes = 0x0;
         while ($v_pos < $v_size) {
             // ----- Read a byte
             $v_byte = @fread($this->zip_fd, 1);
             // -----  Add the byte
             //$v_bytes = ($v_bytes << 8) | Ord($v_byte);
             // Note we mask the old value down such that once shifted we can never end up with more than a 32bit number
             // Otherwise on systems where we have 64bit integers the check below for the magic number will fail.
             $v_bytes = ($v_bytes & 0xffffff) << 8 | Ord($v_byte);
             // ----- Compare the bytes
             if ($v_bytes == 0x504b0506) {
                 $v_pos++;
                 break;
             }
             $v_pos++;
         }
         // ----- Look if not found end of central dir
         if ($v_pos == $v_size) {
             // ----- Error log
             PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Unable to find End of Central Dir Record signature");
             // ----- Return
             return PclZip::errorCode();
         }
     }
     // ----- Read the first 18 bytes of the header
     $v_binary_data = fread($this->zip_fd, 18);
     // ----- Look for invalid block size
     if (strlen($v_binary_data) != 18) {
         // ----- Error log
         PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Invalid End of Central Dir Record size : " . strlen($v_binary_data));
         // ----- Return
         return PclZip::errorCode();
     }
     // ----- Extract the values
     $v_data = unpack('vdisk/vdisk_start/vdisk_entries/ventries/Vsize/Voffset/vcomment_size', $v_binary_data);
     // ----- Check the global size
     if ($v_pos + $v_data['comment_size'] + 18 != $v_size) {
         // ----- Removed in release 2.2 see readme file
         // The check of the file size is a little too strict.
         // Some bugs where found when a zip is encrypted/decrypted with 'crypt'.
         // While decrypted, zip has training 0 bytes
         if (0) {
             // ----- Error log
             PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'The central dir is not at the end of the archive.' . ' Some trailing bytes exists after the archive.');
             // ----- Return
             return PclZip::errorCode();
         }
     }
     // ----- Get comment
     if ($v_data['comment_size'] != 0) {
         $p_central_dir['comment'] = fread($this->zip_fd, $v_data['comment_size']);
     } else {
         $p_central_dir['comment'] = '';
     }
     $p_central_dir['entries'] = $v_data['entries'];
     $p_central_dir['disk_entries'] = $v_data['disk_entries'];
     $p_central_dir['offset'] = $v_data['offset'];
     $p_central_dir['size'] = $v_data['size'];
     $p_central_dir['disk'] = $v_data['disk'];
     $p_central_dir['disk_start'] = $v_data['disk_start'];
     // TBC
     //for(reset($p_central_dir); $key = key($p_central_dir); next($p_central_dir)) {
     //}
     // ----- Return
     return $v_result;
 }
Example #24
0
 function ConnectToHost($domain, $port, $resolve_message)
 {
     if ($this->ssl) {
         $version = explode(".", function_exists("phpversion") ? phpversion() : "3.0.7");
         $php_version = intval($version[0]) * 1000000 + intval($version[1]) * 1000 + intval($version[2]);
         if ($php_version < 4003000) {
             return "establishing SSL connections requires at least PHP version 4.3.0";
         }
         if (!function_exists("extension_loaded") || !extension_loaded("openssl")) {
             return "establishing SSL connections requires the OpenSSL extension enabled";
         }
     }
     if (strlen($this->Resolve($domain, $ip, 'SMTP'))) {
         return $this->error;
     }
     if (strlen($this->socks_host_name)) {
         switch ($this->socks_version) {
             case '4':
                 $version = 4;
                 break;
             case '5':
                 $version = 5;
                 break;
             default:
                 return 'it was not specified a supported SOCKS protocol version';
                 break;
         }
         $host_ip = $ip;
         $host_port = $port;
         if (strlen($this->error = $this->Resolve($this->socks_host_name, $ip, 'SOCKS'))) {
             return $this->error;
         }
         if ($this->ssl) {
             $ip = "ssl://" . $ip;
         }
         if ($this->connection = $this->timeout ? @fsockopen($ip, $this->socks_host_port, $errno, $error, $this->timeout) : @fsockopen($ip, $this->socks_host_port, $errno)) {
             $timeout = $this->data_timeout ? $this->data_timeout : $this->timeout;
             if ($timeout && function_exists("socket_set_timeout")) {
                 socket_set_timeout($this->connection, $timeout, 0);
             }
             if (strlen($this->socks_host_name)) {
                 if ($this->debug) {
                     $this->OutputDebug('Connected to the SOCKS server ' . $this->socks_host_name);
                 }
                 $send_error = 'it was not possible to send data to the SOCKS server';
                 $receive_error = 'it was not possible to receive data from the SOCKS server';
                 switch ($version) {
                     case 4:
                         $command = 1;
                         $user = '';
                         if (!fputs($this->connection, chr($version) . chr($command) . pack('nN', $host_port, ip2long($host_ip)) . $user . Chr(0))) {
                             $error = $this->SetDataAccessError($send_error);
                         } else {
                             $response = fgets($this->connection, 9);
                             if (strlen($response) != 8) {
                                 $error = $this->SetDataAccessError($receive_error);
                             } else {
                                 $socks_errors = array("Z" => '', "[" => 'request rejected', "\\" => 'request failed because client is not running identd (or not reachable from the server)', "]" => 'request failed because client\'s identd could not confirm the user ID string in the request');
                                 $error_code = $response[1];
                                 $error = isset($socks_errors[$error_code]) ? $socks_errors[$error_code] : 'unknown';
                                 if (strlen($error)) {
                                     $error = 'SOCKS error: ' . $error;
                                 }
                             }
                         }
                         break;
                     case 5:
                         if ($this->debug) {
                             $this->OutputDebug('Negotiating the authentication method ...');
                         }
                         $methods = 1;
                         $method = 0;
                         if (!fputs($this->connection, chr($version) . chr($methods) . chr($method))) {
                             $error = $this->SetDataAccessError($send_error);
                         } else {
                             $response = fgets($this->connection, 3);
                             if (strlen($response) != 2) {
                                 $error = $this->SetDataAccessError($receive_error);
                             } elseif (Ord($response[1]) != $method) {
                                 $error = 'the SOCKS server requires an authentication method that is not yet supported';
                             } else {
                                 if ($this->debug) {
                                     $this->OutputDebug('Connecting to SMTP server IP ' . $host_ip . ' port ' . $host_port . '...');
                                 }
                                 $command = 1;
                                 $address_type = 1;
                                 if (!fputs($this->connection, chr($version) . chr($command) . "" . chr($address_type) . pack('Nn', ip2long($host_ip), $host_port))) {
                                     $error = $this->SetDataAccessError($send_error);
                                 } else {
                                     $response = fgets($this->connection, 11);
                                     if (strlen($response) != 10) {
                                         $error = $this->SetDataAccessError($receive_error);
                                     } else {
                                         $socks_errors = array("" => '', "" => 'general SOCKS server failure', "" => 'connection not allowed by ruleset', "" => 'Network unreachable', "" => 'Host unreachable', "" => 'Connection refused', "" => 'TTL expired', "" => 'Command not supported', "" => 'Address type not supported');
                                         $error_code = $response[1];
                                         $error = isset($socks_errors[$error_code]) ? $socks_errors[$error_code] : 'unknown';
                                         if (strlen($error)) {
                                             $error = 'SOCKS error: ' . $error;
                                         }
                                     }
                                 }
                             }
                         }
                         break;
                     default:
                         $error = 'support for SOCKS protocol version ' . $this->socks_version . ' is not yet implemented';
                         break;
                 }
                 if (strlen($this->error = $error)) {
                     fclose($this->connection);
                     return $error;
                 }
             }
             return '';
         }
     } elseif (strlen($this->http_proxy_host_name)) {
         if (strlen($error = $this->Resolve($this->http_proxy_host_name, $ip, 'SMTP'))) {
             return $error;
         }
         if ($this->debug) {
             $this->OutputDebug("Connecting to proxy host address \"" . $ip . "\" port " . $this->http_proxy_host_port . "...");
         }
         if ($this->connection = $this->timeout ? @fsockopen(($this->ssl ? "ssl://" : "") . $ip, $this->http_proxy_host_port, $errno, $error, $this->timeout) : @fsockopen(($this->ssl ? "ssl://" : "") . $ip, $this->http_proxy_host_port)) {
             if ($this->debug) {
                 $this->OutputDebug('Connected to HTTP proxy host "' . $this->http_proxy_host_name . '".');
             }
             $timeout = $this->data_timeout ? $this->data_timeout : $this->timeout;
             if ($timeout && function_exists("socket_set_timeout")) {
                 socket_set_timeout($this->connection, $timeout, 0);
             }
             if ($this->PutLine('CONNECT ' . $domain . ':' . $port . ' HTTP/1.0') && $this->PutLine('User-Agent: ' . $this->user_agent) && $this->PutLine('')) {
                 if (GetType($response = $this->GetLine()) == 'string') {
                     if (!preg_match('/^http\\/[0-9]+\\.[0-9]+[ \\t]+([0-9]+)[ \\t]*(.*)$/i', $response, $matches)) {
                         return $this->SetError("3 it was received an unexpected HTTP response status");
                     }
                     $error = $matches[1];
                     switch ($error) {
                         case '200':
                             for (;;) {
                                 if (GetType($response = $this->GetLine()) != 'string') {
                                     break;
                                 }
                                 if (strlen($response) == 0) {
                                     return '';
                                 }
                             }
                             break;
                         default:
                             $this->error = 'the HTTP proxy returned error ' . $error . ' ' . $matches[2];
                             break;
                     }
                 }
             }
             if ($this->debug) {
                 $this->OutputDebug("Disconnected.");
             }
             fclose($this->connection);
             $this->connection = 0;
             return $this->error;
         }
     } else {
         if ($this->debug) {
             $this->OutputDebug("Connecting to host address \"" . $ip . "\" port " . $port . "...");
         }
         if ($this->connection = $this->timeout ? @fsockopen(($this->ssl ? "ssl://" : "") . $ip, $port, $errno, $error, $this->timeout) : @fsockopen(($this->ssl ? "ssl://" : "") . $ip, $port)) {
             return "";
         }
     }
     $error = $this->timeout ? strval($error) : "??";
     switch ($error) {
         case "-3":
             return "-3 socket could not be created";
         case "-4":
             return "-4 dns lookup on hostname \"" . $domain . "\" failed";
         case "-5":
             return "-5 connection refused or timed out";
         case "-6":
             return "-6 fdopen() call failed";
         case "-7":
             return "-7 setvbuf() call failed";
     }
     return "could not connect to the host \"" . $domain . "\": " . $error;
 }
Example #25
0
 $text = tokenise('\\r' . $text . "\r", false, true, true);
 $text = substr($text, 3, -1);
 for ($i = 0; $i < strlen($text); $i++) {
     $content[] = ord($text[$i]);
 }
 for ($i = 0; $i < 70; $i++) {
     $entete[$i] = 0;
 }
 $entete[0] = Ord('*');
 $entete[1] = Ord('*');
 $entete[2] = Ord('T');
 $entete[3] = Ord('I');
 $entete[4] = Ord('8');
 $entete[5] = Ord('2');
 $entete[6] = Ord('*');
 $entete[7] = Ord('*');
 $entete[8] = 26;
 $entete[9] = 10;
 $entete[10] = 0;
 $onti_comment = substr($onti_comment . str_repeat(chr(0), 40), 0, 40);
 for ($i = 11; $i < 11 + 40; $i++) {
     $entete[$i] = ord($onti_comment[$i - 11]);
 }
 $entete[53] = count($content) + 0x11 & 0xff;
 $entete[54] = count($content) + 0x11 >> 8 & 0xff;
 $entete[55] = 0xb;
 $entete[59] = 0x3;
 $entete[60] = 0x5e;
 $names = array('Y1', 'Y2', 'Y3', 'Y4', 'Y5', 'Y6', 'Y7', 'Y8', 'Y9', 'Y0', 'X1T', 'Y1T', 'X2T', 'Y2T', 'X3T', 'Y3T', 'X4T', 'Y4T', 'X5T', 'Y5T', 'X6T', 'XYT', 'r1', 'r2', 'r3', 'r4', 'r5', 'r6', 'Un', 'Vn');
 $value = array(0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x80, 0x81);
 for ($i = 0; $i < count($names); $i++) {
 function qpencode($text, $header_charset = "", $break_lines = 1)
 {
     $ln = strlen($text);
     if ($ln < 1) {
         return '';
     }
     $h = strlen($header_charset) > 0;
     $lb = XOX_LOCAL_MODE ? "\r\n" : "\n";
     if ($h) {
         $break_lines = 0;
         for ($i = 0; $i < $ln; $i++) {
             switch ($text[$i]) {
                 case "=":
                 case "?":
                 case "_":
                 case "(":
                 case ")":
                     break 2;
                 default:
                     $o = Ord($text[$i]);
                     if ($o < 32 || $o > 127) {
                         break 2;
                     }
             }
         }
         if ($i > 0) {
             return substr($text, 0, $i) . qpencode(substr($text, $i), $header_charset, 0);
         }
     }
     for ($w = $e = "", $l = 0, $i = 0; $i < $ln; $i++) {
         $c = $text[$i];
         $o = Ord($c);
         $en = 0;
         switch ($o) {
             case 9:
             case 32:
                 if (!$h) {
                     $w = $c;
                     $c = "";
                 } else {
                     if ($o == 32) {
                         $c = "_";
                     } else {
                         $en = 1;
                     }
                 }
                 break;
             case 10:
             case 13:
                 if (strlen($w)) {
                     if ($break_lines && $l + 3 > 75) {
                         $e .= "=" . $lb;
                         $l = 0;
                     }
                     $e .= sprintf("=%02X", Ord($w));
                     $l += 3;
                     $w = "";
                 }
                 $e .= $c;
                 $l = 0;
                 continue 2;
             default:
                 if ($o > 127 || $o < 32 || !strcmp($c, "=") || $h && (!strcmp($c, "?") || !strcmp($c, "_") || !strcmp($c, "(") || !strcmp($c, ")"))) {
                     $en = 1;
                 }
                 break;
         }
         if (strlen($w)) {
             if ($break_lines && $l + 1 > 75) {
                 $e .= "=" . $lb;
                 $l = 0;
             }
             $e .= $w;
             $l++;
             $w = "";
         }
         if (strlen($c)) {
             if ($en) {
                 $c = sprintf("=%02X", $o);
                 $el = 3;
             } else {
                 $el = 1;
             }
             if ($break_lines && $l + $el > 75) {
                 $e .= "=" . $lb;
                 $l = 0;
             }
             $e .= $c;
             $l += $el;
         }
     }
     if (strlen($w)) {
         if ($break_lines && $l + 3 > 75) {
             $e .= "=" . $lb;
         }
         $e .= sprintf("=%02X", Ord($w));
     }
     return $h && strcmp($text, $e) ? "=?{$header_charset}?q?{$e}?=" : $e;
 }
Example #27
0
 /**
  * Archive_Zip::_readEndCentralDir()
  *
  * { Description }
  *
  */
 function _readEndCentralDir(&$p_central_dir)
 {
     $v_result = 1;
     // ----- Go to the end of the zip file
     $v_size = filesize($this->_zipname);
     @fseek($this->_zip_fd, $v_size);
     if (@ftell($this->_zip_fd) != $v_size) {
         $this->_errorLog(ARCHIVE_ZIP_ERR_BAD_FORMAT, 'Unable to go to the end of the archive \'' . $this->_zipname . '\'');
         return Archive_Zip::errorCode();
     }
     // ----- First try : look if this is an archive with no commentaries
     // (most of the time)
     // in this case the end of central dir is at 22 bytes of the file end
     $v_found = 0;
     if ($v_size > 26) {
         @fseek($this->_zip_fd, $v_size - 22);
         if (($v_pos = @ftell($this->_zip_fd)) != $v_size - 22) {
             $this->_errorLog(ARCHIVE_ZIP_ERR_BAD_FORMAT, 'Unable to seek back to the middle of the archive \'' . $this->_zipname . '\'');
             return Archive_Zip::errorCode();
         }
         // ----- Read for bytes
         $v_binary_data = @fread($this->_zip_fd, 4);
         $v_data = unpack('Vid', $v_binary_data);
         // ----- Check signature
         if ($v_data['id'] == 0x6054b50) {
             $v_found = 1;
         }
         $v_pos = ftell($this->_zip_fd);
     }
     // ----- Go back to the maximum possible size of the Central Dir End Record
     if (!$v_found) {
         $v_maximum_size = 65557;
         // 0xFFFF + 22;
         if ($v_maximum_size > $v_size) {
             $v_maximum_size = $v_size;
         }
         @fseek($this->_zip_fd, $v_size - $v_maximum_size);
         if (@ftell($this->_zip_fd) != $v_size - $v_maximum_size) {
             $this->_errorLog(ARCHIVE_ZIP_ERR_BAD_FORMAT, 'Unable to seek back to the middle of the archive \'' . $this->_zipname . '\'');
             return Archive_Zip::errorCode();
         }
         // ----- Read byte per byte in order to find the signature
         $v_pos = ftell($this->_zip_fd);
         $v_bytes = 0x0;
         while ($v_pos < $v_size) {
             // ----- Read a byte
             $v_byte = @fread($this->_zip_fd, 1);
             // -----  Add the byte
             $v_bytes = $v_bytes << 8 | Ord($v_byte);
             // ----- Compare the bytes
             if ($v_bytes == 0x504b0506) {
                 $v_pos++;
                 break;
             }
             $v_pos++;
         }
         // ----- Look if not found end of central dir
         if ($v_pos == $v_size) {
             $this->_errorLog(ARCHIVE_ZIP_ERR_BAD_FORMAT, "Unable to find End of Central Dir Record signature");
             return Archive_Zip::errorCode();
         }
     }
     // ----- Read the first 18 bytes of the header
     $v_binary_data = fread($this->_zip_fd, 18);
     // ----- Look for invalid block size
     if (strlen($v_binary_data) != 18) {
         $this->_errorLog(ARCHIVE_ZIP_ERR_BAD_FORMAT, "Invalid End of Central Dir Record size : " . strlen($v_binary_data));
         return Archive_Zip::errorCode();
     }
     // ----- Extract the values
     $v_data = unpack('vdisk/vdisk_start/vdisk_entries/ventries/Vsize/Voffset/vcomment_size', $v_binary_data);
     // ----- Check the global size
     if ($v_pos + $v_data['comment_size'] + 18 != $v_size) {
         $this->_errorLog(ARCHIVE_ZIP_ERR_BAD_FORMAT, "Fail to find the right signature");
         return Archive_Zip::errorCode();
     }
     // ----- Get comment
     if ($v_data['comment_size'] != 0) {
         $p_central_dir['comment'] = fread($this->_zip_fd, $v_data['comment_size']);
     } else {
         $p_central_dir['comment'] = '';
     }
     $p_central_dir['entries'] = $v_data['entries'];
     $p_central_dir['disk_entries'] = $v_data['disk_entries'];
     $p_central_dir['offset'] = $v_data['offset'];
     $p_central_dir['size'] = $v_data['size'];
     $p_central_dir['disk'] = $v_data['disk'];
     $p_central_dir['disk_start'] = $v_data['disk_start'];
     // ----- Return
     return $v_result;
 }
Example #28
0
 private function read_packet_length($socket)
 {
     $a = 0;
     $b = 0;
     while (true) {
         $c = socket_read($socket, 1);
         if (!$c) {
             return 0;
         }
         $c = Ord($c);
         $a |= ($c & 0x7f) << $b++ * 7;
         if ($b > 5) {
             return false;
         }
         if (($c & 0x80) != 128) {
             break;
         }
     }
     return $a;
 }
Example #29
0
 function privReadEndCentralDir(&$p_central_dir)
 {
     //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privReadEndCentralDir", "");
     $v_result = 1;
     // ----- Go to the end of the zip file
     $v_size = filesize($this->zipname);
     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Size of the file :$v_size");
     @fseek($this->zip_fd, $v_size);
     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Position at end of zip file : \''.ftell($this->zip_fd).'\'');
     if (@ftell($this->zip_fd) != $v_size) {
         // ----- Error log
         PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Unable to go to the end of the archive \'' . $this->zipname . '\'');
         // ----- Return
         //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
         return PclZip::errorCode();
     }
     // ----- First try : look if this is an archive with no commentaries (most of the time)
     // in this case the end of central dir is at 22 bytes of the file end
     $v_found = 0;
     if ($v_size > 26) {
         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Look for central dir with no comment');
         @fseek($this->zip_fd, $v_size - 22);
         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Position after min central position : \''.ftell($this->zip_fd).'\'');
         if (($v_pos = @ftell($this->zip_fd)) != $v_size - 22) {
             // ----- Error log
             PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Unable to seek back to the middle of the archive \'' . $this->zipname . '\'');
             // ----- Return
             //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
             return PclZip::errorCode();
         }
         // ----- Read for bytes
         $v_binary_data = @fread($this->zip_fd, 4);
         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Binary data is : '".sprintf("%08x", $v_binary_data)."'");
         $v_data = @unpack('Vid', $v_binary_data);
         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Binary signature is : '".sprintf("0x%08x", $v_data['id'])."'");
         // ----- Check signature
         if ($v_data['id'] == 0x6054b50) {
             //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Found central dir at the default position.");
             $v_found = 1;
         }
         $v_pos = ftell($this->zip_fd);
     }
     // ----- Go back to the maximum possible size of the Central Dir End Record
     if (!$v_found) {
         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Start extended search of end central dir');
         $v_maximum_size = 65557;
         // 0xFFFF + 22;
         if ($v_maximum_size > $v_size) {
             $v_maximum_size = $v_size;
         }
         @fseek($this->zip_fd, $v_size - $v_maximum_size);
         if (@ftell($this->zip_fd) != $v_size - $v_maximum_size) {
             // ----- Error log
             PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Unable to seek back to the middle of the archive \'' . $this->zipname . '\'');
             // ----- Return
             //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
             return PclZip::errorCode();
         }
         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Position after max central position : \''.ftell($this->zip_fd).'\'');
         // ----- Read byte per byte in order to find the signature
         $v_pos = ftell($this->zip_fd);
         $v_bytes = 0x0;
         while ($v_pos < $v_size) {
             // ----- Read a byte
             $v_byte = @fread($this->zip_fd, 1);
             // -----  Add the byte
             $v_bytes = $v_bytes << 8 | Ord($v_byte);
             // ----- Compare the bytes
             if ($v_bytes == 0x504b0506) {
                 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, 'Found End Central Dir signature at position : \''.ftell($this->zip_fd).'\'');
                 $v_pos++;
                 break;
             }
             $v_pos++;
         }
         // ----- Look if not found end of central dir
         if ($v_pos == $v_size) {
             //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Unable to find End of Central Dir Record signature");
             // ----- Error log
             PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Unable to find End of Central Dir Record signature");
             // ----- Return
             //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
             return PclZip::errorCode();
         }
     }
     // ----- Read the first 18 bytes of the header
     $v_binary_data = fread($this->zip_fd, 18);
     // ----- Look for invalid block size
     if (strlen($v_binary_data) != 18) {
         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Invalid End of Central Dir Record size : ".strlen($v_binary_data));
         // ----- Error log
         PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Invalid End of Central Dir Record size : " . strlen($v_binary_data));
         // ----- Return
         //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
         return PclZip::errorCode();
     }
     // ----- Extract the values
     ////--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Central Dir Record : '".$v_binary_data."'");
     ////--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Central Dir Record (Hex) : '".bin2hex($v_binary_data)."'");
     $v_data = unpack('vdisk/vdisk_start/vdisk_entries/ventries/Vsize/Voffset/vcomment_size', $v_binary_data);
     // ----- Check the global size
     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Comment length : ".$v_data['comment_size']);
     if ($v_pos + $v_data['comment_size'] + 18 != $v_size) {
         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "The central dir is not at the end of the archive. Some trailing bytes exists after the archive.");
         // ----- Removed in release 2.2 see readme file
         // The check of the file size is a little too strict.
         // Some bugs where found when a zip is encrypted/decrypted with 'crypt'.
         // While decrypted, zip has training 0 bytes
         if (0) {
             // ----- Error log
             PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'The central dir is not at the end of the archive.' . ' Some trailing bytes exists after the archive.');
             // ----- Return
             //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
             return PclZip::errorCode();
         }
     }
     // ----- Get comment
     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Comment size : \''.$v_data['comment_size'].'\'');
     if ($v_data['comment_size'] != 0) {
         $p_central_dir['comment'] = fread($this->zip_fd, $v_data['comment_size']);
     } else {
         $p_central_dir['comment'] = '';
     }
     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Comment : \''.$p_central_dir['comment'].'\'');
     $p_central_dir['entries'] = $v_data['entries'];
     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Nb of entries : \''.$p_central_dir['entries'].'\'');
     $p_central_dir['disk_entries'] = $v_data['disk_entries'];
     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Nb of entries for this disk : \''.$p_central_dir['disk_entries'].'\'');
     $p_central_dir['offset'] = $v_data['offset'];
     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Offset of Central Dir : \''.$p_central_dir['offset'].'\'');
     $p_central_dir['size'] = $v_data['size'];
     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Size of Central Dir : \''.$p_central_dir['size'].'\'');
     $p_central_dir['disk'] = $v_data['disk'];
     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Disk number : \''.$p_central_dir['disk'].'\'');
     $p_central_dir['disk_start'] = $v_data['disk_start'];
     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Start disk number : \''.$p_central_dir['disk_start'].'\'');
     // TBC
     //for(reset($p_central_dir); $key = key($p_central_dir); next($p_central_dir)) {
     //  //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "central_dir[$key] = ".$p_central_dir[$key]);
     //}
     // ----- Return
     //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
     return $v_result;
 }
Example #30
0
 function QuotedPrintableEncode($text, $header_charset = '', $break_lines = 1, $email_header = 0)
 {
     $ln = strlen($text);
     $h = strlen($header_charset) > 0;
     if ($h) {
         $encode = array('=' => 1, '?' => 1, '_' => 1, '(' => 1, ')' => 1, '<' => 1, '>' => 1, '@' => 1, ',' => 1, ';' => 1, '"' => 1, '\\' => 1, '[' => 1, ']' => 1, ':' => 1);
         $s = $email_header ? $encode : array();
         $b = $space = $break_lines = 0;
         for ($i = 0; $i < $ln; ++$i) {
             $c = $text[$i];
             if (isset($s[$c])) {
                 $b = 1;
                 break;
             }
             switch ($o = Ord($c)) {
                 case 9:
                 case 32:
                     $space = $i + 1;
                     $b = 1;
                     break 2;
                 case 10:
                 case 13:
                     break 2;
                 default:
                     if ($o < 32 || $o > 127) {
                         $b = 1;
                         $s = $encode;
                         break 2;
                     }
             }
         }
         if ($i == $ln) {
             return $text;
         }
         if ($space > 0) {
             return substr($text, 0, $space) . ($space < $ln ? $this->QuotedPrintableEncode(substr($text, $space), $header_charset, $break_lines, $email_header) : "");
         }
     }
     for ($w = $e = '', $n = 0, $l = 0, $i = 0; $i < $ln; ++$i) {
         $c = $text[$i];
         $o = Ord($c);
         $en = 0;
         switch ($o) {
             case 9:
             case 32:
                 if (!$h) {
                     $w = $c;
                     $c = '';
                 } else {
                     if ($b) {
                         if ($o == 32) {
                             $c = '_';
                         } else {
                             $en = 1;
                         }
                     }
                 }
                 break;
             case 10:
             case 13:
                 if (strlen($w)) {
                     if ($break_lines && $l + 3 > 75) {
                         $e .= '=' . $this->line_break;
                         $l = 0;
                     }
                     $e .= sprintf('=%02X', Ord($w));
                     $l += 3;
                     $w = '';
                 }
                 $e .= $c;
                 if ($h) {
                     $e .= "\t";
                 }
                 $l = 0;
                 continue 2;
             case 46:
             case 70:
             case 102:
                 $en = !$h && ($l == 0 || $l + 1 > 75);
                 break;
             default:
                 if ($o > 127 || $o < 32 || !strcmp($c, '=')) {
                     $en = 1;
                 } elseif ($h && isset($s[$c])) {
                     $en = 1;
                 }
                 break;
         }
         if (strlen($w)) {
             if ($break_lines && $l + 1 > 75) {
                 $e .= '=' . $this->line_break;
                 $l = 0;
             }
             $e .= $w;
             ++$l;
             $w = '';
         }
         if (strlen($c)) {
             if ($en) {
                 $c = sprintf('=%02X', $o);
                 $el = 3;
                 $n = 1;
                 $b = 1;
             } else {
                 $el = 1;
             }
             if ($break_lines && $l + $el > 75) {
                 $e .= '=' . $this->line_break;
                 $l = 0;
             }
             $e .= $c;
             $l += $el;
         }
     }
     if (strlen($w)) {
         if ($break_lines && $l + 3 > 75) {
             $e .= '=' . $this->line_break;
         }
         $e .= sprintf('=%02X', Ord($w));
     }
     if ($h && $n) {
         return '=?' . $header_charset . '?q?' . $e . '?=';
     } else {
         return $e;
     }
 }