{"id":5289,"date":"2013-05-08T12:02:02","date_gmt":"2013-05-08T16:02:02","guid":{"rendered":"https:\/\/www.caskeys.com\/dc\/?p=5289"},"modified":"2017-01-15T15:21:58","modified_gmt":"2017-01-15T20:21:58","slug":"class-utility","status":"publish","type":"post","link":"https:\/\/www.caskeys.com\/dc\/class-utility\/","title":{"rendered":"Class &#8211; Utility"},"content":{"rendered":"<pre class=\"brush: php; title: Code:; notranslate\" title=\"Code:\">\r\n&lt;?php \r\n\r\nclass class_utility {\r\n\r\n\t\/*\r\n\tUtility\r\n\tDamon Vaughn Caskey\r\n\t2013-01-09\r\n\t\r\n\tMiscellaneous utility functions.\r\n\t*\/\r\n\t\r\n\tpublic function utl_color_alternation($i, $cColorEven=&quot;#DDDDFF&quot;, $cColorOdd=&quot;#CECEFF&quot;)\r\n\t{\t\t\r\n\t\t\/*\r\n\t\tback_alt_0001 - https:\/\/www.caskeys.com\/dc\/?p=4900\r\n\t\tDamon Vaughn Caskey\r\n\t\t2012-10-18\r\n\t\t\r\n\t\tOutput alternating background colors for even\/odd rows in tables or other types of layouts.\r\n\t\t\r\n\t\t$i:\t\t\t\tCurrent row\/location\/count. \r\n\t\t$cColorEven:\tColor to output if $i is an even number.\r\n\t\t$cColorEven:\tColor to output if $i is an odd number.\r\n\t\t*\/\r\n\t\t\r\n\t\tif($i%2)\t\t\t\t\/\/Even number?\r\n\t\t{\t\t\t\t\r\n\t\t\treturn $cColorEven;\t\/\/Return even color.\r\n\t\t}\r\n\t\telse\r\n\t\t{\t\r\n\t\t\treturn $cColorOdd;\t\/\/Return odd color.\r\n\t\t}\r\n\t}\r\n\t\r\n\tpublic function utl_get_get($cID, $cDefault=NULL)\r\n\t{\r\n\t\t\/*\r\n\t\tutl_get_get\r\n\t\tDamon Vaughn Caskey\r\n\t\t2013-01-01\r\n\t\t\r\n\t\tWrapper to obtain get value.\r\n\t\t\r\n\t\t$cID:\t\tIndex.\r\n\t\t$cDefault:\tDefault on not set.\r\n\t\t*\/\r\n\t\t\r\n\t\treturn $this-&gt;utl_validate_isset($_GET&#x5B;$cID], $cDefault);\r\n\t}\r\n\t\r\n\tpublic function utl_get_post($cID, $cDefault=NULL)\r\n\t{\r\n\t\t\/*\r\n\t\tutl_get_post\r\n\t\tDamon Vaughn Caskey\r\n\t\t2013-01-01\r\n\t\t\r\n\t\tWrapper to obtain post value.\r\n\t\t\r\n\t\t$cID:\t\tIndex.\r\n\t\t$cDefault:\tDefault on not set.\r\n\t\t*\/\r\n\t\t\/\/echo $_POST&#x5B;$cID];\r\n\t\t\r\n\t\treturn $this-&gt;utl_validate_isset($_POST&#x5B;$cID], $cDefault);\r\n\t}\r\n\t\r\n\tpublic function utl_get_server_value($cID, $cDefault=NULL)\r\n\t{\r\n\t\t\/*\r\n\t\tutl_get_server_value\r\n\t\tDamon Vaughn Caskey\r\n\t\t2013-01-01\r\n\t\t\r\n\t\tWrapper to obtain server value.\r\n\t\t\r\n\t\t$cID:\t\tIndex.\r\n\t\t$cDefault:\tDefault on not set.\r\n\t\t*\/\r\n\t\t\r\n\t\treturn $this-&gt;utl_validate_isset($_SERVER&#x5B;$cID], $cDefault);\r\n\t}\r\n\t\r\n\tpublic function utl_str_to_array($cList=NULL, $cDel=&quot;,&quot;)\r\n\t{\t\r\n\t\t\/*\r\n\t\tstr_to_array\r\n\t\tDamon Vaughn Caskey\r\n\t\t2013-01-09\r\n\t\t\r\n\t\tBreak string into indexed array with no spaces.\r\n\t\t\r\n\t\t$cList:\tList array to break up.\r\n\t\t$cDel:\tDelimiter.\r\n\t\t*\/\r\n\t\r\n\t\t\/*\r\n\t\tIf list is populated remove spaces and break into array.\r\n\t\t*\/\r\n\t\tif($cList)\t\t\t\t\t\t\t\t\t\/\/List populated?\t\t\t\t\t\t\t\t\r\n\t\t{\r\n\t\t\t$cList = str_replace (&quot; &quot;, &quot;&quot;, $cList);\t\/\/Remove spaces.\r\n\t\t\t$cList = explode($cDel, $cList);\t\t\/\/Break into array.\r\n\t\t}\r\n\t\t\r\n\t\t\/*\r\n\t\tReturn end result.\r\n\t\t*\/\r\n\t\treturn $cList;\r\n\t}\r\n\t\r\n\tpublic function utl_redirect($cURL=NULL)\r\n\t{\t\r\n\t\t\/*\r\n\t\tutl_redirect\r\n\t\tDamon Vaughn Caskey\r\n\t\t2013-01-09\r\n\t\t\r\n\t\tSend header that redirects client to new page.\r\n\t\t\r\n\t\t$cURL:\tTarget address.\r\n\t\t*\/\r\n\r\n\t\t\/*\r\n\t\tIf headers haven't been sent, redirect user to an error page. Otherwise we'll just have to die and settle for a plain text message.\r\n\t\t*\/\r\n\t\t\r\n\t\tif(headers_sent())\r\n\t\t{ \r\n\t\t\t\/*\r\n\t\t\tGood coding will always avoid attempting to resend headers, but let's make sure to catch them here before PHP throws a nasty error.\r\n\t\t\t*\/\t\t\t\r\n\t\t}\r\n\t\telse\r\n\t\t{\t\t\t\r\n\t\t\theader('Location: '.$cURL);\r\n\t\t\treturn TRUE;\r\n\t\t}\r\n\t\t\r\n\t\t\/*\r\n\t\tReturn end result.\r\n\t\t*\/\r\n\t\treturn FALSE;\r\n\t}\r\n\t\r\n\tpublic function utl_validate_email(&amp;$cValue, $cDefault=NULL)\r\n\t{\r\n\t\t\/*\r\n\t\tutl_validate_email\r\n\t\tDamon Vaughn Caskey\r\n\t\t2013_01_01\r\n\t\t\r\n\t\tValidate email variable.\r\n\t\t\r\n\t\t$cValue:\tEmail value.\r\n\t\t$cDefault:\tDefault on fail.\r\n\t\t*\/\r\n\t\t\t\t\r\n\t\tif(filter_var($cValue, FILTER_VALIDATE_EMAIL)) \r\n\t\t{\t\t\t\r\n\t\t\tlist($user,$domaine)=explode(&quot;@&quot;, $cValue, 2);\r\n\t\t\t\r\n\t\t\tif(!checkdnsrr($domaine, &quot;MX&quot;)&amp;&amp; !checkdnsrr($domaine, &quot;A&quot;))\r\n\t\t\t{\r\n\t\t\t\t\/*\r\n\t\t\t\tBad domain.\r\n\t\t\t\t*\/\r\n\t\t\t\t$cValue = FALSE;\r\n\t\t\t}\r\n\t\t\telse \r\n\t\t\t{\r\n\t\t\t\t\/\/Domain OK.\r\n\t\t\t}\r\n\t\t}\r\n\t\telse \r\n\t\t{\t\t\r\n\t\t\t\/*\r\n\t\t\tBad address.\r\n\t\t\t*\/\t\r\n\t\t\t$cValue = FALSE;\r\n\t\t} \r\n\t\t\r\n\t\tif($cValue == FALSE)\r\n\t\t{\r\n\t\t\t$cValue = $cDefault;\r\n\t\t}\r\n\t\t\r\n\t\treturn $cValue;\r\n\t}\r\n\t\r\n\tpublic function utl_validate_ip(&amp;$cValue, $cDefault=NULL)\r\n\t{\r\n\t\t\/*\r\n\t\tutl_validate_ip\r\n\t\tDamon Vaughn Caskey\r\n\t\t2013-01-01\r\n\t\t\r\n\t\tValidate ip variable.\r\n\t\t\r\n\t\t$cValue:\tip value.\r\n\t\t$cDefault:\tDefault on fail.\r\n\t\t*\/\r\n\t\t\r\n\t\t$cValue = filter_var($cValue, FILTER_VALIDATE_IP);\t\t\r\n\t\t\r\n\t\tif($cValue == FALSE)\r\n\t\t{\r\n\t\t\t$cValue = $cDefault;\r\n\t\t}\r\n\t\t\r\n\t\treturn $cValue;\r\n\t}\r\n\t\r\n\tpublic function utl_validate_isset(&amp;$cValue, $cDefault=NULL)\r\n\t{\r\n\t\t\/*\r\n\t\tutl_validate_isset\r\n\t\tDamon Vaughn Caskey\r\n\t\t2013-01-01\r\n\t\t\r\n\t\tReturn default if variable is not set.\r\n\t\t\r\n\t\t$cValue:\tValue.\r\n\t\t$cDefault:\tDefault on not set.\r\n\t\t*\/\r\n\t\t\r\n\t\tif(!isset($cValue))\r\n\t\t{\r\n\t\t\t$cValue = $cDefault;\r\n\t\t}\r\n\t\t\r\n\t\treturn $cValue;\r\n\t}\r\n\t\r\n\tpublic function utl_if_exists($cValue, $cValPrefix=NULL, $cValCadence=NULL, $cAlt=NULL)\r\n\t{\r\n\t\t\/*\r\n\t\tutl_\r\n\t\tDamon Vaughn Caskey\r\n\t\t2013-01-01\r\n\t\t\r\n\t\tReturn self if self has a value, or $cAlt if $cValue is empty or null. Reduces need to retype and \r\n\t\tpotentiality mix up variable names or array keys twice in common &quot;echo &lt;X&gt; if it has a value&quot; situations.\r\n\r\n\t\tPreconditions:\r\n\t\t\t$cValue: Value to test and return if it exists.\r\n\t\t\t$cValPrefix: Add to front of $cValue on return.\r\n\t\t\t$cValCadence: Add to end of $cValue on return.\r\n\t\t\t$cAlt: Value to return if $cValue is NULL.\r\n\t\t*\/\r\n\t\t\r\n\t\t\/* Vaiables *\/\r\n\t\t$cReturn = $cAlt;\t\/\/Final value to return.\r\n\t\t\r\n\t\t\/* Value exists? *\/\r\n\t\tif($cValue)\r\n\t\t{\r\n\t\t\t\/* Tack on additions and return value. *\/\t\t\t\r\n\t\t\t$cReturn = $cValPrefix.$cValue.$cValCadence;\r\n\t\t}\r\n\t\t\r\n\t\treturn $cReturn;\t\t\r\n\t}\r\n}\r\n?&gt;\r\n\r\n\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Utility class.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[71],"tags":[27,234],"class_list":["post-5289","post","type-post","status-publish","format-standard","hentry","category-technology-temerity","tag-coding","tag-coding-php"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5lNM5-1nj","jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.caskeys.com\/dc\/wp-json\/wp\/v2\/posts\/5289","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.caskeys.com\/dc\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.caskeys.com\/dc\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.caskeys.com\/dc\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.caskeys.com\/dc\/wp-json\/wp\/v2\/comments?post=5289"}],"version-history":[{"count":2,"href":"https:\/\/www.caskeys.com\/dc\/wp-json\/wp\/v2\/posts\/5289\/revisions"}],"predecessor-version":[{"id":6180,"href":"https:\/\/www.caskeys.com\/dc\/wp-json\/wp\/v2\/posts\/5289\/revisions\/6180"}],"wp:attachment":[{"href":"https:\/\/www.caskeys.com\/dc\/wp-json\/wp\/v2\/media?parent=5289"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.caskeys.com\/dc\/wp-json\/wp\/v2\/categories?post=5289"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.caskeys.com\/dc\/wp-json\/wp\/v2\/tags?post=5289"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}