functions.php 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522
  1. <?php
  2. /*
  3. FusionPBX
  4. Version: MPL 1.1
  5. The contents of this file are subject to the Mozilla Public License Version
  6. 1.1 (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.mozilla.org/MPL/
  9. Software distributed under the License is distributed on an "AS IS" basis,
  10. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11. for the specific language governing rights and limitations under the
  12. License.
  13. The Original Code is FusionPBX
  14. The Initial Developer of the Original Code is
  15. Mark J Crane <[email protected]>
  16. Portions created by the Initial Developer are Copyright (C) 2008-2022
  17. the Initial Developer. All Rights Reserved.
  18. Contributor(s):
  19. Mark J Crane <[email protected]>
  20. Tim Fry <[email protected]>
  21. Luis Daniel Lucio Quiroz <[email protected]>
  22. */
  23. if (!function_exists('str_starts_with')) {
  24. /**
  25. * Checks if a string starts with a given substring
  26. * <p>Performs a case-sensitive check indicating if <b>haystack</b> begins with <b>needle</b>.</p>
  27. * @param string $haystack The string to search in.
  28. * @param string $needle The substring to search for in the <b>haystack</b>.
  29. * @return bool Returns <i>true</i> if <b>haystack</b> begins with <b>needle</b>, <i>false</i> otherwise
  30. * @link https://www.php.net/manual/en/function.str-starts-with.php Official PHP documentation
  31. */
  32. function str_starts_with(string $haystack, string $needle): bool {
  33. return substr_compare($haystack, $needle, 0, strlen($needle)) === 0;
  34. }
  35. }
  36. if (!function_exists('str_ends_with')) {
  37. /**
  38. * Checks if a string ends with a given substring
  39. * <p>Performs a case-sensitive check indicating if <b>haystack</b> ends with <b>needle</b>.</p>
  40. * @param string $haystack The string to search in.
  41. * @param string $needle The substring to search for in the <b>haystack</b>.
  42. * @return bool Returns <i>true</i> if <b>haystack</b> ends with <b>needle</b>, <i>false</i> otherwise.
  43. */
  44. function str_ends_with(string $haystack, string $needle): bool {
  45. return substr_compare($haystack, $needle, -1*strlen($needle)) === 0;
  46. }
  47. }
  48. if (!function_exists('mb_strtoupper')) {
  49. function mb_strtoupper($string) {
  50. return strtoupper($string);
  51. }
  52. }
  53. if (!function_exists('check_float')) {
  54. function check_float($string) {
  55. $string = str_replace(",", ".", $string ?? '');
  56. return trim($string);
  57. }
  58. }
  59. if (!function_exists('check_str')) {
  60. function check_str($string, $trim = true) {
  61. global $db_type, $db;
  62. //when code in db is urlencoded the ' does not need to be modified
  63. if ($db_type == "sqlite") {
  64. if (function_exists('sqlite_escape_string')) {
  65. $string = sqlite_escape_string($string);
  66. } else {
  67. $string = str_replace("'", "''", $string);
  68. }
  69. }
  70. if ($db_type == "pgsql") {
  71. $string = str_replace("'", "''", $string);
  72. }
  73. if ($db_type == "mysql") {
  74. if (function_exists('mysql_real_escape_string')) {
  75. $tmp_str = mysql_real_escape_string($string);
  76. } else {
  77. $tmp_str = mysqli_real_escape_string($db, $string);
  78. }
  79. if (!empty($tmp_str)) {
  80. $string = $tmp_str;
  81. } else {
  82. $search = array("\x00", "\n", "\r", "\\", "'", "\"", "\x1a");
  83. $replace = array("\\x00", "\\n", "\\r", "\\\\", "\'", "\\\"", "\\\x1a");
  84. $string = str_replace($search, $replace, $string);
  85. }
  86. }
  87. $string = ($trim) ? trim($string) : $string;
  88. return $string;
  89. }
  90. }
  91. if (!function_exists('check_sql')) {
  92. function check_sql($string) {
  93. return trim($string); //remove white space
  94. }
  95. }
  96. if (!function_exists('check_cidr')) {
  97. function check_cidr($cidr, $ip_address) {
  98. if (isset($cidr) && !empty($cidr)) {
  99. list ($subnet, $mask) = explode('/', $cidr);
  100. return ( ip2long($ip_address) & ~((1 << (32 - $mask)) - 1) ) == ip2long($subnet);
  101. } else {
  102. return false;
  103. }
  104. }
  105. }
  106. if (!function_exists('fix_postback')) {
  107. function fix_postback($post_array) {
  108. foreach ($post_array as $index => $value) {
  109. if (is_array($value)) {
  110. fix_postback($value);
  111. } else {
  112. $value = str_replace('"', "&#34;", $value);
  113. $value = str_replace("'", "&#39;", $value);
  114. $post_array[$index] = $value;
  115. }
  116. }
  117. return $post_array;
  118. }
  119. }
  120. if (!function_exists('uuid')) {
  121. function uuid() {
  122. $uuid = null;
  123. if (PHP_OS === 'FreeBSD') {
  124. $uuid = trim(shell_exec("uuidgen"));
  125. if (is_uuid($uuid)) {
  126. return $uuid;
  127. } else {
  128. echo "Please install uuidgen.\n";
  129. exit;
  130. }
  131. }
  132. if (PHP_OS === 'Linux') {
  133. $uuid = trim(file_get_contents('/proc/sys/kernel/random/uuid'));
  134. if (is_uuid($uuid)) {
  135. return $uuid;
  136. } else {
  137. $uuid = trim(shell_exec("uuidgen"));
  138. if (is_uuid($uuid)) {
  139. return $uuid;
  140. } else {
  141. echo "Please install uuidgen.\n";
  142. exit;
  143. }
  144. }
  145. }
  146. if ((strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') && function_exists('com_create_guid')) {
  147. $uuid = trim(com_create_guid(), '{}');
  148. if (is_uuid($uuid)) {
  149. return $uuid;
  150. } else {
  151. echo "The com_create_guid() function failed to create a uuid.\n";
  152. exit;
  153. }
  154. }
  155. }
  156. }
  157. if (!function_exists('is_uuid')) {
  158. function is_uuid($str) {
  159. $is_uuid = false;
  160. if (gettype($str) == 'string') {
  161. if (substr_count($str, '-') != 0 && strlen($str) == 36) {
  162. $regex = '/^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i';
  163. $is_uuid = preg_match($regex, $str);
  164. } else if (strlen(preg_replace("#[^a-fA-F0-9]#", '', $str)) == 32) {
  165. $regex = '/^[0-9A-F]{32}$/i';
  166. $is_uuid = preg_match($regex, $str);
  167. }
  168. }
  169. return $is_uuid;
  170. }
  171. }
  172. if (!function_exists('is_xml')) {
  173. function is_xml($string) {
  174. $pattern = '/^<\?xml(?:\s+[^>]+\s*)?\?>\s*<(\w+)>.*<\/\1>\s*$/s';
  175. return preg_match($pattern, $string) === 1;
  176. }
  177. }
  178. if (!function_exists('recursive_copy')) {
  179. if (file_exists('/bin/cp')) {
  180. function recursive_copy($source, $destination, $options = '') {
  181. if (strtoupper(substr(PHP_OS, 0, 3)) === 'SUN') {
  182. //copy -R recursive, preserve attributes for SUN
  183. $cmd = 'cp -Rp ' . $source . '/* ' . $destination;
  184. } else {
  185. //copy -R recursive, -L follow symbolic links, -p preserve attributes for other Posix systemss
  186. $cmd = 'cp -RLp ' . $options . ' ' . $source . '/* ' . $destination;
  187. }
  188. exec($cmd);
  189. }
  190. } elseif (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
  191. function recursive_copy($source, $destination, $options = '') {
  192. $source = normalize_path_to_os($source);
  193. $destination = normalize_path_to_os($destination);
  194. exec("xcopy /E /Y \"$source\" \"$destination\"");
  195. }
  196. } else {
  197. function recursive_copy($source, $destination, $options = '') {
  198. $dir = opendir($source);
  199. if (!$dir) {
  200. throw new Exception("recursive_copy() source directory '" . $source . "' does not exist.");
  201. }
  202. if (!is_dir($destination)) {
  203. if (!mkdir($destination, 02770, true)) {
  204. throw new Exception("recursive_copy() failed to create destination directory '" . $destination . "'");
  205. }
  206. }
  207. while (false !== ( $file = readdir($dir))) {
  208. if (( $file != '.' ) && ( $file != '..' )) {
  209. if (is_dir($source . '/' . $file)) {
  210. recursive_copy($source . '/' . $file, $destination . '/' . $file);
  211. } else {
  212. copy($source . '/' . $file, $destination . '/' . $file);
  213. }
  214. }
  215. }
  216. closedir($dir);
  217. }
  218. }
  219. }
  220. if (!function_exists('recursive_delete')) {
  221. if (file_exists('/usr/bin/find')) {
  222. function recursive_delete($directory) {
  223. if (isset($directory) && strlen($directory) > 8) {
  224. exec('/usr/bin/find ' . $directory . '/* -name "*" -delete');
  225. //exec('rm -Rf '.$directory.'/*');
  226. clearstatcache();
  227. }
  228. }
  229. } elseif (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
  230. function recursive_delete($directory) {
  231. $directory = normalize_path_to_os($directory);
  232. //$this->write_debug("del /S /F /Q \"$dir\"");
  233. exec("del /S /F /Q \"$directory\"");
  234. clearstatcache();
  235. }
  236. } else {
  237. function recursive_delete($directory) {
  238. foreach (glob($directory) as $file) {
  239. if (is_dir($file)) {
  240. //$this->write_debug("rm dir: ".$file);
  241. recursive_delete("$file/*");
  242. rmdir($file);
  243. } else {
  244. //$this->write_debug("delete file: ".$file);
  245. unlink($file);
  246. }
  247. }
  248. clearstatcache();
  249. }
  250. }
  251. }
  252. if (!function_exists('if_group')) {
  253. function if_group($group) {
  254. //set default false
  255. $result = false;
  256. //search for the permission
  257. if (isset($_SESSION['groups']) && count($_SESSION["groups"]) > 0) {
  258. foreach ($_SESSION["groups"] as $row) {
  259. if ($row['group_name'] == $group) {
  260. $result = true;
  261. break;
  262. }
  263. }
  264. }
  265. //return the result
  266. return $result;
  267. }
  268. }
  269. //check if the permission exists
  270. if (!function_exists('permission_exists')) {
  271. function permission_exists($permission_name) {
  272. global $domain_uuid, $user_uuid;
  273. $database = database::new();
  274. $permission = permissions::new($database, $domain_uuid, $user_uuid);
  275. return $permission->exists($permission_name);
  276. }
  277. }
  278. if (!function_exists('if_group_member')) {
  279. function if_group_member($group_members, $group) {
  280. if (stripos($group_members, "||" . $group . "||") === false) {
  281. return false; //group does not exist
  282. } else {
  283. return true; //group exists
  284. }
  285. }
  286. }
  287. if (!function_exists('superadmin_list')) {
  288. function superadmin_list() {
  289. global $domain_uuid;
  290. $sql = "select * from v_user_groups ";
  291. $sql .= "where group_name = 'superadmin' ";
  292. $database = database::new();
  293. $result = $database->select($sql, null, 'all');
  294. $superadmin_list = "||";
  295. if (is_array($result) && @sizeof($result) != 0) {
  296. foreach ($result as $field) {
  297. //get the list of superadmins
  298. $superadmin_list .= $field['user_uuid'] . "||";
  299. }
  300. }
  301. unset($sql, $result, $field);
  302. return $superadmin_list;
  303. }
  304. }
  305. if (!function_exists('if_superadmin')) {
  306. function if_superadmin($superadmin_list, $user_uuid) {
  307. if (stripos($superadmin_list, "||" . $user_uuid . "||") === false) {
  308. return false;
  309. } else {
  310. return true; //user_uuid exists
  311. }
  312. }
  313. }
  314. if (!function_exists('html_select_other')) {
  315. function html_select_other($table_name, $field_name, $sql_where_optional, $field_current_value, $sql_order_by = null, $label_other = 'Other...') {
  316. //html select other: build a select box from distinct items in db with option for other
  317. global $domain_uuid;
  318. $table_name = preg_replace("#[^a-zA-Z0-9_]#", "", $table_name);
  319. $field_name = preg_replace("#[^a-zA-Z0-9_]#", "", $field_name);
  320. $html = "<table border='0' cellpadding='1' cellspacing='0'>\n";
  321. $html .= "<tr>\n";
  322. $html .= "<td id=\"cell" . escape($field_name) . "1\">\n";
  323. $html .= "\n";
  324. $html .= "<select id=\"" . escape($field_name) . "\" name=\"" . escape($field_name) . "\" class='formfld' onchange=\"if (document.getElementById('" . $field_name . "').value == 'Other') { /*enabled*/ document.getElementById('" . $field_name . "_other').style.display=''; document.getElementById('" . $field_name . "_other').className='formfld'; document.getElementById('" . $field_name . "_other').focus(); } else { /*disabled*/ document.getElementById('" . $field_name . "_other').value = ''; document.getElementById('" . $field_name . "_other').style.display='none'; } \">\n";
  325. $html .= "<option value=''></option>\n";
  326. $sql = "select distinct(" . $field_name . ") as " . $field_name . " ";
  327. $sql .= "from " . $table_name . " " . $sql_where_optional . " ";
  328. $sql .= "order by " . (!empty($sql_order_by) ? $sql_order_by : $field_name . ' asc');
  329. $database = database::new();
  330. $result = $database->select($sql, null, 'all');
  331. if (is_array($result) && @sizeof($result) != 0) {
  332. foreach ($result as $field) {
  333. if (!empty($field[$field_name])) {
  334. $html .= "<option value=\"" . escape($field[$field_name]) . "\" " . ($field_current_value == $field[$field_name] ? "selected='selected'" : null) . ">" . escape($field[$field_name]) . "</option>\n";
  335. }
  336. }
  337. }
  338. unset($sql, $result, $field);
  339. $html .= "<option value='' disabled='disabled'></option>\n";
  340. $html .= "<option value='Other'>" . $label_other . "</option>\n";
  341. $html .= "</select>\n";
  342. $html .= "</td>\n";
  343. $html .= "<td id=\"cell" . $field_name . "2\" width='5'>\n";
  344. $html .= "<input id=\"" . $field_name . "_other\" name=\"" . $field_name . "_other\" value='' type='text' class='formfld' style='display: none;'>\n";
  345. $html .= "</td>\n";
  346. $html .= "</tr>\n";
  347. $html .= "</table>";
  348. return $html;
  349. }
  350. }
  351. if (!function_exists('html_select')) {
  352. function html_select($table_name, $field_name, $sql_where_optional, $field_current_value, $field_value = '', $style = '', $on_change = '') {
  353. //html select: build a select box from distinct items in db
  354. global $domain_uuid;
  355. $table_name = preg_replace("#[^a-zA-Z0-9_]#", "", $table_name);
  356. $field_name = preg_replace("#[^a-zA-Z0-9_]#", "", $field_name);
  357. $field_value = preg_replace("#[^a-zA-Z0-9_]#", "", $field_value);
  358. if (!empty($field_value)) {
  359. $html .= "<select id=\"" . $field_value . "\" name=\"" . $field_value . "\" class='formfld' style='" . $style . "' " . (!empty($on_change) ? "onchange=\"" . $on_change . "\"" : null) . ">\n";
  360. $html .= " <option value=\"\"></option>\n";
  361. $sql = "select distinct(" . $field_name . ") as " . $field_name . ", " . $field_value . " from " . $table_name . " " . $sql_where_optional . " order by " . $field_name . " asc ";
  362. } else {
  363. $html .= "<select id=\"" . $field_name . "\" name=\"" . $field_name . "\" class='formfld' style='" . $style . "' " . (!empty($on_change) ? "onchange=\"" . $on_change . "\"" : null) . ">\n";
  364. $html .= " <option value=\"\"></option>\n";
  365. $sql = "select distinct(" . $field_name . ") as " . $field_name . " from " . $table_name . " " . $sql_where_optional . " ";
  366. }
  367. $database = database::new();
  368. $result = $database->select($sql, null, 'all');
  369. if (is_array($result) && @sizeof($result) != 0) {
  370. foreach ($result as $field) {
  371. if (!empty($field[$field_name])) {
  372. $selected = $field_current_value == $field[$field_name] ? "selected='selected'" : null;
  373. $array_key = empty($field_value) ? $field_name : $field_value;
  374. $html .= "<option value=\"" . urlencode($field[$array_key]) . "\" " . $selected . ">" . urlencode($field[$field_name]) . "</option>\n";
  375. }
  376. }
  377. }
  378. unset($sql, $result, $field);
  379. $html .= "</select>\n";
  380. return $html;
  381. }
  382. }
  383. if (!function_exists('th_order_by')) {
  384. //html table header order by
  385. function th_order_by($field_name, $column_title, $order_by, $order, $app_uuid = '', $css = '', $http_get_params = '', $description = '') {
  386. global $text;
  387. if (is_uuid($app_uuid) > 0) {
  388. $app_uuid = "&app_uuid=" . urlencode($app_uuid);
  389. } // accomodate need to pass app_uuid where necessary (inbound/outbound routes lists)
  390. $field_name = preg_replace("#[^a-zA-Z0-9_]#", "", $field_name);
  391. $field_value = preg_replace("#[^a-zA-Z0-9_]#", "", $field_value ?? '');
  392. $sanitized_parameters = '';
  393. if (isset($http_get_params) && !empty($http_get_params)) {
  394. $parameters = explode('&', $http_get_params);
  395. if (is_array($parameters)) {
  396. foreach ($parameters as $parameter) {
  397. if (substr_count($parameter, '=') != 0) {
  398. $array = explode('=', $parameter);
  399. $key = preg_replace('#[^a-zA-Z0-9_\-]#', '', $array['0']);
  400. $value = urldecode($array['1']);
  401. if ($key == 'order_by' && !empty($value)) {
  402. //validate order by
  403. $sanitized_parameters .= "&order_by=" . preg_replace('#[^a-zA-Z0-9_\-]#', '', $value);
  404. } else if ($key == 'order' && !empty($value)) {
  405. //validate order
  406. switch ($value) {
  407. case 'asc':
  408. $sanitized_parameters .= "&order=asc";
  409. break;
  410. case 'desc':
  411. $sanitized_parameters .= "&order=desc";
  412. break;
  413. }
  414. } else if (!empty($value) && is_numeric($value)) {
  415. $sanitized_parameters .= "&" . $key . "=" . $value;
  416. } else {
  417. $sanitized_parameters .= "&" . $key . "=" . urlencode($value);
  418. }
  419. }
  420. }
  421. }
  422. }
  423. $html = "<th " . $css . " nowrap='nowrap'>";
  424. $description = empty($description) ? '' : $description . ', ';
  425. if (empty($order_by)) {
  426. $order = 'asc';
  427. }
  428. if ($order_by == $field_name) {
  429. if ($order == "asc") {
  430. $description .= $text['label-order'] . ' ' . $text['label-descending'];
  431. $html .= "<a href='?order_by=" . urlencode($field_name) . "&order=desc" . $app_uuid . $sanitized_parameters . "' title=\"" . escape($description) . "\">" . escape($column_title) . "</a>";
  432. } else {
  433. $description .= $text['label-order'] . ' ' . $text['label-ascending'];
  434. $html .= "<a href='?order_by=" . urlencode($field_name) . "&order=asc" . $app_uuid . $sanitized_parameters . "' title=\"" . escape($description) . "\">" . escape($column_title) . "</a>";
  435. }
  436. } else {
  437. $description .= $text['label-order'] . ' ' . $text['label-ascending'];
  438. $html .= "<a href='?order_by=" . urlencode($field_name) . "&order=asc" . $app_uuid . $sanitized_parameters . "' title=\"" . escape($description) . "\">" . escape($column_title) . "</a>";
  439. }
  440. $html .= "</th>";
  441. return $html;
  442. }
  443. }
  444. if (!function_exists('get_ext')) {
  445. function get_ext($filename) {
  446. preg_match('/[^?]*/', $filename, $matches);
  447. $string = $matches[0];
  448. $pattern = preg_split('/\./', $string, -1, PREG_SPLIT_OFFSET_CAPTURE);
  449. // check if there is any extension
  450. if (count($pattern) == 1) {
  451. //echo 'No File Extension Present';
  452. return '';
  453. }
  454. if (count($pattern) > 1) {
  455. $filenamepart = $pattern[count($pattern) - 1][0];
  456. preg_match('/[^?]*/', $filenamepart, $matches);
  457. return $matches[0];
  458. }
  459. }
  460. //echo "ext: ".get_ext('test.txt');
  461. }
  462. if (!function_exists('file_upload')) {
  463. function file_upload($field = '', $file_type = '', $dest_dir = '') {
  464. $uploadtempdir = $_ENV["TEMP"] . "\\";
  465. ini_set('upload_tmp_dir', $uploadtempdir);
  466. $tmp_name = $_FILES[$field]["tmp_name"];
  467. $file_name = $_FILES[$field]["name"];
  468. $file_type = $_FILES[$field]["type"];
  469. $file_size = $_FILES[$field]["size"];
  470. $file_ext = get_ext($file_name);
  471. $file_name_orig = $file_name;
  472. $file_name_base = substr($file_name, 0, (strlen($file_name) - (strlen($file_ext) + 1)));
  473. //$dest_dir = '/tmp';
  474. if ($file_size == 0) {
  475. return;
  476. }
  477. if (!is_dir($dest_dir)) {
  478. echo "dest_dir not found<br />\n";
  479. return;
  480. }
  481. //check if allowed file type
  482. if ($file_type == "img") {
  483. switch (strtolower($file_ext)) {
  484. case "jpg":
  485. case "png":
  486. case "gif":
  487. case "bmp":
  488. case "psd":
  489. case "tif": break;
  490. default: return false;
  491. }
  492. }
  493. if ($file_type == "file") {
  494. switch (strtolower($file_ext)) {
  495. case "doc":
  496. case "pdf":
  497. case "ppt":
  498. case "xls":
  499. case "zip":
  500. case "exe": break;
  501. default: return false;
  502. }
  503. }
  504. //find unique filename: check if file exists if it does then increment the filename
  505. $i = 1;
  506. while (file_exists($dest_dir . '/' . $file_name)) {
  507. if (!empty($file_ext)) {
  508. $file_name = $file_name_base . $i . '.' . $file_ext;
  509. } else {
  510. $file_name = $file_name_orig . $i;
  511. }
  512. $i++;
  513. }
  514. //echo "file_type: ".$file_type."<br />\n";
  515. //echo "tmp_name: ".$tmp_name."<br />\n";
  516. //echo "file_name: ".$file_name."<br />\n";
  517. //echo "file_ext: ".$file_ext."<br />\n";
  518. //echo "file_name_orig: ".$file_name_orig."<br />\n";
  519. //echo "file_name_base: ".$file_name_base."<br />\n";
  520. //echo "dest_dir: ".$dest_dir."<br />\n";
  521. //move the file to upload directory
  522. //bool move_uploaded_file ( string $filename, string $destination )
  523. if (move_uploaded_file($tmp_name, $dest_dir . '/' . $file_name)) {
  524. return $file_name;
  525. } else {
  526. echo "File upload failed! Here's some debugging info:\n";
  527. return false;
  528. }
  529. exit;
  530. }
  531. }
  532. if (!function_exists('sys_get_temp_dir')) {
  533. function sys_get_temp_dir() {
  534. if ($temp = getenv('TMP')) {
  535. return $temp;
  536. }
  537. if ($temp = getenv('TEMP')) {
  538. return $temp;
  539. }
  540. if ($temp = getenv('TMPDIR')) {
  541. return $temp;
  542. }
  543. $temp = tempnam(__FILE__, '');
  544. if (file_exists($temp)) {
  545. unlink($temp);
  546. return dirname($temp);
  547. }
  548. return null;
  549. }
  550. }
  551. //echo realpath(sys_get_temp_dir());
  552. if (!function_exists('normalize_path')) {
  553. //don't use DIRECTORY_SEPARATOR as it will change on a per platform basis and we need consistency
  554. function normalize_path($path) {
  555. return str_replace(array('/', '\\'), '/', $path);
  556. }
  557. }
  558. if (!function_exists('normalize_path_to_os')) {
  559. function normalize_path_to_os($path) {
  560. return str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $path);
  561. }
  562. }
  563. if (!function_exists('username_exists')) {
  564. function username_exists($username) {
  565. global $domain_uuid;
  566. $sql = "select count(*) from v_users ";
  567. $sql .= "where domain_uuid = :domain_uuid ";
  568. $sql .= "and username = :username ";
  569. $parameters['domain_uuid'] = $domain_uuid;
  570. $parameters['username'] = $username;
  571. $database = database::new();
  572. $num_rows = $database->select($sql, $parameters, 'column');
  573. return $num_rows > 0 ? true : false;
  574. }
  575. }
  576. if (!function_exists('add_extension_user')) {
  577. function add_extension_user($extension_uuid, $username) {
  578. global $domain_uuid;
  579. //get the user_uuid by using the username
  580. $sql = "select user_uuid from v_users ";
  581. $sql .= "where domain_uuid = :domain_uuid ";
  582. $sql .= "and username = :username ";
  583. $parameters['domain_uuid'] = $domain_uuid;
  584. $parameters['username'] = $username;
  585. $database = database::new();
  586. $user_uuid = $database->select($sql, $parameters, 'column');
  587. unset($sql, $parameters);
  588. if (is_uuid($user_uuid)) {
  589. //check if the user_uuid exists in v_extension_users
  590. $sql = "select count(*) from v_extension_users ";
  591. $sql .= "where domain_uuid = :domain_uuid ";
  592. $sql .= "and user_uuid = :user_uuid ";
  593. $parameters['domain_uuid'] = $domain_uuid;
  594. $parameters['user_uuid'] = $user_uuid;
  595. $database = database::new();
  596. $num_rows = $database->select($sql, $parameters, 'column');
  597. unset($sql, $parameters);
  598. //assign the extension to the user
  599. if ($num_rows == 0) {
  600. //build insert array
  601. $extension_user_uuid = uuid();
  602. $array['extension_users'][$x]['extension_user_uuid'] = $extension_user_uuid;
  603. $array['extension_users'][$x]['domain_uuid'] = $domain_uuid;
  604. $array['extension_users'][$x]['extension_uuid'] = $extension_uuid;
  605. $array['extension_users'][$x]['user_uuid'] = $row["user_uuid"];
  606. //grant temporary permissions
  607. $p = permissions::new();
  608. $p->add('extension_user_add', 'temp');
  609. //execute insert
  610. $database = database::new();
  611. $database->app_name = 'function-add_extension_user';
  612. $database->app_uuid = 'e68d9689-2769-e013-28fa-6214bf47fca3';
  613. $database->save($array);
  614. unset($array);
  615. //revoke temporary permissions
  616. $p->delete('extension_user_add', 'temp');
  617. }
  618. }
  619. }
  620. }
  621. if (!function_exists('user_add')) {
  622. function user_add($username, $password, $user_email = '') {
  623. global $domain_uuid;
  624. if (empty($username)) {
  625. return false;
  626. }
  627. if (empty($password)) {
  628. return false;
  629. }
  630. if (!username_exists($username)) {
  631. //build user insert array
  632. $user_uuid = uuid();
  633. $salt = generate_password('20', '4');
  634. $array['users'][0]['user_uuid'] = $user_uuid;
  635. $array['users'][0]['domain_uuid'] = $domain_uuid;
  636. $array['users'][0]['username'] = $username;
  637. $array['users'][0]['password'] = md5($salt . $password);
  638. $array['users'][0]['salt'] = $salt;
  639. if (valid_email($user_email)) {
  640. $array['users'][0]['user_email'] = $user_email;
  641. }
  642. $array['users'][0]['add_date'] = 'now()';
  643. $array['users'][0]['add_user'] = $_SESSION["username"];
  644. //build user group insert array
  645. $user_group_uuid = uuid();
  646. $array['user_groups'][0]['user_group_uuid'] = $user_group_uuid;
  647. $array['user_groups'][0]['domain_uuid'] = $domain_uuid;
  648. $array['user_groups'][0]['group_name'] = 'user';
  649. $array['user_groups'][0]['user_uuid'] = $user_uuid;
  650. //grant temporary permissions
  651. $p = permissions::new();
  652. $p->add('user_add', 'temp');
  653. $p->add('user_group_add', 'temp');
  654. //execute insert
  655. $database = database::new();
  656. $database->app_name = 'function-user_add';
  657. $database->app_uuid = '15a8d74b-ac7e-4468-add4-3e6ebdcb8e22';
  658. $database->save($array);
  659. unset($array);
  660. //revoke temporary permissions
  661. $p->delete('user_add', 'temp');
  662. $p->delete('user_group_add', 'temp');
  663. }
  664. }
  665. }
  666. function switch_module_is_running($mod, event_socket $esl = null) {
  667. //if the object does not exist create it
  668. if ($esl === null) {
  669. $esl = event_socket::create();
  670. }
  671. //if we are not connected to freeswitch show an error message
  672. if (!$esl->is_connected()) {
  673. return false;
  674. }
  675. //send the api command to check if the module exists
  676. $switch_result = event_socket::api("module_exists $mod");
  677. return (trim($switch_result) == "true");
  678. }
  679. //print (switch_module_is_running('mod_spidermonkey') ? "true" : "false");
  680. //format a number (n) replace with a number (r) remove the number
  681. function format_string($format, $data) {
  682. //nothing to do so return
  683. if (empty($format))
  684. return $data;
  685. //preset values
  686. $x = 0;
  687. $tmp = '';
  688. //count the characters
  689. $format_count = substr_count($format, 'x');
  690. $format_count = $format_count + substr_count($format, 'R');
  691. $format_count = $format_count + substr_count($format, 'r');
  692. //format the string if it matches
  693. if ($format_count == strlen($data)) {
  694. for ($i = 0; $i <= strlen($format); $i++) {
  695. $tmp_format = strtolower(substr($format, $i, 1));
  696. if ($tmp_format == 'x') {
  697. $tmp .= substr($data, $x, 1);
  698. $x++;
  699. } elseif ($tmp_format == 'r') {
  700. $x++;
  701. } else {
  702. $tmp .= $tmp_format;
  703. }
  704. }
  705. }
  706. if (empty($tmp)) {
  707. return $data;
  708. } else {
  709. return $tmp;
  710. }
  711. }
  712. //get the format and use it to format the phone number
  713. function format_phone($phone_number) {
  714. if (is_numeric(trim($phone_number ?? '', ' +'))) {
  715. if (isset($_SESSION["format"]["phone"])) {
  716. $phone_number = trim($phone_number, ' +');
  717. foreach ($_SESSION["format"]["phone"] as $format) {
  718. $format_count = substr_count($format, 'x');
  719. $format_count = $format_count + substr_count($format, 'R');
  720. $format_count = $format_count + substr_count($format, 'r');
  721. if ($format_count == strlen($phone_number)) {
  722. //format the number
  723. $phone_number = format_string($format, $phone_number);
  724. }
  725. }
  726. }
  727. }
  728. return $phone_number;
  729. }
  730. //format seconds into hh:mm:ss
  731. function format_hours($seconds) {
  732. $seconds = (int) $seconds; //convert seconds to an integer
  733. $hours = floor($seconds / 3600);
  734. $minutes = floor(floor($seconds / 60) % 60);
  735. $seconds = $seconds % 60;
  736. return sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds);
  737. }
  738. //format seconds
  739. function format_seconds($seconds) {
  740. return gmdate("H:i:s", $seconds);
  741. }
  742. //browser detection without browscap.ini dependency
  743. function http_user_agent($info = '') {
  744. //set default values
  745. $user_agent = $_SERVER['HTTP_USER_AGENT'];
  746. $browser_name = 'Unknown';
  747. $platform = 'Unknown';
  748. $version = '';
  749. $mobile = false;
  750. //get the platform
  751. if (preg_match('/linux/i', $user_agent)) {
  752. $platform = 'Linux';
  753. } elseif (preg_match('/macintosh|mac os x/i', $user_agent)) {
  754. $platform = 'Apple';
  755. } elseif (preg_match('/windows|win32/i', $user_agent)) {
  756. $platform = 'Windows';
  757. }
  758. //set mobile to true or false
  759. if (preg_match('/mobile/i', $user_agent)) {
  760. $platform = 'Mobile';
  761. $mobile = true;
  762. } elseif (preg_match('/android/i', $user_agent)) {
  763. $platform = 'Android';
  764. $mobile = true;
  765. }
  766. //get the name of the useragent
  767. if (preg_match('/MSIE/i', $user_agent) || preg_match('/Trident/i', $user_agent)) {
  768. $browser_name = 'Internet Explorer';
  769. $browser_name_short = 'MSIE';
  770. } elseif (preg_match('/Firefox/i', $user_agent)) {
  771. $browser_name = 'Mozilla Firefox';
  772. $browser_name_short = 'Firefox';
  773. } elseif (preg_match('/Chrome/i', $user_agent)) {
  774. $browser_name = 'Google Chrome';
  775. $browser_name_short = 'Chrome';
  776. } elseif (preg_match('/Safari/i', $user_agent)) {
  777. $browser_name = 'Apple Safari';
  778. $browser_name_short = 'Safari';
  779. } elseif (preg_match('/Opera/i', $user_agent)) {
  780. $browser_name = 'Opera';
  781. $browser_name_short = 'Opera';
  782. } elseif (preg_match('/Netscape/i', $user_agent)) {
  783. $browser_name = 'Netscape';
  784. $browser_name_short = 'Netscape';
  785. } else {
  786. $browser_name = 'Unknown';
  787. $browser_name_short = 'Unknown';
  788. }
  789. //finally get the correct version number
  790. $known = array('Version', $browser_name_short, 'other');
  791. $pattern = '#(?<browser>' . join('|', $known) . ')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';
  792. if (!preg_match_all($pattern, $user_agent, $matches)) {
  793. //we have no matching number just continue
  794. }
  795. //see how many we have
  796. $i = count($matches['browser']);
  797. if ($i != 1) {
  798. //we will have two since we are not using 'other' argument yet
  799. //see if version is before or after the name
  800. if (strripos($user_agent, "Version") < strripos($user_agent, $browser_name_short)) {
  801. $version = $matches['version'][0];
  802. } else {
  803. $version = $matches['version'][1];
  804. }
  805. } else {
  806. $version = $matches['version'][0];
  807. }
  808. //check if we have a number
  809. if ($version == null || $version == "") {
  810. $version = "?";
  811. }
  812. //return the data
  813. switch ($info) {
  814. case "agent": return $user_agent;
  815. break;
  816. case "name": return $browser_name;
  817. break;
  818. case "name_short": return $browser_name_short;
  819. break;
  820. case "version": return $version;
  821. break;
  822. case "platform": return $platform;
  823. break;
  824. case "mobile": return $mobile;
  825. break;
  826. case "pattern": return $pattern;
  827. break;
  828. default :
  829. return array(
  830. 'user_agent' => $user_agent,
  831. 'name' => $browser_name,
  832. 'name_short' => $browser_name_short,
  833. 'version' => $version,
  834. 'platform' => $platform,
  835. 'mobile' => $mobile,
  836. 'pattern' => $pattern
  837. );
  838. }
  839. }
  840. //tail php function for non posix systems
  841. function tail($file, $num_to_get = 10) {
  842. $esl = fopen($file, 'r');
  843. $position = filesize($file);
  844. $chunklen = 4096;
  845. if ($position - $chunklen <= 0) {
  846. fseek($esl, 0);
  847. } else {
  848. fseek($esl, $position - $chunklen);
  849. }
  850. $data = "";
  851. $ret = "";
  852. $lc = 0;
  853. while ($chunklen > 0) {
  854. $data = fread($esl, $chunklen);
  855. $dl = strlen($data);
  856. for ($i = $dl - 1; $i >= 0; $i--) {
  857. if ($data[$i] == "\n") {
  858. if ($lc == 0 && $ret != "")
  859. $lc++;
  860. $lc++;
  861. if ($lc > $num_to_get)
  862. return $ret;
  863. }
  864. $ret = $data[$i] . $ret;
  865. }
  866. if ($position - $chunklen <= 0) {
  867. fseek($esl, 0);
  868. $chunklen = $chunklen - abs($position - $chunklen);
  869. } else {
  870. fseek($esl, $position - $chunklen);
  871. }
  872. $position = $position - $chunklen;
  873. }
  874. return $ret;
  875. }
  876. //generate a random password with upper, lowercase and symbols
  877. function generate_password($length = 0, $strength = 0) {
  878. $password = '';
  879. $chars = '';
  880. if ($length === 0 && $strength === 0) { //set length and strenth if specified in default settings and strength isn't numeric-only
  881. $length = (is_numeric($_SESSION["users"]["password_length"]["numeric"])) ? $_SESSION["users"]["password_length"]["numeric"] : 20;
  882. $strength = (is_numeric($_SESSION["users"]["password_strength"]["numeric"])) ? $_SESSION["users"]["password_strength"]["numeric"] : 4;
  883. }
  884. if ($strength >= 1) {
  885. $chars .= "0123456789";
  886. }
  887. if ($strength >= 2) {
  888. $chars .= "abcdefghijkmnopqrstuvwxyz";
  889. }
  890. if ($strength >= 3) {
  891. $chars .= "ABCDEFGHIJKLMNPQRSTUVWXYZ";
  892. }
  893. if ($strength >= 4) {
  894. $chars .= "!^$%*?.";
  895. }
  896. for ($i = 0; $i < $length; $i++) {
  897. $password .= $chars[random_int(0, strlen($chars) - 1)];
  898. }
  899. return $password;
  900. }
  901. //check password strength against requirements (if any)
  902. function check_password_strength($password, $text, $type = 'default') {
  903. if (!empty($password)) {
  904. if ($type == 'default') {
  905. $req['length'] = $_SESSION['extension']['password_length']['numeric'];
  906. $req['number'] = ($_SESSION['extension']['password_number']['boolean'] == 'true') ? true : false;
  907. $req['lowercase'] = ($_SESSION['extension']['password_lowercase']['boolean'] == 'true') ? true : false;
  908. $req['uppercase'] = ($_SESSION['extension']['password_uppercase']['boolean'] == 'true') ? true : false;
  909. $req['special'] = ($_SESSION['extension']['password_special']['boolean'] == 'true') ? true : false;
  910. } elseif ($type == 'user') {
  911. $req['length'] = $_SESSION['user']['password_length']['numeric'];
  912. $req['number'] = ($_SESSION['user']['password_number']['boolean'] == 'true') ? true : false;
  913. $req['lowercase'] = ($_SESSION['user']['password_lowercase']['boolean'] == 'true') ? true : false;
  914. $req['uppercase'] = ($_SESSION['user']['password_uppercase']['boolean'] == 'true') ? true : false;
  915. $req['special'] = ($_SESSION['user']['password_special']['boolean'] == 'true') ? true : false;
  916. }
  917. if (is_numeric($req['length']) && $req['length'] != 0 && !preg_match_all('$\S*(?=\S{' . $req['length'] . ',})\S*$', $password)) { // length
  918. $msg_errors[] = $req['length'] . '+ ' . $text['label-characters'];
  919. }
  920. if ($req['number'] && !preg_match_all('$\S*(?=\S*[\d])\S*$', $password)) { //number
  921. $msg_errors[] = '1+ ' . $text['label-numbers'];
  922. }
  923. if ($req['lowercase'] && !preg_match_all('$\S*(?=\S*[a-z])\S*$', $password)) { //lowercase
  924. $msg_errors[] = '1+ ' . $text['label-lowercase_letters'];
  925. }
  926. if ($req['uppercase'] && !preg_match_all('$\S*(?=\S*[A-Z])\S*$', $password)) { //uppercase
  927. $msg_errors[] = '1+ ' . $text['label-uppercase_letters'];
  928. }
  929. if ($req['special'] && !preg_match_all('$\S*(?=\S*[\W])\S*$', $password)) { //special
  930. $msg_errors[] = '1+ ' . $text['label-special_characters'];
  931. }
  932. if (is_array($msg_errors) && sizeof($msg_errors) > 0) {
  933. message::add($_SESSION["message"] = $text['message-password_requirements'] . ': ' . implode(', ', $msg_errors), 'negative', 6000);
  934. return false;
  935. } else {
  936. return true;
  937. }
  938. }
  939. return true;
  940. }
  941. //based on Wez Furlong do_post_request
  942. if (!function_exists('send_http_request')) {
  943. function send_http_request($url, $data, $method = "POST", $optional_headers = null) {
  944. $params = array('http' => array(
  945. 'method' => $method,
  946. 'content' => $data
  947. ));
  948. if ($optional_headers !== null) {
  949. $params['http']['header'] = $optional_headers;
  950. }
  951. $ctx = stream_context_create($params);
  952. $esl = @fopen($url, 'rb', false, $ctx);
  953. if (!$esl) {
  954. throw new Exception("Problem with $url, $php_errormsg");
  955. }
  956. $response = @stream_get_contents($esl);
  957. if ($response === false) {
  958. throw new Exception("Problem reading data from $url, $php_errormsg");
  959. }
  960. return $response;
  961. }
  962. }
  963. //convert the string to a named array
  964. if (!function_exists('csv_to_named_array')) {
  965. function csv_to_named_array($tmp_str, $tmp_delimiter) {
  966. $tmp_array = explode("\n", $tmp_str);
  967. $result = array();
  968. if (trim(strtoupper($tmp_array[0])) !== "+OK") {
  969. $tmp_field_name_array = explode($tmp_delimiter, $tmp_array[0]);
  970. $x = 0;
  971. foreach ($tmp_array as $row) {
  972. if ($x > 0) {
  973. $tmp_field_value_array = explode($tmp_delimiter, $tmp_array[$x]);
  974. $y = 0;
  975. foreach ($tmp_field_value_array as $tmp_value) {
  976. $tmp_name = $tmp_field_name_array[$y];
  977. if (trim(strtoupper($tmp_value)) !== "+OK") {
  978. $result[$x][$tmp_name] = $tmp_value;
  979. }
  980. $y++;
  981. }
  982. }
  983. $x++;
  984. }
  985. unset($row);
  986. }
  987. return $result;
  988. }
  989. }
  990. function get_time_zone_offset($remote_tz, $origin_tz = 'UTC') {
  991. $origin_dtz = new DateTimeZone($origin_tz);
  992. $remote_dtz = new DateTimeZone($remote_tz);
  993. $origin_dt = new DateTime("now", $origin_dtz);
  994. $remote_dt = new DateTime("now", $remote_dtz);
  995. $offset = $remote_dtz->getOffset($remote_dt) - $origin_dtz->getOffset($origin_dt);
  996. return $offset;
  997. }
  998. function number_pad($number, $n) {
  999. return str_pad((int) $number, $n, "0", STR_PAD_LEFT);
  1000. }
  1001. // validate email address syntax
  1002. if (!function_exists('valid_email')) {
  1003. function valid_email($email) {
  1004. return (filter_var($email, FILTER_VALIDATE_EMAIL)) ? true : false;
  1005. }
  1006. }
  1007. //function to convert hexidecimal color value to rgb string/array value
  1008. if (!function_exists('hex_to_rgb')) {
  1009. function hex_to_rgb($hex, $delim = null, $include_alpha = false, $alpha = 1) {
  1010. $hex = str_replace("#", "", $hex);
  1011. if (strlen($hex) == 3) {
  1012. $r = hexdec(substr($hex, 0, 1) . substr($hex, 0, 1));
  1013. $g = hexdec(substr($hex, 1, 1) . substr($hex, 1, 1));
  1014. $b = hexdec(substr($hex, 2, 1) . substr($hex, 2, 1));
  1015. }
  1016. else {
  1017. $r = hexdec(substr($hex, 0, 2));
  1018. $g = hexdec(substr($hex, 2, 2));
  1019. $b = hexdec(substr($hex, 4, 2));
  1020. }
  1021. $rgb = array($r, $g, $b);
  1022. if ($include_alpha) { $rgb[] = $alpha; }
  1023. if (!empty($delim)) {
  1024. return implode($delim, $rgb); // return rgb delimited string
  1025. }
  1026. else {
  1027. return $rgb; // return array of rgb values
  1028. }
  1029. }
  1030. }
  1031. //function to convert a hex or rgb/a color to an rgba array
  1032. if (!function_exists('color_to_rgba_array')) {
  1033. function color_to_rgba_array($string, $alpha = null) {
  1034. if (!empty($string)) {
  1035. if (strpos($string, '#') === 0) { //is hex
  1036. return hex_to_rgb($string, null, true, $alpha);
  1037. }
  1038. else if (strpos($string, 'rgb') === 0) { //is rgb/a
  1039. $string = str_replace(['rgba(','rgb(',')'], '', $string); //values to csv
  1040. $array = explode(',', $string); //create array
  1041. if (!empty($array)) {
  1042. if (@sizeof($array) == 3) { //add alpha
  1043. $array[] = $alpha ?? 1;
  1044. }
  1045. else if (@sizeof($array) == 4 && !empty($alpha)) { //replace alpha
  1046. $array[3] = $alpha;
  1047. }
  1048. }
  1049. return !empty($array) && is_array($array) ? $array : false;
  1050. }
  1051. }
  1052. return false;
  1053. }
  1054. }
  1055. //function to get a color's luminence level -- dependencies: rgb_to_hsl()
  1056. if (!function_exists('get_color_luminence')) {
  1057. function get_color_luminence($color) {
  1058. //convert hex to rgb
  1059. if (substr_count($color, ',') == 0) {
  1060. $color = str_replace(' ', '', $color);
  1061. $color = str_replace('#', '', $color);
  1062. if (strlen($color) == 3) {
  1063. $r = hexdec(substr($color, 0, 1) . substr($color, 0, 1));
  1064. $g = hexdec(substr($color, 1, 1) . substr($color, 1, 1));
  1065. $b = hexdec(substr($color, 2, 1) . substr($color, 2, 1));
  1066. } else {
  1067. $r = hexdec(substr($color, 0, 2));
  1068. $g = hexdec(substr($color, 2, 2));
  1069. $b = hexdec(substr($color, 4, 2));
  1070. }
  1071. $color = $r . ',' . $g . ',' . $b;
  1072. }
  1073. //color to array, pop alpha
  1074. if (substr_count($color, ',') > 0) {
  1075. $color = str_replace(' ', '', $color);
  1076. $color = str_replace('rgb', '', $color);
  1077. $color = str_replace('a(', '', $color);
  1078. $color = str_replace(')', '', $color);
  1079. $color = explode(',', $color);
  1080. $hsl = rgb_to_hsl($color[0], $color[1], $color[2]);
  1081. }
  1082. //return luminence value
  1083. return (is_array($hsl) && is_numeric($hsl[2])) ? $hsl[2] : null;
  1084. }
  1085. }
  1086. //function to lighten or darken a hexidecimal, rgb, or rgba color value by a percentage -- dependencies: rgb_to_hsl(), hsl_to_rgb()
  1087. if (!function_exists('color_adjust')) {
  1088. function color_adjust($color, $percent) {
  1089. /*
  1090. USAGE
  1091. 20% Lighter
  1092. color_adjust('#3f4265', 0.2);
  1093. color_adjust('234,120,6,0.3', 0.2);
  1094. 20% Darker
  1095. color_adjust('#3f4265', -0.2); //
  1096. color_adjust('rgba(234,120,6,0.3)', -0.2);
  1097. RETURNS
  1098. Same color format provided (hex in = hex out, rgb(a) in = rgb(a) out)
  1099. */
  1100. //convert hex to rgb
  1101. if (substr_count($color, ',') == 0) {
  1102. $color = str_replace(' ', '', $color);
  1103. if (substr_count($color, '#') > 0) {
  1104. $color = str_replace('#', '', $color);
  1105. $hash = '#';
  1106. }
  1107. if (strlen($color) == 3) {
  1108. $r = hexdec(substr($color, 0, 1) . substr($color, 0, 1));
  1109. $g = hexdec(substr($color, 1, 1) . substr($color, 1, 1));
  1110. $b = hexdec(substr($color, 2, 1) . substr($color, 2, 1));
  1111. } else {
  1112. $r = hexdec(substr($color, 0, 2));
  1113. $g = hexdec(substr($color, 2, 2));
  1114. $b = hexdec(substr($color, 4, 2));
  1115. }
  1116. $color = $r . ',' . $g . ',' . $b;
  1117. }
  1118. //color to array, pop alpha
  1119. if (substr_count($color, ',') > 0) {
  1120. $color = str_replace(' ', '', $color);
  1121. $wrapper = false;
  1122. if (substr_count($color, 'rgb') != 0) {
  1123. $color = str_replace('rgb', '', $color);
  1124. $color = str_replace('a(', '', $color);
  1125. $color = str_replace('(', '', $color);
  1126. $color = str_replace(')', '', $color);
  1127. $wrapper = true;
  1128. }
  1129. $colors = explode(',', $color);
  1130. $alpha = (sizeof($colors) == 4) ? array_pop($colors) : null;
  1131. $color = $colors;
  1132. unset($colors);
  1133. //adjust color using rgb > hsl > rgb conversion
  1134. $hsl = rgb_to_hsl($color[0], $color[1], $color[2]);
  1135. $hsl[2] = $hsl[2] + $percent;
  1136. $color = hsl_to_rgb($hsl[0], $hsl[1], $hsl[2]);
  1137. //return adjusted color in format received
  1138. if (isset($hash) && $hash == '#') { //hex
  1139. $hex = '';
  1140. for ($i = 0; $i <= 2; $i++) {
  1141. $hex_color = dechex($color[$i]);
  1142. if (strlen($hex_color) == 1) {
  1143. $hex_color = '0' . $hex_color;
  1144. }
  1145. $hex .= $hex_color;
  1146. }
  1147. return $hash . $hex;
  1148. } else { //rgb(a)
  1149. $rgb = implode(',', $color);
  1150. if (!empty($alpha)) {
  1151. $rgb .= ',' . $alpha;
  1152. $a = 'a';
  1153. }
  1154. if ($wrapper) {
  1155. $rgb = 'rgb' . ($a ?? '') . '(' . $rgb . ')';
  1156. }
  1157. return $rgb;
  1158. }
  1159. }
  1160. return $color;
  1161. }
  1162. }
  1163. //function to convert an rgb color array to an hsl color array
  1164. if (!function_exists('rgb_to_hsl')) {
  1165. function rgb_to_hsl($r, $g, $b) {
  1166. $r /= 255;
  1167. $g /= 255;
  1168. $b /= 255;
  1169. $max = max($r, $g, $b);
  1170. $min = min($r, $g, $b);
  1171. $h;
  1172. $s;
  1173. $l = ($max + $min) / 2;
  1174. $d = $max - $min;
  1175. if ($d == 0) {
  1176. $h = $s = 0; // achromatic
  1177. } else {
  1178. $s = $d / (1 - abs((2 * $l) - 1));
  1179. switch ($max) {
  1180. case $r:
  1181. $h = 60 * fmod((($g - $b) / $d), 6);
  1182. if ($b > $g) {
  1183. $h += 360;
  1184. }
  1185. break;
  1186. case $g:
  1187. $h = 60 * (($b - $r) / $d + 2);
  1188. break;
  1189. case $b:
  1190. $h = 60 * (($r - $g) / $d + 4);
  1191. break;
  1192. }
  1193. }
  1194. return array(round($h, 2), round($s, 2), round($l, 2));
  1195. }
  1196. }
  1197. //function to convert an hsl color array to an rgb color array
  1198. if (!function_exists('hsl_to_rgb')) {
  1199. function hsl_to_rgb($h, $s, $l) {
  1200. $r;
  1201. $g;
  1202. $b;
  1203. $c = (1 - abs((2 * $l) - 1)) * $s;
  1204. $x = $c * (1 - abs(fmod(($h / 60), 2) - 1));
  1205. $m = $l - ($c / 2);
  1206. if ($h < 60) {
  1207. $r = $c;
  1208. $g = $x;
  1209. $b = 0;
  1210. } else if ($h < 120) {
  1211. $r = $x;
  1212. $g = $c;
  1213. $b = 0;
  1214. } else if ($h < 180) {
  1215. $r = 0;
  1216. $g = $c;
  1217. $b = $x;
  1218. } else if ($h < 240) {
  1219. $r = 0;
  1220. $g = $x;
  1221. $b = $c;
  1222. } else if ($h < 300) {
  1223. $r = $x;
  1224. $g = 0;
  1225. $b = $c;
  1226. } else {
  1227. $r = $c;
  1228. $g = 0;
  1229. $b = $x;
  1230. }
  1231. $r = ($r + $m) * 255;
  1232. $g = ($g + $m) * 255;
  1233. $b = ($b + $m) * 255;
  1234. if ($r > 255) {
  1235. $r = 255;
  1236. }
  1237. if ($g > 255) {
  1238. $g = 255;
  1239. }
  1240. if ($b > 255) {
  1241. $b = 255;
  1242. }
  1243. if ($r < 0) {
  1244. $r = 0;
  1245. }
  1246. if ($g < 0) {
  1247. $g = 0;
  1248. }
  1249. if ($b < 0) {
  1250. $b = 0;
  1251. }
  1252. return array(floor($r), floor($g), floor($b));
  1253. }
  1254. }
  1255. //function to send email
  1256. if (!function_exists('send_email')) {
  1257. function send_email($email_recipients, $email_subject, $email_body, &$email_error = '', $email_from_address = '', $email_from_name = '', $email_priority = 3, $email_debug_level = 0, $email_attachments = '', $email_read_confirmation = false) {
  1258. /*
  1259. RECIPIENTS NOTE:
  1260. Pass in a single email address...
  1261. [email protected]
  1262. Pass in a comma or semi-colon delimited string of e-mail addresses...
  1263. [email protected],[email protected],[email protected]
  1264. [email protected];[email protected];[email protected]
  1265. Pass in a simple array of email addresses...
  1266. Array (
  1267. [0] => [email protected]
  1268. [1] => [email protected]
  1269. [2] => [email protected]
  1270. )
  1271. Pass in a multi-dimentional array of addresses (delivery, address, name)...
  1272. Array (
  1273. [0] => Array (
  1274. [delivery] => to
  1275. [address] => [email protected]
  1276. [name] => user 1
  1277. )
  1278. [1] => Array (
  1279. [delivery] => cc
  1280. [address] => [email protected]
  1281. [name] => user 2
  1282. )
  1283. [2] => Array (
  1284. [delivery] => bcc
  1285. [address] => [email protected]
  1286. [name] => user 3
  1287. )
  1288. )
  1289. ATTACHMENTS NOTE:
  1290. Pass in as many files as necessary in an array in the following format...
  1291. Array (
  1292. [0] => Array (
  1293. [type] => file (or 'path')
  1294. [name] => filename.ext
  1295. [value] => /folder/filename.ext
  1296. )
  1297. [1] => Array (
  1298. [type] => string
  1299. [name] => filename.ext
  1300. [value] => (string of file contents - if base64, will be decoded automatically)
  1301. )
  1302. )
  1303. ERROR RESPONSE:
  1304. Error messages are stored in the variable passed into $email_error BY REFERENCE
  1305. */
  1306. //add the email recipients
  1307. $address_found = false;
  1308. if (!is_array($email_recipients)) { // must be a single or delimited recipient address(s)
  1309. $email_recipients = str_replace(' ', '', $email_recipients);
  1310. $email_recipients = str_replace(',', ';', $email_recipients);
  1311. $email_recipients = explode(';', $email_recipients); // convert to array of addresses
  1312. }
  1313. foreach ($email_recipients as $email_recipient) {
  1314. if (is_array($email_recipient)) { // check if each recipient has multiple fields
  1315. if (!empty($email_recipient["address"]) && valid_email($email_recipient["address"])) { // check if valid address
  1316. $recipients = $email_recipient["address"];
  1317. $address_found = true;
  1318. }
  1319. } else if (!empty($email_recipient) && valid_email($email_recipient)) { // check if recipient value is simply (only) an address
  1320. $email_recipients = $email_recipient;
  1321. $address_found = true;
  1322. }
  1323. }
  1324. if (is_array($recipients)) {
  1325. $email_recipients = implode(",", $recipients);
  1326. }
  1327. if (!$address_found) {
  1328. $email_error = "No valid e-mail address provided.";
  1329. return false;
  1330. }
  1331. //get the from address and name
  1332. $email_from_address = (!empty($email_from_address)) ? $email_from_address : $_SESSION['email']['smtp_from']['text'];
  1333. $email_from_name = (!empty($email_from_name)) ? $email_from_name : $_SESSION['email']['smtp_from_name']['text'];
  1334. //send email
  1335. $email = new email;
  1336. $email->recipients = $email_recipients;
  1337. $email->subject = $email_subject;
  1338. $email->body = $email_body;
  1339. $email->from_address = $email_from_address;
  1340. $email->from_name = $email_from_name;
  1341. $email->attachments = $email_attachments;
  1342. $email->debug_level = 3;
  1343. $sent = $email->send();
  1344. //$email_error = $email->email_error;
  1345. }
  1346. }
  1347. //encrypt a string
  1348. if (!function_exists('encrypt')) {
  1349. function encrypt($key, $data) {
  1350. $encryption_key = base64_decode($key);
  1351. $iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-cbc'));
  1352. $encrypted = openssl_encrypt($data, 'aes-256-cbc', $encryption_key, 0, $iv);
  1353. return base64_encode($encrypted . '::' . $iv);
  1354. }
  1355. }
  1356. //decrypt a string
  1357. if (!function_exists('decrypt')) {
  1358. function decrypt($key, $data) {
  1359. $encryption_key = base64_decode($key);
  1360. list($encrypted_data, $iv) = explode('::', base64_decode($data), 2);
  1361. return openssl_decrypt($encrypted_data, 'aes-256-cbc', $encryption_key, 0, $iv);
  1362. }
  1363. }
  1364. //json detection
  1365. if (!function_exists('is_json')) {
  1366. function is_json($str) {
  1367. return is_string($str) && is_array(json_decode($str, true)) ? true : false;
  1368. }
  1369. }
  1370. //mac detection
  1371. if (!function_exists('is_mac')) {
  1372. function is_mac($str) {
  1373. return preg_match('/([a-fA-F0-9]{2}[:|\-]?){6}/', $str) == 1 && strlen(preg_replace("#[^a-fA-F0-9]#", '', $str)) == 12 ? true : false;
  1374. }
  1375. }
  1376. //detect if php is running as command line interface
  1377. if (!function_exists('is_cli')) {
  1378. function is_cli() {
  1379. if (defined('STDIN')) {
  1380. return true;
  1381. }
  1382. if (php_sapi_name() == 'cli' && !isset($_SERVER['HTTP_USER_AGENT']) && is_numeric($_SERVER['argc'])) {
  1383. return true;
  1384. }
  1385. return false;
  1386. }
  1387. }
  1388. //format device address
  1389. if (!function_exists('format_device_address')) {
  1390. function format_device_address($str, $delim = '-', $case = 'lower') {
  1391. if (empty($str)) {
  1392. return false;
  1393. }
  1394. $str = preg_replace("#[^a-fA-F0-9]#", '', $str); //remove formatting, if any
  1395. if (is_mac($str)) {
  1396. $str = join($delim, str_split($str, 2));
  1397. } else if (is_uuid($str)) {
  1398. $str = substr($str, 0, 8) . '-' . substr($str, 8, 4) . '-' . substr($str, 12, 4) . '-' . substr($str, 16, 4) . '-' . substr($str, 20, 12);
  1399. }
  1400. $str = $case == 'upper' ? strtoupper($str) : strtolower($str);
  1401. return $str;
  1402. }
  1403. }
  1404. //transparent gif
  1405. if (!function_exists('img_spacer')) {
  1406. function img_spacer($width = '1px', $height = '1px', $custom = null) {
  1407. return "<img src='data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7' style='width: " . $width . "; height: " . $height . "; " . $custom . "'>";
  1408. }
  1409. }
  1410. //lower case
  1411. function lower_case($string) {
  1412. if (function_exists('mb_strtolower')) {
  1413. return mb_strtolower($string, 'UTF-8');
  1414. } else {
  1415. return strtolower($string);
  1416. }
  1417. }
  1418. //upper case
  1419. function upper_case($string) {
  1420. if (function_exists('mb_strtoupper')) {
  1421. return mb_strtoupper($string, 'UTF-8');
  1422. } else {
  1423. return strtoupper($string);
  1424. }
  1425. }
  1426. //write javascript function that detects select key combinations to perform designated actions
  1427. if (!function_exists('key_press')) {
  1428. function key_press($key, $direction = 'up', $subject = 'document', $exceptions = array(), $prompt = null, $action = null, $script_wrapper = true) {
  1429. //determine key code
  1430. switch (strtolower($key)) {
  1431. case 'escape':
  1432. $key_code = '(e.which == 27)';
  1433. break;
  1434. case 'delete':
  1435. $key_code = '(e.which == 46)';
  1436. break;
  1437. case 'enter':
  1438. $key_code = '(e.which == 13)';
  1439. break;
  1440. case 'backspace':
  1441. $key_code = '(e.which == 8)';
  1442. break;
  1443. case 'space':
  1444. $key_code = '(e.which == 32)';
  1445. break;
  1446. case 'ctrl+s':
  1447. $key_code = '(((e.which == 115 || e.which == 83) && (e.ctrlKey || e.metaKey)) || (e.which == 19))';
  1448. break;
  1449. case 'ctrl+q':
  1450. $key_code = '(((e.which == 113 || e.which == 81) && (e.ctrlKey || e.metaKey)) || (e.which == 19))';
  1451. break;
  1452. case 'ctrl+a':
  1453. $key_code = '(((e.which == 97 || e.which == 65) && (e.ctrlKey || e.metaKey)) || (e.which == 19))';
  1454. break;
  1455. case 'ctrl+c':
  1456. $key_code = '(((e.which == 99 || e.which == 67) && (e.ctrlKey || e.metaKey)) || (e.which == 19))';
  1457. break;
  1458. case 'ctrl+enter':
  1459. $key_code = '(((e.which == 13 || e.which == 10) && (e.ctrlKey || e.metaKey)) || (e.which == 19))';
  1460. break;
  1461. default:
  1462. return;
  1463. }
  1464. //filter direction
  1465. switch ($direction) {
  1466. case 'down': $direction = 'keydown';
  1467. break;
  1468. case 'press': $direction = 'keypress';
  1469. break;
  1470. case 'up': $direction = 'keyup';
  1471. break;
  1472. }
  1473. //check for element exceptions
  1474. if (is_array($exceptions)) {
  1475. if (sizeof($exceptions) > 0) {
  1476. $exceptions = "!$(e.target).is('" . implode(',', $exceptions) . "') && ";
  1477. }
  1478. }
  1479. //quote if selector is id or class
  1480. $subject = ($subject != 'window' && $subject != 'document') ? "'" . $subject . "'" : $subject;
  1481. //output script
  1482. echo "\n\n\n";
  1483. if ($script_wrapper) {
  1484. echo "<script language='JavaScript' type='text/javascript'>\n";
  1485. }
  1486. echo " $(" . $subject . ").on('" . $direction . "', function(e) {\n";
  1487. echo " if (" . $exceptions . $key_code . ") {\n";
  1488. if (!empty($prompt)) {
  1489. $action = (!empty($action)) ? $action : "alert('" . $key . "');";
  1490. echo " if (confirm('" . $prompt . "')) {\n";
  1491. echo " e.preventDefault();\n";
  1492. echo " " . $action . "\n";
  1493. echo " }\n";
  1494. } else {
  1495. echo " e.preventDefault();\n";
  1496. echo " " . $action . "\n";
  1497. }
  1498. echo " }\n";
  1499. echo " });\n";
  1500. if ($script_wrapper) {
  1501. echo "</script>\n";
  1502. }
  1503. echo "\n\n\n";
  1504. }
  1505. }
  1506. //format border radius values
  1507. if (!function_exists('format_border_radius')) {
  1508. function format_border_radius($radius_value, $default = 5) {
  1509. $radius_value = (!empty($radius_value)) ? $radius_value : $default;
  1510. $br_a = explode(' ', $radius_value);
  1511. foreach ($br_a as $index => $br) {
  1512. if (substr_count($br, '%') > 0) {
  1513. $br_b[$index]['number'] = str_replace('%', '', $br);
  1514. $br_b[$index]['unit'] = '%';
  1515. } else {
  1516. $br_b[$index]['number'] = str_replace('px', '', strtolower($br));
  1517. $br_b[$index]['unit'] = 'px';
  1518. }
  1519. }
  1520. unset($br_a, $br);
  1521. if (sizeof($br_b) == 4) {
  1522. $br['tl']['n'] = $br_b[0]['number'];
  1523. $br['tr']['n'] = $br_b[1]['number'];
  1524. $br['br']['n'] = $br_b[2]['number'];
  1525. $br['bl']['n'] = $br_b[3]['number'];
  1526. $br['tl']['u'] = $br_b[0]['unit'];
  1527. $br['tr']['u'] = $br_b[1]['unit'];
  1528. $br['br']['u'] = $br_b[2]['unit'];
  1529. $br['bl']['u'] = $br_b[3]['unit'];
  1530. } else if (sizeof($br_b) == 2) {
  1531. $br['tl']['n'] = $br_b[0]['number'];
  1532. $br['tr']['n'] = $br_b[0]['number'];
  1533. $br['br']['n'] = $br_b[1]['number'];
  1534. $br['bl']['n'] = $br_b[1]['number'];
  1535. $br['tl']['u'] = $br_b[0]['unit'];
  1536. $br['tr']['u'] = $br_b[0]['unit'];
  1537. $br['br']['u'] = $br_b[1]['unit'];
  1538. $br['bl']['u'] = $br_b[1]['unit'];
  1539. } else {
  1540. $br['tl']['n'] = $br_b[0]['number'];
  1541. $br['tr']['n'] = $br_b[0]['number'];
  1542. $br['br']['n'] = $br_b[0]['number'];
  1543. $br['bl']['n'] = $br_b[0]['number'];
  1544. $br['tl']['u'] = $br_b[0]['unit'];
  1545. $br['tr']['u'] = $br_b[0]['unit'];
  1546. $br['br']['u'] = $br_b[0]['unit'];
  1547. $br['bl']['u'] = $br_b[0]['unit'];
  1548. }
  1549. unset($br_b);
  1550. return $br; //array
  1551. }
  1552. }
  1553. //converts a string to a regular expression
  1554. if (!function_exists('string_to_regex')) {
  1555. function string_to_regex($string, $prefix = '') {
  1556. $original_string = $string;
  1557. //escape the plus
  1558. if (substr($string, 0, 1) == "+") {
  1559. $string = "^\\+(" . substr($string, 1) . ")$";
  1560. }
  1561. //add prefix
  1562. if (!empty($prefix)) {
  1563. if (!empty($prefix) && strlen($prefix) < 4) {
  1564. $plus = (substr($string, 0, 1) == "+") ? '' : '\+?';
  1565. $prefix = $plus . $prefix . '?';
  1566. } else {
  1567. $prefix = '(?:' . $prefix . ')?';
  1568. }
  1569. }
  1570. //convert N,X,Z syntax to regex
  1571. if (preg_match('/^[NnXxZz]+$/', $original_string)) {
  1572. $string = str_ireplace("N", "[2-9]", $string);
  1573. $string = str_ireplace("X", "[0-9]", $string);
  1574. $string = str_ireplace("Z", "[1-9]", $string);
  1575. }
  1576. //add ^ to the start of the string if missing
  1577. if (substr($string, 0, 1) != "^") {
  1578. $string = "^" . $string;
  1579. }
  1580. //add $ to the end of the string if missing
  1581. if (substr($string, -1) != "$") {
  1582. $string = $string . "$";
  1583. }
  1584. //add the round brackets
  1585. if (!strstr($string, '(')) {
  1586. if (strstr($string, '^')) {
  1587. $string = str_replace("^", "^" . $prefix . "(", $string);
  1588. } else {
  1589. $string = '^(' . $string;
  1590. }
  1591. if (strstr($string, '$')) {
  1592. $string = str_replace("$", ")$", $string);
  1593. } else {
  1594. $string = $string . ')$';
  1595. }
  1596. }
  1597. //return the result
  1598. return $string;
  1599. }
  1600. //$string = "+12089068227"; echo $string." ".string_to_regex($string)."\n";
  1601. //$string = "12089068227"; echo $string." ".string_to_regex($string)."\n";
  1602. //$string = "2089068227"; echo $string." ".string_to_regex($string)."\n";
  1603. //$string = "^(20890682[0-9][0-9])$"; echo $string." ".string_to_regex($string)."\n";
  1604. //$string = "1208906xxxx"; echo $string." ".string_to_regex($string)."\n";
  1605. //$string = "nxxnxxxxxxx"; echo $string." ".string_to_regex($string)."\n";
  1606. //$string = "208906xxxx"; echo $string." ".string_to_regex($string)."\n";
  1607. //$string = "^(2089068227"; echo $string." ".string_to_regex($string)."\n";
  1608. //$string = "^2089068227)"; echo $string." ".string_to_regex($string)."\n";
  1609. //$string = "2089068227$"; echo $string." ".string_to_regex($string)."\n";
  1610. //$string = "2089068227)$"; echo $string." ".string_to_regex($string)."\n";
  1611. }
  1612. //dynamically load available web fonts
  1613. if (!function_exists('get_available_fonts')) {
  1614. function get_available_fonts($sort = 'alpha') {
  1615. if (!empty($_SESSION['theme']['font_source_key']['text'])) {
  1616. if (!is_array($_SESSION['fonts_available']) || sizeof($_SESSION['fonts_available']) == 0) {
  1617. /*
  1618. sort options:
  1619. alpha - alphabetically
  1620. date - by date added (most recent font added or updated first)
  1621. popularity - by popularity (most popular family first)
  1622. style - by number of styles available (family with most styles first)
  1623. trending - by families seeing growth in usage (family seeing the most growth first)
  1624. */
  1625. $google_api_url = 'https://www.googleapis.com/webfonts/v1/webfonts?key=' . $_SESSION['theme']['font_source_key']['text'] . '&sort=' . $sort;
  1626. $response = file_get_contents($google_api_url);
  1627. if (!empty($response)) {
  1628. $data = json_decode($response, true);
  1629. $items = $data['items'];
  1630. foreach ($items as $item) {
  1631. $fonts[] = $item['family'];
  1632. }
  1633. //echo "<pre>".print_r($font_list, true)."</pre>";
  1634. }
  1635. $_SESSION['fonts_available'] = $fonts;
  1636. unset($fonts);
  1637. }
  1638. return (is_array($_SESSION['fonts_available']) && sizeof($_SESSION['fonts_available']) > 0) ? $_SESSION['fonts_available'] : array();
  1639. } else {
  1640. return false;
  1641. }
  1642. }
  1643. }
  1644. //dynamically import web fonts (by reading static css file)
  1645. if (!function_exists('import_fonts')) {
  1646. function import_fonts($file_to_parse, $line_styles_begin = null) {
  1647. /*
  1648. This function reads the contents of $file_to_parse, beginning at $line_styles_begin (if set),
  1649. and attempts to parse the specified google fonts used. The assumption is that each curly brace
  1650. will be on its own line, each CSS style (attribute: value;) will be on its own line, a single
  1651. Google Fonts name will be used per selector, and that it will be surrounded by SINGLE quotes,
  1652. as shown in the example below:
  1653. .class_name {
  1654. font-family: 'Google Font';
  1655. font-weight: 300;
  1656. font-style: italic;
  1657. }
  1658. If the CSS styles are formatted as described, the necessary @import string should be generated
  1659. correctly.
  1660. */
  1661. $file = file_get_contents($_SERVER["DOCUMENT_ROOT"] . $file_to_parse);
  1662. $lines = explode("\n", $file);
  1663. $style_counter = 0;
  1664. foreach ($lines as $line_number => $line) {
  1665. if (!empty($line_styles_begin) && $line_number < $line_styles_begin - 1) {
  1666. continue;
  1667. }
  1668. if (substr_count($line, "{") > 0) {
  1669. $style_lines[$style_counter]['begins'] = $line_number;
  1670. }
  1671. if (substr_count($line, "}") > 0) {
  1672. $style_lines[$style_counter]['ends'] = $line_number;
  1673. $style_counter++;
  1674. }
  1675. }
  1676. //echo "\n\n".print_r($style_lines, true)."\n\n";
  1677. if (is_array($style_lines) && sizeof($style_lines) > 0) {
  1678. foreach ($style_lines as $index => $style_line) {
  1679. for ($l = $style_line['begins'] + 1; $l < $style_line['ends']; $l++) {
  1680. $tmp[] = $lines[$l];
  1681. }
  1682. $style_groups[] = $tmp;
  1683. unset($tmp);
  1684. }
  1685. //echo "\n\n".print_r($style_groups, true)."\n\n";
  1686. if (is_array($style_groups) && sizeof($style_groups) > 0) {
  1687. foreach ($style_groups as $style_group_index => $style_group) {
  1688. foreach ($style_group as $style_index => $style) {
  1689. $tmp = explode(':', $style);
  1690. $attribute = trim($tmp[0]);
  1691. $value = trim(trim($tmp[1]), ';');
  1692. $style_array[$attribute] = $value;
  1693. }
  1694. $style_groups[$style_group_index] = $style_array;
  1695. unset($style_array);
  1696. }
  1697. //echo "\n\n".print_r($style_groups, true)."\n\n";
  1698. foreach ($style_groups as $style_group_index => $style_group) {
  1699. $style_value = $style_group['font-family'];
  1700. if (substr_count($style_value, "'") > 0) {
  1701. //determine font
  1702. $font_begin = strpos($style_value, "'") + 1;
  1703. $font_end = strpos($style_value, "'", $font_begin);
  1704. $font_name = substr($style_value, $font_begin, $font_end - $font_begin);
  1705. //determine modifiers
  1706. $weight = (is_numeric($style_group['font-weight']) || strtolower($style_group['font-weight']) == 'bold') ? strtolower($style_group['font-weight']) : null;
  1707. $italic = (strtolower($style_group['font-style']) == 'italic') ? 'italic' : null;
  1708. //add font to array
  1709. $fonts[$font_name][] = $weight . $italic;
  1710. }
  1711. }
  1712. //echo "\n\n/*".print_r($fonts, true)."*/\n\n";
  1713. if (is_array($fonts)) {
  1714. foreach ($fonts as $font_name => $modifiers) {
  1715. $modifiers = array_unique($modifiers);
  1716. $import_font_string = str_replace(' ', '+', $font_name);
  1717. if (is_array($modifiers) && sizeof($modifiers) > 0) {
  1718. $import_font_string .= ':' . implode(',', $modifiers);
  1719. }
  1720. $import_fonts[] = $import_font_string;
  1721. }
  1722. //echo "\n\n/*".print_r($import_fonts, true)."*/\n\n";
  1723. $import_string = "@import url(//fonts.googleapis.com/css?family=" . implode('|', $import_fonts) . ");";
  1724. echo $import_string . "\n";
  1725. }
  1726. }
  1727. }
  1728. }
  1729. }
  1730. //retrieve array of countries
  1731. if (!function_exists('get_countries')) {
  1732. function get_countries() {
  1733. $sql = "select * from v_countries order by country asc";
  1734. $database = database::new();
  1735. $result = $database->select($sql, null, 'all');
  1736. unset($sql);
  1737. return is_array($result) && @sizeof($result) != 0 ? $result : false;
  1738. }
  1739. }
  1740. //make directory with event socket
  1741. function event_socket_mkdir($dir) {
  1742. //connect to fs
  1743. $esl = event_socket::create();
  1744. if (!$esl->is_connected()) {
  1745. return false;
  1746. }
  1747. //send the mkdir command to freeswitch
  1748. //build and send the mkdir command to freeswitch
  1749. $switch_cmd = "lua mkdir.lua " . escapeshellarg($dir);
  1750. $switch_result = event_socket::api($switch_cmd);
  1751. //check result
  1752. if (trim($switch_result) == "-ERR no reply") {
  1753. return true;
  1754. }
  1755. //can not create directory
  1756. return false;
  1757. }
  1758. //escape user data
  1759. function escape($string) {
  1760. if (is_string($string)) {
  1761. return htmlentities($string, ENT_QUOTES | ENT_HTML5, 'UTF-8');
  1762. } elseif (is_numeric($string)) {
  1763. return $string;
  1764. } else {
  1765. $string = (array) $string;
  1766. if (isset($string[0])) {
  1767. return htmlentities($string[0], ENT_QUOTES | ENT_HTML5, 'UTF-8');
  1768. }
  1769. }
  1770. return false;
  1771. }
  1772. //output pre-formatted array keys and values
  1773. if (!function_exists('view_array')) {
  1774. function view_array($array, $exit = true, $return = false) {
  1775. $html = "<br><pre style='text-align: left;'>" . print_r($array, true) . '</pre><br>';
  1776. if ($return) {
  1777. return $html;
  1778. } else {
  1779. echo $html;
  1780. }
  1781. $exit and exit();
  1782. }
  1783. }
  1784. //format db date and/or time to local date and/or time
  1785. if (!function_exists('format_when_local')) {
  1786. function format_when_local($when, $format = 'dt', $include_seconds = false) {
  1787. if (!empty($when)) {
  1788. // determine when format
  1789. if (substr_count($when, ' ') > 0) { // date and time
  1790. $tmp = explode(' ', $when);
  1791. $date = $tmp[0];
  1792. $time = $tmp[1];
  1793. } else if (substr_count($when, '-') > 0) { // date only
  1794. $date = $when;
  1795. } else if (substr_count($when, ':') > 0) { // time only
  1796. $time = $when;
  1797. }
  1798. unset($when, $tmp);
  1799. // format date
  1800. if (!empty($date)) {
  1801. $tmp = explode('-', $date);
  1802. $date = $tmp[1] . '-' . $tmp[2] . '-' . $tmp[0];
  1803. }
  1804. // format time
  1805. if (!empty($time)) {
  1806. $tmp = explode(':', $time);
  1807. if ($tmp[0] >= 0 && $tmp[0] <= 11) {
  1808. $meridiem = 'AM';
  1809. $hour = ($tmp[0] == 0) ? 12 : $tmp[0];
  1810. } else {
  1811. $meridiem = 'PM';
  1812. $hour = ($tmp[0] > 12) ? ($tmp[0] - 12) : $tmp[0];
  1813. }
  1814. $minute = $tmp[1];
  1815. $second = $tmp[2];
  1816. }
  1817. // structure requested time format
  1818. $time = $hour . ':' . $minute;
  1819. if ($include_seconds) {
  1820. $time .= ':' . $second;
  1821. }
  1822. $time .= ' ' . $meridiem;
  1823. $return['d'] = $date;
  1824. $return['t'] = $time;
  1825. $return['dt'] = $date . ' ' . $time;
  1826. return $return[$format];
  1827. } else {
  1828. return false;
  1829. }
  1830. }
  1831. }
  1832. //define email button (src: https://buttons.cm)
  1833. if (!function_exists('email_button')) {
  1834. function email_button($text = 'Click Here!', $link = URL, $bg_color = '#dddddd', $fg_color = '#000000', $radius = '') {
  1835. // default button radius
  1836. $radius = !empty($radius) ? $radius : '3px';
  1837. // retrieve single/first numeric radius value for ms arc
  1838. $tmp = $radius;
  1839. if (substr_count($radius, ' ') > 0) {
  1840. $tmp = explode(' ', $radius);
  1841. $tmp = $tmp[0];
  1842. }
  1843. $tmp = preg_replace("/[^0-9,.]/", '', $tmp); // remove non-numeric characters
  1844. $arc = floor($tmp / 35 * 100); // calculate percentage
  1845. // create button code
  1846. $btn = "
  1847. <div>
  1848. <!--[if mso]>
  1849. <v:roundrect xmlns:v='urn:schemas-microsoft-com:vml' xmlns:w='urn:schemas-microsoft-com:office:word' href='" . $link . "' style='height: 35px; v-text-anchor: middle; width: 140px;' arcsize='" . $arc . "%' stroke='f' fillcolor='" . $bg_color . "'>
  1850. <w:anchorlock/>
  1851. <center>
  1852. <![endif]-->
  1853. <a href='" . $link . "' style='background-color: " . $bg_color . "; border-radius: " . $radius . "; color: " . $fg_color . "; display: inline-block; font-family: sans-serif; font-size: 13px; font-weight: bold; line-height: 35px; text-align: center; text-decoration: none; width: 140px; -webkit-text-size-adjust: none;'>" . $text . "</a>
  1854. <!--[if mso]>
  1855. </center>
  1856. </v:roundrect>
  1857. <![endif]-->
  1858. </div>
  1859. ";
  1860. return $btn;
  1861. }
  1862. }
  1863. //validate and format order by clause of select statement
  1864. if (!function_exists('order_by')) {
  1865. function order_by($col, $dir, $col_default = '', $dir_default = 'asc', $sort = '') {
  1866. global $db_type;
  1867. $order_by = ' order by ';
  1868. $col = preg_replace('#[^a-zA-Z0-9-_.]#', '', $col ?? '');
  1869. $dir = !empty($dir) && strtolower($dir) == 'desc' ? 'desc' : 'asc';
  1870. if (!empty($col)) {
  1871. if ($sort == 'natural' && $db_type == "pgsql") {
  1872. return $order_by . 'natural_sort(' . $col . '::text) ' . $dir . ' ';
  1873. } else {
  1874. return $order_by . $col . ' ' . $dir . ' ';
  1875. }
  1876. } else if (!empty($col_default)) {
  1877. if (is_array($col_default) && @sizeof($col_default) != 0) {
  1878. foreach ($col_default as $k => $column) {
  1879. $direction = (is_array($dir_default) && @sizeof($dir_default) != 0 && (strtolower($dir_default[$k]) == 'asc' || strtolower($dir_default[$k]) == 'desc')) ? $dir_default[$k] : 'asc';
  1880. $order_bys[] = $column . ' ' . $direction . ' ';
  1881. }
  1882. if (is_array($order_bys) && @sizeof($order_bys) != 0) {
  1883. return $order_by . implode(', ', $order_bys);
  1884. }
  1885. } else {
  1886. if ($sort == 'natural' && $db_type == "pgsql") {
  1887. return $order_by . 'natural_sort(' . $col_default . '::text) ' . $dir_default . ' ';
  1888. } else {
  1889. return $order_by . $col_default . ' ' . $dir_default . ' ';
  1890. }
  1891. }
  1892. }
  1893. }
  1894. }
  1895. //validate and format limit and offset clause of select statement
  1896. if (!function_exists('limit_offset')) {
  1897. function limit_offset($limit = null, $offset = 0) {
  1898. $regex = '#[^0-9]#';
  1899. if (!empty($limit)) {
  1900. $limit = preg_replace($regex, '', $limit);
  1901. $offset = preg_replace($regex, '', $offset ?? '');
  1902. if (is_numeric($limit) && $limit > 0) {
  1903. $clause = ' limit ' . $limit;
  1904. $offset = is_numeric($offset) ? $offset : 0;
  1905. $clause .= ' offset ' . $offset;
  1906. }
  1907. return $clause . ' ';
  1908. } else {
  1909. return '';
  1910. }
  1911. }
  1912. }
  1913. //add a random_bytes function when it doesn't exist for old versions of PHP
  1914. if (!function_exists('random_bytes')) {
  1915. function random_bytes($length) {
  1916. $chars .= "0123456789";
  1917. $chars .= "abcdefghijkmnopqrstuvwxyz";
  1918. $chars .= "ABCDEFGHIJKLMNPQRSTUVWXYZ";
  1919. for ($i = 0; $i < $length; $i++) {
  1920. $string .= $chars[random_int(0, strlen($chars) - 1)];
  1921. }
  1922. return $string . ' ';
  1923. }
  1924. }
  1925. //add a hash_equals function when it doesn't exist for old versions of PHP
  1926. if (!function_exists('hash_equals')) {
  1927. function hash_equals($var1, $var2) {
  1928. if ($var1 == $var2) {
  1929. return true;
  1930. } else {
  1931. return false;
  1932. }
  1933. }
  1934. }
  1935. //convert bytes to readable human format
  1936. if (!function_exists('byte_convert')) {
  1937. function byte_convert($bytes, $precision = 2) {
  1938. static $units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
  1939. $step = 1024;
  1940. $i = 0;
  1941. while (($bytes / $step) > 0.9) {
  1942. $bytes = $bytes / $step;
  1943. $i++;
  1944. }
  1945. return round($bytes, $precision) . ' ' . $units[$i];
  1946. }
  1947. }
  1948. //convert bytes to readable human format
  1949. if (!function_exists('random_int')) {
  1950. function random_int($min, $max) {
  1951. return rand($min, $max);
  1952. }
  1953. }
  1954. //manage submitted form values in a session array
  1955. if (!function_exists('persistent_form_values')) {
  1956. function persistent_form_values($action, $array = null) {
  1957. switch ($action) {
  1958. case 'store':
  1959. // $array is expected to be an array of key / value pairs to store in the session
  1960. if (is_array($array) && @sizeof($array) != 0) {
  1961. $_SESSION['persistent'][$_SERVER['PHP_SELF']] = $array;
  1962. }
  1963. break;
  1964. case 'exists':
  1965. return !empty($_SESSION['persistent']) && is_array($_SESSION['persistent'][$_SERVER['PHP_SELF']]) && @sizeof($_SESSION['persistent'][$_SERVER['PHP_SELF']]) != 0 ? true : false;
  1966. break;
  1967. case 'load':
  1968. // $array is expected to be the name of the array to create containing the key / value pairs
  1969. if ($array && !is_array($array)) {
  1970. global $$array;
  1971. }
  1972. if (!empty($_SESSION['persistent']) && is_array($_SESSION['persistent'][$_SERVER['PHP_SELF']]) && @sizeof($_SESSION['persistent'][$_SERVER['PHP_SELF']]) != 0) {
  1973. foreach ($_SESSION['persistent'][$_SERVER['PHP_SELF']] as $key => $value) {
  1974. if ($key != 'XID' && $key != 'ACT' && $key != 'RET') {
  1975. if ($array && !is_array($array)) {
  1976. $$array[$key] = $value;
  1977. } else {
  1978. global $$key;
  1979. $$key = $value;
  1980. }
  1981. }
  1982. }
  1983. global $unsaved;
  1984. $unsaved = true;
  1985. }
  1986. break;
  1987. case 'view':
  1988. if (is_array($_SESSION['persistent'][$_SERVER['PHP_SELF']]) && @sizeof($_SESSION['persistent'][$_SERVER['PHP_SELF']]) != 0) {
  1989. view_array($_SESSION['persistent'][$_SERVER['PHP_SELF']], false);
  1990. }
  1991. break;
  1992. case 'clear':
  1993. unset($_SESSION['persistent'][$_SERVER['PHP_SELF']]);
  1994. break;
  1995. }
  1996. }
  1997. }
  1998. //add alternative array_key_first for older verisons of PHP
  1999. if (!function_exists('array_key_first')) {
  2000. function array_key_first(array $arr) {
  2001. foreach ($arr as $key => $unused) {
  2002. return $key;
  2003. }
  2004. return NULL;
  2005. }
  2006. }
  2007. //get accountcode
  2008. if (!function_exists('get_accountcode')) {
  2009. function get_accountcode() {
  2010. if (!empty($accountcode = $_SESSION['domain']['accountcode']['text'] ?? '')) {
  2011. if ($accountcode == "none") {
  2012. return;
  2013. }
  2014. } else {
  2015. $accountcode = $_SESSION['domain_name'];
  2016. }
  2017. return $accountcode;
  2018. }
  2019. }
  2020. //user exists
  2021. if (!function_exists('user_exists')) {
  2022. function user_exists($login, $domain_name = null) {
  2023. //connect to freeswitch
  2024. $esl = event_socket::create();
  2025. if (!$esl->is_connected()) {
  2026. return false;
  2027. }
  2028. if (is_null($domain_name)) {
  2029. $domain_name = $_SESSION['domain_name'];
  2030. }
  2031. $switch_cmd = "user_exists id '$login' '$domain_name'";
  2032. $switch_result = event_socket::api($switch_cmd);
  2033. return ($switch_result == 'true' ? true : false);
  2034. }
  2035. }
  2036. //git pull
  2037. if (!function_exists('git_pull')) {
  2038. function git_pull($path) {
  2039. $cwd = getcwd();
  2040. chdir($path);
  2041. exec("GIT_SSH_COMMAND='ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' git pull 2>&1", $response_source_update);
  2042. $update_status = false;
  2043. if (sizeof($response_source_update) == 0) {
  2044. chdir($cwd);
  2045. return array('result' => false, 'message' => null);
  2046. }
  2047. foreach ($response_source_update as $response_line) {
  2048. if (substr_count($response_line, "Updating ") > 0 || substr_count($response_line, "Already up to date.") > 0) {
  2049. $update_status = true;
  2050. }
  2051. if (substr_count($response_line, "error") > 0) {
  2052. $update_status = false;
  2053. break;
  2054. }
  2055. }
  2056. chdir($cwd);
  2057. return array('result' => $update_status,
  2058. 'message' => $response_source_update);
  2059. }
  2060. }
  2061. //git is repository
  2062. if (!function_exists('is_git_repo')) {
  2063. function is_git_repo($path) {
  2064. if(!is_dir($path)) {return false;}
  2065. $cwd = $_SERVER["PROJECT_ROOT"];
  2066. chdir($path);
  2067. exec("git rev-parse --show-toplevel", $git_repo, $git_repo_response);
  2068. chdir($cwd);
  2069. if ((is_array($git_repo) && count($git_repo) > 0) && ($git_repo[0] != $cwd) && $git_repo_response == 0) {
  2070. return $git_repo[0];
  2071. }
  2072. return false;
  2073. }
  2074. }
  2075. //git repo version information
  2076. if (!function_exists('git_repo_info')) {
  2077. function git_repo_info($path) {
  2078. if(!is_dir($path)) {
  2079. return false;
  2080. }
  2081. $cwd = getcwd();
  2082. chdir($path);
  2083. //get current branch
  2084. exec("git rev-parse --abbrev-ref HEAD 2>&1", $git_branch, $git_branch_return);
  2085. $repo['branch'] = $git_branch[0];
  2086. //get current commit id
  2087. exec("git log --pretty=format:'%H' -n 1 2>&1", $git_commit, $git_commit_return);
  2088. $repo['commit'] = $git_commit[0];
  2089. //get remote origin url for updates
  2090. exec("git config --get remote.origin.url", $git_url);
  2091. $repo['url'] = preg_replace('/\.git$/', '', $git_url[0] );
  2092. $repo['path'] = $path;
  2093. //to-do detect remote over ssh and reformat to equivalent https url
  2094. chdir($cwd);
  2095. if (!$git_branch_return && !$git_commit_return && $git_url) {
  2096. return $repo;
  2097. }
  2098. else {
  2099. return false;
  2100. }
  2101. }
  2102. }
  2103. //git locate app repositories
  2104. if (!function_exists('git_find_repos')) {
  2105. function git_find_repos($path) {
  2106. $apps = scandir($path);
  2107. $git_repos = array();
  2108. foreach ($apps as $app) {
  2109. $git_repo_name = is_git_repo($path."/".$app);
  2110. if ($git_repo_name != false && !empty($git_repo_name)) {
  2111. $git_repos[$git_repo_name][] = $app;
  2112. }
  2113. unset($git_repo_name);
  2114. }
  2115. return $git_repos;
  2116. }
  2117. }
  2118. //get contents of the supplied url
  2119. if (!function_exists('url_get_contents')) {
  2120. function url_get_contents($URL){
  2121. $ch = curl_init();
  2122. curl_setopt($ch, CURLOPT_VERBOSE, 1);
  2123. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  2124. curl_setopt($ch, CURLOPT_URL, $URL);
  2125. $data = curl_exec($ch);
  2126. curl_close($ch);
  2127. return $data;
  2128. }
  2129. }
  2130. //get system memory details
  2131. if (!function_exists('get_memory_details')) {
  2132. function get_memory_details() {
  2133. if (PHP_OS == 'Linux') {
  2134. $meminfo = file_get_contents("/proc/meminfo");
  2135. $data = [];
  2136. foreach (explode("\n", $meminfo) as $line) {
  2137. if (preg_match('/^(\w+):\s+(\d+)\skB$/', $line, $matches)) {
  2138. $data[$matches[1]] = $matches[2];
  2139. }
  2140. }
  2141. if (isset($data['MemTotal']) && isset($data['MemAvailable'])) {
  2142. $array['total_memory'] = $data['MemTotal'];
  2143. $array['available_memory'] = $data['MemAvailable'];
  2144. $array['used_memory'] = $array['total_memory'] - $array['available_memory'];
  2145. $array['memory_usage'] = ($array['used_memory'] / $array['total_memory']) * 100;
  2146. $array['memory_percent'] = round($array['memory_usage'], 2);
  2147. return $array;
  2148. }
  2149. }
  2150. if (PHP_OS == 'FreeBSD') {
  2151. //define the output array
  2152. $output = [];
  2153. // get the memory information using sysctl
  2154. exec('sysctl -n hw.physmem hw.pagesize vm.stats.vm.v_free_count vm.stats.vm.v_inactive_count vm.stats.vm.v_cache_count vm.stats.vm.v_wire_count', $output);
  2155. if (count($output) === 6) {
  2156. list($array['total_memory'], $page_size, $free_pages, $inactive_pages, $cache_pages, $wired_pages) = $output;
  2157. // total memory in bytes
  2158. $array['total_memory'] = (int)$array['total_memory'];
  2159. // pages to bytes conversion
  2160. $array['available_memory'] = ($free_pages + $inactive_pages + $cache_pages) * (int)$page_size;
  2161. $array['used_memory'] = $array['total_memory'] - $array['available_memory'];
  2162. // calculate memory usage percentage
  2163. $array['memory_usage'] = ($array['used_memory'] / $array['total_memory']) * 100;
  2164. $array['memory_percent'] = round($array['memory_usage'], 2) . '%';
  2165. return $array;
  2166. }
  2167. }
  2168. return false;
  2169. }
  2170. }