PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/crypticrod/sr_wp_code
PHP | 1144 lines | 771 code | 118 blank | 255 comment | 137 complexity | 6d40bfd5572d8200d39232989345af64 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. * Wapple Architect WAPL Builder class
  4. *
  5. * This class allows you to build WAPL ensuring that it's always valid and
  6. * always is in line with the schema at http://wapl.wapple.net/wapl.xsd
  7. *
  8. * @author Rich Gubby
  9. * @link http://blog.wapple.net
  10. * @version 1.0
  11. * @package WappleArchitect
  12. */
  13. class waplAdminBuilder
  14. {
  15. /**
  16. * Any universal attributes that apply to all elements
  17. * @access public
  18. * @var array
  19. */
  20. var $universalAttributes = array('class', 'id');
  21. /**
  22. * The current tag we're building
  23. * @access public
  24. * @var string
  25. */
  26. var $tag;
  27. /**
  28. * Tag prefix - used when a "link" becomes an "externalLink"
  29. * @access public
  30. * @var string
  31. */
  32. var $tagPrefix;
  33. /**
  34. * Parent tag
  35. * @access public
  36. * @var string
  37. */
  38. var $parentTag;
  39. /**
  40. * WAPL schema
  41. * @access public
  42. * @var array
  43. * @todo Make sure it's up to date!
  44. */
  45. var $schema = array(
  46. 'head' => array(
  47. 'children' => array('title')
  48. ),
  49. 'title' => array(
  50. 'direct' => true,
  51. 'parent' => 'head'
  52. ),
  53. 'meta' => array(
  54. 'attributes' => array('name', 'content'),
  55. 'parent' => 'head',
  56. 'selfClosing' => true
  57. ),
  58. 'css' => array(
  59. 'children' => array('url'),
  60. 'parent' => 'head'
  61. ),
  62. 'link' => array(
  63. 'children' => array('url', 'label', 'prefix', 'linkVars', 'insertion', 'suffix', 'prefix', 'externalImage')
  64. ),
  65. 'chars' => array(
  66. 'attributes' => array('make_safe'),
  67. 'children' => array('value')
  68. ),
  69. 'linkVars' => array(
  70. 'parent' => 'link',
  71. 'children' => array('var')
  72. ),
  73. 'var' => array(
  74. 'children' => array('name', 'value'),
  75. 'parent' => 'linkVars'
  76. ),
  77. 'externalLink' => array(
  78. 'children' => array('url', 'label', 'prefix', 'suffix', 'externalImage', 'accessKey')
  79. ),
  80. 'externalImage' => array(
  81. 'attributes' => array('filetype', 'scale', 'quality'),
  82. 'children' => array('url', 'transcol', 'safe_mode', 'alt')
  83. ),
  84. 'form' => array(
  85. 'children' => array('action', 'input', 'formItem', 'url')
  86. ),
  87. 'formItem' => array(
  88. 'parent' => 'form',
  89. 'attributes' => array('item_type', 'default'),
  90. 'children' => array('name', 'label', 'value', 'possibility')
  91. ),
  92. 'possibility' => array(
  93. 'parent' => 'formItem',
  94. 'children' => array('label', 'value')
  95. ),
  96. 'rule' => array(
  97. 'attributes' => array('type', 'criteria', 'condition')
  98. ),
  99. 'phonecallChunk' => array(
  100. 'children' => array('phone_number', 'show_label')
  101. ),
  102. 'phonebookChunk' => array(
  103. 'children' => array('phone_number', 'contact_name', 'show_label')
  104. ),
  105. 'rssChunk' => array(
  106. 'children' => array('url', 'max_items', 'items_per_page', 'graphic_scale', 'icon_scale', 'tease_length', 'show_no_tease',
  107. 'do_channel_title', 'do_channel_image', 'do_channel_link', 'do_channel_description', 'do_channel_language',
  108. 'do_channel_copyright', 'do_channel_managing_editor', 'do_channel_webmaster', 'do_channel_publish_date',
  109. 'do_channel_last_build_date', 'do_channel_category', 'do_channel_generator', 'do_channel_documentation',
  110. 'do_channel_cloud', 'do_channel_ttl', 'do_channel_rating', 'do_channel_text_input', 'do_channel_skip_hours',
  111. 'do_channel_skip_days', 'do_item_title', 'do_item_link', 'do_item_description', 'do_item_author',
  112. 'do_item_category', 'do_item_comment', 'do_item_comments', 'do_item_enclosure', 'do_item_guid',
  113. 'do_item_publish_date', 'do_item_source'
  114. )
  115. ),
  116. 'rubberduckChunk' => array(
  117. 'children' => array('rubberduck_client_id', 'rubberduck_set_id', 'graphic_scale', 'delivery_performance',
  118. 'display_mode')
  119. ),
  120. 'jumpChunk' => array(
  121. 'children' => array('destination_page', 'url')
  122. ),
  123. 'waplChunk' => array(
  124. 'children' => array('url')
  125. ),
  126. 'wordsChunk' => array(
  127. 'children' => array('display_as', 'quick_text')
  128. ),
  129. 'spacemakerChunk' => array(
  130. 'children' => array('scale')
  131. ),
  132. 'hrChunk' => array(),
  133. 'admobChunk' => array(
  134. 'children' => array('mobile_site_id', 'iphone_site_id', 'iphone_background_color', 'iphone_text_color')
  135. ),
  136. 'decktradeChunk' => array(
  137. 'children' => array('decktrade_siteid')
  138. ),
  139. 'adsenseChunk' => array(
  140. 'children' => array('channel_id', 'client_id', 'items_per_page', 'col_border', 'col_bg', 'col_link', 'col_text', 'col_url')
  141. ),
  142. 'mbrandChunk' => array(
  143. 'children' => array('mbrand_placement_id', 'mbrand_ad_server')
  144. ),
  145. 'mkhojChunk' => array(
  146. 'children' => array('mkhoj_site_id')
  147. ),
  148. 'mobilecommercemmsChunk' => array(
  149. 'children' => array('client_id', 'items_per_page', 'backfill', 'filter_out_adult', 'use_page_keywords',
  150. 'ad_keyword', 'do_search', 'test_mode'
  151. )
  152. ),
  153. 'mpressionChunk' => array(
  154. 'children' => array('adslot_id', 'ad_category', 'ad_keyword')
  155. ),
  156. 'nokiaadsChunk' => array(
  157. 'children' => array('nokiaads_spot_name', 'nokiaads_ad_server')
  158. ),
  159. 'stampChunk' => array(
  160. 'children' => array('stamp_si', 'stamp_al', 'stamp_mode', 'items_per_page', 'graphic_scale')
  161. ),
  162. 'zestadzChunk' => array(
  163. 'children' => array('zestadz_cid', 'zestadz_meta')
  164. ),
  165. 'settings' => array(
  166. 'children' => array('name', 'default_page_header', 'bangoPageTrackingId', 'iphoneTextSizeAdjust', 'iphoneUserScaleable',
  167. 'iphoneInitalScale', 'iphoneMinScale', 'iphoneMaxScale', 'iphoneMagicWidth')
  168. )
  169. );
  170. /**
  171. * List of tags that don't have rows or cells
  172. * @access public
  173. * @var array
  174. */
  175. var $noRowTags = array(
  176. 'head', 'title', 'meta', 'css', 'linkVars', 'var', 'formItem', 'possibility',
  177. 'rule', 'phonecallChunk', 'phonebookChunk', 'rssChunk', 'rubberduckChunk',
  178. 'jumpChunk', 'waplChunk', 'wordsChunk', 'spacemakerChunk', 'hrChunk', 'admobChunk',
  179. 'decktradeChunk', 'adsenseChunk', 'mbrandChunk', 'mkhojChunk', 'mobilecommercemmsChunk',
  180. 'mpressionChunk', 'nokiaadsChunk', 'stampChunk', 'zestadzChunk', 'settings', 'row', 'cell'
  181. );
  182. /**
  183. * Number of pages in an article
  184. * @access public
  185. * @var array
  186. */
  187. var $pages = array();
  188. /**
  189. * The current page we're on
  190. * @access public
  191. * @var integer
  192. */
  193. var $currentPage;
  194. /**
  195. * Any other tag for php5 only
  196. * @param string $name
  197. * @param array $arguments
  198. * @access public
  199. * @return string
  200. */
  201. function __call($name, $options)
  202. {
  203. $this->tag = $name;
  204. $this->__doExternal($options);
  205. return $this->__do_tag($options[0]);
  206. }
  207. /**
  208. * Build a WAPL head element
  209. * @param array $options
  210. * @access public
  211. * @return string
  212. */
  213. function head($options = array())
  214. {
  215. $this->tag = 'head';
  216. $this->tagPrefix = null;
  217. return $this->__do_tag($options);
  218. }
  219. /**
  220. * Build a META element
  221. * @param array $options
  222. * @access public
  223. * @return string
  224. */
  225. function meta($options = array())
  226. {
  227. $this->tag = 'meta';
  228. $this->tagPrefix = null;
  229. return $this->__do_tag($options);
  230. }
  231. /**
  232. * Build a CSS element
  233. * @param array $options
  234. * @access public
  235. * @return string
  236. */
  237. function css($options = array())
  238. {
  239. $this->tag = 'css';
  240. $this->tagPrefix = null;
  241. return $this->__do_tag($options);
  242. }
  243. /**
  244. * Build a WAPL link element
  245. * @param array $options
  246. * @access public
  247. * @return string
  248. */
  249. function link($options = array())
  250. {
  251. $this->tag = 'link';
  252. $this->__doExternal($options);
  253. return $this->__do_tag($options);
  254. }
  255. /**
  256. * Build a WAPL image element
  257. *
  258. * @param array $options
  259. * @access public
  260. * @return string
  261. */
  262. function image($options = array())
  263. {
  264. $this->tag = 'image';
  265. $this->__doExternal($options);
  266. return $this->__do_tag($options);
  267. }
  268. /**
  269. * Build a WAPL chars element
  270. * @param array $options
  271. * @access public
  272. * @return string
  273. */
  274. function chars($options = array())
  275. {
  276. $this->tag = 'chars';
  277. $this->tagPrefix = null;
  278. return $this->__do_tag($options);
  279. }
  280. /**
  281. * Build a WAPL chunk element
  282. * @param string $type
  283. * @param array $options
  284. * @return string
  285. */
  286. function chunk($type, $options)
  287. {
  288. $this->tag = $type.'Chunk';
  289. $this->tagPrefix = null;
  290. return $this->__do_tag($options);
  291. }
  292. /**
  293. * Build a WAPL form element (includes all form item elements)
  294. * @param array $options
  295. * @access public
  296. * @return string
  297. */
  298. function form($options = array())
  299. {
  300. $this->tag = 'form';
  301. $this->tagPrefix = null;
  302. return $this->__do_tag($options);
  303. }
  304. /**
  305. * Build a WAPL rule
  306. * @param array $options
  307. * @access public
  308. * @return string
  309. */
  310. function rule($options)
  311. {
  312. $this->tag = 'rule';
  313. return $this->__do_tag($options);
  314. }
  315. /**
  316. * Build any page settings
  317. * @param array $options
  318. * @access public
  319. * @return string
  320. */
  321. function settings($options)
  322. {
  323. $this->tag = 'settings';
  324. $this->tagPrefix = null;
  325. return $this->__do_tag($options);
  326. }
  327. /**
  328. * Build a row start tag
  329. * @param array $options
  330. * @access public
  331. * @return string
  332. */
  333. function rowStart($options = array())
  334. {
  335. $this->tag = 'row';
  336. $this->tagPrefix = null;
  337. return $this->__do_tag_start($options);
  338. }
  339. /**
  340. * Build a row end tag
  341. * @param array $options
  342. * @access public
  343. * @return string
  344. */
  345. function rowEnd($options = array())
  346. {
  347. $this->tag = 'row';
  348. $this->tagPrefix = null;
  349. return $this->__do_tag_end($options);
  350. }
  351. /**
  352. * Build a cell start tag
  353. * @param array $options
  354. * @access public
  355. * @return string
  356. */
  357. function cellStart($options = array())
  358. {
  359. $this->tag = 'cell';
  360. return $this->__do_tag_start($options);
  361. }
  362. /**
  363. * Build a cell end tag
  364. * @param array $options
  365. * @access public
  366. * @return string
  367. */
  368. function cellEnd($options = array())
  369. {
  370. $this->tag = 'cell';
  371. return $this->__do_tag_end($options);
  372. }
  373. /**
  374. * Format content into WAPL readable format
  375. * @param string $content
  376. * @param integer $imagescale
  377. * @param integer $imagequality
  378. * @param string $class
  379. * @param integer $length
  380. * @param string $transcol
  381. * @access public
  382. * @return string
  383. */
  384. function format_text($content, $imagescale = 95, $imagequality = 90, $class = '', $length = null, $transcol = '')
  385. {
  386. require_once('simple_html_dom.php');
  387. $html = wpma_str_get_html($content);
  388. $replacements = array();
  389. if(get_option('architect_doform'))
  390. {
  391. $text = '';
  392. // Do a bit of cleanup on select boxes that dont have </option> elements
  393. $cleanup = false;
  394. preg_match_all('/<select(.*?)<\/select>/is', $content, $matches);
  395. $innerContent = $content;
  396. foreach($matches[1] as $key => $val)
  397. {
  398. if(strpos($val, '</option>') === false)
  399. {
  400. $cleanup = true;
  401. $innerContent = str_replace($val, str_replace(array('<br>', '<br />'), '</option>', $val), $innerContent);
  402. }
  403. }
  404. if($cleanup == true)
  405. {
  406. $content = $innerContent;
  407. $html = wpma_str_get_html($content);
  408. }
  409. // Forms - doing my best but so many ways of doing forms!
  410. foreach($html->find('form') as $element)
  411. {
  412. $text = '</quick_text></wordsChunk><row class="'.$class.'"><cell><form>';
  413. $text .= '<action>'.$element->action.'</action>';
  414. // Get input
  415. foreach($element->find('input, textarea, select') as $input)
  416. {
  417. $text .= '<formItem item_type="';
  418. if($input->tag == 'textarea')
  419. {
  420. $text .= 'textarea';
  421. $value = $input->innertext;
  422. } else if($input->tag == 'input')
  423. {
  424. $text .= $input->type;
  425. $value = $input->value;
  426. } else if($input->tag == 'select')
  427. {
  428. $text .= 'select';
  429. }
  430. $text .= '">';
  431. // Get the label
  432. if($input->type != 'hidden' && $input->type != 'submit')
  433. {
  434. if($input->prev_sibling())
  435. {
  436. $sibling = $input->prev_sibling();
  437. if($sibling->tag == 'label')
  438. {
  439. $text .= '<label>'.$sibling->innertext.'</label>';
  440. } else if($siblingBackup = $sibling->prev_sibling())
  441. {
  442. if($siblingBackup->tag == 'label')
  443. {
  444. $text .= '<label>'.$siblingBackup->innertext.'</label>';
  445. }
  446. }
  447. } else
  448. {
  449. $text .= '<label>'.str_replace($input->parent, '', $input->parent->parent).'</label>';
  450. }
  451. }
  452. // Get the name
  453. if(isset($input->name) && $input->name != '')
  454. {
  455. $text .= '<name>'.$input->name.'</name>';
  456. }
  457. // Get the value
  458. if(isset($value) && $value != '')
  459. {
  460. $text .= '<value>'.$value.'</value>';
  461. }
  462. // Get possibilities
  463. if($input->tag == 'select')
  464. {
  465. foreach($input->find('option') as $option)
  466. {
  467. $text .= '<possibility>';
  468. $text .= '<label>'.$option->innertext.'</label>';
  469. $text .= '<value>'.$option->value.'</value>';
  470. $text .= '</possibility>';
  471. }
  472. }
  473. $text .= '</formItem>';
  474. }
  475. $text .= '</form></cell></row><wordsChunk class="'.$class.'"><quick_text>';
  476. if(isset($element))
  477. {
  478. $content = str_ireplace($element->outertext, $text, $content);
  479. }
  480. }
  481. } else
  482. {
  483. foreach($html->find('form') as $element)
  484. {
  485. $replacements[trim($element->outertext())] = '';
  486. }
  487. }
  488. // Replace p tags with a new wordsChunk element
  489. foreach($html->find('p') as $element)
  490. {
  491. $replacements[trim($element->outertext())] = '</quick_text></wordsChunk><wordsChunk class="'.$class.'"><quick_text>'.$element->innertext.'</quick_text></wordsChunk><wordsChunk class="'.$class.'"><quick_text>';
  492. }
  493. // Sort out captions first
  494. foreach($html->find('caption') as $element)
  495. {
  496. $text = '</quick_text></wordsChunk>';
  497. // find image in element
  498. foreach($element->find('img') as $img)
  499. {
  500. $transcoltext = '';
  501. if($transcol != '')
  502. {
  503. $transcoltext = '<transcol>'.$transcol.'</transcol>';
  504. }
  505. $text .= '<row><cell class="postCaptionImg"><externalImage filetype="'.strtolower(substr($img->src,-3)).'" scale="'.$imagescale.'" quality="'.$imagequality.'"><url>'.htmlspecialchars($img->src).'</url>'.$transcoltext.'</externalImage></cell></row>';
  506. }
  507. if($element->caption && $element->caption != '')
  508. {
  509. $text .= '<wordsChunk class="'.$class.' postCaption"><quick_text>'.$element->caption.'</quick_text></wordsChunk>';
  510. }
  511. $text .= '<wordsChunk class="'.$class.'"><quick_text>';
  512. $content = str_ireplace($element->outertext, $text, $content);
  513. }
  514. // Replace images with WAPL versions
  515. foreach($html->find('img') as $element)
  516. {
  517. $alt = '';
  518. if(isset($element->alt) && $element->alt != '')
  519. {
  520. $alt = $element->alt;
  521. } else if(isset($element->title) && $element->title != '')
  522. {
  523. $alt = $element->title;
  524. }
  525. // don't scale smiley images
  526. $noscaleimageclasses = array('wp-smiley', 'sociable-hovers');
  527. if(in_array($element->class, $noscaleimageclasses))
  528. {
  529. $thisimagescale = 0;
  530. } else
  531. {
  532. $thisimagescale = $imagescale;
  533. }
  534. if($element->parent->class == 'share' || $element->parent->parent->class == 'share' || $element->parent->parent->parent->class == 'share')
  535. {
  536. $thisimagescale = 0;
  537. }
  538. $transcoltext = '';
  539. if($transcol != '')
  540. {
  541. $transcoltext = '<transcol>'.$transcol.'</transcol>';
  542. }
  543. $innerText = '<externalImage filetype="'.strtolower(substr($element->src,-3)).'" scale="'.$thisimagescale.'" quality="'.$imagequality.'"><url>'.htmlspecialchars($element->src).'</url>'.$transcoltext.'<alt>"'.$alt.'"</alt></externalImage>';
  544. if($element->parent->tag == 'a')
  545. {
  546. $innerText = '<externalLink><url>'.htmlspecialchars($element->parent->href).'</url>'.$innerText.'</externalLink>';
  547. }
  548. $text = '</quick_text></wordsChunk><row><cell>'.$innerText.'</cell></row><wordsChunk class="'.$class.'"><quick_text>';
  549. $replacements[trim($element->outertext())] = $text;
  550. }
  551. // Any other combos
  552. foreach($html->find('span') as $element)
  553. {
  554. if($element->style == 'font-weight: bold;')
  555. {
  556. $replacements[trim($element->outertext())] = '[b]'.$element->innertext.'[/b]';
  557. } else if($element->style == 'font-style: italic;')
  558. {
  559. $replacements[trim($element->outertext())] = '[i]'.$element->innertext.'[/i]';
  560. } else if($element->style == 'text-decoration: underline;')
  561. {
  562. $replacements[trim($element->outertext())] = '[u]'.$element->innertext.'[/u]';
  563. } else if($element->style == 'font-weight: bold; font-style: italic;')
  564. {
  565. $replacements[trim($element->outertext())] = '[i][b]'.$element->innertext.'[/b][/i]';
  566. } else if($element->style == 'text-decoration: line-through;')
  567. {
  568. $replacements[trim($element->outertext())] = '[s]'.$element->innertext.'[/s]';
  569. }
  570. }
  571. // Sometimes WP puts strong/em tags in different orders...
  572. $content = str_replace(array('<strong><em>', '</em></strong>'), array('[b][i]', '[/i][/b]'), $content);
  573. // Replace any em tags with WTF [i] tags
  574. foreach($html->find('em') as $element)
  575. {
  576. $replacements[trim($element->outertext())] = '[i]'.$element->innertext.'[/i]';
  577. }
  578. // Replace any strong tags with WTF [b] tags
  579. foreach($html->find('strong') as $element)
  580. {
  581. $replacements[trim($element->outertext())] = '[b]'.$element->innertext.'[/b]';
  582. }
  583. // H* tags
  584. foreach($html->find('h1,h2,h3,h4,h5,h6') as $element)
  585. {
  586. $replacements[trim($element->outertext())] = '</quick_text></wordsChunk><wordsChunk class="'.$class.'"><display_as>'.$element->tag.'</display_as><quick_text>'.$element->innertext.'</quick_text></wordsChunk><wordsChunk class="'.$class.'"><quick_text>';
  587. }
  588. // Lists
  589. foreach($html->find('ul, ol') as $element)
  590. {
  591. $text = '</quick_text></wordsChunk><wordsChunk class="'.$class.' list"><display_as>'.$element->tag.'</display_as><quick_text>';
  592. foreach($element->children as $child)
  593. {
  594. if($child->tag == 'li')
  595. {
  596. $text .= $child->innertext.'
  597. ';
  598. }
  599. }
  600. $text .= '</quick_text></wordsChunk><wordsChunk class="'.$class.'"><quick_text>';
  601. $replacements[trim($element->outertext())] = $text;
  602. }
  603. // Links
  604. foreach($html->find('a') as $element)
  605. {
  606. // handle elements that just have newlines in them
  607. if(trim($element->innertext) == '<br />')
  608. {
  609. continue;
  610. }
  611. $elementparent = $element->parent();
  612. if(isset($element->href) && $element->href != '' && $elementparent->tag != 'caption')
  613. {
  614. $replacements[trim($element->outertext())] = '[url='.htmlspecialchars($element->href).']'.$element->innertext.'[/url]';
  615. }
  616. }
  617. // Do the conversion
  618. foreach($replacements as $key => $val)
  619. {
  620. $content = str_ireplace(trim($key), $val, $content);
  621. }
  622. // Replace any rogue wordsChunk chars
  623. $content = preg_replace('/<\/wordsChunk><quick_text>(.*?)<\/quick_text><\/wordsChunk>/i', '', $content);
  624. $content = preg_replace('/<\/wordsChunk><quick_text>(.*?)<\/quick_text><\/wordsChunk>/i', '', $content);
  625. $content = preg_replace('/<wordsChunk><quick_text>([\s\t])+<\/quick_text><\/wordsChunk>/i', '', $content);
  626. $content = str_replace('</wordsChunk><quick_text>', '', $content);
  627. $content = str_replace('</externalLink></quick_text></wordsChunk>', '', $content);
  628. // Lastly strip any other tags
  629. $stripTagExclude = array();
  630. foreach($this->schema as $key => $val)
  631. {
  632. $stripTagExclude[$key] = $key;
  633. if(isset($val['children']))
  634. {
  635. foreach($val['children'] as $child)
  636. {
  637. $stripTagExclude[$child] = $child;
  638. }
  639. }
  640. }
  641. $stripTagExcludeString = '';
  642. foreach($stripTagExclude as $tag)
  643. {
  644. $stripTagExcludeString .= '<'.$tag.'>';
  645. }
  646. $stripTagExcludeString.= '<row><cell>';
  647. // Strip any remaining html
  648. $content = strip_tags($content, $stripTagExcludeString);
  649. // Convert new lines to BR
  650. $content = nl2br($content);
  651. // Remove all tabs and spaces to a newline
  652. $content = preg_replace('/\s\s+/', "\n", $content);
  653. // Fix some dodgy WP code
  654. $content = str_replace('<<br />', '<br />', $content);
  655. // Remove multiple BR and make a new wordschunk
  656. $content = preg_replace('/(<br \/>\s){2,}/', '[span=textSpacer] [/span]', $content);
  657. // Clean up any empty wordsChunk elements
  658. $empty = wpma_str_get_html($content);
  659. foreach($empty->find('wordsChunk') as $quick)
  660. {
  661. if($quick->innertext == '<quick_text></quick_text>')
  662. {
  663. $content = str_ireplace($quick->outertext, '', $content);
  664. }
  665. }
  666. // Replace all BR with newline (all done to make it nice and tidy)
  667. $content = preg_replace('/(<br\s*\/?>\s*){1,}/', "\n", $content);
  668. // Clean up "&nbsp;"
  669. $content = str_replace('&nbsp;', '', $content);
  670. $content = str_replace('<</quick_text>', '</quick_text>', $content);
  671. if($length)
  672. {
  673. $this->getPages($content, $length);
  674. $tidyContent = $this->pages[($this->currentPage - 1)];
  675. if((strpos($tidyContent, '<row>') === 0) OR (strpos($tidyContent, '<wordsChunk') === 0))
  676. {
  677. $tidyContent = '</quick_text></wordsChunk>'.$tidyContent;
  678. }
  679. if(strrpos($tidyContent, '</quick_text></wordsChunk>') === (strlen($tidyContent) - strlen('</quick_text></wordsChunk>')))
  680. {
  681. $tidyContent = substr($tidyContent, 0, strrpos($tidyContent, '</quick_text></wordsChunk'));
  682. }
  683. // Tidy up any BB code undone by splitting
  684. $bbhtml = wpma_str_get_html($tidyContent);
  685. $bbcode = array('i', 'b', 'u', 's');
  686. foreach($bbhtml->find('quick_text ') as $element)
  687. {
  688. foreach($bbcode as $code)
  689. {
  690. $bbreplacements = array();
  691. if(((strpos($element->innertext, '['.$code.']') !== false) && (strpos($element->innertext, '[/'.$code.']') === false)))
  692. {
  693. $bbreplacements[trim($element->innertext)] = $element->innertext.'[/'.$code.']';
  694. } else if(((strpos($element->innertext, '['.$code.']') === false) && (strpos($element->innertext, '[/'.$code.']') !== false)))
  695. {
  696. $bbreplacements[trim($element->innertext)] = '['.$code.']'.$element->innertext;
  697. }
  698. foreach($bbreplacements as $key => $val)
  699. {
  700. $tidyContent = str_ireplace(trim($key), $val, $tidyContent);
  701. }
  702. }
  703. }
  704. return $tidyContent;
  705. } else
  706. {
  707. $this->pages = array('page');
  708. $this->currentPage = 1;
  709. return $content;
  710. }
  711. }
  712. /**
  713. * Get the number of pages
  714. * @param string $content
  715. * @access public
  716. * @return unknown_type
  717. */
  718. function getPages($content, $length)
  719. {
  720. $chunks = preg_split('/<\/quick_text><\/wordsChunk>/', $content, -1);
  721. $r = 0;
  722. $tmpPages = array();
  723. for($i = 0; $i < count($chunks); $i++)
  724. {
  725. if(!isset($tmpPages[$r])){ $tmpPages[$r] = '';}
  726. if (strlen($tmpPages[$r] . $chunks[$i].' ') < $length)
  727. {
  728. $tmpPages[$r] .= $chunks[$i] . '</quick_text></wordsChunk>';
  729. } else
  730. {
  731. $r++;
  732. if(!isset($tmpPages[$r])){ $tmpPages[$r] = '';}
  733. $tmpPages[$r] .= $chunks[$i] . '</quick_text></wordsChunk>';
  734. }
  735. }
  736. // Order pages correctly
  737. foreach($tmpPages as $val)
  738. {
  739. if(!empty($val) && $val != '')
  740. {
  741. $this->pages[] = $val;
  742. }
  743. }
  744. // which page are we on?
  745. if(isset($_GET['page']))
  746. {
  747. $this->currentPage = $_GET['page'];
  748. } else
  749. {
  750. $this->currentPage = 1;
  751. }
  752. }
  753. /**
  754. * Main function to build a tag
  755. * @param array $options
  756. * @access public
  757. * @return string
  758. */
  759. function __do_tag($options)
  760. {
  761. if(in_array($this->__getTag(), $this->noRowTags))
  762. {
  763. $options['row'] = false;
  764. $options['cell'] = false;
  765. }
  766. $string = '';
  767. $string .= $this->__do_tag_start($options);
  768. $string .= $this->__do_tag_contents($options);
  769. $string .= $this->__do_tag_end($options);
  770. return $string;
  771. }
  772. /**
  773. * Start an element off by building a row and cell if needed and setting up a tag prefix
  774. * @param array $options
  775. * @access public
  776. * @return string
  777. */
  778. function __do_tag_start($options)
  779. {
  780. if(isset($options['start']) AND $options['start'] == false)
  781. {
  782. return;
  783. }
  784. $string = null;
  785. if(!isset($options['row']) OR $options['row'] !== false)
  786. {
  787. $string .= $this->__do_tag_row_start($options);
  788. }
  789. if(!isset($options['cell']) OR $options['cell'] !== false)
  790. {
  791. $string .= $this->__do_tag_cell_start($options);
  792. }
  793. $string .= '<'.$this->tagPrefix;
  794. if($this->tagPrefix)
  795. {
  796. $tag = $this->tagPrefix.ucwords($this->tag);
  797. $string .= ucwords($this->tag);
  798. } else
  799. {
  800. $tag = $this->tag;
  801. $string .= $this->tag;
  802. }
  803. foreach($this->universalAttributes as $val)
  804. {
  805. if(isset($options[$val]))
  806. {
  807. $string .= ' '.$val.'="'.$options[$val].'"';
  808. }
  809. }
  810. if(isset($this->schema[$tag]['attributes']))
  811. {
  812. foreach($this->schema[$tag]['attributes'] as $val)
  813. {
  814. if(isset($options[trim($val)]))
  815. {
  816. $string .= ' '.trim($val).'="'.$options[trim($val)].'"';
  817. }
  818. }
  819. }
  820. if(!isset($this->schema[$tag]['selfClosing']) OR $this->schema[$tag]['selfClosing'] == false)
  821. {
  822. $string .= '>';
  823. }
  824. return $string;
  825. }
  826. /**
  827. * Build the internal contents of an element
  828. * @param array $options
  829. * @access public
  830. * @return string
  831. */
  832. function __do_tag_contents($options)
  833. {
  834. if(isset($options['contents']) AND $options['contents'] == false)
  835. {
  836. return;
  837. }
  838. $string = null;
  839. $tag = $this->__getTag();
  840. if(isset($this->schema[$tag]['children']) AND !empty($this->schema[$tag]['children']))
  841. {
  842. foreach($this->schema[$tag]['children'] as $val)
  843. {
  844. if((isset($this->schema[$val]['parent']) AND ($this->schema[$val]['parent'] == $this->__getTag())) OR !isset($this->schema[$val]['parent']))
  845. {
  846. if(isset($options[$val]))
  847. {
  848. $string .= '<'.$val.'>'.$options[$val].'</'.$val.'>';
  849. }
  850. }
  851. }
  852. }
  853. if(isset($options['children']) AND !empty($options['children']) AND is_array($options['children']))
  854. {
  855. $childString = null;
  856. $this->parentTag = $this->tag;
  857. $tmpTagPrefix = $this->tagPrefix;
  858. foreach($options['children'] as $child)
  859. {
  860. if(isset($child['tag']))
  861. {
  862. $this->tag = $child['tag'];
  863. $options = array();
  864. // Build up any attributes
  865. if(isset($this->schema[$this->tag]['attributes']))
  866. {
  867. foreach($this->schema[$this->tag]['attributes'] as $attVal)
  868. {
  869. if(isset($child[$attVal]))
  870. {
  871. $options[$attVal] = $child[$attVal];
  872. }
  873. }
  874. }
  875. if(isset($child['options']))
  876. {
  877. $options = array_merge($options, $child['options']);
  878. }
  879. if(method_exists($this, $this->tag))
  880. {
  881. $childString .= $this->{$this->tag}($options);
  882. } else
  883. {
  884. $childString .= $this->__do_tag($options);
  885. }
  886. }
  887. }
  888. $this->tagPrefix = $tmpTagPrefix;
  889. $this->tag = $this->parentTag;
  890. $string .= $childString;
  891. }
  892. // Set a direct value into a tag
  893. if(isset($this->schema[$tag]['direct']) AND $this->schema[$tag]['direct'] == true)
  894. {
  895. $string .= $options['value'];
  896. }
  897. return $string;
  898. }
  899. /**
  900. * Close elements properly
  901. * @param array $options
  902. * @access public
  903. * @return string
  904. */
  905. function __do_tag_end($options)
  906. {
  907. if(isset($options['end']) AND $options['end'] == false)
  908. {
  909. return;
  910. }
  911. $string = '';
  912. if(isset($this->schema[$this->tag]['selfClosing']) AND $this->schema[$this->tag]['selfClosing'] == true)
  913. {
  914. $string .= ' />';
  915. } else
  916. {
  917. $string .= '</'.$this->tagPrefix;
  918. if($this->tagPrefix)
  919. {
  920. $string .= ucwords($this->tag);
  921. } else
  922. {
  923. $string .= $this->tag;
  924. }
  925. $string .= '>';
  926. if(!isset($options['cell']) OR $options['cell'] !== false)
  927. {
  928. $string .= $this->__do_tag_cell_end($options);
  929. }
  930. if(!isset($options['row']) OR $options['row'] !== false)
  931. {
  932. $string .= $this->__do_tag_row_end($options);
  933. }
  934. }
  935. return $string;
  936. }
  937. /**
  938. * Open an elements row
  939. * @param array $options
  940. * @access public
  941. * @return string
  942. */
  943. function __do_tag_row_start($options)
  944. {
  945. $string = '<row';
  946. if(isset($options['rowClass']))
  947. {
  948. $string .= ' class="'.$options['rowClass'].'"';
  949. }
  950. if(isset($options['rowId']))
  951. {
  952. $string .= ' id="'.$options['rowId'].'"';
  953. }
  954. $string .= '>';
  955. return $string;
  956. }
  957. /**
  958. * Close a row
  959. * @access public
  960. * @return string
  961. */
  962. function __do_tag_row_end()
  963. {
  964. return '</row>';
  965. }
  966. /**
  967. * Open an elements cell
  968. * @param array $options
  969. * @access public
  970. * @return string
  971. */
  972. function __do_tag_cell_start($options)
  973. {
  974. $string = '<cell';
  975. if(isset($options['cellClass']))
  976. {
  977. $string .= ' class="'.$options['cellClass'].'"';
  978. }
  979. if(isset($options['cellId']))
  980. {
  981. $string .= ' id="'.$options['cellId'].'"';
  982. }
  983. $string .= '>';
  984. return $string;
  985. }
  986. /**
  987. * Close a cell
  988. * @param array $options
  989. * @access public
  990. * @return string
  991. */
  992. function __do_tag_cell_end($options)
  993. {
  994. return '</cell>';
  995. }
  996. /**
  997. * Get proper tag
  998. * @access public
  999. * @return string
  1000. */
  1001. function __getTag()
  1002. {
  1003. if($this->tagPrefix)
  1004. {
  1005. return $this->tagPrefix.ucwords($this->tag);
  1006. } else
  1007. {
  1008. return $this->tag;
  1009. }
  1010. }
  1011. /**
  1012. * Work out external link
  1013. * @param array $options
  1014. * @access public
  1015. * @return void
  1016. */
  1017. function __doExternal($options)
  1018. {
  1019. $this->tagPrefix = null;
  1020. if(!isset($options['external']) || $options['external'] == true)
  1021. {
  1022. $this->tagPrefix = 'external';
  1023. }
  1024. }
  1025. }
  1026. ?>