Some Crazy things  done on PHP     Taras Kalapun
Connecting PHP to M$ Wordgetting string starting with “subject:”                    $app = com_get_active_object("Word.App...
getting Thumbnail data fromAdobe Lightroom *.noindex                 $path = 3.noindex; // Thumbnail File                 ...
getting elevation data   private function _dataFile($lat, $lon)                         {from SRTM source             $X =...
private function _getAlt($lat, $lon){    $lat = abs($lat);    $lon = abs($lon);     $latx = ($lat - floor($lat)) * 100000;...
/**  * Converts a 2-byte High-Order sign magnitudeto integer.  * Bit 15 is sign bit, bits 0-14 are magnitudeof number  */p...
$radius = new Monitor_RADIUS;if ($radius->isError()) {    foreach ($radius->getErrors() as $ip => $error) {        // Send...
public function isError() {    $this->connect();    $data = $this->getStat();    $this->findErrors($data);    if ($this->e...
protected function findErrors(&$data) {    $error = array();    $server_up = array();     foreach ($data as $row) {       ...
protected static function prepareTrapPacket($varBinds=null, $community=public, $requestId=null){       $requestId = ($requ...
Avaya OSSIS
public function connect($host, $port=5023, $login=null, $passwd=null,$pin=null) {  if (!$this->_connected) {   $this->_hos...
protected function _fidsToDataArray($data) {  $newData = array();  foreach($data as $fid=>$val) {   if (substr($fid, 0, 5)...
Win32 service on PHP// First you need to create a service, you only need to do this oncewin32_create_service(array(    ser...
$myservicename = phpavtelnet; $socket = null;$port = 999;$max_clients = 10;$telnet = null; // Connect to service dispatche...
function sock_server_init() { global $port, $socket, $clients, $max_clients;  $socket = socket_create(AF_INET,SOCK_STREAM,...
function process_data($data, $client) { global $avaya;   if (strstr($data, getAgentSkills) !== false) {    $x = explode( ,...
if (!$this->ds) {   $this->ds = ldap_connect($host);   if ($this->ds) {      ldap_set_option($this->ds, LDAP_OPT_PROTOCOL_...
include("pChart/pData.class");include("pChart/pChart.class"); // Dataset definition$DataSet = new pData;$DataSet->AddPoint...
What next?
Crazy things done on PHP
Crazy things done on PHP
Crazy things done on PHP
Crazy things done on PHP
Crazy things done on PHP
Crazy things done on PHP
Crazy things done on PHP
Crazy things done on PHP
Crazy things done on PHP
Crazy things done on PHP
Crazy things done on PHP
Crazy things done on PHP
Crazy things done on PHP
Upcoming SlideShare
Loading in …5
×

Crazy things done on PHP

8,508 views
8,292 views

Published on

0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
8,508
On SlideShare
0
From Embeds
0
Number of Embeds
20
Actions
Shares
0
Downloads
19
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Crazy things done on PHP

    1. 1. Some Crazy things done on PHP Taras Kalapun
    2. 2. Connecting PHP to M$ Wordgetting string starting with “subject:” $app = com_get_active_object("Word.Application") or die("Cant connect to Word");   echo $app->version;   $app->Selection->WholeStory(); $x = $app->Selection->Text;   $text = explode("r", $x); $subject = ; foreach ($text as $s) { if (stristr($s, subject:)) { preg_match("/subject:(.+)$/i", $s, $subject); $subject = trim($subject[1]); } } echo Subject: . $subject;
    3. 3. getting Thumbnail data fromAdobe Lightroom *.noindex $path = 3.noindex; // Thumbnail File $level = 4; // Thumbnail level   header(Content-Type: image/jpeg); header(Last-Modified: .date(r, filemtime($path)));   $file = file_get_contents($path);   $AgHg = explode(AgHg, $file);   $max_level = count($AgHg)-2; if ($level > $max_level) $level = $max_level;   // 0 - array // 1 - small img   $level_img = explode(level_.$level, $AgHg[$level+1]); $image = ltrim($level_img[1], "x00"); echo $image;
    4. 4. getting elevation data private function _dataFile($lat, $lon) {from SRTM source $X = $lon; $Y = $lat;   $Ix = abs($X + 0.5); // nearest degree $Iy = abs($Y + 0.5); // 1 second tolerance if (abs($X - $Ix) < 0.0002) $X = $Ix; if (abs($Y - $Iy) < 0.0002) $Y = $Iy;   if ($X <= -180)$X = $X + 360; if ($X > 180) $X = $X - 360;   $EW = ($X > 0) ? "W" : "E";   $lon = abs( (int) $X );   if ($lon < 100) $lon = "0".$lon;   $NS = ($Y >= 0) ? "N" : "S"; $lat = abs( (int) $Y );   $this->_lat0 = (int) $Y; $this->_lon0 = (int) $X;   return $NS.$lat.$EW.$lon; }
    5. 5. private function _getAlt($lat, $lon){ $lat = abs($lat); $lon = abs($lon);  $latx = ($lat - floor($lat)) * 100000; //x $lonx = ($lon - floor($lon)) * 100000; //y  $dim = 83.26394671; // 99999/1201 //$dim = 83.19384;  $lat_p = round($latx/$dim); $lon_p = round($lonx/$dim);  // seeking in file $seek = -1201 * 2*($lat_p+1) + 2*(abs($lon_p-1)); fseek($this->_fp, $seek, SEEK_END); $b = fread($this->_fp, 2);  // swapping $ht = $this->signMag($b[0], $b[1]); return $ht;}
    6. 6. /** * Converts a 2-byte High-Order sign magnitudeto integer. * Bit 15 is sign bit, bits 0-14 are magnitudeof number */private function signMag($b1, $b2){ $b1 = ord($b1); $b2 = ord($b2); if ($b1 < = 127) { $ht = ($b1 * 256) + $b2; } else if ($b1 > 127) { $ht = -(($b1 - 128) * 256 + $b2); } return $ht;}
    7. 7. $radius = new Monitor_RADIUS;if ($radius->isError()) { foreach ($radius->getErrors() as $ip => $error) { // Send SNMP Trap (-v 2c -c PUBLIC_COMMON 10.36.1.212 ""OS ucdDemoPublicString.0 s "OS") Notify::send( $radius->getErrorType(), $radius->getServiceName($ip), $radius->getErrorDescription($ip) ); }}
    8. 8. public function isError() { $this->connect(); $data = $this->getStat(); $this->findErrors($data); if ($this->error != false) return true; else return false;} public function connect() { try { $this->dbh = new PDO(oci:dbname=.$this->_db_db, $this->_db_user, $this->_db_pass); } catch (PDOException $e) { print "Error!: " . $e->getMessage() . "<br />"; die(); } return $this;} protected function getStat() { $time = date("H:i:00", mktime(date("H"), date("i") - $this->minute_diff )); $date = date("Y-m-d " . $time);  $radius_ip_sql = " AND server_ip ". $this->ip2sql($this->radius_ip);  $sql = "SELECT TO_CHAR(reg_date, YYYY-MM-DD HH24:MI:SS) AS reg_date, server_ip,packet_all, packet_err, packet_autor, timeave_fun FROM statistic " ."WHEREreg_date>to_date(" . $date . ",YYYY-MM-DD HH24:MI:SS) " . $radius_ip_sql . "ORDER BYreg_date DESC";  $sth = $this->dbh->prepare($sql); $sth->execute(); $data = $sth->fetchAll();  return $data;}
    9. 9. protected function findErrors(&$data) { $error = array(); $server_up = array();  foreach ($data as $row) { $server_up[ $row[SERVER_IP] ] = true; if ($row[PACKET_ALL] < = 0) { $error[ $row[SERVER_IP] ][] = $row[REG_DATE] . : . packet_all = .$row[PACKET_ALL]; } if ($row[PACKET_AUTOR] <= 0) { $error[ $row[SERVER_IP] ][] = $row[REG_DATE] . : . packet_autor = .$row[PACKET_AUTOR]; } if ($row[PACKET_ERR] > 5) { $error[ $row[SERVER_IP] ][] = $row[REG_DATE] . : . packet_err = .$row[PACKET_ERR]; } if ($row[TIMEAVE_FUN] > 400) { $error[ $row[SERVER_IP] ][] = $row[REG_DATE] . : . timeave_fun = .$row[TIMEAVE_FUN]; }  }  foreach ($this->radius_ip as $server) { if (!array_key_exists($server, $server_up)) $error[$server][] = No packet information for thisServer; }  $this->error = $error; return $this;}
    10. 10. protected static function prepareTrapPacket($varBinds=null, $community=public, $requestId=null){ $requestId = ($requestId) ? $requestId : rand(10000, 30000); // from 10000 to 40000 ? $upTime = rand(100, 900) * 1000000;  $trap[version] = self::varPack(1); //pack(H*, 0201.01); // SNMP v2c $trap[community] = self::varPack($community); $trap[header] = pack(H*, 0202 . self::dec2hex($requestId) // request-id .020100 // error-status: noError (0) .020100 // error-index: 0 );  $var_sysUpTime = self::packVarBind(array( oid => 1.3.6.1.2.1.1.3.0, value => $upTime, type o // x43 => )); // sysUpTime.0  $var_snmpTrapOID = self::packVarBind(array( oid => 1.3.6.1.6.3.1.1.4.1.0, value => 1.3.6.1.2.1.31.2.1, type oid // x06 => )); // snmpTrapOID.0  $varBindsPack = $var_sysUpTime . $var_snmpTrapOID . self::packVarBinds($varBinds);  $trap[varBindsHeader] = pack(H*, 30 . self::hexlen($varBindsPack,1)); // varBindssize  $trapBody = $trap[header] . $trap[varBindsHeader] . $varBindsPack;  $trap[header0] = pack(H*, a7. self::hexlen($trapBody,1));  $trap = $trap[version] . $trap[community] . $trap[header0] . $trapBody; $trap = pack(H*, 30 . self::hexlen($trap,1)) . $trap; return $trap; }
    11. 11. Avaya OSSIS
    12. 12. public function connect($host, $port=5023, $login=null, $passwd=null,$pin=null) { if (!$this->_connected) { $this->_host = $host; $this->_port = $port; $this->setCredentials($login, $passwd, $pin);  $this->telnet = new Telnet($this->_host, $this->_port); $this->telnet->login($this->_login, $this->_password);  if ($this->_pin) { $this->telnet->waitPrompt(Pin:); $this->telnet->write($pin); }  $class = Avaya_Protocol_.$this->_protocolType; $this->protocol = new $class; $this->protocol->setTelnet($this->telnet); //$this->protocol->connect();  // Term Emulation $this->telnet->waitPrompt([513]); $this->telnet->write($this->protocol->getTermEmulation()); $this->telnet->waitPrompt([y]); $this->telnet->write(y);  $this->telnet->waitPrompt($this->protocol->getTermPrompt());  $this->_connected = true; } return $this->_connected; }
    13. 13. protected function _fidsToDataArray($data) { $newData = array(); foreach($data as $fid=>$val) { if (substr($fid, 0, 5) == 000d0) { $iRecord = substr($fid, 6, 2); $iVal = substr($fid, 4, 2); // 4,2 or 5,1 ??? $iRecord = hexdec($iRecord); $iVal = hexdec($iVal);  $newData[data][$iRecord][$iVal] = $val; } else { $newData[fids][$fid] = $val; } } //$newData[data] = array_filter($newData[data]); $newData[data] = array_remove_empty($newData[data]);  return $newData; } public function getSkillLog($skill) { $cmd = list bcms skill; $fids = array( 3e82ff00,// => time, 3e83ff00,// => acd_calls, 3e84ff00,// => avg_speed_ans, 3e85ff00,// => aband_calls, 3e86ff00,// => avg_aband_time
    14. 14. Win32 service on PHP// First you need to create a service, you only need to do this oncewin32_create_service(array( service => phpavtelnet, display => PHP Avaya Telnet, params => c:xampphtdocsservicephpavtelnet.php, path => c:xamppphpphp.exe));
    15. 15. $myservicename = phpavtelnet; $socket = null;$port = 999;$max_clients = 10;$telnet = null; // Connect to service dispatcher and notify that startup was successfulif (!win32_start_service_ctrl_dispatcher($myservicename)) die(Could not connect toservice :.$myservicename);win32_set_service_status(WIN32_SERVICE_RUNNING);  sock_server_init(); require_once Telnet.php; require_once CM.php; $avaya = new Avaya_CM; $avaya->connect(); // Main Server Loopwhile (1) { switch (win32_get_last_control_message()) { case WIN32_SERVICE_CONTROL_CONTINUE: break; case WIN32_SERVICE_CONTROL_INTERROGATE:win32_set_service_status(WIN32_NO_ERROR); break; case WIN32_SERVICE_CONTROL_STOP:win32_set_service_status(WIN32_SERVICE_STOPPED); service_stop(); default: win32_set_service_status(WIN32_ERROR_CALL_NOT_IMPLEMENTED); } sock_server_loop();} win32_set_service_status(WIN32_SERVICE_STOPPED);
    16. 16. function sock_server_init() { global $port, $socket, $clients, $max_clients;  $socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);  socket_set_option($socket,SOL_SOCKET,SO_REUSEADDR,1); socket_bind($socket,0,$port); socket_listen($socket,$max_clients);  write_log(Server started on port .$port);  $clients = array(0 => array(socket => $socket));} function sock_server_loop() { for($i=1;$i<$max_clients+1;$i++) { if(in_array($clients[$i][socket],$read)) {   $data = @socket_read($clients[$i][socket],1024000, PHP_NORMAL_READ);   if ($data === FALSE) { unset($clients[$i]); continue; }   $data = trim($data); if(!empty($data)) { process_data($data, $clients[$i]); } } }
    17. 17. function process_data($data, $client) { global $avaya; if (strstr($data, getAgentSkills) !== false) { $x = explode( , $data); $ext = $x[1]; $skills = $avaya->getAgentSkills($ext); $skills = $avaya->skills2str($skills); socket_write($client[socket], $skills."rn"); }
    18. 18. if (!$this->ds) { $this->ds = ldap_connect($host); if ($this->ds) { ldap_set_option($this->ds, LDAP_OPT_PROTOCOL_VERSION, 3); ldap_set_option($this->ds, LDAP_OPT_REFERRALS, 0); if (!@ldap_bind($this->ds, $login, $passwd)) { return false; } return $this; } else { return false; }} public function searchUser($s) { $attributes = array(sAMAccountName, cn); $filter = (&(objectCategory=user)(displayName=*.$s.*)); $sr = ldap_search($this->ds, $this->dn, $filter, $attributes); $num_enntries = ldap_count_entries($this->ds, $sr); $users = array(); if ($num_enntries > 0) { $info = ldap_get_entries($this->ds, $sr); foreach($info as $u) { if (!empty($u[cn][0])) $users[] = array( login => $u[samaccountname][0], name => $u[cn][0], ); } return $users; } else { return false; }}
    19. 19. include("pChart/pData.class");include("pChart/pChart.class"); // Dataset definition$DataSet = new pData;$DataSet->AddPoint($data,"Serie1");$DataSet->AddPoint($legend,"Serie2");$DataSet->AddAllSeries();$DataSet->SetAbsciseLabelSerie("Serie2"); // Initialise the graph$graph = new pChart(720,350);$graph->loadColorPalette(hard_bright.txt); // Draw the pie chart$graph->setFontProperties("Fonts/tahoma.ttf",12);$graph->drawTitle(50,20,Top 20 Принятых за .$date ,0,0,0); $graph->setFontProperties("Fonts/tahoma.ttf",9); $graph->drawPieGraph($DataSet->GetData(),$DataSet->GetDataDescription(),210,160,180,PIE_PERCENTAGE,TRUE,50,20,5);$graph->drawPieLegend(420,5,$DataSet->GetData(),$DataSet->GetDataDescription(),250,250,250); $graph->Stroke();
    20. 20. What next?

    ×