Completed
Pull Request — gcconnex (#1585)
by
unknown
26:26 queued 10:13
created

start.php ➔ thewire_image_delete_attached_files()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 3
nop 3
dl 0
loc 16
rs 9.2
c 0
b 0
f 0
1
<?php
2
/**
3
 * The Wire Attachment.
4
 *
5
 * Attach files to the wire!
6
 */
7
8
elgg_register_event_handler('init', 'system', 'thewire_image');
9
10
/**
11
 * Inits the plugin
12
 *
13
 * @return void
14
 */
15 View Code Duplication
function thewire_image() {
16
	$plugin_root = dirname(__FILE__);
17
	elgg_register_library('thewire_image', "$plugin_root/lib/thewire_image.php");
18
	elgg_register_js('dropzone', 'mod/thewire_images/js/dropzone.js');
19
	elgg_register_css('dropzone', 'mod/thewire_images/css/dropzone.css');
20
	
21
	elgg_extend_view('js/elgg', 'js/thewire_image');
22
23
	elgg_register_event_handler('create', 'object', 'thewire_image_check_attachments');
24
	elgg_register_event_handler('delete', 'object', 'thewire_image_delete_attached_files');
25
26
	// overrule default save action
27
	elgg_unregister_action("thewire/add");
28
	elgg_register_action("thewire/add", "$plugin_root/actions/thewire/add.php");
29
30
	// downloads are served through pages instead of actions so the download link can be shared.
31
	// action tokens prevent sharing action links.
32
	// this means we need to implement our own security in the page handler using gatekeeper().
33
	elgg_register_page_handler('thewire_image', 'thewire_image_page_handler');
34
}
35
36
/**
37
 * Check for attachments when wire posts are created.
38
 *
39
 * @param type $event
40
 * @param type $type
41
 * @param type $object
42
 * @return type mixed
43
 */
44
function thewire_image_check_attachments($event, $type, $object) {
0 ignored issues
show
Coding Style introduced by
thewire_image_check_attachments uses the super-global variable $_FILES which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
45
	if (!elgg_instanceof($object, 'object', 'thewire')) {
46
		return null;
47
	}
48
49
	$file = elgg_extract('thewire_image_file', $_FILES, null);
50
51
	if ($file) {
52
		$file_obj = new TheWireImage();
53
54
		$file_obj->setFilename('thewire_image/' . rand().".jpg");
55
		$file_obj->setMimeType($file['type']);
56
		$file_obj->original_filename = $file['name'];
57
		$file_obj->simpletype = file_get_simple_type($file['type']);
58
		$file_obj->access_id = ACCESS_PUBLIC;
59
60
		$file_obj->open("write");
61
		$file_obj->write(get_uploaded_file('thewire_image_file'));
0 ignored issues
show
Security Bug introduced by
It seems like get_uploaded_file('thewire_image_file') targeting get_uploaded_file() can also be of type false; however, ElggFile::write() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
62
		$file_obj->close();
63
64
		if ($file_obj->save()) {
65
			$file_obj->addRelationship($object->getGUID(), 'is_attachment');
66
		} else {
67
			register_error(elgg_echo('thewire_image:could_not_save_image'));
68
		}
69
	}
70
71
	return null;
72
}
73
74
/**
75
 * The wire attachment page handler
76
 *
77
 * Supports:
78
 *	Download an attachment: thewire_image/download/<guid>/<title>
79
 *
80
 * @param array $page From the page_handler function
81
 * @return bool
82
 */
83
function thewire_image_page_handler($page) {
84
	gatekeeper();
85
	$pages = dirname(__FILE__) . '/pages/thewire_image';
86
	$section = elgg_extract(0, $page);
87
88
	switch($section) {
89
		case 'download':
90
			$guid = elgg_extract(1, $page);
91
			set_input('guid', $guid);
92
			require "$pages/download.php";
93
			break;
94
95
		default:
96
			// in the future we'll be able to register this as a 404
97
			// for now, act like an action and forward away.
98
			register_error(elgg_echo('thewire_image:invalid_section'));
99
			forward(REFERRER);
100
	}
101
}
102
103
/**
104
 * Deletes any attachments when wire posts are deleted.
105
 *
106
 * @param type $event
107
 * @param type $type
108
 * @param type $object
109
 * @return null
110
 */
111
function thewire_image_delete_attached_files($event, $type, $object) {
112
113
	if (!elgg_instanceof($object, 'object', 'thewire')) {
114
		return null;
115
	}
116
	
117
	// we want to use the thewire_image_get_attachments() function,
118
	// so load the library.
119
	elgg_load_library('thewire_image');
120
121
	$attachment = thewire_image_get_attachments($object->getGUID());
122
123
	if ($attachment && !$attachment->delete()) {
124
		register_error(elgg_echo('thewire_image:could_not_delete'));
125
	}
126
}