Symlinks cause breakage with path variables like [civicrm.files]
CiviCRM is presenting problems when certain directories referenced with path variables such as [civicrm.files] contain symlinks.
Symptoms
Symptoms seen so far seem to include failure to load extension resources (e.g., script and style files). For example, on one site of mine which has the following configuration
- Site url: http://mysite.example.com
- docroot symlinked from the users' home directory
/home/myuser/www -> /var/www/mysite
- civicrm root at
/var/www/mysite/wp-content/plugins/civicrm/civicrm
- civicrm extensions at
/var/www/mysite/wp-content/uploads/civicrm/ext
- "Extensions Directory" setting set to
[civicrm.files]/ext/
- an extension which declares a managed entity ContactType with property
"image_URL" => CRM_Core_Resources::singleton()->getUrl('myextension', 'images/icon.png', TRUE)
The following happens when civicrm cache is rebuilt (an action which updates managed entities):
- If cache is rebuilt from the browser via Administer > Cleanup Caches and Update Paths, the contact type icon url is correctly updated to
http://mysite.example.com/wp-content/uploads/civicrm/ext/myextension/images/icon.png
- If cache is rebuilt from the command line with
cd /var/www/mysite; cv flush
orcv --cwd=/var/www/mysite flush
, the contact type icon url is correctly updated tohttp://mysite.example.com/wp-content/uploads/civicrm/ext/myextension/images/icon.png
- If cache is rebuilt from the command line with
cd /home/myuser/www; cv flush
orcv --cwd=/home/myuser/www flush
, the contact type icon url is incorrectly updated tohttp://mysite.example.com/var/www/mysite/wp-content/uploads/civicrm/ext/myextension/images/icon.png
Analysis and prior discussion
- This issue is referenced on SE here: https://civicrm.stackexchange.com/a/28287/907
- Discussion on MM (https://chat.civicrm.org/civicrm/pl/xxiwai9h1jgd5b9iwnmheq5y5a) indicates that
CRM_Utils_File::relativize()
might benefit from use of PHP'srealpath()
to de-reference symlinks:
@kcristiano says on MM (permalink):
We had issues with WP engine and Pantheon and it lead to updating CRM_Utils_System_Wordpress in function getCiviSourceStorage to use
civiRelPath = CRM_Utils_File::relativize(realpath(
civicrm_root), realpath($cmsPath)); This solved some issues, but I noticed if an older site had the path to the symlink set in the settings file eg /srv/www/cvwp/wp-content/uploads/civicrm instead of it's real location of /srv/www/cvwp_content/uploads/civicrm Problems would occur. In my example there is a symlink to wp-content loking at the cvwp_content directory. This gets worse if all of wp-content is moved including plugins, or if the plugins directory is moved. On a new install we detect the relpath and it works, at least in my tetsing, but in the real world there are issues
@AllenShaw says on MM (permalink):
My debugging yesterday also pointed me to CRM_Utils_File::relativize(). ... perhaps using realpath() inside of CRM_Utils_File::relativize() will cover more cases; ... because that's where the string-based path replacement is happening, which is why somehting like /srv/www/cvwp/wp-content/uploads/civicrm would not be properly matched to its real location of /srv/www/cvwp_content/uploads/civicrm. ... I think it's that string-based path replacement that's really causing the problem.