source: trunk/plugins/InsertPicture/InsertPicture.php @ 437

Last change on this file since 437 was 419, checked in by mokhet, 14 years ago

more ; fixed (a few were missing, a lot were used with no reason) for ticket #385

File size: 9.1 KB
Line 
1<?PHP
2  //this plugin only use the relativ webpath to the picturefolder
3  //default ~  /htmlarea/plugins/InsertPicture/demo_pictures/
4  strstr( PHP_OS, "WIN") ? $strPathSeparator = "\\" : $strPathSeparator = "/";
5  if (isset($_REQUEST['picturepath'])) {
6    $getRequest = true;
7    $PicturePath = 'http://'.$_SERVER['HTTP_HOST'].$_REQUEST['picturepath'];
8    //$LocalPicturePath = $_REQUEST['localpicturepath'];
9
10    $AInsertPicturePath = explode ('/', 'http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']).'/demo_pictures/');
11    $ALocalInsertPicturePath = explode($strPathSeparator, dirname(__FILE__).$strPathSeparator.'demo_pictures');
12    $APicturePath = explode('/', 'http://'.$_SERVER['HTTP_HOST'].$_REQUEST['picturepath']);
13
14    $AtheFilePath = array_values (array_diff ($APicturePath, $AInsertPicturePath));
15    $theFilePath = implode($strPathSeparator, $AtheFilePath).$strPathSeparator;
16
17    $AtheRootPath = array_values (array_diff ($ALocalInsertPicturePath, $AInsertPicturePath));
18    $theRootPath = implode($strPathSeparator, $AtheRootPath);
19
20    $LocalPicturePath = $theRootPath.$strPathSeparator.$theFilePath.$strPathSeparator;
21  } else {
22    $getRequest = false;
23    $PicturePath =  'http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']).'/demo_pictures/';
24    $LocalPicturePath = dirname(__FILE__).$strPathSeparator.'demo_pictures';
25  }
26  $limitedext = array(".gif",".jpg",".png",".jpeg"); //Extensions you want files uploaded limited to.
27  $limitedsize = "1000000"; //size limit in bytes
28  $message = "";
29  function formatSize($size)
30  {
31    if($size < 1024)
32      return $size.' bytes';   
33    else if($size >= 1024 && $size < 1024*1024)
34      return sprintf('%01.2f',$size/1024.0).' Kb';     
35    else
36      return sprintf('%01.2f',$size/(1024.0*1024)).' Mb';       
37  }
38
39  if (isset($_FILES['file'])) {
40    $file = $_FILES['file'];
41    $ext = strrchr($file['name'],'.');
42    if (!in_array($ext,$limitedext)) {
43      $message = "The file you are uploading doesn't have the correct extension.";
44    } else if (file_exists($LocalPicturePath.'\\'.$file['name'])) {
45      $message = "The file you are uploading already exists.";
46    } else if ($file['size'] > $limitedsize) {
47      $message = "The file you are uploading is to big. The max Filesize is</span><span> ".formatSize($limitedsize).".";
48    } else {
49      copy($file['tmp_name'], $LocalPicturePath.$strPathSeparator.$file['name']);
50    }
51  }
52
53?>
54<html>
55
56<head>
57  <title>Insert Image</title>
58       
59<link rel="stylesheet" type="text/css" href="../../popups/popup.css" />
60<script type="text/javascript" src="../../popups/popup.js"></script>
61
62<script type="text/javascript">
63
64var HTMLArea = window.opener.HTMLArea;
65function i18n(str) {
66  return (HTMLArea._lc(str, 'HTMLArea'));
67}
68
69function Init() {
70  __dlg_translate("InsertPicture");
71  __dlg_init();
72  window.resizeTo(470, 490);
73  // Make sure the translated string appears in the drop down. (for gecko)
74  document.getElementById("f_align").selectedIndex = 1;
75  document.getElementById("f_align").selectedIndex = 5;
76  var param = window.dialogArguments;
77  if (param) {
78      document.getElementById("f_url").value = param["f_url"];
79      document.getElementById("f_alt").value = param["f_alt"];
80      document.getElementById("f_border").value = param["f_border"];
81      document.getElementById("f_align").value = param["f_align"];
82      document.getElementById("f_vert").value = param["f_vert"];
83      document.getElementById("f_horiz").value = param["f_horiz"];
84      window.ipreview.location.replace(param.f_url);
85  }
86  document.getElementById("f_url").focus();
87}
88
89function onOK() {
90  var required = {
91    "f_url": i18n("You must enter the URL")
92  };
93  for (var i in required) {
94    var el = document.getElementById(i);
95    if (!el.value) {
96      alert(required[i]);
97      el.focus();
98      return false;
99    }
100  }
101  // pass data back to the calling window
102  var fields = ["f_url", "f_alt", "f_align", "f_border",
103                "f_horiz", "f_vert"];
104  var param = new Object();
105  for (var i in fields) {
106    var id = fields[i];
107    var el = document.getElementById(id);
108    param[id] = el.value;
109  }
110  __dlg_close(param);
111  return false;
112}
113
114function onUpload() {
115  var required = {
116    "file": i18n("Please select a file to upload.")
117  };
118  for (var i in required) {
119    var el = document.getElementById(i);
120    if (!el.value) {
121      alert(required[i]);
122      el.focus();
123      return false;
124    }
125  }
126  submit();
127  return false;
128}
129
130function onCancel() {
131  __dlg_close(null);
132  return false;
133}
134
135function onPreview() {
136  var f_url = document.getElementById("f_url");
137  var url = f_url.value;
138  if (!url) {
139    alert(i18n("You must enter the URL"));
140    f_url.focus();
141    return false;
142  }
143  if (document.all) {
144    window.ipreview.location.replace('viewpicture.html?'+url);
145  } else {
146    window.ipreview.location.replace(url);
147  }
148  return false;
149}
150
151function CopyToURL(imgName) {
152  document.getElementById("f_url").value = imgName;
153  onPreview();
154}
155
156function openFile() {
157  window.open(document.getElementById("f_url").value,'','');
158}
159</script>
160
161</head>
162
163<body class="dialog" onload="Init()">
164
165<div class="title">Insert Image</div>
166<table border="0" width="100%" style="padding: 0px; margin: 0px">
167  <tbody>
168  <tr>
169    <td>Images on the Server:<br>
170    <select value="" style="width:200" size="10" onClick="CopyToURL(this[this.selectedIndex].value);">
171<?php
172  $d = @dir($LocalPicturePath);
173  while (false !== ($entry = $d->read())) { //not a dot file or directory
174    if(substr($entry,0,1) != '.') {
175      echo '<OPTION value="' . $PicturePath.$entry. '">' . $entry . '(' . formatSize(filesize($LocalPicturePath.'\\'.$entry)) .')</OPTION>';
176    }
177  }
178  $d->close();
179?>
180    </select>
181<?php
182  if ($getRequest == true) {
183    echo '<form method="post" action="'.$_SERVER['PHP_SELF'].'?picturepath='.$_REQUEST['picturepath'].'" enctype="multipart/form-data">';
184  } else {
185    echo '<form method="post" action="'.$_SERVER['PHP_SELF'].'" enctype="multipart/form-data">';
186  }
187?>
188        <input type="file" name="file" id="file" size="30"><br>
189        <button type="submit" name="ok" onclick="return onUpload();">Upload file</button><br>
190        <span><?php echo $message ?></span>
191      </form>
192
193    </td>
194    <td valign="center" width="200" height="230">
195    <span>Image Preview:</span>
196    <a href="#" onClick="javascript:openFile();"title=" Open file in new window"><img src="img/btn_open.gif"  width="18" height="18" border="0" title="Open file in new window" /></a><br />
197    <iframe name="ipreview" id="ipreview" frameborder="0" style="border : 1px solid gray;" height="200" width="200" src=""></iframe>
198    </td>
199  </tr>
200  </tbody>
201</table>
202
203<form action="" method="get">
204<table border="0" width="100%" style="padding: 0px; margin: 0px">
205  <tbody>
206
207  <tr>
208    <td style="width: 7em; text-align: right">Image URL:</td>
209    <td><input type="text" name="url" id="f_url" style="width:75%"
210      title="Enter the image URL here" />
211      <button name="preview" onclick="return onPreview();"
212      title="Preview the image in a new window">Preview</button>
213    </td>
214  </tr>
215  <tr>
216    <td style="width: 7em; text-align: right">Alternate text:</td>
217    <td><input type="text" name="alt" id="f_alt" style="width:100%"
218      title="For browsers that don't support images" /></td>
219  </tr>
220
221  </tbody>
222</table>
223
224<p />
225
226<fieldset style="float: left; margin-left: 5px;">
227<legend>Layout</legend>
228
229<div class="space"></div>
230
231<div class="fl">Alignment:</div>
232<select size="1" name="align" id="f_align"
233  title="Positioning of this image">
234  <option value=""                             >Not set</option>
235  <option value="left"                         >Left</option>
236  <option value="right"                        >Right</option>
237  <option value="texttop"                      >Texttop</option>
238  <option value="absmiddle"                    >Absmiddle</option>
239  <option value="baseline" selected="1"        >Baseline</option>
240  <option value="absbottom"                    >Absbottom</option>
241  <option value="bottom"                       >Bottom</option>
242  <option value="middle"                       >Middle</option>
243  <option value="top"                          >Top</option>
244</select>
245
246<p />
247
248<div class="fl">Border thickness:</div>
249<input type="text" name="border" id="f_border" size="5"
250title="Leave empty for no border" />
251
252<div class="space"></div>
253
254</fieldset>
255
256<fieldset style="float:right; margin-right: 5px;">
257<legend>Spacing</legend>
258
259<div class="space"></div>
260
261<div class="fr">Horizontal:</div>
262<input type="text" name="horiz" id="f_horiz" size="5"
263title="Horizontal padding" />
264
265<p />
266
267<div class="fr">Vertical:</div>
268<input type="text" name="vert" id="f_vert" size="5"
269title="Vertical padding" />
270
271<div class="space"></div>
272
273</fieldset>
274<br clear="all" />
275
276<div id="buttons">
277  <button type="submit" name="ok" onclick="return onOK();">OK</button>
278  <button type="button" name="cancel" onclick="return onCancel();">Cancel</button>
279</div>
280</form>
281</body>
282</html>
Note: See TracBrowser for help on using the repository browser.