A South African opposition leader has been handed a five-year prison sentence for firing a rifle in the air at a rally.