1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# Load the EWS Managed API #https://gsexdev.blogspot.com/2012/03/ews-managed-api-and-powershell-how-to_27.html Remove-Variable * -ErrorAction SilentlyContinue; Remove-Module *; $error.Clear(); Clear-Host #You cannot use this with Param Add-Type -Path "C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll" #param ( # [string[]]$ParameterName # ) function GetFirstOpenTime ([Object]$Attendeesbatch, $pStartTime, $pEndTime) { try { $drDuration = new-object Microsoft.Exchange.WebServices.Data.TimeWindow($pStartTime,$pEndTime) $AvailabilityOptions = new-object Microsoft.Exchange.WebServices.Data.AvailabilityOptions $AvailabilityOptions.MeetingDuration = 30 $AvailabilityOptions.RequestedFreeBusyView = [Microsoft.Exchange.WebServices.Data.FreeBusyViewType]::DetailedMerged $availresponse = $exchangeService.GetUserAvailability($Attendeesbatch, $drDuration, [Microsoft.Exchange.WebServices.Data.AvailabilityData]::Suggestions, $AvailabilityOptions) foreach($availSug in $availresponse.Suggestions) { foreach($suggestion in $availSug) { if (($suggestion.Date.DayOfWeek -eq "Saturday") -or ($suggestion.Date.DayOfWeek -eq "Sunday")) { continue } if ($availSug.TimeSuggestions.Count -gt 0) { "Date : " + $suggestion.Date + " - " + $suggestion.Date.DayOfWeek "Quality : " + $suggestion.Quality } foreach($timesug in $availSug.TimeSuggestions) { if (($timesug.MeetingTime.Date.AddHours(8) -ge $timesug.MeetingTime) -or ($timesug.MeetingTime.Date.AddHours(16) -le $timesug.MeetingTime)) { continue } foreach($timeCon in $timesug.Conflicts) { "`t`tConflictType : " + $timeCon.ConflictType "`t`tNumberOfMembersAvailable : " + $timeCon.NumberOfMembersAvailable "`t`tNumberOfMembers : " + $timeCon.NumberOfMembers } "`tIsWorkTime : " + $timesug.IsWorkTime "`tQuality : " + $timesug.Quality "`tTimeSuggestions : " + $timesug.MeetingTime "" return $timesug #Return to parent function, Remove to continue enumerating } "" } } foreach($avail in $availresponse.AttendeesAvailability) { $avail foreach($cvtEnt in $avail.CalendarEvents) { #$cvtEnt.Details "`tStart : " + $cvtEnt.StartTime "`tEnd : " + $cvtEnt.EndTime "`tSubject : " + $cvtEnt.Details.Subject "`tLocation : " + $cvtEnt.Details.Location "" } foreach($cvtEntFree in $avail.MergedFreeBusyStatus) { #$cvtEnt.Details $cvtEntFree.value__ "`tStart : " + $cvtEntFree.StartTime "`tEnd : " + $cvtEntFree.EndTime "`tSubject : " + $cvtEntFree.Details.Subject "`tLocation : " + $cvtEntFree.Details.Location "" } } } catch { $_ $entryType = "Error" $subject = "Error in mailbox monitor script" $messageBody = "{0}`r`n{1}" -f $_.Exception.Message,$_.InvocationInfo.PositionMessage } } $Exchange2007SP1 = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2007_SP1 $Exchange2010 = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010 $Exchange2010SP1 = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP1 $Exchange2010SP2 = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP2 $Exchange2013 = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2013 $Exchange2013SP1 = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2013_SP1 $mailboxName1 = "email0@domain.com" $mailboxName2 = "email1@domain.com" # create EWS Service object for the target mailbox name $exchangeService = New-Object -TypeName Microsoft.Exchange.WebServices.Data.ExchangeService -ArgumentList $Exchange2010SP2 $exchangeService.UseDefaultCredentials = $true $exchangeService.AutodiscoverUrl($mailboxName1) $listtype = ("System.Collections.Generic.List"+'`'+"1") -as "Type" $listtype = $listtype.MakeGenericType("Microsoft.Exchange.WebServices.Data.AttendeeInfo" -as "Type") $Attendeesbatch = [Activator]::CreateInstance($listtype) $Attendee = new-object Microsoft.Exchange.WebServices.Data.AttendeeInfo($mailboxName1) $Attendeesbatch.add($Attendee) $Attendee = new-object Microsoft.Exchange.WebServices.Data.AttendeeInfo($mailboxName2) $Attendeesbatch.add($Attendee) $StartTime = [DateTime]::Parse([DateTime]::Now.ToString("yyyy-MM-dd 0:00")) $EndTime = $StartTime.AddDays(7) GetFirstOpenTime $Attendeesbatch $StartTime $EndTime |
Convert images on the fly with PHP
Sample code used to resize images in PHP.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
<?php function createThumbnail($image_name,$new_width,$new_height,$uploadDir,$moveToDir) { $path = $uploadDir . '/' . $image_name; $mime = getimagesize($path); if($mime['mime']=='image/png') { $src_img = imagecreatefrompng($path); } if($mime['mime']=='image/jpg' || $mime['mime']=='image/jpeg' || $mime['mime']=='image/pjpeg') { $src_img = imagecreatefromjpeg($path); } $old_x = imageSX($src_img); $old_y = imageSY($src_img); if($old_x > $old_y) { $thumb_w = $new_width; $thumb_h = $old_y*($new_height/$old_x); } if($old_x < $old_y) { $thumb_w = $old_x*($new_width/$old_y); $thumb_h = $new_height; } if($old_x == $old_y) { $thumb_w = $new_width; $thumb_h = $new_height; } $dst_img = ImageCreateTrueColor($thumb_w,$thumb_h); imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); // New save location $new_thumb_loc = $moveToDir . $image_name; if($mime['mime']=='image/png') { $result = imagepng($dst_img,$new_thumb_loc,8); } if($mime['mime']=='image/jpg' || $mime['mime']=='image/jpeg' || $mime['mime']=='image/pjpeg') { $result = imagejpeg($dst_img,$new_thumb_loc,80); } imagedestroy($dst_img); imagedestroy($src_img); return $result; } function compress($source, $destination, $quality) { $info = getimagesize($source); if ($info['mime'] == 'image/jpeg') $image = imagecreatefromjpeg($source); elseif ($info['mime'] == 'image/gif') $image = imagecreatefromgif($source); elseif ($info['mime'] == 'image/png') $image = imagecreatefrompng($source); imagejpeg($image, $destination, $quality); return $destination; } //$source_img = 'source.jpg'; //$destination_img = 'destination .jpg'; //$d = compress($source_img, $destination_img, 90); $image_name = 'https://media.geeksforgeeks.org/wp-content/uploads/geeksforgeeks-15.png'; $img_src = "Image1"; if(isset($_GET['Image2'])) { $dest_size = 1800; list($width, $height) = getimagesize($image_name); $img = imagecreatefrompng($image_name); $largersize = $width; if ($height > $width) { $largersize = $height; } $ratioToShrink = 0; if ($largersize > $dest_size || true) { $ratioToShrink = $largersize / $dest_size; //echo $ratioToShrink . "<br>"; $newheight = round($height / $ratioToShrink, 0, PHP_ROUND_HALF_UP); $newwidth = round($width / $ratioToShrink, 0, PHP_ROUND_HALF_UP); //echo $newheight . "<br>"; //echo $newwidth . "<br>"; //$img = imagescale( $img, 600, 200 ); $img = imagescale( $img, $newwidth, $newheight ); } //header("content-type: text/html; charset=UTF-8"); header("Content-type: image/png"); imagepng($img); return; } if(isset($_GET['Image'])) { // Assign image file to variable // Load image file $image = imagecreatefrompng($image_name); // Use imagescale() function to scale the image $img = imagescale( $image, 500, 400 ); // Output image in the browser header("Content-type: image/png"); imagepng($img); //$img_src=$_GET['img_src'] } else { echo "<br>Before: " . var_dump(getimagesize($image_name)); echo "<br><img src='./" . $_SERVER['PHP_SELF'] . "?Image=" . $img_src . "' alt='img'><br>After: "; echo "<br><img src='" . $image_name . "' alt='img'><br>"; echo "<br><img src='./" . $_SERVER['PHP_SELF'] . "?Image2=" . $img_src . "' alt='img'><br>After: "; //echo "Before: " . var_dump(getimagesize("./" . $_SERVER['PHP_SELF'] . "?Image=" . $img_src . ")); header("content-type: text/html; charset=UTF-8"); echo 15/2; } |
Formatting and verifying phone number formats to message with PHP and Amason SES
Prepping the Environment
1 2 3 4 5 6 7 8 9 10 11 12 |
root@ip:~# echo "export AWS_SHARED_CREDENTIALS_FILE=/root/.aws/credentials" >> /etc/apache2/envvars root@ip:~# chown -R www-data:www-data /root root@ip:~# service apache2 restart root@ip:~/.aws# cat credentials [default] aws_access_key_id = AKIZXZXZXZXZXXZZXQW aws_secret_access_key = jzv6v9v6a9S+aQWSA70DA6/JADA6A6A2GAW7 [project1] aws_access_key_id = ANOTHER_AWS_ACCESS_KEY_ID aws_secret_access_key = ANOTHER_AWS_SECRET_ACCESS_KEY |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
<?php require '/home/ubuntu/vendor/autoload.php'; use Aws\Sns\SnsClient; //use Aws\S3\S3Client; use Aws\Exception\AwsException; function formatNumber($number) { $formatted = preg_replace('~.*(\d{3})[^\d]{0,7}(\d{3})[^\d]{0,7}(\d{4}).*~', '+1$1$2$3', $number). "\n"; $formatted = trim($formatted, "\n\r "); if(preg_match("/^\+1[0-9]{10}$/", $formatted)) { print "valid: "; // $phone is valid print $number . " - > " . $formatted . "\r\n"; return $formatted; } else { print "not a phone: "; print $number . "\r\n"; return NULL; } } function sendSMS($number, $body) { $myvar = formatNumber($number); if (!is_null($myvar)) { $SnSclient = new SnsClient([ 'profile' => 'default', 'region' => 'us-west-2', 'version' => '2010-03-31' ]); $message = $body; //'This is being sent from AWS with sms.php in the public folder.'; $phone = $myvar; //'+12223334444'; try { $result = $SnSclient->publish([ 'Message' => $message, 'PhoneNumber' => $phone, ]); //var_dump($result); } catch (AwsException $e) { // output error message if fails echo ($e); error_log($e->getMessage()); } return true; } else { //invalid number return false; } } //sendSMS("+12223334444", "test"); ?> |
Using Twilio with ASP.Net
For the HandleTwilioSMS use a URL like: http://MyWebsite.com/MyApi.asmx/HandleTwilioSMS
For HandleTwiliPhone use a URL like: http://MyWebsite.com/TwilioPhoneHandler.aspx
For Decide use “widgets.HandleTwilioPhoneCallAPI.parsed.Status” then set your conditions returned.
Aannnnnnddd your welcome!

