26 require_once
'PEAR.php';
151 $this->_delimiters = array(
155 $this->data = array();
159 $this->warnings = array();
160 $this->_options = array(
161 'stripDelimiter' =>
true,
164 'wordWrapWidth' =>
false,
165 'wordWrapBreak' =>
"\n",
167 'removeCurlyBraces' =>
false,
168 'extractAuthors' =>
true,
170 foreach (
$options as $option => $value) {
176 $this->rtfstring =
'AUTHORS, "{\b TITLE}", {\i JOURNAL}, YEAR';
177 $this->htmlstring =
'AUTHORS, "<strong>TITLE</strong>", <em>JOURNAL</em>, YEAR<br />';
178 $this->allowedEntryTypes = array(
194 $this->authorstring =
'VON LAST, JR, FIRST';
210 if (array_key_exists($option, $this->_options)) {
211 $this->_options[$option] = $value;
231 if (($this->content = @file_get_contents(
$filename)) ===
false) {
254 $this->warnings = array();
255 $this->data = array();
262 for ($i = 0; $i < strlen($this->content); $i ++) {
263 $char = substr($this->content, $i, 1);
264 if ((0 != $open) && (
'@' == $char)) {
272 if ((0 == $open) && (
'@' == $char)) {
274 } elseif ($entry && (
'{' == $char)
275 && (
'\\' != $lastchar)
278 } elseif ($entry && (
'}' == $char)
279 && (
'\\' != $lastchar)
296 $this->data[] = $entrydata;
312 $this->data[] = $entrydata;
322 if ($this->_options[
'validate']) {
324 foreach ($this->data as $entry) {
325 $cites[] = $entry[
'cite'];
327 $unique = array_unique($cites);
328 if (
sizeof($cites) !=
sizeof($unique)) {
329 $notuniques = array();
330 for ($i = 0; $i <
sizeof($cites); $i ++) {
331 if (
'' == $unique[$i]) {
332 $notuniques[] = $cites[$i];
335 $this->
_generateWarning(
'WARNING_MULTIPLE_ENTRIES', implode(
',', $notuniques));
372 if ($this->_options[
'validate']) {
376 if (
'@string' == strtolower(substr($entry, 0, 7))) {
378 if ($this->_options[
'validate']) {
379 $this->
_generateWarning(
'STRING_ENTRY_NOT_YET_SUPPORTED',
'', $entry .
'}');
381 } elseif (
'@preamble' == strtolower(substr($entry, 0, 9))) {
383 if ($this->_options[
'validate']) {
384 $this->
_generateWarning(
'PREAMBLE_ENTRY_NOT_YET_SUPPORTED',
'', $entry .
'}');
388 while (strrpos($entry,
'=') !==
false) {
389 $position = strrpos($entry,
'=');
392 if (substr($entry, $position - 1, 1) ==
'\\') {
399 $substring = substr($entry, 0, $position);
400 $position = strrpos($substring,
'=');
402 if (substr($entry, $position - 1, 1) ==
'\\') {
409 $value = trim(substr($entry, $position + 1));
410 $entry = substr($entry, 0, $position);
411 if (
',' == substr($value, strlen($value) - 1, 1)) {
412 $value = substr($value, 0, - 1);
414 if ($this->_options[
'validate']) {
417 if ($this->_options[
'stripDelimiter']) {
420 if ($this->_options[
'unwrap']) {
421 $value = $this->
_unwrap($value);
423 if ($this->_options[
'removeCurlyBraces']) {
426 $position = strrpos($entry,
',');
427 $field = strtolower(trim(substr($entry, $position + 1)));
428 $ret[$field] = $value;
429 $entry = substr($entry, 0, $position);
432 $arr = explode(
'{', $entry);
433 $ret[
'cite'] = trim($arr[1]);
434 $ret[
'entryType'] = strtolower(trim($arr[0]));
435 if (
'@' ==
$ret[
'entryType']{0}) {
436 $ret[
'entryType'] = substr(
$ret[
'entryType'], 1);
438 if ($this->_options[
'validate']) {
444 if (in_array(
'author', array_keys(
$ret)) && $this->_options[
'extractAuthors']) {
472 $length = strlen($entry);
474 for ($i = $length - 1; $i >= $position; $i --) {
475 $precedingchar = substr($entry, $i - 1, 1);
476 $char = substr($entry, $i, 1);
477 if ((
'{' == $char) && (
'\\' != $precedingchar)) {
480 if ((
'}' == $char) && (
'\\' != $precedingchar)) {
490 $entrycopy = trim($entry);
491 $lastchar = $entrycopy{strlen($entrycopy) - 1};
492 if (
',' == $lastchar) {
493 $lastchar = $entrycopy{strlen($entrycopy) - 2};
495 if (
'"' == $lastchar) {
501 for ($i = $length; $i >= $position; $i --) {
502 $precedingchar = substr($entry, $i - 1, 1);
503 $char = substr($entry, $i, 1);
504 if ((
'"' == $char) && (
'\\' != $precedingchar)) {
529 return in_array($entry, $this->allowedEntryTypes);
548 $opening = array_keys($this->_delimiters);
549 $closing = array_values($this->_delimiters);
551 if (strrpos($entry,
'=') !==
false) {
552 $position = strrpos($entry,
'=');
554 if (substr($entry, $position - 1, 1) ==
'\\') {
558 $substring = substr($entry, 0, $position);
559 $position = strrpos($substring,
'=');
561 if (substr($entry, $position - 1, 1) ==
'\\') {
565 $value = trim(substr($entry, $position + 1));
569 for ($i = 0; $i < strlen($value); $i ++) {
570 $char = substr($this->content, $i, 1);
571 if (in_array($char, $opening) && (
'\\' != $lastchar)) {
573 } elseif (in_array($char, $closing) && (
'\\' != $lastchar)) {
598 $beginningdels = array_keys($this->_delimiters);
599 $length = strlen($entry);
600 $firstchar = substr($entry, 0, 1);
601 $lastchar = substr($entry, - 1, 1);
602 while (in_array($firstchar, $beginningdels)) {
603 if ($lastchar == $this->_delimiters[$firstchar]) {
604 $entry = substr($entry, 1, - 1);
608 $firstchar = substr($entry, 0, 1);
609 $lastchar = substr($entry, - 1, 1);
626 $entry = preg_replace(
'/\s+/',
' ', $entry);
642 if ((
'' != $entry) && (is_string($entry))) {
643 $entry = wordwrap($entry, $this->_options[
'wordWrapWidth'], $this->_options[
'wordWrapBreak'], $this->_options[
'wordWrapCut']);
660 $entry = $this->
_unwrap($entry);
661 $authorarray = array();
662 $authorarray = explode(
' and ', $entry);
663 for ($i = 0; $i <
sizeof($authorarray); $i ++) {
664 $author = trim($authorarray[$i]);
671 if (strpos($author,
',') ===
false) {
674 $tmparray = explode(
' |~', $author);
675 $size =
sizeof($tmparray);
677 $last = $tmparray[0];
678 } elseif (2 ==
$size) {
679 $first = $tmparray[0];
680 $last = $tmparray[1];
684 for ($j = 0; $j < (
$size - 1); $j ++) {
686 $last .=
' ' . $tmparray[$j];
691 } elseif ((0 == $case) || (- 1 == $case)) {
694 for ($k = ($j + 1); $k < (
$size - 1); $k ++) {
698 } elseif (0 == $futurecase) {
705 $last .=
' ' . $tmparray[$j];
707 $von .=
' ' . $tmparray[$j];
710 $von .=
' ' . $tmparray[$j];
713 $von .=
' ' . $tmparray[$j];
719 } elseif (0 == $case) {
721 $von .=
' ' . $tmparray[$j];
723 $first .=
' ' . $tmparray[$j];
728 $last .=
' ' . $tmparray[
$size - 1];
732 $tmparray = explode(
',', $author);
734 $vonlastarray = array();
735 $vonlastarray = explode(
' ', $tmparray[0]);
736 $size =
sizeof($vonlastarray);
738 $last = $vonlastarray[0];
741 for ($j = 0; $j < (
$size - 1); $j ++) {
743 $last .=
' ' . $vonlastarray[$j];
747 for ($k = ($j + 1); $k < (
$size - 1); $k ++) {
752 } elseif (0 == $case) {
758 $last .=
' ' . $vonlastarray[$j];
760 $von .=
' ' . $vonlastarray[$j];
763 $von .=
' ' . $vonlastarray[$j];
767 $last .=
' ' . $vonlastarray[
$size - 1];
770 if (3 ==
sizeof($tmparray)) {
774 $first = $tmparray[
sizeof($tmparray) - 1];
776 $authorarray[$i] = array(
777 'first' => trim($first),
779 'last' => trim($last),
805 $trimmedword = trim($word);
808 if (is_string($word) && (strlen($trimmedword) > 0)) {
812 while (! $found && ($i <= strlen($word))) {
813 $letter = substr($trimmedword, $i, 1);
821 if (($ord >= 65) && ($ord <= 90) && (0 == $openbrace)) {
824 } elseif (($ord >= 97) && ($ord <= 122) && (0 == $openbrace)) {
855 if (preg_match(
'/^{.*@.*}$/', $entry)) {
859 if (preg_match(
'/^\".*\\".*\"$/', $entry)) {
860 $this->
_generateWarning(
'WARNING_ESCAPED_DOUBLE_QUOTE_INSIDE_DOUBLE_QUOTES', $entry, $wholeentry);
866 for ($i = 0; $i < strlen($entry); $i ++) {
867 $char = substr($entry, $i, 1);
868 if ((
'{' == $char) && (
'\\' != $lastchar)) {
871 if ((
'}' == $char) && (
'\\' != $lastchar)) {
877 $this->
_generateWarning(
'WARNING_UNBALANCED_AMOUNT_OF_BRACES', $entry, $wholeentry);
892 $beginningdels = array_keys($this->_delimiters);
893 $firstchar = substr($entry, 0, 1);
894 $lastchar = substr($entry, - 1, 1);
897 while (in_array($firstchar, $beginningdels)) {
898 if ($lastchar == $this->_delimiters[$firstchar]) {
899 $begin .= $firstchar;
901 $value = substr($value, 1, - 1);
905 $firstchar = substr($value, 0, 1);
906 $lastchar = substr($value, - 1, 1);
909 $pattern =
'/([^\\\\])\{(.*?[^\\\\])\}/';
910 $replacement =
'$1$2';
911 $value = preg_replace($pattern, $replacement, $value);
913 $value = $begin . $value . $end;
929 $warning[
'warning'] = $type;
930 $warning[
'entry'] = $entry;
931 $warning[
'wholeentry'] = $wholeentry;
932 $this->warnings[] = $warning;
942 $this->warnings = array();
953 if (
sizeof($this->warnings) > 0) {
984 if (! array_key_exists(
'von', $array)) {
987 $array[
'von'] = trim($array[
'von']);
989 if (! array_key_exists(
'last', $array)) {
992 $array[
'last'] = trim($array[
'last']);
994 if (! array_key_exists(
'jr', $array)) {
997 $array[
'jr'] = trim($array[
'jr']);
999 if (! array_key_exists(
'first', $array)) {
1000 $array[
'first'] =
'';
1002 $array[
'first'] = trim($array[
'first']);
1005 $ret = str_replace(
"VON", $array[
'von'],
$ret);
1006 $ret = str_replace(
"LAST", $array[
'last'],
$ret);
1007 $ret = str_replace(
"JR", $array[
'jr'],
$ret);
1008 $ret = str_replace(
"FIRST", $array[
'first'],
$ret);
1024 foreach ($this->data as $entry) {
1026 $bibtex .=
'@' . strtolower($entry[
'entryType']) .
' { ' . $entry[
'cite'] .
",\n";
1028 foreach ($entry as $key => $val) {
1029 if ($this->_options[
'wordWrapWidth'] > 0) {
1030 $val = $this->_wordWrap($val);
1032 if (! in_array($key, array(
'cite',
'entryType',
'author' ))) {
1033 $bibtex .=
"\t" . $key .
' = {' . $val .
"},\n";
1037 if (array_key_exists(
'author', $entry)) {
1038 if ($this->_options[
'extractAuthors']) {
1039 $tmparray = array();
1040 foreach ($entry[
'author'] as $authorentry) {
1043 $author = join(
' and ', $tmparray);
1045 $author = $entry[
'author'];
1050 $bibtex .=
"\tauthor = {" . $author .
"}\n";
1068 $this->data[] = $newentry;
1083 foreach ($this->data as $entry) {
1084 if (array_key_exists($entry[
'entryType'],
$ret)) {
1085 $ret[$entry[
'entryType']] ++;
1087 $ret[$entry[
'entryType']] = 1;
1111 foreach ($this->data as $entry) {
1117 if (array_key_exists(
'title', $entry)) {
1118 $title = $this->
_unwrap($entry[
'title']);
1120 if (array_key_exists(
'journal', $entry)) {
1121 $journal = $this->
_unwrap($entry[
'journal']);
1123 if (array_key_exists(
'year', $entry)) {
1124 $year = $this->
_unwrap($entry[
'year']);
1126 if (array_key_exists(
'author', $entry)) {
1127 if ($this->_options[
'extractAuthors']) {
1128 $tmparray = array();
1129 foreach ($entry[
'author'] as $authorentry) {
1132 $authors = join(
', ', $tmparray);
1134 $authors = $entry[
'author'];
1137 if ((
'' != $title) || (
'' != $journal) || (
'' != $year) || (
'' != $authors)) {
1138 $line = str_replace(
"TITLE", $title, $line);
1139 $line = str_replace(
"JOURNAL", $journal, $line);
1140 $line = str_replace(
"YEAR", $year, $line);
1141 $line = str_replace(
"AUTHORS", $authors, $line);
1142 $line .=
"\n\\par\n";
1145 $this->
_generateWarning(
'WARNING_LINE_WAS_NOT_CONVERTED',
'', print_r($entry, 1));
1169 foreach ($this->data as $entry) {
1175 if (array_key_exists(
'title', $entry)) {
1176 $title = $this->
_unwrap($entry[
'title']);
1178 if (array_key_exists(
'journal', $entry)) {
1179 $journal = $this->
_unwrap($entry[
'journal']);
1181 if (array_key_exists(
'year', $entry)) {
1182 $year = $this->
_unwrap($entry[
'year']);
1184 if (array_key_exists(
'author', $entry)) {
1185 if ($this->_options[
'extractAuthors']) {
1186 $tmparray = array();
1187 foreach ($entry[
'author'] as $authorentry) {
1190 $authors = join(
', ', $tmparray);
1192 $authors = $entry[
'author'];
1195 if ((
'' != $title) || (
'' != $journal) || (
'' != $year) || (
'' != $authors)) {
1196 $line = str_replace(
"TITLE", $title, $line);
1197 $line = str_replace(
"JOURNAL", $journal, $line);
1198 $line = str_replace(
"YEAR", $year, $line);
1199 $line = str_replace(
"AUTHORS", $authors, $line);
1203 $this->
_generateWarning(
'WARNING_LINE_WAS_NOT_CONVERTED',
'', print_r($entry, 1));