| 1 | <?php |
| 2 | /*************************************************************************** |
| 3 | * Copyright (C) 2003-2009 Polytechnique.org * |
| 4 | * http://opensource.polytechnique.org/ * |
| 5 | * * |
| 6 | * This program is free software; you can redistribute it and/or modify * |
| 7 | * it under the terms of the GNU General Public License as published by * |
| 8 | * the Free Software Foundation; either version 2 of the License, or * |
| 9 | * (at your option) any later version. * |
| 10 | * * |
| 11 | * This program is distributed in the hope that it will be useful, * |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 | * GNU General Public License for more details. * |
| 15 | * * |
| 16 | * You should have received a copy of the GNU General Public License * |
| 17 | * along with this program; if not, write to the Free Software * |
| 18 | * Foundation, Inc., * |
| 19 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * |
| 20 | ***************************************************************************/ |
| 21 | |
| 22 | |
| 23 | require_once('applis.func.inc.php'); |
| 24 | |
| 25 | function replace_ifset(&$var,$req) { |
| 26 | if (Env::has($req)){ |
| 27 | $var = Env::v($req); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | function replace_ifset_i(&$var,$req,$i) { |
| 32 | if (isset($_REQUEST[$req][$i])){ |
| 33 | $var[$i] = $_REQUEST[$req][$i]; |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | function replace_ifset_i_j(&$var,$req,$i,$j) { |
| 38 | if (isset($_REQUEST[$req][$j])){ |
| 39 | $var[$i] = $_REQUEST[$req][$j]; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | //pour rentrer qqchose dans la base |
| 44 | function put_in_db($string){ |
| 45 | return trim($string); |
| 46 | } |
| 47 | |
| 48 | // example of use for diff_user_details : get $b from database, $a from other site |
| 49 | // calculate diff $c and add $c in database (with set_user_details) |
| 50 | function diff_user_details(&$a, &$b, $view = 'private') { // compute $c = $a - $b |
| 51 | // if (!isset($b) || !$b || !is_array($b) || count($b) == 0) |
| 52 | // return $a; |
| 53 | // if (!isset($a) || !$a || !is_array($a)) |
| 54 | // $c = array(); |
| 55 | // else |
| 56 | $c = $a; |
| 57 | foreach ($b as $val => $bvar) { |
| 58 | if (isset($a[$val])) { |
| 59 | if ($a[$val] == $bvar) |
| 60 | unset($c[$val]); |
| 61 | else { |
| 62 | switch ($val) { |
| 63 | case 'adr' : if (!($c['adr'] = diff_user_addresses($a[$val], $bvar, $view))) unset($c['adr']); break; |
| 64 | case 'adr_pro' : if (!($c['adr_pro'] = diff_user_pros($a[$val], $bvar, $view))) unset($c['adr_pro']); break; |
| 65 | case 'mobile' : if (same_tel($a[$val], $bvar)) unset($c['mobile']); break; |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | // don't modify mobile if you don't have the right |
| 71 | if (isset($b['mobile_pub']) && !has_user_right($b['mobile_pub'], $view) && isset($c['mobile'])) |
| 72 | unset($c['mobile']); |
| 73 | if (isset($b['web_pub']) && !has_user_right($b['web_pub'], $view) && isset($c['web'])) |
| 74 | unset($c['web']); |
| 75 | if (isset($b['freetext_pub']) && !has_user_right($b['freetext_pub'], $view) && isset($c['freetext'])) |
| 76 | unset($c['freetext']); |
| 77 | if (!count($c)) |
| 78 | return false; |
| 79 | return $c; |
| 80 | } |
| 81 | |
| 82 | function same_tel(&$a, &$b) { |
| 83 | $numbera = preg_replace('/[^0-9]/', '', (string) $a); |
| 84 | $numberb = preg_replace('/[^0-9]/', '', (string) $b); |
| 85 | return $numbera === $numberb; |
| 86 | } |
| 87 | function same_address(&$a, &$b) { |
| 88 | return |
| 89 | (same_field($a['adr1'],$b['adr1'])) && |
| 90 | (same_field($a['adr1'],$b['adr1'])) && |
| 91 | (same_field($a['adr1'],$b['adr1'])) && |
| 92 | (same_field($a['postcode'],$b['postcode'])) && |
| 93 | (same_field($a['city'],$b['city'])) && |
| 94 | (same_field($a['countrytxt'],$b['countrytxt'])) && |
| 95 | true; |
| 96 | } |
| 97 | function same_pro(&$a, &$b) { |
| 98 | return |
| 99 | (same_field($a['entreprise'],$b['entreprise'])) && |
| 100 | (same_field($a['fonction'],$b['fonction'])) && |
| 101 | true; |
| 102 | } |
| 103 | function same_field(&$a, &$b) { |
| 104 | if ($a == $b) return true; |
| 105 | if (is_array($a)) { |
| 106 | if (!is_array($b) || count($a) != count($b)) return false; |
| 107 | foreach ($a as $val => $avar) |
| 108 | if (!isset($b[$val]) || !same_field($avar, $b[$val])) return false; |
| 109 | return true; |
| 110 | } elseif (is_string($a)) |
| 111 | return (strtoupper($a) == strtoupper($b)); |
| 112 | } |
| 113 | function diff_user_tel(&$a, &$b) { |
| 114 | $c = $a; |
| 115 | if (isset($b['tel_pub']) && isset($a['tel_pub']) && has_user_right($b['tel_pub'], $a['tel_pub'])) |
| 116 | $c['tel_pub'] = $b['tel_pub']; |
| 117 | foreach ($b as $val => $bvar) { |
| 118 | if (isset($a[$val])) { |
| 119 | if ($a[$val] == $bvar) |
| 120 | unset($c[$val]); |
| 121 | } |
| 122 | } |
| 123 | if (!count($c)) |
| 124 | return false; |
| 125 | $c['telid'] = $a['telid']; |
| 126 | return $c; |
| 127 | } |
| 128 | |
| 129 | function diff_user_address($a, $b) { |
| 130 | if (isset($b['pub']) && isset($a['pub']) && has_user_right($b['pub'], $a['pub'])) |
| 131 | $a['pub'] = $b['pub']; |
| 132 | if (isset($b['tels'])) { |
| 133 | $bvar = $b['tels']; |
| 134 | |
| 135 | $telids_b = array(); |
| 136 | foreach ($bvar as $i => $telb) $telids_b[$telb['telid']] = $i; |
| 137 | |
| 138 | if (isset($a['tels'])) |
| 139 | $avar = $a['tels']; |
| 140 | else |
| 141 | $avar = array(); |
| 142 | $ctels = $avar; |
| 143 | foreach ($avar as $j => $tela) { |
| 144 | if (isset($tela['telid'])) { |
| 145 | // if b has a tel with the same telid, compute diff |
| 146 | if (isset($telids_b[$tela['telid']])) { |
| 147 | if (!($ctels[$j] = diff_user_tel($tela, $varb[$telids_b[$tela['adrid']]]))) |
| 148 | unset($ctels[$j]); |
| 149 | unset($telids_b[$tela['telid']]); |
| 150 | } |
| 151 | } else { |
| 152 | // try to find a match in b |
| 153 | foreach ($bvar as $i => $telb) { |
| 154 | if (same_tel($tela['tel'], $telb['tel'])) { |
| 155 | $tela['telid'] = $telb['telid']; |
| 156 | if (!($ctels[$j] = diff_user_tel($tela, $telb))) |
| 157 | unset($ctels[$j]); |
| 158 | unset($telids_b[$tela['telid']]); |
| 159 | break; |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | foreach ($telids_b as $telidb => $i) |
| 166 | $ctels[] = array('telid' => $telidb, 'remove' => 1); |
| 167 | |
| 168 | if (!count($ctels)) { |
| 169 | $b['tels'] = $avar; |
| 170 | } else |
| 171 | $a['tels'] = $ctels; |
| 172 | } |
| 173 | |
| 174 | foreach ($a as $val => $avar) { |
| 175 | if (!isset($b[$val]) || !same_field($avar,$b[$val])) { |
| 176 | return $a; |
| 177 | } |
| 178 | } |
| 179 | return false; |
| 180 | } |
| 181 | |
| 182 | // $b need to use adrids |
| 183 | function diff_user_addresses(&$a, &$b) { |
| 184 | $c = $a; |
| 185 | $adrids_b = array(); |
| 186 | foreach ($b as $i => $adrb) $adrids_b[$adrb['adrid']] = $i; |
| 187 | |
| 188 | foreach ($a as $j => $adra) { |
| 189 | if (isset($adra['adrid'])) { |
| 190 | // if b has an address with the same adrid, compute diff |
| 191 | if (isset($adrids_b[$adra['adrid']])) { |
| 192 | if (!($c[$j] = diff_user_address($adra, $b[$adrids_b[$adra['adrid']]]))) |
| 193 | unset($c[$j]); |
| 194 | unset($adrids_b[$adra['adrid']]); |
| 195 | } |
| 196 | } else { |
| 197 | // try to find a match in b |
| 198 | foreach ($b as $i => $adrb) { |
| 199 | if (same_address($adra, $adrb)) { |
| 200 | $adra['adrid'] = $adrb['adrid']; |
| 201 | if (!($c[$j] = diff_user_address($adra, $adrb))) |
| 202 | unset($c[$j]); |
| 203 | if ($c[$j]) $c[$j]['adrid'] = $adra['adrid']; |
| 204 | unset($adrids_b[$adra['adrid']]); |
| 205 | break; |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | foreach ($adrids_b as $adridb => $i) |
| 212 | $c[] = array('adrid' => $adridb, 'remove' => 1); |
| 213 | |
| 214 | if (!count($c)) return false; |
| 215 | return $c; |
| 216 | } |
| 217 | |
| 218 | function diff_user_pro($a, &$b, $view = 'private') { |
| 219 | if (isset($b['pub']) && isset($a['pub']) && has_user_right($b['pub'], $a['pub'])) |
| 220 | $a['pub'] = $b['pub']; |
| 221 | if (isset($b['adr_pub']) && !has_user_right($b['adr_pub'], $view)) { |
| 222 | unset($a['adr1']); |
| 223 | unset($a['adr2']); |
| 224 | unset($a['adr3']); |
| 225 | unset($a['postcode']); |
| 226 | unset($a['city']); |
| 227 | unset($a['countrytxt']); |
| 228 | unset($a['region']); |
| 229 | } |
| 230 | if (isset($b['adr_pub']) && isset($a['adr_pub']) && has_user_right($b['adr_pub'], $a['adr_pub'])) |
| 231 | $a['adr_pub'] = $b['adr_pub']; |
| 232 | if (isset($b['tel_pub']) && !has_user_right($b['tel_pub'], $view)) { |
| 233 | unset($a['tel']); |
| 234 | unset($a['fax']); |
| 235 | unset($a['mobile']); |
| 236 | } |
| 237 | if (isset($b['tel_pub']) && isset($a['tel_pub']) && has_user_right($b['tel_pub'], $a['tel_pub'])) |
| 238 | $a['tel_pub'] = $b['tel_pub']; |
| 239 | if (isset($b['email_pub']) && !has_user_right($b['email_pub'], $view)) |
| 240 | unset($a['email']); |
| 241 | if (isset($b['email_pub']) && isset($a['email_pub']) && has_user_right($b['email_pub'], $a['email_pub'])) |
| 242 | $a['email_pub'] = $b['email_pub']; |
| 243 | foreach ($a as $val => $avar) { |
| 244 | if (($avar && !isset($b[$val])) || !same_field($avar,$b[$val])) { |
| 245 | return $a; |
| 246 | } |
| 247 | } |
| 248 | return false; |
| 249 | } |
| 250 | |
| 251 | // $b need to use entrids |
| 252 | function diff_user_pros(&$a, &$b, $view = 'private') { |
| 253 | $c = $a; |
| 254 | $entrids_b = array(); |
| 255 | foreach ($b as $i => $prob) $entrids_b[$prob['entrid']] = $i; |
| 256 | |
| 257 | foreach ($a as $j => $proa) { |
| 258 | if (isset($proa['entrid'])) { |
| 259 | // if b has an address with the same adrid, compute diff |
| 260 | if (isset($entrids_b[$proa['entrid']])) { |
| 261 | if (!($c[$j] = diff_user_pro($proa, $b[$entrids_b[$proa['entrid']]], $view))) |
| 262 | unset($c[$j]); |
| 263 | unset($entrids_b[$proa['entrid']]); |
| 264 | } |
| 265 | } else { |
| 266 | // try to find a match in b |
| 267 | foreach ($b as $i => $prob) { |
| 268 | if (same_pro($proa, $prob)) { |
| 269 | $proa['entrid'] = $prob['entrid']; |
| 270 | if (!($c[$j] = diff_user_pro($proa, $prob, $view))) |
| 271 | unset($c[$j]); |
| 272 | if ($c[$j]) $c[$j]['entrid'] = $proa['entrid']; |
| 273 | unset($entrids_b[$proa['entrid']]); |
| 274 | break; |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | foreach ($entrids_b as $entridb => $i) |
| 281 | $c[] = array('entrid' => $entridb, 'remove' => 1); |
| 282 | |
| 283 | if (!count($c)) return false; |
| 284 | return $c; |
| 285 | } |
| 286 | |
| 287 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
| 288 | ?> |