From 2097515a4145f0e1b2faffa520283545e44c7cbe Mon Sep 17 00:00:00 2001
From: Colan Schwartz <colan@58704.no-reply.drupal.org>
Date: Wed, 20 Jul 2016 15:10:14 -0400
Subject: [PATCH] Issue #2769587 by colan: Check for PHP 7 FPM sockets as well
 as in PHP 5.

---
 .../Config/Nginx/Inc/vhost_include.tpl.php         | 20 +++--
 http/Provision/Config/Nginx/subdir.tpl.php         | 14 ++--
 http/Provision/Service/http/nginx.php              | 87 ++++++++++++++++++----
 http/Provision/Service/http/nginx/ssl.php          | 18 +----
 4 files changed, 94 insertions(+), 45 deletions(-)

diff --git a/http/Provision/Config/Nginx/Inc/vhost_include.tpl.php b/http/Provision/Config/Nginx/Inc/vhost_include.tpl.php
index 6785d08..8e759ff 100644
--- a/http/Provision/Config/Nginx/Inc/vhost_include.tpl.php
+++ b/http/Provision/Config/Nginx/Inc/vhost_include.tpl.php
@@ -19,6 +19,10 @@ if (!$phpfpm_mode && $server->phpfpm_mode) {
   $phpfpm_mode = $server->phpfpm_mode;
 }
 
