00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 class String extends Object {
00034
00035
00036
00037
00038
00039
00040
00041 function &getInstance() {
00042 static $instance = array();
00043
00044 if (!$instance) {
00045 $instance[0] =& new String();
00046 }
00047 return $instance[0];
00048 }
00049
00050
00051
00052
00053
00054
00055
00056 function uuid() {
00057 $node = env('SERVER_ADDR');
00058 $pid = null;
00059
00060 if (strpos($node, ':') !== false) {
00061 if (substr_count($node, '::')) {
00062 $node = str_replace('::', str_repeat(':0000', 8 - substr_count($node, ':')) . ':', $node);
00063 }
00064 $node = explode(':', $node) ;
00065 $ipv6 = '' ;
00066
00067 foreach ($node as $id) {
00068 $ipv6 .= str_pad(base_convert($id, 16, 2), 16, 0, STR_PAD_LEFT);
00069 }
00070 $node = base_convert($ipv6, 2, 10);
00071
00072 if (strlen($node) < 38) {
00073 $node = null;
00074 } else {
00075 $node = crc32($node);
00076 }
00077 } elseif (empty($node)) {
00078 $host = env('HOSTNAME');
00079
00080 if (empty($host)) {
00081 $host = env('HOST');
00082 }
00083
00084 if (!empty($host)) {
00085 $ip = gethostbyname($host);
00086
00087 if ($ip === $host) {
00088 $node = crc32($host);
00089 } else {
00090 $node = ip2long($ip);
00091 }
00092 }
00093 } elseif ($node !== '127.0.0.1') {
00094 $node = ip2long($node);
00095 } else {
00096 $node = null;
00097 }
00098
00099 if (empty($node)) {
00100 $node = crc32(Configure::read('Security.salt'));
00101 }
00102
00103 if (function_exists('zend_thread_id')) {
00104 $pid = zend_thread_id();
00105 } else {
00106 $pid = getmypid();
00107 }
00108
00109 if (!$pid || $pid > 65535) {
00110 $pid = mt_rand(0, 0xfff) | 0x4000;
00111 }
00112
00113 list($timeMid, $timeLow) = explode(' ', microtime());
00114 $uuid = sprintf("%08x-%04x-%04x-%02x%02x-%04x%08x", (int)$timeLow, (int)substr($timeMid, 2) & 0xffff,
00115 mt_rand(0, 0xfff) | 0x4000, mt_rand(0, 0x3f) | 0x80, mt_rand(0, 0xff), $pid, $node);
00116
00117 return $uuid;
00118 }
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129 function tokenize($data, $separator = ',', $leftBound = '(', $rightBound = ')') {
00130 if (empty($data) || is_array($data)) {
00131 return $data;
00132 }
00133
00134 $depth = 0;
00135 $offset = 0;
00136 $buffer = '';
00137 $results = array();
00138 $length = strlen($data);
00139 $open = false;
00140
00141 while ($offset <= $length) {
00142 $tmpOffset = -1;
00143 $offsets = array(strpos($data, $separator, $offset), strpos($data, $leftBound, $offset), strpos($data, $rightBound, $offset));
00144 for ($i = 0; $i < 3; $i++) {
00145 if ($offsets[$i] !== false && ($offsets[$i] < $tmpOffset || $tmpOffset == -1)) {
00146 $tmpOffset = $offsets[$i];
00147 }
00148 }
00149 if ($tmpOffset !== -1) {
00150 $buffer .= substr($data, $offset, ($tmpOffset - $offset));
00151 if ($data{$tmpOffset} == $separator && $depth == 0) {
00152 $results[] = $buffer;
00153 $buffer = '';
00154 } else {
00155 $buffer .= $data{$tmpOffset};
00156 }
00157 if ($leftBound != $rightBound) {
00158 if ($data{$tmpOffset} == $leftBound) {
00159 $depth++;
00160 }
00161 if ($data{$tmpOffset} == $rightBound) {
00162 $depth--;
00163 }
00164 } else {
00165 if ($data{$tmpOffset} == $leftBound) {
00166 if (!$open) {
00167 $depth++;
00168 $open = true;
00169 } else {
00170 $depth--;
00171 $open = false;
00172 }
00173 }
00174 }
00175 $offset = ++$tmpOffset;
00176 } else {
00177 $results[] = $buffer . substr($data, $offset);
00178 $offset = $length + 1;
00179 }
00180 }
00181 if (empty($results) && !empty($buffer)) {
00182 $results[] = $buffer;
00183 }
00184
00185 if (!empty($results)) {
00186 $data = array_map('trim', $results);
00187 } else {
00188 $data = array();
00189 }
00190 return $data;
00191 }
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213 function insert($str, $data, $options = array()) {
00214 $defaults = array(
00215 'before' => ':', 'after' => null, 'escape' => '\\', 'format' => null, 'clean' => false
00216 );
00217 $options += $defaults;
00218 $format = $options['format'];
00219
00220 if (!isset($format)) {
00221 $format = sprintf(
00222 '/(?<!%s)%s%%s%s/',
00223 preg_quote($options['escape'], '/'),
00224 str_replace('%', '%%', preg_quote($options['before'], '/')),
00225 str_replace('%', '%%', preg_quote($options['after'], '/'))
00226 );
00227 }
00228 if (!is_array($data)) {
00229 $data = array($data);
00230 }
00231
00232 if (array_keys($data) === array_keys(array_values($data))) {
00233 $offset = 0;
00234 while (($pos = strpos($str, '?', $offset)) !== false) {
00235 $val = array_shift($data);
00236 $offset = $pos + strlen($val);
00237 $str = substr_replace($str, $val, $pos, 1);
00238 }
00239 } else {
00240 asort($data);
00241
00242 $hashKeys = array_map('md5', array_keys($data));
00243 $tempData = array_combine(array_keys($data), array_values($hashKeys));
00244 foreach ($tempData as $key => $hashVal) {
00245 $key = sprintf($format, preg_quote($key, '/'));
00246 $str = preg_replace($key, $hashVal, $str);
00247 }
00248 $dataReplacements = array_combine($hashKeys, array_values($data));
00249 foreach ($dataReplacements as $tmpHash => $data) {
00250 $str = str_replace($tmpHash, $data, $str);
00251 }
00252 }
00253
00254 if (!isset($options['format']) && isset($options['before'])) {
00255 $str = str_replace($options['escape'].$options['before'], $options['before'], $str);
00256 }
00257 if (!$options['clean']) {
00258 return $str;
00259 }
00260 return String::cleanInsert($str, $options);
00261 }
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273 function cleanInsert($str, $options) {
00274 $clean = $options['clean'];
00275 if (!$clean) {
00276 return $str;
00277 }
00278 if ($clean === true) {
00279 $clean = array('method' => 'text');
00280 }
00281 if (!is_array($clean)) {
00282 $clean = array('method' => $options['clean']);
00283 }
00284 switch ($clean['method']) {
00285 case 'html':
00286 $clean = array_merge(array(
00287 'word' => '[\w,.]+',
00288 'andText' => true,
00289 'replacement' => '',
00290 ), $clean);
00291 $kleenex = sprintf(
00292 '/[\s]*[a-z]+=(")(%s%s%s[\s]*)+\\1/i',
00293 preg_quote($options['before'], '/'),
00294 $clean['word'],
00295 preg_quote($options['after'], '/')
00296 );
00297 $str = preg_replace($kleenex, $clean['replacement'], $str);
00298 if ($clean['andText']) {
00299 $options['clean'] = array('method' => 'text');
00300 $str = String::cleanInsert($str, $options);
00301 }
00302 break;
00303 case 'text':
00304 $clean = array_merge(array(
00305 'word' => '[\w,.]+',
00306 'gap' => '[\s]*(?:(?:and|or)[\s]*)?',
00307 'replacement' => '',
00308 ), $clean);
00309
00310 $kleenex = sprintf(
00311 '/(%s%s%s%s|%s%s%s%s)/',
00312 preg_quote($options['before'], '/'),
00313 $clean['word'],
00314 preg_quote($options['after'], '/'),
00315 $clean['gap'],
00316 $clean['gap'],
00317 preg_quote($options['before'], '/'),
00318 $clean['word'],
00319 preg_quote($options['after'], '/')
00320 );
00321 $str = preg_replace($kleenex, $clean['replacement'], $str);
00322 break;
00323 }
00324 return $str;
00325 }
00326 }
00327 ?>