| 1 |
<?php |
|---|
| 2 |
/* |
|---|
| 3 |
*Name : helper.php |
|---|
| 4 |
*Author : Michael Yara |
|---|
| 5 |
*Created : June 15, 2008 |
|---|
| 6 |
*Last Updated : January 27, 2009 |
|---|
| 7 |
*History : 0.2 Beta |
|---|
| 8 |
*Purpose : Contains utility functions used by the library. |
|---|
| 9 |
*Copyright : 2008 HEHE Enterprises, LLC |
|---|
| 10 |
*/ |
|---|
| 11 |
|
|---|
| 12 |
/*############################################### |
|---|
| 13 |
* Sends a message to the server and listens for a reply |
|---|
| 14 |
* Takes: request |
|---|
| 15 |
* Returns: an error or the server reply |
|---|
| 16 |
*/ |
|---|
| 17 |
function sendAndRecive($request) { |
|---|
| 18 |
|
|---|
| 19 |
global $SOCKET, $READ_LENGTH, $NEW_LINE; |
|---|
| 20 |
|
|---|
| 21 |
//Add new line to request |
|---|
| 22 |
$request = $request . $NEW_LINE; |
|---|
| 23 |
|
|---|
| 24 |
//Check if socket is initialized |
|---|
| 25 |
if(($SOCKET !== null) && ($SOCKET !== false)) { |
|---|
| 26 |
|
|---|
| 27 |
//Read out prompt |
|---|
| 28 |
if(($buf = socket_read($SOCKET, $READ_LENGTH, PHP_BINARY_READ)) !== false) { |
|---|
| 29 |
|
|---|
| 30 |
//Send request |
|---|
| 31 |
if(socket_write($SOCKET, $request, strlen($request))) { |
|---|
| 32 |
|
|---|
| 33 |
//Read reply |
|---|
| 34 |
if(($buf = socket_read($SOCKET, $READ_LENGTH, PHP_BINARY_READ)) !== false) { |
|---|
| 35 |
return $buf; |
|---|
| 36 |
} else { |
|---|
| 37 |
return "Error: Could not read from socket. " . socket_strerror(socket_last_error()); |
|---|
| 38 |
} |
|---|
| 39 |
} else { |
|---|
| 40 |
return "Error: Could not write to socket. " . socket_strerror(socket_last_error()); |
|---|
| 41 |
} |
|---|
| 42 |
} else { |
|---|
| 43 |
return "Error: Could not read from socket. " . socket_strerror(socket_last_error()); |
|---|
| 44 |
} |
|---|
| 45 |
} else { |
|---|
| 46 |
return "Error: Socket not connected. Call iSymphonyConnect() to initialize connection."; |
|---|
| 47 |
} |
|---|
| 48 |
} |
|---|
| 49 |
|
|---|
| 50 |
/*############################################### |
|---|
| 51 |
* Checks if reply was an error |
|---|
| 52 |
* Takes: server reply |
|---|
| 53 |
* Returns: true or false |
|---|
| 54 |
*/ |
|---|
| 55 |
function isError($reply) { |
|---|
| 56 |
return strpos($reply, "Error") === 0; |
|---|
| 57 |
} |
|---|
| 58 |
|
|---|
| 59 |
/*############################################### |
|---|
| 60 |
* Makes a request that returns nothing and checks reply for error. If error is found $ISERROR is set |
|---|
| 61 |
* Takes: request |
|---|
| 62 |
* Returns: true if successful and false if error |
|---|
| 63 |
*/ |
|---|
| 64 |
function checkAndSetErrorNone($request) { |
|---|
| 65 |
|
|---|
| 66 |
global $ISERROR; |
|---|
| 67 |
|
|---|
| 68 |
if(isError($reply = sendAndRecive($request))) { |
|---|
| 69 |
$ISERROR = $reply; |
|---|
| 70 |
return false; |
|---|
| 71 |
} else { |
|---|
| 72 |
return true; |
|---|
| 73 |
} |
|---|
| 74 |
} |
|---|
| 75 |
|
|---|
| 76 |
/*############################################### |
|---|
| 77 |
* Makes a request that returns a string and checks reply for error. If error is found $ISERROR is set |
|---|
| 78 |
* Takes: request |
|---|
| 79 |
* Returns: the string if successful and false if error |
|---|
| 80 |
*/ |
|---|
| 81 |
function checkAndSetErrorString($request) { |
|---|
| 82 |
|
|---|
| 83 |
global $ISERROR; |
|---|
| 84 |
|
|---|
| 85 |
if(isError($reply = sendAndRecive($request))) { |
|---|
| 86 |
$ISERROR = $reply; |
|---|
| 87 |
return false; |
|---|
| 88 |
} else { |
|---|
| 89 |
return trim($reply); |
|---|
| 90 |
} |
|---|
| 91 |
} |
|---|
| 92 |
|
|---|
| 93 |
/*############################################### |
|---|
| 94 |
* Makes a request that returns a list and checks reply for error. If error is found $ISERROR is set |
|---|
| 95 |
* Takes: request |
|---|
| 96 |
* Returns: an array of values if successful and false if error |
|---|
| 97 |
*/ |
|---|
| 98 |
function checkAndSetErrorList($request) { |
|---|
| 99 |
|
|---|
| 100 |
global $ISERROR, $NEW_LINE; |
|---|
| 101 |
|
|---|
| 102 |
if(isError($reply = sendAndRecive($request))) { |
|---|
| 103 |
$ISERROR = $reply; |
|---|
| 104 |
return false; |
|---|
| 105 |
} else { |
|---|
| 106 |
$explodeArray = explode($NEW_LINE, trim($reply)); |
|---|
| 107 |
$returnArray = array(); |
|---|
| 108 |
|
|---|
| 109 |
//Search array for empty values and remove them |
|---|
| 110 |
foreach($explodeArray as $val) { |
|---|
| 111 |
if(strlen($val) != 0) { |
|---|
| 112 |
array_push($returnArray, $val); |
|---|
| 113 |
} |
|---|
| 114 |
} |
|---|
| 115 |
|
|---|
| 116 |
return $returnArray; |
|---|
| 117 |
} |
|---|
| 118 |
} |
|---|
| 119 |
|
|---|
| 120 |
|
|---|
| 121 |
/*############################################### |
|---|
| 122 |
* Coverts a property list into a associative key => value array |
|---|
| 123 |
* Takes: an array of properties with values structured like so (property = value) |
|---|
| 124 |
* Returns: an associative array with the structure (property => value) |
|---|
| 125 |
*/ |
|---|
| 126 |
function convertPropertyArray($array) { |
|---|
| 127 |
|
|---|
| 128 |
$returnArray = array(); |
|---|
| 129 |
|
|---|
| 130 |
foreach($array as $val) { |
|---|
| 131 |
|
|---|
| 132 |
//Check if the property has a value and set the key value pair appropriately |
|---|
| 133 |
if(endsWith($val," =")) { |
|---|
| 134 |
$keyValuePair = array(rtrim($val," =") ,""); |
|---|
| 135 |
} else if(endsWith($val," = ")) { |
|---|
| 136 |
$keyValuePair = array(rtrim($val," = "),""); |
|---|
| 137 |
} else { |
|---|
| 138 |
$keyValuePair = explode(" = ", $val); |
|---|
| 139 |
} |
|---|
| 140 |
|
|---|
| 141 |
$returnArray[$keyValuePair[0]] = $keyValuePair[1]; |
|---|
| 142 |
} |
|---|
| 143 |
|
|---|
| 144 |
return $returnArray; |
|---|
| 145 |
} |
|---|
| 146 |
|
|---|
| 147 |
/*############################################### |
|---|
| 148 |
* Checks if a string ends with another specified string |
|---|
| 149 |
* Takes: a string, the string you wish to check is at the end |
|---|
| 150 |
* Returns: true if $endString is at the end of $string false if not |
|---|
| 151 |
*/ |
|---|
| 152 |
function endsWith($string, $endString){ |
|---|
| 153 |
return strrpos($string, $endString) === strlen($string)-strlen($endString); |
|---|
| 154 |
} |
|---|
| 155 |
|
|---|
| 156 |
/*############################################### |
|---|
| 157 |
* Removes an element from an array by value |
|---|
| 158 |
* Takes: an array and the element to be removed |
|---|
| 159 |
* Returns: and new re-orderd array without the removed element |
|---|
| 160 |
*/ |
|---|
| 161 |
function remove_element($arr, $val){ |
|---|
| 162 |
foreach ($arr as $key => $value){ |
|---|
| 163 |
if ($arr[$key] == $val){ |
|---|
| 164 |
unset($arr[$key]); |
|---|
| 165 |
} |
|---|
| 166 |
} |
|---|
| 167 |
return $arr = array_values($arr); |
|---|
| 168 |
} |
|---|
| 169 |
|
|---|
| 170 |
?> |
|---|