action.php 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /** SMS **/
  3. //Envois d'un contact par sms
  4. Action::register('directory_send_sms',function(&$response){
  5. global $myUser,$_,$conf;
  6. if(!$myUser->connected()) throw new Exception("Vous devez être connecté",401);
  7. Plugin::need('sms/Sms');
  8. $user = User::byLogin($_['user']);
  9. if(empty($myUser->mobile))throw new Exception("Votre numéro de téléphone n'est pas renseigné");
  10. if(empty($conf->get('sms_api_url')) || empty($conf->get('sms_api_token'))) throw new Exception("Api SMS non configurée");
  11. $sms = new Sms($conf->get('sms_api_url'),$conf->get('sms_api_token'));
  12. $sms->phone = str_replace(' ','',$myUser->mobile);
  13. $card = ROOT_URL.'/action.php?action=directory_get_cardav&token='.sha1('vcard:'.$user->phone.'-'.$user->id.'-'.$user->login);
  14. $sms->message = $user->fullName().': ';
  15. $sms->message .= PHP_EOL;
  16. $sms->message .= 'fixe : '.str_replace(' ','',$user->phone);
  17. $sms->message .= PHP_EOL;
  18. $sms->message .= 'mobile : '.str_replace(' ','',$user->mobile);
  19. $sms->message .= PHP_EOL;
  20. $sms->message .= 'mail : '.$user->mail;
  21. $sms->message .= PHP_EOL;
  22. $sms->message .=' vcard: '.$card;
  23. Log::put('Envois à '.$sms->phone.' : '.$sms->message,'Sms');
  24. try{
  25. $response = $sms->send();
  26. Log::put('Envois à '.$sms->phone.' : '.json_encode($response),'Sms');
  27. $response['phone'] = $myUser->mobile;
  28. }catch(Exception $e){
  29. throw new Exception('ERREUR : '.$e->getMessage());
  30. Log::put('Envois à '.$sms->phone.' ERREUR : '.$e->getMessage(),'Sms');
  31. }
  32. });
  33. Action::register('directory_get_qr',function(&$response){
  34. global $myUser,$_,$conf;
  35. if(!$myUser->connected()) throw new Exception("Permission denied");
  36. header('Content-type:image/gif');
  37. $user = User::byLogin($_['user']);
  38. $meCard = 'MECARD:N:'.utf8_decode($user->fullName()).';ORG:Firm;TEL:'.str_replace(' ','',$user->phone).';URL:http\://;EMAIL:'.$user->mail.';;';
  39. $a = new QR($meCard);
  40. echo $a->image(7);
  41. exit();
  42. });
  43. Action::register('directory_get_cardav',function(&$response){
  44. global $myUser,$_,$conf;
  45. if(!$myUser->connected()) throw new Exception("Permission denied");
  46. if(isset($_['token'])){
  47. foreach (User::getAll() as $user) {
  48. if(sha1('vcard:'.$user->phone.'-'.$user->id.'-'.$user->login) == $_['user']) $target = $user;
  49. }
  50. }
  51. if(isset($_['user'])) $user = User::byLogin($_['user']);
  52. if(isset($target)) $user = $target;
  53. if(empty($user)) throw new Exception("Utilisateur introuvable");
  54. header('Content-Type: text/x-vCard');
  55. header('Content-Disposition: attachment; filename= "'.$user->login.'.vcf"');
  56. $vCard = 'BEGIN:VCARD'.PHP_EOL;
  57. $vCard .= 'VERSION:3.0'.PHP_EOL;
  58. $vCard .= 'N:'.utf8_decode($user->name.';'.$user->firstname).PHP_EOL;
  59. $vCard .= 'ORG:Firm'.PHP_EOL;
  60. $vCard .= 'ADR;TYPE=WORK:;;122 Avenue Saint Emilion;Martignas;Aquitaine;30127;France'.PHP_EOL;
  61. $vCard .= 'LABEL;TYPE=WORK:122 Avenue Saint Emilion;\nMartignas, Aquitaine 30127\nFrance'.PHP_EOL;
  62. $vCard .= 'TEL;CELL:'.str_replace(' ','',$user->phone).PHP_EOL;
  63. $vCard .= 'URL:'.PHP_EOL;
  64. $vCard .= 'EMAIL:'.utf8_decode($user->mail).PHP_EOL;
  65. $vCard .= 'END:VCARD';
  66. header('Content-Length: '.strlen($vCard));
  67. echo $vCard;
  68. exit();
  69. });
  70. Action::register('directory_get_excel',function(&$response){
  71. global $myUser,$_,$conf;
  72. if(!$myUser->connected()) throw new Exception("Permission denied");
  73. list($columns,$userMapping) = directory_get_contacts();
  74. $excel = implode(';',$columns)."\n";
  75. foreach($userMapping as $user=>$infos){
  76. foreach ($infos['values'] as $key => $value):
  77. $excel .= strip_tags(utf8_decode($value)).';';
  78. endforeach;
  79. $excel .= "\n";
  80. }
  81. File::downloadStream($excel,"annuaire ".date('d-m-Y').".csv",'text/csv');
  82. exit();
  83. });
  84. ?>