Conditions | 157 |
Total Lines | 491 |
Code Lines | 295 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
565 | function vcardtoegw($_vcard, $charset=null) |
||
566 | { |
||
567 | // the horde class does the charset conversion. DO NOT CONVERT HERE. |
||
568 | // be as flexible as possible |
||
569 | |||
570 | if ($this->log) |
||
571 | { |
||
572 | error_log(__FILE__.'['.__LINE__.'] '.__METHOD__."()\n" . |
||
573 | array2string($_vcard)."\n",3,$this->logfile); |
||
574 | } |
||
575 | |||
576 | if(!($_vcard instanceof Horde_Icalendar)) |
||
577 | { |
||
578 | $container = false; |
||
579 | $vCard = Horde_Icalendar::newComponent('vcard', $container); |
||
580 | |||
581 | if ($charset && $charset != 'utf-8') |
||
582 | { |
||
583 | $_vcard = Api\Translation::convert($_vcard, $charset, 'utf-8'); |
||
584 | } |
||
585 | if (!$vCard->parsevCalendar($_vcard, 'VCARD')) |
||
586 | { |
||
587 | return False; |
||
588 | } |
||
589 | } |
||
590 | else |
||
591 | { |
||
592 | $vCard = $_vcard; |
||
593 | } |
||
594 | $vcardValues = $vCard->getAllAttributes(); |
||
595 | |||
596 | if (!empty($GLOBALS['egw_info']['user']['preferences']['syncml']['minimum_uid_length'])) |
||
597 | { |
||
598 | $minimum_uid_length = $GLOBALS['egw_info']['user']['preferences']['syncml']['minimum_uid_length']; |
||
599 | } |
||
600 | else |
||
601 | { |
||
602 | $minimum_uid_length = 8; |
||
603 | } |
||
604 | |||
605 | #print "<pre>$_vcard</pre>"; |
||
606 | |||
607 | #error_log(print_r($vcardValues, true)); |
||
608 | //Horde::logMessage("vCalAddressbook vcardtoegw: " . print_r($vcardValues, true), __FILE__, __LINE__, PEAR_LOG_DEBUG); |
||
609 | |||
610 | $email = 1; |
||
611 | $tel = 1; |
||
612 | $cell = 1; |
||
613 | $url = 1; |
||
614 | $pref_tel = false; |
||
615 | |||
616 | $rowNames = array(); |
||
617 | foreach($vcardValues as $key => $vcardRow) |
||
618 | { |
||
619 | $rowName = strtoupper($vcardRow['name']); |
||
620 | if ($vcardRow['value'] == '' && implode('', $vcardRow['values']) == '') |
||
621 | { |
||
622 | unset($vcardRow); |
||
623 | continue; |
||
624 | } |
||
625 | $rowTypes = array(); |
||
626 | |||
627 | $vcardRow['uparams'] = array(); |
||
628 | foreach ($vcardRow['params'] as $pname => $params) |
||
629 | { |
||
630 | $pname = strtoupper($pname); |
||
631 | $vcardRow['uparams'][$pname] = $params; |
||
632 | } |
||
633 | |||
634 | |||
635 | // expand 3.0 TYPE paramters to 2.1 qualifiers |
||
636 | $vcardRow['tparams'] = array(); |
||
637 | foreach ($vcardRow['uparams'] as $pname => $params) |
||
638 | { |
||
639 | switch ($pname) |
||
640 | { |
||
641 | case 'TYPE': |
||
642 | if (is_array($params)) |
||
643 | { |
||
644 | $rowTypes = array(); |
||
645 | foreach ($params as $param) |
||
646 | { |
||
647 | $rowTypes[] = strtoupper($param); |
||
648 | } |
||
649 | } |
||
650 | else |
||
651 | { |
||
652 | $rowTypes[] = strtoupper($params); |
||
653 | } |
||
654 | foreach ($rowTypes as $type) |
||
655 | { |
||
656 | switch ($type) |
||
657 | { |
||
658 | |||
659 | case 'OTHER': |
||
660 | case 'WORK': |
||
661 | case 'HOME': |
||
662 | $vcardRow['tparams'][$type] = ''; |
||
663 | break; |
||
664 | case 'CELL': |
||
665 | case 'PAGER': |
||
666 | case 'FAX': |
||
667 | case 'VOICE': |
||
668 | case 'CAR': |
||
669 | case 'PREF': |
||
670 | case 'X-CUSTOMLABEL-CAR': |
||
671 | case 'X-CUSTOMLABEL-IPHONE': |
||
672 | case 'IPHONE': |
||
673 | if ($vcardRow['name'] == 'TEL') |
||
674 | { |
||
675 | $vcardRow['tparams'][$type] = ''; |
||
676 | } |
||
677 | default: |
||
678 | break; |
||
679 | } |
||
680 | } |
||
681 | break; |
||
682 | default: |
||
683 | break; |
||
684 | } |
||
685 | } |
||
686 | |||
687 | $vcardRow['uparams'] += $vcardRow['tparams']; |
||
688 | ksort($vcardRow['uparams']); |
||
689 | |||
690 | foreach ($vcardRow['uparams'] as $pname => $params) |
||
691 | { |
||
692 | switch ($pname) |
||
693 | { |
||
694 | case 'PREF': |
||
695 | if (substr($rowName,0,3) == 'TEL' && !$pref_tel) |
||
696 | { |
||
697 | $pref_tel = $key; |
||
698 | } |
||
699 | break; |
||
700 | case 'FAX': |
||
701 | case 'PAGER': |
||
702 | case 'VOICE': |
||
703 | case 'OTHER': |
||
704 | case 'CELL': |
||
705 | if ($rowName != 'TEL') break; |
||
706 | case 'WORK': |
||
707 | case 'HOME': |
||
708 | $rowName .= ';' . $pname; |
||
709 | break; |
||
710 | case 'CAR': |
||
711 | case 'X-CUSTOMLABEL-CAR': |
||
712 | if ($rowName == 'TEL') |
||
713 | { |
||
714 | $rowName = 'TEL;CAR'; |
||
715 | } |
||
716 | break; |
||
717 | case 'X-CUSTOMLABEL-IPHONE': |
||
718 | case 'IPHONE': |
||
719 | if ($rowName == 'TEL' || $rowName == 'TEL;CELL') |
||
720 | { |
||
721 | $rowName = 'TEL;CELL;HOME'; |
||
722 | } |
||
723 | break; |
||
724 | default: |
||
725 | if (strpos($pname, 'X-FUNAMBOL-') === 0) |
||
726 | { |
||
727 | // Propriatary Funambol extension will be ignored |
||
728 | $rowName .= ';' . $pname; |
||
729 | } |
||
730 | break; |
||
731 | } |
||
732 | } |
||
733 | |||
734 | if ($rowName == 'EMAIL') |
||
735 | { |
||
736 | $rowName .= ';X-egw-Ref' . $email++; |
||
737 | } |
||
738 | |||
739 | if (($rowName == 'TEL;CELL') || |
||
740 | ($rowName == 'TEL;CELL;VOICE')) |
||
741 | { |
||
742 | $rowName = 'TEL;CELL;X-egw-Ref' . $cell++; |
||
743 | } |
||
744 | |||
745 | if (($rowName == 'TEL') || |
||
746 | ($rowName == 'TEL;VOICE')) |
||
747 | { |
||
748 | $rowName = 'TEL;X-egw-Ref' . $tel++; |
||
749 | } |
||
750 | |||
751 | if ($rowName == 'URL') |
||
752 | { |
||
753 | $rowName = 'URL;X-egw-Ref' . $url++; |
||
754 | } |
||
755 | |||
756 | // current algorithm cant cope with multiple attributes of same name |
||
757 | // --> cumulate them in values, so they can be used later (works only for values, not for parameters!) |
||
758 | if (($k = array_search($rowName, $rowNames)) != false) |
||
759 | { |
||
760 | $vcardValues[$k]['values'] = array_merge($vcardValues[$k]['values'],$vcardValues[$key]['values']); |
||
761 | } |
||
762 | $rowNames[$key] = $rowName; |
||
763 | } |
||
764 | |||
765 | if ($this->log) |
||
766 | { |
||
767 | error_log(__FILE__.'['.__LINE__.'] '.__METHOD__."()\n" . |
||
768 | array2string($rowNames)."\n",3,$this->logfile); |
||
769 | } |
||
770 | |||
771 | // All rowNames of the vCard are now concatenated with their qualifiers. |
||
772 | // If qualifiers are missing we apply a default strategy. |
||
773 | // E.g. ADR will be either ADR;WORK, if no ADR;WORK is given, |
||
774 | // or else ADR;HOME, if not available elsewhere. |
||
775 | |||
776 | $finalRowNames = array(); |
||
777 | |||
778 | foreach ($rowNames as $vcardKey => $rowName) |
||
779 | { |
||
780 | switch ($rowName) |
||
781 | { |
||
782 | case 'VERSION': |
||
783 | break; |
||
784 | case 'ADR': |
||
785 | if (!in_array('ADR;WORK', $rowNames) |
||
786 | && !isset($finalRowNames['ADR;WORK'])) |
||
787 | { |
||
788 | $finalRowNames['ADR;WORK'] = $vcardKey; |
||
789 | } |
||
790 | elseif (!in_array('ADR;HOME', $rowNames) |
||
791 | && !isset($finalRowNames['ADR;HOME'])) |
||
792 | { |
||
793 | $finalRowNames['ADR;HOME'] = $vcardKey; |
||
794 | } |
||
795 | break; |
||
796 | case 'TEL;FAX': |
||
797 | if (!in_array('TEL;FAX;WORK', $rowNames) |
||
798 | && !isset($finalRowNames['TEL;FAX;WORK'])) |
||
799 | { |
||
800 | $finalRowNames['TEL;FAX;WORK'] = $vcardKey; |
||
801 | } |
||
802 | elseif (!in_array('TEL;FAX;HOME', $rowNames) |
||
803 | && !isset($finalRowNames['TEL;FAX;HOME'])) |
||
804 | { |
||
805 | $finalRowNames['TEL;FAX;HOME'] = $vcardKey; |
||
806 | } |
||
807 | break; |
||
808 | case 'TEL;WORK': |
||
809 | if (!in_array('TEL;VOICE;WORK', $rowNames) |
||
810 | && !isset($finalRowNames['TEL;VOICE;WORK'])) |
||
811 | { |
||
812 | $finalRowNames['TEL;VOICE;WORK'] = $vcardKey; |
||
813 | } |
||
814 | break; |
||
815 | case 'TEL;HOME': |
||
816 | if (!in_array('TEL;HOME;VOICE', $rowNames) |
||
817 | && !isset($finalRowNames['TEL;HOME;VOICE'])) |
||
818 | { |
||
819 | $finalRowNames['TEL;HOME;VOICE'] = $vcardKey; |
||
820 | } |
||
821 | break; |
||
822 | case 'TEL;OTHER;VOICE': |
||
823 | if (!in_array('TEL;OTHER', $rowNames) |
||
824 | && !isset($finalRowNames['TEL;OTHER'])) |
||
825 | { |
||
826 | $finalRowNames['TEL;OTHER'] = $vcardKey; |
||
827 | } |
||
828 | break; |
||
829 | case 'TEL;PAGER;WORK': |
||
830 | case 'TEL;PAGER;HOME': |
||
831 | if (!in_array('TEL;PAGER', $rowNames) |
||
832 | && !isset($finalRowNames['TEL;PAGER'])) |
||
833 | { |
||
834 | $finalRowNames['TEL;PAGER'] = $vcardKey; |
||
835 | } |
||
836 | break; |
||
837 | case 'TEL;CAR;VOICE': |
||
838 | case 'TEL;CAR;CELL': |
||
839 | case 'TEL;CAR;CELL;VOICE': |
||
840 | if (!isset($finalRowNames['TEL;CAR'])) |
||
841 | { |
||
842 | $finalRowNames['TEL;CAR'] = $vcardKey; |
||
843 | } |
||
844 | break; |
||
845 | case 'TEL;X-egw-Ref1': |
||
846 | if (!in_array('TEL;VOICE;WORK', $rowNames) |
||
847 | && !in_array('TEL;WORK', $rowNames) |
||
848 | && !isset($finalRowNames['TEL;VOICE;WORK'])) |
||
849 | { |
||
850 | $finalRowNames['TEL;VOICE;WORK'] = $vcardKey; |
||
851 | break; |
||
852 | } |
||
853 | case 'TEL;X-egw-Ref2': |
||
854 | if (!in_array('TEL;HOME;VOICE', $rowNames) |
||
855 | && !in_array('TEL;HOME', $rowNames) |
||
856 | && !isset($finalRowNames['TEL;HOME;VOICE'])) |
||
857 | { |
||
858 | $finalRowNames['TEL;HOME;VOICE'] = $vcardKey; |
||
859 | break; |
||
860 | } |
||
861 | case 'TEL;X-egw-Ref3': |
||
862 | if (!in_array('TEL;OTHER', $rowNames) |
||
863 | && !in_array('TEL;OTHER;VOICE', $rowNames) |
||
864 | && !isset($finalRowNames['TEL;OTHER'])) |
||
865 | { |
||
866 | $finalRowNames['TEL;OTHER'] = $vcardKey; |
||
867 | } |
||
868 | break; |
||
869 | case 'TEL;CELL;X-egw-Ref1': |
||
870 | if (!in_array('TEL;CELL;WORK', $rowNames) |
||
871 | && !isset($finalRowNames['TEL;CELL;WORK'])) |
||
872 | { |
||
873 | $finalRowNames['TEL;CELL;WORK'] = $vcardKey; |
||
874 | break; |
||
875 | } |
||
876 | case 'TEL;CELL;X-egw-Ref2': |
||
877 | if (!in_array('TEL;CELL;HOME', $rowNames) |
||
878 | && !isset($finalRowNames['TEL;CELL;HOME'])) |
||
879 | { |
||
880 | $finalRowNames['TEL;CELL;HOME'] = $vcardKey; |
||
881 | break; |
||
882 | } |
||
883 | case 'TEL;CELL;X-egw-Ref3': |
||
884 | if (!in_array('TEL;CAR', $rowNames) |
||
885 | && !in_array('TEL;CAR;VOICE', $rowNames) |
||
886 | && !in_array('TEL;CAR;CELL', $rowNames) |
||
887 | && !in_array('TEL;CAR;CELL;VOICE', $rowNames) |
||
888 | && !isset($finalRowNames['TEL;CAR'])) |
||
889 | { |
||
890 | $finalRowNames['TEL;CAR'] = $vcardKey; |
||
891 | } |
||
892 | break; |
||
893 | case 'EMAIL;X-egw-Ref1': |
||
894 | if (!in_array('EMAIL;WORK', $rowNames) && |
||
895 | !isset($finalRowNames['EMAIL;WORK'])) |
||
896 | { |
||
897 | $finalRowNames['EMAIL;WORK'] = $vcardKey; |
||
898 | break; |
||
899 | } |
||
900 | case 'EMAIL;X-egw-Ref2': |
||
901 | if (!in_array('EMAIL;HOME', $rowNames) && |
||
902 | !isset($finalRowNames['EMAIL;HOME'])) |
||
903 | { |
||
904 | $finalRowNames['EMAIL;HOME'] = $vcardKey; |
||
905 | } |
||
906 | break; |
||
907 | case 'URL;X-egw-Ref1': |
||
908 | if (!in_array('URL;WORK', $rowNames) && |
||
909 | !isset($finalRowNames['URL;WORK'])) |
||
910 | { |
||
911 | $finalRowNames['URL;WORK'] = $vcardKey; |
||
912 | break; |
||
913 | } |
||
914 | case 'URL;X-egw-Ref2': |
||
915 | if (!in_array('URL;HOME', $rowNames) && |
||
916 | !isset($finalRowNames['URL;HOME'])) |
||
917 | { |
||
918 | $finalRowNames['URL;HOME'] = $vcardKey; |
||
919 | } |
||
920 | break; |
||
921 | case 'X-EVOLUTION-ASSISTANT': |
||
922 | if (!isset($finalRowNames['X-ASSISTANT'])) |
||
923 | { |
||
924 | $finalRowNames['X-ASSISTANT'] = $vcardKey; |
||
925 | } |
||
926 | break; |
||
927 | default: |
||
928 | if (!isset($finalRowNames[$rowName])) |
||
929 | { |
||
930 | $finalRowNames[$rowName] = $vcardKey; |
||
931 | } |
||
932 | break; |
||
933 | } |
||
934 | } |
||
935 | |||
936 | if ($this->log) |
||
937 | { |
||
938 | error_log(__FILE__.'['.__LINE__.'] '.__METHOD__."()\n" . |
||
939 | array2string($finalRowNames)."\n",3,$this->logfile); |
||
940 | } |
||
941 | |||
942 | $contact = array(); |
||
943 | // to be able to delete fields, we have to set all supported fields to at least null |
||
944 | foreach($this->supportedFields as $fields) |
||
945 | { |
||
946 | foreach($fields as $field) |
||
947 | { |
||
948 | if ($field != 'fileas_type') $contact[$field] = null; |
||
949 | } |
||
950 | } |
||
951 | |||
952 | foreach ($finalRowNames as $key => $vcardKey) |
||
953 | { |
||
954 | if (isset($this->databaseFields[$key])) |
||
955 | { |
||
956 | $fieldNames = $this->databaseFields[$key]; |
||
957 | foreach ($fieldNames as $fieldKey => $fieldName) |
||
958 | { |
||
959 | if (!empty($fieldName)) |
||
960 | { |
||
961 | $value = trim($vcardValues[$vcardKey]['values'][$fieldKey]); |
||
962 | |||
963 | if ($pref_tel && (($vcardKey == $pref_tel) || |
||
964 | ($vcardValues[$vcardKey]['name'] == 'TEL') && |
||
965 | ($vcardValues[$vcardKey]['value'] == $vcardValues[$pref_tel]['value']))) |
||
966 | { |
||
967 | $contact['tel_prefer'] = $fieldName; |
||
968 | } |
||
969 | switch($fieldName) |
||
970 | { |
||
971 | case 'bday': |
||
972 | $contact[$fieldName] = $vcardValues[$vcardKey]['value']['year'] . |
||
973 | '-' . $vcardValues[$vcardKey]['value']['month'] . |
||
974 | '-' . $vcardValues[$vcardKey]['value']['mday']; |
||
975 | break; |
||
976 | |||
977 | case 'private': |
||
978 | $contact[$fieldName] = (int) ( strtoupper($value) == 'PRIVATE'); |
||
979 | break; |
||
980 | |||
981 | case 'cat_id': |
||
982 | $contact[$fieldName] = $vcardValues[$vcardKey]['values']; |
||
983 | break; |
||
984 | |||
985 | case 'jpegphoto': |
||
986 | $contact[$fieldName] = $vcardValues[$vcardKey]['value']; |
||
987 | if(in_array($vcardValues[$vcardKey]['params']['ENCODING'],array('b','B','BASE64'))) |
||
988 | { |
||
989 | $contact[$fieldName] = base64_decode($contact[$fieldName]); |
||
990 | } |
||
991 | break; |
||
992 | |||
993 | case 'pubkey': |
||
994 | $content = $vcardValues[$vcardKey]['value']; |
||
995 | if(in_array($vcardValues[$vcardKey]['params']['ENCODING'],array('b','B','BASE64'))) |
||
996 | { |
||
997 | $content = base64_decode($content); |
||
998 | } |
||
999 | if ($vcardValues[$vcardKey]['params']['ENCODING'] === 'SMIME') |
||
1000 | { |
||
1001 | // ignore re-importing of S/Mime pubkey for now, as we might be called for a new contact |
||
1002 | continue; |
||
1003 | } |
||
1004 | break; |
||
1005 | |||
1006 | case 'note': |
||
1007 | $contact[$fieldName] = str_replace("\r\n", "\n", $vcardValues[$vcardKey]['value']); |
||
1008 | break; |
||
1009 | |||
1010 | case 'fileas_type': |
||
1011 | // store Apple's X-ABSHOWAS:COMPANY as fileas_type == 'org_name' |
||
1012 | if ($vcardValues[$vcardKey]['value'] == 'COMPANY') |
||
1013 | { |
||
1014 | $contact[$fieldName] = 'org_name'; |
||
1015 | } |
||
1016 | break; |
||
1017 | |||
1018 | case 'uid': |
||
1019 | if (strlen($value) < $minimum_uid_length) { |
||
1020 | // we don't use it |
||
1021 | break; |
||
1022 | } |
||
1023 | default: |
||
1024 | $contact[$fieldName] = $value; |
||
1025 | break; |
||
1026 | } |
||
1027 | } |
||
1028 | } |
||
1029 | } |
||
1030 | // add unsupported attributes as with '##' prefix |
||
1031 | elseif(($attribute = $vcardValues[$vcardKey]) && !in_array($attribute['name'],array('PRODID','REV'))) |
||
1032 | { |
||
1033 | // for attributes with multiple values in multiple lines, merge the values |
||
1034 | if (isset($contact['##'.$attribute['name']])) |
||
1035 | { |
||
1036 | error_log(__METHOD__."() contact['##$attribute[name]'] = ".array2string($contact['##'.$attribute['name']])); |
||
1037 | $attribute['values'] = array_merge( |
||
1038 | is_array($contact['##'.$attribute['name']]) ? $contact['##'.$attribute['name']]['values'] : (array)$contact['##'.$attribute['name']], |
||
1039 | $attribute['values']); |
||
1040 | } |
||
1041 | $contact['##'.$attribute['name']] = $attribute['params'] || count($attribute['values']) > 1 ? |
||
1042 | serialize($attribute) : $attribute['value']; |
||
1043 | } |
||
1044 | } |
||
1045 | |||
1046 | $this->fixup_contact($contact); |
||
1047 | |||
1048 | if ($this->log) |
||
1049 | { |
||
1050 | error_log(__FILE__.'['.__LINE__.'] '.__METHOD__ . |
||
1051 | "() '$this->productManufacturer','$this->productName'\n",3,$this->logfile); |
||
1052 | error_log(__FILE__.'['.__LINE__.'] '.__METHOD__."()\n" . |
||
1053 | array2string($contact)."\n",3,$this->logfile); |
||
1054 | } |
||
1055 | return $contact; |
||
1056 | } |
||
1123 |