/extra/mass-reapprov.php

https://github.com/Eyepea/xivo-gallifrey · PHP · 148 lines · 101 code · 30 blank · 17 comment · 11 complexity · 1b3f2c57ca24501ac1c99395922f9d4e MD5 · raw file

  1. #! /usr/bin/php -q
  2. <?php
  3. /**
  4. Copyright (C) 2006-2010 Proformatique <technique@proformatique.com>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. if(($opt = getopt('h:p:c:r')) === false
  17. || isset($opt['h'],$opt['p'],$opt['c']) === false)
  18. help();
  19. $sockhost = $opt['h'];
  20. if(strtolower(substr($opt['c'],-4)) === '.csv')
  21. $csv = $opt['c'];
  22. else
  23. $csv = $opt['c'].'.csv';
  24. $out = 'POST /prov HTTP/1.1'."\r\n";
  25. $out .= 'Host: '.$opt['h']."\r\n";
  26. $out .= 'Keep-Alive: 300'."\r\n";
  27. $out .= 'Connection: keep-alive'."\r\n";
  28. $out .= 'Cache-Control: max-age=0'."\r\n";
  29. $row = 1;
  30. $total = $ok = $err = 0;
  31. $handle = fopen($csv,'r');
  32. $r = array();
  33. $r['mode'] = 'mode=notification';
  34. $r['from'] = 'from=webi';
  35. $r['actions'] = 'actions=' . (isset($opt['r'])?'yes':'no');
  36. $r['proto'] = 'proto=sip';
  37. while(($data = fgetcsv($handle,1000,',')) !== false)
  38. {
  39. $num = count($data);
  40. $error = false;
  41. if(isset($data[0]) === true
  42. && ($iduserfeatures = trim($data[0])) !== ''
  43. && ctype_digit($iduserfeatures) === true)
  44. $r['iduserfeatures'] = 'iduserfeatures='.$iduserfeatures;
  45. else
  46. $error = 'iduserfeatures';
  47. if(isset($data[1]) === true)
  48. {
  49. $macaddr = trim($data[1]);
  50. if(empty($macaddr) === true)
  51. $error = 'macaddr';
  52. else
  53. $r['macaddr'] = 'macaddr='.$macaddr;
  54. }
  55. else
  56. $error = 'macaddr';
  57. if($error !== false)
  58. {
  59. $err++;
  60. echo $row,': ERR - ',$error,"\n";
  61. $row++;
  62. $total++;
  63. continue;
  64. }
  65. $str = implode("\r\n",$r)."\r\n";
  66. $len = strlen($str);
  67. $errno = 0;
  68. $errstr = '';
  69. $fp = fsockopen($sockhost, $opt['p'], $errno, $errstr, 60);
  70. $write = $out;
  71. if($fp === false)
  72. {
  73. $err++;
  74. echo $errstr,' (',$errno,')',"\n",
  75. $row,': ERR - ',$iduserfeatures,"\n";
  76. }
  77. else
  78. {
  79. $write .= 'Content-Length: '.$len."\r\n";
  80. $write .= 'Connection: close'."\r\n\r\n";
  81. $write .= $str;
  82. fwrite($fp, $write);
  83. $recv = trim(fgets($fp,32));
  84. if(preg_match('/^HTTP\/1\.(x|1|0) 200 OK/',$recv) === 1)
  85. {
  86. $ok++;
  87. echo $row,': OK - ',$iduserfeatures,"\n";
  88. }
  89. else
  90. {
  91. $err++;
  92. echo $row,': ERR - ',$iduserfeatures,"\n";
  93. }
  94. fclose($fp);
  95. }
  96. $total++;
  97. sleep(1);
  98. $row++;
  99. }
  100. fclose($handle);
  101. echo '----- REAPPROV RESULTS -----',"\n",
  102. '- OK: ',$ok,"\n",
  103. '- ERR: ',$err,"\n",
  104. '- TOTAL: ',$total,"\n",
  105. '----- REAPPROV RESULTS -----',"\n";
  106. die();
  107. function help()
  108. {
  109. echo '-h',"\t\t",'hostname',"\n",
  110. '-p',"\t\t",'port',"\n",
  111. '-c',"\t\t",'csv file (format: idusefeatures,macaddr)',"\n",
  112. '-r',"\t\t",'reboot phones',"\n";
  113. die();
  114. }
  115. ?>