- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
private function isCorrectFileName($fileName) { $fileName = strtolower($fileName); $suffix = $this->getMessageNumberForAttachmentFilename() . $this->getAttachmentSuffix(); $suffixLocation = strpos($fileName, $suffix); if ($suffixLocation !== false) { $fileName = str_replace($suffix, '', $fileName); $fileName = str_replace($this->getAttachmentPrefix(), '', $fileName); if (is_numeric($fileName) && ($fileName == 0)) return true; } return FileNotFound; }
Admin
It actually took me a while - including rereading the introduction - to realise WHY this would fail for message IDs greater than 999. And then having seen why it did - wow, whoever coded this obviously knew this wouldn't work for numbers past 1000. Or at least knew they were relying on there always being a 0 before the number in the filename, and didn't think to look up, or ask anyone, where this was coming from. So I'm not sure whether this is a case of
Probably 1) is much more likely, but whichever it is, that's TRRRRRWTF.
Admin
It's generic and will work on any system, assuming they use a very specific format for their file names.
Admin
Run inotify on the directory. Log stale files and notify.
Admin
Instead of specifications you get a set of, say, 150, sample files "msg0000.wav" through "msg0149.wav". The message number begins with "0" in all the samples. So do you assume it always begins with zero, or that it's always four digits?
That depends on whether or not you are an idiot.
Admin
The real WTF here is that you can enable email notifications directly in Asterisk
https://github.com/asterisk/asterisk/blob/master/configs/samples/voicemail.conf.sample
Admin
Why not just use the builtin asterisk feature of emailing the voicemail in the first place?
Admin
Chaotic neutral bugfix: replace the filename prefix with '0' instead.
Chaotic evil bugfix: just remove the is_numeric, won't PHP autocast string to int if you have the arguments the right way around? And I doubt '' wouldn't cast to 0 there. I can totally see this being an error that was introduced during a code review/cleanup by somebody else who just saw "oh, it's casting to int and doesn't run is_numeric first, that's got to be wrong".
Admin
See, this is exactly why I like to code my stuff that interacts with other systems in the stupidest way possible. Your spec says the filename is going to be "msg<four digits>.wav"? Fine, that's what I'll code for. I'll test to see if it meets that criteria but I won't help you if it doesn't.
You'd be amazed how quickly daft little edge cases get shaken out by this. Oh, it's four digits, except when it's five digits and the codec is raw G.711? Sure. Good to know.
Oh and now we're using a totally different format string? I'll just write another decoder then.
Keep it simple. Keep it about as technical as a hammer. Keep it working.