source: trunk/www/url.php @ 1184

Revision 1184, 10.8 KB checked in by bogdan2412, 4 weeks ago (diff)

Merged changes from live and a few extra

  • Updated backup script to include new configuration files after migration to HPHP
  • Copied over updated code from the textblock controller to the user controller in order to fix crash on invalid revision number
  • Fixed Twitter button
  • Fixed a bug with task test case grouping when string was too large to be stored in database
  • Add IA_HTTPS_ENABLED configuration option. Allows disableing of secure connection enforcement in development mode.
  • More user friendly strings for 'security' in userinfo macro.
  • Linting fixes
  • Property svn:eol-style set to native
Line 
1<?php
2
3require_once(IA_ROOT_DIR . 'www/config.php');
4require_once(IA_ROOT_DIR . 'common/common.php');
5
6// Creates URLs to various parts of the infoarena website.
7// Please avoid hard-coding URLs throughout the code.
8
9// Compute complex url. Avoid using this function directly, prefer more
10// specific url_ functions.
11//
12// The params array contains http get parameter,
13// it's formatted in the end result as a series
14// of key1=value1&key2=value2.
15//
16// NOTE: don't add ?x=y stuff in $document
17//
18// If $absolute is true(default false) then IA_URL_HOST will be included in
19// the url.
20function url_complex($document = '', $args = array(), $absolute = false,
21        $secure_connection = false) {
22    log_assert(false === strpos($document, '?'), 'Page name contains ?');
23    log_assert(is_array($args), "Argument list must be an array");
24    log_assert(!array_key_exists("page", $args), "Argument list contains page");
25    if ($secure_connection == true) {
26        log_assert($absolute == true);
27    }
28    $args['page'] = $document;
29    $url = url_from_args($args, $absolute);
30    if ($absolute) {
31        return url_absolute($url, $secure_connection);
32    } else  {
33        return $url;
34    }
35}
36
37// Makes an url absolute. It just prepends IA_URL_HOST
38function url_absolute($url, $force_secure_connection = false)
39{
40    log_assert(strpos($url, 'http') !== 0, "Url begins with http");
41    $url = IA_URL_HOST . $url;
42    if ($force_secure_connection == true || is_connection_secure()) {
43        $count = 1;
44        $url = str_replace('http', 'https', $url, $count);
45    }
46
47    return $url;
48}
49
50// Construct an URL from an argument list.
51// These are the exact $args you will receive in $_GET
52function url_from_args($args)
53{
54    // First part.
55    $url = IA_URL_PREFIX . getattr($args, "page", "home");
56
57    // Actual args.
58    $first = true;
59    foreach ($args as $k => $v) {
60        if (is_null($v)) {
61            continue;
62        }
63        if ($k != 'page') {
64            if (is_array($v)) {
65                foreach ($v as $sv) {
66                    $url .= ($first ? "?" : "&");
67                    $first = false;
68                    $url .= $k. '[]=' . urlencode($sv);
69                }
70            } else {
71                $url .= ($first ? "?" : "&");
72                $first = false;
73                $url .= $k . '=' . urlencode($v);
74            }
75        }
76    }
77    return $url;
78}
79
80// Here are the specific url functions you should use.
81// Names should should be more or less obvious.
82
83// First: textblocks(wiki pages).
84
85function url_textblock($page_name) {
86    return url_complex($page_name, array());
87}
88
89function url_textblock_revision($page_name, $rev) {
90    return url_complex($page_name, array('revision' => $rev));
91}
92
93function url_textblock_edit($page_name) {
94    return url_complex($page_name, array('action' => 'edit'));
95}
96
97function url_textblock_history($page_name) {
98    return url_complex($page_name, array('action' => 'history'));
99}
100
101function url_blog_feed() {
102    return url_complex('blog', array('action' => 'rss'));
103}
104
105function url_blog($tag = null) {
106    if ($tag) {
107        return url_complex('blog', array('tag' => $tag));
108    }
109    return url_complex('blog');
110}
111
112function url_textblock_diff($page_name, $revfrom, $revto) {
113    return url_complex($page_name, array(
114            'action' => 'diff',
115            'rev_from' => $revfrom,
116            'rev_to' => $revto,
117    ));
118}
119
120function url_textblock_move($page_name) {
121    return url_complex($page_name, array(
122            'action' => 'move'
123    ));
124}
125
126function url_textblock_copy($page_name) {
127    return url_complex($page_name, array(
128            'action' => 'copy'
129    ));
130}
131
132function url_textblock_delete($page_name) {
133    return url_complex($page_name, array(
134            'action' => 'delete'
135    ));
136}
137
138function url_textblock_restore($page_name, $rev) {
139    return url_complex($page_name, array(
140            'action' => 'restore',
141            'revision' => $rev,
142    ));
143}
144
145function url_textblock_delete_revision($page_name, $rev) {
146    return url_complex($page_name, array(
147            'action' => 'delete-revision',
148            'revision' => $rev
149    ));
150}
151
152// Textblock attachments:
153
154function url_attachment($page, $file, $restrict_to_safe_mime_types = false) {
155    log_assert(is_page_name($page));
156    log_assert(is_attachment_name($file));
157    return url_complex($page, array(
158            'action' => 'download',
159            'file' => $file,
160            'safe_only' => ($restrict_to_safe_mime_types ? 'true' : 'false'),
161    ));
162}
163
164function url_attachment_new($page_name) {
165    log_assert(is_page_name($page_name));
166    return url_complex($page_name, array('action' => 'attach'));
167}
168
169function url_attachment_list($page_name) {
170    log_assert(is_page_name($page_name));
171    return url_complex($page_name, array('action' => 'attach-list'));
172}
173
174function url_attachment_delete($page_name, $file_name) {
175    log_assert(is_page_name($page_name));
176    return url_complex($page_name, array(
177                'action' => 'attach-del',
178                'file' => $file_name,
179    ));
180}
181
182function url_attachment_rename($page_name) {
183    log_assert(is_page_name($page_name));
184    return url_complex($page_name, array('action' => 'attach-rename'));
185}
186
187function url_image_resize($page, $file, $resize)
188{
189    if ($resize) {
190        return url_complex($page, array(
191                'action' => 'download',
192                'file' => $file,
193                'resize' => $resize,
194        ));
195    } else {
196        return url_attachment($page, $file, true);
197    }
198}
199
200// User stuff
201
202function url_login() {
203    return url_complex("login", array(), true, IA_HTTPS_ENABLED);
204}
205
206function url_logout() {
207    return url_complex("logout");
208}
209
210function url_account($user = false) {
211    if ($user === false) {
212        return url_complex("account");
213    } else {
214        return url_complex("account/$user");
215    }
216}
217
218function url_register() {
219    return url_complex("register", array(), true, IA_HTTPS_ENABLED);
220}
221
222function url_resetpass($username = false) {
223    if ($username === false) {
224        return url_complex('resetpass');
225    } else {
226        return url_complex('resetpass/'.$username);
227    }
228}
229
230function url_resetpass_confirm($username, $key) {
231    log_assert($key);
232    return url_complex('confirm/'.$username, array('c' => $key));
233}
234
235function url_user_profile($username) {
236    return url_complex(IA_USER_TEXTBLOCK_PREFIX . $username, array(), true);
237}
238
239function url_user_rating($username) {
240    return url_complex(IA_USER_TEXTBLOCK_PREFIX . $username, array(
241            'action' => 'rating'
242    ), true);
243}
244
245function url_user_stats($username) {
246    return url_complex(IA_USER_TEXTBLOCK_PREFIX . $username, array(
247            'action' => 'stats'
248    ));
249}
250
251/**
252 * Returns an url to an user's avatar with a given size
253 *
254 * @param  string  $username
255 * @param  string  $size_type
256 * @return string
257 */
258function url_user_avatar($username, $size_type = "full") {
259    return url_complex("avatar/".$size_type."/".$username);
260}
261
262function url_unsubscribe($username, $key) {
263    log_assert($key);
264    return url_complex('unsubscribe/'.$username, array('c' => $key));
265}
266
267// Task/round stuff.
268
269function url_task($task_id) {
270    log_assert(is_task_id($task_id));
271    return url_complex("problema/$task_id");
272}
273
274function url_task_edit($task_id, $action = 'edit') {
275    log_assert(is_task_id($task_id));
276    return url_complex("problema/$task_id", array('action' => $action));
277}
278
279function url_task_create() {
280    return url_complex("admin/problema-noua");
281}
282
283function url_task_delete() {
284    return url_complex("admin/sterge-problema");
285}
286
287function url_task_search($tag_ids) {
288    log_assert(is_array($tag_ids), "Tag ids must be an array");
289    return url_complex("cauta-probleme", array("tag_id" => $tag_ids));
290}
291
292function url_task_list($page_name, $filter = null) {
293    $args = $filter ? array('filtru' => $filter) : array();
294    return url_complex($page_name, $args);
295}
296
297function url_task_tags() {
298    return url_complex("admin/task-tags");
299}
300
301function url_task_tags_add() {
302    return url_complex("admin/task-tags", array("action" => "add"));
303}
304
305function url_task_tags_delete() {
306    return url_complex("admin/task-tags", array("action" => "delete"));
307}
308
309function url_task_tags_rename() {
310    return url_complex("admin/task-tags", array("action" => "rename"));
311}
312
313function url_round($round_id) {
314    log_assert(is_round_id($round_id));
315    $round = round_get($round_id);
316    return url_complex($round['page_name']);
317}
318
319function url_round_edit($round_id) {
320    log_assert(is_round_id($round_id));
321    $round = round_get($round_id);
322    return url_complex($round['page_name'], array('action' => 'edit'));
323}
324
325function url_round_edit_params($round_id) {
326    log_assert(is_round_id($round_id));
327    return url_complex("admin/runda/" . $round_id,
328                       array('action' => 'edit-params'));
329}
330
331function url_round_edit_task_order($round_id) {
332    log_assert(is_round_id($round_id));
333    return url_complex("admin/runda/" . $round_id,
334                       array('action' => 'edit-task-order'));
335}
336
337function url_round_create() {
338    return url_complex("admin/runda-noua");
339}
340
341function url_round_register($round_id) {
342    log_assert(is_round_id($round_id));
343    return url_complex("inregistrare-runda/$round_id");
344}
345
346function url_round_register_view($round_id) {
347    log_assert(is_round_id($round_id));
348    return url_complex("lista-inregistrare/$round_id");
349}
350
351function url_round_delete($round_id) {
352    log_assert(is_round_id($round_id));
353    return url_complex("admin/runda/$round_id", array('action' => 'sterge-runda'));
354}
355
356// Job/monitor stuff.
357
358function url_submit() {
359    return url_complex("submit", array());
360}
361
362function url_monitor($filters = null) {
363    if (is_null($filters)) {
364        $filters = array();
365    }
366    return url_complex("monitor", $filters);
367}
368
369function url_reeval($filters = null) {
370    if (is_null($filters)) {
371        $filters = array();
372    }
373    return url_complex("reeval", $filters);
374}
375
376function url_job_detail($job_id) {
377    log_assert(is_numeric($job_id));
378    return url_complex("job_detail/".$job_id, array());
379}
380
381function url_job_view_source($job_id) {
382    return url_complex("job_detail/".$job_id, array('action' => 'view-source'));
383}
384
385// Misc urls
386
387function url_home() {
388    return url_complex('', array());
389}
390
391function url_static($path) {
392    return url_complex("static/$path", array());
393}
394
395function url_changes() {
396    return url_complex("changes", array());
397}
398
399function url_changes_rss() {
400    return url_complex("changes", array('format' => 'rss'));
401}
402
403function url_forum() {
404    return IA_SMF_URL;
405}
406
407function url_userwidget($user_id) {
408    return url_complex("userwidget/".$user_id, array());
409}
410
411// Newsletter
412
413function url_newsletter($page_name, $rev = null) {
414    return url_textblock_revision($page_name, $rev);
415}
416
417function url_newsletter_preview_body($page_name, $rev = null) {
418    return url_complex($page_name, array('revision' => $rev,
419            'preview' => 'body'));
420}
421
422function url_admin() {
423    return url_textblock('admin');
424}
Note: See TracBrowser for help on using the repository browser.