PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/public_html/lists/admin/template.php

https://github.com/samtuke/phplist
PHP | 319 lines | 283 code | 26 blank | 10 comment | 68 complexity | bba26effdb38042df25d9d2af1f23862 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. require_once dirname(__FILE__).'/accesscheck.php';
  3. $actionresult = '';
  4. if (!empty($_FILES['file_template']) && is_uploaded_file($_FILES['file_template']['tmp_name'])) {
  5. $content = file_get_contents($_FILES['file_template']['tmp_name']);
  6. } elseif (isset($_POST['template'])) {
  7. $content = $_POST['template'];
  8. } else {
  9. $content = '';
  10. }
  11. $sendtestresult = '';
  12. $testtarget = getConfig('admin_address');
  13. $systemTemplateID = getConfig('systemmessagetemplate');
  14. if (isset($_REQUEST['id'])) {
  15. $id = sprintf('%d',$_REQUEST['id']);
  16. } else {
  17. $id = 0;
  18. }
  19. function getTemplateImages($content) {
  20. $html_images = array();
  21. $image_types = array(
  22. 'gif' => 'image/gif',
  23. 'jpg' => 'image/jpeg',
  24. 'jpeg' => 'image/jpeg',
  25. 'jpe' => 'image/jpeg',
  26. 'bmp' => 'image/bmp',
  27. 'png' => 'image/png',
  28. 'tif' => 'image/tiff',
  29. 'tiff' => 'image/tiff',
  30. 'swf' => 'application/x-shockwave-flash'
  31. );
  32. // Build the list of image extensions
  33. while(list($key,) = each($image_types)) {
  34. $extensions[] = $key;
  35. }
  36. preg_match_all('/"([^"]+\.('.implode('|', $extensions).'))"/Ui', stripslashes($content), $images);
  37. while (list($key,$val) = each ($images[1])) {
  38. if (isset($html_images[$val])) {
  39. $html_images[$val]++;
  40. } else {
  41. $html_images[$val] = 1;
  42. }
  43. }
  44. return $html_images;
  45. }
  46. function getTemplateLinks($content) {
  47. preg_match_all('/href="([^"]+)"/Ui', stripslashes($content), $links);
  48. return $links[1];
  49. }
  50. $msg = '';
  51. $checkfullimages = !empty($_POST['checkfullimages']) ? 1 : 0;
  52. $checkimagesexist = !empty($_POST['checkimagesexist']) ? 1 : 0;
  53. $checkfulllinks = !empty($_POST['checkfulllinks']) ? 1 : 0;
  54. $baseurl = '';
  55. if (!empty($_POST['action']) && $_POST['action'] == "addimages") {
  56. if (!$id) {
  57. $msg = $GLOBALS['I18N']->get('No such template');
  58. } else {
  59. $content_req = Sql_Fetch_Row_Query("select template from {$tables["template"]} where id = $id");
  60. $images = getTemplateImages($content_req[0]);
  61. if (sizeof($images)) {
  62. include "class.image.inc";
  63. $image = new imageUpload();
  64. while (list($key,$val) = each ($images)) {
  65. # printf('Image name: <b>%s</b> (%d times used)<br />',$key,$val);
  66. $image->uploadImage($key,$id);
  67. }
  68. $msg = $GLOBALS['I18N']->get('Images stored');
  69. } else {
  70. $msg = $GLOBALS['I18N']->get('No images found');
  71. }
  72. }
  73. $_SESSION['action_result'] = $msg.'<br/>'.s('Template saved and ready for use in campaigns');
  74. Redirect('templates');
  75. return;
  76. //print '<p class="actionresult">'.$msg.'</p>';
  77. //$msg = '';
  78. } elseif (!empty($_POST['save']) || !empty($_POST['sendtest'])) { ## let's save when sending a test
  79. $templateok = 1;
  80. $title = removeXss($_POST['title']);
  81. if ($title && strpos($content,"[CONTENT]") !== false) {
  82. $images = getTemplateImages($content);
  83. // var_dump($images);
  84. if (($checkfullimages || $checkimagesexist) && sizeof($images)) {
  85. foreach ($images as $key => $val) {
  86. if (!preg_match("#^https?://#i",$key)) {
  87. if ($checkfullimages) {
  88. $actionresult .= $GLOBALS['I18N']->get('Image')." $key => ".$GLOBALS['I18N']->get('"not full URL')."<br/>\n";
  89. $templateok = 0;
  90. }
  91. } else {
  92. if ($checkimagesexist) {
  93. $imageFound = testUrl($key);
  94. if ($imageFound != 200) {
  95. $actionresult .= $GLOBALS['I18N']->get('Image')." $key => ".$GLOBALS['I18N']->get('does not exist')."<br/>\n";
  96. $templateok = 0;
  97. }
  98. }
  99. }
  100. }
  101. }
  102. if ($checkfulllinks) {
  103. $links = getTemplateLinks($content);
  104. foreach ($links as $key => $val) {
  105. if (!preg_match("#^https?://#i",$val) && !preg_match("#^mailto:#i",$val)) {
  106. $actionresult .= $GLOBALS['I18N']->get('Not a full URL').": $val<br/>\n";
  107. $templateok = 0;
  108. }
  109. }
  110. }
  111. } else {
  112. if (!$title) $actionresult .= $GLOBALS['I18N']->get('No Title')."<br/>";
  113. else $actionresult .= $GLOBALS['I18N']->get('Template does not contain the [CONTENT] placeholder')."<br/>";
  114. $templateok = 0;
  115. }
  116. if ($templateok) {
  117. if (!$id) {
  118. Sql_Query("insert into {$tables["template"]} (title) values(\"$title\")");
  119. $id = Sql_Insert_Id($tables['template'], 'id');
  120. }
  121. Sql_Query(sprintf('update %s set title = "%s",template = "%s" where id = %d',
  122. $tables["template"],$title,sql_escape($content),$id));
  123. Sql_Query(sprintf('select * from %s where filename = "%s" and template = %d',
  124. $tables["templateimage"],"powerphplist.png",$id));
  125. if (!Sql_Affected_Rows())
  126. Sql_Query(sprintf('insert into %s (template,mimetype,filename,data,width,height)
  127. values(%d,"%s","%s","%s",%d,%d)',
  128. $tables["templateimage"],$id,"image/png","powerphplist.png",
  129. $newpoweredimage,
  130. 70,30));
  131. $actionresult .= '<p class="information">'.s('Template saved').'</p>';
  132. if (sizeof($images)) {
  133. include dirname(__FILE__) . "/class.image.inc";
  134. $image = new imageUpload();
  135. print "<h3>".$GLOBALS['I18N']->get('Images').'</h3><p class="information">'.$GLOBALS['I18N']->get('Below is the list of images used in your template. If an image is currently unavailable, please upload it to the database.')."</p>";
  136. print '<p class="information">'.$GLOBALS['I18N']->get('This includes all images, also fully referenced ones, so you may choose not to upload some. If you upload images, they will be included in the campaigns that use this template.')."</p>";
  137. print formStart('enctype="multipart/form-data" class="template1" ');
  138. print '<input type="hidden" name="id" value="'.$id.'" />';
  139. ksort($images);
  140. reset($images);
  141. while (list($key,$val) = each ($images)) {
  142. $key = trim($key);
  143. if (preg_match('~^https?://~i',$key)) {
  144. $missingImage = true;
  145. $imageFound = testUrl($key);
  146. if ($imageFound != 200) {
  147. printf($GLOBALS['I18N']->get('Image name:').' <b>%s</b> ('.$GLOBALS['I18N']->get('%d times used').')<br/>',$key,$val);
  148. print $image->showInput($key,$val,$id);
  149. }
  150. } else {
  151. printf($GLOBALS['I18N']->get('Image name:').' <b>%s</b> ('.$GLOBALS['I18N']->get('%d times used').')<br/>',$key,$val);
  152. print $image->showInput($key,$val,$id);
  153. }
  154. }
  155. print '<input type="hidden" name="id" value="'.$id.'" /><input type="hidden" name="action" value="addimages" />
  156. <input class="submit" type="submit" name="addimages" value="'.$GLOBALS['I18N']->get('Save Images').'" /></form>';
  157. if (empty($_POST['sendtest'])) return;
  158. # return;
  159. } else {
  160. print '<p class="information">'.$GLOBALS['I18N']->get('Template does not contain local images')."</p>";
  161. if (empty($_POST['sendtest'])) return;
  162. # return;
  163. }
  164. } else {
  165. $actionresult .= $GLOBALS['I18N']->get('Some errors were found, template NOT saved!');
  166. $data["title"] = $title;
  167. $data["template"] = $content;
  168. }
  169. if (!empty($_POST['sendtest'])) {
  170. ## check if it's the system message template or a normal one:
  171. $targetEmails = explode(',',$_POST['testtarget']);
  172. $testtarget = '';
  173. $actionresult .= '<h3>'.$GLOBALS['I18N']->get('Sending test').'</h3>';
  174. if ($id == $systemTemplateID) {
  175. foreach ($targetEmails as $email) {
  176. if (validateEmail($email)) {
  177. $testtarget .= $email.', ';
  178. $actionresult .= $GLOBALS['I18N']->get('Sending test "Request for confirmation" to').' '.$email.' ';
  179. if (sendMail ($email,getConfig('subscribesubject'),getConfig('subscribemessage'))) {
  180. $actionresult .= s('OK');
  181. } else {
  182. $actionresult .= s('FAILED');
  183. }
  184. $actionresult .= '<br/>';
  185. $actionresult .= $GLOBALS['I18N']->get('Sending test "Welcome" to').' '.$email.' ';
  186. if (sendMail ($email,getConfig('confirmationsubject'),getConfig('confirmationmessage'))) {
  187. $actionresult .= s('OK');
  188. } else {
  189. $actionresult .= s('FAILED');
  190. }
  191. $actionresult .= '<br/>';
  192. $actionresult .= $GLOBALS['I18N']->get('Sending test "Unsubscribe confirmation" to').' '.$email.' ';
  193. if (sendMail ($email,getConfig('unsubscribesubject'),getConfig('unsubscribemessage'))) {
  194. $actionresult .= s('OK');
  195. } else {
  196. $actionresult .= s('FAILED');
  197. }
  198. } elseif (trim($email) != '') {
  199. $actionresult .= '<p>'.$GLOBALS['I18N']->get('Error sending test messages to').' '.htmlspecialchars($email).'</p>';
  200. }
  201. }
  202. } else {
  203. ## Sending test emails of non system templates to be added.
  204. }
  205. if (empty($testtarget)) {
  206. $testtarget = getConfig('admin_address');
  207. }
  208. $testtarget = preg_replace('/, $/','',$testtarget);
  209. }
  210. }
  211. if (!empty($actionresult)) {
  212. print '<div class="actionresult">'.$actionresult.'</div>';
  213. }
  214. if ($id) {
  215. $req = Sql_Query("select * from {$tables["template"]} where id = $id");
  216. $data = Sql_Fetch_Array($req);
  217. ## keep POSTED data, even if not saved
  218. if (!empty($_POST['template'])) {
  219. $data['template'] = $content;
  220. }
  221. } else {
  222. $data = array();
  223. $data["title"] = '';
  224. $data["template"] = '';
  225. }
  226. ?>
  227. <p class="information"><?php echo $msg?></p>
  228. <?php echo '<p class="button">'.PageLink2("templates",$GLOBALS['I18N']->get('List of Templates')).'</p>';?>
  229. <?php echo formStart(' enctype="multipart/form-data" class="template2" ')?>
  230. <input type="hidden" name="id" value="<?php echo $id?>" />
  231. <div class="panel">
  232. <table class="templateForm">
  233. <tr>
  234. <td><?php echo $GLOBALS['I18N']->get('Title of this template')?></td>
  235. <td><input type="text" name="title" value="<?php echo stripslashes(htmlspecialchars($data["title"]))?>" size="30" /></td>
  236. </tr>
  237. <tr>
  238. <td colspan="2"><?php echo s('Content of the template.')?><br /><?php echo s('The content should at least have <b>[CONTENT]</b> somewhere.')?><br/><?php echo s('You can upload a template file or paste the text in the box below'); ?></td>
  239. </tr>
  240. <tr>
  241. <td><?php echo s('Template file.')?></td>
  242. <td><input type="file" name="file_template" /></td>
  243. </tr>
  244. <tr>
  245. <td colspan="2">
  246. <?php
  247. if ($GLOBALS['editorplugin']) {
  248. print $GLOBALS['plugins'][$GLOBALS['editorplugin']]->editor('template', stripslashes($data["template"])) .'</div>';
  249. } else {
  250. print '<textarea name="template" id="template" cols="65" rows="20">';
  251. print stripslashes(htmlspecialchars($data["template"]));
  252. print '</textarea>';
  253. }
  254. ?>
  255. </td>
  256. </tr>
  257. <!--tr>
  258. <td>Make sure all images<br/>start with this URL (optional)</td>
  259. <td><input type="text" name="baseurl" size="40" value="<?php echo htmlspecialchars($baseurl)?>" /></td>
  260. </tr-->
  261. <tr>
  262. <td><?php echo $GLOBALS['I18N']->get('Check that all links have a full URL')?></td>
  263. <td><input type="checkbox" name="checkfulllinks" <?php echo $checkfulllinks?'checked="checked"':''?> /></td>
  264. </tr>
  265. <tr>
  266. <td><?php echo $GLOBALS['I18N']->get('Check that all images have a full URL')?></td>
  267. <td><input type="checkbox" name="checkfullimages" <?php echo $checkfullimages?'checked="checked"':''?> /></td>
  268. </tr>
  269. <?php if ($GLOBALS['can_fetchUrl']) { ?>
  270. <tr>
  271. <td><?php echo $GLOBALS['I18N']->get('Check that all external images exist')?></td>
  272. <td><input type="checkbox" name="checkimagesexist" <?php echo $checkimagesexist?'checked="checked"':''?> /></td>
  273. </tr>
  274. <?php } ?>
  275. <tr>
  276. <td colspan="2"><input class="submit" type="submit" name="save" value="<?php echo $GLOBALS['I18N']->get('Save Changes')?>" /></td>
  277. </tr>
  278. </table>
  279. </div>
  280. <?php $sendtest_content = sprintf('<div class="sendTest" id="sendTest">
  281. '.$sendtestresult .'
  282. <input class="submit" type="submit" name="sendtest" value="%s"/> %s:
  283. <input type="text" name="testtarget" size="40" value="'.htmlspecialchars($testtarget).'"/><br />%s
  284. </div>',
  285. $GLOBALS['I18N']->get('Send test message'),$GLOBALS['I18N']->get('to email addresses'),
  286. $GLOBALS['I18N']->get('(comma separate addresses - all must be existing subscribers)'));
  287. $testpanel = new UIPanel($GLOBALS['I18N']->get('Send Test'),$sendtest_content);
  288. $testpanel->setID('testpanel');
  289. if ($systemTemplateID == $id) { ## for now, testing only for system message templates
  290. print $testpanel->display();
  291. }
  292. ?>
  293. </form>