phone.ua.inc 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. <?php
  2. /**
  3. * @file
  4. * CCK Field for Ukrainian phone numbers.
  5. */
  6. /**
  7. * Helper function knows all valid country codes.
  8. *
  9. * @return array of stings for use in preg_match* fuunctions.
  10. *
  11. * Return PREG string elements in array like "44\d{7}|278\d{6}" which will match
  12. * all nine-digit valid numbers.
  13. * Please note - leading zero stripped for both mobile and landline.
  14. *
  15. * For performance reason there are a set of strings instead of single very long one.
  16. */
  17. function _phone_ua_get_valid_masks() {
  18. $result = cache_get('phone' . __FUNCTION__);
  19. if( !empty($result) ) {
  20. return $result->data;
  21. }
  22. $mobiles = array(
  23. "39",
  24. "50",
  25. "63",
  26. "66",
  27. "67",
  28. "68",
  29. "91",
  30. "92",
  31. "93",
  32. "94",
  33. "95",
  34. "96",
  35. "97",
  36. "98",
  37. "99",
  38. );
  39. $landlines = array(
  40. "255",
  41. "260",
  42. "263",
  43. "265",
  44. "268",
  45. "269",
  46. "271",
  47. "272",
  48. "278",
  49. "312",
  50. "3131",
  51. "3132",
  52. "3133",
  53. "3134",
  54. "3135",
  55. "3136",
  56. "3137",
  57. "3141",
  58. "3142",
  59. "3143",
  60. "3144",
  61. "3145",
  62. "3146",
  63. "322",
  64. "3230",
  65. "3231",
  66. "3234",
  67. "3236",
  68. "3238",
  69. "3239",
  70. "3241",
  71. "3244",
  72. "3245",
  73. "3247",
  74. "3248",
  75. "3249",
  76. "3251",
  77. "3252",
  78. "3254",
  79. "3255",
  80. "3256",
  81. "3257",
  82. "3259",
  83. "32606",
  84. "3261",
  85. "3263",
  86. "3264",
  87. "3265",
  88. "3266",
  89. "3269",
  90. "3310",
  91. "3322",
  92. "3342",
  93. "3344",
  94. "3346",
  95. "3348",
  96. "3352",
  97. "3355",
  98. "3357",
  99. "3362",
  100. "3363",
  101. "3365",
  102. "3366",
  103. "3368",
  104. "3372",
  105. "3374",
  106. "3376",
  107. "3377",
  108. "3379",
  109. "342",
  110. "3430",
  111. "3431",
  112. "3432",
  113. "3433",
  114. "3434",
  115. "3435",
  116. "3436",
  117. "3438",
  118. "3471",
  119. "3472",
  120. "3474",
  121. "3475",
  122. "3476",
  123. "3477",
  124. "3478",
  125. "3479",
  126. "352",
  127. "3540",
  128. "3541",
  129. "3542",
  130. "3543",
  131. "3544",
  132. "3546",
  133. "3547",
  134. "3548",
  135. "3549",
  136. "3550",
  137. "3551",
  138. "3552",
  139. "3554",
  140. "3555",
  141. "3557",
  142. "3558",
  143. "362",
  144. "3632",
  145. "3633",
  146. "3634",
  147. "3635",
  148. "3636",
  149. "3637",
  150. "3650",
  151. "3651",
  152. "3652",
  153. "3653",
  154. "3654",
  155. "3655",
  156. "3656",
  157. "3657",
  158. "3658",
  159. "3659",
  160. "372",
  161. "373",
  162. "3740",
  163. "3741",
  164. "382",
  165. "384",
  166. "385",
  167. "412",
  168. "4130",
  169. "4132",
  170. "4133",
  171. "4134",
  172. "4135",
  173. "4136",
  174. "4137",
  175. "4138",
  176. "4139",
  177. "414",
  178. "4161",
  179. "4162",
  180. "4231",
  181. "4232",
  182. "4241",
  183. "432",
  184. "4330",
  185. "4331",
  186. "4332",
  187. "4333",
  188. "4334",
  189. "4335",
  190. "4336",
  191. "4337",
  192. "43388",
  193. "4340",
  194. "4341",
  195. "4342",
  196. "4343",
  197. "4344",
  198. "4345",
  199. "4346",
  200. "4347",
  201. "4348",
  202. "4349",
  203. "4350",
  204. "4351",
  205. "4352",
  206. "4353",
  207. "4355",
  208. "4356",
  209. "4357",
  210. "4358",
  211. "44",
  212. "462",
  213. "4631",
  214. "4632",
  215. "4633",
  216. "4634",
  217. "4635",
  218. "4636",
  219. "4637",
  220. "4641",
  221. "4642",
  222. "4643",
  223. "4644",
  224. "4645",
  225. "4646",
  226. "4653",
  227. "4654",
  228. "4655",
  229. "4656",
  230. "4657",
  231. "4658",
  232. "4659",
  233. "472",
  234. "473",
  235. "4740",
  236. "4741",
  237. "4742",
  238. "4744",
  239. "4745",
  240. "4746",
  241. "4747",
  242. "4748",
  243. "4749",
  244. "482",
  245. "484",
  246. "4851",
  247. "4852",
  248. "4853",
  249. "4854",
  250. "4855",
  251. "4856",
  252. "4857",
  253. "4858",
  254. "4859",
  255. "4860",
  256. "4861",
  257. "4862",
  258. "4863",
  259. "4864",
  260. "4865",
  261. "4866",
  262. "4867",
  263. "4868",
  264. "512",
  265. "5131",
  266. "5132",
  267. "5133",
  268. "5134",
  269. "5135",
  270. "5136",
  271. "5151",
  272. "5152",
  273. "5153",
  274. "5154",
  275. "5158",
  276. "5159",
  277. "5161",
  278. "5162",
  279. "5163",
  280. "5167",
  281. "5168",
  282. "522",
  283. "5233",
  284. "5234",
  285. "5235",
  286. "5236",
  287. "5237",
  288. "5238",
  289. "5239",
  290. "5240",
  291. "5241",
  292. "5242",
  293. "5250",
  294. "5251",
  295. "5252",
  296. "5253",
  297. "5254",
  298. "5256",
  299. "5257",
  300. "5258",
  301. "5259",
  302. "532",
  303. "5340",
  304. "5341",
  305. "5342",
  306. "5343",
  307. "5344",
  308. "5345",
  309. "5346",
  310. "5347",
  311. "5348",
  312. "5350",
  313. "5351",
  314. "5352",
  315. "5353",
  316. "5354",
  317. "5355",
  318. "5356",
  319. "5357",
  320. "5358",
  321. "5359",
  322. "536",
  323. "5361",
  324. "5362",
  325. "5363",
  326. "5364",
  327. "5365",
  328. "5366",
  329. "542",
  330. "5422",
  331. "5442",
  332. "5443",
  333. "5444",
  334. "5445",
  335. "5446",
  336. "5447",
  337. "5448",
  338. "5449",
  339. "5451",
  340. "5452",
  341. "5453",
  342. "5454",
  343. "5455",
  344. "5456",
  345. "5457",
  346. "5458",
  347. "5459",
  348. "552",
  349. "553",
  350. "5542",
  351. "5543",
  352. "5544",
  353. "5545",
  354. "5546",
  355. "5547",
  356. "5548",
  357. "5549",
  358. "56",
  359. "5610",
  360. "5611",
  361. "5612",
  362. "5615",
  363. "5616",
  364. "5617",
  365. "5618",
  366. "562",
  367. "5630",
  368. "5632",
  369. "5633",
  370. "5636",
  371. "5638",
  372. "5639",
  373. "564",
  374. "5650",
  375. "5651",
  376. "5652",
  377. "5653",
  378. "5655",
  379. "5656",
  380. "5657",
  381. "5658",
  382. "5662",
  383. "5663",
  384. "5665",
  385. "5667",
  386. "5668",
  387. "567",
  388. "569",
  389. "572",
  390. "574",
  391. "575",
  392. "5761",
  393. "5762",
  394. "5763",
  395. "5764",
  396. "5765",
  397. "5766",
  398. "612",
  399. "6131",
  400. "6132",
  401. "6133",
  402. "6136",
  403. "6137",
  404. "6138",
  405. "6139",
  406. "6140",
  407. "6141",
  408. "6142",
  409. "6143",
  410. "6144",
  411. "6145",
  412. "6147",
  413. "6153",
  414. "6156",
  415. "6162",
  416. "6165",
  417. "6175",
  418. "6178",
  419. "619",
  420. "6212",
  421. "6214",
  422. "6217",
  423. "622",
  424. "6232",
  425. "6236",
  426. "6237",
  427. "6239",
  428. "6242",
  429. "6243",
  430. "6244",
  431. "6246",
  432. "6247",
  433. "6249",
  434. "6250",
  435. "6252",
  436. "6253",
  437. "6254",
  438. "6255",
  439. "6256",
  440. "6257",
  441. "6259",
  442. "6261",
  443. "6262",
  444. "6264",
  445. "6267",
  446. "6269",
  447. "6272",
  448. "6273",
  449. "6274",
  450. "6275",
  451. "6277",
  452. "6278",
  453. "6279",
  454. "629",
  455. "642",
  456. "6431",
  457. "6432",
  458. "6433",
  459. "6434",
  460. "6435",
  461. "6436",
  462. "6441",
  463. "6442",
  464. "6443",
  465. "6444",
  466. "6445",
  467. "6446",
  468. "6451",
  469. "6452",
  470. "6453",
  471. "6454",
  472. "6455",
  473. "6456",
  474. "6461",
  475. "6462",
  476. "6463",
  477. "6464",
  478. "6465",
  479. "6466",
  480. "6471",
  481. "6472",
  482. "6473",
  483. "6474",
  484. "652",
  485. "654",
  486. "655",
  487. "6560",
  488. "6561",
  489. "6562",
  490. "6563",
  491. "6564",
  492. "6565",
  493. "6566",
  494. "6569",
  495. "692",
  496. );
  497. $all_codes = array_merge($mobiles, $landlines);
  498. $TOTAL_DIGITS = 9;
  499. $ITEMS_PER_LINE = 30;
  500. $counter = 0;
  501. $line = '';
  502. foreach ($all_codes as $code) {
  503. if (++$counter >= $ITEMS_PER_LINE) {
  504. $result[] = $line;
  505. $counter = 0;
  506. $line = '';
  507. }
  508. $item = $code . '\d{' . ($TOTAL_DIGITS - strlen($code)) . '}';
  509. $line .= (empty($line) ? '' : '|') . $item;
  510. }
  511. $result[] = $line;
  512. cache_set('phone' . __FUNCTION__, $result);
  513. return $result;
  514. }
  515. function phone_ua_metadata() {
  516. // These strings are translated using t() on output.
  517. return array(
  518. 'error' => '"%value" is not a valid Ukrainian mobile phone number<br />'
  519. . 'Ukrainian phone numbers should only have 10 digits staring with 0 with optional country prefix +38 .<br />'
  520. . 'And, of course, city or mobile code - a first few digits after 0 - have to be valid.',
  521. );
  522. }
  523. /**
  524. * Verifies that $phonenumber is valid
  525. *
  526. * @param string $phonenumber
  527. * @return boolean Returns boolean FALSE if the phone number is not valid.
  528. */
  529. function valid_ua_phone_number($phonenumber) {
  530. // For adressing Ukraine phones used both +38 and +380 as a prefix.
  531. // So lets clean up any spaces, pareenthesis, minus signs etc first.
  532. $cleaning_pattern = '/( |\-|\(|\))*/';
  533. $refined_phonenumber = preg_replace($cleaning_pattern, '', $phonenumber);
  534. // define regular expression
  535. $code_packs = _phone_ua_get_valid_masks();
  536. foreach ($code_packs as $codes) {
  537. // $regex = "/^((8|\+38)-?)?\s*(\(?044\)?)?-?\d{3}-?\d{2}-?\d{2}$/i";
  538. // Please note - pure 8 as a prefix is obsolete.
  539. $regex = "/^(\+38)?0(" . $codes . ")$/";
  540. if (preg_match($regex, $refined_phonenumber)) {
  541. return true;
  542. }
  543. }
  544. // return true if valid, false otherwise
  545. return false;
  546. }
  547. /**
  548. * Formatting for Unkraine Phone Numbers.
  549. *
  550. * @param string $phonenumber
  551. * @return string Returns a string containting the phone number with some formatting.
  552. */
  553. function format_ua_phone_number($phonenumber, $field) {
  554. //$phonenumber = trim($phonenumber);
  555. // do some formatting on the phone number
  556. /* ==> to be done ==> add the country code
  557. if ($field['phone_country_code']) {
  558. if ($matches[1] != "+39") {
  559. $phonenumber = "+39" . " " . $phonenumber;
  560. }
  561. }
  562. */
  563. return $phonenumber;
  564. }