root/trunk/utils.php

Revision 11, 33.4 kB (checked in by skyblue, 2 years ago)

2006-08-23 Enver ALTIN <ealtin@…>

  • faxnotify.php: Implement automatic removal of temporary files. Add fax2ps to fax(tif)-to-multiple-jpg conversion command line. Implement sending partial success and success messages with pages as JPG images in inline HTML mail (fixes #1).
  • config.php: Declare PRG_FAX2PS.
  • tmpl/faxnotify_success.tpl.html, tmpl/faxnotify_partialsuccess.tpl.html: Add templates for success and partial success messages.
  • utils.php, index.php, toolbtns.php: More WebFax substitutions.
Line 
1<?php
2        /*
3         *  WebFax - The Web frontend for HylaFAX.
4         *
5         *  (C) 2006  Enver ALTIN  <ealtin@parkyeri.com>
6         *  (C) 2003  FrontSITE A.S. [http://www.frontsite.com.tr]
7         *
8         *  This program is free software; you can redistribute it and/or modify
9         *  it under the terms of the GNU General Public License as published by
10         *  the Free Software Foundation; either version 2 of the License, or
11         *  (at your option) any later version.
12         *
13         *  This program is distributed in the hope that it will be useful,
14         *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15         *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16         *  GNU Library General Public License for more details.
17         *
18         *  You should have received a copy of the GNU General Public License
19         *  along with this program; if not, write to the Free Software
20         *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21         */
22
23        require_once("toolbtns.php");
24        require_once("calendar.php");
25
26        function now() {
27                return date("r");
28        }
29
30        function formattimestamp($stamp) {
31                return date("H:i - d.m.Y", strtotime(substr($stamp, 1, 15)));
32        }
33
34        function formattimestamptotimeonly($stamp) {
35                return date("H:i", strtotime(substr($stamp, 1, 15)));
36        }
37
38        function checkperm($perm1, $perm2) {
39                return ($perm1 & $perm2)==$perm2;
40        }
41
42        function userperm($perm=PERM_NONE) {
43                if (session_is_registered("userid")) {
44                        global $userinfo;
45                        getuserinfo();
46                        return checkperm($userinfo->permission, $perm);
47                } else return FALSE;
48        }
49
50        function getuserinfo() {
51                global $db, $userinfo, $_SESSION;
52                if (!isset($userinfo) && session_is_registered("userid")) // Avoiding duplicate SQL queries so calling the function twice is pretty safe.
53                        $userinfo=db_getuserinfo($db, $_SESSION["userid"]);
54        }
55
56        function _get($str) {
57                return (isset($_GET[$str])?$_GET[$str]:"");
58        }
59
60        function _post($str) {
61                return (isset($_POST[$str])?$_POST[$str]:"");
62        }
63
64        function toolbtn($btnname, $active=FALSE, $params=FALSE) {
65                global $buttons;
66                $tb=$buttons[$btnname];
67                $classsuffix=($active?"active":"");
68                if ($tb[1]=="") $classsuffix.="icon";
69                $imgsuffix=($tb[1]==""?"icon":"");
70                echo '<td class=toolbtn><a class=toolbtn'.$classsuffix.' href="'.sprintf($tb[3], $params).'" title="'.$tb[2].'"><img class=toolbtn'.$imgsuffix.' src="img/icons/'.$tb[0].'.png">'.$tb[1].'</a></td>';
71        }
72
73        function gen_code($size=20) {
74                $r="";
75                for ($i=0; $i<$size; $i++) $r.=chr(rand(65, 90));
76                return $r;
77        }
78
79        function getfile_httpheaders($filename, $filesize) {
80                // application/octet-stream
81                // application/pdf
82                // image/jpeg
83                header("Content-Type: application/pdf");
84                // inline or attachment
85                header("Content-Disposition: attachment; filename=\"$filename\"");
86                header("Content-Length: $filesize");
87                # header("Cache-Control: no-cache, must-revalidate");
88                # header("Pragma: no-cache");
89                header("Cache-Control: private");
90        }
91
92        function convertjobtojpegandgettmpfileidlist($db, $jobid) {
93                $jf=db_getfaxjobfiles($db, $jobid);
94                while ($f=$jf->fetchRow()) {
95                        if (!is_dir(FAX_TMPCONVQ."/file".$f->id))
96                                mkdir(FAX_TMPCONVQ."/file".$f->id);
97                        system(PRG_GS." -r".FAX_PREVIEW_DPI." -sDEVICE=jpeg -sOutputFile=".FAX_TMPCONVQ."/file".$f->id."/page%04d.jpg -dNOPAUSE -dBATCH -q ".$f->filename." 1>/dev/null 2>&1");
98                        $dir=opendir(FAX_TMPCONVQ."/file".$f->id);
99                        while (FALSE!==($file=readdir($dir)))
100                                if ($file!="." && $file!="..")
101                                        $r[]=db_addtempconvfile($db, FAX_TMPCONVQ."/file".$f->id."/$file");
102                }
103                if (isset($r)) return $r;
104        }
105
106        function ps2pdfconvertandreadfile($filename) {
107                system(PRG_PS2PDF." $filename $filename.faxtemp");
108                readfile("$filename.faxtemp");
109                unlink("$filename.faxtemp");
110        }
111
112        function addblockheader($label) {
113                echo "<tr><td class=blhead>$label</td><td></td></tr>";
114        }
115
116        function addseperator() {
117                ?><tr><td colspan=2 class=sep></td></tr><?
118        }
119
120        function addinput($type, $name, $value="", $label="", $size=0, $max=0, $class="", $rows=8) {
121                switch ($type) {
122                        case 'hidden':
123                                echo "<input type=$type name=$name value=\"$value\">";
124                                break;
125                        case 'radio':
126                                echo "<tr><td class=l>$label</td><td class=i>";
127                                foreach($value as $x)
128                                        echo "<label><input class=\"$class\" type=$type name=$name value=\"".$x[0]."\" ".($x[2]?'checked':'').">".$x[1]."</label>";
129                                echo "</td></tr>";
130                                break;
131                        case 'label':
132                                echo "<tr><td class=l>$label</td><td class=i>$value</td>";
133                                if ($name!="") echo "<input type=hidden name=$name value=\"$value\">";
134                                break;
135                        case 'memo':
136                                echo "<tr><td class=l>$label</td><td class=i><textarea name=$name class=$class cols=$size rows=$rows maxlength=$max>$value</textarea></td></tr>";
137                                break;
138                        case 'check':
139                                echo "<tr><td class=l>$label</td><td class=i>";
140                                foreach($name as $i)
141                                        echo "<label><input type=checkbox value=".$i[2]." name=".$i[1].($i[3]?" checked":"").">".$i[0]."</label><br>";
142                                echo "</td></tr>";
143                                break;
144                        case 'select':
145                                echo "<tr><td class=l>$label</td><td class=i><select class=\"$class\" name=$name>";
146                                foreach($value as $i)
147                                        echo "<option value=".$i[0].($i[2]?" selected":"").">".$i[1];
148                                echo "</select></td></tr>";
149                                break;
150                        case 'file':
151                                echo "<tr><td class=l>$label</td><td class=i><input class=$class type=file accept=\"ps,pdf\" name=$name size=$size></td></tr>";
152                                break;
153                        case 'link':
154                                echo "<tr><td class=l>$label</td><td class=i><a href=\"$value\">$name</a></td></tr>";
155                                break;
156                        default:
157                                if (!is_array($name)) {
158                                        echo "<tr><td class=l>$label</td><td class=i><input class=$class type=$type name=$name size=$size maxlength=$max".($value!=""?" value=\"$value\"":"")."></td></tr>";
159                                } else {
160                                        echo "<tr><td class=l>$label</td><td class=i>";
161                                        for($i=0;$i<count($name);$i++)
162                                                echo "<input class=$class type=$type name=".$name[$i]." value=\"".$value[$i]."\" size=".$size[$i]." maxlength=".$max[$i].">";
163                                        echo "</td></tr>";
164                                }
165                }
166        }
167
168        function loginform($message="", $user="") {
169                ?>
170                        <h2>Kullanıcı girişi</h2>
171                        <table class=login cellspacing=1 cellpadding=0>
172                                <form name="loginform" action="?p=lc" method=post>
173                                        <tr class=toolbar>
174                                                <!--td class=toolbtn><label><input type=image class=img value="Gönder" src="img/icons/send.png">Gönder</label></td-->
175                                                <td colspan=2 class=toolbar><img class=toolbtnicon src="img/icons/send.png" style="margin-right: 5px"><input type=submit class=btn2 value="Gönder"><input type=submit class=btn2 name=forgot value="Unuttum"></td>
176                                        </tr>
177                                        <? if ($message!="") { ?><tr><td class=loginmsg colspan=2><?=$message?></td></tr><? } ?>
178                                        <tr><td class=loginl><b>Kullanıcı</b></td><td class=logini><input type=text class=text name=user size=30 maxlength=50 value="<?=$user?>"></td></tr>
179                                        <tr><td class=loginl>Parola</td><td class=logini><input type=password class=text name=pass size=20 maxlength=50></td></tr>
180                                </form>
181                        </table>
182                        <p class=w>WebFax Fax sunucu arabirimini kullanabilmek için kullanıcı girişi yapmalısınız.
183                                Eğer sisteme girebilmek için bir kullanıcı hesabına sahip değilseniz, <a href="mailto:<?=ADMIN_EMAIL?>">sistem yöneticinize</a> başvurun.</p>
184                <?
185        }
186
187        function faxeditor($db=FALSE, $faxid=-1, $defaultrecipients="") {
188                ?>
189                        <h2>Fax <?=($faxid==-1?"gönder":"düzenle")?></h2>
190                        <table class=genericform cellspacing=0 cellpadding=0>
191                                <form name=sendfax enctype="multipart/form-data" action="?p=fax&amp;m=post" method=post>
192                                        <tr class=toolbar>
193                                                <td class=toolbtn><label><input type=image class=img value="Gönder" src="img/icons/send.png">Gönder</label></td>
194                                                <td class=toolbtn>
195                                                        <!--label><input type=image class=img name=addfile value=1 src="img/icons/filenew.png">Dosya ekle</label-->
196                                                        <input type=submit name=addfile value="Dosya ekle">
197                                                </td>
198                                        </tr>
199                                        <?
200                                                if ($faxid!=-1)
201                                                        $job=db_getfaxjob($db, $faxid);
202                                                addseperator();
203                                                addblockheader("Fax Bilgileri");
204                                                addinput("text", "to", $faxid!=-1?$job->fto_phone:$defaultrecipients, "Fax No", 30, 500, "text");
205                                                addinput("text", "desc", $faxid!=-1?$job->fdesc:"", "Açıklama", 30, 100, "text");
206                                                if ($faxid!=-1) {
207                                                        $f=db_getfaxjobfiles($db, $faxid);
208                                                        while ($r=$f->fetchRow())
209                                                                addinput("link", $r->filename, "?p=fax&amp;m=preview&amp;id=$faxid", "Dosya");
210                                                }
211                                                addinput("file", "att", "", "Dosya ekle", 20, 0, "text");
212                                                addinput("hidden", "faxid", $faxid);
213                                                // Hylafax sendfax can handle very interesting file types so removing.
214                                                // addinput("select", "ftype", array(array("ps", "Postscript", TRUE), array("pdf", "Adobe PDF", FALSE)), "Dosya Türü", 20, 0, "");
215                                        ?>
216                                </form>
217                        </table>
218                        <p class=w><b>İpucu:</b> Bir dosya ekledikten sonra, <b>&quot;Gönder&quot;</b> butonu yerine, <b>&quot;Dosya ekle&quot;</b> butonunu kullanırsanız, fax kaydedildikten sonra yeni bir dosya ekleyebileceksiniz.</p>
219                <?
220        }
221
222        function displayapprovequeue($db) {
223                global $userinfo;
224                ?>
225                        <h2>Onay bekleyen iş listesi</h2>
226                        <form name="approve" method=post action="?p=fax&amp;m=approve">
227                                <table class=queue cellspacing=1 cellpadding=0>
228                                        <tr class=toolbar>
229                                                <th class=head style="width: 25px"><input type=checkbox onclick=""></th>
230                                                <th class=head>ID</th>
231                                                <th class=head style="width: 150px">Gönderen</th>
232                                                <th class=head style="width: 150px">Alıcılar</th>
233                                                <th class=head style="width: 200px">Açıklama</th>
234                                                <th class=head style="width: 130px">Tarih</th>
235                                                <th class=head style="width: 200px">Onaylar</th>
236                                        </tr>
237                                        <?
238                                                $i="hi";
239                                                if ($list=db_listsendqueue($db, $userinfo->department, $userinfo->superapprove))
240                                                        while ($r=$list->fetchRow())
241                                                                if (!db_checkapproval($db, $r->id, $userinfo->id)) {
242                                                                ?>
243                                                                        <tr class=<?=$i?>>
244                                                                                <td class=col style="text-align: center"><input type=checkbox name="faxes[]" value="<?=$r->id?>"></td>
245                                                                                <td class=col style="text-align: right"><a href="?p=fax&amp;m=edit&amp;id=<?=$r->id?>"><?=$r->id?></a></td>
246                                                                                <td class=col><a href="?p=users&amp;m=edit&amp;id=<?=$r->ffrom?>" title="<?=$r->deptname?>"><?=$r->name?></a></td>
247                                                                                <td class=col>
248                                                                                        <?
249                                                                                                $s=explode(":", $r->fto_phone);
250                                                                                                foreach($s as $recipient)
251                                                                                                        if ($pbentry=db_getphonebookentrybyfaxno($db, $recipient)) {
252                                                                                                                ?><a href="?p=phonebook&amp;m=edit&amp;id=<?=$pbentry->id?>" title="<?=$pbentry->faxno?>"><?=$pbentry->name?></a>&nbsp;<?
253                                                                                                        } else {
254                                                                                                                ?><a href="?p=fax&amp;m=edit&amp;id=<?=$r->id?>"><?=$r->fto_phone?></a><?
255                                                                                                        }
256                                                                                        ?>
257                                                                                </td>
258                                                                                <td class=col><a href="?p=fax&amp;m=preview&amp;id=<?=$r->id?>" title="Önizleme için tıklayınız."><?=$r->fdesc?></a></td>
259                                                                                <td class=col><a href="?p=fax&amp;m=edit&amp;id=<?=$r->id?>"><?=formattimestamp($r->queued)?></a></td>
260                                                                                <td class=col>
261                                                                                        <?
262                                                                                                $al=db_getjobapprovallist($db, $r->id);
263                                                                                                while ($x=$al->fetchRow())
264                                                                                                        echo '<a href="?p=users&amp;m=edit&amp;id='.$x->approvedby.'" title="'.$x->deptname.'">'.$x->name.'</a>&nbsp;';
265                                                                                        ?>
266                                                                                </td>
267                                                                        </tr>
268                                                                <?
269                                                                $i=($i=="lo"?"hi":"lo");
270                                                        }
271                                        ?>
272                                        <tr class=toolbar>
273                                                <td class=col style="text-align: center"><input type=checkbox onclick=""></td>
274                                                <td class=col colspan=6>
275                                                        <? if (userperm(PERM_APPR)) { ?><input type=submit name=approve value="Onayla"><? } ?>
276                                                        <? if ($userinfo->superapprove=="t") { ?>
277                                                                <input type=submit name=approvesingle value="Tek onay">
278                                                                <input type=submit name=delete value="Sil">
279                                                        <? }
280                                                           if ($userinfo->approverequired=="t" || userperm(PERM_APPR)) { ?>
281                                                                <label style="margin-left: 20px; margin-right: 5px">Red nedeni<input type=text class=text style="margin-left: 5px" name=rejectreason size=30 maxlength=200></label>
282                                                                <input type=submit name=reject value="Reddet">
283                                                        <? } ?>
284                                                </td>
285                                        </tr>
286                                </table>
287                        </form>
288                <?
289        }
290
291        function displaysentarchive($db) {
292                global $userinfo;
293                ?>
294                        <h2>Gönderilmiş/Reddedilmiş Fax Arşivi</h2>
295                        <table cellspacing=0 cellpadding=0><tr><td style="vertical-align: top; padding-right: 5px"><? $caldate=calendar("?p=fax&amp;m=archive", _get("m")); ?></td><td style="vertical-align: top">
296                        <form name="approve" method=post action="?p=fax&amp;m=approve">
297                                <table class=queue cellspacing=1 cellpadding=0>
298                                        <tr class=toolbar>
299                                                <th class=head>ID</th>
300                                                <th class=head style="width: 150px">Gönderen</th>
301                                                <th class=head style="width: 150px">Alıcılar</th>
302                                                <th class=head style="width: 200px">Açıklama</th>
303                                                <th class=head style="width: 50px">Saat</th>
304                                                <th class=head style="width: 200px">Onaylar</th>
305                                        </tr>
306                                        <?
307                                                $i="hi";
308                                                if ($list=db_listsentqueue($db, $userinfo->department, $userinfo->superapprove, $caldate))
309                                                        while ($r=$list->fetchRow()) {
310                                                                ?>
311                                                                        <tr class=<?=($r->rejected?"rej":$i)?>>
312                                                                                <td class=col style="text-align: right"><?=$r->id?></td>
313                                                                                <td class=col><a class="<?=($r->rejected?"rej":"")?>" href="?p=users&amp;m=edit&amp;id=<?=$r->ffrom?>" title="<?=$r->deptname?>"><?=$r->name?></a></td>
314                                                                                <td class=col>
315                                                                                        <?
316                                                                                                $s=explode(":", $r->fto_phone);
317                                                                                                foreach($s as $recipient)
318                                                                                                        if ($pbentry=db_getphonebookentrybyfaxno($db, $recipient)) {
319                                                                                                                ?><a class="<?=($r->rejected?"rej":"")?>" href="?p=phonebook&amp;m=edit&amp;id=<?=$pbentry->id?>" title="<?=$pbentry->faxno?>"><?=$pbentry->name?></a>&nbsp;<?
320                                                                                                        } else {
321                                                                                                                ?><a class="<?=($r->rejected?"rej":"")?>" href="?p=fax&amp;m=edit&amp;id=<?=$r->id?>"><?=$r->fto_phone?></a>&nbsp;<?
322                                                                                                        }
323                                                                                        ?>
324                                                                                </td>
325                                                                                <td class=col><a class="<?=($r->rejected?"rej":"")?>" href="?p=fax&amp;m=preview&amp;id=<?=$r->id?>" title="Önizleme için tıklayınız."><?=$r->fdesc?></a></td>
326                                                                                <td class=col><a class="<?=($r->rejected?"rej":"")?>" href="?p=fax&amp;m=edit&amp;id=<?=$r->id?>"><?=formattimestamptotimeonly($r->queued)?></a></td>
327                                                                                <td class=col>
328                                                                                        <?
329                                                                                                $al=db_getjobapprovallist($db, $r->id);
330                                                                                                while ($x=$al->fetchRow())
331                                                                                                        echo '<a class="'.($r->rejected?"rej":"").'" href="?p=users&amp;m=edit&amp;id='.$x->approvedby.'" title="'.$x->deptname.'">'.$x->name.'</a>&nbsp;';
332                                                                                                if ($r->rejected)
333                                                                                                        if ($rejuser=db_getuserinfo($db, $r->rejectedby))
334                                                                                                                echo '<a class=rej href="?p=users&amp;m=edit&amp;id='.$r->rejectedby.'" title="Red nedeni: '.$r->rejectreason.'"><b>'.$rejuser->name.' (Red)</b></a>';
335                                                                                        ?>
336                                                                                </td>
337                                                                        </tr>
338                                                                <?
339                                                                $i=($i=="lo"?"hi":"lo");
340                                                        }
341                                        ?>
342                                </table>
343                        </form></td></tr></table>
344                <?
345        }
346
347        function displaymyfaxes($db, $userid) {
348                ?>
349                        <h2>Faxlarım</h2>
350                        <table cellspacing=0 cellpadding=0><tr><td style="vertical-align: top; padding-right: 5px"><? $caldate=calendar("?p=fax&amp;m=my", _get("m")); ?></td><td style="vertical-align: top">
351                        <table class=queue cellspacing=1 cellpadding=0>
352                                <tr class=toolbar>
353                                        <th class=head>ID</th>
354                                        <th class=head style="width: 150px">Alıcılar</th>
355                                        <th class=head style="width: 200px">Açıklama</th>
356                                        <th class=head style="width: 50px">Saat</th>
357                                        <th class=head style="width: 150px">Durum</th>
358                                        <th class=head style="width: 200px">Onaylar</th>
359                                </tr>
360                                        <?
361                                                $i="hi";
362                                                if ($list=db_getmyfaxes($db, $userid, $caldate))
363                                                        while ($r=$list->fetchRow()) {
364                                                                ?>
365                                                                        <tr class=<?=($r->rejected?"rej":$i)?>>
366                                                                                <td class=col style="text-align: right"><?=$r->id?></td>
367                                                                                <td class=col>
368                                                                                        <?
369                                                                                                $s=explode(":", $r->fto_phone);
370                                                                                                foreach($s as $recipient)
371                                                                                                        if ($pbentry=db_getphonebookentrybyfaxno($db, $recipient)) {
372                                                                                                                ?><a class="<?=($r->rejected?"rej":"")?>" href="?p=phonebook&amp;m=edit&amp;id=<?=$pbentry->id?>" title="<?=$pbentry->faxno?>"><?=$pbentry->name?></a>&nbsp;<?
373                                                                                                        } else {
374                                                                                                                ?><a class="<?=($r->rejected?"rej":"")?>" href="?p=fax&amp;m=edit&amp;id=<?=$r->id?>"><?=$r->fto_phone?></a>&nbsp;<?
375                                                                                                        }
376                                                                                        ?>
377                                                                                </td>
378                                                                                <td class=col><a class="<?=($r->rejected?"rej":"")?>" href="?p=fax&amp;m=preview&amp;id=<?=$r->id?>" title="Önizleme için tıklayınız."><?=$r->fdesc?></a></td>
379                                                                                <td class=col><a class="<?=($r->rejected?"rej":"")?>" href="?p=fax&amp;m=edit&amp;id=<?=$r->id?>"><?=formattimestamptotimeonly($r->sent?$r->sentat:$r->queued)?></a></td>
380                                                                                <td class=col><?=($r->sent?"Gönderildi":($r->rejected?"Reddedildi":"Onay bekliyor"))?></td>
381                                                                                <td class=col>
382                                                                                        <?
383                                                                                                $al=db_getjobapprovallist($db, $r->id);
384                                                                                                while ($x=$al->fetchRow())
385                                                                                                        echo '<a class="'.($r->rejected?"rej":"").'" href="?p=users&amp;m=edit&amp;id='.$x->approvedby.'" title="'.$x->deptname.'">'.$x->name.'</a>&nbsp;';
386                                                                                                if ($r->rejected)
387                                                                                                        if ($rejuser=db_getuserinfo($db, $r->rejectedby))
388                                                                                                                echo '<b><a class=rej href="?p=users&amp;m=edit&amp;id='.$r->rejectedby.'">'.$rejuser->name.' ('.$r->rejectreason.')</a></b>';
389                                                                                        ?>
390                                                                                </td>
391                                                                        </tr>
392                                                                <?
393                                                                $i=($i=="lo"?"hi":"lo");
394                                                        }
395                                        ?>
396                        </table></td></tr></table>
397                <?
398        }
399
400        function displaydepartmentlist($db) {
401                ?>
402                        <h2>Departman Listesi</h2>
403                        <form name="departments" method=post action="?p=departments&amp;m=groupdel">
404                                <table class=queue cellspacing=1 cellpadding=0>
405                                        <tr class=toolbar>
406                                                <td colspan=4 class=toolbar>
407                                                        <table cellspacing=1 cellpadding=0 class=toolbar>
408                                                                <tr>
409                                              Â