PageRenderTime 51ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/wordpress-mobile-admin/wp-admin/functions.php

https://bitbucket.org/crypticrod/sr_wp_code
PHP | 421 lines | 297 code | 52 blank | 72 comment | 49 complexity | 0ed8aabb42556d55c30f80354db9ae1b MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.1, GPL-3.0, LGPL-2.0, AGPL-3.0
  1. <?php
  2. /**
  3. * @package Wapple_Architect
  4. * @subpackage WAPL Theme
  5. */
  6. /**
  7. * Debug function
  8. * @param $debug mixed
  9. * @access public
  10. * @return void
  11. */
  12. if(!function_exists('debug'))
  13. {
  14. function debug($debug)
  15. {
  16. echo '<pre>'.print_r($debug,true).'</pre>';
  17. }
  18. }
  19. /**
  20. * Process WAPL into meaningful markup
  21. * @param $waplString string
  22. * @access public
  23. * @return void
  24. */
  25. if(!function_exists('process_admin_wapl'))
  26. {
  27. function process_admin_wapl($waplString)
  28. {
  29. if(ARCHITECT_ADMIN_DEBUG)
  30. {
  31. header('Content-type: application/xml');
  32. echo $waplString;
  33. die();
  34. }
  35. if(ARCHITECT_ADMIN_DO_SOAP)
  36. {
  37. global $adminWaplHeaders;
  38. global $adminWaplSoapClient;
  39. $params = array(
  40. 'devKey' => get_option('architect_admin_devkey'),
  41. 'wapl' => $waplString,
  42. 'deviceHeaders' => $adminWaplHeaders
  43. );
  44. // Send markup to API and parse through simplexml
  45. $result = @$adminWaplSoapClient->getMarkupFromWapl($params);
  46. if(is_soap_fault($result))
  47. {
  48. if(strpos($result->faultstring, 'Account API request limit reached') === 0)
  49. {
  50. setcookie('architectError', $result->faultstring, time()+1800, '/');
  51. }
  52. setcookie('isMobile', "0", time()+1800, '/');
  53. header('Location:'.get_permalink());
  54. exit();
  55. } else
  56. {
  57. setcookie('architectError', "", time()-3600, '/');
  58. }
  59. $xml = simplexml_load_string($result);
  60. foreach($xml->header->item as $val)
  61. {
  62. header($val);
  63. }
  64. // Flush output buffer - to clean up any other plugin mess!
  65. ob_end_clean();
  66. $markup = trim($xml->markup);
  67. $markup = str_replace('""http://www.wapforum.org', '" "http://www.wapforum.org', $markup);
  68. // Echo correct markup
  69. echo trim($markup);
  70. die();
  71. } else if(ARCHITECT_ADMIN_DO_REST)
  72. {
  73. global $adminWaplHeaders;
  74. $postfields = array(
  75. 'devKey' => get_option('architect_admin_devkey'),
  76. 'wapl' => $waplString,
  77. 'headers' => $adminWaplHeaders
  78. );
  79. $c = curl_init();
  80. curl_setopt($c, CURLOPT_URL, 'http://webservices.wapple.net/getMarkupFromWapl.php');
  81. curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
  82. curl_setopt($c, CURLOPT_POST, 1);
  83. curl_setopt($c, CURLOPT_POSTFIELDS, $postfields);
  84. $result = curl_exec($c);
  85. if(strpos($result, 'Account API request limit reached')!== false)
  86. {
  87. setcookie('architectError', 'Account API request limit reached', time()+1800, '/');
  88. setcookie('isMobile', "0", time()+1800, '/');
  89. header('Location:'.get_permalink());
  90. exit();
  91. } else if(strpos($result, 'Developer key authentication error') !== false)
  92. {
  93. setcookie('architectError', 'Developer key authentication error', time()+1800, '/');
  94. setcookie('isMobile', "0", time()+1800, '/');
  95. header('Location:'.get_permalink());
  96. exit();
  97. } else
  98. {
  99. setcookie('architectError', "", time()-3600, '/');
  100. }
  101. curl_close($c);
  102. $xml = simplexml_load_string($result);
  103. foreach($xml->header->item as $val)
  104. {
  105. header($val);
  106. }
  107. // Flush output buffer - to clean up any other plugin mess!
  108. ob_end_clean();
  109. $markup = trim($xml->markup);
  110. $markup = str_replace('""http://www.wapforum.org', '" "http://www.wapforum.org', $markup);
  111. // Echo correct markup
  112. echo trim($xml->markup);
  113. die();
  114. } else
  115. {
  116. header('Content-type: application/xml');
  117. echo $waplString;
  118. }
  119. }
  120. }
  121. require_once('wapl_builder.php');
  122. /**
  123. * WordPress WAPL parser
  124. *
  125. * Extends the waplBuilder class to build perfect markup
  126. * @author Rich Gubby
  127. */
  128. class WordPressAdminWapl extends waplAdminBuilder
  129. {
  130. /**
  131. * Format content into WAPL readable format
  132. * @param string $content
  133. * @param integer $imagescale
  134. * @param integer $imagequality
  135. * @param string $class
  136. * @param integer $length
  137. * @param string $transcol
  138. * @access public
  139. * @return string
  140. */
  141. function format_text($content, $imagescale = 95, $imagequality = 90, $class = 'entry', $length = null, $transcol = '')
  142. {
  143. // Remove comments
  144. $content = str_replace(
  145. array('&#8211;', '<strong><strong>', '&copy;', '&nbsp;'),
  146. array('--', '<strong>', '&#169;', '&#160;'),
  147. $content
  148. );
  149. $content = $this->foreignChars($content);
  150. // Ampersand cleanup
  151. $content = preg_replace('/(?!&#)&/', '&amp;', $content);
  152. preg_match_all('/&#[0-9]{1,}(;)?/', $content, $matches, PREG_OFFSET_CAPTURE);
  153. foreach($matches[0] as $key => $val)
  154. {
  155. if(substr($val[0], -1) != ';')
  156. {
  157. $content = substr_replace($content, str_replace('&#', '&amp;#', $val[0]), $val[1], strlen($val[0]));
  158. }
  159. }
  160. $replacements = array();
  161. require_once('simple_html_dom.php');
  162. $html = wpma_str_get_html($content);
  163. // Remove comments
  164. foreach($html->find('comment') as $element)
  165. {
  166. $content = str_ireplace($element->outertext, '', $content);
  167. }
  168. // Remove script tags
  169. foreach($html->find('script') as $element)
  170. {
  171. $replacements[trim($element->outertext())] = '';
  172. }
  173. // Replace [caption] with <caption>
  174. $content = preg_replace('/(\[caption(.*?)\])(.*?)(\[\/caption\])/i', '<caption ${2}>${3}</caption>', $content);
  175. foreach($replacements as $key => $val)
  176. {
  177. $content = str_ireplace(trim($key), $val, $content);
  178. }
  179. // Use parent format text
  180. $content = parent::format_text($content, $imagescale, $imagequality, $class, $length, $transcol);
  181. return utf8_encode(str_replace('& ', '&amp; ', $content));
  182. }
  183. /**
  184. * Clean up ampersands in text
  185. *
  186. * @param string $content
  187. * @access public
  188. * @return string
  189. */
  190. function ampersand_cleanup($content)
  191. {
  192. $content = preg_replace('/(?!&#)&/', '&amp;', $content);
  193. preg_match_all('/&#[0-9]{1,}(;)?/', $content, $matches, PREG_OFFSET_CAPTURE);
  194. foreach($matches[0] as $key => $val)
  195. {
  196. if(substr($val[0], -1) != ';')
  197. {
  198. $content = substr_replace($content, str_replace('&#', '&amp;#', $val[0]), $val[1], strlen($val[0]));
  199. }
  200. }
  201. return $content;
  202. }
  203. /**
  204. * Check if a string is UTF8 or not
  205. *
  206. * @param string $string
  207. * @access public
  208. * @return string
  209. */
  210. function is_utf8($string)
  211. {
  212. return (preg_match('/^([\x00-\x7f]|[\xc0-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xf7][\x80-\xbf]{3}|[\xf8-\xfb][\x80-\xbf]{4}|[\xfc-\xfd][\x80-\xbf]{5})*$/', $string) === 1);
  213. }
  214. /**
  215. * Convert foreign characters to ascii alternatives
  216. *
  217. * @param string $string
  218. * @access public
  219. * @return string
  220. */
  221. function foreignChars($string)
  222. {
  223. // Add support for Japanese, Chinese, Korean, etc characters
  224. if(function_exists('mb_detect_encoding'))
  225. {
  226. $newString = '';
  227. for ($i=0; $i < mb_strlen($string, 'UTF-8'); $i++)
  228. {
  229. $ch = mb_substr($string, $i, 1, 'UTF-8');
  230. if($ch && trim($ch) != '')
  231. {
  232. $returnVal = $this->uniord($ch);
  233. if($returnVal['encode'] == true)
  234. {
  235. $newString .= '&#'.$returnVal['ud'].';';
  236. } else if(!$returnVal['die'])
  237. {
  238. $newString .= mb_substr($string, $i, 1, 'UTF-8');
  239. }
  240. } else
  241. {
  242. $newString .= mb_substr($string, $i, 1, 'UTF-8');
  243. }
  244. }
  245. $string = $newString;
  246. require_once('language.php');
  247. $chars = architectAdminGetTranslation('chars');
  248. // Convert any other characters
  249. $string = str_replace(
  250. array_keys($chars),
  251. array_values($chars),
  252. $string);
  253. } else
  254. {
  255. // Check for dodgy chars
  256. for($i = 0; $i < strlen($string); $i++)
  257. {
  258. if(ord($string[$i]) == 26)
  259. {
  260. $string[$i] = '';
  261. }
  262. }
  263. require_once('language.php');
  264. $chars = architectAdminGetTranslation('chars');
  265. $charsOriginal = architectAdminGetTranslation('charsOriginal');
  266. // Convert characters manually
  267. $string = str_replace(
  268. array_keys(array_merge($chars, $charsOriginal)),
  269. array_values(array_merge($chars, $charsOriginal)),
  270. $string
  271. );
  272. }
  273. // Any other characters
  274. $string = architectAdminCharsOther($string);
  275. return utf8_decode($string);
  276. }
  277. function uniord($c)
  278. {
  279. $ud = 0;
  280. $encode = true;
  281. $die = false;
  282. if (ord($c{0})>=0 && ord($c{0})<=127)
  283. {
  284. if(ord($c{0}) == 26)
  285. {
  286. $die = true;
  287. }
  288. $encode = false;
  289. $ud = $c{0};
  290. }
  291. if (ord($c{0})>=192 && ord($c{0})<=223)
  292. {
  293. $ud = (ord($c{0})-192)*64 + (ord($c{1})-128);
  294. }
  295. if (ord($c{0})>=224 && ord($c{0})<=239)
  296. {
  297. $ud = (ord($c{0})-224)*4096 + (ord($c{1})-128)*64 + (ord($c{2})-128);
  298. }
  299. if (ord($c{0})>=240 && ord($c{0})<=247)
  300. {
  301. $ud = (ord($c{0})-240)*262144 + (ord($c{1})-128)*4096 + (ord($c{2})-128)*64 + (ord($c{3})-128);
  302. }
  303. if (ord($c{0})>=248 && ord($c{0})<=251)
  304. {
  305. $ud = (ord($c{0})-248)*16777216 + (ord($c{1})-128)*262144 + (ord($c{2})-128)*4096 + (ord($c{3})-128)*64 + (ord($c{4})-128);
  306. }
  307. if (ord($c{0})>=252 && ord($c{0})<=253)
  308. {
  309. $ud = (ord($c{0})-252)*1073741824 + (ord($c{1})-128)*16777216 + (ord($c{2})-128)*262144 + (ord($c{3})-128)*4096 + (ord($c{4})-128)*64 + (ord($c{5})-128);
  310. }
  311. if (ord($c{0})>=254 && ord($c{0})<=255)
  312. {
  313. //error
  314. $ud = false;
  315. }
  316. return array('ud' => $ud, 'encode' => $encode, 'die' => $die);
  317. }
  318. }
  319. if(!function_exists('architect_post_rows'))
  320. {
  321. function architect_post_rows()
  322. {
  323. global $wp_query, $post, $mode;
  324. add_filter('the_title','esc_html');
  325. // Create array of post IDs.
  326. $post_ids = array();
  327. if ( empty($posts) )
  328. $posts = &$wp_query->posts;
  329. return $posts;
  330. }
  331. }
  332. if(!function_exists('architect_return_posts_per_page'))
  333. {
  334. function architect_return_posts_per_page()
  335. {
  336. return 10;
  337. }
  338. }
  339. if(!function_exists('architect_mobile_info'))
  340. {
  341. function architect_mobile_info()
  342. {
  343. if(isset($_COOKIE['architectMobileInformation']))
  344. {
  345. $return = array();
  346. foreach(explode('|', $_COOKIE['architectMobileInformation']) as $infoVal)
  347. {
  348. list($key,$val) = explode('=', $infoVal);
  349. $return[$key] = $val;
  350. }
  351. return $return;
  352. } else
  353. {
  354. return false;
  355. }
  356. }
  357. }
  358. if(!function_exists('architectAdminSplit'))
  359. {
  360. function architectAdminSplit($string)
  361. {
  362. return str_replace(
  363. array('http://'),
  364. array(' http://'),
  365. $string
  366. );
  367. }
  368. }
  369. ?>