| <?php |
| |
| |
| |
| namespace aae\std { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| class std extends \aae\abstr\FunctionCollection { |
| private static $_previousErrorHandler; |
| |
| |
| |
| protected static function startsWith($haystack, $needle) { |
| return $needle === "" || strpos($haystack, $needle) === 0; |
| } |
| protected static function endsWith($haystack, $needle) { |
| return $needle === "" || substr($haystack, -strlen($needle)) === $needle; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| protected static function strrstr($haystack, $needle, $before_needle = false) { |
| $result = false; |
| $pos = strrpos($haystack, $needle); |
| if ($pos !== false) { |
| if (!$before_needle) { |
| $pos += strlen($needle); |
| $result = substr($haystack, $pos); |
| } else { |
| $result = substr($haystack, 0, $pos); |
| } |
| } |
| return $result; |
| } |
| |
| protected static function strstrNoNeedle($haystack, $needle, $before_needle = false) { |
| $result = false; |
| $includingNeedle = strstr($haystack, $needle, $before_needle); |
| if ($includingNeedle !== false) { |
| $needleLength = strlen($needle); |
| if (!$before_needle) { |
| $result = substr($includingNeedle, $needleLength); |
| } else { |
| $result = $includingNeedle; |
| } |
| } |
| return $result; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| protected static function strBeforeStr($haystack, $needle1, $needle2, $processBackToFront = false) { |
| $result = false; |
| if (!$processBackToFront) { |
| $posN1 = strpos($haystack, $needle1); |
| $posN2 = strpos($haystack, $needle2); |
| if ($posN1 !== false && $posN2 !== false) { |
| $result = ($posN1 < $posN2) ? true : false; |
| } |
| } else { |
| $posN1 = strrpos($haystack, $needle1); |
| $posN2 = strrpos($haystack, $needle2); |
| if ($posN1 !== false && $posN2 !== false) { |
| $result = ($posN1 > $posN2) ? true : false; |
| } |
| } |
| return $result; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| protected static function isInStr($haystack, $needles, $offset=0) { |
| if (!is_array($needles)) $needles = array($needles); |
| foreach($needles as $needle) { |
| if (strpos($haystack, $needle, $offset) !== false) { |
| return true; |
| } |
| } |
| return false; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public static function strNbrChar($haystack, $nbr, &$strPos) { |
| if ($strPos >= strlen($haystack)) { |
| $strPos = strlen($haystack); |
| return false; |
| } |
| $result = substr($haystack, $strPos, $nbr); |
| $strPos +=$nbr; |
| $strLen = strlen($haystack); |
| if ($strPos > $strLen) { |
| $strPos = $strLen; |
| } |
| return $result; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public static function strIsStr($haystack, $needle, &$strPos = 0) { |
| if ($strPos >= strlen($haystack)) { |
| $strPos = strlen($haystack); |
| return false; |
| } |
| $hayChunk = substr($haystack, $strPos, strlen($needle)); |
| if ($hayChunk == $needle) { |
| $strPos += strlen($needle); |
| return true; |
| } else return false; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| protected static function strReplaceBetween($haystack, $needle1, $needle2, $replacement, $removeNeedles = true) { |
| $needle1Pos = strpos($haystack, $needle1); |
| $needle2Pos = strpos($haystack, $needle2, $needle1Pos + strlen($needle1)); |
| if ($needle1Pos === false || $needle2Pos === false) { |
| $result = false; |
| } else { |
| if ($removeNeedles === false) { |
| $needle1Pos += strlen($needle1); |
| $needle2Pos -= strlen($needle2); |
| } |
| $result = substr($haystack, 0, $needle1Pos); |
| $result .= $replacement; |
| $result .= substr($haystack, $needle2Pos + strlen($needle2)); |
| } |
| return $result; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public static function strUntil($haystack, $until, &$strPos = 0) { |
| if ($strPos >= strlen($haystack)) { |
| $strPos = strlen($haystack); |
| return false; |
| } |
| $untilPos = strpos($haystack, $until, $strPos); |
| if ($untilPos === false) $untilPos = strlen($haystack); |
| $nbr = $untilPos - $strPos; |
| $result = substr($haystack, $strPos, $nbr); |
| $strPos += $nbr + strlen($until); |
| |
| $strLen = strlen($haystack); |
| if ($strPos > $strLen) { |
| $strPos = $strLen; |
| } |
| return $result; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public static function strUntilIfNot($haystack, $until, $ifNots, &$strPos) { |
| if ($strPos >= strlen($haystack)) { |
| $strPos = strlen($haystack); |
| return false; |
| } |
| if (!is_array($ifNots)) { |
| $ifNots = array($ifNots); |
| } |
| $ifNotPos = strlen($haystack); |
| foreach ($ifNots as $ifNot) { |
| $tempIfNotPos = strpos($haystack, $ifNot, $strPos); |
| if ($tempIfNotPos < $ifNotPos) $ifNotPos = $tempIfNotPos; |
| } |
| |
| $untilPos = strpos($haystack, $until, $strPos); |
| |
| if ($untilPos === false) $untilPos = strlen($haystack); |
| if ($ifNotPos === false) $ifNotPos = strlen($haystack); |
| |
| if ($ifNotPos <= $untilPos && $untilPos !== strlen($haystack)) return false; |
| $nbrChar = $untilPos - $strPos; |
| $result = static::strNbrChar($haystack, $nbrChar, $strPos); |
| |
| $strPos += strlen($until); |
| $strLen = strlen($haystack); |
| if ($strPos > $strLen) { |
| $strPos = $strLen; |
| } |
| |
| if (strlen($result) === 0) $result = false; |
| return $result; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| protected static function strIsFolder($folderStr) { |
| $result = false; |
| if (strpos($folderStr, '/') !== false) { |
| $noPeriodsBeforeSlashes = !static::strBeforeStr($folderStr, '.', '/', true); |
| $result = $noPeriodsBeforeSlashes; |
| } else { |
| if (strpos($folderStr, '.') === false) { |
| $result = true; |
| } |
| } |
| return $result; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| protected static function strIsFile($dirStr) { |
| $result = false; |
| if (strpos($dirStr, '/') !== false) { |
| $periodBeforeSlashes = static::strBeforeStr($dirStr, '.', '/', true); |
| $result = $periodBeforeSlashes; |
| } else { |
| if (strpos($dirStr, '.') !== false) { |
| $result = true; |
| } |
| } |
| return $result; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| protected static function strGetFolder($dir) { |
| $result = false; |
| if (static::strIsFile($dir)) { |
| if (strpos($dir, '/') !== false) { |
| $folderParts = explode('/',$dir); |
| if (count($folderParts) > 1) { |
| array_pop($folderParts); |
| } |
| $result = implode('/', $folderParts); |
| } else { |
| $result = '/'; |
| } |
| } else if (static::strIsFolder($dir)) { |
| $result = $dir; |
| } |
| if ($result == '') { |
| $result = '/'; |
| } else if ($result && substr($result, -1) != '/') { |
| $result = $result.'/'; |
| } |
| return $result; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| protected static function strGetFile($dir) { |
| $result = false; |
| if (static::strIsFile($dir)) { |
| if (strpos($dir, '/') !== false) { |
| $result = static::strrstr($dir, '/'); |
| } else { |
| $result = $dir; |
| } |
| } |
| return $result; |
| } |
| |
| |
| protected static function strDirEqual($dir1, $dir2) { |
| $dir1 = static::strStandadizePath($dir1); |
| $dir2 = static::strStandadizePath($dir2); |
| return ($dir1 == $dir2); |
| } |
| |
| protected static function strPathHasParent($strDir, $strDirParent) { |
| $strDir = static::strStandadizePath($strDir); |
| $strDirParent = static::strStandadizePath($strDirParent); |
| |
| $strDir = static::strGetFolder($strDir); |
| |
| return static::strIsStr($strDir, $strDirParent); |
| } |
| |
| protected static function strPathIsInFolder($strDir, $strDirParent) { |
| $strDir = static::strGetFolder($strDir); |
| $strPos = 0; |
| $result = false; |
| do { |
| $tempResult = false; |
| $tempFolder = substr($strDir, $strPos); |
| $strPos = strpos($strDir, '/', $strPos + 1); |
| |
| $tempResult = static::strPathHasParent($tempFolder, $strDirParent); |
| if ($tempResult) { |
| $result = true; |
| break; |
| } |
| } while ($strPos !== false); |
| return $result; |
| } |
| |
| protected static function strStandadizePath($path, $stripSurroundingSlashes = false) { |
| if ($stripSurroundingSlashes) { |
| $length = strlen($path); |
| $startPos = 0; |
| if (strlen($path) < 1) { |
| return false; |
| } |
| if ($path[0] == '/') { |
| $startPos = 1; |
| } |
| if (static::strIsFolder($path)) { |
| if ($path[strlen($path) - 1] == '/') { |
| $length--; |
| } |
| } |
| $length -= $startPos; |
| $path = substr($path, $startPos, $length); |
| } else { |
| if ($path[0] != '/') { |
| $path = '/'.$path; |
| } |
| if (static::strIsFolder($path)) { |
| if ($path[strlen($path) - 1] != '/') { |
| $path .= '/'; |
| } |
| } |
| } |
| return $path; |
| } |
| |
| protected static function strRelativePath($path, $referenceDir) { |
| $path = static::strStandadizePath($path); |
| $referenceDir = static::strStandadizePath($referenceDir); |
| |
| $relativePath = static::strstrNoNeedle($path, $referenceDir); |
| if (!$relativePath) { |
| $relativePath = substr($path, 1, strlen($path) - 1); |
| } |
| return $relativePath; |
| } |
| |
| protected static function strAbsolutePath($path, $referenceDir) { |
| $referenceDir = static::strStandadizePath($referenceDir); |
| |
| $relativePath = static::strRelativePath($path, $referenceDir); |
| return $referenceDir.$relativePath; |
| } |
| |
| |
| |
| |
| |
| |
| |
| protected static function getTopmostFolderName($dir, $directorySeperator = '/') { |
| $result = static::strrstr($dir, $directorySeperator); |
| if ($result === false) { |
| $result = $dir; |
| } |
| return $result; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| protected static function namespaceFromNSClassName($className) { |
| if (!is_string($className)) throw new \InvalidArgumentException('Arguments passed to '.__FUNCTION__.' have to be of type string.'); |
| return static::strrstr($className, '\\', true); |
| } |
| |
| |
| |
| |
| |
| |
| |
| protected static function classFromNSClassName($className) { |
| if (is_object($className)) $className = get_class($className); |
| $result = static::strrstr($className, '\\'); |
| if ($result === false) { |
| $result = $className; |
| } |
| |
| return $result; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| protected static function namespaceComponent($namespacedId, $pos) { |
| if (!is_int($pos)) { |
| throw new \InvalidArgumentException(sprintf(constant\Error::WRONG_TYPE, 'pos', 'integer')); |
| } else if ($pos === 0) { |
| throw new \InvalidArgumentException(sprintf(constant\Error::INVALID_POSITION)); |
| } |
| $result = false; |
| $parts = explode('\\', $namespacedId); |
| if (strlen($parts[0]) < 1) array_shift($parts); |
| $maxLength = count($parts); |
| if ($maxLength < 1) { |
| $result = false; |
| } else if ($maxLength < abs($pos)) { |
| $result = false; |
| } else { |
| if ($pos > 0) { |
| $result = $parts[$pos - 1]; |
| } else { |
| $result = $parts[$maxLength + $pos]; |
| } |
| } |
| return $result; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| protected static function namespaceDepth($className) { |
| $parts = explode('\\', $className); |
| if (strlen($parts[0]) < 1) unset($parts[0]); |
| return count($parts) - 1; |
| } |
| |
| |
| |
| |
| |
| |
| |
| protected static function isNamespaced($className) { |
| $result = strpos($className, '\\'); |
| if ($result !== false) { |
| $result = true; |
| } |
| return $result; |
| } |
| |
| |
| |
| |
| |
| |
| |
| protected static function resolveRelativeNamespace($relativeNamespace) { |
| $count = 1; |
| $result = $relativeNamespace; |
| while ($count > 0) { |
| $count = 0; |
| $result = preg_replace('/\w+(\\\.\.\\\)/', '', $result, 1, $count); |
| } |
| if (substr($relativeNamespace, 0, 4) == '\\..\\' || substr($relativeNamespace, 0, 3) == '..\\') { |
| throw new \Exception(sprintf(constant\Error::COULD_NOT_RESOLVE_NAMESPACE, $result)); |
| } |
| return $result; |
| } |
| |
| |
| |
| |
| |
| |
| |
| protected static function namespaceToDir($namespaceString) { |
| $parts = explode('\\', $namespaceString); |
| $dir = implode('/', $parts); |
| return $dir; |
| } |
| |
| protected static function snakeToCamel($snake, $isVar = false) { |
| $camel = implode('',array_map('ucfirst',explode('_',$snake))); |
| if ($isVar) $camel = lcfirst($camel); |
| return $camel; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| protected static function implodeAssArray($glue, $format, $pieces) { |
| $result = ""; |
| |
| $i = 0; |
| foreach ($pieces as $key => $value) { |
| if ($i > 0) { |
| $result .= $glue; |
| } |
| $result .= sprintf($format, $key, $value); |
| $i++; |
| } |
| return $result; |
| } |
| |
| protected static function implodeAssArrayDBSave($link, $glue, $format, $pieces) { |
| $result = ""; |
| |
| $i = 0; |
| foreach ($pieces as $key => $value) { |
| if ($i > 0) { |
| $result .= $glue; |
| } |
| $result .= sprintf($format, mysqli_real_escape_string($link, $key), mysqli_real_escape_string($link, $value)); |
| $i++; |
| } |
| return $result; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| protected static function json_clean_decode($json, $assoc = false, $depth = 512, $options = 0) { |
| |
| $jsonString = preg_replace("#(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)|([\s\t](//).*)#", '', $json); |
| |
| if(version_compare(phpversion(), '5.4.0', '>=')) { |
| $json = json_decode($jsonString, $assoc, $depth, $options); |
| } |
| |
| else if(version_compare(phpversion(), '5.3.0', '>=')) { |
| $json = json_decode($jsonString, $assoc, $depth); |
| } else { |
| $json = json_decode($jsonString, $assoc); |
| } |
| |
| if ($json === NULL) { |
| |
| throw new \Exception(sprintf(constant\ERROR::INVALID_JSON, $jsonString)); |
| } |
| return $json; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| protected static function staticInitFromJSON($className, $dir, $setterName) { |
| if (file_exists($dir)) { |
| $fileString = file_get_contents($dir); |
| $fileJSON = std::json_clean_decode($fileString, true); |
| $noErrors = true; |
| static::turnOnErrorExceptions(); |
| foreach ($fileJSON as $constName => $value) { |
| try { |
| $className::$setterName($constName, $value); |
| } catch (\ErrorException $e) { |
| static::turnOffErrorExceptions(); |
| if (is_object($className)) $className = get_class($className); |
| throw new \Exception(sprintf(constant\Error::STATIC_FUNCTION_DOES_NOT_EXIST, $className, $setterName)); |
| } |
| } |
| static::turnOffErrorExceptions(); |
| |
| } else throw new \Exception(sprintf(constant\Error::FILE_NOT_FOUND, $dir)); |
| } |
| |
| |
| |
| |
| |
| protected static function turnOnErrorExceptions() { |
| static::$_previousErrorHandler = set_error_handler( |
| function ($errno, $errstr, $errfile, $errline, array $errcontext) { |
| throw new \ErrorException($errstr, 0, $errno, $errfile, $errline); |
| } |
| ); |
| return static::$_previousErrorHandler; |
| } |
| |
| |
| |
| |
| |
| protected static function turnOffErrorExceptions() { |
| if (isset(static::$_previousErrorHandler)) { |
| set_error_handler(static::$_previousErrorHandler); |
| return true; |
| } else return false; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| protected static function getClassPedegree($object) { |
| $pedegree = array(); |
| if (is_string($object)) { |
| $pedegree[] = $object; |
| $class = new \ReflectionClass($object); |
| while ($class = $class->getParentClass()) { |
| $pedegree[] = $class->getName(); |
| } |
| return array_reverse($pedegree); |
| } else { |
| $class = get_class($object); |
| do { |
| $pedegree[] = $class; |
| } while (($class = get_parent_class($class)) !== false); |
| return array_reverse($pedegree); |
| } |
| } |
| |
| |
| |
| |
| |
| |
| protected static function getType($value) { |
| if ($value === NULL) { |
| $result = 'NULL'; |
| } elseif (is_array($value)) { |
| $result = 'array'; |
| } elseif (is_scalar($value)) { |
| $scalarType = gettype($value); |
| $result = $scalarType; |
| } else { |
| $valueClassType = get_class($value); |
| $result = $valueClassType; |
| } |
| return $result; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| protected static function getInstanceWithArgs($className, $args = array()) { |
| if (!static::isNamespaced($className)) throw new \Exception(sprintf(static::WARNING_CLASS_NAME_NOT_NAMESPACED, $className)); |
| if(count($args) < 1) |
| $obj = new $className; |
| else { |
| $r = new \ReflectionClass($className); |
| $obj = $r->newInstanceArgs($args); |
| } |
| return $obj; |
| } |
| |
| |
| |
| protected static function callFunctionWithArguments($class, $function, $arguments) { |
| $result = false; |
| if (method_exists($class, $function)) { |
| if (!is_array($arguments)) { |
| $arguments = array($arguments); |
| } |
| $result = call_user_func_array(array($class, $function), $arguments); |
| } |
| return $result; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| protected static function formatBytes($bytes, $precision = 2) { |
| $units = array('B', 'KB', 'MB', 'GB', 'TB'); |
| |
| $bytes = max($bytes, 0); |
| $pow = floor(($bytes ? log($bytes) : 0) / log(1024)); |
| $pow = min($pow, count($units) - 1); |
| $bytes /= pow(1024, $pow); |
| |
| return round($bytes, $precision) . ' ' . $units[$pow]; |
| } |
| |
| protected static function isSSL() { |
| return ($_SERVER['SERVER_PORT'] == 443); |
| } |
| |
| protected static function modRewriteIsOn() { |
| $moduleExists = false; |
| if (function_exists('apache_get_modules')) { |
| $modules = apache_get_modules(); |
| $moduleExists = in_array('mod_rewrite', $modules); |
| } |
| if (getenv('HTTP_MOD_REWRITE')=='On') { |
| $environMentVariableSet = true; |
| } else { |
| $environMentVariableSet = false; |
| } |
| |
| $result = $moduleExists && $environMentVariableSet; |
| return $result; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| protected static function curlGetJSON($url, $postArray = array()) { |
| $ch = curl_init(); |
| |
| curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| curl_setopt($ch, CURLOPT_URL, $url); |
| if (defined('aae\framework\FW_CURL_VERIFY_PEER') && |
| !\aae\framework\FW_CURL_VERIFY_PEER) { |
| curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
| } |
| curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); |
| curl_setopt($ch, CURLOPT_SSLVERSION,3); |
| curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
| curl_setopt($ch, CURLOPT_USERAGENT, "cURL call"); |
| curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120); |
| curl_setopt($ch, CURLOPT_TIMEOUT , 120); |
| curl_setopt($ch, CURLOPT_MAXREDIRS , 10); |
| |
| |
| if (count($postArray) > 0) { |
| $postString = http_build_query($postArray); |
| curl_setopt($ch,CURLOPT_POST, count($postArray)); |
| curl_setopt($ch,CURLOPT_POSTFIELDS, $postString); |
| } |
| |
| $data = curl_exec($ch); |
| $resultCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
| curl_close($ch); |
| |
| if ($resultCode == 200) { |
| $result = static::json_clean_decode($data); |
| if ($result === NULL) throw new \Exception(sprintf(\aae\fw\constant\ERROR::INVALID_JSON,$data)); |
| return $result; |
| } else { |
| throw new \Exception(sprintf(\aae\fw\constant\ERROR::CURL_REQUEST_FAILED, $resultCode)); |
| } |
| } |
| |
| |
| static function execInBackground($cmd, $output = NULL, $errorOutput = NULL) { |
| if (substr(php_uname(), 0, 7) == "Windows"){ |
| pclose(popen("start /B ". $cmd, "r")); |
| } |
| else { |
| if ($output === NULL) { |
| $output = '/dev/null'; |
| } |
| if ($errorOutput === NULL) { |
| $errorOutput = '/dev/null'; |
| } |
| exec($cmd . " > $output 2> $errorOutput &"); |
| } |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| } |
| } |
| |
| |
| |
| |
| |
| |