Turkey's Prime Minister Tayyip Erdogan greets his supporters during an election in Istanbul March 23, 2014. (Murad Sezer/Reuters)

When Arabs rose up three years ago to demand greater freedoms, it was to the Turkey of Prime Minister Recep Tayyip Erdogan that many looked for inspiration, as an example of a country in which Islam and democracy could peacefully and prosperously coexist.

Now, after 11 years in office, Turkey’s leader is starting to act in ways that more closely resemble some of the Arab autocrats with whom he had been favorably compared, calling into question the durability of his much-vaunted Turkish miracle.

Bans imposed on the social media sites Twitter and YouTube in recent days are just the latest examples of Erdogan’s increasingly highhanded tactics. An avalanche of mysterious leaks in the past three months, most disseminated on YouTube, have suggested corruption and malfeasance within his administration. In an attempt to suppress the scandals, he has muzzled journalists, fired police officers and reassigned judges.

Ahead of local elections Sunday that will serve as a crucial referendum on his rule, Erdogan’s behavior seems only to have grown more erratic. In campaign speeches, he has labeled his enemies “perverts,” denounced social media as a “menace” and blamed the leaks on elaborate foreign plots, language reminiscent of that used by Arab leaders seeking to resist demands for change.

To many of his critics, the outbursts suggest panic ahead of the voting, which could prove decisive for Erdogan’s political future. His aides dispute that and say that the prime minister has been misunderstood.

Criticism of the Twitter and YouTube bans is unfair and overlooks the political context of the leaks, which suggest a campaign to discredit the prime minister ahead of the elections, they say. Officials say Twitter was outlawed because of complaints from constituents about its intrusion on people’s private lives. The ban on YouTube was a matter of national security, after the latest leaks broadcast a highly sensitive discussion between top officials over Turkey’s military options in neighboring Syria, they say.

Erdogan and other government officials have pointed blame in the past at supporters of Fethullah Gulen, a wealthy U.S.-based cleric who commands the loyalties of a large and shadowy network of influential Turks and who until recently was an Erdogan ally.

But Gulen has denied responsibility, and it remains unclear whether the leaks — more than 50 recordings anonymously uploaded to the Internet that feature purported attempts to hide money, sexual indiscretions and other misdeeds — have all come from one source.

Whoever is responsible, “this is not an issue of authoritarianism versus free speech. It is a matter of protecting privacy and national security,” said a senior Turkish official, who spoke on the condition of anonymity because of the sensitivity of the subject.

“How can a liberal democracy be expected to decide its future on the basis of illegal wiretappings?” he asked, referring to the effect the leaks might have on the elections.

Meanwhile, the official said, Erdogan has confidence in opinion polls suggesting that his Justice and Development Party (AKP) will retain a plurality of the vote, although by a lower margin than his landslide victory of 50 percent in the parliamentary elections of 2011.

If the AKP’s nationwide share of the vote falls much below the 42 to 46 percent forecast by polls, however, Erdogan’s hopes of running for president in the summer or for a fourth term as prime minister next year could be in trouble, analysts say.

But there is no reason to believe that the upheaval will dislodge Erdogan from his position as Turkey’s foremost politician. In a crowded field, no other leader matches his charisma or the powerful network of patronage he has cultivated over the past 11 years.

From Erdogan’s perspective, much of his recent behavior makes sense, analysts say. His constituency comprises the legions of lower-middle- and working-class Turks who share his humble origins and whose living standards have soared under his tenure. Few are among the country’s 12 million Twitter subscribers; they are unlikely to watch videos on YouTube; and they tend to share Erdogan’s suspicions of social media.

In the modest, working-class Istanbul neighborhood of Kasimpasa, where Erdogan grew up, residents lavish praise on his achievements, citing improvements to everything from the cleanliness of the streets and the availability of water to Turkey’s newfound stature on the world stage.

“This government brought us to a position we could only have dreamed of,” said Ismail Senkulak, 55, who works for the government’s electricity department and was sipping tea on a stool outside a small tea shop. “I might have some reservations about his dictatorship and cronyism, but they are not significant compared to all the good he has done.”

Mehmet Kabaca, 53, described how he was bankrupt when Erdogan came to power but has since earned enough money to buy his own home and secure a decent pension. He said he had not been planning to vote for Erdogan, but the recent wiretaps convinced him that foreign powers are engaged in a plot to undermine Turkey.

“As Turks we have to rally behind our leader,” he said. “We are the sons of the thousands of years of history, we are the children of emperors, and we never bow to other countries.”

Such sentiments are widely shared among ordinary Turks, and Erdogan has exploited them adroitly, said Hugh Pope of the International Crisis Group. Yet, paradoxically, it is Turkey’s international reputation that may be the biggest loser in the political turmoil, he said. The social media bans have drawn international condemnation, and European Union officials have hinted that they may put the brakes on Turkey’s already stalled bid to join the bloc.

An even bigger loss may be Turkey’s opportunity to point the way to a different future for a region whose efforts to embrace greater freedoms have gone badly awry, said Soli Ozel, professor of international relations at Istanbul’s Kadir Has University.

“The ability to speak both the language of religion and the language of democracy gave Erdogan an extraordinary chance to create a new kind of synthesis,” he said. “Right now, it looks like he’s blown it.”