PageRenderTime 26ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/core/jaxl.util.php

https://github.com/passkey1510/JAXL
PHP | 212 lines | 125 code | 28 blank | 59 comment | 18 complexity | 959b664b83ab25b554564573f2c4fda3 MD5 | raw file
  1. <?php
  2. /**
  3. * Jaxl (Jabber XMPP Library)
  4. *
  5. * Copyright (c) 2009-2010, Abhinav Singh <me@abhinavsingh.com>.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * * Neither the name of Abhinav Singh nor the names of his
  21. * contributors may be used to endorse or promote products derived
  22. * from this software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  27. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  28. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  29. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  30. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  31. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRIC
  33. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  34. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  35. * POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. * @package jaxl
  38. * @subpackage core
  39. * @author Abhinav Singh <me@abhinavsingh.com>
  40. * @copyright Abhinav Singh
  41. * @link http://code.google.com/p/jaxl
  42. */
  43. /**
  44. * Jaxl Utility Class
  45. */
  46. class JAXLUtil {
  47. public static function curl($url, $type='GET', $headers=array(), $data=false, $user=false, $pass=false) {
  48. $ch = curl_init($url);
  49. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  50. curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
  51. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  52. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  53. curl_setopt($ch, CURLOPT_VERBOSE, false);
  54. if($type == 'POST') {
  55. curl_setopt($ch, CURLOPT_POST, 1);
  56. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  57. }
  58. if($user && $pass) {
  59. curl_setopt($ch, CURLOPT_USERPWD, $user.':'.$pass);
  60. curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
  61. }
  62. $rs = array();
  63. $rs['content'] = curl_exec($ch);
  64. $rs['errno'] = curl_errno($ch);
  65. $rs['errmsg'] = curl_error($ch);
  66. $rs['header'] = curl_getinfo($ch);
  67. curl_close($ch);
  68. return $rs;
  69. }
  70. public static function isWin() {
  71. return strtoupper(substr(PHP_OS,0,3)) == "WIN" ? true : false;
  72. }
  73. public static function pcntlEnabled() {
  74. return extension_loaded('pcntl');
  75. }
  76. public static function sslEnabled() {
  77. return extension_loaded('openssl');
  78. }
  79. public static function getTime() {
  80. list($usec, $sec) = explode(" ", microtime());
  81. return (float) $sec + (float) $usec;
  82. }
  83. public static function splitXML($xml) {
  84. $xmlarr = array();
  85. $temp = preg_split("/<(message|iq|presence|stream|proceed|challenge|success|failure)(?=[\:\s\>])/", $xml, -1, PREG_SPLIT_DELIM_CAPTURE);
  86. for($a=1; $a<count($temp); $a=$a+2) $xmlarr[] = "<".$temp[$a].$temp[($a+1)];
  87. return $xmlarr;
  88. }
  89. public static function explodeData($data) {
  90. $data = explode(',', $data);
  91. $pairs = array();
  92. $key = false;
  93. foreach($data as $pair) {
  94. $dd = strpos($pair, '=');
  95. if($dd) {
  96. $key = trim(substr($pair, 0, $dd));
  97. $pairs[$key] = trim(trim(substr($pair, $dd + 1)), '"');
  98. }
  99. else if(strpos(strrev(trim($pair)), '"') === 0 && $key) {
  100. $pairs[$key] .= ',' . trim(trim($pair), '"');
  101. continue;
  102. }
  103. }
  104. return $pairs;
  105. }
  106. public static function implodeData($data) {
  107. $return = array();
  108. foreach($data as $key => $value)
  109. $return[] = $key . '="' . $value . '"';
  110. return implode(',', $return);
  111. }
  112. public static function generateNonce() {
  113. $str = '';
  114. mt_srand((double) microtime()*10000000);
  115. for($i=0; $i<32; $i++)
  116. $str .= chr(mt_rand(0, 255));
  117. return $str;
  118. }
  119. public static function encryptPassword($data, $user, $pass) {
  120. foreach(array('realm', 'cnonce', 'digest-uri') as $key)
  121. if(!isset($data[$key]))
  122. $data[$key] = '';
  123. $pack = md5($user.':'.$data['realm'].':'.$pass);
  124. if(isset($data['authzid']))
  125. $a1 = pack('H32',$pack).sprintf(':%s:%s:%s',$data['nonce'],$data['cnonce'],$data['authzid']);
  126. else
  127. $a1 = pack('H32',$pack).sprintf(':%s:%s',$data['nonce'],$data['cnonce']);
  128. $a2 = 'AUTHENTICATE:'.$data['digest-uri'];
  129. return md5(sprintf('%s:%s:%s:%s:%s:%s', md5($a1), $data['nonce'], $data['nc'], $data['cnonce'], $data['qop'], md5($a2)));
  130. }
  131. public static function hmacMD5($key, $data) {
  132. if(strlen($key) > 64) $key = pack('H32', md5($key));
  133. if(strlen($key) < 64) $key = str_pad($key, 64, chr(0));
  134. $k_ipad = substr($key, 0, 64) ^ str_repeat(chr(0x36), 64);
  135. $k_opad = substr($key, 0, 64) ^ str_repeat(chr(0x5C), 64);
  136. $inner = pack('H32', md5($k_ipad . $data));
  137. $digest = md5($k_opad . $inner);
  138. return $digest;
  139. }
  140. public static function pbkdf2($data, $secret, $iteration, $dkLen=32, $algo='sha1') {
  141. $hLen = strlen(hash($algo, null, true));
  142. $l = ceil($dkLen/$hLen);
  143. $t = null;
  144. for($i=1; $i<=$l; $i++) {
  145. $f = $u = hash_hmac($algo, $s.pack('N', $i), $p, true);
  146. for($j=1; $j<$c; $j++)
  147. $f ^= ($u = hash_hmac($algo, $u, $p, true));
  148. $t .= $f;
  149. }
  150. return substr($t, 0, $dk_len);
  151. }
  152. public static function getBareJid($jid) {
  153. list($user,$domain,$resource) = self::splitJid($jid);
  154. return ($user ? $user."@" : "").$domain;
  155. }
  156. public static function splitJid($jid) {
  157. preg_match("/(?:([^\@]+)\@)?([^\/]+)(?:\/(.*))?$/",$jid,$matches);
  158. return array($matches[1],$matches[2],@$matches[3]);
  159. }
  160. /*
  161. * xmlentities method for PHP supporting
  162. * 1) Rserved characters in HTML
  163. * 2) ISO 8859-1 Symbols
  164. * 3) ISO 8859-1 Characters
  165. * 4) Math Symbols Supported by HTML
  166. * 5) Greek Letters Supported by HTML
  167. * 6) Other Entities Supported by HTML
  168. *
  169. * Credits:
  170. * --------
  171. * http://www.sourcerally.net/Scripts/39-Convert-HTML-Entities-to-XML-Entities
  172. * http://www.w3schools.com/tags/ref_entities.asp
  173. * http://www.w3schools.com/tags/ref_symbols.asp
  174. */
  175. public static function xmlentities($str) {
  176. $str = htmlentities($str, ENT_QUOTES, 'UTF-8');
  177. $xml = array('&#34;','&#38;','&#38;','&#60;','&#62;','&#160;','&#161;','&#162;','&#163;','&#164;','&#165;','&#166;','&#167;','&#168;','&#169;','&#170;','&#171;','&#172;','&#173;','&#174;','&#175;','&#176;','&#177;','&#178;','&#179;','&#180;','&#181;','&#182;','&#183;','&#184;','&#185;','&#186;','&#187;','&#188;','&#189;','&#190;','&#191;','&#192;','&#193;','&#194;','&#195;','&#196;','&#197;','&#198;','&#199;','&#200;','&#201;','&#202;','&#203;','&#204;','&#205;','&#206;','&#207;','&#208;','&#209;','&#210;','&#211;','&#212;','&#213;','&#214;','&#215;','&#216;','&#217;','&#218;','&#219;','&#220;','&#221;','&#222;','&#223;','&#224;','&#225;','&#226;','&#227;','&#228;','&#229;','&#230;','&#231;','&#232;','&#233;','&#234;','&#235;','&#236;','&#237;','&#238;','&#239;','&#240;','&#241;','&#242;','&#243;','&#244;','&#245;','&#246;','&#247;','&#248;','&#249;','&#250;','&#251;','&#252;','&#253;','&#254;','&#255;','&#8704;','&#8706;','&#8707;','&#8709;','&#8711;','&#8712;','&#8713;','&#8715;','&#8719;','&#8721;','&#8722;','&#8727;','&#8730;','&#8733;','&#8734;','&#8736;','&#8743;','&#8744;','&#8745;','&#8746;','&#8747;','&#8756;','&#8764;','&#8773;','&#8776;','&#8800;','&#8801;','&#8804;','&#8805;','&#8834;','&#8835;','&#8836;','&#8838;','&#8839;','&#8853;','&#8855;','&#8869;','&#8901;','&#913;','&#914;','&#915;','&#916;','&#917;','&#918;','&#919;','&#920;','&#921;','&#922;','&#923;','&#924;','&#925;','&#926;','&#927;','&#928;','&#929;','&#931;','&#932;','&#933;','&#934;','&#935;','&#936;','&#937;','&#945;','&#946;','&#947;','&#948;','&#949;','&#950;','&#951;','&#952;','&#953;','&#954;','&#955;','&#956;','&#957;','&#958;','&#959;','&#960;','&#961;','&#962;','&#963;','&#964;','&#965;','&#966;','&#967;','&#968;','&#969;','&#977;','&#978;','&#982;','&#338;','&#339;','&#352;','&#353;','&#376;','&#402;','&#710;','&#732;','&#8194;','&#8195;','&#8201;','&#8204;','&#8205;','&#8206;','&#8207;','&#8211;','&#8212;','&#8216;','&#8217;','&#8218;','&#8220;','&#8221;','&#8222;','&#8224;','&#8225;','&#8226;','&#8230;','&#8240;','&#8242;','&#8243;','&#8249;','&#8250;','&#8254;','&#8364;','&#8482;','&#8592;','&#8593;','&#8594;','&#8595;','&#8596;','&#8629;','&#8968;','&#8969;','&#8970;','&#8971;','&#9674;','&#9824;','&#9827;','&#9829;','&#9830;');
  178. $html = array('&quot;','&amp;','&amp;','&lt;','&gt;','&nbsp;','&iexcl;','&cent;','&pound;','&curren;','&yen;','&brvbar;','&sect;','&uml;','&copy;','&ordf;','&laquo;','&not;','&shy;','&reg;','&macr;','&deg;','&plusmn;','&sup2;','&sup3;','&acute;','&micro;','&para;','&middot;','&cedil;','&sup1;','&ordm;','&raquo;','&frac14;','&frac12;','&frac34;','&iquest;','&Agrave;','&Aacute;','&Acirc;','&Atilde;','&Auml;','&Aring;','&AElig;','&Ccedil;','&Egrave;','&Eacute;','&Ecirc;','&Euml;','&Igrave;','&Iacute;','&Icirc;','&Iuml;','&ETH;','&Ntilde;','&Ograve;','&Oacute;','&Ocirc;','&Otilde;','&Ouml;','&times;','&Oslash;','&Ugrave;','&Uacute;','&Ucirc;','&Uuml;','&Yacute;','&THORN;','&szlig;','&agrave;','&aacute;','&acirc;','&atilde;','&auml;','&aring;','&aelig;','&ccedil;','&egrave;','&eacute;','&ecirc;','&euml;','&igrave;','&iacute;','&icirc;','&iuml;','&eth;','&ntilde;','&ograve;','&oacute;','&ocirc;','&otilde;','&ouml;','&divide;','&oslash;','&ugrave;','&uacute;','&ucirc;','&uuml;','&yacute;','&thorn;','&yuml;','&forall;','&part;','&exist;','&empty;','&nabla;','&isin;','&notin;','&ni;','&prod;','&sum;','&minus;','&lowast;','&radic;','&prop;','&infin;','&ang;','&and;','&or;','&cap;','&cup;','&int;','&there4;','&sim;','&cong;','&asymp;','&ne;','&equiv;','&le;','&ge;','&sub;','&sup;','&nsub;','&sube;','&supe;','&oplus;','&otimes;','&perp;','&sdot;','&Alpha;','&Beta;','&Gamma;','&Delta;','&Epsilon;','&Zeta;','&Eta;','&Theta;','&Iota;','&Kappa;','&Lambda;','&Mu;','&Nu;','&Xi;','&Omicron;','&Pi;','&Rho;','&Sigma;','&Tau;','&Upsilon;','&Phi;','&Chi;','&Psi;','&Omega;','&alpha;','&beta;','&gamma;','&delta;','&epsilon;','&zeta;','&eta;','&theta;','&iota;','&kappa;','&lambda;','&mu;','&nu;','&xi;','&omicron;','&pi;','&rho;','&sigmaf;','&sigma;','&tau;','&upsilon;','&phi;','&chi;','&psi;','&omega;','&thetasym;','&upsih;','&piv;','&OElig;','&oelig;','&Scaron;','&scaron;','&Yuml;','&fnof;','&circ;','&tilde;','&ensp;','&emsp;','&thinsp;','&zwnj;','&zwj;','&lrm;','&rlm;','&ndash;','&mdash;','&lsquo;','&rsquo;','&sbquo;','&ldquo;','&rdquo;','&bdquo;','&dagger;','&Dagger;','&bull;','&hellip;','&permil;','&prime;','&Prime;','&lsaquo;','&rsaquo;','&oline;','&euro;','&trade;','&larr;','&uarr;','&rarr;','&darr;','&harr;','&crarr;','&lceil;','&rceil;','&lfloor;','&rfloor;','&loz;','&spades;','&clubs;','&hearts;','&diams;');
  179. $str = str_replace($html,$xml,$str);
  180. $str = str_ireplace($html,$xml,$str);
  181. return $str;
  182. }
  183. }
  184. ?>