1 2 3 4 5 6 7 8 9 10 11 12 |
Public Shared MyTwilio As New List(Of TwilioEndPoint) Public Class TwilioEndPoint Public PhoneNumber As String Public AutoReply As Boolean Public AutoReplyMessage As String Public LastAutoReplyTimestamp As DateTime Public Block As Boolean Public BlockMessage As String Public Forward As Boolean Public ForwardNumber As String End Class |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
Imports Newtonsoft.Json Public Class TwilioPhoneHandler Inherits System.Web.UI.Page Public Class PhoneHandlerResponse Public Property Status As String Public Property Argument1 As String End Class Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Response.Clear() Dim FromNumber = Request.QueryString("FromNumber") Dim MyPhoneHandlerResponse As New PhoneHandlerResponse If Not IsNothing(FromNumber) Then FromNumber = FromNumber.Trim.Replace("+", "") FromNumber = "+" & FromNumber 'Readd '+' sign that was filtered by HTTP Dim MyClient As Global_asax.TwilioEndPoint = GetTwilioItem(FromNumber) If Not IsNothing(MyClient) Then If MyClient.Forward Then MyPhoneHandlerResponse.Status = "forward" MyPhoneHandlerResponse.Argument1 = MyClient.ForwardNumber End If If MyClient.Block Then MyPhoneHandlerResponse.Status = "blocked" End If Else MyPhoneHandlerResponse.Status = "default" 'Goes to Twilio NoCondition, Application call End If Else MyPhoneHandlerResponse.Status = "default" End If If IsNothing(MyPhoneHandlerResponse.Status) Then MyPhoneHandlerResponse.Status = "default" End If Dim StringToReturn = JsonConvert.SerializeObject(MyPhoneHandlerResponse) Response.StatusCode = 200 Response.ContentType = "application/json" Response.Write(StringToReturn) Response.Flush() Response.Close() End Sub End Class |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
<WebMethod()> Public Sub HandleTwilioSMS(ByVal FromNumber As String, ByVal ToNumber As String, ByVal Message As String) If IsNothing(GetTwilioItem(FromNumber)) Then Dim DefaultMessage As String = "Put your default message here" AddTwilioToList(FromNumber, True, DefaultMessage, False, "") End If For Each TwilioClient As Global_asax.TwilioEndPoint In Global_asax.MyTwilio If TwilioClient.PhoneNumber = FromNumber Then If TwilioClient.Block Then If Not IsNothing(TwilioClient.BlockMessage) Then PublicModule.SendSMSWithTwilio(FromNumber, ToNumber, TwilioClient.BlockMessage) Else Throw New Exception("Blocked") End If Return End If If Message = "1" Then PublicModule.SendSMSWithTwilio(FromNumber, ToNumber, "User Sent back Number 1") Return End If If TwilioClient.AutoReply Then If Not IsNothing(TwilioClient.AutoReplyMessage) Then PublicModule.SendSMSWithTwilio(FromNumber, ToNumber, TwilioClient.AutoReplyMessage) End If End If End If Next Try Catch ex As Exception Return End Try End Sub Public Function SendSMSWithTwilio(ByVal FromNumber As String, ByVal ToNumber As String, ByVal Body As String) As Boolean Try Net.ServicePointManager.SecurityProtocol = 3072 Dim accountSid As String = "PutAccountSidHere" Dim accountToken As String = "PutTokenHere" Twilio.TwilioClient.Init(accountSid, accountToken) 'From = Dest 'ToNumber = Should be your registered Twilio Number Dim CreateMessageOptions As New CreateMessageOptions(New Twilio.Types.PhoneNumber(FromNumber)) CreateMessageOptions.Body = Body CreateMessageOptions.From = New Twilio.Types.PhoneNumber(ToNumber) Dim res = MessageResource.Create(CreateMessageOptions) Debug.WriteLine("Message SID: " & res.Sid) Catch ex As Exception Debug.WriteLine(ex.Message) Return False End Try Return True End Function |
Using ClientID Get Property Sets ID value for ASP.net controls
So my discovery today is an example of silly programming errors that I tend to supprise me.
Some of my controls in ASP.net don’t have assigned ID’s. However using the code below you should see an example of one control returning the first IsNothing line without a value, but once ClientID is used it will populate the control’s ID so it will be found the second time. I recommend just using ClientID for logic, if the ID is not populated it will autogenerate it for you to be handled on your code proceeding after.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
EnumerateControls(Me.Controls, 0) Private Function EnumerateControls(MyControlCollection As ControlCollection, index As Integer) index += 1 For Each MyControl As Control In MyControlCollection For index = 0 To index 'Debug.Write(vbTab) Next If Not IsNothing(MyControl.ID) Then 'If MyControl.ID.Length > 0 Then If MyControl.ID = "TableRow2" Then End If Debug.WriteLine(MyControl.GetType().ToString & ": " & MyControl.ID & " - " & MyControl.Parent.ID) If MyControl.HasControls Then EnumerateControls(MyControl.Controls, index) End If 'End If Else If IsNothing(MyControl.ID) Then Debug.WriteLine("Nothing - " & MyControl.ID) End If If IsNothing(MyControl.ID) Then Debug.WriteLine("Nothing2 - " & MyControl.ClientID & MyControl.ID) End If Debug.WriteLine(MyControl.ID) If MyControl.ClientID = "ctl46" Then Debug.WriteLine(MyControl.GetType()) If IsNothing(MyControl.ID) Then Debug.WriteLine("Nothing - " & MyControl.ClientID & MyControl.ID) End If End If Debug.WriteLine(MyControl.ClientID) End If Next End Function |