root/afridex/plugins/fresh-page/RCCWP_upload.php @ 22

Revision 21, 6.0 kB (checked in by admin, 18 years ago)
Line 
1<html><head>
2<?php
3require( dirname(__FILE__) . '/../../../wp-config.php' );
4if (!(is_user_logged_in() && current_user_can('edit_posts')))
5        die("Athentication failed!");
6
7
8$imagesExts = array('gif','jpg','png');
9$audiosExts = array('wav','mp3');
10
11$acceptedExtsString = "";
12
13function DownloadFile(){
14        global $acceptedExtsString, $imagesExts, $audiosExts;
15
16        $url = $_POST['upload_url'];
17
18        // Prepare accpeted extensions
19        $acceptedExts = array();
20
21        if ('1' == $_POST['type'])
22                $acceptedExts = $imagesExts;
23        elseif ('2' == $_POST['type']) 
24                $acceptedExts = $audiosExts;
25
26
27        //Retrieve file
28        if ($fp_source = @fopen($url, 'rb')) {
29                //Get target filename
30                $exploded_url = explode( '.', $url );
31
32                $ext = array_pop( $exploded_url );
33
34                // Check extension
35                if (false != $acceptedExts)
36                        if (false === array_search(strtolower($ext), $acceptedExts)){
37                                foreach($acceptedExts as $acceptedExt)
38                                        if ($acceptedExtsString == "")
39                                                $acceptedExtsString = $acceptedExt;
40                                        else
41                                                $acceptedExtsString = $acceptedExtsString." - ".$acceptedExt;
42                                return false;
43                        }
44               
45
46                $filename = time() . '_' . str_replace( 'rc_cwp_meta_', '', $_POST["input_name"]) . '.' . $ext;
47               
48                $directory = dirname(__FILE__) . '/files/';
49
50                $fp_dest = @fopen($directory . $filename,"wb");
51                if ($fp_dest == false) return false;
52
53                while(!feof($fp_source)) {
54                        set_time_limit(30);
55
56                        //if (connection_status()!=0) return false;
57
58                        $readData = fread($fp_source, 1024*2);
59                        //if ($readData == false) return false;
60                       
61                        fwrite($fp_dest,$readData);
62                       
63                }
64                fclose($fp_source) ;
65                fclose($fp_dest) ;
66                //chmod($directory . $filename, 0644);
67
68                return $filename;
69        }
70        return false;
71
72}
73
74
75if (isset($_POST['fileframe'])) 
76{
77        $operationSuccess = "false";
78        // A file is uploaded
79        if (isset($_FILES['file']) && (!empty($_FILES['file']['tmp_name'])))  // file was send from browser
80        {
81               
82                if ($_FILES['file']['error'] == UPLOAD_ERR_OK)  // no error
83                {
84                        $filename = time() . $_FILES['file']['name'];
85                        @move_uploaded_file( $_FILES['file']['tmp_name'], dirname(__FILE__) . '/files/' . $filename );
86                        @chmod(dirname(__FILE__) . '/files/' . $filename, 0644);
87
88                        $result_msg = '<p>The file '.$_FILES['file']['name'].' was uploaded successfuly. Please remember to click the submit button to save the post</p>';
89                        $operationSuccess = "true";
90                }
91                elseif ($_FILES['file']['error'] == UPLOAD_ERR_INI_SIZE)
92                        $result_msg = '<p>The uploaded file exceeds the maximum upload limit</p>';
93                else 
94                        $result_msg = '<p>The upload failed</p>';
95       
96        }
97        elseif (isset($_POST['upload_url']) && (!empty($_POST['upload_url'])))  // file was send from browser
98        {
99                if ( (substr($_POST['upload_url'],0,4) != "http") && (substr($_POST['upload_url'],0,3) != "ftp"))
100                        $_POST['upload_url'] = "http://".$_POST['upload_url'];
101
102                $filename = DownloadFile();
103               
104
105                if (false == $filename) {
106                        if ($acceptedExtsString != "") $infoStr = ". Make sure the file ends with: $acceptedExtsString";
107                        $result_msg = "Error downloading file: ".$_POST['upload_url'].$infoStr;
108                }
109                else{
110                        $result_msg = '<p>The URL '.$_POST['upload_url'].' was downloaded successfuly. Please remember to click the submit button to save the post</p>';
111                        $operationSuccess = "true";
112                }
113               
114        }       
115
116        // If operation is success, make sure the file was created properly
117        if ($operationSuccess == "true"){
118                if ($fp_check_file = @fopen(dirname(__FILE__) . '/files/' . $filename, 'rb')) {
119                        fclose($fp_check_file);
120                }
121                else{
122                        $operationSuccess = "false";
123                        $result_msg = "Failed to upload the file!";
124                }
125               
126        }
127       
128
129?>
130
131        <script language="javascript">
132
133                var par = window.parent.document;
134                var iframe = par.getElementById('upload_internal_iframe_<?=$_POST["input_name"]?>');
135                par.getElementById('upload_progress_<?=$_POST["input_name"]?>').innerHTML = '<?=$result_msg?>';
136
137                if (<?=$operationSuccess?>){
138                       
139                        iframe.style.visibility = "visible";
140                        iframe.style.height = "75px";
141                       
142                        par.getElementById('<?=$_POST["input_name"]?>').value = '<?=$filename?>';
143                        par.getElementById('<?=$_POST["input_name"]?>').onchange();
144                }
145                else{
146                        iframe.style.visibility = "visible";
147                        iframe.style.height = "75px";
148                }
149               
150        </script>
151
152
153<?php
154        //exit();
155}
156?>
157
158<script language="javascript">
159function upload(){
160        // hide old iframe
161        var par = window.parent.document;
162
163        var iframe = par.getElementById('upload_internal_iframe_<?=$_GET["input_name"]?>');
164        iframe.style.height = '0px';
165               
166        // update progress
167        par.getElementById('upload_progress_<?=$_GET["input_name"]?>').style.visibility = "visible";
168        par.getElementById('upload_progress_<?=$_GET["input_name"]?>').style.height = "auto";
169        par.getElementById('upload_progress_<?=$_GET["input_name"]?>').innerHTML = "Transferring ";
170
171        setTimeout("transferring(0)",1000) ;
172       
173        // send
174        document.iform.submit();
175        //iframe.style.visibility = 'hidden';
176        //par.getElementById('upload_progress').style.visibility = "hidden";
177}
178
179function transferring(dots){
180       
181        newString = "Transferring ";
182        for (var x=1; x<=dots; x++) {
183                newString = newString + ".";
184        }
185       
186        var par = window.parent.document;
187
188        // update progress
189        if (par.getElementById('upload_progress_<?=$_GET["input_name"]?>').innerHTML.substring(0,5) != "Trans") return;
190        par.getElementById('upload_progress_<?=$_GET["input_name"]?>').innerHTML = newString;
191        if (dots == 4) dots = 0; else dots = dots + 1;
192        setTimeout("transferring("+dots+")",1000) ;
193       
194}
195
196</script>
197<style>
198body {vertical-align:top;}
199</style>
200<link type="text/css" href="http://localhost/wp/wp-admin/wp-admin.css?version=2.3.3" rel="stylesheet">
201</head>
202<body>
203<form name="iform" action="" method="post" enctype="multipart/form-data">
204<input id="file" type="file" name="file"  />
205<input type="hidden" name="fileframe" value="true" /></br>
206<input id="upload_url"
207                        name="upload_url"
208                        type="text"
209                        size="40"
210                        /> :or URL
211<input type="button" onclick="upload()" value="Upload" />
212<input type="hidden" name="fileframe" value="true" />
213<input type="hidden" name="imgnum" />
214<input type="hidden" name="input_name" value="<?=$_GET["input_name"]?>" />
215<input type="hidden" name="type" value="<?=$_GET["type"]?>" />
216</form></body>
217</html>
Note: See TracBrowser for help on using the browser.