mysociety /
theyworkforyou
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * URL Class |
||
| 4 | * |
||
| 5 | * @package TheyWorkForYou |
||
| 6 | */ |
||
| 7 | |||
| 8 | namespace MySociety\TheyWorkForYou; |
||
| 9 | |||
| 10 | /** |
||
| 11 | * URL Class |
||
| 12 | * |
||
| 13 | * The URL class is used for generating URLs and other related things. |
||
| 14 | * Relies on there being a get_http_var() function. |
||
| 15 | * |
||
| 16 | * This is probably how you'll use it most: |
||
| 17 | * |
||
| 18 | * ```php |
||
| 19 | * $URL = new \MySociety\TheyWorkForYou\Url("YOURPAGENAME"); |
||
| 20 | * print $URL->generate(); |
||
| 21 | * ``` |
||
| 22 | * |
||
| 23 | * In the metadata you should set a session_vars variable, an array. |
||
| 24 | * The default page session_vars may be just array("debug"). |
||
| 25 | * These can then be overridden on a per-page basis. |
||
| 26 | * Session vars are GET/POST vars that will be passed by default to that page. |
||
| 27 | * ie, if "foo=bar" is in the current URL and you generate a URL to a page that has "foo" |
||
| 28 | * as a session_var, "foo=bar" will be automatically added to the generated URL. |
||
| 29 | * You can modify the session vars that will be included in the URL generated using the functions below. |
||
| 30 | * |
||
| 31 | * @author Phil Gyford <[email protected]> |
||
| 32 | */ |
||
| 33 | |||
| 34 | class Url { |
||
| 35 | public function __construct($pagename) { |
||
| 36 | 25 | // Initialise. |
|
| 37 | global $DATA; |
||
| 38 | 25 | ||
| 39 | // The page we're going to be generating URL(s) for. |
||
| 40 | $this->destinationpage = $pagename; |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 41 | 25 | ||
| 42 | // These stores an associative array of key/value pairs that |
||
| 43 | // we'll want passed on to other pages. |
||
| 44 | $this->session_vars = []; |
||
|
0 ignored issues
–
show
|
|||
| 45 | 25 | ||
| 46 | // Prevent things using $DATA running if it hasn't been set, ie in testing |
||
| 47 | if (isset($DATA)) { |
||
| 48 | 25 | ||
| 49 | // Set the contents of $this->session_vars. |
||
| 50 | // session_vars are variables we generally want to pass between pages, if any. |
||
| 51 | // Will only be added as vars if they have values. |
||
| 52 | |||
| 53 | $keys = $DATA->page_metadata($this->destinationpage, "session_vars"); |
||
| 54 | 25 | foreach ($keys as $key) { |
|
| 55 | 25 | if (get_http_var($key) != "") { |
|
| 56 | 25 | $this->session_vars[$key] = get_http_var($key); |
|
| 57 | } |
||
| 58 | } |
||
| 59 | |||
| 60 | // Some pages have the same URL, modified by a "pg" variable. |
||
| 61 | // See if this page is one such, and add the variable if so. |
||
| 62 | if ($pg = $DATA->page_metadata($this->destinationpage, "pg")) { |
||
| 63 | 25 | $this->session_vars["pg"] = $pg; |
|
| 64 | } |
||
| 65 | |||
| 66 | } |
||
| 67 | |||
| 68 | // So we can restore the originals. |
||
| 69 | $this->original_session_vars = $this->session_vars; |
||
|
0 ignored issues
–
show
|
|||
| 70 | 25 | ||
| 71 | } |
||
| 72 | 25 | ||
| 73 | /** |
||
| 74 | * Restore Session Variables |
||
| 75 | * |
||
| 76 | * Restores the session variables to their state when the object was |
||
| 77 | * instantiated. |
||
| 78 | */ |
||
| 79 | |||
| 80 | public function restore() { |
||
| 81 | // Call this to reset the session vars to how they were when |
||
| 82 | // the object was instantiated. |
||
| 83 | $this->session_vars = $this->original_session_vars; |
||
|
0 ignored issues
–
show
|
|||
| 84 | |||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Reset Session Variables |
||
| 89 | * |
||
| 90 | * Removes all session variables. |
||
| 91 | */ |
||
| 92 | |||
| 93 | public function reset() { |
||
| 94 | 1 | // Call this to remove all the session_vars. |
|
| 95 | $this->session_vars = []; |
||
|
0 ignored issues
–
show
|
|||
| 96 | 1 | } |
|
| 97 | 1 | ||
| 98 | /** |
||
| 99 | * Insert Session Key/Value Pairs |
||
| 100 | * |
||
| 101 | * @param array $arr An associative array of key/value pairs |
||
| 102 | */ |
||
| 103 | |||
| 104 | public function insert($arr) { |
||
| 105 | 19 | // $arr is an associative array of key/value pairs. |
|
| 106 | // These will be used as session_vars in addition to any that |
||
| 107 | // already exist. |
||
| 108 | foreach ($arr as $key => $val) { |
||
| 109 | 19 | $this->session_vars[$key] = $val; |
|
|
0 ignored issues
–
show
|
|||
| 110 | 19 | } |
|
| 111 | } |
||
| 112 | 19 | ||
| 113 | /** |
||
| 114 | * Remove Session Key/Value Pair |
||
| 115 | * |
||
| 116 | * @param array $arr A list array of key names to remove |
||
| 117 | */ |
||
| 118 | |||
| 119 | public function remove($arr) { |
||
| 120 | 5 | // $arr is a list array of key names. Any key/value pairs |
|
| 121 | // in session_vars with keys found in $arr will be removed. |
||
| 122 | foreach ($arr as $key) { |
||
| 123 | 5 | if (isset($this->session_vars[$key])) { |
|
| 124 | 5 | unset($this->session_vars[$key]); |
|
| 125 | 4 | } |
|
| 126 | } |
||
| 127 | } |
||
| 128 | 5 | ||
| 129 | /** |
||
| 130 | * Update Values |
||
| 131 | * |
||
| 132 | * Any keys in session_vars that are also in $arr |
||
| 133 | * will have their values overwritten by those in $arr. |
||
| 134 | * Other session_var key/vals are not affected. |
||
| 135 | * |
||
| 136 | * @param array $arr An associative array of key/value pairs. |
||
| 137 | */ |
||
| 138 | |||
| 139 | public function update($arr) { |
||
| 140 | 8 | // |
|
| 141 | // |
||
| 142 | foreach ($arr as $key => $val) { |
||
| 143 | 8 | if (isset($this->session_vars[$key])) { |
|
| 144 | 8 | $this->session_vars[$key] = $arr[$key]; |
|
|
0 ignored issues
–
show
|
|||
| 145 | 8 | } |
|
| 146 | } |
||
| 147 | } |
||
| 148 | 8 | ||
| 149 | /** |
||
| 150 | * Generate URL |
||
| 151 | * |
||
| 152 | * Generate a URL to the page specified with session vars. |
||
| 153 | * |
||
| 154 | * @param string encode "html" will make the URL suitable for inclusion |
||
|
0 ignored issues
–
show
The type
MySociety\TheyWorkForYou\encode was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 155 | * in a page directly, "none" will leave it alone, |
||
| 156 | * "url" will do exactly the same. |
||
| 157 | * @param array overrideVars A key=>value mapping which allows some specific |
||
|
0 ignored issues
–
show
The type
MySociety\TheyWorkForYou\overrideVars was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 158 | * variable/value pairs to be overridden/inserted |
||
| 159 | * into the query. Use this when you want to keep |
||
| 160 | * the standard 'session vars' in a url, but |
||
| 161 | * override just one or two of them. |
||
| 162 | */ |
||
| 163 | |||
| 164 | public function generate($encode = "html", $overrideVars = []) { |
||
| 165 | 25 | ||
| 166 | global $DATA; |
||
| 167 | 25 | ||
| 168 | $url_args = []; |
||
| 169 | 25 | ||
| 170 | foreach (array_merge($this->session_vars, $overrideVars) as $key => $var) { |
||
| 171 | 25 | if (is_array($var)) { |
|
| 172 | 19 | foreach ($var as $v) { |
|
| 173 | $url_args[] = "$key=" . urlencode(stripslashes($v)); |
||
| 174 | } |
||
| 175 | } elseif ($var != null) { |
||
| 176 | 19 | $url_args[] = "$key=" . urlencode(stripslashes($var)); |
|
| 177 | 19 | } |
|
| 178 | } |
||
| 179 | |||
| 180 | $page_url = WEBPATH . $DATA->page_metadata($this->destinationpage, "url"); |
||
| 181 | 25 | ||
| 182 | if (sizeof($url_args) == 0) { |
||
| 183 | 25 | return $page_url; |
|
| 184 | 10 | } else { |
|
| 185 | if ($encode == "html") { |
||
| 186 | 19 | return $page_url . "?" . implode("&", $url_args); |
|
| 187 | 11 | } elseif ($encode == "none" || $encode == "url") { |
|
| 188 | 8 | return $page_url . "?" . implode("&", $url_args); |
|
| 189 | 8 | } |
|
| 190 | } |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Generate a URL for the social image with the given header and subheader. |
||
| 195 | * |
||
| 196 | * @param string $header The header text for the social image. |
||
| 197 | * @param string $subheader The subheader text for the social image. |
||
| 198 | * @param string $parliament The parliament to use (default is "uk"). |
||
| 199 | * @return string The URL to the generated social image. |
||
| 200 | */ |
||
| 201 | public static function generateSocialImageUrl(string $header, string $subheader = "", string $parliament = "uk"): string { |
||
| 202 | |||
| 203 | function chamber_to_parliament($chamber) { |
||
| 204 | switch ($chamber) { |
||
| 205 | case 'uk-commons': |
||
| 206 | return 'uk'; |
||
| 207 | case 'uk-lords': |
||
| 208 | return 'uk'; |
||
| 209 | case 'wales': |
||
| 210 | return 'senedd'; |
||
| 211 | case 'house-of-commons': |
||
| 212 | return 'uk'; |
||
| 213 | case 'scottish-parliament': |
||
| 214 | return 'scotland'; |
||
| 215 | case 'senedd': |
||
| 216 | return 'senedd'; |
||
| 217 | case 'northern-ireland-assembly': |
||
| 218 | return 'ni'; |
||
| 219 | default: |
||
| 220 | return $chamber; |
||
| 221 | } |
||
| 222 | } |
||
| 223 | |||
| 224 | $parliament = chamber_to_parliament($parliament); |
||
| 225 | |||
| 226 | $protocol = 'https://'; |
||
| 227 | if (defined('DEVSITE') && DEVSITE) { |
||
|
0 ignored issues
–
show
|
|||
| 228 | $protocol = 'http://'; |
||
| 229 | } |
||
| 230 | // Update the path for the social image URL |
||
| 231 | $base_url = $protocol . DOMAIN . '/opengraph/image.php'; |
||
| 232 | |||
| 233 | // Generate a hash for validation |
||
| 234 | $hash = substr(hash_hmac('sha256', $header . $subheader, OPENGRAPH_IMAGE_SALT), 0, 10); |
||
|
0 ignored issues
–
show
|
|||
| 235 | |||
| 236 | // Append the truncated hash to the query parameters |
||
| 237 | $query = http_build_query(['heading' => $header, |
||
| 238 | 'subheading' => $subheader, |
||
| 239 | 'parl' => $parliament, |
||
| 240 | 'hash' => $hash]); |
||
| 241 | |||
| 242 | return $base_url . '?' . $query; |
||
| 243 | } |
||
| 244 | |||
| 245 | } |
||
| 246 |