+// We can use $server here once we have proper inheritance.
+// See Provision_Service_http_nginx_ssl for details.
+$phpfpm_socket_path = Provision_Service_http_nginx::getPhpFpmSocketPath();
+
 $nginx_is_modern = drush_get_option('nginx_is_modern');
 if (!$nginx_is_modern && $server->nginx_is_modern) {
   $nginx_is_modern = $server->nginx_is_modern;
@@ -227,7 +231,7 @@ location = /fpm-status {
 <?php elseif ($phpfpm_mode == 'port'): ?>
   fastcgi_pass 127.0.0.1:9000;
 <?php else: ?>
-  fastcgi_pass unix:/var/run/php5-fpm.sock;
+  fastcgi_pass unix:<?php print $phpfpm_socket_path; ?>;
 <?php endif; ?>
 }
 
@@ -243,7 +247,7 @@ location = /fpm-ping {
 <?php elseif ($phpfpm_mode == 'port'): ?>
   fastcgi_pass 127.0.0.1:9000;
 <?php else: ?>
-  fastcgi_pass unix:/var/run/php5-fpm.sock;
+  fastcgi_pass unix:<?php print $phpfpm_socket_path; ?>;
 <?php endif; ?>
 }
 <?php endif; ?>
@@ -266,7 +270,7 @@ location = /cron.php {
 <?php elseif ($phpfpm_mode == 'port'): ?>
   fastcgi_pass 127.0.0.1:9000;
 <?php else: ?>
-  fastcgi_pass unix:/var/run/php5-fpm.sock;
+  fastcgi_pass unix:<?php print $phpfpm_socket_path; ?>;
 <?php endif; ?>
 }
 
@@ -915,7 +919,7 @@ location ~* /(?:modules|libraries)/(?:contrib/)?(?:ad|tinybrowser|f?ckeditor|tin
 <?php elseif ($phpfpm_mode == 'port'): ?>
   fastcgi_pass 127.0.0.1:9000;
 <?php else: ?>
-  fastcgi_pass unix:/var/run/php5-fpm.sock;
+  fastcgi_pass unix:<?php print $phpfpm_socket_path; ?>;
 <?php endif; ?>
 }
 
@@ -1066,7 +1070,7 @@ location ~ ^/(?<esi>esi/.*)"$ {
 <?php elseif ($phpfpm_mode == 'port'): ?>
   fastcgi_pass  127.0.0.1:9000;
 <?php else: ?>
-  fastcgi_pass  unix:/var/run/php5-fpm.sock;
+  fastcgi_pass  unix:<?php print $phpfpm_socket_path; ?>;
 <?php endif; ?>
   ###
   ### Use Nginx cache for all visitors.
@@ -1220,7 +1224,7 @@ location = /index.php {
 <?php elseif ($phpfpm_mode == 'port'): ?>
   fastcgi_pass  127.0.0.1:9000;
 <?php else: ?>
-  fastcgi_pass  unix:/var/run/php5-fpm.sock;
+  fastcgi_pass  unix:<?php print $phpfpm_socket_path; ?>;
 <?php endif; ?>
 <?php if ($nginx_has_upload_progress): ?>
   track_uploads uploads 60s; ### required for upload progress
@@ -1274,7 +1278,7 @@ location ~* ^/(?:index|cron|boost_stats|update|authorize|xmlrpc)\.php$ {
 <?php elseif ($phpfpm_mode == 'port'): ?>
   fastcgi_pass 127.0.0.1:9000;
 <?php else: ?>
-  fastcgi_pass unix:/var/run/php5-fpm.sock;
+  fastcgi_pass unix:<?php print $phpfpm_socket_path; ?>;
 <?php endif; ?>
 }
 
@@ -1304,7 +1308,7 @@ location @allowupdate {
 <?php elseif ($phpfpm_mode == 'port'): ?>
   fastcgi_pass 127.0.0.1:9000;
 <?php else: ?>
-  fastcgi_pass unix:/var/run/php5-fpm.sock;
+  fastcgi_pass unix:<?php print $phpfpm_socket_path; ?>;
 <?php endif; ?>
 }
 <?php endif; ?>
diff --git a/http/Provision/Config/Nginx/subdir.tpl.php b/http/Provision/Config/Nginx/subdir.tpl.php
index 22a7c1b..9435cc8 100644
--- a/http/Provision/Config/Nginx/subdir.tpl.php
+++ b/http/Provision/Config/Nginx/subdir.tpl.php
@@ -19,6 +19,10 @@ if (!$phpfpm_mode && $server->phpfpm_mode) {
   $phpfpm_mode = $server->phpfpm_mode;
 }
 
+// We can use $server here once we have proper inheritance.
+// See Provision_Service_http_nginx_ssl for details.
+$phpfpm_socket_path = Provision_Service_http_nginx::getPhpFpmSocketPath();
+
 $nginx_is_modern = drush_get_option('nginx_is_modern');
 if (!$nginx_is_modern && $server->nginx_is_modern) {
   $nginx_is_modern = $server->nginx_is_modern;
@@ -315,7 +319,7 @@ location ^~ /<?php print $subdir; ?> {
 <?php elseif ($phpfpm_mode == 'port'): ?>
     fastcgi_pass 127.0.0.1:9000;
 <?php else: ?>
-    fastcgi_pass unix:/var/run/php5-fpm.sock;
+    fastcgi_pass unix:<?php print $phpfpm_socket_path; ?>;
 <?php endif; ?>
   }
 
@@ -781,7 +785,7 @@ location ^~ /<?php print $subdir; ?> {
 <?php elseif ($phpfpm_mode == 'port'): ?>
     fastcgi_pass 127.0.0.1:9000;
 <?php else: ?>
-    fastcgi_pass unix:/var/run/php5-fpm.sock;
+    fastcgi_pass unix:<?php print $phpfpm_socket_path; ?>;
 <?php endif; ?>
   }
 
@@ -980,7 +984,7 @@ location ^~ /<?php print $subdir; ?> {
 <?php elseif ($phpfpm_mode == 'port'): ?>
     fastcgi_pass 127.0.0.1:9000;
 <?php else: ?>
-    fastcgi_pass unix:/var/run/php5-fpm.sock;
+    fastcgi_pass unix:<?php print $phpfpm_socket_path; ?>;
 <?php endif; ?>
   }
 
@@ -1059,7 +1063,7 @@ location ^~ /<?php print $subdir; ?> {
 <?php elseif ($phpfpm_mode == 'port'): ?>
     fastcgi_pass  127.0.0.1:9000;
 <?php else: ?>
-    fastcgi_pass  unix:/var/run/php5-fpm.sock;
+    fastcgi_pass unix:<?php print $phpfpm_socket_path; ?>;
 <?php endif; ?>
 <?php if ($nginx_has_upload_progress): ?>
     track_uploads uploads 60s; ### required for upload progress
@@ -1207,7 +1211,7 @@ location @allowupdate_<?php print $subdir_loc; ?> {
 <?php elseif ($phpfpm_mode == 'port'): ?>
   fastcgi_pass 127.0.0.1:9000;
 <?php else: ?>
-  fastcgi_pass unix:/var/run/php5-fpm.sock;
+  fastcgi_pass unix:<?php print $phpfpm_socket_path; ?>;
 <?php endif; ?>
 }
 <?php endif; ?>
diff --git a/http/Provision/Service/http/nginx.php b/http/Provision/Service/http/nginx.php
index e146a1f..afe7da0 100644
--- a/http/Provision/Service/http/nginx.php
+++ b/http/Provision/Service/http/nginx.php
@@ -1,6 +1,11 @@
 <?php
 
 class Provision_Service_http_nginx extends Provision_Service_http_public {
+
+  // Define socket file locations for various PHP versions.
+  const SOCKET_PATH_PHP5 = '/var/run/php5-fpm.sock';
+  const SOCKET_PATH_PHP7 = '/var/run/php/php7.0-fpm.sock';
+
   protected $application_name = 'nginx';
   protected $has_restart_cmd = TRUE;
 
@@ -71,14 +76,7 @@ class Provision_Service_http_nginx extends Provision_Service_http_public {
     }
 
     // Check if there is php-fpm listening on unix socket, otherwise use port 9000 to connect
-    if (provision_file()->exists('/var/run/php5-fpm.sock')->status()) {
-      $this->server->phpfpm_mode = 'socket';
-      drush_log(dt('PHP-FPM unix socket mode detected -SAVE- YES socket found @path.', array('@path' => '/var/run/php5-fpm.sock')));
-    }
-    else {
-      $this->server->phpfpm_mode = 'port';
-      drush_log(dt('PHP-FPM port mode detected -SAVE- NO socket found @path.', array('@path' => '/var/run/php5-fpm.sock')));
-    }
+    $this->server->phpfpm_mode = $this->getPhpFpmMode('save');
 
     // Check if there is BOA specific global.inc file to enable extra Nginx locations
     if (provision_file()->exists('/data/conf/global.inc')->status()) {
@@ -133,14 +131,7 @@ class Provision_Service_http_nginx extends Provision_Service_http_public {
     }
 
     // Check if there is php-fpm listening on unix socket, otherwise use port 9000 to connect
-    if (provision_file()->exists('/var/run/php5-fpm.sock')->status()) {
-      $this->server->phpfpm_mode = 'socket';
-      drush_log(dt('PHP-FPM unix socket mode detected -VERIFY- YES socket found @path.', array('@path' => '/var/run/php5-fpm.sock')));
-    }
-    else {
-      $this->server->phpfpm_mode = 'port';
-      drush_log(dt('PHP-FPM port mode detected -VERIFY- NO socket found @path.', array('@path' => '/var/run/php5-fpm.sock')));
-    }
+    $this->server->phpfpm_mode = $this->getPhpFpmMode('verify');
 
     // Check if there is BOA specific global.inc file to enable extra Nginx locations
     if (provision_file()->exists('/data/conf/global.inc')->status()) {
@@ -162,6 +153,70 @@ class Provision_Service_http_nginx extends Provision_Service_http_public {
   }
 
   /**
+   * Determines the PHP FPM mode.
+   *
+   * @param string $server_task
+   *   The server task type for logging purposes. Leave blank to skip logging.
+   * @return string
+   *   The mode, either 'socket' or 'port'.
+   */
+  public static function getPhpFpmMode($server_task = NULL) {
+
+    // Search for socket files or fall back to port mode.
+    switch (TRUE) {
+      case provision_file()->exists(self::SOCKET_PATH_PHP5)->status():
+        $mode = 'socket';
+        $socket_path = self::SOCKET_PATH_PHP5;
+      break;
+      case provision_file()->exists(self::SOCKET_PATH_PHP7)->status():
+        $mode = 'socket';
+        $socket_path = self::SOCKET_PATH_PHP7;
+      break;
+      default:
+        $mode = 'port';
+        $socket_path = '';
+      break;
+    }
+
+    // Report results in the log if requested.
+    if (!empty($server_task)) {
+      drush_log(dt('PHP-FPM @mode mode detected -' . '@task' . '- @yes_or_no socket found @path.', array(
+        '@mode' => ($mode == 'socket') ? 'unix socket' : 'port',
+        '@task' => drupal_strtoupper($server_task),
+        '@yes_or_no' => ($mode == 'socket') ? 'YES' : 'NO',
+        '@path' => ($socket_path ? $socket_path : self::SOCKET_PATH_PHP5 . ' or ' . self::SOCKET_PATH_PHP7),
+      )));
+    }
+
+    // Return the discovered mode.
+    return $mode;
+  }
+
+  /**
+   * Gets the PHP FPM unix socket path.
+   *
+   * If we're running in port mode, there is no socket path. FALSE would be
+   * returned in this case.
+   *
+   * @return string
+   *   The path, or FALSE if there isn't one.
+   */
+  public static function getPhpFpmSocketPath() {
+    // Simply return FALSE if we're in port mode.
+    if (self::getPhpFpmMode() == 'port') {
+      return FALSE;
+    }
+
+    // Return the socket path based on the PHP version.
+    if (strtok(phpversion(), '.') == 7) {
+      return self::SOCKET_PATH_PHP7;
+    }
+    else {
+      return self::SOCKET_PATH_PHP5;
+    }
+  }
+
+  /**
    * Guess at the likely value of the http_restart_cmd.
    *
    * This method is a static so that it can be re-used by the nginx_ssl
diff --git a/http/Provision/Service/http/nginx/ssl.php b/http/Provision/Service/http/nginx/ssl.php
index 157bf8f..934e93d 100644
--- a/http/Provision/Service/http/nginx/ssl.php
+++ b/http/Provision/Service/http/nginx/ssl.php
@@ -80,14 +80,7 @@ class Provision_Service_http_nginx_ssl extends Provision_Service_http_ssl {
     }
 
     // Check if there is php-fpm listening on unix socket, otherwise use port 9000 to connect
-    if (provision_file()->exists('/var/run/php5-fpm.sock')->status()) {
-      $this->server->phpfpm_mode = 'socket';
-      drush_log(dt('PHP-FPM unix socket mode detected -SAVE- YES socket found @path.', array('@path' => '/var/run/php5-fpm.sock')));
-    }
-    else {
-      $this->server->phpfpm_mode = 'port';
-      drush_log(dt('PHP-FPM port mode detected -SAVE- NO socket found @path.', array('@path' => '/var/run/php5-fpm.sock')));
-    }
+    $this->server->phpfpm_mode = Provision_Service_http_nginx::getPhpFpmMode('save');
 
     // Check if there is BOA specific global.inc file to enable extra Nginx locations
     if (provision_file()->exists('/data/conf/global.inc')->status()) {
@@ -133,14 +126,7 @@ class Provision_Service_http_nginx_ssl extends Provision_Service_http_ssl {
     }
 
     // Check if there is php-fpm listening on unix socket, otherwise use port 9000 to connect
-    if (provision_file()->exists('/var/run/php5-fpm.sock')->status()) {
-      $this->server->phpfpm_mode = 'socket';
-      drush_log(dt('PHP-FPM unix socket mode detected -VERIFY- YES socket found @path.', array('@path' => '/var/run/php5-fpm.sock')));
-    }
-    else {
-      $this->server->phpfpm_mode = 'port';
-      drush_log(dt('PHP-FPM port mode detected -VERIFY- NO socket found @path.', array('@path' => '/var/run/php5-fpm.sock')));
-    }
+    $this->server->phpfpm_mode = Provision_Service_http_nginx::getPhpFpmMode('verify');
 
     // Check if there is BOA specific global.inc file to enable extra Nginx locations
     if (provision_file()->exists('/data/conf/global.inc')->status()) {
-- 
2.7.4

