2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4 * SugarCRM Community Edition is a customer relationship management program developed by
5 * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
7 * This program is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU Affero General Public License version 3 as published by the
9 * Free Software Foundation with the addition of the following permission added
10 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
19 * You should have received a copy of the GNU Affero General Public License along with
20 * this program; if not, see http://www.gnu.org/licenses or write to the Free
21 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
27 * The interactive user interfaces in modified source and object code versions
28 * of this program must display Appropriate Legal Notices, as required under
29 * Section 5 of the GNU Affero General Public License version 3.
31 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32 * these Appropriate Legal Notices must retain the display of the "Powered by
33 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34 * technical reasons, the Appropriate Legal Notices must display the words
35 * "Powered by SugarCRM".
36 ********************************************************************************/
42 * This is a utility base file to parse HTML
53 function MetaParser() {
58 return "NOT AVAILABLE";
63 * Parses for contents enclosed within <form>...</form> tags
65 function getFormContents($contents, $all = true) {
67 preg_match_all("'(<form[^>]*?>)(.*?)(</form[^>]*?>)'si", $contents, $matches);
71 preg_match("'(<form[^>]*?>)(.*?)(</form[^>]*?>)'si", $contents, $matches);
72 return $this->convertToTagElement($matches);
79 * Parses for input, select, textarea types from string content
80 * @param $contents The String contents to parse
81 * @return $matches Array of matches of PREG_SET_ORDER
83 function getFormElements($contents) {
84 preg_match_all("'(<[ ]*?)(textarea|input|select)([^>]*?)(>)'si", $contents, $matches, PREG_PATTERN_ORDER);
86 foreach($matches[3] as $match) {
94 * getFormElementsNames
95 * Parses for the name values of input, select, textarea types from string content
96 * @param $contents The String contents to parse
97 * @return $matches Array of name/value pairs
99 function getFormElementsNames($contents) {
100 preg_match_all("'(<[ ]*?)(textarea|input|select)[^>]*?name=[\'\"]([^\'\"]*?)(\[\])?(_basic)?[\'\"]([^>]*?>)'si", $contents, $matches, PREG_PATTERN_ORDER);
101 return !empty($matches[3]) ? $matches[3] : null;
107 * Returns the name/value of a tag attribute where name is set to $name
108 * @param $name The name of the attribute
109 * @param $contents The contents to parse
110 * @param $filter Option regular expression to filter value
111 * @return Array of name/value for matching attribute
113 function getTagAttribute($name, $contents, $filter = '') {
114 //$exp = "'".$name."[ ]*?=[ ]*?[\'\"]([a-zA-Z0-9\_\[\]]*)[\'\"]'si";
116 $exp = "'".$name."[\s]*?=[\s]*?[\'\"]([^\'^\"]*?)[\'\"]'si";
117 preg_match_all($exp, $contents, $matches, PREG_SET_ORDER);
119 return !empty($matches[0][1]) ? $matches[0][1] : '';
123 foreach($matches as $tag) {
124 if(preg_match($filter, $tag[1])) {
133 * Returns an Array of the tables found in the file. If $tableClass parameter
134 * is supplied, it'll return only those tables that have a matching class attribute
135 * equal to $tableClass
136 * @param $tableClass Optional table class parameter value
137 * @return Array of table elements found
139 function getTables($tableClass = null, $contents) {
140 preg_match_all("'(<table[^>]*?>)(.*?)(</table[^>]*?>)'si", $contents, $matches, PREG_SET_ORDER);
141 if($tableClass == null) {
146 foreach($matches as $key => $table) {
147 if(strpos($table[1], $tableClass) > 0) {
151 return $this->convertToTagElement($tables);
157 * Returns an Array of all elements matching type. It will match
158 * for the outermost tags. For example given contents:
159 * "<tr><td>Text <table><tr><td>a</td></tr></table></td></tr>"
160 * and method call getElementsByType("<td>", $contents) returns
161 * "<td>Text <table><tr><td>a</td></tr></table></td>"
163 * @param $type The type of element to parse out and return
164 * @return a tag element format Array
166 function getElementsByType($type, $contents) {
167 $x = strlen($contents);
170 $stag1 = "<" . trim($type, " <>") . '>';
171 $stag2 = "<" . trim($type, " <>") . ' ';
172 $etag = "</".$type.">";
173 $sincrement = strlen($stag1);
174 $eincrement = strlen($etag);
179 $stok = substr($contents, $count, $sincrement);
180 $etok = substr($contents, $count, $eincrement);
181 if($stok == $stag1 || $stok == $stag2) {
183 if(count($sarr) == 0) {
188 } else if($etok == $etag) {
190 if(count($sarr) == 0) {
191 $val = substr($contents, $mark, ($count - $mark) + $eincrement);
209 function getElementValue($type, $contents, $filter = "(.*?)") {
210 $exp = "'<".$type."[^>]*?>".$filter."</".$type."[^>]*?>'si";
211 preg_match($exp, $contents, $matches);
212 return isset($matches[1]) ? $matches[1] : '';
216 function stripComments($contents) {
217 return preg_replace("'(<!--.*?-->)'si", "", $contents);
222 * This method accepts the file contents and uses the $GLOBALS['sugar_flavor'] value
223 * to remove the flavor tags in the file contents if present. If $GLOBALS['sugar_flavor']
224 * is not set, it defaults to PRO flavor
225 * @param $contents The file contents as a String value
226 * @param $result The file contents with non-matching flavor tags and their nested comments removed
228 function stripFlavorTags($contents) {
229 $flavor = isset($GLOBALS['sugar_flavor']) ? $GLOBALS['sugar_flavor'] : 'PRO';
230 $isPro = ($flavor == 'ENT' || $flavor == 'PRO') ? true : false;
232 $contents = preg_replace('/<!-- BEGIN: open_source -->.*?<!-- END: open_source -->/', '', $contents);
234 $contents = preg_replace('/<!-- BEGIN: pro -->.*?<!-- END: pro -->/', '', $contents);
241 * Returns the highest number of <td>...</td> blocks within a <tr>...</tr> block.
242 * @param $contents The table contents to parse
243 * @param $filter Optional filter to parse for an attribute within the td block.
244 * @return The maximum column count
246 function getMaxColumns($contents, $filter) {
247 preg_match_all("'(<tr[^>]*?>)(.*?)(</tr[^>]*?>)'si", $contents, $matches, PREG_SET_ORDER);
249 foreach($matches as $tableRows) {
250 $count = substr_count($tableRows[2], $filter);
259 function convertToTagElement($matches) {
263 foreach($matches as $data) {
264 // We need 4 because the 1,2,3 indexes make up start,body,end
265 if(count($data) == 4) {
267 $element['start'] = $data[1];
268 $element['body'] = $data[2];
269 $element['end'] = $data[3];
270 $elements[] = $element;
274 return empty($elements) ? $matches : $elements;
279 * This function removes the \r (return), \n (newline) and \t (tab) markup from string
281 function trimHTML($contents) {
282 $contents = str_replace(array("\r"), array(""), $contents);
283 $contents = str_replace(array("\n"), array(""), $contents);
284 $contents = str_replace(array("\t"), array(""), $contents);
292 * This method parses the given $contents String and grabs all <script...>...</script> blocks.
293 * The method also converts values enclosed within "{...}" blocks that may need to be converted
296 * @param $contents The HTML String contents to parse
298 * @return $javascript The formatted script blocks or null if none found
300 function getJavascript($contents, $addLiterals = true) {
304 //Check if there are Javascript blocks of code to process
305 preg_match_all("'(<script[^>]*?>)(.*?)(</script[^>]*?>)'si", $contents, $matches, PREG_PATTERN_ORDER);
306 if(empty($matches)) {
310 foreach($matches[0] as $scriptBlock) {
311 $javascript .= "\n" . $scriptBlock;
314 $javascript = substr($javascript, 1);
317 //1) Calendar.setup {..} blocks
318 $javascript = preg_replace('/Calendar.setup[\s]*[\(][^\)]*?[\)][\s]*;/si', '', $javascript);
320 //Find all blocks that may need to be replaced with Smarty syntax
321 preg_match_all("'([\{])([a-zA-Z0-9_]*?)([\}])'si", $javascript, $matches, PREG_PATTERN_ORDER);
322 if(!empty($matches)) {
325 foreach($matches[0] as $xTemplateCode) {
326 if(!isset($replace[$xTemplateCode])) {
327 $replace[$xTemplateCode] = str_replace("{", "{\$", $xTemplateCode);
331 $javascript = str_replace(array_keys($replace), array_values($replace), $javascript);
338 return $this->parseDelimiters($javascript);
342 function parseDelimiters($javascript) {
344 $scriptLength = strlen($javascript);
346 $inSmartyVariable = false;
348 while($count < $scriptLength) {
350 if($inSmartyVariable) {
353 while(isset($javascript[$count]) && $javascript[$count] != '}') {
358 $newJavascript .= substr($javascript, $start, $numOfChars);
359 $inSmartyVariable = false;
363 $char = $javascript[$count];
364 $nextChar = ($count + 1 >= $scriptLength) ? '' : $javascript[$count + 1];
366 if($char == "{" && $nextChar == "$") {
367 $inSmartyVariable = true;
368 $newJavascript .= $javascript[$count];
369 } else if($char == "{") {
370 $newJavascript .= " {ldelim} ";
371 } else if($char == "}") {
372 $newJavascript .= " {rdelim} ";
374 $newJavascript .= $javascript[$count];
380 return $newJavascript;
384 * findAssignedVariableName
385 * This method provides additional support in attempting to parse the module's corresponding
386 * PHP file for either the EditView or DetailView. In the event that the subclasses cannot
387 * find a matching vardefs.php entry in the HTML file, this method can be called to parse the
388 * PHP file to see if the assignment was made using the bean's variable. If so, we return
389 * this variable name.
391 * @param $name The tag name found in the HTML file for which we want to search
392 * @param $filePath The full file path for the HTML file
393 * @return The variable name found in PHP file, original $name variable if not found
395 function findAssignedVariableName($name, $filePath) {
397 if($this->mPHPFile == "INVALID") {
401 if(!isset($this->mPHPFile)) {
402 if(preg_match('/(.*?)(DetailView).html$/', $filePath, $matches)) {
404 } else if(preg_match('/(.*?)(EditView).html$/', $filePath, $matches)) {
408 if(!isset($dir) || !is_dir($dir)) {
409 $this->mPHPFile = "INVALID";
413 $filesInDir = $this->dirList($dir);
414 $phpFile = $matches[2].'.*?[\.]php';
415 foreach($filesInDir as $file) {
416 if(preg_match("/$phpFile/", $file)) {
417 $this->mPHPFile = $matches[1] . $file;
422 if(!isset($this->mPHPFile) || !file_exists($this->mPHPFile)) {
423 $this->mPHPFile = "INVALID";
428 $phpContents = file_get_contents($this->mPHPFile);
429 $uname = strtoupper($name);
430 if(preg_match("/xtpl->assign[\(][\"\']".$uname."[\"\'][\s]*?,[\s]*?[\$]focus->(.*?)[\)]/si", $phpContents, $matches)) {
439 * Utility method to list all the files in a given directory.
441 * @param $directory The directory to scan
442 * @return $results The files in the directory that were found
444 function dirList ($directory) {
446 // create an array to hold directory list
449 // create a handler for the directory
450 $handler = opendir($directory);
452 // keep going until all files in directory have been read
453 while ($file = readdir($handler)) {
454 // if $file isn't this directory or its parent,
455 // add it to the results array
456 if ($file != '.' && $file != '..')
460 // tidy up: close the handler
468 * This method checks the mixed variable $elementNames to see if it is a custom field. A custom
469 * field is simply defined as a field that ends with "_c". If $elementNames is an Array
470 * any matching custom field value will result in a true evaluation
471 * @param $elementNames Array or String value of form element name(s).
472 * @return String name of custom field; null if none found
474 function getCustomField($elementNames) {
476 if(!isset($elementNames) || (!is_string($elementNames) && !is_array($elementNames))) {
480 if(is_string($elementNames)) {
481 if(preg_match('/(.+_c)(_basic)?(\[\])?$/', $elementNames, $matches)) {
482 return count($matches) == 1 ? $matches[0] : $matches[1];
487 foreach($elementNames as $name) {
488 if(preg_match('/(.+_c)(_basic)?(\[\])?$/', $name, $matches)) {
489 return count($matches) == 1 ? $matches[0] : $matches[1];
496 function applyPreRules($moduleDir, $panels) {
497 if(file_exists("include/SugarFields/Parsers/Rules/".$moduleDir."ParseRule.php")) {
498 require_once("include/SugarFields/Parsers/Rules/".$moduleDir."ParseRule.php");
499 $class = $moduleDir."ParseRule";
500 $parseRule = new $class();
501 $panels = $parseRule->preParse($panels, $this->mView);
506 function applyRules($moduleDir, $panels) {
507 return $this->applyPostRules($moduleDir, $panels);
510 function applyPostRules($moduleDir, $panels) {
511 //Run module specific rules
512 if(file_exists("include/SugarFields/Parsers/Rules/".$moduleDir."ParseRule.php")) {
513 require_once("include/SugarFields/Parsers/Rules/".$moduleDir."ParseRule.php");
514 $class = $moduleDir."ParseRule";
515 $parseRule = new $class();
516 $panels = $parseRule->parsePanels($panels, $this->mView);
519 //Now run defined rules
520 require_once("include/SugarFields/Parsers/Rules/ParseRules.php");
521 $rules = ParseRules::getRules();
523 foreach($rules as $rule) {
524 if(!file_exists($rule['file'])) {
525 $GLOBALS['log']->error("Cannot run rule for " . $rule['file']);
528 require_once($rule['file']);
529 $runRule = new $rule['class'];
530 $panels = $runRule->parsePanels($panels, $this->mView);
537 function createFileContents($moduleDir, $panels, $templateMeta=array(), $htmlFilePath) {
539 $header = "<?php\n\n";
541 if(empty($templateMeta)) {
542 $header .= "\$viewdefs['$moduleDir']['$this->mView'] = array(
543 'templateMeta' => array('maxColumns' => '2',
545 array('label' => '10', 'field' => '30'),
546 array('label' => '10', 'field' => '30')
550 $header .= "\$viewdefs['$moduleDir']['$this->mView'] = array(
551 'templateMeta' =>" . var_export($templateMeta, true) . ",";
554 //Replace all the @sq (single quote tags that may have been inserted)
555 $header = preg_replace('/\@sq/', "'", $header);
558 $contents = file_get_contents($htmlFilePath);
560 $javascript = $this->getJavascript($contents, true);
562 if(!empty($javascript)) {
563 $javascript = str_replace("'", "\\'", $javascript);
564 $header .= "\n 'javascript' => '" . $javascript . "',\n";
567 $header .= "\n 'panels' =>";
575 $body = var_export($panels, true);
576 $metadata = $header . $body . $footer;
577 $metadata = preg_replace('/(\d+)[\s]=>[\s]?/',"",$metadata);
585 * This function merges the $panels Array against the $masterCopy's meta data definition
586 * @param $panels meta data Array to merge
587 * @param $moduleDir Directory name of the module
588 * @param $masterCopy file path to the meta data master copy
589 * @return Array of merged $panel definition
591 function mergePanels($panels, $vardefs, $moduleDir, $masterCopy) {
592 require($masterCopy);
593 $masterpanels = $viewdefs[$moduleDir][$this->mView]['panels'];
594 $hasMultiplePanels = $this->hasMultiplePanels($masterpanels);
596 if(!$hasMultiplePanels) {
597 $keys = array_keys($viewdefs[$moduleDir][$this->mView]['panels']);
598 if(!empty($keys) && count($keys) == 1) {
599 if(strtolower($keys[0]) == 'default') {
600 $masterpanels = array('default'=>$viewdefs[$moduleDir][$this->mView]['panels'][$keys[0]]);
602 $firstPanel = array_values($viewdefs[$moduleDir][$this->mView]['panels']);
603 $masterpanels = array('default'=> $firstPanel[0]);
606 $masterpanels = array('default'=>$viewdefs[$moduleDir][$this->mView]['panels']);
609 foreach($masterpanels as $name=>$masterpanel) {
610 if(isset($panels[$name])) {
611 // Get all the names in the panel
612 $existingElements = array();
613 $existingLocation = array();
615 foreach($panels[$name] as $rowKey=>$row) {
616 foreach($row as $colKey=>$column) {
617 if(is_array($column) && !empty($column['name'])) {
618 $existingElements[$column['name']] = $column['name'];
619 $existingLocation[$column['name']] = array("panel"=>$name, "row"=>$rowKey, "col"=>$colKey);
620 } else if(!is_array($column) && !empty($column)) {
621 $existingElements[$column] = $column;
622 $existingLocation[$column] = array("panel"=>$name, "row"=>$rowKey, "col"=>$colKey);
627 // Now check against the $masterCopy
628 foreach($masterpanel as $rowKey=>$row) {
632 foreach($row as $colKey=>$column) {
633 if(is_array($column) && isset($column['name'])) {
634 $id = $column['name'];
635 } else if(!is_array($column) && !empty($column)) {
640 if(empty($existingElements[$id])) {
642 // 1) if it is a required field (as defined in metadata)
643 // 2) or if it has a customLabel and customCode (a very deep customization)
644 if((is_array($column) && !empty($column['displayParams']['required'])) ||
645 (is_array($column) && !empty($column['customCode']) && !empty($column['customLabel']))) {
649 //Use definition from master copy instead
650 $panels[$existingLocation[$id]['panel']][$existingLocation[$id]['row']][$existingLocation[$id]['col']] = $column;
654 // Add it to the $panels
655 if(!empty($addRow)) {
656 $panels[$name][] = $addRow;
661 $panels[$name] = $masterpanel;
665 // We're not done yet... go through the $panels Array now and try to remove duplicate
667 foreach($panels as $name=>$panel) {
668 if(count($panel) == 0 || !isset($masterpanels[$name])) {
669 unset($panels[$name]);
678 * This function merges the $templateMeta Array against the $masterCopy's meta data definition
679 * @param $templateMeta meta data Array to merge
680 * @param $moduleDir Directory name of the module
681 * @param $masterCopy file path to the meta data master copy
682 * @return Array of merged $templateMeta definition
684 function mergeTemplateMeta($templateMeta, $moduleDir, $masterCopy) {
685 require($masterCopy);
686 $masterTemplateMeta = $viewdefs[$moduleDir][$this->mView]['templateMeta'];
688 if(isset($masterTemplateMeta['javascript'])) {
689 //Insert the getJSPath code back into src value
690 $masterTemplateMeta['javascript'] = preg_replace('/src\s*=\s*[\'\"].*?(modules\/|include\/)([^\.]*?\.js)([^\'\"]*?)[\'\"]/i', 'src="@sq . getJSPath(@sq${1}${2}@sq) . @sq"', $masterTemplateMeta['javascript']);
693 return $masterTemplateMeta;
696 function hasRequiredSpanLabel($html) {
701 return preg_match('/\<(div|span) class=(\")?required(\")?\s?>\*<\/(div|span)>/si', $html);
704 function hasMultiplePanels($panels) {
706 if(!isset($panels) || empty($panels) || !is_array($panels)) {
710 if(is_array($panels) && (count($panels) == 0 || count($panels) == 1)) {
714 foreach($panels as $panel) {
715 if(!empty($panel) && !is_array($panel)) {
718 foreach($panel as $row) {
719 if(!empty($row) && !is_array($row)) {
729 function getRelateFieldName($mixed='') {
730 if(!is_array($mixed)) {
732 } else if(count($mixed) == 2){
735 foreach($mixed as $el) {
736 if(preg_match('/_id$/', $el)) {
738 } else if(preg_match('/_name$/', $el)) {
742 return (!empty($id) && !empty($name)) ? $name : '';
747 function getCustomPanels() {
748 return $this->mCustomPanels;
752 * fixTablesWithMissingTr
753 * This is a very crude function to fix instances where files declared a table as
754 * <table...><td> instead of <table...><tr><td>. Without this helper function, the
755 * parsing could messed up.
758 function fixTablesWithMissingTr($tableContents) {
759 if(preg_match('/(<table[^>]*?[\/]?>\s*?<td)/i', $tableContents, $matches)) {
760 return preg_replace('/(<table[^>]*?[\/]?>\s*?<td)/i', '<table><tr><td', $tableContents);
762 return $tableContents;
766 * fixRowsWithMissingTr
767 * This is a very crude function to fix instances where files have an </tr> tag immediately followed by a <td> tag
769 function fixRowsWithMissingTr($tableContents) {
770 if(preg_match('/(<\/tr[^>]*?[\/]?>\s*?<td)/i', $tableContents, $matches)) {
771 return preg_replace('/(<\/tr[^>]*?[\/]?>\s*?<td)/i', '</tr><tr><td', $tableContents);
773 return $tableContents;
778 * This is a very crude function to fix instances where files have two consecutive <tr> tags
780 function fixDuplicateTrTags($tableContents) {
781 if(preg_match('/(<tr[^>]*?[\/]?>\s*?<tr)/i', $tableContents, $matches)) {
782 return preg_replace('/(<tr[^>]*?[\/]?>\s*?<tr)/i', '<tr', $tableContents);
784 return $tableContents;
788 * findSingleVardefElement
789 * Scans array of form elements to see if just one is a vardef element and, if so,
790 * return that vardef name
792 function findSingleVardefElement($formElements=array(), $vardefs=array()) {
793 if(empty($formElements) || !is_array($formElements)) {
798 foreach($formElements as $el) {
799 if(isset($vardefs[$el])) {
804 return count($found) == 1 ? $found[0] : '';