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-2012 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 ********************************************************************************/
39 private $manifestMap = array(
40 'pre_execute'=>'pre_execute',
41 'install_mkdirs'=>'mkdir',
42 'install_copy'=>'copy',
43 'install_images'=>'image_dir',
44 'install_menus'=>'menu',
45 'install_userpage'=>'user_page',
46 'install_dashlets'=>'dashlets',
47 'install_administration'=>'administration',
48 'install_connectors'=>'connectors',
49 'install_vardefs'=>'vardefs',
50 'install_layoutdefs'=>'layoutdefs',
51 'install_layoutfields'=>'layoutfields',
52 'install_relationships'=>'relationships',
53 'install_languages'=>'language',
54 'install_logichooks'=>'logic_hooks',
55 'post_execute'=>'post_execute',
59 private $blackListExempt = array();
61 private $validExt = array('png', 'gif', 'jpg', 'css', 'js', 'php', 'txt', 'html', 'htm', 'tpl', 'pdf', 'md5', 'xml');
62 private $blackList = array(
112 'realpath_cache_get',
113 'realpath_cache_size',
153 'call_user_func_array',
157 //mutliple files per function call
162 'move_uploaded_file',
165 'create_cache_directory',
167 'write_array_to_file',
168 'write_encoded_file',
169 'create_custom_directory',
174 'sugar_file_put_contents',
181 public function printToWiki(){
182 echo "'''Default Extensions'''<br>";
183 foreach($this->validExt as $b){
184 echo '#' . $b . '<br>';
187 echo "'''Default Black Listed Functions'''<br>";
188 foreach($this->blackList as $b){
189 echo '#' . $b . '<br>';
195 public function __construct(){
196 if(!empty($GLOBALS['sugar_config']['moduleInstaller']['blackListExempt'])){
197 $this->blackListExempt = array_merge($this->blackListExempt, $GLOBALS['sugar_config']['moduleInstaller']['blackListExempt']);
199 if(!empty($GLOBALS['sugar_config']['moduleInstaller']['blackList'])){
200 $this->blackList = array_merge($this->blackList, $GLOBALS['sugar_config']['moduleInstaller']['blackList']);
202 if(!empty($GLOBALS['sugar_config']['moduleInstaller']['validExt'])){
203 $this->validExt = array_merge($this->validExt, $GLOBALS['sugar_config']['moduleInstaller']['validExt']);
208 private $issues = array();
209 private $pathToModule = '';
212 *returns a list of issues
214 public function getIssues(){
215 return $this->issues;
219 *returns true or false if any issues were found
221 public function hasIssues(){
222 return !empty($this->issues);
226 *Ensures that a file has a valid extension
228 private function isValidExtension($file){
229 $file = strtolower($file);
231 $extPos = strrpos($file, '.');
232 //make sure they don't override the files.md5
233 if($extPos === false || $file == 'files.md5')return false;
234 $ext = substr($file, $extPos + 1);
235 return in_array($ext, $this->validExt);
240 *Scans a directory and calls on scan file for each file
242 public function scanDir($path){
243 static $startPath = '';
244 if(empty($startPath))$startPath = $path;
245 if(!is_dir($path))return false;
247 while($e = $d->read()){
248 $next = $path . '/' . $e;
250 if(substr($e, 0, 1) == '.')continue;
251 $this->scanDir($next);
253 $issues = $this->scanFile($next);
262 * Check if the file contents looks like PHP
263 * @param string $contents File contents
266 public function isPHPFile($contents)
268 if(stripos($contents, '<?php') !== false) return true;
269 for($tag=0;($tag = stripos($contents, '<?', $tag)) !== false;$tag++) {
270 if(strncasecmp(substr($contents, $tag, 13), '<?xml version', 13) == 0) {
271 // <?xml version is OK, skip it
275 // found <?, it's PHP
282 * Given a file it will open it's contents and check if it is a PHP file (not safe to just rely on extensions) if it finds <?php tags it will use the tokenizer to scan the file
283 * $var() and ` are always prevented then whatever is in the blacklist.
284 * It will also ensure that all files are of valid extension types
287 public function scanFile($file){
289 if(!$this->isValidExtension($file)){
290 $issues[] = translate('ML_INVALID_EXT');
291 $this->issues['file'][$file] = $issues;
294 $contents = file_get_contents($file);
295 if(!$this->isPHPFile($contents)) return $issues;
296 $tokens = @token_get_all($contents);
297 $checkFunction = false;
300 foreach($tokens as $index=>$token){
301 if(is_string($token[0])){
304 $issues['backtick'] = translate('ML_INVALID_FUNCTION') . " '`'";
306 if($checkFunction)$issues[] = $possibleIssue;
309 $checkFunction = false;
312 $token['_msi'] = token_name($token[0]);
314 case T_WHITESPACE: continue;
316 if(in_array('eval', $this->blackList) && !in_array('eval', $this->blackListExempt))
317 $issues[]= translate('ML_INVALID_FUNCTION') . ' eval()';
320 $token[1] = strtolower($token[1]);
321 if(!in_array($token[1], $this->blackList))break;
322 if(in_array($token[1], $this->blackListExempt))break;
323 if ($lastToken !== false &&
324 ($lastToken[0] == T_NEW || $lastToken[0] == T_OBJECT_OPERATOR || $lastToken[0] == T_DOUBLE_COLON))
329 $checkFunction = true;
330 $possibleIssue = translate('ML_INVALID_FUNCTION') . ' ' . $token[1] . '()';
334 $checkFunction = false;
338 if ($token[0] != T_WHITESPACE)
346 $this->issues['file'][$file] = $issues;
354 * checks files.md5 file to see if the file is from sugar
355 * ONLY WORKS ON FILES
357 public function sugarFileExists($path){
358 static $md5 = array();
359 if(empty($md5) && file_exists('files.md5'))
361 include('files.md5');
364 if(isset($md5['./' . $path]))return true;
371 *This function will scan the Manifest for disabled actions specified in $GLOBALS['sugar_config']['moduleInstaller']['disableActions']
372 *if $GLOBALS['sugar_config']['moduleInstaller']['disableRestrictedCopy'] is set to false or not set it will call on scanCopy to ensure that it is not overriding files
374 public function scanManifest($manifestPath){
376 if(!file_exists($manifestPath)){
377 $this->issues['manifest'][$manifestPath] = translate('ML_NO_MANIFEST');
380 $fileIssues = $this->scanFile($manifestPath);
381 //if the manifest contains malicious code do not open it
382 if(!empty($fileIssues)){
385 include($manifestPath);
388 //scan for disabled actions
389 if(isset($GLOBALS['sugar_config']['moduleInstaller']['disableActions'])){
390 foreach($GLOBALS['sugar_config']['moduleInstaller']['disableActions'] as $action){
391 if(isset($installdefs[$this->manifestMap[$action]])){
392 $issues[] = translate('ML_INVALID_ACTION_IN_MANIFEST') . $this->manifestMap[$action];
397 //now lets scan for files that will override our files
398 if(empty($GLOBALS['sugar_config']['moduleInstaller']['disableRestrictedCopy']) && isset($installdefs['copy'])){
399 foreach($installdefs['copy'] as $copy){
400 $from = str_replace('<basepath>', $this->pathToModule, $copy['from']);
402 if(substr_count($from, '..')){
403 $this->issues['copy'][$from] = translate('ML_PATH_MAY_NOT_CONTAIN').' ".." -' . $from;
405 if(substr_count($to, '..')){
406 $this->issues['copy'][$to] = translate('ML_PATH_MAY_NOT_CONTAIN'). ' ".." -' . $to;
408 while(substr_count($from, '//')){
409 $from = str_replace('//', '/', $from);
411 while(substr_count($to, '//')){
412 $to = str_replace('//', '/', $to);
414 $this->scanCopy($from, $to);
418 $this->issues['manifest'][$manifestPath] = $issues;
428 * Takes in where the file will is specified to be copied from and to
429 * and ensures that there is no official sugar file there. If the file exists it will check
430 * against the MD5 file list to see if Sugar Created the file
433 function scanCopy($from, $to){
434 //if the file doesn't exist for the $to then it is not overriding anything
435 if(!file_exists($to))return;
436 //if $to is a dir and $from is a file then make $to a full file path as well
437 if(is_dir($to) && is_file($from)){
438 if(substr($to,-1) === '/'){
439 $to = substr($to, 0 , strlen($to) - 1);
441 $to .= '/'. basename($from);
443 //if the $to is a file and it is found in sugarFileExists then don't allow overriding it
444 if(is_file($to) && $this->sugarFileExists($to)){
445 $this->issues['copy'][$from] = translate('ML_OVERRIDE_CORE_FILES') . '(' . $to . ')';
450 while($e = $d->read()){
451 if($e == '.' || $e == '..')continue;
452 $this->scanCopy($from .'/'. $e, $to .'/' . $e);
464 *Main external function that takes in a path to a package and then scans
465 *that package's manifest for disabled actions and then it scans the PHP files
466 *for restricted function calls
469 public function scanPackage($path){
470 $this->pathToModule = $path;
471 $this->scanManifest($path . '/manifest.php');
472 if(empty($GLOBALS['sugar_config']['moduleInstaller']['disableFileScan'])){
473 $this->scanDir($path);
478 *This function will take all issues of the current instance and print them to the screen
480 public function displayIssues($package='Package'){
481 echo '<h2>'.str_replace('{PACKAGE}' , $package ,translate('ML_PACKAGE_SCANNING')). '</h2><BR><h2 class="error">' . translate('ML_INSTALLATION_FAILED') . '</h2><br><p>' .str_replace('{PACKAGE}' , $package ,translate('ML_PACKAGE_NOT_CONFIRM')). '</p><ul><li>'. translate('ML_OBTAIN_NEW_PACKAGE') . '<li>' . translate('ML_RELAX_LOCAL').
482 '</ul></p><br>' . translate('ML_SUGAR_LOADING_POLICY') . ' <a href=" http://kb.sugarcrm.com/custom/module-loader-restrictions-for-sugar-open-cloud/">' . translate('ML_SUGAR_KB') . '</a>.'.
483 '<br>' . translate('ML_AVAIL_RESTRICTION'). ' <a href=" http://developers.sugarcrm.com/wordpress/2009/08/14/module-loader-restrictions/">' . translate('ML_SUGAR_DZ') . '</a>.<br><br>';
486 foreach($this->issues as $type=>$issues){
487 echo '<div class="error"><h2>'. ucfirst($type) .' ' . translate('ML_ISSUES') . '</h2> </div>';
488 echo '<div id="details' . $type . '" >';
489 foreach($issues as $file=>$issue){
490 $file = str_replace($this->pathToModule . '/', '', $file);
491 echo '<div style="position:relative;left:10px"><b>' . $file . '</b></div><div style="position:relative;left:20px">';
492 if(is_array($issue)){
493 foreach($issue as $i){
504 echo "<br><input class='button' onclick='document.location.href=\"index.php?module=Administration&action=UpgradeWizard&view=module\"' type='button' value=\"" . translate('LBL_UW_BTN_BACK_TO_MOD_LOADER') . "\" />";