Projet

Général

Profil

Paste
Télécharger (2,27 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views_pdf / modules / views_append / views_append.module @ 11b63505

1
<?php
2
/**
3
 * @file
4
 * Module for appending PDF views to other PDF views
5
 *
6
 * The module adds a field which can be used to append another view. The view is
7
 * created by calling the View over a HTTP request.
8
 *
9
 */
10

    
11

    
12
/**
13
 * Implementation of hook_views_api().
14
 */
15
function views_append_views_api() {
16
  return array(
17
    'api' => 3,
18
  );
19
}
20

    
21
/**
22
 * This function emulates the request of a browser. This is used to get the
23
 * PDF file to append.
24
 */
25
function views_append_request_with_cookie($url, $save_path) {
26

    
27
  $urlComponents = parse_url($url);
28

    
29
  // Define the specified port
30
  if ($urlComponents['scheme'] === 'http')  {
31
    $port = 80;
32
  }
33
  elseif ($urlComponents['scheme'] === 'https') {
34
    $port = 443;
35
  }
36
  else {
37
    $port = 80;
38
  }
39

    
40
  // Define the host
41
  $host = $urlComponents['host'];
42

    
43
  // Define the path
44
  if (!empty($urlComponents['query'])) {
45
    $path = $urlComponents['path'] . '?' . $urlComponents['query'];
46
  }
47
  else {
48
    $path = $urlComponents['path'];
49
  }
50

    
51
  // Change host if ssl is used:
52
  if ($port == 443) {
53
    $hostUrl = "ssl://" . $host;
54
  }
55
  else {
56
    $hostUrl = $host;
57
  }
58

    
59
  $fp = fsockopen($hostUrl, $port, $errno, $errstr, 30);
60

    
61
  $method = 'GET';
62
  $content = '';
63

    
64
  if (!$fp) {
65
    echo "$errstr ($errno)<br />\n";
66
  }
67
  else {
68
    $out = "$method $path HTTP/1.1\r\n";
69
    $out .= "Host: $host\r\n";
70
    if ($method == 'POST') {
71
      $out .= "Content-Type: application/x-www-form-urlencoded\r\n";
72
    }
73
    $out .= "Content-length: " . strlen($content) . "\r\n";
74
    $out .= "Cookie: " . session_name() . '=' . session_id() . "; \r\n";
75
    $out .= "Connection: Close\r\n\r\n";
76

    
77
    if ($method == 'POST') {
78
      $out .= $content;
79
    }
80

    
81
    fwrite($fp, $out);
82

    
83
    $newFile = fopen($save_path, 'w');
84

    
85
    $header = TRUE;
86

    
87
    while (!feof($fp)) {
88
      $content = fgets($fp, 8096);
89
      if ($content == "\r\n") {
90
        $header = FALSE;
91
      }
92
      elseif (!$header) {
93
        fwrite($newFile, $content);
94
      }
95

    
96
    }
97
    fclose($fp);
98
    fclose($newFile);
99
  }
100

    
101
}
102

    
103
function _views_append_parse_array_to_string($array) {
104

    
105
  if (is_array($array)) {
106
    foreach ($array as $key => $value) {
107
      $string .= $key . '=' . $value . '&';
108
    }
109
    if (!empty($string)) {
110
      $string = substr($string, 0, -1);
111
    }
112

    
113
    return $string;
114
  }
115
  else {
116
    return $array;
117
  }
118
}